首页 > 技术文章 > verilog-module练习

czShare 2021-07-29 22:42 原文

可以做为参考---->>     https://www.zhihu.com/column/c_1131528588117385216

  • 可以在一个module内部,实例化instantiating另一个module,只要这另一个module和本module在同一个project即可

    the compiler knows where to find the module

  • 端口信号传递,by position,by name

 

  • Module shift

module top_module ( input clk, input d, output q );
//内部是三个相同的module,内部是输入传到输出,这个昨弄??
    wire q1,q2,q3;  //缓存一下中间的信号
    my_dff dff1(.clk(clk),.d(d),.q(q1));
    my_dff dff2(.clk(clk),.d(q1),.q(q2));
    my_dff dff3(.clk(clk),.d(q2),.q(q3));
    assign q=q3;
    //吼吼吼,成功了
endmodule

 

  • 用 generate 语句进行多个模块的重复例化
module full_adder4(
    input [3:0]   a ,   //adder1
    input [3:0]   b ,   //adder2
    input         c ,   //input carry bit
 
    output [3:0]  so ,  //adding result
    output        co    //output carry bit
    );
 
    wire [3:0]    co_temp ;
    //第一个例化模块一般格式有所差异,需要单独例化
    full_adder1  u_adder0(
        .Ai     (a[0]),
        .Bi     (b[0]),
        .Ci     (c==1'b1 ? 1'b1 : 1'b0),
        .So     (so[0]),
        .Co     (co_temp[0]));
 
    genvar        i ;
    generate
        for(i=1; i<=3; i=i+1) begin: adder_gen
        full_adder1  u_adder(
            .Ai     (a[i]),
            .Bi     (b[i]),
            .Ci     (co_temp[i-1]), //上一个全加器的溢位是下一个的进位
            .So     (so[i]),
            .Co     (co_temp[i]));
        end
    endgenerate
 
    assign co    = co_temp[3] ;
 
endmodule
  • Module shift8

module top_module ( 
    input clk, 
    input [7:0] d, 
    input [1:0] sel, 
    output [7:0] q 
);
    //wire [7,0] tmp1,tmp2,tmp3;//傻了,这vector中间是冒号是冒号是冒号是冒号!
    wire [7:0] tmp1,tmp2,tmp3;
    my_dff8  m1(clk,d,tmp1);
    my_dff8  m2(clk,tmp1,tmp2);
    my_dff8  m3(clk,tmp2,tmp3);
    always @(*)
        case (sel)
            //2'b00:q=d;
            //2'b01:q=tmp1;
            //2'b02:q=tmp2;
            //2'b03:q=tmp3;   //这都是啥,太低级的错误了,看来需要休息一下了
            2'd00:q=d;
            2'd01:q=tmp1;
            2'd02:q=tmp2;
            2'd03:q=tmp3;
        endcase
endmodule

-----------有点不太清醒了,明天再看看----------20210729-------------------

下面这个是 有点问题的代码

module top_module(
    input [31:0] a,
    input [31:0] b,
    output [31:0] sum
);
    wire cout1;
    always @(*)
        begin
        add16 ad1(a[15:0],b[15:0],1'b0,sum[15:0],cout1);
    
        case (cout1)
            1'b0:add16 ad2(a[31:16],b[31:16],1'b0,sum[31:16],);
            1'b1:add16 ad3(a[31:16],b[31:16],1'b1,sum[31:16],);
            default:add16 ad2(a[31:16],b[31:16],1'b0,sum[31:16],);
        endcase
        
        end
        
endmodule

Error (10170): Verilog HDL syntax error at top_module.v(9) near text: "(";  expecting ";". 
Check for and fix any syntax errors that appear immediately before or at the specified keyword.
The Intel FPGA Knowledge Database contains many articles with specific details on
how to resolve this error. Visit the Knowledge Database
at https://www.altera.com/support/support-resources/knowledge-base/search.html
and search for this specific error message number.
File: /home/h/work/hdlbits.1362317/top_module.v Line: 9
 

 

module top_module(
    input [31:0] a,
    input [31:0] b,
    input sub,
    output [31:0] sum
);
    wire [31:0] b1;
    wire cout1;
    
    //xor(b1,{32{sub}},b); //为什么这一条就不行,和下面有什么区别呢?
    assign b1=b^{32{sub}};
    add16 ad1(a[15:0],b1[15:0],sub,sum[15:0],cout1);
    add16 ad2(a[31:16],b1[31:16],cout1,sum[31:16],);
endmodule

 

推荐阅读