// ESPSSS 002 — read speed of internal SRAM, PSRAM and flash on the ESP32-S3.
// The read loops run from IRAM; time is counted in CPU cycles.
// "stride-32" reads one word every 32 bytes, so every read touches a new cache line.
// Needs Tools -> PSRAM: OPI PSRAM for the PSRAM part. CPU at 240 MHz.
// Read speed: internal SRAM vs PSRAM vs flash (rodata), ESP32-S3, arduino-esp32 3.3.x
#include <esp_heap_caps.h>
#include <esp_cpu.h>

#define FLASH_N (512 * 1024 / 4)
// big const table in flash; filled by constexpr so the compiler cannot fold reads
struct Tab { uint32_t v[FLASH_N]; constexpr Tab() : v() { for (uint32_t i = 0; i < FLASH_N; i++) v[i] = i * 2654435761u; } };
static constexpr Tab flash_tab{};

static uint32_t IRAM_ATTR seq(const uint32_t* p, size_t n) {
  uint32_t s = 0;
  for (size_t i = 0; i < n; i += 4) s += p[i] + p[i + 1] + p[i + 2] + p[i + 3];
  return s;
}
static uint32_t IRAM_ATTR stride(const uint32_t* p, size_t n) {  // one word per 32-byte cache line
  uint32_t s = 0;
  for (size_t i = 0; i < n; i += 8) s += p[i];
  return s;
}

void run(const char* name, const uint32_t* p, size_t bytes) {
  size_t n = bytes / 4;
  volatile uint32_t sink = 0;
  for (int pass = 0; pass < 3; pass++) {
    uint32_t t0 = esp_cpu_get_cycle_count(); sink += seq(p, n); uint32_t t1 = esp_cpu_get_cycle_count();
    sink += stride(p, n); uint32_t t2 = esp_cpu_get_cycle_count();
    double seq_mbs = bytes / ((t1 - t0) / 240.0);                 // bytes per us = MB/s
    double cyc_line = (double)(t2 - t1) / (n / 8);                // cycles per cache line touched
    Serial.printf("  %-22s %7u B pass %d: seq read %7.1f MB/s  (%.2f cyc/word)   stride-32: %6.1f cyc/line\n",
                  name, (unsigned)bytes, pass, seq_mbs, (double)(t1 - t0) / n, cyc_line);
  }
}

void setup() {
  Serial.begin(115200);
  delay(3000);
  Serial.printf("\n=== MEMSPEED %s, CPU %u MHz, psram=%d ===\n", __DATE__ " " __TIME__, getCpuFrequencyMhz(), psramFound());
  const size_t SMALL = 16 * 1024, BIG = 256 * 1024;
  uint32_t* in = (uint32_t*)heap_caps_malloc(BIG, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  for (size_t i = 0; i < BIG / 4; i++) in[i] = i;
  run("internal SRAM", in, SMALL);
  run("internal SRAM", in, BIG);
  if (psramFound()) {
    const size_t HUGE_ = 2 * 1024 * 1024;
    uint32_t* ps = (uint32_t*)heap_caps_malloc(HUGE_, MALLOC_CAP_SPIRAM);
    for (size_t i = 0; i < HUGE_ / 4; i++) ps[i] = i;
    run("PSRAM", ps, SMALL);
    run("PSRAM", ps, HUGE_);
    // memcpy
    uint32_t t0 = esp_cpu_get_cycle_count(); memcpy(ps, in, BIG); uint32_t t1 = esp_cpu_get_cycle_count();
    memcpy(in, ps + BIG / 4, BIG); uint32_t t2 = esp_cpu_get_cycle_count();
    Serial.printf("  memcpy 256K internal->PSRAM %.1f MB/s, PSRAM->internal %.1f MB/s\n",
                  BIG / ((t1 - t0) / 240.0), BIG / ((t2 - t1) / 240.0));
  }
  { uint32_t t0 = esp_cpu_get_cycle_count(); memcpy(in, in + BIG / 8, BIG / 2); uint32_t t1 = esp_cpu_get_cycle_count();
    Serial.printf("  memcpy 128K internal->internal %.1f MB/s\n", (BIG / 2) / ((t1 - t0) / 240.0)); }
  run("flash rodata", flash_tab.v, SMALL);
  run("flash rodata", flash_tab.v, sizeof(flash_tab.v));
  Serial.printf("  flash_tab at %p\n", flash_tab.v);
  Serial.println("=== END ===");
}
void loop() { delay(1000); }
