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