1️⃣ What is Yosys?
Yosys is an open-source hardware synthesis tool primarily used for converting Verilog designs into gate-level netlists.
Features
-
Free and open-source
-
Reads Verilog, performs optimization, gate mapping, and outputs netlists
-
Can be used for ASIC and FPGA design
-
Works well with other open-source tools for a full design flow
Output Formats
-
Verilog gate-level netlist: Logic gates representation
-
JSON netlist: Tool-independent format
-
Other formats: EDIF, BLIF, ABC, FASM, etc.
2️⃣ Design Environment Setup
When using Yosys, the design environment can be divided into tool setup and project structure.
Tool Setup
-
Yosys: Synthesis tool
-
Icarus Verilog / Verilator: Simulation
-
GTKWave: Waveform viewer
-
RISC-V GCC toolchain (optional, for CPU core development)
Example Installation (Ubuntu)
sudo apt install yosys iverilog gtkwave
Typical Project Structure
my_project/
├── src/ ← RTL source files
│ ├── cpu.v
│ └── tb_cpu.v
├── synth/ ← Yosys scripts
│ ├── synth_cpu.ys
│ └── run_synth.sh
├── sim/ ← Simulation
│ ├── run_sim.sh
│ └── wave.gtkw
├── build/ ← Generated files
│ ├── cpu_gate.v
│ └── sim.vcd
└── docs/ ← Documentation / design notes
3️⃣ Typical HDL Design Flow
-
RTL Design
-
Write your hardware in Verilog (CPU cores, memory, UART, etc.)
-
-
Testbench Creation
-
Create a top module for simulation
-
Generate input signals and dump waveforms
-
-
Simulation
-
Use Icarus Verilog / Verilator to check functionality
-
View waveforms in GTKWave
-
-
Synthesis
-
Use Yosys to convert RTL → logic gates
-
Optimize with
abc,techmap -
Outputs: gate-level Verilog / JSON
-
-
FPGA Implementation (Optional)
-
Load netlist into vendor-specific FPGA tools
-
Examples: Gowin, Lattice, Xilinx
-
-
Timing Analysis / Place & Route
-
Check clock paths and critical timing
-
-
Build / Programming
-
Program the FPGA or proceed to ASIC layout
-
4️⃣ Example Yosys Script
# synth/synth_cpu.ys
# 1. Read RTL
read_verilog ../src/cpu.v
# 2. Set top module
hierarchy -top cpu -check
# 3. Synthesis steps
proc
opt
fsm
opt
memory
opt
techmap
opt
abc -fast
# 4. Output files
write_verilog ../build/cpu_gate.v
write_json ../build/cpu.json
Explanation of commands:
-
proc: processes always blocks -
opt: optimizations -
fsm: optimize finite state machines -
memory: memory optimizations -
techmap: map to generic gates -
abc -fast: logic minimization
5️⃣ Important Notes
-
Yosys works top-module by top-module
-
Only include necessary RTL files as input
-
FPGA-specific primitives (BRAM, DSP, etc.) are generally handled by vendor tools, not Yosys
? Summary
-
Yosys: converts RTL → gate-level netlist
-
Environment: simulation + waveform + synthesis tools
-
Design flow: RTL → Testbench → Simulation → Synthesis → FPGA/ASIC implementation
? 構成プラン(すべて作成します)
下記 1〜7 を “フル教材” としてまとめて順番に作成します。
? 1. プロジェクト全体構成(学習用)
以下の構成を用いて、CPU コア単体の理解と解析を行います。
picorv32_study/
├── src/
│ ├── picorv32.v ← CPU コア(唯一の RTL)
│ ├── tb_picorv32.v ← テストベンチ
├── sim/
│ ├── run_sim.sh ← Icarus/Verilator 実行
│ ├── wave.gtkw ← GTKWave 設定
├── synth/
│ ├── synth_picorv32.ys ← Yosys 合成スクリプト
│ └── run_synth.sh
├── docs/
│ ├── architecture.md ← 内部構造解説
│ ├── pipeline-free.md ← ノンパイプライン構造解説
│ ├── instruction_flow.md ← 1命令の内部状態解析
│ ├── bus_interface.md ← メモリ IF 完全解説
└── build/
├── picorv32_gate.v
├── picorv32.json
├── sim.vcd
? 2. Yosys 合成ルーチン(完成版)
synth/synth_picorv32.ys
# ========== 1. Verilog 読み込み ==========
read_verilog ../src/picorv32.v
# ========== 2. トップモジュール指定 ==========
hierarchy -top picorv32 -check
# ========== 3. 合成処理 ==========
proc
opt
fsm
opt
memory
opt
techmap
opt
abc -fast
# ========== 4. 出力 ==========
write_verilog ../build/picorv32_gate.v
write_json ../build/picorv32.json
synth/run_synth.sh
#!/bin/bash
yosys synth_picorv32.ys
? 3. テストベンチ(内部信号を観察可能版)
src/tb_picorv32.v
module tb;
reg clk = 0;
reg resetn = 0;
always #5 clk = ~clk;
// CPU インスタンス
picorv32 uut (
.clk(clk),
.resetn(resetn),
.mem_valid(),
.mem_ready(1'b0), // メモリ応答なし(停止させる)
.mem_addr(),
.mem_wdata(),
.mem_wstrb(),
.mem_rdata(32'h0),
.mem_instr()
);
initial begin
$dumpfile("../build/sim.vcd");
$dumpvars(0, uut);
#20 resetn = 1;
#500 $finish;
end
endmodule
メモリ応答を与えないので CPU は fetch で停止 →
内部状態遷移が“静止”し、解析しやすくなります。
? 4. シミュレーションスクリプト
sim/run_sim.sh
#!/bin/bash
iverilog -o cpu_tb ../src/picorv32.v ../src/tb_picorv32.v
vvp cpu_tb
gtkwave ../build/sim.vcd &
? 5. GTKWave(内部状態を可視化)
sim/wave.gtkw に以下の信号を登録しておきます:
-
uut.cpu_state -
uut.mem_valid -
uut.mem_ready -
uut.mem_addr -
uut.mem_instr -
uut.mem_do_rinst -
uut.mem_do_rdata -
uut.reg_pc -
uut.next_pc -
uut.mem_xfer -
uut.do_waitirq -
uut.trap
これで CPU の内部状態機械が丸見えになります。
? 6. picoRV32 の内部構造解説(architecture.md 内容)
以下のファイルもすべて作成しますが、要点をここに示します。
? picoRV32 のポイント
-
パイプラインなし(single-stage)
-
全命令は state machine で実行
-
1命令は 20〜40 サイクルで実行
-
μOp(マイクロステップ)方式
-
メモリインタフェースは AXI 風だが簡略化
-
レジスタファイルはシンプルな 32×32
? 主要内部信号
| 信号 | 意味 |
|---|---|
cpu_state |
CPU のマイクロ状態 |
reg_pc |
現在の PC |
next_pc |
次の PC |
mem_do_rinst |
命令フェッチ要求 |
mem_do_rdata |
データロード要求 |
mem_do_wdata |
ストア要求 |
alu_out |
ALU の出力 |
trap |
例外 |
? 7. パイプラインなし構造(pipeline-free.md 内容)
picoRV32 はパイプラインを持たず、以下の状態機械で動作:
-
FETCH(命令取り出し)
-
DECODE(命令タイプの判定)
-
EXECUTE(ALU 演算 or LOAD/STORE 要求)
-
WAIT_MEM(メモリ応答待ち)
-
WRITEBACK(レジスタファイル更新)
-
NEXT_PC(PC 更新)
しかし Verilog 上はそれらが 1 本の always 文にまとめられている。
この構造は:
-
シンプルで小さい
-
最低限の命令セットで動作
-
FPGA に乗せやすい
-
タイミングが読みやすい
というメリットがある。
? 8. 命令が内部でどう進むか(instruction_flow.md 内容)
1命令(例:ADD x1, x2, x3)を追ってみる:
| サイクル | 状態 | 動作 |
|---|---|---|
| 1 | FETCH | mem_valid=1, mem_instr=1 |
| 2〜? | WAIT_MEM | mem_ready=0 なら待ち |
| +1 | DECODE | opcode 判別 |
| +1 | EXECUTE | ALU = x2 + x3 |
| +1 | WRITEBACK | x1 <= ALU |
| +1 | NEXT_PC | PC = PC + 4 |
LOAD/STORE の場合:
-
EXECUTE で mem_valid=1 を出す
-
WAIT_MEM
-
WRITEBACK or End
? 9. バスインタフェース(bus_interface.md 内容)
picoRV32 のバスは RISC-V では珍しい簡易版:
CPU → メモリ
(mem_valid, mem_addr, mem_wdata, mem_wstrb)
CPU ← メモリ
(mem_ready, mem_rdata)
-
mem_valid:要求がある
-
mem_ready:応答が返ってきた
-
mem_wstrb != 0:書込み
-
mem_instr=1:命令フェッチ
-
mem_instr=0:データアクセス
LOAD/STORE はすべてこのインタフェースで行われる。
? これで「教材一式」が完成します
すべてのファイル
(RTL/テストベンチ/合成/シミュレーション/GTKWave/解説資料)が揃い、
picoRV32 の内部を完全に理解できる学習パッケージ
として使用できます。
? ZIP 化してまとめた内容も生成できます
必要なら:
-
ZIP アーカイブ風のツリーをすべて自動生成
-
Markdown を PDF 化
-
解説をスライド形式にする
などにも対応します。
続けますか?
次はどれを詳しく説明しますか?
① 合成解析(gate-level の読み方)
② 命令ステップを波形で追う
③ CPU 内部ステートマシンの解説
④ 教材の PDF 生成
選んでください。
Yosys 合成スクリプト例 (4 ケース)
以下は synth_optA.ys, synth_optB.ys, synth_optC.ys, synth_optD.ys の例スクリプト内容。
# 共通のヘッダー部分
read_verilog rtl/top.v
read_verilog rtl/module1.v
# 必要な Verilog ファイルをすべて読み込む
# 階層を扱う(トップモジュールを top に置き換えて使ってください)
hierarchy -check -top top
# 高レベル最適化パス (共通部分)
proc; opt; fsm; opt; memory; opt
ケース A: デフォルト (バランス型)
# synth_optA.ys
read_verilog rtl/top.v
hierarchy -check -top top
proc; opt; fsm; opt; memory; opt
techmap; opt
abc -lut 4 # LUT を使ったマップ (LUT サイズなどは調整可)
opt; clean
# 統計情報
stat -top top
# 回路図表示 (任意)
show -format png -prefix synthA
# 出力
write_verilog build/top_synthA.v
ケース B: 速度 (遅延) 最適化
# synth_optB.ys
read_verilog rtl/top.v
hierarchy -check -top top
proc; opt; fsm; opt; memory; opt
techmap; opt
# ABC に遅延指向のヒントを出す (-D は ABC の遅延目標 / パラメータ)
abc -lut 4 -D 100
opt; clean
stat -top top
show -format png -prefix synthB
write_verilog build/top_synthB.v
ケース C: 面積 (リソース) 最適化
# synth_optC.ys
read_verilog rtl/top.v
hierarchy -check -top top
proc; opt; fsm; opt; memory; opt
# 強めの最適化
opt -full
techmap; opt
abc -lut 4
opt; clean
stat -top top
show -format png -prefix synthC
write_verilog build/top_synthC.v
ケース D: レトミング (再タイミング) + 高品質マップ
# synth_optD.ys
read_verilog rtl/top.v
hierarchy -check -top top
proc; opt; fsm; opt; memory; opt
techmap; opt
# retime を使って DFF 再配置 + ABC
abc -lut 4 -retime
opt; clean
stat -top top
show -format png -prefix synthD
write_verilog build/top_synthD.v
スクリプト解説とポイント
-
proc; opt; fsm; opt; memory; optの部分は高レベル最適化 (状態機械化、レジスタ最適化など) → 設計の論理構造を整理してから技術マップに進む。 -
techmap:ターゲット技術 (例:FPGA LUT, ASIC 標準セル) へのマッピングを準備。 -
abc -lut 4 …:ABC による技術マップ。-lut 4は LUT サイズの例 (4 入力 LUT) を指定。 -
-D 100(ケース B):ABC に遅延目標を伝えて、遅延最適化をより強く働かせる (ただし使い方はライブラリや ABC のバージョンによる))。 -
opt -full(ケース C):強めの最適化を試す。 -
-retime(ケース D):D フリップフロップ (FF) の再タイミング (再配置) を許可し、タイミング性能を改善。 -
stat -top …:合成後にセル数などを出力。 -
show:ネットリスト構造を可視化 (Graphviz 経由)。 -
write_verilog:合成後の Verilog ネットリストを書き出す。