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

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

Successfully loaded midas.abs from disk.

  • Property mode set to 100644
File size: 6.7 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
[c5b6c90]24int32_t ser_verbose = 0;
25
26#define WIN_W (1520 * 2 / 3)
27#define WIN_H (950 * 2 / 3)
28
[0726522]29#define BEL_CYC 10000
30
[bb4fd0c]31#define CON_W 80
32#define CON_H 25
33
34#define CON_BGR 0x00000000
[0726522]35#define CON_BEL 0x00808080
[bb4fd0c]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
[3c30832]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
46typedef struct {
[375f7fb]47 bool irq_r;
48 bool irq_t;
49 bool rdr_ok;
[3c30832]50 uint8_t rdr;
51} state_t;
52
53static state_t state[] = {
[375f7fb]54 { .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 },
55 { .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 }
[3c30832]56};
57
[bb4fd0c]58static uint8_t mem[CON_H][CON_W + 1];
59
[caff491]60static SDL_Window *win;
61static SDL_Renderer *ren;
62
[bb4fd0c]63static TTF_Font *fon;
64static int32_t fon_w, fon_h;
65
66static int32_t sur_w, sur_h;
67static SDL_Surface *sur;
68
69static int32_t cur_x = 0, cur_y = 0;
[0726522]70static int32_t bel = 0;
[bb4fd0c]71
72static void update(void)
73{
[0726522]74 if (SDL_FillRect(sur, NULL, bel == 0 ? CON_BGR : CON_BEL) < 0) {
[bb4fd0c]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
[caff491]106 SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, sur);
[bb4fd0c]107
108 if (tex == NULL) {
109 fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
110 }
111
[caff491]112 if (SDL_RenderCopy(ren, tex, NULL, NULL) < 0) {
[bb4fd0c]113 fail("SDL_RenderCopy() failed: %s", SDL_GetError());
114 }
115
116 SDL_DestroyTexture(tex);
[caff491]117 SDL_RenderPresent(ren);
[bb4fd0c]118}
119
120static 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
126static 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
143static 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
158static void down(void)
159{
160 if (cur_y < CON_H - 1) {
161 ++cur_y;
162 return;
163 }
164
165 scroll();
166}
167
168static void echo(uint8_t c)
169{
[2a75e87]170 if (c < 32) {
171 switch (c) {
172 case '\r':
173 cur_x = 0;
174 break;
[bb4fd0c]175
[2a75e87]176 case '\n':
177 down();
178 break;
[bb4fd0c]179
[2a75e87]180 case '\b':
181 back();
182 break;
[bb4fd0c]183
[0726522]184 case '\a':
185 bel = BEL_CYC;
186 break;
187
[2a75e87]188 default:
[bb4fd0c]189 echo('^');
190 echo((uint8_t)(c + '@'));
191 return;
192 }
[2a75e87]193 }
194 else {
[bb4fd0c]195 mem[cur_y][cur_x] = c;
196 forw();
197 }
198
199 update();
200}
201
[caff491]202static 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
[bb4fd0c]209void ser_key(SDL_KeyboardEvent *ev)
210{
211 switch (ev->keysym.sym) {
212 case SDLK_BACKSPACE:
[3c30832]213 out(1, '\b');
[bb4fd0c]214 break;
215
216 case SDLK_RETURN:
[3c30832]217 out(1, '\r');
[bb4fd0c]218 break;
[a23f3d9]219
220 default:
221 if ((ev->keysym.mod & KMOD_CTRL) != 0 &&
222 ev->keysym.sym >= SDLK_a && ev->keysym.sym <= SDLK_z) {
[3c30832]223 out(1, (uint8_t)(ev->keysym.sym - SDLK_a + 1));
[a23f3d9]224 }
225
226 break;
[bb4fd0c]227 }
228}
229
230void ser_text(SDL_TextInputEvent *ev)
231{
232 for (int32_t i = 0; ev->text[i] != 0; ++i) {
[3c30832]233 out(1, (uint8_t)ev->text[i]);
[bb4fd0c]234 }
235}
236
[a06aa8b]237void ser_init(void)
238{
239 ver("ser init");
[bb4fd0c]240
[caff491]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
[bb4fd0c]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();
[a06aa8b]290}
291
292void ser_quit(void)
293{
294 ver("ser quit");
[bb4fd0c]295
296 SDL_FreeSurface(sur);
297 TTF_CloseFont(fon);
[caff491]298
299 SDL_DestroyRenderer(ren);
300 SDL_DestroyWindow(win);
[a06aa8b]301}
302
[3c30832]303bool ser_exec(void)
[a06aa8b]304{
[4c71d39]305 ver3("ser exec");
[0726522]306
307 if (bel > 0) {
308 --bel;
309
310 if (bel == BEL_CYC - 1 || bel == 0) {
311 update();
312 }
313 }
314
[375f7fb]315 return state[0].irq_r || state[0].irq_t || state[1].irq_r || state[1].irq_t;
[a06aa8b]316}
317
318uint32_t ser_read(uint32_t off, int32_t sz)
319{
[4c71d39]320 ver2("ser rd %u:%d", off, sz * 8);
[3c30832]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:
[375f7fb]333 rv = (uint32_t)(0xc0 | (state[un].rdr_ok ? 0x01 : 0x00));
334 state[un].irq_r = false;
335 state[un].irq_t = false;
[3c30832]336 ver2("ISR[%d] 0x%02x", un, rv);
337 break;
338
339 case REG_TDR_RDR:
340 rv = state[un].rdr;
[375f7fb]341 state[un].rdr_ok = false;
[3c30832]342 ver2("RDR[%d] 0x%02x", un, rv);
343 break;
344
345 default:
346 rv = 0x00;
347 break;
348 }
349
350 return rv;
[a06aa8b]351}
352
353void ser_write(uint32_t off, int32_t sz, uint32_t val)
354{
[4c71d39]355 ver2("ser wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
[3c30832]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);
[375f7fb]368 state[un].irq_t = true;
[3c30832]369 break;
370
371 default:
372 break;
373 }
[a06aa8b]374}
Note: See TracBrowser for help on using the repository browser.