+/TECS
git clone https://git.julia.blue/+/TECS
b17cc4b
master
1/** 2 * A library of functions for writing text on the screen. 3 * The Hack physical screen consists of 512 rows of 256 pixels each. 4 * The library uses a fixed font, in which each character is displayed 5 * within a frame which is 11 pixels high (including 1 pixel for inter-line 6 * spacing) and 8 pixels wide (including 2 pixels for inter-character spacing). 7 * The resulting grid accommodates 23 rows (indexed 0..22, top to bottom) 8 * of 64 characters each (indexed 0..63, left to right). The top left 9 * character position on the screen is indexed (0,0). A cursor, implemented 10 * as a small filled square, indicates where the next character will be displayed. 11 */ 12class Output { 13 14 // Character map for displaying characters 15 static Array charMaps; 16 static int x, y; 17 18 /** Initializes the screen, and locates the cursor at the screen's top-left. */ 19 function void init() { 20 do Output.initMap(); 21 let x = 0; 22 let y = 0; 23 return; 24 } 25 26 // Initializes the character map array 27 function void initMap() { 28 var int i; 29 30 let charMaps = Array.new(127); 31 32 // Black square, used for displaying non-printable characters. 33 do Output.create(0,63,63,63,63,63,63,63,63,63,0,0); 34 35 // Assigns the bitmap for each character in the charachter set. 36 // The first parameter is the character index, the next 11 numbers 37 // are the values of each row in the frame that represents this character. 38 do Output.create(32,0,0,0,0,0,0,0,0,0,0,0); // 39 do Output.create(33,12,30,30,30,12,12,0,12,12,0,0); // ! 40 do Output.create(34,54,54,20,0,0,0,0,0,0,0,0); // " 41 do Output.create(35,0,18,18,63,18,18,63,18,18,0,0); // # 42 do Output.create(36,12,30,51,3,30,48,51,30,12,12,0); // $ 43 do Output.create(37,0,0,35,51,24,12,6,51,49,0,0); // % 44 do Output.create(38,12,30,30,12,54,27,27,27,54,0,0); // & 45 do Output.create(39,12,12,6,0,0,0,0,0,0,0,0); // ' 46 do Output.create(40,24,12,6,6,6,6,6,12,24,0,0); // ( 47 do Output.create(41,6,12,24,24,24,24,24,12,6,0,0); // ) 48 do Output.create(42,0,0,0,51,30,63,30,51,0,0,0); // * 49 do Output.create(43,0,0,0,12,12,63,12,12,0,0,0); // + 50 do Output.create(44,0,0,0,0,0,0,0,12,12,6,0); // , 51 do Output.create(45,0,0,0,0,0,63,0,0,0,0,0); // - 52 do Output.create(46,0,0,0,0,0,0,0,12,12,0,0); // . 53 do Output.create(47,0,0,32,48,24,12,6,3,1,0,0); // / 54 55 do Output.create(48,12,30,51,51,51,51,51,30,12,0,0); // 0 56 do Output.create(49,12,14,15,12,12,12,12,12,63,0,0); // 1 57 do Output.create(50,30,51,48,24,12,6,3,51,63,0,0); // 2 58 do Output.create(51,30,51,48,48,28,48,48,51,30,0,0); // 3 59 do Output.create(52,16,24,28,26,25,63,24,24,60,0,0); // 4 60 do Output.create(53,63,3,3,31,48,48,48,51,30,0,0); // 5 61 do Output.create(54,28,6,3,3,31,51,51,51,30,0,0); // 6 62 do Output.create(55,63,49,48,48,24,12,12,12,12,0,0); // 7 63 do Output.create(56,30,51,51,51,30,51,51,51,30,0,0); // 8 64 do Output.create(57,30,51,51,51,62,48,48,24,14,0,0); // 9 65 66 do Output.create(58,0,0,12,12,0,0,12,12,0,0,0); // : 67 do Output.create(59,0,0,12,12,0,0,12,12,6,0,0); // ; 68 do Output.create(60,0,0,24,12,6,3,6,12,24,0,0); // < 69 do Output.create(61,0,0,0,63,0,0,63,0,0,0,0); // = 70 do Output.create(62,0,0,3,6,12,24,12,6,3,0,0); // > 71 do Output.create(64,30,51,51,59,59,59,27,3,30,0,0); // @ 72 do Output.create(63,30,51,51,24,12,12,0,12,12,0,0); // ? 73 74 do Output.create(65,12,30,51,51,63,51,51,51,51,0,0); // A 75 do Output.create(66,31,51,51,51,31,51,51,51,31,0,0); // B 76 do Output.create(67,28,54,35,3,3,3,35,54,28,0,0); // C 77 do Output.create(68,15,27,51,51,51,51,51,27,15,0,0); // D 78 do Output.create(69,63,51,35,11,15,11,35,51,63,0,0); // E 79 do Output.create(70,63,51,35,11,15,11,3,3,3,0,0); // F 80 do Output.create(71,28,54,35,3,59,51,51,54,44,0,0); // G 81 do Output.create(72,51,51,51,51,63,51,51,51,51,0,0); // H 82 do Output.create(73,30,12,12,12,12,12,12,12,30,0,0); // I 83 do Output.create(74,60,24,24,24,24,24,27,27,14,0,0); // J 84 do Output.create(75,51,51,51,27,15,27,51,51,51,0,0); // K 85 do Output.create(76,3,3,3,3,3,3,35,51,63,0,0); // L 86 do Output.create(77,33,51,63,63,51,51,51,51,51,0,0); // M 87 do Output.create(78,51,51,55,55,63,59,59,51,51,0,0); // N 88 do Output.create(79,30,51,51,51,51,51,51,51,30,0,0); // O 89 do Output.create(80,31,51,51,51,31,3,3,3,3,0,0); // P 90 do Output.create(81,30,51,51,51,51,51,63,59,30,48,0);// Q 91 do Output.create(82,31,51,51,51,31,27,51,51,51,0,0); // R 92 do Output.create(83,30,51,51,6,28,48,51,51,30,0,0); // S 93 do Output.create(84,63,63,45,12,12,12,12,12,30,0,0); // T 94 do Output.create(85,51,51,51,51,51,51,51,51,30,0,0); // U 95 do Output.create(86,51,51,51,51,51,30,30,12,12,0,0); // V 96 do Output.create(87,51,51,51,51,51,63,63,63,18,0,0); // W 97 do Output.create(88,51,51,30,30,12,30,30,51,51,0,0); // X 98 do Output.create(89,51,51,51,51,30,12,12,12,30,0,0); // Y 99 do Output.create(90,63,51,49,24,12,6,35,51,63,0,0); // Z 100 101 do Output.create(91,30,6,6,6,6,6,6,6,30,0,0); // [ 102 do Output.create(92,0,0,1,3,6,12,24,48,32,0,0); // \ 103 do Output.create(93,30,24,24,24,24,24,24,24,30,0,0); // ] 104 do Output.create(94,8,28,54,0,0,0,0,0,0,0,0); // ^ 105 do Output.create(95,0,0,0,0,0,0,0,0,0,63,0); // _ 106 do Output.create(96,6,12,24,0,0,0,0,0,0,0,0); // ` 107 108 do Output.create(97,0,0,0,14,24,30,27,27,54,0,0); // a 109 do Output.create(98,3,3,3,15,27,51,51,51,30,0,0); // b 110 do Output.create(99,0,0,0,30,51,3,3,51,30,0,0); // c 111 do Output.create(100,48,48,48,60,54,51,51,51,30,0,0); // d 112 do Output.create(101,0,0,0,30,51,63,3,51,30,0,0); // e 113 do Output.create(102,28,54,38,6,15,6,6,6,15,0,0); // f 114 do Output.create(103,0,0,30,51,51,51,62,48,51,30,0); // g 115 do Output.create(104,3,3,3,27,55,51,51,51,51,0,0); // h 116 do Output.create(105,12,12,0,14,12,12,12,12,30,0,0); // i 117 do Output.create(106,48,48,0,56,48,48,48,48,51,30,0); // j 118 do Output.create(107,3,3,3,51,27,15,15,27,51,0,0); // k 119 do Output.create(108,14,12,12,12,12,12,12,12,30,0,0); // l 120 do Output.create(109,0,0,0,29,63,43,43,43,43,0,0); // m 121 do Output.create(110,0,0,0,29,51,51,51,51,51,0,0); // n 122 do Output.create(111,0,0,0,30,51,51,51,51,30,0,0); // o 123 do Output.create(112,0,0,0,30,51,51,51,31,3,3,0); // p 124 do Output.create(113,0,0,0,30,51,51,51,62,48,48,0); // q 125 do Output.create(114,0,0,0,29,55,51,3,3,7,0,0); // r 126 do Output.create(115,0,0,0,30,51,6,24,51,30,0,0); // s 127 do Output.create(116,4,6,6,15,6,6,6,54,28,0,0); // t 128 do Output.create(117,0,0,0,27,27,27,27,27,54,0,0); // u 129 do Output.create(118,0,0,0,51,51,51,51,30,12,0,0); // v 130 do Output.create(119,0,0,0,51,51,51,63,63,18,0,0); // w 131 do Output.create(120,0,0,0,51,30,12,12,30,51,0,0); // x 132 do Output.create(121,0,0,0,51,51,51,62,48,24,15,0); // y 133 do Output.create(122,0,0,0,63,27,12,6,51,63,0,0); // z 134 135 do Output.create(123,56,12,12,12,7,12,12,12,56,0,0); // { 136 do Output.create(124,12,12,12,12,12,12,12,12,12,0,0); // | 137 do Output.create(125,7,12,12,12,56,12,12,12,7,0,0); // } 138 do Output.create(126,38,45,25,0,0,0,0,0,0,0,0); // ~ 139 140 return; 141 } 142 143 // Creates the character map array of the given character index, using the given values. 144 function void create(int index, int a, int b, int c, int d, int e, 145 int f, int g, int h, int i, int j, int k) { 146 var Array map; 147 148 let map = Array.new(11); 149 let charMaps[index] = map; 150 151 let map[0] = a; 152 let map[1] = b; 153 let map[2] = c; 154 let map[3] = d; 155 let map[4] = e; 156 let map[5] = f; 157 let map[6] = g; 158 let map[7] = h; 159 let map[8] = i; 160 let map[9] = j; 161 let map[10] = k; 162 163 return; 164 } 165 166 // Returns the character map (array of size 11) of the given character. 167 // If the given character is invalid or non-printable, returns the 168 // character map of a black square. 169 function Array getMap(char c) { 170 if ((c < 32) | (c > 126)) { 171 let c = 0; 172 } 173 return charMaps[c]; 174 } 175 176 /** Moves the cursor to the j-th column of the i-th row, 177 * and erases the character displayed there. */ 178 function void moveCursor(int i, int j) { 179 if((j < 0) | (j > 63) | (i < 0) | (i > 22)){ 180 do Sys.error(20); 181 } 182 let x = j; 183 let y = i; 184 do Output.printChar(32); //space 185 do Output.move(x - 1, y); 186 return; 187 } 188 189 /** Moves the cursor, but doesn't erase, and 190 * applies wrapping instead of throwing an 191 * error for out of bounds coordinates. */ 192 function void move(int i, int j) { 193 if(i < 0){ 194 let i = 63; 195 let j = j - 1; 196 } 197 if(i > 63){ 198 let i = 0; 199 let j = j + 1; 200 } 201 if(j < 0){ 202 let j = 22; 203 } 204 if(j > 22){ 205 let j = 0; 206 } 207 let x = i; 208 let y = j; 209 return; 210 } 211 212 /** Displays the given character at the cursor location, 213 * and advances the cursor one column forward. */ 214 function void printChar(char c) { 215 var Array temp, mem; 216 var int i, j, pos; 217 var boolean left; 218 if(c == 128){ 219 do Output.println(); 220 return; 221 } 222 if(c == 129){ 223 do Output.backSpace(); 224 return; 225 } 226 let temp = Output.getMap(c); 227 let left = true; 228 let i = x / 2; 229 if((i + i) == x){ // only needs <<8 if it's the left half of the word 230 let left = false; 231 } 232 let mem = 0; 233 let pos = 16384 + ((y * 11 * 32) + 64) + (x / 2); // Translate cursor pos to memory address 234 let i = 0; 235 while(i < 11){ 236 let c = temp[i]; 237 if(left){ 238 let j = 0; 239 while(j < 8){ 240 let c = c + c; 241 let j = j + 1; 242 } 243 let mem[pos] = (mem[pos] & 255) | c; 244 } 245 else{ 246 let mem[pos] = (mem[pos] & ~255) | c; 247 } 248 let pos = pos + 32; 249 let i = i + 1; 250 } 251 do Output.move(x + 1, y); 252 return; 253 } 254 255 /** displays the given string starting at the cursor location, 256 * and advances the cursor appropriately. */ 257 function void printString(String s) { 258 var int i, len; 259 let len = s.length(); 260 let i = 0; 261 while(i < len){ 262 do Output.printChar(s.charAt(i)); 263 let i = i + 1; 264 } 265 return; 266 } 267 268 /** Displays the given integer starting at the cursor location, 269 * and advances the cursor appropriately. */ 270 function void printInt(int i) { 271 var String temp; 272 let temp = String.new(6); 273 do temp.setInt(i); 274 do Output.printString(temp); 275 do temp.dispose(); 276 return; 277 } 278 279 /** Advances the cursor to the beginning of the next line. */ 280 function void println() { 281 do Output.move(0, y + 1); 282 return; 283 } 284 285 /** Moves the cursor one column back. */ 286 function void backSpace() { 287 do Output.move(x - 1, y); 288 do Output.printChar(32); //space 289 do Output.move(x - 1, y); 290 return; 291 } 292}