4-bit Adder를 Hierarchical modeling과 Structural modeling을 통해 아래와 같이 구현할 것이다. half adder 2개를 불러와 full adder를 구현하고, full adder 4개를 연결하여 4-bit adder를 구현할 수 있다.

Hierarchical Design of 4-bit Adder

 

1. half adder

 

module half_adder(x,y,s,c);

input x,y;
output s,c;

xor (s,x,y);
and (c,x,y);

endmodule

 

 

2. full adder

 

module full_adder(x,y,c_in,s,c_out);

input x,y,c_in;
output s,c_out;

wire s1,c1,c2;

half_adder ha1 (x,y,s1,c1);
half_adder ha2 (c_in,s1,s,c2);

or (c_out, c2, c1);

endmodule

 

 

3. 4-bit adder

 

module four_bit_adder(x,y,c_in,sum,c_out);

input [3:0] x,y;
input c_in;
output [3:0] sum;
output c_out;

wire c1, c2, c3;

full_adder fa_1 (x[0], y[0], c_in, sum[0], c1);
full_adder fa_2 (x[1], y[1], c1, sum[1], c2);
full_adder fa_3 (x[2], y[2], c2, sum[2], c3);
full_adder fa_4 (x[3], y[3], c3, sum[3], c_out);

endmodule

 

 

4. 시뮬레이션 결과

x = 5, y = 0 - 15, c_in = 0 - 1일 때, 출력값 sum과 c_out이 올바르게 나온것을 확인할 수 있다. 4-bit adder이므로 십진수로 최대 표현할 수 있는 숫자는 15이다. 따라서 5 + 10 + 1 ( a + b + c_in )인 경우 sum = 0, c_out = 1인 것을 확인할 수 있다.

'전자공학 > Verilog' 카테고리의 다른 글

Testbench를 기술하는 방법  (0) 2020.07.20
Full Adder & MUX & Decoder ( Structural Modeling )  (0) 2020.07.17
Structural Modeling  (0) 2020.07.17

Testbench를 만들어 Simulation을 할 수 있다. 똑같은 입력값이어도 사용자에 의해 다르게 기술될 수 있다.

 

아래의 진리표를 가지고 3가지 방법으로 Testbench를 작성할 것이다.

 

1-Bit Full Adder 입력값

 

1. 시간마다 입력값을 정해주기

 

module tb_full_adder;

    reg x, y, c_in;
    wire s, c_out;

    full_adder_SM uut (.x(x), .y(x), .c_in(c_in), .s(s), .c_out(c_out));

    initial begin
    x = 0; y = 0; c_in = 0;
    #20 x = 0; y = 0; c_in = 1;
    #20 x = 0; y = 1; c_in = 0;
    #20 x = 0; y = 1; c_in = 1;
    #20 x = 1; y = 0; c_in = 0;
    #20 x = 1; y = 0; c_in = 1;
    #20 x = 1; y = 1; c_in = 0;
    #20 x = 1; y = 1; c_in = 1;
    end


endmodule

 

 

2. 입력값 반전 이용하기

 

module tb_full_adder;

    reg x, y, c_in;
    wire s, c_out;

    full_adder_SM uut (.x(x), .y(x), .c_in(c_in), .s(s), .c_out(c_out));

    initial begin
        x = 0; y = 0; c_in = 0;
    end

    always #80 x <= ~x;
    always #40 y <= ~y;
    always #20 c_in <= ~ c_in

endmodule

 

 

3. test register 이용하기

 

module tb_full_adder; 

    reg x, y, c_in; 
    wire s, c_out; 

    full_adder_SM uut (.x(x), .y(x), .c_in(c_in), .s(s), .c_out(c_out)); 

    initial begin 
        x = 0; y = 0; c_in = 0; 
    end 

    always #80 x <= ~x; 
    always #40 y <= ~y; 
    always #20 c_in <= ~ c_in 

endmodule

 

"똑같은 입력값이라 해도 다양한 방법으로 Testbench를 기술할 수 있다."

'전자공학 > Verilog' 카테고리의 다른 글

Hierarchical Design of 4-bit Adder  (0) 2020.07.20
Full Adder & MUX & Decoder ( Structural Modeling )  (0) 2020.07.17
Structural Modeling  (0) 2020.07.17

1. 1-bit Full Adder

- gate-level diagram

 

- Verilog code

module

module full_adder_1bit_SM(sum, c_out, x, y, c_in);

input x, y, c_in;

output sum, c_out;

wire s1, c1, c2;

xor xor1 (s1, x, y);

and and1 (c1, x, y);

xor xor2 (sum, c_in, s1);

and and2 (c2, c_in, s1);

or or3 (c_out, c1, c2);

endmodule

testbench

module tb;

    reg x, y, c_in;

    wire sum, c_out;

    

    full_adder_1bit_SM uut (sum, c_out, x, y, c_in);

    

    initial begin

    x = 0; y = 0; c_in = 0;

    

    end

    

    always #80 x <= ~x;

    always #40 y <= ~y;

    always #20 c_in <= ~c_in;

    

endmodule

- Output

 

2. 4-to-1 MUX

- gate-level diagram

 

- Verilog code

module 

module mux4_to_1_SM (i0, i1, i2, i3, s1, s0, out);

    input i0, i1, i2, i3, s1, s0;

    output out;

    wire s1n, s0n;

    wire i0o, i1o, i2o, i3o;

    not not1 (s0n, s0);

    not not2 (s1n, s1);

    and and1 (i0o, i0, s1n, s0n);

    and and2 (i1o, i1, s1n, s0);

    and and3 (i2o, i2, s1, s0n);

    and and4 (i3o, i3, s1, s0);

    or or1 (out, i0o, i1o, i2o, i3o);

endmodule

Testbench

module tb; 

    reg i0, i1, i2, i3, s1, s0; 
    wire out; 
     
    mux4_to_1_SM uut (i0, i1, i2, i3, s1, s0, out); 
     
    initial begin 
    i0 = 0; i1 = 0; i2 = 0; i3 = 0; s1 = 0; s0 = 0; 
     
    #20 i0 = 1; i1 = 0; i2 = 0; i3 = 0; s1 = 0; s0 = 0; 
    #20 i0 = 0; i1 = 1; i2 = 0; i3 = 0; s1 = 0; s0 = 1; 
    #20 i0 = 0; i1 = 0; i2 = 1; i3 = 0; s1 = 1; s0 = 0; 
    #20 i0 = 0; i1 = 0; i2 = 0; i3 = 1; s1 = 1; s0 = 1;  
     
    end 
endmodule

 

- Output

 

3. 2-to-4 Decoder

- gate-level diagram

 

- Verilog code

module

module decoder_2_to_4_SM(D0, D1, D2, D3, EN, A1, A0);

input EN, A1, A0;

output D0, D1, D2, D3;

wire A1n, A0n, N0, N1, N2, N3;

not not1 (A1n, A1),

    not2 (A0n, A0);

and and1 (N0, A1n, A0n),

    and2 (N1, A1n, A0),

    and3 (N2, A1, A0n),

    and4 (N3, A1, A0);

    

and and5 (D0, N0, EN),

    and6 (D1, N1, EN),

    and7 (D2, N2, EN),

    and8 (D3, N3, EN);

    

endmodule

 

- Output

truth table과 동일하게 작동하는 것을 확인할 수 있다.

'전자공학 > Verilog' 카테고리의 다른 글

Hierarchical Design of 4-bit Adder  (0) 2020.07.20
Testbench를 기술하는 방법  (0) 2020.07.20
Structural Modeling  (0) 2020.07.17

Verilog에는 3가지 기법이 있다.

 - Structural Modeling

 - Dataflow Modeling

 - Behavioral Modeling

 

3가지를 각각 활용해도 되고 mixed description하기도 한다.

 

그 중 Structural Modeling은 Gate 단위의 모델링 방법이다. AND, NAND, OR, XOR, Transmission(not, buf) 등 다양한 Gate가 존재한다. Verilog에서는 아래와 같이 선언할 수 있다.

 

 ex) and a1(OUT, IN1, IN2);                    // a1 이름을 가지는 2-input AND gate 

      or a2(OUT, IN1, IN2, IN3)                // a2 이름을 가지는 3-input OR gate

      buffif1 a3(OUT, IN1, Enable_HIGH)    // a3 이름을 가지는 Tri-state buffer 

 

'전자공학 > Verilog' 카테고리의 다른 글

Hierarchical Design of 4-bit Adder  (0) 2020.07.20
Testbench를 기술하는 방법  (0) 2020.07.20
Full Adder & MUX & Decoder ( Structural Modeling )  (0) 2020.07.17

+ Recent posts