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