/* Chassis polysynth framework Copyright 2024 Gordon JC Pearce Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ // contains the emulation of the digital bits #include "digital.hpp" #include #include #include "voice.hpp" using namespace Digital; bool Voice::isFree() { return ff10 == false; } void Voice::calcPitch(Synth &s) { uint32_t bc, ea, a; // 03ad ea = 0x1818; // add in tuning value from ff61, not implemented // 03ba bc = s.ff51; // computed pitch LFO if (s.ff4a & 0x02) { ea -= bc; } else { ea += bc; } // 03c6 // add in bender from ff68 // 03d2 s.ff6f = ea; ea = ff71; a = note; // 03e3 bc = a << 8; // 03e6 a = 0; // set from porta coefficient ff7d if (a != 0) goto h03f5; // 3eb ea = bc; h03ec: ff71 = ea; // STEAX (DE++) // we're not looping so we can ignore until // 03ee inrw ff0f voice counter // 03f0 eqiw ff0f, 06 // 03f3 jr 03e0 goto h0407; h03f5: if (ea == bc) goto h03ec; // DNE EA, BC; JR 03EC store value if (!(ea > bc)) goto h0401; // DGT EA, BC; JR 0401 ea -= a; if (!(ea > bc)) ea = bc; goto h03ec; h0401: ea += a; if (!(ea < bc)) ea = bc; goto h03ec; h0407: // this outputs the Sub Osc CV // ignore until // 0413 mviw ff0f, 0 reset voice counter s.ff34 = 1; // unsure what this is used for // 0419 ea = ff71; // pitch + fraction per voice bc = s.ff6f; // tune + lfo + bend ea += bc; // 0424 s.ff6e = ea & 0xff; // MOV A, EAL; STAW 006e a = ea >> 8; // mov a, EAH // 0428 if (a <= 0x2f) goto h04a5; // GTI A,$2F; JRE $04A5 if (a >= 0x97) goto h04ac; // LTI A,$97; JRE $04AC a -= 0x30; h0432: // printf("setting omega for note %d \n", a); omega = ((s.pitchCV[a + 1] - s.pitchCV[a]) * (s.ff6e / 256.0)) + s.pitchCV[a]; // 0432 onwards calculates the address for the CV // table at E60 and stacks it // 043a onwards fetches the value from the divider // table and computes a linear interpolation with the next one up // using the fractional value stored in ff6e // 045a onwards decides which divider to program // 0471 unstacks the CV table address and calculates a linear // interpolation of this and the next CV value using ff6e // 048b onwards sends it to the correct DAC // 0496 onwards works out which vocie to do next and loops // 04a3 goto h04d5; h04a5: // pitch too low s.ff6e = 0; a = 0; goto h0432; h04ac: // pitch too high s.ff6e = 0; a = 0x66; goto h0432; // 04b3 programs the dividers somehow // ignore until 04d5 h04d5: return; } void Voice::on(uint32_t key, bool reset = 0) { // what's with the crazy private variables and all the gotos with crazy labels? // this code emulates the 78C11 code directly (probably inefficiently) // to allow for documenting what the variables actually do // they're really bitfields holding a bit for each voice // this current implementation doesn't reset the voice (void)reset; // printf("called with key=%d\n", note); ff10 = true; // note held from keyboard ff07 = true; // attack phase if (note == key) goto h0144; note = key; if (ff11) goto h013e; h0132: if (ff33) goto h0149; // sustained ff33 = false; goto h0149; h013e: ff00 = true; // in a real one, voice counter needs programmed goto h0149; h0144: if (!ff11) goto h0132; // unsure, copied from ff10 at start of mainloop h0149: // this is in the wrong place really but is the equivalent of programming the counter // and VCO ramp DAC // omega = (261.63 * powf(2, (note - 60) / 12.0f)) / 48000.0f; // printf("note, key = %d, %d\n", note, key); return; } void Voice::off() { bool sustain = false; ff10 = false; if (!sustain) { // dummy sustain ff33 = false; } } void Voice::envelope(Synth &s) { uint16_t bc, ea = env; ff11 = ff10; // 0509 if (!ff11) goto h0538; // 050e if (!ff33) goto h0563; // 0513 if (!ff07) goto h051e; h0517: ff07 = false; h051e: bc = s.patchRam.env_s << 7; // half scale if (ea < bc) ea = bc; ea -= bc; bc = ea; ea = (ea * decayTable[s.patchRam.env_d]) >> 16; ea += s.patchRam.env_s << 7; // printf("returning from decay phase\n"); goto h0590; h0538: // printf("got to 0x0538\n"); if (!ff07) goto h054a; // note on? if not skip ahead // 053c if (ff08) goto h0517; ff07 = false; h054a: // printf("release phase\n"); ff33 = false; ff08 = false; bc = ea; ea = (ea * decayTable[s.patchRam.env_r]) >> 16; // printf("returning from release phase\n"); goto h0590; h0563: // printf("attack phase\n"); ff08 = false; ea += attackTable[s.patchRam.env_a]; if (ea & 0xc000) { ea = 0x3fff; ff33 = true; ff08 = true; } h0590: env = ea; // printf("%04x %d %d %d %d %d \n", ea, ff07, ff08, ff10, ff11, ff33); } void Synth::lfoDelay() { // compute LFO delay uint16_t a, bc, d, ea, tos; // 030d if (!keyon /* ff11 */) goto h036b; // skip ahead if no notes are pressed // 0312 if (!(ff1e & 0x08)) goto h0323; // a note is running, don't reset // 0315 no note is running, reset it all bc = 0; ff56 = 0; // delay envelope ff5a = 0; // holdoff timer ff1e &= 0xf1; // mask bits in flag byte h0323: if (!(ff1e & 0x02)) goto h0370; // compute delay envelope if (!(ff1e & 0x04)) goto h0388; // compute holdoff timer // 032b bc |= 0xff00; // initial scaling value? // printf("0323 "); h032d: tos = bc; // push bc // 032e a = lfoDepthTable[patchRam.vcoLfo]; // MUL B; MOV A, EAH a = (a * (bc >> 8)) >> 8; // 0333 ADDNCW $0064; MVI A, $FF a += ff64; if (a > 0xff) a = 0xff; // 0338 sets up HL to store computed pitch LFO output // 33b bc = ff4d; // current LFO output // printf("-----------------------%02x %04x\n", a, bc); // 033f ea = (bc & 0xff) * a; // MUL C d = a; // MOV D,A a = (ea >> 8); // MOV A,EAH bc &= 0xff00; bc |= a; // MOV C,A a = d; // MOV A,D // 0345 ea = (bc >> 8) * a; // MUL B ea += (bc & 0xff); // EADD EA, C // 0349 ea >>= 3; // divide by eight // 034f ff51 = ea; // save scaled pitch LFO bc = tos; // pop BC, contains scaling amount a = patchRam.vcfLfo << 1; // amount is doubled and stored at ff48 // 0354 ea = (bc >> 8) * a; // MUL B a = ea >> 8; // MOV A, EAH bc = ff4d; // current LFO output // 035b ea = (bc & 0xff) * a; // MUL C d = a; // MOV D,A a = ea >> 8; // MOV A, EAH bc &= 0xff00; bc |= a; // MOV C,A a = d; // MOV A,D ea = (bc >> 8) * a; // MUL B ea += (bc & 0xff); // EADD EA,C ea >>= 1; // DSLR A ff53 = ea; // save scaled VCF LFO goto h03a1; h036b: ff1e |= 0x08; // set LFO flag goto h0323; h0370: // calculate holdoff time // printf("0370 "); ea = ff56; // holdoff time bc = attackTable[patchRam.lfoDelay]; // stored at ff58 // 0379 ea += bc; // DADD EA,BC ff56 = ea; // STEAX (DE) which still holds ff56 from 0x370 a = ea >> 8; // MOV A, EAH if (a & 0xc0) goto h0385; // OFFI A, $C0 bc &= 0xff; // MOV B, 0 goto h032d; h0385: ff1e |= 0x02; // stop predelay flag h0388: // printf("0388 "); ea = ff5a; // envelope speed // 038d bc = lfoDelayTable[patchRam.lfoDelay >> 4]; // delay setting divided by 8 and saved at ff6c // printf("---------------------------------------- %04x %04x\n", ea, bc); // 0391 DADDNC EA, BC if ((ea + bc) > 0xffff) goto h039a; ea += bc; // 394 ff5a = ea; // STEAX (HL) hl still contains ff5a bc |= (ea & 0xff00); // MOV A, EAH; MOV B, A goto h032d; h039a: ff1e |= 0x04; bc |= 0xff; // MVI B, $ff goto h032d; h03a1: // printf("LFO=%04x VCF=%04x flags=%02x holdoff=%04x envelope=%04x\n", ff51, ff53, ff1e, ff56, ff5a); return; } void Synth::runLFO() { // compute a loop's worth of LFO uint16_t bc, ea; // 074e ea = ff4d; // lfo value bc = lfoRateTable[patchRam.lfoRate]; // bit zero is low for rising slope, high for falling if (!(ff4a & 0x01)) goto h078b; // 075b DSUBNB EA, BC subtract BC from EA, skip next instruction if EA < BC // 075d JRE 079a routine that handles flipping from down to up ea -= bc; if (ea < bc) goto h079a; h075f: ff4d = ea; // LFO output variable // bit one seems to be used to represent negative values of LFO if (!(ff4a & 0x02)) goto h07a2; // routine that adds on 0x2000 to ea // 0765 LFO is negative (bit 1 is high) so invert the value of EA // so that we have a positive-only LFO running from 0 to 0x3fff bc = ea; ea = 0x2000; ea -= bc; h076b: bc = ea; // BC now contains an LFO range from 0 to 0x3fff, always positive if (patchRam.switch2 & 0x01) { // LFO Manual? bc = 0x3fff; // fixed maximum value } // 0771 bc = (bc * patchRam.pwmLfo) >> 7; // scale by PWM pot amount // 077d bc = 0x3fff - bc; // invert so pot = 0 gives 0x3fff // test if squarewave is on or off - if it's off set PW to 0 if (!(patchRam.switch1 & 0x08)) bc = 0x0000; // square off // final computed PWM value ff4f = bc; // 078a goto h07a9; h078b: // BC contains rate, EA contains LFO value ea += bc; if (ea & 0xe000) { // if we've exceeded 0x1fff ea = 0x1fff; // clamp ff4a++; // increment the flags } goto h075f; // store in LFO output variable h079a: ea = 0; // output is close (enough) to zero, clamp ff4a++; // increment the flags goto h075f; // store in LFO output variable h07a2: // LFO output is positive ea += 0x2000; // add on 0x2000 to scale PWM to 0 - 0x3fff goto h076b; // jump back to scale LFO amount h07a9: return; }