summaryrefslogtreecommitdiff
path: root/projects/12/MemoryTest/Main.jack
blob: a9817f402eb7fc4c9582275406310a96991f07f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 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/MemoryTest/Main.jack

/** Test program for the OS Memory class. */
class Main {

    /** Performs various memory manipulations. */
    function void main() {
        var int temp, err;
        var Array a, b, c;
        
        do Memory.poke(8000, 333);       // RAM[8000] = 333
        let temp = Memory.peek(8000);
        do Memory.poke(8001, temp + 1);  // RAM[8001] = 334
        
        let a = Array.new(3);            // uses Memory.alloc
        let a[2] = 222;
        do Memory.poke(8002, a[2]);      // RAM[8002] = 222
        
		let err = 0;
        let b = Array.new(3);
        let b[1] = a[2] - 100;
		if (b = a) {					  // Fail compare if b = a
			let err = 1; }
        do Memory.poke(8003, b[1] + err); // RAM[8003] = 122
        
		let err = 0;
        let c = Array.new(500);
        let c[499] = a[2] - b[1];
		if (c = a) {					  // Fail compare if c = a
			let err = 1; }
		if (c = b) {					  // Fail compare if c = b
			let err = err + 10; }
        do Memory.poke(8004, c[499]+err); // RAM[8004] = 100
        
        do a.dispose();                   // uses Memory.deAlloc
        do b.dispose();
        
		let err = 0;
        let b = Array.new(3);
        let b[0] = c[499] - 90;
		if (b = c) {					  // Fail compare if b = c
			let err = 1; }
        do Memory.poke(8005, b[0] + err); // RAM[8005] = 10
        
        do c.dispose();
        do b.dispose();
        
        return;
    }
}