LEVEL0 - Adder

정다훈·2025년 6월 23일

Verilog study

목록 보기
6/6

[L0-P06] Adder - Introduction to Examples, About Adder, Problem Solving

Half adder

Full adder

half adder는 올림수를 반영할 수 없음 --> carry만 발생시킴

full adder는 cin이라는 input port를 활용하여 올림수를 반영하여 비트를 계산함

우리가 verilog에서 사용하는 '+'연산은 adder들로 이루어져 있음

my answer

module adder (
    input [3:0] a,    
    input [3:0] b,    
    input cin,        
    output [4:0] sum, 
    output cout       
);

//TODO
wire [3:0] cout_w;

f_adder add1(a[0],b[0],cin,sum[0],cout_w[0]);
f_adder add2(a[1],b[1],cin,sum[1],cout_w[1]);
f_adder add3(a[2],b[2],cin,sum[2],cout_w[2]);
f_adder add4(a[3],b[3],cin,sum[3],cout_w[3]);

assign sum[4] = cout_w[3];
assign cout = cout_w[3];

endmodule


module f_adder(
  input a,
  input b,
  input cin,
  output sum,
  output cout
);

assign sum = (a ^ b) ^ cin;
assign cout = ((a ^ b) & cin) | (a & b);

endmodule

answer

module adder_answer(
  input  [3:0] a,
  input  [3:0] b,
  input        cin,
  output [3:0] sum,
  output cout
);

assign {cout,sum} = a + b + cin;

endmodule

위와 같이 verilog 상에서는 +기호는 adder의 역할을 함

schematic

0개의 댓글