From 33a660aef8592fc087c76e0c5abc9ceb1f2c2363 Mon Sep 17 00:00:00 2001 From: neauoire Date: Wed, 7 Apr 2021 17:42:30 -0700 Subject: [PATCH] Moved some extra things into PPU --- src/emulator.c | 50 +++----------------------------------------------- src/ppu.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/ppu.h | 18 ++++++++---------- 3 files changed, 61 insertions(+), 57 deletions(-) diff --git a/src/emulator.c b/src/emulator.c index 72270be..226f372 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -20,24 +20,6 @@ int initapu(Uxn *u, Uint8 id); static Ppu screen; -Uint8 font[][8] = { - {0x00, 0x3c, 0x46, 0x4a, 0x52, 0x62, 0x3c, 0x00}, - {0x00, 0x18, 0x28, 0x08, 0x08, 0x08, 0x3e, 0x00}, - {0x00, 0x3c, 0x42, 0x02, 0x3c, 0x40, 0x7e, 0x00}, - {0x00, 0x3c, 0x42, 0x1c, 0x02, 0x42, 0x3c, 0x00}, - {0x00, 0x08, 0x18, 0x28, 0x48, 0x7e, 0x08, 0x00}, - {0x00, 0x7e, 0x40, 0x7c, 0x02, 0x42, 0x3c, 0x00}, - {0x00, 0x3c, 0x40, 0x7c, 0x42, 0x42, 0x3c, 0x00}, - {0x00, 0x7e, 0x02, 0x04, 0x08, 0x10, 0x10, 0x00}, - {0x00, 0x3c, 0x42, 0x3c, 0x42, 0x42, 0x3c, 0x00}, - {0x00, 0x3c, 0x42, 0x42, 0x3e, 0x02, 0x3c, 0x00}, - {0x00, 0x3c, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x00}, - {0x00, 0x7c, 0x42, 0x7c, 0x42, 0x42, 0x7c, 0x00}, - {0x00, 0x3c, 0x42, 0x40, 0x40, 0x42, 0x3c, 0x00}, - {0x00, 0x78, 0x44, 0x42, 0x42, 0x44, 0x78, 0x00}, - {0x00, 0x7e, 0x40, 0x7c, 0x40, 0x40, 0x7e, 0x00}, - {0x00, 0x7e, 0x40, 0x40, 0x7c, 0x40, 0x40, 0x00}}; - static SDL_Window *gWindow; static SDL_Renderer *gRenderer; static SDL_Texture *gTexture; @@ -67,8 +49,6 @@ getflag(Uint8 *a, char flag) return *a & flag; } -#pragma mark - Helpers - #pragma mark - Core int @@ -78,25 +58,6 @@ error(char *msg, const char *err) return 0; } -void -drawdebugger(Uint32 *dst, Uxn *u) -{ - Uint8 i, x, y, b; - for(i = 0; i < 0x10; ++i) { /* memory */ - x = ((i % 8) * 3 + 3) * 8, y = screen.x1 + 8 + i / 8 * 8, b = u->wst.dat[i]; - drawicn(&screen, x, y, font[(b >> 4) & 0xf], 1 + (u->wst.ptr == i), 0); - drawicn(&screen, x + 8, y, font[b & 0xf], 1 + (u->wst.ptr == i), 0); - } - for(x = 0; x < 32; ++x) { - drawpixel(&screen, x, HEIGHT / 2, 2); - drawpixel(&screen, WIDTH - x, HEIGHT / 2, 2); - drawpixel(&screen, WIDTH / 2, HEIGHT - x, 2); - drawpixel(&screen, WIDTH / 2, x, 2); - drawpixel(&screen, WIDTH / 2 - 16 + x, HEIGHT / 2, 2); - drawpixel(&screen, WIDTH / 2, HEIGHT / 2 - 16 + x, 2); - } -} - void redraw(Uint32 *dst, Uxn *u) { @@ -108,7 +69,7 @@ redraw(Uint32 *dst, Uxn *u) drawchr(&screen, (x + PAD) * 8, (y + PAD) * 8, &screen.fg[key], 1); } if(screen.debugger) - drawdebugger(dst, u); + drawdebugger(&screen, u->wst.dat, u->wst.ptr); SDL_UpdateTexture(gTexture, NULL, dst, WIDTH * sizeof(Uint32)); SDL_RenderClear(gRenderer); SDL_RenderCopy(gRenderer, gTexture, NULL, NULL); @@ -159,15 +120,10 @@ init(void) gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, WIDTH, HEIGHT); if(gTexture == NULL) return error("Texture", SDL_GetError()); - if(!(screen.output = (Uint32 *)malloc(WIDTH * HEIGHT * sizeof(Uint32)))) - return error("Pixels", "Failed to allocate memory"); - clear(&screen); SDL_StartTextInput(); SDL_ShowCursor(SDL_DISABLE); - screen.x1 = PAD * 8; - screen.x2 = WIDTH - PAD * 8 - 1; - screen.y1 = PAD * 8; - screen.y2 = HEIGHT - PAD * 8 - 1; + if(!initppu(&screen)) + return error("PPU", "Init failure"); return 1; } diff --git a/src/ppu.c b/src/ppu.c index 607bcdd..cd56581 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -12,6 +12,24 @@ WITH REGARD TO THIS SOFTWARE. #include "ppu.h" +Uint8 font[][8] = { + {0x00, 0x3c, 0x46, 0x4a, 0x52, 0x62, 0x3c, 0x00}, + {0x00, 0x18, 0x28, 0x08, 0x08, 0x08, 0x3e, 0x00}, + {0x00, 0x3c, 0x42, 0x02, 0x3c, 0x40, 0x7e, 0x00}, + {0x00, 0x3c, 0x42, 0x1c, 0x02, 0x42, 0x3c, 0x00}, + {0x00, 0x08, 0x18, 0x28, 0x48, 0x7e, 0x08, 0x00}, + {0x00, 0x7e, 0x40, 0x7c, 0x02, 0x42, 0x3c, 0x00}, + {0x00, 0x3c, 0x40, 0x7c, 0x42, 0x42, 0x3c, 0x00}, + {0x00, 0x7e, 0x02, 0x04, 0x08, 0x10, 0x10, 0x00}, + {0x00, 0x3c, 0x42, 0x3c, 0x42, 0x42, 0x3c, 0x00}, + {0x00, 0x3c, 0x42, 0x42, 0x3e, 0x02, 0x3c, 0x00}, + {0x00, 0x3c, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x00}, + {0x00, 0x7c, 0x42, 0x7c, 0x42, 0x42, 0x7c, 0x00}, + {0x00, 0x3c, 0x42, 0x40, 0x40, 0x42, 0x3c, 0x00}, + {0x00, 0x78, 0x44, 0x42, 0x42, 0x44, 0x78, 0x00}, + {0x00, 0x7e, 0x40, 0x7c, 0x40, 0x40, 0x7e, 0x00}, + {0x00, 0x7e, 0x40, 0x40, 0x7c, 0x40, 0x40, 0x00}}; + void clear(Ppu *p) { @@ -52,6 +70,25 @@ drawicn(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 fg, Uint8 bg) } } +void +drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr) +{ + Uint8 i, x, y, b; + for(i = 0; i < 0x10; ++i) { /* memory */ + x = ((i % 8) * 3 + 3) * 8, y = p->x1 + 8 + i / 8 * 8, b = stack[i]; + drawicn(p, x, y, font[(b >> 4) & 0xf], 1 + (ptr == i), 0); + drawicn(p, x + 8, y, font[b & 0xf], 1 + (ptr == i), 0); + } + for(x = 0; x < 32; ++x) { + drawpixel(p, x, HEIGHT / 2, 2); + drawpixel(p, WIDTH - x, HEIGHT / 2, 2); + drawpixel(p, WIDTH / 2, HEIGHT - x, 2); + drawpixel(p, WIDTH / 2, x, 2); + drawpixel(p, WIDTH / 2 - 16 + x, HEIGHT / 2, 2); + drawpixel(p, WIDTH / 2, HEIGHT / 2 - 16 + x, 2); + } +} + void paintpixel(Uint8 *dst, Uint16 x, Uint16 y, Uint8 color) { @@ -80,4 +117,17 @@ loadtheme(Ppu *p, Uint8 *addr) p->colors[i] = (r << 20) + (r << 16) + (g << 12) + (g << 8) + (b << 4) + b; } p->reqdraw = 1; +} + +int +initppu(Ppu *p) +{ + if(!(p->output = (Uint32 *)malloc(WIDTH * HEIGHT * sizeof(Uint32)))) + return 0; + clear(p); + p->x1 = PAD * 8; + p->x2 = WIDTH - PAD * 8 - 1; + p->y1 = PAD * 8; + p->y2 = HEIGHT - PAD * 8 - 1; + return 1; } \ No newline at end of file diff --git a/src/ppu.h b/src/ppu.h index a013d70..8401723 100644 --- a/src/ppu.h +++ b/src/ppu.h @@ -1,4 +1,5 @@ #include +#include /* Copyright (c) 2021 Devine Lu Linvega @@ -31,14 +32,11 @@ typedef struct Ppu { Uint32 *output, colors[4]; } Ppu; -void -clear(Ppu *p); -void -drawpixel(Ppu *p, Uint16 x, Uint16 y, Uint8 color); -void -drawchr(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 alpha); -void -drawicn(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 fg, Uint8 bg); - +void clear(Ppu *p); +void drawpixel(Ppu *p, Uint16 x, Uint16 y, Uint8 color); +void drawchr(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 alpha); +void drawicn(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 fg, Uint8 bg); +void drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr); void paintpixel(Uint8 *dst, Uint16 x, Uint16 y, Uint8 color); -void loadtheme(Ppu *p, Uint8 *addr); \ No newline at end of file +void loadtheme(Ppu *p, Uint8 *addr); +int initppu(Ppu *p); \ No newline at end of file