source: buchla-emu/emu/vid.c@ 4ed9bfe

Last change on this file since 4ed9bfe was 4ed9bfe, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Support data keys, X, E, and M.

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