added v2.0

This commit is contained in:
Michael Schröder
2023-01-11 11:13:09 +01:00
parent 2a5a64ca91
commit 971b323822
584 changed files with 159319 additions and 0 deletions

View File

@@ -0,0 +1 @@
../Array.jack

View File

@@ -0,0 +1,40 @@
class Button{
field int position;
field String text;
constructor Button new(int p,String t){
let position = p;
let text = t;
return this;
}
method void dispose(){
if (~(text=null)){
do text.dispose();
}
do Memory.deAlloc(this);
return;
}
method void draw(boolean press){
var int x;
var int y;
var int color;
let x = position & 3;
let y = position/4;
if (press) {
let color = Screen.DARKGREY();
} else {
let color = Screen.LIGHTGREY();
}
do Screen.setPenColor(color);
do Screen.drawRectangle((x*60)+1,288-(y*33),(x*60)+58,318-(y*33));
do Screen.setBackColor(color);
do Screen.setPenColor(Screen.BLACK());
do Output.moveCursor(27-(3*y),1+((x*15)/2));
if (~(text=null)){do Output.printString(text);}
do Screen.setBackColor(Screen.WHITE());
return;
}
}

View File

@@ -0,0 +1 @@
../GPIO.jack

View File

@@ -0,0 +1,89 @@
class Grid {
field Array grid;
/** Constructs a new Tile
*/
constructor Grid new() {
var int i;
let grid = Array.new(220);
do Screen.setPenColor(Screen.DARKGREY());
do Screen.drawRectangle(2,4,3,283);
do Screen.drawRectangle(2,284,147,285);
do Screen.drawRectangle(146,4,147,285);
let i=0;
while (i<220){
let grid[i]=-1;
let i=i+1;
}
return this;
}
/** Deallocates the Tile's memory. */
method void dispose() {
do grid.dispose();
do Memory.deAlloc(this);
return;
}
method void draw(int x, int y, int color){
do Screen.setPenColor(color);
do Screen.drawRectangle((x*14)+6,(y*14)-24,(x*14)+18,(y*14)-12);
if ((x<10)&(x>-1)&(y>-1)&(y<22)) {let grid[(y*10)+x] = color;}
return;
}
method boolean hasTile(int x, int y){
if ((x>9)|(x<0)|(y>21)|(y<0)){
return true;
}
if (grid[(y*10)+x]=-1){
return false;
}
return true;
}
method boolean isClear(int x, int y){
return (grid[(y*10)+x]=Screen.DARKGREY());
}
method int clearRow(){
var int x,y,z;
var int r;
var boolean isClear;
let y=0;
let r=0;
while (y<22){
let isClear = true;
let x=0;
while (x<10){
if (~hasTile(x,y)) { let isClear = false;}
let x=x+1;
}
if (isClear){
let r=r+1;
let x=0;
while (x<10){
do draw(x,y,Screen.DARKGREY());
let x=x+1;
}
}
let y = y+1;
}
let y=1;
while (y<22){
let x=0;
while (x<10){
if (isClear(x,y)) {
let z=y;
while (z>0){
do draw(x,z,grid[(z*10)-10+x]);
let z=z-1;
}
}
let x=x+1;
}
let y = y+1;
}
return r;
}
}

View File

@@ -0,0 +1,67 @@
/**
* The main class of the Tetris game.
*/
class Main {
static Tile tile;
static Menu menu;
static TetrisGame game;
/** Initializes a Tetris game and starts running it. */
function void main() {
var int x;
var int time;
let menu = Menu.new(null,null,null," Play");
do Tile.init();
let time = 10000;
while(time > 0){
if (time = 10000){
do Main.drawStart();
do Main.drawTile();
do menu.draw();
}
if (time = 1){
do Main.clearTile();
do Main.drawTile();
let time = 10000;
}
let x = menu.getButton();
if (x = 3) {
do Main.clearTile();
let game = TetrisGame.new();
do game.run();
do game.dispose();
do menu.draw();
do Sys.wait(5000);
let time = 20000;
}
let time = time - 1;
}
return;
}
function void drawTile(){
let tile = Tile.new();
do tile.move(6,12);
do tile.draw();
return;
}
function void clearTile(){
do tile.erase();
do tile.dispose();
return;
}
function void drawStart(){
var String n2t;
var String hv;
let n2t = "nand2tetris-fpga";
do Screen.clearScreen();
do Screen.setPenColor(0);
do Output.moveCursor(6,7);
do Output.printString(n2t);
do n2t.dispose();
return;
}
}

View File

@@ -0,0 +1,18 @@
all: jack vm asm bin sim
jack:
../../tools/JackCompiler/JackCompiler.pyc ./
vm:
../../tools/VMTranslator/VMTranslator.pyc ./
asm:
../../tools/Assembler/assembler.pyc out.asm
bin:
../../tools/AsciiToBin.py out.hack
sim:
cp out.hack ../00_HACK/ROM.hack
upload:
iceprogduino -o 64k -w out.bin
clean:
rm -f *.vm *.asm *.hack *.bin *~
.PHONY: all clean

View File

@@ -0,0 +1 @@
../Math.jack

View File

@@ -0,0 +1 @@
../Memory.jack

View File

@@ -0,0 +1,70 @@
class Menu{
field Array button;
field int last;
field boolean pressed;
constructor void new(String t0, String t1, String t2, String t3){
let button = Array.new(4);
let button[0] = Button.new(0,t0);
let button[1] = Button.new(1,t1);
let button[2] = Button.new(2,t2);
let button[3] = Button.new(3,t3);
let last = 0;
let pressed = false;
return this;
}
method void dispose(){
var Button b;
var int i;
let i=0;
while (i<4){
let b=button[i];
do b.dispose();
let i=i+1;
}
do button.dispose();
do Memory.deAlloc(this);
return;
}
method void draw(){
var Button b;
var int i;
let i=0;
while (i<4){
let b=button[i];
do b.draw(false);
let i=i+1;
}
return;
}
method int getButton(){
var boolean t;
var Button b;
let t = Touch.getEvent();
if (~t){
return -1;
}
if (pressed){
if (Touch.getPen()=0) {
let pressed = false;
let b=button[last];
do b.draw(false);
}
} else {
if ((Touch.getPen()=1) & (Touch.getY()<1024)){
let pressed = true;
let b = button[last];
do b.draw(false);
let last = Touch.getX()/1024;
let b = button[last];
do b.draw(true);
return last;
}
return -1;
}
return -1;
}
}

View File

@@ -0,0 +1 @@
../Output.jack

View File

@@ -0,0 +1,13 @@
class Random {
static int r;
function int nextInt() {
let r = (r * 31421) + 6927;
return r;
}
function void seed(int i){
let r = i;
return;
}
}

View File

@@ -0,0 +1 @@
../Screen.jack

View File

@@ -0,0 +1 @@
../StdIO.jack

View File

@@ -0,0 +1 @@
../String.jack

View File

@@ -0,0 +1 @@
../Sys.jack

View File

@@ -0,0 +1,106 @@
/**
* Represents a Tetris game.
*/
class TetrisGame {
field String S_next;
field String S_score;
field String S_game;
field Tile tile; // the tile
field Tile next;
field boolean exit; // true when the game is over
field int score; // the current score.
field boolean update;
field Grid grid;
field int time;
field Menu menu;
/** Constructs a new Tetris game. */
constructor TetrisGame new() {
let score = 0;
let S_next = "Next:";
let S_score = "Score:";
let S_game = "Game over!";
do Screen.clearScreen();
let menu = Menu.new(" <-"," L"," R"," ->");
do menu.draw();
do Screen.setPenColor(0);
do Output.moveCursor(4,20);
do Output.printString(S_next);
do Output.moveCursor(13,20);
do Output.printString(S_score);
do Output.moveCursor(15,21);
do Output.printInt(score);
let exit = false;
let grid = Grid.new();
let next = Tile.new();
do next.draw();
let time = 2000;
return this;
}
/** Deallocates the object's memory. */
method void dispose() {
do S_next.dispose();
do S_score.dispose();
do S_game.dispose();
do grid.dispose();
do next.dispose();
do menu.dispose();
do Memory.deAlloc(this);
return;
}
method void score(int r){
if (r=1) {let score = score + 40; }
if (r=2) {let score = score + 100; }
if (r=3) {let score = score + 300; }
if (r=4) {let score = score +1200; }
do Screen.setPenColor(0);
do Output.moveCursor(15,21);
do Output.printInt(score);
return;
}
/** Starts the game, and handles inputs from the user that control
* the bat's movement direction. */
method void run() {
var int p;
let exit = false;
let tile = next;
do tile.span(grid);
let next = Tile.new();
do next.draw();
let exit = false;
while (~exit){
let p = menu.getButton();
if (p = 3) { do tile.moveRight();}
if (p = 2) { do tile.rotateRight();}
if (p = 1) { do tile.rotateLeft();}
if (p = 0) { do tile.moveLeft();}
let time = time -1;
if (time=0){
if (~(tile.moveDown())){
do score(tile.getScore());
do tile.dispose();
let tile = next;
if (~(next.span(grid))) {
let exit = true;
} else {
let next = Tile.new();
do next.draw();
}
}
if (exit) {
do Screen.setPenColor(0);
do Output.moveCursor(10,5);
do Output.printString(S_game);
}
let time = 2000;
}
}
return;
}
}

View File

@@ -0,0 +1,260 @@
/**
* A graphical tile. Characterized by a screen location and color.
* Has methods for drawing, erasing and moving on the screen.
*/
class Tile {
field int x;
field int y;
field int r;
field int type;
field Grid grid;
static Array colors;
static Array bits;
function void init(){
let colors = Array.new(7);
let colors[0] = Screen.YELLOW();
let colors[1] = Screen.TEAL();
let colors[2] = Screen.PURPLE();
let colors[3] = Screen.GREEN();
let colors[4] = Screen.RED();
let colors[5] = Screen.BLUE();
let colors[6] = Screen.ORANGE();
let bits = Array.new(28);
let bits[0] = 1632;
let bits[1] = 1632;
let bits[2] = 1632;
let bits[3] = 1632;
let bits[4] = 3840;
let bits[5] = 8738;
let bits[6] = 240;
let bits[7] = 17476;
let bits[8] = 19968;
let bits[9] = 17984;
let bits[10] = 3648;
let bits[11] = 19520;
let bits[12] = 27648;
let bits[13] = 17952;
let bits[14] = 1728;
let bits[15] = ~29631;
let bits[16] = ~14847;
let bits[17] = 9792;
let bits[18] = 3168;
let bits[19] = 19584;
let bits[20] = ~29183;
let bits[21] = 25664;
let bits[22] = 3616;
let bits[23] = 17600;
let bits[24] = 11776;
let bits[25] = 17504;
let bits[26] = 3712;
let bits[27] = ~15295;
return;
}
/** Constructs a new Tile
*/
constructor Tile new() {
var int r;
let grid = null;
let type=7;
while (type = 7){
let type = Random.nextInt() & 7;
}
let x = 11;
let y = 6;
let r = 0;
return this;
}
method void move(int px,int py){
let x=px;
let y=py;
return;
}
/** Deallocates the Tile's memory. */
method void dispose() {
do Memory.deAlloc(this);
return;
}
method boolean check(){
var int i;
var int j;
var int bit;
let j = 0;
let bit = bits[(type*4)+r];
while (j<4){
let i=0;
while (i<4){
if (bit<0){
if (grid.hasTile(x+i,y+j)) {
return false;
}
}
let i=i+1;
let bit = bit + bit;
}
let j = j+1;
}
return true;
}
method int getScore(){
return grid.clearRow();
}
method void rotateRight(){
do erase();
let r = r+1;
if (r>3) {
let r=r-4;
}
if (check()) {
do draw();
} else {
let r = r-1;
if (r<0) {
let r=r+4;
}
do draw();
}
return;
}
method void rotateLeft(){
do erase();
let r = r-1;
if (r<0) {
let r=r+4;
}
if (check()) {
do draw();
} else {
let r = r+1;
if (r>3) {
let r=r-4;
}
do draw();
}
return;
}
method void moveRight(){
do erase();
let x = x + 1;
if (check()) {
do draw();
} else {
let x = x-1;
do draw();
}
return;
}
method void moveLeft(){
do erase();
let x = (x-1);
if (~(check())) {
let x = x + 1;
}
do draw();
return;
}
method void moveUp(){
do erase();
let y = y - 1;
if (check()) {
do draw();
} else {
let y = y + 1;
do draw();
}
return;
}
method boolean moveDown(){
do erase();
let y = y + 1;
if (check()) {
do draw();
return true;
}
let y = y - 1;
do draw();
return false;
}
method boolean span(Grid g){
do erase();
let grid=g;
let x=3;
let y=0;
if (check()){
do draw();
return true;
}
return false;
}
method void draw(){
var int i,j;
var int bit;
let i = 0;
let j = 0;
let bit = bits[(type*4)+r];
while (j<4){
let i=0;
while (i<4){
if (bit<0){
if (~(grid=null)) {do grid.draw(x+i,y+j,colors[type]);}
else {
do Screen.setPenColor(colors[type]);
do Screen.drawRectangle(((x+i)*14)+6,((y+j)*14)-24,((x+i)*14)+18,((y+j)*14)-12);
}
}
let i = i+1;
let bit = bit + bit;
}
let j = j+1;
}
return;
}
method void erase(){
var int i,j;
var int bit;
let i = 0;
let j = 0;
let bit = bits[(type*4)+r];
while (j<4){
let i=0;
while (i<4){
if (bit<0){
if (~(grid=null)) {
do grid.draw(x+i,y+j,-1);
}
else {
do Screen.setPenColor(-1);
do Screen.drawRectangle(((x+i)*14)+6,((y+j)*14)-24,((x+i)*14)+18,((y+j)*14)-12);
}
}
let i = i+1;
let bit = bit + bit;
}
let j = j+1;
}
return;
}
}

View File

@@ -0,0 +1 @@
../Touch.jack

View File

@@ -0,0 +1 @@
../UART.jack