// wtest.c - a tiny bare-metal ESP32-S3 program: the round-AMOLED board's
// "self-test". It runs on the from-scratch emulator with no mask ROM and no
// firmware image - just this one small binary loaded straight into SRAM. It
//
//   1. prints the board's details over UART0, probing the flash chip's JEDEC
//      id live through the emulated SPI command engine (so the 32 MB number
//      comes from the modelled hardware, not a constant), and
//   2. paints a colour-bar test pattern onto the CO5300 panel over QSPI, using
//      the same SPI2 + GDMA descriptor path the real display driver uses.
//
// It is deliberately small and self-contained: no libc, no stack beyond a few
// frames, and shallow calls, so it needs none of the ROM/IDF runtime. Built
// with the ESP xtensa-esp32s3-elf toolchain; see the Makefile `selftest` rule.

#include <stdint.h>

#define REG(a)  (*(volatile uint32_t *)(uintptr_t)(a))
#define REG8(a) (*(volatile uint8_t  *)(uintptr_t)(a))

// ---- UART0: a write to the TX register emits one byte on the console ----
#define UART0_TX 0x60000000u
static void putc_(char c)            { REG(UART0_TX) = (uint8_t)c; }
static void puts_(const char *s)     { while (*s) putc_(*s++); }
static void puthex2(uint8_t v)       { const char *h = "0123456789ABCDEF"; putc_(h[v >> 4]); putc_(h[v & 15]); }
static void putdec(uint32_t v)       { char b[12]; int i = 0; if (!v) { putc_('0'); return; }
                                       while (v) { b[i++] = '0' + v % 10; v /= 10; } while (i) putc_(b[--i]); }

// ---- SPI flash command engine (SPIMEM1 @ 0x6000_2000) ----
// Writing a dedicated-command bit to SPI_MEM_CMD runs one manual transaction;
// RDID (bit 28) returns the 3-byte JEDEC id into the W0 data buffer.
#define MSPI       0x60002000u
#define MSPI_CMD   (MSPI + 0x00)
#define MSPI_MISO  (MSPI + 0x28)      // SPI_MEM_MS_DLEN: MISO length in bits, minus 1
#define MSPI_W0    (MSPI + 0x58)      // read-data buffer
#define MSPI_RDID  (1u << 28)

// ---- GP-SPI2 (the CO5300's QSPI bus) + GDMA channel 0 ----
// Each panel command/parameter/pixel run is one SPI_USR transaction; the bytes
// are handed to the controller through a GDMA OUT descriptor, exactly as the
// esp_lcd QSPI panel driver does it.
#define SPI2_CMD      0x60024000u
#define SPI2_USR      (1u << 24)
#define GDMA_OUT_LINK 0x6003f080u

// DMA scratch, in SRAM1 on the data bus. GDMA rebuilds a descriptor's address
// as 0x3FC0_0000 | (link & 0xFFFFF), so these must be data-bus SRAM1 addresses.
#define DESC 0x3FCE0000u
#define PBUF 0x3FCE0100u

// Push `n` bytes to the panel as one GDMA-backed SPI2 transaction.
static void dma_send(const uint8_t *src, uint32_t n)
{
    for (uint32_t i = 0; i < n; i++) REG8(PBUF + i) = src[i];
    REG(DESC + 0) = (n << 12) | (1u << 30) | (1u << 31);  // length, suc_eof, owner=DMA
    REG(DESC + 4) = PBUF;                                  // buffer pointer
    REG(DESC + 8) = 0;                                     // last in chain
    REG(GDMA_OUT_LINK) = DESC & 0xFFFFFu;
    REG(SPI2_CMD) = SPI2_USR;                             // run it
}

// A CO5300 command header is {QSPI-opcode, 0, command, 0}; 0x02 = command,
// 0x32 = pixel write. Parameters and pixels follow as their own transactions.
static void cmd(uint8_t c)                           { uint8_t h[4] = { 0x02, 0x00, c, 0x00 }; dma_send(h, 4); }
static void ramwr(void)                              { uint8_t h[4] = { 0x32, 0x00, 0x2C, 0x00 }; dma_send(h, 4); }
static void p4(uint8_t a, uint8_t b, uint8_t c, uint8_t d) { uint8_t h[4] = { a, b, c, d }; dma_send(h, 4); }

#define W 466
#define H 466

// Set the panel's column/row window and open a RAMWR pixel stream into it.
static void window(int x0, int y0, int x1, int y1)
{
    cmd(0x2A); p4(x0 >> 8, x0 & 0xff, x1 >> 8, x1 & 0xff);   // CASET
    cmd(0x2B); p4(y0 >> 8, y0 & 0xff, y1 >> 8, y1 & 0xff);   // RASET
    ramwr();
}

int main(void)
{
    puts_("\r\nESP32-S3 round-AMOLED board - emulator self-test\r\n\r\n");
    puts_("  Board    Waveshare ESP32-S3-Touch-AMOLED-1.75C\r\n");
    puts_("  CPU      Xtensa LX7, dual core, up to 240 MHz\r\n");

    // Probe the flash chip for real: RDID through the emulated command engine.
    REG(MSPI_MISO) = 24 - 1;            // ask for 3 bytes
    REG(MSPI_CMD)  = MSPI_RDID;         // run RDID (0x9F)
    uint32_t id  = REG(MSPI_W0);
    uint8_t  mfg = id & 0xff, typ = (id >> 8) & 0xff, cap = (id >> 16) & 0xff;
    puts_("  Flash    JEDEC "); puthex2(mfg); putc_(' '); puthex2(typ); putc_(' '); puthex2(cap);
    puts_(" -> "); putdec(1u << (cap - 20)); puts_(" MB NOR\r\n");
    puts_("  PSRAM    8 MB octal\r\n");
    puts_("  Display  CO5300 AMOLED, 466x466, QSPI\r\n");
    puts_("  Touch    CST9217 capacitive, over I2C\r\n\r\n");

    puts_("  Painting QSPI display test pattern...\r\n");
    // Seven colour bands down the round panel. Colours are RGB565 sent
    // big-endian, which is the byte order the CO5300 wants over QSPI.
    static const uint16_t colour[7] = {
        0xF800, 0xFD20, 0xFFE0, 0x07E0, 0x07FF, 0x001F, 0xF81F,   // R O Y G C B M
    };
    for (int band = 0; band < 7; band++) {
        int y0 = band * H / 7;
        int y1 = (band + 1) * H / 7 - 1;
        if (band == 6) y1 = H - 1;
        uint16_t c = colour[band];
        uint8_t row[W * 2];
        for (int x = 0; x < W; x++) { row[x * 2] = c >> 8; row[x * 2 + 1] = c & 0xff; }
        window(0, y0, W - 1, y1);
        for (int y = y0; y <= y1; y++) dma_send(row, sizeof row);   // stream the band
    }
    puts_("  Done: 7 colour bands sent over the CO5300 QSPI bus.\r\n");

    for (;;) { }   // idle; the emulator simply stops stepping
    return 0;
}
