import json
import sys
def gate_and(a: int, b: int) -> int:
return a & b
def gate_or(a: int, b: int) -> int:
return a | b
def gate_not(a: int) -> int:
return 1 - a
def gate_nand(a: int, b: int) -> int:
return 1 - (a & b)
def gate_xor(a: int, b: int) -> int:
return a ^ b
def run_cases() -> list[dict]:
cases: list[dict] = []
# Case 1: 2-input access lock (A AND B) and 3-input majority voter SOP vs simplified
lock_table = [(a, b, gate_and(a, b)) for a in (0, 1) for b in (0, 1)]
# Majority voter: SOP = (~A&B&C)|(A&~B&C)|(A&B&~C)|(A&B&C); simplified = (A&B)|(B&C)|(A&C)
maj_sop = []
maj_simp = []
for a in (0, 1):
for b in (0, 1):
for c in (0, 1):
sop = (
(gate_not(a) & b & c)
| (a & gate_not(b) & c)
| (a & b & gate_not(c))
| (a & b & c)
)
simp = (a & b) | (b & c) | (a & c)
maj_sop.append((a, b, c, sop))
maj_simp.append((a, b, c, simp))
actual1 = {
"lock_outputs": [r[2] for r in lock_table],
"majority_equivalent": maj_sop == maj_simp,
"majority_outputs": [r[3] for r in maj_simp],
"sop_gate_count": {"NOT": 3, "AND3": 4, "OR4": 1, "total": 8},
"simplified_gate_count": {"AND2": 3, "OR3": 1, "total": 4},
}
expected1 = {
"lock_outputs": [0, 0, 0, 1],
"majority_equivalent": True,
"majority_outputs": [0, 0, 0, 1, 0, 1, 1, 1],
"sop_gate_count": {"NOT": 3, "AND3": 4, "OR4": 1, "total": 8},
"simplified_gate_count": {"AND2": 3, "OR3": 1, "total": 4},
}
cases.append(
{
"name": "majority_voter_simplification",
"summary": "验证双输入门禁与三人多数表决器的标准与或式及化简后逻辑等价性与门数缩减",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
}
)
# Case 2: 1-bit magnitude comparator (GT, EQ, LT)
comp_rows = []
for a in (0, 1):
for b in (0, 1):
gt = gate_and(a, gate_not(b))
lt = gate_and(gate_not(a), b)
eq = gate_not(gate_xor(a, b))
comp_rows.append((a, b, gt, eq, lt))
actual2 = {
"rows": comp_rows,
"one_hot_all": all((r[2] + r[3] + r[4]) == 1 for r in comp_rows),
}
expected2 = {
"rows": [(0, 0, 0, 1, 0), (0, 1, 0, 0, 1), (1, 0, 1, 0, 0), (1, 1, 0, 1, 0)],
"one_hot_all": True,
}
cases.append(
{
"name": "one_bit_comparator",
"summary": "验证 1 位二进制数值比较器的三路互斥输出(大于、等于、小于)",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
}
)
# Case 3: 2-to-4 decoder
dec_rows = []
for a1 in (0, 1):
for a0 in (0, 1):
y0 = gate_and(gate_not(a1), gate_not(a0))
y1 = gate_and(gate_not(a1), a0)
y2 = gate_and(a1, gate_not(a0))
y3 = gate_and(a1, a0)
dec_rows.append((a1, a0, [y0, y1, y2, y3]))
actual3 = {
"decoder_outputs": dec_rows,
"unique_active_line": all(sum(r[2]) == 1 for r in dec_rows),
}
expected3 = {
"decoder_outputs": [
(0, 0, [1, 0, 0, 0]),
(0, 1, [0, 1, 0, 0]),
(1, 0, [0, 0, 1, 0]),
(1, 1, [0, 0, 0, 1]),
],
"unique_active_line": True,
}
cases.append(
{
"name": "two_to_four_decoder",
"summary": "验证 2-4 译码器将 2 位二进制地址精准译码为 4 路独热片选信号",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
}
)
# Case 4: NAND functional completeness (NOT, AND, OR, XOR constructed solely from NAND)
nand_not = lambda x: gate_nand(x, x)
nand_and = lambda x, y: nand_not(gate_nand(x, y))
nand_or = lambda x, y: gate_nand(nand_not(x), nand_not(y))
nand_xor = lambda x, y: gate_nand(
gate_nand(x, gate_nand(x, y)), gate_nand(y, gate_nand(x, y))
)
pairs = [(a, b) for a in (0, 1) for b in (0, 1)]
actual4 = {
"not_ok": all(nand_not(a) == gate_not(a) for a in (0, 1)),
"and_ok": all(nand_and(a, b) == gate_and(a, b) for a, b in pairs),
"or_ok": all(nand_or(a, b) == gate_or(a, b) for a, b in pairs),
"xor_ok": all(nand_xor(a, b) == gate_xor(a, b) for a, b in pairs),
"nand_counts": {"NOT": 1, "AND": 2, "OR": 3, "XOR": 4},
}
expected4 = {
"not_ok": True,
"and_ok": True,
"or_ok": True,
"xor_ok": True,
"nand_counts": {"NOT": 1, "AND": 2, "OR": 3, "XOR": 4},
}
cases.append(
{
"name": "nand_universal_gate",
"summary": "验证仅用与非门(NAND)构造非门、与门、或门与异或门的功能完备性",
"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-building-logic-circuits-lab/explore-building-logic-circuits.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-building-logic-circuits.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2618.zip
课程关联工程包(登记关联22章) · 含当前章节或实例 · mbskycom202612a2616.zip