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_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 |
|
---|
47 | #define OD0_TDE 0x0004
|
---|
48 | #define OD0_BLA 0x0010
|
---|
49 | #define OD0_CB 0x0800
|
---|
50 |
|
---|
51 | #define WIN_SZ_W 0x10000
|
---|
52 | #define MEM_SZ_W 0x20000
|
---|
53 |
|
---|
54 | #define N_BANKS 2
|
---|
55 |
|
---|
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 |
|
---|
67 | static int32_t reg_off = REG_OFF;
|
---|
68 |
|
---|
69 | static uint16_t mem[MEM_SZ_W];
|
---|
70 | static int32_t bank = 0;
|
---|
71 |
|
---|
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 | }
|
---|
161 |
|
---|
162 | void vid_init(void)
|
---|
163 | {
|
---|
164 | ver("vid init");
|
---|
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);
|
---|
195 | }
|
---|
196 |
|
---|
197 | void vid_quit(void)
|
---|
198 | {
|
---|
199 | ver("vid quit");
|
---|
200 |
|
---|
201 | SDL_DestroyTexture(tex);
|
---|
202 | SDL_DestroyRenderer(ren);
|
---|
203 | SDL_DestroyWindow(win);
|
---|
204 | }
|
---|
205 |
|
---|
206 | bool vid_exec(void)
|
---|
207 | {
|
---|
208 | ver3("vid exec");
|
---|
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;
|
---|
251 |
|
---|
252 | n_bm_objs = 0;
|
---|
253 |
|
---|
254 | for (int32_t i = 0; i < 16; ++i) {
|
---|
255 | int32_t flips[SCR_H];
|
---|
256 | int32_t n_flips = 0;
|
---|
257 |
|
---|
258 | uint16_t *od = mem + odtba + 4 * i;
|
---|
259 |
|
---|
260 | if ((od[0] & OD0_BLA) != 0) {
|
---|
261 | ver3("vid obj %d blanked", i);
|
---|
262 | continue;
|
---|
263 | }
|
---|
264 |
|
---|
265 | int32_t w = (od[1] & 0xfc00) >> 6;
|
---|
266 |
|
---|
267 | if (w == 0) {
|
---|
268 | ver3("vid obj %d empty", i);
|
---|
269 | continue;
|
---|
270 | }
|
---|
271 |
|
---|
272 | int32_t mask = 1 << i;
|
---|
273 |
|
---|
274 | for (int32_t k = 0; k < SCR_H; ++k) {
|
---|
275 | if ((mem[atba + k] & mask) == 0) {
|
---|
276 | flips[n_flips] = k;
|
---|
277 | ++n_flips;
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | if (n_flips == 0) {
|
---|
282 | ver3("vid obj %d unused", i);
|
---|
283 | continue;
|
---|
284 | }
|
---|
285 |
|
---|
286 | if (n_flips == 1) {
|
---|
287 | flips[n_flips] = SCR_H;
|
---|
288 | ++n_flips;
|
---|
289 | }
|
---|
290 |
|
---|
291 | bool cb = (od[0] & OD0_CB) != 0;
|
---|
292 | bool tde = (od[0] & OD0_TDE) != 0;
|
---|
293 |
|
---|
294 | int32_t x = (od[1] & 0x03ff) * 2;
|
---|
295 | int32_t off = ((od[0] & 0x00c0) << 10) | od[2];
|
---|
296 |
|
---|
297 | ver2("vid obj %d %c %c %d:%d %d+%d 0x%05x",
|
---|
298 | i, cb ? 'c' : 'b', tde ? 't' : '-', flips[0], flips[1], x, w, off);
|
---|
299 |
|
---|
300 | if (!cb) {
|
---|
301 | bm_obj_t *bm_obj = bm_objs + n_bm_objs;
|
---|
302 |
|
---|
303 | bm_obj->x = x;
|
---|
304 | bm_obj->y = flips[0];
|
---|
305 | bm_obj->w = w;
|
---|
306 | bm_obj->h = flips[1] - flips[0];
|
---|
307 | bm_obj->mem = mem + off;
|
---|
308 | bm_obj->tran = tde;
|
---|
309 |
|
---|
310 | ++n_bm_objs;
|
---|
311 | }
|
---|
312 | else {
|
---|
313 | int32_t vcr1 = mem[REG_VCR1];
|
---|
314 | int32_t fon_h = (vcr1 & 0xf000) >> 12;
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | SDL_AtomicAdd(&frame, 1);
|
---|
319 | return false;
|
---|
320 | }
|
---|
321 |
|
---|
322 | uint32_t vid_read(uint32_t off, int32_t sz)
|
---|
323 | {
|
---|
324 | ver2("vid rd %d 0x%05x:%d", bank, off, sz * 8);
|
---|
325 |
|
---|
326 | int32_t off16 = (int32_t)(off / 2);
|
---|
327 |
|
---|
328 | if (sz != 2 || bank >= N_BANKS || off % 2 != 0 || off16 >= WIN_SZ_W) {
|
---|
329 | fail("invalid vid rd %d %u:%d", bank, off, sz * 8);
|
---|
330 | }
|
---|
331 |
|
---|
332 | if (off16 >= reg_off && off16 < reg_off + 16) {
|
---|
333 | return mem[off16 - reg_off];
|
---|
334 | }
|
---|
335 |
|
---|
336 | return mem[bank * WIN_SZ_W + off16];
|
---|
337 | }
|
---|
338 |
|
---|
339 | void vid_write(uint32_t off, int32_t sz, uint32_t val)
|
---|
340 | {
|
---|
341 | ver2("vid wr %d 0x%05x:%d 0x%0*x", bank, off, sz * 8, sz * 2, val);
|
---|
342 |
|
---|
343 | int32_t off16 = (int32_t)(off / 2);
|
---|
344 |
|
---|
345 | if (sz != 2 || bank >= N_BANKS || off % 2 != 0 || (off16 >= WIN_SZ_W && off16 != PAL_OFF)) {
|
---|
346 | fail("invalid vid wr %d %u:%d", bank, off, sz * 8);
|
---|
347 | }
|
---|
348 |
|
---|
349 | if (off16 == PAL_OFF) {
|
---|
350 | int32_t i_pal = ((int32_t)val & 0x03c0) >> 6;
|
---|
351 |
|
---|
352 | uint32_t r = ((val & 0x0004) >> 1) | ((val & 0x0020) >> 5);
|
---|
353 | uint32_t g = ((val & 0x0002) >> 0) | ((val & 0x0010) >> 4);
|
---|
354 | uint32_t b = ((val & 0x0001) << 1) | ((val & 0x0008) >> 3);
|
---|
355 |
|
---|
356 | r = (r ^ 3) * 85;
|
---|
357 | g = (g ^ 3) * 85;
|
---|
358 | b = (b ^ 3) * 85;
|
---|
359 |
|
---|
360 | pal[i_pal] = (r << 24) | (g << 16) | (b << 8) | 0xff;
|
---|
361 | return;
|
---|
362 | }
|
---|
363 |
|
---|
364 | if (off16 >= reg_off && off16 < reg_off + 16) {
|
---|
365 | mem[off16 - reg_off] = (uint16_t)val;
|
---|
366 | return;
|
---|
367 | }
|
---|
368 |
|
---|
369 | mem[bank * WIN_SZ_W + off16] = (uint16_t)val;
|
---|
370 | }
|
---|