[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
|
---|
[657abdf] | 30 | #define MOU_CYC 10000
|
---|
[0726522] | 31 |
|
---|
[bb4fd0c] | 32 | #define CON_W 80
|
---|
| 33 | #define CON_H 25
|
---|
| 34 |
|
---|
| 35 | #define CON_BGR 0x00000000
|
---|
[0726522] | 36 | #define CON_BEL 0x00808080
|
---|
[bb4fd0c] | 37 | #define CON_CUR 0x00e87000
|
---|
| 38 | #define CON_FGR ((SDL_Color){ .r = 255, .b = 255, .g = 255, .a = 255 })
|
---|
| 39 |
|
---|
[657abdf] | 40 | #define BUF_SZ 16
|
---|
| 41 |
|
---|
[3c30832] | 42 | #define REG_IER_ISR 0
|
---|
| 43 | #define REG_CFR_SR 1
|
---|
| 44 | #define REG_CDR_TBR 2
|
---|
| 45 | #define REG_TDR_RDR 3
|
---|
| 46 |
|
---|
| 47 | typedef struct {
|
---|
[657abdf] | 48 | int32_t buf_hd;
|
---|
| 49 | int32_t buf_tl;
|
---|
| 50 | uint8_t buf[BUF_SZ];
|
---|
[375f7fb] | 51 | bool irq_r;
|
---|
| 52 | bool irq_t;
|
---|
| 53 | bool rdr_ok;
|
---|
[3c30832] | 54 | uint8_t rdr;
|
---|
| 55 | } state_t;
|
---|
| 56 |
|
---|
| 57 | static state_t state[] = {
|
---|
[657abdf] | 58 | { .buf_hd = 0, .buf_tl = 0, .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 },
|
---|
| 59 | { .buf_hd = 0, .buf_tl = 0, .irq_r = false, .irq_t = false, .rdr_ok = false, .rdr = 0x00 }
|
---|
[3c30832] | 60 | };
|
---|
| 61 |
|
---|
[bb4fd0c] | 62 | static uint8_t mem[CON_H][CON_W + 1];
|
---|
| 63 |
|
---|
[caff491] | 64 | static SDL_Window *win;
|
---|
| 65 | static SDL_Renderer *ren;
|
---|
[bdd5a63] | 66 | static SDL_atomic_t frame;
|
---|
[caff491] | 67 |
|
---|
[bb4fd0c] | 68 | static TTF_Font *fon;
|
---|
| 69 | static int32_t fon_w, fon_h;
|
---|
| 70 |
|
---|
| 71 | static int32_t sur_w, sur_h;
|
---|
| 72 | static SDL_Surface *sur;
|
---|
| 73 |
|
---|
| 74 | static int32_t cur_x = 0, cur_y = 0;
|
---|
[0726522] | 75 | static int32_t bel = 0;
|
---|
[bb4fd0c] | 76 |
|
---|
[657abdf] | 77 | static int32_t mou;
|
---|
| 78 | static int32_t mou_dx, mou_dy;
|
---|
| 79 | static bool mou_l, mou_r;
|
---|
| 80 |
|
---|
[bb4fd0c] | 81 | static void scroll(void)
|
---|
| 82 | {
|
---|
| 83 | memmove(mem, mem + 1, (CON_H - 1) * (CON_W + 1));
|
---|
| 84 | memset(mem + (CON_H - 1), ' ', CON_W);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | static void forw(void)
|
---|
| 88 | {
|
---|
| 89 | if (cur_x < CON_W - 1) {
|
---|
| 90 | ++cur_x;
|
---|
| 91 | return;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | if (cur_y == CON_H - 1) {
|
---|
| 95 | cur_x = 0;
|
---|
| 96 | scroll();
|
---|
| 97 | return;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | cur_x = 0;
|
---|
| 101 | ++cur_y;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | static void back(void)
|
---|
| 105 | {
|
---|
| 106 | if (cur_x > 0) {
|
---|
| 107 | --cur_x;
|
---|
| 108 | return;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | if (cur_y == 0) {
|
---|
| 112 | return;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | cur_x = CON_W - 1;
|
---|
| 116 | --cur_y;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | static void down(void)
|
---|
| 120 | {
|
---|
| 121 | if (cur_y < CON_H - 1) {
|
---|
| 122 | ++cur_y;
|
---|
| 123 | return;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | scroll();
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | static void echo(uint8_t c)
|
---|
| 130 | {
|
---|
[2a75e87] | 131 | if (c < 32) {
|
---|
| 132 | switch (c) {
|
---|
| 133 | case '\r':
|
---|
| 134 | cur_x = 0;
|
---|
| 135 | break;
|
---|
[bb4fd0c] | 136 |
|
---|
[2a75e87] | 137 | case '\n':
|
---|
| 138 | down();
|
---|
| 139 | break;
|
---|
[bb4fd0c] | 140 |
|
---|
[2a75e87] | 141 | case '\b':
|
---|
| 142 | back();
|
---|
| 143 | break;
|
---|
[bb4fd0c] | 144 |
|
---|
[0726522] | 145 | case '\a':
|
---|
| 146 | bel = BEL_CYC;
|
---|
| 147 | break;
|
---|
| 148 |
|
---|
[2a75e87] | 149 | default:
|
---|
[bb4fd0c] | 150 | echo('^');
|
---|
| 151 | echo((uint8_t)(c + '@'));
|
---|
| 152 | return;
|
---|
| 153 | }
|
---|
[2a75e87] | 154 | }
|
---|
| 155 | else {
|
---|
[bb4fd0c] | 156 | mem[cur_y][cur_x] = c;
|
---|
| 157 | forw();
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[bdd5a63] | 160 | SDL_AtomicAdd(&frame, 1);
|
---|
[bb4fd0c] | 161 | }
|
---|
| 162 |
|
---|
[657abdf] | 163 | static void xmit(int32_t un)
|
---|
| 164 | {
|
---|
| 165 | int32_t i = state[un].buf_tl;
|
---|
| 166 | ver2("ser xmit %d %d", i, state[un].buf_hd);
|
---|
| 167 |
|
---|
| 168 | if (i >= state[un].buf_hd) {
|
---|
| 169 | return;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | uint8_t byte = state[un].buf[i % BUF_SZ];
|
---|
| 173 | ver2("ser xmit 0x%02x", byte);
|
---|
| 174 |
|
---|
| 175 | state[un].rdr = byte;
|
---|
| 176 | state[un].rdr_ok = true;
|
---|
| 177 | state[un].irq_r = true;
|
---|
| 178 |
|
---|
| 179 | state[un].buf_tl = i + 1;
|
---|
| 180 |
|
---|
| 181 | if (state[un].buf_tl >= BUF_SZ) {
|
---|
| 182 | state[un].buf_hd -= BUF_SZ;
|
---|
| 183 | state[un].buf_tl -= BUF_SZ;
|
---|
| 184 | ver2("ser adj %d %d", state[un].buf_tl, state[un].buf_hd);
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | static void out_lk(int32_t un, uint8_t c)
|
---|
| 189 | {
|
---|
| 190 | int32_t i = state[un].buf_hd;
|
---|
| 191 | ver2("ser out %d %d 0x%02x", state[un].buf_tl, i, c);
|
---|
| 192 |
|
---|
| 193 | if (i >= state[un].buf_tl + BUF_SZ) {
|
---|
| 194 | err("serial port %d losing data", un);
|
---|
| 195 | return;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | state[un].buf[i % BUF_SZ] = c;
|
---|
| 199 | state[un].buf_hd = i + 1;
|
---|
| 200 |
|
---|
| 201 | if (!state[un].irq_r && !state[un].rdr_ok) {
|
---|
| 202 | xmit(un);
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[caff491] | 206 | static void out(int32_t un, uint8_t c)
|
---|
| 207 | {
|
---|
[bdd5a63] | 208 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 209 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[657abdf] | 212 | out_lk(un, c);
|
---|
[bdd5a63] | 213 |
|
---|
| 214 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 215 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[657abdf] | 219 | static void mouse(uint8_t c)
|
---|
| 220 | {
|
---|
| 221 | if (c == 't') {
|
---|
| 222 | ver2("ser mou init");
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | out_lk(0, 'V');
|
---|
| 226 | out_lk(0, 'O');
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[bdd5a63] | 229 | void ser_sdl(void)
|
---|
| 230 | {
|
---|
| 231 | ver3("ser_sdl()");
|
---|
| 232 |
|
---|
| 233 | static int32_t last = 0;
|
---|
| 234 | int32_t now = SDL_AtomicGet(&frame);
|
---|
| 235 |
|
---|
| 236 | if (last == now) {
|
---|
| 237 | ver3("no update");
|
---|
| 238 | return;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | last = now;
|
---|
| 242 |
|
---|
| 243 | if (SDL_FillRect(sur, NULL, bel == 0 ? CON_BGR : CON_BEL) < 0) {
|
---|
| 244 | fail("SDL_FillRect() failed: %s", SDL_GetError());
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | if (SDL_FillRect(sur, &(SDL_Rect){
|
---|
| 248 | .x = cur_x * fon_w,
|
---|
| 249 | .y = cur_y * fon_h,
|
---|
| 250 | .w = fon_w,
|
---|
| 251 | .h = fon_h
|
---|
| 252 | }, CON_CUR) < 0) {
|
---|
| 253 | fail("SDL_FillRect() failed: %s", SDL_GetError());
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | for (int32_t y = 0; y < CON_H; ++y) {
|
---|
| 257 | char line[CON_W + 1];
|
---|
| 258 |
|
---|
| 259 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 260 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | memcpy(line, mem[y], CON_W + 1);
|
---|
| 264 |
|
---|
| 265 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 266 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | SDL_Surface *lin = TTF_RenderText_Blended(fon, line, CON_FGR);
|
---|
| 270 |
|
---|
| 271 | if (lin == NULL) {
|
---|
| 272 | fail("TTF_RenderText_Blended() failed: %s", TTF_GetError());
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | if (SDL_BlitSurface(lin, NULL, sur, &(SDL_Rect){
|
---|
| 276 | .x = 0,
|
---|
| 277 | .y = y * fon_h,
|
---|
| 278 | .w = CON_W * fon_w,
|
---|
| 279 | .h = fon_h
|
---|
| 280 | })) {
|
---|
| 281 | fail("SDL_BlitSurface() failed: %s", SDL_GetError());
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | SDL_FreeSurface(lin);
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, sur);
|
---|
| 288 |
|
---|
| 289 | if (tex == NULL) {
|
---|
| 290 | fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | if (SDL_RenderCopy(ren, tex, NULL, NULL) < 0) {
|
---|
| 294 | fail("SDL_RenderCopy() failed: %s", SDL_GetError());
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | SDL_DestroyTexture(tex);
|
---|
| 298 | SDL_RenderPresent(ren);
|
---|
[caff491] | 299 | }
|
---|
| 300 |
|
---|
[bb4fd0c] | 301 | void ser_key(SDL_KeyboardEvent *ev)
|
---|
| 302 | {
|
---|
| 303 | switch (ev->keysym.sym) {
|
---|
| 304 | case SDLK_BACKSPACE:
|
---|
[3c30832] | 305 | out(1, '\b');
|
---|
[bb4fd0c] | 306 | break;
|
---|
| 307 |
|
---|
| 308 | case SDLK_RETURN:
|
---|
[3c30832] | 309 | out(1, '\r');
|
---|
[bb4fd0c] | 310 | break;
|
---|
[a23f3d9] | 311 |
|
---|
| 312 | default:
|
---|
| 313 | if ((ev->keysym.mod & KMOD_CTRL) != 0 &&
|
---|
| 314 | ev->keysym.sym >= SDLK_a && ev->keysym.sym <= SDLK_z) {
|
---|
[3c30832] | 315 | out(1, (uint8_t)(ev->keysym.sym - SDLK_a + 1));
|
---|
[a23f3d9] | 316 | }
|
---|
| 317 |
|
---|
| 318 | break;
|
---|
[bb4fd0c] | 319 | }
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | void ser_text(SDL_TextInputEvent *ev)
|
---|
| 323 | {
|
---|
| 324 | for (int32_t i = 0; ev->text[i] != 0; ++i) {
|
---|
[3c30832] | 325 | out(1, (uint8_t)ev->text[i]);
|
---|
[bb4fd0c] | 326 | }
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[657abdf] | 329 | static void mou_ev(void)
|
---|
| 330 | {
|
---|
| 331 | ver2("ser mou ev (%d, %d) %c %c",
|
---|
| 332 | mou_dx, mou_dy, mou_l ? 'l' : '-', mou_r ? 'r' : '-');
|
---|
| 333 |
|
---|
| 334 | int32_t dx = mou_dx;
|
---|
| 335 | int32_t dy = mou_dy;
|
---|
| 336 |
|
---|
| 337 | if (dx < -128) {
|
---|
| 338 | dx = -128;
|
---|
| 339 | }
|
---|
| 340 | else if (dx > 127) {
|
---|
| 341 | dx = 127;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | if (dy < -128) {
|
---|
| 345 | dy = -128;
|
---|
| 346 | }
|
---|
| 347 | else if (dy > 127) {
|
---|
| 348 | dy = 127;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | dx = dx & 0xff;
|
---|
| 352 | dy = dy & 0xff;
|
---|
| 353 |
|
---|
| 354 | int32_t b1 = 0x40;
|
---|
| 355 |
|
---|
| 356 | if (mou_l) {
|
---|
| 357 | b1 |= 0x20;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | if (mou_r) {
|
---|
| 361 | b1 |= 0x10;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | b1 |= (dy & 0xc0) >> 4;
|
---|
| 365 | b1 |= (dx & 0xc0) >> 6;
|
---|
| 366 |
|
---|
| 367 | int32_t b2 = dx & 0x3f;
|
---|
| 368 | int32_t b3 = dy & 0x3f;
|
---|
| 369 |
|
---|
| 370 | out_lk(0, (uint8_t)b1);
|
---|
| 371 | out_lk(0, (uint8_t)b2);
|
---|
| 372 | out_lk(0, (uint8_t)b3);
|
---|
| 373 |
|
---|
| 374 | mou_dx = mou_dy = 0;
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | static void mou_ev_chk(void)
|
---|
| 378 | {
|
---|
| 379 | if (--mou > 0) {
|
---|
| 380 | return;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | mou = MOU_CYC;
|
---|
| 384 |
|
---|
| 385 | if (mou_dx == 0 && mou_dy == 0) {
|
---|
| 386 | return;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | mou_ev();
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | void ser_mou_res(void)
|
---|
| 393 | {
|
---|
| 394 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 395 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | mou = MOU_CYC;
|
---|
| 399 | mou_dx = mou_dy = 0;
|
---|
| 400 | mou_l = mou_r = false;
|
---|
| 401 |
|
---|
| 402 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 403 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 404 | }
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | void ser_mou_mov(SDL_MouseMotionEvent *ev)
|
---|
| 408 | {
|
---|
| 409 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 410 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | mou_dx += ev->xrel;
|
---|
| 414 | mou_dy += ev->yrel;
|
---|
| 415 |
|
---|
| 416 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 417 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 418 | }
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | void ser_mou_dn(SDL_MouseButtonEvent *ev)
|
---|
| 422 | {
|
---|
| 423 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 424 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | if (ev->button == SDL_BUTTON_LEFT) {
|
---|
| 428 | mou_l = true;
|
---|
| 429 | }
|
---|
| 430 | else if (ev->button == SDL_BUTTON_RIGHT) {
|
---|
| 431 | mou_r = true;
|
---|
| 432 | }
|
---|
| 433 | else {
|
---|
| 434 | return;
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | mou_ev();
|
---|
| 438 |
|
---|
| 439 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 440 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 441 | }
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | void ser_mou_up(SDL_MouseButtonEvent *ev)
|
---|
| 445 | {
|
---|
| 446 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 447 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | if (ev->button == SDL_BUTTON_LEFT) {
|
---|
| 451 | mou_l = false;
|
---|
| 452 | }
|
---|
| 453 | else if (ev->button == SDL_BUTTON_RIGHT) {
|
---|
| 454 | mou_r = false;
|
---|
| 455 | }
|
---|
| 456 | else {
|
---|
| 457 | return;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | mou_ev();
|
---|
| 461 |
|
---|
| 462 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 463 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 464 | }
|
---|
| 465 | }
|
---|
| 466 |
|
---|
[a06aa8b] | 467 | void ser_init(void)
|
---|
| 468 | {
|
---|
| 469 | ver("ser init");
|
---|
[bb4fd0c] | 470 |
|
---|
[caff491] | 471 | win = SDL_CreateWindow("Serial Console", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
---|
| 472 | WIN_W, WIN_H, 0);
|
---|
| 473 |
|
---|
| 474 | if (win == NULL) {
|
---|
| 475 | fail("SDL_CreateWindow() failed: %s", SDL_GetError());
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | ren = SDL_CreateRenderer(win, -1, 0);
|
---|
| 479 |
|
---|
| 480 | if (ren == NULL) {
|
---|
| 481 | fail("SDL_CreateRenderer() failed: %s", SDL_GetError());
|
---|
| 482 | }
|
---|
| 483 |
|
---|
[bdd5a63] | 484 | SDL_AtomicSet(&frame, 1);
|
---|
| 485 |
|
---|
[0edef06] | 486 | SDL_RWops *ops = SDL_RWFromFile(font, "rb");
|
---|
[bb4fd0c] | 487 |
|
---|
| 488 | if (ops == NULL) {
|
---|
[0edef06] | 489 | fail("error while opening font file %s: %s", font, SDL_GetError());
|
---|
[bb4fd0c] | 490 | }
|
---|
| 491 |
|
---|
| 492 | fon = TTF_OpenFontRW(ops, 1, 32);
|
---|
| 493 |
|
---|
| 494 | if (fon == NULL) {
|
---|
[0edef06] | 495 | fail("error while loading font file %s: %s", font, TTF_GetError());
|
---|
[bb4fd0c] | 496 | }
|
---|
| 497 |
|
---|
| 498 | fon_h = TTF_FontLineSkip(fon);
|
---|
| 499 |
|
---|
| 500 | if (TTF_GlyphMetrics(fon, 'X', NULL, NULL, NULL, NULL, &fon_w) < 0) {
|
---|
| 501 | fail("error while measuring font width: %s", TTF_GetError());
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | sur_w = CON_W * fon_w;
|
---|
| 505 | sur_h = CON_H * fon_h;
|
---|
| 506 |
|
---|
| 507 | sur = SDL_CreateRGBSurface(0, sur_w, sur_h, 32, 0, 0, 0, 0);
|
---|
| 508 |
|
---|
| 509 | if (sur == NULL) {
|
---|
| 510 | fail("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | for (int32_t y = 0; y < CON_H; ++y) {
|
---|
| 514 | for (int32_t x = 0; x < CON_W; ++x) {
|
---|
| 515 | mem[y][x] = ' ';
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | mem[y][CON_W] = 0;
|
---|
| 519 | }
|
---|
[a06aa8b] | 520 | }
|
---|
| 521 |
|
---|
| 522 | void ser_quit(void)
|
---|
| 523 | {
|
---|
| 524 | ver("ser quit");
|
---|
[bb4fd0c] | 525 |
|
---|
| 526 | SDL_FreeSurface(sur);
|
---|
| 527 | TTF_CloseFont(fon);
|
---|
[caff491] | 528 |
|
---|
| 529 | SDL_DestroyRenderer(ren);
|
---|
| 530 | SDL_DestroyWindow(win);
|
---|
[a06aa8b] | 531 | }
|
---|
| 532 |
|
---|
[3c30832] | 533 | bool ser_exec(void)
|
---|
[a06aa8b] | 534 | {
|
---|
[4c71d39] | 535 | ver3("ser exec");
|
---|
[0726522] | 536 |
|
---|
| 537 | if (bel > 0) {
|
---|
| 538 | --bel;
|
---|
| 539 |
|
---|
| 540 | if (bel == BEL_CYC - 1 || bel == 0) {
|
---|
[bdd5a63] | 541 | SDL_AtomicAdd(&frame, 1);
|
---|
[0726522] | 542 | }
|
---|
| 543 | }
|
---|
| 544 |
|
---|
[657abdf] | 545 | mou_ev_chk();
|
---|
| 546 |
|
---|
[375f7fb] | 547 | return state[0].irq_r || state[0].irq_t || state[1].irq_r || state[1].irq_t;
|
---|
[a06aa8b] | 548 | }
|
---|
| 549 |
|
---|
| 550 | uint32_t ser_read(uint32_t off, int32_t sz)
|
---|
| 551 | {
|
---|
[4c71d39] | 552 | ver2("ser rd %u:%d", off, sz * 8);
|
---|
[3c30832] | 553 |
|
---|
| 554 | if (sz != 1 || off > 7) {
|
---|
| 555 | fail("invalid ser rd %u:%d", off, sz * 8);
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | int32_t rg = (int32_t)(off % 4);
|
---|
| 559 | int32_t un = (int32_t)(off / 4);
|
---|
| 560 |
|
---|
| 561 | uint32_t rv;
|
---|
| 562 |
|
---|
| 563 | switch (rg) {
|
---|
| 564 | case REG_IER_ISR:
|
---|
[375f7fb] | 565 | rv = (uint32_t)(0xc0 | (state[un].rdr_ok ? 0x01 : 0x00));
|
---|
| 566 | state[un].irq_r = false;
|
---|
| 567 | state[un].irq_t = false;
|
---|
[3c30832] | 568 | ver2("ISR[%d] 0x%02x", un, rv);
|
---|
| 569 | break;
|
---|
| 570 |
|
---|
| 571 | case REG_TDR_RDR:
|
---|
| 572 | rv = state[un].rdr;
|
---|
[375f7fb] | 573 | state[un].rdr_ok = false;
|
---|
[3c30832] | 574 | ver2("RDR[%d] 0x%02x", un, rv);
|
---|
| 575 | break;
|
---|
| 576 |
|
---|
| 577 | default:
|
---|
| 578 | rv = 0x00;
|
---|
| 579 | break;
|
---|
| 580 | }
|
---|
| 581 |
|
---|
[657abdf] | 582 | if (!state[un].irq_r && !state[un].rdr_ok) {
|
---|
| 583 | xmit(un);
|
---|
| 584 | }
|
---|
| 585 |
|
---|
[3c30832] | 586 | return rv;
|
---|
[a06aa8b] | 587 | }
|
---|
| 588 |
|
---|
| 589 | void ser_write(uint32_t off, int32_t sz, uint32_t val)
|
---|
| 590 | {
|
---|
[4c71d39] | 591 | ver2("ser wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
|
---|
[3c30832] | 592 |
|
---|
| 593 | if (sz != 1 || off > 7) {
|
---|
| 594 | fail("invalid ser wr %u:%d", off, sz * 8);
|
---|
| 595 | }
|
---|
| 596 |
|
---|
| 597 | int32_t rg = (int32_t)(off % 4);
|
---|
| 598 | int32_t un = (int32_t)(off / 4);
|
---|
| 599 |
|
---|
| 600 | switch (rg) {
|
---|
| 601 | case REG_TDR_RDR:
|
---|
| 602 | ver2("TDR[%d] 0x%02x", un, val);
|
---|
[657abdf] | 603 |
|
---|
| 604 | if (un == 1) {
|
---|
| 605 | echo((uint8_t)val);
|
---|
| 606 | }
|
---|
| 607 | else {
|
---|
| 608 | mouse((uint8_t)val);
|
---|
| 609 | }
|
---|
| 610 |
|
---|
[375f7fb] | 611 | state[un].irq_t = true;
|
---|
[3c30832] | 612 | break;
|
---|
| 613 |
|
---|
| 614 | default:
|
---|
| 615 | break;
|
---|
| 616 | }
|
---|
[a06aa8b] | 617 | }
|
---|