emulator-template: RGB565 wire-order contract + ready-to-build HW twin
Propagate the lessons from tron-room-demo's hardware bring-up back into the canonical template, so demos are hardware-testable from day one: - Contract change: the sketch renders into emuGfx::fb565 (320x240 uint16_t, RGB565 in PANEL WIRE ORDER - big-endian, the byte sequence the ST7789 receives over SPI, the same convention TFT_eSprite's primitives use). The host expands 565 to 8888 for SDL only. Native little-endian writes look right in the emulator but show byte-swapped on the panel (seen on hardware: BG 0x0002 -> green, fogged floor 0x0882 -> pink) - the new contract makes that bug impossible. - Recommended sketch structure: examples/APP/Core.h holds all rendering/logic device-agnostically (demoInit/demoInput/demoFrame); the .cpp is a thin emulator shell. Bundled sample demonstrates it. - New fw/ scaffold: standalone PlatformIO project (shared boards/lib, T-Deck pin map) with DemoHW.ino driving the same core through a TFT_eSprite frameBuffer() and reporting true fps + render/push split over USB serial every 2 s. -O2 explicitly (pio default -Os skews benchmarks). - shim.h gains rgb565()/wire565() helpers; Makefile deps now include the sketch's headers; --dump now also fires on safety-hatch exit (a long --frames run was silently losing its dump before). Verified: template builds + smoke renders correct 565 colors; DemoHW builds clean for ESP32-S3 (328 KB flash).
This commit is contained in:
@@ -5,7 +5,18 @@
|
||||
* 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.
|
||||
* no I2S, no GPIO. It only depends on SDL3 and the shim.
|
||||
*
|
||||
* COLOR CONTRACT (matches the real T-Deck panel pipeline): the sketch
|
||||
* renders into emuGfx::fb565 — a 320x240 buffer of 16-bit RGB565 pixels
|
||||
* held in PANEL WIRE ORDER (big-endian: each pixel's high byte is what
|
||||
* the ST7789 receives first over SPI; the same convention TFT_eSprite's
|
||||
* own primitives use on hardware). The host expands 565 -> 8888 for the
|
||||
* desktop window only. On the device the 565 buffer is pushed as-is, so
|
||||
* anything you see here is exactly what the panel would show — including
|
||||
* 5-bit color banding. Sketches that ignore this and write native
|
||||
* little-endian values will render byte-swapped on hardware even though
|
||||
* they look right in the emulator.
|
||||
*/
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
@@ -24,7 +35,7 @@
|
||||
#include "shim.h"
|
||||
|
||||
// --- Emulator shared state (defined in main.cpp below) ---
|
||||
// The sketch writes to emuGfx::fb; this file presents it each frame.
|
||||
// The sketch writes to emuGfx::fb565; this file presents it each frame.
|
||||
// ---------------------------------------------------------------------------
|
||||
namespace emu {
|
||||
uint8_t pinLevels[64];
|
||||
@@ -37,11 +48,26 @@ namespace emu {
|
||||
} // namespace emu
|
||||
|
||||
namespace emuGfx {
|
||||
uint32_t fb[320 * 240]; // ABGR8888 pixels, 320x240
|
||||
uint16_t fb565[320 * 240]; // RGB565, panel wire order — THE sketch target
|
||||
uint32_t fb[320 * 240]; // ABGR8888 staging for SDL (host-internal)
|
||||
SDL_Texture *tex = nullptr;
|
||||
bool dirty = true;
|
||||
} // namespace emuGfx
|
||||
|
||||
// Expand the 565 wire-order buffer into the 8888 staging buffer.
|
||||
// Wire order = big-endian 565: unswap, then replicate 5/6/5 -> 8/8/8 bits.
|
||||
static void expand565()
|
||||
{
|
||||
for (int i = 0; i < 320 * 240; i++) {
|
||||
uint16_t c = (uint16_t)((emuGfx::fb565[i] << 8) | (emuGfx::fb565[i] >> 8));
|
||||
uint32_t r5 = (c >> 11) & 31, g6 = (c >> 5) & 63, b5 = c & 31;
|
||||
uint32_t r = (r5 << 3) | (r5 >> 2);
|
||||
uint32_t g = (g6 << 2) | (g6 >> 4);
|
||||
uint32_t b = (b5 << 3) | (b5 >> 2);
|
||||
emuGfx::fb[i] = 0xFF000000u | (b << 16) | (g << 8) | r;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Forward declarations of the user's sketch functions ---
|
||||
// These will be defined in the sketch .cpp file included below.
|
||||
extern "C" void setup();
|
||||
@@ -56,6 +82,22 @@ extern "C" void loop();
|
||||
#include APP_SRC
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// Save the current frame (after 565 expansion) as a BMP.
|
||||
static void dumpFrame(const char *path)
|
||||
{
|
||||
if (!path) return;
|
||||
expand565();
|
||||
SDL_Surface *s = SDL_CreateSurface(320, 240, SDL_PIXELFORMAT_ABGR8888);
|
||||
if (!s) return;
|
||||
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_SaveBMP(s, path);
|
||||
SDL_DestroySurface(s);
|
||||
std::printf("[emu] frame dumped to %s\n", path);
|
||||
}
|
||||
|
||||
// --- Main entry point ---
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
@@ -155,6 +197,7 @@ int main(int argc, char **argv)
|
||||
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);
|
||||
dumpFrame(dumpPath);
|
||||
running = false;
|
||||
break;
|
||||
}
|
||||
@@ -164,8 +207,9 @@ int main(int argc, char **argv)
|
||||
|
||||
// --- Present frame if dirty ---
|
||||
if (emuGfx::dirty) {
|
||||
expand565();
|
||||
SDL_UpdateTexture(emuGfx::tex, nullptr, emuGfx::fb,
|
||||
320 * sizeof(uint32_t));
|
||||
320 * sizeof(uint32_t));
|
||||
emuGfx::dirty = false;
|
||||
}
|
||||
|
||||
@@ -177,26 +221,7 @@ int main(int argc, char **argv)
|
||||
// --- 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);
|
||||
}
|
||||
}
|
||||
dumpFrame(dumpPath);
|
||||
}
|
||||
|
||||
// --- Frame pacing: sleep until the next 60 Hz tick ---
|
||||
@@ -213,22 +238,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
|
||||
// --- 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");
|
||||
}
|
||||
}
|
||||
dumpFrame("frame.bmp");
|
||||
|
||||
// --- Cleanup ---
|
||||
TTF_Quit();
|
||||
@@ -237,4 +247,4 @@ int main(int argc, char **argv)
|
||||
SDL_DestroyWindow(win);
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user