# Verilog - 16 Bit RCA - Help w/ Code

#### bkpatel7

Joined Feb 23, 2009
2
For class, we are learning a little bit of Verilog and using ModelSim to code it in. I needed some assistance/guidance on how to code buses. I just can't seem to understand it fully. Basically, I have a 1-bit adder already coded:

Rich (BB code):
module Bit1Adder (x1, x2, cin, cout, sum);
input x1, x2, cin;
output cout, sum;
assign cout = ((x1 & x2) | (cin & (x1 ^ x2)));
assign sum = (x1 ^ x2) ^ cin;
endmodule
Now, using this I want to create a 16-bit cascading adder. So for that, I have this so far:

Rich (BB code):
include 1BitAdder.v

Bit1Adder Adder15 (a, b, cout14, sum15, cout15);
***I have it set up so all the inputs on the adders are the same (a, b) and then the Carry-Out (cout) of each Adder feeds into the Carry-In of the next Adder.

My question is that how do I use/set up buses to finish the rest of this correctly?

**Would it be something like this:
Rich (BB code):
module Bit16Adder (a, b, cin, cout, sum);
input [15:0] a;
input [15:0] b;
input cin;
output [15:0] sum;
output cout;`
This seems to me to be somewhat correct, but I feel I have something missing or something just not right about this.

Please guide me in the right direction and my apologizes for the long post.

#### tuborggg

Joined Jan 3, 2009
37
Hi,
So you have 1 bit adder.
You have the 16 bit module ready with its in and out ports (yes, it's correct what you wrote).
All you need to do is connect the wires b/w the adders inside...
(i.e. connect x1 from the input to Adder0 port a)
btw, instead of making sure that your ports has the same order, use dot '.' for the internal port which you connect outside.

#### bkpatel7

Joined Feb 23, 2009
2
Hi,
So you have 1 bit adder.
You have the 16 bit module ready with its in and out ports (yes, it's correct what you wrote).
All you need to do is connect the wires b/w the adders inside...
(i.e. connect x1 from the input to Adder0 port a)
btw, instead of making sure that your ports has the same order, use dot '.' for the internal port which you connect outside.
Thanks for your post and help.

Could you provide a quick code example with your answer, I grasp things better when I see an example of it, that way I can implement it better in my own code.

#### tuborggg

Joined Jan 3, 2009
37
a , // first input
b , // Second input
ci , // Carry input
sum , // sum output
co // carry output
);
//Input declaration
input a;
input b;
input ci;
//Ouput declaration
output sum;
output co;
//Port Data types
wire a;
wire b;
wire ci;
wire sum;
wire co;
//Code starts here
assign {co,sum} = a + b + ci;

endmodule // End of Module addbit

Now if you want to use this 1 bit there's the big module:
result , // Output of the adder
carry , // Carry output of adder
r1 , // first input
r2 , // second input
ci // carry input
);

// Input Port Declarations
input [3:0] r1 ;
input [3:0] r2 ;
input ci ;

// Output Port Declarations
output [3:0] result ;
output carry ;

// Port Wires
wire [3:0] r1 ;
wire [3:0] r2 ;
wire ci ;
wire [3:0] result ;
wire carry ;

// Internal variables
wire c1 ;
wire c2 ;
wire c3 ;

// Code Starts Here
.a (r1[0]) ,
.b (r2[0]) ,
.ci (ci) ,
.sum (result[0]) ,
.co (c1)
);

.a (r1[1]) ,
.b (r2[1]) ,
.ci (c1) ,
.sum (result[1]) ,
.co (c2)
);

.a (r1[2]) ,
.b (r2[2]) ,
.ci (c2) ,
.sum (result[2]) ,
.co (c3)
);