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

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

Support ASCII BEL code (G).

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