aboutsummaryrefslogtreecommitdiff
path: root/projects/12/Memory.jack
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2018-01-20 15:41:49 +0100
committerYuchen Pei <me@ypei.me>2018-01-20 15:41:49 +0100
commitd3a0cc3a8ba6dfeb64d3faeffdeb6845b60e5840 (patch)
treed58df9ec2480e2a9ec6240f9c797f83d1a0b1056 /projects/12/Memory.jack
parent3571f998b28fbc8d9250ba04c983935f10a16c15 (diff)
rearranged the dir for github
- removed tools and pdfs - rearranged the projects dirs - added md files - other minor changes
Diffstat (limited to 'projects/12/Memory.jack')
-rw-r--r--projects/12/Memory.jack72
1 files changed, 0 insertions, 72 deletions
diff --git a/projects/12/Memory.jack b/projects/12/Memory.jack
deleted file mode 100644
index 54e04aa..0000000
--- a/projects/12/Memory.jack
+++ /dev/null
@@ -1,72 +0,0 @@
-// 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/12/Memory.jack
-
-/**
- * This library provides two services: direct access to the computer's main
- * memory (RAM), and allocation and recycling of memory blocks. The Hack RAM
- * consists of 32,768 words, each holding a 16-bit binary number.
- */
-class Memory {
- static Array memory;
- static Array heap;
- static int last;
-
- /** Initializes the class. */
- function void init() {
- let memory = 0;
- let heap = 2048;
- let heap[0] = -1;
- let heap[1] = 14334;
- let last = 0;
- return;
- }
-
- /** Returns the RAM value at the given address. */
- function int peek(int address) {
- return memory[address];
- }
-
- /** Sets the RAM value at the given address to the given value. */
- function void poke(int address, int value) {
- let memory[address] = value;
- return;
- }
-
- /** Finds an available RAM block of the given size and returns
- * a reference to its base address. */
- function int alloc(int size) {
- var int current;
- var int size2;
- var int newLast;
- let current = 0;
- let size2 = size + 2;
- while ((current > -1) & (heap[current + 1] < size2)) {
- let current = heap[current];
- }
- if (current = -1) {
- do String.println("Insufficient space for allocation!");
- do Sys.error(1);
- return -1;
- } else {
- let heap[current + 1] = heap[current + 1] - size2;
- let newLast = current + heap[current + 1] + 2;
- let heap[newLast] = heap[current];
- let heap[current] = newLast;
- let heap[newLast + 1] = size;
- return heap + newLast + 2;
- }
- }
-
- /** De-allocates the given object (cast as an array) by making
- * it available for future allocations. */
- function void deAlloc(Array o) {
- var int oBase;
- let oBase = o - heap - 2;
- let heap[oBase] = -1;
- let heap[last] = oBase;
- let last = oBase;
- return;
- }
-}