This commit is contained in:
QkoSad
2025-07-16 13:00:37 +03:00
commit 7894b48931
806 changed files with 162532 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
// 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/ScreenTest/Main.jack
/** Test program for the OS Screen class. */
class Main {
/** Draws a sample pictue on the screen using lines and circles. */
function void main() {
do Screen.drawLine(0,220,511,220); // base line
do Screen.drawRectangle(280,90,410,220); // house
do Screen.setColor(false);
do Screen.drawRectangle(350,120,390,219); // door
do Screen.drawRectangle(292,120,332,150); // window
do Screen.setColor(true);
do Screen.drawCircle(360,170,3); // door handle
do Screen.drawLine(280,90,345,35); // roof
do Screen.drawLine(345,35,410,90); // roof
do Screen.drawCircle(140,60,30); // sun
do Screen.drawLine(140,26, 140, 6);
do Screen.drawLine(163,35,178,20);
do Screen.drawLine(174,60,194,60);
do Screen.drawLine(163,85,178,100);
do Screen.drawLine(140,94,140,114);
do Screen.drawLine(117,85,102,100);
do Screen.drawLine(106,60,86,60);
do Screen.drawLine(117,35,102,20);
return;
}
}
+104
View File
@@ -0,0 +1,104 @@
function Main.main 0
push constant 0
push constant 220
push constant 511
push constant 220
call Screen.drawLine 4
pop temp 0
push constant 280
push constant 90
push constant 410
push constant 220
call Screen.drawRectangle 4
pop temp 0
push constant 0
call Screen.setColor 1
pop temp 0
push constant 350
push constant 120
push constant 390
push constant 219
call Screen.drawRectangle 4
pop temp 0
push constant 292
push constant 120
push constant 332
push constant 150
call Screen.drawRectangle 4
pop temp 0
push constant 1
neg
call Screen.setColor 1
pop temp 0
push constant 360
push constant 170
push constant 3
call Screen.drawCircle 3
pop temp 0
push constant 280
push constant 90
push constant 345
push constant 35
call Screen.drawLine 4
pop temp 0
push constant 345
push constant 35
push constant 410
push constant 90
call Screen.drawLine 4
pop temp 0
push constant 140
push constant 60
push constant 30
call Screen.drawCircle 3
pop temp 0
push constant 140
push constant 26
push constant 140
push constant 6
call Screen.drawLine 4
pop temp 0
push constant 163
push constant 35
push constant 178
push constant 20
call Screen.drawLine 4
pop temp 0
push constant 174
push constant 60
push constant 194
push constant 60
call Screen.drawLine 4
pop temp 0
push constant 163
push constant 85
push constant 178
push constant 100
call Screen.drawLine 4
pop temp 0
push constant 140
push constant 94
push constant 140
push constant 114
call Screen.drawLine 4
pop temp 0
push constant 117
push constant 85
push constant 102
push constant 100
call Screen.drawLine 4
pop temp 0
push constant 106
push constant 60
push constant 86
push constant 60
call Screen.drawLine 4
pop temp 0
push constant 117
push constant 35
push constant 102
push constant 20
call Screen.drawLine 4
pop temp 0
push constant 0
return
+206
View File
@@ -0,0 +1,206 @@
// 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 256 rows (indexed 0..255, top to bottom)
* of 512 pixels each (indexed 0..511, left to right). The top left pixel on
* the screen is indexed (0,0).
*/
class Screen {
static Array mem;
static Array st;
static boolean color;
/** Initializes the Screen. */
function void init() {
var int a;
let st = Array.new(16);
let st[0] = 1;
while (a < 15){
let a = a + 1;
let st[a] = st[a - 1] + st[a - 1];
}
let color = true;
return;
}
/** Erases the entire screen. */
function void clearScreen() {
var int a;
let a = 16384;
while (a < 24575){
let mem[a] = 0;
let a = a + 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 color = b;
return;
}
/** Draws the (x,y) pixel, using the current color. */
function void drawPixel(int x, int y) {
var int value, address, bit, mem_block;
if ((x > 511) | (x < 0) | (y > 255) | (y < 0)){
do Sys.error(7);
}
let mem_block = x / 16;
let address = (32 * y) + mem_block + 16384;
let value = mem[address];
let bit = x - (mem_block * 16);
if (color){
let mem[address] = st[bit] | value;
}
else{
let mem[address] = (~st[bit])& 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 diff, dx, dy, a, b;
if((x1 < 0) | (x2 > 511) | (y1 < 0) | (y2 > 255)){
do Sys.error(8);
}
let dx = x2 - x1;
let dy = y2 - y1;
if (dx = 0){
if (dy > 0){
while(~(b > dy)){
do Screen.drawPixel(x1, y1 + b);
let b = b + 1;
}
return;
}
else{
while(~(b < dy)){
do Screen.drawPixel(x1, y1 + b);
let b = b - 1;
}
return;
}
}
if (dy = 0){
if (dx > 0){
while(~(a > dx)){
do Screen.drawPixel(x1 + a, y1);
let a = a + 1;
}
return;
}
else{
while(~(a < dx)){
do Screen.drawPixel(x1 + a, y1);
let a = a - 1;
}
return;
}
}
if (dx < 0){
if (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;
}
}
}
else{
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;
}
}
}
}
else{
if (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;
}
}
}
else{
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;
}
/** 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) {
var int a, tmp;
if (x1 > x2){
let tmp = x1;
let x1 = x2;
let x2 = tmp;
}
if (y1 > y2){
let tmp = y1;
let y1 = y2;
let y2 = tmp;
}
if((x1 < 0) | (x2 > 511) | (y1 < 0) | (y2 > 255)){
do Sys.error(8);
}
let tmp = y2 - y1;
while(a < tmp){
do Screen.drawLine(x1, y1 + a, x2, y1 + a);
let a = a + 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 dy, dx, tmp;
if(r > 181){
do Sys.error(7);
}
let dy = - r;
while (dy < r){
let tmp = Math.sqrt((r * r) - (dy * dy));
do Screen.drawLine(x - tmp, y + dy, x + tmp, y + dy);
let dy = dy + 1;
}
return;
}
}
+656
View File
@@ -0,0 +1,656 @@
function Screen.init 1
push constant 16
call Array.new 1
pop static 1
push static 1
push constant 0
push constant 1
pop temp 1
add
pop pointer 1
push temp 1
pop that 0
label L0
push local 0
push constant 15
lt
not
if-goto L1
push local 0
push constant 1
add
pop local 0
push static 1
push local 0
push static 1
push local 0
push constant 1
sub
add
pop pointer 1
push that 0
push static 1
push local 0
push constant 1
sub
add
pop pointer 1
push that 0
add
pop temp 1
add
pop pointer 1
push temp 1
pop that 0
goto L0
label L1
push constant 1
neg
pop static 2
push constant 0
return
function Screen.clearScreen 1
push constant 16384
pop local 0
label L2
push local 0
push constant 24575
lt
not
if-goto L3
push static 0
push local 0
push constant 0
pop temp 1
add
pop pointer 1
push temp 1
pop that 0
push local 0
push constant 1
add
pop local 0
goto L2
label L3
push constant 0
return
function Screen.setColor 0
push argument 0
pop static 2
push constant 0
return
function Screen.drawPixel 4
push argument 0
push constant 511
gt
push argument 0
push constant 0
lt
or
push argument 1
push constant 255
gt
or
push argument 1
push constant 0
lt
or
not
if-goto L4
push constant 7
call Sys.error 1
pop temp 0
goto L5
label L4
label L5
push argument 0
push constant 16
call Math.divide 2
pop local 3
push constant 32
push argument 1
call Math.multiply 2
push local 3
add
push constant 16384
add
pop local 1
push static 0
push local 1
add
pop pointer 1
push that 0
pop local 0
push argument 0
push local 3
push constant 16
call Math.multiply 2
sub
pop local 2
push static 2
not
if-goto L6
push static 0
push local 1
push static 1
push local 2
add
pop pointer 1
push that 0
push local 0
or
pop temp 1
add
pop pointer 1
push temp 1
pop that 0
goto L7
label L6
push static 0
push local 1
push static 1
push local 2
add
pop pointer 1
push that 0
not
push local 0
and
pop temp 1
add
pop pointer 1
push temp 1
pop that 0
label L7
push constant 0
return
function Screen.drawLine 5
push argument 0
push constant 0
lt
push argument 2
push constant 511
gt
or
push argument 1
push constant 0
lt
or
push argument 3
push constant 255
gt
or
not
if-goto L8
push constant 8
call Sys.error 1
pop temp 0
goto L9
label L8
label L9
push argument 2
push argument 0
sub
pop local 1
push argument 3
push argument 1
sub
pop local 2
push local 1
push constant 0
eq
not
if-goto L10
push local 2
push constant 0
gt
not
if-goto L11
label L12
push local 4
push local 2
gt
not
not
if-goto L13
push argument 0
push argument 1
push local 4
add
call Screen.drawPixel 2
pop temp 0
push local 4
push constant 1
add
pop local 4
goto L12
label L13
push constant 0
return
goto L14
label L11
label L15
push local 4
push local 2
lt
not
not
if-goto L16
push argument 0
push argument 1
push local 4
add
call Screen.drawPixel 2
pop temp 0
push local 4
push constant 1
sub
pop local 4
goto L15
label L16
push constant 0
return
label L14
goto L17
label L10
label L17
push local 2
push constant 0
eq
not
if-goto L18
push local 1
push constant 0
gt
not
if-goto L19
label L20
push local 3
push local 1
gt
not
not
if-goto L21
push argument 0
push local 3
add
push argument 1
call Screen.drawPixel 2
pop temp 0
push local 3
push constant 1
add
pop local 3
goto L20
label L21
push constant 0
return
goto L22
label L19
label L23
push local 3
push local 1
lt
not
not
if-goto L24
push argument 0
push local 3
add
push argument 1
call Screen.drawPixel 2
pop temp 0
push local 3
push constant 1
sub
pop local 3
goto L23
label L24
push constant 0
return
label L22
goto L25
label L18
label L25
push local 1
push constant 0
lt
not
if-goto L26
push local 2
push constant 0
lt
not
if-goto L27
label L28
push local 3
push local 1
lt
not
push local 4
push local 2
lt
not
and
not
if-goto L29
push argument 0
push local 3
add
push argument 1
push local 4
add
call Screen.drawPixel 2
pop temp 0
push local 0
push constant 0
lt
not
if-goto L30
push local 3
push constant 1
sub
pop local 3
push local 0
push local 2
sub
pop local 0
goto L31
label L30
push local 4
push constant 1
sub
pop local 4
push local 0
push local 1
add
pop local 0
label L31
goto L28
label L29
goto L32
label L27
label L33
push local 3
push local 1
lt
not
push local 4
push local 2
gt
not
and
not
if-goto L34
push argument 0
push local 3
add
push argument 1
push local 4
add
call Screen.drawPixel 2
pop temp 0
push local 0
push constant 0
lt
not
if-goto L35
push local 3
push constant 1
sub
pop local 3
push local 0
push local 2
add
pop local 0
goto L36
label L35
push local 4
push constant 1
add
pop local 4
push local 0
push local 1
add
pop local 0
label L36
goto L33
label L34
label L32
goto L37
label L26
push local 2
push constant 0
lt
not
if-goto L38
label L39
push local 3
push local 1
gt
not
push local 4
push local 2
lt
not
and
not
if-goto L40
push argument 0
push local 3
add
push argument 1
push local 4
add
call Screen.drawPixel 2
pop temp 0
push local 0
push constant 0
lt
not
if-goto L41
push local 3
push constant 1
add
pop local 3
push local 0
push local 2
sub
pop local 0
goto L42
label L41
push local 4
push constant 1
sub
pop local 4
push local 0
push local 1
sub
pop local 0
label L42
goto L39
label L40
goto L43
label L38
label L44
push local 3
push local 1
gt
not
push local 4
push local 2
gt
not
and
not
if-goto L45
push argument 0
push local 3
add
push argument 1
push local 4
add
call Screen.drawPixel 2
pop temp 0
push local 0
push constant 0
lt
not
if-goto L46
push local 3
push constant 1
add
pop local 3
push local 0
push local 2
add
pop local 0
goto L47
label L46
push local 4
push constant 1
add
pop local 4
push local 0
push local 1
sub
pop local 0
label L47
goto L44
label L45
label L43
label L37
push constant 0
return
function Screen.drawRectangle 2
push argument 0
push argument 2
gt
not
if-goto L48
push argument 0
pop local 1
push argument 2
pop argument 0
push local 1
pop argument 2
goto L49
label L48
label L49
push argument 1
push argument 3
gt
not
if-goto L50
push argument 1
pop local 1
push argument 3
pop argument 1
push local 1
pop argument 3
goto L51
label L50
label L51
push argument 0
push constant 0
lt
push argument 2
push constant 511
gt
or
push argument 1
push constant 0
lt
or
push argument 3
push constant 255
gt
or
not
if-goto L52
push constant 8
call Sys.error 1
pop temp 0
goto L53
label L52
label L53
push argument 3
push argument 1
sub
pop local 1
label L54
push local 0
push local 1
lt
not
if-goto L55
push argument 0
push argument 1
push local 0
add
push argument 2
push argument 1
push local 0
add
call Screen.drawLine 4
pop temp 0
push local 0
push constant 1
add
pop local 0
goto L54
label L55
push constant 0
return
function Screen.drawCircle 3
push argument 2
push constant 181
gt
not
if-goto L56
push constant 7
call Sys.error 1
pop temp 0
goto L57
label L56
label L57
push argument 2
neg
pop local 0
label L58
push local 0
push argument 2
lt
not
if-goto L59
push argument 2
push argument 2
call Math.multiply 2
push local 0
push local 0
call Math.multiply 2
sub
call Math.sqrt 1
pop local 2
push argument 0
push local 2
sub
push argument 1
push local 0
add
push argument 0
push local 2
add
push argument 1
push local 0
add
call Screen.drawLine 4
pop temp 0
push local 0
push constant 1
add
pop local 0
goto L58
label L59
push constant 0
return
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB