/* SPDX-License-Identifier: GPL-3.0-only * Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */ /* The real audio ISR with a simulated DMA and clock: overload must free work * within one block without cutting another part's lead or bass. */ #define main hostsim_main #include "hostsim.c" #undef main #define FM1_AUDIO_HALF 0x80u #define FM1_TICKS_PER_US 1u static uint32_t host_clock, host_us, host_half_reads, host_dma_advanced, host_acks, host_nest; static uint8_t host_pending; static volatile uint32_t t5_nested_ticks; /* audio.c's: TIMER5 nested in the render (main.c) */ static uint32_t fm1_ticks(void) { t5_nested_ticks = host_nest; host_clock += host_us; return host_clock; } static uint8_t fm1_audio_pending(void) { return host_pending; } static void fm1_audio_ack_aux(uint8_t p) { (void)p; } static uint32_t fm1_audio_free_half(void) { return host_dma_advanced && host_half_reads++ > 0u; } static void fm1_audio_ack_half(void) { host_acks++; } static void fm1_audio_init(int32_t *b, uint32_t n, void (*isr)(void), uint32_t p) { (void)b; (void)n; (void)isr; (void)p; } void isr_alnk0(void) {} #define FELUCCA_UAC 1 /* audio.c taps USB audio: here into host_tap */ static int32_t host_tap[2 * 64]; static uint32_t host_tap_n; static void uac_tap(const int32_t *out, uint32_t n) { memcpy(host_tap, out, 8u * n); host_tap_n = n; } static void uac_render_start(void) {} #include "../firmware/src/audio.c" enum { FM1_ADC_BATT = 3, FM1_ADC_MASTER = 4 }; static int32_t host_pot = 1023; /* the MASTER pot as the ADC reads it (-1: timeout) */ static int32_t fm1_adc_read(uint32_t ch) { return ch == FM1_ADC_MASTER ? host_pot : 620; } #include "../firmware/src/master.c" static int bad; static void check(const char *what, int ok) { printf("audio: %-74s %s\n", what, ok ? "ok" : "FAIL"); bad += !ok; } static void fresh(void) { uint32_t p; memset(trk, 0, sizeof trk); memset(&song, 0, sizeof song); memset(&chain, 0, sizeof chain); host_tracks_init(); for (p = 0; p < NPART; p++) host_preset(&trk[p], 0, 0); memset(&felucca_dbg, 0, sizeof felucca_dbg); audio_cpu_rem = audio_halves = audio_max_us = 0; shed_req = 0; shed_count = 0; shed_over = 0; host_clock = host_us = host_half_reads = host_dma_advanced = host_acks = host_nest = 0; host_pending = FM1_AUDIO_HALF; transport_req = panic_req = 0; } static voice_t *held(track_t *t, uint32_t note) { uint32_t i; for (i = 0; i < NVOICE; i++) if (t->v[i].active && t->v[i].note == note) return &t->v[i]; return 0; } static void overload(void) { voice_t *lead, *bass, *extra; uint32_t mode, i; int32_t out[2 * CTL]; fresh(); trk[0].p[P_VOICE] = V_MONO; trk_note_on(&trk[0], 40, 100); trk[1].p[P_VOICE] = V_POLY; trk_note_on(&trk[1], 48, 100); trk_note_on(&trk[1], 60, 100); lead = held(&trk[0], 40); bass = held(&trk[1], 48); extra = held(&trk[1], 60); shed_voice(); check("overload keeps each part's MONO lead and lowest POLY note", lead && bass && extra && lead->gate && bass->gate && extra->stage == 4u && shed_count == 1u); mix_block(out, CTL); check("overload frees its victim within one control block", extra && !extra->active && voices_busy() == 2u); shed_voice(); check("a lead alone on each part stays protected", lead->gate && bass->gate && shed_count == 1u); fresh(); host_preset(&trk[0], ENGI_DRUM, 0); trk_note_on(&trk[0], 36, 100); trk_note_on(&trk[0], 38, 100); lead = held(&trk[0], 36); extra = held(&trk[0], 38); shed_voice(); mix_block(out, CTL); check("overload frees a held one-shot drum without waiting for its decay", lead && extra && lead->active && !extra->active && voices_busy() == 1u); fresh(); trk[0].p[P_VOICE] = V_UNISON; trk_note_on(&trk[0], 55, 100); shed_voice(); mix_block(out, CTL); check("UNISON loses an extra voice and retains its lead", trk[0].v[0].active && trk[0].v[0].gate && voices_busy() == NVOICE - 1u); for (mode = V_MONO; mode <= V_LEGATO; mode++) { fresh(); trk[0].p[P_VOICE] = V_POLY; for (i = 0; i < NVOICE; i++) trk_note_on(&trk[0], 48u + 2u * i, 100); trk[0].p[P_VOICE] = (int16_t)mode; shed_voice(); mix_block(out, CTL); check("overload can reclaim stale POLY voices after a MONO / LEGATO change", shed_count == 1u && trk[0].v[0].gate && voices_busy() == NVOICE - 1u); } fresh(); trk[0].p[P_VOICE] = V_POLY; trk_note_on(&trk[0], 60, 100); trk_note_on(&trk[0], 64, 100); lead = held(&trk[0], 60); extra = held(&trk[0], 64); trk_note_off(&trk[0], 60); trk_note_off(&trk[0], 64); lead->env = 10000; extra->env = 10; shed_voice(); check("released voices shed the quieter tail first", extra->stage == 4u && lead->stage != 4u); } static void dma(void) { uint32_t i, expected; fresh(); host_pending = 0; fm1_alnk0_irq(); check("an auxiliary interrupt does not render a DMA half", !host_acks && !audio_halves && !felucca_dbg.in_audio); host_pending = FM1_AUDIO_HALF; host_us = 5000; fm1_alnk0_irq(); check("one slow half records elapsed load but does not shed yet", host_acks == 1u && !shed_req && felucca_dbg.last_us == 5000u && !felucca_dbg.late); fm1_alnk0_irq(); check("a second slow half in a row requests shedding", host_acks == 2u && shed_req); shed_req = 0; host_half_reads = 0; host_dma_advanced = 1; host_us = 100; fm1_alnk0_irq(); check("DMA advancing during a render is counted once", host_acks == 3u && felucca_dbg.late == 1u && !felucca_dbg.in_audio && !shed_req); fresh(); expected = HALF_FRAMES * 1000000u / FS; /* the half's deadline in us */ host_us = expected * 90u / 100u; host_nest = expected * 30u / 100u; /* the render 60 %, TIMER5 nested 30 % */ fm1_alnk0_irq(); fm1_alnk0_irq(); check("TIMER5 nested in the render counts toward the deadline", shed_req && felucca_dbg.last_us == host_us - host_nest); fresh(); host_us = 5000; fm1_alnk0_irq(); host_us = 100; fm1_alnk0_irq(); host_us = 5000; fm1_alnk0_irq(); check("slow halves that are not consecutive do not shed", !shed_req); fresh(); host_us = 580; for (i = 0; i < 512u; i++) fm1_alnk0_irq(); expected = host_us * 256u / (HALF_FRAMES * 1000000u / FS); check("steady CPU load converges without integer smoothing bias", song.cpu_q8 >= expected - 1u && song.cpu_q8 <= expected); } /* MENU > USB LEVEL (fx.c fx_usb_fixed, audio.c audio_block): MASTER (default) as before, USB = the DAC's signal and * follows the knob; FIXED: USB at the full level whatever MASTER is (MASTER 0: USB still plays, the DAC is silent), * the DAC exactly USB scaled by MASTER */ static double block_rms(const int32_t *b, uint32_t n) { double s = 0; uint32_t i; for (i = 0; i < 2u * n; i++) s += (double)b[i] * b[i]; return sqrt(s / (2.0 * n)); } static double usb_run(uint32_t fixed, uint32_t master, int dac_is_tap_scaled_ok[1], double *dac_rms) { int32_t out[2 * 32]; uint32_t b, i; double usb = 0, dac = 0; fresh(); lim_env = LIM_T; dc_l = dc_r = dce_l = dce_r = 0; /* (the master's state as at power-on: runs compare) */ trk[0].p[P_VOICE] = V_POLY; trk[0].p[P_DIST] = trk[0].p[P_CHOR] = trk[0].p[P_DLY] = trk[0].p[P_REV] = 0; /* (no tails between runs) */ trk_note_on(&trk[0], 48, 110); trk_note_on(&trk[0], 55, 110); trk_note_on(&trk[0], 64, 110); fx_usb_fixed = (uint8_t)fixed; song.master_q12 = master; dac_is_tap_scaled_ok[0] = 1; for (b = 0; b < 400u; b++) { /* ~0.3 s */ audio_block(out, 32); for (i = 0; i < 64u; i++) { int32_t want = fixed ? ((host_tap[i] * (int32_t)master) >> 12) << OUT_SHIFT : host_tap[i] << OUT_SHIFT; if (out[i] != want) dac_is_tap_scaled_ok[0] = 0; } if (b >= 100u) { usb += block_rms(host_tap, 32); dac += block_rms(out, 32) / (1 << OUT_SHIFT); } } fx_usb_fixed = 0; *dac_rms = dac / 300.0; return usb / 300.0; } static void usb_level(void) { int ok_full, ok_quiet, ok_off, ok_fixed, ok_fixed0; double d_full, d_quiet, d_off, d_fixed, d_fixed0; double u_full = usb_run(0, 4096, &ok_full, &d_full), u_quiet = usb_run(0, 1024, &ok_quiet, &d_quiet); double u_off = usb_run(0, 0, &ok_off, &d_off), u_fixed = usb_run(1, 1024, &ok_fixed, &d_fixed); double u_fixed0 = usb_run(1, 0, &ok_fixed0, &d_fixed0); printf("audio: USB / DAC rms: MASTER full %.0f / %.0f, 1/4 %.0f / %.0f, 0 %.0f / %.0f; FIXED 1/4 %.0f / %.0f, 0 %.0f / %.0f\n", u_full, d_full, u_quiet, d_quiet, u_off, d_off, u_fixed, d_fixed, u_fixed0, d_fixed0); check("USB LEVEL MASTER (default): USB is the DAC's signal and follows the knob (0: silent)", ok_full && ok_quiet && ok_off && u_full > 1000.0 && u_quiet < u_full * 0.5 && u_off == 0.0); check("USB LEVEL FIXED: USB at the full level whatever MASTER is (within 0.1 dB), MASTER 0 too", fabs(20.0 * log10(u_fixed / u_full)) < 0.1 && fabs(20.0 * log10(u_fixed0 / u_full)) < 0.1); check("USB LEVEL FIXED: the DAC gets exactly USB scaled by MASTER (MASTER 0: silent, 1/4: -12 dB)", ok_fixed && ok_fixed0 && d_fixed0 == 0.0 && fabs(20.0 * log10(d_fixed / u_fixed) + 12.04) < 0.2); } /* #137: keys pressed under the splash. main.c: master_boot before audio_init (the pot's level from the first block), * kb_boot_hold until the splash ends (seq.c keyboard_block: the keys sound nothing; one held across stays silent until * pressed again); the splash polls the pot (master_poll). boot 0: as before the fix */ static void boot_fresh(void) { fresh(); lim_env = LIM_T; dc_l = dc_r = dce_l = dce_r = 0; memset(kb_note, 0, sizeof kb_note); kb_prev = 0; kb_layer = 0; trk[0].p[P_VOICE] = V_POLY; trk[0].p[P_DIST] = trk[0].p[P_CHOR] = trk[0].p[P_DLY] = trk[0].p[P_REV] = 0; } static double boot_run(int32_t pot, int boot, uint32_t *splash_peak, uint32_t *first_master, uint32_t *busy) { int32_t out[2 * 32]; uint32_t b, i, peak = 0; double r = 0; boot_fresh(); song.master_q12 = 2048; /* felucca_init */ master_knob = 512 * 16; host_pot = pot; if (boot) { master_boot(); kb_boot_hold = 1; } *first_master = song.master_q12; for (b = 0; b < 600u; b++) { /* the splash, ~0.43 s: keys hammered, one held at its end */ fm1_in.notes = b >= 580u ? 1u << 12 : (b / 40u) & 1u ? (1u << 7) | (1u << 11) : 1u << 4; if (b % 14u == 0u && boot) master_poll(); audio_block(out, 32); for (i = 0; i < 64u; i++) if ((uint32_t)abs(out[i]) > peak) peak = (uint32_t)abs(out[i]); } *splash_peak = peak; kb_boot_hold = 0; /* the UI */ *busy = 0; for (b = 0; b < 400u; b++) { /* the held key (silent), let go; then a chord pressed */ fm1_in.notes = b < 40u ? 1u << 12 : b < 60u ? 0u : (1u << 0) | (1u << 4) | (1u << 7); audio_block(out, 32); if (b >= 60u) r += block_rms(out, 32); if (b == 39u) *busy = voices_busy(); } fm1_in.notes = 0; return r / 340.0; } static void boot_keys(void) { uint32_t peak, peak0, peak_old, m, m0, m_old, busy, busy0, busy_old, ref_m, b; double quiet, silent, old, ref = 0; int32_t out[2 * 32]; quiet = boot_run(300, 1, &peak, &m, &busy); silent = boot_run(0, 1, &peak0, &m0, &busy0); old = boot_run(300, 0, &peak_old, &m_old, &busy_old); boot_fresh(); /* the reference: the chord, MASTER at the pot's level by hand */ song.master_q12 = ref_m = master_of_pot(300); for (b = 0; b < 340u; b++) { fm1_in.notes = (1u << 0) | (1u << 4) | (1u << 7); audio_block(out, 32); ref += block_rms(out, 32); } fm1_in.notes = 0; ref /= 340.0; printf("audio: boot, pot 300 (MASTER %u): splash peak %u, then rms %.0f (by hand %.0f); before the fix: " "MASTER %u, splash peak %u, then rms %.0f\n", m, peak, quiet, ref, m_old, peak_old, old); check("#137 boot: MASTER is the pot's level before the first block (pot 300, pot 0)", m == ref_m && m0 == 0u && m_old == 2048u); check("#137 boot: keys hammered under the splash sound nothing (before the fix: they did)", peak == 0u && peak0 == 0u && peak_old > 0u); check("#137 boot: a key held across the splash's end stays silent until pressed again", busy == 0u && busy0 == 0u && busy_old > 0u); check("#137 boot: after the splash the keys play at the pot's level, as set by hand (pot 0: silent)", ref > 100.0 && fabs(quiet - ref) < 1e-9 && silent == 0.0); } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); usb_level(); boot_keys(); overload(); dma(); printf(bad ? "audio: %d FAILED\n" : "audio: all passed\n", bad); return bad != 0; }