Python

Python으로 verilog 회로와 TestBench에서 시뮬레이션 돌려보기

BrianaAI 2025. 7. 8. 21:15

 

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