source: buchla-emu/emu/lcd.c@ 18e37d6

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

Refactoring for master merge.

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