From bc3f7172858218ba16105c1cf7cf7f12e55f3273 Mon Sep 17 00:00:00 2001 From: Matthew Laux Date: Thu, 14 Jul 2022 02:11:20 -0500 Subject: [PATCH] add vram tile viewer thing --- cli/imgui_example.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/cpu.c | 8 ++++---- src/dmg.c | 2 +- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/cli/imgui_example.cpp b/cli/imgui_example.cpp index d327068..0e3c721 100644 --- a/cli/imgui_example.cpp +++ b/cli/imgui_example.cpp @@ -50,6 +50,7 @@ GLuint make_output_texture() { } unsigned char output_image[256 * 256 * 4]; +unsigned char vram_tiles[256 * 96 * 4]; void convert_output(struct lcd *lcd) { int x, y; @@ -66,6 +67,33 @@ void convert_output(struct lcd *lcd) { } } +void convert_vram(struct dmg *dmg) { + int tile_y, tile_x; + int off, in; + for (tile_y = 0; tile_y < 12; tile_y++) { + for (tile_x = 0; tile_x < 32; tile_x++) { + off = 256 * 8 * tile_y + 8 * tile_x; + in = 16 * (tile_y * 32 + tile_x); + int b, i; + for (b = 0; b < 16; b += 2) { + int data1 = dmg->video_ram[in + b]; + int data2 = dmg->video_ram[in + b + 1]; + for (i = 7; i >= 0; i--) { + // monochrome for now + int fill = (data1 & (1 << i)) ? 255 : 0; + vram_tiles[4 * off + 0] = fill; + vram_tiles[4 * off + 1] = fill; + vram_tiles[4 * off + 2] = fill; + vram_tiles[4 * off + 3] = 255; + //dmg->lcd->buf[off] |= (data2 & (1 << i)) ? 1 : 0; + off++; + } + off += 248; + } + } + } +} + char full_address_space[0x10000]; void fill_memory_editor(struct dmg *dmg) { @@ -165,6 +193,7 @@ int main(int argc, char *argv[]) // setup output GLuint texture = make_output_texture(); + GLuint vram_texture = make_output_texture(); // Our state bool z_flag = false; @@ -247,6 +276,17 @@ int main(int argc, char *argv[]) ImGui::End(); } + { + ImGui::Begin("VRAM"); + + convert_vram(&dmg); + glBindTexture(GL_TEXTURE_2D, vram_texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 96, 0, GL_RGBA, GL_UNSIGNED_BYTE, vram_tiles); + ImGui::Image((void*)(intptr_t) vram_texture, ImVec2(256, 96)); + + ImGui::End(); + } + fill_memory_editor(&dmg); editor.DrawWindow("Memory", full_address_space, 0x10000, 0x0000); diff --git a/src/cpu.c b/src/cpu.c index e13f813..e7d77df 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -136,12 +136,12 @@ static u8 rlc(struct cpu *cpu, u8 val) static u8 rotate_right(struct cpu *regs, u8 reg) { - // copy old rightmost bit to carry flag + int old_carry = flag_isset(regs, FLAG_CARRY) << 7; + // copy old rightmost bit to carry flag, clear ZNH regs->f = (reg & 0x01) << 4; // rotate - int result = reg >> 1; - // restore rightmost bit to left - result |= (regs->f & FLAG_CARRY) << 3; + int result = old_carry | reg >> 1; + if (!result) set_flag(regs, FLAG_ZERO); return result; } diff --git a/src/dmg.c b/src/dmg.c index 3a9f91b..6463bbe 100644 --- a/src/dmg.c +++ b/src/dmg.c @@ -92,7 +92,7 @@ void dmg_step(void *_dmg) int use_unsigned = lcdc & LCDC_BG_TILE_DATA; int tilebase = use_unsigned ? 0x8000 : 0x9000; - printf("bg_base %04x, tilebase %04x\n", bg_base, tilebase); + printf("tile map: %04x, tile data: %04x\n", bg_base, tilebase); int k = 0, off = 0; int tile_y = 0, tile_x = 0;