# RP2350 (Raspberry Pi Pico 2) integration RP2350 boots either two Arm Cortex-M33 cores or two Hazard3 RISC-V cores - picked as a pair at boot (via OTP/bootloader configuration), never both at once. libchdr builds for either; both are cross-compiled and RAM-budgeted in CI: | | Arm (Cortex-M33) | RISC-V (Hazard3) | |---|---|---| | toolchain file | `cmake/toolchain-cortex-m33.cmake` | `cmake/toolchain-hazard3.cmake` | | ISA/ABI | Armv8-M Mainline, hard-float | RV32IMAC_Zicsr_Zifencei, **soft-float** (no FPU) | | CI workflow | `.github/workflows/rp2350-arm-ram-budget.yml` | `.github/workflows/rp2350-riscv-ram-budget.yml` | | test dir | `tests/rp2350-arm/` | `tests/rp2350-riscv/` | ## Which one to actually use **Arm (Cortex-M33) is the better default.** It's what the Pico SDK targets by default, has a hardware FPU, and has the toolchain/debugger/SDK support every current RP2350 board and example assumes. Hazard3 is real and Pico-SDK supported, but newer, has no hardware FPU (everything soft-float, so more CPU-bound), and has a smaller ecosystem of examples/libraries to build on - pick it only if you have a specific reason to (e.g. wanting the open-hardware RISC-V core, or needing its extra PIO/interrupt features some Pico SDK release notes call out). For libchdr's own memory footprint, the choice barely matters: peak heap per codec is driven by allocation sizes inside the codecs, not the core ISA/ABI, so the two CI workflows above measure almost identical numbers (see each `check_budget.py` for the current baseline) - both comfortably fit the RP2350's 520KB SRAM. Soft-float Hazard3 code is somewhat larger/slower than hard-float Cortex-M33 code for the little floating-point libchdr uses, but that's a CPU-time/code-size tradeoff, not a RAM one. ## Using this with the Pico SDK 1. Vendor `include/`, `src/`, and `deps/` from this repo into your Pico SDK project (or add libchdr as a git submodule), building with `CHDR_LOWRAM_TARGET=ON`. The Pico SDK's own `pico_sdk_import.cmake` toolchain already targets the right core/ABI for whichever `PICO_PLATFORM` you pick (`rp2350-arm-s` or `rp2350-riscv`), so the toolchain files in this repo are for CI/local RAM-budget measurement only, not needed for a real Pico SDK build. 2. Access storage through FatFS (e.g. [carlk3/no-OS-FatFS-SD-SPI-RPi-Pico](https://github.com/carlk3/no-OS-FatFS-SD-SPI-RPi-Pico) for SD/MMC over SPI) and reuse `../tangcore-bl616/chd/chd_fatfs.{h,c}` as-is - it's a generic FatFS `core_file_callbacks` bridge (only calls `ff.h`'s `f_open`/`f_read`/`f_lseek`/`f_close`/`f_size`, nothing BL616-specific), not duplicated here. 3. Mount your SD card / flash filesystem, then call `chd_fatfs_open("game.chd", &fil, &chd)`. ## CI Both workflows cross-compile `chdr-static` and run a peak-heap-per-codec probe under QEMU (`qemu-system-arm`'s `mps2-an505` for Cortex-M33, `qemu-system-riscv32`'s `virt` for Hazard3/rv32imac) - neither is the real RP2350 SoC, only the core ISA/ABI and heap high-water numbers matter here. Measured baseline (2026-08-31, `CHDR_LOWRAM_TARGET=ON`, both cores): | codec | Cortex-M33 peak | Hazard3 peak | budget | |---------|-----------------|--------------|---------| | hd_zlib | 52,074 B | 52,074 B | 100,000 | | hd_zstd | 105,817 B | 105,825 B | 200,000 | | hd_lzma | 30,642 B | 30,642 B | 80,000 | | hd_huff | 21,839 B | 21,839 B | 60,000 | | cd_cdzl | 136,341 B | 136,341 B | 250,000 | | cd_cdzs | 243,768 B | 243,784 B | 400,000 | | cd_cdlz | 135,427 B | 135,427 B | 250,000 | All well inside the RP2350's 520KB SRAM even before accounting for `LOWRAM_TARGET`'s per-hunk-map savings on real (larger, more-hunks) discs - see each `check_budget.py` for how these are enforced and `.github/workflows/rv32-ram-budget.yml`'s comment for why `LOWRAM_TARGET` matters most on real full-size CHDs, not this workflow's tiny synthetic corpus. No RP2350 hardware in CI, so this can't prove `chd_open()`/ `chd_read()` work at runtime against real flash/SD storage - only that neither build regresses correctness or blows past budget on what CI *can* see. ## FLAC backend: micro-flac by default The benchmark here decodes FLAC through [micro-flac](https://github.com/esphome-libs/micro-flac) rather than dr_flac, fetched at a pinned commit unless `CHDR_MICROFLAC_SOURCE_DIR` points at a local one. `-DCHDR_FLAC_BACKEND=drflac` goes back. Output is byte-identical. Measured on this board across seven real discs, two runs each with a spread under 0.17%: **1.032x** overall, **1.12-1.14x** on two of them, and a worst-case largest-free-block of 51 KB against dr_flac's 47 KB. On an ESP32-S3 with I/O excluded it is 1.198x on a CD-FLAC hunk. libchdr's own default stays dr_flac - a desktop consumer vendoring `src/` should not have to fetch an Apache-2.0 C++ dependency. An MCU integrator is already cloning an SDK, so the trade is different here. ## Real-hardware benchmark (this directory) The CI workflows above run under QEMU and measure RAM only; as their note says, they cannot prove `chd_open()`/`chd_read()` work against real storage. `benchmark_main.c` closes that gap: it runs on an actual Waveshare RP2350-PiZero (RP2350B, Cortex-M33 @150MHz, 520KB SRAM, 16MB flash) and reads CHDs from the onboard microSD. Per file it reports wall time, decoded and compressed throughput, the share of time spent inside FatFs/SPI rather than decoding, per-hunk latency, peak heap, and an FNV-1a hash of the decoded output. The hash is the point of running on silicon rather than QEMU: it shows the decode is byte-identical to a desktop build, not merely that it completed. It does **not** reuse `../tangcore-bl616/chd/chd_fatfs.c`, which is the generic bridge to prefer for real integrations. This harness needs to time each `f_read`/`f_lseek` to separate storage cost from decode cost, so it carries its own instrumented copy of the same callbacks. Use `chd_fatfs.c` for anything that is not measuring I/O. Peak heap is polled via `mallinfo()` rather than `-Wl,--wrap=malloc`: the wrap symbols collide with `pico_malloc`'s own, and on rv32 wrapping produced unexplained corruption at real-content scale where polling did not. ### Wiring microSD is on SPI1 - SCK=GPIO30, MOSI=GPIO31, MISO=GPIO40, CS=GPIO43, with CS driven in software rather than by the SPI block (`hw_config.c`). GPIO 40 and 43 exist only on the RP2350B, which is why the SDK board header sets `PICO_RP2350A 0`. ### Building Needs the Pico SDK and carlk3's SD/FatFs library; neither is vendored. export PICO_SDK_PATH=/path/to/pico-sdk export SDFAT_PATH=/path/to/no-OS-FatFS-SD-SDIO-SPI-RPi-Pico cmake -S contrib/rp2350 -B build-rp2350 -DCMAKE_BUILD_TYPE=Release cmake --build build-rp2350 -j `-DLOWRAM_TARGET_VAL=0|1` (default 1), `-DBENCH_HUNK_CAP=N` (default 0 = every hunk) and `-DSD_BAUD_HZ=N` (default 25000000). The SD clock is worth more than any codec-side tuning found on this board. Measured over three I/O-heavy files, 12 MHz -> 25 MHz is 1.219x aggregate (kinst2 1.378x, Insanity 1.261x, Shadowrun 1.169x) with byte-identical output; the gain tracks each file's I/O share, and io% falls roughly in proportion to the clock. 25 MHz is the SD Default Speed rate and SPI mode has no open-drain derating, so it is in spec - but MCU SPI blocks and board routing do not always meet timing there, so drop it if a card misbehaves. ### Flashing Hold BOOT while plugging in USB, then copy the UF2 onto the `RP2350` volume. After the first flash the SDK's reset interface is present, so a 1200-baud touch re-enters BOOTSEL without the button: python3 -c "import serial,time; s=serial.Serial('/dev/ttyACM0',1200); \ s.dtr=False; time.sleep(0.3); s.close()" cp build-rp2350/rp2350-bench.uf2 /media/$USER/RP2350/ Two gotchas on this board: it has two USB-C ports and only one is the RP2350's device port - the other is a PIO-USB host port and fails to enumerate (`error -71`). And `pico_stdio_usb` stays silent until the host asserts DTR, so a monitor that leaves DTR low sees nothing.