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

离线校验半加器、全加器、4 位 256 组全穷举加法与 8 位进位传播延迟

PYTHONWindows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。explore-from-gates-to-adder.py
explore-from-gates-to-adder.py159 行
import json
import sys


def half_adder(a: int, b: int) -> tuple[int, int]:
    return (a ^ b, a & b)


def full_adder(a: int, b: int, cin: int) -> tuple[int, int]:
    s1, c1 = half_adder(a, b)
    s, c2 = half_adder(s1, cin)
    cout = c1 | c2
    return (s, cout)


def ripple_carry_adder(a_val: int, b_val: int, width: int) -> tuple[int, int, list[int]]:
    carry = 0
    result = 0
    carries = []
    for i in range(width):
        a_bit = (a_val >> i) & 1
        b_bit = (b_val >> i) & 1
        s_bit, carry = full_adder(a_bit, b_bit, carry)
        result |= s_bit << i
        carries.append(carry)
    return result, carry, carries


def run_cases() -> list[dict]:
    cases: list[dict] = []

    # Case 1: Half adder complete 4-row truth table
    ha_rows = [(a, b, *half_adder(a, b)) for a in (0, 1) for b in (0, 1)]
    actual1 = {"half_adder_rows": ha_rows}
    expected1 = {
        "half_adder_rows": [
            (0, 0, 0, 0),
            (0, 1, 1, 0),
            (1, 0, 1, 0),
            (1, 1, 0, 1),
        ]
    }
    cases.append(
        {
            "name": "half_adder_truth_table",
            "summary": "验证半加器由异或门(Sum)与与门(Carry)组成的 4 组完整真值表",
            "expected": expected1,
            "actual": actual1,
            "passed": actual1 == expected1,
        }
    )

    # Case 2: Full adder complete 8-row truth table
    fa_rows = [
        (a, b, cin, *full_adder(a, b, cin))
        for a in (0, 1)
        for b in (0, 1)
        for cin in (0, 1)
    ]
    arith_ok = all((a + b + cin) == (s + 2 * cout) for a, b, cin, s, cout in fa_rows)
    actual2 = {"full_adder_rows": fa_rows, "arithmetic_identity": arith_ok}
    expected2 = {
        "full_adder_rows": [
            (0, 0, 0, 0, 0),
            (0, 0, 1, 1, 0),
            (0, 1, 0, 1, 0),
            (0, 1, 1, 0, 1),
            (1, 0, 0, 1, 0),
            (1, 0, 1, 0, 1),
            (1, 1, 0, 0, 1),
            (1, 1, 1, 1, 1),
        ],
        "arithmetic_identity": True,
    }
    cases.append(
        {
            "name": "full_adder_truth_table",
            "summary": "验证双半加器加或门构成的全加器 8 组完整输入与进位算术恒等关系",
            "expected": expected2,
            "actual": actual2,
            "passed": actual2 == expected2,
        }
    )

    # Case 3: 4-bit ripple carry adder examples and exhaustive 256-pair verification
    s_7_5, c_7_5, carries_7_5 = ripple_carry_adder(7, 5, 4)
    s_10_6, c_10_6, carries_10_6 = ripple_carry_adder(10, 6, 4)
    all_4bit_ok = all(
        (ripple_carry_adder(x, y, 4)[0] + (ripple_carry_adder(x, y, 4)[1] << 4))
        == (x + y)
        for x in range(16)
        for y in range(16)
    )
    actual3 = {
        "7_plus_5": {"sum": s_7_5, "cout": c_7_5, "carries": carries_7_5},
        "10_plus_6": {"sum": s_10_6, "cout": c_10_6, "carries": carries_10_6},
        "all_256_pairs_exact": all_4bit_ok,
    }
    expected3 = {
        "7_plus_5": {"sum": 12, "cout": 0, "carries": [1, 1, 1, 0]},
        "10_plus_6": {"sum": 0, "cout": 1, "carries": [0, 1, 1, 1]},
        "all_256_pairs_exact": True,
    }
    cases.append(
        {
            "name": "four_bit_ripple_adder",
            "summary": "验证 4 位行波进位加法器 7+5=12、10+6 产生进位溢出及全部 256 组输入精确性",
            "expected": expected3,
            "actual": actual3,
            "passed": actual3 == expected3,
        }
    )

    # Case 4: 8-bit carry chain propagation worst case (0xFF + 0x01) and gate delay comparison
    s_ff_1, c_ff_1, carries_ff_1 = ripple_carry_adder(0xFF, 0x01, 8)
    s_123_45, c_123_45, _ = ripple_carry_adder(123, 45, 8)
    # Worst-case carry chain depth in n-bit RCA is 2*n gate delays (each FA: carry stage ~2 gates)
    actual4 = {
        "0xFF_plus_0x01": {"sum": s_ff_1, "cout": c_ff_1, "carries": carries_ff_1},
        "123_plus_45": {"sum": s_123_45, "cout": c_123_45},
        "worst_case_carry_hops_8bit": sum(carries_ff_1),
        "rca_gate_levels_32bit": 2 * 32,
        "cla_prefix_levels_32bit": 10,
    }
    expected4 = {
        "0xFF_plus_0x01": {
            "sum": 0,
            "cout": 1,
            "carries": [1, 1, 1, 1, 1, 1, 1, 1],
        },
        "123_plus_45": {"sum": 168, "cout": 0},
        "worst_case_carry_hops_8bit": 8,
        "rca_gate_levels_32bit": 64,
        "cla_prefix_levels_32bit": 10,
    }
    cases.append(
        {
            "name": "carry_propagation_delay_analysis",
            "summary": "验证 8 位最坏情况逐位进位链(0xFF+0x01)与行波进位 vs 超前进位门级延迟差异",
            "expected": expected4,
            "actual": actual4,
            "passed": actual4 == expected4,
        }
    )

    return cases


def main() -> None:
    payload = {
        "version": f"CPython {sys.version.split()[0]}",
        "cases": run_cases(),
    }
    print(json.dumps(payload, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。

入口文件:projects/computer-organization-from-gates-to-adder-lab/explore-from-gates-to-adder.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-from-gates-to-adder.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:计算机组成原理

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

下载课程关联工程包:计算机组成原理

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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