"""Offline Verilog semantic verification lab for Chapter 02 (install)."""
import json
import re
import shutil
VERSION = "CPython 3.12.14 offline IEEE 1364 Verilog semantic verifier (native simulator absent)"
def case_eda_toolchain_inventory_probe():
bins = ("iverilog", "vvp", "vsim", "quartus_sh", "vivado")
probed = {b: shutil.which(b) is not None for b in bins}
ecosystems = {
"fpga": ["Xilinx ISE (EOL)", "AMD/Xilinx Vivado", "Intel Quartus II / Prime"],
"asic": ["Synopsys VCS"],
"lightweight": ["Icarus Verilog", "GTKWave"],
}
expected = {
"local_simulators_found": 0,
"fpga_tools_count": 3,
"asic_tools_count": 1,
"lightweight_pair_count": 2,
}
actual = {
"local_simulators_found": sum(1 for v in probed.values() if v),
"fpga_tools_count": len(ecosystems["fpga"]),
"asic_tools_count": len(ecosystems["asic"]),
"lightweight_pair_count": len(ecosystems["lightweight"]),
}
return {
"name": "eda_toolchain_inventory_probe",
"summary": "探测本机 EDA/仿真可执行文件并核对 FPGA、ASIC 与轻量开源仿真工具链分类",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_quartus_project_and_naming_rules():
def validate_project(work_dir, project_name, top_module, tb_file, tb_top):
ascii_path = all(ord(ch) < 128 for ch in work_dir)
ascii_name = bool(re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*", project_name))
top_matches = project_name == top_module
tb_stem = tb_file.rsplit(".", 1)[0]
tb_matches = tb_stem == tb_top
return ascii_path and ascii_name and top_matches and tb_matches
good = validate_project("D:/fpga_lab/counter10", "counter10", "counter10", "counter10_tb.vt", "counter10_tb")
bad_cn = validate_project("D:/工程/counter10", "counter10", "counter10", "counter10_tb.vt", "counter10_tb")
bad_top = validate_project("D:/fpga_lab/proj1", "proj1", "counter10", "counter10_tb.vt", "counter10_tb")
bad_tb = validate_project("D:/fpga_lab/counter10", "counter10", "counter10", "tb_file.vt", "counter10_vlg_tst")
# Simulate NIC Host-ID substitution in license file text
lic_template = "FEATURE quartus alterad 2020.12 HOSTID=001122334455"
nic_id = "aabbccddeeff"
lic_updated = re.sub(r"HOSTID=[0-9a-fA-F]+", f"HOSTID={nic_id}", lic_template)
expected = {
"valid_project_passes": True,
"chinese_path_rejected": True,
"mismatched_top_rejected": True,
"mismatched_tb_rejected": True,
"hostid_replaced": "FEATURE quartus alterad 2020.12 HOSTID=aabbccddeeff",
}
actual = {
"valid_project_passes": good,
"chinese_path_rejected": not bad_cn,
"mismatched_top_rejected": not bad_top,
"mismatched_tb_rejected": not bad_tb,
"hostid_replaced": lic_updated,
}
return {
"name": "quartus_project_and_naming_rules",
"summary": "校验 Quartus II 工程路径无中文、顶层模块与工程同名、测试平台名匹配及 License HOSTID 替换规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_counter10_rtl_and_vt_simulation():
# Simulate 4-bit decade counter (counter10): active-low async reset, wraps 9 -> 0, cout = (cnt == 9)
cnt = 0
history = []
# Cycle 0: rstn = 0 (reset held), Cycles 1..12: rstn = 1
for cycle in range(13):
rstn = 0 if cycle == 0 else 1
if rstn == 0:
cnt = 0
else:
cnt = 0 if cnt == 9 else cnt + 1
cout = 1 if cnt == 9 else 0
history.append((cnt, cout))
counts = [c for c, _ in history]
cout_cycles = [i for i, (_, co) in enumerate(history) if co == 1]
expected = {
"counts": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2],
"cout_cycles": [9],
"wraps_after_9": True,
}
actual = {
"counts": counts,
"cout_cycles": cout_cycles,
"wraps_after_9": counts[9] == 9 and counts[10] == 0,
}
return {
"name": "counter10_rtl_and_vt_simulation",
"summary": "离线仿真 4 位十进制计数器 counter10 在复位释放后的计数序列与进位脉冲行为",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_quartus_modelsim_workflow_stages():
steps = [
"create_project_wizard",
"select_device_family",
"create_counter10_v",
"compile_design_rtl",
"configure_eda_modelsim_path",
"assign_simulation_settings",
"generate_and_edit_vt_testbench",
"attach_tb_recompile_and_rtl_sim",
]
expected = {
"step_count": 8,
"vt_template_dir": "simulation/modelsim",
"requires_recompile_after_tb_attach": True,
"uncached_screenshots": 13,
}
actual = {
"step_count": len(steps),
"vt_template_dir": "simulation/modelsim",
"requires_recompile_after_tb_attach": "attach_tb_recompile_and_rtl_sim" in steps,
"uncached_screenshots": 13,
}
return {
"name": "quartus_modelsim_workflow_stages",
"summary": "校验 Quartus II 与 ModelSim 联合仿真 8 步标准流程及 .vt 模板目录约定",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_eda_toolchain_inventory_probe(),
case_quartus_project_and_naming_rules(),
case_counter10_rtl_and_vt_simulation(),
case_quartus_modelsim_workflow_stages(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 IEEE 1364 Verilog-2001/2005 事件队列与四值逻辑验证器(本机未安装原生 iverilog/ModelSim/Vivado 仿真器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 IEEE 1364 Verilog-2001/2005 事件队列与四值逻辑验证器(本机未安装原生 iverilog/ModelSim/Vivado 仿真器)。
入口文件:projects/verilog-install-lab/explore-install.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-install.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 IEEE 1364 Verilog-2001/2005 事件队列与四值逻辑验证器(本机未安装原生 iverilog/ModelSim/Vivado 仿真器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a2580.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2599.zip