// helloSample.c - Phase 2.4 docram demo.  Stages a small sine-wave
// sample from caller RAM into the Ensoniq DOC's audio RAM via
// iigsLoadDocSample(), then triggers playback via iigsPlayDocSample().
//
// Exercises the full WriteRamBlock -> FFStartSound path that was
// previously unwrapped.  The marker store at $70 confirms control
// returned from WriteRamBlock cleanly (the toolbox call has no error
// return; a hang or stack imbalance would prevent the store).
//
// Build with: bash demos/build.sh helloSample
// Run with:   bash scripts/runViaFinder.sh demos/helloSample.omf \
//                  --check 0x70=0x99
//
// Audio output: a brief sine-wave tone on generator 0.  Headless runs
// will only verify the marker; an interactive run will hear the tone.

#include "iigs/sound.h"

// 256-byte (one DOC RAM page) signed-8-bit sine wave at full
// amplitude.  Pre-computed at build time to keep the demo standalone
// (no soft-float dependency just for sin()).  Each entry is
// sin(2*pi*i/256) * 127, rounded to the nearest signed-byte.
static const signed char gSineWave[256] = {
       0,    3,    6,    9,   12,   15,   18,   21,   24,   28,
      31,   34,   37,   40,   43,   46,   48,   51,   54,   57,
      60,   63,   65,   68,   71,   73,   76,   78,   81,   83,
      85,   88,   90,   92,   94,   96,   98,  100,  102,  104,
     106,  107,  109,  111,  112,  113,  115,  116,  117,  118,
     120,  121,  122,  122,  123,  124,  125,  125,  126,  126,
     126,  127,  127,  127,  127,  127,  127,  127,  126,  126,
     126,  125,  125,  124,  123,  122,  122,  121,  120,  118,
     117,  116,  115,  113,  112,  111,  109,  107,  106,  104,
     102,  100,   98,   96,   94,   92,   90,   88,   85,   83,
      81,   78,   76,   73,   71,   68,   65,   63,   60,   57,
      54,   51,   48,   46,   43,   40,   37,   34,   31,   28,
      24,   21,   18,   15,   12,    9,    6,    3,
       0,   -3,   -6,   -9,  -12,  -15,  -18,  -21,  -24,  -28,
     -31,  -34,  -37,  -40,  -43,  -46,  -48,  -51,  -54,  -57,
     -60,  -63,  -65,  -68,  -71,  -73,  -76,  -78,  -81,  -83,
     -85,  -88,  -90,  -92,  -94,  -96,  -98, -100, -102, -104,
    -106, -107, -109, -111, -112, -113, -115, -116, -117, -118,
    -120, -121, -122, -122, -123, -124, -125, -125, -126, -126,
    -126, -127, -127, -127, -127, -127, -127, -127, -126, -126,
    -126, -125, -125, -124, -123, -122, -122, -121, -120, -118,
    -117, -116, -115, -113, -112, -111, -109, -107, -106, -104,
    -102, -100,  -98,  -96,  -94,  -92,  -90,  -88,  -85,  -83,
     -81,  -78,  -76,  -73,  -71,  -68,  -65,  -63,  -60,  -57,
     -54,  -51,  -48,  -46,  -43,  -40,  -37,  -34,  -31,  -28,
     -24,  -21,  -18,  -15,  -12,   -9,   -6,   -3
};


int main(void) {
    // SoundManager comes up via Finder's app-launch chain; the
    // tool-reference-count idempotent call still re-arms it just in
    // case (and is required for bare-metal-run scenarios where Finder
    // is bypassed).
    iigsSoundProbeInit();

    // Stage the 256-byte (1 page) sine wave to DOC RAM at offset 0.
    iigsLoadDocSample(gSineWave, sizeof(gSineWave), 0);

    // Marker AFTER WriteRamBlock returns - proves the toolbox call
    // didn't hang or imbalance the stack.  The audio path past this
    // point is verified by ear (or by reading $E1:8000 DOC registers
    // in a more thorough probe; out of scope for this smoke).
    *(volatile unsigned char *)0x70 = 0x99;

    // Play on generator 0 at unit pitch (freqOffset = 0x0100 is the
    // natural sample rate for a 1-page wave), full volume.
    iigsPlayDocSample((void *)0, 1, 0x0100, 0xFF, 0);

    // Linger long enough for the tone to play + the headless harness
    // to snapshot the marker.
    for (volatile unsigned long s = 0; s < 600000UL; s++) { }

    iigsSoundStop(0xFF);
    return 0;
}
