From ecedd8fd5923a3319096683017613374679d2bb0 Mon Sep 17 00:00:00 2001 From: Bjorn Einar Bjarntes Date: Sat, 18 Dec 2021 21:00:31 +0100 Subject: [PATCH] it works --- readme.md | 4 +- src/main.c | 147 ++++++++++++++++++++++++++++++++--------------------- 2 files changed, 92 insertions(+), 59 deletions(-) diff --git a/readme.md b/readme.md index 67cc132..09c121e 100644 --- a/readme.md +++ b/readme.md @@ -49,4 +49,6 @@ https://github.com/empathicqubit/vscode-cc65-debugger/tree/master/src/__tests__/ https://archive.org/details/The_Graphics_Book_for_the_Commodore_64/page/n25/mode/2up -http://www.appleoldies.ca/cc65/docs/shr/shrworld.pd \ No newline at end of file +http://www.appleoldies.ca/cc65/docs/shr/shrworld.pd + +https://archive.org/details/The_Graphics_Book_for_the_Commodore_64/page/n59 \ No newline at end of file diff --git a/src/main.c b/src/main.c index c270f23..55f1b57 100644 --- a/src/main.c +++ b/src/main.c @@ -13,18 +13,17 @@ unsigned short i; -unsigned short x; // maybe if the ant does not go tooo far it could be an int.. -BYTE y; -BYTE direction; // 0 = right, 64 = up, 128 = left, 192 = down -void setAndClearHiRes(void) { - // Hi-res is turned on by setting bits 5 and 6 (bit 6 must be set in any event) - // of register 17 of the VIC and clearing bit 4 of register 22. - - +short x; // maybe if the ant does not go tooo far it could be an int.. +short y; +short direction; // 0 = right, 64 = up, 128 = left, 192 = down +void setHiRes(void) { *(BYTE*)0xd011 = *(BYTE*)0xd011 | 0xb0 ; // Graphics on *(BYTE*)0xd016 = *(BYTE*)0xd016 & 240; //Multi color off *(BYTE*)0xd018 = *(BYTE*)0xd018 | 8 ; // Graphics to $2000 - +} +void clearHiRes(void) { + // Hi-res is turned on by setting bits 5 and 6 (bit 6 must be set in any event) + // of register 17 of the VIC and clearing bit 4 of register 22. // Clear memory for (i =0;i< 8000; i++) { @@ -34,85 +33,117 @@ void setAndClearHiRes(void) { // Clear colors for (i =0;i< 1000; i++) { - *(BYTE*)(0x400+i) = 0xF0; + *(BYTE*)(0x400+i) = 0xf0; } } -int power2(int exponent) -{ - int result=1; - for (exponent; exponent>0; exponent--) +void fillHiRes(void) { + // Hi-res is turned on by setting bits 5 and 6 (bit 6 must be set in any event) + // of register 17 of the VIC and clearing bit 4 of register 22. + // Clear memory + for (i =0;i< 8000; i++) { - result = result * 2; + *(BYTE*)(0x2000+i) = 0xff; } - return result; +} + + +void setAndClearHiRes(){ + setHiRes(); + clearHiRes(); } // need to think -BYTE isPositionBlack(unsigned short x, BYTE y) +BYTE isPositionWhite(short x, short y) { - unsigned short ra = (320 * (BYTE)(y/8)) + (y & 7); - unsigned short ba = 8 * (BYTE)(x/8); - unsigned short ma = 255 - power2((7-(x & 7))); - unsigned short sa = 0x2000; - unsigned short ad = sa+ra+ba; - return 1; -// return *(BYTE*)(ad) & ma; + short ra = (320 * (short)(y/8)) + (y & 7); + short ba = 8 * (short)(x/8); + short ma = 1 << ((7-(x & 7))); + short sa = 0x2000; + short ad = sa+ra+ba; + return *(short*)(ad) & ma; } // https://archive.org/details/The_Graphics_Book_for_the_Commodore_64/page/n129/ -void setPositionWhite(unsigned short x, BYTE y) +void setPositionWhite(short x, short y) { - unsigned short ra = (320 * (BYTE)(y/8)) + (y & 7); - unsigned short ba = 8 * (BYTE)(x/8); - unsigned short ma = power2((7-(x & 7))); - unsigned short sa = 0x2000; - unsigned short ad = sa+ra+ba; - *(unsigned short*)(ad) = *(unsigned short*)(ad) | ma; + short ra = (320 * (short)(y/8)) + (y & 7); + short ba = 8 * (short)(x/8); + short ma = 1 << ((7-(x & 7))); + short sa = 0x2000; + short ad = sa+ra+ba; + *(short*)(ad) = *(short*)(ad) | ma; } -void setPositionBlack(unsigned short x, BYTE y) +void setPositionBlack(unsigned short x, unsigned short y) { - unsigned short ra = (320 * (BYTE)(y/8)) + (y & 7); - unsigned short ba = 8 * (BYTE)(x/8); - unsigned short ma = 255 - power2((7-(x & 7))); - unsigned short sa = 0x2000; - unsigned short ad = sa+ra+ba; - *(unsigned short*)(ad) = *(unsigned short*)(ad) & ma; - + short ra = (320 * (short)(y/8)) + (y & 7); + short ba = 8 * (short)(x/8); + short ma = (1 << ((7-(x & 7)))); + short sa = 0x2000; + short ad = sa+ra+ba; + *(short*)(ad) = (*(short*)(ad)) & ~ma; } -void makeMove() { - if (isPositionBlack(x,y)) { - setPositionWhite(x,y); - direction = direction - 64; - } else { - setPositionBlack(x,y); - direction = direction + 64; +void moveForward() { + if (direction == 0) { + x = x+1; + } else if (direction == 64) { + y = y+1; + } else if (direction == 128) { + x = x-1; + } else if (direction == 192) { + y = y-1; } } +void turnLeft(){ + direction = direction + 64; + if (direction >= 255) + direction = 0; +} + +void turnRight(){ + direction = direction - 64; + if (direction <0) + direction = 192; +} + + +void makeMove() { + if (isPositionWhite(x,y)) { + turnRight(); + setPositionBlack(x,y); + } else { + turnLeft(); + setPositionWhite(x,y); + } + moveForward(); +} unsigned short line; +unsigned short foo; +unsigned short j; int main(void) { + + setAndClearHiRes(); + for (i = 0;i<50;i++){ + for (j= 0;j<50;j++){ + setPositionWhite(i,j); + } + } + for (i = 0;i<50;i++){ + for (j= 0;j<50;j++){ + setPositionBlack(i,j); + } + } x = 160; y = 100; direction = 0; - setAndClearHiRes(); - while(1) { - if (direction == 0) { - x = x+1; - } else if (direction == 64) { - y = y +1; - } else if (direction == 128) { - x = x-1; - } else { - y = y -1; - } - makeMove(); + makeMove(); } return 0;