source: buchla-emu/emu/ser.c@ caff491

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

Moved serial console window to ser emulation.

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