You are on page 1of 2

f

ull
adder

modul e f
ull
adder
     

  
input 
a,
b,ci
n,
       
output 
sum,car
ry
       
);

al
way
s_comb

begi
n

sum = a 

b ^
 ci
n;
 
carr
y = (
a & 
b) 
|
 (ci
n & 
b) 
|
 (a 
& ci
n);

end

endmodul
e

CARRYSAVEADDER

modul e CSA
     

  
input 
[3:0] x,
y,z,
       
output [4:0] 
s,
       
output cout
       
);
       
wire 
[3:0] 
c1,
s1, c2;

f
ull
adderf
a_i
nst
10(
x[0]
,
y[0]
,
z[0]
,
s1[
0],
c1[
0])
;
f
ull
adderf
a_i
nst
11(
x[1]
,
y[1]
,
z[1]
,
s1[
1],
c1[
1])
;
f
ull
adderf
a_i
nst
12(
x[2]
,
y[2]
,
z[2]
,
s1[
2],
c1[
2])
;
f
ull
adderf
a_i
nst
13(
x[3]
,
y[3]
,
z[3]
,
s1[
3],
c1[
3])

f
ull
adderf
a_i
nst
20(
s1[1]
,c1[0]
,1'
b0,s[1],
c2[1]);
f
ull
adderf
a_i
nst
21(
s1[2]
,c1[1]
,c2[1]
,s[2]
,c2[2])
;
f
ull
adderf
a_i
nst
22(
s1[3]
,c1[2]
,c2[2]
,s[3]
,c2[3])
;
f
ull
adderf
a_i
nst
23(
1'b0,
c1[3],
c2[3],
s[4],
cout )

assi
gn 
s[0]
 = 
s1[
0];

endmodul
e
TESTBENCHCSA

modul
e t
b_adder
;

  
 r
eg [3:0] 
x,y
,z;
  
 wir
e [4:0]
 s;
  
 wir
e cout ;
  
  
 i
nteger  
i,
j
,k,
err
or;

   
//I
nst
anti
atetheUnitUnderTest(
UUT)
  CSAuut 
(x,
y,
z,
s,cout
);

//Stimul usbl ock-al ltheinputcombi nati


onsaretestedher
e.
//thenumberofer r
orsarer ecordedinthesignalnamed"er
ror
".
   
initi
al  
begi n
     //Initiali
zeI nput s
    x  = 0;
    y  
=  0;
    z  = 0;
    er ror = 0;
     //threef orl oopst otestal li
nputcombinati
ons.
    for(i
=0; i
<16; i
=i +1) 
begin
        
for (
j=0; j
<16; j
=j+1) 
begi n
          for(k=0; k<16;k=k+1)  
begin
            x = i
;
            y = j
;
            z = k;
            #10;
            if
( {
cout ,s}
 != 
(i
+j+k) )
 
             er ror <= er
ror + 1;
          end      
        
end   
    end
   
end 
   
endmodul e

You might also like