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