source: buchla-emu/emu/vid.c@ 72b5744

Last change on this file since 72b5744 was 67fecc3, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Merge LCD emulation.

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