"""Offline Computer Organization lab for Chapter 01 (tutorial)."""
import json
VERSION = "CPython 3.12.14 offline computer organization simulation verifier"
def case_computer_system_hierarchy():
system = {
"hardware": {
"CPU": ["ALU", "CU", "Register"],
"Memory": ["RAM", "ROM/Cache"],
"Input": ["Keyboard", "Mouse", "Microphone"],
"Output": ["Monitor", "Printer", "Speaker"],
"Buses": ["Data Bus", "Address Bus", "Control Bus"],
},
"software": ["System Software", "Application Software"],
}
actual = {
"top_layers": sorted(system.keys()),
"cpu_subunits": system["hardware"]["CPU"],
"bus_count": len(system["hardware"]["Buses"]),
"io_categories": ["Input", "Output"],
}
expected = {
"top_layers": ["hardware", "software"],
"cpu_subunits": ["ALU", "CU", "Register"],
"bus_count": 3,
"io_categories": ["Input", "Output"],
}
return {
"name": "computer_system_hierarchy",
"summary": "校验计算机系统软硬件划分、CPU 三大内部组成(ALU/CU/Register)与三类系统总线拓扑",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_c_hello_execution_pipeline():
c_source = (
"#include <stdio.h>\n\n"
"int main()\n"
"{\n"
' printf("Hello World!\\n");\n'
" return 0;\n"
"}\n"
)
stages = [
"compile_to_machine_code",
"load_into_memory",
"fetch",
"decode",
"execute",
"write_back",
]
actual = {
"has_stdio_include": "#include <stdio.h>" in c_source,
"has_printf_hello": 'printf("Hello World!\\n");' in c_source,
"stage_count": len(stages),
"cpu_four_phases": stages[2:],
}
expected = {
"has_stdio_include": True,
"has_printf_hello": True,
"stage_count": 6,
"cpu_four_phases": ["fetch", "decode", "execute", "write_back"],
}
return {
"name": "c_hello_execution_pipeline",
"summary": "校验首个 C 程序示例从编译、装载到 CPU 取指-译码-执行-写回的六步运行主链",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_minimal_instruction_trace():
memory = ["LOAD ACC 10", "ADD ACC 20", "HALT"]
pc, acc, ir = 0, 0, None
trace = []
while pc < len(memory):
ir = memory[pc]
pc += 1
parts = ir.split()
op = parts[0]
if op == "LOAD":
acc = int(parts[2])
elif op == "ADD":
acc += int(parts[2])
elif op == "HALT":
trace.append((op, pc, acc))
break
trace.append((op, pc, acc))
actual = {"final_pc": pc, "final_acc": acc, "trace": trace}
expected = {
"final_pc": 3,
"final_acc": 30,
"trace": [("LOAD", 1, 10), ("ADD", 2, 30), ("HALT", 3, 30)],
}
return {
"name": "minimal_instruction_trace",
"summary": "离线追踪极简指令流的取指、译码、执行与写回寄存器状态演变",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_curriculum_prerequisites_and_refs():
goals = [
"hardware_structure",
"program_execution",
"cpu_memory_bus",
"isa_pipeline_interrupt_cache",
"os_compiler_embedded_foundation",
]
refs = [
"Computer Organization and Design (Patterson & Hennessy)",
"CSAPP (Bryant & O'Hallaron)",
"计算机组成原理(唐朔飞)",
"Intel 64 and IA-32 Architectures SDM",
"ARM Architecture Reference Manual",
]
actual = {"goal_count": len(goals), "ref_count": len(refs), "has_csapp": any("CSAPP" in r for r in refs)}
expected = {"goal_count": 5, "ref_count": 5, "has_csapp": True}
return {
"name": "curriculum_prerequisites_and_refs",
"summary": "核对课程学习目标条目数与经典参考教材和架构手册列表",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_computer_system_hierarchy(),
case_c_hello_execution_pipeline(),
case_minimal_instruction_trace(),
case_curriculum_prerequisites_and_refs(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。
入口文件:projects/computer-organization-tutorial-lab/explore-tutorial.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2637.zip
课程关联工程包(登记关联22章) · 含当前章节或实例 · mbskycom202612a2616.zip