summaryrefslogtreecommitdiff
path: root/projects/12
diff options
context:
space:
mode:
Diffstat (limited to 'projects/12')
-rw-r--r--projects/12/Array.jack3
-rw-r--r--projects/12/Keyboard.jack44
-rw-r--r--projects/12/Math.jack19
-rw-r--r--projects/12/Memory.jack32
-rw-r--r--projects/12/Output.jack73
-rw-r--r--projects/12/Screen.jack22
-rw-r--r--projects/12/String.jack49
-rw-r--r--projects/12/Sys.jack19
8 files changed, 125 insertions, 136 deletions
diff --git a/projects/12/Array.jack b/projects/12/Array.jack
index c712b69..343c25c 100644
--- a/projects/12/Array.jack
+++ b/projects/12/Array.jack
@@ -20,6 +20,7 @@ class Array {
/** Disposes this array. */
method void dispose() {
- return Memory.deAlloc(this);
+ do Memory.deAlloc(this);
+ return;
}
}
diff --git a/projects/12/Keyboard.jack b/projects/12/Keyboard.jack
index ceb3438..a35704a 100644
--- a/projects/12/Keyboard.jack
+++ b/projects/12/Keyboard.jack
@@ -46,13 +46,15 @@ class Keyboard {
var int key, key1;
let key = 0;
while (key = 0) {
- key = Memory.peek(24576);
+ let key = Memory.peek(24576);
}
- key1 = key;
+ let key1 = key;
while (key1 = key) {
- key1 = Memory.peek(24576);
+ let key1 = Memory.peek(24576);
+ }
+ if ((key > 31) & (key < 127) | (key = 128) | (key = 129)) {
+ do Output.printChar(key);
}
- Output.printChar(key);
return key;
}
@@ -64,8 +66,8 @@ class Keyboard {
function String readLine(String message) {
var int c;
var String s;
- let s = "";
- Output.printString(message);
+ let s = String.new(140);
+ do Output.printString(message);
let c = Keyboard.readChar();
while (~(c = 128)) {
if (c = 129) {
@@ -87,35 +89,9 @@ class Keyboard {
* entered text is detected). Also handles user backspaces.
*/
function int readInt(String message) {
- var int n, i;
- var char t;
var String s;
- let n = 0;
- let neg = false;
+ do Output.printString(message);
let s = Keyboard.readLine("");
- let i = 0;
- if (s.charAt(0) = 45) {
- let i = 1;
- let neg = true;
- }
- let t = s.charAt(i);
- if ((t < 48) | (t > 57)) {
- do Sys.error(3);
- Output.printString("Keyboard.readInt: the input data is not number!");
- }
- let done = false;
- while (~done) {
- if ((s.charAt(i) > 47) & (s.charAt(i) < 58)) {
- n = n * 10 + (t - 48);
- } else {
- done = true;
- }
- let i = i + 1;
- done = done | (i < s.length());
- }
- if (neg) {
- n = - n;
- }
- return n;
+ return s.intValue();
}
}
diff --git a/projects/12/Math.jack b/projects/12/Math.jack
index 61e09fc..01bce8f 100644
--- a/projects/12/Math.jack
+++ b/projects/12/Math.jack
@@ -28,6 +28,18 @@ class Math {
return ~(x & twoToThe[i] = 0);
}
+ function int sign(int x) {
+ if (x > 0) {
+ return 1;
+ } else {
+ if (x < 0) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+ }
+
/** Returns the absolute value of x. */
function int abs(int x) {
if (x > 0){
@@ -43,11 +55,12 @@ class Math {
* the Jack expressions x*y and multiply(x,y) return the same value.
*/
function int multiply(int x, int y) {
- var int z, res, shiftedX, i;
+ var int res, shiftedX, i;
let shiftedX = x;
let i = 0;
let res = 0;
- while ((twoToThe[i] > x) & (i < 16)) {
+ //while ((~(twoToThe[i] > y)) & (i < 16)) {
+ while (i < 16) {
if (Math.bit(y, i)){
let res = res + shiftedX;
}
@@ -117,7 +130,7 @@ class Math {
while (~(k < 0)) {
let z = y + twoToThe[k];
let w = z * z;
- if ((w < x) & (w > 0)) {
+ if ((~(w > x)) & (w > 0)) {
let y = z;
}
let k = k - 1;
diff --git a/projects/12/Memory.jack b/projects/12/Memory.jack
index b1e67ef..54e04aa 100644
--- a/projects/12/Memory.jack
+++ b/projects/12/Memory.jack
@@ -11,7 +11,7 @@
class Memory {
static Array memory;
static Array heap;
- static int first;
+ static int last;
/** Initializes the class. */
function void init() {
@@ -19,6 +19,8 @@ class Memory {
let heap = 2048;
let heap[0] = -1;
let heap[1] = 14334;
+ let last = 0;
+ return;
}
/** Returns the RAM value at the given address. */
@@ -29,6 +31,7 @@ class Memory {
/** 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
@@ -43,8 +46,9 @@ class Memory {
let current = heap[current];
}
if (current = -1) {
- String.println("Insufficient space for allocation!");
- Sys.error(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;
@@ -58,23 +62,11 @@ class Memory {
/** De-allocates the given object (cast as an array) by making
* it available for future allocations. */
function void deAlloc(Array o) {
- var int base;
- var int current;
- var int prev;
- let prev = 0;
- let current = heap[prev];
+ var int oBase;
let oBase = o - heap - 2;
- while ((current > -1) & (~(current = oBase))){
- let prev = current;
- let current = heap[current];
- }
- if (current = -1) {
- String.println("The impossible happened");
- Sys.error(1);
- } else {
- let heap[prev] = heap[current]
- let heap[prev + 1] = heap[prev] - prev - 2;
- return;
- }
+ let heap[oBase] = -1;
+ let heap[last] = oBase;
+ let last = oBase;
+ return;
}
}
diff --git a/projects/12/Output.jack b/projects/12/Output.jack
index a0cb284..61b24de 100644
--- a/projects/12/Output.jack
+++ b/projects/12/Output.jack
@@ -22,6 +22,7 @@ class Output {
/** Initializes the screen, and locates the cursor at the screen's top-left. */
function void init() {
+ do Output.initMap();
do Output.moveCursor(0, 0);
return;
}
@@ -74,7 +75,7 @@ class Output {
do Output.create(64,30,51,51,59,59,59,27,3,30,0,0); // @
do Output.create(63,30,51,51,24,12,12,0,12,12,0,0); // ?
- do Output.create(65,12,12,30,30,51,51,63,51,51,0,0); // A ** TO BE FILLED **
+ do Output.create(65,12,12,30,30,51,51,63,51,51,0,0); // A
do Output.create(66,31,51,51,51,31,51,51,51,31,0,0); // B
do Output.create(67,28,54,35,3,3,3,35,54,28,0,0); // C
do Output.create(68,15,27,51,51,51,51,51,27,15,0,0); // D
@@ -146,9 +147,9 @@ class Output {
// Creates the character map array of the given character index, using the given values.
function void create(int index, int a, int b, int c, int d, int e,
int f, int g, int h, int i, int j, int k) {
- var Array map;
+ var Array map;
- let map = Array.new(11);
+ let map = Array.new(11);
let charMaps[index] = map;
let map[0] = a;
@@ -180,8 +181,8 @@ class Output {
* and erases the character displayed there. */
function void moveCursor(int i, int j) {
var int x, y;
- let x = cursorI * 8;
- let y = cursorJ * 11;
+ let x = cursorJ * 8;
+ let y = cursorI * 11;
do Screen.setColor(false);
do Screen.drawRectangle(x, y, x + 7, y + 10);
let x = j * 8;
@@ -198,19 +199,32 @@ class Output {
function void printChar(char c) {
var int k, x, y, addr;
var Array cm;
- if (c = 8) { //backspace
+ if (c = 129) {
do Output.backSpace();
return;
- }
- if (c = 10) { //newline
+ }
+ if (cursorI = 22) {
+ if (c = 128) {
+ return;
+ }
+ if (cursorJ = 63) {
+ return;
+ }
+ }
+ if (c = 128) {
do Output.println();
return;
}
let k = 0;
- let x = cursorI / 2;
- let y = cursorI - (x * 2); // y = 0: lsb, y = 1: msb
- let addr = cursorJ * 352 + x + 16384;
+ let x = cursorJ / 2;
+ let y = cursorJ - (x * 2); // y = 0: lsb, y = 1: msb
+ let addr = cursorI * 352 + x + 16384;
let cm = Output.getMap(c);
+ if (cursorJ = 63) {
+ do Output.moveCursor(cursorI + 1, 0);
+ } else {
+ do Output.moveCursor(cursorI, cursorJ + 1);
+ }
while (k < 11) {
if (y = 0){
do Memory.poke(addr, Memory.peek(addr) & (-128) + cm[k]);
@@ -220,11 +234,6 @@ class Output {
let k = k + 1;
let addr = addr + 32;
}
- if (cursorJ = 63) {
- do Output.moveCursor(cursorI + 1, 0);
- } else {
- do Output.moveCursor(cursorI, cursorJ + 1);
- }
return;
}
@@ -245,43 +254,27 @@ class Output {
/** Displays the given integer starting at the cursor location,
* and advances the cursor appropriately. */
function void printInt(int i) {
- var int n, k;
var String s;
- if (i = 0) {
- do Output.printChar(48);
- return;
- }
- let n = i;
- if (i < 0) {
- do Output.printChar(45);
- let n = -n;
- }
- let s = "";
- while (n > 0) {
- let k = n / 10;
- do s.appendChar(48 + n - (k * 10));
- let n = k;
- }
- let k = s.length();
- while (k > 0) {
- do Output.printChar(s.charAt(k));
- let k = k - 1;
- }
+ let s = String.new(6);
+ do s.setInt(i);
+ do Output.printString(s);
return;
}
/** Advances the cursor to the beginning of the next line. */
function void println() {
- do Output.moveCursor(cursorI, 0);
+ if (cursorI < 22) {
+ do Output.moveCursor(cursorI + 1, 0);
+ }
return;
}
/** Moves the cursor one column back. */
function void backSpace() {
if (cursorJ > 0) {
- do Output.moveCursor(cursorI - 1, 63);
- } else {
do Output.moveCursor(cursorI, cursorJ - 1);
+ } else {
+ do Output.moveCursor(cursorI - 1, 63);
}
return;
}
diff --git a/projects/12/Screen.jack b/projects/12/Screen.jack
index 337a7e2..a370ab9 100644
--- a/projects/12/Screen.jack
+++ b/projects/12/Screen.jack
@@ -26,6 +26,7 @@ class Screen {
let i = i + 1;
}
let screen = 16384;
+ let color = true;
return;
}
@@ -34,7 +35,7 @@ class Screen {
var boolean c;
let c = color;
let color = false;
- do drawRectangle(0, 0, 511, 255);
+ do Screen.drawRectangle(0, 0, 511, 255);
let color = c;
return;
}
@@ -59,17 +60,17 @@ class Screen {
if (color) {
let screen[addr] = screen[addr] | twoToThe[x - (t * 16)];
} else {
- let screen[addr] = screen[addr] & (- (twoToThe[x - (t * 16)] + 1))
+ let screen[addr] = screen[addr] & (- (twoToThe[x - (t * 16)] + 1));
}
return;
}
/** Draws a line from pixel (x1,y1) to pixel (x2,y2), using the current color. */
function void drawLine(int x1, int y1, int x2, int y2) {
- var int dx, dy, x, y, diff;
+ var int dx, dy, x, y, a, b, diff;
let x = x1;
let y = y1;
- do drawPixel(x, y);
+ do Screen.drawPixel(x, y);
if (x1 = x2) {
if (y1 = y2) {
return;
@@ -97,19 +98,22 @@ class Screen {
let y = y + b;
let diff = diff - dx;
}
- do drawPixel(x, y);
+ do Screen.drawPixel(x, y);
}
+ return;
}
/** Draws a filled rectangle whose top left corner is (x1, y1)
* and bottom right corner is (x2,y2), using the current color. */
function void drawRectangle(int x1, int y1, int x2, int y2) {
+ var int x;
if ((x1 > x2) | (y1 > y2)) {
return;
}
let x = x1;
while (~(x > x2)) {
- do drawLine(x, y1, x, y2);
+ do Screen.drawLine(x, y1, x, y2);
+ let x = x + 1;
}
return;
}
@@ -118,15 +122,15 @@ class Screen {
function void drawCircle(int x, int y, int r) {
var int dx, dy, r2;
if (r > 181) {
- String.println("drawCircle: radius too big!");
- Sys.error(2);
+ do String.println("drawCircle: radius too big!");
+ do Sys.error(2);
return;
}
let dy = -r;
let r2 = r * r;
while (~(dy > r)) {
let dx = Math.sqrt(r2 - (dy * dy));
- do drawLine(x - dx, y + dy, x + dx, y + dy);
+ do Screen.drawLine(x - dx, y + dy, x + dx, y + dy);
let dy = dy + 1;
}
return;
diff --git a/projects/12/String.jack b/projects/12/String.jack
index 398fdfe..3ef40b2 100644
--- a/projects/12/String.jack
+++ b/projects/12/String.jack
@@ -18,7 +18,9 @@ class String {
/** constructs a new empty string with a maximum length of maxLength
* and initial length of 0. */
constructor String new(int maxLength) {
- let s = new Array(maxLength);
+ if (maxLength > 0) {
+ let s = Array.new(maxLength);
+ }
let maxLen = maxLength;
let len = 0;
return this;
@@ -26,7 +28,9 @@ class String {
/** Disposes this string. */
method void dispose() {
- do s.dispose();
+ if (maxLen > 0) {
+ do s.dispose();
+ }
do Memory.deAlloc(this);
return;
}
@@ -39,8 +43,8 @@ class String {
/** Returns the character at the j-th location of this string. */
method char charAt(int j) {
if ((j < 0) | (j + 1 > len)){
- Output.printString("String.charAt: index out of range!");
- Sys.error(5);
+ do Output.printString("String.charAt: index out of range!");
+ do Sys.error(5);
}
return s[j];
}
@@ -48,8 +52,8 @@ class String {
/** Sets the character at the j-th location of this string to c. */
method void setCharAt(int j, char c) {
if ((j < 0) | (j + 1 > len)){
- Output.printString("String.setCharAt: index out of range!");
- Sys.error(5);
+ do Output.printString("String.setCharAt: index out of range!");
+ do Sys.error(5);
}
let s[j] = c;
return;
@@ -58,8 +62,8 @@ class String {
/** Appends c to this string's end and returns this string. */
method String appendChar(char c) {
if (len = maxLen) {
- Output.printString("String.appendChar: reached max length!");
- Sys.error(5);
+ do Output.printString("String.appendChar: reached max length!");
+ do Sys.error(5);
}
let s[len] = c;
let len = len + 1;
@@ -69,8 +73,8 @@ class String {
/** Erases the last character from this string. */
method void eraseLastChar() {
if (len = 0){
- Output.printString("String.eraseLastChar: string is already empty!");
- Sys.error(5);
+ do Output.printString("String.eraseLastChar: string is already empty!");
+ do Sys.error(5);
}
let len = len - 1;
return;
@@ -91,15 +95,15 @@ class String {
let neg = false;
}
let c = s[i];
- if ((t < 48) | (t > 57)) {
+ if ((c < 48) | (c > 57)) {
do Sys.error(3);
do Output.printString("String.intValue: the input data is not number!");
}
let done = false;
while ((~done) & (i < len)) {
- let t = s[i];
- if ((t > 47) & (t < 58)) {
- let n = n * 10 + (t - 48);
+ let c = s[i];
+ if ((c > 47) & (c < 58)) {
+ let n = n * 10 + (c - 48);
} else {
let done = true;
}
@@ -115,14 +119,14 @@ class String {
/** Sets this string to hold a representation of the given value. */
method void setInt(int val) { //change Output.printInt after this
- var int l;
+ var int x, i, y;
var boolean neg;
if (val < 0) {
let neg = true;
- len = 2;
+ let len = 2;
} else {
let neg = false;
- len = 1;
+ let len = 1;
}
let x = Math.abs(val);
if (x > 9999) {
@@ -135,21 +139,22 @@ class String {
let len = len + 1;
}}}}
if (len > maxLen) {
- Output.printString("String.setInt: val is too big for the string!");
- Sys.error(5);
+ do Output.printString("String.setInt: val is too big for the string!");
+ do Sys.error(5);
}
if (x = 0) {
- setCharAt(0, 48);
+ do setCharAt(0, 48);
return;
}
if (neg) {
- setCharAt(0, 45);
+ do setCharAt(0, 45);
}
let i = len - 1;
while (x > 0) {
let y = x / 10;
- setCharAt(i, x - (y * 10) + 48);
+ do setCharAt(i, x - (y * 10) + 48);
let x = y;
+ let i = i - 1;
}
return;
}
diff --git a/projects/12/Sys.jack b/projects/12/Sys.jack
index d6b99f6..53c078c 100644
--- a/projects/12/Sys.jack
+++ b/projects/12/Sys.jack
@@ -10,14 +10,14 @@ class Sys {
/** Performs all the initializations required by the OS. */
function void init() {
- do Array.init();
do Keyboard.init();
do Math.init();
do Memory.init();
do Output.init();
do Screen.init();
- do String.init();
do Main.main();
+ do Sys.halt();
+ return;
}
/** Halts the program execution. */
@@ -29,18 +29,23 @@ class Sys {
/** Waits approximately duration milliseconds and returns. */
function void wait(int duration) {
- Output.printString("waiting")
+ var int i, j;
let i = 0;
- while (i < 50000){
+ while (i < duration){
let i = i + 1;
+ let j = 0;
+ while (j < 318){
+ let j = j + 1;
+ }
}
- Output.printString("done")
+ return;
}
/** Displays the given error code in the form "ERR<errorCode>",
* and halts the program's execution. */
function void error(int errorCode) {
- Output.printString("ERR");
- Output.printInt(errorCode);
+ do Output.printString("ERR");
+ do Output.printInt(errorCode);
+ return;
}
}