source: buchla-emu/emu/ser.c@ 3c30832

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

Interrupt handling. Serial console shows ROMP.

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