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
RevLine 
[a06aa8b]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
[2147e53]15 * "gpl.txt" in the top directory of this repository.
[a06aa8b]16 */
17
18#include <all.h>
19
[4c71d39]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__)
[a06aa8b]23
[0726522]24#define BEL_CYC 10000
25
[bb4fd0c]26#define CON_W 80
27#define CON_H 25
28
29#define CON_BGR 0x00000000
[0726522]30#define CON_BEL 0x00808080
[bb4fd0c]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
[3c30832]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 {
[375f7fb]42 bool irq_r;
43 bool irq_t;
44 bool rdr_ok;
[3c30832]45 uint8_t rdr;
46} state_t;
47
[4c71d39]48int32_t ser_verbose = 0;
[a06aa8b]49
[3c30832]50static state_t state[] = {
[375f7fb]51 { .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 },
52 { .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 }
[3c30832]53};
54
[bb4fd0c]55static uint8_t mem[CON_H][CON_W + 1];
56
[caff491]57static SDL_Window *win;
58static SDL_Renderer *ren;
59
[bb4fd0c]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;
[0726522]67static int32_t bel = 0;
[bb4fd0c]68
69static void update(void)
70{
[0726522]71 if (SDL_FillRect(sur, NULL, bel == 0 ? CON_BGR : CON_BEL) < 0) {
[bb4fd0c]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
[caff491]103 SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, sur);
[bb4fd0c]104
105 if (tex == NULL) {
106 fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
107 }
108
[caff491]109 if (SDL_RenderCopy(ren, tex, NULL, NULL) < 0) {
[bb4fd0c]110 fail("SDL_RenderCopy() failed: %s", SDL_GetError());
111 }
112
113 SDL_DestroyTexture(tex);
[caff491]114 SDL_RenderPresent(ren);
[bb4fd0c]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{
[2a75e87]167 if (c < 32) {
168 switch (c) {
169 case '\r':
170 cur_x = 0;
171 break;
[bb4fd0c]172
[2a75e87]173 case '\n':
174 down();
175 break;
[bb4fd0c]176
[2a75e87]177 case '\b':
178 back();
179 break;
[bb4fd0c]180
[0726522]181 case '\a':
182 bel = BEL_CYC;
183 break;
184
[2a75e87]185 default:
[bb4fd0c]186 echo('^');
187 echo((uint8_t)(c + '@'));
188 return;
189 }
[2a75e87]190 }
191 else {
[bb4fd0c]192 mem[cur_y][cur_x] = c;
193 forw();
194 }
195
196 update();
197}
198
[caff491]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
[bb4fd0c]206void ser_key(SDL_KeyboardEvent *ev)
207{
208 switch (ev->keysym.sym) {
209 case SDLK_BACKSPACE:
[3c30832]210 out(1, '\b');
[bb4fd0c]211 break;
212
213 case SDLK_RETURN:
[3c30832]214 out(1, '\r');
[bb4fd0c]215 break;
[a23f3d9]216
217 default:
218 if ((ev->keysym.mod & KMOD_CTRL) != 0 &&
219 ev->keysym.sym >= SDLK_a && ev->keysym.sym <= SDLK_z) {
[3c30832]220 out(1, (uint8_t)(ev->keysym.sym - SDLK_a + 1));
[a23f3d9]221 }
222
223 break;
[bb4fd0c]224 }
225}
226
227void ser_text(SDL_TextInputEvent *ev)
228{
229 for (int32_t i = 0; ev->text[i] != 0; ++i) {
[3c30832]230 out(1, (uint8_t)ev->text[i]);
[bb4fd0c]231 }
232}
233
[a06aa8b]234void ser_init(void)
235{
236 ver("ser init");
[bb4fd0c]237
[caff491]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
[bb4fd0c]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();
[a06aa8b]287}
288
289void ser_quit(void)
290{
291 ver("ser quit");
[bb4fd0c]292
293 SDL_FreeSurface(sur);
294 TTF_CloseFont(fon);
[caff491]295
296 SDL_DestroyRenderer(ren);
297 SDL_DestroyWindow(win);
[a06aa8b]298}
299
[3c30832]300bool ser_exec(void)
[a06aa8b]301{
[4c71d39]302 ver3("ser exec");
[0726522]303
304 if (bel > 0) {
305 --bel;
306
307 if (bel == BEL_CYC - 1 || bel == 0) {
308 update();
309 }
310 }
311
[375f7fb]312 return state[0].irq_r || state[0].irq_t || state[1].irq_r || state[1].irq_t;
[a06aa8b]313}
314
315uint32_t ser_read(uint32_t off, int32_t sz)
316{
[4c71d39]317 ver2("ser rd %u:%d", off, sz * 8);
[3c30832]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:
[375f7fb]330 rv = (uint32_t)(0xc0 | (state[un].rdr_ok ? 0x01 : 0x00));
331 state[un].irq_r = false;
332 state[un].irq_t = false;
[3c30832]333 ver2("ISR[%d] 0x%02x", un, rv);
334 break;
335
336 case REG_TDR_RDR:
337 rv = state[un].rdr;
[375f7fb]338 state[un].rdr_ok = false;
[3c30832]339 ver2("RDR[%d] 0x%02x", un, rv);
340 break;
341
342 default:
343 rv = 0x00;
344 break;
345 }
346
347 return rv;
[a06aa8b]348}
349
350void ser_write(uint32_t off, int32_t sz, uint32_t val)
351{
[4c71d39]352 ver2("ser wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
[3c30832]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);
[375f7fb]365 state[un].irq_t = true;
[3c30832]366 break;
367
368 default:
369 break;
370 }
[a06aa8b]371}
Note: See TracBrowser for help on using the repository browser.