From f4295898e04de8ef2878aac1774dc7ccda501317 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Wed, 22 Mar 2023 19:58:15 +0100 Subject: load static/tilemap.bin in ppusim --- src/ppu/consts.h | 6 ++++++ src/ppu/ppu.c | 24 ++++++++++++++++++++++++ src/ppu/ppu.h | 3 +++ src/ppusim/sim.c | 32 ++++++++++---------------------- src/ppusim/sim.h | 4 ++-- 5 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/ppu/consts.h b/src/ppu/consts.h index a27b7b7..d7caf5e 100644 --- a/src/ppu/consts.h +++ b/src/ppu/consts.h @@ -55,3 +55,9 @@ #define HH_PPU_VRAM_AUX_OFFSET ((hh_ppu_addr_t) 0xde00) /** @brief auxiliary memory size in words */ #define HH_PPU_VRAM_AUX_SIZE ((hh_ppu_addr_t) 0x0002) + +/** @brief sprite size in bytes */ +#define HH_PPU_BYTE_SPRITE_SIZE (HH_PPU_VRAM_TMM_SPRITE_SIZE * sizeof(hh_ppu_data_t)) +/** @brief sprite size in 32-bit words (for stm) */ +#define HH_PPU_NATIVE_SPRITE_SIZE (HH_PPU_BYTE_SPRITE_SIZE / sizeof(uint32_t)) + diff --git a/src/ppu/ppu.c b/src/ppu/ppu.c index df14b9f..1f46b16 100644 --- a/src/ppu/ppu.c +++ b/src/ppu/ppu.c @@ -3,6 +3,7 @@ #include "ppu/consts.h" #include "ppu/internals.h" #include "ppu/ppu.h" +#include "tilemap.h" 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); @@ -46,3 +47,26 @@ 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_load_tilemap() { + for (size_t sprite_idx = 0; sprite_idx < HH_TM_SIZE; sprite_idx++) { + for (size_t word_idx = 0; word_idx < HH_PPU_NATIVE_SPRITE_SIZE; word_idx++) { + uint32_t native_word = g_hh_tilemap_rom[word_idx + sprite_idx * HH_PPU_NATIVE_SPRITE_SIZE]; + uint32_t native_word_bytes[4] = { // TODO: check endianness of stm32 + (native_word >> 24) & 0xff, + (native_word >> 16) & 0xff, + (native_word >> 8) & 0xff, + (native_word >> 0) & 0xff, + }; + uint16_t ppu_words[2] = { + (native_word_bytes[2] << 8) | (native_word_bytes[3] << 0), + (native_word_bytes[0] << 8) | (native_word_bytes[1] << 0), + }; + hh_ppu_vram_write((hh_s_ppu_vram_data) { + .data = ppu_words, + .offset = HH_PPU_VRAM_TMM_OFFSET + sprite_idx * HH_PPU_VRAM_TMM_SPRITE_SIZE + word_idx * 2, + .size = 2, + }); + } + } +} diff --git a/src/ppu/ppu.h b/src/ppu/ppu.h index 75d97c1..46b58af 100644 --- a/src/ppu/ppu.h +++ b/src/ppu/ppu.h @@ -23,3 +23,6 @@ void hh_ppu_update_palette_table(hh_ppu_loc_palette_table_t table); void hh_ppu_update_palette(unsigned palette_index, hh_ppu_loc_palette_data_t palette); /** @brief update single color in palette */ void hh_ppu_update_color(unsigned palette_index, unsigned color_index, hh_ppu_rgb_color_t color); + +/** @brief copy g_hh_tilemap_rom into PPU vram */ +void hh_ppu_load_tilemap(); diff --git a/src/ppusim/sim.c b/src/ppusim/sim.c index 4d171a0..0f8d687 100644 --- a/src/ppusim/sim.c +++ b/src/ppusim/sim.c @@ -27,39 +27,27 @@ void hh_ppu_init() { g_hh_ppusim_vram = malloc(sizeof(hh_ppu_data_t) * 0xffff); memset(g_hh_ppusim_vram, 0x0000, 0xffff); + hh_ppusim_load_tilemap(); + hh_ppu_load_tilemap(); } -void hh_ppu_load_tilemap() { - g_hh_tilemap_rom = NULL; // TODO: read/copy file (static/tilemap.bin) - //TODO: remove magic file name here - char* filename = "static/tiles.bin"; - FILE* fp = fopen(filename,"rb"); - if (!fp){ - fprintf(stderr,"Error: Failed to load tiles."); - return;//error - } - int sprite_size = (HH_PPU_SPRITE_WIDTH * HH_PPU_SPRITE_HEIGHT); - fseek(fp, 0, SEEK_END);//goto EOF - int _size = ftell(fp)/sprite_size; - fseek(fp, 0, 0);//goto start of file +void hh_ppusim_load_tilemap() { + FILE* fp = fopen("static/tilemap.bin", "rb"); - for (int i = 0; i < _size; i++) { - uint8_t data[sprite_size]; + fseek(fp, 0, SEEK_END); + size_t file_size = ftell(fp); + rewind(fp); - fread(data,sizeof(uint8_t),sprite_size,fp); - - hh_s_ppu_vram_data sprite = hh_ppu_2nat_sprite(data); - sprite.offset = i*HH_PPU_VRAM_TMM_SPRITE_SIZE; - hh_ppu_vram_write(sprite); - free(sprite.data); - } + g_hh_tilemap_rom = malloc(file_size); + fread(g_hh_tilemap_rom, file_size, 1, fp); fclose(fp); } void hh_ppu_deinit() { free(g_hh_ppusim_threads); free(g_hh_ppusim_vram); + free(g_hh_tilemap_rom); SDL_DestroyRenderer(g_hh_renderer); SDL_DestroyWindow(g_hh_window); diff --git a/src/ppusim/sim.h b/src/ppusim/sim.h index 4d1d718..218d61d 100644 --- a/src/ppusim/sim.h +++ b/src/ppusim/sim.h @@ -5,5 +5,5 @@ /** @brief max framerate for PPUSIM */ #define HH_PPUSIM_FRAMERATE 60 -/** @brief pump tilemap from rom to ppu ram */ -void hh_ppu_load_tilemap(); //ppu sim? +/** @brief load static/tilemap.bin into memory */ +void hh_ppusim_load_tilemap(); -- cgit v1.2.3