From 77e153f99cb279c1deb5b257fb54c7fdbdd4bc19 Mon Sep 17 00:00:00 2001 From: Daniel Hader Date: Wed, 26 Aug 2026 21:38:21 -0500 Subject: fixed screen entries being set incorrectly --- source/main.cpp | 18 ++++++++--------- source/util.hpp | 62 +++++++++++++++++++++++++++++---------------------------- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index ec6be22..21d1c26 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -2,8 +2,6 @@ #include "util.hpp" #include "input.hpp" -ScreenEntry *bg0_map = screenblock_mem[28]; - void bad_vsync() { while (*REG_VCOUNT >= 160); while (*REG_VCOUNT < 160); @@ -12,7 +10,7 @@ void bad_vsync() { int main() { auto bg_flags = BGControlFlags() .set_charblock_base(0) - .set_screenblock_base(0) + .set_screenblock_base(28) .set_size(BGControlSize::Regular_64x64); set_bg_control(0, bg_flags); @@ -26,15 +24,17 @@ int main() { set_tile(0, 0, tiles[0]); set_tile(0, 1, tiles[1]); - set_bg_palette(0, 1, Color::from_rgb(31, 0, 0)); - set_bg_palette(1, 1, Color::from_rgb(31, 0, 0)); - set_bg_palette(2, 1, Color::from_rgb(31, 0, 0)); - set_bg_palette(3, 1, Color::from_rgb(31, 0, 0)); + set_bg_palette(0, 1, Color::from_rgb(31, 0, 0)); + set_bg_palette(1, 1, Color::from_rgb( 0, 31, 0)); + set_bg_palette(2, 1, Color::from_rgb( 0, 0, 31)); + set_bg_palette(3, 1, Color::from_rgb(16, 16, 16)); - ScreenEntry *screen_entry_ptr = bg0_map; for (int i = 0; i < 4; i++) { for (int j = 0; j < 32*32; j++) { - *screen_entry_ptr++ = ScreenEntry().set_palette_bank(i); + auto screen_entry = ScreenEntry() + .set_tile_index(i % 2) + .set_palette_bank(i); + set_screen_entry(28+i, j, screen_entry); } } diff --git a/source/util.hpp b/source/util.hpp index 7b39376..88a29ca 100644 --- a/source/util.hpp +++ b/source/util.hpp @@ -67,7 +67,7 @@ struct BGControlFlags { constexpr BGControlFlags &set_charblock_base(u16 block) { assert(block < 4); - this->flags = (this->flags & 0xfff3) | (block << 2); + this->flags = (this->flags & 0x000c) | (block << 2); return *this; } @@ -83,7 +83,7 @@ struct BGControlFlags { constexpr BGControlFlags &set_screenblock_base(u16 block) { assert(block < 32); - this->flags = (this->flags & 0xe0ff) | (block << 8); + this->flags = (this->flags & ~0x1f00) | (block << 8); return *this; } @@ -93,7 +93,7 @@ struct BGControlFlags { } constexpr BGControlFlags &set_size(BGControlSize size) { - this->flags = (this->flags & 0x3fff) | size; + this->flags = (this->flags & ~0xc000) | size; return *this; } @@ -129,34 +129,11 @@ typedef Color PaletteBank[16]; struct Tile4 { u32 data[8]; }; typedef Tile4 CharBlock4[512]; -template -static inline void volatile_copy(volatile T* dst, const T* src) { - using U = std::conditional_t<(sizeof(T) % sizeof(u32) == 0) && (alignof(T) % alignof(u32) == 0), u32, u16>; - static_assert(sizeof(T) % sizeof(U) == 0); - static_assert(alignof(T) % alignof(U) == 0); - auto *d = reinterpret_cast(dst); - auto *s = reinterpret_cast(src); - for (unsigned i = 0; i < sizeof(T) / sizeof(U); i++) { - d[i] = s[i]; - } -} - -static inline void set_tile(u16 block, u16 index, const Tile4 &tile) { - // TODO bounds check - volatile CharBlock4 *const blocks = reinterpret_cast(0x06000000); - volatile_copy(&blocks[block][index], &tile); -} - -static inline void set_bg_palette(u32 bank, u32 index, const Color &color) { - volatile PaletteBank *const banks = reinterpret_cast(0x05000000); - volatile_copy(&banks[bank][index], &color); -} - -// pair of screen entry bytes struct ScreenEntry { u16 data; constexpr ScreenEntry() : data(0) {} + constexpr ScreenEntry &set_tile_index(u16 tid) { assert(tid < 1024); this->data = (this->data & ~0x03ff) | tid; @@ -184,8 +161,33 @@ struct ScreenEntry { typedef ScreenEntry ScreenBlock[1024]; -CharBlock4 *const tile4_mem = reinterpret_cast(0x06000000); -PaletteBank *const palette_bg_bank = reinterpret_cast(0x05000000); -ScreenBlock *const screenblock_mem = reinterpret_cast(0x06000000); +template +static inline void volatile_copy(volatile T* dst, const T* src) { + using U = std::conditional_t<(sizeof(T) % sizeof(u32) == 0) && (alignof(T) % alignof(u32) == 0), u32, u16>; + static_assert(sizeof(T) % sizeof(U) == 0); + static_assert(alignof(T) % alignof(U) == 0); + auto *d = reinterpret_cast(dst); + auto *s = reinterpret_cast(src); + for (unsigned i = 0; i < sizeof(T) / sizeof(U); i++) { + d[i] = s[i]; + } +} + +static inline void set_tile(u16 block, u16 index, const Tile4 &tile) { + // TODO bounds check + volatile CharBlock4 *const blocks = reinterpret_cast(0x06000000); + volatile_copy(&blocks[block][index], &tile); +} + +static inline void set_bg_palette(u32 bank, u32 index, const Color &color) { + volatile PaletteBank *const banks = reinterpret_cast(0x05000000); + volatile_copy(&banks[bank][index], &color); +} + + +static inline void set_screen_entry(u32 block, u32 index, const ScreenEntry &entry) { + volatile ScreenBlock *const blocks = reinterpret_cast(0x06000000); + volatile_copy(&blocks[block][index], &entry); +} #endif -- cgit v1.2.3