"""Offline Verilog RTL verification lab for Chapter 19 (module-port)."""
import json
import shutil
VERSION = "CPython 3.12.14 offline Verilog RTL bit-accurate simulator (iverilog absent)"
def resolve_net(drivers):
"""Resolve IEEE 1364 4-state wire from multiple drivers ('0','1','z','x')."""
active = [d for d in drivers if d != "z"]
if not active:
return "z"
unique = set(active)
if len(unique) == 1:
return active[0]
return "x"
def sim_pad_cell(din, oen, pull, ext_pad_reg):
"""Bit-accurate behavioral model of the bidirectional PAD module."""
# PAD internal driver: assign PAD = OEN ? 'bz : DIN;
drv_internal = "z" if oen == "1" else din
# Testbench external driver: assign PAD = OEN ? PAD_REG : 1'bz;
drv_external = ext_pad_reg if oen == "1" else "z"
# Puller bufif1(PAD, PULL[0], PULL[1]) acting as weak pull when no strong driver
pull_en = (pull >> 1) & 1
pull_val = str(pull & 1)
strong = resolve_net([drv_internal, drv_external])
if strong != "z":
pad_net = strong
else:
pad_net = pull_val if pull_en else "z"
# always @(*) DOUT = (OEN == 1) ? PAD : 'bz;
dout = pad_net if oen == "1" else "z"
return {"PAD": pad_net, "DOUT": dout}
def case_module_port_direction_rules():
"""Verify IEEE 1364 port direction vs internal/external data type rules."""
rules = {
"input": {"internal_allowed": ["wire"], "external_allowed": ["wire", "reg"]},
"output": {"internal_allowed": ["wire", "reg"], "external_allowed": ["wire"]},
"inout": {"internal_allowed": ["wire"], "external_allowed": ["wire"]},
}
implicit_default = "wire"
ansi_vs_non_ansi_equivalent = True
expected = {
"input_internal_wire_only": True,
"output_internal_reg_allowed": True,
"inout_both_wire_only": True,
"implicit_type": "wire",
"ansi_equivalent": True,
}
actual = {
"input_internal_wire_only": rules["input"]["internal_allowed"] == ["wire"],
"output_internal_reg_allowed": "reg" in rules["output"]["internal_allowed"],
"inout_both_wire_only": (
rules["inout"]["internal_allowed"] == ["wire"]
and rules["inout"]["external_allowed"] == ["wire"]
),
"implicit_type": implicit_default,
"ansi_equivalent": ansi_vs_non_ansi_equivalent,
}
return {
"name": "module_port_direction_rules",
"summary": "校验 input/output/inout 端口内部与外部数据类型约束、隐式 wire 规则及 ANSI 风格等价性",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_pad_input_pull_modes():
"""Verify PAD pull-down (2'b10), pull-up (2'b11), and dispull (2'b00) in input mode."""
t0 = sim_pad_cell(din="z", oen="1", pull=0b10, ext_pad_reg="z")
t20 = sim_pad_cell(din="z", oen="1", pull=0b11, ext_pad_reg="z")
t40 = sim_pad_cell(din="z", oen="1", pull=0b00, ext_pad_reg="z")
expected = {
"t0_pulldown": {"PAD": "0", "DOUT": "0"},
"t20_pullup": {"PAD": "1", "DOUT": "1"},
"t40_dispull": {"PAD": "z", "DOUT": "z"},
}
actual = {"t0_pulldown": t0, "t20_pullup": t20, "t40_dispull": t40}
return {
"name": "pad_input_pull_modes",
"summary": "仿真 PAD 输入高阻悬空时 0ns 下拉、20ns 上拉与 40ns 关闭上下拉的三态响应",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_pad_external_drive_and_output_mode():
"""Verify PAD external driving (60ns-100ns) and output driving (OEN=0) transitions."""
t60 = sim_pad_cell(din="z", oen="1", pull=0b00, ext_pad_reg="0")
t80 = sim_pad_cell(din="z", oen="1", pull=0b00, ext_pad_reg="1")
t110 = sim_pad_cell(din="z", oen="0", pull=0b00, ext_pad_reg="1")
t125 = sim_pad_cell(din="0", oen="0", pull=0b00, ext_pad_reg="1")
t140 = sim_pad_cell(din="1", oen="0", pull=0b00, ext_pad_reg="1")
expected = {
"t60_in0": {"PAD": "0", "DOUT": "0"},
"t80_in1": {"PAD": "1", "DOUT": "1"},
"t110_outz": {"PAD": "z", "DOUT": "z"},
"t125_out0": {"PAD": "0", "DOUT": "z"},
"t140_out1": {"PAD": "1", "DOUT": "z"},
}
actual = {
"t60_in0": t60,
"t80_in1": t80,
"t110_outz": t110,
"t125_out0": t125,
"t140_out1": t140,
}
return {
"name": "pad_external_drive_and_output_mode",
"summary": "验证双向 PAD 外部驱动输入(PAD->DOUT)及 OEN=0 输出模式(DIN->PAD、DOUT=z)",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_pad_bus_contention_and_probe():
"""Verify contention ('0' vs '1' -> 'x') and probe native Verilog simulator absence."""
contention = resolve_net(["0", "1"])
agree = resolve_net(["1", "1", "z"])
iverilog_found = shutil.which("iverilog") is not None
expected = {"contention": "x", "agree": "1", "iverilog_found": False}
actual = {"contention": contention, "agree": agree, "iverilog_found": iverilog_found}
return {
"name": "pad_bus_contention_and_probe",
"summary": "验证双向总线双向冲突解析为未知态 x 及本地原生仿真器探测结果",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_module_port_direction_rules(),
case_pad_input_pull_modes(),
case_pad_external_drive_and_output_mode(),
case_pad_bus_contention_and_probe(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Verilog RTL 位精确数字算法与周期仿真模型(本机未安装 iverilog/vvp/verilator/vsim/vivado)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Verilog RTL 位精确数字算法与周期仿真模型(本机未安装 iverilog/vvp/verilator/vsim/vivado)。
入口文件:projects/verilog-module-port-lab/explore-module-port.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-module-port.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Verilog RTL 位精确数字算法与周期仿真模型(本机未安装 iverilog/vvp/verilator/vsim/vivado)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a2580.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2603.zip