【Verilog基础】11.部分模块代码

移位操作符(放大19倍)

module amp19( 
     clk, din, dout ); 
     input clk; 
     input [7:0] din; 
     output 11:0] dout; 
     reg [11:0] dint16; 
     reg [11:0] dint2; 
     reg [11:0] dint; 
     //将放大倍数 19 分解为 16+2+1
always @(posedge clk) begin 
     dint16 <= din << 4; 
     dint2<= din << 1; 
     dint<= din; 
     end 
     //将 2 的各次幂值加起来
assign dout = dint16 + dint2 + dint;
endmodule

for 语句描述的七人投票表决器

module voter7(pass,vote);
    output pass;
    input[6:0] vote;
    reg[2:0] sum;
    integer i;
    reg pass;
always @(vote)
    begin
        sum=0;
    for(i=0;i<=6;i=i+1) //for 语句
        if(vote[i]) sum=sum+1;
            if(sum[2]) pass=1; //若超过 4 人赞成,则 pass=1
        else pass=0;
    end
endmodule

parameter 参数化调用

module param_counter( 
     clk_in, reset, cnt_out ); 
     input clk_in; 
     input reset; 
     output [15:0] cnt_out; 
 // 参数化调用,利用#符号将计数器的模值 10 传入被调用模块
    cnt #(10) inst_cnt( 
     .clk_in(clk_in), 
     .reset(reset), 
     .cnt_out(cnt_out) 
     ); 
endmodule
module cnt(
    clk_in, reset, cnt_out 
     ); 
 //定义参数化变量
     parameter [15:0]Cmax = 1024; 
     input clk_in; 
     input reset; 
     output [15:0] cnt_out; 
     reg [15:0] cnt_out; 
 // 完成模值可控的计数器
 always @(posedge clk_in) begin 
     if(!reset) 
         cnt_out <= 0; 
     else 
         if(cnt_out == Cmax) 
         cnt_out <= 0; 
     else 
         cnt_out <= cnt_out + 1; 
    end 
endmodule

通过 defparam 参数指定例化模块的内部参数

module param_counter( 
     clk_in, reset, cnt_out ); 
     input clk_in; 
     input reset; 
     output [15:0] cnt_out; 
 // 调用计数器子模块
     cnt inst_cnt( 
     .clk_in(clk_in), 
     .reset(reset), 
     .cnt_out(cnt_out) 
    ); 
// 通过 defparam 参数指定例化模块的内部参数
defparam inst_cnt.Cmax = 12; 
endmodule

task 任务调用

module alutask(code,a,b,c);
    input[1:0] code;
    input[3:0] a,b;
    output[4:0] c;
    reg[4:0] c;

task my_and; //任务定义,注意无端口列表
    input[3:0] a,b; //a,b,out 名称的作用域范围为 task 任务内部
    output[4:0] out;
    integer i;
        begin
            for(i=3;i>=0;i=i-1)
            out[i]=a[i]&b[i]; //按位与
        end
endtask
always@(code or a or b)
    begin
    case(code)
        2'b00: my_and(a,b,c);
        /*调用任务 my_and,需注意端口列表的顺序应与任务定义中的一致,这里的
        a,b,c
        分别对应任务定义中的 a,b,out */
        2'b01: c=a|b; //或
        2'b10: c=a-b; //相减
        2'b11: c=a+b; //相加
    endcase
    end
endmodule

阶乘运算

module funct(clk,n,result,reset);
    output[31:0] result;
    input[3:0] n;
    input reset,clk;
    reg[31:0] result;

always @(posedge clk) //在 clk 的上升沿时执行运算
    begin
    if(!reset) result<=0; //复位
    else begin
        result <= 2 * factorial(n); //调用 factorial 函数
    end
end

function[31:0] factorial; //阶乘运算函数定义(注意无端口列表)
    input[3:0] opa; //函数只能定义输入端,输出端口为函数名本身
    reg[3:0] i;
    begin
    factorial = opa ? 1 : 0;
    for(i= 2; i <= opa; i = i+1) //该句若要综合通过,opa 应赋具体的数值
    factorial = i* factorial; //阶乘运算
    end
    endfunction
endmodule

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值