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
clear(Ppu *p)
{
int i, sz = p->height * p->width, rows = sz / 4;
for(i = 0; i < sz; ++i)
p->output[i] = p->colors[0];
for(i = 0; i < rows; i++) {
p->fg[i] = 0;
p->bg[i] = 0;
int i, sz = p->height * p->width;
for(i = 0; i < sz; ++i) {
p->fg.pixels[i] = p->fg.colors[0];
p->bg.pixels[i] = p->bg.colors[0];
}
}
@ -59,28 +57,23 @@ putcolors(Ppu *p, Uint8 *addr)
r = (*(addr + i / 2) >> (!(i % 2) << 2)) & 0x0f,
g = (*(addr + 2 + 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
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->hor * 8 || y >= p->ver * 8 || row > (p->hor * p->ver * 16) - 8)
if(x >= p->width || y >= p->height)
return;
if(color == 0 || color == 2)
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;
layer->pixels[y * p->width + x] = layer->colors[color];
}
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;
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
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;
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 */
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
drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr)
{
Uint8 i, x, y, b;
for(i = 0; i < 0x20; ++i) { /* memory */
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 + 8, y, font[b & 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);
}
for(x = 0; x < 0x20; ++x) {
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);
putpixel(p, &p->bg, x, p->height / 2, 2);
putpixel(p, &p->bg, p->width - x, p->height / 2, 2);
putpixel(p, &p->bg, p->width / 2, p->height - x, 2);
putpixel(p, &p->bg, p->width / 2, x, 2);
putpixel(p, &p->bg, p->width / 2 - 16 + x, p->height / 2, 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
initppu(Ppu *p, Uint8 hor, Uint8 ver, Uint8 pad)
initppu(Ppu *p, Uint8 hor, Uint8 ver)
{
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))))
p->width = 8 * p->hor;
p->height = 8 * p->ver;
if(!(p->bg.pixels = malloc(p->width * p->height * sizeof(Uint32))))
return 0;
if(!(p->bg = malloc(p->width * p->height * sizeof(Uint8) / 4)))
return 0;
if(!(p->fg = malloc(p->width * p->height * sizeof(Uint8) / 4)))
if(!(p->fg.pixels = malloc(p->width * p->height * sizeof(Uint32))))
return 0;
clear(p);
return 1;

View File

@ -17,16 +17,18 @@ typedef unsigned char Uint8;
typedef unsigned short Uint16;
typedef unsigned int Uint32;
typedef struct Layer {
Uint32 *pixels, colors[4];
} Layer;
typedef struct Ppu {
Uint8 *bg, *fg;
Uint16 hor, ver, pad, width, height;
Uint32 *output, colors[4];
Uint16 hor, ver, width, height;
Layer fg, bg;
} 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 putpixel(Ppu *p, Uint8 *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 putchr(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
void drawppu(Ppu *p);
void putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color);
void puticn(Ppu *p, Layer *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 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_Window *gWindow;
static SDL_Renderer *gRenderer;
static SDL_Texture *gTexture;
static SDL_Texture *fgTexture, *bgTexture;
static SDL_Rect gRect;
static Ppu ppu;
static Apu apu[POLYPHONY];
static Mpu mpu;
static Device *devscreen, *devmouse, *devctrl, *devmidi, *devaudio0;
#define PAD 16
Uint8 zoom = 0, debug = 0, reqdraw = 0, bench = 0;
int
@ -53,14 +56,15 @@ audio_callback(void *u, Uint8 *stream, int len)
}
void
redraw(Uint32 *dst, Uxn *u)
redraw(Uxn *u)
{
drawppu(&ppu);
if(debug)
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_RenderCopy(gRenderer, gTexture, NULL, NULL);
SDL_RenderCopy(gRenderer, bgTexture, NULL, NULL);
SDL_RenderCopy(gRenderer, fgTexture, NULL, NULL);
SDL_RenderPresent(gRenderer);
reqdraw = 0;
}
@ -69,26 +73,27 @@ void
toggledebug(Uxn *u)
{
debug = !debug;
redraw(ppu.output, u);
redraw(u);
}
void
togglezoom(Uxn *u)
{
zoom = zoom == 3 ? 1 : zoom + 1;
SDL_SetWindowSize(gWindow, ppu.width * zoom, ppu.height * zoom);
redraw(ppu.output, u);
SDL_SetWindowSize(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
redraw(u);
}
void
quit(void)
{
free(ppu.output);
free(ppu.fg);
free(ppu.bg);
free(ppu.fg.pixels);
free(ppu.bg.pixels);
SDL_UnlockAudioDevice(audio_id);
SDL_DestroyTexture(gTexture);
gTexture = NULL;
SDL_DestroyTexture(bgTexture);
bgTexture = NULL;
SDL_DestroyTexture(fgTexture);
fgTexture = NULL;
SDL_DestroyRenderer(gRenderer);
gRenderer = NULL;
SDL_DestroyWindow(gWindow);
@ -101,22 +106,31 @@ int
init(void)
{
SDL_AudioSpec as;
if(!initppu(&ppu, 48, 32, 16))
if(!initppu(&ppu, 48, 32))
return error("PPU", "Init failure");
gRect.x = PAD;
gRect.y = PAD;
gRect.w = ppu.width;
gRect.h = ppu.height;
if(!initmpu(&mpu, 1))
return error("MPU", "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, 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)
return error("Window", SDL_GetError());
gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
if(gRenderer == NULL)
return error("Renderer", SDL_GetError());
SDL_RenderSetLogicalSize(gRenderer, ppu.width, ppu.height);
gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width, ppu.height);
if(gTexture == NULL)
SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2);
bgTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
if(bgTexture == NULL || SDL_SetTextureBlendMode(bgTexture, SDL_BLENDMODE_NONE))
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_ShowCursor(SDL_DISABLE);
SDL_zero(as);
@ -137,8 +151,8 @@ void
domouse(SDL_Event *event)
{
Uint8 flag = 0x00;
Uint16 x = clamp(event->motion.x - ppu.pad, 0, ppu.hor * 8 - 1);
Uint16 y = clamp(event->motion.y - ppu.pad, 0, ppu.ver * 8 - 1);
Uint16 x = clamp(event->motion.x - PAD, 0, ppu.hor * 8 - 1);
Uint16 y = clamp(event->motion.y - PAD, 0, ppu.ver * 8 - 1);
mempoke16(devmouse->dat, 0x2, x);
mempoke16(devmouse->dat, 0x4, y);
devmouse->dat[7] = 0x00;
@ -223,7 +237,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
Uint16 x = mempeek16(d->dat, 0x8);
Uint16 y = mempeek16(d->dat, 0xa);
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;
if(!mode)
putpixel(&ppu, layer, x, y, d->dat[0xe] & 0x3);
@ -319,11 +333,11 @@ int
start(Uxn *u)
{
evaluxn(u, 0x0100);
redraw(ppu.output, u);
redraw(u);
while(1) {
int i;
SDL_Event event;
double elapsed, start;
double elapsed, start = 0;
if(!bench)
start = SDL_GetPerformanceCounter();
while(SDL_PollEvent(&event) != 0) {
@ -348,7 +362,7 @@ start(Uxn *u)
break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_EXPOSED)
redraw(ppu.output, u);
redraw(u);
break;
}
}
@ -361,7 +375,7 @@ start(Uxn *u)
}
evaluxn(u, mempeek16(devscreen->dat, 0));
if(reqdraw)
redraw(ppu.output, u);
redraw(u);
if(!bench) {
elapsed = (SDL_GetPerformanceCounter() - start) / (double)SDL_GetPerformanceFrequency() * 1000.0f;
SDL_Delay(clamp(16.666f - elapsed, 0, 1000));