source: buchla-emu/emu/ser.c@ 375f7fb

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

Better interrupt emulation.

  • Property mode set to 100644
File size: 6.0 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_r;
40 bool irq_t;
41 bool rdr_ok;
42 uint8_t rdr;
43} state_t;
44
45int32_t ser_verbose = 0;
46
47static state_t state[] = {
48 { .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 },
49 { .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 }
50};
51
52static uint8_t mem[CON_H][CON_W + 1];
53
54static TTF_Font *fon;
55static int32_t fon_w, fon_h;
56
57static int32_t sur_w, sur_h;
58static SDL_Surface *sur;
59
60static int32_t cur_x = 0, cur_y = 0;
61
62static void out(int32_t un, uint8_t c)
63{
64 state[un].rdr = c;
65 state[un].rdr_ok = true;
66 state[un].irq_r = true;
67}
68
69static void update(void)
70{
71 if (SDL_FillRect(sur, NULL, CON_BGR) < 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(sdl_ren, sur);
104
105 if (tex == NULL) {
106 fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
107 }
108
109 if (SDL_RenderCopy(sdl_ren, tex, NULL, NULL) < 0) {
110 fail("SDL_RenderCopy() failed: %s", SDL_GetError());
111 }
112
113 SDL_DestroyTexture(tex);
114 SDL_RenderPresent(sdl_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 default:
182 echo('^');
183 echo((uint8_t)(c + '@'));
184 return;
185 }
186 }
187 else {
188 mem[cur_y][cur_x] = c;
189 forw();
190 }
191
192 update();
193}
194
195void ser_key(SDL_KeyboardEvent *ev)
196{
197 switch (ev->keysym.sym) {
198 case SDLK_BACKSPACE:
199 out(1, '\b');
200 break;
201
202 case SDLK_RETURN:
203 out(1, '\r');
204 break;
205
206 default:
207 if ((ev->keysym.mod & KMOD_CTRL) != 0 &&
208 ev->keysym.sym >= SDLK_a && ev->keysym.sym <= SDLK_z) {
209 out(1, (uint8_t)(ev->keysym.sym - SDLK_a + 1));
210 }
211
212 break;
213 }
214}
215
216void ser_text(SDL_TextInputEvent *ev)
217{
218 for (int32_t i = 0; ev->text[i] != 0; ++i) {
219 out(1, (uint8_t)ev->text[i]);
220 }
221}
222
223void ser_init(void)
224{
225 ver("ser init");
226
227 SDL_RWops *ops = SDL_RWFromFile(CON_FONT, "rb");
228
229 if (ops == NULL) {
230 fail("error while opening font file " CON_FONT ": %s", SDL_GetError());
231 }
232
233 fon = TTF_OpenFontRW(ops, 1, 32);
234
235 if (fon == NULL) {
236 fail("error while loading font file " CON_FONT ": %s", TTF_GetError());
237 }
238
239 fon_h = TTF_FontLineSkip(fon);
240
241 if (TTF_GlyphMetrics(fon, 'X', NULL, NULL, NULL, NULL, &fon_w) < 0) {
242 fail("error while measuring font width: %s", TTF_GetError());
243 }
244
245 sur_w = CON_W * fon_w;
246 sur_h = CON_H * fon_h;
247
248 sur = SDL_CreateRGBSurface(0, sur_w, sur_h, 32, 0, 0, 0, 0);
249
250 if (sur == NULL) {
251 fail("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
252 }
253
254 for (int32_t y = 0; y < CON_H; ++y) {
255 for (int32_t x = 0; x < CON_W; ++x) {
256 mem[y][x] = ' ';
257 }
258
259 mem[y][CON_W] = 0;
260 }
261
262 update();
263}
264
265void ser_quit(void)
266{
267 ver("ser quit");
268
269 SDL_FreeSurface(sur);
270 TTF_CloseFont(fon);
271}
272
273bool ser_exec(void)
274{
275 ver3("ser exec");
276 return state[0].irq_r || state[0].irq_t || state[1].irq_r || state[1].irq_t;
277}
278
279uint32_t ser_read(uint32_t off, int32_t sz)
280{
281 ver2("ser rd %u:%d", off, sz * 8);
282
283 if (sz != 1 || off > 7) {
284 fail("invalid ser rd %u:%d", off, sz * 8);
285 }
286
287 int32_t rg = (int32_t)(off % 4);
288 int32_t un = (int32_t)(off / 4);
289
290 uint32_t rv;
291
292 switch (rg) {
293 case REG_IER_ISR:
294 rv = (uint32_t)(0xc0 | (state[un].rdr_ok ? 0x01 : 0x00));
295 state[un].irq_r = false;
296 state[un].irq_t = false;
297 ver2("ISR[%d] 0x%02x", un, rv);
298 break;
299
300 case REG_TDR_RDR:
301 rv = state[un].rdr;
302 state[un].rdr_ok = false;
303 ver2("RDR[%d] 0x%02x", un, rv);
304 break;
305
306 default:
307 rv = 0x00;
308 break;
309 }
310
311 return rv;
312}
313
314void ser_write(uint32_t off, int32_t sz, uint32_t val)
315{
316 ver2("ser wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
317
318 if (sz != 1 || off > 7) {
319 fail("invalid ser wr %u:%d", off, sz * 8);
320 }
321
322 int32_t rg = (int32_t)(off % 4);
323 int32_t un = (int32_t)(off / 4);
324
325 switch (rg) {
326 case REG_TDR_RDR:
327 ver2("TDR[%d] 0x%02x", un, val);
328 echo((uint8_t)val);
329 state[un].irq_t = true;
330 break;
331
332 default:
333 break;
334 }
335}
Note: See TracBrowser for help on using the repository browser.