Converted PPU to use two textures; moved padding to src/emulator.c

This commit is contained in:
Andrew Alderwick 2021-05-19 23:17:58 +01:00
parent 229a0f0184
commit 15480b238a
3 changed files with 75 additions and 94 deletions

View File

@ -41,12 +41,10 @@ readpixel(Uint8 *sprite, Uint8 h, Uint8 v)
void void
clear(Ppu *p) clear(Ppu *p)
{ {
int i, sz = p->height * p->width, rows = sz / 4; int i, sz = p->height * p->width;
for(i = 0; i < sz; ++i) for(i = 0; i < sz; ++i) {
p->output[i] = p->colors[0]; p->fg.pixels[i] = p->fg.colors[0];
for(i = 0; i < rows; i++) { p->bg.pixels[i] = p->bg.colors[0];
p->fg[i] = 0;
p->bg[i] = 0;
} }
} }
@ -59,28 +57,23 @@ putcolors(Ppu *p, Uint8 *addr)
r = (*(addr + i / 2) >> (!(i % 2) << 2)) & 0x0f, r = (*(addr + i / 2) >> (!(i % 2) << 2)) & 0x0f,
g = (*(addr + 2 + i / 2) >> (!(i % 2) << 2)) & 0x0f, g = (*(addr + 2 + i / 2) >> (!(i % 2) << 2)) & 0x0f,
b = (*(addr + 4 + i / 2) >> (!(i % 2) << 2)) & 0x0f; b = (*(addr + 4 + i / 2) >> (!(i % 2) << 2)) & 0x0f;
p->colors[i] = (r << 20) + (r << 16) + (g << 12) + (g << 8) + (b << 4) + b; p->bg.colors[i] = 0xff000000 | (r << 20) | (r << 16) | (g << 12) | (g << 8) | (b << 4) | b;
p->fg.colors[i] = 0xff000000 | (r << 20) | (r << 16) | (g << 12) | (g << 8) | (b << 4) | b;
} }
p->fg.colors[0] = 0;
clear(p);
} }
void void
putpixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color) putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color)
{ {
Uint16 row = (y % 8) + ((x / 8 + y / 8 * p->hor) * 16), col = 7 - (x % 8); if(x >= p->width || y >= p->height)
if(x >= p->hor * 8 || y >= p->ver * 8 || row > (p->hor * p->ver * 16) - 8)
return; return;
if(color == 0 || color == 2) layer->pixels[y * p->width + x] = layer->colors[color];
layer[row] &= ~(1UL << col);
else
layer[row] |= 1UL << col;
if(color == 0 || color == 1)
layer[row + 8] &= ~(1UL << col);
else
layer[row + 8] |= 1UL << col;
} }
void void
puticn(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
{ {
Uint16 v, h; Uint16 v, h;
for(v = 0; v < 8; v++) for(v = 0; v < 8; v++)
@ -96,7 +89,7 @@ puticn(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uin
} }
void void
putchr(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) putchr(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
{ {
Uint16 v, h; Uint16 v, h;
for(v = 0; v < 8; v++) for(v = 0; v < 8; v++)
@ -113,63 +106,35 @@ putchr(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uin
/* output */ /* output */
void
drawpixel(Ppu *p, Uint16 x, Uint16 y, Uint8 color)
{
if(x >= p->pad && x <= p->width - p->pad - 1 && y >= p->pad && y <= p->height - p->pad - 1)
p->output[y * p->width + x] = p->colors[color];
}
void void
drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr) drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr)
{ {
Uint8 i, x, y, b; Uint8 i, x, y, b;
for(i = 0; i < 0x20; ++i) { /* memory */ for(i = 0; i < 0x20; ++i) { /* memory */
x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i]; x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
puticn(p, p->bg, x, y, font[(b >> 4) & 0xf], 1 + (ptr == i) * 0x7, 0, 0); puticn(p, &p->bg, x, y, font[(b >> 4) & 0xf], 1 + (ptr == i) * 0x7, 0, 0);
puticn(p, p->bg, x + 8, y, font[b & 0xf], 1 + (ptr == i) * 0x7, 0, 0); puticn(p, &p->bg, x + 8, y, font[b & 0xf], 1 + (ptr == i) * 0x7, 0, 0);
} }
for(x = 0; x < 0x20; ++x) { for(x = 0; x < 0x20; ++x) {
drawpixel(p, x, p->height / 2, 2); putpixel(p, &p->bg, x, p->height / 2, 2);
drawpixel(p, p->width - x, p->height / 2, 2); putpixel(p, &p->bg, p->width - x, p->height / 2, 2);
drawpixel(p, p->width / 2, p->height - x, 2); putpixel(p, &p->bg, p->width / 2, p->height - x, 2);
drawpixel(p, p->width / 2, x, 2); putpixel(p, &p->bg, p->width / 2, x, 2);
drawpixel(p, p->width / 2 - 16 + x, p->height / 2, 2); putpixel(p, &p->bg, p->width / 2 - 16 + x, p->height / 2, 2);
drawpixel(p, p->width / 2, p->height / 2 - 16 + x, 2); putpixel(p, &p->bg, p->width / 2, p->height / 2 - 16 + x, 2);
} }
} }
void
drawppu(Ppu *p)
{
Uint16 x, y;
for(y = 0; y < p->ver; ++y)
for(x = 0; x < p->hor; ++x) {
Uint8 v, h;
Uint16 key = (y * p->hor + x) * 16;
for(v = 0; v < 8; v++)
for(h = 0; h < 8; h++) {
Uint8 color = readpixel(&p->fg[key], h, v);
if(color == 0)
color = readpixel(&p->bg[key], h, v);
drawpixel(p, x * 8 + p->pad + 7 - h, y * 8 + p->pad + v, color);
}
}
}
int int
initppu(Ppu *p, Uint8 hor, Uint8 ver, Uint8 pad) initppu(Ppu *p, Uint8 hor, Uint8 ver)
{ {
p->hor = hor; p->hor = hor;
p->ver = ver; p->ver = ver;
p->pad = pad; p->width = 8 * p->hor;
p->width = (8 * p->hor + p->pad * 2); p->height = 8 * p->ver;
p->height = (8 * p->ver + p->pad * 2); if(!(p->bg.pixels = malloc(p->width * p->height * sizeof(Uint32))))
if(!(p->output = malloc(p->width * p->height * sizeof(Uint32))))
return 0; return 0;
if(!(p->bg = malloc(p->width * p->height * sizeof(Uint8) / 4))) if(!(p->fg.pixels = malloc(p->width * p->height * sizeof(Uint32))))
return 0;
if(!(p->fg = malloc(p->width * p->height * sizeof(Uint8) / 4)))
return 0; return 0;
clear(p); clear(p);
return 1; return 1;

View File

@ -17,16 +17,18 @@ typedef unsigned char Uint8;
typedef unsigned short Uint16; typedef unsigned short Uint16;
typedef unsigned int Uint32; typedef unsigned int Uint32;
typedef struct Layer {
Uint32 *pixels, colors[4];
} Layer;
typedef struct Ppu { typedef struct Ppu {
Uint8 *bg, *fg; Uint16 hor, ver, width, height;
Uint16 hor, ver, pad, width, height; Layer fg, bg;
Uint32 *output, colors[4];
} Ppu; } Ppu;
int initppu(Ppu *p, Uint8 hor, Uint8 ver, Uint8 pad); int initppu(Ppu *p, Uint8 hor, Uint8 ver);
void putcolors(Ppu *p, Uint8 *addr); void putcolors(Ppu *p, Uint8 *addr);
void putpixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color); void putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color);
void puticn(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); void puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
void putchr(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); void putchr(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
void drawppu(Ppu *p);
void drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr); void drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr);

View File

@ -20,12 +20,15 @@ WITH REGARD TO THIS SOFTWARE.
static SDL_AudioDeviceID audio_id; static SDL_AudioDeviceID audio_id;
static SDL_Window *gWindow; static SDL_Window *gWindow;
static SDL_Renderer *gRenderer; static SDL_Renderer *gRenderer;
static SDL_Texture *gTexture; static SDL_Texture *fgTexture, *bgTexture;
static SDL_Rect gRect;
static Ppu ppu; static Ppu ppu;
static Apu apu[POLYPHONY]; static Apu apu[POLYPHONY];
static Mpu mpu; static Mpu mpu;
static Device *devscreen, *devmouse, *devctrl, *devmidi, *devaudio0; static Device *devscreen, *devmouse, *devctrl, *devmidi, *devaudio0;
#define PAD 16
Uint8 zoom = 0, debug = 0, reqdraw = 0, bench = 0; Uint8 zoom = 0, debug = 0, reqdraw = 0, bench = 0;
int int
@ -53,14 +56,15 @@ audio_callback(void *u, Uint8 *stream, int len)
} }
void void
redraw(Uint32 *dst, Uxn *u) redraw(Uxn *u)
{ {
drawppu(&ppu);
if(debug) if(debug)
drawdebugger(&ppu, u->wst.dat, u->wst.ptr); drawdebugger(&ppu, u->wst.dat, u->wst.ptr);
SDL_UpdateTexture(gTexture, NULL, dst, ppu.width * sizeof(Uint32)); SDL_UpdateTexture(bgTexture, &gRect, ppu.bg.pixels, ppu.width * sizeof(Uint32));
SDL_UpdateTexture(fgTexture, &gRect, ppu.fg.pixels, ppu.width * sizeof(Uint32));
SDL_RenderClear(gRenderer); SDL_RenderClear(gRenderer);
SDL_RenderCopy(gRenderer, gTexture, NULL, NULL); SDL_RenderCopy(gRenderer, bgTexture, NULL, NULL);
SDL_RenderCopy(gRenderer, fgTexture, NULL, NULL);
SDL_RenderPresent(gRenderer); SDL_RenderPresent(gRenderer);
reqdraw = 0; reqdraw = 0;
} }
@ -69,26 +73,27 @@ void
toggledebug(Uxn *u) toggledebug(Uxn *u)
{ {
debug = !debug; debug = !debug;
redraw(ppu.output, u); redraw(u);
} }
void void
togglezoom(Uxn *u) togglezoom(Uxn *u)
{ {
zoom = zoom == 3 ? 1 : zoom + 1; zoom = zoom == 3 ? 1 : zoom + 1;
SDL_SetWindowSize(gWindow, ppu.width * zoom, ppu.height * zoom); SDL_SetWindowSize(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
redraw(ppu.output, u); redraw(u);
} }
void void
quit(void) quit(void)
{ {
free(ppu.output); free(ppu.fg.pixels);
free(ppu.fg); free(ppu.bg.pixels);
free(ppu.bg);
SDL_UnlockAudioDevice(audio_id); SDL_UnlockAudioDevice(audio_id);
SDL_DestroyTexture(gTexture); SDL_DestroyTexture(bgTexture);
gTexture = NULL; bgTexture = NULL;
SDL_DestroyTexture(fgTexture);
fgTexture = NULL;
SDL_DestroyRenderer(gRenderer); SDL_DestroyRenderer(gRenderer);
gRenderer = NULL; gRenderer = NULL;
SDL_DestroyWindow(gWindow); SDL_DestroyWindow(gWindow);
@ -101,22 +106,31 @@ int
init(void) init(void)
{ {
SDL_AudioSpec as; SDL_AudioSpec as;
if(!initppu(&ppu, 48, 32, 16)) if(!initppu(&ppu, 48, 32))
return error("PPU", "Init failure"); return error("PPU", "Init failure");
gRect.x = PAD;
gRect.y = PAD;
gRect.w = ppu.width;
gRect.h = ppu.height;
if(!initmpu(&mpu, 1)) if(!initmpu(&mpu, 1))
return error("MPU", "Init failure"); return error("MPU", "Init failure");
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
return error("Init", SDL_GetError()); return error("Init", SDL_GetError());
gWindow = SDL_CreateWindow("Uxn", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ppu.width * zoom, ppu.height * zoom, SDL_WINDOW_SHOWN); gWindow = SDL_CreateWindow("Uxn", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom, SDL_WINDOW_SHOWN);
if(gWindow == NULL) if(gWindow == NULL)
return error("Window", SDL_GetError()); return error("Window", SDL_GetError());
gRenderer = SDL_CreateRenderer(gWindow, -1, 0); gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
if(gRenderer == NULL) if(gRenderer == NULL)
return error("Renderer", SDL_GetError()); return error("Renderer", SDL_GetError());
SDL_RenderSetLogicalSize(gRenderer, ppu.width, ppu.height); SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2);
gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width, ppu.height); bgTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
if(gTexture == NULL) if(bgTexture == NULL || SDL_SetTextureBlendMode(bgTexture, SDL_BLENDMODE_NONE))
return error("Texture", SDL_GetError()); return error("Texture", SDL_GetError());
fgTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
if(fgTexture == NULL || SDL_SetTextureBlendMode(fgTexture, SDL_BLENDMODE_BLEND))
return error("Texture", SDL_GetError());
SDL_UpdateTexture(bgTexture, NULL, ppu.bg.pixels, 4);
SDL_UpdateTexture(fgTexture, NULL, ppu.fg.pixels, 4);
SDL_StartTextInput(); SDL_StartTextInput();
SDL_ShowCursor(SDL_DISABLE); SDL_ShowCursor(SDL_DISABLE);
SDL_zero(as); SDL_zero(as);
@ -137,8 +151,8 @@ void
domouse(SDL_Event *event) domouse(SDL_Event *event)
{ {
Uint8 flag = 0x00; Uint8 flag = 0x00;
Uint16 x = clamp(event->motion.x - ppu.pad, 0, ppu.hor * 8 - 1); Uint16 x = clamp(event->motion.x - PAD, 0, ppu.hor * 8 - 1);
Uint16 y = clamp(event->motion.y - ppu.pad, 0, ppu.ver * 8 - 1); Uint16 y = clamp(event->motion.y - PAD, 0, ppu.ver * 8 - 1);
mempoke16(devmouse->dat, 0x2, x); mempoke16(devmouse->dat, 0x2, x);
mempoke16(devmouse->dat, 0x4, y); mempoke16(devmouse->dat, 0x4, y);
devmouse->dat[7] = 0x00; devmouse->dat[7] = 0x00;
@ -223,7 +237,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
Uint16 x = mempeek16(d->dat, 0x8); Uint16 x = mempeek16(d->dat, 0x8);
Uint16 y = mempeek16(d->dat, 0xa); Uint16 y = mempeek16(d->dat, 0xa);
Uint8 *addr = &d->mem[mempeek16(d->dat, 0xc)]; Uint8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
Uint8 *layer = d->dat[0xe] >> 4 & 0x1 ? ppu.fg : ppu.bg; Layer *layer = d->dat[0xe] >> 4 & 0x1 ? &ppu.fg : &ppu.bg;
Uint8 mode = d->dat[0xe] >> 5; Uint8 mode = d->dat[0xe] >> 5;
if(!mode) if(!mode)
putpixel(&ppu, layer, x, y, d->dat[0xe] & 0x3); putpixel(&ppu, layer, x, y, d->dat[0xe] & 0x3);
@ -319,11 +333,11 @@ int
start(Uxn *u) start(Uxn *u)
{ {
evaluxn(u, 0x0100); evaluxn(u, 0x0100);
redraw(ppu.output, u); redraw(u);
while(1) { while(1) {
int i; int i;
SDL_Event event; SDL_Event event;
double elapsed, start; double elapsed, start = 0;
if(!bench) if(!bench)
start = SDL_GetPerformanceCounter(); start = SDL_GetPerformanceCounter();
while(SDL_PollEvent(&event) != 0) { while(SDL_PollEvent(&event) != 0) {
@ -348,7 +362,7 @@ start(Uxn *u)
break; break;
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_EXPOSED) if(event.window.event == SDL_WINDOWEVENT_EXPOSED)
redraw(ppu.output, u); redraw(u);
break; break;
} }
} }
@ -361,7 +375,7 @@ start(Uxn *u)
} }
evaluxn(u, mempeek16(devscreen->dat, 0)); evaluxn(u, mempeek16(devscreen->dat, 0));
if(reqdraw) if(reqdraw)
redraw(ppu.output, u); redraw(u);
if(!bench) { if(!bench) {
elapsed = (SDL_GetPerformanceCounter() - start) / (double)SDL_GetPerformanceFrequency() * 1000.0f; elapsed = (SDL_GetPerformanceCounter() - start) / (double)SDL_GetPerformanceFrequency() * 1000.0f;
SDL_Delay(clamp(16.666f - elapsed, 0, 1000)); SDL_Delay(clamp(16.666f - elapsed, 0, 1000));