[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(lcd_verbose, 0, __VA_ARGS__)
|
---|
| 21 | #define ver2(...) _ver(lcd_verbose, 1, __VA_ARGS__)
|
---|
| 22 | #define ver3(...) _ver(lcd_verbose, 2, __VA_ARGS__)
|
---|
[a06aa8b] | 23 |
|
---|
[18e37d6] | 24 | int32_t lcd_verbose = 0;
|
---|
| 25 |
|
---|
[a2b296e] | 26 | #define WIN_W (1615 / 2)
|
---|
| 27 | #define WIN_H (304 / 2)
|
---|
[7ba68aa] | 28 |
|
---|
[cd50b8c] | 29 | #define GFX_BGR 0x00000000
|
---|
| 30 | #define GFX_FGR 0xFFFFFFFF
|
---|
| 31 |
|
---|
| 32 | #define TXT_BGR 0x000000FF
|
---|
| 33 | #define TXT_FGR ((SDL_Color){ .r = 255, .b = 255, .g = 255, .a = 255 })
|
---|
[7ba68aa] | 34 |
|
---|
[18e37d6] | 35 | #define REG_ARG 0
|
---|
| 36 | #define REG_COM 1
|
---|
| 37 |
|
---|
[1b30217] | 38 | #define COM_NONE 0x00
|
---|
| 39 | #define COM_MWRITE 0x42
|
---|
| 40 | #define COM_MREAD 0x43
|
---|
| 41 | #define COM_CRSWR 0x46
|
---|
| 42 | #define COM_CRSMRT 0x4C
|
---|
| 43 | #define COM_CRSMUP 0x4E
|
---|
| 44 | #define COM_CRSMDN 0x4F
|
---|
| 45 | #define COM_DSPOFF 0x58
|
---|
| 46 | #define COM_DSPON 0x59
|
---|
[7ba68aa] | 47 |
|
---|
[c8a92ef] | 48 | #define TXT_W 85
|
---|
| 49 | #define TXT_H 8
|
---|
| 50 |
|
---|
| 51 | #define GFX_W 85
|
---|
| 52 | #define GFX_H 64
|
---|
[cd50b8c] | 53 | #define GFX_PIX 6
|
---|
[c8a92ef] | 54 |
|
---|
| 55 | #define BASE_TXT 0x0000
|
---|
| 56 | #define BASE_GFX 0x2000
|
---|
| 57 |
|
---|
[18e37d6] | 58 | #define DIR_UP -85
|
---|
| 59 | #define DIR_DOWN 85
|
---|
| 60 | #define DIR_RIGHT 1
|
---|
| 61 |
|
---|
[c8a92ef] | 62 | static uint8_t mem_txt[TXT_H * TXT_W];
|
---|
| 63 | static uint8_t mem_gfx[GFX_H * GFX_W];
|
---|
[7ba68aa] | 64 |
|
---|
| 65 | static SDL_Window *win;
|
---|
[81d7cdc] | 66 | uint32_t lcd_win;
|
---|
| 67 |
|
---|
[7ba68aa] | 68 | static SDL_Renderer *ren;
|
---|
| 69 | static SDL_atomic_t frame;
|
---|
[18e37d6] | 70 | static SDL_atomic_t ena;
|
---|
[43ea417] | 71 |
|
---|
[7ba68aa] | 72 | static TTF_Font *fon;
|
---|
| 73 | static int32_t fon_w, fon_h;
|
---|
| 74 |
|
---|
[cd50b8c] | 75 | static int32_t txt_w, txt_h;
|
---|
| 76 | static SDL_Surface *txt;
|
---|
| 77 | static SDL_Texture *gfx;
|
---|
[c8a92ef] | 78 |
|
---|
[18e37d6] | 79 | static int32_t com;
|
---|
| 80 | static int32_t n_arg;
|
---|
| 81 | static int32_t cur = BASE_TXT;
|
---|
| 82 | static int32_t dir = DIR_RIGHT;
|
---|
[a06aa8b] | 83 |
|
---|
[c8a92ef] | 84 | void lcd_sdl(void)
|
---|
[7ba68aa] | 85 | {
|
---|
[c8a92ef] | 86 | ver3("lcd_sdl()");
|
---|
[7ba68aa] | 87 |
|
---|
| 88 | static int32_t last = 0;
|
---|
| 89 | int32_t now = SDL_AtomicGet(&frame);
|
---|
| 90 |
|
---|
| 91 | if (last == now) {
|
---|
| 92 | ver3("no update");
|
---|
| 93 | return;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | last = now;
|
---|
| 97 |
|
---|
[c6f1cf5] | 98 | if (SDL_FillRect(txt, NULL, TXT_BGR) < 0) {
|
---|
| 99 | fail("SDL_FillRect() failed: %s", SDL_GetError());
|
---|
[cd50b8c] | 100 | }
|
---|
| 101 |
|
---|
[c6f1cf5] | 102 | if (SDL_AtomicGet(&ena) == 0) {
|
---|
| 103 | SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, txt);
|
---|
[cd50b8c] | 104 |
|
---|
[c6f1cf5] | 105 | if (tex == NULL) {
|
---|
| 106 | fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
|
---|
[cd50b8c] | 107 | }
|
---|
| 108 |
|
---|
[c6f1cf5] | 109 | if (SDL_RenderCopy(ren, tex, NULL, NULL) < 0) {
|
---|
[cd50b8c] | 110 | fail("SDL_RenderCopy() failed: %s", SDL_GetError());
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[c6f1cf5] | 113 | SDL_DestroyTexture(tex);
|
---|
[cd50b8c] | 114 | SDL_RenderPresent(ren);
|
---|
[18e37d6] | 115 | return;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[c8a92ef] | 118 | for (int32_t y = 0; y < TXT_H; ++y) {
|
---|
| 119 | char line[TXT_W + 1];
|
---|
[81d7cdc] | 120 | line[TXT_W] = 0;
|
---|
[7ba68aa] | 121 |
|
---|
| 122 | if (SDL_LockMutex(cpu_mutex) < 0) {
|
---|
| 123 | fail("SDL_LockMutex() failed: %s", SDL_GetError());
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[cd50b8c] | 126 | memcpy(line, mem_txt + y * TXT_W, TXT_W);
|
---|
[7ba68aa] | 127 |
|
---|
| 128 | if (SDL_UnlockMutex(cpu_mutex) < 0) {
|
---|
| 129 | fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[cd50b8c] | 132 | for (int32_t x = 0; x < TXT_W; ++x) {
|
---|
| 133 | if (line[x] == 0x00) {
|
---|
| 134 | line[x] = ' ';
|
---|
| 135 | }
|
---|
[5e2fc8b] | 136 | else if (line[x] == 0x7e) {
|
---|
| 137 | line[x] = (char) 0xbb;
|
---|
| 138 | }
|
---|
| 139 | else if (line[x] == 0x7f) {
|
---|
| 140 | line[x] = (char) 0xab;
|
---|
| 141 | }
|
---|
[cd50b8c] | 142 | }
|
---|
| 143 |
|
---|
| 144 | SDL_Surface *lin = TTF_RenderText_Blended(fon, line, TXT_FGR);
|
---|
[7ba68aa] | 145 |
|
---|
| 146 | if (lin == NULL) {
|
---|
| 147 | fail("TTF_RenderText_Blended() failed: %s", TTF_GetError());
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[cd50b8c] | 150 | if (SDL_BlitSurface(lin, NULL, txt, &(SDL_Rect){
|
---|
[7ba68aa] | 151 | .x = 0,
|
---|
| 152 | .y = y * fon_h,
|
---|
[c8a92ef] | 153 | .w = TXT_W * fon_w,
|
---|
[7ba68aa] | 154 | .h = fon_h
|
---|
| 155 | })) {
|
---|
| 156 | fail("SDL_BlitSurface() failed: %s", SDL_GetError());
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | SDL_FreeSurface(lin);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[cd50b8c] | 162 | SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, txt);
|
---|
[7ba68aa] | 163 |
|
---|
| 164 | if (tex == NULL) {
|
---|
| 165 | fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | if (SDL_RenderCopy(ren, tex, NULL, NULL) < 0) {
|
---|
| 169 | fail("SDL_RenderCopy() failed: %s", SDL_GetError());
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[cd50b8c] | 172 | SDL_DestroyTexture(tex);
|
---|
[c8a92ef] | 173 |
|
---|
[c6f1cf5] | 174 | void *buf;
|
---|
| 175 | int32_t pitch;
|
---|
| 176 |
|
---|
| 177 | if (SDL_LockTexture(gfx, NULL, &buf, &pitch) < 0) {
|
---|
| 178 | fail("SDL_LockTexture() failed: %s", SDL_GetError());
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | uint32_t *pix = buf;
|
---|
| 182 |
|
---|
| 183 | for (int32_t y = 0; y < GFX_H; ++y) {
|
---|
| 184 | for (int32_t x = 0; x < GFX_W; ++x) {
|
---|
| 185 | uint8_t b = mem_gfx[y * GFX_W + x];
|
---|
| 186 |
|
---|
| 187 | for (int32_t p = 0; p < GFX_PIX; ++p) {
|
---|
| 188 | bool set = (b & (1 << (7 - p))) != 0;
|
---|
| 189 | *pix++ = set ? GFX_FGR : GFX_BGR;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | pix += pitch / 4 - GFX_W * GFX_PIX;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | SDL_UnlockTexture(gfx);
|
---|
| 197 |
|
---|
[cd50b8c] | 198 | if (SDL_RenderCopy(ren, gfx, NULL, NULL) < 0) {
|
---|
[c8a92ef] | 199 | fail("SDL_RenderCopy() failed: %s", SDL_GetError());
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[7ba68aa] | 202 | SDL_RenderPresent(ren);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[a06aa8b] | 205 | void lcd_init(void)
|
---|
| 206 | {
|
---|
| 207 | ver("lcd init");
|
---|
[7ba68aa] | 208 |
|
---|
[cd50b8c] | 209 | win = SDL_CreateWindow("LCD", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
---|
[7ba68aa] | 210 | WIN_W, WIN_H, 0);
|
---|
| 211 |
|
---|
| 212 | if (win == NULL) {
|
---|
| 213 | fail("SDL_CreateWindow() failed: %s", SDL_GetError());
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[81d7cdc] | 216 | lcd_win = SDL_GetWindowID(win);
|
---|
| 217 |
|
---|
| 218 | if (lcd_win == 0) {
|
---|
| 219 | fail("SDL_GetWindowID() failed: %s", SDL_GetError());
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[7ba68aa] | 222 | ren = SDL_CreateRenderer(win, -1, 0);
|
---|
| 223 |
|
---|
| 224 | if (ren == NULL) {
|
---|
| 225 | fail("SDL_CreateRenderer() failed: %s", SDL_GetError());
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[cd50b8c] | 228 | gfx = SDL_CreateTexture(ren, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING,
|
---|
| 229 | GFX_W * GFX_PIX, GFX_H);
|
---|
| 230 |
|
---|
| 231 | if (gfx == NULL) {
|
---|
| 232 | fail("SDL_CreateTexture() failed: %s", SDL_GetError());
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | if (SDL_SetTextureBlendMode(gfx, SDL_BLENDMODE_BLEND) < 0) {
|
---|
| 236 | fail("SDL_SetTextureBlendMode() failed: %s", SDL_GetError());
|
---|
| 237 | }
|
---|
[7ba68aa] | 238 |
|
---|
| 239 | SDL_RWops *ops = SDL_RWFromFile(font, "rb");
|
---|
| 240 |
|
---|
| 241 | if (ops == NULL) {
|
---|
| 242 | fail("error while opening font file %s: %s", font, SDL_GetError());
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | fon = TTF_OpenFontRW(ops, 1, 32);
|
---|
| 246 |
|
---|
| 247 | if (fon == NULL) {
|
---|
| 248 | fail("error while loading font file %s: %s", font, TTF_GetError());
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | fon_h = TTF_FontLineSkip(fon);
|
---|
| 252 |
|
---|
| 253 | if (TTF_GlyphMetrics(fon, 'X', NULL, NULL, NULL, NULL, &fon_w) < 0) {
|
---|
| 254 | fail("error while measuring font width: %s", TTF_GetError());
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[cd50b8c] | 257 | txt_w = TXT_W * fon_w;
|
---|
| 258 | txt_h = TXT_H * fon_h;
|
---|
[7ba68aa] | 259 |
|
---|
[cd50b8c] | 260 | txt = SDL_CreateRGBSurfaceWithFormat(0, txt_w, txt_h, 32, SDL_PIXELFORMAT_RGBA8888);
|
---|
[7ba68aa] | 261 |
|
---|
[cd50b8c] | 262 | if (txt == NULL) {
|
---|
[7ba68aa] | 263 | fail("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[cd50b8c] | 266 | for (int32_t i = 0; i < TXT_W * TXT_H; ++i) {
|
---|
| 267 | mem_txt[i] = ' ';
|
---|
[c8a92ef] | 268 | }
|
---|
[a06aa8b] | 269 | }
|
---|
| 270 |
|
---|
| 271 | void lcd_quit(void)
|
---|
| 272 | {
|
---|
| 273 | ver("lcd quit");
|
---|
[7ba68aa] | 274 |
|
---|
[cd50b8c] | 275 | SDL_FreeSurface(txt);
|
---|
[7ba68aa] | 276 | TTF_CloseFont(fon);
|
---|
| 277 |
|
---|
[cd50b8c] | 278 | SDL_DestroyTexture(gfx);
|
---|
| 279 |
|
---|
[7ba68aa] | 280 | SDL_DestroyRenderer(ren);
|
---|
| 281 | SDL_DestroyWindow(win);
|
---|
[a06aa8b] | 282 | }
|
---|
| 283 |
|
---|
[3c30832] | 284 | bool lcd_exec(void)
|
---|
[a06aa8b] | 285 | {
|
---|
[4c71d39] | 286 | ver3("lcd exec");
|
---|
[3c30832] | 287 | return false;
|
---|
[a06aa8b] | 288 | }
|
---|
| 289 |
|
---|
| 290 | uint32_t lcd_read(uint32_t off, int32_t sz)
|
---|
| 291 | {
|
---|
[4c71d39] | 292 | ver2("lcd rd %u:%d", off, sz * 8);
|
---|
[7ba68aa] | 293 |
|
---|
[cd50b8c] | 294 | if (sz != 1 || off != 1) {
|
---|
[18e37d6] | 295 | fail("invalid lcd rd %u:%d", off, sz * 8);
|
---|
| 296 | }
|
---|
[7ba68aa] | 297 |
|
---|
[18e37d6] | 298 | switch (com) {
|
---|
[1b30217] | 299 | case COM_MREAD:
|
---|
[18e37d6] | 300 | if (cur >= BASE_TXT && cur < BASE_TXT + TXT_W * TXT_H) {
|
---|
| 301 | return mem_txt[cur - BASE_TXT];
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | if (cur >= BASE_GFX && cur < BASE_GFX + GFX_W * GFX_H) {
|
---|
| 305 | return mem_gfx[cur - BASE_GFX];
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | return 0x00;
|
---|
[43ea417] | 309 |
|
---|
[18e37d6] | 310 | default:
|
---|
| 311 | return 0x00;
|
---|
[7ba68aa] | 312 | }
|
---|
[a06aa8b] | 313 | }
|
---|
| 314 |
|
---|
[18e37d6] | 315 | static void proc_arg(int32_t val)
|
---|
[a06aa8b] | 316 | {
|
---|
[18e37d6] | 317 | switch (com) {
|
---|
[1b30217] | 318 | case COM_MWRITE:
|
---|
[18e37d6] | 319 | if (cur >= BASE_TXT && cur < BASE_TXT + TXT_W * TXT_H) {
|
---|
| 320 | mem_txt[cur - BASE_TXT] = (uint8_t)val;
|
---|
[43ea417] | 321 | }
|
---|
[18e37d6] | 322 | else if (cur >= BASE_GFX && cur < BASE_GFX + GFX_W * GFX_H) {
|
---|
| 323 | mem_gfx[cur - BASE_GFX] = (uint8_t)val;
|
---|
[43ea417] | 324 | }
|
---|
| 325 |
|
---|
[18e37d6] | 326 | cur += dir;
|
---|
| 327 | SDL_AtomicAdd(&frame, 1);
|
---|
| 328 | break;
|
---|
| 329 |
|
---|
[1b30217] | 330 | case COM_CRSWR:
|
---|
[18e37d6] | 331 | if (n_arg == 0) {
|
---|
| 332 | cur = val;
|
---|
| 333 | }
|
---|
| 334 | else if (n_arg == 1) {
|
---|
| 335 | cur |= val << 8;
|
---|
| 336 |
|
---|
| 337 | if (cur < BASE_TXT ||
|
---|
| 338 | (cur >= BASE_TXT + TXT_W * TXT_H && cur < BASE_GFX) ||
|
---|
| 339 | cur >= BASE_GFX + GFX_W * GFX_H) {
|
---|
| 340 | fail("invalid address 0x%04x", cur);
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | break;
|
---|
| 345 |
|
---|
| 346 | default:
|
---|
| 347 | break;
|
---|
[43ea417] | 348 | }
|
---|
[18e37d6] | 349 | }
|
---|
| 350 |
|
---|
| 351 | static void proc_com(int32_t val)
|
---|
| 352 | {
|
---|
| 353 | switch (val) {
|
---|
[1b30217] | 354 | case COM_CRSWR:
|
---|
| 355 | case COM_MREAD:
|
---|
| 356 | case COM_MWRITE:
|
---|
[18e37d6] | 357 | com = val;
|
---|
| 358 | break;
|
---|
| 359 |
|
---|
[1b30217] | 360 | case COM_CRSMRT:
|
---|
[18e37d6] | 361 | dir = DIR_RIGHT;
|
---|
[1b30217] | 362 | com = COM_NONE;
|
---|
[18e37d6] | 363 | break;
|
---|
| 364 |
|
---|
[1b30217] | 365 | case COM_CRSMUP:
|
---|
[18e37d6] | 366 | dir = DIR_UP;
|
---|
[1b30217] | 367 | com = COM_NONE;
|
---|
[18e37d6] | 368 | break;
|
---|
| 369 |
|
---|
[1b30217] | 370 | case COM_CRSMDN:
|
---|
[18e37d6] | 371 | dir = DIR_DOWN;
|
---|
[1b30217] | 372 | com = COM_NONE;
|
---|
[18e37d6] | 373 | break;
|
---|
| 374 |
|
---|
[1b30217] | 375 | case COM_DSPOFF:
|
---|
[18e37d6] | 376 | SDL_AtomicSet(&ena, 0);
|
---|
[1b30217] | 377 | com = COM_NONE;
|
---|
[18e37d6] | 378 | break;
|
---|
| 379 |
|
---|
[1b30217] | 380 | case COM_DSPON:
|
---|
[18e37d6] | 381 | SDL_AtomicSet(&ena, 1);
|
---|
[1b30217] | 382 | com = COM_NONE;
|
---|
[18e37d6] | 383 | break;
|
---|
| 384 |
|
---|
| 385 | default:
|
---|
[1b30217] | 386 | com = COM_NONE;
|
---|
[18e37d6] | 387 | break;
|
---|
[43ea417] | 388 | }
|
---|
[18e37d6] | 389 | }
|
---|
[43ea417] | 390 |
|
---|
[18e37d6] | 391 | void lcd_write(uint32_t off, int32_t sz, uint32_t val)
|
---|
| 392 | {
|
---|
| 393 | ver2("lcd wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
|
---|
| 394 |
|
---|
| 395 | if (sz != 1 || off > 1) {
|
---|
| 396 | fail("invalid lcd wr %u:%d", off, sz * 8);
|
---|
[7ba68aa] | 397 | }
|
---|
[18e37d6] | 398 |
|
---|
| 399 | switch (off) {
|
---|
| 400 | case REG_ARG:
|
---|
| 401 | proc_arg((int32_t)val);
|
---|
| 402 | ++n_arg;
|
---|
| 403 | break;
|
---|
| 404 |
|
---|
| 405 | case REG_COM:
|
---|
| 406 | proc_com((int32_t)val);
|
---|
| 407 | n_arg = 0;
|
---|
| 408 | break;
|
---|
| 409 |
|
---|
| 410 | default:
|
---|
| 411 | break;
|
---|
[7ba68aa] | 412 | }
|
---|
[a06aa8b] | 413 | }
|
---|