From 5d739383aa2d57eeeaaeb2017452d0d56a1faf27 Mon Sep 17 00:00:00 2001 From: hkz Date: Wed, 20 Aug 2025 20:57:26 +0200 Subject: [PATCH] Replace enums with simple defs, replace kb routine with asm calypsi uses "int" as the type for enums, also replace the basic keyboard routine for key reading with an assembly written one --- Makefile | 2 +- src/game_graphics.c | 10 +++---- src/game_graphics.h | 12 ++++----- src/game_logic.c | 10 +++---- src/game_logic.h | 12 ++++----- src/input.c | 35 +++---------------------- src/input.h | 16 +++++------- src/input_asm.s | 63 +++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 16 ++++++------ 9 files changed, 103 insertions(+), 73 deletions(-) create mode 100644 src/input_asm.s diff --git a/Makefile b/Makefile index 037b824..798da8b 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ LIBS=clib-6502.a # Common source files -ASM_SRCS = tk2k_startup.s preserve_zero_pages.s +ASM_SRCS = tk2k_startup.s preserve_zero_pages.s input_asm.s C_SRCS = main.c monitor_subroutines.c utility.c \ game_graphics.c game_logic.c input.c \ line_data.c tiles.c graph_misc_data.c \ diff --git a/src/game_graphics.c b/src/game_graphics.c index 9a6ba1c..d06e014 100644 --- a/src/game_graphics.c +++ b/src/game_graphics.c @@ -336,14 +336,14 @@ void draw_picture(uint8_t w, uint8_t h, uint8_t x, uint8_t y, const uint8_t *dat } } -void ddraw_direction_arrows(arrow_direction dir) { +void ddraw_direction_arrows(uint8_t dir) { uint8_t pic_buffer[ARROWS_HEIGHT]; int8_t start, step, end, flip; uint8_t ext, x, y; switch(dir) { - case ARROW_UP: + case GRAPH_ARROW_UP: x = 2; y = TOP_OFFSET + 1; ext = 1; @@ -352,7 +352,7 @@ void ddraw_direction_arrows(arrow_direction dir) { end = ARROWS_HEIGHT; flip = 0; break; - case ARROW_DOWN: + case GRAPH_ARROW_DOWN: x = 2; y = TOP_OFFSET + (GRID_SIDE * GRID_CELL_SIDE); ext = 1; @@ -361,7 +361,7 @@ void ddraw_direction_arrows(arrow_direction dir) { end = 0; flip = 0; break; - case ARROW_LEFT: + case GRAPH_ARROW_LEFT: x = 1; y = TOP_OFFSET + 7; ext = 0; @@ -370,7 +370,7 @@ void ddraw_direction_arrows(arrow_direction dir) { end = (ARROWS_HEIGHT * 2); flip = 0; break; - case ARROW_RIGHT: + case GRAPH_ARROW_RIGHT: x = 1 + (GRID_SIDE * (GRID_CELL_SIDE/7)); y = TOP_OFFSET + 7; ext = 0; diff --git a/src/game_graphics.h b/src/game_graphics.h index eb04e18..126ac18 100644 --- a/src/game_graphics.h +++ b/src/game_graphics.h @@ -13,12 +13,10 @@ #define BRD_SKIP_LEFT(a) (a & 0x40) #define BRD_SKIP_RIGHT(a) (a & 0x80) -typedef enum { - ARROW_UP, - ARROW_DOWN, - ARROW_LEFT, - ARROW_RIGHT -} arrow_direction; +#define GRAPH_ARROW_UP 0 +#define GRAPH_ARROW_DOWN 1 +#define GRAPH_ARROW_LEFT 2 +#define GRAPH_ARROW_RIGHT 3 void ddraw_field_borders_on_buffer(uint8_t brd); void draw_game_background(uint16_t hi_score); @@ -28,7 +26,7 @@ void ddraw_single_tile(uint8_t offset); void swap_display_buffers(void); void clear_display_buffers(void); void clear_box(uint8_t w, uint8_t h, uint8_t off_x, uint8_t off_y, uint8_t *disp_buf); -void ddraw_direction_arrows(arrow_direction dir); +void ddraw_direction_arrows(uint8_t dir); void ddraw_endgame_box(int8_t done, uint16_t score, uint16_t hi_score); #endif /* _GAME_GRAPHICS_HEADER_ */ diff --git a/src/game_logic.c b/src/game_logic.c index c98307e..aa19674 100644 --- a/src/game_logic.c +++ b/src/game_logic.c @@ -61,7 +61,7 @@ uint8_t add_random_tile(void) { return 0; // Return 0 if we were not able to place the tile, else we return (offset + 1) to indicate where the tile was placed } -int8_t step_game(step_direction dir) { +int8_t step_game(uint8_t dir) { int8_t done = 0; uint8_t start_offset; int8_t column_step; @@ -75,22 +75,22 @@ int8_t step_game(step_direction dir) { */ switch(dir) { - case STEP_UP: + case GAME_STEP_UP: start_offset = GRID_SIDE - 1; column_step = GRID_SIDE; row_step = -1; break; - case STEP_DOWN: + case GAME_STEP_DOWN: start_offset = (GRID_SIDE * GRID_SIDE) - 1; column_step = -GRID_SIDE; row_step = -1; break; - case STEP_LEFT: + case GAME_STEP_LEFT: start_offset = 0; column_step = 1; row_step = GRID_SIDE; break; - case STEP_RIGHT: + case GAME_STEP_RIGHT: start_offset = GRID_SIDE - 1; column_step = -1; row_step = GRID_SIDE; diff --git a/src/game_logic.h b/src/game_logic.h index 19d7939..288d072 100644 --- a/src/game_logic.h +++ b/src/game_logic.h @@ -5,16 +5,14 @@ #define GRID_SIDE 5 -typedef enum { - STEP_UP, - STEP_DOWN, - STEP_LEFT, - STEP_RIGHT -} step_direction; +#define GAME_STEP_UP 0 +#define GAME_STEP_DOWN 1 +#define GAME_STEP_LEFT 2 +#define GAME_STEP_RIGHT 3 uint8_t reset_game(void); uint8_t *get_front_grid(void); -int8_t step_game(step_direction dir); +int8_t step_game(uint8_t dir); uint16_t calculate_score(void); uint8_t add_random_tile(void); diff --git a/src/input.c b/src/input.c index 1954e5b..3278088 100644 --- a/src/input.c +++ b/src/input.c @@ -3,38 +3,11 @@ #include "utility.h" #include "mem_registers.h" -key __internal_read_kb(void); +uint8_t __internal_read_kb(void); -inline key __internal_read_kb(void) { - PEEK(IO_KB_CTRL_HI); - - if(PEEK(IO_DATAIN) & 0x01) { - POKE(IO_DATAOUT, 0x04); - if (PEEK(IO_DATAIN) & 0x04) { - return K_CTRL_R; - } - } - - PEEK(IO_KB_CTRL_LOW); - - POKE(IO_DATAOUT, 0x40); - if (PEEK(IO_DATAIN) & 0x01) return K_UP; - - POKE(IO_DATAOUT, 0x20); - if (PEEK(IO_DATAIN) & 0x01) return K_DOWN; - - POKE(IO_DATAOUT, 0x10); - if (PEEK(IO_DATAIN) & 0x01) return K_RIGHT; - - POKE(IO_DATAOUT, 0x08); - if (PEEK(IO_DATAIN) & 0x01) return K_LEFT; - - return K_NONE; -} - -key read_kb(void) { - static key last_press = K_NONE; - key cur_press = __internal_read_kb(); +uint8_t read_kb(void) { + static uint8_t last_press = K_NONE; + uint8_t cur_press = __internal_read_kb(); if (cur_press != last_press) { last_press = cur_press; diff --git a/src/input.h b/src/input.h index d57b3f4..0c40334 100644 --- a/src/input.h +++ b/src/input.h @@ -3,16 +3,14 @@ #include -typedef enum { - K_NONE = 0, - K_UP, - K_DOWN, - K_LEFT, - K_RIGHT, - K_CTRL_R, -} key; +#define K_NONE 0 +#define K_UP 1 +#define K_DOWN 2 +#define K_LEFT 3 +#define K_RIGHT 4 +#define K_CTRL_R 5 -key read_kb(void); +uint8_t read_kb(void); uint8_t read_any_key(void); diff --git a/src/input_asm.s b/src/input_asm.s new file mode 100644 index 0000000..d143a8b --- /dev/null +++ b/src/input_asm.s @@ -0,0 +1,63 @@ + .rtmodel version,"1" + .rtmodel core,"6502" + + .extern _Vfp + .extern _Vsp + .extern _Zp + +K_NONE: .equ 0 +K_UP .equ 1 +K_DOWN .equ 2 +K_LEFT .equ 3 +K_RIGHT .equ 4 +K_CTRL_R .equ 5 + +__internal_read_kb: +IO_DATAOUT$:.equ 0xC000 +IO_DATAIN$: .equ 0xC010 +IO_KB_CTRL_LOW$: .equ 0xC05E +IO_KB_CTRL_HI$: .equ 0xC05F + + lda IO_KB_CTRL_HI$ + lda IO_DATAIN$ + ror a + bcc NoCtrl$ + lda #0x04 + sta IO_DATAOUT$ + lda IO_DATAIN$ + ror a + ror a + ror a + lda #K_CTRL_R + bcs Return$ + +NoCtrl$: + lda IO_KB_CTRL_LOW$ + ldy #3 +NextKey$: lda _rkb_key_inp,y + sta IO_DATAOUT$ + lda IO_DATAIN$ + ror a + lda _rkb_key_ret,y + bcs Return$ + dey + bpl NextKey$ + + lda #K_NONE +Return$: + rts + +_rkb_key_ret: + .byte K_LEFT + .byte K_RIGHT + .byte K_DOWN + .byte K_UP + +_rkb_key_inp: + .byte 0x08 + .byte 0x10 + .byte 0x20 + .byte 0x40 + +;;; Declaration of public symbols + .public __internal_read_kb diff --git a/src/main.c b/src/main.c index 44d46b0..cd2b163 100644 --- a/src/main.c +++ b/src/main.c @@ -86,23 +86,23 @@ __task int main(void) { switch(read_kb()) { case K_UP: BELL1(); - done = step_game(STEP_UP); - ddraw_direction_arrows(ARROW_UP); + done = step_game(GAME_STEP_UP); + ddraw_direction_arrows(GRAPH_ARROW_UP); break; case K_DOWN: BELL1(); - done = step_game(STEP_DOWN); - ddraw_direction_arrows(ARROW_DOWN); + done = step_game(GAME_STEP_DOWN); + ddraw_direction_arrows(GRAPH_ARROW_DOWN); break; case K_LEFT: BELL1(); - done = step_game(STEP_LEFT); - ddraw_direction_arrows(ARROW_LEFT); + done = step_game(GAME_STEP_LEFT); + ddraw_direction_arrows(GRAPH_ARROW_LEFT); break; case K_RIGHT: BELL1(); - done = step_game(STEP_RIGHT); - ddraw_direction_arrows(ARROW_RIGHT); + done = step_game(GAME_STEP_RIGHT); + ddraw_direction_arrows(GRAPH_ARROW_RIGHT); break; case K_CTRL_R: BELL1();