1.3 Verilog 环境搭建
介绍常见 FPGA/ASIC 仿真工具链选型,并详细梳理 Windows 64 位环境下 Quartus II 与 ModelSim 安装配置、counter10 工程创建及 .vt 测试平台联合仿真全流程。
学完这一章,你能够
- 介绍常见 FPGA/ASIC 仿真工具链选型,并详细梳理 Windows 64 位环境下 Quartus II 与 ModelSim 安装配置、counter10 工程创建及 .vt 测试平台联合仿真全流程。
主流 Verilog 开发与仿真工具选型
学习与工程开发 Verilog 时可按目标平台选择不同工具链:FPGA 方向常见 Xilinx ISE(已停止维护)、AMD/Xilinx Vivado 与 Intel Quartus II / Prime;ASIC 方向广泛使用 Synopsys VCS 等商业仿真器;而轻量级学习与持续集成环境常采用开源的 Icarus Verilog(`iverilog`/`vvp`)搭配 GTKWave 查看波形。由于 ISE 或 Quartus II 内置仿真器功能相对受限,教学与工程实践通常以 64 位 Windows 下的 Quartus II + ModelSim 联合仿真作为典型演示流程。
Quartus II、器件库与 ModelSim 安装要点
历史教程以 Quartus II 10.1 为例说明安装流程;当前 Intel 官网已下架 13.1 之前的旧版安装包,建议安装 13.1 及以上版本(界面与核心流程基本一致),且在官网下载 Quartus II 时会同步提供匹配版本的 ModelSim 安装包。安装向导中只需按需调整目标路径(避免中文与空格),其余步骤可保持默认直至完成。
若启动时弹出 License file 提示,需指定合法授权文件;若涉及本地网卡绑定配置,可将授权文件中的 `HOSTID` 字段替换为本机 NIC 选项显示的对应物理网卡 ID。此外,旧版 Quartus II(如 10.1)安装主程序后必须单独安装器件库(Device),且安装路径需指向 Quartus II 根目录以供自动识别,否则无法正常建工程;而 2016 及之后的较新版本已整合为一体化安装流程。
ModelSim 侧(例如 `modelsim-win64-10.1c-se` 或对应 Quartus 配套版本)同样只需设定英文安装路径并按默认选项完成安装;安装结束后若系统提示重启,应按提示重启以使环境变量与驱动注册生效。
创建 Quartus II 工程与编写 counter10.v
在 Quartus II 中通过 `File -> New Project Wizard` 启动新建工程向导,依次设定工作目录、工程名以及顶层模块名(Top-level design entity),务必注意路径与名称均不得包含中文字符。若当前仅做功能仿真而不涉及硬件下载烧录,在器件型号(Device)页面任意选定一款已安装器件后连续点击 `Next` 至 `Finish` 即可。
接下来以 4 位十进制计数器 `counter10` 为例创建设计文件:点击 `File -> New -> Verilog HDL File -> OK`,再执行 `File -> Save As` 保存为 `counter10.v`。关键约束在于顶层 `module` 名称必须与工程名称严格一致,否则编译会报错。填入 RTL 源码后点击一键编译(涵盖分析综合与布局布线),若出现错误可通过 `Error log` 逐条定位修正直至零 Error。
配置 ModelSim 路径、编写 .vt 测试平台与联合仿真
为了让 Quartus II 自动拉起 ModelSim,先在 `Tools -> Options -> EDA Tool Options` 中将 ModelSim 路径指向本机 ModelSim 可执行程序所在目录;随后进入 `Assignments -> Simulation`(或 Settings 下的 EDA Tool Settings -> Simulation),将 Tool name 设为 `ModelSim`(或 ModelSim-Altera),并按需配置网表格式(Verilog HDL)与时间精度(Time scale)。
接着通过 `Processing -> Start -> Start Test Bench Template Writer` 自动生成测试平台模板;配置正确时会在工程下的 `simulation/modelsim` 目录生成 `.vt` 模板文件。该模板已自动声明被测模块端口对应变量并完成例化映射,开发者只需在模板中补充时钟翻转、复位释放及其他输入激励逻辑。
完成 `.vt` 编写后,打开 `Assignments -> Settings -> Simulation`,在 `Compile test bench` 栏新建配置项,填写 Test bench name 并关联上述 `.vt` 文件。务必保持 `.vt` 文件名与其中顶层测试模块名一致,否则拉起 ModelSim 时会报找不到顶层模块。添加完毕后 `Tasks` 窗口的编译状态会变为问号,需重新执行一次工程编译以同步设置。
最后点击 `Tools -> Run Simulation Tool -> RTL Simulation`,Quartus II 会自动调用 ModelSim 编译设计及测试平台并展开仿真波形窗口。观察波形即可确认 `counter10` 在复位释放后按 0 至 9 循环递增并在计满 9 时拉高进位输出,验证十进制计数逻辑正确。
联合仿真实践小结
Quartus II 与 ModelSim 联合仿真虽然初次配置涉及路径、顶层命名与测试平台关联等诸多细节,但在中大型数字模块工程中具备极高的调试与自动化回归价值。后续章节的部分演示可能采用其他轻量仿真工具,界面样式虽与 ModelSim 不同,但只要遵循相同的 IEEE 1364 仿真语义与给出的设计/测试源码,仿真逻辑结果保持一致;本站离线脚本亦对 `counter10` 行为与命名规范进行了确定性校验。
离线校验 Quartus II 工程命名约束、counter10 计数仿真与联合仿真 8 步流程
展开查看收起
"""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()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
第一是工程名称(Project Name)必须与设计源文件中的顶层 `module` 名一致;第二是测试平台 `.vt` 文件配置中的 Test bench 顶层名必须与 `.vt` 文件内实际声明的 `module` 名一致,且工程路径不能包含中文字符。