source: buchla-emu/emu/ser.c@ 657abdf

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

Serial FIFO. Mouse support.

  • 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 + 1];
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 + 1));
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
259 if (SDL_LockMutex(cpu_mutex) < 0) {
260 fail("SDL_LockMutex() failed: %s", SDL_GetError());
261 }
262
263 memcpy(line, mem[y], CON_W + 1);
264
265 if (SDL_UnlockMutex(cpu_mutex) < 0) {
266 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
267 }
268
269 SDL_Surface *lin = TTF_RenderText_Blended(fon, line, CON_FGR);
270
271 if (lin == NULL) {
272 fail("TTF_RenderText_Blended() failed: %s", TTF_GetError());
273 }
274
275 if (SDL_BlitSurface(lin, NULL, sur, &(SDL_Rect){
276 .x = 0,
277 .y = y * fon_h,
278 .w = CON_W * fon_w,
279 .h = fon_h
280 })) {
281 fail("SDL_BlitSurface() failed: %s", SDL_GetError());
282 }
283
284 SDL_FreeSurface(lin);
285 }
286
287 SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, sur);
288
289 if (tex == NULL) {
290 fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
291 }
292
293 if (SDL_RenderCopy(ren, tex, NULL, NULL) < 0) {
294 fail("SDL_RenderCopy() failed: %s", SDL_GetError());
295 }
296
297 SDL_DestroyTexture(tex);
298 SDL_RenderPresent(ren);
299}
300
301void ser_key(SDL_KeyboardEvent *ev)
302{
303 switch (ev->keysym.sym) {
304 case SDLK_BACKSPACE:
305 out(1, '\b');
306 break;
307
308 case SDLK_RETURN:
309 out(1, '\r');
310 break;
311
312 default:
313 if ((ev->keysym.mod & KMOD_CTRL) != 0 &&
314 ev->keysym.sym >= SDLK_a && ev->keysym.sym <= SDLK_z) {
315 out(1, (uint8_t)(ev->keysym.sym - SDLK_a + 1));
316 }
317
318 break;
319 }
320}
321
322void ser_text(SDL_TextInputEvent *ev)
323{
324 for (int32_t i = 0; ev->text[i] != 0; ++i) {
325 out(1, (uint8_t)ev->text[i]);
326 }
327}
328
329static void mou_ev(void)
330{
331 ver2("ser mou ev (%d, %d) %c %c",
332 mou_dx, mou_dy, mou_l ? 'l' : '-', mou_r ? 'r' : '-');
333
334 int32_t dx = mou_dx;
335 int32_t dy = mou_dy;
336
337 if (dx < -128) {
338 dx = -128;
339 }
340 else if (dx > 127) {
341 dx = 127;
342 }
343
344 if (dy < -128) {
345 dy = -128;
346 }
347 else if (dy > 127) {
348 dy = 127;
349 }
350
351 dx = dx & 0xff;
352 dy = dy & 0xff;
353
354 int32_t b1 = 0x40;
355
356 if (mou_l) {
357 b1 |= 0x20;
358 }
359
360 if (mou_r) {
361 b1 |= 0x10;
362 }
363
364 b1 |= (dy & 0xc0) >> 4;
365 b1 |= (dx & 0xc0) >> 6;
366
367 int32_t b2 = dx & 0x3f;
368 int32_t b3 = dy & 0x3f;
369
370 out_lk(0, (uint8_t)b1);
371 out_lk(0, (uint8_t)b2);
372 out_lk(0, (uint8_t)b3);
373
374 mou_dx = mou_dy = 0;
375}
376
377static void mou_ev_chk(void)
378{
379 if (--mou > 0) {
380 return;
381 }
382
383 mou = MOU_CYC;
384
385 if (mou_dx == 0 && mou_dy == 0) {
386 return;
387 }
388
389 mou_ev();
390}
391
392void ser_mou_res(void)
393{
394 if (SDL_LockMutex(cpu_mutex) < 0) {
395 fail("SDL_LockMutex() failed: %s", SDL_GetError());
396 }
397
398 mou = MOU_CYC;
399 mou_dx = mou_dy = 0;
400 mou_l = mou_r = false;
401
402 if (SDL_UnlockMutex(cpu_mutex) < 0) {
403 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
404 }
405}
406
407void ser_mou_mov(SDL_MouseMotionEvent *ev)
408{
409 if (SDL_LockMutex(cpu_mutex) < 0) {
410 fail("SDL_LockMutex() failed: %s", SDL_GetError());
411 }
412
413 mou_dx += ev->xrel;
414 mou_dy += ev->yrel;
415
416 if (SDL_UnlockMutex(cpu_mutex) < 0) {
417 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
418 }
419}
420
421void ser_mou_dn(SDL_MouseButtonEvent *ev)
422{
423 if (SDL_LockMutex(cpu_mutex) < 0) {
424 fail("SDL_LockMutex() failed: %s", SDL_GetError());
425 }
426
427 if (ev->button == SDL_BUTTON_LEFT) {
428 mou_l = true;
429 }
430 else if (ev->button == SDL_BUTTON_RIGHT) {
431 mou_r = true;
432 }
433 else {
434 return;
435 }
436
437 mou_ev();
438
439 if (SDL_UnlockMutex(cpu_mutex) < 0) {
440 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
441 }
442}
443
444void ser_mou_up(SDL_MouseButtonEvent *ev)
445{
446 if (SDL_LockMutex(cpu_mutex) < 0) {
447 fail("SDL_LockMutex() failed: %s", SDL_GetError());
448 }
449
450 if (ev->button == SDL_BUTTON_LEFT) {
451 mou_l = false;
452 }
453 else if (ev->button == SDL_BUTTON_RIGHT) {
454 mou_r = false;
455 }
456 else {
457 return;
458 }
459
460 mou_ev();
461
462 if (SDL_UnlockMutex(cpu_mutex) < 0) {
463 fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
464 }
465}
466
467void ser_init(void)
468{
469 ver("ser init");
470
471 win = SDL_CreateWindow("Serial Console", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
472 WIN_W, WIN_H, 0);
473
474 if (win == NULL) {
475 fail("SDL_CreateWindow() failed: %s", SDL_GetError());
476 }
477
478 ren = SDL_CreateRenderer(win, -1, 0);
479
480 if (ren == NULL) {
481 fail("SDL_CreateRenderer() failed: %s", SDL_GetError());
482 }
483
484 SDL_AtomicSet(&frame, 1);
485
486 SDL_RWops *ops = SDL_RWFromFile(font, "rb");
487
488 if (ops == NULL) {
489 fail("error while opening font file %s: %s", font, SDL_GetError());
490 }
491
492 fon = TTF_OpenFontRW(ops, 1, 32);
493
494 if (fon == NULL) {
495 fail("error while loading font file %s: %s", font, TTF_GetError());
496 }
497
498 fon_h = TTF_FontLineSkip(fon);
499
500 if (TTF_GlyphMetrics(fon, 'X', NULL, NULL, NULL, NULL, &fon_w) < 0) {
501 fail("error while measuring font width: %s", TTF_GetError());
502 }
503
504 sur_w = CON_W * fon_w;
505 sur_h = CON_H * fon_h;
506
507 sur = SDL_CreateRGBSurface(0, sur_w, sur_h, 32, 0, 0, 0, 0);
508
509 if (sur == NULL) {
510 fail("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
511 }
512
513 for (int32_t y = 0; y < CON_H; ++y) {
514 for (int32_t x = 0; x < CON_W; ++x) {
515 mem[y][x] = ' ';
516 }
517
518 mem[y][CON_W] = 0;
519 }
520}
521
522void ser_quit(void)
523{
524 ver("ser quit");
525
526 SDL_FreeSurface(sur);
527 TTF_CloseFont(fon);
528
529 SDL_DestroyRenderer(ren);
530 SDL_DestroyWindow(win);
531}
532
533bool ser_exec(void)
534{
535 ver3("ser exec");
536
537 if (bel > 0) {
538 --bel;
539
540 if (bel == BEL_CYC - 1 || bel == 0) {
541 SDL_AtomicAdd(&frame, 1);
542 }
543 }
544
545 mou_ev_chk();
546
547 return state[0].irq_r || state[0].irq_t || state[1].irq_r || state[1].irq_t;
548}
549
550uint32_t ser_read(uint32_t off, int32_t sz)
551{
552 ver2("ser rd %u:%d", off, sz * 8);
553
554 if (sz != 1 || off > 7) {
555 fail("invalid ser rd %u:%d", off, sz * 8);
556 }
557
558 int32_t rg = (int32_t)(off % 4);
559 int32_t un = (int32_t)(off / 4);
560
561 uint32_t rv;
562
563 switch (rg) {
564 case REG_IER_ISR:
565 rv = (uint32_t)(0xc0 | (state[un].rdr_ok ? 0x01 : 0x00));
566 state[un].irq_r = false;
567 state[un].irq_t = false;
568 ver2("ISR[%d] 0x%02x", un, rv);
569 break;
570
571 case REG_TDR_RDR:
572 rv = state[un].rdr;
573 state[un].rdr_ok = false;
574 ver2("RDR[%d] 0x%02x", un, rv);
575 break;
576
577 default:
578 rv = 0x00;
579 break;
580 }
581
582 if (!state[un].irq_r && !state[un].rdr_ok) {
583 xmit(un);
584 }
585
586 return rv;
587}
588
589void ser_write(uint32_t off, int32_t sz, uint32_t val)
590{
591 ver2("ser wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
592
593 if (sz != 1 || off > 7) {
594 fail("invalid ser wr %u:%d", off, sz * 8);
595 }
596
597 int32_t rg = (int32_t)(off % 4);
598 int32_t un = (int32_t)(off / 4);
599
600 switch (rg) {
601 case REG_TDR_RDR:
602 ver2("TDR[%d] 0x%02x", un, val);
603
604 if (un == 1) {
605 echo((uint8_t)val);
606 }
607 else {
608 mouse((uint8_t)val);
609 }
610
611 state[un].irq_t = true;
612 break;
613
614 default:
615 break;
616 }
617}
Note: See TracBrowser for help on using the repository browser.