[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 |
|
---|
[8c8a883] | 62 | static uint8_t mem[CON_H][CON_W];
|
---|
[bb4fd0c] | 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 | {
|
---|
[8c8a883] | 83 | memmove(mem, mem + 1, (CON_H - 1) * CON_W);
|
---|
[bb4fd0c] | 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];
|
---|
[8c8a883] | 258 | line[CON_W] = 0;
|
---|
[bdd5a63] | 259 |
|
---|
| 260 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 261 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[8c8a883] | 264 | memcpy(line, mem[y], CON_W);
|
---|
[bdd5a63] | 265 |
|
---|
| 266 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 267 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | SDL_Surface *lin = TTF_RenderText_Blended(fon, line, CON_FGR);
|
---|
| 271 |
|
---|
| 272 | if (lin == NULL) {
|
---|
| 273 | fail("TTF_RenderText_Blended() failed: %s", TTF_GetError());
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | if (SDL_BlitSurface(lin, NULL, sur, &(SDL_Rect){
|
---|
| 277 | .x = 0,
|
---|
| 278 | .y = y * fon_h,
|
---|
| 279 | .w = CON_W * fon_w,
|
---|
| 280 | .h = fon_h
|
---|
| 281 | })) {
|
---|
| 282 | fail("SDL_BlitSurface() failed: %s", SDL_GetError());
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | SDL_FreeSurface(lin);
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, sur);
|
---|
| 289 |
|
---|
| 290 | if (tex == NULL) {
|
---|
| 291 | fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | if (SDL_RenderCopy(ren, tex, NULL, NULL) < 0) {
|
---|
| 295 | fail("SDL_RenderCopy() failed: %s", SDL_GetError());
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | SDL_DestroyTexture(tex);
|
---|
| 299 | SDL_RenderPresent(ren);
|
---|
[caff491] | 300 | }
|
---|
| 301 |
|
---|
[bb4fd0c] | 302 | void ser_key(SDL_KeyboardEvent *ev)
|
---|
| 303 | {
|
---|
| 304 | switch (ev->keysym.sym) {
|
---|
| 305 | case SDLK_BACKSPACE:
|
---|
[3c30832] | 306 | out(1, '\b');
|
---|
[bb4fd0c] | 307 | break;
|
---|
| 308 |
|
---|
| 309 | case SDLK_RETURN:
|
---|
[3c30832] | 310 | out(1, '\r');
|
---|
[bb4fd0c] | 311 | break;
|
---|
[a23f3d9] | 312 |
|
---|
| 313 | default:
|
---|
| 314 | if ((ev->keysym.mod & KMOD_CTRL) != 0 &&
|
---|
| 315 | ev->keysym.sym >= SDLK_a && ev->keysym.sym <= SDLK_z) {
|
---|
[3c30832] | 316 | out(1, (uint8_t)(ev->keysym.sym - SDLK_a + 1));
|
---|
[a23f3d9] | 317 | }
|
---|
| 318 |
|
---|
| 319 | break;
|
---|
[bb4fd0c] | 320 | }
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | void ser_text(SDL_TextInputEvent *ev)
|
---|
| 324 | {
|
---|
| 325 | for (int32_t i = 0; ev->text[i] != 0; ++i) {
|
---|
[3c30832] | 326 | out(1, (uint8_t)ev->text[i]);
|
---|
[bb4fd0c] | 327 | }
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[657abdf] | 330 | static void mou_ev(void)
|
---|
| 331 | {
|
---|
| 332 | ver2("ser mou ev (%d, %d) %c %c",
|
---|
| 333 | mou_dx, mou_dy, mou_l ? 'l' : '-', mou_r ? 'r' : '-');
|
---|
| 334 |
|
---|
| 335 | int32_t dx = mou_dx;
|
---|
| 336 | int32_t dy = mou_dy;
|
---|
| 337 |
|
---|
| 338 | if (dx < -128) {
|
---|
| 339 | dx = -128;
|
---|
| 340 | }
|
---|
| 341 | else if (dx > 127) {
|
---|
| 342 | dx = 127;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | if (dy < -128) {
|
---|
| 346 | dy = -128;
|
---|
| 347 | }
|
---|
| 348 | else if (dy > 127) {
|
---|
| 349 | dy = 127;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | dx = dx & 0xff;
|
---|
| 353 | dy = dy & 0xff;
|
---|
| 354 |
|
---|
| 355 | int32_t b1 = 0x40;
|
---|
| 356 |
|
---|
| 357 | if (mou_l) {
|
---|
| 358 | b1 |= 0x20;
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | if (mou_r) {
|
---|
| 362 | b1 |= 0x10;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | b1 |= (dy & 0xc0) >> 4;
|
---|
| 366 | b1 |= (dx & 0xc0) >> 6;
|
---|
| 367 |
|
---|
| 368 | int32_t b2 = dx & 0x3f;
|
---|
| 369 | int32_t b3 = dy & 0x3f;
|
---|
| 370 |
|
---|
| 371 | out_lk(0, (uint8_t)b1);
|
---|
| 372 | out_lk(0, (uint8_t)b2);
|
---|
| 373 | out_lk(0, (uint8_t)b3);
|
---|
| 374 |
|
---|
| 375 | mou_dx = mou_dy = 0;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | static void mou_ev_chk(void)
|
---|
| 379 | {
|
---|
| 380 | if (--mou > 0) {
|
---|
| 381 | return;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | mou = MOU_CYC;
|
---|
| 385 |
|
---|
| 386 | if (mou_dx == 0 && mou_dy == 0) {
|
---|
| 387 | return;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | mou_ev();
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | void ser_mou_res(void)
|
---|
| 394 | {
|
---|
| 395 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 396 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | mou = MOU_CYC;
|
---|
| 400 | mou_dx = mou_dy = 0;
|
---|
| 401 | mou_l = mou_r = false;
|
---|
| 402 |
|
---|
| 403 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 404 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 405 | }
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | void ser_mou_mov(SDL_MouseMotionEvent *ev)
|
---|
| 409 | {
|
---|
| 410 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 411 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | mou_dx += ev->xrel;
|
---|
| 415 | mou_dy += ev->yrel;
|
---|
| 416 |
|
---|
| 417 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 418 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 419 | }
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | void ser_mou_dn(SDL_MouseButtonEvent *ev)
|
---|
| 423 | {
|
---|
| 424 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 425 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | if (ev->button == SDL_BUTTON_LEFT) {
|
---|
| 429 | mou_l = true;
|
---|
| 430 | }
|
---|
| 431 | else if (ev->button == SDL_BUTTON_RIGHT) {
|
---|
| 432 | mou_r = true;
|
---|
| 433 | }
|
---|
| 434 | else {
|
---|
| 435 | return;
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | mou_ev();
|
---|
| 439 |
|
---|
| 440 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 441 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 442 | }
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | void ser_mou_up(SDL_MouseButtonEvent *ev)
|
---|
| 446 | {
|
---|
| 447 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 448 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | if (ev->button == SDL_BUTTON_LEFT) {
|
---|
| 452 | mou_l = false;
|
---|
| 453 | }
|
---|
| 454 | else if (ev->button == SDL_BUTTON_RIGHT) {
|
---|
| 455 | mou_r = false;
|
---|
| 456 | }
|
---|
| 457 | else {
|
---|
| 458 | return;
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | mou_ev();
|
---|
| 462 |
|
---|
| 463 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 464 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 465 | }
|
---|
| 466 | }
|
---|
| 467 |
|
---|
[a06aa8b] | 468 | void ser_init(void)
|
---|
| 469 | {
|
---|
| 470 | ver("ser init");
|
---|
[bb4fd0c] | 471 |
|
---|
[caff491] | 472 | win = SDL_CreateWindow("Serial Console", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
---|
| 473 | WIN_W, WIN_H, 0);
|
---|
| 474 |
|
---|
| 475 | if (win == NULL) {
|
---|
| 476 | fail("SDL_CreateWindow() failed: %s", SDL_GetError());
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | ren = SDL_CreateRenderer(win, -1, 0);
|
---|
| 480 |
|
---|
| 481 | if (ren == NULL) {
|
---|
| 482 | fail("SDL_CreateRenderer() failed: %s", SDL_GetError());
|
---|
| 483 | }
|
---|
| 484 |
|
---|
[bdd5a63] | 485 | SDL_AtomicSet(&frame, 1);
|
---|
| 486 |
|
---|
[0edef06] | 487 | SDL_RWops *ops = SDL_RWFromFile(font, "rb");
|
---|
[bb4fd0c] | 488 |
|
---|
| 489 | if (ops == NULL) {
|
---|
[0edef06] | 490 | fail("error while opening font file %s: %s", font, SDL_GetError());
|
---|
[bb4fd0c] | 491 | }
|
---|
| 492 |
|
---|
| 493 | fon = TTF_OpenFontRW(ops, 1, 32);
|
---|
| 494 |
|
---|
| 495 | if (fon == NULL) {
|
---|
[0edef06] | 496 | fail("error while loading font file %s: %s", font, TTF_GetError());
|
---|
[bb4fd0c] | 497 | }
|
---|
| 498 |
|
---|
| 499 | fon_h = TTF_FontLineSkip(fon);
|
---|
| 500 |
|
---|
| 501 | if (TTF_GlyphMetrics(fon, 'X', NULL, NULL, NULL, NULL, &fon_w) < 0) {
|
---|
| 502 | fail("error while measuring font width: %s", TTF_GetError());
|
---|
| 503 | }
|
---|
| 504 |
|
---|
| 505 | sur_w = CON_W * fon_w;
|
---|
| 506 | sur_h = CON_H * fon_h;
|
---|
| 507 |
|
---|
| 508 | sur = SDL_CreateRGBSurface(0, sur_w, sur_h, 32, 0, 0, 0, 0);
|
---|
| 509 |
|
---|
| 510 | if (sur == NULL) {
|
---|
| 511 | fail("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 | for (int32_t y = 0; y < CON_H; ++y) {
|
---|
| 515 | for (int32_t x = 0; x < CON_W; ++x) {
|
---|
| 516 | mem[y][x] = ' ';
|
---|
| 517 | }
|
---|
| 518 | }
|
---|
[a06aa8b] | 519 | }
|
---|
| 520 |
|
---|
| 521 | void ser_quit(void)
|
---|
| 522 | {
|
---|
| 523 | ver("ser quit");
|
---|
[bb4fd0c] | 524 |
|
---|
| 525 | SDL_FreeSurface(sur);
|
---|
| 526 | TTF_CloseFont(fon);
|
---|
[caff491] | 527 |
|
---|
| 528 | SDL_DestroyRenderer(ren);
|
---|
| 529 | SDL_DestroyWindow(win);
|
---|
[a06aa8b] | 530 | }
|
---|
| 531 |
|
---|
[3c30832] | 532 | bool ser_exec(void)
|
---|
[a06aa8b] | 533 | {
|
---|
[4c71d39] | 534 | ver3("ser exec");
|
---|
[0726522] | 535 |
|
---|
| 536 | if (bel > 0) {
|
---|
| 537 | --bel;
|
---|
| 538 |
|
---|
| 539 | if (bel == BEL_CYC - 1 || bel == 0) {
|
---|
[bdd5a63] | 540 | SDL_AtomicAdd(&frame, 1);
|
---|
[0726522] | 541 | }
|
---|
| 542 | }
|
---|
| 543 |
|
---|
[657abdf] | 544 | mou_ev_chk();
|
---|
| 545 |
|
---|
[375f7fb] | 546 | return state[0].irq_r || state[0].irq_t || state[1].irq_r || state[1].irq_t;
|
---|
[a06aa8b] | 547 | }
|
---|
| 548 |
|
---|
| 549 | uint32_t ser_read(uint32_t off, int32_t sz)
|
---|
| 550 | {
|
---|
[4c71d39] | 551 | ver2("ser rd %u:%d", off, sz * 8);
|
---|
[3c30832] | 552 |
|
---|
| 553 | if (sz != 1 || off > 7) {
|
---|
| 554 | fail("invalid ser rd %u:%d", off, sz * 8);
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | int32_t rg = (int32_t)(off % 4);
|
---|
| 558 | int32_t un = (int32_t)(off / 4);
|
---|
| 559 |
|
---|
| 560 | uint32_t rv;
|
---|
| 561 |
|
---|
| 562 | switch (rg) {
|
---|
| 563 | case REG_IER_ISR:
|
---|
[375f7fb] | 564 | rv = (uint32_t)(0xc0 | (state[un].rdr_ok ? 0x01 : 0x00));
|
---|
| 565 | state[un].irq_r = false;
|
---|
| 566 | state[un].irq_t = false;
|
---|
[3c30832] | 567 | ver2("ISR[%d] 0x%02x", un, rv);
|
---|
| 568 | break;
|
---|
| 569 |
|
---|
| 570 | case REG_TDR_RDR:
|
---|
| 571 | rv = state[un].rdr;
|
---|
[375f7fb] | 572 | state[un].rdr_ok = false;
|
---|
[3c30832] | 573 | ver2("RDR[%d] 0x%02x", un, rv);
|
---|
| 574 | break;
|
---|
| 575 |
|
---|
| 576 | default:
|
---|
| 577 | rv = 0x00;
|
---|
| 578 | break;
|
---|
| 579 | }
|
---|
| 580 |
|
---|
[657abdf] | 581 | if (!state[un].irq_r && !state[un].rdr_ok) {
|
---|
| 582 | xmit(un);
|
---|
| 583 | }
|
---|
| 584 |
|
---|
[3c30832] | 585 | return rv;
|
---|
[a06aa8b] | 586 | }
|
---|
| 587 |
|
---|
| 588 | void ser_write(uint32_t off, int32_t sz, uint32_t val)
|
---|
| 589 | {
|
---|
[4c71d39] | 590 | ver2("ser wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
|
---|
[3c30832] | 591 |
|
---|
| 592 | if (sz != 1 || off > 7) {
|
---|
| 593 | fail("invalid ser wr %u:%d", off, sz * 8);
|
---|
| 594 | }
|
---|
| 595 |
|
---|
| 596 | int32_t rg = (int32_t)(off % 4);
|
---|
| 597 | int32_t un = (int32_t)(off / 4);
|
---|
| 598 |
|
---|
| 599 | switch (rg) {
|
---|
| 600 | case REG_TDR_RDR:
|
---|
| 601 | ver2("TDR[%d] 0x%02x", un, val);
|
---|
[657abdf] | 602 |
|
---|
| 603 | if (un == 1) {
|
---|
| 604 | echo((uint8_t)val);
|
---|
| 605 | }
|
---|
| 606 | else {
|
---|
| 607 | mouse((uint8_t)val);
|
---|
| 608 | }
|
---|
| 609 |
|
---|
[375f7fb] | 610 | state[un].irq_t = true;
|
---|
[3c30832] | 611 | break;
|
---|
| 612 |
|
---|
| 613 | default:
|
---|
| 614 | break;
|
---|
| 615 | }
|
---|
[a06aa8b] | 616 | }
|
---|