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