Monday, 7 November 2016

5. Verilog code for Half Adder ( Different moduling style).

//Verilog code for Half Adder
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: RED_BLUE
// Engineer:
//
// Create Date:    21:46:02 11/04/2016
// Designer Name: Madhu Krishna
// Module Name:    half_adder
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module half_adder_code(input_A,
                                         input_B,
                                    sum,
                                    carry);

//INPUTS

  input  input_A;
  input  input_B;

//OUTPUTS

  output  sum;
  output  carry;
 
  reg  sum;
  reg  carry;

//Declaring the sum and carry value
  initial begin

    assign   sum = (input_A) ^ (input_B);
    assign carry = (input_A) & (input_B);
  end
endmodule

------------------------------------------------------------------------------------------------------------------------------------------------------------------

// Verilog code for Half Adder structural
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: RED BLUE
// Engineer:
//
// Create Date:    00:30:18 11/07/2016
// Designer Name:  Madhu Krishna
// Module Name:    half_adder_str
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module half_adder_str( input_a,
                                       input_b,
                                   sum,
                                   carry);


 // INPUTS

    input    input_a;
    input    input_b;

 // OUTPUT
    output   sum;
    output   carry;

 // AND GATE instantiation template
   and_gate_code U_AND_GATE
(
     .input_a (input_a),
     .input_b (input_b),
     .output_y(carry)
    );

 //XOR GATE instantiation template
   xor_gate_code U_XOR_GATE
(
     .input_a (input_a),
     .input_b (input_b),
     .output_y(sum)
    );

endmodule

----------------------------------------------------------------------------------------------------------------------------------------------------------

//Verilog code for Half Adder behavioural
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: RED-BULE
// Engineer:
//
// Create Date:    08:48:15 11/07/2016
// Designer Name:  Madhu Krishna
// Module Name:    half_adder_beh
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module  half_adder_beh(input_a,
                       input_b,
                       sum,
                       carry);

//INPUTS
  input  input_a;
  input  input_b;

 // OUTPUTs
  output  sum;
  output  carry;


  wire  [1:0] co_out;
  reg  sum;
  reg  carry;

  // Declaration of Half Adder

  assign  co_out = {input_a,input_b};

  always @(*) begin
    if(co_out  ==  2'b00) begin
  sum   <= 1'b0;
   carry <= 1'b0;
end else if (co_out == 2'b01) begin
           sum   <= 1'b1;
           carry <= 1'b0;
    end else if (co_out == 2'b10) begin
          sum   <= 1'b1;
          carry <= 1'b0;
    end else begin
         sum   <= 1'b0;
        carry <= 1'b1;
    end
  end

endmodule
-------------------------------------------------------------------------------------------------------------------------------------------------------------


// Vrilog code for Half Adder Behavioural using case
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: RED-BLUE
// Engineer:
//
// Create Date:    10:37:31 11/07/2016
// Designer Name:  Madhu Krishna
// Module Name:    half_adder_beh2
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module  half_adder_beh2( input_a,
                                           input_b,
   sum,
   carry);
  //INPUTS
     input   input_a;
     input   input_b;
 
  //OUTPUTS
     output   sum;
     output  carry;

     reg  sum;
     reg  carry;
     wire  [1:0] con_out;

  // half adder using case
   assign con_out  = {input_a,input_b};

  always @(*) begin
    case (con_out)
 2'b00: begin
     sum  = 1'b0;
   carry = 1'b0;
 end

 2'b01: begin
   sum   = 1'b1;
    carry = 1'b0;
 end

          2'b10: begin
   sum   = 1'b1;
    carry  = 1'b0;
 end

 2'b11: begin
   sum   = 1'b0;
    carry = 1'b1;
 end

 default: begin
            sum   = 1'bz;
    carry = 1'bz ;
 end
    endcase
  end
endmodule

---------------------------------------------------------------------------------------------------------------------------------------------------------------------


No comments:

Post a Comment