1. 单输出单模块
方法一:按照端口的位置
module top_module ( input a, input b, output out );
mod_a(a,b,out);
endmodule
方法二:按照端口的名字
module top_module ( input a, input b, output out );
mod_a(
.in1(a),
.in2(b),
.out(out)
);
endmodule
2. 多输出单模块
方法一:按照端口位置
module top_module (
input a,
input b,
input c,
input d,
output out1,
output out2
);
mod_a(out1,out2,a,b,c,d); // 注意端口顺序,先输出,后输入
endmodule
方法二:按照端口名称
module top_module (
input a,
input b,
input c,
input d,
output out1,
output out2
);
mod_a(
.out1(out1),
.out2(out2),
.in1(a),
.in2(b),
.in3(c),
.in4(d)
); // 注意端口顺序,先输出,后输入
endmodule
3. 单输出多模块
module top_module ( input clk, input d, output q );
wire q1,q2; //构建中间导线
my_dff one(clk,d,q1);
my_dff two(clk,q1,q2);
my_dff three(clk,q2,q);
endmodule
4. Medules and vector.
module top_module (
input clk,
input [7:0] d,
input [1:0] sel,
output reg [7:0] q
);
wire [7:0] o1, o2, o3; // output of each my_dff8
// Instantiate three my_dff8s
my_dff8 d1 ( clk, d, o1 );
my_dff8 d2 ( clk, o1, o2 );
my_dff8 d3 ( clk, o2, o3 );
// This is one way to make a 4-to-1 multiplexer
always @(*) // Combinational always block
case(sel)
2'h0: q = d;
2'h1: q = o1;
2'h2: q = o2;
2'h3: q = o3;
endcase
endmodule
5. 加法器Module add
module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
//构建3根中间导线
wire [31:0] w1,w2; //定义变量时,变量多大有多少位在名字前
wire cout;
assign w1 = a;
assign w2 = b; //使用变量时,使用谁的哪些位
add16 low(.a(w1[15:0]),.b(w2[15:0]),.cin(1'b0),.sum(sum[15:0]),.cout(cout));
add16 high(.a(w1[31:16]),.b(w2[31:16]),.cin(cout),.sum(sum[31:16]));
endmodule
module top_module (
input [31:0] a,
input [31:0] b,
output [31:0] sum
);//
wire cout1;
add16 low(.a(a[15:0]),.b(b[15:0]),.cin(1'b0),.sum(sum[15:0]),.cout(cout1));
add16 high(.a(a[31:16]),.b(b[31:16]),.cin(cout1),.sum(sum[31:16]));
endmodule
module add1 ( input a, input b, input cin, output sum, output cout );
// Full adder module here
assign {cout,sum} = a + b + cin ; //串联表示{,},高位在前
endmodule
6. Carry-select adder 纹波进位加法器
需要明确:
整体同时进行3个加法器的计算,耗时一致,最终的输出结果sum【31:0】装载谁,由最后面的判断来决定。
module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
wire cout1; // 低位加法器 进位值
wire [15:0] sum0,sum1; //高位加法器计算值,非最终装载值
add16 low(.a(a[15:0]),.b(b[15:0]),.cin(1'b0),.sum(sum[15:0]),.cout(cout1)); //低位加法器计算
add16 high_0(.a(a[31:16]),.b(b[31:16]),.cin(1'b0),.sum(sum0)); //高位加法器计算
add16 high_1(.a(a[31:16]),.b(b[31:16]),.cin(1'b1),.sum(sum1));
// 根据 低位加法器 是否进位,选择 使用 哪个高位加法器的值
assign sum[31:16] = (cout1?sum1:sum0); //(判断表达式?(为真1则执行表达式1:为假0则执行表达式0))
endmodule
采用case语句判断也可行,就是语言较繁琐。
module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
wire cout1; // 低位加法器 进位值
wire [15:0] sum0,sum1; //高位加法器计算值,非最终装载值
add16 low(.a(a[15:0]),.b(b[15:0]),.cin(1'b0),.sum(sum[15:0]),.cout(cout1)); //低位加法器计算
add16 high_0(.a(a[31:16]),.b(b[31:16]),.cin(1'b0),.sum(sum0)); //高位加法器计算
add16 high_1(.a(a[31:16]),.b(b[31:16]),.cin(1'b1),.sum(sum1));
// 根据 低位加法器 是否进位,选择 使用 哪个高位加法器的值
//assign sum[31:16] = (cout1?sum1:sum0); //(判断表达式?(为真1则执行表达式1:为假0则执行表达式0))
always@(*) // 根据 低位加法器 是否进位,选择 使用 哪个高位加法器的值
case(cout1)
1'b0: sum[31:16] = sum0;
1'b1: sum[31:16] = sum1;
endcase
endmodule
7. Adder-subtractor 加法器-减法器