UV-K5 V3 emulator: QEMU machine for the PY32F071

Adds a QEMU machine for the Puya PY32F071 (Cortex-M0+) so Quansheng UV-K5 V3
firmware can run on a PC. The firmware boots to its main loop in about five
seconds and the LCD contents are readable.

Register layouts come from the vendor CMSIS header shipped with the firmware
rather than guesswork. Modelled: RCC, GPIO, ADC, both SPI controllers, DMA1 and
the PY25Q16 flash; everything else answers through a logging catch-all, which is
how the next thing worth modelling gets identified.

Seven things had to be right before it would boot, each found by watching where
the firmware stopped: flash aliased at the application offset, clock ready bits,
self-clearing ADC calibration, SPI transfer flags, DMA-driven flash reads,
SysTick poll acceleration, and the bit-banged transceiver bus idling low.

SysTick needs explanation. SYSTICK_DelayUs polls the counter and accumulates
differences; under emulation a register read costs far more relative to guest
time, so a measured 120 ms delay would have taken about 7.7 hours. Lowering the
clock does not help because the bottleneck is loop iterations, not counter speed.
Reporting a value that runs ahead of the real counter does, via a new poll-boost
property on SysTick. Guest time therefore runs fast during delays: fine for
exercising menus and control flow, wrong for judging signal timing.

Also includes the host build of the CW timing chain (harness, stubs, shim,
tests), which compiles app/cwkeyer.c and app/cwmacro.c unmodified against stub
drivers with a virtual clock and scripted paddle input.

Known gap: keypad rows reach the firmware's scan and KEYBOARD_Poll returns the
right key code, but the UI does not react yet.

Not modelled, and not intended to be: radio behaviour. The transceiver chip has
no public datasheet, so keying envelopes and emissions need real hardware.
This commit is contained in:
mckero committed 2026-08-27 14:59:21 +01:00
commit c0a09827ed
53 files changed
+5068

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
"""Build the 2 MB SPI flash image the emulator boots from.
Starts from erased flash (0xFF) and drops the calibration dump at physical
0x010000, which is where driver/eeprom_compat.c maps the 512-byte calibration
block. Without it the firmware takes error branches in the frequency and power
paths, so the emulated radio would not represent a real one.
The image itself is not committed: it is 2 MB and fully derived from
assets/calibration.bin.
Usage: make_flash.py [--calibration FILE] [--out FILE]
"""
import argparse
import pathlib
import sys
FLASH_SIZE = 2 * 1024 * 1024
CALIBRATION_ADDR = 0x010000
CALIBRATION_SIZE = 512
HERE = pathlib.Path(__file__).resolve().parent
ASSETS = HERE.parent / "assets"
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--calibration", type=pathlib.Path,
default=ASSETS / "calibration.bin")
ap.add_argument("--out", type=pathlib.Path, default=ASSETS / "flash.img")
args = ap.parse_args()
if not args.calibration.is_file():
raise SystemExit(f"calibration dump not found: {args.calibration}")
cal = args.calibration.read_bytes()
if len(cal) != CALIBRATION_SIZE:
print(f"warning: calibration is {len(cal)} bytes, expected {CALIBRATION_SIZE}",
file=sys.stderr)
image = bytearray(b"\xff" * FLASH_SIZE)
image[CALIBRATION_ADDR:CALIBRATION_ADDR + len(cal)] = cal
args.out.write_bytes(image)
print(f"wrote {args.out} ({len(image)} bytes)")
print(f" calibration at {CALIBRATION_ADDR:#08x}: "
+ " ".join(f"{b:02X}" for b in image[CALIBRATION_ADDR:CALIBRATION_ADDR + 8]))
return 0
if __name__ == "__main__":
sys.exit(main())