Web-Ide mit aufgenommen

This commit is contained in:
Riwoldt
2026-04-09 14:14:56 +02:00
parent 64816c45cc
commit 15cfaf332d
489 changed files with 186891 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
{
"name": "@nand2tetris/projects",
"version": "0.0.0",
"private": true,
"description": "",
"author": "David Souther <davidsouther@gmail.com>",
"license": "ISC",
"homepage": "https://davidsouther.github.io/nand2tetris",
"type": "module",
"exports": {
"./*": "./build/*"
},
"typesVersions": {
"*": {
"*": [
"build/*"
]
}
},
"dependencies": {
"@davidsouther/jiffies": "^2.2.5"
},
"scripts": {
"build": "tsc"
}
}
+254
View File
@@ -0,0 +1,254 @@
export { FIBONACCI } from "./samples/vm/fibonnaci.js";
/**
* Duplicated for web from node:path.
* A parsed path object generated by path.parse() or consumed by path.format().
*/
export interface ParsedPath {
/** The root of the path such as '/' or 'c:\' */
root: string;
/** The full directory path such as '/home/user/dir' or 'c:\path\dir' */
dir: string;
/** The file name including extension (if any) such as 'index.html' */
base: string;
/** The file extension (if any) such as '.html' */
ext: string;
/** The file name without extension (if any) such as 'index' */
name: string;
}
export type Assignment = ParsedPath;
// To rebuild this list of stubs using node.js:
//
// full = await import("./full.js")
// Object.entries(full.Assignments).map(([key, entry]) => ({key, entries: Object.keys(entry).reduce((a, b) => (a[b] = '', a), {})})).reduce(((a, b) => (a[b.key] = b.entries, a)), {})
export const AssignmentStubs = {
Nand: { "Nand.tst": "", "Nand.cmp": "" },
Not: { "Not.hdl": "", "Not.tst": "", "Not.cmp": "" },
And: { "And.hdl": "", "And.tst": "", "And.cmp": "" },
Or: { "Or.hdl": "", "Or.tst": "", "Or.cmp": "" },
Xor: { "Xor.hdl": "", "Xor.tst": "", "Xor.cmp": "" },
Mux: { "Mux.hdl": "", "Mux.tst": "", "Mux.cmp": "" },
DMux: { "DMux.hdl": "", "DMux.tst": "", "DMux.cmp": "" },
Not16: { "Not16.hdl": "", "Not16.tst": "", "Not16.cmp": "" },
And16: { "And16.hdl": "", "And16.tst": "", "And16.cmp": "" },
Or16: { "Or16.hdl": "", "Or16.tst": "", "Or16.cmp": "" },
Mux16: { "Mux16.hdl": "", "Mux16.tst": "", "Mux16.cmp": "" },
Mux4Way16: { "Mux4Way16.hdl": "", "Mux4Way16.tst": "", "Mux4Way16.cmp": "" },
Mux8Way16: { "Mux8Way16.hdl": "", "Mux8Way16.tst": "", "Mux8Way16.cmp": "" },
DMux4Way: { "DMux4Way.hdl": "", "DMux4Way.tst": "", "DMux4Way.cmp": "" },
DMux8Way: { "DMux8Way.hdl": "", "DMux8Way.tst": "", "DMux8Way.cmp": "" },
Or8Way: { "Or8Way.hdl": "", "Or8Way.tst": "", "Or8Way.cmp": "" },
HalfAdder: { "HalfAdder.hdl": "", "HalfAdder.tst": "", "HalfAdder.cmp": "" },
FullAdder: { "FullAdder.hdl": "", "FullAdder.tst": "", "FullAdder.cmp": "" },
Add16: { "Add16.hdl": "", "Add16.tst": "", "Add16.cmp": "" },
Inc16: { "Inc16.hdl": "", "Inc16.tst": "", "Inc16.cmp": "" },
ALU: {
"ALU.hdl": "",
"ALU.tst": "",
"ALU.cmp": "",
"ALU-basic.tst": "",
"ALU-basic.cmp": "",
},
Bit: { "Bit.hdl": "", "Bit.tst": "", "Bit.cmp": "" },
Register: { "Register.hdl": "", "Register.tst": "", "Register.cmp": "" },
PC: { "PC.hdl": "", "PC.tst": "", "PC.cmp": "" },
RAM8: { "RAM8.hdl": "", "RAM8.tst": "", "RAM8.cmp": "" },
RAM64: { "RAM64.hdl": "", "RAM64.tst": "", "RAM64.cmp": "" },
RAM512: { "RAM512.hdl": "", "RAM512.tst": "", "RAM512.cmp": "" },
RAM4K: { "RAM4K.hdl": "", "RAM4K.tst": "", "RAM4K.cmp": "" },
RAM16K: { "RAM16K.hdl": "", "RAM16K.tst": "", "RAM16K.cmp": "" },
Memory: { "Memory.hdl": "", "Memory.tst": "", "Memory.cmp": "" },
CPU: {
"CPU.hdl": "",
"CPU.tst": "",
"CPU.cmp": "",
"CPU-external.tst": "",
"CPU-external.cmp": "",
},
Computer: {
"Computer.hdl": "",
"ComputerAdd.tst": "",
"ComputerAdd.cmp": "",
"ComputerMax.tst": "",
"ComputerMax.cmp": "",
"ComputerRect.tst": "",
"ComputerRect.cmp": "",
},
SimpleAdd: {
"SimpleAdd.vm": "",
"SimpleAddVME.tst": "",
"SimpleAdd.cmp": "",
"SimpleAdd.tst": "",
},
StackTest: {
"StackTest.vm": "",
"StackTestVME.tst": "",
"StackTest.cmp": "",
"StackTest.tst": "",
},
BasicTest: {
"BasicTest.vm": "",
"BasicTestVME.tst": "",
"BasicTest.cmp": "",
"BasicTest.tst": "",
},
PointerTest: {
"PointerTest.vm": "",
"PointerTestVME.tst": "",
"PointerTest.cmp": "",
"PointerTest.tst": "",
},
StaticTest: {
"StaticTest.vm": "",
"StaticTestVME.tst": "",
"StaticTest.cmp": "",
"StaticTest.tst": "",
},
BasicLoop: {
"BasicLoop.vm": "",
"BasicLoopVME.tst": "",
"BasicLoop.cmp": "",
"BasicLoop.tst": "",
},
FibonacciSeries: {
"FibonacciSeries.vm": "",
"FibonacciSeriesVME.tst": "",
"FibonacciSeries.cmp": "",
"FibonacciSeries.tst": "",
},
SimpleFunction: {
"SimpleFunction.vm": "",
"SimpleFunctionVME.tst": "",
"SimpleFunction.cmp": "",
"SimpleFunction.tst": "",
},
NestedCall: {
"Sys.vm": "",
"NestedCallVME.tst": "",
"NestedCall.cmp": "",
"NestedCall.tst": "",
},
FibonacciElement: {
"Sys.vm": "",
"Main.vm": "",
"FibonacciElementVME.tst": "",
"FibonacciElement.cmp": "",
"FibonacciElement.tst": "",
},
StaticsTest: {
"Class1.vm": "",
"Class2.vm": "",
"Sys.vm": "",
"StaticsTestVME.tst": "",
"StaticsTest.cmp": "",
"StaticsTest.tst": "",
},
};
export const BUILTIN_CHIP_PROJECTS: Record<
"01" | "02" | "03" | "05",
string[]
> = {
"01": ["Nand"],
"02": [],
"03": ["DFF"],
"05": ["Screen", "Keyboard", "DRegister", "ARegister", "ROM32K", "RAM16K"],
};
export const CHIP_PROJECTS: Record<"01" | "02" | "03" | "05", string[]> = {
"01": [
"Not",
"And",
"Or",
"Xor",
"Mux",
"DMux",
"Not16",
"And16",
"Or16",
"Mux16",
"Or8Way",
"Mux4Way16",
"Mux8Way16",
"DMux4Way",
"DMux8Way",
],
"02": [
"HalfAdder",
"FullAdder",
"Add16",
"Inc16",
"ALU",
// "ALUAll", // Special secret undocumented ALU tests
],
"03": ["Bit", "Register", "RAM8", "RAM64", "RAM512", "RAM4K", "RAM16K", "PC"],
"05": ["Memory", "CPU", "Computer"],
};
export const CHIP_ORDER: Partial<Record<"01" | "02" | "03" | "05", string[]>> =
{
"05": [
"Memory",
"CPU",
"Computer",
"Screen",
"Keyboard",
"DRegister",
"ARegister",
"ROM32K",
"RAM16K",
],
};
export const ASM_PROJECTS: Record<"06", string[]> = {
"06": ["Add", "Max", "Rectangle", "Pong"],
};
export const VM_PROJECTS: Record<"07" | "08", string[]> = {
"07": ["SimpleAdd", "StackTest", "BasicTest", "PointerTest", "StaticTest"],
"08": [
"BasicLoop",
"FibonacciSeries",
"SimpleFunction",
"NestedCall",
"FibonacciElement",
"StaticsTest",
],
};
function partitionChips(project: "01" | "02" | "03" | "05", chips: string[]) {
const base = CHIP_PROJECTS[project];
// Get all the default project chips, in order, and remove those that aren't in `chips`.
const core = base.filter((name) => chips.includes(name));
// Get all the chips that aren't in base.
const rest = chips.filter((name) => !base.includes(name));
rest.sort();
return [...core, ...rest];
}
export function sortChips(project: string, chips: string[]): string[] {
switch (project) {
case "1":
case "01": {
return partitionChips("01", chips);
}
case "2":
case "02": {
return partitionChips("02", chips);
}
case "3":
case "03": {
return partitionChips("03", chips);
}
case "5":
case "05": {
return partitionChips("05", chips);
}
default:
chips.sort();
return chips;
}
}
+74
View File
@@ -0,0 +1,74 @@
import { FileSystem, Tree } from "@davidsouther/jiffies/lib/esm/fs.js";
import * as project_01 from "./project_01/index.js";
import * as project_02 from "./project_02/index.js";
import * as project_03 from "./project_03/index.js";
import * as project_04 from "./project_04/index.js";
import * as project_05 from "./project_05/index.js";
import * as project_07 from "./project_07/index.js";
import * as project_08 from "./project_08/index.js";
import { reset } from "./reset.js";
import * as project_06 from "./samples/project_06/index.js";
export const ChipProjects = {
"01": project_01,
"02": project_02,
"03": project_03,
"05": project_05,
};
export const VmProjects = {
"07": project_07,
"08": project_08,
};
export const Projects = {
"1": project_01,
"2": project_02,
"3": project_03,
"4": project_04,
"5": project_05,
"6": project_06,
"7": project_07,
"8": project_08,
};
export const ProjectIDs: (keyof typeof Projects)[] = Object.keys(
Projects,
) as unknown as (keyof typeof Projects)[];
const ProjectFiles: Record<keyof typeof Projects, Tree> = {
"1": project_01.CHIPS,
"2": project_02.CHIPS,
"3": project_03.CHIPS,
"4": project_04.TESTS,
"5": project_05.CHIPS,
"6": project_06.FILES,
"7": project_07.VMS,
"8": project_08.VMS,
};
export const resetFiles = async (fs: FileSystem, projects = ProjectIDs) => {
for (const project of projects) {
await Projects[project as keyof typeof Projects].resetFiles(fs);
}
};
export const resetTests = async (fs: FileSystem, projects = ProjectIDs) => {
for (const project of projects) {
await Projects[project as keyof typeof Projects].resetTests(fs);
}
};
export const createFiles = async (fs: FileSystem) => {
await reset(fs, ProjectFiles, "/", false);
};
export const Assignments = {
...project_01.CHIPS,
...project_02.CHIPS,
...project_03.CHIPS,
...project_05.CHIPS,
...project_07.VMS,
...project_08.VMS,
};
+36
View File
@@ -0,0 +1,36 @@
import { FileSystem } from "@davidsouther/jiffies/lib/esm/fs.js";
import { type Projects } from "./full.js";
export async function resetFiles(
fs: FileSystem,
projects?: (keyof typeof Projects)[],
) {
await (await import("./full.js")).resetFiles(fs, projects);
}
export async function resetTests(
fs: FileSystem,
projects?: (keyof typeof Projects)[],
) {
await (await import("./full.js")).resetTests(fs, projects);
}
export async function createFiles(fs: FileSystem) {
await (await import("./full.js")).createFiles(fs);
}
export async function loadSamples(fs: FileSystem) {
(await import("./samples/index.js")).loadSamples(fs);
}
export async function loadSolutions(fs: FileSystem) {
(await import("./testing/index.js")).loadSolutions(fs);
}
export const loaders = {
resetFiles,
loadSolutions,
loadSamples,
};
export default loaders;
@@ -0,0 +1,48 @@
export const hdl = `// 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/1/Nand.hdl
/**
* Nand gate:
* if (a and b) out = 0, else out = 1
*/
CHIP Nand {
IN a, b;
OUT out;
PARTS:
BUILTIN Nand;
}`;
export const tst = `// 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/1/Nand.tst
output-list a b out;
set a 0,
set b 0,
eval,
output;
set a 0,
set b 1,
eval,
output;
set a 1,
set b 0,
eval,
output;
set a 1,
set b 1,
eval,
output;`;
export const cmp = `| a | b |out|
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |`;
@@ -0,0 +1,34 @@
export const cmp = `|in |out|
| 0 | 1 |
| 1 | 0 |`;
export const hdl = `// 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/1/Not.hdl
/**
* Not gate:
* if (in) out = 0, else out = 1
*/
CHIP Not {
IN in;
OUT out;
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/Not.tst
load Not.hdl,
compare-to Not.cmp,
output-list in out;
set in 0,
eval,
output;
set in 1,
eval,
output;`;
@@ -0,0 +1,49 @@
export const hdl = `// 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/1/And.hdl
/**
* And gate:
* if (a and b) out = 1, else out = 0
*/
CHIP And {
IN a, b;
OUT out;
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/And.tst
load And.hdl,
compare-to And.cmp,
output-list a b out;
set a 0,
set b 0,
eval,
output;
set a 0,
set b 1,
eval,
output;
set a 1,
set b 0,
eval,
output;
set a 1,
set b 1,
eval,
output;`;
export const cmp = `| a | b |out|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |`;
@@ -0,0 +1,48 @@
export const cmp = `| a | b |out|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |`;
export const hdl = `// 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/1/Or.hdl
/**
* Or gate:
* if (a or b) out = 1, else out = 0
*/
CHIP Or {
IN a, b;
OUT out;
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/Or.tst
load Or.hdl,
compare-to Or.cmp,
output-list a b out;
set a 0,
set b 0,
eval,
output;
set a 0,
set b 1,
eval,
output;
set a 1,
set b 0,
eval,
output;
set a 1,
set b 1,
eval,
output;`;
@@ -0,0 +1,48 @@
export const cmp = `| a | b |out|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |`;
export const hdl = `// 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/1/Xor.hdl
/**
* Exclusive-or gate:
* if ((a and Not(b)) or (Not(a) and b)) out = 1, else out = 0
*/
CHIP Xor {
IN a, b;
OUT out;
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/Xor.tst
load Xor.hdl,
compare-to Xor.cmp,
output-list a b out;
set a 0,
set b 0,
eval,
output;
set a 0,
set b 1,
eval,
output;
set a 1,
set b 0,
eval,
output;
set a 1,
set b 1,
eval,
output;`;
@@ -0,0 +1,72 @@
export const cmp = `| a | b |sel|out|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 |`;
export const hdl = `// 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/1/Mux.hdl
/**
* Multiplexor:
* if (sel = 0) out = a, else out = b
*/
CHIP Mux {
IN a, b, sel;
OUT out;
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/Mux.tst
load Mux.hdl,
compare-to Mux.cmp,
output-list a b sel out;
set a 0,
set b 0,
set sel 0,
eval,
output;
set sel 1,
eval,
output;
set a 0,
set b 1,
set sel 0,
eval,
output;
set sel 1,
eval,
output;
set a 1,
set b 0,
set sel 0,
eval,
output;
set sel 1,
eval,
output;
set a 1,
set b 1,
set sel 0,
eval,
output;
set sel 1,
eval,
output;`;
@@ -0,0 +1,48 @@
export const hdl = `// 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/1/DMux.hdl
/**
* Demultiplexor:
* [a, b] = [in, 0] if sel = 0
* [0, in] if sel = 1
*/
CHIP DMux {
IN in, sel;
OUT a, b;
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/DMux.tst
load DMux.hdl,
compare-to DMux.cmp,
output-list in sel a b;
set in 0,
set sel 0,
eval,
output;
set sel 1,
eval,
output;
set in 1,
set sel 0,
eval,
output;
set sel 1,
eval,
output;`;
export const cmp = `|in |sel| a | b |
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |`;
@@ -0,0 +1,50 @@
export const hdl = `// 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/01/Not16.hdl
/**
* 16-bit Not gate:
* for i = 0, ..., 15:
* out[i] = Not(a[i])
*/
CHIP Not16 {
IN in[16];
OUT out[16];
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/Not16.tst
load Not16.hdl,
compare-to Not16.cmp,
output-list in%B1.16.1 out%B1.16.1;
set in %B0000000000000000,
eval,
output;
set in %B1111111111111111,
eval,
output;
set in %B1010101010101010,
eval,
output;
set in %B0011110011000011,
eval,
output;
set in %B0001001000110100,
eval,
output;`;
export const cmp = `| in | out |
| 0000000000000000 | 1111111111111111 |
| 1111111111111111 | 0000000000000000 |
| 1010101010101010 | 0101010101010101 |
| 0011110011000011 | 1100001100111100 |
| 0001001000110100 | 1110110111001011 |`;
@@ -0,0 +1,62 @@
export const hdl = `// 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/1/And16.hdl
/**
* 16-bit And gate:
* for i = 0, ..., 15:
* out[i] = a[i] And b[i]
*/
CHIP And16 {
IN a[16], b[16];
OUT out[16];
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/And16.tst
load And16.hdl,
compare-to And16.cmp,
output-list a%B1.16.1 b%B1.16.1 out%B1.16.1;
set a %B0000000000000000,
set b %B0000000000000000,
eval,
output;
set a %B0000000000000000,
set b %B1111111111111111,
eval,
output;
set a %B1111111111111111,
set b %B1111111111111111,
eval,
output;
set a %B1010101010101010,
set b %B0101010101010101,
eval,
output;
set a %B0011110011000011,
set b %B0000111111110000,
eval,
output;
set a %B0001001000110100,
set b %B1001100001110110,
eval,
output;`;
export const cmp = `| a | b | out |
| 0000000000000000 | 0000000000000000 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 0000000000000000 |
| 1111111111111111 | 1111111111111111 | 1111111111111111 |
| 1010101010101010 | 0101010101010101 | 0000000000000000 |
| 0011110011000011 | 0000111111110000 | 0000110011000000 |
| 0001001000110100 | 1001100001110110 | 0001000000110100 |`;
@@ -0,0 +1,61 @@
export const cmp = `| a | b | out |
| 0000000000000000 | 0000000000000000 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 1111111111111111 |
| 1111111111111111 | 1111111111111111 | 1111111111111111 |
| 1010101010101010 | 0101010101010101 | 1111111111111111 |
| 0011110011000011 | 0000111111110000 | 0011111111110011 |
| 0001001000110100 | 1001100001110110 | 1001101001110110 |`;
export const hdl = `// 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/1/Or16.hdl
/**
* 16-bit Or gate:
* for i = 0, ..., 15:
* out[i] = a[i] Or b[i]
*/
CHIP Or16 {
IN a[16], b[16];
OUT out[16];
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/Or16.tst
load Or16.hdl,
compare-to Or16.cmp,
output-list a%B1.16.1 b%B1.16.1 out%B1.16.1;
set a %B0000000000000000,
set b %B0000000000000000,
eval,
output;
set a %B0000000000000000,
set b %B1111111111111111,
eval,
output;
set a %B1111111111111111,
set b %B1111111111111111,
eval,
output;
set a %B1010101010101010,
set b %B0101010101010101,
eval,
output;
set a %B0011110011000011,
set b %B0000111111110000,
eval,
output;
set a %B0001001000110100,
set b %B1001100001110110,
eval,
output;`;
@@ -0,0 +1,73 @@
export const cmp = `| a | b |sel| out |
| 0000000000000000 | 0000000000000000 | 0 | 0000000000000000 |
| 0000000000000000 | 0000000000000000 | 1 | 0000000000000000 |
| 0000000000000000 | 0001001000110100 | 0 | 0000000000000000 |
| 0000000000000000 | 0001001000110100 | 1 | 0001001000110100 |
| 1001100001110110 | 0000000000000000 | 0 | 1001100001110110 |
| 1001100001110110 | 0000000000000000 | 1 | 0000000000000000 |
| 1010101010101010 | 0101010101010101 | 0 | 1010101010101010 |
| 1010101010101010 | 0101010101010101 | 1 | 0101010101010101 |`;
export const hdl = `// 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/1/Mux16.hdl
/**
* 16-bit multiplexor:
* for i = 0, ..., 15:
* if (sel = 0) out[i] = a[i], else out[i] = b[i]
*/
CHIP Mux16 {
IN a[16], b[16], sel;
OUT out[16];
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/Mux16.tst
load Mux16.hdl,
compare-to Mux16.cmp,
output-list a%B1.16.1 b%B1.16.1 sel out%B1.16.1;
set a 0,
set b 0,
set sel 0,
eval,
output;
set sel 1,
eval,
output;
set a %B0000000000000000,
set b %B0001001000110100,
set sel 0,
eval,
output;
set sel 1,
eval,
output;
set a %B1001100001110110,
set b %B0000000000000000,
set sel 0,
eval,
output;
set sel 1,
eval,
output;
set a %B1010101010101010,
set b %B0101010101010101,
set sel 0,
eval,
output;
set sel 1,
eval,
output;`;
@@ -0,0 +1,75 @@
export const cmp = `| a | b | c | d | sel | out |
| 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 00 | 0000000000000000 |
| 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 01 | 0000000000000000 |
| 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 10 | 0000000000000000 |
| 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 11 | 0000000000000000 |
| 0001001000110100 | 1001100001110110 | 1010101010101010 | 0101010101010101 | 00 | 0001001000110100 |
| 0001001000110100 | 1001100001110110 | 1010101010101010 | 0101010101010101 | 01 | 1001100001110110 |
| 0001001000110100 | 1001100001110110 | 1010101010101010 | 0101010101010101 | 10 | 1010101010101010 |
| 0001001000110100 | 1001100001110110 | 1010101010101010 | 0101010101010101 | 11 | 0101010101010101 |`;
export const hdl = `// 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/1/Mux4Way16.hdl
/**
* 4-way 16-bit multiplexor:
* out = a if sel = 00
* b if sel = 01
* c if sel = 10
* d if sel = 11
*/
CHIP Mux4Way16 {
IN a[16], b[16], c[16], d[16], sel[2];
OUT out[16];
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/Mux4Way16.tst
load Mux4Way16.hdl,
compare-to Mux4Way16.cmp,
output-list a%B1.16.1 b%B1.16.1 c%B1.16.1 d%B1.16.1 sel%B2.2.2 out%B1.16.1;
set a 0,
set b 0,
set c 0,
set d 0,
set sel 0,
eval,
output;
set sel 1,
eval,
output;
set sel 2,
eval,
output;
set sel 3,
eval,
output;
set a %B0001001000110100,
set b %B1001100001110110,
set c %B1010101010101010,
set d %B0101010101010101,
set sel 0,
eval,
output;
set sel 1,
eval,
output;
set sel 2,
eval,
output;
set sel 3,
eval,
output;`;
@@ -0,0 +1,129 @@
export const cmp = `| a | b | c | d | e | f | g | h | sel | out |
| 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 000 | 0000000000000000 |
| 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 001 | 0000000000000000 |
| 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 010 | 0000000000000000 |
| 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 011 | 0000000000000000 |
| 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 100 | 0000000000000000 |
| 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 101 | 0000000000000000 |
| 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 110 | 0000000000000000 |
| 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 0000000000000000 | 111 | 0000000000000000 |
| 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 000 | 0001001000110100 |
| 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 001 | 0010001101000101 |
| 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 010 | 0011010001010110 |
| 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 011 | 0100010101100111 |
| 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 100 | 0101011001111000 |
| 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 101 | 0110011110001001 |
| 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 110 | 0111100010011010 |
| 0001001000110100 | 0010001101000101 | 0011010001010110 | 0100010101100111 | 0101011001111000 | 0110011110001001 | 0111100010011010 | 1000100110101011 | 111 | 1000100110101011 |`;
export const hdl = `// 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/1/Mux8Way16.hdl
/**
* 8-way 16-bit multiplexor:
* out = a if sel = 000
* b if sel = 001
* c if sel = 010
* d if sel = 011
* e if sel = 100
* f if sel = 101
* g if sel = 110
* h if sel = 111
*/
CHIP Mux8Way16 {
IN a[16], b[16], c[16], d[16],
e[16], f[16], g[16], h[16],
sel[3];
OUT out[16];
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/Mux8Way16.tst
load Mux8Way16.hdl,
compare-to Mux8Way16.cmp,
output-list a%B1.16.1 b%B1.16.1 c%B1.16.1 d%B1.16.1 e%B1.16.1 f%B1.16.1 g%B1.16.1 h%B1.16.1 sel%B2.3.2 out%B1.16.1;
set a 0,
set b 0,
set c 0,
set d 0,
set e 0,
set f 0,
set g 0,
set h 0,
set sel 0,
eval,
output;
set sel 1,
eval,
output;
set sel 2,
eval,
output;
set sel 3,
eval,
output;
set sel 4,
eval,
output;
set sel 5,
eval,
output;
set sel 6,
eval,
output;
set sel 7,
eval,
output;
set a %B0001001000110100,
set b %B0010001101000101,
set c %B0011010001010110,
set d %B0100010101100111,
set e %B0101011001111000,
set f %B0110011110001001,
set g %B0111100010011010,
set h %B1000100110101011,
set sel 0,
eval,
output;
set sel 1,
eval,
output;
set sel 2,
eval,
output;
set sel 3,
eval,
output;
set sel 4,
eval,
output;
set sel 5,
eval,
output;
set sel 6,
eval,
output;
set sel 7,
eval,
output;`;
@@ -0,0 +1,70 @@
export const hdl = `// 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/1/DMux4Way.hdl
/**
* 4-way demultiplexor:
* [a, b, c, d] = [in, 0, 0, 0] if sel = 00
* [0, in, 0, 0] if sel = 01
* [0, 0, in, 0] if sel = 10
* [0, 0, 0, in] if sel = 11
*/
CHIP DMux4Way {
IN in, sel[2];
OUT a, b, c, d;
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/DMux4Way.tst
load DMux4Way.hdl,
compare-to DMux4Way.cmp,
output-list in sel%B2.2.2 a b c d;
set in 0,
set sel %B00,
eval,
output;
set sel %B01,
eval,
output;
set sel %B10,
eval,
output;
set sel %B11,
eval,
output;
set in 1,
set sel %B00,
eval,
output;
set sel %B01,
eval,
output;
set sel %B10,
eval,
output;
set sel %B11,
eval,
output;`;
export const cmp = `|in | sel | a | b | c | d |
| 0 | 00 | 0 | 0 | 0 | 0 |
| 0 | 01 | 0 | 0 | 0 | 0 |
| 0 | 10 | 0 | 0 | 0 | 0 |
| 0 | 11 | 0 | 0 | 0 | 0 |
| 1 | 00 | 1 | 0 | 0 | 0 |
| 1 | 01 | 0 | 1 | 0 | 0 |
| 1 | 10 | 0 | 0 | 1 | 0 |
| 1 | 11 | 0 | 0 | 0 | 1 |`;
@@ -0,0 +1,115 @@
export const hdl = `// 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/1/DMux8Way.hdl
/**
* 8-way demultiplexor:
* [a, b, c, d, e, f, g, h] = [in, 0, 0, 0, 0, 0, 0, 0] if sel = 000
* [0, in, 0, 0, 0, 0, 0, 0] if sel = 001
* [0, 0, in, 0, 0, 0, 0, 0] if sel = 010
* [0, 0, 0, in, 0, 0, 0, 0] if sel = 011
* [0, 0, 0, 0, in, 0, 0, 0] if sel = 100
* [0, 0, 0, 0, 0, in, 0, 0] if sel = 101
* [0, 0, 0, 0, 0, 0, in, 0] if sel = 110
* [0, 0, 0, 0, 0, 0, 0, in] if sel = 111
*/
CHIP DMux8Way {
IN in, sel[3];
OUT a, b, c, d, e, f, g, h;
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/DMux8Way.tst
load DMux8Way.hdl,
compare-to DMux8Way.cmp,
output-list in sel%B2.3.2 a b c d e f g h;
set in 0,
set sel %B000,
eval,
output;
set sel %B001,
eval,
output;
set sel %B010,
eval,
output;
set sel %B011,
eval,
output;
set sel %B100,
eval,
output;
set sel %B101,
eval,
output;
set sel %B110,
eval,
output;
set sel %B111,
eval,
output;
set in 1,
set sel %B000,
eval,
output;
set sel %B001,
eval,
output;
set sel %B010,
eval,
output;
set sel %B011,
eval,
output;
set sel %B100,
eval,
output;
set sel %B101,
eval,
output;
set sel %B110,
eval,
output;
set sel %B111,
eval,
output;`;
export const cmp = `|in | sel | a | b | c | d | e | f | g | h |
| 0 | 000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 001 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 010 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 011 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 100 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 101 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 110 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 111 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 000 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 001 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 010 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
| 1 | 011 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
| 1 | 100 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
| 1 | 101 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
| 1 | 110 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
| 1 | 111 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |`;
@@ -0,0 +1,49 @@
export const cmp = `| in |out|
| 00000000 | 0 |
| 11111111 | 1 |
| 00010000 | 1 |
| 00000001 | 1 |
| 00100110 | 1 |`;
export const hdl = `// 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/1/Or8Way.hdl
/**
* 8-way Or gate:
* out = in[0] Or in[1] Or ... Or in[7]
*/
CHIP Or8Way {
IN in[8];
OUT out;
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/1/Or8Way.tst
load Or8Way.hdl,
compare-to Or8Way.cmp,
output-list in%B2.8.2 out;
set in %B00000000,
eval,
output;
set in %B11111111,
eval,
output;
set in %B00010000,
eval,
output;
set in %B00000001,
eval,
output;
set in %B00100110,
eval,
output;`;
@@ -0,0 +1,86 @@
import { FileSystem, reset } from "@davidsouther/jiffies/lib/esm/fs.js";
import { resetBySuffix } from "../reset.js";
import * as Nand from "./00_nand.js";
import * as Not from "./01_not.js";
import * as And from "./02_and.js";
import * as Or from "./03_or.js";
import * as Xor from "./04_xor.js";
import * as Mux from "./05_mux.js";
import * as DMux from "./06_dmux.js";
import * as Not16 from "./07_not16.js";
import * as And16 from "./08_and16.js";
import * as Or16 from "./09_or16.js";
import * as Mux16 from "./10_mux16.js";
import * as Mux4Way16 from "./11_mux4way16.js";
import * as Mux8Way16 from "./12_mux8way16.js";
import * as DMux4Way from "./13_dmux4way.js";
import * as DMux8Way from "./14_dmux8way.js";
import * as Or8Way from "./15_or8way.js";
export const CHIPS = {
"Nand.tst": Nand.tst,
"Nand.cmp": Nand.cmp,
"Not.hdl": Not.hdl,
"Not.tst": Not.tst,
"Not.cmp": Not.cmp,
"And.hdl": And.hdl,
"And.tst": And.tst,
"And.cmp": And.cmp,
"Or.hdl": Or.hdl,
"Or.tst": Or.tst,
"Or.cmp": Or.cmp,
"Xor.hdl": Xor.hdl,
"Xor.tst": Xor.tst,
"Xor.cmp": Xor.cmp,
"Mux.hdl": Mux.hdl,
"Mux.tst": Mux.tst,
"Mux.cmp": Mux.cmp,
"DMux.hdl": DMux.hdl,
"DMux.tst": DMux.tst,
"DMux.cmp": DMux.cmp,
"Not16.hdl": Not16.hdl,
"Not16.tst": Not16.tst,
"Not16.cmp": Not16.cmp,
"And16.hdl": And16.hdl,
"And16.tst": And16.tst,
"And16.cmp": And16.cmp,
"Or16.hdl": Or16.hdl,
"Or16.tst": Or16.tst,
"Or16.cmp": Or16.cmp,
"Mux16.hdl": Mux16.hdl,
"Mux16.tst": Mux16.tst,
"Mux16.cmp": Mux16.cmp,
"Mux4Way16.hdl": Mux4Way16.hdl,
"Mux4Way16.tst": Mux4Way16.tst,
"Mux4Way16.cmp": Mux4Way16.cmp,
"Mux8Way16.hdl": Mux8Way16.hdl,
"Mux8Way16.tst": Mux8Way16.tst,
"Mux8Way16.cmp": Mux8Way16.cmp,
"DMux4Way.hdl": DMux4Way.hdl,
"DMux4Way.tst": DMux4Way.tst,
"DMux4Way.cmp": DMux4Way.cmp,
"DMux8Way.hdl": DMux8Way.hdl,
"DMux8Way.tst": DMux8Way.tst,
"DMux8Way.cmp": DMux8Way.cmp,
"Or8Way.hdl": Or8Way.hdl,
"Or8Way.tst": Or8Way.tst,
"Or8Way.cmp": Or8Way.cmp,
};
export const BUILTIN_CHIPS = {
Nand: Nand.hdl,
};
export async function resetFiles(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/01");
await reset(fs, CHIPS);
await fs.popd();
}
export async function resetTests(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/01");
await resetBySuffix(fs, CHIPS, ".tst");
await resetBySuffix(fs, CHIPS, ".cmp");
await fs.popd();
}
@@ -0,0 +1,48 @@
export const hdl = `// 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/2/HalfAdder.hdl
/**
* Computes the sum of two bits.
*/
CHIP HalfAdder {
IN a, b; // 1-bit inputs
OUT sum, // Right bit of a + b
carry; // Left bit of a + b
PARTS:
//// Replace this comment with your code.
}`;
export const cmp = `| a | b |sum|car|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |`;
export const tst = `// 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/2/HalfAdder.tst
load HalfAdder.hdl,
compare-to HalfAdder.cmp,
output-list a b sum carry;
set a 0,
set b 0,
eval,
output;
set a 0,
set b 1,
eval,
output;
set a 1,
set b 0,
eval,
output;
set a 1,
set b 1,
eval,
output;`;
@@ -0,0 +1,70 @@
export const hdl = `// 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/2/FullAdder.hdl
/**
* Computes the sum of three bits.
*/
CHIP FullAdder {
IN a, b, c; // 1-bit inputs
OUT sum, // Right bit of a + b + c
carry; // Left bit of a + b + c
PARTS:
//// Replace this comment with your code.
}`;
export const cmp = `| a | b | c |sum|carry|
| 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 |`;
export const tst = `// 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/2/FullAdder.tst
load FullAdder.hdl,
compare-to FullAdder.cmp,
output-list a b c sum carry%B2.1.2;
set a 0,
set b 0,
set c 0,
eval,
output;
set c 1,
eval,
output;
set b 1,
set c 0,
eval,
output;
set c 1,
eval,
output;
set a 1,
set b 0,
set c 0,
eval,
output;
set c 1,
eval,
output;
set b 1,
set c 0,
eval,
output;
set c 1,
eval,
output;`;
@@ -0,0 +1,60 @@
export const hdl = `// 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/2/Add16.hdl
/**
* 16-bit adder: Adds two 16-bit two's complement values.
* The most significant carry bit is ignored.
*/
CHIP Add16 {
IN a[16], b[16];
OUT out[16];
PARTS:
//// Replace this comment with your code.
}`;
export const cmp = `| a | b | out |
| 0000000000000000 | 0000000000000000 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 1111111111111111 |
| 1111111111111111 | 1111111111111111 | 1111111111111110 |
| 1010101010101010 | 0101010101010101 | 1111111111111111 |
| 0011110011000011 | 0000111111110000 | 0100110010110011 |
| 0001001000110100 | 1001100001110110 | 1010101010101010 |`;
export const tst = `// 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/2/Add16.tst
load Add16.hdl,
compare-to Add16.cmp,
output-list a%B1.16.1 b%B1.16.1 out%B1.16.1;
set a %B0000000000000000,
set b %B0000000000000000,
eval,
output;
set a %B0000000000000000,
set b %B1111111111111111,
eval,
output;
set a %B1111111111111111,
set b %B1111111111111111,
eval,
output;
set a %B1010101010101010,
set b %B0101010101010101,
eval,
output;
set a %B0011110011000011,
set b %B0000111111110000,
eval,
output;
set a %B0001001000110100,
set b %B1001100001110110,
eval,
output;`;
@@ -0,0 +1,45 @@
export const hdl = `// 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/2/Inc16.hdl
/**
* 16-bit incrementer:
* out = in + 1
*/
CHIP Inc16 {
IN in[16];
OUT out[16];
PARTS:
//// Replace this comment with your code.
}`;
export const cmp = `| in | out |
| 0000000000000000 | 0000000000000001 |
| 1111111111111111 | 0000000000000000 |
| 0000000000000101 | 0000000000000110 |
| 1111111111111011 | 1111111111111100 |
`;
export const tst = `// 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/2/Inc16.tst
load Inc16.hdl,
compare-to Inc16.cmp,
output-list in%B1.16.1 out%B1.16.1;
set in %B0000000000000000, // in = 0
eval,
output;
set in %B1111111111111111, // in = -1
eval,
output;
set in %B0000000000000101, // in = 5
eval,
output;
set in %B1111111111111011, // in = -5
eval,
output;`;
@@ -0,0 +1,422 @@
export const hdl = `/**
* The ALU (Arithmetic Logic Unit).
* Computes one of the following functions:
* x+y, x-y, y-x, 0, 1, -1, x, y, -x, -y, !x, !y,
* x+1, y+1, x-1, y-1, x&y, x|y on two 16-bit inputs,
* according to 6 input bits denoted zx,nx,zy,ny,f,no.
* In addition, the ALU computes two 1-bit outputs:
* if the ALU output == 0, zr is set to 1; otherwise zr is set to 0;
* if the ALU output < 0, ng is set to 1; otherwise ng is set to 0.
*/
// Implementation: the ALU logic manipulates the x and y inputs
// and operates on the resulting values, as follows:
// if (zx == 1) set x = 0 // 16-bit constant
// if (nx == 1) set x = !x // bitwise not
// if (zy == 1) set y = 0 // 16-bit constant
// if (ny == 1) set y = !y // bitwise not
// if (f == 1) set out = x + y // integer 2's complement addition
// if (f == 0) set out = x & y // bitwise and
// if (no == 1) set out = !out // bitwise not
// if (out == 0) set zr = 1
// if (out < 0) set ng = 1
CHIP ALU {
IN
x[16], y[16], // 16-bit inputs
zx, // zero the x input?
nx, // negate the x input?
zy, // zero the y input?
ny, // negate the y input?
f, // compute out = x + y (if 1) or x & y (if 0)
no; // negate the out output?
OUT
out[16]; // 16-bit output
PARTS:
}`;
export const cmp = `| x | y |zx |nx |zy |ny | f |no | out |
| 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 |
| 0000000000000000 | 1111111111111111 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 0 | 1111111111111111 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 0 | 1111111111111110 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 1 | 0 | 1111111111111111 |
| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111111111 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 1 | 0 | 1 | 1111111111111111 |
| 0101101110100000 | 0001111011010010 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 0 | 0 | 0101101110100000 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 0 | 0 | 0001111011010010 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 0 | 1 | 1010010001011111 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 0 | 1 | 1110000100101101 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 1 | 1 | 1010010001100000 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 1 | 1 | 1110000100101110 |
| 0101101110100000 | 0001111011010010 | 0 | 1 | 1 | 1 | 1 | 1 | 0101101110100001 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 1 | 1 | 1 | 0001111011010011 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 1 | 0 | 0101101110011111 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 1 | 0 | 0001111011010001 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 0 | 1 | 0 | 0111101001110010 |
| 0101101110100000 | 0001111011010010 | 0 | 1 | 0 | 0 | 1 | 1 | 0011110011001110 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 1 | 1 | 1 | 1100001100110010 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 0 | 0 | 0 | 0001101010000000 |
| 0101101110100000 | 0001111011010010 | 0 | 1 | 0 | 1 | 0 | 1 | 0101111111110010 |
`;
export const tst = `
// ALU no stat tst provides a partial test of the ALU chip.
// It IS NOT a replacement for ALU.tst.
// ALU-nostat.tst tests only the computation part of the ALU.
// The 'zr' and 'ng' status outputs are ignored.
// This test lets you concentrate on getting the ALU computation right without the
// additional task of handling the status outputs.
// Once your ALU passes ALU-nostat.tst you need to test it with ALU.tst.
// This way, any comparison failures during ALU.tst will be caused by errors in
// the handling of the 'zr' and 'ng' status outputs.
output-list x%B1.16.1 y%B1.16.1 zx%B1.1.1 nx%B1.1.1 zy%B1.1.1
ny%B1.1.1 f%B1.1.1 no%B1.1.1 out%B1.16.1;
set x %B0000000000000000,
set y %B1111111111111111,
set zx 1,
set nx 0,
set zy 1,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 1,
set nx 1,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 1,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 0,
set no 0,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 0,
set no 0,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 0,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 0,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 1,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 1,
set no 0,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 0,
set nx 0,
set zy 0,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 0,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 0,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 0,
set ny 0,
set f 0,
set no 0,
eval,
output;
set zx 0,
set nx 1,
set zy 0,
set ny 1,
set f 0,
set no 1,
eval,
output;
set x %B101101110100000,
set y %B001111011010010,
set zx 1,
set nx 0,
set zy 1,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 1,
set nx 1,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 1,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 0,
set no 0,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 0,
set no 0,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 0,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 0,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 1,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 1,
set no 0,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 0,
set nx 0,
set zy 0,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 0,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 0,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 0,
set ny 0,
set f 0,
set no 0,
eval,
output;
set zx 0,
set nx 1,
set zy 0,
set ny 1,
set f 0,
set no 1,
eval,
output;`;
@@ -0,0 +1,834 @@
export const hdl = `// 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/2/ALU.hdl
/**
* ALU (Arithmetic Logic Unit):
* Computes out = one of the following functions:
* 0, 1, -1,
* x, y, !x, !y, -x, -y,
* x + 1, y + 1, x - 1, y - 1,
* x + y, x - y, y - x,
* x & y, x | y
* on the 16-bit inputs x, y,
* according to the input bits zx, nx, zy, ny, f, no.
* In addition, computes the two output bits:
* if (out == 0) zr = 1, else zr = 0
* if (out < 0) ng = 1, else ng = 0
*/
// Implementation: Manipulates the x and y inputs
// and operates on the resulting values, as follows:
// if (zx == 1) sets x = 0 // 16-bit constant
// if (nx == 1) sets x = !x // bitwise not
// if (zy == 1) sets y = 0 // 16-bit constant
// if (ny == 1) sets y = !y // bitwise not
// if (f == 1) sets out = x + y // integer 2's complement addition
// if (f == 0) sets out = x & y // bitwise and
// if (no == 1) sets out = !out // bitwise not
CHIP ALU {
IN
x[16], y[16], // 16-bit inputs
zx, // zero the x input?
nx, // negate the x input?
zy, // zero the y input?
ny, // negate the y input?
f, // compute (out = x + y) or (out = x & y)?
no; // negate the out output?
OUT
out[16], // 16-bit output
zr, // if (out == 0) equals 1, else 0
ng; // if (out < 0) equals 1, else 0
PARTS:
//// Replace this comment with your code.
}`;
export const cmp = `| x | y |zx |nx |zy |ny | f |no | out |zr |ng |
| 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 0 | 0 |
| 0000000000000000 | 1111111111111111 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 0 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 0 | 1111111111111110 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 0 | 0 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000010001 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 0 | 0 | 0000000000000011 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111101110 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 0 | 1 | 1111111111111100 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 1 | 1 | 1111111111101111 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 1 | 1 | 1111111111111101 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000010010 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000100 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 1 | 0 | 0000000000010000 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 1 | 0 | 0000000000000010 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 0 | 1 | 0 | 0000000000010100 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000001110 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111110010 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000001 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 0 | 1 | 0 | 1 | 0 | 1 | 0000000000010011 | 0 | 0 |`;
export const tst = `// 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/2/ALU.tst
load ALU.hdl,
compare-to ALU.cmp,
output-list x%B1.16.1 y%B1.16.1 zx nx zy ny f no out zr ng;
set x %B0000000000000000, // x = 0
set y %B1111111111111111; // y = -1
// Compute 0
set zx 1,
set nx 0,
set zy 1,
set ny 0,
set f 1,
set no 0,
eval,
output;
// Compute 1
set zx 1,
set nx 1,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
// Compute -1
set zx 1,
set nx 1,
set zy 1,
set ny 0,
set f 1,
set no 0,
eval,
output;
// Compute x
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 0,
set no 0,
eval,
output;
// Compute y
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 0,
set no 0,
eval,
output;
// Compute !x
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 0,
set no 1,
eval,
output;
// Compute !y
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 0,
set no 1,
eval,
output;
// Compute -x
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
// Compute -y
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 1,
eval,
output;
// Compute x + 1
set zx 0,
set nx 1,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
// Compute y + 1
set zx 1,
set nx 1,
set zy 0,
set ny 1,
set f 1,
set no 1,
eval,
output;
// Compute x - 1
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 1,
set no 0,
eval,
output;
// Compute y - 1
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 0,
eval,
output;
// Compute x + y
set zx 0,
set nx 0,
set zy 0,
set ny 0,
set f 1,
set no 0,
eval,
output;
// Compute x - y
set zx 0,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 1,
eval,
output;
// Compute y - x
set zx 0,
set nx 0,
set zy 0,
set ny 1,
set f 1,
set no 1,
eval,
output;
// Compute x & y
set zx 0,
set nx 0,
set zy 0,
set ny 0,
set f 0,
set no 0,
eval,
output;
// Compute x | y
set zx 0,
set nx 1,
set zy 0,
set ny 1,
set f 0,
set no 1,
eval,
output;
set x %B000000000010001, // x = 17
set y %B000000000000011; // y = 3
// Compute 0
set zx 1,
set nx 0,
set zy 1,
set ny 0,
set f 1,
set no 0,
eval,
output;
// Compute 1
set zx 1,
set nx 1,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
// Compute -1
set zx 1,
set nx 1,
set zy 1,
set ny 0,
set f 1,
set no 0,
eval,
output;
// Compute x
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 0,
set no 0,
eval,
output;
// Compute y
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 0,
set no 0,
eval,
output;
// Compute !x
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 0,
set no 1,
eval,
output;
// Compute !y
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 0,
set no 1,
eval,
output;
// Compute -x
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
// Compute -y
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 1,
eval,
output;
// Compute x + 1
set zx 0,
set nx 1,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
// Compute y + 1
set zx 1,
set nx 1,
set zy 0,
set ny 1,
set f 1,
set no 1,
eval,
output;
// Compute x - 1
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 1,
set no 0,
eval,
output;
// Compute y - 1
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 0,
eval,
output;
// Compute x + y
set zx 0,
set nx 0,
set zy 0,
set ny 0,
set f 1,
set no 0,
eval,
output;
// Compute x - y
set zx 0,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 1,
eval,
output;
// Compute y - x
set zx 0,
set nx 0,
set zy 0,
set ny 1,
set f 1,
set no 1,
eval,
output;
// Compute x & y
set zx 0,
set nx 0,
set zy 0,
set ny 0,
set f 0,
set no 0,
eval,
output;
// Compute x | y
set zx 0,
set nx 1,
set zy 0,
set ny 1,
set f 0,
set no 1,
eval,
output;`;
export const basic_tst = `// 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/2/ALU-basic.tst
// Tests the basic version of the ALU chip.
// DOES NOT replace the final test provided by ALU.tst.
// Specifically: Tests the ALU logic that computes the 'out' output;
// The 'zr' and 'ng' output bits are ignored.
output-list x%B1.16.1 y%B1.16.1 zx nx zy ny f no out%B1.16.1;
set x %B0000000000000000,
set y %B1111111111111111,
set zx 1,
set nx 0,
set zy 1,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 1,
set nx 1,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 1,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 0,
set no 0,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 0,
set no 0,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 0,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 0,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 1,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 1,
set no 0,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 0,
set nx 0,
set zy 0,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 0,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 0,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 0,
set ny 0,
set f 0,
set no 0,
eval,
output;
set zx 0,
set nx 1,
set zy 0,
set ny 1,
set f 0,
set no 1,
eval,
output;
set x %B101101110100000,
set y %B001111011010010,
set zx 1,
set nx 0,
set zy 1,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 1,
set nx 1,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 1,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 0,
set no 0,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 0,
set no 0,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 0,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 0,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 1,
set zy 1,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 1,
set ny 1,
set f 1,
set no 0,
eval,
output;
set zx 1,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 0,
set nx 0,
set zy 0,
set ny 0,
set f 1,
set no 0,
eval,
output;
set zx 0,
set nx 1,
set zy 0,
set ny 0,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 0,
set ny 1,
set f 1,
set no 1,
eval,
output;
set zx 0,
set nx 0,
set zy 0,
set ny 0,
set f 0,
set no 0,
eval,
output;
set zx 0,
set nx 1,
set zy 0,
set ny 1,
set f 0,
set no 1,
eval,
output;`;
export const basic_cmp = `| x | y |zx |nx |zy |ny | f |no | out |
| 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 |
| 0000000000000000 | 1111111111111111 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 0 | 1111111111111111 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 0 | 1111111111111110 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 1 | 0 | 1111111111111111 |
| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111111111 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 1 | 0 | 1 | 1111111111111111 |
| 0101101110100000 | 0001111011010010 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 0 | 0 | 0101101110100000 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 0 | 0 | 0001111011010010 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 0 | 1 | 1010010001011111 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 0 | 1 | 1110000100101101 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 1 | 1 | 1010010001100000 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 1 | 1 | 1110000100101110 |
| 0101101110100000 | 0001111011010010 | 0 | 1 | 1 | 1 | 1 | 1 | 0101101110100001 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 1 | 1 | 1 | 0001111011010011 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 1 | 1 | 1 | 0 | 0101101110011111 |
| 0101101110100000 | 0001111011010010 | 1 | 1 | 0 | 0 | 1 | 0 | 0001111011010001 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 0 | 1 | 0 | 0111101001110010 |
| 0101101110100000 | 0001111011010010 | 0 | 1 | 0 | 0 | 1 | 1 | 0011110011001110 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 1 | 1 | 1 | 1100001100110010 |
| 0101101110100000 | 0001111011010010 | 0 | 0 | 0 | 0 | 0 | 0 | 0001101010000000 |
| 0101101110100000 | 0001111011010010 | 0 | 1 | 0 | 1 | 0 | 1 | 0101111111110010 |`;
@@ -0,0 +1,245 @@
export const hdl = `/**
* The ALU (Arithmetic Logic Unit).
* Computes one of the following functions:
* x+y, x-y, y-x, 0, 1, -1, x, y, -x, -y, !x, !y,
* x+1, y+1, x-1, y-1, x&y, x|y on two 16-bit inputs,
* according to 6 input bits denoted zx,nx,zy,ny,f,no.
*
* All operations:
* Ensure your solution handles these undocumented opcodes:
*
* -2, !(x&y), !(x|y), !x&y, !(!x&y), !(x&!y), x&!y
* x+y+1, x-y-1, -(x+2), -(y+2), -(x+y+2), -(x+y+1), -(x-y+1)
*
* In addition, the ALU computes two 1-bit outputs:
* if the ALU output == 0, zr is set to 1; otherwise zr is set to 0;
* if the ALU output < 0, ng is set to 1; otherwise ng is set to 0.
*/
// Implementation: the ALU logic manipulates the x and y inputs
// and operates on the resulting values, as follows:
// if (zx == 1) set x = 0 // 16-bit constant
// if (nx == 1) set x = !x // bitwise not
// if (zy == 1) set y = 0 // 16-bit constant
// if (ny == 1) set y = !y // bitwise not
// if (f == 1) set out = x + y // integer 2's complement addition
// if (f == 0) set out = x & y // bitwise and
// if (no == 1) set out = !out // bitwise not
// if (out == 0) set zr = 1
// if (out < 0) set ng = 1
// Testing: the test script below has tests for the undocumented instructions
// listed first, and then has the tests for the documented ALU instructions.
CHIP ALU {
IN
x[16], y[16], // 16-bit inputs
zx, // zero the x input?
nx, // negate the x input?
zy, // zero the y input?
ny, // negate the y input?
f, // compute out = x + y (if 1) or x & y (if 0)
no; // negate the out output?
OUT
out[16], // 16-bit output
zr, // 1 if (out == 0), 0 otherwise
ng; // 1 if (out < 0), 0 otherwise
PARTS:
// Put you code here:
}`;
export const cmp = `| x | y |zx |nx |zy |ny | f |no | out |zr |ng |
| 0000000000000000 | 0000000000000000 | 1 | 1 | 1 | 1 | 1 | 0 | 1111111111111110 | 0 | 1 |
| 0000000000000011 | 0000000000000101 | 0 | 0 | 0 | 0 | 0 | 1 | 1111111111111110 | 0 | 1 |
| 0000000000000011 | 0000000000000101 | 0 | 1 | 0 | 1 | 0 | 0 | 1111111111111000 | 0 | 1 |
| 0000000000000011 | 0000000000000101 | 0 | 1 | 0 | 0 | 0 | 0 | 1111111111110100 | 0 | 1 |
| 0000000000000011 | 0000000000000101 | 0 | 1 | 0 | 0 | 0 | 1 | 1111111111111011 | 0 | 1 |
| 0000000000000011 | 0000000000000101 | 0 | 0 | 0 | 1 | 0 | 1 | 1111111111111101 | 0 | 1 |
| 0000000000000011 | 0000000000000101 | 0 | 0 | 0 | 1 | 0 | 0 | 1111111111110010 | 0 | 1 |
| 0000000000001101 | 0000000000010011 | 0 | 1 | 0 | 1 | 1 | 1 | 0000000000100001 | 0 | 0 |
| 0000000000001101 | 0000000000010011 | 0 | 0 | 0 | 1 | 1 | 0 | 1111111111111001 | 0 | 1 |
| 0000000000001101 | 0000000000010011 | 0 | 1 | 1 | 1 | 1 | 0 | 1111111111110001 | 0 | 1 |
| 0000000000001101 | 0000000000010011 | 1 | 1 | 0 | 1 | 1 | 0 | 1111111111101011 | 0 | 1 |
| 0000000000001101 | 0000000000010011 | 0 | 1 | 0 | 1 | 1 | 0 | 1111111111011110 | 0 | 1 |
| 0000000000001101 | 0000000000010011 | 0 | 0 | 0 | 0 | 1 | 1 | 1111111111011111 | 0 | 1 |
| 0000000000001101 | 0000000000010011 | 0 | 1 | 0 | 0 | 1 | 0 | 0000000000000101 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 0 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 0 | 1 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 1 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 0 | 0 |
| 0000000000000000 | 1111111111111111 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 1 | 1 | 1 | 0 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 1 | 1 | 0 | 0 | 1 | 0 | 1111111111111110 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000000001 | 0 | 0 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111111111 | 0 | 1 |
| 0000000000000000 | 1111111111111111 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000000 | 1 | 0 |
| 0000000000000000 | 1111111111111111 | 0 | 1 | 0 | 1 | 0 | 1 | 1111111111111111 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 1 | 0 | 1 | 0 | 1 | 0 | 0000000000000000 | 1 | 0 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 1 | 1 | 1 | 1 | 0000000000000001 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 1 | 0 | 1 | 0 | 1111111111111111 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 0 | 0 | 0000000000010001 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 0 | 0 | 0000000000000011 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 0 | 1 | 1111111111101110 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 0 | 1 | 1111111111111100 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 1 | 1 | 1111111111101111 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 1 | 1 | 1111111111111101 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 0 | 1 | 1 | 1 | 1 | 1 | 0000000000010010 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 1 | 1 | 1 | 0000000000000100 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 1 | 1 | 1 | 0 | 0000000000010000 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 1 | 1 | 0 | 0 | 1 | 0 | 0000000000000010 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 0 | 1 | 0 | 0000000000010100 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 0 | 1 | 0 | 0 | 1 | 1 | 0000000000001110 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 1 | 1 | 1 | 1111111111110010 | 0 | 1 |
| 0000000000010001 | 0000000000000011 | 0 | 0 | 0 | 0 | 0 | 0 | 0000000000000001 | 0 | 0 |
| 0000000000010001 | 0000000000000011 | 0 | 1 | 0 | 1 | 0 | 1 | 0000000000010011 | 0 | 0 |`;
export const tst = `output-list x%B1.16.1 y%B1.16.1 zx%B1.1.1 nx%B1.1.1 zy%B1.1.1
ny%B1.1.1 f%B1.1.1 no%B1.1.1 out%B1.16.1 zr%B1.1.1
ng%B1.1.1;
// Compute -2
set x %B0000000000000000,
set y %B0000000000000000,
set zx 1, set nx 1, set zy 1, set ny 1, set f 1, set no 0, eval, output;
set x %B0000000000000011,
set y %B0000000000000101;
set zx 0, set nx 0, set zy 0, set ny 0, set f 0, set no 1, eval, output;
set zx 0, set nx 1, set zy 0, set ny 1, set f 0, set no 0, eval, output;
set zx 0, set nx 1, set zy 0, set ny 0, set f 0, set no 0, eval, output;
set zx 0, set nx 1, set zy 0, set ny 0, set f 0, set no 1, eval, output;
set zx 0, set nx 0, set zy 0, set ny 1, set f 0, set no 1, eval, output;
set zx 0, set nx 0, set zy 0, set ny 1, set f 0, set no 0, eval, output;
set x %B0000000000001101, // x = 13
set y %B0000000000010011; // y = 19
set zx 0, set nx 1, set zy 0, set ny 1, set f 1, set no 1, eval, output;
set zx 0, set nx 0, set zy 0, set ny 1, set f 1, set no 0, eval, output;
set zx 0, set nx 1, set zy 1, set ny 1, set f 1, set no 0, eval, output;
set zx 1, set nx 1, set zy 0, set ny 1, set f 1, set no 0, eval, output;
set zx 0, set nx 1, set zy 0, set ny 1, set f 1, set no 0, eval, output;
set zx 0, set nx 0, set zy 0, set ny 0, set f 1, set no 1, eval, output;
set zx 0, set nx 1, set zy 0, set ny 0, set f 1, set no 0, eval, output;
set x %B0000000000000000, // x = 0
set y %B1111111111111111; // y = -1
// Compute 0
set zx 1, set nx 0, set zy 1, set ny 0, set f 1, set no 0, eval, output;
// Compute 1
set zx 1, set nx 1, set zy 1, set ny 1, set f 1, set no 1, eval, output;
// Compute -1
set zx 1, set nx 1, set zy 1, set ny 0, set f 1, set no 0, eval, output;
// Compute x
set zx 0, set nx 0, set zy 1, set ny 1, set f 0, set no 0, eval, output;
// Compute y
set zx 1, set nx 1, set zy 0, set ny 0, set f 0, set no 0, eval, output;
// Compute !x
set zx 0, set nx 0, set zy 1, set ny 1, set f 0, set no 1, eval, output;
// Compute !y
set zx 1, set nx 1, set zy 0, set ny 0, set f 0, set no 1, eval, output;
// Compute -x
set zx 0, set nx 0, set zy 1, set ny 1, set f 1, set no 1, eval, output;
// Compute -y
set zx 1, set nx 1, set zy 0, set ny 0, set f 1, set no 1, eval, output;
// Compute x + 1
set zx 0, set nx 1, set zy 1, set ny 1, set f 1, set no 1, eval, output;
// Compute y + 1
set zx 1, set nx 1, set zy 0, set ny 1, set f 1, set no 1, eval, output;
// Compute x - 1
set zx 0, set nx 0, set zy 1, set ny 1, set f 1, set no 0, eval, output;
// Compute y - 1
set zx 1, set nx 1, set zy 0, set ny 0, set f 1, set no 0, eval, output;
// Compute x + y
set zx 0, set nx 0, set zy 0, set ny 0, set f 1, set no 0, eval, output;
// Compute x - y
set zx 0, set nx 1, set zy 0, set ny 0, set f 1, set no 1, eval, output;
// Compute y - x
set zx 0, set nx 0, set zy 0, set ny 1, set f 1, set no 1, eval, output;
// Compute x & y
set zx 0, set nx 0, set zy 0, set ny 0, set f 0, set no 0, eval, output;
// Compute x | y
set zx 0, set nx 1, set zy 0, set ny 1, set f 0, set no 1, eval, output;
set x %B000000000010001, // x = 17
set y %B000000000000011; // y = 3
// Compute 0
set zx 1, set nx 0, set zy 1, set ny 0, set f 1, set no 0, eval, output;
// Compute 1
set zx 1, set nx 1, set zy 1, set ny 1, set f 1, set no 1, eval, output;
// Compute -1
set zx 1, set nx 1, set zy 1, set ny 0, set f 1, set no 0, eval, output;
// Compute x
set zx 0, set nx 0, set zy 1, set ny 1, set f 0, set no 0, eval, output;
// Compute y
set zx 1, set nx 1, set zy 0, set ny 0, set f 0, set no 0, eval, output;
// Compute !x
set zx 0, set nx 0, set zy 1, set ny 1, set f 0, set no 1, eval, output;
// Compute !y
set zx 1, set nx 1, set zy 0, set ny 0, set f 0, set no 1, eval, output;
// Compute -x
set zx 0, set nx 0, set zy 1, set ny 1, set f 1, set no 1, eval, output;
// Compute -y
set zx 1, set nx 1, set zy 0, set ny 0, set f 1, set no 1, eval, output;
// Compute x + 1
set zx 0, set nx 1, set zy 1, set ny 1, set f 1, set no 1, eval, output;
// Compute y + 1
set zx 1, set nx 1, set zy 0, set ny 1, set f 1, set no 1, eval, output;
// Compute x - 1
set zx 0, set nx 0, set zy 1, set ny 1, set f 1, set no 0, eval, output;
// Compute y - 1
set zx 1, set nx 1, set zy 0, set ny 0, set f 1, set no 0, eval, output;
// Compute x + y
set zx 0, set nx 0, set zy 0, set ny 0, set f 1, set no 0, eval, output;
// Compute x - y
set zx 0, set nx 1, set zy 0, set ny 0, set f 1, set no 1, eval, output;
// Compute y - x
set zx 0, set nx 0, set zy 0, set ny 1, set f 1, set no 1, eval, output;
// Compute x & y
set zx 0, set nx 0, set zy 0, set ny 0, set f 0, set no 0, eval, output;
// Compute x | y
set zx 0, set nx 1, set zy 0, set ny 1, set f 0, set no 1, eval, output;`;
@@ -0,0 +1,43 @@
import { FileSystem, reset } from "@davidsouther/jiffies/lib/esm/fs.js";
import { resetBySuffix } from "../reset.js";
import * as HalfAdder from "./01_half_adder.js";
import * as FullAdder from "./02_full_adder.js";
import * as Add16 from "./03_add16.js";
import * as Inc16 from "./04_inc16.js";
import * as Alu from "./06_alu.js";
export const CHIPS = {
"HalfAdder.hdl": HalfAdder.hdl,
"HalfAdder.tst": HalfAdder.tst,
"HalfAdder.cmp": HalfAdder.cmp,
"FullAdder.hdl": FullAdder.hdl,
"FullAdder.tst": FullAdder.tst,
"FullAdder.cmp": FullAdder.cmp,
"Add16.hdl": Add16.hdl,
"Add16.tst": Add16.tst,
"Add16.cmp": Add16.cmp,
"Inc16.hdl": Inc16.hdl,
"Inc16.tst": Inc16.tst,
"Inc16.cmp": Inc16.cmp,
"ALU.hdl": Alu.hdl,
"ALU.tst": Alu.tst,
"ALU.cmp": Alu.cmp,
"ALU-basic.tst": Alu.basic_tst,
"ALU-basic.cmp": Alu.basic_cmp,
};
export const BUILTIN_CHIPS = {};
export async function resetFiles(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/02");
await reset(fs, CHIPS);
await fs.popd();
}
export async function resetTests(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/02");
await resetBySuffix(fs, CHIPS, ".tst");
await resetBySuffix(fs, CHIPS, ".cmp");
await fs.popd();
}
@@ -0,0 +1,17 @@
export const hdl = `// 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/03/DFF.hdl
/**
* Data Flip-flop:
* maintains its current value:
* out(t+1) = in(t)
*/
CHIP DFF {
IN in;
OUT out;
PARTS:
BUILTIN DFF;
CLOCKED in;
}`;
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,735 @@
export const hdl = `// 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/3/a/Register.hdl
/**
* 16-bit register:
* If load is asserted, the register's value is set to in;
* Otherwise, the register maintains its current value:
* if (load(t)) out(t+1) = int(t), else out(t+1) = out(t)
*/
CHIP Register {
IN in[16], load;
OUT out[16];
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/3/a/Register.tst
load Register.hdl,
compare-to Register.cmp,
output-list time%S1.3.1 in%D1.6.1 load%B2.1.1 out%D1.6.1;
set in 0,
set load 0,
tick,
output;
tock,
output;
set in 0,
set load 1,
tick,
output;
tock,
output;
set in -32123,
set load 0,
tick,
output;
tock,
output;
set in 11111,
set load 0,
tick,
output;
tock,
output;
set in -32123,
set load 1,
tick,
output;
tock,
output;
set in -32123,
set load 1,
tick,
output;
tock,
output;
set in -32123,
set load 0,
tick,
output;
tock,
output;
set in 12345,
set load 1,
tick,
output;
tock,
output;
set in 0,
set load 0,
tick,
output;
tock,
output;
set in 0,
set load 1,
tick,
output;
tock,
output;
set in %B0000000000000001,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0000000000000010,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0000000000000100,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0000000000001000,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0000000000010000,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0000000000100000,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0000000001000000,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0000000010000000,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0000000100000000,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0000001000000000,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0000010000000000,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0000100000000000,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0001000000000000,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0010000000000000,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0100000000000000,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1000000000000000,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1111111111111110,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1111111111111101,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1111111111111011,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1111111111110111,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1111111111101111,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1111111111011111,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1111111110111111,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1111111101111111,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1111111011111111,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1111110111111111,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1111101111111111,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1111011111111111,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1110111111111111,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1101111111111111,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B1011111111111111,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in %B0111111111111111,
set load 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;`;
export const cmp = `|time | in |load| out |
| 0+ | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 |
| 1+ | 0 | 1 | 0 |
| 2 | 0 | 1 | 0 |
| 2+ | -32123 | 0 | 0 |
| 3 | -32123 | 0 | 0 |
| 3+ | 11111 | 0 | 0 |
| 4 | 11111 | 0 | 0 |
| 4+ | -32123 | 1 | 0 |
| 5 | -32123 | 1 | -32123 |
| 5+ | -32123 | 1 | -32123 |
| 6 | -32123 | 1 | -32123 |
| 6+ | -32123 | 0 | -32123 |
| 7 | -32123 | 0 | -32123 |
| 7+ | 12345 | 1 | -32123 |
| 8 | 12345 | 1 | 12345 |
| 8+ | 0 | 0 | 12345 |
| 9 | 0 | 0 | 12345 |
| 9+ | 0 | 1 | 12345 |
| 10 | 0 | 1 | 0 |
| 10+ | 1 | 0 | 0 |
| 11 | 1 | 0 | 0 |
| 11+ | 1 | 1 | 0 |
| 12 | 1 | 1 | 1 |
| 12+ | 2 | 0 | 1 |
| 13 | 2 | 0 | 1 |
| 13+ | 2 | 1 | 1 |
| 14 | 2 | 1 | 2 |
| 14+ | 4 | 0 | 2 |
| 15 | 4 | 0 | 2 |
| 15+ | 4 | 1 | 2 |
| 16 | 4 | 1 | 4 |
| 16+ | 8 | 0 | 4 |
| 17 | 8 | 0 | 4 |
| 17+ | 8 | 1 | 4 |
| 18 | 8 | 1 | 8 |
| 18+ | 16 | 0 | 8 |
| 19 | 16 | 0 | 8 |
| 19+ | 16 | 1 | 8 |
| 20 | 16 | 1 | 16 |
| 20+ | 32 | 0 | 16 |
| 21 | 32 | 0 | 16 |
| 21+ | 32 | 1 | 16 |
| 22 | 32 | 1 | 32 |
| 22+ | 64 | 0 | 32 |
| 23 | 64 | 0 | 32 |
| 23+ | 64 | 1 | 32 |
| 24 | 64 | 1 | 64 |
| 24+ | 128 | 0 | 64 |
| 25 | 128 | 0 | 64 |
| 25+ | 128 | 1 | 64 |
| 26 | 128 | 1 | 128 |
| 26+ | 256 | 0 | 128 |
| 27 | 256 | 0 | 128 |
| 27+ | 256 | 1 | 128 |
| 28 | 256 | 1 | 256 |
| 28+ | 512 | 0 | 256 |
| 29 | 512 | 0 | 256 |
| 29+ | 512 | 1 | 256 |
| 30 | 512 | 1 | 512 |
| 30+ | 1024 | 0 | 512 |
| 31 | 1024 | 0 | 512 |
| 31+ | 1024 | 1 | 512 |
| 32 | 1024 | 1 | 1024 |
| 32+ | 2048 | 0 | 1024 |
| 33 | 2048 | 0 | 1024 |
| 33+ | 2048 | 1 | 1024 |
| 34 | 2048 | 1 | 2048 |
| 34+ | 4096 | 0 | 2048 |
| 35 | 4096 | 0 | 2048 |
| 35+ | 4096 | 1 | 2048 |
| 36 | 4096 | 1 | 4096 |
| 36+ | 8192 | 0 | 4096 |
| 37 | 8192 | 0 | 4096 |
| 37+ | 8192 | 1 | 4096 |
| 38 | 8192 | 1 | 8192 |
| 38+ | 16384 | 0 | 8192 |
| 39 | 16384 | 0 | 8192 |
| 39+ | 16384 | 1 | 8192 |
| 40 | 16384 | 1 | 16384 |
| 40+ | -32768 | 0 | 16384 |
| 41 | -32768 | 0 | 16384 |
| 41+ | -32768 | 1 | 16384 |
| 42 | -32768 | 1 | -32768 |
| 42+ | -2 | 0 | -32768 |
| 43 | -2 | 0 | -32768 |
| 43+ | -2 | 1 | -32768 |
| 44 | -2 | 1 | -2 |
| 44+ | -3 | 0 | -2 |
| 45 | -3 | 0 | -2 |
| 45+ | -3 | 1 | -2 |
| 46 | -3 | 1 | -3 |
| 46+ | -5 | 0 | -3 |
| 47 | -5 | 0 | -3 |
| 47+ | -5 | 1 | -3 |
| 48 | -5 | 1 | -5 |
| 48+ | -9 | 0 | -5 |
| 49 | -9 | 0 | -5 |
| 49+ | -9 | 1 | -5 |
| 50 | -9 | 1 | -9 |
| 50+ | -17 | 0 | -9 |
| 51 | -17 | 0 | -9 |
| 51+ | -17 | 1 | -9 |
| 52 | -17 | 1 | -17 |
| 52+ | -33 | 0 | -17 |
| 53 | -33 | 0 | -17 |
| 53+ | -33 | 1 | -17 |
| 54 | -33 | 1 | -33 |
| 54+ | -65 | 0 | -33 |
| 55 | -65 | 0 | -33 |
| 55+ | -65 | 1 | -33 |
| 56 | -65 | 1 | -65 |
| 56+ | -129 | 0 | -65 |
| 57 | -129 | 0 | -65 |
| 57+ | -129 | 1 | -65 |
| 58 | -129 | 1 | -129 |
| 58+ | -257 | 0 | -129 |
| 59 | -257 | 0 | -129 |
| 59+ | -257 | 1 | -129 |
| 60 | -257 | 1 | -257 |
| 60+ | -513 | 0 | -257 |
| 61 | -513 | 0 | -257 |
| 61+ | -513 | 1 | -257 |
| 62 | -513 | 1 | -513 |
| 62+ | -1025 | 0 | -513 |
| 63 | -1025 | 0 | -513 |
| 63+ | -1025 | 1 | -513 |
| 64 | -1025 | 1 | -1025 |
| 64+ | -2049 | 0 | -1025 |
| 65 | -2049 | 0 | -1025 |
| 65+ | -2049 | 1 | -1025 |
| 66 | -2049 | 1 | -2049 |
| 66+ | -4097 | 0 | -2049 |
| 67 | -4097 | 0 | -2049 |
| 67+ | -4097 | 1 | -2049 |
| 68 | -4097 | 1 | -4097 |
| 68+ | -8193 | 0 | -4097 |
| 69 | -8193 | 0 | -4097 |
| 69+ | -8193 | 1 | -4097 |
| 70 | -8193 | 1 | -8193 |
| 70+ | -16385 | 0 | -8193 |
| 71 | -16385 | 0 | -8193 |
| 71+ | -16385 | 1 | -8193 |
| 72 | -16385 | 1 | -16385 |
| 72+ | 32767 | 0 | -16385 |
| 73 | 32767 | 0 | -16385 |
| 73+ | 32767 | 1 | -16385 |
| 74 | 32767 | 1 | 32767 |`;
@@ -0,0 +1,173 @@
export const hdl = `// 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/3/a/PC.hdl
/**
* A 16-bit counter.
* if reset(t): out(t+1) = 0
* else if load(t): out(t+1) = in(t)
* else if inc(t): out(t+1) = out(t) + 1
* else out(t+1) = out(t)
*/
CHIP PC {
IN in[16], reset, load, inc;
OUT out[16];
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/03/a/PC.tst
load PC.hdl,
compare-to PC.cmp,
output-list time%S1.3.1 in%D1.6.1 reset%B2.1.2 load%B2.1.2 inc%B2.1.2 out%D1.6.1;
set in 0,
set reset 0,
set load 0,
set inc 0,
tick,
output;
tock,
output;
set inc 1,
tick,
output;
tock,
output;
set in -32123,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set load 0,
tick,
output;
tock,
output;
tick,
output;
tock,
output;
set in 12345,
set load 1,
set inc 0,
tick,
output;
tock,
output;
set reset 1,
tick,
output;
tock,
output;
set reset 0,
set inc 1,
tick,
output;
tock,
output;
set reset 1,
tick,
output;
tock,
output;
set reset 0,
set load 0,
tick,
output;
tock,
output;
set reset 1,
tick,
output;
tock,
output;
set in 0,
set reset 0,
set load 1,
tick,
output;
tock,
output;
set load 0,
set inc 1,
tick,
output;
tock,
output;
set in 22222,
set reset 1,
set inc 0,
tick,
output;
tock,
output;`;
export const cmp = `|time | in |reset|load | inc | out |
| 0+ | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 | 0 | 0 |
| 1+ | 0 | 0 | 0 | 1 | 0 |
| 2 | 0 | 0 | 0 | 1 | 1 |
| 2+ | -32123 | 0 | 0 | 1 | 1 |
| 3 | -32123 | 0 | 0 | 1 | 2 |
| 3+ | -32123 | 0 | 1 | 1 | 2 |
| 4 | -32123 | 0 | 1 | 1 | -32123 |
| 4+ | -32123 | 0 | 0 | 1 | -32123 |
| 5 | -32123 | 0 | 0 | 1 | -32122 |
| 5+ | -32123 | 0 | 0 | 1 | -32122 |
| 6 | -32123 | 0 | 0 | 1 | -32121 |
| 6+ | 12345 | 0 | 1 | 0 | -32121 |
| 7 | 12345 | 0 | 1 | 0 | 12345 |
| 7+ | 12345 | 1 | 1 | 0 | 12345 |
| 8 | 12345 | 1 | 1 | 0 | 0 |
| 8+ | 12345 | 0 | 1 | 1 | 0 |
| 9 | 12345 | 0 | 1 | 1 | 12345 |
| 9+ | 12345 | 1 | 1 | 1 | 12345 |
| 10 | 12345 | 1 | 1 | 1 | 0 |
| 10+ | 12345 | 0 | 0 | 1 | 0 |
| 11 | 12345 | 0 | 0 | 1 | 1 |
| 11+ | 12345 | 1 | 0 | 1 | 1 |
| 12 | 12345 | 1 | 0 | 1 | 0 |
| 12+ | 0 | 0 | 1 | 1 | 0 |
| 13 | 0 | 0 | 1 | 1 | 0 |
| 13+ | 0 | 0 | 0 | 1 | 0 |
| 14 | 0 | 0 | 0 | 1 | 1 |
| 14+ | 22222 | 1 | 0 | 0 | 1 |
| 15 | 22222 | 1 | 0 | 0 | 0 |`;
@@ -0,0 +1,749 @@
export const hdl = `// 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/3/a/RAM8.hdl
/**
* Memory of eight 16-bit registers.
* If load is asserted, the value of the register selected by
* address is set to in; Otherwise, the value does not change.
* The value of the selected register is emitted by out.
*/
CHIP RAM8 {
IN in[16], load, address[3];
OUT out[16];
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/3/a/RAM8.tst
load RAM8.hdl,
compare-to RAM8.cmp,
output-list time%S1.3.1 in%D1.6.1 load%B2.1.1 address%D3.1.3 out%D1.6.1;
set in 0,
set load 0,
set address 0,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set in 11111,
set load 0,
tick,
output;
tock,
output;
set load 1,
set address 1,
tick,
output;
tock,
output;
set load 0,
set address 0,
tick,
output;
tock,
output;
set in 3333,
set address 3,
tick,
output;
tock,
output;
set load 1,
tick,
output;
tock,
output;
set load 0,
tick,
output;
tock,
output;
set address 1,
eval,
output;
set in 7777,
tick,
output;
tock,
output;
set load 1,
set address 7,
tick,
output;
tock,
output;
set load 0,
tick,
output;
tock,
output;
set address 3,
eval,
output;
set address 7,
eval,
output;
set load 0,
set address 0,
tick,
output;
tock,
output;
set address 1,
eval,
output;
set address 2,
eval,
output;
set address 3,
eval,
output;
set address 4,
eval,
output;
set address 5,
eval,
output;
set address 6,
eval,
output;
set address 7,
eval,
output;
set load 1,
set in %B0101010101010101,
set address 0,
tick,
output;
tock,
output;
set address 1,
tick,
output,
tock,
output;
set address 2,
tick,
output,
tock,
output;
set address 3,
tick,
output,
tock,
output;
set address 4,
tick,
output,
tock,
output;
set address 5,
tick,
output,
tock,
output;
set address 6,
tick,
output,
tock,
output;
set address 7,
tick,
output,
tock,
output;
set load 0,
set address 0,
tick,
output;
tock,
output;
set address 1,
eval,
output;
set address 2,
eval,
output;
set address 3,
eval,
output;
set address 4,
eval,
output;
set address 5,
eval,
output;
set address 6,
eval,
output;
set address 7,
eval,
output;
set load 1,
set address 0,
set in %B1010101010101010,
tick,
output;
tock,
output;
set load 0,
set address 0,
tick,
output;
tock,
output;
set address 1,
eval,
output;
set address 2,
eval,
output;
set address 3,
eval,
output;
set address 4,
eval,
output;
set address 5,
eval,
output;
set address 6,
eval,
output;
set address 7,
eval,
output;
set load 1,
set address 0,
set in %B0101010101010101,
tick,
output,
tock,
output;
set address 1,
set in %B1010101010101010,
tick,
output;
tock,
output;
set load 0,
set address 0,
tick,
output;
tock,
output;
set address 1,
eval,
output;
set address 2,
eval,
output;
set address 3,
eval,
output;
set address 4,
eval,
output;
set address 5,
eval,
output;
set address 6,
eval,
output;
set address 7,
eval,
output;
set load 1,
set address 1,
set in %B0101010101010101,
tick,
output,
tock,
output;
set address 2,
set in %B1010101010101010,
tick,
output;
tock,
output;
set load 0,
set address 0,
tick,
output;
tock,
output;
set address 1,
eval,
output;
set address 2,
eval,
output;
set address 3,
eval,
output;
set address 4,
eval,
output;
set address 5,
eval,
output;
set address 6,
eval,
output;
set address 7,
eval,
output;
set load 1,
set address 2,
set in %B0101010101010101,
tick,
output,
tock,
output;
set address 3,
set in %B1010101010101010,
tick,
output;
tock,
output;
set load 0,
set address 0,
tick,
output;
tock,
output;
set address 1,
eval,
output;
set address 2,
eval,
output;
set address 3,
eval,
output;
set address 4,
eval,
output;
set address 5,
eval,
output;
set address 6,
eval,
output;
set address 7,
eval,
output;
set load 1,
set address 3,
set in %B0101010101010101,
tick,
output,
tock,
output;
set address 4,
set in %B1010101010101010,
tick,
output;
tock,
output;
set load 0,
set address 0,
tick,
output;
tock,
output;
set address 1,
eval,
output;
set address 2,
eval,
output;
set address 3,
eval,
output;
set address 4,
eval,
output;
set address 5,
eval,
output;
set address 6,
eval,
output;
set address 7,
eval,
output;
set load 1,
set address 4,
set in %B0101010101010101,
tick,
output,
tock,
output;
set address 5,
set in %B1010101010101010,
tick,
output;
tock,
output;
set load 0,
set address 0,
tick,
output;
tock,
output;
set address 1,
eval,
output;
set address 2,
eval,
output;
set address 3,
eval,
output;
set address 4,
eval,
output;
set address 5,
eval,
output;
set address 6,
eval,
output;
set address 7,
eval,
output;
set load 1,
set address 5,
set in %B0101010101010101,
tick,
output,
tock,
output;
set address 6,
set in %B1010101010101010,
tick,
output;
tock,
output;
set load 0,
set address 0,
tick,
output;
tock,
output;
set address 1,
eval,
output;
set address 2,
eval,
output;
set address 3,
eval,
output;
set address 4,
eval,
output;
set address 5,
eval,
output;
set address 6,
eval,
output;
set address 7,
eval,
output;
set load 1,
set address 6,
set in %B0101010101010101,
tick,
output,
tock,
output;
set address 7,
set in %B1010101010101010,
tick,
output;
tock,
output;
set load 0,
set address 0,
tick,
output;
tock,
output;
set address 1,
eval,
output;
set address 2,
eval,
output;
set address 3,
eval,
output;
set address 4,
eval,
output;
set address 5,
eval,
output;
set address 6,
eval,
output;
set address 7,
eval,
output;
set load 1,
set address 7,
set in %B0101010101010101,
tick,
output,
tock,
output;
set load 0,
set address 0,
tick,
output;
tock,
output;
set address 1,
eval,
output;
set address 2,
eval,
output;
set address 3,
eval,
output;
set address 4,
eval,
output;
set address 5,
eval,
output;
set address 6,
eval,
output;
set address 7,
eval,
output;
`;
export const cmp = `|time | in |load|address| out |
| 0+ | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 | 0 |
| 1+ | 0 | 1 | 0 | 0 |
| 2 | 0 | 1 | 0 | 0 |
| 2+ | 11111 | 0 | 0 | 0 |
| 3 | 11111 | 0 | 0 | 0 |
| 3+ | 11111 | 1 | 1 | 0 |
| 4 | 11111 | 1 | 1 | 11111 |
| 4+ | 11111 | 0 | 0 | 0 |
| 5 | 11111 | 0 | 0 | 0 |
| 5+ | 3333 | 0 | 3 | 0 |
| 6 | 3333 | 0 | 3 | 0 |
| 6+ | 3333 | 1 | 3 | 0 |
| 7 | 3333 | 1 | 3 | 3333 |
| 7+ | 3333 | 0 | 3 | 3333 |
| 8 | 3333 | 0 | 3 | 3333 |
| 8 | 3333 | 0 | 1 | 11111 |
| 8+ | 7777 | 0 | 1 | 11111 |
| 9 | 7777 | 0 | 1 | 11111 |
| 9+ | 7777 | 1 | 7 | 0 |
| 10 | 7777 | 1 | 7 | 7777 |
| 10+ | 7777 | 0 | 7 | 7777 |
| 11 | 7777 | 0 | 7 | 7777 |
| 11 | 7777 | 0 | 3 | 3333 |
| 11 | 7777 | 0 | 7 | 7777 |
| 11+ | 7777 | 0 | 0 | 0 |
| 12 | 7777 | 0 | 0 | 0 |
| 12 | 7777 | 0 | 1 | 11111 |
| 12 | 7777 | 0 | 2 | 0 |
| 12 | 7777 | 0 | 3 | 3333 |
| 12 | 7777 | 0 | 4 | 0 |
| 12 | 7777 | 0 | 5 | 0 |
| 12 | 7777 | 0 | 6 | 0 |
| 12 | 7777 | 0 | 7 | 7777 |
| 12+ | 21845 | 1 | 0 | 0 |
| 13 | 21845 | 1 | 0 | 21845 |
| 13+ | 21845 | 1 | 1 | 11111 |
| 14 | 21845 | 1 | 1 | 21845 |
| 14+ | 21845 | 1 | 2 | 0 |
| 15 | 21845 | 1 | 2 | 21845 |
| 15+ | 21845 | 1 | 3 | 3333 |
| 16 | 21845 | 1 | 3 | 21845 |
| 16+ | 21845 | 1 | 4 | 0 |
| 17 | 21845 | 1 | 4 | 21845 |
| 17+ | 21845 | 1 | 5 | 0 |
| 18 | 21845 | 1 | 5 | 21845 |
| 18+ | 21845 | 1 | 6 | 0 |
| 19 | 21845 | 1 | 6 | 21845 |
| 19+ | 21845 | 1 | 7 | 7777 |
| 20 | 21845 | 1 | 7 | 21845 |
| 20+ | 21845 | 0 | 0 | 21845 |
| 21 | 21845 | 0 | 0 | 21845 |
| 21 | 21845 | 0 | 1 | 21845 |
| 21 | 21845 | 0 | 2 | 21845 |
| 21 | 21845 | 0 | 3 | 21845 |
| 21 | 21845 | 0 | 4 | 21845 |
| 21 | 21845 | 0 | 5 | 21845 |
| 21 | 21845 | 0 | 6 | 21845 |
| 21 | 21845 | 0 | 7 | 21845 |
| 21+ | -21846 | 1 | 0 | 21845 |
| 22 | -21846 | 1 | 0 | -21846 |
| 22+ | -21846 | 0 | 0 | -21846 |
| 23 | -21846 | 0 | 0 | -21846 |
| 23 | -21846 | 0 | 1 | 21845 |
| 23 | -21846 | 0 | 2 | 21845 |
| 23 | -21846 | 0 | 3 | 21845 |
| 23 | -21846 | 0 | 4 | 21845 |
| 23 | -21846 | 0 | 5 | 21845 |
| 23 | -21846 | 0 | 6 | 21845 |
| 23 | -21846 | 0 | 7 | 21845 |
| 23+ | 21845 | 1 | 0 | -21846 |
| 24 | 21845 | 1 | 0 | 21845 |
| 24+ | -21846 | 1 | 1 | 21845 |
| 25 | -21846 | 1 | 1 | -21846 |
| 25+ | -21846 | 0 | 0 | 21845 |
| 26 | -21846 | 0 | 0 | 21845 |
| 26 | -21846 | 0 | 1 | -21846 |
| 26 | -21846 | 0 | 2 | 21845 |
| 26 | -21846 | 0 | 3 | 21845 |
| 26 | -21846 | 0 | 4 | 21845 |
| 26 | -21846 | 0 | 5 | 21845 |
| 26 | -21846 | 0 | 6 | 21845 |
| 26 | -21846 | 0 | 7 | 21845 |
| 26+ | 21845 | 1 | 1 | -21846 |
| 27 | 21845 | 1 | 1 | 21845 |
| 27+ | -21846 | 1 | 2 | 21845 |
| 28 | -21846 | 1 | 2 | -21846 |
| 28+ | -21846 | 0 | 0 | 21845 |
| 29 | -21846 | 0 | 0 | 21845 |
| 29 | -21846 | 0 | 1 | 21845 |
| 29 | -21846 | 0 | 2 | -21846 |
| 29 | -21846 | 0 | 3 | 21845 |
| 29 | -21846 | 0 | 4 | 21845 |
| 29 | -21846 | 0 | 5 | 21845 |
| 29 | -21846 | 0 | 6 | 21845 |
| 29 | -21846 | 0 | 7 | 21845 |
| 29+ | 21845 | 1 | 2 | -21846 |
| 30 | 21845 | 1 | 2 | 21845 |
| 30+ | -21846 | 1 | 3 | 21845 |
| 31 | -21846 | 1 | 3 | -21846 |
| 31+ | -21846 | 0 | 0 | 21845 |
| 32 | -21846 | 0 | 0 | 21845 |
| 32 | -21846 | 0 | 1 | 21845 |
| 32 | -21846 | 0 | 2 | 21845 |
| 32 | -21846 | 0 | 3 | -21846 |
| 32 | -21846 | 0 | 4 | 21845 |
| 32 | -21846 | 0 | 5 | 21845 |
| 32 | -21846 | 0 | 6 | 21845 |
| 32 | -21846 | 0 | 7 | 21845 |
| 32+ | 21845 | 1 | 3 | -21846 |
| 33 | 21845 | 1 | 3 | 21845 |
| 33+ | -21846 | 1 | 4 | 21845 |
| 34 | -21846 | 1 | 4 | -21846 |
| 34+ | -21846 | 0 | 0 | 21845 |
| 35 | -21846 | 0 | 0 | 21845 |
| 35 | -21846 | 0 | 1 | 21845 |
| 35 | -21846 | 0 | 2 | 21845 |
| 35 | -21846 | 0 | 3 | 21845 |
| 35 | -21846 | 0 | 4 | -21846 |
| 35 | -21846 | 0 | 5 | 21845 |
| 35 | -21846 | 0 | 6 | 21845 |
| 35 | -21846 | 0 | 7 | 21845 |
| 35+ | 21845 | 1 | 4 | -21846 |
| 36 | 21845 | 1 | 4 | 21845 |
| 36+ | -21846 | 1 | 5 | 21845 |
| 37 | -21846 | 1 | 5 | -21846 |
| 37+ | -21846 | 0 | 0 | 21845 |
| 38 | -21846 | 0 | 0 | 21845 |
| 38 | -21846 | 0 | 1 | 21845 |
| 38 | -21846 | 0 | 2 | 21845 |
| 38 | -21846 | 0 | 3 | 21845 |
| 38 | -21846 | 0 | 4 | 21845 |
| 38 | -21846 | 0 | 5 | -21846 |
| 38 | -21846 | 0 | 6 | 21845 |
| 38 | -21846 | 0 | 7 | 21845 |
| 38+ | 21845 | 1 | 5 | -21846 |
| 39 | 21845 | 1 | 5 | 21845 |
| 39+ | -21846 | 1 | 6 | 21845 |
| 40 | -21846 | 1 | 6 | -21846 |
| 40+ | -21846 | 0 | 0 | 21845 |
| 41 | -21846 | 0 | 0 | 21845 |
| 41 | -21846 | 0 | 1 | 21845 |
| 41 | -21846 | 0 | 2 | 21845 |
| 41 | -21846 | 0 | 3 | 21845 |
| 41 | -21846 | 0 | 4 | 21845 |
| 41 | -21846 | 0 | 5 | 21845 |
| 41 | -21846 | 0 | 6 | -21846 |
| 41 | -21846 | 0 | 7 | 21845 |
| 41+ | 21845 | 1 | 6 | -21846 |
| 42 | 21845 | 1 | 6 | 21845 |
| 42+ | -21846 | 1 | 7 | 21845 |
| 43 | -21846 | 1 | 7 | -21846 |
| 43+ | -21846 | 0 | 0 | 21845 |
| 44 | -21846 | 0 | 0 | 21845 |
| 44 | -21846 | 0 | 1 | 21845 |
| 44 | -21846 | 0 | 2 | 21845 |
| 44 | -21846 | 0 | 3 | 21845 |
| 44 | -21846 | 0 | 4 | 21845 |
| 44 | -21846 | 0 | 5 | 21845 |
| 44 | -21846 | 0 | 6 | 21845 |
| 44 | -21846 | 0 | 7 | -21846 |
| 44+ | 21845 | 1 | 7 | -21846 |
| 45 | 21845 | 1 | 7 | 21845 |
| 45+ | 21845 | 0 | 0 | 21845 |
| 46 | 21845 | 0 | 0 | 21845 |
| 46 | 21845 | 0 | 1 | 21845 |
| 46 | 21845 | 0 | 2 | 21845 |
| 46 | 21845 | 0 | 3 | 21845 |
| 46 | 21845 | 0 | 4 | 21845 |
| 46 | 21845 | 0 | 5 | 21845 |
| 46 | 21845 | 0 | 6 | 21845 |
| 46 | 21845 | 0 | 7 | 21845 |`;
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,56 @@
import { FileSystem, reset } from "@davidsouther/jiffies/lib/esm/fs.js";
import { resetBySuffix } from "../reset.js";
import * as DFF from "./00_dff.js";
import * as Bit from "./01_bit.js";
import * as Register from "./02_register.js";
import * as PC from "./03_pc.js";
import * as RAM8 from "./04_ram8.js";
import * as RAM64 from "./05_ram64.js";
import * as RAM512 from "./06_ram512.js";
import * as RAM4k from "./07_ram4k.js";
import * as RAM16k from "./08_ram16k.js";
export const CHIPS = {
"Bit.hdl": Bit.hdl,
"Bit.tst": Bit.tst,
"Bit.cmp": Bit.cmp,
"Register.hdl": Register.hdl,
"Register.tst": Register.tst,
"Register.cmp": Register.cmp,
"PC.hdl": PC.hdl,
"PC.tst": PC.tst,
"PC.cmp": PC.cmp,
"RAM8.hdl": RAM8.hdl,
"RAM8.tst": RAM8.tst,
"RAM8.cmp": RAM8.cmp,
"RAM64.hdl": RAM64.hdl,
"RAM64.tst": RAM64.tst,
"RAM64.cmp": RAM64.cmp,
"RAM512.hdl": RAM512.hdl,
"RAM512.tst": RAM512.tst,
"RAM512.cmp": RAM512.cmp,
"RAM4K.hdl": RAM4k.hdl,
"RAM4K.tst": RAM4k.tst,
"RAM4K.cmp": RAM4k.cmp,
"RAM16K.hdl": RAM16k.hdl,
"RAM16K.tst": RAM16k.tst,
"RAM16K.cmp": RAM16k.cmp,
};
export const BUILTIN_CHIPS = {
DFF: DFF.hdl,
};
export async function resetFiles(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/03");
await reset(fs, CHIPS);
await fs.popd();
}
export async function resetTests(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/03");
await resetBySuffix(fs, CHIPS, ".tst");
await resetBySuffix(fs, CHIPS, ".cmp");
await fs.popd();
}
@@ -0,0 +1,96 @@
export const asm = `// 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/4/Mult.asm
// Multiplies R0 and R1 and stores the result in R2.
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
// The algorithm is based on repetitive addition.
//// Replace this comment with your code.`;
export const tst = `// 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/4/mult/Mult.tst
// Tests the Mult program, designed to compute R2 = R0 * R1.
// Tests the program by having it multiply several sets of
// R0 and R1 values.
load Mult.asm,
compare-to Mult.cmp,
output-list RAM[0]%D2.6.2 RAM[1]%D2.6.2 RAM[2]%D2.6.2;
set RAM[0] 0, // Sets R0 and R1 to some input values
set RAM[1] 0,
set RAM[2] -1; // Ensures that the program initialized R2 to 0
repeat 20 {
ticktock;
}
set RAM[0] 0, // Restores R0 and R1 in case the program changed them
set RAM[1] 0,
output;
set PC 0,
set RAM[0] 1, // Sets R0 and R1 to some input values
set RAM[1] 0,
set RAM[2] -1; // Ensures that the program initialized R2 to 0
repeat 50 {
ticktock;
}
set RAM[0] 1, // Restores R0 and R1 in case the program changed them
set RAM[1] 0,
output;
set PC 0,
set RAM[0] 0, // Sets R0 and R1 to some input values
set RAM[1] 2,
set RAM[2] -1; // Ensures that the program initialized R2 to 0
repeat 80 {
ticktock;
}
set RAM[0] 0, // Restores R0 and R1 in case the program changed them
set RAM[1] 2,
output;
set PC 0,
set RAM[0] 3, // Sets R0 and R1 to some input values
set RAM[1] 1,
set RAM[2] -1; // Ensures that the program initialized R2 to 0
repeat 120 {
ticktock;
}
set RAM[0] 3, // Restores R0 and R1 in case the program changed them
set RAM[1] 1,
output;
set PC 0,
set RAM[0] 2, // Sets R0 and R1 to some input values
set RAM[1] 4,
set RAM[2] -1; // Ensures that the program initialized R2 to 0
repeat 150 {
ticktock;
}
set RAM[0] 2, // Restores R0 and R1 in case the program changed them
set RAM[1] 4,
output;
set PC 0,
set RAM[0] 6, // Sets R0 and R1 to some input values
set RAM[1] 7,
set RAM[2] -1; // Ensures that the program initialized R2 to 0
repeat 210 {
ticktock;
}
set RAM[0] 6, // Restores R0 and R1 in case the program changed them
set RAM[1] 7,
output;`;
export const cmp = `| RAM[0] | RAM[1] | RAM[2] |
| 0 | 0 | 0 |
| 1 | 0 | 0 |
| 0 | 2 | 0 |
| 3 | 1 | 3 |
| 2 | 4 | 8 |
| 6 | 7 | 42 |`;
@@ -0,0 +1,66 @@
export const asm = `// 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/4/Fill.asm
// Runs an infinite loop that listens to the keyboard input.
// When a key is pressed (any key), the program blackens the screen,
// i.e. writes "black" in every pixel. When no key is pressed,
// the screen should be cleared.
//// Replace this comment with your code.`;
export const tst = `// 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/4/fill/Fill.tst
// Tests the Fill.hack program in the CPU emulator.
load Fill.asm;
echo "Select the highest speed and 'enable keyboard'. Then press any key for some time, and inspect the screen.";
repeat {
ticktock;
}`;
export const autoTst = `// 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/4/fill/FillAutomatic
// This script can be used to test the Fill program automatically,
// rather than interactively. Specifically, the script sets the keyboard
// memory map (RAM[24576]) to 0, 1, and then again to 0. This simulates the
// acts of leaving the keyboard untouched, pressing some key, and then releasing
// the key. After each one of these simulated events, the script outputs the values
// of some selected registers from the screen memory map (RAM[16384]-RAM[24576]).
// This is done in order to test that these registers are set to 000...0 or 111....1,
// as mandated by how the Fill program should react to the keyboard events.
load Fill.asm,
compare-to FillAutomatic.cmp,
output-list RAM[16384]%D2.6.2 RAM[17648]%D2.6.2 RAM[18349]%D2.6.2 RAM[19444]%D2.6.2 RAM[20771]%D2.6.2 RAM[21031]%D2.6.2 RAM[22596]%D2.6.2 RAM[23754]%D2.6.2 RAM[24575]%D2.6.2;
set RAM[24576] 0, // the keyboard is untouched
repeat 1000000 {
ticktock;
}
output; // tests that the screen is white
set RAM[24576] 1, // a keyboard key is pressed
repeat 1000000 {
ticktock;
}
output; // tests that the screen is black
set RAM[24576] 0, // the keyboard is untouched
repeat 1000000 {
ticktock;
}
output; // tests that the screen is white`;
export const autoCmp = `|RAM[16384]|RAM[17648]|RAM[18349]|RAM[19444]|RAM[20771]|RAM[21031]|RAM[22596]|RAM[23754]|RAM[24575]|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |`;
@@ -0,0 +1,31 @@
import { FileSystem, reset } from "@davidsouther/jiffies/lib/esm/fs.js";
import { resetBySuffix } from "../reset.js";
import * as Mult from "./01_mult.js";
import * as Fill from "./02_fill.js";
export const TESTS = {
Mult: {
"Mult.asm": Mult.asm,
"Mult.tst": Mult.tst,
"Mult.cmp": Mult.cmp,
},
Fill: {
"Fill.asm": Fill.asm,
"Fill.tst": Fill.tst,
"FillAutomatic.tst": Fill.autoTst,
"FillAutomatic.cmp": Fill.autoCmp,
},
};
export async function resetFiles(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/04");
await reset(fs, TESTS);
await fs.popd();
}
export async function resetTests(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/04");
await resetBySuffix(fs, TESTS, ".tst");
await resetBySuffix(fs, TESTS, ".cmp");
await fs.popd();
}
@@ -0,0 +1,287 @@
export const hdl = `// 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/Memory.hdl
/**
* The complete address space of the Hack computer's memory,
* including RAM and memory-mapped I/O.
* The chip facilitates read and write operations, as follows:
* Read: out(t) = Memory[address(t)](t)
* Write: if load(t-1) then Memory[address(t-1)](t) = in(t-1)
* In words: the chip always outputs the value stored at the memory
* location specified by address. If load=1, the in value is loaded
* into the memory location specified by address. This value becomes
* available through the out output from the next time step onward.
* Address space rules:
* Only the upper 16K+8K+1 words of the Memory chip are used.
* Access to address>0x6000 is invalid and reads 0. Access to any address
* in the range 0x4000-0x5FFF results in accessing the screen memory
* map. Access to address 0x6000 results in accessing the keyboard
* memory map. The behavior in these addresses is described in the Screen
* and Keyboard chip specifications given in the lectures and the book.
*/
CHIP Memory {
IN in[16], load, address[15];
OUT out[16];
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/05/Memory.tst
// Tests the Memory chip by inputting values to selected addresses,
// verifying that these addresses were indeed written to, and verifying
// that other addresses were not accessed by mistake. In particular, we
// focus on probing the registers in addresses 'lower RAM', 'upper RAM',
// and 'Screen', which correspond to 0, %X2000, and %X4000 in Hexadecimal
// (0, 8192 (8K), and 16385 (16K+1) in decimal).
load Memory.hdl,
compare-to Memory.cmp,
output-list in%D1.6.1 load%B2.1.2 address%B1.15.1 out%D1.6.1;
echo "Before you run this script, select the 'Screen' option from the 'View' menu";
// We've noticed a common design mistake in several students' Memory.hdl files.
// This error leads to zeros being written in the offset of inactive memory segments
// instead of the intended location. To identify this issue, the test should check not
// only for incorrect writes into the wrong segment but also for any unexpected changes.
// To prepare for this, we've initialized the memory with a specific number in the areas
// where these erroneous writes might happen.
//// Sets RAM[2000], RAM[4000] = 12345 (for the following overwrite test)
set in 12345, set load 1, set address %X2000, tick, output; tock, output;
set address %X4000, tick, output; tock, output;
set in -1, // Sets RAM[0] = -1
set load 1,
set address 0,
tick,
output;
tock,
output;
set in 9999, // RAM[0] holds value
set load 0,
tick,
output;
tock,
output;
set address %X2000, // Did not also write to upper RAM or Screen
eval,
output;
set address %X4000,
eval,
output;
//// Sets RAM[0], RAM[4000] = 12345 (for following overwrite test)
set in 12345, set load 1, set address %X0000, tick, output; tock, output;
set address %X4000, tick, output; tock, output;
set in 2222, // Sets RAM[2000] = 2222
set load 1,
set address %X2000,
tick,
output;
tock,
output;
set in 9999, // RAM[2000] holds value
set load 0,
tick,
output;
tock,
output;
set address 0, // Did not also write to lower RAM or Screen
eval,
output;
set address %X4000,
eval,
output;
set load 0, // Low order address bits connected
set address %X0001, eval, output;
set address %X0002, eval, output;
set address %X0004, eval, output;
set address %X0008, eval, output;
set address %X0010, eval, output;
set address %X0020, eval, output;
set address %X0040, eval, output;
set address %X0080, eval, output;
set address %X0100, eval, output;
set address %X0200, eval, output;
set address %X0400, eval, output;
set address %X0800, eval, output;
set address %X1000, eval, output;
set address %X2000, eval, output;
set address %X1234, // RAM[1234] = 1234
set in 1234,
set load 1,
tick,
output;
tock,
output;
set load 0,
set address %X2234, // Did not also write to upper RAM or Screen
eval, output;
set address %X6234,
eval, output;
set address %X2345, // RAM[2345] = 2345
set in 2345,
set load 1,
tick,
output;
tock,
output;
set load 0,
set address %X0345, // Did not also write to lower RAM or Screen
eval, output;
set address %X4345,
eval, output;
//// Clears the overwrite detection value from the screen
set in 0, set load 1, set address %X4000, tick, output; tock, output;
// Keyboard test
set address 24576,
echo "Click the Keyboard icon and hold down the 'K' key (uppercase) until you see the next message...",
// It's important to keep holding the key down since if the system is busy,
// the memory will zero itself before being outputted.
while out <> 75 {
tick, tock; // tick, tock prevents hang if sync. parts used in KB path.
}
clear-echo,
output;
// Screen test
//// Sets RAM[0FCF], RAM[2FCF] = 12345 (for following overwrite test)
set in 12345, set load 1, set address %X0FCF, tick, output; tock, output;
set address %X2FCF, tick, output; tock, output;
set load 1,
set in -1,
set address %X4FCF,
tick,
tock,
output,
set address %X504F,
tick,
tock,
output;
set address %X0FCF, // Did not also write to lower or upper RAM
eval,
output;
set address %X2FCF,
eval,
output;
set load 0, // Low order address bits connected
set address %X4FCE, eval, output;
set address %X4FCD, eval, output;
set address %X4FCB, eval, output;
set address %X4FC7, eval, output;
set address %X4FDF, eval, output;
set address %X4FEF, eval, output;
set address %X4F8F, eval, output;
set address %X4F4F, eval, output;
set address %X4ECF, eval, output;
set address %X4DCF, eval, output;
set address %X4BCF, eval, output;
set address %X47CF, eval, output;
set address %X5FCF, eval, output;
set load 0,
set address 24576,
echo "Two horizontal lines should be in the middle of the screen. Hold down 'Y' (uppercase) until you see the next message ...",
// It's important to keep holding the key down since if the system is busy,
// the memory will zero itself before being outputted.
while out <> 89 {
tick, tock; // tick, tock prevents hang if sync. parts used in KB path.
}
clear-echo,
output;`;
export const cmp = `| in |load | address | out |
| 12345 | 1 | 010000000000000 | 0 |
| 12345 | 1 | 010000000000000 | 12345 |
| 12345 | 1 | 100000000000000 | 0 |
| 12345 | 1 | 100000000000000 | 12345 |
| -1 | 1 | 000000000000000 | 0 |
| -1 | 1 | 000000000000000 | -1 |
| 9999 | 0 | 000000000000000 | -1 |
| 9999 | 0 | 000000000000000 | -1 |
| 9999 | 0 | 010000000000000 | 12345 |
| 9999 | 0 | 100000000000000 | 12345 |
| 12345 | 1 | 000000000000000 | -1 |
| 12345 | 1 | 000000000000000 | 12345 |
| 12345 | 1 | 100000000000000 | 12345 |
| 12345 | 1 | 100000000000000 | 12345 |
| 2222 | 1 | 010000000000000 | 12345 |
| 2222 | 1 | 010000000000000 | 2222 |
| 9999 | 0 | 010000000000000 | 2222 |
| 9999 | 0 | 010000000000000 | 2222 |
| 9999 | 0 | 000000000000000 | 12345 |
| 9999 | 0 | 100000000000000 | 12345 |
| 9999 | 0 | 000000000000001 | 0 |
| 9999 | 0 | 000000000000010 | 0 |
| 9999 | 0 | 000000000000100 | 0 |
| 9999 | 0 | 000000000001000 | 0 |
| 9999 | 0 | 000000000010000 | 0 |
| 9999 | 0 | 000000000100000 | 0 |
| 9999 | 0 | 000000001000000 | 0 |
| 9999 | 0 | 000000010000000 | 0 |
| 9999 | 0 | 000000100000000 | 0 |
| 9999 | 0 | 000001000000000 | 0 |
| 9999 | 0 | 000010000000000 | 0 |
| 9999 | 0 | 000100000000000 | 0 |
| 9999 | 0 | 001000000000000 | 0 |
| 9999 | 0 | 010000000000000 | 2222 |
| 1234 | 1 | 001001000110100 | 0 |
| 1234 | 1 | 001001000110100 | 1234 |
| 1234 | 0 | 010001000110100 | 0 |
| 1234 | 0 | 110001000110100 | 0 |
| 2345 | 1 | 010001101000101 | 0 |
| 2345 | 1 | 010001101000101 | 2345 |
| 2345 | 0 | 000001101000101 | 0 |
| 2345 | 0 | 100001101000101 | 0 |
| 0 | 1 | 100000000000000 | 12345 |
| 0 | 1 | 100000000000000 | 0 |
| 0 | 1 | 110000000000000 | 75 |
| 12345 | 1 | 000111111001111 | 0 |
| 12345 | 1 | 000111111001111 | 12345 |
| 12345 | 1 | 010111111001111 | 0 |
| 12345 | 1 | 010111111001111 | 12345 |
| -1 | 1 | 100111111001111 | -1 |
| -1 | 1 | 101000001001111 | -1 |
| -1 | 1 | 000111111001111 | 12345 |
| -1 | 1 | 010111111001111 | 12345 |
| -1 | 0 | 100111111001110 | 0 |
| -1 | 0 | 100111111001101 | 0 |
| -1 | 0 | 100111111001011 | 0 |
| -1 | 0 | 100111111000111 | 0 |
| -1 | 0 | 100111111011111 | 0 |
| -1 | 0 | 100111111101111 | 0 |
| -1 | 0 | 100111110001111 | 0 |
| -1 | 0 | 100111101001111 | 0 |
| -1 | 0 | 100111011001111 | 0 |
| -1 | 0 | 100110111001111 | 0 |
| -1 | 0 | 100101111001111 | 0 |
| -1 | 0 | 100011111001111 | 0 |
| -1 | 0 | 101111111001111 | 0 |
| -1 | 0 | 110000000000000 | 89 |`;
@@ -0,0 +1,522 @@
export const hdl = `// 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/CPU.hdl
/**
* The Hack Central Processing unit (CPU).
* Parses the binary code in the instruction input and executes it according to the
* Hack machine language specification. In the case of a C-instruction, computes the
* function specified by the instruction. If the instruction specifies to read a memory
* value, the inM input is expected to contain this value. If the instruction specifies
* to write a value to the memory, sets the outM output to this value, sets the addressM
* output to the target address, and asserts the writeM output (when writeM = 0, any
* value may appear in outM).
* If the reset input is 0, computes the address of the next instruction and sets the
* pc output to that value. If the reset input is 1, sets pc to 0.
* Note: The outM and writeM outputs are combinational: they are affected by the
* instruction's execution during the current cycle. The addressM and pc outputs are
* clocked: although they are affected by the instruction's execution, they commit to
* their new values only in the next cycle.
*/
CHIP CPU {
IN inM[16], // M value input (M = contents of RAM[A])
instruction[16], // Instruction for execution
reset; // Signals whether to re-start the current
// program (reset==1) or continue executing
// the current program (reset==0).
OUT outM[16], // M value output
writeM, // Write to M?
addressM[15], // Address in data memory (of M)
pc[15]; // address of next instruction
PARTS:
//// Replace this comment with your code.
}`;
export const tst = `// 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/CPU.tst
load CPU.hdl,
compare-to CPU.cmp,
output-list time%S0.4.0 inM%D0.6.0 instruction%B0.16.0 reset%B2.1.2 outM%D1.6.0 writeM%B3.1.3 addressM%D0.5.0 pc%D0.5.0 DRegister[]%D1.6.1;
set instruction %B0011000000111001, // @12345
tick, output, tock, output;
set instruction %B1110110000010000, // D=A
tick, output, tock, output;
set instruction %B0101101110100000, // @23456
tick, output, tock, output;
set instruction %B1110000111010000, // D=A-D
tick, output, tock, output;
set instruction %B0000001111101000, // @1000
tick, output, tock, output;
set instruction %B1110001100001000, // M=D
tick, output, tock, output;
set instruction %B0000001111101001, // @1001
tick, output, tock, output;
set instruction %B1110001110011000, // MD=D-1
tick, output, tock, output;
set instruction %B0000001111101000, // @1000
tick, output, tock, output;
set instruction %B1111010011010000, // D=D-M
set inM 11111,
tick, output, tock, output;
set instruction %B0000000000001110, // @14
tick, output, tock, output;
set instruction %B1110001100000100, // D;jlt
tick, output, tock, output;
set instruction %B0000001111100111, // @999
tick, output, tock, output;
set instruction %B1110110111100000, // A=A+1
tick, output, tock, output;
set instruction %B1110001100001000, // M=D
tick, output, tock, output;
set instruction %B0000000000010101, // @21
tick, output, tock, output;
set instruction %B1110011111000010, // D+1;jeq
tick, output, tock, output;
set instruction %B0000000000000010, // @2
tick, output, tock, output;
set instruction %B1110000010010000, // D=D+A
tick, output, tock, output;
set instruction %B0000001111101000, // @1000
tick, output, tock, output;
set instruction %B1110111010010000, // D=-1
tick, output, tock, output;
set instruction %B1110001100000001, // D;JGT
tick, output, tock, output;
set instruction %B1110001100000010, // D;JEQ
tick, output, tock, output;
set instruction %B1110001100000011, // D;JGE
tick, output, tock, output;
set instruction %B1110001100000100, // D;JLT
tick, output, tock, output;
set instruction %B1110001100000101, // D;JNE
tick, output, tock, output;
set instruction %B1110001100000110, // D;JLE
tick, output, tock, output;
set instruction %B1110001100000111, // D;JMP
tick, output, tock, output;
set instruction %B1110101010010000, // D=0
tick, output, tock, output;
set instruction %B1110001100000001, // D;JGT
tick, output, tock, output;
set instruction %B1110001100000010, // D;JEQ
tick, output, tock, output;
set instruction %B1110001100000011, // D;JGE
tick, output, tock, output;
set instruction %B1110001100000100, // D;JLT
tick, output, tock, output;
set instruction %B1110001100000101, // D;JNE
tick, output, tock, output;
set instruction %B1110001100000110, // D;JLE
tick, output, tock, output;
set instruction %B1110001100000111, // D;JMP
tick, output, tock, output;
set instruction %B1110111111010000, // D=1
tick, output, tock, output;
set instruction %B1110001100000001, // D;JGT
tick, output, tock, output;
set instruction %B1110001100000010, // D;JEQ
tick, output, tock, output;
set instruction %B1110001100000011, // D;JGE
tick, output, tock, output;
set instruction %B1110001100000100, // D;JLT
tick, output, tock, output;
set instruction %B1110001100000101, // D;JNE
tick, output, tock, output;
set instruction %B1110001100000110, // D;JLE
tick, output, tock, output;
set instruction %B1110001100000111, // D;JMP
tick, output, tock, output;
set reset 1;
tick, output, tock, output;
set instruction %B0111111111111111, // @32767
set reset 0;
tick, output, tock, output;`;
export const cmp = `|time| inM | instruction |reset| outM |writeM |addre| pc |DRegiste|
|0+ | 0|0011000000111001| 0 |*******| 0 | 0| 0| 0 |
|1 | 0|0011000000111001| 0 |*******| 0 |12345| 1| 0 |
|1+ | 0|1110110000010000| 0 |*******| 0 |12345| 1| 12345 |
|2 | 0|1110110000010000| 0 |*******| 0 |12345| 2| 12345 |
|2+ | 0|0101101110100000| 0 |*******| 0 |12345| 2| 12345 |
|3 | 0|0101101110100000| 0 |*******| 0 |23456| 3| 12345 |
|3+ | 0|1110000111010000| 0 |*******| 0 |23456| 3| 11111 |
|4 | 0|1110000111010000| 0 |*******| 0 |23456| 4| 11111 |
|4+ | 0|0000001111101000| 0 |*******| 0 |23456| 4| 11111 |
|5 | 0|0000001111101000| 0 |*******| 0 | 1000| 5| 11111 |
|5+ | 0|1110001100001000| 0 | 11111| 1 | 1000| 5| 11111 |
|6 | 0|1110001100001000| 0 | 11111| 1 | 1000| 6| 11111 |
|6+ | 0|0000001111101001| 0 |*******| 0 | 1000| 6| 11111 |
|7 | 0|0000001111101001| 0 |*******| 0 | 1001| 7| 11111 |
|7+ | 0|1110001110011000| 0 | 11110| 1 | 1001| 7| 11110 |
|8 | 0|1110001110011000| 0 | 11109| 1 | 1001| 8| 11110 |
|8+ | 0|0000001111101000| 0 |*******| 0 | 1001| 8| 11110 |
|9 | 0|0000001111101000| 0 |*******| 0 | 1000| 9| 11110 |
|9+ | 11111|1111010011010000| 0 |*******| 0 | 1000| 9| -1 |
|10 | 11111|1111010011010000| 0 |*******| 0 | 1000| 10| -1 |
|10+ | 11111|0000000000001110| 0 |*******| 0 | 1000| 10| -1 |
|11 | 11111|0000000000001110| 0 |*******| 0 | 14| 11| -1 |
|11+ | 11111|1110001100000100| 0 |*******| 0 | 14| 11| -1 |
|12 | 11111|1110001100000100| 0 |*******| 0 | 14| 14| -1 |
|12+ | 11111|0000001111100111| 0 |*******| 0 | 14| 14| -1 |
|13 | 11111|0000001111100111| 0 |*******| 0 | 999| 15| -1 |
|13+ | 11111|1110110111100000| 0 |*******| 0 | 999| 15| -1 |
|14 | 11111|1110110111100000| 0 |*******| 0 | 1000| 16| -1 |
|14+ | 11111|1110001100001000| 0 | -1| 1 | 1000| 16| -1 |
|15 | 11111|1110001100001000| 0 | -1| 1 | 1000| 17| -1 |
|15+ | 11111|0000000000010101| 0 |*******| 0 | 1000| 17| -1 |
|16 | 11111|0000000000010101| 0 |*******| 0 | 21| 18| -1 |
|16+ | 11111|1110011111000010| 0 |*******| 0 | 21| 18| -1 |
|17 | 11111|1110011111000010| 0 |*******| 0 | 21| 21| -1 |
|17+ | 11111|0000000000000010| 0 |*******| 0 | 21| 21| -1 |
|18 | 11111|0000000000000010| 0 |*******| 0 | 2| 22| -1 |
|18+ | 11111|1110000010010000| 0 |*******| 0 | 2| 22| 1 |
|19 | 11111|1110000010010000| 0 |*******| 0 | 2| 23| 1 |
|19+ | 11111|0000001111101000| 0 |*******| 0 | 2| 23| 1 |
|20 | 11111|0000001111101000| 0 |*******| 0 | 1000| 24| 1 |
|20+ | 11111|1110111010010000| 0 |*******| 0 | 1000| 24| -1 |
|21 | 11111|1110111010010000| 0 |*******| 0 | 1000| 25| -1 |
|21+ | 11111|1110001100000001| 0 |*******| 0 | 1000| 25| -1 |
|22 | 11111|1110001100000001| 0 |*******| 0 | 1000| 26| -1 |
|22+ | 11111|1110001100000010| 0 |*******| 0 | 1000| 26| -1 |
|23 | 11111|1110001100000010| 0 |*******| 0 | 1000| 27| -1 |
|23+ | 11111|1110001100000011| 0 |*******| 0 | 1000| 27| -1 |
|24 | 11111|1110001100000011| 0 |*******| 0 | 1000| 28| -1 |
|24+ | 11111|1110001100000100| 0 |*******| 0 | 1000| 28| -1 |
|25 | 11111|1110001100000100| 0 |*******| 0 | 1000| 1000| -1 |
|25+ | 11111|1110001100000101| 0 |*******| 0 | 1000| 1000| -1 |
|26 | 11111|1110001100000101| 0 |*******| 0 | 1000| 1000| -1 |
|26+ | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000| -1 |
|27 | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000| -1 |
|27+ | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000| -1 |
|28 | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000| -1 |
|28+ | 11111|1110101010010000| 0 |*******| 0 | 1000| 1000| 0 |
|29 | 11111|1110101010010000| 0 |*******| 0 | 1000| 1001| 0 |
|29+ | 11111|1110001100000001| 0 |*******| 0 | 1000| 1001| 0 |
|30 | 11111|1110001100000001| 0 |*******| 0 | 1000| 1002| 0 |
|30+ | 11111|1110001100000010| 0 |*******| 0 | 1000| 1002| 0 |
|31 | 11111|1110001100000010| 0 |*******| 0 | 1000| 1000| 0 |
|31+ | 11111|1110001100000011| 0 |*******| 0 | 1000| 1000| 0 |
|32 | 11111|1110001100000011| 0 |*******| 0 | 1000| 1000| 0 |
|32+ | 11111|1110001100000100| 0 |*******| 0 | 1000| 1000| 0 |
|33 | 11111|1110001100000100| 0 |*******| 0 | 1000| 1001| 0 |
|33+ | 11111|1110001100000101| 0 |*******| 0 | 1000| 1001| 0 |
|34 | 11111|1110001100000101| 0 |*******| 0 | 1000| 1002| 0 |
|34+ | 11111|1110001100000110| 0 |*******| 0 | 1000| 1002| 0 |
|35 | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000| 0 |
|35+ | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000| 0 |
|36 | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000| 0 |
|36+ | 11111|1110111111010000| 0 |*******| 0 | 1000| 1000| 1 |
|37 | 11111|1110111111010000| 0 |*******| 0 | 1000| 1001| 1 |
|37+ | 11111|1110001100000001| 0 |*******| 0 | 1000| 1001| 1 |
|38 | 11111|1110001100000001| 0 |*******| 0 | 1000| 1000| 1 |
|38+ | 11111|1110001100000010| 0 |*******| 0 | 1000| 1000| 1 |
|39 | 11111|1110001100000010| 0 |*******| 0 | 1000| 1001| 1 |
|39+ | 11111|1110001100000011| 0 |*******| 0 | 1000| 1001| 1 |
|40 | 11111|1110001100000011| 0 |*******| 0 | 1000| 1000| 1 |
|40+ | 11111|1110001100000100| 0 |*******| 0 | 1000| 1000| 1 |
|41 | 11111|1110001100000100| 0 |*******| 0 | 1000| 1001| 1 |
|41+ | 11111|1110001100000101| 0 |*******| 0 | 1000| 1001| 1 |
|42 | 11111|1110001100000101| 0 |*******| 0 | 1000| 1000| 1 |
|42+ | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000| 1 |
|43 | 11111|1110001100000110| 0 |*******| 0 | 1000| 1001| 1 |
|43+ | 11111|1110001100000111| 0 |*******| 0 | 1000| 1001| 1 |
|44 | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000| 1 |
|44+ | 11111|1110001100000111| 1 |*******| 0 | 1000| 1000| 1 |
|45 | 11111|1110001100000111| 1 |*******| 0 | 1000| 0| 1 |
|45+ | 11111|0111111111111111| 0 |*******| 0 | 1000| 0| 1 |
|46 | 11111|0111111111111111| 0 |*******| 0 |32767| 1| 1 |`;
export const external_tst = `// 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/CPU-external.tst
load CPU.hdl,
output-list time%S0.4.0 inM%D0.6.0 instruction%B0.16.0 reset%B2.1.2 outM%D1.6.0 writeM%B3.1.3 addressM%D0.5.0 pc%D0.5.0;
set instruction %B0011000000111001, // @12345
tick, output, tock, output;
set instruction %B1110110000010000, // D=A
tick, output, tock, output;
set instruction %B0101101110100000, // @23456
tick, output, tock, output;
set instruction %B1110000111010000, // D=A-D
tick, output, tock, output;
set instruction %B0000001111101000, // @1000
tick, output, tock, output;
set instruction %B1110001100001000, // M=D
tick, output, tock, output;
set instruction %B0000001111101001, // @1001
tick, output, tock, output;
set instruction %B1110001110011000, // MD=D-1
tick, output, tock, output;
set instruction %B0000001111101000, // @1000
tick, output, tock, output;
set instruction %B1111010011010000, // D=D-M
set inM 11111,
tick, output, tock, output;
set instruction %B0000000000001110, // @14
tick, output, tock, output;
set instruction %B1110001100000100, // D;jlt
tick, output, tock, output;
set instruction %B0000001111100111, // @999
tick, output, tock, output;
set instruction %B1110110111100000, // A=A+1
tick, output, tock, output;
set instruction %B1110001100001000, // M=D
tick, output, tock, output;
set instruction %B0000000000010101, // @21
tick, output, tock, output;
set instruction %B1110011111000010, // D+1;jeq
tick, output, tock, output;
set instruction %B0000000000000010, // @2
tick, output, tock, output;
set instruction %B1110000010010000, // D=D+A
tick, output, tock, output;
set instruction %B0000001111101000, // @1000
tick, output, tock, output;
set instruction %B1110111010010000, // D=-1
tick, output, tock, output;
set instruction %B1110001100000001, // D;JGT
tick, output, tock, output;
set instruction %B1110001100000010, // D;JEQ
tick, output, tock, output;
set instruction %B1110001100000011, // D;JGE
tick, output, tock, output;
set instruction %B1110001100000100, // D;JLT
tick, output, tock, output;
set instruction %B1110001100000101, // D;JNE
tick, output, tock, output;
set instruction %B1110001100000110, // D;JLE
tick, output, tock, output;
set instruction %B1110001100000111, // D;JMP
tick, output, tock, output;
set instruction %B1110101010010000, // D=0
tick, output, tock, output;
set instruction %B1110001100000001, // D;JGT
tick, output, tock, output;
set instruction %B1110001100000010, // D;JEQ
tick, output, tock, output;
set instruction %B1110001100000011, // D;JGE
tick, output, tock, output;
set instruction %B1110001100000100, // D;JLT
tick, output, tock, output;
set instruction %B1110001100000101, // D;JNE
tick, output, tock, output;
set instruction %B1110001100000110, // D;JLE
tick, output, tock, output;
set instruction %B1110001100000111, // D;JMP
tick, output, tock, output;
set instruction %B1110111111010000, // D=1
tick, output, tock, output;
set instruction %B1110001100000001, // D;JGT
tick, output, tock, output;
set instruction %B1110001100000010, // D;JEQ
tick, output, tock, output;
set instruction %B1110001100000011, // D;JGE
tick, output, tock, output;
set instruction %B1110001100000100, // D;JLT
tick, output, tock, output;
set instruction %B1110001100000101, // D;JNE
tick, output, tock, output;
set instruction %B1110001100000110, // D;JLE
tick, output, tock, output;
set instruction %B1110001100000111, // D;JMP
tick, output, tock, output;
set reset 1;
tick, output, tock, output;
set instruction %B0111111111111111, // @32767
set reset 0;
tick, output, tock, output;`;
export const external_cmp = `|time| inM | instruction |reset| outM |writeM |addre| pc |
|0+ | 0|0011000000111001| 0 |*******| 0 | 0| 0|
|1 | 0|0011000000111001| 0 |*******| 0 |12345| 1|
|1+ | 0|1110110000010000| 0 |*******| 0 |12345| 1|
|2 | 0|1110110000010000| 0 |*******| 0 |12345| 2|
|2+ | 0|0101101110100000| 0 |*******| 0 |12345| 2|
|3 | 0|0101101110100000| 0 |*******| 0 |23456| 3|
|3+ | 0|1110000111010000| 0 |*******| 0 |23456| 3|
|4 | 0|1110000111010000| 0 |*******| 0 |23456| 4|
|4+ | 0|0000001111101000| 0 |*******| 0 |23456| 4|
|5 | 0|0000001111101000| 0 |*******| 0 | 1000| 5|
|5+ | 0|1110001100001000| 0 | 11111| 1 | 1000| 5|
|6 | 0|1110001100001000| 0 | 11111| 1 | 1000| 6|
|6+ | 0|0000001111101001| 0 |*******| 0 | 1000| 6|
|7 | 0|0000001111101001| 0 |*******| 0 | 1001| 7|
|7+ | 0|1110001110011000| 0 | 11110| 1 | 1001| 7|
|8 | 0|1110001110011000| 0 | 11109| 1 | 1001| 8|
|8+ | 0|0000001111101000| 0 |*******| 0 | 1001| 8|
|9 | 0|0000001111101000| 0 |*******| 0 | 1000| 9|
|9+ | 11111|1111010011010000| 0 |*******| 0 | 1000| 9|
|10 | 11111|1111010011010000| 0 |*******| 0 | 1000| 10|
|10+ | 11111|0000000000001110| 0 |*******| 0 | 1000| 10|
|11 | 11111|0000000000001110| 0 |*******| 0 | 14| 11|
|11+ | 11111|1110001100000100| 0 |*******| 0 | 14| 11|
|12 | 11111|1110001100000100| 0 |*******| 0 | 14| 14|
|12+ | 11111|0000001111100111| 0 |*******| 0 | 14| 14|
|13 | 11111|0000001111100111| 0 |*******| 0 | 999| 15|
|13+ | 11111|1110110111100000| 0 |*******| 0 | 999| 15|
|14 | 11111|1110110111100000| 0 |*******| 0 | 1000| 16|
|14+ | 11111|1110001100001000| 0 | -1| 1 | 1000| 16|
|15 | 11111|1110001100001000| 0 | -1| 1 | 1000| 17|
|15+ | 11111|0000000000010101| 0 |*******| 0 | 1000| 17|
|16 | 11111|0000000000010101| 0 |*******| 0 | 21| 18|
|16+ | 11111|1110011111000010| 0 |*******| 0 | 21| 18|
|17 | 11111|1110011111000010| 0 |*******| 0 | 21| 21|
|17+ | 11111|0000000000000010| 0 |*******| 0 | 21| 21|
|18 | 11111|0000000000000010| 0 |*******| 0 | 2| 22|
|18+ | 11111|1110000010010000| 0 |*******| 0 | 2| 22|
|19 | 11111|1110000010010000| 0 |*******| 0 | 2| 23|
|19+ | 11111|0000001111101000| 0 |*******| 0 | 2| 23|
|20 | 11111|0000001111101000| 0 |*******| 0 | 1000| 24|
|20+ | 11111|1110111010010000| 0 |*******| 0 | 1000| 24|
|21 | 11111|1110111010010000| 0 |*******| 0 | 1000| 25|
|21+ | 11111|1110001100000001| 0 |*******| 0 | 1000| 25|
|22 | 11111|1110001100000001| 0 |*******| 0 | 1000| 26|
|22+ | 11111|1110001100000010| 0 |*******| 0 | 1000| 26|
|23 | 11111|1110001100000010| 0 |*******| 0 | 1000| 27|
|23+ | 11111|1110001100000011| 0 |*******| 0 | 1000| 27|
|24 | 11111|1110001100000011| 0 |*******| 0 | 1000| 28|
|24+ | 11111|1110001100000100| 0 |*******| 0 | 1000| 28|
|25 | 11111|1110001100000100| 0 |*******| 0 | 1000| 1000|
|25+ | 11111|1110001100000101| 0 |*******| 0 | 1000| 1000|
|26 | 11111|1110001100000101| 0 |*******| 0 | 1000| 1000|
|26+ | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000|
|27 | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000|
|27+ | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000|
|28 | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000|
|28+ | 11111|1110101010010000| 0 |*******| 0 | 1000| 1000|
|29 | 11111|1110101010010000| 0 |*******| 0 | 1000| 1001|
|29+ | 11111|1110001100000001| 0 |*******| 0 | 1000| 1001|
|30 | 11111|1110001100000001| 0 |*******| 0 | 1000| 1002|
|30+ | 11111|1110001100000010| 0 |*******| 0 | 1000| 1002|
|31 | 11111|1110001100000010| 0 |*******| 0 | 1000| 1000|
|31+ | 11111|1110001100000011| 0 |*******| 0 | 1000| 1000|
|32 | 11111|1110001100000011| 0 |*******| 0 | 1000| 1000|
|32+ | 11111|1110001100000100| 0 |*******| 0 | 1000| 1000|
|33 | 11111|1110001100000100| 0 |*******| 0 | 1000| 1001|
|33+ | 11111|1110001100000101| 0 |*******| 0 | 1000| 1001|
|34 | 11111|1110001100000101| 0 |*******| 0 | 1000| 1002|
|34+ | 11111|1110001100000110| 0 |*******| 0 | 1000| 1002|
|35 | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000|
|35+ | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000|
|36 | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000|
|36+ | 11111|1110111111010000| 0 |*******| 0 | 1000| 1000|
|37 | 11111|1110111111010000| 0 |*******| 0 | 1000| 1001|
|37+ | 11111|1110001100000001| 0 |*******| 0 | 1000| 1001|
|38 | 11111|1110001100000001| 0 |*******| 0 | 1000| 1000|
|38+ | 11111|1110001100000010| 0 |*******| 0 | 1000| 1000|
|39 | 11111|1110001100000010| 0 |*******| 0 | 1000| 1001|
|39+ | 11111|1110001100000011| 0 |*******| 0 | 1000| 1001|
|40 | 11111|1110001100000011| 0 |*******| 0 | 1000| 1000|
|40+ | 11111|1110001100000100| 0 |*******| 0 | 1000| 1000|
|41 | 11111|1110001100000100| 0 |*******| 0 | 1000| 1001|
|41+ | 11111|1110001100000101| 0 |*******| 0 | 1000| 1001|
|42 | 11111|1110001100000101| 0 |*******| 0 | 1000| 1000|
|42+ | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000|
|43 | 11111|1110001100000110| 0 |*******| 0 | 1000| 1001|
|43+ | 11111|1110001100000111| 0 |*******| 0 | 1000| 1001|
|44 | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000|
|44+ | 11111|1110001100000111| 1 |*******| 0 | 1000| 1000|
|45 | 11111|1110001100000111| 1 |*******| 0 | 1000| 0|
|45+ | 11111|0111111111111111| 0 |*******| 0 | 1000| 0|
|46 | 11111|0111111111111111| 0 |*******| 0 |32767| 1|
`;
@@ -0,0 +1,284 @@
export const hdl = `// 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.
}`;
export const add_tst = `// 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/ComputerAdd.tst
// Tests the Computer chip by having it execute the program Add.hack.
// The program adds up the constants 2 and 3 and writes the result in RAM[0].
// Tracks the values of the time, reset bit, A-register, D-register,
// program counter, R0, R1, and R2.
load Computer.hdl,
compare-to ComputerAdd.cmp,
output-list time%S1.3.1 reset%B2.1.2 ARegister[0]%D1.7.1 DRegister[0]%D1.7.1 PC[]%D0.4.0 RAM16K[0]%D1.7.1 RAM16K[1]%D1.7.1 RAM16K[2]%D1.7.1;
// Loads the binary program Add.hack into the computer's instruction memory
ROM32K load Add.hack,
output;
// First run (at the beginning PC=0)
repeat 6 {
tick, tock, output;
}
// Resets the PC
set reset 1,
set RAM16K[0] 0,
tick, tock, output;
// Second run, to check that the PC was reset correctly.
set reset 0,
repeat 6 {
tick, tock, output;
}`;
export const add_cmp = `|time |reset|ARegister|DRegister|PC[]|RAM16K[0]|RAM16K[1]|RAM16K[2]|
| 0 | 0 | 0 | 0 | 0| 0 | 0 | 0 |
| 1 | 0 | 2 | 0 | 1| 0 | 0 | 0 |
| 2 | 0 | 2 | 2 | 2| 0 | 0 | 0 |
| 3 | 0 | 3 | 2 | 3| 0 | 0 | 0 |
| 4 | 0 | 3 | 5 | 4| 0 | 0 | 0 |
| 5 | 0 | 0 | 5 | 5| 0 | 0 | 0 |
| 6 | 0 | 0 | 5 | 6| 5 | 0 | 0 |
| 7 | 1 | 0 | 5 | 0| 0 | 0 | 0 |
| 8 | 0 | 2 | 5 | 1| 0 | 0 | 0 |
| 9 | 0 | 2 | 2 | 2| 0 | 0 | 0 |
| 10 | 0 | 3 | 2 | 3| 0 | 0 | 0 |
| 11 | 0 | 3 | 5 | 4| 0 | 0 | 0 |
| 12 | 0 | 0 | 5 | 5| 0 | 0 | 0 |
| 13 | 0 | 0 | 5 | 6| 5 | 0 | 0 |`;
export const max_tst = `// 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/ComputerMax.tst
// Tests the Computer chip by having it execute the program Max.hack.
// The program computes maximum(R0, R1) and writes the result in RAM[2].
// Tracks the values of the time, reset bit, A-register, D-register,
// program counter, R0, R1, and R2.
load Computer.hdl,
compare-to ComputerMax.cmp,
output-list time%S1.3.1 reset%B2.1.2 ARegister[]%D1.7.1 DRegister[]%D1.7.1 PC[]%D0.4.0 RAM16K[0]%D1.7.1 RAM16K[1]%D1.7.1 RAM16K[2]%D1.7.1;
// Loads the binary program Add.hack into the computer's instruction memory
ROM32K load Max.hack,
// first run: computes max(3,5)
set RAM16K[0] 3,
set RAM16K[1] 5,
output;
repeat 14 {
tick, tock, output;
}
// resets the PC
set reset 1,
tick, tock, output;
// second run: computes max(23456,12345)
set reset 0,
set RAM16K[0] 23456,
set RAM16K[1] 12345,
output;
// The run on these inputs requires less cycles (different branching)
repeat 10 {
tick, tock, output;
}
`;
export const max_cmp = `|time |reset|ARegister|DRegister|PC[]|RAM16K[0]|RAM16K[1]|RAM16K[2]|
| 0 | 0 | 0 | 0 | 0| 3 | 5 | 0 |
| 1 | 0 | 0 | 0 | 1| 3 | 5 | 0 |
| 2 | 0 | 0 | 3 | 2| 3 | 5 | 0 |
| 3 | 0 | 1 | 3 | 3| 3 | 5 | 0 |
| 4 | 0 | 1 | -2 | 4| 3 | 5 | 0 |
| 5 | 0 | 10 | -2 | 5| 3 | 5 | 0 |
| 6 | 0 | 10 | -2 | 6| 3 | 5 | 0 |
| 7 | 0 | 1 | -2 | 7| 3 | 5 | 0 |
| 8 | 0 | 1 | 5 | 8| 3 | 5 | 0 |
| 9 | 0 | 12 | 5 | 9| 3 | 5 | 0 |
| 10 | 0 | 12 | 5 | 12| 3 | 5 | 0 |
| 11 | 0 | 2 | 5 | 13| 3 | 5 | 0 |
| 12 | 0 | 2 | 5 | 14| 3 | 5 | 5 |
| 13 | 0 | 14 | 5 | 15| 3 | 5 | 5 |
| 14 | 0 | 14 | 5 | 14| 3 | 5 | 5 |
| 15 | 1 | 14 | 5 | 0| 3 | 5 | 5 |
| 15 | 0 | 14 | 5 | 0| 23456 | 12345 | 5 |
| 16 | 0 | 0 | 5 | 1| 23456 | 12345 | 5 |
| 17 | 0 | 0 | 23456 | 2| 23456 | 12345 | 5 |
| 18 | 0 | 1 | 23456 | 3| 23456 | 12345 | 5 |
| 19 | 0 | 1 | 11111 | 4| 23456 | 12345 | 5 |
| 20 | 0 | 10 | 11111 | 5| 23456 | 12345 | 5 |
| 21 | 0 | 10 | 11111 | 10| 23456 | 12345 | 5 |
| 22 | 0 | 0 | 11111 | 11| 23456 | 12345 | 5 |
| 23 | 0 | 0 | 23456 | 12| 23456 | 12345 | 5 |
| 24 | 0 | 2 | 23456 | 13| 23456 | 12345 | 5 |
| 25 | 0 | 2 | 23456 | 14| 23456 | 12345 | 23456 |`;
export const rect_tst = `// 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/ComputerRect.tst
// Tests the Computer chip by having it execute the program Rect.hack.
// The program draws a rectangle of width 16 pixels and length RAM[0]
// at the top left corner of the screen.
// Tracks the values of the time, A-register, D-register, program counter, R0, R1, and R2.
load Computer.hdl,
compare-to ComputerRect.cmp,
output-list time%S1.3.1 ARegister[]%D1.7.1 DRegister[]%D1.7.1 PC[]%D0.4.0 RAM16K[0]%D1.7.1 RAM16K[1]%D1.7.1 RAM16K[2]%D1.7.1;
// Loads the binary program Rect.hack into the computer's instruction memory
ROM32K load Rect.hack,
echo "Before you run this script, select the 'Screen' option from the 'View' menu";
echo "A small rectangle should be drawn at the top left of the screen (the 'Screen' option of the 'View' menu should be selected.)";
// Draws a rectangle consisting of 4 rows (each 16 pixels wide)
set RAM16K[0] 4,
output;
repeat 63 {
tick, tock, output;
}`;
export const rect_cmp = `|time |ARegister|DRegister|PC[]|RAM16K[0]|RAM16K[1]|RAM16K[2]|
| 0 | 0 | 0 | 0| 4 | 0 | 0 |
| 1 | 0 | 0 | 1| 4 | 0 | 0 |
| 2 | 0 | 4 | 2| 4 | 0 | 0 |
| 3 | 23 | 4 | 3| 4 | 0 | 0 |
| 4 | 23 | 4 | 4| 4 | 0 | 0 |
| 5 | 16 | 4 | 5| 4 | 0 | 0 |
| 6 | 16 | 4 | 6| 4 | 0 | 0 |
| 7 | 16384 | 4 | 7| 4 | 0 | 0 |
| 8 | 16384 | 16384 | 8| 4 | 0 | 0 |
| 9 | 17 | 16384 | 9| 4 | 0 | 0 |
| 10 | 17 | 16384 | 10| 4 | 0 | 0 |
| 11 | 17 | 16384 | 11| 4 | 0 | 0 |
| 12 | 16384 | 16384 | 12| 4 | 0 | 0 |
| 13 | 16384 | 16384 | 13| 4 | 0 | 0 |
| 14 | 17 | 16384 | 14| 4 | 0 | 0 |
| 15 | 17 | 16384 | 15| 4 | 0 | 0 |
| 16 | 32 | 16384 | 16| 4 | 0 | 0 |
| 17 | 32 | 16416 | 17| 4 | 0 | 0 |
| 18 | 17 | 16416 | 18| 4 | 0 | 0 |
| 19 | 17 | 16416 | 19| 4 | 0 | 0 |
| 20 | 16 | 16416 | 20| 4 | 0 | 0 |
| 21 | 16 | 3 | 21| 4 | 0 | 0 |
| 22 | 10 | 3 | 22| 4 | 0 | 0 |
| 23 | 10 | 3 | 10| 4 | 0 | 0 |
| 24 | 17 | 3 | 11| 4 | 0 | 0 |
| 25 | 16416 | 3 | 12| 4 | 0 | 0 |
| 26 | 16416 | 3 | 13| 4 | 0 | 0 |
| 27 | 17 | 3 | 14| 4 | 0 | 0 |
| 28 | 17 | 16416 | 15| 4 | 0 | 0 |
| 29 | 32 | 16416 | 16| 4 | 0 | 0 |
| 30 | 32 | 16448 | 17| 4 | 0 | 0 |
| 31 | 17 | 16448 | 18| 4 | 0 | 0 |
| 32 | 17 | 16448 | 19| 4 | 0 | 0 |
| 33 | 16 | 16448 | 20| 4 | 0 | 0 |
| 34 | 16 | 2 | 21| 4 | 0 | 0 |
| 35 | 10 | 2 | 22| 4 | 0 | 0 |
| 36 | 10 | 2 | 10| 4 | 0 | 0 |
| 37 | 17 | 2 | 11| 4 | 0 | 0 |
| 38 | 16448 | 2 | 12| 4 | 0 | 0 |
| 39 | 16448 | 2 | 13| 4 | 0 | 0 |
| 40 | 17 | 2 | 14| 4 | 0 | 0 |
| 41 | 17 | 16448 | 15| 4 | 0 | 0 |
| 42 | 32 | 16448 | 16| 4 | 0 | 0 |
| 43 | 32 | 16480 | 17| 4 | 0 | 0 |
| 44 | 17 | 16480 | 18| 4 | 0 | 0 |
| 45 | 17 | 16480 | 19| 4 | 0 | 0 |
| 46 | 16 | 16480 | 20| 4 | 0 | 0 |
| 47 | 16 | 1 | 21| 4 | 0 | 0 |
| 48 | 10 | 1 | 22| 4 | 0 | 0 |
| 49 | 10 | 1 | 10| 4 | 0 | 0 |
| 50 | 17 | 1 | 11| 4 | 0 | 0 |
| 51 | 16480 | 1 | 12| 4 | 0 | 0 |
| 52 | 16480 | 1 | 13| 4 | 0 | 0 |
| 53 | 17 | 1 | 14| 4 | 0 | 0 |
| 54 | 17 | 16480 | 15| 4 | 0 | 0 |
| 55 | 32 | 16480 | 16| 4 | 0 | 0 |
| 56 | 32 | 16512 | 17| 4 | 0 | 0 |
| 57 | 17 | 16512 | 18| 4 | 0 | 0 |
| 58 | 17 | 16512 | 19| 4 | 0 | 0 |
| 59 | 16 | 16512 | 20| 4 | 0 | 0 |
| 60 | 16 | 0 | 21| 4 | 0 | 0 |
| 61 | 10 | 0 | 22| 4 | 0 | 0 |
| 62 | 10 | 0 | 23| 4 | 0 | 0 |
| 63 | 23 | 0 | 24| 4 | 0 | 0 |`;
export const add = `
0000000000000010
1110110000010000
0000000000000011
1110000010010000
0000000000000000
1110001100001000
`;
export const max = `0000000000000000
1111110000010000
0000000000000001
1111010011010000
0000000000001010
1110001100000001
0000000000000001
1111110000010000
0000000000001100
1110101010000111
0000000000000000
1111110000010000
0000000000000010
1110001100001000
0000000000001110
1110101010000111`;
export const rect = `
0000000000000000
1111110000010000
0000000000010111
1110001100000110
0000000000010000
1110001100001000
0100000000000000
1110110000010000
0000000000010001
1110001100001000
0000000000010001
1111110000100000
1110111010001000
0000000000010001
1111110000010000
0000000000100000
1110000010010000
0000000000010001
1110001100001000
0000000000010000
1111110010011000
0000000000001010
1110001100000001
0000000000010111
1110101010000111`;
@@ -0,0 +1,29 @@
export const hdl = `// 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/05/Screen.hdl
/**
* The Screen (memory map).
* Same functionality as a 16-bit 8K RAM:
* If load is asserted, the value of the register selected by
* address is set to in; Otherwise, the value does not change.
* The value of the selected register is emitted by out.
*
* This built-in implementation has the side effect of continuously
* refreshing a visual 256 by 512 black-and-white screen, simulated
* by the simulator. Each row in the visual screen is represented
* by 32 consecutive 16-bit words, starting at the top left corner
* of the visual screen. Thus the pixel at row r from the top and
* column c from the left (0<=r<256, 0<=c<512) reflects the c%16
* bit (counting from LSB to MSB) of the word found in Screen[r*32+c/16].
*/
CHIP Screen {
IN in[16], // what to write
load, // write-enable bit
address[13]; // where to read/write
OUT out[16]; // Screen value at the given address
PARTS:
BUILTIN Screen;
CLOCKED in, load;
}`;
@@ -0,0 +1,17 @@
export const hdl = `// 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/05/Keyboard.hdl
/**
* The keyboard (memory map).
* Outputs the character code of the currently pressed key,
* or 0 if no key is pressed.
*
* This built-in implementation has a visualization side effect.
*/
CHIP Keyboard {
OUT out[16];
PARTS:
BUILTIN Keyboard;
}`;
@@ -0,0 +1,21 @@
export const hdl = `// 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/05/DRegister.hdl
/**
* A 16-bit register named DRegister with the same functionality
* of the Register chip:
* If load is asserted, the register's value is set to in;
* Otherwise, the register maintains its current value.
* out(t+1) = (load(t), in(t), out(t))
*
* This built-in implementation has a visualization side effect.
*/
CHIP DRegister {
IN in[16], load;
OUT out[16];
PARTS:
BUILTIN DRegister;
CLOCKED in, load;
}`;
@@ -0,0 +1,21 @@
export const hdl = `// 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/05/ARegister.hdl
/**
* A 16-bit register named ARegister with the same functionality
* of the Register chip:
* If load is asserted, the register's value is set to in;
* Otherwise, the register maintains its current value.
* out(t+1) = (load(t), in(t), out(t))
*
* This built-in implementation has a visualization side effect.
*/
CHIP ARegister {
IN in[16], load;
OUT out[16];
PARTS:
BUILTIN ARegister;
CLOCKED in, load;
}`;
@@ -0,0 +1,25 @@
export const hdl = `// 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/05/ROM32K.hdl
/**
* Read-Only memory (ROM) of 32K registers, each 16-bit wide.
* Facilitates data read, as follows:
* out(t) = ROM32K[address(t)](t)
* In words: the chip outputs the value stored at the
* memory location specified by address.
*
* Can be used as the instruction memory of the Hack computer.
* To that end, the built-in chip implementation supports the handling
* of the "ROM32K load Xxx" script command, where Xxx is the name of a
* text file containing a program written in the binary Hack machine language.
* When the simulator encounters such a command in a test script,
* the file's contents is loaded into the simulated ROM32K chip.
*/
CHIP ROM32K {
IN address[15];
OUT out[16];
PARTS:
BUILTIN ROM32K;
}`;
@@ -0,0 +1,58 @@
import { FileSystem, reset } from "@davidsouther/jiffies/lib/esm/fs.js";
import * as RAM16K from "../project_03/08_ram16k.js";
import { resetBySuffix } from "../reset.js";
import * as Memory from "./01_memory.js";
import * as CPU from "./02_cpu.js";
import * as Computer from "./03_computer.js";
import * as Screen from "./04_screen.js";
import * as Keyboard from "./05_keyboard.js";
import * as DRegister from "./06_d_register.js";
import * as ARegister from "./07_a_register.js";
import * as ROM32K from "./08_rom32k.js";
export const CHIPS = {
"Memory.hdl": Memory.hdl,
"Memory.tst": Memory.tst,
"Memory.cmp": Memory.cmp,
"CPU.hdl": CPU.hdl,
"CPU.tst": CPU.tst,
"CPU.cmp": CPU.cmp,
"CPU-external.tst": CPU.external_tst,
"CPU-external.cmp": CPU.external_cmp,
"Computer.hdl": Computer.hdl,
"ComputerAdd.tst": Computer.add_tst,
"ComputerAdd.cmp": Computer.add_cmp,
"ComputerMax.tst": Computer.max_tst,
"ComputerMax.cmp": Computer.max_cmp,
"ComputerRect.tst": Computer.rect_tst,
"ComputerRect.cmp": Computer.rect_cmp,
"Add.hack": Computer.add,
"Max.hack": Computer.max,
"Rect.hack": Computer.rect,
};
export const BUILTIN_CHIPS = {
Screen: Screen.hdl,
Keyboard: Keyboard.hdl,
DRegister: DRegister.hdl,
ARegister: ARegister.hdl,
ROM32K: ROM32K.hdl,
RAM16K: RAM16K.hdl.replace(
"//// Replace this comment with your code.",
"BUILTIN RAM16K;",
),
};
export async function resetFiles(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/05");
await reset(fs, CHIPS);
await fs.popd();
}
export async function resetTests(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/05");
await resetBySuffix(fs, CHIPS, ".tst");
await resetBySuffix(fs, CHIPS, ".cmp");
await fs.popd();
}
@@ -0,0 +1,14 @@
#!/bin/bash
# write FileName.ts varname SourceFile
function write () {
echo -n "export const $2 = \`" >> $1
cat $3 >> $1
echo "\`;\n\n" >> $1
}
# write_all ts_name ProjName
function write_all() {
for e in vm vm_tst hdl_tst cmp ; do write $1 $e $2.$e ; done
}
@@ -0,0 +1,55 @@
export const vm = `// 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/7/StackArithmetic/SimpleAdd/SimpleAdd.vm
// Pushes and adds two constants.
push constant 7
push constant 8
add
`;
export const vm_tst = `// 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/7/StackArithmetic/SimpleAdd/SimpleAddVME.tst
// Tests and illustrates SimpleAdd.vm on the VM simulator.
load SimpleAdd.vm,
compare-to SimpleAdd.cmp,
set RAM[0] 256, // initializes the stack pointer
repeat 3 { // SimpleAdd.vm has 3 VM commands
vmstep;
}
// Outputs the stack pointer and the value at the stack's base
output-list RAM[0]%D2.6.2 RAM[256]%D2.6.2;
output;`;
export const hdl_tst = `// 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/7/StackArithmetic/SimpleAdd/SimpleAdd.tst
// Tests SimpleAdd.asm on the CPU emulator.
compare-to SimpleAdd.cmp,
set RAM[0] 256, // initializes the stack pointer
repeat 60 { // enough cycles to complete the execution
ticktock;
}
// Outputs the stack pointer and the value at the stack's base
output-list RAM[0]%D2.6.2 RAM[256]%D2.6.2;
output;
`;
export const cmp = `| RAM[0] | RAM[256] |
| 257 | 15 |
`;
@@ -0,0 +1,99 @@
export const vm = `// 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/7/StackArithmetic/StackTest/StackTest.vm
// Executes a sequence of arithmetic and logical operations on the stack.
push constant 17
push constant 17
eq
push constant 17
push constant 16
eq
push constant 16
push constant 17
eq
push constant 892
push constant 891
lt
push constant 891
push constant 892
lt
push constant 891
push constant 891
lt
push constant 32767
push constant 32766
gt
push constant 32766
push constant 32767
gt
push constant 32766
push constant 32766
gt
push constant 57
push constant 31
push constant 53
add
push constant 112
sub
neg
and
push constant 82
or
not
`;
export const vm_tst = `// 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/7/StackArithmetic/StackTest/StackTestVME.tst
// Tests and illustrates StackTest.vm on the VM simulator.
load StackTest.vm,
compare-to StackTest.cmp,
set RAM[0] 256, // initializes the stack pointer
repeat 38 { // StackTest.vm has 38 VM commands
vmstep;
}
// Outputs the stack pointer (RAM[0]) and the stack contents: RAM[256]-RAM[265]
output-list RAM[0]%D2.6.2
RAM[256]%D2.6.2 RAM[257]%D2.6.2 RAM[258]%D2.6.2 RAM[259]%D2.6.2 RAM[260]%D2.6.2;
output;
output-list RAM[261]%D2.6.2 RAM[262]%D2.6.2 RAM[263]%D2.6.2 RAM[264]%D2.6.2 RAM[265]%D2.6.2;
output;
`;
export const hdl_tst = `// 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/7/StackArithmetic/StackTest/StackTest.tst
// Tests StackTest.asm on the CPU emulator.
compare-to StackTest.cmp,
set RAM[0] 256, // initializes the stack pointer
repeat 1000 { // enough cycles to complete the execution
ticktock;
}
// Outputs the stack pointer and the stack contents: RAM[256]-RAM[265]
output-list RAM[0]%D2.6.2
RAM[256]%D2.6.2 RAM[257]%D2.6.2 RAM[258]%D2.6.2 RAM[259]%D2.6.2 RAM[260]%D2.6.2;
output;
output-list RAM[261]%D2.6.2 RAM[262]%D2.6.2 RAM[263]%D2.6.2 RAM[264]%D2.6.2 RAM[265]%D2.6.2;
output;
`;
export const cmp = `| RAM[0] | RAM[256] | RAM[257] | RAM[258] | RAM[259] | RAM[260] |
| 266 | -1 | 0 | 0 | 0 | -1 |
| RAM[261] | RAM[262] | RAM[263] | RAM[264] | RAM[265] |
| 0 | -1 | 0 | 0 | -91 |
`;
@@ -0,0 +1,91 @@
export const vm = `// 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/7/MemoryAccess/BasicTest/BasicTest.vm
// Executes pop and push commands.
push constant 10
pop local 0
push constant 21
push constant 22
pop argument 2
pop argument 1
push constant 36
pop this 6
push constant 42
push constant 45
pop that 5
pop that 2
push constant 510
pop temp 6
push local 0
push that 5
add
push argument 1
sub
push this 6
push this 6
add
sub
push temp 6
add
`;
export const vm_tst = `// 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/7/MemoryAccess/BasicTest/BasicTestVME.tst
// Tests and illustrates BasicTest.vm on the VM simulator.
// Starts by setting the stack pointer and the base addresses
// of relevant memory segments to selected RAM addresses.
load BasicTest.vm,
compare-to BasicTest.cmp,
set sp 256, // stack pointer
set local 300, // base address of the local segment
set argument 400, // base address of the argument segment
set this 3000, // base address of the this segment
set that 3010, // base address of the that segment
repeat 25 { // BasicTest.vm has 25 VM commands
vmstep;
}
// Outputs the value at the stack's base and some values from the tested memory segments
output-list RAM[256]%D1.6.1 RAM[300]%D1.6.1 RAM[401]%D1.6.1
RAM[402]%D1.6.1 RAM[3006]%D1.6.1 RAM[3012]%D1.6.1
RAM[3015]%D1.6.1 RAM[11]%D1.6.1;
output;
`;
export const hdl_tst = `// 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/7/MemoryAccess/BasicTest/BasicTest.tst
// Tests BasicTest.asm on the CPU emulator.
compare-to BasicTest.cmp,
set RAM[0] 256, // stack pointer
set RAM[1] 300, // base address of the local segment
set RAM[2] 400, // base address of the argument segment
set RAM[3] 3000, // base address of the this segment
set RAM[4] 3010, // base address of the that segment
repeat 600 { // enough cycles to complete the execution
ticktock;
}
// Outputs the value at the stack's base and some values from the tested memory segments
output-list RAM[256]%D1.6.1 RAM[300]%D1.6.1 RAM[401]%D1.6.1
RAM[402]%D1.6.1 RAM[3006]%D1.6.1 RAM[3012]%D1.6.1
RAM[3015]%D1.6.1 RAM[11]%D1.6.1;
output;
`;
export const cmp = `|RAM[256]|RAM[300]|RAM[401]|RAM[402]|RAM[3006|RAM[3012|RAM[3015|RAM[11] |
| 472 | 10 | 21 | 22 | 36 | 42 | 45 | 510 |`;
@@ -0,0 +1,72 @@
export const vm = `// 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/7/MemoryAccess/PointerTest/PointerTest.vm
// Executes pop and push commands using the
// pointer, this, and that segments.
push constant 3030
pop pointer 0
push constant 3040
pop pointer 1
push constant 32
pop this 2
push constant 46
pop that 6
push pointer 0
push pointer 1
add
push this 2
sub
push that 6
add
`;
export const vm_tst = `// 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/7/MemoryAccess/PointerTest/PointerTestVME.tst
// Tests and illustrates PointerTest.vm on the VM simulator.
load PointerTest.vm,
compare-to PointerTest.cmp,
set RAM[0] 256, // initializes the stack pointer
repeat 15 { // PointerTest.vm has 15 VM commands
vmstep;
}
// Outputs the stack base, THIS, THAT, and
// some values from the the this and that segments
output-list RAM[256]%D1.6.1 RAM[3]%D1.6.1 RAM[4]%D1.6.1
RAM[3032]%D1.6.1 RAM[3046]%D1.6.1;
output;
`;
export const hdl_tst = `// 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/7/MemoryAccess/PointerTest/PointerTest.tst
// Tests PointerTest.asm on the CPU emulator.
compare-to PointerTest.cmp,
set RAM[0] 256, // initializes the stack pointer
repeat 450 { // enough cycles to complete the execution
ticktock;
}
// Outputs the value at the stack's base, THIS, THAT, and
// some values from the the this and that segments
output-list RAM[256]%D1.6.1 RAM[3]%D1.6.1
RAM[4]%D1.6.1 RAM[3032]%D1.6.1 RAM[3046]%D1.6.1;
output;
`;
export const cmp = `|RAM[256]| RAM[3] | RAM[4] |RAM[3032|RAM[3046|
| 6084 | 3030 | 3040 | 32 | 46 |`;
@@ -0,0 +1,63 @@
export const vm = `// 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/7/MemoryAccess/StaticTest/StaticTest.vm
// Executes pop and push commands using the static segment.
push constant 111
push constant 333
push constant 888
pop static 8
pop static 3
pop static 1
push static 3
push static 1
sub
push static 8
add
`;
export const vm_tst = `// 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/7/MemoryAccess/StaticTest/StaticTestVME.tst
// Tests and illustrates StaticTest.vm on the VM simulator.
load StaticTest.vm,
compare-to StaticTest.cmp,
set sp 256, // initializes the stack pointer
repeat 11 { // StaticTest.vm has 11 VM commands
vmstep;
}
// Outputs the value at the stack's base
output-list RAM[256]%D1.6.1;
output;
`;
export const hdl_tst = `// 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/7/MemoryAccess/StaticTest/StaticTest.tst
// Tests StaticTest.asm on the CPU emulator.
compare-to StaticTest.cmp,
set RAM[0] 256, // initializes the stack pointer
repeat 200 { // enough cycles to complete the execution
ticktock;
}
// Outputs the value at the stack's base
output-list RAM[256]%D1.6.1;
output;
`;
export const cmp = `|RAM[256]|
| 1110 |`;
@@ -0,0 +1,54 @@
import { FileSystem, reset } from "@davidsouther/jiffies/lib/esm/fs.js";
import { resetBySuffix } from "../reset.js";
import * as simple_add from "./11_simple_add.js";
import * as stack_test from "./12_stack_test.js";
import * as basic_test from "./21_basic_test.js";
import * as pointer_test from "./22_pointer_test.js";
import * as static_test from "./23_static_test.js";
export const VMS = {
SimpleAdd: {
"SimpleAdd.vm": simple_add.vm,
"SimpleAddVME.tst": simple_add.vm_tst,
"SimpleAdd.cmp": simple_add.cmp,
"SimpleAdd.tst": simple_add.hdl_tst,
},
StackTest: {
"StackTest.vm": stack_test.vm,
"StackTestVME.tst": stack_test.vm_tst,
"StackTest.cmp": stack_test.cmp,
"StackTest.tst": stack_test.hdl_tst,
},
BasicTest: {
"BasicTest.vm": basic_test.vm,
"BasicTestVME.tst": basic_test.vm_tst,
"BasicTest.cmp": basic_test.cmp,
"BasicTest.tst": basic_test.hdl_tst,
},
PointerTest: {
"PointerTest.vm": pointer_test.vm,
"PointerTestVME.tst": pointer_test.vm_tst,
"PointerTest.cmp": pointer_test.cmp,
"PointerTest.tst": pointer_test.hdl_tst,
},
StaticTest: {
"StaticTest.vm": static_test.vm,
"StaticTestVME.tst": static_test.vm_tst,
"StaticTest.cmp": static_test.cmp,
"StaticTest.tst": static_test.hdl_tst,
},
};
export async function resetFiles(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/07");
await reset(fs, VMS);
await fs.popd();
}
export async function resetTests(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/07");
await resetBySuffix(fs, VMS, ".tst");
await resetBySuffix(fs, VMS, "VME.tst");
await resetBySuffix(fs, VMS, ".cmp");
await fs.popd();
}
@@ -0,0 +1,81 @@
export const vm = `// 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/8/ProgramFlow/BasicLoop/BasicLoop.vm
// Computes the sum 1 + 2 + ... + n and pushes the result onto
// the stack. The value n is given in argument[0], which must be
// initialized by the caller of this code.
push constant 0
pop local 0 // sum = 0
label LOOP
push argument 0
push local 0
add
pop local 0 // sum = sum + n
push argument 0
push constant 1
sub
pop argument 0 // n--
push argument 0
if-goto LOOP // if n > 0, goto LOOP
push local 0 // else, pushes sum to the stack's top
`;
export const vm_tst = `// 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/8/ProgramFlow/BasicLoop/BasicLoopVME.tst
// Tests and illustrates BasicLoop.vm on the VM emulator.
// Before executing the code, initializes the stack pointer
// and the base addresses of the local and argument segments,
// and sets argument[0].
load BasicLoop.vm,
compare-to BasicLoop.cmp,
set sp 256,
set local 300,
set argument 400,
set argument[0] 3,
repeat 33 {
vmstep;
}
// Outputs the stack pointer and the value at the stack's base
output-list RAM[0]%D1.6.1 RAM[256]%D1.6.1;
output;
`;
export const hdl_tst = `// 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/8/ProgramFlow/BasicLoop/BasicLoop.tst
// Tests BasicLoop.asm on the CPU emulator.
// Before executing the code, initializes the stack pointer
// and the base addresses of the local and argument segments,
// and sets argument[0].
compare-to BasicLoop.cmp,
set RAM[0] 256, // SP
set RAM[1] 300, // LCL
set RAM[2] 400, // ARG
set RAM[400] 3, // argument 0
repeat 600 {
ticktock;
}
// Outputs the stack pointer and the value at the stack's base
output-list RAM[0]%D1.6.1 RAM[256]%D1.6.1;
output;
`;
export const cmp = `| RAM[0] |RAM[256]|
| 257 | 6 |
`;
@@ -0,0 +1,107 @@
export const vm = `// 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/8/ProgramFlow/FibonacciSeries/FibonacciSeries.vm
// Puts the first n elements of the Fibonacci series in the memory,
// starting at address addr. n and addr are given in argument[0] and
// argument[1], which must be initialized by the caller of this code.
push argument 1 // sets THAT, the base address of the
pop pointer 1 // that segment, to argument[1]
push constant 0 // sets the series' first and second
pop that 0 // elements to 0 and 1, respectively
push constant 1
pop that 1
push argument 0 // sets n, the number of remaining elements
push constant 2 // to be computed, to argument[0] minus 2,
sub // since 2 elements were already computed.
pop argument 0
label LOOP
push argument 0
if-goto COMPUTE_ELEMENT // if n > 0, goto COMPUTE_ELEMENT
goto END // otherwise, goto END
label COMPUTE_ELEMENT
// that[2] = that[0] + that[1]
push that 0
push that 1
add
pop that 2
// THAT += 1 (updates the base address of that)
push pointer 1
push constant 1
add
pop pointer 1
// updates n-- and loops
push argument 0
push constant 1
sub
pop argument 0
goto LOOP
label END
`;
export const vm_tst = `// 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/8/ProgramFlow/FibonacciSeries/FibonacciSeriesVME.tst
// Tests and illustrates FibonacciSeries.vm on the VM emulator.
// Before executing the code, initializes the stack pointer
// and the base addresses of the local and argument segments,
// and sets argument[0] to n and argument [1] to the base address
// of the generated series.
load FibonacciSeries.vm,
compare-to FibonacciSeries.cmp,
set sp 256,
set local 300,
set argument 400,
set argument[0] 6,
set argument[1] 3000,
repeat 73 {
vmstep;
}
// Outputs the series of values generated and written by the code.
output-list RAM[3000]%D1.6.2 RAM[3001]%D1.6.2 RAM[3002]%D1.6.2
RAM[3003]%D1.6.2 RAM[3004]%D1.6.2 RAM[3005]%D1.6.2;
output;
`;
export const hdl_tst = `// 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/8/ProgramFlow/FibonacciSeries/FibonacciSeries.tst
// Tests FibonacciSeries.asm on the CPU emulator.
// Before executing the code, initializes the stack pointer
// and the base addresses of the local and argument segments,
// and sets argument[0] and argument [1].
compare-to FibonacciSeries.cmp,
set RAM[0] 256, // SP
set RAM[1] 300, // LCL
set RAM[2] 400, // ARG
set RAM[400] 6, // argument[0], n
set RAM[401] 3000, // argument[1], base address of the generated series
repeat 1100 {
ticktock;
}
// Outputs the series of values generated and written by the code.
output-list RAM[3000]%D1.6.2 RAM[3001]%D1.6.2 RAM[3002]%D1.6.2
RAM[3003]%D1.6.2 RAM[3004]%D1.6.2 RAM[3005]%D1.6.2;
output;
`;
export const cmp = `|RAM[3000]|RAM[3001]|RAM[3002]|RAM[3003]|RAM[3004]|RAM[3005]|
| 0 | 1 | 1 | 2 | 3 | 5 |
`;
@@ -0,0 +1,96 @@
export const vm = `// 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/8/FunctionCalls/SimpleFunction/SimpleFunction.vm
// Performs a simple calculation and returns the result.
// argument[0] and argument[1] must be set by the caller.
function SimpleFunction.test 2
push local 0
push local 1
add
not
push argument 0
add
push argument 1
sub
return
`;
export const vm_tst = `// 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/8/FunctionCalls/SimpleFunction/SimpleFunctionVME.tst
// Tests and illustrates SimpleFunction.vm in the VM emulator.
// Before executing the code, initializes the stack pointer
// and the base addresses of some of the memory segments,
// and sets some values in the argument segment.
load SimpleFunction.vm,
compare-to SimpleFunction.cmp,
set sp 317,
set local 317,
set argument 310,
set this 3000,
set that 4000,
set argument[0] 1234,
set argument[1] 37,
set argument[2] 9,
set argument[3] 305,
set argument[4] 300,
set argument[5] 3010,
set argument[6] 4010,
repeat 10 {
vmstep;
}
// Outputs SP, LCL, ARG, THIS, THAT, and the return value.
output-list RAM[0]%D1.6.1 RAM[1]%D1.6.1 RAM[2]%D1.6.1
RAM[3]%D1.6.1 RAM[4]%D1.6.1 RAM[310]%D1.6.1;
output;
`;
export const hdl_tst = `// 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/8/FunctionCalls/SimpleFunction/SimpleFunction.tst
// Tests SimpleFunction.asm in the CPU emulator.
// In particular, tests how the assembly implementation of the 'function'
// VM command initializes local variables, and how the assembly implementation
// of the 'return' VM command handles the return value, SP, LCL, ARG, THIS, and THAT.
// Before executing the code, initializes the stack pointer and the pointers of some
// of the memory segments, and sets some values in the argument segment.
compare-to SimpleFunction.cmp,
set RAM[0] 317, // SP
set RAM[1] 317, // LCL
set RAM[2] 310, // ARG
set RAM[3] 3000, // THIS
set RAM[4] 4000, // THAT
set RAM[310] 1234,
set RAM[311] 37,
set RAM[312] 1000,
set RAM[313] 305,
set RAM[314] 300,
set RAM[315] 3010,
set RAM[316] 4010,
repeat 300 {
ticktock;
}
// Outputs SP, LCL, ARG, THIS, THAT, and the return value.
output-list RAM[0]%D1.6.1 RAM[1]%D1.6.1 RAM[2]%D1.6.1
RAM[3]%D1.6.1 RAM[4]%D1.6.1 RAM[310]%D1.6.1;
output;
`;
export const cmp = `| RAM[0] | RAM[1] | RAM[2] | RAM[3] | RAM[4] |RAM[310]|
| 311 | 305 | 300 | 3010 | 4010 | 1196 |
`;
@@ -0,0 +1,204 @@
export const sys = `// Sys.vm. Tested by the NestedCall test script.
// Consists of three functions: Sys.init, Sys.main, and Sys.add12.
// Calls Sys.main() and stores a return value in temp 1.
// Does not return (enters infinite loop).
// The VM implementation starts running the Sys.init function, by default.
function Sys.init 0
push constant 4000 // tests that THIS and THAT are handled correctly
pop pointer 0
push constant 5000
pop pointer 1
call Sys.main 0
pop temp 1
label LOOP
goto LOOP
// Sets locals 1, 2 and 3 to some values. Leaves locals 0 and 4 unchanged,
// to test that the 'function' VM command initializes them to 0 (the test
// script sets them to -1 before this code starts running).
// Calls Sys.add12(123) and stores the return value (should be 135) in temp 0.
// Returns local 0 + local 1 + local 2 + local 3 + local 4 (should be 456), to
// confirm that locals were not mangled by the function call.
function Sys.main 5
push constant 4001
pop pointer 0
push constant 5001
pop pointer 1
push constant 200
pop local 1
push constant 40
pop local 2
push constant 6
pop local 3
push constant 123
call Sys.add12 1
pop temp 0
push local 0
push local 1
push local 2
push local 3
push local 4
add
add
add
add
return
// Returns (argument 0) + 12.
function Sys.add12 0
push constant 4002
pop pointer 0
push constant 5002
pop pointer 1
push argument 0
push constant 12
add
return
`;
export const vm_tst = `// Tests and illustrates how the VM implementation handles function-call-and-return,
// by executing the functions in Sys.vm in the VM emulator.
// In particular, loads and runs the functions in Sys.vm.
load Sys.vm,
compare-to NestedCall.cmp,
output-list RAM[0]%D1.6.1 RAM[1]%D1.6.1 RAM[2]%D1.6.1 RAM[3]%D1.6.1 RAM[4]%D1.6.1 RAM[5]%D1.6.1 RAM[6]%D1.6.1;
set RAM[0] 261,
set RAM[1] 261,
set RAM[2] 256,
set RAM[3] -3,
set RAM[4] -4,
set RAM[5] -1, // test results
set RAM[6] -1,
set RAM[256] 1234, // fake stack frame from call Sys.init
set RAM[257] -1,
set RAM[258] -2,
set RAM[259] -3,
set RAM[260] -4,
set RAM[261] -1, // Initialize stack to check for local segment
set RAM[262] -1, // being cleared to zero.
set RAM[263] -1,
set RAM[264] -1,
set RAM[265] -1,
set RAM[266] -1,
set RAM[267] -1,
set RAM[268] -1,
set RAM[269] -1,
set RAM[270] -1,
set RAM[271] -1,
set RAM[272] -1,
set RAM[273] -1,
set RAM[274] -1,
set RAM[275] -1,
set RAM[276] -1,
set RAM[277] -1,
set RAM[278] -1,
set RAM[279] -1,
set RAM[280] -1,
set RAM[281] -1,
set RAM[282] -1,
set RAM[283] -1,
set RAM[284] -1,
set RAM[285] -1,
set RAM[286] -1,
set RAM[287] -1,
set RAM[288] -1,
set RAM[289] -1,
set RAM[290] -1,
set RAM[291] -1,
set RAM[292] -1,
set RAM[293] -1,
set RAM[294] -1,
set RAM[295] -1,
set RAM[296] -1,
set RAM[297] -1,
set RAM[298] -1,
set RAM[299] -1,
set sp 261,
set local 261,
set argument 256,
set this 3000,
set that 4000;
repeat 50 {
vmstep;
}
output;
`;
export const hdl_tst = `// Tests how the VM implementation handles function-call-and-return,
// by executing the functions in Sys.vm.
// In particular, loads and runs NestedCall.asm, which results when
// the VM translator is applied to the NestedCall folder, which
// includes only one VM file: Sys.vm.
compare-to NestedCall.cmp,
set RAM[0] 261,
set RAM[1] 261,
set RAM[2] 256,
set RAM[3] -3,
set RAM[4] -4,
set RAM[5] -1, // test results
set RAM[6] -1,
set RAM[256] 1234, // fake stack frame from call Sys.init
set RAM[257] -1,
set RAM[258] -2,
set RAM[259] -3,
set RAM[260] -4,
set RAM[261] -1, // Initializes the stack, to check that the local segment
set RAM[262] -1, // is initialized to zeros by the 'function' VM command.
set RAM[263] -1,
set RAM[264] -1,
set RAM[265] -1,
set RAM[266] -1,
set RAM[267] -1,
set RAM[268] -1,
set RAM[269] -1,
set RAM[270] -1,
set RAM[271] -1,
set RAM[272] -1,
set RAM[273] -1,
set RAM[274] -1,
set RAM[275] -1,
set RAM[276] -1,
set RAM[277] -1,
set RAM[278] -1,
set RAM[279] -1,
set RAM[280] -1,
set RAM[281] -1,
set RAM[282] -1,
set RAM[283] -1,
set RAM[284] -1,
set RAM[285] -1,
set RAM[286] -1,
set RAM[287] -1,
set RAM[288] -1,
set RAM[289] -1,
set RAM[290] -1,
set RAM[291] -1,
set RAM[292] -1,
set RAM[293] -1,
set RAM[294] -1,
set RAM[295] -1,
set RAM[296] -1,
set RAM[297] -1,
set RAM[298] -1,
set RAM[299] -1,
repeat 4000 {
ticktock;
}
output-list RAM[0]%D1.6.1 RAM[1]%D1.6.1 RAM[2]%D1.6.1 RAM[3]%D1.6.1 RAM[4]%D1.6.1 RAM[5]%D1.6.1 RAM[6]%D1.6.1;
output;
`;
export const cmp = `| RAM[0] | RAM[1] | RAM[2] | RAM[3] | RAM[4] | RAM[5] | RAM[6] |
| 261 | 261 | 256 | 4000 | 5000 | 135 | 246 |
`;
@@ -0,0 +1,98 @@
export const main = `// 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/8/FunctionCalls/FibonacciElement/Main.vm
// Contains one function: Main.fibonacci.
// Computes the n'th element of the Fibonacci series, recursively.
// n is given in argument[0]. Called by the Sys.init function
// (part of the Sys.vm file), which sets argument[0] to an input
// value and then calls Main.fibonacci.
function Main.fibonacci 0
push argument 0
push constant 2
lt
if-goto N_LT_2
goto N_GE_2
label N_LT_2 // if n < 2 returns n
push argument 0
return
label N_GE_2 // if n >= 2 returns fib(n - 2) + fib(n - 1)
push argument 0
push constant 2
sub
call Main.fibonacci 1 // computes fib(n - 2)
push argument 0
push constant 1
sub
call Main.fibonacci 1 // computes fib(n - 1)
add // returns fib(n - 1) + fib(n - 2)
return
`;
export const sys = `// 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/8/FunctionCalls/FibonacciElement/Sys.vm
// Containts one function: Sys.init.
// Pushes a constant n onto the stack, and calls the Main.fibonacii
// function, which computes the n'th element of the Fibonacci series.
// Note that by convention, the Sys.init function is called "automatically"
// by the bootstrap code generated by the VM translator.
function Sys.init 0
// Computes fibonacci(4)
push constant 4
// Calls the function, informing that one argument was pushed onto the stack
call Main.fibonacci 1
label END
goto END // loops infinitely`;
export const vm_tst = `// 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/8/FunctionCalls/FibonacciElement/FibonacciElementVME.tst
// Tests and illustrates the given Fibonacci element program on the VM emulator.
load,
compare-to FibonacciElement.cmp,
set sp 261,
repeat 110 {
vmstep;
}
// Outputs the stack pointer and the value at the stack's base.
// That's where the implementation should put the return value.
output-list RAM[0]%D1.6.1 RAM[261]%D1.6.1;
output;
`;
export const hdl_tst = `// 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/8/FunctionCalls/FibonacciElement/FibonacciElement.tst
// Tests FibonacciElement.asm on the CPU emulator.
// FibonacciElement.asm results from translating Main.vm and Sys.vm into
// a single assembly program, stored in the file FibonacciElement.asm.
compare-to FibonacciElement.cmp,
repeat 6000 {
ticktock;
}
// Outputs the stack pointer and the value at the stack's base.
// That's where the implementation should put the return value.
output-list RAM[0]%D1.6.1 RAM[261]%D1.6.1;
output;
`;
export const cmp = `| RAM[0] |RAM[261]|
| 262 | 3 |
`;
@@ -0,0 +1,107 @@
export const class1 = `// 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/8/FunctionCalls/StaticsTest/Class1.vm
// Stores two supplied arguments in static[0] and static[1].
function Class1.set 0
push argument 0
pop static 0
push argument 1
pop static 1
push constant 0
return
// Returns static[0] - static[1].
function Class1.get 0
push static 0
push static 1
sub
return`;
export const class2 = `// 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/8/FunctionCalls/StaticsTest/Class2.vm
// Stores two supplied arguments in static[0] and static[1].
function Class2.set 0
push argument 0
pop static 0
push argument 1
pop static 1
push constant 0
return
// Returns static[0] - static[1].
function Class2.get 0
push static 0
push static 1
sub
return`;
export const sys = `// 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/8/FunctionCalls/StaticsTest/Sys.vm
// Tests that different functions, stored in two different
// class files, manipulate the static segment correctly.
function Sys.init 0
push constant 6
push constant 8
call Class1.set 2
pop temp 0 // dumps the return value
push constant 23
push constant 15
call Class2.set 2
pop temp 0 // dumps the return value
call Class1.get 0
call Class2.get 0
label END
goto END`;
export const vm_tst = `// 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/8/FunctionCalls/StaticsTest/StaticsTestVME.tst
// Tests and illustrates the statics test on the VM emulator.
load,
compare-to StaticsTest.cmp,
set sp 261,
repeat 36 {
vmstep;
}
output-list RAM[0]%D1.6.1 RAM[261]%D1.6.1 RAM[262]%D1.6.1;
output;
`;
export const hdl_tst = `// 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/8/FunctionCalls/StaticsTest/StaticsTest.tst
// Tests StaticTest.asm in the CPU emulator.
// This assembly file results from translating the staticsTest folder.
compare-to StaticsTest.cmp,
set RAM[0] 256,
repeat 2500 {
ticktock;
}
output-list RAM[0]%D1.6.1 RAM[261]%D1.6.1 RAM[262]%D1.6.1;
output;
`;
export const cmp = `| RAM[0] |RAM[261]|RAM[262]|
| 263 | -2 | 8 |
`;
@@ -0,0 +1,64 @@
import { FileSystem, reset } from "@davidsouther/jiffies/lib/esm/fs.js";
import { resetBySuffix } from "../reset.js";
import * as basic_loop from "./11_basic_loop.js";
import * as fibonacci_series from "./12_fibonacci_series.js";
import * as simple_function from "./20_simple_function.js";
import * as nested_call from "./21_nested_call.js";
import * as fibonacci_element from "./22_fibonacci_element.js";
import * as statics_test from "./23_statics_test.js";
export const VMS = {
BasicLoop: {
"BasicLoop.vm": basic_loop.vm,
"BasicLoopVME.tst": basic_loop.vm_tst,
"BasicLoop.cmp": basic_loop.cmp,
"BasicLoop.tst": basic_loop.hdl_tst,
},
FibonacciSeries: {
"FibonacciSeries.vm": fibonacci_series.vm,
"FibonacciSeriesVME.tst": fibonacci_series.vm_tst,
"FibonacciSeries.cmp": fibonacci_series.cmp,
"FibonacciSeries.tst": fibonacci_series.hdl_tst,
},
SimpleFunction: {
"SimpleFunction.vm": simple_function.vm,
"SimpleFunctionVME.tst": simple_function.vm_tst,
"SimpleFunction.cmp": simple_function.cmp,
"SimpleFunction.tst": simple_function.hdl_tst,
},
NestedCall: {
"Sys.vm": nested_call.sys, // Test uses a special name here
"NestedCallVME.tst": nested_call.vm_tst,
"NestedCall.cmp": nested_call.cmp,
"NestedCall.tst": nested_call.hdl_tst,
},
FibonacciElement: {
"Sys.vm": fibonacci_element.sys,
"Main.vm": fibonacci_element.main,
"FibonacciElementVME.tst": fibonacci_element.vm_tst,
"FibonacciElement.cmp": fibonacci_element.cmp,
"FibonacciElement.tst": fibonacci_element.hdl_tst,
},
StaticsTest: {
"Class1.vm": statics_test.class1,
"Class2.vm": statics_test.class2,
"Sys.vm": statics_test.sys,
"StaticsTestVME.tst": statics_test.vm_tst,
"StaticsTest.cmp": statics_test.cmp,
"StaticsTest.tst": statics_test.hdl_tst,
},
};
export async function resetFiles(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/08");
await reset(fs, VMS);
await fs.popd();
}
export async function resetTests(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/08");
await resetBySuffix(fs, VMS, ".tst");
await resetBySuffix(fs, VMS, "VME.tst");
await resetBySuffix(fs, VMS, ".cmp");
await fs.popd();
}
+40
View File
@@ -0,0 +1,40 @@
import { Tree } from "@davidsouther/jiffies/lib/esm/fs";
import { FileSystem } from "@davidsouther/jiffies/lib/esm/fs.js";
export async function resetBySuffix(
fs: FileSystem,
tree: Tree,
suffix: string,
) {
for (const [key, value] of Object.entries(tree)) {
if (typeof value === "string") {
if (key.endsWith(`${suffix}`)) {
await fs.writeFile(key, value);
}
} else {
fs.cd(key);
await resetBySuffix(fs, value as Tree, suffix);
fs.cd("..");
}
}
}
export async function reset(
fs: FileSystem,
tree: Tree,
base?: string,
override = true,
) {
const items = (await fs.scandir(base ?? "/")).map((item) => item.name);
for (const [key, value] of Object.entries(tree)) {
const path = `${base ? `${base}/` : ""}${key}`;
if (typeof value === "string") {
if (override || !items.includes(key)) {
await fs.writeFile(path, value);
}
} else {
await fs.mkdir(path);
await reset(fs, value as Tree, path);
}
}
}
@@ -0,0 +1,7 @@
| RAM[2] |
| 0 |
| 0 |
| 0 |
| 3 |
| 8 |
| 42 |
@@ -0,0 +1,48 @@
output-list RAM[2]%D2.6.2;
set RAM[0] 0,
set RAM[1] 0;
repeat 20 {
ticktock;
}
output;
set PC 0,
set RAM[0] 1,
set RAM[1] 0;
repeat 50 {
ticktock;
}
output;
set PC 0,
set RAM[0] 0,
set RAM[1] 2;
repeat 80 {
ticktock;
}
output;
set PC 0,
set RAM[0] 3,
set RAM[1] 1;
repeat 120 {
ticktock;
}
output;
set PC 0,
set RAM[0] 2,
set RAM[1] 4;
repeat 150 {
ticktock;
}
output;
set PC 0,
set RAM[0] 6,
set RAM[1] 7;
repeat 210 {
ticktock;
}
output;
+276
View File
@@ -0,0 +1,276 @@
export const Fill = `// Runs an infinite loop that listens to the keyboard input.
// When a key is pressed (any key), the program blackens the screen,
// i.e. writes "black" in every pixel. When no key is pressed,
// the screen should be cleared.
// initialize variables
(START) // while (true)
@SCREEN
D=A
@i
M=D // i = SCREEN
(LOOP) // while (i < screen - 1)
@i
D=M
@24575 // screen - 1
D=A-D
@START
D;JLT // i < screen - 1 (negative check)
@KBD
D=M
@BLACKEN // if (kbd != 0)
D;JNE
// Clear the screen (write "white" in every pixel)
@i
D=M
A=D
M=0 // write "white"
@CONTINUE
0;JMP
(BLACKEN)
@i
D=M
A=D
M=-1 // write "black"
(CONTINUE)
@i
M=M+1 // i++
@LOOP
0;JMP
`;
export const FillAlt1 = `// 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/04/Fill.asm
// This version uses two similar loops to fill the screen either with
// black or white. Each loop always starts filling the screen from
// the top. The press or release of a key flips between the two loops.
//
// The advantage of this program over the single loop program is that
// it is much more responsive to changes in key state. One can watch
// it working even when the CPU simulator is running with animation.
(FILL_WHITE)
@SCREEN
D=A
@pScreen
M=D // pScreen = pointer to SCREEN
(WHITE_LOOP)
@KBD // if key down fill screen with black
D=M
@FILL_BLACK
D;JNE
@pScreen // Memory[pScreen] = black (0)
A=M
M=0
@pScreen // pScreen = pScreen + 1
MD=M+1
@24576 // SCREEN + size of screen
D=D-A
@WHITE_LOOP
D;JLT // loop if pScreen still within screen
@FILL_WHITE // restart at beginning of screen
0;JMP
(FILL_BLACK)
@SCREEN
D=A
@pScreen
M=D // pScreen = pointer to SCREEN
(BLACK_LOOP)
@KBD // if key down fill screen with white
D=M
@FILL_WHITE
D;JEQ
@pScreen // Memory[pScreen] = white (-1)
A=M
M=-1
@pScreen // pScreen = pScreen + 1
MD=M+1
@24576 // SCREEN + size of screen
D=D-A
@BLACK_LOOP
D;JLT // loop if pScreen still within screen
@FILL_BLACK // restart at beginning of screen
0;JMP
`;
export const FillAlt2 = `// This version uses a single loop. When a key is down, the loop
// writes black and increments the screen pointer. When no key is
// down, it decrements the screen pointer and writes white.
//
// The advantages of this program over the original single loop program
// and the double loop program are that it is much more responsive to
// changes in key state and that it does not leave partially written
// black bands temporarily visible on the screen. (And it looks cool!)
@SCREEN
D=A
@pScreen
M=D // pScreen = pointer to SCREEN
(LOOP)
@KBD // if key down fill screen with black
D=M
@FILL_BLACK
D;JNE
// Roll back the darkness
@pScreen
D=M
@SCREEN // if pScreen <= SCREEN there is nothing to do
D=D-A
@LOOP
D;JLE
@pScreen
AM=M-1 // A = pScreen = pScreen - 1
M=0 // Memory[pScreen] = white (0)
@LOOP
0;JMP
(FILL_BLACK) // Let darkness descend
@pScreen
D=M
@24576 // if pScreen >= SCREEN + size of screen,
D=D-A // there is nothing to do
@LOOP
D;JGE
@pScreen // Memory[pScreen] = black (-1)
A=M
M=-1
@pScreen // pScreen = pScreen + 1
M=M+1
@LOOP
0;JMP
`;
export const Mult = `// Multiplies R0 and R1 and stores the result in R2.
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
// The algorithm is based on repetitive addition.
// Initialize the variables
@sum
M=0
@R0
D=M
@i
M=D
(LOOP)
@i
MD=M-1 // decrement the counter
@FINISH
D;JLT
@R1
D=M
@sum
M=D+M // tally the sum
@LOOP
0;JMP
(FINISH)
@sum
D=M
@R2
M=D
(END)
@END
0;JMP`;
export const MultAlt1 = `// Multiplies R0 and R1 and stores the result in R2.
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
// The algorithm is based on repetitive addition.
@R2 // Zero the partial product
M=0
(LOOP)
@R1
MD=M-1 // Decrement and test the multiplier
@HALT
D;JLT
@R0 // Add the multiplicand to the partial product
D=M
@R2
M=D+M
@LOOP
0;JMP
(HALT)
@HALT
0;JMP`;
export const MultCmp = `| RAM[2] |
| 0 |
| 0 |
| 0 |
| 3 |
| 8 |
| 42 |`;
export const MultTst = `output-list RAM[2]%D2.6.2;
set RAM[0] 0,
set RAM[1] 0;
repeat 20 {
ticktock;
}
output;
set PC 0,
set RAM[0] 1,
set RAM[1] 0;
repeat 50 {
ticktock;
}
output;
set PC 0,
set RAM[0] 0,
set RAM[1] 2;
repeat 80 {
ticktock;
}
output;
set PC 0,
set RAM[0] 3,
set RAM[1] 1;
repeat 120 {
ticktock;
}
output;
set PC 0,
set RAM[0] 2,
set RAM[1] 4;
repeat 150 {
ticktock;
}
output;
set PC 0,
set RAM[0] 6,
set RAM[1] 7;
repeat 210 {
ticktock;
}
output;`;
+52
View File
@@ -0,0 +1,52 @@
export const Add = `
0000000000000010
1110110000010000
0000000000000011
1110000010010000
0000000000000000
1110001100001000`;
export const Max = `
0000000000000000
1111110000010000
0000000000000001
1111010011010000
0000000000001010
1110001100000001
0000000000000001
1111110000010000
0000000000001100
1110101010000111
0000000000000000
1111110000010000
0000000000000010
1110001100001000
0000000000001110
1110101010000111`;
export const Rect = `
0000000000000000
1111110000010000
0000000000010111
1110001100000110
0000000000010000
1110001100001000
0100000000000000
1110110000010000
0000000000010001
1110001100001000
0000000000010001
1111110000100000
1110111010001000
0000000000010001
1111110000010000
0000000000100000
1110000010010000
0000000000010001
1110001100001000
0000000000010000
1111110010011000
0000000000001010
1110001100000001
0000000000010111
1110101010000111`;
@@ -0,0 +1,43 @@
// Runs an infinite loop that listens to the keyboard input.
// When a key is pressed (any key), the program blackens the screen,
// i.e. writes "black" in every pixel. When no key is pressed,
// the screen should be cleared.
// initialize variables
(START) // while (true)
@SCREEN
D=A
@i
M=D // i = SCREEN
(LOOP) // while (i < screen - 1)
@i
D=M
@24575 // screen - 1
D=A-D
@START
D;JLT // i < screen - 1 (negative check)
@KBD
D=M
@BLACKEN // if (kbd != 0)
D;JNE
// Clear the screen (write "white" in every pixel)
@i
D=M
A=D
M=0 // write "white"
@CONTINUE
0;JMP
(BLACKEN)
@i
D=M
A=D
M=-1 // write "black"
(CONTINUE)
@i
M=M+1 // i++
@LOOP
0;JMP
@@ -0,0 +1,65 @@
// 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/04/Fill.asm
// This version uses two similar loops to fill the screen either with
// black or white. Each loop always starts filling the screen from
// the top. The press or release of a key flips between the two loops.
//
// The advantage of this program over the single loop program is that
// it is much more responsive to changes in key state. One can watch
// it working even when the CPU simulator is running with animation.
(FILL_WHITE)
@SCREEN
D=A
@pScreen
M=D // pScreen = pointer to SCREEN
(WHITE_LOOP)
@KBD // if key down fill screen with black
D=M
@FILL_BLACK
D;JNE
@pScreen // Memory[pScreen] = black (0)
A=M
M=0
@pScreen // pScreen = pScreen + 1
MD=M+1
@24576 // SCREEN + size of screen
D=D-A
@WHITE_LOOP
D;JLT // loop if pScreen still within screen
@FILL_WHITE // restart at beginning of screen
0;JMP
(FILL_BLACK)
@SCREEN
D=A
@pScreen
M=D // pScreen = pointer to SCREEN
(BLACK_LOOP)
@KBD // if key down fill screen with white
D=M
@FILL_WHITE
D;JEQ
@pScreen // Memory[pScreen] = white (-1)
A=M
M=-1
@pScreen // pScreen = pScreen + 1
MD=M+1
@24576 // SCREEN + size of screen
D=D-A
@BLACK_LOOP
D;JLT // loop if pScreen still within screen
@FILL_BLACK // restart at beginning of screen
0;JMP
@@ -0,0 +1,83 @@
// This version uses a single loop. When a key is down, the loop
// writes black and increments the screen pointer. When no key is
// down, it decrements the screen pointer and writes white.
//
// The advantages of this program over the original single loop program
// and the double loop program are that it is much more responsive to
// changes in key state and that it does not leave partially written
// black bands temporarily visible on the screen. (And it looks cool!)
@SCREEN
D=A
@pScreen
M=D // pScreen = pointer to SCREEN
(LOOP)
@KBD // if key down fill screen with black
D=M
@FILL_BLACK
D;JNE
// Roll back the darkness
@pScreen
D=M
@SCREEN // if pScreen <= SCREEN there is nothing to do
D=D-A
@LOOP
D;JLE
@pScreen
AM=M-1 // A = pScreen = pScreen - 1
M=0 // Memory[pScreen] = white (0)
@LOOP
0;JMP
(FILL_BLACK) // Let darkness descend
@pScreen
D=M
@24576 // if pScreen >= SCREEN + size of screen,
D=D-A // there is nothing to do
@LOOP
D;JGE
@pScreen // Memory[pScreen] = black (-1)
A=M
M=-1
@pScreen // pScreen = pScreen + 1
M=M+1
@LOOP
0;JMP
`;
export const Mult = `// Multiplies R0 and R1 and stores the result in R2.
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
// The algorithm is based on repetitive addition.
// Initialize the variables
@sum
M=0
@R0
D=M
@i
M=D
(LOOP)
@i
MD=M-1 // decrement the counter
@FINISH
D;JLT
@R1
D=M
@sum
M=D+M // tally the sum
@LOOP
0;JMP
(FINISH)
@sum
D=M
@R2
M=D
(END)
@END
0;JMP
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,21 @@
// Computes R2 = max(R0, R1)
@R0
D=M // D = first number
@R1
D=D-M // D = first number - second number
@OUTPUT_FIRST
D;JGT // if D>0 (first is greater) goto output_first
@R1
D=M // D = second number
@OUTPUT_D
0;JMP // goto output_d
(OUTPUT_FIRST)
@R0
D=M // D = first number
(OUTPUT_D)
@R2
M=D // M[2] = D (greatest number)
(INFINITE_LOOP)
@INFINITE_LOOP
0;JMP
@@ -0,0 +1,6 @@
load Max.asm;
set %0 16548, set %1 12944;
repeat 14 {
tick, tock;
}
@@ -0,0 +1,23 @@
// Multiplies R0 and R1 and stores the result in R2.
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
// The algorithm is based on repetitive addition.
@R2 // Zero the partial product
M=0
(LOOP)
@R1
MD=M-1 // Decrement and test the multiplier
@HALT
D;JLT
@R0 // Add the multiplicand to the partial product
D=M
@R2
M=D+M
@LOOP
0;JMP
(HALT)
@HALT
0;JMP
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,18 @@
import { FileSystem, reset } from "@davidsouther/jiffies/lib/esm/fs.js";
import * as project_06 from "./project_06/index.js";
import * as Tst from "./tst.js";
const TST = {
"TickTock.tst": Tst.TickTock,
};
export async function loadSamples(fs: FileSystem) {
await fs.pushd("/samples");
await reset(fs, TST);
await reset(fs, project_06.FILES);
await fs.popd();
}
export const AsmProjects = {
"06": project_06,
};
@@ -0,0 +1,15 @@
export const AddTst = `repeat 6 { tick, tock; }`;
export const AddAsm = `// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// Computes R0 = 2 + 3 (R0 refers to RAM[0])
@2
D=A
@3
D=D+A
@0
M=D`;
export const AddHack = [2, 60432, 3, 57488, 0, 58120];
@@ -0,0 +1,69 @@
export const MaxTst = `
load Max.asm;
set %0 16548, set %1 12944;
repeat 14 {
tick, tock;
}
`;
export const MaxAsm = `// 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/6/max/Max.asm
// Computes R2 = max(R0, R1) (R0,R1,R2 refer to RAM[0],RAM[1],RAM[2])
// Usage: Before executing, put two values in R0 and R1.
// D = R0 - R1
@R0
D=M
@R1
D=D-M
// If (D > 0) goto ITSR0
@ITSR0
D;JGT
// Its R1
@R1
D=M
@OUTPUT_D
0;JMP
(ITSR0)
@R0
D=M
(OUTPUT_D)
@R2
M=D
(END)
@END
0;JMP
`;
export const MaxLAsm = `// 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/6/max/MaxL.asm
// Symbol-less version of the Max.asm program.
// Designed for testing the basic version of the assembler.
@0
D=M
@1
D=D-M
@10
D;JGT
@1
D=M
@12
0;JMP
@0
D=M
@2
M=D
@14
0;JMP`;
export const MaxHack = [
0, 64528, 1, 62672, 10, 58113, 1, 64528, 12, 60039, 0, 64528, 2, 58120, 14,
60039,
];
@@ -0,0 +1,88 @@
export const RectTst = `
load Rect.asm;
set %0 16;
repeat {
tick, tock;
}
`;
export const RectAsm = `// 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/6/rect/Rect.asm
// Draws a rectangle at the top-left corner of the screen.
// The rectangle is 16 pixels wide and R0 pixels high.
// Usage: Before executing, put a value in R0.
// If (R0 <= 0) goto END else n = R0
@R0
D=M
@END
D;JLE
@n
M=D
// addr = base address of first screen row
@SCREEN
D=A
@addr
M=D
(LOOP)
// RAM[addr] = -1
@addr
A=M
M=-1
// addr = base address of next screen row
@addr
D=M
@32
D=D+A
@addr
M=D
// decrements n and loops
@n
MD=M-1
@LOOP
D;JGT
(END)
@END
0;JMP`;
export const RectLAsm = `// 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/6/rect/RectL.asm
// Symbol-less version of the Rect.asm program.
// Designed for testing the basic version of the assembler.
@0
D=M
@23
D;JLE
@16
M=D
@16384
D=A
@17
M=D
@17
A=M
M=-1
@17
D=M
@32
D=D+A
@17
M=D
@16
MD=M-1
@10
D;JGT
@23
0;JMP`;
export const RectHack = [
0, 64528, 23, 58118, 16, 58120, 16384, 60432, 17, 58120, 17, 64544, 61064, 17,
64528, 32, 57488, 17, 58120, 16, 64664, 10, 58113, 23, 60039,
];
@@ -0,0 +1,3 @@
export const PongAsm = async () => (await import("./04_pong_asm.js")).PongAsm;
export const PongLAsm = async () => (await import("./04_pong_asm.js")).PongLAsm;
export const PongHack = async () => (await import("./04_pong_asm.js")).PongHack;
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,49 @@
export const FillAsm = `// Runs an infinite loop that listens to the keyboard input.
// When a key is pressed (any key), the program blackens the screen,
// i.e. writes "black" in every pixel. When no key is pressed,
// the screen should be cleared.
// initialize variables
(START) // while (true)
@SCREEN
D=A
@i
M=D // i = SCREEN
(LOOP) // while (i < screen - 1)
@i
D=M
@24575 // screen - 1
D=A-D
@START
D;JLT // i < screen - 1 (negative check)
@KBD
D=M
@BLACKEN // if (kbd != 0)
D;JNE
// Clear the screen (write "white" in every pixel)
@i
D=M
A=D
M=0 // write "white"
@CONTINUE
0;JMP
(BLACKEN)
@i
D=M
A=D
M=-1 // write "black"
(CONTINUE)
@i
M=M+1 // i++
@LOOP
0;JMP`;
export const FillHack = [
16384, 60432, 16, 58120, 16, 64528, 24575, 57808, 0, 58116, 24576, 64528, 20,
58117, 16, 64528, 58144, 60040, 24, 60039, 16, 64528, 58144, 61064, 16, 64968,
4, 60039,
];
@@ -0,0 +1,35 @@
import { FileSystem, reset } from "@davidsouther/jiffies/lib/esm/fs.js";
import { AddAsm, AddHack } from "./01_add.js";
import { MaxAsm, MaxHack, MaxLAsm } from "./02_max.js";
import { RectAsm, RectHack, RectLAsm } from "./03_rect.js";
import { PongAsm, PongHack, PongLAsm } from "./04_pong_asm.js";
export const FILES = {
"Add.asm": AddAsm,
"Max.asm": MaxAsm,
"MaxL.asm": MaxLAsm,
"Rect.asm": RectAsm,
"RectL.asm": RectLAsm,
"Pong.asm": PongAsm,
"PongL.asm": PongLAsm,
};
export const ASM_SOLS: Record<keyof typeof FILES, number[]> = {
"Add.asm": AddHack,
"Max.asm": MaxHack,
"MaxL.asm": MaxHack,
"Rect.asm": RectHack,
"RectL.asm": RectHack,
"Pong.asm": PongHack,
"PongL.asm": PongHack,
};
export async function resetFiles(fs: FileSystem): Promise<void> {
await fs.pushd("/projects/06");
await reset(fs, FILES);
await fs.popd();
}
export async function resetTests(fs: FileSystem): Promise<void> {
return;
}
@@ -0,0 +1,439 @@
export const jack = `// 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/11/Average/Main.jack
// Inputs some numbers and computes their average
class Main {
function void main() {
var Array a;
var int length;
var int i, sum;
let length = Keyboard.readInt("How many numbers? ");
let a = Array.new(length); // constructs the array
let i = 0;
while (i < length) {
let a[i] = Keyboard.readInt("Enter a number: ");
let sum = sum + a[i];
let i = i + 1;
}
do Output.printString("The average is ");
do Output.printInt(sum / length);
return;
}
}`;
export const parsed = {
name: { value: "Main", span: { start: 234, end: 238, line: 7 } },
varDecs: [],
subroutines: [
{
type: "function",
returnType: { value: "void", span: { start: 253, end: 258, line: 8 } },
name: { value: "main", span: { start: 258, end: 262, line: 8 } },
parameters: [],
body: {
varDecs: [
{
type: { value: "Array", span: { start: 277, end: 283, line: 9 } },
names: ["a"],
},
{
type: { value: "int", span: { start: 297, end: 301, line: 10 } },
names: ["length"],
},
{
type: { value: "int", span: { start: 319, end: 323, line: 11 } },
names: ["i", "sum"],
},
],
statements: [
{
statementType: "letStatement",
name: { value: "length", span: { start: 342, end: 348, line: 13 } },
value: {
nodeType: "expression",
term: {
termType: "subroutineCall",
name: {
value: "Keyboard.readInt",
span: { start: 351, end: 367, line: 13 },
},
parameters: [
{
nodeType: "expression",
term: {
termType: "stringLiteral",
value: "How many numbers? ",
},
rest: [],
},
],
span: { start: 351, end: 389, line: 13 },
},
rest: [],
},
span: { start: 338, end: 390, line: 13 },
},
{
statementType: "letStatement",
name: { value: "a", span: { start: 401, end: 402, line: 14 } },
value: {
nodeType: "expression",
term: {
termType: "subroutineCall",
name: {
value: "Array.new",
span: { start: 405, end: 414, line: 14 },
},
parameters: [
{
nodeType: "expression",
term: {
termType: "variable",
name: "length",
span: { start: 415, end: 421, line: 14 },
},
rest: [],
},
],
span: { start: 405, end: 422, line: 14 },
},
rest: [],
},
span: { start: 397, end: 423, line: 14 },
},
{
statementType: "letStatement",
name: { value: "i", span: { start: 464, end: 465, line: 16 } },
value: {
nodeType: "expression",
term: { termType: "numericLiteral", value: 0 },
rest: [],
},
span: { start: 460, end: 470, line: 16 },
},
{
statementType: "whileStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "i",
span: { start: 484, end: 485, line: 17 },
},
rest: [
{
op: "<",
term: {
termType: "variable",
name: "length",
span: { start: 488, end: 494, line: 17 },
},
},
],
},
body: [
{
statementType: "letStatement",
name: { value: "a", span: { start: 511, end: 512, line: 18 } },
arrayIndex: {
nodeType: "expression",
term: {
termType: "variable",
name: "i",
span: { start: 513, end: 514, line: 18 },
},
rest: [],
},
value: {
nodeType: "expression",
term: {
termType: "subroutineCall",
name: {
value: "Keyboard.readInt",
span: { start: 518, end: 534, line: 18 },
},
parameters: [
{
nodeType: "expression",
term: {
termType: "stringLiteral",
value: "Enter a number: ",
},
rest: [],
},
],
span: { start: 518, end: 554, line: 18 },
},
rest: [],
},
span: { start: 507, end: 555, line: 18 },
},
{
statementType: "letStatement",
name: {
value: "sum",
span: { start: 569, end: 572, line: 19 },
},
value: {
nodeType: "expression",
term: {
termType: "variable",
name: "sum",
span: { start: 575, end: 578, line: 19 },
},
rest: [
{
op: "+",
term: {
termType: "arrayAccess",
name: {
value: "a",
span: { start: 581, end: 582, line: 19 },
},
index: {
nodeType: "expression",
term: {
termType: "variable",
name: "i",
span: { start: 583, end: 584, line: 19 },
},
rest: [],
},
span: { start: 581, end: 585, line: 19 },
},
},
],
},
span: { start: 565, end: 586, line: 19 },
},
{
statementType: "letStatement",
name: { value: "i", span: { start: 600, end: 601, line: 20 } },
value: {
nodeType: "expression",
term: {
termType: "variable",
name: "i",
span: { start: 604, end: 605, line: 20 },
},
rest: [
{ op: "+", term: { termType: "numericLiteral", value: 1 } },
],
},
span: { start: 596, end: 610, line: 20 },
},
],
},
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "Output.printString",
span: { start: 629, end: 647, line: 23 },
},
parameters: [
{
nodeType: "expression",
term: { termType: "stringLiteral", value: "The average is " },
rest: [],
},
],
span: { start: 629, end: 666, line: 23 },
},
},
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "Output.printInt",
span: { start: 677, end: 692, line: 24 },
},
parameters: [
{
nodeType: "expression",
term: {
termType: "variable",
name: "sum",
span: { start: 693, end: 696, line: 24 },
},
rest: [
{
op: "/",
term: {
termType: "variable",
name: "length",
span: { start: 699, end: 705, line: 24 },
},
},
],
},
],
span: { start: 677, end: 706, line: 24 },
},
},
{
statementType: "returnStatement",
span: { start: 714, end: 721, line: 25 },
},
],
},
},
],
};
export const compiled = `function Main.main 4
push constant 18
call String.new 1
push constant 72
call String.appendChar 2
push constant 111
call String.appendChar 2
push constant 119
call String.appendChar 2
push constant 32
call String.appendChar 2
push constant 109
call String.appendChar 2
push constant 97
call String.appendChar 2
push constant 110
call String.appendChar 2
push constant 121
call String.appendChar 2
push constant 32
call String.appendChar 2
push constant 110
call String.appendChar 2
push constant 117
call String.appendChar 2
push constant 109
call String.appendChar 2
push constant 98
call String.appendChar 2
push constant 101
call String.appendChar 2
push constant 114
call String.appendChar 2
push constant 115
call String.appendChar 2
push constant 63
call String.appendChar 2
push constant 32
call String.appendChar 2
call Keyboard.readInt 1
pop local 1
push local 1
call Array.new 1
pop local 0
push constant 0
pop local 2
label Main_0
push local 2
push local 1
lt
not
if-goto Main_1
push local 2
push local 0
add
push constant 16
call String.new 1
push constant 69
call String.appendChar 2
push constant 110
call String.appendChar 2
push constant 116
call String.appendChar 2
push constant 101
call String.appendChar 2
push constant 114
call String.appendChar 2
push constant 32
call String.appendChar 2
push constant 97
call String.appendChar 2
push constant 32
call String.appendChar 2
push constant 110
call String.appendChar 2
push constant 117
call String.appendChar 2
push constant 109
call String.appendChar 2
push constant 98
call String.appendChar 2
push constant 101
call String.appendChar 2
push constant 114
call String.appendChar 2
push constant 58
call String.appendChar 2
push constant 32
call String.appendChar 2
call Keyboard.readInt 1
pop temp 0
pop pointer 1
push temp 0
pop that 0
push local 3
push local 2
push local 0
add
pop pointer 1
push that 0
add
pop local 3
push local 2
push constant 1
add
pop local 2
goto Main_0
label Main_1
push constant 15
call String.new 1
push constant 84
call String.appendChar 2
push constant 104
call String.appendChar 2
push constant 101
call String.appendChar 2
push constant 32
call String.appendChar 2
push constant 97
call String.appendChar 2
push constant 118
call String.appendChar 2
push constant 101
call String.appendChar 2
push constant 114
call String.appendChar 2
push constant 97
call String.appendChar 2
push constant 103
call String.appendChar 2
push constant 101
call String.appendChar 2
push constant 32
call String.appendChar 2
push constant 105
call String.appendChar 2
push constant 115
call String.appendChar 2
push constant 32
call String.appendChar 2
call Output.printString 1
pop temp 0
push local 3
push local 1
call Math.divide 2
call Output.printInt 1
pop temp 0
push constant 0
return`;
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,734 @@
export const jack = `// 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/11/ConvertToBin/Main.jack
/**
* Unpacks a 16-bit number into its binary representation:
* Takes the 16-bit number stored in RAM[8000] and stores its individual
* bits in RAM[8001]..RAM[8016] (each location will contain 0 or 1).
* Before the conversion, RAM[8001]..RAM[8016] are initialized to -1.
*
* The program should be tested as follows:
* 1) Load the program into the supplied VM emulator
* 2) Put some value in RAM[8000]
* 3) Switch to "no animation"
* 4) Run the program (give it enough time to run)
* 5) Stop the program
* 6) Check that RAM[8001]..RAM[8016] contain the correct bits, and
* that none of these memory locations contains -1.
*/
class Main {
/**
* Initializes RAM[8001]..RAM[8016] to -1,
* and converts the value in RAM[8000] to binary.
*/
function void main() {
var int value;
do Main.fillMemory(8001, 16, -1); // sets RAM[8001]..RAM[8016] to -1
let value = Memory.peek(8000); // Uses an OS routine to read the input
do Main.convert(value); // performs the conversion
return;
}
/** Converts the given decimal value to binary, and puts
* the resulting bits in RAM[8001]..RAM[8016]. */
function void convert(int value) {
var int mask, position;
var boolean loop;
let loop = true;
while (loop) {
let position = position + 1;
let mask = Main.nextMask(mask);
if (~(position > 16)) {
if (~((value & mask) = 0)) {
do Memory.poke(8000 + position, 1);
}
else {
do Memory.poke(8000 + position, 0);
}
}
else {
let loop = false;
}
}
return;
}
/** Returns the next mask (the mask that should follow the given mask). */
function int nextMask(int mask) {
if (mask = 0) {
return 1;
}
else {
return mask * 2;
}
}
/** Fills 'length' consecutive memory locations with 'value',
* starting at 'address'. */
function void fillMemory(int address, int length, int value) {
while (length > 0) {
do Memory.poke(address, value);
let length = length - 1;
let address = address + 1;
}
return;
}
}`;
export const parsed = {
name: { value: "Main", span: { start: 831, end: 835, line: 20 } },
varDecs: [],
subroutines: [
{
type: "function",
returnType: { value: "void", span: { start: 973, end: 978, line: 26 } },
name: { value: "main", span: { start: 978, end: 982, line: 26 } },
parameters: [],
body: {
varDecs: [
{
type: { value: "int", span: { start: 997, end: 1001, line: 27 } },
names: ["value"],
},
],
statements: [
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "Main.fillMemory",
span: { start: 1019, end: 1034, line: 28 },
},
parameters: [
{
nodeType: "expression",
term: { termType: "numericLiteral", value: 8001 },
rest: [],
},
{
nodeType: "expression",
term: { termType: "numericLiteral", value: 16 },
rest: [],
},
{
nodeType: "expression",
term: {
termType: "unaryExpression",
op: "-",
term: { termType: "numericLiteral", value: 1 },
},
rest: [],
},
],
span: { start: 1019, end: 1048, line: 28 },
},
},
{
statementType: "letStatement",
name: {
value: "value",
span: { start: 1097, end: 1102, line: 29 },
},
value: {
nodeType: "expression",
term: {
termType: "subroutineCall",
name: {
value: "Memory.peek",
span: { start: 1105, end: 1116, line: 29 },
},
parameters: [
{
nodeType: "expression",
term: { termType: "numericLiteral", value: 8000 },
rest: [],
},
],
span: { start: 1105, end: 1122, line: 29 },
},
rest: [],
},
span: { start: 1093, end: 1123, line: 29 },
},
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "Main.convert",
span: { start: 1178, end: 1190, line: 30 },
},
parameters: [
{
nodeType: "expression",
term: {
termType: "variable",
name: "value",
span: { start: 1191, end: 1196, line: 30 },
},
rest: [],
},
],
span: { start: 1178, end: 1197, line: 30 },
},
},
{
statementType: "returnStatement",
span: { start: 1244, end: 1251, line: 31 },
},
],
},
},
{
type: "function",
returnType: { value: "void", span: { start: 1393, end: 1398, line: 36 } },
name: { value: "convert", span: { start: 1398, end: 1405, line: 36 } },
parameters: [
{
type: { value: "int", span: { start: 1406, end: 1410, line: 36 } },
name: "value",
},
],
body: {
varDecs: [
{
type: { value: "int", span: { start: 1429, end: 1433, line: 37 } },
names: ["mask", "position"],
},
{
type: {
value: "boolean",
span: { start: 1459, end: 1467, line: 38 },
},
names: ["loop"],
},
],
statements: [
{
statementType: "letStatement",
name: { value: "loop", span: { start: 1490, end: 1494, line: 40 } },
value: {
nodeType: "expression",
term: { termType: "keywordLiteral", value: "true" },
rest: [],
},
span: { start: 1486, end: 1502, line: 40 },
},
{
statementType: "whileStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "loop",
span: { start: 1516, end: 1520, line: 41 },
},
rest: [],
},
body: [
{
statementType: "letStatement",
name: {
value: "position",
span: { start: 1538, end: 1546, line: 42 },
},
value: {
nodeType: "expression",
term: {
termType: "variable",
name: "position",
span: { start: 1549, end: 1557, line: 42 },
},
rest: [
{ op: "+", term: { termType: "numericLiteral", value: 1 } },
],
},
span: { start: 1534, end: 1562, line: 42 },
},
{
statementType: "letStatement",
name: {
value: "mask",
span: { start: 1577, end: 1581, line: 43 },
},
value: {
nodeType: "expression",
term: {
termType: "subroutineCall",
name: {
value: "Main.nextMask",
span: { start: 1584, end: 1597, line: 43 },
},
parameters: [
{
nodeType: "expression",
term: {
termType: "variable",
name: "mask",
span: { start: 1598, end: 1602, line: 43 },
},
rest: [],
},
],
span: { start: 1584, end: 1603, line: 43 },
},
rest: [],
},
span: { start: 1573, end: 1604, line: 43 },
},
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "unaryExpression",
op: "~",
term: {
termType: "groupedExpression",
expression: {
nodeType: "expression",
term: {
termType: "variable",
name: "position",
span: { start: 1628, end: 1636, line: 45 },
},
rest: [
{
op: ">",
term: { termType: "numericLiteral", value: 16 },
},
],
},
},
},
rest: [],
},
body: [
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "unaryExpression",
op: "~",
term: {
termType: "groupedExpression",
expression: {
nodeType: "expression",
term: {
termType: "groupedExpression",
expression: {
nodeType: "expression",
term: {
termType: "variable",
name: "value",
span: { start: 1674, end: 1679, line: 47 },
},
rest: [
{
op: "&",
term: {
termType: "variable",
name: "mask",
span: {
start: 1682,
end: 1686,
line: 47,
},
},
},
],
},
},
rest: [
{
op: "=",
term: { termType: "numericLiteral", value: 0 },
},
],
},
},
},
rest: [],
},
body: [
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "Memory.poke",
span: { start: 1717, end: 1728, line: 48 },
},
parameters: [
{
nodeType: "expression",
term: { termType: "numericLiteral", value: 8000 },
rest: [
{
op: "+",
term: {
termType: "variable",
name: "position",
span: { start: 1736, end: 1744, line: 48 },
},
},
],
},
{
nodeType: "expression",
term: { termType: "numericLiteral", value: 1 },
rest: [],
},
],
span: { start: 1717, end: 1748, line: 48 },
},
},
],
else: [
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "Memory.poke",
span: { start: 1811, end: 1822, line: 51 },
},
parameters: [
{
nodeType: "expression",
term: { termType: "numericLiteral", value: 8000 },
rest: [
{
op: "+",
term: {
termType: "variable",
name: "position",
span: { start: 1830, end: 1838, line: 51 },
},
},
],
},
{
nodeType: "expression",
term: { termType: "numericLiteral", value: 0 },
rest: [],
},
],
span: { start: 1811, end: 1842, line: 51 },
},
},
],
},
],
else: [
{
statementType: "letStatement",
name: {
value: "loop",
span: { start: 1913, end: 1917, line: 55 },
},
value: {
nodeType: "expression",
term: { termType: "keywordLiteral", value: "false" },
rest: [],
},
span: { start: 1909, end: 1926, line: 55 },
},
],
},
],
},
{
statementType: "returnStatement",
span: { start: 1953, end: 1960, line: 58 },
},
],
},
},
{
type: "function",
returnType: { value: "int", span: { start: 2061, end: 2065, line: 62 } },
name: { value: "nextMask", span: { start: 2065, end: 2073, line: 62 } },
parameters: [
{
type: { value: "int", span: { start: 2074, end: 2078, line: 62 } },
name: "mask",
},
],
body: {
varDecs: [],
statements: [
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "mask",
span: { start: 2096, end: 2100, line: 63 },
},
rest: [
{ op: "=", term: { termType: "numericLiteral", value: 0 } },
],
},
body: [
{
statementType: "returnStatement",
value: {
nodeType: "expression",
term: { termType: "numericLiteral", value: 1 },
rest: [],
},
span: { start: 2118, end: 2127, line: 64 },
},
],
else: [
{
statementType: "returnStatement",
value: {
nodeType: "expression",
term: {
termType: "variable",
name: "mask",
span: { start: 2162, end: 2166, line: 67 },
},
rest: [
{ op: "*", term: { termType: "numericLiteral", value: 2 } },
],
},
span: { start: 2155, end: 2171, line: 67 },
},
],
},
],
},
},
{
type: "function",
returnType: { value: "void", span: { start: 2304, end: 2309, line: 73 } },
name: { value: "fillMemory", span: { start: 2309, end: 2319, line: 73 } },
parameters: [
{
type: { value: "int", span: { start: 2320, end: 2324, line: 73 } },
name: "address",
},
{
type: { value: "int", span: { start: 2333, end: 2337, line: 73 } },
name: "length",
},
{
type: { value: "int", span: { start: 2345, end: 2349, line: 73 } },
name: "value",
},
],
body: {
varDecs: [],
statements: [
{
statementType: "whileStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "length",
span: { start: 2373, end: 2379, line: 74 },
},
rest: [
{ op: ">", term: { termType: "numericLiteral", value: 0 } },
],
},
body: [
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "Memory.poke",
span: { start: 2402, end: 2413, line: 75 },
},
parameters: [
{
nodeType: "expression",
term: {
termType: "variable",
name: "address",
span: { start: 2414, end: 2421, line: 75 },
},
rest: [],
},
{
nodeType: "expression",
term: {
termType: "variable",
name: "value",
span: { start: 2423, end: 2428, line: 75 },
},
rest: [],
},
],
span: { start: 2402, end: 2429, line: 75 },
},
},
{
statementType: "letStatement",
name: {
value: "length",
span: { start: 2447, end: 2453, line: 76 },
},
value: {
nodeType: "expression",
term: {
termType: "variable",
name: "length",
span: { start: 2456, end: 2462, line: 76 },
},
rest: [
{ op: "-", term: { termType: "numericLiteral", value: 1 } },
],
},
span: { start: 2443, end: 2467, line: 76 },
},
{
statementType: "letStatement",
name: {
value: "address",
span: { start: 2484, end: 2491, line: 77 },
},
value: {
nodeType: "expression",
term: {
termType: "variable",
name: "address",
span: { start: 2494, end: 2501, line: 77 },
},
rest: [
{ op: "+", term: { termType: "numericLiteral", value: 1 } },
],
},
span: { start: 2480, end: 2506, line: 77 },
},
],
},
{
statementType: "returnStatement",
span: { start: 2525, end: 2532, line: 79 },
},
],
},
},
],
};
export const compiled = `function Main.main 1
push constant 8001
push constant 16
push constant 1
neg
call Main.fillMemory 3
pop temp 0
push constant 8000
call Memory.peek 1
pop local 0
push local 0
call Main.convert 1
pop temp 0
push constant 0
return
function Main.convert 3
push constant 1
neg
pop local 2
label Main_0
push local 2
not
if-goto Main_1
push local 1
push constant 1
add
pop local 1
push local 0
call Main.nextMask 1
pop local 0
push local 1
push constant 16
gt
not
not
if-goto Main_3
push argument 0
push local 0
and
push constant 0
eq
not
not
if-goto Main_5
push constant 8000
push local 1
add
push constant 1
call Memory.poke 2
pop temp 0
goto Main_4
label Main_5
push constant 8000
push local 1
add
push constant 0
call Memory.poke 2
pop temp 0
label Main_4
goto Main_2
label Main_3
push constant 0
pop local 2
label Main_2
goto Main_0
label Main_1
push constant 0
return
function Main.nextMask 0
push argument 0
push constant 0
eq
not
if-goto Main_7
push constant 1
return
goto Main_6
label Main_7
push argument 0
push constant 2
call Math.multiply 2
return
label Main_6
function Main.fillMemory 0
label Main_8
push argument 1
push constant 0
gt
not
if-goto Main_9
push argument 0
push argument 2
call Memory.poke 2
pop temp 0
push argument 1
push constant 1
sub
pop argument 1
push argument 0
push constant 1
add
pop argument 0
goto Main_8
label Main_9
push constant 0
return`;
@@ -0,0 +1,29 @@
import * as average from "./average.js";
import * as complex_arrays from "./complex_arrays.js";
import * as convert_to_bin from "./convert_to_bin.js";
import * as pong_ball from "./pong/ball.js";
import * as pong_bat from "./pong/bat.js";
import * as pong_main from "./pong/main.js";
import * as pong_game from "./pong/pong_game.js";
import * as seven from "./seven.js";
import * as square_main from "./square/main.js";
import * as square from "./square/square.js";
import * as square_game from "./square/square_game.js";
type Program = Record<
string,
{ jack: string; parsed: unknown; compiled: string }
>;
export const Programs: Record<string, Program> = {
average: { Main: average },
complex_arrays: { Main: complex_arrays },
convert_to_bin: { Main: convert_to_bin },
pong: {
Ball: pong_ball,
Bat: pong_bat,
PongGame: pong_game,
Main: pong_main,
},
seven: { Main: seven },
square: { Square: square, Main: square_main, SquareGame: square_game },
};
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,118 @@
export const jack = `// 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/11/Pong/Main.jack
// (Same as projects/9/Pong/Main.jack)
/**
* Main class of the Pong game.
*/
class Main {
/** Initializes a Pong game and starts running it. */
function void main() {
var PongGame game;
do PongGame.newInstance();
let game = PongGame.getInstance();
do game.run();
do game.dispose();
return;
}
}`;
export const parsed = {
name: { value: "Main", span: { start: 259, end: 263, line: 9 } },
varDecs: [],
subroutines: [
{
type: "function",
returnType: { value: "void", span: { start: 338, end: 343, line: 12 } },
name: { value: "main", span: { start: 343, end: 347, line: 12 } },
parameters: [],
body: {
varDecs: [
{
type: {
value: "PongGame",
span: { start: 364, end: 373, line: 13 },
},
names: ["game"],
},
],
statements: [
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "PongGame.newInstance",
span: { start: 390, end: 410, line: 14 },
},
parameters: [],
span: { start: 390, end: 412, line: 14 },
},
},
{
statementType: "letStatement",
name: { value: "game", span: { start: 426, end: 430, line: 15 } },
value: {
nodeType: "expression",
term: {
termType: "subroutineCall",
name: {
value: "PongGame.getInstance",
span: { start: 433, end: 453, line: 15 },
},
parameters: [],
span: { start: 433, end: 455, line: 15 },
},
rest: [],
},
span: { start: 422, end: 456, line: 15 },
},
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "game.run",
span: { start: 468, end: 476, line: 16 },
},
parameters: [],
span: { start: 468, end: 478, line: 16 },
},
},
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "game.dispose",
span: { start: 491, end: 503, line: 17 },
},
parameters: [],
span: { start: 491, end: 505, line: 17 },
},
},
{
statementType: "returnStatement",
span: { start: 515, end: 522, line: 18 },
},
],
},
},
],
};
export const compiled = `function Main.main 1
call PongGame.newInstance 0
pop temp 0
call PongGame.getInstance 0
pop local 0
push local 0
call PongGame.run 1
pop temp 0
push local 0
call PongGame.dispose 1
pop temp 0
push constant 0
return`;
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,84 @@
export const jack = `// 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/11/Seven/Main.jack
/**
* Computes the value of 1 + (2 * 3) and prints the result
* at the top-left of the screen.
*/
class Main {
function void main() {
do Output.printInt(1 + (2 * 3));
return;
}
}`;
export const parsed = {
name: { value: "Main", span: { start: 284, end: 288, line: 9 } },
varDecs: [],
subroutines: [
{
type: "function",
returnType: { value: "void", span: { start: 304, end: 309, line: 11 } },
name: { value: "main", span: { start: 309, end: 313, line: 11 } },
parameters: [],
body: {
varDecs: [],
statements: [
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "Output.printInt",
span: { start: 327, end: 342, line: 12 },
},
parameters: [
{
nodeType: "expression",
term: { termType: "numericLiteral", value: 1 },
rest: [
{
op: "+",
term: {
termType: "groupedExpression",
expression: {
nodeType: "expression",
term: { termType: "numericLiteral", value: 2 },
rest: [
{
op: "*",
term: { termType: "numericLiteral", value: 3 },
},
],
},
},
},
],
},
],
span: { start: 327, end: 355, line: 12 },
},
},
{
statementType: "returnStatement",
span: { start: 363, end: 370, line: 13 },
},
],
},
},
],
};
export const compiled = `function Main.main 0
push constant 1
push constant 2
push constant 3
call Math.multiply 2
add
call Output.printInt 1
pop temp 0
push constant 0
return`;
@@ -0,0 +1,99 @@
export const jack = `// 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/11/Square/Main.jack
/** Initializes a new Square game and starts running it. */
class Main {
function void main() {
var SquareGame game;
let game = SquareGame.new();
do game.run();
do game.dispose();
return;
}
}`;
export const parsed = {
name: { value: "Main", span: { start: 243, end: 247, line: 7 } },
varDecs: [],
subroutines: [
{
type: "function",
returnType: { value: "void", span: { start: 263, end: 268, line: 8 } },
name: { value: "main", span: { start: 268, end: 272, line: 8 } },
parameters: [],
body: {
varDecs: [
{
type: {
value: "SquareGame",
span: { start: 289, end: 300, line: 9 },
},
names: ["game"],
},
],
statements: [
{
statementType: "letStatement",
name: { value: "game", span: { start: 318, end: 322, line: 10 } },
value: {
nodeType: "expression",
term: {
termType: "subroutineCall",
name: {
value: "SquareGame.new",
span: { start: 325, end: 339, line: 10 },
},
parameters: [],
span: { start: 325, end: 341, line: 10 },
},
rest: [],
},
span: { start: 314, end: 342, line: 10 },
},
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "game.run",
span: { start: 354, end: 362, line: 11 },
},
parameters: [],
span: { start: 354, end: 364, line: 11 },
},
},
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "game.dispose",
span: { start: 377, end: 389, line: 12 },
},
parameters: [],
span: { start: 377, end: 391, line: 12 },
},
},
{
statementType: "returnStatement",
span: { start: 401, end: 408, line: 13 },
},
],
},
},
],
};
export const compiled = `function Main.main 1
call SquareGame.new 0
pop local 0
push local 0
call SquareGame.run 1
pop temp 0
push local 0
call SquareGame.dispose 1
pop temp 0
push constant 0
return`;
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,948 @@
export const jack = `// 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/11/Square/SquareGame.jack
/**
* Implements the Square game.
* This simple game allows the user to move a black square around
* the screen, and change the square's size during the movement.
* When the game starts, a square of 30 by 30 pixels is shown at the
* top-left corner of the screen. The user controls the square as follows.
* The 4 arrow keys are used to move the square up, down, left, and right.
* The 'z' and 'x' keys are used, respectively, to decrement and increment
* the square's size. The 'q' key is used to quit the game.
*/
class SquareGame {
field Square square; // the square of this game
field int direction; // the square's current direction:
// 0=none, 1=up, 2=down, 3=left, 4=right
/** Constructs a new square game. */
constructor SquareGame new() {
// The initial square is located in (0,0), has size 30, and is not moving.
let square = Square.new(0, 0, 30);
let direction = 0;
return this;
}
/** Disposes this game. */
method void dispose() {
do square.dispose();
do Memory.deAlloc(this);
return;
}
/** Moves the square in the current direction. */
method void moveSquare() {
if (direction = 1) { do square.moveUp(); }
if (direction = 2) { do square.moveDown(); }
if (direction = 3) { do square.moveLeft(); }
if (direction = 4) { do square.moveRight(); }
do Sys.wait(5); // delays the next movement
return;
}
/** Runs the game: handles the user's inputs and moves the square accordingly */
method void run() {
var char key; // the key currently pressed by the user
var boolean exit;
let exit = false;
while (~exit) {
// waits for a key to be pressed
while (key = 0) {
let key = Keyboard.keyPressed();
do moveSquare();
}
if (key = 81) { let exit = true; } // q key
if (key = 90) { do square.decSize(); } // z key
if (key = 88) { do square.incSize(); } // x key
if (key = 131) { let direction = 1; } // up arrow
if (key = 133) { let direction = 2; } // down arrow
if (key = 130) { let direction = 3; } // left arrow
if (key = 132) { let direction = 4; } // right arrow
// waits for the key to be released
while (~(key = 0)) {
let key = Keyboard.keyPressed();
do moveSquare();
}
} // while
return;
}
}
`;
export const parsed = {
name: { value: "SquareGame", span: { start: 712, end: 722, line: 15 } },
varDecs: [
{
varType: "field",
type: { value: "Square", span: { start: 734, end: 741, line: 16 } },
names: ["square"],
},
{
varType: "field",
type: { value: "int", span: { start: 785, end: 789, line: 17 } },
names: ["direction"],
},
],
subroutines: [
{
type: "constructor",
returnType: {
value: "SquareGame",
span: { start: 957, end: 968, line: 21 },
},
name: { value: "new", span: { start: 968, end: 971, line: 21 } },
parameters: [],
body: {
varDecs: [],
statements: [
{
statementType: "letStatement",
name: {
value: "square",
span: { start: 1067, end: 1073, line: 23 },
},
value: {
nodeType: "expression",
term: {
termType: "subroutineCall",
name: {
value: "Square.new",
span: { start: 1076, end: 1086, line: 23 },
},
parameters: [
{
nodeType: "expression",
term: { termType: "numericLiteral", value: 0 },
rest: [],
},
{
nodeType: "expression",
term: { termType: "numericLiteral", value: 0 },
rest: [],
},
{
nodeType: "expression",
term: { termType: "numericLiteral", value: 30 },
rest: [],
},
],
span: { start: 1076, end: 1096, line: 23 },
},
rest: [],
},
span: { start: 1063, end: 1097, line: 23 },
},
{
statementType: "letStatement",
name: {
value: "direction",
span: { start: 1108, end: 1117, line: 24 },
},
value: {
nodeType: "expression",
term: { termType: "numericLiteral", value: 0 },
rest: [],
},
span: { start: 1104, end: 1122, line: 24 },
},
{
statementType: "returnStatement",
value: {
nodeType: "expression",
term: { termType: "keywordLiteral", value: "this" },
rest: [],
},
span: { start: 1129, end: 1141, line: 25 },
},
],
},
},
{
type: "method",
returnType: { value: "void", span: { start: 1188, end: 1193, line: 29 } },
name: { value: "dispose", span: { start: 1193, end: 1200, line: 29 } },
parameters: [],
body: {
varDecs: [],
statements: [
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "square.dispose",
span: { start: 1214, end: 1228, line: 30 },
},
parameters: [],
span: { start: 1214, end: 1230, line: 30 },
},
},
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "Memory.deAlloc",
span: { start: 1241, end: 1255, line: 31 },
},
parameters: [
{
nodeType: "expression",
term: { termType: "keywordLiteral", value: "this" },
rest: [],
},
],
span: { start: 1241, end: 1261, line: 31 },
},
},
{
statementType: "returnStatement",
span: { start: 1269, end: 1276, line: 32 },
},
],
},
},
{
type: "method",
returnType: { value: "void", span: { start: 1346, end: 1351, line: 36 } },
name: { value: "moveSquare", span: { start: 1351, end: 1361, line: 36 } },
parameters: [],
body: {
varDecs: [],
statements: [
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "direction",
span: { start: 1376, end: 1385, line: 37 },
},
rest: [
{ op: "=", term: { termType: "numericLiteral", value: 1 } },
],
},
body: [
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "square.moveUp",
span: { start: 1396, end: 1409, line: 37 },
},
parameters: [],
span: { start: 1396, end: 1411, line: 37 },
},
},
],
else: [],
},
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "direction",
span: { start: 1425, end: 1434, line: 38 },
},
rest: [
{ op: "=", term: { termType: "numericLiteral", value: 2 } },
],
},
body: [
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "square.moveDown",
span: { start: 1445, end: 1460, line: 38 },
},
parameters: [],
span: { start: 1445, end: 1462, line: 38 },
},
},
],
else: [],
},
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "direction",
span: { start: 1476, end: 1485, line: 39 },
},
rest: [
{ op: "=", term: { termType: "numericLiteral", value: 3 } },
],
},
body: [
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "square.moveLeft",
span: { start: 1496, end: 1511, line: 39 },
},
parameters: [],
span: { start: 1496, end: 1513, line: 39 },
},
},
],
else: [],
},
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "direction",
span: { start: 1527, end: 1536, line: 40 },
},
rest: [
{ op: "=", term: { termType: "numericLiteral", value: 4 } },
],
},
body: [
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "square.moveRight",
span: { start: 1547, end: 1563, line: 40 },
},
parameters: [],
span: { start: 1547, end: 1565, line: 40 },
},
},
],
else: [],
},
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "Sys.wait",
span: { start: 1578, end: 1586, line: 41 },
},
parameters: [
{
nodeType: "expression",
term: { termType: "numericLiteral", value: 5 },
rest: [],
},
],
span: { start: 1578, end: 1589, line: 41 },
},
},
{
statementType: "returnStatement",
span: { start: 1626, end: 1633, line: 42 },
},
],
},
},
{
type: "method",
returnType: { value: "void", span: { start: 1734, end: 1739, line: 46 } },
name: { value: "run", span: { start: 1739, end: 1742, line: 46 } },
parameters: [],
body: {
varDecs: [
{
type: { value: "char", span: { start: 1757, end: 1762, line: 47 } },
names: ["key"],
},
{
type: {
value: "boolean",
span: { start: 1819, end: 1827, line: 48 },
},
names: ["exit"],
},
],
statements: [
{
statementType: "letStatement",
name: { value: "exit", span: { start: 1843, end: 1847, line: 49 } },
value: {
nodeType: "expression",
term: { termType: "keywordLiteral", value: "false" },
rest: [],
},
span: { start: 1839, end: 1856, line: 49 },
},
{
statementType: "whileStatement",
condition: {
nodeType: "expression",
term: {
termType: "unaryExpression",
op: "~",
term: {
termType: "variable",
name: "exit",
span: { start: 1878, end: 1882, line: 51 },
},
},
rest: [],
},
body: [
{
statementType: "whileStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "key",
span: { start: 1944, end: 1947, line: 53 },
},
rest: [
{ op: "=", term: { termType: "numericLiteral", value: 0 } },
],
},
body: [
{
statementType: "letStatement",
name: {
value: "key",
span: { start: 1971, end: 1974, line: 54 },
},
value: {
nodeType: "expression",
term: {
termType: "subroutineCall",
name: {
value: "Keyboard.keyPressed",
span: { start: 1977, end: 1996, line: 54 },
},
parameters: [],
span: { start: 1977, end: 1998, line: 54 },
},
rest: [],
},
span: { start: 1967, end: 1999, line: 54 },
},
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "moveSquare",
span: { start: 2015, end: 2025, line: 55 },
},
parameters: [],
span: { start: 2015, end: 2027, line: 55 },
},
},
],
},
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "key",
span: { start: 2053, end: 2056, line: 57 },
},
rest: [
{
op: "=",
term: { termType: "numericLiteral", value: 81 },
},
],
},
body: [
{
statementType: "letStatement",
name: {
value: "exit",
span: { start: 2070, end: 2074, line: 57 },
},
value: {
nodeType: "expression",
term: { termType: "keywordLiteral", value: "true" },
rest: [],
},
span: { start: 2066, end: 2082, line: 57 },
},
],
else: [],
},
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "key",
span: { start: 2111, end: 2114, line: 58 },
},
rest: [
{
op: "=",
term: { termType: "numericLiteral", value: 90 },
},
],
},
body: [
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "square.decSize",
span: { start: 2127, end: 2141, line: 58 },
},
parameters: [],
span: { start: 2127, end: 2143, line: 58 },
},
},
],
else: [],
},
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "key",
span: { start: 2169, end: 2172, line: 59 },
},
rest: [
{
op: "=",
term: { termType: "numericLiteral", value: 88 },
},
],
},
body: [
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "square.incSize",
span: { start: 2185, end: 2199, line: 59 },
},
parameters: [],
span: { start: 2185, end: 2201, line: 59 },
},
},
],
else: [],
},
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "key",
span: { start: 2227, end: 2230, line: 60 },
},
rest: [
{
op: "=",
term: { termType: "numericLiteral", value: 131 },
},
],
},
body: [
{
statementType: "letStatement",
name: {
value: "direction",
span: { start: 2244, end: 2253, line: 60 },
},
value: {
nodeType: "expression",
term: { termType: "numericLiteral", value: 1 },
rest: [],
},
span: { start: 2240, end: 2258, line: 60 },
},
],
else: [],
},
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "key",
span: { start: 2288, end: 2291, line: 61 },
},
rest: [
{
op: "=",
term: { termType: "numericLiteral", value: 133 },
},
],
},
body: [
{
statementType: "letStatement",
name: {
value: "direction",
span: { start: 2305, end: 2314, line: 61 },
},
value: {
nodeType: "expression",
term: { termType: "numericLiteral", value: 2 },
rest: [],
},
span: { start: 2301, end: 2319, line: 61 },
},
],
else: [],
},
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "key",
span: { start: 2351, end: 2354, line: 62 },
},
rest: [
{
op: "=",
term: { termType: "numericLiteral", value: 130 },
},
],
},
body: [
{
statementType: "letStatement",
name: {
value: "direction",
span: { start: 2368, end: 2377, line: 62 },
},
value: {
nodeType: "expression",
term: { termType: "numericLiteral", value: 3 },
rest: [],
},
span: { start: 2364, end: 2382, line: 62 },
},
],
else: [],
},
{
statementType: "ifStatement",
condition: {
nodeType: "expression",
term: {
termType: "variable",
name: "key",
span: { start: 2414, end: 2417, line: 63 },
},
rest: [
{
op: "=",
term: { termType: "numericLiteral", value: 132 },
},
],
},
body: [
{
statementType: "letStatement",
name: {
value: "direction",
span: { start: 2431, end: 2440, line: 63 },
},
value: {
nodeType: "expression",
term: { termType: "numericLiteral", value: 4 },
rest: [],
},
span: { start: 2427, end: 2445, line: 63 },
},
],
else: [],
},
{
statementType: "whileStatement",
condition: {
nodeType: "expression",
term: {
termType: "unaryExpression",
op: "~",
term: {
termType: "groupedExpression",
expression: {
nodeType: "expression",
term: {
termType: "variable",
name: "key",
span: { start: 2529, end: 2532, line: 66 },
},
rest: [
{
op: "=",
term: { termType: "numericLiteral", value: 0 },
},
],
},
},
},
rest: [],
},
body: [
{
statementType: "letStatement",
name: {
value: "key",
span: { start: 2557, end: 2560, line: 67 },
},
value: {
nodeType: "expression",
term: {
termType: "subroutineCall",
name: {
value: "Keyboard.keyPressed",
span: { start: 2563, end: 2582, line: 67 },
},
parameters: [],
span: { start: 2563, end: 2584, line: 67 },
},
rest: [],
},
span: { start: 2553, end: 2585, line: 67 },
},
{
statementType: "doStatement",
call: {
termType: "subroutineCall",
name: {
value: "moveSquare",
span: { start: 2601, end: 2611, line: 68 },
},
parameters: [],
span: { start: 2601, end: 2613, line: 68 },
},
},
],
},
],
},
{
statementType: "returnStatement",
span: { start: 2647, end: 2654, line: 71 },
},
],
},
},
],
};
export const compiled = `function SquareGame.new 0
push constant 2
call Memory.alloc 1
pop pointer 0
push constant 0
push constant 0
push constant 30
call Square.new 3
pop this 0
push constant 0
pop this 1
push pointer 0
return
function SquareGame.dispose 0
push argument 0
pop pointer 0
push this 0
call Square.dispose 1
pop temp 0
push pointer 0
call Memory.deAlloc 1
pop temp 0
push constant 0
return
function SquareGame.moveSquare 0
push argument 0
pop pointer 0
push this 1
push constant 1
eq
not
if-goto SquareGame_1
push this 0
call Square.moveUp 1
pop temp 0
goto SquareGame_0
label SquareGame_1
label SquareGame_0
push this 1
push constant 2
eq
not
if-goto SquareGame_3
push this 0
call Square.moveDown 1
pop temp 0
goto SquareGame_2
label SquareGame_3
label SquareGame_2
push this 1
push constant 3
eq
not
if-goto SquareGame_5
push this 0
call Square.moveLeft 1
pop temp 0
goto SquareGame_4
label SquareGame_5
label SquareGame_4
push this 1
push constant 4
eq
not
if-goto SquareGame_7
push this 0
call Square.moveRight 1
pop temp 0
goto SquareGame_6
label SquareGame_7
label SquareGame_6
push constant 5
call Sys.wait 1
pop temp 0
push constant 0
return
function SquareGame.run 2
push argument 0
pop pointer 0
push constant 0
pop local 1
label SquareGame_8
push local 1
not
not
if-goto SquareGame_9
label SquareGame_10
push local 0
push constant 0
eq
not
if-goto SquareGame_11
call Keyboard.keyPressed 0
pop local 0
push pointer 0
call SquareGame.moveSquare 1
pop temp 0
goto SquareGame_10
label SquareGame_11
push local 0
push constant 81
eq
not
if-goto SquareGame_13
push constant 1
neg
pop local 1
goto SquareGame_12
label SquareGame_13
label SquareGame_12
push local 0
push constant 90
eq
not
if-goto SquareGame_15
push this 0
call Square.decSize 1
pop temp 0
goto SquareGame_14
label SquareGame_15
label SquareGame_14
push local 0
push constant 88
eq
not
if-goto SquareGame_17
push this 0
call Square.incSize 1
pop temp 0
goto SquareGame_16
label SquareGame_17
label SquareGame_16
push local 0
push constant 131
eq
not
if-goto SquareGame_19
push constant 1
pop this 1
goto SquareGame_18
label SquareGame_19
label SquareGame_18
push local 0
push constant 133
eq
not
if-goto SquareGame_21
push constant 2
pop this 1
goto SquareGame_20
label SquareGame_21
label SquareGame_20
push local 0
push constant 130
eq
not
if-goto SquareGame_23
push constant 3
pop this 1
goto SquareGame_22
label SquareGame_23
label SquareGame_22
push local 0
push constant 132
eq
not
if-goto SquareGame_25
push constant 4
pop this 1
goto SquareGame_24
label SquareGame_25
label SquareGame_24
label SquareGame_26
push local 0
push constant 0
eq
not
not
if-goto SquareGame_27
call Keyboard.keyPressed 0
pop local 0
push pointer 0
call SquareGame.moveSquare 1
pop temp 0
goto SquareGame_26
label SquareGame_27
goto SquareGame_8
label SquareGame_9
push constant 0
return`;

Some files were not shown because too many files have changed in this diff Show More