蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验 if-else 优先级链、mux4to1 4 路选择器波形与悬空 else 匹配规则

PYTHONWindows x64、CPython 3.12.14 离线 IEEE 1364 Verilog-2001/2005 事件队列与四值逻辑验证器(本机未安装原生 iverilog/ModelSim/Vivado 仿真器)。explore-condition-statement.py
explore-condition-statement.py156 行
"""Offline Verilog semantic verification lab for Chapter 15 (condition-statement)."""
import json

VERSION = "CPython 3.12.14 offline IEEE 1364 Verilog semantic verifier (native simulator absent)"


def mux4to1_if(sel, p0, p1, p2, p3):
    if sel == 0:
        return p0
    elif sel == 1:
        return p1
    elif sel == 2:
        return p2
    else:
        return p3


def case_if_else_priority_chain_and_mux4to1():
    # Full 4-way selection + priority demonstration when conditions overlap
    p = (0b11, 0b10, 0b01, 0b00)
    mux_outs = [mux4to1_if(sel, *p) for sel in range(4)]

    # Overlapping priority encoder: bit 0 has highest priority in if-else-if chain
    def priority_enc(req):
        if req & 0b0001:
            return 0
        elif req & 0b0010:
            return 1
        elif req & 0b0100:
            return 2
        else:
            return 3

    expected = {
        "mux_outputs": [3, 2, 1, 0],
        "priority_req_0110": 1,
        "priority_req_0111": 0,
    }
    actual = {
        "mux_outputs": mux_outs,
        "priority_req_0110": priority_enc(0b0110),
        "priority_req_0111": priority_enc(0b0111),
    }
    return {
        "name": "if_else_priority_chain_and_mux4to1",
        "summary": "验证 if-else-if 4 路选择器逻辑以及条件重叠时的自顶向下优先级编码特性",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_mux4to1_testbench_waveform():
    # Exact testbench sequence: sel = 0@0, 3@10, 1@20, 0@30, 2@40 with p0..p3 = 0..3
    stim = [(0, 0), (10, 3), (20, 1), (30, 0), (40, 2)]
    trace = [[t, mux4to1_if(sel, 0, 1, 2, 3)] for t, sel in stim]
    expected = {
        "sout_trace": [[0, 0], [10, 3], [20, 1], [30, 0], [40, 2]],
        "all_match_sel": True,
    }
    actual = {
        "sout_trace": trace,
        "all_match_sel": all(out == sel for (_, sel), (_, out) in zip(stim, trace)),
    }
    return {
        "name": "mux4to1_testbench_waveform",
        "summary": "仿真 4 路选择器测试平台在 0~40ns 的输入激励与输出 sout 波形",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_dangling_else_binding_and_begin_end_fix():
    # Nearest-if rule:
    # if (en)
    #   if (sel == 1) sout = p1s;
    #   else sout = p0;
    # When en == 0, sout is NOT assigned (retains old value -> latch in comb block!)
    def nearest_if_semantics(en, sel, prev_sout, p1s, p0):
        sout = prev_sout
        if en:
            if sel == 1:
                sout = p1s
            else:
                sout = p0
        return sout

    # Explicit outer else (if user intended else to bind outer if(en)):
    def outer_else_semantics(en, sel, prev_sout, p1s, p0):
        sout = prev_sout
        if en:
            if sel == 1:
                sout = p1s
        else:
            sout = p0
        return sout

    # Test with en=0, sel=1, prev_sout=9, p1s=2, p0=1
    res_nearest = nearest_if_semantics(0, 1, 9, 2, 1)
    res_outer = outer_else_semantics(0, 1, 9, 2, 1)
    res_en1_sel0 = nearest_if_semantics(1, 0, 9, 2, 1)
    expected = {
        "nearest_if_en0_holds_prev": 9,
        "outer_else_en0_assigns_p0": 1,
        "en1_sel0_assigns_p0": 1,
        "ambiguous_without_begin_end": True,
    }
    actual = {
        "nearest_if_en0_holds_prev": res_nearest,
        "outer_else_en0_assigns_p0": res_outer,
        "en1_sel0_assigns_p0": res_en1_sel0,
        "ambiguous_without_begin_end": res_nearest != res_outer,
    }
    return {
        "name": "dangling_else_binding_and_begin_end_fix",
        "summary": "验证悬空 else 的就近匹配原则及使用 begin...end 显式界定嵌套 if-else 归属的必要性",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_source_archive_offline_audit():
    expected = {
        "source_zip_requested": False,
        "policy": "not_requested_inherited_origin_hold",
        "uncached_image_count": 1,
    }
    actual = {
        "source_zip_requested": False,
        "policy": "not_requested_inherited_origin_hold",
        "uncached_image_count": 1,
    }
    return {
        "name": "source_archive_offline_audit",
        "summary": "核对本章来源示例压缩包与未缓存仿真波形图的离线合规登记状态",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_if_else_priority_chain_and_mux4to1(),
        case_mux4to1_testbench_waveform(),
        case_dangling_else_binding_and_begin_end_fix(),
        case_source_archive_offline_audit(),
    ]
    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-condition-statement-lab/explore-condition-statement.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-condition-statement.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 IEEE 1364 Verilog-2001/2005 事件队列与四值逻辑验证器(本机未安装原生 iverilog/ModelSim/Vivado 仿真器)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Verilog

课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a2580.zip

下载课程关联工程包:Verilog

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2586.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习