其他分享
首页 > 其他分享> > HDL Bits---Modules:Hierarchy

HDL Bits---Modules:Hierarchy

作者:互联网

 

2.3.1 Modules

 

 module top_module ( input a, input b, output out );

mod_a david(.out(out),.in1(a),.in2(b));

endmodule

 

 

 

2.3.2 Connecting ports by position

 

 目前有一个名为 mod_a 的模块,该模块按顺序具有 2 个输出和 4 个输入。你必须将6个端口按位置与你的顶层模块端口 out1、out2、a、b、c、d按顺序依次连接。

module top_module (
input a,
input b,
input c,
input d,
output out1,
output out2
);
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 inst(
.in1(a),
.in2(b),
.in3(c),
.in4(d),
.out1(out1),
.out2(out2)
);

endmodule

 

2.3.4 Three modules(Module shift)

 

 

module top_module (
input clk,
input d,
output q
);

wire a, b; // 声明两个wire变量,命名为a, b

// 对my_dff进行了三次实例化,用了三个不用的名字 d1, d2, d3
// 端口使用了按位置连接的方式( input clk, input d, output q)
my_dff d1 ( clk, d, a );
my_dff d2 ( clk, a, b );
my_dff d3 ( clk, b, q );

endmodule

 

 

2.3.5 Modules and vectors(Module shift8)

 

module top_module (

input clk,
input [7:0] d,
input [1:0] sel,
output [7:0] q
);
wire [7:0]a,b,c;             //记得加位宽    
my_dff8 d1(clk,d,a);
my_dff8 d2(clk,a,b);
my_dff8 d3(clk,b,c);

always@(*)
begin
case(sel)
2'b00:q=d;
2'b01:q=a;
2'b10:q=b;
2'b11:q=c;
endcase
end
endmodule

 

 

2.3.6 Adder 1

 

 方法一:

module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
wire cout1;     //记得定义cout1数据类型
add16 add_low(a[15:0],b[15:0],1'b0,sum[15:0],cout1);
add16 add_high(a[31:16],b[31:16],cout1,sum[31:16],);      //省了cout2
endmodule

 

方法二:

module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
wire cout1;
wire [15:0] sum_1;
wire [15:0] sum_2;
add16 add_low(a[15:0],b[15:0],1'b0,sum_1,cout1);
add16 add_high(a[31:16],b[31:16],cout1,sum_2,);          //省了cout2
assign sum={sum_2,sum_1};
endmodule

2.3.7 Adder2

module top_module (
input [31:0] a,
input [31:0] b,
output [31:0] sum
);//
wire cout_1;
add16 add_low(a[15:0],b[15:0],0,sum[15:0],cout_1);
add16 add_high(a[31:16],b[31:16],cout_1,sum[31:16],);
endmodule

module add1 ( input a, input b, input cin, output sum, output cout );

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 cout_1;
wire [15:0]sum_0;                ////wire [15:0] sum_0,sum_1;
wire [15:0]sum_1;
add16 d1(a[15:0],b[15:0],1'b0,sum[15:0],cout_1);
add16 d2(a[31:16],b[31:16],1'b0,sum_0,);
add16 d3(a[31:16],b[31:16],1'b1,sum_1,);

always@(*)                     //assign sum[31:16]=cout?sum_1:sum_0;

begin
case(cout_1)
1'b0:sum[31:16]=sum_0;
1'b1:sum[31:16]=sum_1;
endcase
end

assign sum={sum[31:16],sum[15:0]};      //可省略
endmodule

 

2.3.9 Adder-subtractor

 

 

module top_module(
input [31:0] a,
input [31:0] b,
input sub,
output [31:0] sum
);
wire cout;
wire [31:0]c;
assign c={32{sub}}^b;
add16 d1(a[15:0],c[15:0],sub,sum[15:0],cout);
add16 d2(a[31:16],c[31:16],cout,sum[31:16],);
endmodule

 

标签:15,16,sum,Modules,module,---,input,HDL,31
来源: https://www.cnblogs.com/luckyu922/p/16419065.html