/* Cloudhil real case: the same fixed-point workload timed on two real boards.
 *
 * 1. FIR filter, 64 taps, Q15, 4096 input samples  -> ticks in out[0], checksum in out[1]
 * 2. CRC-32 (bitwise, poly 0xEDB88320) over 8192 bytes -> ticks in out[2], CRC in out[3]
 *
 * Time base: TIM2 (32 bits), prescaler 0. With the clock tree left at its reset
 * values the timer counts one tick per CPU clock cycle (HSI 16 MHz on STM32F429,
 * HSI 64 MHz on STM32H735). No FPU, no cache setup, no libc.
 *
 * pass = 1 only when both results equal the values computed on the host
 * from this same file (cc -DHOST bench.c).
 */
#include <stdint.h>

#define TAPS 64
#define NIN 4096
#define NCRC 8192

#ifdef HOST
#include <stdio.h>
#else
#ifndef MAILBOX
#error "MAILBOX"
#endif
#define NNOK 0x4E4E4F4Bu
#define TIM2_BASE 0x40000000u
#define TIM_CR1 (*(volatile uint32_t *)(TIM2_BASE + 0x00u))
#define TIM_EGR (*(volatile uint32_t *)(TIM2_BASE + 0x14u))
#define TIM_CNT (*(volatile uint32_t *)(TIM2_BASE + 0x24u))
#define TIM_PSC (*(volatile uint32_t *)(TIM2_BASE + 0x28u))
#define TIM_ARR (*(volatile uint32_t *)(TIM2_BASE + 0x2Cu))
#ifndef RCC_TIM2EN_REG
#error "RCC_TIM2EN_REG"
#endif
#define RCC_TIM2EN (*(volatile uint32_t *)RCC_TIM2EN_REG)

extern uint32_t _estack;
void Reset_Handler(void);
__attribute__((section(".isr_vector"), used))
const uint32_t vectors[2] = {(uint32_t)&_estack, (uint32_t)Reset_Handler};

struct Mailbox {
    uint32_t nn_out[4];
    uint32_t nn_pass;
    uint32_t nn_fail;
    uint32_t nn_magic;
} __attribute__((packed));
#endif

static int16_t x[NIN];
static int16_t h[TAPS];
static uint8_t buf[NCRC];

static uint32_t lcg(uint32_t *s) {
    *s = *s * 1664525u + 1013904223u;
    return *s;
}

static void fill(void) {
    uint32_t s = 0xC10D411u;
    for (int i = 0; i < NIN; i++) x[i] = (int16_t)(lcg(&s) >> 16);
    for (int k = 0; k < TAPS; k++) h[k] = (int16_t)((k < TAPS / 2 ? k + 1 : TAPS - k) * 16);
    for (int i = 0; i < NCRC; i++) buf[i] = (uint8_t)(lcg(&s) >> 24);
}

__attribute__((noipa)) static uint32_t fir(void) {
    uint32_t sum = 0;
    for (int n = TAPS - 1; n < NIN; n++) {
        int32_t acc = 0;
        for (int k = 0; k < TAPS; k++) acc += (int32_t)h[k] * x[n - k];
        int16_t y = (int16_t)(acc >> 15);
        sum = sum * 31u + (uint16_t)y;
    }
    return sum;
}

__attribute__((noipa)) static uint32_t crc32(void) {
    uint32_t c = 0xFFFFFFFFu;
    for (int i = 0; i < NCRC; i++) {
        c ^= buf[i];
        for (int b = 0; b < 8; b++) c = (c >> 1) ^ (0xEDB88320u & (0u - (c & 1u)));
    }
    return ~c;
}

#ifdef HOST
int main(void) {
    fill();
    printf("-DEXP_FIR=0x%08Xu -DEXP_CRC=0x%08Xu\n", fir(), crc32());
    return 0;
}
#else
int main(void) {
    volatile struct Mailbox *mb = (volatile struct Mailbox *)(uintptr_t)MAILBOX;
    RCC_TIM2EN |= 1u;
    (void)RCC_TIM2EN;
    TIM_PSC = 0;
    TIM_ARR = 0xFFFFFFFFu;
    TIM_EGR = 1u;
    TIM_CR1 = 1u;
    fill();
    uint32_t t0 = TIM_CNT;
    uint32_t f = fir();
    uint32_t t1 = TIM_CNT;
    uint32_t c = crc32();
    uint32_t t2 = TIM_CNT;
    mb->nn_out[0] = t1 - t0;
    mb->nn_out[1] = f;
    mb->nn_out[2] = t2 - t1;
    mb->nn_out[3] = c;
    int ok = (f == EXP_FIR) && (c == EXP_CRC) && (t1 != t0);
    mb->nn_pass = ok ? 1u : 0u;
    mb->nn_fail = ok ? 0u : 1u;
    mb->nn_magic = NNOK;
    __asm volatile("dsb" ::: "memory");
    for (;;) __asm volatile("wfi");
}

void Reset_Handler(void) {
    main();
    for (;;) {
    }
}
#endif
