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
|
---|
15 | * "gpl.txt" in the top directory of this repository.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <all.h>
|
---|
19 |
|
---|
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__)
|
---|
23 |
|
---|
24 | int32_t vid_verbose = 0;
|
---|
25 |
|
---|
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_CGBA 10
|
---|
37 | #define REG_ATBAC 11
|
---|
38 |
|
---|
39 | #define VCR0_UCF 0x0001
|
---|
40 | #define VCR0_DEN 0x0008
|
---|
41 |
|
---|
42 | #define DSBA_BS1 0x0080
|
---|
43 | #define DSBA_BS0 0x0100
|
---|
44 |
|
---|
45 | #define REG_OFF 0x200
|
---|
46 | #define PAL_OFF 0x40000
|
---|
47 |
|
---|
48 | #define OD0_TDE 0x0004
|
---|
49 | #define OD0_BLA 0x0010
|
---|
50 | #define OD0_CB 0x0800
|
---|
51 |
|
---|
52 | #define AT_UND 0x0100
|
---|
53 | #define AT_CG 0x8000
|
---|
54 |
|
---|
55 | #define WIN_SZ_W 0x10000
|
---|
56 | #define MEM_SZ_W 0x20000
|
---|
57 |
|
---|
58 | #define N_BANKS 2
|
---|
59 |
|
---|
60 | #define SCALE(_x) (_x * 2)
|
---|
61 |
|
---|
62 | #define WIN_W SCALE(SCR_W)
|
---|
63 | #define WIN_H SCALE(SCR_H)
|
---|
64 |
|
---|
65 | typedef struct {
|
---|
66 | int32_t x, y, w, h;
|
---|
67 | uint16_t *mem;
|
---|
68 | bool tran;
|
---|
69 | int32_t fon_h;
|
---|
70 | } obj_t;
|
---|
71 |
|
---|
72 | static int32_t reg_off = REG_OFF;
|
---|
73 |
|
---|
74 | static uint16_t mem[MEM_SZ_W];
|
---|
75 | static int32_t bank = 0;
|
---|
76 |
|
---|
77 | static uint32_t pal[16];
|
---|
78 |
|
---|
79 | static obj_t objs[16];
|
---|
80 | static int32_t n_objs = 0;
|
---|
81 |
|
---|
82 | static SDL_Window *win;
|
---|
83 | uint32_t vid_win;
|
---|
84 |
|
---|
85 | static SDL_Renderer *ren;
|
---|
86 | static SDL_Texture *tex;
|
---|
87 | static SDL_atomic_t frame;
|
---|
88 |
|
---|
89 | static void rend_bm(uint16_t *vid, int32_t w, int32_t h, uint32_t *pix, int32_t pitch)
|
---|
90 | {
|
---|
91 | for (int32_t y = 0; y < h; ++y) {
|
---|
92 | for (int32_t x = 0; x < w / 4; ++x) {
|
---|
93 | uint16_t v4 = *vid++;
|
---|
94 |
|
---|
95 | *pix++ = pal[(v4 & 0x000f) >> 0];
|
---|
96 | *pix++ = pal[(v4 & 0x00f0) >> 4];
|
---|
97 | *pix++ = pal[(v4 & 0x0f00) >> 8];
|
---|
98 | *pix++ = pal[(v4 & 0xf000) >> 12];
|
---|
99 | }
|
---|
100 |
|
---|
101 | pix += pitch / 4 - w;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | static void rend_tx(uint16_t *vid, int32_t w, int32_t h, int32_t fon_h,
|
---|
106 | uint32_t *pix, int32_t pitch)
|
---|
107 | {
|
---|
108 | int32_t cgba = mem[REG_CGBA];
|
---|
109 |
|
---|
110 | uint16_t *cg0 = mem + ((cgba & 0x00f0) << 8);
|
---|
111 | uint16_t *cg1 = mem + ((cgba & 0x000f) << 12);
|
---|
112 |
|
---|
113 | int32_t line = fon_h - 1;
|
---|
114 |
|
---|
115 | for (int32_t y = 0; y < h; ++y) {
|
---|
116 | uint16_t *walk = vid;
|
---|
117 |
|
---|
118 | for (int32_t col2 = 0; col2 < w / 16; ++col2) {
|
---|
119 | uint16_t ch2 = *walk++;
|
---|
120 |
|
---|
121 | for (int32_t i = 0; i < 2; ++i) {
|
---|
122 | int32_t ch = (uint8_t)ch2;
|
---|
123 | int32_t at = *walk++;
|
---|
124 |
|
---|
125 | int32_t bg = at & 0x000f;
|
---|
126 | int32_t fg = (at & 0x00f0) >> 4;
|
---|
127 |
|
---|
128 | int32_t bits;
|
---|
129 |
|
---|
130 | if ((at & AT_UND) != 0 && line == 1) {
|
---|
131 | bits = 0xff;
|
---|
132 | }
|
---|
133 | else {
|
---|
134 | uint16_t *cg = (at & AT_CG) != 0 ? cg0 : cg1;
|
---|
135 | bits = cg[256 * line + ch];
|
---|
136 | }
|
---|
137 |
|
---|
138 | int32_t mask = 0x01;
|
---|
139 |
|
---|
140 | for (int32_t k = 0; k < 8; ++k) {
|
---|
141 | *pix++ = (bits & mask) != 0 ? pal[fg] : pal[bg];
|
---|
142 | mask <<= 1;
|
---|
143 | }
|
---|
144 |
|
---|
145 | ch2 >>= 8;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (--line < 0) {
|
---|
150 | line = fon_h - 1;
|
---|
151 | vid = walk;
|
---|
152 | }
|
---|
153 |
|
---|
154 | pix += pitch / 4 - w;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | void vid_sdl(void)
|
---|
159 | {
|
---|
160 | ver3("vid_sdl()");
|
---|
161 |
|
---|
162 | static int32_t last = 0;
|
---|
163 | int32_t now = SDL_AtomicGet(&frame);
|
---|
164 |
|
---|
165 | if (last == now) {
|
---|
166 | ver3("no update");
|
---|
167 | return;
|
---|
168 | }
|
---|
169 |
|
---|
170 | last = now;
|
---|
171 |
|
---|
172 | if (SDL_RenderFillRect(ren, NULL) < 0) {
|
---|
173 | fail("SDL_RenderFillRect() failed: %s", SDL_GetError());
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
177 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
178 | }
|
---|
179 |
|
---|
180 | for (int32_t i = 0; i < n_objs; ++i) {
|
---|
181 | obj_t *obj = objs + i;
|
---|
182 |
|
---|
183 | pal[0] = obj->tran ? pal[0] & 0xffffff00 : pal[0] | 0x000000ff;
|
---|
184 |
|
---|
185 | SDL_Rect src = {
|
---|
186 | .x = 0, .y = 0, .w = obj->w, .h = obj->h
|
---|
187 | };
|
---|
188 |
|
---|
189 | void *buf;
|
---|
190 | int32_t pitch;
|
---|
191 |
|
---|
192 | if (SDL_LockTexture(tex, &src, &buf, &pitch) < 0) {
|
---|
193 | fail("SDL_LockTexture() failed: %s", SDL_GetError());
|
---|
194 | }
|
---|
195 |
|
---|
196 | if (obj->fon_h < 0) {
|
---|
197 | rend_bm(obj->mem, obj->w, obj->h, buf, pitch);
|
---|
198 | }
|
---|
199 | else {
|
---|
200 | rend_tx(obj->mem, obj->w, obj->h, obj->fon_h, buf, pitch);
|
---|
201 | }
|
---|
202 |
|
---|
203 | SDL_UnlockTexture(tex);
|
---|
204 |
|
---|
205 | SDL_Rect dst = {
|
---|
206 | .x = SCALE(obj->x), .y = SCALE(obj->y),
|
---|
207 | .w = SCALE(obj->w), .h = SCALE(obj->h)
|
---|
208 | };
|
---|
209 |
|
---|
210 | ver2("vid rend %d %dx%d -> (%d, %d) (%d, %d) %dx%d",
|
---|
211 | i,
|
---|
212 | obj->w, obj->h,
|
---|
213 | obj->x, obj->y,
|
---|
214 | SCALE(obj->x), SCALE(obj->y),
|
---|
215 | SCALE(obj->w), SCALE(obj->h));
|
---|
216 |
|
---|
217 | if (SDL_RenderCopy(ren, tex, &src, &dst) < 0) {
|
---|
218 | fail("SDL_RenderCopy() failed: %s", SDL_GetError());
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
223 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
224 | }
|
---|
225 |
|
---|
226 | SDL_RenderPresent(ren);
|
---|
227 | }
|
---|
228 |
|
---|
229 | void vid_init(void)
|
---|
230 | {
|
---|
231 | ver("vid init");
|
---|
232 |
|
---|
233 | win = SDL_CreateWindow("Display", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
---|
234 | WIN_W, WIN_H, 0);
|
---|
235 |
|
---|
236 | if (win == NULL) {
|
---|
237 | fail("SDL_CreateWindow() failed: %s", SDL_GetError());
|
---|
238 | }
|
---|
239 |
|
---|
240 | vid_win = SDL_GetWindowID(win);
|
---|
241 |
|
---|
242 | if (vid_win == 0) {
|
---|
243 | fail("SDL_GetWindowID() failed: %s", SDL_GetError());
|
---|
244 | }
|
---|
245 |
|
---|
246 | ren = SDL_CreateRenderer(win, -1, 0);
|
---|
247 |
|
---|
248 | if (ren == NULL) {
|
---|
249 | fail("SDL_CreateRenderer() failed: %s", SDL_GetError());
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (SDL_SetRenderDrawColor(ren, 0x00, 0x00, 0x00, 0xff) < 0) {
|
---|
253 | fail("SDL_SetRenderDrawColor() failed: %s", SDL_GetError());
|
---|
254 | }
|
---|
255 |
|
---|
256 | tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING,
|
---|
257 | SCR_W, SCR_H);
|
---|
258 |
|
---|
259 | if (tex == NULL) {
|
---|
260 | fail("SDL_CreateTexture() failed: %s", SDL_GetError());
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND) < 0) {
|
---|
264 | fail("SDL_SetTextureBlendMode() failed: %s", SDL_GetError());
|
---|
265 | }
|
---|
266 |
|
---|
267 | SDL_AtomicSet(&frame, 1);
|
---|
268 | }
|
---|
269 |
|
---|
270 | void vid_quit(void)
|
---|
271 | {
|
---|
272 | ver("vid quit");
|
---|
273 |
|
---|
274 | SDL_DestroyTexture(tex);
|
---|
275 | SDL_DestroyRenderer(ren);
|
---|
276 | SDL_DestroyWindow(win);
|
---|
277 | }
|
---|
278 |
|
---|
279 | bool vid_exec(void)
|
---|
280 | {
|
---|
281 | ver3("vid exec");
|
---|
282 |
|
---|
283 | static int32_t skip = 99999;
|
---|
284 | static int32_t line = 99999;
|
---|
285 |
|
---|
286 | if (++skip < 10) {
|
---|
287 | ver3("vid skip %d", skip);
|
---|
288 | return false;
|
---|
289 | }
|
---|
290 |
|
---|
291 | skip = 0;
|
---|
292 | int32_t vcr0 = mem[REG_VCR0];
|
---|
293 |
|
---|
294 | if ((vcr0 & VCR0_DEN) == 0) {
|
---|
295 | ver3("vid dis");
|
---|
296 | return false;
|
---|
297 | }
|
---|
298 |
|
---|
299 | if (++line < SCR_H + VBL_H) {
|
---|
300 | ver3("vid line %d", line);
|
---|
301 |
|
---|
302 | if (line < SCR_H) {
|
---|
303 | ++mem[REG_ATBAC];
|
---|
304 | }
|
---|
305 |
|
---|
306 | return line == SCR_H;
|
---|
307 | }
|
---|
308 |
|
---|
309 | // We get here every 4,000 invocations -> 25 fps.
|
---|
310 |
|
---|
311 | ver2("vid frame");
|
---|
312 | line = 0;
|
---|
313 |
|
---|
314 | int32_t dsba = mem[REG_DSBA];
|
---|
315 | bank = ((dsba & DSBA_BS0) != 0 ? 1 : 0) + ((dsba & DSBA_BS1) != 0 ? 2 : 0);
|
---|
316 |
|
---|
317 | int32_t rwba = mem[REG_RWBA];
|
---|
318 | reg_off = (rwba & 0xfff0) << 1;
|
---|
319 |
|
---|
320 | int32_t atba = mem[REG_ATBA];
|
---|
321 | mem[REG_ATBAC] = (uint16_t)atba;
|
---|
322 |
|
---|
323 | int32_t odtba = mem[REG_ODTBA] & 0xffc0;
|
---|
324 |
|
---|
325 | n_objs = 0;
|
---|
326 |
|
---|
327 | for (int32_t i = 0; i < 16; ++i) {
|
---|
328 | int32_t flips[SCR_H];
|
---|
329 | int32_t n_flips = 0;
|
---|
330 |
|
---|
331 | uint16_t *od = mem + odtba + 4 * i;
|
---|
332 |
|
---|
333 | if ((od[0] & OD0_BLA) != 0) {
|
---|
334 | ver3("vid obj %d blanked", i);
|
---|
335 | continue;
|
---|
336 | }
|
---|
337 |
|
---|
338 | int32_t w64 = (od[1] & 0xfc00) >> 10;
|
---|
339 |
|
---|
340 | if (w64 == 0) {
|
---|
341 | ver3("vid obj %d empty", i);
|
---|
342 | continue;
|
---|
343 | }
|
---|
344 |
|
---|
345 | int32_t mask = 1 << i;
|
---|
346 |
|
---|
347 | for (int32_t k = 0; k < SCR_H; ++k) {
|
---|
348 | if ((mem[atba + k] & mask) == 0) {
|
---|
349 | flips[n_flips] = k;
|
---|
350 | ++n_flips;
|
---|
351 | }
|
---|
352 | }
|
---|
353 |
|
---|
354 | if (n_flips == 0) {
|
---|
355 | ver3("vid obj %d unused", i);
|
---|
356 | continue;
|
---|
357 | }
|
---|
358 |
|
---|
359 | if (n_flips == 1) {
|
---|
360 | flips[n_flips] = SCR_H;
|
---|
361 | ++n_flips;
|
---|
362 | }
|
---|
363 |
|
---|
364 | bool cb = (od[0] & OD0_CB) != 0;
|
---|
365 | bool tde = (od[0] & OD0_TDE) != 0;
|
---|
366 |
|
---|
367 | int32_t x = (od[1] & 0x03ff) * 2;
|
---|
368 | int32_t off, fon_h, w;
|
---|
369 |
|
---|
370 | if (cb) {
|
---|
371 | off = od[2];
|
---|
372 | int32_t vcr1 = mem[REG_VCR1];
|
---|
373 | fon_h = (vcr1 & 0xf000) >> 12;
|
---|
374 | w = w64 * 4 / 3 * 2 * 8;
|
---|
375 | }
|
---|
376 | else {
|
---|
377 | off = ((od[0] & 0x00c0) << 10) | od[2];
|
---|
378 | fon_h = -1;
|
---|
379 | w = w64 * 16;
|
---|
380 | }
|
---|
381 |
|
---|
382 | obj_t *obj = objs + n_objs;
|
---|
383 |
|
---|
384 | obj->x = x;
|
---|
385 | obj->y = flips[0];
|
---|
386 | obj->w = w;
|
---|
387 | obj->h = flips[1] - flips[0];
|
---|
388 | obj->mem = mem + off;
|
---|
389 | obj->tran = tde;
|
---|
390 | obj->fon_h = fon_h;
|
---|
391 |
|
---|
392 | ver2("vid obj %d %c %c %d:%d %d+%d 0x%05x",
|
---|
393 | i, cb ? 'c' : 'b', tde ? 't' : '-', flips[0], flips[1], x, w, off);
|
---|
394 |
|
---|
395 | ++n_objs;
|
---|
396 | }
|
---|
397 |
|
---|
398 | SDL_AtomicAdd(&frame, 1);
|
---|
399 | return false;
|
---|
400 | }
|
---|
401 |
|
---|
402 | uint32_t vid_read(uint32_t off, int32_t sz)
|
---|
403 | {
|
---|
404 | ver2("vid rd %d 0x%05x:%d", bank, off, sz * 8);
|
---|
405 |
|
---|
406 | int32_t off16 = (int32_t)(off / 2);
|
---|
407 |
|
---|
408 | if (sz != 2 || bank >= N_BANKS || off % 2 != 0 || off16 >= WIN_SZ_W) {
|
---|
409 | fail("invalid vid rd %d %u:%d", bank, off, sz * 8);
|
---|
410 | }
|
---|
411 |
|
---|
412 | if (off16 >= reg_off && off16 < reg_off + 16) {
|
---|
413 | return mem[off16 - reg_off];
|
---|
414 | }
|
---|
415 |
|
---|
416 | return mem[bank * WIN_SZ_W + off16];
|
---|
417 | }
|
---|
418 |
|
---|
419 | void vid_write(uint32_t off, int32_t sz, uint32_t val)
|
---|
420 | {
|
---|
421 | ver2("vid wr %d 0x%05x:%d 0x%0*x", bank, off, sz * 8, sz * 2, val);
|
---|
422 |
|
---|
423 | int32_t off16 = (int32_t)(off / 2);
|
---|
424 |
|
---|
425 | if (sz != 2 || bank >= N_BANKS || off % 2 != 0 || (off16 >= WIN_SZ_W && off16 != PAL_OFF)) {
|
---|
426 | fail("invalid vid wr %d %u:%d", bank, off, sz * 8);
|
---|
427 | }
|
---|
428 |
|
---|
429 | if (off16 == PAL_OFF) {
|
---|
430 | int32_t i_pal = ((int32_t)val & 0x03c0) >> 6;
|
---|
431 |
|
---|
432 | uint32_t r = ((val & 0x0004) >> 1) | ((val & 0x0020) >> 5);
|
---|
433 | uint32_t g = ((val & 0x0002) >> 0) | ((val & 0x0010) >> 4);
|
---|
434 | uint32_t b = ((val & 0x0001) << 1) | ((val & 0x0008) >> 3);
|
---|
435 |
|
---|
436 | r = (r ^ 3) * 85;
|
---|
437 | g = (g ^ 3) * 85;
|
---|
438 | b = (b ^ 3) * 85;
|
---|
439 |
|
---|
440 | pal[i_pal] = (r << 24) | (g << 16) | (b << 8) | 0xff;
|
---|
441 | return;
|
---|
442 | }
|
---|
443 |
|
---|
444 | if (off16 >= reg_off && off16 < reg_off + 16) {
|
---|
445 | mem[off16 - reg_off] = (uint16_t)val;
|
---|
446 | return;
|
---|
447 | }
|
---|
448 |
|
---|
449 | mem[bank * WIN_SZ_W + off16] = (uint16_t)val;
|
---|
450 | }
|
---|