/* EP07 - DDR3 native-UI DMA / pattern / CRC / bandwidth test.
 * Board: ALINX PI-AR035-F1-1, XC7A35T-FGG484-2, MT41J128M16 DDR3 (256 MiB).
 * CPU: CV32E20 at 50 MHz. DDR3 user clock: 200 MHz, 128-bit native UI.
 */
#include <stdint.h>

#define UART_BASE   0x10000000UL
#define UART_TX     (*(volatile unsigned char *)(UART_BASE + 0x00))
#define UART_STAT   (*(volatile uint32_t *)(UART_BASE + 0x04))

#define DDR_BASE    0x20010000UL
#define DDR_CTRL    (*(volatile uint32_t *)(DDR_BASE + 0x00))
#define DDR_STATUS  (*(volatile uint32_t *)(DDR_BASE + 0x04))
#define DDR_SIZE    (*(volatile uint32_t *)(DDR_BASE + 0x08))
#define DDR_WR_CYC  (*(volatile uint32_t *)(DDR_BASE + 0x0c))
#define DDR_RD_CYC  (*(volatile uint32_t *)(DDR_BASE + 0x10))
#define DDR_CRC_CYC (*(volatile uint32_t *)(DDR_BASE + 0x14))
#define DDR_BEATS   (*(volatile uint32_t *)(DDR_BASE + 0x18))
#define DDR_ERRORS  (*(volatile uint32_t *)(DDR_BASE + 0x1c))
#define DDR_FERR    (*(volatile uint32_t *)(DDR_BASE + 0x20))
#define DDR_CRC_D   (*(volatile uint32_t *)(DDR_BASE + 0x24))
#define DDR_CRC_E   (*(volatile uint32_t *)(DDR_BASE + 0x28))
#define DDR_VER     (*(volatile uint32_t *)(DDR_BASE + 0x2c))

#define ST_CALIB    0x01u
#define ST_BUSY     0x02u
#define ST_DONE     0x04u
#define ST_FAIL     0x08u
#define ST_CRC_OK   0x10u

static void uart_putc(char c)
{
    while (UART_STAT & 1u) { }
    UART_TX = (unsigned char)c;
}

static void uart_puts(const char *s)
{
    while (*s) uart_putc(*s++);
}

static void uart_putu(uint32_t v)
{
    char b[11];
    int i = 0;
    if (v == 0u) { uart_putc('0'); return; }
    while (v) { b[i++] = (char)('0' + (v % 10u)); v /= 10u; }
    while (i) uart_putc(b[--i]);
}

static void uart_putu64(uint64_t v)
{
    char b[21];
    int i = 0;
    if (v == 0u) { uart_putc('0'); return; }
    while (v) { b[i++] = (char)('0' + (int)(v % 10u)); v /= 10u; }
    while (i) uart_putc(b[--i]);
}

static void uart_puthex(uint32_t v)
{
    int i;
    uart_puts("0x");
    for (i = 7; i >= 0; i--)
        uart_putc("0123456789abcdef"[(v >> (i * 4)) & 0xfu]);
}

static inline uint32_t rd_lo(void)
{
    uint32_t v;
    __asm__ volatile ("csrr %0, mcycle" : "=r"(v));
    return v;
}

static inline uint32_t rd_hi(void)
{
    uint32_t v;
    __asm__ volatile ("csrr %0, mcycleh" : "=r"(v));
    return v;
}

/* 64-bit cycle read, robust against the low word wrapping. */
static uint64_t cycles64(void)
{
    uint32_t hi, lo, hi2;
    do {
        hi  = rd_hi();
        lo  = rd_lo();
        hi2 = rd_hi();
    } while (hi != hi2);
    return ((uint64_t)hi << 32) | lo;
}

static uint32_t mb_per_s(uint32_t bytes, uint32_t cycles)
{
    uint64_t v;
    if (cycles == 0u) return 0u;
    /* bytes / (cycles * 5 ns) / 1e6 = bytes * 100 / cycles */
    v = (uint64_t)bytes * 100u / cycles;
    return (uint32_t)v;
}

static int wait_calib(void)
{
    uint64_t start = cycles64();
    while ((DDR_STATUS & ST_CALIB) == 0u) {
        if (cycles64() - start > 250000000ull)   /* 5 s at 50 MHz */
            return -1;
    }
    return 0;
}

static int run_one(uint32_t sel, uint32_t bytes, const char *name, int verbose)
{
    uint64_t t0, t1;
    uint32_t status, timeout;
    uint32_t wr, rd, crc, beats, errs, crcd, crce;
    uint32_t wm, rm, cm;

    uart_puts("EP07 RUN size=");
    uart_putu(bytes);
    uart_puts(" name=");
    uart_puts(name);
    uart_putc('\r'); uart_putc('\n');

    t0 = cycles64();
    DDR_CTRL = 0x01u | (sel << 1) | (1u << 4);   /* start, size, CRC enable */
    timeout = 0u;
    do {
        status = DDR_STATUS;
        if (status & ST_FAIL) break;
        timeout++;
        if (timeout > 40000000u) break;           /* > 5 s CPU loop guard */
    } while ((status & ST_DONE) == 0u);
    t1 = cycles64();

    wr    = DDR_WR_CYC;
    rd    = DDR_RD_CYC;
    crc   = DDR_CRC_CYC;
    beats = DDR_BEATS;
    errs  = DDR_ERRORS;
    crcd  = DDR_CRC_D;
    crce  = DDR_CRC_E;
    wm    = mb_per_s(bytes, wr);
    rm    = mb_per_s(bytes, rd);
    cm    = mb_per_s(bytes, crc);

    uart_puts("EP07 METRIC size=");
    uart_putu(bytes);
    uart_puts(" beats=");
    uart_putu(beats);
    uart_puts(" wr_cycles=");
    uart_putu(wr);
    uart_puts(" rd_cycles=");
    uart_putu(rd);
    uart_puts(" crc_cycles=");
    uart_putu(crc);
    uart_puts(" wr_MBps=");
    uart_putu(wm);
    uart_puts(" rd_MBps=");
    uart_putu(rm);
    uart_puts(" crc_MBps=");
    uart_putu(cm);
    uart_puts(" err=");
    uart_putu(errs);
    uart_puts(" crc_data=");
    uart_puthex(crcd);
    uart_puts(" crc_exp=");
    uart_puthex(crce);
    uart_putc('\r'); uart_putc('\n');

    if (verbose) {
        uart_puts("EP07 DETAIL status=");
        uart_puthex(status);
        uart_puts(" cpu_cycles=");
        uart_putu64(t1 - t0);
        uart_puts(" first_err=");
        uart_puthex(DDR_FERR);
        uart_puts(" timeout=");
        uart_putu(timeout);
        uart_putc('\r'); uart_putc('\n');
    }

    if ((status & ST_DONE) == 0u) return -1;
    if ((status & ST_FAIL) != 0u) return -1;
    if (errs != 0u) return -1;
    if (beats != (bytes >> 4)) return -1;
    if ((status & ST_CRC_OK) == 0u) return -1;
    if (crcd != crce) return -1;
    return 0;
}

int main(void)
{
    int fail = 0;
    int round;

    uart_puts("\r\nEP07 DDR3 DMA on ALINX PI-AR035-F1-1\r\n");
    uart_puts("EP07 VERSION=");
    uart_puthex(DDR_VER);
    uart_puts(" DDR_SIZE=");
    uart_putu(DDR_SIZE);
    uart_puts(" UI_CLK=100000000\r\n");

    if (wait_calib() != 0) {
        uart_puts("EP07 CALIB: FAIL\r\nEP07 RESULT: FAIL\r\n");
        for (;;) { }
    }
    uart_puts("EP07 CALIB: PASS\r\n");

    for (round = 1; round <= 2; round++) {
        uart_puts("EP07 ROUND ");
        uart_putu((uint32_t)round);
        uart_puts(" BEGIN\r\n");

        if (run_one(0u, 1u * 1024u * 1024u, "1MiB", 1) != 0) fail = 1;
        if (run_one(1u, 16u * 1024u * 1024u, "16MiB", 1) != 0) fail = 1;
        if (run_one(2u, 256u * 1024u * 1024u, "256MiB", 1) != 0) fail = 1;

        uart_puts("EP07 ROUND ");
        uart_putu((uint32_t)round);
        uart_puts(fail ? " FAIL\r\n" : " PASS\r\n");
    }

    uart_puts(fail ? "EP07 RESULT: FAIL\r\n" : "EP07 RESULT: PASS\r\n");
    for (;;) { }
    return 0;
}
