day3 Modules: Hierarchy
目标:调用子模块,注意别忘了“,”和“;”
module top_module ( input a, input b, output out );
mod_a u2(
.in1(a),
.in2(b),
.out(out),
);
endmodule
注意:未给出mod_a端口名,需按顺序调用
module top_module (
input a,
input b,
input c,
input d,
output out1,
output out2
);
mod_a u2(
out1, //按顺序对应mod_a
out2,
a,
b,
c,
d,
);
endmodule
要求用名称调用
module top_module (
input a,
input b,
input c,
input d,
output out1,
output out2
);
mod_a u2(
.out1(out1),
.out2(out2),
.in1(a),
.in2(b),
.in3(c),
.in4(d),
);
endmodule
想复杂了,参考了一个大佬的HDLBits答案(3)_Verilog模块的例化与调用_verilog多路选择器调用模块-优快云博客,按顺序写模块外的连线名字即可(模块的端口名不用写)
module top_module ( input clk, input d, output q );
wire temp1,temp2;
my_dff block1(clk,d,temp1);
my_dff block2(clk,temp1,temp2);
my_dff block3(clk,temp2,q);
endmodule
这里的多用复用器没有给出了,参考了另一篇文章大佬的写法模型机设计(VERILOG)-多路复用器和移位逻辑_verilog 多路复用器-优快云博客
module top_module (
input clk,
input [7:0] d,
input [1:0] sel,
output [7:0] q
);
wire [7:0] temp1,temp2,temp3;
my_dff8 block1(clk,d,temp1);
my_dff8 block2(clk,temp1,temp2);
my_dff8 block3(clk,temp2,temp3);
always@(temp1,temp2,temp3,sel)
begin
case(sel)
2'b00:q[7:0]=d[7:0];
2'b01:q[7:0]=temp1[7:0];
2'b10:q[7:0]=temp2[7:0];
2'b11:q[7:0]=temp3[7:0];
default;
endcase
end
endmodule
module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
wire [15:0]result1,result2;
wire cin1,cin2;
add16 a1(a[15:0],b[15:0],1'b0,result1,cin1);
add16 a2(a[31:16],b[31:16],cin1,result2,cin2);
assign sum[31:0]={result2,result1};
endmodule
module top_module (
input [31:0] a,
input [31:0] b,
output [31:0] sum
);//
wire [15:0] result1,result2;
wire cin1,cin2;
add16 add16_1(a[15:0],b[15:0],1'b0,result1,cin1);
add16 add16_2(a[31:16],b[31:16],cin1,result2,cin2);
assign sum[31:0]={result2,result1};
endmodule
module add1 ( input a, input b, input cin, output sum, output cout );
assign sum=a^b^cin;
assign cout=(a&b)|(a&cin)|(b&cin);
// Full adder module here
endmodule
module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
wire [15:0] result1,result2,result3;
wire cont1,cont2,cont3;
add16 u1(a[15:0],b[15:0],1'b0,result1,cont1);
add16 u2(a[31:16],b[31:16],1'b0,result2,cont2);
add16 u3(a[31:16],b[31:16],1'b1,result3,cont3);
always@(cont1)
begin
case(cont1)
1'b0:sum[31:0]={result2,result1};
1'b1:sum[31:0]={result3,result1};
default;
endcase
end
endmodule
但是出现了这个警告,目前不知道是啥原因
module top_module(
input [31:0] a,
input [31:0] b,
input sub,
output [31:0] sum
);
wire [15:0] result1,result2;
wire cont1,cont2;
wire [31:0] nb;
assign nb[31:0]= b[31:0]^{32{sub}};
add16 u1(a[15:0],nb[15:0],sub,result1,cont1);
add16 u2(a[31:16],nb[31:16],cont1,result2,cont2);
assign sum[31:0]={result2[15:0],result1[15:0]};
endmodule
为啥用 (a + b + 0) and (a + ~b + 1)的思路写不对?
这里很奇怪,b[31:0]^{32{sub}}的结果应该是把输入的结果变成反码(正数不变,负数按位取反)