source: buchla-emu/emu/ser.c@ 8c8a883

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

Don't keep NUL bytes in display memory.

  • Property mode set to 100644
File size: 10.6 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#define MOU_CYC 10000
31
32#define CON_W 80
33#define CON_H 25
34
35#define CON_BGR 0x00000000
36#define CON_BEL 0x00808080
37#define CON_CUR 0x00e87000
38#define CON_FGR ((SDL_Color){ .r = 255, .b = 255, .g = 255, .a = 255 })
39
40#define BUF_SZ 16
41
42#define REG_IER_ISR 0
43#define REG_CFR_SR 1
44#define REG_CDR_TBR 2
45#define REG_TDR_RDR 3
46
47typedef struct {
48 int32_t buf_hd;
49 int32_t buf_tl;
50 uint8_t buf[BUF_SZ];
51 bool irq_r;
52 bool irq_t;
53 bool rdr_ok;
54 uint8_t rdr;
55} state_t;
56
57static state_t state[] = {
58 { .buf_hd = 0, .buf_tl = 0, .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 },
59 { .buf_hd = 0, .buf_tl = 0, .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 }
60};
61
62static uint8_t mem[CON_H][CON_W];
63
64static SDL_Window *win;
65static SDL_Renderer *ren;
66static SDL_atomic_t frame;
67
68static TTF_Font *fon;
69static int32_t fon_w, fon_h;
70
71static int32_t sur_w, sur_h;
72static SDL_Surface *sur;
73
74static int32_t cur_x = 0, cur_y = 0;
75static int32_t bel = 0;
76
77static int32_t mou;
78static int32_t mou_dx, mou_dy;
79static bool mou_l, mou_r;
80
81static void scroll(void)
82{
83 memmove(mem, mem + 1, (CON_H - 1) * CON_W);
84 memset(mem + (CON_H - 1), ' ', CON_W);
85}
86
87static void forw(void)
88{
89 if (cur_x < CON_W - 1) {
90 ++cur_x;
91 return;
92 }
93
94 if (cur_y == CON_H - 1) {
95 cur_x = 0;
96 scroll();
97 return;
98 }
99
100 cur_x = 0;
101 ++cur_y;
102}
103
104static void back(void)
105{
106 if (cur_x > 0) {
107 --cur_x;
108 return;
109 }
110
111 if (cur_y == 0) {
112 return;
113 }
114
115 cur_x = CON_W - 1;
116 --cur_y;
117}
118
119static void down(void)
120{
121 if (cur_y < CON_H - 1) {
122 ++cur_y;
123 return;
124 }
125
126 scroll();
127}
128
129static void echo(uint8_t c)
130{
131 if (c < 32) {
132 switch (c) {
133 case '\r':
134 cur_x = 0;
135 break;
136
137 case '\n':
138 down();
139 break;
140
141 case '\b':
142 back();
143 break;
144
145 case '\a':
146 bel = BEL_CYC;
147 break;
148
149 default:
150 echo('^');
151 echo((uint8_t)(c + '@'));
152 return;
153 }
154 }
155 else {
156 mem[cur_y][cur_x] = c;
157 forw();
158 }
159
160 SDL_AtomicAdd(&frame, 1);
161}
162
163static void xmit(int32_t un)
164{
165 int32_t i = state[un].buf_tl;
166 ver2("ser xmit %d %d", i, state[un].buf_hd);
167
168 if (i >= state[un].buf_hd) {
169 return;
170 }
171
172 uint8_t byte = state[un].buf[i % BUF_SZ];
173 ver2("ser xmit 0x%02x", byte);
174
175 state[un].rdr = byte;
176 state[un].rdr_ok = true;
177 state[un].irq_r = true;
178
179 state[un].buf_tl = i + 1;
180
181 if (state[un].buf_tl >= BUF_SZ) {
182 state[un].buf_hd -= BUF_SZ;
183 state[un].buf_tl -= BUF_SZ;
184 ver2("ser adj %d %d", state[un].buf_tl, state[un].buf_hd);
185 }
186}
187
188static void out_lk(int32_t un, uint8_t c)
189{
190 int32_t i = state[un].buf_hd;
191 ver2("ser out %d %d 0x%02x", state[un].buf_tl, i, c);
192
193 if (i >= state[un].buf_tl + BUF_SZ) {
194 err("serial port %d losing data", un);
195 return;
196 }
197
198 state[un].buf[i % BUF_SZ] = c;
199 state[un].buf_hd = i + 1;
200
201 if (!state[un].irq_r && !state[un].rdr_ok) {
202 xmit(un);
203 }
204}
205
206static void out(int32_t un, uint8_t c)
207{
208 if (SDL_LockMutex(cpu_mutex) < 0) {
209 fail("SDL_LockMutex() failed: %s", SDL_GetError());
210 }
211
212 out_lk(un, c);
213
214 if (SDL_UnlockMutex(cpu_mutex) < 0) {
215 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
216 }
217}
218
219static void mouse(uint8_t c)
220{
221 if (c == 't') {
222 ver2("ser mou init");
223 }
224
225 out_lk(0, 'V');
226 out_lk(0, 'O');
227}
228
229void ser_sdl(void)
230{
231 ver3("ser_sdl()");
232
233 static int32_t last = 0;
234 int32_t now = SDL_AtomicGet(&frame);
235
236 if (last == now) {
237 ver3("no update");
238 return;
239 }
240
241 last = now;
242
243 if (SDL_FillRect(sur, NULL, bel == 0 ? CON_BGR : CON_BEL) < 0) {
244 fail("SDL_FillRect() failed: %s", SDL_GetError());
245 }
246
247 if (SDL_FillRect(sur, &(SDL_Rect){
248 .x = cur_x * fon_w,
249 .y = cur_y * fon_h,
250 .w = fon_w,
251 .h = fon_h
252 }, CON_CUR) < 0) {
253 fail("SDL_FillRect() failed: %s", SDL_GetError());
254 }
255
256 for (int32_t y = 0; y < CON_H; ++y) {
257 char line[CON_W + 1];
258 line[CON_W] = 0;
259
260 if (SDL_LockMutex(cpu_mutex) < 0) {
261 fail("SDL_LockMutex() failed: %s", SDL_GetError());
262 }
263
264 memcpy(line, mem[y], CON_W);
265
266 if (SDL_UnlockMutex(cpu_mutex) < 0) {
267 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
268 }
269
270 SDL_Surface *lin = TTF_RenderText_Blended(fon, line, CON_FGR);
271
272 if (lin == NULL) {
273 fail("TTF_RenderText_Blended() failed: %s", TTF_GetError());
274 }
275
276 if (SDL_BlitSurface(lin, NULL, sur, &(SDL_Rect){
277 .x = 0,
278 .y = y * fon_h,
279 .w = CON_W * fon_w,
280 .h = fon_h
281 })) {
282 fail("SDL_BlitSurface() failed: %s", SDL_GetError());
283 }
284
285 SDL_FreeSurface(lin);
286 }
287
288 SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, sur);
289
290 if (tex == NULL) {
291 fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
292 }
293
294 if (SDL_RenderCopy(ren, tex, NULL, NULL) < 0) {
295 fail("SDL_RenderCopy() failed: %s", SDL_GetError());
296 }
297
298 SDL_DestroyTexture(tex);
299 SDL_RenderPresent(ren);
300}
301
302void ser_key(SDL_KeyboardEvent *ev)
303{
304 switch (ev->keysym.sym) {
305 case SDLK_BACKSPACE:
306 out(1, '\b');
307 break;
308
309 case SDLK_RETURN:
310 out(1, '\r');
311 break;
312
313 default:
314 if ((ev->keysym.mod & KMOD_CTRL) != 0 &&
315 ev->keysym.sym >= SDLK_a && ev->keysym.sym <= SDLK_z) {
316 out(1, (uint8_t)(ev->keysym.sym - SDLK_a + 1));
317 }
318
319 break;
320 }
321}
322
323void ser_text(SDL_TextInputEvent *ev)
324{
325 for (int32_t i = 0; ev->text[i] != 0; ++i) {
326 out(1, (uint8_t)ev->text[i]);
327 }
328}
329
330static void mou_ev(void)
331{
332 ver2("ser mou ev (%d, %d) %c %c",
333 mou_dx, mou_dy, mou_l ? 'l' : '-', mou_r ? 'r' : '-');
334
335 int32_t dx = mou_dx;
336 int32_t dy = mou_dy;
337
338 if (dx < -128) {
339 dx = -128;
340 }
341 else if (dx > 127) {
342 dx = 127;
343 }
344
345 if (dy < -128) {
346 dy = -128;
347 }
348 else if (dy > 127) {
349 dy = 127;
350 }
351
352 dx = dx & 0xff;
353 dy = dy & 0xff;
354
355 int32_t b1 = 0x40;
356
357 if (mou_l) {
358 b1 |= 0x20;
359 }
360
361 if (mou_r) {
362 b1 |= 0x10;
363 }
364
365 b1 |= (dy & 0xc0) >> 4;
366 b1 |= (dx & 0xc0) >> 6;
367
368 int32_t b2 = dx & 0x3f;
369 int32_t b3 = dy & 0x3f;
370
371 out_lk(0, (uint8_t)b1);
372 out_lk(0, (uint8_t)b2);
373 out_lk(0, (uint8_t)b3);
374
375 mou_dx = mou_dy = 0;
376}
377
378static void mou_ev_chk(void)
379{
380 if (--mou > 0) {
381 return;
382 }
383
384 mou = MOU_CYC;
385
386 if (mou_dx == 0 && mou_dy == 0) {
387 return;
388 }
389
390 mou_ev();
391}
392
393void ser_mou_res(void)
394{
395 if (SDL_LockMutex(cpu_mutex) < 0) {
396 fail("SDL_LockMutex() failed: %s", SDL_GetError());
397 }
398
399 mou = MOU_CYC;
400 mou_dx = mou_dy = 0;
401 mou_l = mou_r = false;
402
403 if (SDL_UnlockMutex(cpu_mutex) < 0) {
404 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
405 }
406}
407
408void ser_mou_mov(SDL_MouseMotionEvent *ev)
409{
410 if (SDL_LockMutex(cpu_mutex) < 0) {
411 fail("SDL_LockMutex() failed: %s", SDL_GetError());
412 }
413
414 mou_dx += ev->xrel;
415 mou_dy += ev->yrel;
416
417 if (SDL_UnlockMutex(cpu_mutex) < 0) {
418 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
419 }
420}
421
422void ser_mou_dn(SDL_MouseButtonEvent *ev)
423{
424 if (SDL_LockMutex(cpu_mutex) < 0) {
425 fail("SDL_LockMutex() failed: %s", SDL_GetError());
426 }
427
428 if (ev->button == SDL_BUTTON_LEFT) {
429 mou_l = true;
430 }
431 else if (ev->button == SDL_BUTTON_RIGHT) {
432 mou_r = true;
433 }
434 else {
435 return;
436 }
437
438 mou_ev();
439
440 if (SDL_UnlockMutex(cpu_mutex) < 0) {
441 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
442 }
443}
444
445void ser_mou_up(SDL_MouseButtonEvent *ev)
446{
447 if (SDL_LockMutex(cpu_mutex) < 0) {
448 fail("SDL_LockMutex() failed: %s", SDL_GetError());
449 }
450
451 if (ev->button == SDL_BUTTON_LEFT) {
452 mou_l = false;
453 }
454 else if (ev->button == SDL_BUTTON_RIGHT) {
455 mou_r = false;
456 }
457 else {
458 return;
459 }
460
461 mou_ev();
462
463 if (SDL_UnlockMutex(cpu_mutex) < 0) {
464 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
465 }
466}
467
468void ser_init(void)
469{
470 ver("ser init");
471
472 win = SDL_CreateWindow("Serial Console", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
473 WIN_W, WIN_H, 0);
474
475 if (win == NULL) {
476 fail("SDL_CreateWindow() failed: %s", SDL_GetError());
477 }
478
479 ren = SDL_CreateRenderer(win, -1, 0);
480
481 if (ren == NULL) {
482 fail("SDL_CreateRenderer() failed: %s", SDL_GetError());
483 }
484
485 SDL_AtomicSet(&frame, 1);
486
487 SDL_RWops *ops = SDL_RWFromFile(font, "rb");
488
489 if (ops == NULL) {
490 fail("error while opening font file %s: %s", font, SDL_GetError());
491 }
492
493 fon = TTF_OpenFontRW(ops, 1, 32);
494
495 if (fon == NULL) {
496 fail("error while loading font file %s: %s", font, TTF_GetError());
497 }
498
499 fon_h = TTF_FontLineSkip(fon);
500
501 if (TTF_GlyphMetrics(fon, 'X', NULL, NULL, NULL, NULL, &fon_w) < 0) {
502 fail("error while measuring font width: %s", TTF_GetError());
503 }
504
505 sur_w = CON_W * fon_w;
506 sur_h = CON_H * fon_h;
507
508 sur = SDL_CreateRGBSurface(0, sur_w, sur_h, 32, 0, 0, 0, 0);
509
510 if (sur == NULL) {
511 fail("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
512 }
513
514 for (int32_t y = 0; y < CON_H; ++y) {
515 for (int32_t x = 0; x < CON_W; ++x) {
516 mem[y][x] = ' ';
517 }
518 }
519}
520
521void ser_quit(void)
522{
523 ver("ser quit");
524
525 SDL_FreeSurface(sur);
526 TTF_CloseFont(fon);
527
528 SDL_DestroyRenderer(ren);
529 SDL_DestroyWindow(win);
530}
531
532bool ser_exec(void)
533{
534 ver3("ser exec");
535
536 if (bel > 0) {
537 --bel;
538
539 if (bel == BEL_CYC - 1 || bel == 0) {
540 SDL_AtomicAdd(&frame, 1);
541 }
542 }
543
544 mou_ev_chk();
545
546 return state[0].irq_r || state[0].irq_t || state[1].irq_r || state[1].irq_t;
547}
548
549uint32_t ser_read(uint32_t off, int32_t sz)
550{
551 ver2("ser rd %u:%d", off, sz * 8);
552
553 if (sz != 1 || off > 7) {
554 fail("invalid ser rd %u:%d", off, sz * 8);
555 }
556
557 int32_t rg = (int32_t)(off % 4);
558 int32_t un = (int32_t)(off / 4);
559
560 uint32_t rv;
561
562 switch (rg) {
563 case REG_IER_ISR:
564 rv = (uint32_t)(0xc0 | (state[un].rdr_ok ? 0x01 : 0x00));
565 state[un].irq_r = false;
566 state[un].irq_t = false;
567 ver2("ISR[%d] 0x%02x", un, rv);
568 break;
569
570 case REG_TDR_RDR:
571 rv = state[un].rdr;
572 state[un].rdr_ok = false;
573 ver2("RDR[%d] 0x%02x", un, rv);
574 break;
575
576 default:
577 rv = 0x00;
578 break;
579 }
580
581 if (!state[un].irq_r && !state[un].rdr_ok) {
582 xmit(un);
583 }
584
585 return rv;
586}
587
588void ser_write(uint32_t off, int32_t sz, uint32_t val)
589{
590 ver2("ser wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
591
592 if (sz != 1 || off > 7) {
593 fail("invalid ser wr %u:%d", off, sz * 8);
594 }
595
596 int32_t rg = (int32_t)(off % 4);
597 int32_t un = (int32_t)(off / 4);
598
599 switch (rg) {
600 case REG_TDR_RDR:
601 ver2("TDR[%d] 0x%02x", un, val);
602
603 if (un == 1) {
604 echo((uint8_t)val);
605 }
606 else {
607 mouse((uint8_t)val);
608 }
609
610 state[un].irq_t = true;
611 break;
612
613 default:
614 break;
615 }
616}
Note: See TracBrowser for help on using the repository browser.