Bootstrap

HDLbits答案更新系列2(2 Verilog Language 2.3 Module: Hierarchy)

目录

前言

2.3 Module: Hierarchy

2.3.1 Modules(Module)

2.3.2 Connecting ports by position(Module pos)

2.3.3 Connecting ports by name(Module name)

2.3.4 Three modules(Module shift)

2.3.5 Modules and vectors(Module shift8)

2.3.6 Adder 1(Module add)

2.3.7 Adder 2(Module fadd)

2.3.8 Carry-select adder(Module cseladd)

2.3.9 Adder-subtractor(Module addsub)

结语

HDLbits网站链接


前言

今天由于要写一些文档,耽搁了一点时间,非常抱歉。

2.3 Module: Hierarchy

2.3.1 Modules(Module)

module top_module ( input a, input b, output out );
    
    mod_a u_mod_a(
        .in1	(a   ),
        .in2	(b   ),
        .out	(out )
    );

endmodule

2.3.2 Connecting ports by position(Module pos)

module top_module ( 
    input a, 
    input b, 
    input c,
    input d,
    output out1,
    output out2
);
    
    mod_a u_mod_a(out1, out2, a, b, c, d);

endmodule

2.3.3 Connecting ports by name(Module name)

module top_module ( 
    input a, 
    input b, 
    input c,
    input d,
    output out1,
    output out2
);
    
    mod_a u_mod_a(
        .out1	(out1),
        .out2	(out2),
        .in1	(a   ),
        .in2	(b   ),
        .in3	(c   ),
        .in4	(d   )
    );

endmodule

2.3.4 Three modules(Module shift)

module top_module ( input clk, input d, output q );
    
    wire	q1;
    wire	q2;
    
    my_dff u1_my_dff(
        .clk    (clk	),
        .d      (d      ),
        .q      (q1     )
    );
    
    my_dff u2_my_dff(
        .clk	(clk	),
        .d      (q1     ),
        .q      (q2     )
    );
    
    my_dff u3_my_dff(
        .clk	(clk	),
        .d      (q2     ),
        .q      (q      )
    );

endmodule

2.3.5 Modules and vectors(Module shift8)

module top_module ( 
    input clk, 
    input [7:0] d, 
    input [1:0] sel, 
    output [7:0] q 
);
    
    wire [7:0]	q1;
    wire [7:0]	q2;
    wire [7:0]	q3;
    
    my_dff8	u1_my_dff8(
        .clk    (clk	),
        .d      (d      ),
        .q      (q1     )
    );
    
    my_dff8	u2_my_dff8(
        .clk	(clk	),
        .d      (q1     ),
        .q      (q2     )
    );
    
    my_dff8	u3_my_dff8(
        .clk	(clk	),
        .d      (q2     ),
        .q      (q3     )
    );
    
    always@(*)begin
        case(sel)
            2'd0:begin
                q = d;
            end
            2'd1:begin
                q = q1;
            end
            2'd2:begin
                q = q2;
            end
            2'd3:begin
                q = q3;
            end
        endcase
    end

endmodule

2.3.6 Adder 1(Module add)

module top_module(
    input [31:0] a,
    input [31:0] b,
    output [31:0] sum
);
    
    wire		cout;
    
    add16 u1_add16(
        .a      (a[15:0]	),
        .b      (b[15:0]	),
        .cin	(1'b0		),
        .sum	(sum[15:0]	),
        .cout	(cout		)
    );

    add16 u2_add16(
        .a      (a[31:16]	),
        .b      (b[31:16]	),
        .cin	(cout		),
        .sum	(sum[31:16]	),
        .cout	(   		)
    );

endmodule

2.3.7 Adder 2(Module fadd)

module top_module (
    input [31:0] a,
    input [31:0] b,
    output [31:0] sum
);//
    
    wire	cout;
    
    add16 u1_add16(
        .a      (a[15:0]	),
        .b      (b[15:0]	),
        .cin	(1'b0		),
        .sum	(sum[15:0]	),
        .cout	(cout		)
    );
    add16 u2_add16(
        .a      (a[31:16]	),
        .b      (b[31:16]	),
        .cin	(cout		),
        .sum	(sum[31:16]	),
        .cout	(        	)
    );
    
endmodule

module add1 ( input a, input b, input cin,   output sum, output cout );
    
    assign {cout, sum} = a + b + cin;
    
endmodule

2.3.8 Carry-select adder(Module cseladd)

module top_module(
    input [31:0] a,
    input [31:0] b,
    output [31:0] sum
);
    
    wire[15:0]	sum_1;
    wire[15:0]	sum_2;
    wire[15:0]	sum_3;
    wire        cout;
    
    assign sum = cout ? {sum_3, sum_1} : {sum_2, sum_1};
    
    add16 u1_add16(
        .a      (a[15:0]    ),
        .b      (b[15:0]    ),
        .cin	(1'b0	    ),
        .sum	(sum_1	    ),
        .cout   (cout       )
    );
    
    add16 u2_add16(
        .a      (a[31:16]   ),
        .b      (b[31:16]   ),
        .cin	(1'b0       ),
        .sum	(sum_2      ),
        .cout	(           )
    );
    add16 u3_add16(
        .a      (a[31:16]   ),
        .b      (b[31:16]   ),
        .cin	(1'b1	    ),
        .sum	(sum_3	    ),
        .cout	(           )
    );

endmodule

2.3.9 Adder-subtractor(Module addsub)

module top_module(
    input [31:0] a,
    input [31:0] b,
    input sub,
    output [31:0] result
);
    
    wire[31:0]	b_com;
    wire		cout;
    
    assign b_com = {32{sub}} ^ b;

    add16 u1_add16(
        .a      (a[15:0]        ),
        .b      (b_com[15:0]	),
        .cin    (sub            ),
        .sum    (result[15:0]   ),
        .cout   (cout           )
    );
    
    add16 u2_add16(
        .a      (a[31:16]       ),
        .b      (b_com[31:16]	),
        .cin    (cout	        ),
        .sum    (result[31:16]	),
        .cout   (               )
    );

endmodule

结语

今天先更新一个小节吧,今天有点忙,耽误了时间,检讨一下自己。还有CSDN对于TAB和空格的差异太过明显,导致括号好多没有对齐,明天抽时间一个一个改正一下。请问各位富文本编辑器有没有方法直接定义TAB为4个空格呢?

唉,这个TAB问题还是没有解决,明明在代码框里看着很整齐,不知道为什么一显示括号就乱了...还有CSDN的富文本编辑器好像不能针对Verilog HDL进行关键词高亮,代码不美观...

HDLbits网站链接

https://hdlbits.01xz.net/wiki/Main_Page

;