2.3.1、Modules
module top_module ( input a, input b, output out );
mod_a mod_a_inst(
.in1 (a),
.in2 (b),
.out (out)
);
endmodule
2.3.2、Connecting ports by position
module top_module (
input a,
input b,
input c,
input d,
output out1,
output out2
);
mod_a mod_a_inst(out1,out2,a,b,c,d);
endmodule
2.3.3、Connecting ports by name
module top_module (
input a,
input b,
input c,
input d,
output out1,
output out2
);
mod_a mod_a_inst(
.in1(a),
.in2(b),
.in3(c),
.in4(d),
.out1(out1),
.out2(out2)
);
2.3.4、Three modules
module top_module ( input clk, input d, output q );
wire q1,q2;
my_dff u0(.clk(clk),.d(d),.q(q1));
my_dff u1(.clk(clk),.d(q1),.q(q2));
my_dff u2(.clk(clk),.d(q2),.q(q));
endmodule
2.3.5、modules and vectors
module top_module (
input clk,
input [7:0] d,
input [1:0] sel,
output [7:0] q
);
wire [7:0] q1,q2,q3;
always@(*) begin
case(sel)
2'd0: q = d;
2'd1: q = q1;
2'd2: q = q2;
2'd3: q = q3;
endcase
end
my_dff8 u0(.clk(clk),.d(d),.q(q1));
my_dff8 u1(.clk(clk),.d(q1),.q(q2));
my_dff8 u2(.clk(clk),.d(q2),.q(q3));
endmodule
2.3.6、Adder 1
module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
wire cout_2_cin;
add16 a0(.a(a[15:0]),
.b(b[15:0]),
.cin(1'b0),
.sum(sum[15:0]),
.cout(cout_2_cin)
);
add16 a1(.a(a[31:16]),
.b(b[31:16]),
.cin(cout_2_cin),
.sum(sum[31:16]),
.cout()
);
endmodule
2.3.7、Adder 2
module top_module (
input [31:0] a,
input [31:0] b,
output [31:0] sum
);//
wire cout_2_cin;
add16 u0(
.a (a[15:0]),
.b (b[15:0]),
.cin (1'b0),
.cout (cout_2_cin),
.sum (sum[15:0])
);
add16 u1(
.a (a[31:16]),
.b (b[31:16]),
.cin (cout_2_cin),
.cout (),
.sum (sum[31:16])
);
endmodule
module add1 ( input a, input b, input cin, output sum, output cout );
assign sum=a^b^cin;
assign cout=(a&&b)||((a^b)&&cin); //assign {cout,sum}=a+b+cin
// Full adder module here
endmodule
2.3.8、Carry-select adder
module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
wire [15:0] c1,c2;
wire sel;
always @(*) begin
case(sel)
1'b0:sum[31:16]=c1;
1'b1:sum[31:16]=c2;
endcase
end
add16 u0(.a(a[15:0]),.b(b[15:0]),.cin(0),.sum(sum[15:0]),.cout(sel));
add16 u1(.a(a[31:16]),.b(b[31:16]),.cin(0),.sum(c1),.cout());
add16 u2(.a(a[31:16]),.b(b[31:16]),.cin(1),.sum(c2),.cout());
endmodule
module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
wire [15:0] sum1,sum2,sum3;
wire cout;
assign sum = cout ? {sum3,sum1} : {sum2,sum1};
add16 inst1(
.a (a[15:0] ),
.b (b[15:0] ),
.cin (1'b0 ),
.cout (cout ),
.sum (sum1 )
);
add16 inst2(
.a (a[31:16] ),
.b (b[31:16] ),
.cin (1'b0 ),
.cout ( ),
.sum (sum2 )
);
add16 inst3(
.a (a[31:16] ),
.b (b[31:16] ),
.cin (1'b1 ),
.cout ( ),
.sum (sum3 )
);
endmodul
2.3.9、Adder subtractor
module top_module(
input [31:0] a,
input [31:0] b,
input sub,
output [31:0] sum
);
wire [31:0] c;
wire cout_2_cin;
assign c={32{sub}}^b;
add16 u0(.a(a[15:0]),.b(c[15:0]),.cin(sub),.sum(sum[15:0]),.cout(cout_2_cin));
add16 u1(.a(a[31:16]),.b(c[31:16]),.cin(cout_2_cin),.sum(sum[31:16]),.cout());
endmodule