_TOP_MENU

Showing posts with label Mathematics. Show all posts
Showing posts with label Mathematics. Show all posts

Nov 26, 2016

Type of Adders with Verilog Code


Addition on 2 -bit  ->

d_out[1:0] =  a_in + b_in ;


Half Adder ->






Full Adder -->







Verilog Code ->


Half Adder -> 
module half_adder(a,b,sum,carry);
input a,b;
output sum, carry;
wire sum, carry;
assign sum = a^b; // sum bit
assign carry = (a&b) ;
endmodule


Types of Adders :
  1. Ripple Carry Adder 
  2. Carry Lookahead Adder
  3. RCLA (Ripple block carry lookahead adder)
  4. BCLA (Binary carry lookahead adder)
  5. conditional sum adder 
  6. carry select adder 
  7. carry skip adder 
    8.Parallel prefix adder -
  • Ladner-Fischer adder
  • Kogge-Stone adder
  • Brent-Kung adder
  • Han-Carlson adder

Ripple Carry Adder -> 
The most straightforward implementation of a final stage adder for two n-bit operands is a ripple carry adder, which requires n full adders (FAs). The carry-out of the ith FA is connected to the carry-in of the (i+1)th FA. Figure 1 shows a ripple carry adder for n-bit operands, producing n-bit sum outputs and a carry out. 


Verilog Code for Ripple carry Adder - 

-------------------------------------------------------------------- VERILOG CODE --------------------------------
module ripple_carry_adder ( 
 input [n-1:0] A_in,
 input [n-1:0] B_in,
 output [n-1:0] Sum,
 output Cn 
);


parameter n =10;

full_adder i_full_adder_0 ( .A(A_in[0] , .B(B_in[0], .sum(sum[0]) , .cin(1'b0), .cout(carry[0]) );

genvar c;
generate
for(c=1 ; c < n ; c = c +1) begin

  full_adder i_full_adder[c] ( .A(A_in[c]),
  .B(B_in[c]),
  .sum(sum[c]),
  .cin(carry[c-1]) ,
  .cout(carry[c])
  );
end

endgenerate

endmodule
-------------------------------------------------------------------- VERILOG CODE End -------------------------

Carry Lookahead Adder

 ---- Ref ---
 http://www.aoki.ecei.tohoku.ac.jp/arith/mg/algorithm.html#fsa_pfx

Jul 1, 2016

Two's Complement

The two's complement of an N-bit number is defined as the complement with respect to 2N; in other words, it is the result of subtracting the number from 2N



Three-bit two's-complement integers
Bits Unsigned
value
Two's
complement
value
011 3  3 
010 2  2 
001 1  1 
000 0  0 
111 7  -1 
110 6  -2 
101 5  -3 
100 4  -4


Eight-bit two's-complement integers
Bits Unsigned
value
Two's
complement
value
0111 1111 127  127 
0111 1110 126  126 
0000 0010 2  2 
0000 0001 1  1 
0000 0000 0  0 
1111 1111 255  −1 
1111 1110 254  −2 
1000 0010 130  −126 
1000 0001 129  −127 
1000 0000 128  −128 


The term two's complement can mean either a number format or a mathematical operator. For example, 0111 represents decimal 7 in two's-complement notation, but the two's complement of 7 in a 4-bit register is actually the "1001" bit string (the same as represents 9 = 24 − 7 in unsigned arithmetics) which is the two's complement representation of −7. The statement "convert x to two's complement" may be ambiguous, since it could describe either the process of representing x in two's-complement notation without changing its value, or the calculation of the two's complement, which is the arithmetic negative of x if two's complement representation is used.

Converting to two's complement representation

Invert all bits and add 1.

Example

1.
+ 15   ->  0000 1111
+  5   ->   0000 0101
------       --------------------
  20          0001 0100

2.

+ 15  ->   0000 1111       ->    0000 1111
-  5    -> -  0000 0101  2's ->   1111 1011 
                                              --------------------
                                             1 0000 1010  -> 10 , ignore Carry

3.
- 15   ->   -  0000 1111  2's  ->  1111 0001
+  5  ->      0000 0101         ->  0000 0101
                                                -------------------
                                                 1111 0110   ->  sign bit is 1 , take 2's complement
                  2's ->   0000 1010   -> -10 (Answer)

4.
- 15  ->   - 0000 1111   2's ->  1111 0001
-  5   ->   - 0000 0101   2's ->  1111 1011
                                               --------------------
                                             1 1110 1100  -> Ignore Carry ,Sign bit is 1 , take 2's complement
             1110 1100   -> 2's ->  0001 0100  ->  -20 (Answer)