uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

commit 33a660aef8592fc087c76e0c5abc9ceb1f2c2363
parent 31a9e3d64ca3ea30c269b5a394ab2a15d680ab92
Author: neauoire <aliceffekt@gmail.com>
Date:   Wed,  7 Apr 2021 17:42:30 -0700

Moved some extra things into PPU

Diffstat:
Msrc/emulator.c | 50+++-----------------------------------------------
Msrc/ppu.c | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/ppu.h | 20+++++++++-----------
3 files changed, 62 insertions(+), 58 deletions(-)

diff --git 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 @@ -79,25 +59,6 @@ error(char *msg, const char *err) } 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) { Uint16 x, y; @@ -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 @@ -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) { @@ -53,6 +71,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) { Uint16 row = (y % 8) + ((x / 8 + y / 8 * HOR) * 16), col = 7 - (x % 8); @@ -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 @@ -1,4 +1,5 @@ #include <stdio.h> +#include <stdlib.h> /* 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