Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.42 KB | None | 0 0
  1. // This file is part of www.nand2tetris.org
  2. // and the book "The Elements of Computing Systems"
  3. // by Nisan and Schocken, MIT Press.
  4. // File name: projects/12/Output.jack
  5.  
  6. /**
  7. * Handles writing characters to the screen.
  8. * The text screen (256 columns and 512 roes) is divided into 23 text rows (0..22),
  9. * each containing 64 text columns (0..63).
  10. * Each row is 11 pixels high (including 1 space pixel), and 8 pixels wide
  11. * (including 2 space pixels).
  12. */
  13. class Output {
  14.  
  15. // Character map for printing on the left of a screen word
  16. static Array charMaps;
  17. static int line, column;
  18.  
  19. /** Initializes the screen and locates the cursor at the screen's top-left. */
  20. function void init() {
  21. let line = 0;
  22. let column = 0;
  23. /* let line_y = 0; */
  24. /* let column_x = 0; */
  25. do Output.initMap();
  26. return;
  27. }
  28.  
  29. // Initalizes the character map array
  30. function void initMap() {
  31. var int i;
  32.  
  33. let charMaps = Array.new(127);
  34.  
  35. // black square (used for non printable characters)
  36. do Output.create(0,63,63,63,63,63,63,63,63,63,0,0);
  37.  
  38. // Assigns the bitmap for each character in the charachter set.
  39. do Output.create(32,0,0,0,0,0,0,0,0,0,0,0); //
  40. do Output.create(33,12,30,30,30,12,12,0,12,12,0,0); // !
  41. do Output.create(34,54,54,20,0,0,0,0,0,0,0,0); // "
  42. do Output.create(35,0,18,18,63,18,18,63,18,18,0,0); // #
  43. do Output.create(36,12,30,51,3,30,48,51,30,12,12,0); // $
  44. do Output.create(37,0,0,35,51,24,12,6,51,49,0,0); // %
  45. do Output.create(38,12,30,30,12,54,27,27,27,54,0,0); // &
  46. do Output.create(39,12,12,6,0,0,0,0,0,0,0,0); // '
  47. do Output.create(40,24,12,6,6,6,6,6,12,24,0,0); // (
  48. do Output.create(41,6,12,24,24,24,24,24,12,6,0,0); // )
  49. do Output.create(42,0,0,0,51,30,63,30,51,0,0,0); // *
  50. do Output.create(43,0,0,0,12,12,63,12,12,0,0,0); // +
  51. do Output.create(44,0,0,0,0,0,0,0,12,12,6,0); // ,
  52. do Output.create(45,0,0,0,0,0,63,0,0,0,0,0); // -
  53. do Output.create(46,0,0,0,0,0,0,0,12,12,0,0); // .
  54. do Output.create(47,0,0,32,48,24,12,6,3,1,0,0); // /
  55.  
  56. do Output.create(48,12,30,51,51,51,51,51,30,12,0,0); // 0
  57. do Output.create(49,12,14,15,12,12,12,12,12,63,0,0); // 1
  58. do Output.create(50,30,51,48,24,12,6,3,51,63,0,0); // 2
  59. do Output.create(51,30,51,48,48,28,48,48,51,30,0,0); // 3
  60. do Output.create(52,16,24,28,26,25,63,24,24,60,0,0); // 4
  61. do Output.create(53,63,3,3,31,48,48,48,51,30,0,0); // 5
  62. do Output.create(54,28,6,3,3,31,51,51,51,30,0,0); // 6
  63. do Output.create(55,63,49,48,48,24,12,12,12,12,0,0); // 7
  64. do Output.create(56,30,51,51,51,30,51,51,51,30,0,0); // 8
  65. do Output.create(57,30,51,51,51,62,48,48,24,14,0,0); // 9
  66.  
  67. do Output.create(58,0,0,12,12,0,0,12,12,0,0,0); // :
  68. do Output.create(59,0,0,12,12,0,0,12,12,6,0,0); // ;
  69. do Output.create(60,0,0,24,12,6,3,6,12,24,0,0); // <
  70. do Output.create(61,0,0,0,63,0,0,63,0,0,0,0); // =
  71. do Output.create(62,0,0,3,6,12,24,12,6,3,0,0); // >
  72. do Output.create(64,30,51,51,59,59,59,27,3,30,0,0); // @
  73. do Output.create(63,30,51,51,24,12,12,0,12,12,0,0); // ?
  74.  
  75. do Output.create(65,12,30,51,51,63,51,51,51,51,51,51); // A ** TO BE FILLED **
  76. do Output.create(66,31,51,51,51,31,51,51,51,31,0,0); // B
  77. do Output.create(67,28,54,35,3,3,3,35,54,28,0,0); // C
  78. do Output.create(68,15,27,51,51,51,51,51,27,15,0,0); // D
  79. do Output.create(69,63,51,35,11,15,11,35,51,63,0,0); // E
  80. do Output.create(70,63,51,35,11,15,11,3,3,3,0,0); // F
  81. do Output.create(71,28,54,35,3,59,51,51,54,44,0,0); // G
  82. do Output.create(72,51,51,51,51,63,51,51,51,51,0,0); // H
  83. do Output.create(73,30,12,12,12,12,12,12,12,30,0,0); // I
  84. do Output.create(74,60,24,24,24,24,24,27,27,14,0,0); // J
  85. do Output.create(75,51,51,51,27,15,27,51,51,51,0,0); // K
  86. do Output.create(76,3,3,3,3,3,3,35,51,63,0,0); // L
  87. do Output.create(77,33,51,63,63,51,51,51,51,51,0,0); // M
  88. do Output.create(78,51,51,55,55,63,59,59,51,51,0,0); // N
  89. do Output.create(79,30,51,51,51,51,51,51,51,30,0,0); // O
  90. do Output.create(80,31,51,51,51,31,3,3,3,3,0,0); // P
  91. do Output.create(81,30,51,51,51,51,51,63,59,30,48,0);// Q
  92. do Output.create(82,31,51,51,51,31,27,51,51,51,0,0); // R
  93. do Output.create(83,30,51,51,6,28,48,51,51,30,0,0); // S
  94. do Output.create(84,63,63,45,12,12,12,12,12,30,0,0); // T
  95. do Output.create(85,51,51,51,51,51,51,51,51,30,0,0); // U
  96. do Output.create(86,51,51,51,51,51,30,30,12,12,0,0); // V
  97. do Output.create(87,51,51,51,51,51,63,63,63,18,0,0); // W
  98. do Output.create(88,51,51,30,30,12,30,30,51,51,0,0); // X
  99. do Output.create(89,51,51,51,51,30,12,12,12,30,0,0); // Y
  100. do Output.create(90,63,51,49,24,12,6,35,51,63,0,0); // Z
  101.  
  102. do Output.create(91,30,6,6,6,6,6,6,6,30,0,0); // [
  103. do Output.create(92,0,0,1,3,6,12,24,48,32,0,0); // \
  104. do Output.create(93,30,24,24,24,24,24,24,24,30,0,0); // ]
  105. do Output.create(94,8,28,54,0,0,0,0,0,0,0,0); // ^
  106. do Output.create(95,0,0,0,0,0,0,0,0,0,63,0); // _
  107. do Output.create(96,6,12,24,0,0,0,0,0,0,0,0); // `
  108.  
  109. do Output.create(97,0,0,0,14,24,30,27,27,54,0,0); // a
  110. do Output.create(98,3,3,3,15,27,51,51,51,30,0,0); // b
  111. do Output.create(99,0,0,0,30,51,3,3,51,30,0,0); // c
  112. do Output.create(100,48,48,48,60,54,51,51,51,30,0,0); // d
  113. do Output.create(101,0,0,0,30,51,63,3,51,30,0,0); // e
  114. do Output.create(102,28,54,38,6,15,6,6,6,15,0,0); // f
  115. do Output.create(103,0,0,30,51,51,51,62,48,51,30,0); // g
  116. do Output.create(104,3,3,3,27,55,51,51,51,51,0,0); // h
  117. do Output.create(105,12,12,0,14,12,12,12,12,30,0,0); // i
  118. do Output.create(106,48,48,0,56,48,48,48,48,51,30,0); // j
  119. do Output.create(107,3,3,3,51,27,15,15,27,51,0,0); // k
  120. do Output.create(108,14,12,12,12,12,12,12,12,30,0,0); // l
  121. do Output.create(109,0,0,0,29,63,43,43,43,43,0,0); // m
  122. do Output.create(110,0,0,0,29,51,51,51,51,51,0,0); // n
  123. do Output.create(111,0,0,0,30,51,51,51,51,30,0,0); // o
  124. do Output.create(112,0,0,0,30,51,51,51,31,3,3,0); // p
  125. do Output.create(113,0,0,0,30,51,51,51,62,48,48,0); // q
  126. do Output.create(114,0,0,0,29,55,51,3,3,7,0,0); // r
  127. do Output.create(115,0,0,0,30,51,6,24,51,30,0,0); // s
  128. do Output.create(116,4,6,6,15,6,6,6,54,28,0,0); // t
  129. do Output.create(117,0,0,0,27,27,27,27,27,54,0,0); // u
  130. do Output.create(118,0,0,0,51,51,51,51,30,12,0,0); // v
  131. do Output.create(119,0,0,0,51,51,51,63,63,18,0,0); // w
  132. do Output.create(120,0,0,0,51,30,12,12,30,51,0,0); // x
  133. do Output.create(121,0,0,0,51,51,51,62,48,24,15,0); // y
  134. do Output.create(122,0,0,0,63,27,12,6,51,63,0,0); // z
  135.  
  136. do Output.create(123,56,12,12,12,7,12,12,12,56,0,0); // {
  137. do Output.create(124,12,12,12,12,12,12,12,12,12,0,0); // |
  138. do Output.create(125,7,12,12,12,56,12,12,12,7,0,0); // }
  139. do Output.create(126,38,45,25,0,0,0,0,0,0,0,0); // ~
  140.  
  141. return;
  142. }
  143.  
  144. // Creates a character map array of the given char index with the given values.
  145. function void create(int index, int a, int b, int c, int d, int e,
  146. int f, int g, int h, int i, int j, int k) {
  147. var Array map;
  148.  
  149. let map = Array.new(11);
  150. let charMaps[index] = map;
  151.  
  152. let map[0] = a;
  153. let map[1] = b;
  154. let map[2] = c;
  155. let map[3] = d;
  156. let map[4] = e;
  157. let map[5] = f;
  158. let map[6] = g;
  159. let map[7] = h;
  160. let map[8] = i;
  161. let map[9] = j;
  162. let map[10] = k;
  163.  
  164. return;
  165. }
  166.  
  167. // Returns the character map (array of size 11) for the given character
  168. // If an invalid character is given, returns the character map of a black square.
  169. function Array getMap(char c) {
  170.  
  171. if ((c < 32) | (c > 126)) {
  172. let c = 0;
  173. }
  174.  
  175. return charMaps[c];
  176. }
  177.  
  178. /** Moves the cursor to the j’th column of the i’th row,
  179. * and erases the character that was there. */
  180. function void moveCursor(int i, int j) {
  181. /* let line_y = i; */
  182. /* let column_x = j; */
  183. let line = i * 11;
  184. let column = j * 8;
  185. do Output.printChar(32);
  186. do Output.backSpace();
  187. return;
  188. }
  189.  
  190. function Array int_to_binary(int n){
  191. var int i, j;
  192. var Array binary;
  193. let binary = Array.new(8);
  194. let i = 0;
  195. let j = 0;
  196.  
  197. while(j < 8){
  198. let binary[j] = 0;
  199. let j = j + 1;
  200. }
  201.  
  202. if(~(n = 0)){
  203. while(i < 8){
  204. let binary[i] = Output.get_mod(n);
  205. let n = n / 2;
  206. let i = i + 1;
  207. if (n = 0){
  208. return binary;
  209. }
  210. }
  211. }
  212. return binary;
  213. }
  214.  
  215. function int get_mod(int n){
  216. var int quotient, mod;
  217. let quotient = n / 2;
  218. let mod = n - (quotient * 2);
  219. return mod;
  220. }
  221.  
  222. /** Prints c at the cursor location and advances the cursor one
  223. * column forward. */
  224. function void printChar(char c) {
  225. var Array binary, charMap;
  226. var int x, y, i, j, pre_column, pre_line;
  227.  
  228. let pre_column = column;
  229. let pre_line = line;
  230. let charMap = Output.getMap(c);
  231. let i = 0;
  232.  
  233. /* do Output.printChar(32); */
  234. /* do Output.backSpace(); */
  235.  
  236. while (i < 11){
  237. let j = 0;
  238. let binary = Output.int_to_binary(charMap[i]);
  239. while(j < 8){
  240. let x = column + j;
  241. let y = line + i;
  242. if(binary[j] = 1){
  243. do Screen.drawPixel(x, y);
  244. }
  245. let j = j + 1;
  246. }
  247. let i = i + 1;
  248. }
  249. if(pre_column = 504){
  250. if(pre_line = 242){
  251. do Output.moveCursor(0, 0);
  252. }else{
  253. let column = 0;
  254. let line = line + 11;
  255. }
  256. }else{
  257. let column = column + 8;
  258. }
  259. return;
  260. }
  261.  
  262. /** Prints s starting at the cursor location, and advances the
  263. * cursor appropriately. */
  264. function void printString(String s) {
  265. var int i;
  266. var char c;
  267. let i = 0;
  268. while(i < s.length()){
  269. let c = s.charAt(i);
  270. do Output.printChar(c);
  271. let i = i + 1;
  272. }
  273. return;
  274. }
  275.  
  276. /** Prints i starting at the cursor location, and advances the
  277. * cursor appropriately. */
  278. function void printInt(int i) {
  279. var string int_string;
  280. do int_string.setInt(i);
  281. do Output.printString(int_string);
  282. return;
  283. }
  284.  
  285. /** Advances the cursor to the beginning of the next line. */
  286. function void println() {
  287. let column = 0;
  288. if(line = 242){
  289. let line = 0;
  290. }else{
  291. let line = line + 11;
  292. }
  293. return;
  294. }
  295.  
  296. /** Moves the cursor one column back. */
  297. function void backSpace() {
  298. if(column = 0){
  299. if (line = 0){
  300. /* do Output.moveCursor(0,0); */
  301. let column = 504;
  302. let line = 242;
  303. }else{
  304. let column = 504;
  305. let line = line - 11;
  306. }
  307. }else{
  308. let column = column - 8;
  309. }
  310. return;
  311. }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement