[a06aa8b] | 1 | /*
|
---|
| 2 | * Copyright (C) 2017 The Contributors
|
---|
| 3 | *
|
---|
| 4 | * This program is free software: you can redistribute it and/or modify
|
---|
| 5 | * it under the terms of the GNU General Public License as published by
|
---|
| 6 | * the Free Software Foundation, either version 3 of the License, or (at
|
---|
| 7 | * your option) any later version.
|
---|
| 8 | *
|
---|
| 9 | * This program is distributed in the hope that it will be useful, but
|
---|
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 12 | * General Public License for more details.
|
---|
| 13 | *
|
---|
| 14 | * A copy of the GNU General Public License can be found in the file
|
---|
[2147e53] | 15 | * "gpl.txt" in the top directory of this repository.
|
---|
[a06aa8b] | 16 | */
|
---|
| 17 |
|
---|
| 18 | #include <all.h>
|
---|
| 19 |
|
---|
[4c71d39] | 20 | #define ver(...) _ver(vid_verbose, 0, __VA_ARGS__)
|
---|
| 21 | #define ver2(...) _ver(vid_verbose, 1, __VA_ARGS__)
|
---|
| 22 | #define ver3(...) _ver(vid_verbose, 2, __VA_ARGS__)
|
---|
[a06aa8b] | 23 |
|
---|
[4c71d39] | 24 | int32_t vid_verbose = 0;
|
---|
[a06aa8b] | 25 |
|
---|
[ac4e192] | 26 | #define SCR_W 512
|
---|
| 27 | #define SCR_H 350
|
---|
| 28 | #define VBL_H 50
|
---|
| 29 |
|
---|
| 30 | #define REG_VCR0 0
|
---|
| 31 | #define REG_VCR1 1
|
---|
| 32 | #define REG_RWBA 2
|
---|
| 33 | #define REG_DSBA 5
|
---|
| 34 | #define REG_ODTBA 7
|
---|
| 35 | #define REG_ATBA 8
|
---|
| 36 | #define REG_ATBAC 11
|
---|
| 37 |
|
---|
| 38 | #define VCR0_UCF 0x0001
|
---|
| 39 | #define VCR0_DEN 0x0008
|
---|
| 40 |
|
---|
| 41 | #define DSBA_BS1 0x0080
|
---|
| 42 | #define DSBA_BS0 0x0100
|
---|
| 43 |
|
---|
| 44 | #define REG_OFF 0x200
|
---|
| 45 | #define PAL_OFF 0x40000
|
---|
| 46 |
|
---|
[f285858] | 47 | #define OD0_TDE 0x0004
|
---|
[ac4e192] | 48 | #define OD0_BLA 0x0010
|
---|
[f285858] | 49 | #define OD0_CB 0x0800
|
---|
[ac4e192] | 50 |
|
---|
| 51 | #define WIN_SZ_W 0x10000
|
---|
| 52 | #define MEM_SZ_W 0x20000
|
---|
| 53 |
|
---|
| 54 | #define N_BANKS 2
|
---|
| 55 |
|
---|
[f285858] | 56 | #define SCALE(_x) (_x * 2)
|
---|
| 57 |
|
---|
| 58 | #define WIN_W SCALE(SCR_W)
|
---|
| 59 | #define WIN_H SCALE(SCR_H)
|
---|
| 60 |
|
---|
| 61 | typedef struct {
|
---|
| 62 | int32_t x, y, w, h;
|
---|
| 63 | uint16_t *mem;
|
---|
| 64 | bool tran;
|
---|
| 65 | } bm_obj_t;
|
---|
| 66 |
|
---|
[ac4e192] | 67 | static int32_t reg_off = REG_OFF;
|
---|
| 68 |
|
---|
| 69 | static uint16_t mem[MEM_SZ_W];
|
---|
| 70 | static int32_t bank = 0;
|
---|
| 71 |
|
---|
[f285858] | 72 | static uint32_t pal[16];
|
---|
| 73 |
|
---|
| 74 | static bm_obj_t bm_objs[16];
|
---|
| 75 | static int32_t n_bm_objs = 0;
|
---|
| 76 |
|
---|
| 77 | static SDL_Window *win;
|
---|
| 78 | static SDL_Renderer *ren;
|
---|
| 79 | static SDL_Texture *tex;
|
---|
| 80 | static SDL_atomic_t frame;
|
---|
| 81 |
|
---|
| 82 | void vid_sdl(void)
|
---|
| 83 | {
|
---|
| 84 | ver3("vid_sdl()");
|
---|
| 85 |
|
---|
| 86 | static int32_t last = 0;
|
---|
| 87 | int32_t now = SDL_AtomicGet(&frame);
|
---|
| 88 |
|
---|
| 89 | if (last == now) {
|
---|
| 90 | ver3("no update");
|
---|
| 91 | return;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | last = now;
|
---|
| 95 |
|
---|
| 96 | if (SDL_RenderFillRect(ren, NULL) < 0) {
|
---|
| 97 | fail("SDL_RenderFillRect() failed: %s", SDL_GetError());
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | void *buf;
|
---|
| 101 | int32_t pitch;
|
---|
| 102 |
|
---|
| 103 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 104 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | for (int32_t i = 0; i < n_bm_objs; ++i) {
|
---|
| 108 | bm_obj_t *bm_obj = bm_objs + i;
|
---|
| 109 |
|
---|
| 110 | pal[0] = bm_obj->tran ? pal[0] & 0xffffff00 : pal[0] | 0x000000ff;
|
---|
| 111 |
|
---|
| 112 | SDL_Rect src = {
|
---|
| 113 | .x = 0, .y = 0, .w = bm_obj->w, .h = bm_obj->h
|
---|
| 114 | };
|
---|
| 115 |
|
---|
| 116 | if (SDL_LockTexture(tex, &src, &buf, &pitch) < 0) {
|
---|
| 117 | fail("SDL_LockTexture() failed: %s", SDL_GetError());
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | uint32_t *pix = buf;
|
---|
| 121 | uint16_t *vid = bm_obj->mem;
|
---|
| 122 |
|
---|
| 123 | for (int32_t y = 0; y < src.h; ++y) {
|
---|
| 124 | for (int32_t x = 0; x < src.w / 4; ++x) {
|
---|
| 125 | uint16_t v4 = *vid++;
|
---|
| 126 |
|
---|
| 127 | *pix++ = pal[(v4 & 0x000f) >> 0];
|
---|
| 128 | *pix++ = pal[(v4 & 0x00f0) >> 4];
|
---|
| 129 | *pix++ = pal[(v4 & 0x0f00) >> 8];
|
---|
| 130 | *pix++ = pal[(v4 & 0xf000) >> 12];
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | pix += pitch / 4 - src.w;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | SDL_UnlockTexture(tex);
|
---|
| 137 |
|
---|
| 138 | SDL_Rect dst = {
|
---|
| 139 | .x = SCALE(bm_obj->x), .y = SCALE(bm_obj->y),
|
---|
| 140 | .w = SCALE(bm_obj->w), .h = SCALE(bm_obj->h)
|
---|
| 141 | };
|
---|
| 142 |
|
---|
| 143 | ver2("vid rend %d %dx%d -> (%d, %d) (%d, %d) %dx%d",
|
---|
| 144 | i,
|
---|
| 145 | bm_obj->w, bm_obj->h,
|
---|
| 146 | bm_obj->x, bm_obj->y,
|
---|
| 147 | SCALE(bm_obj->x), SCALE(bm_obj->y),
|
---|
| 148 | SCALE(bm_obj->w), SCALE(bm_obj->h));
|
---|
| 149 |
|
---|
| 150 | if (SDL_RenderCopy(ren, tex, &src, &dst) < 0) {
|
---|
| 151 | fail("SDL_RenderCopy() failed: %s", SDL_GetError());
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 156 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | SDL_RenderPresent(ren);
|
---|
| 160 | }
|
---|
[ac4e192] | 161 |
|
---|
[a06aa8b] | 162 | void vid_init(void)
|
---|
| 163 | {
|
---|
| 164 | ver("vid init");
|
---|
[f285858] | 165 |
|
---|
| 166 | win = SDL_CreateWindow("Display", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
---|
| 167 | WIN_W, WIN_H, 0);
|
---|
| 168 |
|
---|
| 169 | if (win == NULL) {
|
---|
| 170 | fail("SDL_CreateWindow() failed: %s", SDL_GetError());
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | ren = SDL_CreateRenderer(win, -1, 0);
|
---|
| 174 |
|
---|
| 175 | if (ren == NULL) {
|
---|
| 176 | fail("SDL_CreateRenderer() failed: %s", SDL_GetError());
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | if (SDL_SetRenderDrawColor(ren, 0x00, 0x00, 0x00, 0xff) < 0) {
|
---|
| 180 | fail("SDL_SetRenderDrawColor() failed: %s", SDL_GetError());
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING,
|
---|
| 184 | SCR_W, SCR_H);
|
---|
| 185 |
|
---|
| 186 | if (tex == NULL) {
|
---|
| 187 | fail("SDL_CreateTexture() failed: %s", SDL_GetError());
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | if (SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND) < 0) {
|
---|
| 191 | fail("SDL_SetTextureBlendMode() failed: %s", SDL_GetError());
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | SDL_AtomicSet(&frame, 1);
|
---|
[a06aa8b] | 195 | }
|
---|
| 196 |
|
---|
| 197 | void vid_quit(void)
|
---|
| 198 | {
|
---|
| 199 | ver("vid quit");
|
---|
[f285858] | 200 |
|
---|
| 201 | SDL_DestroyTexture(tex);
|
---|
| 202 | SDL_DestroyRenderer(ren);
|
---|
| 203 | SDL_DestroyWindow(win);
|
---|
[a06aa8b] | 204 | }
|
---|
| 205 |
|
---|
[3c30832] | 206 | bool vid_exec(void)
|
---|
[a06aa8b] | 207 | {
|
---|
[4c71d39] | 208 | ver3("vid exec");
|
---|
[ac4e192] | 209 |
|
---|
| 210 | static int32_t skip = 99999;
|
---|
| 211 | static int32_t line = 99999;
|
---|
| 212 |
|
---|
| 213 | if (++skip < 10) {
|
---|
| 214 | ver3("vid skip %d", skip);
|
---|
| 215 | return false;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | skip = 0;
|
---|
| 219 | int32_t vcr0 = mem[REG_VCR0];
|
---|
| 220 |
|
---|
| 221 | if ((vcr0 & VCR0_DEN) == 0) {
|
---|
| 222 | ver3("vid dis");
|
---|
| 223 | return false;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | if (++line < SCR_H + VBL_H) {
|
---|
| 227 | ver3("vid line %d", line);
|
---|
| 228 |
|
---|
| 229 | if (line < SCR_H) {
|
---|
| 230 | ++mem[REG_ATBAC];
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | return line == SCR_H;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | // We get here every 4,000 invocations -> 25 fps.
|
---|
| 237 |
|
---|
| 238 | ver2("vid frame");
|
---|
| 239 | line = 0;
|
---|
| 240 |
|
---|
| 241 | int32_t dsba = mem[REG_DSBA];
|
---|
| 242 | bank = ((dsba & DSBA_BS0) != 0 ? 1 : 0) + ((dsba & DSBA_BS1) != 0 ? 2 : 0);
|
---|
| 243 |
|
---|
| 244 | int32_t rwba = mem[REG_RWBA];
|
---|
| 245 | reg_off = (rwba & 0xfff0) << 1;
|
---|
| 246 |
|
---|
| 247 | int32_t atba = mem[REG_ATBA];
|
---|
| 248 | mem[REG_ATBAC] = (uint16_t)atba;
|
---|
| 249 |
|
---|
| 250 | int32_t odtba = mem[REG_ODTBA] & 0xffc0;
|
---|
[f285858] | 251 |
|
---|
| 252 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 253 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | n_bm_objs = 0;
|
---|
[ac4e192] | 257 |
|
---|
| 258 | for (int32_t i = 0; i < 16; ++i) {
|
---|
[f285858] | 259 | int32_t flips[SCR_H];
|
---|
| 260 | int32_t n_flips = 0;
|
---|
[ac4e192] | 261 |
|
---|
| 262 | uint16_t *od = mem + odtba + 4 * i;
|
---|
| 263 |
|
---|
| 264 | if ((od[0] & OD0_BLA) != 0) {
|
---|
[f285858] | 265 | ver3("vid obj %d blanked", i);
|
---|
[ac4e192] | 266 | continue;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | int32_t w = (od[1] & 0xfc00) >> 6;
|
---|
| 270 |
|
---|
| 271 | if (w == 0) {
|
---|
[f285858] | 272 | ver3("vid obj %d empty", i);
|
---|
[ac4e192] | 273 | continue;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | int32_t mask = 1 << i;
|
---|
| 277 |
|
---|
| 278 | for (int32_t k = 0; k < SCR_H; ++k) {
|
---|
| 279 | if ((mem[atba + k] & mask) == 0) {
|
---|
[f285858] | 280 | flips[n_flips] = k;
|
---|
| 281 | ++n_flips;
|
---|
[ac4e192] | 282 | }
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[f285858] | 285 | if (n_flips == 0) {
|
---|
| 286 | ver3("vid obj %d unused", i);
|
---|
[ac4e192] | 287 | continue;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[f285858] | 290 | if (n_flips == 1) {
|
---|
| 291 | flips[n_flips] = SCR_H;
|
---|
| 292 | ++n_flips;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | bool cb = (od[0] & OD0_CB) != 0;
|
---|
| 296 | bool tde = (od[0] & OD0_TDE) != 0;
|
---|
| 297 |
|
---|
| 298 | int32_t x = (od[1] & 0x03ff) * 2;
|
---|
| 299 | int32_t off = ((od[0] & 0x00c0) << 10) | od[2];
|
---|
| 300 |
|
---|
| 301 | ver2("vid obj %d %c %c %d:%d %d+%d 0x%05x",
|
---|
| 302 | i, cb ? 'c' : 'b', tde ? 't' : '-', flips[0], flips[1], x, w, off);
|
---|
| 303 |
|
---|
| 304 | if (!cb) {
|
---|
| 305 | bm_obj_t *bm_obj = bm_objs + n_bm_objs;
|
---|
| 306 |
|
---|
| 307 | bm_obj->x = x;
|
---|
| 308 | bm_obj->y = flips[0];
|
---|
| 309 | bm_obj->w = w;
|
---|
| 310 | bm_obj->h = flips[1] - flips[0];
|
---|
| 311 | bm_obj->mem = mem + off;
|
---|
| 312 | bm_obj->tran = tde;
|
---|
| 313 |
|
---|
| 314 | ++n_bm_objs;
|
---|
| 315 | }
|
---|
| 316 | else {
|
---|
| 317 | int32_t vcr1 = mem[REG_VCR1];
|
---|
| 318 | int32_t fon_h = (vcr1 & 0xf000) >> 12;
|
---|
| 319 | }
|
---|
[ac4e192] | 320 | }
|
---|
| 321 |
|
---|
[f285858] | 322 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 323 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 324 | }
|
---|
[ac4e192] | 325 |
|
---|
[f285858] | 326 | SDL_AtomicAdd(&frame, 1);
|
---|
[3c30832] | 327 | return false;
|
---|
[a06aa8b] | 328 | }
|
---|
| 329 |
|
---|
| 330 | uint32_t vid_read(uint32_t off, int32_t sz)
|
---|
| 331 | {
|
---|
[ac4e192] | 332 | ver2("vid rd %d 0x%05x:%d", bank, off, sz * 8);
|
---|
| 333 |
|
---|
| 334 | int32_t off16 = (int32_t)(off / 2);
|
---|
| 335 |
|
---|
| 336 | if (sz != 2 || bank >= N_BANKS || off % 2 != 0 || off16 >= WIN_SZ_W) {
|
---|
| 337 | fail("invalid vid rd %d %u:%d", bank, off, sz * 8);
|
---|
| 338 | }
|
---|
| 339 |
|
---|
[f285858] | 340 | uint32_t res;
|
---|
| 341 |
|
---|
| 342 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 343 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 344 | }
|
---|
| 345 |
|
---|
[ac4e192] | 346 | if (off16 >= reg_off && off16 < reg_off + 16) {
|
---|
[f285858] | 347 | res = mem[off16 - reg_off];
|
---|
| 348 | }
|
---|
| 349 | else {
|
---|
| 350 | res = mem[bank * WIN_SZ_W + off16];
|
---|
[ac4e192] | 351 | }
|
---|
| 352 |
|
---|
[f285858] | 353 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 354 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | return res;
|
---|
[a06aa8b] | 358 | }
|
---|
| 359 |
|
---|
| 360 | void vid_write(uint32_t off, int32_t sz, uint32_t val)
|
---|
| 361 | {
|
---|
[ac4e192] | 362 | ver2("vid wr %d 0x%05x:%d 0x%0*x", bank, off, sz * 8, sz * 2, val);
|
---|
| 363 |
|
---|
| 364 | int32_t off16 = (int32_t)(off / 2);
|
---|
| 365 |
|
---|
| 366 | if (sz != 2 || bank >= N_BANKS || off % 2 != 0 || (off16 >= WIN_SZ_W && off16 != PAL_OFF)) {
|
---|
| 367 | fail("invalid vid wr %d %u:%d", bank, off, sz * 8);
|
---|
| 368 | }
|
---|
| 369 |
|
---|
[f285858] | 370 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 371 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
[ac4e192] | 372 | }
|
---|
| 373 |
|
---|
[f285858] | 374 | if (off16 == PAL_OFF) {
|
---|
| 375 | int32_t i_pal = ((int32_t)val & 0x03c0) >> 6;
|
---|
| 376 |
|
---|
| 377 | uint32_t r = ((val & 0x0004) >> 1) | ((val & 0x0020) >> 5);
|
---|
| 378 | uint32_t g = ((val & 0x0002) >> 0) | ((val & 0x0010) >> 4);
|
---|
| 379 | uint32_t b = ((val & 0x0001) << 1) | ((val & 0x0008) >> 3);
|
---|
| 380 |
|
---|
| 381 | r = (r ^ 3) * 85;
|
---|
| 382 | g = (g ^ 3) * 85;
|
---|
| 383 | b = (b ^ 3) * 85;
|
---|
| 384 |
|
---|
| 385 | pal[i_pal] = (r << 24) | (g << 16) | (b << 8) | 0xff;
|
---|
| 386 | }
|
---|
| 387 | else if (off16 >= reg_off && off16 < reg_off + 16) {
|
---|
[ac4e192] | 388 | mem[off16 - reg_off] = (uint16_t)val;
|
---|
[f285858] | 389 | }
|
---|
| 390 | else {
|
---|
| 391 | mem[bank * WIN_SZ_W + off16] = (uint16_t)val;
|
---|
[ac4e192] | 392 | }
|
---|
| 393 |
|
---|
[f285858] | 394 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 395 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 396 | }
|
---|
[a06aa8b] | 397 | }
|
---|