Files
Claude c4e04dd749 The second code segment (flash 0xC8000..0xCFFFF), ported from JIANT BYTE (e807224d): 31.8 KB out of the app slot
FM's reserve 0xC8000..0xD7FFF (storage.c FL_SAMPLES, written by nothing) now holds, in its first half, cold main-loop
code run in place through the plain XIP window as the Bluetooth segment is. 0xD0000.. stays reserved; JAM's takes at
0xDA000 / 0xDB000 untouched.

- Build: app.ld C2SEG (XIP 0x020C4000), section .c2 (c2_head first, then .c2.text / .c2.rodata); core.h C2_FN / C2_IN /
  C2_RO; src/seg2.c (header, c2_want, c2_seg_ok). tools/build.py writes build/c2.bin (JBC2, length, CRC-32) and patches
  c2_want as bt_want; c2_check: nothing an interrupt, core 1 or RAM code reaches is in it, every call in from the slot
  comes from a hand-checked gate (C2_GATES), no segment address stored as data.
- Moved: the editor protocol's commands and pushes (ed_command, ed_sync, their helpers, FM's ed_fm6_handle; INFO, the
  backup and the project library stay in the slot), GEN's generators (gen_make, gen_parts, gen_pitched, gcpu_fit,
  gen_sound, gen_kit, gen_mix, gl_write, gsong, gplan, grich, gls_prep, gen_arp, trackgen_run ...). FM differs from
  BYTE: gen_make / gen_parts are slot code here (BYTE: Bluetooth segment), so they moved too, gen_go a gate;
  gen_evolve and gx_comps stay in the slot (gen_post calls them); gen_rs_apply was already in the Bluetooth segment;
  no dice_seq. Slot: 583660 -> 551828 B (676 -> 32508 B free). Segment: 31660 of 32768 B (1108 B free).
- Sound: every pinned / recorded audio function and table at its address (layout unchanged, no repin); audio flash
  code identical but for immediates; RAM code +36 B (the flash driver's range check, FL_RANGE_OK). The merged
  constants' floor (build/layout_cst.ld) is the first pool's start as laid out (FM's pools have UI constants before
  the sound's first). seq_steps always inline (no out-of-line copy for the segment's calls).
- Runtime: bt_hw_seg() checks both segments once at power-on; missing / torn / another build's: FLASH PART MISSING /
  REINSTALL at power-on and on GEN; the editor answers INFO ('X' 2), the backup and the project library only.
- Package / loader: fourth UFW entry c2.bin (type 201), checked before anything is erased, all of [0xC8000, 0xD0000)
  written (0xFF past the code), never 0xD0000 and up. FL_STORE_OK / FL_RANGE_OK leave the segment out (static asserts).
- Tests: ldr_test (c2 written, rest erased, damaged c2 refused, 0xD0000.. untouched), editor_test (segments missing),
  storage map assert, layout packing test. Manuals and EDITOR_PROTOCOL: FLASH PART MISSING.

Needs a hardware test on the FM-1 before any release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-12-04 22:33:34 +00:00

120 lines
4.6 KiB
C

/* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
/* Felucca update loader (device side). Runs from RAM, started by the SPL from
* an update record, with interrupts off: USB is polled from the main loop.
* It is a USB-MIDI device "Felucca Update" (1209:0002) that answers the
* M-UPGRADE update protocol as "ota-FM-1_900" (hosts look for "ota-"), and
* writes the app area only (ldr_core.c). The UBOOT soft key works here too.
* Single compilation unit. */
#include <stdint.h>
#define FELUCCA_LOADER 1
#define FELUCCA_CDC 0
#define FELUCCA_OTA 1
#define FELUCCA_OTA_DRYRUN 0
#define FELUCCA_USB_PID 0x0002
#define FELUCCA_ID "ota-FM-1_900"
#define RING_PUBLISH() __asm__ volatile("" ::: "memory")
#include "fm1_time.h"
#include "fm1_sys.h"
/* the loader may write the app area and erase update records; never the head, nor Felucca's store
* [0x97000, 0xE0000) (user data, never a record: ldr_core.c), but (seg2) the second code segment inside it,
* [0xC8000, 0xD0000) (ldr_core.c LDR_C2_*), which only the loader writes */
#define FL_RANGE_OK(off, n) (FL_IN(off, n, 0x4000u, 0x97000u) || FL_IN(off, n, 0xC8000u, 0xD0000u) || \
FL_IN(off, n, 0xE0000u, 0xFC000u))
#include "fm1_flash.h"
/* (seg2) the second code segment whole, and nothing of the store around it (the user wavetables' 0xD0000 up) */
_Static_assert(FL_RANGE_OK(FL_SEG2_LO, FL_SEG2_HI - FL_SEG2_LO) && !FL_RANGE_OK(FL_SEG2_HI, 1u) &&
!FL_RANGE_OK(FL_SEG2_LO - 1u, 1u), "the loader's window: the second code segment, not the store");
#include "../src/libc.c"
#include "../src/usb.c"
static uint32_t ldr_ms(void) /* monotonic ms; survives the TIMER4 wrap (178.9 s) */
{
static uint32_t last, acc, ms;
uint32_t now = fm1_ticks();
acc += now - last;
last = now;
ms += acc / (1000u * FM1_TICKS_PER_US);
acc %= 1000u * FM1_TICKS_PER_US;
return ms;
}
static void ldr_poll(void) /* USB at ~2 kHz, watchdog */
{
static uint32_t last;
uint32_t now = fm1_ticks();
fm1_wdt_feed();
if (now - last >= 500u * FM1_TICKS_PER_US) {
last = now;
usb_poll();
}
}
/* ---- ota.c hooks (the SysEx frame hooks are in usb.c) ---- */
static uint32_t ota_now_ms(void) { return ldr_ms(); }
static void ota_idle(void) { ldr_poll(); }
/* step-1 hooks of ota.c: not used by the loader */
static int ota_erase(uint32_t off) { (void)off; return -1; }
static int ota_prog(uint32_t off, const void *p, uint32_t n) { (void)off; (void)p; (void)n; return -1; }
static int ota_fread(uint32_t off, void *p, uint32_t n) { return fl_read_ram(off, p, n); }
static void ota_show(uint32_t step, int32_t code) { (void)step; (void)code; }
static void ota_commit(const uint8_t *parm) { (void)parm; }
#include "../src/ota.c"
/* ---- ldr_core.c hooks: interrupts are off for the whole loader ---- */
static int ldr_fread(uint32_t off, void *p, uint32_t n) { return fl_read_ram(off, p, n); }
static int ldr_erase(uint32_t off)
{
uint32_t took;
return fl_erase4k_ram(off, &took);
}
static int ldr_prog(uint32_t off, const void *p, uint32_t n)
{
uint32_t took;
return fl_prog_ram(off, p, n, &took);
}
static void ldr_record_clear(void)
{
fm1_updata_parm_clear(); /* CRC 0: the SPL ignores it on a warm reset */
}
static void ldr_progress(uint32_t done, uint32_t total) { (void)done; (void)total; ldr_poll(); }
#include "ldr_core.c"
extern uint32_t _bss_start[], _bss_end[];
void ldr_main(void)
{
fm1_wdt_arm(0x0D); /* ~8 s, fed by ldr_poll */
fm1_time_init();
usb_start();
for (;;) {
ldr_poll();
usb_retry(ldr_ms());
if (usb.uboot_req) { /* soft key: mask-ROM UBOOT, as in Felucca */
usb_detach();
fm1_delay_ms(30);
fm1_enter_uboot();
}
ota_service(); /* handshake */
if (usb.ota_req) {
usb.ota_req = 0;
if (ldr_session() == 0) {
fm1_delay_ms(200); /* the "success" reply leaves */
usb_detach();
fm1_delay_ms(30);
fm1_core_reset(); /* the SPL boots the new app */
}
}
}
}
void ldr_cstart(void)
{
uint32_t *p;
ldr_record_clear(); /* first thing: a crash below cannot loop through a
* warm reset; resume after power loss uses the flash record */
for (p = _bss_start; p < _bss_end; p++)
*p = 0;
ldr_main();
}