diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-04-04 20:44:15 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-04-04 20:44:15 +0200 |
commit | 4881723cf765fbd7bad2a1f08baf5897a7425401 (patch) | |
tree | 96b828de8f27b8bb39a9c3f0aa31166552f99b41 /src/ppu | |
parent | 47bf3627e4f44fa5e8095af0f70d5fccdb0f8cae (diff) |
WIP DMA SPI TX
Diffstat (limited to 'src/ppu')
-rw-r--r-- | src/ppu/internals.c | 24 | ||||
-rw-r--r-- | src/ppu/internals.h | 3 | ||||
-rw-r--r-- | src/ppu/ppu.c | 21 | ||||
-rw-r--r-- | src/ppu/ppu.h | 6 | ||||
-rw-r--r-- | src/ppu/stm.c | 10 |
5 files changed, 47 insertions, 17 deletions
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); } |