/** * Minimal SDL3 host for the T-Deck emulator-only template. * * Sets up an SDL3 window/renderer, presents the user's framebuffer, * and calls the user's sketch setup() / loop() each frame. * * This file is deliberately hardware-free: no TFT_eSPI, no SPI, * no I2S, no GPIO matrix. It only depends on SDL3 and the shim. */ #include #if __has_include() #include #else #include #endif #include #include #include #include // --- Include the template's minimal shim (provides millis(), random, // randomSeed(), String, pinMode/digitalRead, etc.) --- #include "shim.h" // --- Emulator shared state (defined in main.cpp below) --- // The sketch writes to emuGfx::fb; this file presents it each frame. // --------------------------------------------------------------------------- namespace emu { uint8_t pinLevels[64]; const char *pinDrivenBy[64]; std::deque pinEdges; bool touchDown = false; int touchX = 0, touchY = 0; std::deque keyboard; bool quitRequested = false; } // namespace emu namespace emuGfx { uint32_t fb[320 * 240]; // ABGR8888 pixels, 320x240 SDL_Texture *tex = nullptr; bool dirty = true; } // namespace emuGfx // --- Forward declarations of the user's sketch functions --- // These will be defined in the sketch .cpp file included below. extern "C" void setup(); extern "C" void loop(); // --- Include the user's sketch .cpp file --- // The path comes from the Makefile: -DAPP_SRC='"examples/$(APP)/$(APP).cpp"'. // Change APP (or replace examples/Demo/Demo.cpp) to run your own sketch. #ifndef APP_SRC #define APP_SRC "examples/Demo/Demo.cpp" #endif #include APP_SRC // --------------------------------------------------------------------------- // --- Main entry point --- int main(int argc, char **argv) { // Parse simple command-line options long maxFrames = -1; // --frames N: run exactly N frames then exit const char *dumpPath = nullptr; bool userRun = false; // --user-run: disable the safety-hatch timeout long timeoutMs = 10000; // safety hatch: auto-exit after 10 s by default for (int i = 1; i < argc; i++) { if (!strcmp(argv[i], "--frames") && i + 1 < argc) maxFrames = atol(argv[++i]); else if (!strcmp(argv[i], "--dump") && i + 1 < argc) dumpPath = argv[++i]; else if (!strcmp(argv[i], "--user-run")) userRun = true; else if (!strcmp(argv[i], "--timeout") && i + 1 < argc) timeoutMs = atol(argv[++i]); } // SDL3 initialization if (!SDL_Init(SDL_INIT_VIDEO)) { std::printf("SDL_Init failed: %s\n", SDL_GetError()); return 1; } TTF_Init(); // Create window matching the 320x240 canvas SDL_Window *win = SDL_CreateWindow("T-Deck: " APP_NAME, 320, 240, SDL_WINDOW_RESIZABLE); if (!win) { std::printf("SDL_CreateWindow failed: %s\n", SDL_GetError()); TTF_Quit(); SDL_Quit(); return 1; } SDL_Renderer *ren = SDL_CreateRenderer(win, NULL); if (!ren) ren = SDL_CreateRenderer(win, "software"); SDL_SetRenderLogicalPresentation(ren, 320, 240, SDL_LOGICAL_PRESENTATION_LETTERBOX); emuGfx::tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, 320, 240); if (!emuGfx::tex) { std::printf("SDL_CreateTexture failed: %s\n", SDL_GetError()); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); TTF_Quit(); SDL_Quit(); return 1; } // --- User sketch initialization --- setup(); // --- Main loop --- bool running = true; // Frame pacing: the sketch contract is "loop() called once per frame // (~60 fps)". Without pacing this loop busy-spins at hundreds/thousands // of fps on a desktop and every frame-based animation runs too fast, // so we delay until the next 60 Hz tick before starting the next frame. const Uint64 FRAME_MS = 1000 / 60; // ~16.7 ms per frame Uint64 nextFrame = SDL_GetTicks() + FRAME_MS; while (running && !emu::quitRequested) { // --- Event pump --- SDL_Event e; while (SDL_PollEvent(&e)) { if (e.type == SDL_EVENT_QUIT) { running = false; break; } // Forward key presses to the emulated I2C keyboard device if (e.type == SDL_EVENT_KEY_DOWN) { if (e.key.repeat) break; SDL_Keycode k = e.key.key; if (k == SDLK_RETURN || k == SDLK_KP_ENTER) { emu::keyboard.push_back(' '); } else if (k == SDLK_SPACE) { emu::keyboard.push_back(' '); } else if (k == SDLK_BACKSPACE) { emu::keyboard.push_back(0x08); } else if (k == SDLK_DELETE) { emu::keyboard.push_back(0x7F); } else if (k == SDLK_ESCAPE) { running = false; } else { const char *name = SDL_GetKeyName(k); if (name && strlen(name) == 1) { emu::keyboard.push_back((uint8_t)tolower((unsigned char)name[0])); } } } } // --- Safety hatch: auto-exit unless the user asked to play --- if (!userRun && timeoutMs > 0 && millis() >= (unsigned long)timeoutMs) { std::printf("[emu] safety hatch: exiting after %ld ms " "(pass --user-run to run indefinitely)\n", timeoutMs); running = false; break; } // --- Call the user's simulation step --- loop(); // --- Present frame if dirty --- if (emuGfx::dirty) { SDL_UpdateTexture(emuGfx::tex, nullptr, emuGfx::fb, 320 * sizeof(uint32_t)); emuGfx::dirty = false; } // Render: clear -> texture -> present SDL_RenderClear(ren); SDL_RenderTexture(ren, emuGfx::tex, nullptr, nullptr); SDL_RenderPresent(ren); // --- Frame counting / dump --- if (maxFrames > 0 && --maxFrames == 0) { running = false; if (dumpPath) { // Create a surface from the texture and save as BMP SDL_Surface *s = SDL_CreateSurface(320, 240, SDL_PIXELFORMAT_ABGR8888); if (s) { // Read pixels from the texture void *pixels; int pitch; SDL_LockTexture(emuGfx::tex, nullptr, &pixels, &pitch); // Copy the framebuffer directly to the surface Uint32 *sp = (Uint32 *)s->pixels; for (int y = 0; y < 240; y++) { memcpy(sp + y * s->w, emuGfx::fb + y * 320, 320 * sizeof(Uint32)); } SDL_UnlockTexture(emuGfx::tex); SDL_SaveBMP(s, dumpPath); SDL_DestroySurface(s); std::printf("[emu] frame dumped to %s\n", dumpPath); } } } // --- Frame pacing: sleep until the next 60 Hz tick --- Uint64 nowMs = SDL_GetTicks(); if (nowMs >= nextFrame) { // Behind schedule (a slow frame or a stall): resync instead of // burst-catching-up, so animation never runs fast to "make up" // lost frames. nextFrame = nowMs; } else { SDL_Delay((Uint32)(nextFrame - nowMs)); } nextFrame += FRAME_MS; } // --- Save last frame as BMP for AI inspection --- { SDL_Surface *s = SDL_CreateSurface(320, 240, SDL_PIXELFORMAT_ABGR8888); if (s) { void *pixels; int pitch; SDL_LockTexture(emuGfx::tex, nullptr, &pixels, &pitch); Uint32 *sp = (Uint32 *)s->pixels; for (int y = 0; y < 240; y++) { memcpy(sp + y * s->w, emuGfx::fb + y * 320, 320 * sizeof(Uint32)); } SDL_UnlockTexture(emuGfx::tex); SDL_SaveBMP(s, "frame.bmp"); SDL_DestroySurface(s); std::printf("[emu] last frame saved to frame.bmp\n"); } } // --- Cleanup --- TTF_Quit(); SDL_DestroyTexture(emuGfx::tex); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); SDL_Quit(); return 0; }