From 9345fad7941b6d7b2b92821454e7c937fcb9cfc3 Mon Sep 17 00:00:00 2001 From: Steven Hugg Date: Sun, 7 Aug 2022 11:41:31 -0500 Subject: [PATCH] c64: presets, reset keyboard --- presets/c64/23matches.c | 85 +++++++++++++++++++++++++++++++++++++++ presets/c64/skeleton.cc65 | 8 +++- presets/c64/sprite_test.c | 46 +++++++++++++++++++++ src/machine/c64.ts | 2 +- 4 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 presets/c64/23matches.c create mode 100644 presets/c64/sprite_test.c diff --git a/presets/c64/23matches.c b/presets/c64/23matches.c new file mode 100644 index 00000000..1b9d2be8 --- /dev/null +++ b/presets/c64/23matches.c @@ -0,0 +1,85 @@ + +#include +#include +#include +#include +#include +#include +#include + +int matches; // number of matches remaining +int take; // # of matches to take on this turn + +void print_matches() { + printf("\nThere are %d matches left.\n", matches); +} + +void human_moves() { + print_matches(); + // loop until we get a valid move + while (1) { + printf("\n>> Your turn. Take 1, 2, or 3 matches?"); + // did we get exactly one input value? + if (scanf("%d", &take) == 1) { + // is it between 1 and 3? + if (take >= 1 && take <= 3) { + // are there not enough matches? + if (take > matches) { + printf("Not enough matches! Try again.\n"); + continue; // go back to start of loop + } else { + // take the appropriate number of matches + printf("You took %d matches.\n", take); + matches -= take; + break; // break out of loop + } + } + } + printf("Bad input! Type a number from 1 to 3.\n"); + } +} + +void computer_moves() { + print_matches(); + // simple AI: hard coded if 1-4 matches left + // otherwise take random number from 1 to 3 + switch (matches) { + case 1: take = 1; break; + case 2: take = 1; break; + case 3: take = 2; break; + case 4: take = 3; break; + default: take = (rand() % 3) + 1; break; + } + printf("\n<< My turn. I'll take %d matches.\n", take); + matches -= take; +} + +void play_game(void) { + printf( + "When it is your turn, you may take\n" + "1, 2 or 3 matches. I will do the same.\n\n" + "Whoever takes the last match loses.\n"); + + matches = 23; + // loop until no more matches + while (matches > 0) { + // move human, check if they lost + human_moves(); + if (matches == 0) { + printf("You lose, turkey!\nBetter luck next time.\n"); + break; + } + // move computer, check if they lost + computer_moves(); + if (matches == 0) { + printf("I lost! You must be good!\n"); + break; + } + } +} + +void main(void) { + clrscr(); + printf("*** 23 MATCHES ***\n\n"); + play_game(); +} diff --git a/presets/c64/skeleton.cc65 b/presets/c64/skeleton.cc65 index 4de32e87..9a0141e8 100644 --- a/presets/c64/skeleton.cc65 +++ b/presets/c64/skeleton.cc65 @@ -1,10 +1,14 @@ -#include #include +#include +#include +#include +#include #include #include void main(void) { clrscr(); - printf("\r\nHello World!\r\n"); + printf("\nHello World!\n"); + getchar(); } diff --git a/presets/c64/sprite_test.c b/presets/c64/sprite_test.c new file mode 100644 index 00000000..23f8a3b1 --- /dev/null +++ b/presets/c64/sprite_test.c @@ -0,0 +1,46 @@ + +#include +#include +#include +#include +#include +#include +#include + +/*{w:24,h:21,bpp:1,brev:1,count:1}*/ +const char SPRITE_DATA[64] = { + 0x0f,0xff,0x80,0x17,0xff,0x40,0x2b,0xfe, + 0xa0,0x7f,0xff,0xf0,0xff,0xc0,0x3f,0xe0, + 0x3f,0xc0,0x17,0xbe,0xc0,0x2d,0x7f,0xf0, + 0x2b,0x7f,0xf8,0x2a,0xff,0xf8,0x15,0xf6, + 0x00,0x3f,0xf8,0x00,0xfd,0xfc,0x00,0xfd, + 0xff,0x00,0xfe,0xff,0x80,0xff,0x7f,0xc0, + 0xff,0xff,0xc0,0xff,0xff,0xc0,0x0a,0xa8, + 0x00,0x0f,0xf8,0x00,0x0f,0xff,0x80,0x03, +}; + +/*{w:12,h:21,bpp:2,brev:1,count:1,aspect:2}*/ +const char SPRITE_MC_DATA[64] = { + 0x0a,0xaa,0x80,0x0a,0xaa,0x80,0x2a,0xaa, + 0xa0,0x2a,0xaa,0xa0,0xaa,0xaa,0xaa,0xff, + 0xd5,0x40,0x0d,0xd7,0x40,0x3d,0xd5,0x54, + 0x37,0x55,0x54,0x37,0x55,0x54,0x35,0x55, + 0x00,0x3a,0xa0,0x00,0xea,0xa8,0x00,0xab, + 0xaa,0x00,0xab,0xaa,0x00,0xab,0xaa,0x80, + 0xaa,0xea,0x80,0xaa,0xaa,0x80,0x0f,0xfc, + 0x00,0x0f,0xfc,0x00,0x0f,0xff,0xc0,0x83, +}; + +void main(void) { + // copy sprite pattern to RAM address 0x3800 + memcpy((char*)0x3800, SPRITE_DATA, sizeof(SPRITE_DATA)); + // set sprite shape entry (224) + POKE(0x400 + 0x3f8, 0x3800 / 64); + // set X and Y coordinate + VIC.spr_pos[0].x = 172; + VIC.spr_pos[0].y = 145; + // set color + VIC.spr_color[0] = COLOR_GREEN; + // enable sprite 0 + VIC.spr_ena = 0b00000001; +} diff --git a/src/machine/c64.ts b/src/machine/c64.ts index 03e4940c..146364bf 100644 --- a/src/machine/c64.ts +++ b/src/machine/c64.ts @@ -33,7 +33,7 @@ export class C64_WASMMachine extends BaseWASMMachine implements Machine, Probeab super.reset(); // clear keyboard for (var ch=0; ch<128; ch++) { - this.setKeyInput(ch, 0, KeyFlags.KeyUp); + this.exports.machine_key_up(this.sys, ch); } // load rom if (this.romptr && this.romlen) {