source: buchla-emu/emu/lcd.c@ cd50b8c

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

Simplified rendering.

  • Property mode set to 100644
File size: 7.9 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(lcd_verbose, 0, __VA_ARGS__)
21#define ver2(...) _ver(lcd_verbose, 1, __VA_ARGS__)
22#define ver3(...) _ver(lcd_verbose, 2, __VA_ARGS__)
23
24int32_t lcd_verbose = 0;
25
26#define WIN_W (510 * 2)
27#define WIN_H (64 * 2)
28
29#define GFX_BGR 0x00000000
30#define GFX_FGR 0xFFFFFFFF
31
32#define TXT_BGR 0x000000FF
33#define TXT_FGR ((SDL_Color){ .r = 255, .b = 255, .g = 255, .a = 255 })
34
35#define REG_ARG 0
36#define REG_COM 1
37
38#define G_NONE 0x00
39#define G_INIT 0x40
40#define G_MWRITE 0x42
41#define G_MREAD 0x43
42#define G_SETSAD 0x44
43#define G_CRSWR 0x46
44#define G_CRSRD 0x47
45#define G_CRSMRT 0x4C
46#define G_CRSMLT 0x4D
47#define G_CRSMUP 0x4E
48#define G_CRSMDN 0x4F
49#define G_ERASE 0x52
50#define G_SLEEP 0x53
51#define G_DSPOFF 0x58
52#define G_DSPON 0x59
53#define G_HSCRL 0x5A
54#define G_OVRLAY 0x5B
55#define G_CGRAM 0x5C
56#define G_CRSFRM 0x5D
57
58#define TXT_W 85
59#define TXT_H 8
60
61#define GFX_W 85
62#define GFX_H 64
63#define GFX_PIX 6
64
65#define BASE_TXT 0x0000
66#define BASE_GFX 0x2000
67
68#define DIR_UP -85
69#define DIR_DOWN 85
70#define DIR_RIGHT 1
71
72static uint8_t mem_txt[TXT_H * TXT_W];
73static uint8_t mem_gfx[GFX_H * GFX_W];
74
75static SDL_Window *win;
76static SDL_Renderer *ren;
77static SDL_atomic_t frame;
78static SDL_atomic_t ena;
79
80static TTF_Font *fon;
81static int32_t fon_w, fon_h;
82
83static int32_t txt_w, txt_h;
84static SDL_Surface *txt;
85static SDL_Texture *gfx;
86
87static int32_t com;
88static int32_t n_arg;
89static int32_t cur = BASE_TXT;
90static int32_t dir = DIR_RIGHT;
91
92void lcd_sdl(void)
93{
94 ver3("lcd_sdl()");
95
96 static int32_t last = 0;
97 int32_t now = SDL_AtomicGet(&frame);
98
99 if (last == now) {
100 ver3("no update");
101 return;
102 }
103
104 last = now;
105
106 void *buf;
107 int32_t pitch;
108
109 if (SDL_LockTexture(gfx, NULL, &buf, &pitch) < 0) {
110 fail("SDL_LockTexture() failed: %s", SDL_GetError());
111 }
112
113 uint32_t *pix = buf;
114
115 for (int32_t y = 0; y < GFX_H; ++y) {
116 for (int32_t x = 0; x < GFX_W * GFX_PIX; ++x) {
117 *pix++ = GFX_BGR;
118 }
119
120 pix += pitch / 4 - GFX_W * GFX_PIX;
121 }
122
123 if (SDL_AtomicGet(&ena) == 0) {
124 SDL_UnlockTexture(gfx);
125
126 if (SDL_RenderCopy(ren, gfx, NULL, NULL) < 0) {
127 fail("SDL_RenderCopy() failed: %s", SDL_GetError());
128 }
129
130 SDL_RenderPresent(ren);
131 return;
132 }
133
134 pix = buf;
135
136 for (int32_t y = 0; y < GFX_H; ++y) {
137 for (int32_t x = 0; x < GFX_W; ++x) {
138 uint8_t b = mem_gfx[y * GFX_W + x];
139
140 for (int32_t p = 0; p < GFX_PIX; ++p) {
141 if ((b & (1 << (7 - p))) != 0) {
142 *pix = GFX_FGR;
143 }
144
145 ++pix;
146 }
147 }
148
149 pix += pitch / 4 - GFX_W * GFX_PIX;
150 }
151
152 SDL_UnlockTexture(gfx);
153
154 if (SDL_FillRect(txt, NULL, TXT_BGR) < 0) {
155 fail("SDL_FillRect() failed: %s", SDL_GetError());
156 }
157
158 for (int32_t y = 0; y < TXT_H; ++y) {
159 char line[TXT_W + 1];
160
161 if (SDL_LockMutex(cpu_mutex) < 0) {
162 fail("SDL_LockMutex() failed: %s", SDL_GetError());
163 }
164
165 memcpy(line, mem_txt + y * TXT_W, TXT_W);
166
167 if (SDL_UnlockMutex(cpu_mutex) < 0) {
168 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
169 }
170
171 for (int32_t x = 0; x < TXT_W; ++x) {
172 if (line[x] == 0x00) {
173 line[x] = ' ';
174 }
175 }
176
177 line[TXT_W] = 0;
178
179 SDL_Surface *lin = TTF_RenderText_Blended(fon, line, TXT_FGR);
180
181 if (lin == NULL) {
182 fail("TTF_RenderText_Blended() failed: %s", TTF_GetError());
183 }
184
185 if (SDL_BlitSurface(lin, NULL, txt, &(SDL_Rect){
186 .x = 0,
187 .y = y * fon_h,
188 .w = TXT_W * fon_w,
189 .h = fon_h
190 })) {
191 fail("SDL_BlitSurface() failed: %s", SDL_GetError());
192 }
193
194 SDL_FreeSurface(lin);
195 }
196
197 SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, txt);
198
199 if (tex == NULL) {
200 fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
201 }
202
203 if (SDL_RenderCopy(ren, tex, NULL, NULL) < 0) {
204 fail("SDL_RenderCopy() failed: %s", SDL_GetError());
205 }
206
207 SDL_DestroyTexture(tex);
208
209 if (SDL_RenderCopy(ren, gfx, NULL, NULL) < 0) {
210 fail("SDL_RenderCopy() failed: %s", SDL_GetError());
211 }
212
213 SDL_RenderPresent(ren);
214}
215
216void lcd_init(void)
217{
218 ver("lcd init");
219
220 win = SDL_CreateWindow("LCD", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
221 WIN_W, WIN_H, 0);
222
223 if (win == NULL) {
224 fail("SDL_CreateWindow() failed: %s", SDL_GetError());
225 }
226
227 ren = SDL_CreateRenderer(win, -1, 0);
228
229 if (ren == NULL) {
230 fail("SDL_CreateRenderer() failed: %s", SDL_GetError());
231 }
232
233 gfx = SDL_CreateTexture(ren, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING,
234 GFX_W * GFX_PIX, GFX_H);
235
236 if (gfx == NULL) {
237 fail("SDL_CreateTexture() failed: %s", SDL_GetError());
238 }
239
240 if (SDL_SetTextureBlendMode(gfx, SDL_BLENDMODE_BLEND) < 0) {
241 fail("SDL_SetTextureBlendMode() failed: %s", SDL_GetError());
242 }
243
244 SDL_RWops *ops = SDL_RWFromFile(font, "rb");
245
246 if (ops == NULL) {
247 fail("error while opening font file %s: %s", font, SDL_GetError());
248 }
249
250 fon = TTF_OpenFontRW(ops, 1, 32);
251
252 if (fon == NULL) {
253 fail("error while loading font file %s: %s", font, TTF_GetError());
254 }
255
256 fon_h = TTF_FontLineSkip(fon);
257
258 if (TTF_GlyphMetrics(fon, 'X', NULL, NULL, NULL, NULL, &fon_w) < 0) {
259 fail("error while measuring font width: %s", TTF_GetError());
260 }
261
262 txt_w = TXT_W * fon_w;
263 txt_h = TXT_H * fon_h;
264
265 txt = SDL_CreateRGBSurfaceWithFormat(0, txt_w, txt_h, 32, SDL_PIXELFORMAT_RGBA8888);
266
267 if (txt == NULL) {
268 fail("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
269 }
270
271 for (int32_t i = 0; i < TXT_W * TXT_H; ++i) {
272 mem_txt[i] = ' ';
273 }
274}
275
276void lcd_quit(void)
277{
278 ver("lcd quit");
279
280 SDL_FreeSurface(txt);
281 TTF_CloseFont(fon);
282
283 SDL_DestroyTexture(gfx);
284
285 SDL_DestroyRenderer(ren);
286 SDL_DestroyWindow(win);
287}
288
289bool lcd_exec(void)
290{
291 ver3("lcd exec");
292 return false;
293}
294
295uint32_t lcd_read(uint32_t off, int32_t sz)
296{
297 ver2("lcd rd %u:%d", off, sz * 8);
298
299 if (sz != 1 || off != 1) {
300 fail("invalid lcd rd %u:%d", off, sz * 8);
301 }
302
303 switch (com) {
304 case G_MREAD:
305 if (cur >= BASE_TXT && cur < BASE_TXT + TXT_W * TXT_H) {
306 return mem_txt[cur - BASE_TXT];
307 }
308
309 if (cur >= BASE_GFX && cur < BASE_GFX + GFX_W * GFX_H) {
310 return mem_gfx[cur - BASE_GFX];
311 }
312
313 return 0x00;
314
315 default:
316 return 0x00;
317 }
318}
319
320static void proc_arg(int32_t val)
321{
322 switch (com) {
323 case G_MWRITE:
324 if (cur >= BASE_TXT && cur < BASE_TXT + TXT_W * TXT_H) {
325 mem_txt[cur - BASE_TXT] = (uint8_t)val;
326 }
327 else if (cur >= BASE_GFX && cur < BASE_GFX + GFX_W * GFX_H) {
328 mem_gfx[cur - BASE_GFX] = (uint8_t)val;
329 }
330
331 cur += dir;
332 SDL_AtomicAdd(&frame, 1);
333 break;
334
335 case G_CRSWR:
336 if (n_arg == 0) {
337 cur = val;
338 }
339 else if (n_arg == 1) {
340 cur |= val << 8;
341
342 if (cur < BASE_TXT ||
343 (cur >= BASE_TXT + TXT_W * TXT_H && cur < BASE_GFX) ||
344 cur >= BASE_GFX + GFX_W * GFX_H) {
345 fail("invalid address 0x%04x", cur);
346 }
347 }
348
349 break;
350
351 default:
352 break;
353 }
354}
355
356static void proc_com(int32_t val)
357{
358 switch (val) {
359 case G_CRSWR:
360 case G_MREAD:
361 case G_MWRITE:
362 com = val;
363 break;
364
365 case G_CRSMRT:
366 dir = DIR_RIGHT;
367 com = G_NONE;
368 break;
369
370 case G_CRSMUP:
371 dir = DIR_UP;
372 com = G_NONE;
373 break;
374
375 case G_CRSMDN:
376 dir = DIR_DOWN;
377 com = G_NONE;
378 break;
379
380 case G_DSPOFF:
381 SDL_AtomicSet(&ena, 0);
382 com = G_NONE;
383 break;
384
385 case G_DSPON:
386 SDL_AtomicSet(&ena, 1);
387 com = G_NONE;
388 break;
389
390 default:
391 com = G_NONE;
392 break;
393 }
394}
395
396void lcd_write(uint32_t off, int32_t sz, uint32_t val)
397{
398 ver2("lcd wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
399
400 if (sz != 1 || off > 1) {
401 fail("invalid lcd wr %u:%d", off, sz * 8);
402 }
403
404 switch (off) {
405 case REG_ARG:
406 proc_arg((int32_t)val);
407 ++n_arg;
408 break;
409
410 case REG_COM:
411 proc_com((int32_t)val);
412 n_arg = 0;
413 break;
414
415 default:
416 break;
417 }
418}
Note: See TracBrowser for help on using the repository browser.