-
Notifications
You must be signed in to change notification settings - Fork 0
/
Screen.jack
208 lines (187 loc) · 5.44 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
// 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 boolean currentColor;
static Array twoToThe;
/** Initializes the Screen. */
function void init() {
let currentColor = true;
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;
return;
}
/** Erases the entire screen. */
function void clearScreen() {
var int address;
let address = 16384;
while (address < 24576) {
do Memory.poke(address, 0);
let address = address + 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 currentColor = b;
return;
}
/** Draws the (x,y) pixel, using the current color. */
function void drawPixel(int x, int y) {
var int pixelAddress, currentValue, targetPixel, modifiedValue;
let pixelAddress = 16384 + (y * 32) + (x / 16);
let currentValue = Memory.peek(pixelAddress);
let targetPixel = twoToThe[(x & 15)];
if (currentColor) {
let modifiedValue = currentValue | targetPixel;
}
else {
let modifiedValue = currentValue & -targetPixel;
}
do Memory.poke(pixelAddress, modifiedValue);
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 a, b, diff, dx, dy;
let dx = x2 - x1;
let dy = y2 - y1;
let a = 0;
let b = 0;
let diff = a * dy - b * dx;
if ((dx > 0) & (dy > 0)){
while(~(a > dx) & ~(b > dy)){
do Screen.drawPixel(x1 + a,y1 + b);
if (diff < 0) {
let a = a + 1;
let diff = diff + dy;
}
else {
let b = b + 1;
let diff = diff - dx;
}
}
return;
}
if ((dx = 0) & (dy > 0)){
while(~(b > dy)){
do Screen.drawPixel(x1, y1 + b);
let b = b + 1;
}
return;
}
if ((dx = 0) & (dy < 0)){
while(~(b > Math.abs(dy))){
do Screen.drawPixel(x2, y2 + b);
let b = b + 1;
}
return;
}
if ((dx < 0) & (dy = 0)){
while(~(a > Math.abs(dx))){
do Screen.drawPixel(x2 + a, y2);
let a = a + 1;
}
return;
}
if ((dx > 0) & (dy = 0)){
while(~(a > dx)){
do Screen.drawPixel(x1 + a, y1);
let a = a + 1;
}
return;
}
if((dx > 0) & (dy < 0)){
while(~(a > dx) & ~(b > Math.abs(dy))){
do Screen.drawPixel(x1 + a, y1 - b);
if (diff < 0) {
let a = a + 1;
let diff = diff + Math.abs(dy);
}
else {
let b = b + 1;
let diff = diff - dx;
}
}
return;
}
if((dx < 0) & (dy > 0)){
while(~(a > Math.abs(dx)) & ~(b > dy)){
do Screen.drawPixel(x1 - a, y1 + b);
if (diff < 0) {
let a = a + 1;
let diff = diff + dy;
}
else {
let b = b + 1;
let diff = diff - Math.abs(dx);
}
}
return;
}
if((dx < 0) & (dy < 0)){
while(~(a > Math.abs(dx)) & ~(b > Math.abs(dy))){
do Screen.drawPixel(x1 - a,y1 - b);
if (diff < 0) {
let a = a + 1;
let diff = diff + Math.abs(dy);
}
else {
let b = b + 1;
let diff = diff - Math.abs(dx);
}
}
return;
}
if((dx = 0) & (dy = 0)){
do Screen.drawPixel(x1, y1);
return;
}
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 x1, y1And2, x2, dy;
let dy = -r;
while (dy < r) {
let x1 = x - Math.sqrt((r * r) - (dy * dy));
let y1And2 = y + dy;
let x2 = x + Math.sqrt((r * r) - (dy * dy));
do Screen.drawLine(x1, y1And2, x2, y1And2);
let dy = dy + 1;
}
return;
}
}