// 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/Output.jack /** * A library of functions for writing text on the screen. * The Hack physical screen consists of 512 rows of 256 pixels each. * The library uses a fixed font, in which each character is displayed * within a frame which is 11 pixels high (including 1 pixel for inter-line * spacing) and 8 pixels wide (including 2 pixels for inter-character spacing). * The resulting grid accommodates 23 rows (indexed 0..22, top to bottom) * of 64 characters each (indexed 0..63, left to right). The top left * character position on the screen is indexed (0,0). A cursor, implemented * as a small filled square, indicates where the next character will be displayed. */ class Output { // Character map for displaying characters static Array charMaps, cursor; /** Initializes the screen, and locates the cursor at the screen's top-left. */ function void init() { let cursor = Array.new(2); //column: 0-63 let cursor[0] = 0; //line: 0-22 let cursor[1] = 0; do Output.initMap(); return; } // Initializes the character map array function void initMap() { var int i; let charMaps = Array.new(127); // Black square, used for displaying non-printable characters. do Output.create(0,63,63,63,63,63,63,63,63,63,0,0); // Assigns the bitmap for each character in the charachter set. // The first parameter is the character index, the next 11 numbers // are the values of each row in the frame that represents this character. do Output.create(32,0,0,0,0,0,0,0,0,0,0,0); // do Output.create(33,12,30,30,30,12,12,0,12,12,0,0); // ! do Output.create(34,54,54,20,0,0,0,0,0,0,0,0); // " do Output.create(35,0,18,18,63,18,18,63,18,18,0,0); // # do Output.create(36,12,30,51,3,30,48,51,30,12,12,0); // $ do Output.create(37,0,0,35,51,24,12,6,51,49,0,0); // % do Output.create(38,12,30,30,12,54,27,27,27,54,0,0); // & do Output.create(39,12,12,6,0,0,0,0,0,0,0,0); // ' do Output.create(40,24,12,6,6,6,6,6,12,24,0,0); // ( do Output.create(41,6,12,24,24,24,24,24,12,6,0,0); // ) do Output.create(42,0,0,0,51,30,63,30,51,0,0,0); // * do Output.create(43,0,0,0,12,12,63,12,12,0,0,0); // + do Output.create(44,0,0,0,0,0,0,0,12,12,6,0); // , do Output.create(45,0,0,0,0,0,63,0,0,0,0,0); // - do Output.create(46,0,0,0,0,0,0,0,12,12,0,0); // . do Output.create(47,0,0,32,48,24,12,6,3,1,0,0); // / do Output.create(48,12,30,51,51,51,51,51,30,12,0,0); // 0 do Output.create(49,12,14,15,12,12,12,12,12,63,0,0); // 1 do Output.create(50,30,51,48,24,12,6,3,51,63,0,0); // 2 do Output.create(51,30,51,48,48,28,48,48,51,30,0,0); // 3 do Output.create(52,16,24,28,26,25,63,24,24,60,0,0); // 4 do Output.create(53,63,3,3,31,48,48,48,51,30,0,0); // 5 do Output.create(54,28,6,3,3,31,51,51,51,30,0,0); // 6 do Output.create(55,63,49,48,48,24,12,12,12,12,0,0); // 7 do Output.create(56,30,51,51,51,30,51,51,51,30,0,0); // 8 do Output.create(57,30,51,51,51,62,48,48,24,14,0,0); // 9 do Output.create(58,0,0,12,12,0,0,12,12,0,0,0); // : do Output.create(59,0,0,12,12,0,0,12,12,6,0,0); // ; do Output.create(60,0,0,24,12,6,3,6,12,24,0,0); // < do Output.create(61,0,0,0,63,0,0,63,0,0,0,0); // = do Output.create(62,0,0,3,6,12,24,12,6,3,0,0); // > 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,34,51,51,63,51,51,51,51,0,0); // A ** TO BE FILLED ** 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 do Output.create(69,63,51,35,11,15,11,35,51,63,0,0); // E do Output.create(70,63,51,35,11,15,11,3,3,3,0,0); // F do Output.create(71,28,54,35,3,59,51,51,54,44,0,0); // G do Output.create(72,51,51,51,51,63,51,51,51,51,0,0); // H do Output.create(73,30,12,12,12,12,12,12,12,30,0,0); // I do Output.create(74,60,24,24,24,24,24,27,27,14,0,0); // J do Output.create(75,51,51,51,27,15,27,51,51,51,0,0); // K do Output.create(76,3,3,3,3,3,3,35,51,63,0,0); // L do Output.create(77,33,51,63,63,51,51,51,51,51,0,0); // M do Output.create(78,51,51,55,55,63,59,59,51,51,0,0); // N do Output.create(79,30,51,51,51,51,51,51,51,30,0,0); // O do Output.create(80,31,51,51,51,31,3,3,3,3,0,0); // P do Output.create(81,30,51,51,51,51,51,63,59,30,48,0);// Q do Output.create(82,31,51,51,51,31,27,51,51,51,0,0); // R do Output.create(83,30,51,51,6,28,48,51,51,30,0,0); // S do Output.create(84,63,63,45,12,12,12,12,12,30,0,0); // T do Output.create(85,51,51,51,51,51,51,51,51,30,0,0); // U do Output.create(86,51,51,51,51,51,30,30,12,12,0,0); // V do Output.create(87,51,51,51,51,51,63,63,63,18,0,0); // W do Output.create(88,51,51,30,30,12,30,30,51,51,0,0); // X do Output.create(89,51,51,51,51,30,12,12,12,30,0,0); // Y do Output.create(90,63,51,49,24,12,6,35,51,63,0,0); // Z do Output.create(91,30,6,6,6,6,6,6,6,30,0,0); // [ do Output.create(92,0,0,1,3,6,12,24,48,32,0,0); // \ do Output.create(93,30,24,24,24,24,24,24,24,30,0,0); // ] do Output.create(94,8,28,54,0,0,0,0,0,0,0,0); // ^ do Output.create(95,0,0,0,0,0,0,0,0,0,63,0); // _ do Output.create(96,6,12,24,0,0,0,0,0,0,0,0); // ` do Output.create(97,0,0,0,14,24,30,27,27,54,0,0); // a do Output.create(98,3,3,3,15,27,51,51,51,30,0,0); // b do Output.create(99,0,0,0,30,51,3,3,51,30,0,0); // c do Output.create(100,48,48,48,60,54,51,51,51,30,0,0); // d do Output.create(101,0,0,0,30,51,63,3,51,30,0,0); // e do Output.create(102,28,54,38,6,15,6,6,6,15,0,0); // f do Output.create(103,0,0,30,51,51,51,62,48,51,30,0); // g do Output.create(104,3,3,3,27,55,51,51,51,51,0,0); // h do Output.create(105,12,12,0,14,12,12,12,12,30,0,0); // i do Output.create(106,48,48,0,56,48,48,48,48,51,30,0); // j do Output.create(107,3,3,3,51,27,15,15,27,51,0,0); // k do Output.create(108,14,12,12,12,12,12,12,12,30,0,0); // l do Output.create(109,0,0,0,29,63,43,43,43,43,0,0); // m do Output.create(110,0,0,0,29,51,51,51,51,51,0,0); // n do Output.create(111,0,0,0,30,51,51,51,51,30,0,0); // o do Output.create(112,0,0,0,30,51,51,51,31,3,3,0); // p do Output.create(113,0,0,0,30,51,51,51,62,48,48,0); // q do Output.create(114,0,0,0,29,55,51,3,3,7,0,0); // r do Output.create(115,0,0,0,30,51,6,24,51,30,0,0); // s do Output.create(116,4,6,6,15,6,6,6,54,28,0,0); // t do Output.create(117,0,0,0,27,27,27,27,27,54,0,0); // u do Output.create(118,0,0,0,51,51,51,51,30,12,0,0); // v do Output.create(119,0,0,0,51,51,51,63,63,18,0,0); // w do Output.create(120,0,0,0,51,30,12,12,30,51,0,0); // x do Output.create(121,0,0,0,51,51,51,62,48,24,15,0); // y do Output.create(122,0,0,0,63,27,12,6,51,63,0,0); // z do Output.create(123,56,12,12,12,7,12,12,12,56,0,0); // { do Output.create(124,12,12,12,12,12,12,12,12,12,0,0); // | do Output.create(125,7,12,12,12,56,12,12,12,7,0,0); // } do Output.create(126,38,45,25,0,0,0,0,0,0,0,0); // ~ return; } // 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; let map = Array.new(11); let charMaps[index] = map; let map[0] = a; let map[1] = b; let map[2] = c; let map[3] = d; let map[4] = e; let map[5] = f; let map[6] = g; let map[7] = h; let map[8] = i; let map[9] = j; let map[10] = k; return; } // Returns the character map (array of size 11) of the given character. // If the given character is invalid or non-printable, returns the // character map of a black square. function Array getMap(char c) { if ((c < 32) | (c > 126)) { let c = 0; } return charMaps[c]; } /** Moves the cursor to the j-th column of the i-th row, * and erases the character displayed there. */ function void moveCursor(int i, int j) { var int adr, odd, val, v; //カーソルの位置の文字を消す(00000000にする) //1文字の大きさは 横 x 高 = 8 x 11 pix. //column(j): 0-63 let cursor[0] = j; //line(i): 0-22 let cursor[1] = i; let v = 0; while(v < 11) { //ターゲットの番地 16384 + (i * 32 * 11) + (j / 2) let adr = 16384 + (i * 32 * 11 + (v * 32)) + (j / 2); //2で割ったあまり let odd = j - (j / 2 * 2); let val = Memory.peek(adr); if(odd = 0) { //右8ビットをすべて0にする if(val < 0) { // -1: 11111111 11111111 => 11111111 00000000 = -256 //-256: 11111111 00000000 => 11111111 00000000 = -256 //-257: 11111110 11111111 => 11111110 00000000 = -512 let val = ((Math.abs(val) - 1) / 256 + 1) * 256; let val = -val; } else { let val = val / 256 * 256; } } else { //左8ビットをすべて0にする if(val < 0) { // -1: 11111111 11111111 => 00000000 11111111 = 255 // -2: 11111111 11111110 => 00000000 11111110 = 254 //-256: 11111111 00000000 => 00000000 00000000 = 0 //-257: 11111110 11111111 => 00000000 11111111 = 255 let val = 255 - ((Math.abs(val) - 1) - (((Math.abs(val) - 1) / 256) * 256)); } else { let val = val - (val / 256 * 256); } } do Memory.poke(adr, val); let v = v + 1; } return; } /** Displays the given character at the cursor location, * and advances the cursor one column forward. */ function void printChar(char c) { var int adr, odd, val, v, i, j; var Array charArr; //test //let c = 74; let charArr = charMaps[c]; //スクリーンのメモリマップ[16384-24575]. 512 x 256ドットの白黒スクリーン //0行目[16384~16415] //1行目[16416~16447] //n行目[16384 + (32 * n)~16384 + (32 * (n + 1)) - 1] //255行目[24544~24575] //上からr番目で左からc番目のピクセル[(r * 32) + (c / 16)] (p98) //intは16ビットのため横は2文字分 //cursorの位置を取得 //column(j): 0-63 let j = cursor[0]; //line(i): 0-22 let i = cursor[1]; let v = 0; while(v < 11) { let adr = 16384 + (i * 32 * 11 + (v * 32)) + (j / 2); //2で割ったあまり let odd = j - (j / 2 * 2); let val = Memory.peek(adr); if(odd = 0) { //右8ビットをcharArr[v]にする if(val < 0) { let val = ((Math.abs(val) - 1) / 256 + 1) * 256 - charArr[v]; let val = -val; } else { let val = val / 256 * 256 + charArr[v]; } } else { //左8ビットをcharArr[v]にする //文字の右の方は白いので、charArr[v] * 256してもオーバフローしない(マイナスは扱わなくてよい)はず if(val < 0) { let val = 255 - ((Math.abs(val) - 1) - (((Math.abs(val) - 1) / 256) * 256)) + (charArr[v] * 256); } else { let val = val - (val / 256 * 256) + (charArr[v] * 256); } } do Memory.poke(adr, val); let v = v + 1; } //カーソルを右に進める。右端を超えたら一行下がる //下端を超えたら左上に戻る let cursor[0] = cursor[0] + 1; if(cursor[0] > 63) { let cursor[0] = 0; let cursor[1] = cursor[1] + 1; if(cursor[1] > 22) { let cursor[1] = 0; } } return; } /** displays the given string starting at the cursor location, * and advances the cursor appropriately. */ function void printString(String s) { var int lng, i; var char c; let lng = s.length(); let i = 0; while(i < lng) { let c = s.charAt(i); do Output.printChar(c); 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 String s; //最大でも6桁なので... let s = String.new(6); do s.setInt(i); do Output.printString(s); do s.dispose(); return; } /** Advances the cursor to the beginning of the next line. */ function void println() { let cursor[0] = 0; //カーソルを下に進める。下端を超えたら、左上に戻る let cursor[1] = cursor[1] + 1; if(cursor[1] > 22) { let cursor[1] = 0; } return; } /** Moves the cursor one column back. */ function void backSpace() { //カーソルを左に進める。左端を超えたときは上の行の右端に行き、上の行がないときは左下に行く let cursor[0] = cursor[0] - 1; if(cursor[0] < 0) { let cursor[0] = 63; let cursor[1] = cursor[1] - 1; if(cursor[1] < 0) { let cursor[1] = 22; } } do Output.moveCursor(cursor[1], cursor[0]); return; } }