From 4881723cf765fbd7bad2a1f08baf5897a7425401 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 4 Apr 2023 20:44:15 +0200 Subject: WIP DMA SPI TX --- src/demo.c | 12 +++--------- src/main.c | 3 +-- src/ppu/internals.c | 24 ++++++++++++++++++------ src/ppu/internals.h | 3 +++ src/ppu/ppu.c | 21 ++++++++++++++++----- src/ppu/ppu.h | 6 ++++-- src/ppu/stm.c | 10 ++++++---- src/ppusim/mem.c | 2 +- src/readme.md | 9 +++++++++ src/stm32/setup.c | 2 +- 10 files changed, 62 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/demo.c b/src/demo.c index c4207c7..ecfc79f 100644 --- a/src/demo.c +++ b/src/demo.c @@ -7,14 +7,13 @@ hh_s_ppu_loc_fam_entry g_hh_demo_balls[HH_DEMO_BALL_COUNT]; void hh_demo_setup() { - hh_ppu_update_aux((hh_s_ppu_loc_aux) { .sysreset = 1 }); - // load sprites hh_ppu_update_sprite(0, HH_DBG_SPRITE_BALL); hh_ppu_update_sprite(1, HH_DBG_SPRITE_CHECKERBOARD); // background pattern - hh_ppu_update_color(0, 1, (hh_ppu_rgb_color_t) {0x4, 0x4, 0x4}); + hh_ppu_update_color(0, 0, (hh_ppu_rgb_color_t) {0x1, 0x1, 0x3}); + hh_ppu_update_color(0, 1, (hh_ppu_rgb_color_t) {0x4, 0x4, 0x6}); for (unsigned i = 0; i < HH_PPU_BG_CANVAS_TILES_H * HH_PPU_BG_CANVAS_TILES_V; i++) { hh_ppu_update_background(i, (hh_s_ppu_loc_bam_entry) { .horizontal_flip = false, @@ -42,12 +41,7 @@ void hh_demo_setup() { void hh_demo_loop(unsigned long frame) { // set background pattern position - hh_ppu_update_aux((hh_s_ppu_loc_aux) { - .bg_shift_x = (frame / 2) % HH_PPU_SPRITE_WIDTH, - .bg_shift_y = (frame / 8) % HH_PPU_SPRITE_HEIGHT, - .fg_fetch = 0, - .sysreset = 0, - }); + hh_ppu_update_background_pos((frame / 5) % HH_PPU_SPRITE_WIDTH, (frame / 20) % HH_PPU_SPRITE_HEIGHT); for (unsigned i = 0; i < HH_DEMO_BALL_COUNT; i++) { g_hh_demo_balls[i].position_x = HH_PPU_SCREEN_WIDTH/2 - HH_PPU_SPRITE_WIDTH/2 + (int)(60 * (double)sin((1*(double)frame / 10) + (double)i * 12)); diff --git a/src/main.c b/src/main.c index 67490ab..3cb045d 100644 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,6 @@ #include "main.h" #include "demo.h" #include "ppu/ppu.h" -#include "ppu/internals.h" bool g_hh_run = true; volatile unsigned long g_hh_vcount; @@ -12,7 +11,7 @@ volatile unsigned short g_hh_hcount; void hh_ppu_vblank_interrupt() { static unsigned long frame = 0; hh_demo_loop(frame++); - hh_ppu_vram_flush(); + hh_ppu_flush(); } int main() { diff --git a/src/ppu/internals.c b/src/ppu/internals.c index a60cb68..2d90665 100644 --- a/src/ppu/internals.c +++ b/src/ppu/internals.c @@ -4,8 +4,9 @@ #include "ppu/internals.h" #include "ppu/types.h" -uint8_t g_hh_ppu_vram_buffer[HH_PPU_COMMAND_BUFFER_SIZE] = { 0 }; -size_t g_hh_ppu_vram_buffer_head = 0; +uint8_t g_hh_ppu_vram_buffer[HH_PPU_COMMAND_BUFFER_SIZE * 2] = { 0 }; +uint8_t* g_hh_ppu_vram_buffer_ptr = g_hh_ppu_vram_buffer; +size_t g_hh_ppu_vram_buffer_size = 0; bool hh_ppu_vram_valid_address(hh_ppu_addr_t addr) { #pragma GCC diagnostic push @@ -19,6 +20,13 @@ bool hh_ppu_vram_valid_address(hh_ppu_addr_t addr) { return false; } +void hh_ppu_update_aux(hh_s_ppu_loc_aux aux) { + hh_s_ppu_vram_data a = hh_ppu_2nat_aux(aux); + a.offset = HH_PPU_VRAM_AUX_OFFSET; + hh_ppu_vram_write(a); + free(a.data); +} + void hh_ppu_vram_write(hh_s_ppu_vram_data data) { for (unsigned i = 0; i < data.size; i++) { hh_ppu_addr_t ppu_addr = data.offset + i; @@ -87,16 +95,20 @@ hh_s_ppu_vram_data hh_ppu_2nat_color(hh_ppu_rgb_color_t rgb) { } void hh_ppu_vram_buffer(uint8_t data[4]) { - size_t head = g_hh_ppu_vram_buffer_head; + size_t head = g_hh_ppu_vram_buffer_size; g_hh_ppu_vram_buffer[head+0] = data[0]; g_hh_ppu_vram_buffer[head+1] = data[1]; g_hh_ppu_vram_buffer[head+2] = data[2]; g_hh_ppu_vram_buffer[head+3] = data[3]; - g_hh_ppu_vram_buffer_head += 4; + g_hh_ppu_vram_buffer_size += 4; } void hh_ppu_vram_flush() { - hh_ppu_vram_dwrite(g_hh_ppu_vram_buffer, g_hh_ppu_vram_buffer_head); - g_hh_ppu_vram_buffer_head = 0; + static bool buffer_swap = false; + buffer_swap = !buffer_swap; + hh_ppu_vram_buffer((uint8_t[4]){ 0xff, 0xff, 0xff, 0xff }); + hh_ppu_vram_dwrite(g_hh_ppu_vram_buffer_ptr, g_hh_ppu_vram_buffer_size); + g_hh_ppu_vram_buffer_size = 0; + g_hh_ppu_vram_buffer_ptr = g_hh_ppu_vram_buffer + (buffer_swap * HH_PPU_COMMAND_BUFFER_SIZE); } diff --git a/src/ppu/internals.h b/src/ppu/internals.h index 12c556e..1e5ce02 100644 --- a/src/ppu/internals.h +++ b/src/ppu/internals.h @@ -29,6 +29,9 @@ void hh_ppu_vram_flush(); /** @brief write raw spi bytes in ppu format */ void hh_ppu_vram_dwrite(uint8_t* data, size_t size); +/** @brief update aux register */ +void hh_ppu_update_aux(hh_s_ppu_loc_aux aux); + /** @brief convert local background attribute memory entry to PPU format */ hh_s_ppu_vram_data hh_ppu_2nat_bam(hh_s_ppu_loc_bam_entry); /** @brief convert local foreground attribute memory entry to PPU format */ diff --git a/src/ppu/ppu.c b/src/ppu/ppu.c index df14b9f..e32c188 100644 --- a/src/ppu/ppu.c +++ b/src/ppu/ppu.c @@ -4,6 +4,8 @@ #include "ppu/internals.h" #include "ppu/ppu.h" +hh_s_ppu_loc_aux g_hh_aux = { 0 }; + void hh_ppu_update_foreground(unsigned index, hh_s_ppu_loc_fam_entry e) { hh_s_ppu_vram_data s = hh_ppu_2nat_fam(e); s.offset = HH_PPU_VRAM_FAM_OFFSET + HH_PPU_VRAM_FAM_ENTRY_SIZE * index; @@ -19,17 +21,16 @@ void hh_ppu_update_background(unsigned index, hh_s_ppu_loc_bam_entry e) { } void hh_ppu_update_sprite(unsigned tilemap_index, const hh_s_ppu_loc_sprite sprite) { + if (tilemap_index < 16) g_hh_aux.fg_fetch = true; hh_s_ppu_vram_data s = hh_ppu_2nat_sprite(sprite); s.offset = HH_PPU_VRAM_TMM_OFFSET + HH_PPU_VRAM_TMM_SPRITE_SIZE * tilemap_index; hh_ppu_vram_write(s); free(s.data); } -void hh_ppu_update_aux(hh_s_ppu_loc_aux aux) { - hh_s_ppu_vram_data a = hh_ppu_2nat_aux(aux); - a.offset = HH_PPU_VRAM_AUX_OFFSET; - hh_ppu_vram_write(a); - free(a.data); +void hh_ppu_update_background_pos(uint16_t shift_x, uint16_t shift_y) { + g_hh_aux.bg_shift_x = shift_x; + g_hh_aux.bg_shift_y = shift_y; } void hh_ppu_update_palette_table(hh_ppu_loc_palette_table_t table) { @@ -46,3 +47,13 @@ void hh_ppu_update_color(unsigned palette_index, unsigned color_index, hh_ppu_rg hh_ppu_vram_write(c); free(c.data); } + +void hh_ppu_flush() { + hh_ppu_update_aux(g_hh_aux); + if (g_hh_aux.fg_fetch) { + g_hh_aux.fg_fetch = false; + hh_ppu_update_aux(g_hh_aux); + } + hh_ppu_vram_flush(); +} + diff --git a/src/ppu/ppu.h b/src/ppu/ppu.h index 75d97c1..3210418 100644 --- a/src/ppu/ppu.h +++ b/src/ppu/ppu.h @@ -8,13 +8,15 @@ void hh_ppu_vblank_interrupt(); void hh_ppu_init(); /** @brief deinitialize ppu interface */ void hh_ppu_deinit(); +/** @brief flush ppu buffer */ +void hh_ppu_flush(); /** @brief update single foreground sprite */ void hh_ppu_update_foreground(unsigned index, hh_s_ppu_loc_fam_entry e); /** @brief update single background sprite */ void hh_ppu_update_background(unsigned index, hh_s_ppu_loc_bam_entry e); -/** @brief update aux register */ -void hh_ppu_update_aux(hh_s_ppu_loc_aux aux); +/** @brief update background shift */ +void hh_ppu_update_background_pos(uint16_t shift_x, uint16_t shift_y); /** @brief update single sprite */ void hh_ppu_update_sprite(unsigned tilemap_index, const hh_s_ppu_loc_sprite sprite); /** @brief update entire palette table */ diff --git a/src/ppu/stm.c b/src/ppu/stm.c index d588a65..6bd9f66 100644 --- a/src/ppu/stm.c +++ b/src/ppu/stm.c @@ -4,14 +4,16 @@ #include "ppu/internals.h" #include "stm32/setup.h" -void hh_ppu_init() {} +void hh_ppu_init() { + hh_ppu_update_aux((hh_s_ppu_loc_aux) { .sysreset = 1 }); +} + void hh_ppu_deinit() {} void hh_ppu_vram_dwrite(uint8_t* data, size_t size) { - HAL_SPI_Transmit(&hspi1, data, size, HAL_MAX_DELAY); - HAL_SPI_Transmit(&hspi1, (uint8_t[4]){ 0xff }, 4, HAL_MAX_DELAY); - // reset SPI HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); + HAL_SPI_Transmit_IT(&hspi1, data, size); + // HAL_SPI_Transmit_IT(&hspi1, (uint8_t[4]){ 0xff }, 4); } diff --git a/src/ppusim/mem.c b/src/ppusim/mem.c index 06eba4b..f536727 100644 --- a/src/ppusim/mem.c +++ b/src/ppusim/mem.c @@ -11,7 +11,7 @@ void hh_ppu_vram_dwrite(uint8_t* data, size_t size) { if (i+4 > size) break; hh_ppu_addr_t ppu_addr = (data[i+0] << 8) | (data[i+1] << 0); hh_ppu_data_t ppu_data = (data[i+2] << 8) | (data[i+3] << 0); - printf("%04x: %04x\n", ppu_addr, ppu_data); + // printf("%04x: %04x\n", ppu_addr, ppu_data); if (!hh_ppu_vram_valid_address(ppu_addr)) continue; g_hh_ppusim_vram[ppu_addr] = ppu_data; } diff --git a/src/readme.md b/src/readme.md index 45e6c16..7b8f11b 100644 --- a/src/readme.md +++ b/src/readme.md @@ -21,3 +21,12 @@ |ppu|PPU interface functions| |static|static game assets (binary files)| |stm32|STM32 specific setup (HAL, FreeRTOS)| + +## others + +### debugging command + +```bash +st-util & arm-none-eabi-gdb ./main.elf -ex 'target remote localhost:4242' -ex 'continue' +``` + diff --git a/src/stm32/setup.c b/src/stm32/setup.c index f692159..dc8d23e 100644 --- a/src/stm32/setup.c +++ b/src/stm32/setup.c @@ -150,7 +150,7 @@ void hh_io_gpio_setup() { gpio_init(HH_IO_SPI_SR_PORT, HH_IO_SPI_SR_PIN, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL); // PPU HBLANK/VBLANK - gpio_init(HH_IO_PPU_HBLANK_PORT, HH_IO_PPU_HBLANK_PIN, GPIO_MODE_IT_RISING, GPIO_NOPULL); + gpio_init(HH_IO_PPU_HBLANK_PORT, HH_IO_PPU_HBLANK_PIN, GPIO_MODE_INPUT, GPIO_NOPULL); gpio_init(HH_IO_PPU_VBLANK_PORT, HH_IO_PPU_VBLANK_PIN, GPIO_MODE_IT_RISING, GPIO_NOPULL); HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0); HAL_NVIC_EnableIRQ(EXTI4_15_IRQn); -- cgit v1.2.3