Question

In: Electrical Engineering

Design of 4 Bit Adder/Subtractor using Loops (Behavior Modeling Style) (verilog Code) -

Design of 4 Bit Adder/Subtractor using Loops (Behavior Modeling Style) (verilog Code) -

Solutions

Expert Solution

if a_s = 0 then it will act as adder and for a_s = 1 will act as subtractor

VERILOG CODE:

module adder_sub_4bit ( a ,b ,sum_diff ,carry_borrow,a_s );

output reg [3:0] sum_diff ;
output reg carry_borrow ;

input [3:0] a ;
input [3:0] b ;
input a_s;

wire [3:0]l;

assign l = ~ b;

integer i;

reg [4:0]s;

always @ (a or b) begin
   if (a_s == 1'b0)
       begin
           s[0] = 0;
           for (i=0;i<=3;i=i+1) begin
               sum_diff [i] = a[i] ^ b[i] ^ s[i];
               s[i+1] = (a[i] & b[i]) | (b[i] & s[i]) | (s[i] & a[i]);
           end
           carry_borrow = s[4];
       end
  
   else begin
       s[0] = 1;
       for (i=0;i<=3;i=i+1) begin
           sum_diff[i] = a[i] ^ l[i] ^ s[i];
           s[i+1] = (a[i] & l[i]) | (l[i] & s[i]) | (s[i] & a[i]) ;
       end
       carry_borrow = ~s[4];
   end
end

endmodule

TEST BENCH:

module tb_add_sub;

   // Inputs
   reg [3:0] a;
   reg [3:0] b;
   reg a_s;

   // Outputs
   wire [3:0] sum_diff;
   wire carry_borrow;

   // Instantiate the Unit Under Test (UUT)
   adder_sub_4bit uut (
       .a(a),
       .b(b),
       .sum_diff(sum_diff),
       .carry_borrow(carry_borrow),
       .a_s(a_s)
   );

   initial begin
       // Initialize Inputs
       a = 1; b = 3; a_s = 0;
       #100 a = 6; b = 3; a_s = 1;


   end
  
endmodule

Simulation Results are


Related Solutions

Design a 32 bit adder using a single 4 bit adder using verilog code
Design a 32 bit adder using a single 4 bit adder using verilog code
Design a 4-bit multiplier by using 4 bit full adder and write a verilog code.
Design a 4-bit multiplier by using 4 bit full adder and write a verilog code.
Design an 8-bit adder. Show Verilog code and testbench.
Design an 8-bit adder. Show Verilog code and testbench.
Design a 32 bit after using a single 4 bit using verilog code
Design a 32 bit after using a single 4 bit using verilog code
Design and Test an 8-bit Adder using 4-bit adder. Use 4-bit adder coded in class using...
Design and Test an 8-bit Adder using 4-bit adder. Use 4-bit adder coded in class using full adder that is coded using data flow model. Use test bench to test 8-bit adder and consider at least five different test vectors to test it.
Design and implementation of 4 bit ALU (74181) using behavioral style of modeling
Design and implementation of 4 bit ALU (74181) using behavioral style of modeling
Make a 2 bit binary adder subtractor multiplier on verilog and display it on seven segment...
Make a 2 bit binary adder subtractor multiplier on verilog and display it on seven segment using fpga
Design an 8-bit adder. Show the truth table, logic circuit, and Verilog code.
Design an 8-bit adder. Show the truth table, logic circuit, and Verilog code.
Design an 8-bit adder. Show the truth table, logic circuit, and Verilog code.
Design an 8-bit adder. Show the truth table, logic circuit, and Verilog code.
(7494) Design and implementation of 4 bit shift register using Behavioral style of modeling .... by...
(7494) Design and implementation of 4 bit shift register using Behavioral style of modeling .... by VHDL .. the program should based on the data sheet of 7494 and the truth table ... 13 input and one output D.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT