[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] | 24 | int32_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 |
|
---|
[3c30832] | 39 | #define REG_IER_ISR 0
|
---|
| 40 | #define REG_CFR_SR 1
|
---|
| 41 | #define REG_CDR_TBR 2
|
---|
| 42 | #define REG_TDR_RDR 3
|
---|
| 43 |
|
---|
| 44 | typedef struct {
|
---|
[375f7fb] | 45 | bool irq_r;
|
---|
| 46 | bool irq_t;
|
---|
| 47 | bool rdr_ok;
|
---|
[3c30832] | 48 | uint8_t rdr;
|
---|
| 49 | } state_t;
|
---|
| 50 |
|
---|
| 51 | static state_t state[] = {
|
---|
[375f7fb] | 52 | { .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 },
|
---|
| 53 | { .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 }
|
---|
[3c30832] | 54 | };
|
---|
| 55 |
|
---|
[bb4fd0c] | 56 | static uint8_t mem[CON_H][CON_W + 1];
|
---|
| 57 |
|
---|
[caff491] | 58 | static SDL_Window *win;
|
---|
| 59 | static SDL_Renderer *ren;
|
---|
[bdd5a63] | 60 | static SDL_atomic_t frame;
|
---|
[caff491] | 61 |
|
---|
[bb4fd0c] | 62 | static TTF_Font *fon;
|
---|
| 63 | static int32_t fon_w, fon_h;
|
---|
| 64 |
|
---|
| 65 | static int32_t sur_w, sur_h;
|
---|
| 66 | static SDL_Surface *sur;
|
---|
| 67 |
|
---|
| 68 | static int32_t cur_x = 0, cur_y = 0;
|
---|
[0726522] | 69 | static int32_t bel = 0;
|
---|
[bb4fd0c] | 70 |
|
---|
| 71 | static void scroll(void)
|
---|
| 72 | {
|
---|
| 73 | memmove(mem, mem + 1, (CON_H - 1) * (CON_W + 1));
|
---|
| 74 | memset(mem + (CON_H - 1), ' ', CON_W);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | static void forw(void)
|
---|
| 78 | {
|
---|
| 79 | if (cur_x < CON_W - 1) {
|
---|
| 80 | ++cur_x;
|
---|
| 81 | return;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | if (cur_y == CON_H - 1) {
|
---|
| 85 | cur_x = 0;
|
---|
| 86 | scroll();
|
---|
| 87 | return;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | cur_x = 0;
|
---|
| 91 | ++cur_y;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | static void back(void)
|
---|
| 95 | {
|
---|
| 96 | if (cur_x > 0) {
|
---|
| 97 | --cur_x;
|
---|
| 98 | return;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | if (cur_y == 0) {
|
---|
| 102 | return;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | cur_x = CON_W - 1;
|
---|
| 106 | --cur_y;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | static void down(void)
|
---|
| 110 | {
|
---|
| 111 | if (cur_y < CON_H - 1) {
|
---|
| 112 | ++cur_y;
|
---|
| 113 | return;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | scroll();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | static void echo(uint8_t c)
|
---|
| 120 | {
|
---|
[2a75e87] | 121 | if (c < 32) {
|
---|
| 122 | switch (c) {
|
---|
| 123 | case '\r':
|
---|
| 124 | cur_x = 0;
|
---|
| 125 | break;
|
---|
[bb4fd0c] | 126 |
|
---|
[2a75e87] | 127 | case '\n':
|
---|
| 128 | down();
|
---|
| 129 | break;
|
---|
[bb4fd0c] | 130 |
|
---|
[2a75e87] | 131 | case '\b':
|
---|
| 132 | back();
|
---|
| 133 | break;
|
---|
[bb4fd0c] | 134 |
|
---|
[0726522] | 135 | case '\a':
|
---|
| 136 | bel = BEL_CYC;
|
---|
| 137 | break;
|
---|
| 138 |
|
---|
[2a75e87] | 139 | default:
|
---|
[bb4fd0c] | 140 | echo('^');
|
---|
| 141 | echo((uint8_t)(c + '@'));
|
---|
| 142 | return;
|
---|
| 143 | }
|
---|
[2a75e87] | 144 | }
|
---|
| 145 | else {
|
---|
[bb4fd0c] | 146 | mem[cur_y][cur_x] = c;
|
---|
| 147 | forw();
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[bdd5a63] | 150 | SDL_AtomicAdd(&frame, 1);
|
---|
[bb4fd0c] | 151 | }
|
---|
| 152 |
|
---|
[caff491] | 153 | static void out(int32_t un, uint8_t c)
|
---|
| 154 | {
|
---|
[bdd5a63] | 155 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 156 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[caff491] | 159 | state[un].rdr = c;
|
---|
| 160 | state[un].rdr_ok = true;
|
---|
| 161 | state[un].irq_r = true;
|
---|
[bdd5a63] | 162 |
|
---|
| 163 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 164 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | void ser_sdl(void)
|
---|
| 169 | {
|
---|
| 170 | ver3("ser_sdl()");
|
---|
| 171 |
|
---|
| 172 | static int32_t last = 0;
|
---|
| 173 | int32_t now = SDL_AtomicGet(&frame);
|
---|
| 174 |
|
---|
| 175 | if (last == now) {
|
---|
| 176 | ver3("no update");
|
---|
| 177 | return;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | last = now;
|
---|
| 181 |
|
---|
| 182 | if (SDL_FillRect(sur, NULL, bel == 0 ? CON_BGR : CON_BEL) < 0) {
|
---|
| 183 | fail("SDL_FillRect() failed: %s", SDL_GetError());
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | if (SDL_FillRect(sur, &(SDL_Rect){
|
---|
| 187 | .x = cur_x * fon_w,
|
---|
| 188 | .y = cur_y * fon_h,
|
---|
| 189 | .w = fon_w,
|
---|
| 190 | .h = fon_h
|
---|
| 191 | }, CON_CUR) < 0) {
|
---|
| 192 | fail("SDL_FillRect() failed: %s", SDL_GetError());
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | for (int32_t y = 0; y < CON_H; ++y) {
|
---|
| 196 | char line[CON_W + 1];
|
---|
| 197 |
|
---|
| 198 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 199 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | memcpy(line, mem[y], CON_W + 1);
|
---|
| 203 |
|
---|
| 204 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 205 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | SDL_Surface *lin = TTF_RenderText_Blended(fon, line, CON_FGR);
|
---|
| 209 |
|
---|
| 210 | if (lin == NULL) {
|
---|
| 211 | fail("TTF_RenderText_Blended() failed: %s", TTF_GetError());
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | if (SDL_BlitSurface(lin, NULL, sur, &(SDL_Rect){
|
---|
| 215 | .x = 0,
|
---|
| 216 | .y = y * fon_h,
|
---|
| 217 | .w = CON_W * fon_w,
|
---|
| 218 | .h = fon_h
|
---|
| 219 | })) {
|
---|
| 220 | fail("SDL_BlitSurface() failed: %s", SDL_GetError());
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | SDL_FreeSurface(lin);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, sur);
|
---|
| 227 |
|
---|
| 228 | if (tex == NULL) {
|
---|
| 229 | fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | if (SDL_RenderCopy(ren, tex, NULL, NULL) < 0) {
|
---|
| 233 | fail("SDL_RenderCopy() failed: %s", SDL_GetError());
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | SDL_DestroyTexture(tex);
|
---|
| 237 | SDL_RenderPresent(ren);
|
---|
[caff491] | 238 | }
|
---|
| 239 |
|
---|
[bb4fd0c] | 240 | void ser_key(SDL_KeyboardEvent *ev)
|
---|
| 241 | {
|
---|
| 242 | switch (ev->keysym.sym) {
|
---|
| 243 | case SDLK_BACKSPACE:
|
---|
[3c30832] | 244 | out(1, '\b');
|
---|
[bb4fd0c] | 245 | break;
|
---|
| 246 |
|
---|
| 247 | case SDLK_RETURN:
|
---|
[3c30832] | 248 | out(1, '\r');
|
---|
[bb4fd0c] | 249 | break;
|
---|
[a23f3d9] | 250 |
|
---|
| 251 | default:
|
---|
| 252 | if ((ev->keysym.mod & KMOD_CTRL) != 0 &&
|
---|
| 253 | ev->keysym.sym >= SDLK_a && ev->keysym.sym <= SDLK_z) {
|
---|
[3c30832] | 254 | out(1, (uint8_t)(ev->keysym.sym - SDLK_a + 1));
|
---|
[a23f3d9] | 255 | }
|
---|
| 256 |
|
---|
| 257 | break;
|
---|
[bb4fd0c] | 258 | }
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | void ser_text(SDL_TextInputEvent *ev)
|
---|
| 262 | {
|
---|
| 263 | for (int32_t i = 0; ev->text[i] != 0; ++i) {
|
---|
[3c30832] | 264 | out(1, (uint8_t)ev->text[i]);
|
---|
[bb4fd0c] | 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[a06aa8b] | 268 | void ser_init(void)
|
---|
| 269 | {
|
---|
| 270 | ver("ser init");
|
---|
[bb4fd0c] | 271 |
|
---|
[caff491] | 272 | win = SDL_CreateWindow("Serial Console", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
---|
| 273 | WIN_W, WIN_H, 0);
|
---|
| 274 |
|
---|
| 275 | if (win == NULL) {
|
---|
| 276 | fail("SDL_CreateWindow() failed: %s", SDL_GetError());
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | ren = SDL_CreateRenderer(win, -1, 0);
|
---|
| 280 |
|
---|
| 281 | if (ren == NULL) {
|
---|
| 282 | fail("SDL_CreateRenderer() failed: %s", SDL_GetError());
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[bdd5a63] | 285 | SDL_AtomicSet(&frame, 1);
|
---|
| 286 |
|
---|
[0edef06] | 287 | SDL_RWops *ops = SDL_RWFromFile(font, "rb");
|
---|
[bb4fd0c] | 288 |
|
---|
| 289 | if (ops == NULL) {
|
---|
[0edef06] | 290 | fail("error while opening font file %s: %s", font, SDL_GetError());
|
---|
[bb4fd0c] | 291 | }
|
---|
| 292 |
|
---|
| 293 | fon = TTF_OpenFontRW(ops, 1, 32);
|
---|
| 294 |
|
---|
| 295 | if (fon == NULL) {
|
---|
[0edef06] | 296 | fail("error while loading font file %s: %s", font, TTF_GetError());
|
---|
[bb4fd0c] | 297 | }
|
---|
| 298 |
|
---|
| 299 | fon_h = TTF_FontLineSkip(fon);
|
---|
| 300 |
|
---|
| 301 | if (TTF_GlyphMetrics(fon, 'X', NULL, NULL, NULL, NULL, &fon_w) < 0) {
|
---|
| 302 | fail("error while measuring font width: %s", TTF_GetError());
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | sur_w = CON_W * fon_w;
|
---|
| 306 | sur_h = CON_H * fon_h;
|
---|
| 307 |
|
---|
| 308 | sur = SDL_CreateRGBSurface(0, sur_w, sur_h, 32, 0, 0, 0, 0);
|
---|
| 309 |
|
---|
| 310 | if (sur == NULL) {
|
---|
| 311 | fail("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | for (int32_t y = 0; y < CON_H; ++y) {
|
---|
| 315 | for (int32_t x = 0; x < CON_W; ++x) {
|
---|
| 316 | mem[y][x] = ' ';
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | mem[y][CON_W] = 0;
|
---|
| 320 | }
|
---|
[a06aa8b] | 321 | }
|
---|
| 322 |
|
---|
| 323 | void ser_quit(void)
|
---|
| 324 | {
|
---|
| 325 | ver("ser quit");
|
---|
[bb4fd0c] | 326 |
|
---|
| 327 | SDL_FreeSurface(sur);
|
---|
| 328 | TTF_CloseFont(fon);
|
---|
[caff491] | 329 |
|
---|
| 330 | SDL_DestroyRenderer(ren);
|
---|
| 331 | SDL_DestroyWindow(win);
|
---|
[a06aa8b] | 332 | }
|
---|
| 333 |
|
---|
[3c30832] | 334 | bool ser_exec(void)
|
---|
[a06aa8b] | 335 | {
|
---|
[4c71d39] | 336 | ver3("ser exec");
|
---|
[0726522] | 337 |
|
---|
| 338 | if (bel > 0) {
|
---|
| 339 | --bel;
|
---|
| 340 |
|
---|
| 341 | if (bel == BEL_CYC - 1 || bel == 0) {
|
---|
[bdd5a63] | 342 | SDL_AtomicAdd(&frame, 1);
|
---|
[0726522] | 343 | }
|
---|
| 344 | }
|
---|
| 345 |
|
---|
[375f7fb] | 346 | return state[0].irq_r || state[0].irq_t || state[1].irq_r || state[1].irq_t;
|
---|
[a06aa8b] | 347 | }
|
---|
| 348 |
|
---|
| 349 | uint32_t ser_read(uint32_t off, int32_t sz)
|
---|
| 350 | {
|
---|
[4c71d39] | 351 | ver2("ser rd %u:%d", off, sz * 8);
|
---|
[3c30832] | 352 |
|
---|
| 353 | if (sz != 1 || off > 7) {
|
---|
| 354 | fail("invalid ser rd %u:%d", off, sz * 8);
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | int32_t rg = (int32_t)(off % 4);
|
---|
| 358 | int32_t un = (int32_t)(off / 4);
|
---|
| 359 |
|
---|
| 360 | uint32_t rv;
|
---|
| 361 |
|
---|
| 362 | switch (rg) {
|
---|
| 363 | case REG_IER_ISR:
|
---|
[375f7fb] | 364 | rv = (uint32_t)(0xc0 | (state[un].rdr_ok ? 0x01 : 0x00));
|
---|
| 365 | state[un].irq_r = false;
|
---|
| 366 | state[un].irq_t = false;
|
---|
[3c30832] | 367 | ver2("ISR[%d] 0x%02x", un, rv);
|
---|
| 368 | break;
|
---|
| 369 |
|
---|
| 370 | case REG_TDR_RDR:
|
---|
| 371 | rv = state[un].rdr;
|
---|
[375f7fb] | 372 | state[un].rdr_ok = false;
|
---|
[3c30832] | 373 | ver2("RDR[%d] 0x%02x", un, rv);
|
---|
| 374 | break;
|
---|
| 375 |
|
---|
| 376 | default:
|
---|
| 377 | rv = 0x00;
|
---|
| 378 | break;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | return rv;
|
---|
[a06aa8b] | 382 | }
|
---|
| 383 |
|
---|
| 384 | void ser_write(uint32_t off, int32_t sz, uint32_t val)
|
---|
| 385 | {
|
---|
[4c71d39] | 386 | ver2("ser wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
|
---|
[3c30832] | 387 |
|
---|
| 388 | if (sz != 1 || off > 7) {
|
---|
| 389 | fail("invalid ser wr %u:%d", off, sz * 8);
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | int32_t rg = (int32_t)(off % 4);
|
---|
| 393 | int32_t un = (int32_t)(off / 4);
|
---|
| 394 |
|
---|
| 395 | switch (rg) {
|
---|
| 396 | case REG_TDR_RDR:
|
---|
| 397 | ver2("TDR[%d] 0x%02x", un, val);
|
---|
| 398 | echo((uint8_t)val);
|
---|
[375f7fb] | 399 | state[un].irq_t = true;
|
---|
[3c30832] | 400 | break;
|
---|
| 401 |
|
---|
| 402 | default:
|
---|
| 403 | break;
|
---|
| 404 | }
|
---|
[a06aa8b] | 405 | }
|
---|