目录
4.层次结构
4.1 模组
4.2按位置连接端口
4.3通过名称连接端口
4.4三个模块
4.5模块和向量
4.6加法器1
4.7加法器2
4.8进位选择加法器
4.9加减法
4.1 模组
module top_module(a,b,out);
input a;
input b;
output out;
mod_a u_mod_a(.in1(a),
.in2(b),
.out(out)
);
endmodule
4.2按位置连接端口
module top_module(a,b,c,d,out1,out2);
input a;
input b;
input c;
input d;
output out1,out2;
mod_a u_mod_a(out1,out2,a,b,c,d);
endmodule
4.3通过名称连接端口
module top_module(a,b,c,d,out1,out2);
input a;
input b;
input c;
input d;
output out1,out2;
mod_a u_mod_a(
.in1(a),
.in2(b),
.in3( c ),
.in4(d),
.out1(out1),
.out2(out2)
);
endmodule
4.4三个模块
module top_module(clk,d,q);
wire q1,q2;
input clk,d;
output q;
my_dff u1_my_dff(
.d(d),
.clk(clk),
.q(q1)
);
my_dff u2_my_dff(
.d(q1),
.clk(clk),
.q(q2)
);
my_dff u3_my_dff(
.q(q),
.clk(clk),
.d(q2)
);
endmodule
4.5模块和向量
module top_module(clk,d,sel,q);
input clk;
input [7:0]d;
input [1:0]sel;
output [7:0]q;
wire [7:0]q1,q2,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’b00:q=d;
2’b01:q=q1;
2’b10:q=q2;
2’b11:q=q3;
endcase
end
endmodule
4.6加法器1
module top_module(a,b,sum);
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]),
.sum(sum[15:0]),
.cin(1’b0),
.cout(cout)
);
add16 u2_add16(
.a(a[31:16]),
.b(b[31:16]),
.sum(sum[31:16]),
.cin(cout),
.cout()
);
endmodule
4.7加法器2
module top_module(a,b,sum);
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(a,b,cin,sum,cout);
input a,b,cin;
output sum,cout;
assign {cout,sum}=a+b+cin;
endmodule
4.8进位选择加法器
module top_module(a,b,sum);
input [31:0]a;
input [31:0]b;
wire cout;
output [31:0]sum;
wire [15:0]sum_1;
wire [15:0]sum_2;
wire [15:0]sum_3;
add16 u1_add16(
.a(a[15:0]),
.b(b[15:0]),
.cin(0),
.cout(cout),
.sum(sum_1)
);
add16 u2_add16(
.a(a[31:16]),
.b(b[31:16]),
.cin(0),
.cout(),
.sum(sum_2)
);
add16 u3_add16(
.a(a[31:16]),
.b(b[31:16]),
.cin(1),
.cout(),
.sum(sum_3)
);
assign sum=cout?{sum_3,sum_1}:{sum_2,sum_1};
endmodule
4.9加减法
module top_module(a,b,sum,sub);
input [31:0]a;
input [31:0]b;
input sub;
output [31:0]sum;
wire cout;
wire [31:0]bout;
assign bout={32{sub}}^b;
add16 u1_add16(
.a(a[15:0]),
.b(bout[15:0]),
.cin(sub),
.cout(cout),
.sum(sum[15:0])
);
add16 u2_add16(
.a(a[31:16]),
.b(bout[31:16]),
.cin(cout),
.cout(),
.sum(sum[31:16])
);
endmodule
欢迎大家交流指正!!!