From e7a5006a4a3f71a8d37989fb00741457420227c7 Mon Sep 17 00:00:00 2001 From: hkz Date: Mon, 13 Oct 2025 21:00:29 +0200 Subject: [PATCH] nitial working version for the VDP --- Makefile | 4 +- src/game_vdp_graphics.s | 8 +++ src/vddem_main.c | 132 ++++++++++++++++++++++++++-------------- src/vdgam_main.c | 1 - 4 files changed, 97 insertions(+), 48 deletions(-) diff --git a/Makefile b/Makefile index 85761ea..9a7598e 100644 --- a/Makefile +++ b/Makefile @@ -44,8 +44,8 @@ VDDLG_C_SRCS = vddlg_main.c input.c utility.c VDGAM_ASM_SRCS = tk2k_startup_module.s preserve_zero_pages.s input_asm.s sound.s vdp.s vdp_utils.s game_vdp_graphics.s VDGAM_C_SRCS = vdgam_main.c input.c utility.c game_logic.c -VDDEM_ASM_SRCS = tk2k_startup_module.s preserve_zero_pages.s input_asm.s sound.s -VDDEM_C_SRCS = vddem_main.c input.c utility.c game_hgr_graphics_demo.c hgr_line_data.c game_logic.c arrows_pic.c tiles.c hgr_graph_misc_data.c +VDDEM_ASM_SRCS = tk2k_startup_module.s preserve_zero_pages.s input_asm.s sound.s vdp.s vdp_utils.s game_vdp_graphics.s +VDDEM_C_SRCS = vddem_main.c input.c utility.c game_logic.c # Object files MASTER_OBJS = $(MASTER_ASM_SRCS:%.s=%.o) $(MASTER_C_SRCS:%.c=%.o) diff --git a/src/game_vdp_graphics.s b/src/game_vdp_graphics.s index 5386fda..b09c2e9 100644 --- a/src/game_vdp_graphics.s +++ b/src/game_vdp_graphics.s @@ -85,6 +85,7 @@ T_X$: .equ _Zp+0 jsr vdp_point_to_vram_xy lda #0 sta VDP_MEM + nop lda #29 sta zp:T_X$ @@ -93,6 +94,7 @@ T_X$: .equ _Zp+0 jsr vdp_point_to_vram_xy lda #0 sta VDP_MEM + nop lda #28 sta zp:T_X$ @@ -102,9 +104,13 @@ T_X$: .equ _Zp+0 lda #0 sta VDP_MEM nop ; Slow down, we're using this while the IC is rendering + nop sta VDP_MEM nop + nop sta VDP_MEM + nop + nop ; Jump to the correct code to handle joystick drawing pla @@ -142,6 +148,8 @@ J_Done$: jsr vdp_point_to_vram_xy lda #120 sta VDP_MEM + nop + nop rts _draw_joystick_jumptable$: diff --git a/src/vddem_main.c b/src/vddem_main.c index 5d9a243..298b4f3 100644 --- a/src/vddem_main.c +++ b/src/vddem_main.c @@ -3,16 +3,19 @@ #include +#include "vdp_utils.h" +#include "game_vdp_graphics.h" + #include "monitor_subroutines.h" #include "utility.h" #include "mem_map.h" +#include "mem_registers.h" #include "shared_page.h" #include "state_page.h" #include "dlog_data.h" #include "game_data.h" #include "input.h" #include "game_logic.h" -#include "game_hgr_graphics.h" #include "monitor_subroutines.h" #include "sound.h" #include "module_list.h" @@ -21,86 +24,116 @@ #pragma require __preserve_zp #pragma require __data_initialization_needed -#define MOVES_TEXT_X 32 -#define MOVES_TEXT_Y 61 -#define MOVES_TEXT_WIDTH 5 +#define HSCORE_TEXT_X 27 +#define HSCORE_TEXT_Y 13 -#define SCORE_TEXT_X 32 -#define SCORE_TEXT_Y 29 -#define SCORE_TEXT_WIDTH 5 +#define SCORE_TEXT_X 27 +#define SCORE_TEXT_Y 4 -#define HIGH_TEXT_X 32 -#define HIGH_TEXT_Y 107 +#define MOVES_TEXT_X 27 +#define MOVES_TEXT_Y 8 -#define KEY_LOOP_LEN 0x2FFF #define MAX_DEMO_MOVES 30 +#define RUNS_TO_SKIP 4000 + static state_page_data* state_page = (state_page_data*)STATE_PAGE; static shared_page_data *shared_page = (shared_page_data*)SHARED_PAGE; +static uint8_t text_buf[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + +static volatile uint8_t key_pressed = 0; + +__attribute__((interrupt)) void irq_handler(void); void main(void) { uint16_t moves_count = 0; uint16_t score = 0; int8_t done = 0; + __disable_interrupts(); // Make sure the interrupts are disabled + // By default, once we return from this, return to the DLOG module and give the master no command to execute shared_page->master_command = MASTER_COMMAND_NONE; - shared_page->next_module_idx = MODULE_DLOG; // Go to the DLOG module + shared_page->next_module_idx = MODULE_DLOG_VDP; // Go to the DLOG module dlog_data *dld = (dlog_data *)(shared_page->module_data); dlog_data *gad = (dlog_data *)(shared_page->module_data); // Make sure the buffers are pointing to the correct memory and are clear - clear_display_buffers(); + vdp_hide_sprites(); + vdp_clear_gamegrid(); + vdp_switch_nt(0); // Make sure VDP shows the gamegrid + // Setup the IRQ handler + POKEW(IRQ_HANDLER_ADDRESS, (uint16_t)irq_handler); + // Reset the game, calculate the initial score depending on which tiles we randomly get score = reset_game(); // Draw the initial state of the game - draw_game_background(state_page->hi_score); - draw_number(moves_count, MOVES_TEXT_WIDTH, MOVES_TEXT_X, MOVES_TEXT_Y); - draw_number(score, SCORE_TEXT_WIDTH, SCORE_TEXT_X, SCORE_TEXT_Y); - draw_tiles(); - - // Swap graphical buffers - swap_display_buffers(); + num_to_decbuf(state_page->hi_score, 5, text_buf); // High score + decbuf_to_ascii(5, text_buf); + vdp_print_string(0, HSCORE_TEXT_X, HSCORE_TEXT_Y, (char*)text_buf); + + num_to_decbuf(moves_count, 5, text_buf); // Moves count + decbuf_to_ascii(5, text_buf); + vdp_print_string(0, MOVES_TEXT_X, MOVES_TEXT_Y, (char*)text_buf); + + num_to_decbuf(score, 5, text_buf); // Score + decbuf_to_ascii(5, text_buf); + vdp_print_string(0, SCORE_TEXT_X, SCORE_TEXT_Y, (char*)text_buf); + + vdp_draw_joystick(JS_POS_CENTER); // Center the joystick + + vdp_redraw_tiles(get_front_grid()); + + __enable_interrupts(); + uint16_t lfsr; + uint16_t current_run = 0; while(1) { // Game loop - uint16_t lfsr = lfsr_update(); + lfsr = lfsr_update(); // Any key will let us out of this, wait some time for a keypress - uint16_t k_loop_count = KEY_LOOP_LEN; - while(k_loop_count--) { - if(read_any_key()) { - snd_mod_button(); - done = 1; - break; - } + __disable_interrupts(); + if(read_any_key()) { + snd_mod_button(); + done = 1; + } + __enable_interrupts(); + + if(!done && current_run < RUNS_TO_SKIP) { + current_run++; + continue; + } else { + current_run = 0; } + __disable_interrupts(); if(!done) { switch((lfsr & 0x0003) + 1) { case K_UP: SND_TAP(); + vdp_draw_joystick(JS_POS_UP); done = step_game(GAME_STEP_UP); - ddraw_direction_arrows(GRAPH_ARROW_UP); break; case K_DOWN: SND_TAP(); + vdp_draw_joystick(JS_POS_DOWN); done = step_game(GAME_STEP_DOWN); - ddraw_direction_arrows(GRAPH_ARROW_DOWN); break; case K_LEFT: SND_TAP(); + vdp_draw_joystick(JS_POS_LEFT); done = step_game(GAME_STEP_LEFT); - ddraw_direction_arrows(GRAPH_ARROW_LEFT); break; case K_RIGHT: SND_TAP(); + vdp_draw_joystick(JS_POS_RIGHT); done = step_game(GAME_STEP_RIGHT); - ddraw_direction_arrows(GRAPH_ARROW_RIGHT); break; default: + __enable_interrupts(); continue; // Do nothing, loop again } } @@ -113,15 +146,19 @@ void main(void) { } // Draw the number of moves - draw_number(moves_count, MOVES_TEXT_WIDTH, MOVES_TEXT_X, MOVES_TEXT_Y); + num_to_decbuf(moves_count, 5, text_buf); + decbuf_to_ascii(5, text_buf); + vdp_print_string(0, MOVES_TEXT_X, MOVES_TEXT_Y, (char*)text_buf); // Draw the moved tiles - draw_tiles(); + vdp_redraw_tiles(get_front_grid()); // If we have won, or we got a reset request, break out of this loop if(done) { - draw_number(score, SCORE_TEXT_WIDTH, SCORE_TEXT_X, SCORE_TEXT_Y); - swap_display_buffers(); // Make sure we show the latest changes + num_to_decbuf(score, 5, text_buf); // Score + decbuf_to_ascii(5, text_buf); + vdp_print_string(0, SCORE_TEXT_X, SCORE_TEXT_Y, (char*)text_buf); + break; } @@ -132,25 +169,30 @@ void main(void) { break; } + score = calculate_score(); // Draw the score - draw_number(score, SCORE_TEXT_WIDTH, SCORE_TEXT_X, SCORE_TEXT_Y); - - swap_display_buffers(); - - // Draw the new tile directly on the front buffer, this way we make it appear with an "animation" - ddraw_single_tile(random_tile_off - 1); + num_to_decbuf(score, 5, text_buf); // Score + decbuf_to_ascii(5, text_buf); + vdp_print_string(0, SCORE_TEXT_X, SCORE_TEXT_Y, (char*)text_buf); + vdp_redraw_tiles(get_front_grid()); + vdp_draw_joystick(JS_POS_CENTER); + + __enable_interrupts(); } - - // Sync the display buffers - sync_display1_buffer(); + + __disable_interrupts(); // Always go back to the start dialog dld->mode = DLOG_MODE_START; dld->score = 0; - clear_display_buffers(); + vdp_redraw_tiles(get_front_grid()); return; } + +__attribute__((interrupt)) void irq_handler(void) { + vdp_write_interleaved_sat(); +} diff --git a/src/vdgam_main.c b/src/vdgam_main.c index 07ed8fe..bf5ca5b 100644 --- a/src/vdgam_main.c +++ b/src/vdgam_main.c @@ -45,7 +45,6 @@ void main(void) { uint16_t moves_count = 0; uint16_t score = 0; int8_t done = 0; - uint8_t read_key; __disable_interrupts(); // Make sure the interrupts are disabled