728x90

 0. [verilog] - 4비트 전가산기



 1. 풀이


2가지 방법을 활용해서 구현해보았다.

첫 번째는 module instance 라는 방식인데, c언어에서 한번 만든 함수를 재활용하는 것이라 생각하면 된다.

여기서는 half_adder를 만들어 full_adder를 구현하는데 활용되는 것을 볼 수 있다.


두 번째는 behavior방식으로 구현하는 것이다.

carry와 sum에 원하는 값이 나오도록 논리를 만들어주었다.



 2. 소스코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    12:31:39 09/25/2019 
// Design Name: 
// Module Name:    full_adder 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module half_adder(A,B,S,C);
input A, B;
output S, C;
 
reg S, C;
 
always @ (A,B)
begin
    if(A && B)
    begin
        C = 1'b1;
        S = 1'b0;
    end
    else if(A || B)
    begin
        C = 1'b0;
        S = 1'b1;
    end
    else
    begin
        C = 1'b0;
        S = 1'b0;
    end
 
end
 
endmodule
 
 
module full_adder(A,B,Cin, Cout, S);
    input A, B, Cin;
    output S, Cout;
    
    //module instantiation
//    wire w1, w2, w3;
//    half_adder U1 (A, B, w1, w2);
//    half_adder U2 (w1, Cin, S, w3);
//    assign Cout = w2|w3;
    
    //Behavioral level modeling
    reg S, Cout;
    always @ (A,B,Cin)
    begin
        if(A&|| A&Cin || B&Cin)
            Cout = 1'b1;
        else
            Cout = 1'b0;
        if(A^B^Cin)
            S = 1'b1;
        else
            S = 1'b0;
        
    end
    
 
endmodule
cs


 3. 참고




질문이나 지적 있으시면 댓글로 남겨주세요~

도움 되셨으면 하트 꾹!


+ Recent posts