From ee04ea4cf4e679b53180bcbbef8705078457af33 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Wed, 17 Jan 2018 14:49:06 +0100 Subject: finished Output --- projects/12/Output.jack | 58 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 5 deletions(-) (limited to 'projects/12/Output.jack') diff --git a/projects/12/Output.jack b/projects/12/Output.jack index 082734c..a0cb284 100644 --- a/projects/12/Output.jack +++ b/projects/12/Output.jack @@ -180,6 +180,10 @@ 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; + do Screen.setColor(false); + do Screen.drawRectangle(x, y, x + 7, y + 10); let x = j * 8; let y = i * 11; do Screen.setColor(true); @@ -194,15 +198,19 @@ class Output { function void printChar(char c) { var int k, x, y, addr; var Array cm; - if (c = 8) { - do Output.backspace(); + if (c = 8) { //backspace + do Output.backSpace(); + return; + } + if (c = 10) { //newline + do Output.println(); return; } let k = 0; let x = cursorI / 2; - let y = cursorI - (x * 2); // y = 0: lsb, y = 1: msb + let y = cursorI - (x * 2); // y = 0: lsb, y = 1: msb let addr = cursorJ * 352 + x + 16384; - let cm = getMap(c); + let cm = Output.getMap(c); while (k < 11) { if (y = 0){ do Memory.poke(addr, Memory.peek(addr) & (-128) + cm[k]); @@ -212,7 +220,7 @@ class Output { let k = k + 1; let addr = addr + 32; } - if (cursorJ = 31) { + if (cursorJ = 63) { do Output.moveCursor(cursorI + 1, 0); } else { do Output.moveCursor(cursorI, cursorJ + 1); @@ -223,18 +231,58 @@ class Output { /** displays the given string starting at the cursor location, * and advances the cursor appropriately. */ function void printString(String s) { + var int i; + var int n; + let i = 0; + let n = s.length(); + while (i < n) { + do Output.printChar(s.charAt(i)); + let i = i + 1; + } + return; } /** 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; + } + return; } /** Advances the cursor to the beginning of the next line. */ function void println() { + do Output.moveCursor(cursorI, 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); + } + return; } } -- cgit v1.2.3