Files
mlkem-sync/create_project.tcl
FallenSigh 0fa7f48ed4 build(vivado): add sd_bram to create_project.tcl, fix run instructions
mlkem_top instantiates sd_bram (ek/dk byte buffers) but create_project.tcl
was missing it -- elaboration would fail. Add sync_rtl/storage/sd_bram.v to
match xsim_run.tcl exactly. Also clarify the run flow: runtime=all means
launch_simulation runs to the TB $finish; a manual 'run all' afterwards trips
the 120ms watchdog and prints a spurious timeout.

Verified: vivado -mode batch builds the project (0 errors) and the simulation
prints 'K=2 CASE 0 PASS: ek==pk, dk==sk'.
2026-06-28 17:14:14 +08:00

125 lines
6.0 KiB
Tcl
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# create_project.tcl — 自动创建 Vivado 工程(仿真用),加载 ML-KEM KeyGen
# 顶层 mlkem_top 及其全部叶子算子与参数化 KAT testbench。
#
# 与已验证的 XSIM 流程sync_rtl/top/TB/xsim_run.tcl保持一致
# - 加载 mlkem_top 实际依赖的全部 RTL 源文件(含 sd_bram 存储)
# - 顶层仿真模块 = tb_mlkem_kg_katK_xsim
# - 运行时安全等级由 generic KP2/3/4选择用例号由 +CASE 选择
#
# Usage:
# cd ~/Dev/mlkem
# vivado -mode batch -source create_project.tcl
# 或在 Vivado Tcl Console 中:
# source create_project.tcl
#
# 切换被仿真的配置(默认 KP=2, CASE=0编辑下方 SIM_KP / SIM_CASE 后重跑,
# 或在工程打开后执行:
# set_property generic "KP=4" [get_filesets sim_1]
# set_property -name {xsim.simulate.xsim.more_options} -value {-testplusarg CASE=2} -objects [get_filesets sim_1]
set PROJECT_NAME mlkem
set PROJECT_DIR [file normalize [file dirname [info script]]]
# 默认仿真配置(可改)
set SIM_KP 2 ;# ML-KEM 方案2=512, 3=768, 4=1024
set SIM_CASE 0 ;# KAT 用例号K=2: 0..4, K=3/4: 0..2
# 仅仿真工程,无需指定 FPGA partXSim 用默认 part 即可)
create_project -force ${PROJECT_NAME} ${PROJECT_DIR}/vivado_prj
set_property target_simulator XSim [current_project]
# ===================================================================
# RTL 源文件 —— 与 xsim_run.tcl 完全一致
# ===================================================================
# ── SHA3 / Keccak ──
read_verilog -sv ${PROJECT_DIR}/sync_rtl/sha3/keccak_round.v
read_verilog -sv ${PROJECT_DIR}/sync_rtl/sha3/keccak_core.v
read_verilog -sv ${PROJECT_DIR}/sync_rtl/sha3/sha3_top_shared.v
# ── 采样(共享 keccak_core 变体)──
read_verilog -sv ${PROJECT_DIR}/sync_rtl/sample_ntt/sample_ntt_sync_shared.v
read_verilog -sv ${PROJECT_DIR}/sync_rtl/sample_cbd/sample_cbd_sync_shared.v
# ── NTT ──
read_verilog -sv ${PROJECT_DIR}/sync_rtl/ntt/barrett_mul.v
read_verilog -sv ${PROJECT_DIR}/sync_rtl/ntt/zeta_rom.v
read_verilog -sv ${PROJECT_DIR}/sync_rtl/ntt/butterfly_unit.v
read_verilog -sv ${PROJECT_DIR}/sync_rtl/ntt/ntt_core.v
# ── 多项式乘法 ──
read_verilog -sv ${PROJECT_DIR}/sync_rtl/poly_mul/basecase_mul.v
read_verilog -sv ${PROJECT_DIR}/sync_rtl/poly_mul/poly_mul_zeta_rom.v
read_verilog -sv ${PROJECT_DIR}/sync_rtl/poly_mul/poly_mul_sync.v
# ── 存储ek/dk 字节缓冲registered-read BRAM──
read_verilog -sv ${PROJECT_DIR}/sync_rtl/storage/sd_bram.v
# ── 顶层 KeyGen 集成 ──
read_verilog -sv ${PROJECT_DIR}/sync_rtl/top/mlkem_top.v
# ── 参数化 KAT testbench ──
read_verilog -sv ${PROJECT_DIR}/sync_rtl/top/TB/tb_mlkem_kg_katK_xsim.v
# ===================================================================
# 仿真设置
# ===================================================================
# 顶层仿真模块
set_property top tb_mlkem_kg_katK_xsim [get_filesets sim_1]
set_property top_lib xil_defaultlib [get_filesets sim_1]
# `include "sync_rtl/common/defines.vh" 的包含路径(相对工程根)
set_property include_dirs ${PROJECT_DIR} [get_filesets sim_1]
# 运行时安全等级:通过 TB 顶层 generic KP 传入2/3/4
set_property generic "KP=${SIM_KP}" [get_filesets sim_1]
# 跑到 $finish 为止显式时标XSim 默认已加 --relax勿重复
set_property -name {xsim.simulate.runtime} -value {all} -objects [get_filesets sim_1]
set_property -name {xsim.elaborate.xelab.more_options} \
-value {--timescale 1ns/1ps} -objects [get_filesets sim_1]
# ===================================================================
# 测试向量Vivado GUI 在 vivado_prj/mlkem.sim/sim_1/behav/xsim/ 下运行
# xsim而 TB 的 $readmemh 路径相对工程根sync_rtl/top/TB/vectors/…)。
# 用 compile 的 pre-hook在 xsim 工作目录、且在 compile/elaborate/simulate
# 之前执行)把整套 KAT 向量复制到同名相对路径下CASE 经 -testplusarg 选择。
#
# 注意2019.2 的 sim_1 没有 simulate.tcl.pre 属性,且追加 -tclbatch 会排在
# Vivado 自带(含 "run all")的 tclbatch 之后、即仿真跑完才执行(太迟)。
# 因此用 xsim.compile.tcl.pre —— 它最早执行且就在仿真工作目录里。
# ===================================================================
set pre_tcl [file join ${PROJECT_DIR} vivado_prj copy_vectors_pre.tcl]
set fp [open $pre_tcl w]
puts $fp "# Auto-generated by create_project.tcl: KAT xsim "
puts $fp "file mkdir sync_rtl/top/TB/vectors"
puts $fp "foreach v \[glob -nocomplain [file join ${PROJECT_DIR} sync_rtl top TB vectors kat_k*_c*_*.hex]\] {"
puts $fp " file copy -force \$v sync_rtl/top/TB/vectors/"
puts $fp "}"
puts $fp "puts {\[create_project\] KAT vectors copied to xsim working dir}"
close $fp
set_property -name {xsim.compile.tcl.pre} -value $pre_tcl -objects [get_filesets sim_1]
set_property -name {xsim.simulate.xsim.more_options} \
-value "-testplusarg CASE=${SIM_CASE}" \
-objects [get_filesets sim_1]
# ===================================================================
puts "========================================"
puts " Project created: ${PROJECT_DIR}/vivado_prj/${PROJECT_NAME}.xpr"
puts " 仿: ML-KEM K=${SIM_KP}, KAT CASE=${SIM_CASE}"
puts ""
puts " 仿runtime=all TB \$finish"
puts " - GUI: Run Simulation Tcl Console: launch_simulation"
puts " - batch: vivado -mode batch -source create_project.tcl "
puts " : open_project vivado_prj/mlkem.xpr; launch_simulation"
puts " launch_simulation run all 仿 \$finish"
puts " run TB 120ms timeout"
puts " : K=${SIM_KP} CASE ${SIM_CASE} PASS: ek==pk, dk==sk"
puts ""
puts " K=4 SIM_KP/SIM_CASE "
puts " generic relaunch_sim"
puts "========================================"