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

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

Minor cleanup.

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