How to assign bitwidth in module ?

Status
Not open for further replies.

yamcake01096

Newbie level 2
Joined
Dec 13, 2010
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,297
consider i need to design a module with two submodule with same function but different bitwidth input, how i gonna define in higher level module?
For example adder1 is addition of 2bit, adder2 is addition of 4bit. how do i assign 'WIDTH' in higher level module?

///////////////////////////////////////////////////

module sum()

adder adder1( //2bit +2bit =2bit
.a(input1),
.b(input2),
.c(output1),
);

adder adder2( //4bit +4bit =4bit
.a(input3),
.b(input4),
.c(output2),
);

endmodule


////////////////////////////////////////////////////////////////

module adder (a,b,c);
parameter WIDTH = 2; //defult value
input [WIDTH-1:0] a;
input [WIDTH-1:0] b;
output [WIDTH-1:0] c;
assign c=a+b;
endmodule
 

Do this:

Code:
module sum()

adder #(.WIDTH()) adder1( //2bit +2bit =2bit  - This shows you are using default value of WIDTH, and you did not forget about it
.a(input1),
.b(input2),
.c(output1),
);

adder #(.WIDTH(4)) adder2( //4bit +4bit =4bit
.a(input3),
.b(input4),
.c(output2),
);

endmodule

Also for your module declaration, use this simpler style; you only mention the port names once.

Code:
module #(WIDTH = 2) //default value
(input [WIDTH-1:0] a,
input [WIDTH-1:0] b,
output [WIDTH-1:0] c
);
assign c=a+b;
endmodule
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…