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