1. Module(Modules)
元件例化时分配端口的方法有:位置关联法、名称关联法
(1)位置关联法
module top_module (
input a, b,
output out
);
mod_a M1 ( a, b, out );
endmodule
注意:使用位置关联法时,分配端口的顺序要与所例化元件的端口顺序相一致。
(2)名称关联法
module top_module (
input a, b,
output out
);
mod_a M1 ( .out(out), .in1(a), .in2(b) );
endmodule
建议使用名称关联法,这样不仅能将端口之间的对应关系表述得更明确,而且还不用考虑端口的顺序问题。
2. Module pos(Connecting ports by position)
使用位置关联法进行例化。
module top_module (
input a, b, c, d,
output out1, output out2
);
mod_a M1 ( out1, out2, a, b, c, d );
endmodule
3. Module name(Connecting ports by name)
module top_module (
input a, b, c, d,
output out1, out2
);
mod_a M1 ( .out1(out1), .out2(out2), .in1(a), .in2(b), .in3(c), .in4(d) );
endmodule
4. Module shift(Three modules)
当进行多个例化时,要备注每个例化元件的名称,且名称不能有重复。
module top_module (
output q,
input clk, d
);
wire w1, w2;
my_dff M1 ( .q(w1), .clk(clk), .d(d) );
my_dff M2 ( .q(w2), .clk(clk), .d(w1) );
my_dff M3 ( .q(q), .clk(clk), .d(w2) );
endmodule
5. Module shift8(Modules and vectors)
module top_module (
output [7:0] q,
input clk,
input [7:0] d,
input [1:0] sel
);
wire [7:0] w1, w2, w3;
always @(*) begin
case(sel)
2'h0: q = d;
2'h1: q = w1;
2'h2: q = w2;
2'h3: q = w3;
endcase
end
my_dff8 M1 ( .q(w1), .clk(clk), .d(d) );
my_dff8 M2 ( .q(w2), .clk(clk), .d(w1) );
my_dff8 M3 ( .q(w3), .clk(clk), .d(w2) );
endmodule
6. Module add(Adder 1)
module top_module (
output [31:0] sum,
input [31:0] a, b
);
wire c;
add16 M1 ( .sum(sum[15:0]), .cout(c), .a(a[15:0]), .b(b[15:0]), .cin(1'b0) );
add16 M2 ( .sum(sum[31:16]), .a(a[31:16]), .b(b[31:16]), .cin(c) );
endmodule
7. Module fadd(Adder 2)
module top_module (
output [31:0] sum,
input [31:0] a, b
);
wire c;
add16 M1 ( .sum(sum[15:0]), .cout(c), .a(a[15:0]), .b(b[15:0]), .cin(1'b0) );
add16 M2 ( .sum(sum[31:16]), .a(a[31:16]), .b(b[31:16]), .cin(c) );
endmodule
module add1 (
output sum, cout,
input a, b, cin
);
assign sum = a^b^cin;
assign cout = a&b | a&cin | b&cin;
endmodule
8. Module cseladd(Carry-select adder)
module top_module (
output [31:0] sum,
input [31:0] a, b
);
wire c;
wire [15:0] w1, w2;
always @(*) begin
case(c)
1'b0: sum[31:16] = w1;
1'b1: sum[31:16] = w2;
endcase
end
add16 M1 ( .sum(sum[15:0]), .cout(c), .a(a[15:0]), .b(b[15:0]), .cin(1'b0) );
add16 M2 ( .sum(w1), .a(a[31:16]), .b(b[31:16]), .cin(1'b0) );
add16 M3 ( .sum(w2), .a(a[31:16]), .b(b[31:16]), .cin(1'b1) );
endmodule
9. Module addsub(Adder-sbutractor)
减法 = 加数 + 被加数的补码 = 加数 + 被加数的反码 + 1
module top_module (
output [31:0] sum,
input [31:0] a, b,
input sub
);
wire c;
wire [31:0] B;
add16 M1 ( .sum(sum[15:0]), .cout(c), .a(a[15:0]), .b(B[15:0]), .cin(sub) );
add16 M2 ( .sum(sum[31:16]), .a(a[31:16]), .b(B[31:16]), .cin(c) );
assign B = {32{sub}} ^ b;
endmodule