source: buchla-emu/emu/ser.c@ 7cc6ac0

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

Separate thread for rendering.

  • Property mode set to 100644
File size: 7.3 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(ser_verbose, 0, __VA_ARGS__)
21#define ver2(...) _ver(ser_verbose, 1, __VA_ARGS__)
22#define ver3(...) _ver(ser_verbose, 2, __VA_ARGS__)
23
24int32_t ser_verbose = 0;
25
26#define WIN_W (1520 * 2 / 3)
27#define WIN_H (950 * 2 / 3)
28
29#define BEL_CYC 10000
30
31#define CON_W 80
32#define CON_H 25
33
34#define CON_BGR 0x00000000
35#define CON_BEL 0x00808080
36#define CON_CUR 0x00e87000
37#define CON_FGR ((SDL_Color){ .r = 255, .b = 255, .g = 255, .a = 255 })
38
39#define CON_FONT "ttf/vera-sans-mono.ttf"
40
41#define REG_IER_ISR 0
42#define REG_CFR_SR 1
43#define REG_CDR_TBR 2
44#define REG_TDR_RDR 3
45
46typedef struct {
47 bool irq_r;
48 bool irq_t;
49 bool rdr_ok;
50 uint8_t rdr;
51} state_t;
52
53static state_t state[] = {
54 { .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 },
55 { .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 }
56};
57
58static uint8_t mem[CON_H][CON_W + 1];
59
60static SDL_Window *win;
61static SDL_Renderer *ren;
62static SDL_atomic_t frame;
63
64static TTF_Font *fon;
65static int32_t fon_w, fon_h;
66
67static int32_t sur_w, sur_h;
68static SDL_Surface *sur;
69
70static int32_t cur_x = 0, cur_y = 0;
71static int32_t bel = 0;
72
73static void scroll(void)
74{
75 memmove(mem, mem + 1, (CON_H - 1) * (CON_W + 1));
76 memset(mem + (CON_H - 1), ' ', CON_W);
77}
78
79static void forw(void)
80{
81 if (cur_x < CON_W - 1) {
82 ++cur_x;
83 return;
84 }
85
86 if (cur_y == CON_H - 1) {
87 cur_x = 0;
88 scroll();
89 return;
90 }
91
92 cur_x = 0;
93 ++cur_y;
94}
95
96static void back(void)
97{
98 if (cur_x > 0) {
99 --cur_x;
100 return;
101 }
102
103 if (cur_y == 0) {
104 return;
105 }
106
107 cur_x = CON_W - 1;
108 --cur_y;
109}
110
111static void down(void)
112{
113 if (cur_y < CON_H - 1) {
114 ++cur_y;
115 return;
116 }
117
118 scroll();
119}
120
121static void echo(uint8_t c)
122{
123 if (c < 32) {
124 switch (c) {
125 case '\r':
126 cur_x = 0;
127 break;
128
129 case '\n':
130 down();
131 break;
132
133 case '\b':
134 back();
135 break;
136
137 case '\a':
138 bel = BEL_CYC;
139 break;
140
141 default:
142 echo('^');
143 echo((uint8_t)(c + '@'));
144 return;
145 }
146 }
147 else {
148 mem[cur_y][cur_x] = c;
149 forw();
150 }
151
152 SDL_AtomicAdd(&frame, 1);
153}
154
155static void out(int32_t un, uint8_t c)
156{
157 if (SDL_LockMutex(cpu_mutex) < 0) {
158 fail("SDL_LockMutex() failed: %s", SDL_GetError());
159 }
160
161 state[un].rdr = c;
162 state[un].rdr_ok = true;
163 state[un].irq_r = true;
164
165 if (SDL_UnlockMutex(cpu_mutex) < 0) {
166 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
167 }
168}
169
170void ser_sdl(void)
171{
172 ver3("ser_sdl()");
173
174 static int32_t last = 0;
175 int32_t now = SDL_AtomicGet(&frame);
176
177 if (last == now) {
178 ver3("no update");
179 return;
180 }
181
182 last = now;
183
184 if (SDL_FillRect(sur, NULL, bel == 0 ? CON_BGR : CON_BEL) < 0) {
185 fail("SDL_FillRect() failed: %s", SDL_GetError());
186 }
187
188 if (SDL_FillRect(sur, &(SDL_Rect){
189 .x = cur_x * fon_w,
190 .y = cur_y * fon_h,
191 .w = fon_w,
192 .h = fon_h
193 }, CON_CUR) < 0) {
194 fail("SDL_FillRect() failed: %s", SDL_GetError());
195 }
196
197 for (int32_t y = 0; y < CON_H; ++y) {
198 char line[CON_W + 1];
199
200 if (SDL_LockMutex(cpu_mutex) < 0) {
201 fail("SDL_LockMutex() failed: %s", SDL_GetError());
202 }
203
204 memcpy(line, mem[y], CON_W + 1);
205
206 if (SDL_UnlockMutex(cpu_mutex) < 0) {
207 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
208 }
209
210 SDL_Surface *lin = TTF_RenderText_Blended(fon, line, CON_FGR);
211
212 if (lin == NULL) {
213 fail("TTF_RenderText_Blended() failed: %s", TTF_GetError());
214 }
215
216 if (SDL_BlitSurface(lin, NULL, sur, &(SDL_Rect){
217 .x = 0,
218 .y = y * fon_h,
219 .w = CON_W * fon_w,
220 .h = fon_h
221 })) {
222 fail("SDL_BlitSurface() failed: %s", SDL_GetError());
223 }
224
225 SDL_FreeSurface(lin);
226 }
227
228 SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, sur);
229
230 if (tex == NULL) {
231 fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
232 }
233
234 if (SDL_RenderCopy(ren, tex, NULL, NULL) < 0) {
235 fail("SDL_RenderCopy() failed: %s", SDL_GetError());
236 }
237
238 SDL_DestroyTexture(tex);
239 SDL_RenderPresent(ren);
240}
241
242void ser_key(SDL_KeyboardEvent *ev)
243{
244 switch (ev->keysym.sym) {
245 case SDLK_BACKSPACE:
246 out(1, '\b');
247 break;
248
249 case SDLK_RETURN:
250 out(1, '\r');
251 break;
252
253 default:
254 if ((ev->keysym.mod & KMOD_CTRL) != 0 &&
255 ev->keysym.sym >= SDLK_a && ev->keysym.sym <= SDLK_z) {
256 out(1, (uint8_t)(ev->keysym.sym - SDLK_a + 1));
257 }
258
259 break;
260 }
261}
262
263void ser_text(SDL_TextInputEvent *ev)
264{
265 for (int32_t i = 0; ev->text[i] != 0; ++i) {
266 out(1, (uint8_t)ev->text[i]);
267 }
268}
269
270void ser_init(void)
271{
272 ver("ser init");
273
274 win = SDL_CreateWindow("Serial Console", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
275 WIN_W, WIN_H, 0);
276
277 if (win == NULL) {
278 fail("SDL_CreateWindow() failed: %s", SDL_GetError());
279 }
280
281 ren = SDL_CreateRenderer(win, -1, 0);
282
283 if (ren == NULL) {
284 fail("SDL_CreateRenderer() failed: %s", SDL_GetError());
285 }
286
287 SDL_AtomicSet(&frame, 1);
288
289 SDL_RWops *ops = SDL_RWFromFile(CON_FONT, "rb");
290
291 if (ops == NULL) {
292 fail("error while opening font file " CON_FONT ": %s", SDL_GetError());
293 }
294
295 fon = TTF_OpenFontRW(ops, 1, 32);
296
297 if (fon == NULL) {
298 fail("error while loading font file " CON_FONT ": %s", TTF_GetError());
299 }
300
301 fon_h = TTF_FontLineSkip(fon);
302
303 if (TTF_GlyphMetrics(fon, 'X', NULL, NULL, NULL, NULL, &fon_w) < 0) {
304 fail("error while measuring font width: %s", TTF_GetError());
305 }
306
307 sur_w = CON_W * fon_w;
308 sur_h = CON_H * fon_h;
309
310 sur = SDL_CreateRGBSurface(0, sur_w, sur_h, 32, 0, 0, 0, 0);
311
312 if (sur == NULL) {
313 fail("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
314 }
315
316 for (int32_t y = 0; y < CON_H; ++y) {
317 for (int32_t x = 0; x < CON_W; ++x) {
318 mem[y][x] = ' ';
319 }
320
321 mem[y][CON_W] = 0;
322 }
323}
324
325void ser_quit(void)
326{
327 ver("ser quit");
328
329 SDL_FreeSurface(sur);
330 TTF_CloseFont(fon);
331
332 SDL_DestroyRenderer(ren);
333 SDL_DestroyWindow(win);
334}
335
336bool ser_exec(void)
337{
338 ver3("ser exec");
339
340 if (bel > 0) {
341 --bel;
342
343 if (bel == BEL_CYC - 1 || bel == 0) {
344 SDL_AtomicAdd(&frame, 1);
345 }
346 }
347
348 return state[0].irq_r || state[0].irq_t || state[1].irq_r || state[1].irq_t;
349}
350
351uint32_t ser_read(uint32_t off, int32_t sz)
352{
353 ver2("ser rd %u:%d", off, sz * 8);
354
355 if (sz != 1 || off > 7) {
356 fail("invalid ser rd %u:%d", off, sz * 8);
357 }
358
359 int32_t rg = (int32_t)(off % 4);
360 int32_t un = (int32_t)(off / 4);
361
362 uint32_t rv;
363
364 switch (rg) {
365 case REG_IER_ISR:
366 rv = (uint32_t)(0xc0 | (state[un].rdr_ok ? 0x01 : 0x00));
367 state[un].irq_r = false;
368 state[un].irq_t = false;
369 ver2("ISR[%d] 0x%02x", un, rv);
370 break;
371
372 case REG_TDR_RDR:
373 rv = state[un].rdr;
374 state[un].rdr_ok = false;
375 ver2("RDR[%d] 0x%02x", un, rv);
376 break;
377
378 default:
379 rv = 0x00;
380 break;
381 }
382
383 return rv;
384}
385
386void ser_write(uint32_t off, int32_t sz, uint32_t val)
387{
388 ver2("ser wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
389
390 if (sz != 1 || off > 7) {
391 fail("invalid ser wr %u:%d", off, sz * 8);
392 }
393
394 int32_t rg = (int32_t)(off % 4);
395 int32_t un = (int32_t)(off / 4);
396
397 switch (rg) {
398 case REG_TDR_RDR:
399 ver2("TDR[%d] 0x%02x", un, val);
400 echo((uint8_t)val);
401 state[un].irq_t = true;
402 break;
403
404 default:
405 break;
406 }
407}
Note: See TracBrowser for help on using the repository browser.