-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathScreen.jack
executable file
·209 lines (172 loc) · 5.5 KB
/
Screen.jack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// 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/Screen.jack
/**
* A library of functions for displaying graphics on the screen.
* The Hack physical screen consists of 512 rows (indexed 0..511, top to bottom)
* of 256 pixels each (indexed 0..255, left to right). The top left pixel on
* the screen is indexed (0,0).
*/
class Screen {
static Array twoToThe;
static int SCREENSTART, SCREENEND;
static int XMAX, YMAX;
static boolean isBlack;
/** Initializes the Screen. */
function void init() {
let twoToThe = Array.new(16);
let twoToThe[0] = 1;
let twoToThe[1] = 2;
let twoToThe[2] = 4;
let twoToThe[3] = 8;
let twoToThe[4] = 16;
let twoToThe[5] = 32;
let twoToThe[6] = 64;
let twoToThe[7] = 128;
let twoToThe[8] = 256;
let twoToThe[9] = 512;
let twoToThe[10] = 1024;
let twoToThe[11] = 2048;
let twoToThe[12] = 4096;
let twoToThe[13] = 8192;
let twoToThe[14] = 16384;
let twoToThe[15] = 16384 + 16384;
let isBlack = true;
let SCREENSTART = 16384;
let SCREENEND = 24576;
let XMAX = 511;
let YMAX = 255;
return;
}
/** Erases the entire screen. */
function void clearScreen() {
var int i;
let i = SCREENSTART;
while (i < SCREENEND) {
do Memory.poke(i, 0);
let i = i + 1;
}
return;
}
/** Sets the current color, to be used for all subsequent drawXXX commands.
* Black is represented by true, white by false. */
function void setColor(boolean b) {
let isBlack = b;
return;
}
/** Draws the (x,y) pixel, using the current color. */
function void drawPixel(int x, int y) {
var int address, value;
let address = SCREENSTART + (32 * y) + (x / 16);
let value = Memory.peek(address);
if (isBlack) {
let value = value | twoToThe[x & 15];
} else {
let value = value & ~(twoToThe[x & 15]);
}
do Memory.poke(address, value);
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;
let dx = x2 - x1;
let dy = y2 - y1;
if ((dx = 0) & (dy = 0)) {
do Screen.drawPixel(x1, y1);
return;
}
if (dx = 0) {
if (dy > 0) {
do Screen.drawVerticalLine(x1, y1, y2);
} else {
do Screen.drawVerticalLine(x1, y2, y1);
}
return;
}
if (dy = 0) {
if (dx > 0) {
do Screen.drawHorizontalLine(y1, x1, x2);
} else {
do Screen.drawHorizontalLine(y1, x2, x1);
}
return;
}
if (dx > 0) {
do Screen.drawDiagonalLine(x1, y1, x2, y2);
} else {
do Screen.drawDiagonalLine(x2, y2, x1, y1);
}
return;
}
/** Draws a vertical line at x1 from y1 to y2. y2 > y1. */
function void drawVerticalLine(int x1, int y1, int y2) {
while (~(y2 < y1)) {
do Screen.drawPixel(x1, y1);
let y1 = y1 + 1;
}
return;
}
/** Draws an horizontal line at y1 from x1 to x2. x2 > x1. */
function void drawHorizontalLine(int y1, int x1, int x2) {
while (~(x2 < x1)) {
do Screen.drawPixel(x1, y1);
let x1 = x1 + 1;
}
return;
}
/** Draws a diagonal line from (x1, y1) to (x2, y2), x2 > x1. */
function void drawDiagonalLine(int x1, int y1, int x2, int y2) {
var int dx, dy, a, b, adyMinusbdx;
let dx = x2 - x1;
let dy = y2 - y1;
let a = 0;
let b = 0;
let adyMinusbdx = 0;
if (dy > 0) {
while (~(a > dx) & ~(b > dy)) {
do Screen.drawPixel(x1 + a, y1 + b);
if (adyMinusbdx < 0) {
let a = a + 1;
let adyMinusbdx = adyMinusbdx + dy;
} else {
let b = b + 1;
let adyMinusbdx = adyMinusbdx - dx;
}
}
} else {
while (~(a > dx) & ~(b < dy)) {
do Screen.drawPixel(x1 + a, y1 + b);
if (adyMinusbdx < 0) {
let b = b - 1;
let adyMinusbdx = adyMinusbdx + dx;
} else {
let a = a + 1;
let adyMinusbdx = adyMinusbdx + dy;
}
}
}
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) {
while (~(x1 > x2)) {
do Screen.drawLine(x1, y1, x1, y2);
let x1 = x1 + 1;
}
return;
}
/** Draws a filled circle of radius r<=181 around (x,y), using the current color. */
function void drawCircle(int x, int y, int r) {
var int dx, dy;
let dy = -r;
while (~(dy > r)) {
let dx = Math.sqrt((r * r) - (dy * dy));
do Screen.drawLine(x - dx, y + dy, x + dx, y + dy);
let dy = dy + 1;
}
return;
}
}