uxn

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

commit 69f770f56fa671067a171fe40b2ce71b44b9c26d
parent c9ddd016965f662ca26339781510fa9a93b88f9b
Author: neauoire <aliceffekt@gmail.com>
Date:   Thu,  8 Apr 2021 09:59:45 -0700

Removed some globals

Diffstat:
Mbuild.sh | 2+-
Msrc/debugger.c | 4++--
Msrc/emulator.c | 40+++++++++++++++++-----------------------
Msrc/ppu.c | 63+++++++++++++++++++++++++++++++++++++++------------------------
Msrc/ppu.h | 19++++++-------------
Msrc/uxn.c | 2+-
Msrc/uxn.h | 4+++-
7 files changed, 69 insertions(+), 65 deletions(-)

diff --git a/build.sh b/build.sh @@ -30,7 +30,7 @@ else fi echo "Assembling.." -./bin/assembler projects/examples/dev.audio.usm bin/boot.rom +./bin/assembler projects/software/noodle.usm bin/boot.rom echo "Running.." if [ "${2}" = '--cli' ]; diff --git a/src/debugger.c b/src/debugger.c @@ -93,11 +93,11 @@ int start(Uxn *u) { printf("RESET --------\n"); - if(!evaluxn(u, PAGE_VECTORS)) + if(!evaluxn(u, PAGE_PROGRAM)) return error("Reset", "Failed"); printstack(&u->wst); printf("FRAME --------\n"); - if(!evaluxn(u, PAGE_VECTORS + 0x08)) + if(!evaluxn(u, PAGE_PROGRAM + 0x08)) return error("Frame", "Failed"); printstack(&u->wst); return 1; diff --git a/src/emulator.c b/src/emulator.c @@ -22,7 +22,6 @@ SDL_AudioDeviceID audio_id; static SDL_Window *gWindow; static SDL_Renderer *gRenderer; static SDL_Texture *gTexture; - static Ppu ppu; static Device *devsystem, *devscreen, *devmouse, *devkey, *devctrl; @@ -45,10 +44,10 @@ error(char *msg, const char *err) void redraw(Uint32 *dst, Uxn *u) { - draw(&ppu); + drawppu(&ppu); if(ppu.debugger) drawdebugger(&ppu, u->wst.dat, u->wst.ptr); - SDL_UpdateTexture(gTexture, NULL, dst, WIDTH * sizeof(Uint32)); + SDL_UpdateTexture(gTexture, NULL, dst, ppu.width * sizeof(Uint32)); SDL_RenderClear(gRenderer); SDL_RenderCopy(gRenderer, gTexture, NULL, NULL); SDL_RenderPresent(gRenderer); @@ -66,7 +65,7 @@ void togglezoom(Uxn *u) { ppu.zoom = ppu.zoom == 3 ? 1 : ppu.zoom + 1; - SDL_SetWindowSize(gWindow, WIDTH * ppu.zoom, HEIGHT * ppu.zoom); + SDL_SetWindowSize(gWindow, ppu.width * ppu.zoom, ppu.height * ppu.zoom); redraw(ppu.output, u); } @@ -74,6 +73,8 @@ void quit(void) { free(ppu.output); + free(ppu.fg); + free(ppu.bg); SDL_DestroyTexture(gTexture); gTexture = NULL; SDL_DestroyRenderer(gRenderer); @@ -87,21 +88,21 @@ quit(void) int init(void) { + if(!initppu(&ppu, 48, 32, 16)) + return error("PPU", "Init failure"); if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) return error("Init", SDL_GetError()); - gWindow = SDL_CreateWindow("Uxn", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH * ppu.zoom, HEIGHT * ppu.zoom, SDL_WINDOW_SHOWN); + gWindow = SDL_CreateWindow("Uxn", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ppu.width * ppu.zoom, ppu.height * ppu.zoom, SDL_WINDOW_SHOWN); if(gWindow == NULL) return error("Window", SDL_GetError()); gRenderer = SDL_CreateRenderer(gWindow, -1, 0); if(gRenderer == NULL) return error("Renderer", SDL_GetError()); - gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, WIDTH, HEIGHT); + gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width, ppu.height); if(gTexture == NULL) return error("Texture", SDL_GetError()); SDL_StartTextInput(); SDL_ShowCursor(SDL_DISABLE); - if(!initppu(&ppu)) - return error("PPU", "Init failure"); return 1; } @@ -110,12 +111,10 @@ domouse(Uxn *u, SDL_Event *event) { Uint8 flag = 0x00; Uint16 addr = devmouse->addr + 2; - Uint16 x = clamp(event->motion.x / ppu.zoom - PAD * 8, 0, HOR * 8 - 1); - Uint16 y = clamp(event->motion.y / ppu.zoom - PAD * 8, 0, VER * 8 - 1); - u->ram.dat[addr + 0] = (x >> 8) & 0xff; - u->ram.dat[addr + 1] = x & 0xff; - u->ram.dat[addr + 2] = (y >> 8) & 0xff; - u->ram.dat[addr + 3] = y & 0xff; + Uint16 x = clamp(event->motion.x / ppu.zoom - ppu.pad, 0, ppu.hor * 8 - 1); + Uint16 y = clamp(event->motion.y / ppu.zoom - ppu.pad, 0, ppu.ver * 8 - 1); + mempoke16(u, addr + 0, x); + mempoke16(u, addr + 2, y); u->ram.dat[addr + 5] = 0x00; switch(event->button.button) { case SDL_BUTTON_LEFT: flag = 0x01; break; @@ -193,9 +192,6 @@ console_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1) case 0x0d: printf("%s\n", &m[(m[ptr + 0x0c] << 8) + b1]); break; } fflush(stdout); - (void)m; - (void)ptr; - (void)b0; return b1; } @@ -207,7 +203,7 @@ screen_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1) if(b0 == 0x0c) { Uint16 x = (m[ptr] << 8) + m[ptr + 1]; Uint16 y = (m[ptr + 2] << 8) + m[ptr + 3]; - putpixel(b1 >> 4 & 0xf ? ppu.fg : ppu.bg, x, y, b1 & 0xf); + putpixel(&ppu, b1 >> 4 & 0xf ? ppu.fg : ppu.bg, x, y, b1 & 0xf); ppu.reqdraw = 1; } return b1; @@ -230,7 +226,7 @@ sprite_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1) Uint8 ch1 = ((sprite[v] >> (7 - h)) & 0x1); if(ch1 == 0 && (blend == 0x05 || blend == 0x0a || blend == 0x0f)) continue; - putpixel(layer, x + h, y + v, ch1 ? blend % 4 : blend / 4); + putpixel(&ppu, layer, x + h, y + v, ch1 ? blend % 4 : blend / 4); } ppu.reqdraw = 1; } @@ -393,10 +389,8 @@ main(int argc, char **argv) portuxn(&u, 0x0f, "---", ppnil); /* Write screen size to dev/screen */ - u.ram.dat[devscreen->addr + 2] = (HOR * 8 >> 8) & 0xff; - u.ram.dat[devscreen->addr + 3] = HOR * 8 & 0xff; - u.ram.dat[devscreen->addr + 4] = (VER * 8 >> 8) & 0xff; - u.ram.dat[devscreen->addr + 5] = VER * 8 & 0xff; + mempoke16(&u, devscreen->addr + 2, ppu.hor * 8); + mempoke16(&u, devscreen->addr + 4, ppu.ver * 8); start(&u); quit(); diff --git a/src/ppu.c b/src/ppu.c @@ -33,16 +33,21 @@ Uint8 font[][8] = { void clear(Ppu *p) { - int i, sz = HEIGHT * WIDTH; - for(i = 0; i < sz; ++i) + int i, sz = p->height * p->width; + for(i = 0; i < sz; ++i) { p->output[i] = p->colors[0]; + p->fg[i] = 0; + p->bg[i] = 0; + p->fg[sz + i] = 0; + p->bg[sz + i] = 0; + } } void drawpixel(Ppu *p, Uint16 x, Uint16 y, Uint8 color) { if(x >= p->x1 && x <= p->x2 && y >= p->x1 && y <= p->y2) - p->output[y * WIDTH + x] = p->colors[color]; + p->output[y * p->width + x] = p->colors[color]; } void @@ -79,20 +84,20 @@ drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr) 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); + drawpixel(p, x, p->height / 2, 2); + drawpixel(p, p->width - x, p->height / 2, 2); + drawpixel(p, p->width / 2, p->height - x, 2); + drawpixel(p, p->width / 2, x, 2); + drawpixel(p, p->width / 2 - 16 + x, p->height / 2, 2); + drawpixel(p, p->width / 2, p->height / 2 - 16 + x, 2); } } void -putpixel(Uint8 *layer, Uint16 x, Uint16 y, Uint8 color) +putpixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color) { - Uint16 row = (y % 8) + ((x / 8 + y / 8 * HOR) * 16), col = 7 - (x % 8); - if(x >= HOR * 8 || y >= VER * 8 || row > RES - 8) + Uint16 row = (y % 8) + ((x / 8 + y / 8 * p->hor) * 16), col = 7 - (x % 8); + if(x >= p->hor * 8 || y >= p->ver * 8 || row > (p->hor * p->ver * 16) - 8) return; if(color == 0 || color == 2) layer[row] &= ~(1UL << col); @@ -119,26 +124,36 @@ loadtheme(Ppu *p, Uint8 *addr) } void -draw(Ppu *p) +drawppu(Ppu *p) { Uint16 x, y; - for(y = 0; y < VER; ++y) - for(x = 0; x < HOR; ++x) { - Uint16 key = (y * HOR + x) * 16; - drawchr(p, (x + PAD) * 8, (y + PAD) * 8, &p->bg[key], 0); - drawchr(p, (x + PAD) * 8, (y + PAD) * 8, &p->fg[key], 1); + for(y = 0; y < p->ver; ++y) + for(x = 0; x < p->hor; ++x) { + Uint16 key = (y * p->hor + x) * 16; + drawchr(p, x * 8 + p->pad, y * 8 + p->pad, &p->bg[key], 0); + drawchr(p, x * 8 + p->pad, y * 8 + p->pad, &p->fg[key], 1); } } int -initppu(Ppu *p) +initppu(Ppu *p, Uint8 hor, Uint8 ver, Uint8 pad) { - if(!(p->output = (Uint32 *)malloc(WIDTH * HEIGHT * sizeof(Uint32)))) + p->hor = hor; + p->ver = ver; + p->pad = pad; + p->width = (8 * p->hor + p->pad * 2); + p->height = (8 * p->ver + p->pad * 2); + + if(!(p->output = malloc(p->width * p->height * sizeof(Uint32)))) + return 0; + if(!(p->bg = malloc(p->width * p->height * sizeof(Uint8) * 2))) + return 0; + if(!(p->fg = malloc(p->width * p->height * sizeof(Uint8) * 2))) return 0; clear(p); - p->x1 = PAD * 8; - p->x2 = WIDTH - PAD * 8 - 1; - p->y1 = PAD * 8; - p->y2 = HEIGHT - PAD * 8 - 1; + p->x1 = p->pad; + p->x2 = p->width - p->pad - 1; + p->y1 = p->pad; + p->y2 = p->height - p->pad - 1; return 1; } \ No newline at end of file diff --git a/src/ppu.h b/src/ppu.h @@ -18,21 +18,14 @@ typedef unsigned short Uint16; typedef signed short Sint16; typedef unsigned int Uint32; -#define HOR 48 -#define VER 32 -#define PAD 2 -#define RES (HOR * VER * 16) -#define WIDTH (8 * HOR + 8 * PAD * 2) -#define HEIGHT (8 * VER + 8 * PAD * 2) - typedef struct Ppu { - Uint8 reqdraw, zoom, debugger, bg[RES], fg[RES]; - Uint16 x1, y1, x2, y2; + Uint8 reqdraw, zoom, debugger, *bg, *fg; + Uint16 hor, ver, pad, width, height, x1, y1, x2, y2; Uint32 *output, colors[4]; } Ppu; -int initppu(Ppu *p); -void draw(Ppu *p); +int initppu(Ppu *p, Uint8 hor, Uint8 ver, Uint8 pad); +void drawppu(Ppu *p); void drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr); void loadtheme(Ppu *p, Uint8 *addr); -void putpixel(Uint8 *layer, Uint16 x, Uint16 y, Uint8 color); -\ No newline at end of file +void putpixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color); +\ No newline at end of file diff --git a/src/uxn.c b/src/uxn.c @@ -178,7 +178,7 @@ loaduxn(Uxn *u, char *filepath) FILE *f; if(!(f = fopen(filepath, "rb"))) return haltuxn(u, "Missing input rom.", 0); - fread(u->ram.dat + PAGE_VECTORS, sizeof(u->ram.dat) - PAGE_VECTORS, 1, f); + fread(u->ram.dat + PAGE_PROGRAM, sizeof(u->ram.dat) - PAGE_PROGRAM, 1, f); printf("Uxn loaded[%s].\n", filepath); return 1; } diff --git a/src/uxn.h b/src/uxn.h @@ -17,7 +17,7 @@ typedef unsigned short Uint16; typedef signed short Sint16; #define PAGE_DEVICE 0x0100 -#define PAGE_VECTORS 0x0200 +#define PAGE_PROGRAM 0x0200 typedef struct { Uint8 ptr, error; @@ -47,3 +47,4 @@ int bootuxn(Uxn *c); int inituxn(Uxn *u, Uint16 vec); int evaluxn(Uxn *u, Uint16 vec); Device *portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Uxn *, Uint16, Uint8, Uint8)); +void mempoke16(Uxn *u, Uint16 a, Uint16 b); +\ No newline at end of file