source: buchla-emu/emu/lcd.c@ 99cc90a

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

Don't distort the font.

  • 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 (1615 * 2 / 3)
27#define WIN_H (304 * 2 / 3)
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 if (SDL_FillRect(txt, NULL, TXT_BGR) < 0) {
107 fail("SDL_FillRect() failed: %s", SDL_GetError());
108 }
109
110 if (SDL_AtomicGet(&ena) == 0) {
111 SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, txt);
112
113 if (tex == NULL) {
114 fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
115 }
116
117 if (SDL_RenderCopy(ren, tex, NULL, NULL) < 0) {
118 fail("SDL_RenderCopy() failed: %s", SDL_GetError());
119 }
120
121 SDL_DestroyTexture(tex);
122 SDL_RenderPresent(ren);
123 return;
124 }
125
126 for (int32_t y = 0; y < TXT_H; ++y) {
127 char line[TXT_W + 1];
128
129 if (SDL_LockMutex(cpu_mutex) < 0) {
130 fail("SDL_LockMutex() failed: %s", SDL_GetError());
131 }
132
133 memcpy(line, mem_txt + y * TXT_W, TXT_W);
134
135 if (SDL_UnlockMutex(cpu_mutex) < 0) {
136 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
137 }
138
139 for (int32_t x = 0; x < TXT_W; ++x) {
140 if (line[x] == 0x00) {
141 line[x] = ' ';
142 }
143 }
144
145 line[TXT_W] = 0;
146
147 SDL_Surface *lin = TTF_RenderText_Blended(fon, line, TXT_FGR);
148
149 if (lin == NULL) {
150 fail("TTF_RenderText_Blended() failed: %s", TTF_GetError());
151 }
152
153 if (SDL_BlitSurface(lin, NULL, txt, &(SDL_Rect){
154 .x = 0,
155 .y = y * fon_h,
156 .w = TXT_W * fon_w,
157 .h = fon_h
158 })) {
159 fail("SDL_BlitSurface() failed: %s", SDL_GetError());
160 }
161
162 SDL_FreeSurface(lin);
163 }
164
165 SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, txt);
166
167 if (tex == NULL) {
168 fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
169 }
170
171 if (SDL_RenderCopy(ren, tex, NULL, NULL) < 0) {
172 fail("SDL_RenderCopy() failed: %s", SDL_GetError());
173 }
174
175 SDL_DestroyTexture(tex);
176
177 void *buf;
178 int32_t pitch;
179
180 if (SDL_LockTexture(gfx, NULL, &buf, &pitch) < 0) {
181 fail("SDL_LockTexture() failed: %s", SDL_GetError());
182 }
183
184 uint32_t *pix = buf;
185
186 for (int32_t y = 0; y < GFX_H; ++y) {
187 for (int32_t x = 0; x < GFX_W; ++x) {
188 uint8_t b = mem_gfx[y * GFX_W + x];
189
190 for (int32_t p = 0; p < GFX_PIX; ++p) {
191 bool set = (b & (1 << (7 - p))) != 0;
192 *pix++ = set ? GFX_FGR : GFX_BGR;
193 }
194 }
195
196 pix += pitch / 4 - GFX_W * GFX_PIX;
197 }
198
199 SDL_UnlockTexture(gfx);
200
201 if (SDL_RenderCopy(ren, gfx, NULL, NULL) < 0) {
202 fail("SDL_RenderCopy() failed: %s", SDL_GetError());
203 }
204
205 SDL_RenderPresent(ren);
206}
207
208void lcd_init(void)
209{
210 ver("lcd init");
211
212 win = SDL_CreateWindow("LCD", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
213 WIN_W, WIN_H, 0);
214
215 if (win == NULL) {
216 fail("SDL_CreateWindow() failed: %s", SDL_GetError());
217 }
218
219 ren = SDL_CreateRenderer(win, -1, 0);
220
221 if (ren == NULL) {
222 fail("SDL_CreateRenderer() failed: %s", SDL_GetError());
223 }
224
225 gfx = SDL_CreateTexture(ren, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING,
226 GFX_W * GFX_PIX, GFX_H);
227
228 if (gfx == NULL) {
229 fail("SDL_CreateTexture() failed: %s", SDL_GetError());
230 }
231
232 if (SDL_SetTextureBlendMode(gfx, SDL_BLENDMODE_BLEND) < 0) {
233 fail("SDL_SetTextureBlendMode() failed: %s", SDL_GetError());
234 }
235
236 SDL_RWops *ops = SDL_RWFromFile(font, "rb");
237
238 if (ops == NULL) {
239 fail("error while opening font file %s: %s", font, SDL_GetError());
240 }
241
242 fon = TTF_OpenFontRW(ops, 1, 32);
243
244 if (fon == NULL) {
245 fail("error while loading font file %s: %s", font, TTF_GetError());
246 }
247
248 fon_h = TTF_FontLineSkip(fon);
249
250 if (TTF_GlyphMetrics(fon, 'X', NULL, NULL, NULL, NULL, &fon_w) < 0) {
251 fail("error while measuring font width: %s", TTF_GetError());
252 }
253
254 txt_w = TXT_W * fon_w;
255 txt_h = TXT_H * fon_h;
256
257 txt = SDL_CreateRGBSurfaceWithFormat(0, txt_w, txt_h, 32, SDL_PIXELFORMAT_RGBA8888);
258
259 if (txt == NULL) {
260 fail("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
261 }
262
263 for (int32_t i = 0; i < TXT_W * TXT_H; ++i) {
264 mem_txt[i] = ' ';
265 }
266}
267
268void lcd_quit(void)
269{
270 ver("lcd quit");
271
272 SDL_FreeSurface(txt);
273 TTF_CloseFont(fon);
274
275 SDL_DestroyTexture(gfx);
276
277 SDL_DestroyRenderer(ren);
278 SDL_DestroyWindow(win);
279}
280
281bool lcd_exec(void)
282{
283 ver3("lcd exec");
284 return false;
285}
286
287uint32_t lcd_read(uint32_t off, int32_t sz)
288{
289 ver2("lcd rd %u:%d", off, sz * 8);
290
291 if (sz != 1 || off != 1) {
292 fail("invalid lcd rd %u:%d", off, sz * 8);
293 }
294
295 switch (com) {
296 case G_MREAD:
297 if (cur >= BASE_TXT && cur < BASE_TXT + TXT_W * TXT_H) {
298 return mem_txt[cur - BASE_TXT];
299 }
300
301 if (cur >= BASE_GFX && cur < BASE_GFX + GFX_W * GFX_H) {
302 return mem_gfx[cur - BASE_GFX];
303 }
304
305 return 0x00;
306
307 default:
308 return 0x00;
309 }
310}
311
312static void proc_arg(int32_t val)
313{
314 switch (com) {
315 case G_MWRITE:
316 if (cur >= BASE_TXT && cur < BASE_TXT + TXT_W * TXT_H) {
317 mem_txt[cur - BASE_TXT] = (uint8_t)val;
318 }
319 else if (cur >= BASE_GFX && cur < BASE_GFX + GFX_W * GFX_H) {
320 mem_gfx[cur - BASE_GFX] = (uint8_t)val;
321 }
322
323 cur += dir;
324 SDL_AtomicAdd(&frame, 1);
325 break;
326
327 case G_CRSWR:
328 if (n_arg == 0) {
329 cur = val;
330 }
331 else if (n_arg == 1) {
332 cur |= val << 8;
333
334 if (cur < BASE_TXT ||
335 (cur >= BASE_TXT + TXT_W * TXT_H && cur < BASE_GFX) ||
336 cur >= BASE_GFX + GFX_W * GFX_H) {
337 fail("invalid address 0x%04x", cur);
338 }
339 }
340
341 break;
342
343 default:
344 break;
345 }
346}
347
348static void proc_com(int32_t val)
349{
350 switch (val) {
351 case G_CRSWR:
352 case G_MREAD:
353 case G_MWRITE:
354 com = val;
355 break;
356
357 case G_CRSMRT:
358 dir = DIR_RIGHT;
359 com = G_NONE;
360 break;
361
362 case G_CRSMUP:
363 dir = DIR_UP;
364 com = G_NONE;
365 break;
366
367 case G_CRSMDN:
368 dir = DIR_DOWN;
369 com = G_NONE;
370 break;
371
372 case G_DSPOFF:
373 SDL_AtomicSet(&ena, 0);
374 com = G_NONE;
375 break;
376
377 case G_DSPON:
378 SDL_AtomicSet(&ena, 1);
379 com = G_NONE;
380 break;
381
382 default:
383 com = G_NONE;
384 break;
385 }
386}
387
388void lcd_write(uint32_t off, int32_t sz, uint32_t val)
389{
390 ver2("lcd wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
391
392 if (sz != 1 || off > 1) {
393 fail("invalid lcd wr %u:%d", off, sz * 8);
394 }
395
396 switch (off) {
397 case REG_ARG:
398 proc_arg((int32_t)val);
399 ++n_arg;
400 break;
401
402 case REG_COM:
403 proc_com((int32_t)val);
404 n_arg = 0;
405 break;
406
407 default:
408 break;
409 }
410}
Note: See TracBrowser for help on using the repository browser.