~groovestomp/gsnes

34a7bb87d9658166f74fbe502c79b3635b28dcf7 — GrooveStomp 1 year, 6 months ago e10e27c
Add CPU debug state for rendering
5 files changed, 76 insertions(+), 12 deletions(-)

M cpu.c
M cpu.h
M graphics.c
M graphics.h
M main.c
M cpu.c => cpu.c +43 -0
@@ 1567,3 1567,46 @@ struct debug_instruction_map *CpuDisassemble(struct cpu *cpu, uint16_t start, ui

        return debug_map;
}

char **CpuDebugState(struct cpu *cpu) {
        char **debug = (char **)malloc(sizeof(char *) * 7);

        debug[0] = "        N V - B D I Z C";
        debug[1] = malloc(strlen("status: 1 1 - 1 1 1 1 1") + 1);
        sprintf(debug[1], "Status: %d %d - %d %d %d %d %d",
                GetFlag(cpu, N),
                GetFlag(cpu, V),
                GetFlag(cpu, B),
                GetFlag(cpu, D),
                GetFlag(cpu, I),
                GetFlag(cpu, Z),
                GetFlag(cpu, C));

        debug[2] = malloc(strlen("pc: $0000") + 1);
        sprintf(debug[2], "PC: $%04X", cpu->pc);

        debug[3] = malloc(strlen("a:  $00") + 1);
        sprintf(debug[3], "A:  $%02X", cpu->a);

        debug[4] = malloc(strlen("x:  $00") + 1);
        sprintf(debug[4], "X:  $%02X", cpu->x);

        debug[5] = malloc(strlen("y:  $00") + 1);
        sprintf(debug[5], "Y:  $%02X", cpu->y);

        debug[6] = malloc(strlen("sp: $0000") + 1);
        sprintf(debug[6], "SP: $%04X", cpu->sp);

        return debug;
}

void CpuDebugStateDeinit(char **debug) {
        if (NULL == debug)
                return;

        for (int i = 1; i < 7; i++) {
                if (NULL == debug[i])
                        continue;
                free(debug[i]);
        }
}

M cpu.h => cpu.h +6 -0
@@ 60,4 60,10 @@ DebugInstructionMapDeinit(struct debug_instruction_map *map);
struct debug_instruction_map *
CpuDisassemble(struct cpu *cpu, uint16_t start, uint16_t stop);

char **
CpuDebugState(struct cpu *cpu);

void
CpuDebugStateDeinit(char **debug);

#endif // CPU_VERSION

M graphics.c => graphics.c +1 -9
@@ 219,15 219,7 @@ uint32_t GraphicsGetPixel(struct graphics *graphics, int x, int y) {
        return 0x00000000;
}

//! \brief Draws a line from (x1,y1) to (x2,y2)
//!
//! \param[in,out] graphics Graphics state to be manipulated
//! \param[in] x1 horizontal position of the line start.
//! \param[in] y1 vertical position of the line start.
//! \param[in] x2 horizontal position of the line end.
//! \param[in] y2 vertical position of the line end.
//! \param[in] color color to render the line with.
void GraphicsDrawLine(struct graphics *graphics, int x1, int y1, int x2, int y2, unsigned int color) {
void GraphicsDrawLine(struct graphics *graphics, int x1, int y1, int x2, int y2, uint32_t color) {
        int dx = x2 - x1;
        int dy = y2 - y1;


M graphics.h => graphics.h +11 -0
@@ 93,4 93,15 @@ GraphicsInitText(struct graphics *graphics, unsigned char *ttfBuffer);
void
GraphicsDrawText(struct graphics *graphics, int x, int y, char *string, int fontHeight, uint32_t color);

//! \brief Draws a line from (x1,y1) to (x2,y2)
//!
//! \param[in,out] graphics Graphics state to be manipulated
//! \param[in] x1 horizontal position of the line start
//! \param[in] y1 vertical position of the line start
//! \param[in] x2 horizontal position of the line end
//! \param[in] y2 vertical position of the line end
//! \param[in] color 32-bit color (R|G|B|A) to render line with
void
GraphicsDrawLine(struct graphics *graphics, int x1, int y1, int x2, int y2, uint32_t color);

#endif // GRAPHICS_VERSION

M main.c => main.c +15 -3
@@ 82,7 82,7 @@ void Deinit(int code) {
}

void Init() {
        char *ttf_filename = "/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf";
        char *ttf_filename = "/usr/share/fonts/truetype/ubuntu/UbuntuMono-R.ttf";

        cpu = CpuInit();
        if (NULL == cpu) {


@@ 161,6 161,7 @@ int main(int argc, char **argv) {
        // Disassemble
        struct debug_instruction_map *map = CpuDisassemble(cpu, 0x0000, 0x000F);

        // TODO: Render text for disassembly in while loop below
        /* for (int i = 0; i < map->count; i++) { */
        /*         printf("map{%p}", (void *)map); */
        /*         if (NULL != map) { */


@@ 180,10 181,21 @@ int main(int argc, char **argv) {
        int running = 1;
        while (running) {
                GraphicsBegin(graphics);
                GraphicsClearScreen(graphics, 0x000000FF);
                GraphicsDrawText(graphics, 50, 50, "Hello", 40, 0xFFFFFFFF);
                GraphicsClearScreen(graphics, 0xFFFFFFFF);
                GraphicsDrawLine(graphics, 801, 0, 801, 800, 0x000000FF);

                GraphicsDrawText(graphics, 805, 780, "CPU State", 25, 0x000000FF);
                char **cpu_state = CpuDebugState(cpu);
                for (int i = 0; i < 7; i++) {
                        GraphicsDrawText(graphics, 805, 760 - (18 * i), cpu_state[i], 15, 0x000000FF);
                }
                CpuDebugStateDeinit(cpu_state);

                GraphicsDrawLine(graphics, 801, 630, 1200, 630, 0x000000FF);

                GraphicsEnd(graphics);

                // TODO: Hookup input with progressing emulation
                InputProcess(input);
                running = !InputIsQuitRequested(input);