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