MAC에서 테스트해보기.

 

 

1. Brew 설치

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

 

2. 환경변수 설정

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

 

3. icarus-verilog 설치

brew install icarus-verilog

 

4. 설치 완료 확인

iverilog -v

 

5. AND 게이트 모듈 + 테스트벤치 ( example.v )

`timescale 1ns / 1ps

// ✅ AND 게이트 모듈 정의
module and_gate (
    input wire A,
    input wire B,
    output wire OUT
);

assign OUT = A & B;

endmodule

// ✅ Testbench
module tb_and_gate;

reg A;
reg B;
wire OUT;

// DUT (Device Under Test) 인스턴스
and_gate uut (
    .A(A),
    .B(B),
    .OUT(OUT)
);

initial begin
    $display("Time\tA\tB\tOUT");
    $monitor("%0dns\t%b\t%b\t%b", $time, A, B, OUT);

    // 시뮬레이션 입력 시나리오
    A = 0; B = 0; #10;
    A = 0; B = 1; #10;
    A = 1; B = 0; #10;
    A = 1; B = 1; #10;

    $finish;
end

endmodule

 

6. Python 코드 (main.py)

import os

def run_simulation(verilog_file='example.v'):
    compile_cmd = f"iverilog -o sim_out {verilog_file}"
    run_cmd = "vvp sim_out"

    print("🛠️ Verilog 컴파일 중...")
    os.system(compile_cmd)

    print("▶️ 시뮬레이션 실행 중...")
    os.system(run_cmd)

# 실행
run_simulation()

 

7. 결과

 

- Time 아래 0ns, 10ns, 20ns, 30ns 는 시뮬레이션 타임 단위와 정밀도에 의해 결정됩니다.

- 1ns (나노초)

- 1ps: 시간 정밀도(해상도), 1피코초

- 40000 -> 총 시뮬레이션 시간이 40ns라는 뜻

- TestBench에서 #10 씩 4번 진행 -> 총 40ns

 

 

문제: 우리가 자주 사용하는 계산기를 하드웨어로 설계 한다면? 

 

첫 번째, 설계를 하기 전 스펙을 정한다.

더하기, 곱하기, 나누기, 빼기 -> 기본적인 사칙연산 only

 

입력: 두개의 숫자를 입력 받아 (x, y)

연산 결과 튀어나와

 

모듈: 하드웨어에서 동작을 수행하는 핵심적인 부분

 

입력: Input Port

출력: Output Port

 

확장자: .v

파일이름: calculator.v

 

계산기(calculate)

 

>>> Code

module calculator(
    input wire[5:0] x,
    input wire[5:0] y,
    
    output wire[6:0] add,
    output wire[6:0] sub,
    output wire[11:0] mul,
    output wire[5:0] div
);

    assign add = x+y;
    assign sub = x-y;
    assign mul = x*y;
    assign div = x/y;
endmodule

 

- 처음: module

- 끝: endmodule

- wire: 모듈 내부로 연결해주고, 밖으로 나가는 신호도 연결

- 디지털 0, 1로 구성

- 사용할 숫자의 범위

    - 5:0 -> 5~0, 6bit, 2의 6승, 0~63까지 표현, 64개 표현 가능

    - 9:0 -> 9~0, 10bit, 2의 10승, 0~1023까지 표현, 1024개 표현 가능

- assign: 모듈 내부에서 output 포트까지 연결시켜줌.

- assign add = x+y; -> x+y값을 add에 연결시킨다

 

Validation

 

- Test Bench: 값을 넣어서 테스트 하는 것, 디자인 또는 모델의 정확성을 확인하는데 사용하는 환경

- 모듈 이름: tb_calculator.v

module tb_calculator();


reg [5:0] x = 6'd4;
reg [5:0] y = 6'd2;

wire [6:0] add;
wire [6:0] sub;
wire [11:0] mul;
wire [5:0] div;

calculator u_calculator(
    .x ( x ),
    .y ( y ),
    
    .add ( add ),
    .sub ( sub ),
    .mul ( mul ),
    .div ( div )
);

endmodule

 

- 테스트 할 대상 호출 -> calculator

- 유닛을 나타내는 u와 테스트모듈 이름 적어준다.

- .x, .add ... -> . 찍고 포트 이름 적어서 호출

- reg: 인풋에 해당되는 신호

- wire: 아웃풋에 해당되는 신호

- 꼭 6bit로 테스트

- 신호들 매핑 .a->.b (사랑의 작대기)

 

 

 

pip install scipy==1.10.0

 

이렇게 설치하면 동작 됩니다.

라이브러리 버전 충돌 문제 입니다.

 

해당 에러에서 pip install yaml 해도 안되길래 찾아봤다.

 

pip install pyyaml

 

이렇게 하면 된다!

+ Recent posts