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