/* * Copyright (C) 2017 The Contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * A copy of the GNU General Public License can be found in the file * "gpl.txt" in the top directory of this repository. */ #include #define ver(...) _ver(vid_verbose, 0, __VA_ARGS__) #define ver2(...) _ver(vid_verbose, 1, __VA_ARGS__) #define ver3(...) _ver(vid_verbose, 2, __VA_ARGS__) int32_t vid_verbose = 0; #define SCR_W 512 #define SCR_H 350 #define VBL_H 50 #define REG_VCR0 0 #define REG_VCR1 1 #define REG_RWBA 2 #define REG_DSBA 5 #define REG_ODTBA 7 #define REG_ATBA 8 #define REG_ATBAC 11 #define VCR0_UCF 0x0001 #define VCR0_DEN 0x0008 #define DSBA_BS1 0x0080 #define DSBA_BS0 0x0100 #define REG_OFF 0x200 #define PAL_OFF 0x40000 #define OD0_TDE 0x0004 #define OD0_BLA 0x0010 #define OD0_CB 0x0800 #define WIN_SZ_W 0x10000 #define MEM_SZ_W 0x20000 #define N_BANKS 2 #define SCALE(_x) (_x * 2) #define WIN_W SCALE(SCR_W) #define WIN_H SCALE(SCR_H) typedef struct { int32_t x, y, w, h; uint16_t *mem; bool tran; } bm_obj_t; static int32_t reg_off = REG_OFF; static uint16_t mem[MEM_SZ_W]; static int32_t bank = 0; static uint32_t pal[16]; static bm_obj_t bm_objs[16]; static int32_t n_bm_objs = 0; static SDL_Window *win; static SDL_Renderer *ren; static SDL_Texture *tex; static SDL_atomic_t frame; void vid_sdl(void) { ver3("vid_sdl()"); static int32_t last = 0; int32_t now = SDL_AtomicGet(&frame); if (last == now) { ver3("no update"); return; } last = now; if (SDL_RenderFillRect(ren, NULL) < 0) { fail("SDL_RenderFillRect() failed: %s", SDL_GetError()); } void *buf; int32_t pitch; if (SDL_LockMutex(cpu_mutex) < 0) { fail("SDL_LockMutex() failed: %s", SDL_GetError()); } for (int32_t i = 0; i < n_bm_objs; ++i) { bm_obj_t *bm_obj = bm_objs + i; pal[0] = bm_obj->tran ? pal[0] & 0xffffff00 : pal[0] | 0x000000ff; SDL_Rect src = { .x = 0, .y = 0, .w = bm_obj->w, .h = bm_obj->h }; if (SDL_LockTexture(tex, &src, &buf, &pitch) < 0) { fail("SDL_LockTexture() failed: %s", SDL_GetError()); } uint32_t *pix = buf; uint16_t *vid = bm_obj->mem; for (int32_t y = 0; y < src.h; ++y) { for (int32_t x = 0; x < src.w / 4; ++x) { uint16_t v4 = *vid++; *pix++ = pal[(v4 & 0x000f) >> 0]; *pix++ = pal[(v4 & 0x00f0) >> 4]; *pix++ = pal[(v4 & 0x0f00) >> 8]; *pix++ = pal[(v4 & 0xf000) >> 12]; } pix += pitch / 4 - src.w; } SDL_UnlockTexture(tex); SDL_Rect dst = { .x = SCALE(bm_obj->x), .y = SCALE(bm_obj->y), .w = SCALE(bm_obj->w), .h = SCALE(bm_obj->h) }; ver2("vid rend %d %dx%d -> (%d, %d) (%d, %d) %dx%d", i, bm_obj->w, bm_obj->h, bm_obj->x, bm_obj->y, SCALE(bm_obj->x), SCALE(bm_obj->y), SCALE(bm_obj->w), SCALE(bm_obj->h)); if (SDL_RenderCopy(ren, tex, &src, &dst) < 0) { fail("SDL_RenderCopy() failed: %s", SDL_GetError()); } } if (SDL_UnlockMutex(cpu_mutex) < 0) { fail("SDL_UnlockMutex() failed: %s", SDL_GetError()); } SDL_RenderPresent(ren); } void vid_init(void) { ver("vid init"); win = SDL_CreateWindow("Display", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIN_W, WIN_H, 0); if (win == NULL) { fail("SDL_CreateWindow() failed: %s", SDL_GetError()); } ren = SDL_CreateRenderer(win, -1, 0); if (ren == NULL) { fail("SDL_CreateRenderer() failed: %s", SDL_GetError()); } if (SDL_SetRenderDrawColor(ren, 0x00, 0x00, 0x00, 0xff) < 0) { fail("SDL_SetRenderDrawColor() failed: %s", SDL_GetError()); } tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCR_W, SCR_H); if (tex == NULL) { fail("SDL_CreateTexture() failed: %s", SDL_GetError()); } if (SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND) < 0) { fail("SDL_SetTextureBlendMode() failed: %s", SDL_GetError()); } SDL_AtomicSet(&frame, 1); } void vid_quit(void) { ver("vid quit"); SDL_DestroyTexture(tex); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); } bool vid_exec(void) { ver3("vid exec"); static int32_t skip = 99999; static int32_t line = 99999; if (++skip < 10) { ver3("vid skip %d", skip); return false; } skip = 0; int32_t vcr0 = mem[REG_VCR0]; if ((vcr0 & VCR0_DEN) == 0) { ver3("vid dis"); return false; } if (++line < SCR_H + VBL_H) { ver3("vid line %d", line); if (line < SCR_H) { ++mem[REG_ATBAC]; } return line == SCR_H; } // We get here every 4,000 invocations -> 25 fps. ver2("vid frame"); line = 0; int32_t dsba = mem[REG_DSBA]; bank = ((dsba & DSBA_BS0) != 0 ? 1 : 0) + ((dsba & DSBA_BS1) != 0 ? 2 : 0); int32_t rwba = mem[REG_RWBA]; reg_off = (rwba & 0xfff0) << 1; int32_t atba = mem[REG_ATBA]; mem[REG_ATBAC] = (uint16_t)atba; int32_t odtba = mem[REG_ODTBA] & 0xffc0; n_bm_objs = 0; for (int32_t i = 0; i < 16; ++i) { int32_t flips[SCR_H]; int32_t n_flips = 0; uint16_t *od = mem + odtba + 4 * i; if ((od[0] & OD0_BLA) != 0) { ver3("vid obj %d blanked", i); continue; } int32_t w = (od[1] & 0xfc00) >> 6; if (w == 0) { ver3("vid obj %d empty", i); continue; } int32_t mask = 1 << i; for (int32_t k = 0; k < SCR_H; ++k) { if ((mem[atba + k] & mask) == 0) { flips[n_flips] = k; ++n_flips; } } if (n_flips == 0) { ver3("vid obj %d unused", i); continue; } if (n_flips == 1) { flips[n_flips] = SCR_H; ++n_flips; } bool cb = (od[0] & OD0_CB) != 0; bool tde = (od[0] & OD0_TDE) != 0; int32_t x = (od[1] & 0x03ff) * 2; int32_t off = ((od[0] & 0x00c0) << 10) | od[2]; ver2("vid obj %d %c %c %d:%d %d+%d 0x%05x", i, cb ? 'c' : 'b', tde ? 't' : '-', flips[0], flips[1], x, w, off); if (!cb) { bm_obj_t *bm_obj = bm_objs + n_bm_objs; bm_obj->x = x; bm_obj->y = flips[0]; bm_obj->w = w; bm_obj->h = flips[1] - flips[0]; bm_obj->mem = mem + off; bm_obj->tran = tde; ++n_bm_objs; } else { int32_t vcr1 = mem[REG_VCR1]; int32_t fon_h = (vcr1 & 0xf000) >> 12; } } SDL_AtomicAdd(&frame, 1); return false; } uint32_t vid_read(uint32_t off, int32_t sz) { ver2("vid rd %d 0x%05x:%d", bank, off, sz * 8); int32_t off16 = (int32_t)(off / 2); if (sz != 2 || bank >= N_BANKS || off % 2 != 0 || off16 >= WIN_SZ_W) { fail("invalid vid rd %d %u:%d", bank, off, sz * 8); } if (off16 >= reg_off && off16 < reg_off + 16) { return mem[off16 - reg_off]; } return mem[bank * WIN_SZ_W + off16]; } void vid_write(uint32_t off, int32_t sz, uint32_t val) { ver2("vid wr %d 0x%05x:%d 0x%0*x", bank, off, sz * 8, sz * 2, val); int32_t off16 = (int32_t)(off / 2); if (sz != 2 || bank >= N_BANKS || off % 2 != 0 || (off16 >= WIN_SZ_W && off16 != PAL_OFF)) { fail("invalid vid wr %d %u:%d", bank, off, sz * 8); } if (off16 == PAL_OFF) { int32_t i_pal = ((int32_t)val & 0x03c0) >> 6; uint32_t r = ((val & 0x0004) >> 1) | ((val & 0x0020) >> 5); uint32_t g = ((val & 0x0002) >> 0) | ((val & 0x0010) >> 4); uint32_t b = ((val & 0x0001) << 1) | ((val & 0x0008) >> 3); r = (r ^ 3) * 85; g = (g ^ 3) * 85; b = (b ^ 3) * 85; pal[i_pal] = (r << 24) | (g << 16) | (b << 8) | 0xff; return; } if (off16 >= reg_off && off16 < reg_off + 16) { mem[off16 - reg_off] = (uint16_t)val; return; } mem[bank * WIN_SZ_W + off16] = (uint16_t)val; }