让我们按照 From Nand to Tetris 里 Project 5 的要求,来完成下列的设计。
- 1.
Memory(已完成,可以参考 From Nand to Tetris 里的 Project 5 (Memory 部分) 一文) - 2.
CPU(已完成,可以参考 From Nand to Tetris 里的 Project 5 (CPU 部分) 一文) - 3.
Computer
本文只涉及 Computer 的实现
说明
我是阅读了《计算机系统要素 (第2版)》 第 5 章的内容后才去完成 Project 5 的。读者朋友在完成 Project 5 时,如果遇到不明白的地方,可以参考书中的描述。书中第 68 页和第 69 页有如下内容,可供参考。
正文
前往 Nand to Tetris Online IDE,选择 Project 5 里的 Computer ⬇️
我们的目标是实现 Computer (计算机),原始的代码如下 ⬇️
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/5/Computer.hdl
/**
* The Hack computer, consisting of CPU, ROM and RAM.
* When reset = 0, the program stored in the ROM executes.
* When reset = 1, the program's execution restarts.
* Thus, to start running the currently loaded program,
* set reset to 1, and then set it to 0.
* From this point onwards, the user is at the mercy of the software.
* Depending on the program's code, and whether the code is correct,
* the screen may show some output, the user may be expected to enter
* some input using the keyboard, or the program may do some procerssing.
*/
CHIP Computer {
IN reset;
PARTS:
//// Replace this comment with your code.
}
实现功能
《计算机系统要素 (第2版)》 一书第 69 页有如下内容可供参考
我们需要将以下芯片组合在一起
ROM32KCPUMemory
参考上图,可以写出如下的代码 ⬇️
CHIP Computer {
IN reset;
PARTS:
ROM32K(address= pc, out= instruction);
CPU(inM= inM, instruction= instruction, reset= reset, outM= outM, writeM= writeM, addressM= addressM, pc= pc);
Memory(in= outM, load= writeM, address= addressM, out= inM);
}
测试
进行测试前,先将控制速度的按钮移动到接近 Fast 的位置
ComputerAdd
从下拉列表中选择 ComputerAdd,然后点击 Run 按钮
运行之后,可以看到测试通过了 ⬇️
ComputerMax
从下拉列表中选择 ComputerMax,然后点击 Run 按钮
运行之后,可以看到测试通过了 ⬇️
ComputerRect
从下拉列表中选择 ComputerRect,然后点击 Run 按钮
运行之后,可以看到测试通过了。在 Chip Computer 面板上的 Screen 部分,可以看到一个很小的黑色矩形 ⬇️