Files

2599 lines
126 KiB
C++

// MODHSYN.IRX natively: Sony's hardware synthesizer of the modular synthesizer. It reads the MIDI streams its input
// ports get (from modmidi, modmsin and the driver) at its tick and plays them on the SPU2's voices through libsd, from
// the banks loaded on each port: an "HD" header (the chunks "SCEI" "Vers", "Head", "Prog", "Sset", "Smpl", "Vagi") for
// a "BD" of samples already in sound memory.
//
// A port's synthesizer state (sceHSyn_Init's ctx: +4 the info, +4 its port table, 16 bytes a port, +0xc the state):
// +0 / +1 ..., +0x14 the master volume (16-bit, 0x100), +0x18 16 channels of 0x38 bytes, +0x398 16 bank slots of 24
// bytes (+0 Head, +4 Prog, +8 Sset, +0xc Smpl, +0x10 Vagi, +0x14 the BD's address).
#include "oni_iop.h"
#include "oni_sound_native.h"
#include <climits>
#include <cstdint>
#include <initializer_list>
#include <stdexcept>
#include <string>
namespace oni::drivers
{
namespace
{
using iop::Call;
// The module's import stubs.
constexpr uint32_t kSdSetParam = 0x55a4c, kSdGetParam = 0x55a54, kSdSetSwitch = 0x55a5c, kSdGetSwitch = 0x55a64,
kSdSetAddr = 0x55a6c, kSdGetAddr = 0x55a74, kSdSetCoreAttr = 0x55a7c, kSdNote2Pitch = 0x55a84;
constexpr uint32_t kRegisterLibraryEntries = 0x55b08, kPrintf = 0x55b2c, kGetSystemTime = 0x55b50,
kSysClock2USec = 0x55b58;
constexpr uint32_t kExports = 0x559c0;
constexpr uint32_t kChunkError = 0x55bd0; // "Chunk Error [%c%c%c%c][%c%c%c%c] -> [...][...]"
constexpr uint32_t kSCEI = 0x55c0c, kVers = 0x55c14, kHead = 0x55c1c, kProg = 0x55c24, kSset = 0x55c2c,
kSmpl = 0x55c34, kVagi = 0x55c3c;
void start(Call &c) { c.ret(c.call(kRegisterLibraryEntries, {kExports}) != 0 ? 1 : 0); }
// A chunk's tag (two words, each four characters stored backwards) against two expected names: 1 when they
// match, else the mismatch printed and 0.
uint32_t chunkIs(Call &c, uint32_t w0, uint32_t w1, uint32_t name0, uint32_t name1)
{
for (int k = 0; k < 4; ++k)
{
if (static_cast<int8_t>(w0 >> (8 * k)) == c.s8(name0 + 3 - k) &&
static_cast<int8_t>(w1 >> (8 * k)) == c.s8(name1 + 3 - k))
continue;
auto s = [](int32_t v) { return static_cast<uint32_t>(v); };
auto b = [&](uint32_t w, int i) { return s(static_cast<int8_t>(w >> (8 * i))); };
c.call(kPrintf, {kChunkError, s(c.s8(name0)), s(c.s8(name0 + 1)), s(c.s8(name0 + 2)), s(c.s8(name0 + 3)),
s(c.s8(name1)), s(c.s8(name1 + 1)), s(c.s8(name1 + 2)), s(c.s8(name1 + 3)), b(w0, 3),
b(w0, 2), b(w0, 1), b(w0, 0), b(w1, 3), b(w1, 2), b(w1, 1), b(w1, 0)});
return 0;
}
return 1;
}
void chunkIsFn(Call &c) { c.ret(chunkIs(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3))); }
// sceHSyn_Load(env, port, bd, hd, bank): the port's bank slot set from an HD (its chunks checked) for the BD at
// bd; with no BD or HD the slot is emptied. -1 for a bad port, bank (16) or chunk.
int32_t load(Call &c, uint32_t env, uint32_t port, uint32_t bd, uint32_t hd, uint32_t bank)
{
if (!env || bank >= 16)
return -1;
const uint32_t info = c.r32(env + 4);
if (!(port * 2 + 1 < c.r32(info)))
return -1;
const uint32_t slot = c.r32(c.r32(info + 4) + (port * 2 + 1) * 8 + 4) + bank * 24 + 0x398;
auto clear = [&] {
for (uint32_t k = 0; k < 6; ++k)
c.w32(slot + 4 * k, 0);
};
if (!bd || !hd)
{
clear();
return 0;
}
const uint32_t head = hd + 0x10;
c.w32(slot + 0, head);
c.w32(slot + 4, hd + c.r32(head + 0x14));
c.w32(slot + 8, hd + c.r32(head + 0x18));
c.w32(slot + 0xc, hd + c.r32(head + 0x1c));
c.w32(slot + 0x10, hd + c.r32(head + 0x20));
const uint32_t names[6][2] = {{hd, kVers}, {0, kHead}, {4, kProg}, {8, kSset}, {0xc, kSmpl}, {0x10, kVagi}};
for (int k = 0; k < 6; ++k)
{
const uint32_t chunk = k == 0 ? hd : c.r32(slot + names[k][0]);
if (!chunkIs(c, c.r32(chunk), c.r32(chunk + 4), kSCEI, names[k][1]))
{
clear();
return -1;
}
}
c.w32(slot + 0x14, bd);
return 0;
}
void loadFn(Call &c) { c.ret(load(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3), c.arg(4))); }
// A channel's defaults (its two voice lists empty, pan and ... centred, the state's back pointer +0x1c).
void channelInit(Call &c, uint32_t ch, uint32_t state)
{
c.w32(ch + 0x4, ch - 8);
c.w32(ch + 0x0, ch - 8);
c.w32(ch + 0x14, ch + 8);
c.w32(ch + 0x10, ch + 8);
c.w8(ch + 0x2b, 0x80);
c.w8(ch + 0x2c, 0x80);
c.w32(ch + 0xc, ch);
c.w32(ch + 0x8, ch);
c.w32(ch + 0x18, 0);
c.w32(ch + 0x1c, state);
c.w32(ch + 0x20, 0);
c.w8(ch + 0x32, 0);
c.w16(ch + 0x26, 0);
c.w16(ch + 0x24, 0);
c.w16(ch + 0x28, 0xffff);
c.w8(ch + 0x2a, 0);
c.w8(ch + 0x2d, 0);
c.w8(ch + 0x2f, 0xff);
c.w8(ch + 0x30, 0xff);
c.w8(ch + 0x2e, 0);
c.w8(ch + 0x31, 0);
c.w8(ch + 0x33, 0xff);
c.w8(ch + 0x37, 0xff);
c.w8(ch + 0x36, 0xff);
c.w8(ch + 0x35, 0xff);
}
void channelInitFn(Call &c)
{
channelInit(c, c.arg(0), c.arg(1));
c.ret(0xff);
}
// The channel's program (+0x18) looked up in its bank's (+0x2a) Prog chunk, the bank slot kept (+0x20): none
// for a bank past 16, a program past the chunk's last, an empty entry or one without splits.
void selectProgram(Call &c, uint32_t banks, uint32_t ch, uint32_t program)
{
uint32_t entry = 0, slot = 0;
if (c.r8(ch + 0x2a) < 0x10)
{
slot = banks + c.r8(ch + 0x2a) * 24 + 0x384;
const uint32_t prog = c.r32(slot + 4);
program &= 0xff;
if (!(c.r32(prog + 0xc) < program))
{
const uint32_t offset = c.r32(prog + 0x10 + 4 * program);
if (offset != 0xffffffffu)
{
entry = prog + offset;
if (!c.r8(entry + 4))
entry = 0;
}
}
}
c.w32(ch + 0x18, entry);
c.w32(ch + 0x20, entry ? slot : 0);
}
void selectProgramFn(Call &c)
{
selectProgram(c, c.arg(0), c.arg(1), c.arg(2));
c.ret(0);
}
// --- A channel (0x38 bytes): three voice lists (heads at -8, +0, +8: a node's next at +8), +0x18 the program,
// +0x1c the state, +0x20 its bank slot, +0x24 portamento (x 20), +0x26 the pitch bend (signed, centred),
// +0x2a the bank, +0x2b / +0x2c volume and expression (x 128 / 127), +0x2d / +0x2e two more levels, +0x31 the
// pan offset, +0x33 / +0x34 the portamento switch and note, ...
// Each voice of the channel's three lists, its next read after f.
template <typename F> void forVoices(Call &c, uint32_t ch, F f)
{
for (uint32_t list = 0; list < 3; ++list)
{
const uint32_t head = ch - 8 + 8 * list;
for (uint32_t voice = c.r32(head + 8); voice != head;)
{
f(voice);
voice = c.r32(voice + 8);
}
}
}
constexpr uint32_t kVoiceBend = 0x51e34, kVoiceLevels = 0x52000, kVoiceLevel2d = 0x5209c, kVoiceLevel2e = 0x52144,
kVoicePan = 0x522c8;
// A pitch bend (MSB, LSB: centred on 0x2000): every voice bent when it changed.
void pitchBend(Call &c, uint32_t ch, uint32_t msb, uint32_t lsb)
{
const int16_t bend = static_cast<int16_t>((lsb & 0xff) + ((msb & 0xff) << 7) - 0x2000);
if (c.s16(ch + 0x26) != bend)
{
c.w16(ch + 0x26, static_cast<uint16_t>(bend));
forVoices(c, ch, [&](uint32_t v) { c.call(kVoiceBend, {v, static_cast<uint32_t>(static_cast<int32_t>(bend))}); });
}
}
// Every voice's levels set again (with expression: each voice's own copy, +0x4c, first).
void updateLevels(Call &c, uint32_t ch, bool expression)
{
forVoices(c, ch, [&](uint32_t v) {
if (expression)
c.w8(v + 0x4c, c.r8(ch + 0x2c));
c.call(kVoiceLevels, {v});
});
}
void updateLevelsFn(Call &c)
{
updateLevels(c, c.arg(0), c.arg(1) != 0);
c.ret(0);
}
// A 7-bit controller as 0..128 (x 128 / 127, signed as the module divides).
uint32_t scaled(uint32_t v) { return static_cast<uint32_t>(static_cast<int32_t>((v & 0xff) << 7) / 127); }
// ... and the unsigned shift form two of them use (the same for 0..127).
uint32_t scaledUnsigned(uint32_t v)
{
const uint32_t x = (v & 0xff) << 7;
const uint32_t hi = static_cast<uint32_t>((static_cast<int64_t>(static_cast<int32_t>(x)) * static_cast<int32_t>(0x81020409u)) >> 32);
return (hi + x) >> 6;
}
void setVolume(Call &c, uint32_t ch, uint32_t value)
{
const uint32_t v = scaled(value);
if (c.r8(ch + 0x2b) != (v & 0xff))
{
c.w8(ch + 0x2b, v);
updateLevels(c, ch, false);
}
}
void setExpression(Call &c, uint32_t ch, uint32_t value)
{
const uint32_t v = scaled(value);
if (c.r8(ch + 0x2c) != (v & 0xff))
{
c.w8(ch + 0x2c, v);
updateLevels(c, ch, true);
}
}
void setLevel2d(Call &c, uint32_t ch, uint32_t value)
{
const uint32_t v = scaledUnsigned(value);
if (c.r8(ch + 0x2d) != (v & 0xffff))
{
c.w8(ch + 0x2d, v);
forVoices(c, ch, [&](uint32_t voice) { c.call(kVoiceLevel2d, {voice, v & 0xff}); });
}
}
void setLevel2e(Call &c, uint32_t ch, uint32_t value)
{
const uint32_t v = scaledUnsigned(value);
if (c.r8(ch + 0x2e) != (v & 0xffff))
{
c.w8(ch + 0x2e, v);
forVoices(c, ch, [&](uint32_t voice) { c.call(kVoiceLevel2e, {voice, v & 0xff}); });
}
}
// Portamento time (x 20).
void setPortamentoTime(Call &c)
{
c.w16(c.arg(0) + 0x24, (c.arg(1) & 0xff) * 20);
c.ret(0);
}
// Portamento on (0x80, its note unset) / off (0xff).
uint32_t setPortamento(Call &c, uint32_t ch, uint32_t on)
{
if (!(on & 0xff))
{
c.w8(ch + 0x33, 0xff);
return 0xff;
}
if (c.r8(ch + 0x33) == 0xff)
{
c.w8(ch + 0x33, 0x80);
c.w8(ch + 0x34, 0x80);
}
return 0x80;
}
void setPortamentoNote(Call &c)
{
c.w8(c.arg(0) + 0x34, c.arg(1));
c.ret(0);
}
// The pan (value - 64, at least -63): every voice's pan when it changed.
void setPan(Call &c, uint32_t ch, uint32_t value)
{
uint32_t stored = value - 0x40;
if (static_cast<int8_t>(stored) < -63)
stored = static_cast<uint32_t>(-63);
const int32_t offset = static_cast<int8_t>(stored);
if (c.s8(ch + 0x31) != offset)
{
c.w8(ch + 0x31, stored);
forVoices(c, ch, [&](uint32_t voice) { c.call(kVoicePan, {voice, static_cast<uint32_t>(offset)}); });
}
}
// The voices a per-note message addresses: its note (+0x3e; bit 7: every note) and id (+0x3f; 0x7f: any).
bool addressed(Call &c, uint32_t voice, uint32_t key, uint32_t id)
{
return (id == 0x7f || c.r8(voice + 0x3f) == id) && ((key & 0x80) || c.r8(voice + 0x3e) == (key & 0xff));
}
// Per-note pan (value - 64, at least -63), level (x 128 / 127: the voice's +0x4c) and pitch bend.
void notePan(Call &c, uint32_t ch, uint32_t key, uint32_t id, uint32_t value)
{
id &= 0xff;
uint32_t offset = value - 0x40;
if (static_cast<int8_t>(offset) < -63)
offset = static_cast<uint32_t>(-63);
forVoices(c, ch, [&](uint32_t v) {
if (addressed(c, v, key, id))
c.call(kVoicePan, {v, static_cast<uint32_t>(static_cast<int32_t>(static_cast<int8_t>(offset)))});
});
}
void noteLevel(Call &c, uint32_t ch, uint32_t key, uint32_t id, uint32_t value)
{
id &= 0xff;
const uint32_t level = scaledUnsigned(value);
forVoices(c, ch, [&](uint32_t v) {
if (addressed(c, v, key, id))
{
c.w8(v + 0x4c, level);
c.call(kVoiceLevels, {v});
}
});
}
void noteBend(Call &c, uint32_t ch, uint32_t key, uint32_t id, uint32_t msb, uint32_t lsb)
{
id &= 0xff;
const uint32_t bend = (lsb & 0xff) + ((msb & 0xff) << 7) - 0x2000;
forVoices(c, ch, [&](uint32_t v) {
if (addressed(c, v, key, id))
c.call(kVoiceBend, {v, static_cast<uint32_t>(static_cast<int32_t>(static_cast<int16_t>(bend)))});
});
}
// Reset all controllers (CC 121): the channel's 0x52db8 state, bend centred, the two levels 0, volume 127, pan
// centred, expression 127, portamento off.
constexpr uint32_t kChannelSustain = 0x52db8;
uint32_t resetControllers(Call &c, uint32_t ch)
{
c.call(kChannelSustain, {ch, 0});
pitchBend(c, ch, 0x40, 0);
setLevel2d(c, ch, 0);
setLevel2e(c, ch, 0);
setVolume(c, ch, 0x7f);
setPan(c, ch, 0x40);
setExpression(c, ch, 0x7f);
return setPortamento(c, ch, 0);
}
// A control change on a channel: bank (CC 0), the two levels (CC 1, 2), portamento time (5), volume (7), pan
// (10), expression (11), sustain (64), portamento (65), portamento note (84), all sound off (120), reset all
// controllers (121), all notes off (123), and Sony's NRPN parameters (99 then 98, then data entry 6: NRPN 2 and 3
// set the synthesizer's two global parameters, 0x50d40 / 0x50db0).
constexpr uint32_t kAllSoundOff = 0x530e8, kAllNotesOff = 0x52f50, kGlobalParam2 = 0x50d40, kGlobalParam3 = 0x50db0;
void controlChange(Call &c, uint32_t ch, uint32_t control, uint32_t value)
{
const uint32_t v = value & 0xff;
switch (control & 0xff)
{
case 0x00: c.w8(ch + 0x2a, value); break;
case 0x01: setLevel2e(c, ch, v); break;
case 0x02: setLevel2d(c, ch, v); break;
case 0x05: c.w16(ch + 0x24, v * 20); break;
case 0x07: setVolume(c, ch, v); break;
case 0x0a: setPan(c, ch, v); break;
case 0x0b: setExpression(c, ch, v); break;
case 0x40: c.call(kChannelSustain, {ch, v}); break;
case 0x41: setPortamento(c, ch, v); break;
case 0x54: c.w8(ch + 0x34, v); break;
case 0x78: c.call(kAllSoundOff, {ch}); break;
case 0x79: resetControllers(c, ch); break;
case 0x7b: c.call(kAllNotesOff, {ch}); break;
case 0x63:
c.w8(ch + 0x35, value);
c.w8(ch + 0x37, 0xff);
c.w8(ch + 0x36, 0xff);
break;
case 0x62: c.w8(ch + 0x36, value); break;
case 0x06:
c.w8(ch + 0x37, value);
if (c.r8(ch + 0x36) != 0xff)
{
if (c.r8(ch + 0x35) == 2)
c.call(kGlobalParam2, {c.r8(ch + 0x36), c.r8(ch + 0x37)});
else if (c.r8(ch + 0x35) == 3)
c.call(kGlobalParam3, {c.r8(ch + 0x36), c.r8(ch + 0x37)});
}
c.w8(ch + 0x37, 0xff);
c.w8(ch + 0x36, 0xff);
c.w8(ch + 0x35, 0xff);
break;
default: break;
}
}
// A channel message for the port's state: control changes, note on (a velocity of 0 is a note off), note
// off, program change, pitch bend; aftertouch is ignored.
constexpr uint32_t kNoteOn = 0x534f8, kNoteOff = 0x52ad4;
void channelMessage(Call &c, uint32_t state, uint32_t port, uint32_t status, uint32_t data1, uint32_t data2)
{
const uint32_t banks = state + 0x14, ch = state + 0x18 + 0x38 * (status & 0xf);
data2 &= 0xff;
switch (status & 0xf0)
{
case 0xb0: controlChange(c, ch, data1, data2); break;
case 0x90:
if (data2)
{
c.call(kNoteOn, {state, ch, port, data1 & 0xff, data2, 0xff});
break;
}
c.call(kNoteOff, {ch, data1 & 0xff, 0xff});
break;
case 0x80: c.call(kNoteOff, {ch, data1 & 0xff, 0xff}); break;
case 0xc0: selectProgram(c, banks, ch, data1 & 0xff); break;
case 0xe0: pitchBend(c, ch, data2, data1 & 0xff); break;
default: break;
}
}
// Sony's synthesizer messages (data: the bytes after the status). 0xF9 (4 bytes: what, channel, value, value
// MSB) sets a channel's +0x2f (0), +0x30 (1) or 16-bit +0x28 (2); 0xFD (6 bytes: what, channel | 0x10 for every
// note, note, id, value, MSB) addresses a note's voices: level (0), pan (1), bend (2), or a note on with that id
// (0x10; velocity 0: off).
void synthMessage(Call &c, uint32_t state, uint32_t port, uint32_t status, uint32_t data)
{
const uint32_t what = c.r8(data), channel = c.r8(data + 1), value = c.r8(data + 2);
const uint32_t ch = state + (channel & 0xf) * 0x38 + 0x18;
status &= 0xff;
if (status == 0xf9)
{
switch (what)
{
case 0: c.w8(ch + 0x2f, value); break;
case 1: c.w8(ch + 0x30, value); break;
case 2: c.w16(ch + 0x28, value + (c.r8(data + 3) << 7)); break;
default: break;
}
return;
}
if (status != 0xfd)
return;
const uint32_t id = c.r8(data + 3), level = c.r8(data + 4);
uint32_t key = value | ((channel & 0x10) ? 0x80 : 0);
switch (what)
{
case 0: noteLevel(c, ch, key & 0xff, id, level); break;
case 1: notePan(c, ch, key & 0xff, id, level); break;
case 2: noteBend(c, ch, key & 0xff, id, c.r8(data + 5), level); break;
case 0x10:
key &= 0x7f;
if (level)
c.call(kNoteOn, {state, ch, port, key, level, id});
else
c.call(kNoteOff, {ch, key, id});
break;
default: break;
}
}
// A port's stream bytes from p to end: channel messages (two data bytes, one for program, aftertouch and
// Sony's note off), system exclusive messages skipped up to 0xF7, synthesizer messages 0xF9 (4 bytes) and
// 0xFD (6); stray data bytes and other system messages skipped. (The first byte is read before the bounds are
// checked, as the module does.)
constexpr uint32_t kHasData2 = 0x55ea0; // per status nibble (0x80..0xF0): a second data byte
void parseStream(Call &c, uint32_t state, uint32_t port, uint32_t p, uint32_t end)
{
do
{
const uint32_t status = c.r8(p++);
if (!(status & 0x80))
continue;
if (status < 0xf0)
{
if (!(p < end))
return;
const uint32_t data1 = c.r8(p++);
uint32_t data2 = 0;
if (c.s8(kHasData2 + ((status - 0x80) >> 4)))
{
if (!(p < end))
return;
data2 = c.r8(p++);
}
channelMessage(c, state, port, status, data1, data2);
}
else if (status == 0xf0)
{
if (!(p < end))
return;
if (c.r8(p++) != 0xf7)
for (;;)
{
if (!(p < end))
return;
if (c.r8(p++) == 0xf7)
break;
}
}
else if (status == 0xf9 || status == 0xfd)
{
const uint32_t data = p;
p += status == 0xf9 ? 4 : 6;
if (!(end < p))
synthMessage(c, state, port, status, data);
}
} while (p < end);
}
// Each input port's stream (16-byte entries: +4 the buffer {+4 the bytes used, +8 the bytes}, +0xc the state)
// parsed, its buffer emptied first.
void readPorts(Call &c, uint32_t ports, int32_t count)
{
uint32_t entry = ports, port = 0;
for (int32_t k = 1; k < count; k += 2, entry += 16, ++port)
{
const uint32_t buffer = c.r32(entry + 4);
const uint32_t start = buffer + 8, end = buffer + c.r32(buffer + 4) + 8;
if (start < end)
{
c.w32(buffer + 4, 0);
parseStream(c, c.r32(entry + 0xc), port, start, end);
}
}
}
// sceHSyn_ATick(env): the pending output mode applied (0x52a30, until it took), each core's voice mixing
// (VMIXL, VMIXR, VMIXEL, VMIXER) read back for the voices the synthesizer owns (0x56128 + 4 x core) into its
// copies (0x56150, 0x56154, 0x584e0, 0x584e4 + 8 x core), the input ports read, then the voices' work (0x511a8)
// and the envelope pass (0x5256c), their results stored where SetVoiceStatBuffer asked (0x56140).
constexpr uint32_t kVoiceStatBuffer = 0x56140; // sceHSyn_SetVoiceStatBuffer's buffer (0: none)
constexpr uint32_t kApplyOutputMode = 0x52a30, kGetSwitch = 0x5509c, kVoiceWork = 0x511a8, kVoiceStatus = 0x5256c;
void tick(Call &c)
{
const uint32_t env = c.arg(0);
if (c.r8(0x56171) != 0xff && c.call(kApplyOutputMode, {}) != 0)
c.w8(0x56171, 0xff);
for (uint32_t core = 0; core < 2; ++core)
{
const uint32_t owned = 0x56128 + 4 * core;
const uint32_t dryL = 0x56150 + 8 * core, dryR = 0x56154 + 8 * core, wetL = 0x584e0 + 8 * core,
wetR = 0x584e4 + 8 * core;
const uint32_t keep = ~c.r32(owned);
c.w32(dryL, c.r32(dryL) & keep);
c.w32(dryR, c.r32(dryR) & keep);
c.w32(wetL, c.r32(wetL) & keep);
c.w32(wetR, c.r32(wetR) & keep);
const uint32_t mask = c.r32(owned);
if (!mask)
continue;
c.w32(dryL, c.r32(dryL) | (c.call(kGetSwitch, {(core | 0x1800) & 0xffff}) & mask));
c.w32(dryR, c.r32(dryR) | (c.call(kGetSwitch, {(core | 0x1a00) & 0xffff}) & mask));
c.w32(wetL, c.r32(wetL) | (c.call(kGetSwitch, {(core | 0x1900) & 0xffff}) & mask));
c.w32(wetR, c.r32(wetR) | (c.call(kGetSwitch, {(core | 0x1b00) & 0xffff}) & mask));
}
const uint32_t info = c.r32(env + 4);
readPorts(c, c.r32(info + 4), c.s32(info));
const uint32_t work = c.call(kVoiceWork, {});
const uint32_t status = c.call(kVoiceStatus, {});
if (const uint32_t out = c.r32(kVoiceStatBuffer))
{
c.w32(out, work);
c.w32(out + 4, status);
}
c.ret(0);
}
// The port's state and its banks base (state + 0x14): false for a port the environment does not have.
bool portState(Call &c, uint32_t env, uint32_t port, uint32_t &state)
{
if (!env)
return false;
const uint32_t info = c.r32(env + 4);
if (!(port * 2 + 1 < c.r32(info)))
return false;
state = c.r32(c.r32(info + 4) + (port * 2 + 1) * 8 + 4);
return true;
}
void portStateFn(Call &c)
{
uint32_t state;
if (!portState(c, c.arg(0), c.arg(1), state))
return c.ret(0);
c.w32(c.arg(2), state);
c.w32(c.arg(3), state + 0x14);
c.ret(1);
}
// sceHSyn_SetVolume(env, port, volume) / GetVolume: the port's master volume (+0x14, 16-bit).
void setMasterVolume(Call &c)
{
uint32_t state;
if (!portState(c, c.arg(0), c.arg(1), state))
return c.ret(static_cast<uint32_t>(-1));
c.w16(state + 0x14, c.arg(2));
c.ret(0);
}
void getMasterVolume(Call &c)
{
uint32_t state;
c.ret(portState(c, c.arg(0), c.arg(1), state) ? c.r16(state + 0x14) : 0);
}
// All notes off / all sound off / reset all controllers on each of the port's 16 channels.
template <uint32_t Op> void eachChannel(Call &c)
{
uint32_t state;
if (!portState(c, c.arg(0), c.arg(1), state))
return c.ret(static_cast<uint32_t>(-1));
for (uint32_t ch = 0; ch < 16; ++ch)
{
const uint32_t channel = state + 0x18 + 0x38 * ch;
if (Op == 0)
c.call(kAllNotesOff, {channel});
else if (Op == 1)
c.call(kAllSoundOff, {channel});
else
resetControllers(c, channel);
}
c.ret(0);
}
// sceHSyn_SetReservVoice(masks): the voices each core keeps for itself (0x56128 + 4 x core): every voice of
// the core's free list (0x56160) moved to the synthesizer's list (0x56100), then those the mask reserves moved
// back, the core's voice count (0x584d4 + core) kept.
constexpr uint32_t kListAppend = 0x510f0, kListPush = 0x5110c, kListRemove = 0x51128;
void setReservedVoices(Call &c)
{
const uint32_t masks = c.arg(0);
for (uint32_t core = 0; core < 2; ++core)
{
const uint32_t mask = c.r32(masks + 4 * core);
const uint32_t reserved = 0x56160 + 8 * core, synth = 0x56100 + 8 * core, count = 0x584d4 + core;
c.w32(0x56128 + 4 * core, mask);
for (uint32_t v = c.r32(reserved); v != reserved; v = c.r32(reserved))
{
c.call(kListRemove, {v});
c.call(kListPush, {v, synth});
c.w8(count, c.r8(count) + 1);
}
for (uint32_t v = c.r32(synth); v != synth; v = c.r32(v))
if (mask & (1u << c.r8(v + 0x3d)))
{
const uint32_t previous = c.r32(v + 4);
c.call(kListRemove, {v});
c.call(kListAppend, {v, reserved});
c.w8(count, c.r8(count) - 1);
v = previous;
}
}
c.ret(0);
}
// sceHSyn_SetEffectAttr (export 9; attr: +0 core, +4 mode (Sony's preset number, bit 0x100: clear the work
// area), +8 / +0xa depth, +0xc delay, +0x10 feedback, +0x14 / +0x16 the effect return volumes): the libsd
// effect attributes set (its mode from the module's table), the effect enabled when the mode is one, the
// return volumes set (EVOLL, EVOLR).
constexpr uint32_t kEffectModes = 0x55ea8, kSetEffect = 0x55844, kGetEffect = 0x558c0, kSetCoreAttr = 0x551f4,
kSetParam = 0x54f38;
void setEffect(Call &c)
{
const uint32_t a = c.arg(0);
iop::Locals attr(c, 0x18);
const uint32_t mode = c.r32(kEffectModes + 4 * (c.r32(a + 4) & 0xf));
c.w32(attr.base + 0, c.r32(a));
c.w32(attr.base + 4, mode);
c.w16(attr.base + 8, c.r16(a + 8));
c.w16(attr.base + 0xa, c.r16(a + 0xa));
c.w32(attr.base + 0xc, c.r32(a + 0xc));
c.w32(attr.base + 0x10, c.r32(a + 0x10));
if (c.r32(a + 4) & 0x100)
c.w32(attr.base + 4, mode | 0x100);
c.call(kSetEffect, {c.r32(a), attr.base});
c.call(kSetCoreAttr, {c.r16(a) | 2, mode != 0 ? 1u : 0u});
c.call(kSetParam, {c.r16(a) | 0xb80, c.r16(a + 0x14)});
c.call(kSetParam, {c.r16(a) | 0xc80, c.r16(a + 0x16)});
c.ret(0);
}
// sceHSyn_SetVoiceStatBuffer(buffer): where the tick stores its two results (0: nowhere).
void setVoiceStatBuffer(Call &c)
{
c.w32(0x56140, c.arg(0));
c.ret(0);
}
// NRPN 2 (param: 0 or 0x10 for core 1, value: a mode below 10): the core's effect mode, its work area cleared.
void effectMode(Call &c)
{
const uint32_t param = c.arg(0), value = c.arg(1) & 0xff;
if (value < 10 && ((param & 0xff) == 0 || (param & 0xff) == 0x10))
{
iop::Locals attr(c, 0x18);
const uint32_t core = (param & 0x10) ? 1 : 0;
c.call(kGetEffect, {core, attr.base});
c.w32(attr.base + 4, value | 0x100);
c.call(kSetEffect, {core, attr.base});
}
c.ret(0);
}
// NRPN 3 (param: what, 0x10 for core 1): the core's effect depth (0: value << 8 on both sides, 1: its
// negative), delay (2) or feedback (3).
void effectParam(Call &c)
{
const uint32_t param = c.arg(0), value = c.arg(1) & 0xff;
if (param & 0xe0)
return c.ret(0);
iop::Locals attr(c, 0x18);
const uint32_t core = (param & 0x10) ? 1 : 0;
c.call(kGetEffect, {core, attr.base});
switch (param & 0xf)
{
case 0:
c.w16(attr.base + 0xa, value << 8);
c.w16(attr.base + 8, value << 8);
break;
case 1:
c.w16(attr.base + 0xa, -(value << 8));
c.w16(attr.base + 8, -(value << 8));
break;
case 2: c.w32(attr.base + 0xc, value); break;
case 3: c.w32(attr.base + 0x10, value); break;
default: break;
}
c.call(kSetEffect, {core, attr.base});
c.ret(0);
}
// --- Voices. 2 cores x 24 voices of 0xbc bytes at 0x56180 (+0x11a0 a core): +0 / +4 their list links
// (free, reserved, playing), +8 / +0xc their channel list links, +0x3c the core, +0x3d the voice number, ...
// Lists: heads of two words linking to themselves when empty.
// node after head / before head (the tail); removed. Each returns what the module's does.
uint32_t listInsertAfter(Call &c, uint32_t node, uint32_t head)
{
c.w32(node + 4, head);
const uint32_t next = c.r32(head);
c.w32(node, next);
c.w32(head, node);
c.w32(next + 4, node);
return next;
}
uint32_t listAppend(Call &c, uint32_t node, uint32_t head)
{
c.w32(node, head);
const uint32_t last = c.r32(head + 4);
c.w32(node + 4, last);
c.w32(head + 4, node);
c.w32(last, node);
return last;
}
uint32_t listRemove(Call &c, uint32_t node)
{
const uint32_t next = c.r32(node), prev = c.r32(node + 4);
c.w32(next + 4, prev);
c.w32(prev, next);
return c.r32(node);
}
// A voice put at the front of a channel list (link: the list's next field), removed from its channel list.
uint32_t channelListPush(Call &c, uint32_t voice, uint32_t link)
{
c.w32(voice + 0xc, link - 8);
const uint32_t next = c.r32(link);
c.w32(voice + 8, next);
c.w32(link, voice);
c.w32(next + 0xc, voice);
return next;
}
uint32_t channelListRemove(Call &c, uint32_t voice)
{
const uint32_t next = c.r32(voice + 8), prev = c.r32(voice + 0xc);
c.w32(next + 0xc, prev);
c.w32(prev + 8, next);
return c.r32(voice + 8);
}
// The voice lists emptied and every voice back on its core's free list (0x56100 + 8 x core), numbered; the
// counts (24 a core) and the mixing copies cleared.
uint32_t voiceReset(Call &c)
{
for (const uint32_t head : {0x56160u, 0x56168u, 0x56130u, 0x56138u, 0x584c0u, 0x584c8u, 0x56100u, 0x56108u})
{
c.w32(head + 4, head);
c.w32(head, head);
}
for (uint32_t core = 0; core < 2; ++core)
for (uint32_t i = 0; i < 24; ++i)
{
const uint32_t v = 0x56180 + core * 0x11a0 + i * 0xbc;
c.w16(v + 0x10, 0);
c.w16(v + 0x12, 0);
c.w16(v + 0x14, 0);
c.w8(v + 0x3c, core);
c.w8(v + 0x3d, i);
listAppend(c, v, 0x56100 + 8 * core);
}
c.w8(0x584d5, 0x18);
c.w8(0x584d4, 0x18);
for (const uint32_t a : {0x5614cu, 0x56148u, 0x5611cu, 0x56118u, 0x5615cu, 0x56158u, 0x56154u, 0x56150u, 0x584ecu,
0x584e8u, 0x584e4u, 0x584e0u})
c.w32(a, 0);
return 0x584e0;
}
// The voices still playing (each core's playing list, from its tail): one whose envelope (ENVX) has fallen
// under 1000 is finished (0x514e8) and moved to the core's ended list (0x56130 + 8 x core). The count of those
// still sounding.
constexpr uint32_t kGetParam = 0x54fb4, kVoiceFinish = 0x514e8;
uint32_t voiceWork(Call &c)
{
c.w32(0x5614c, 0);
c.w32(0x56148, 0);
uint32_t sounding = 0;
for (uint32_t core = 0; core < 2; ++core)
{
const uint32_t playing = 0x584c0 + 8 * core;
for (uint32_t v = c.r32(playing + 4); v != playing; v = c.r32(v + 4))
{
const uint32_t envelope = c.call(kGetParam, {((core | (c.r8(v + 0x3d) << 1)) | 0x500) & 0xffff});
if ((envelope & 0xffff) < 1000)
{
const uint32_t next = c.r32(v);
c.call(kVoiceFinish, {v});
listRemove(c, v);
listInsertAfter(c, v, 0x56130 + 8 * core);
v = next;
}
else
++sounding;
}
}
return sounding;
}
// Saturating sums: a 16-bit and an 8-bit field plus two amounts.
uint32_t add16(uint32_t a, uint32_t b, uint32_t d)
{
int32_t v = static_cast<int32_t>(b + d + static_cast<uint32_t>(static_cast<int16_t>(a)));
v = v > 0x7fff ? 0x7fff : v < -0x8000 ? -0x8000 : v;
return static_cast<uint32_t>(static_cast<int16_t>(v));
}
uint32_t add8(uint32_t a, uint32_t b, uint32_t d)
{
int32_t v = static_cast<int32_t>(b + d + static_cast<uint32_t>(static_cast<int8_t>(a)));
v = v > 0x7f ? 0x7f : v < -0x80 ? -0x80 : v;
return static_cast<uint32_t>(static_cast<int8_t>(v));
}
// A bit field of an ADSR word moved by a key-follow amount (amount x distance / 12, truncated), kept within
// 0..mask.
uint32_t follow(uint32_t word, uint32_t amount, uint32_t distance, uint32_t mask, uint32_t shift)
{
const int32_t x = static_cast<int32_t>(distance * static_cast<uint32_t>(static_cast<int32_t>(static_cast<int8_t>(amount))));
const int32_t hi = static_cast<int32_t>((static_cast<int64_t>(x) * 0x2aaaaaab) >> 32);
const int32_t d = (hi >> 1) - (x >> 31);
if (!d)
return word & 0xffff;
mask &= 0xffff;
int32_t f = static_cast<int32_t>(((word & 0xffff) >> (shift & 31)) & mask) + d;
if (f < 0)
f = 0;
else if (static_cast<int32_t>(mask) < f)
f = static_cast<int32_t>(mask);
word = (word & ~(mask << (shift & 31))) | (static_cast<uint32_t>(f) << (shift & 31));
return word & 0xffff;
}
// A split's ADSR1 (+0x12) and ADSR2 (+0x14) for a note, each rate and level following the key from its centre
// (attack +0x16/+0x17, decay +0x18/+0x19, sustain level +0x1e/+0x1f; sustain rate +0x1a/+0x1b, release
// +0x1c/+0x1d).
uint32_t adsr1(Call &c, uint32_t note, uint32_t split)
{
uint32_t v = c.r16(split + 0x12);
if (c.s8(split + 0x16))
v = follow(v, c.r8(split + 0x16), note - c.r8(split + 0x17), 0x7f, 8);
if (c.s8(split + 0x18))
v = follow(v, c.r8(split + 0x18), note - c.r8(split + 0x19), 15, 4);
if (c.s8(split + 0x1e))
v = follow(v, c.r8(split + 0x1e), note - c.r8(split + 0x1f), 15, 0);
return v & 0xffff;
}
uint32_t adsr2(Call &c, uint32_t note, uint32_t split)
{
uint32_t v = c.r16(split + 0x14);
if (c.s8(split + 0x1a))
v = follow(v, c.r8(split + 0x1a), note - c.r8(split + 0x1b), 0x7f, 6);
if (c.s8(split + 0x1c))
v = follow(v, c.r8(split + 0x1c), note - c.r8(split + 0x1d), 0x1f, 0);
return v & 0xffff;
}
constexpr uint32_t kTickMsValue = 0x56110; // the tick in milliseconds (sceHSyn_Init)
// amount x distance / 12, truncated (the module's 0x2aaaaaab product).
int32_t div12(int32_t x)
{
const int32_t hi = static_cast<int32_t>((static_cast<int64_t>(x) * 0x2aaaaaab) >> 32);
return (hi >> 1) - (x >> 31);
}
// A voice keyed on (0x514e8): its two LFOs (pitch +0x6c, amplitude +0x94) from the program, its pitch and level
// offsets following the key, the channel's levels and pan, its own level (split x sample set x program x
// velocity), its pitch, then either a sample (start address, ADSR following the key, looping) or the noise
// generator at the note's clock; the core's noise, mixing (from the program's +0x29: dry L/R, wet L/R) and
// key-on switches; the time of the key on kept (0x584d0 / 0x56120), the voice's status in the stat buffer.
// The voice: +0x3c core, +0x3d number, +0x3e note, +0x54 its channel, +0x58 split, +0x5c sample set, +0x60
// program, +0x64 the sample (VAG info), +0x68 the bank's sound memory base.
constexpr uint32_t kLfoInit = 0x549c4, kKeyFollow = 0x52038, kVelocity = 0x545f4, kVoiceGain = 0x52414,
kSetPitchOffset = 0x521f4, kVoiceStartPitch = 0x51e8c, kVoiceLoop = 0x51d10,
kSetAddrWrap = 0x5510c, kSetParamWrap = 0x54f38, kSetCoreAttrWrap = 0x551f4,
kSetSwitchWrap = 0x55024;
void keyOn(Call &c, uint32_t v)
{
const uint32_t core = c.r8(v + 0x3c), number = c.r8(v + 0x3d);
const uint32_t ch = c.r32(v + 0x54), state = c.r32(ch + 0x1c);
const uint32_t program = c.r32(v + 0x60), split = c.r32(v + 0x58), set = c.r32(v + 0x5c);
const uint32_t bit = 1u << number, id = core | (number << 1);
c.call(kLfoInit, {v + 0x6c, split, c.r32(state + 8), c.r32(state + 4), c.r8(program + 0x28) & 7,
c.r16(program + 0x20), c.r16(program + 0x22), c.r32(kTickMsValue), 0});
int32_t follow1 = div12(static_cast<int32_t>((c.r8(v + 0x3e) - c.r8(set + 0xb)) *
static_cast<uint32_t>(static_cast<int32_t>(c.s8(set + 0xa)))));
uint32_t key = c.call(kKeyFollow, {v, static_cast<uint32_t>(static_cast<int32_t>(c.s8(program + 5))),
c.r8(program + 6), c.r8(program + 7)});
c.w16(v + 0x24, add16(static_cast<uint32_t>(c.s16(split + 0x18)), static_cast<uint32_t>(follow1), key));
c.w16(v + 0x26, add16(static_cast<uint32_t>(c.s16(split + 0x1a)), static_cast<uint32_t>(follow1), key));
c.call(kLfoInit, {v + 0x94, split, c.r32(state + 8), c.r32(state + 4), c.r8(program + 0x28) & 0x70,
c.r16(program + 0x24), c.r16(program + 0x26), c.r32(kTickMsValue), 1});
follow1 = div12(static_cast<int32_t>((c.r8(v + 0x3e) - c.r8(set + 0xd)) *
static_cast<uint32_t>(static_cast<int32_t>(c.s8(set + 0xc)))));
key = c.call(kKeyFollow, {v, static_cast<uint32_t>(static_cast<int32_t>(c.s8(program + 8))), c.r8(program + 9),
c.r8(program + 0xa)});
c.w8(v + 0x49, add8(static_cast<uint32_t>(c.s8(split + 0x20)), static_cast<uint32_t>(follow1), key));
c.w8(v + 0x4a, add8(static_cast<uint32_t>(c.s8(split + 0x21)), static_cast<uint32_t>(follow1), key));
c.call(kVoiceLevel2d, {v, c.r8(ch + 0x2d)});
c.call(kVoiceLevel2e, {v, c.r8(ch + 0x2e)});
const uint32_t velocity = c.call(kVelocity, {c.r8(v + 0x40), c.r8(v + 0x42), c.r32(state + 0x10), c.r32(state + 0xc)});
int32_t level = static_cast<int32_t>(static_cast<uint32_t>(c.r8(split + 6)) * c.r8(set + 0x10) * c.r8(program + 0x10) * (velocity & 0xff));
if (level < 0)
level += 0x3fff;
const int32_t scaledLevel = level >> 14;
const uint32_t gain = c.call(kVoiceGain, {v});
c.w16(v + 0x16, (static_cast<uint32_t>(scaledLevel) * gain) >> 16);
const uint32_t note = c.r8(v + 0x3e);
const int32_t pitch = static_cast<int32_t>((note - c.r8(set + 0xf)) * static_cast<uint32_t>(static_cast<int32_t>(c.s8(set + 0xe))) +
(note - c.r8(split + 0xb)) * static_cast<uint32_t>(static_cast<int32_t>(c.s8(split + 0xa))));
c.w8(v + 0x46, c.r8(split + 0xc) & 1);
c.call(kSetPitchOffset, {v, static_cast<uint32_t>(div12(pitch))});
uint32_t pan;
if (c.s8(v + 0x4e) & 0x80)
pan = c.r8(ch + 0x31);
else
{
pan = c.r8(v + 0x4e) - 0x40;
if (static_cast<int8_t>(pan) < -63)
pan = static_cast<uint32_t>(-63);
}
c.call(kVoicePan, {v, static_cast<uint32_t>(static_cast<int32_t>(static_cast<int8_t>(pan)))});
uint32_t expression = c.r8(v + 0x4d);
if (expression == 0xff)
expression = c.r8(ch + 0x2c);
c.w8(v + 0x4c, expression);
c.call(kVoiceLevels, {v});
c.w16(v + 0x1c, 0xffff);
c.w16(v + 0x1a, 0xffff);
c.call(kVoiceStartPitch, {v});
const uint32_t noise = 0x56118 + 4 * core;
if (const uint32_t sample = c.r32(v + 0x64))
{
c.w32(noise, c.r32(noise) & ~bit);
c.w32(v + 0x2c, 0x7fffffff);
c.w32(v + 0x38, static_cast<uint32_t>((static_cast<uint64_t>(static_cast<uint32_t>(c.r16(sample + 4)) << 16) * 0x057619f1u) >> 32) >> 10);
const uint32_t bend = (c.s16(v + 0x28) & 0x8000) ? c.r16(ch + 0x26) : c.r16(v + 0x28) - 0x2000;
c.call(kVoiceBend, {v, static_cast<uint32_t>(static_cast<int32_t>(static_cast<int16_t>(bend)))});
c.call(kVoiceLoop, {v});
c.call(kSetAddrWrap, {id | 0x2040, c.r32(v + 0x68) + c.r32(sample)});
c.call(kSetParamWrap, {id | 0x300, adsr1(c, c.r8(v + 0x3e), program)});
c.call(kSetParamWrap, {id | 0x400, adsr2(c, c.r8(v + 0x3e), program)});
if (!(c.r8(c.r32(v + 0x64) + 6) & 1))
c.w16(v + 0x10, c.r16(v + 0x10) | 1);
}
else
{
uint32_t clock = c.r8(v + 0x3e);
if (clock >= 0x40)
clock = 0x3f;
c.call(kSetCoreAttrWrap, {c.r8(v + 0x3c) | 8, clock});
c.w32(noise, c.r32(noise) | bit);
}
c.call(kSetSwitchWrap, {c.r8(v + 0x3c) | 0x1400, c.r32(0x56118 + 4 * c.r8(v + 0x3c))});
const uint32_t mix = c.r8(program + 0x29);
const uint32_t masks[4] = {0x56150, 0x56154, 0x584e0, 0x584e4};
for (uint32_t k = 0; k < 4; ++k)
{
const uint32_t a = masks[k] + 8 * c.r8(v + 0x3c);
c.w32(a, (mix & (1u << k)) ? c.r32(a) | bit : c.r32(a) & ~bit);
}
const uint32_t switches[4] = {0x1800, 0x1a00, 0x1900, 0x1b00};
for (uint32_t k = 0; k < 4; ++k)
c.call(kSetSwitchWrap, {c.r8(v + 0x3c) | switches[k], c.r32(masks[k] + 8 * c.r8(v + 0x3c))});
c.call(kSetSwitchWrap, {c.r8(v + 0x3c) | 0x1500, bit});
c.w8(0x56170, c.r8(v + 0x3c));
c.w8(0x56171, c.r8(v + 0x3d));
{
iop::Locals clock(c, 8);
c.call(kGetSystemTime, {clock.base});
c.call(kSysClock2USec, {clock.base, 0x584d0, 0x56120});
}
c.w8(v + 0x43, 3);
c.w16(v + 0x10, c.r16(v + 0x10) & 0xfff7);
if (const uint32_t stats = c.r32(0x56140))
{
const uint32_t a = stats + c.r8(v + 0x3c) * 24 + 8 + c.r8(v + 0x3d);
c.w8(a, (c.r8(a) & 0x8f) | 0x20);
}
}
// x / 2^shift rounded toward zero, as the module divides (adding 2^shift - 1 to a negative before shifting).
int32_t shiftDown(int32_t x, int shift)
{
if (x < 0)
x += (1 << shift) - 1;
return x >> shift;
}
// The voice's pitch (0x51d10): its pitch offset (+0x30 bend, +0x34 key follow) plus the pitch LFO (+0x7c, by
// its depth up +0x20 or down +0x22), when it changed (+0x2c): the note (the voice's + split +8 + sample set
// +0x12) and fine tune (split +9, program +0xc, sample set +0x13; carried into the note when negative) as
// libsd's pitch for the program's centre note (+0xb), scaled by the sample's rate (+0x38).
constexpr uint32_t kNote2PitchWrap = 0x55270;
void updatePitch(Call &c, uint32_t v)
{
const int32_t lfo = c.s16(v + 0x7c);
const int32_t depth = lfo >= 0 ? c.s16(v + 0x20) : c.s16(v + 0x22);
int32_t pitch = shiftDown(static_cast<int32_t>(static_cast<uint32_t>(depth) * static_cast<uint32_t>(lfo)), 14);
pitch = static_cast<int32_t>(static_cast<uint32_t>(pitch) + c.r32(v + 0x30) + c.r32(v + 0x34));
if (static_cast<uint32_t>(pitch) == c.r32(v + 0x2c))
return;
const uint32_t split = c.r32(v + 0x58), set = c.r32(v + 0x5c);
c.w32(v + 0x2c, static_cast<uint32_t>(pitch));
uint32_t note = c.r8(v + 0x3e) + static_cast<uint32_t>(c.s8(split + 8)) + static_cast<uint32_t>(c.s8(set + 0x12));
pitch = static_cast<int32_t>(static_cast<uint32_t>(pitch) + static_cast<uint32_t>(c.s8(split + 9)) +
static_cast<uint32_t>(c.s8(c.r32(v + 0x60) + 0xc)) + static_cast<uint32_t>(c.s8(set + 0x13)));
if (pitch < 0)
{
const int32_t carry = (pitch + 0x7f) >> 7;
note += static_cast<uint32_t>(carry);
pitch -= carry << 7;
if (pitch)
{
pitch += 0x80;
note -= 1;
}
}
const uint32_t sd = c.call(kNote2PitchWrap, {c.r8(c.r32(v + 0x60) + 0xb), 0, note & 0xffff,
static_cast<uint32_t>(static_cast<int32_t>(static_cast<int16_t>(pitch)))});
const uint32_t id = c.r8(v + 0x3c) | (c.r8(v + 0x3d) << 1) | 0x200;
c.call(kSetParamWrap, {id, ((sd & 0xffff) * c.r32(v + 0x38)) >> 16});
}
// A pitch bend on the voice: the sample set's range up (+8) or down (+6) x bend / 8192 (+0x30).
void voiceBend(Call &c, uint32_t v, uint32_t bend)
{
const int32_t b = static_cast<int16_t>(bend);
const uint32_t range = b >= 0 ? c.r16(c.r32(v + 0x5c) + 8) : c.r16(c.r32(v + 0x5c) + 6);
c.w32(v + 0x30, static_cast<uint32_t>(shiftDown(static_cast<int32_t>(range * static_cast<uint32_t>(b)), 13)));
}
// The voice's volume (0x51e8c): its level (+0x18) x the port's master (+0x50 -> 16-bit) / 256, by the
// amplitude LFO (+0xa4, depth up +0x47 / down +0x48), then each side's (+0x44, +0x45) at most 0x3fff; set when
// it changed (+0x1a, +0x1c), inverted (negative, 15 bits) unless +0x4b.
void updateVolume(Call &c, uint32_t v)
{
int32_t level = shiftDown(static_cast<int32_t>(static_cast<uint32_t>(c.r16(v + 0x18)) * c.r16(c.r32(v + 0x50))), 8);
const int32_t lfo = c.s16(v + 0xa4);
const int32_t depth = lfo >= 0 ? c.s8(v + 0x47) : c.s8(v + 0x48);
const int32_t mod = shiftDown(static_cast<int32_t>(static_cast<uint32_t>(lfo) * static_cast<uint32_t>(depth)), 7) + 0x4000;
level = shiftDown(static_cast<int32_t>(static_cast<uint32_t>(level) * static_cast<uint32_t>(mod)), 14);
if (level < 0)
level = 0;
uint32_t left = (c.r8(v + 0x44) * static_cast<uint32_t>(level)) >> 7;
if (left >= 0x4000)
left = 0x3fff;
uint32_t right = (c.r8(v + 0x45) * static_cast<uint32_t>(level)) >> 7;
if (right >= 0x4000)
right = 0x3fff;
if (left == c.r16(v + 0x1a) && right == c.r16(v + 0x1c))
return;
const uint32_t id = c.r8(v + 0x3c) | (c.r8(v + 0x3d) << 1);
c.w16(v + 0x1a, left);
c.w16(v + 0x1c, right);
if (c.s8(v + 0x4b))
{
c.call(kSetParamWrap, {id, left & 0xffff});
c.call(kSetParamWrap, {id | 0x100, right & 0xffff});
}
else
{
c.call(kSetParamWrap, {id, (0u - left) & 0x7fff});
c.call(kSetParamWrap, {id | 0x100, (0u - right) & 0x7fff});
}
}
// The voice's level (+0x18): the channel's volume (+0x2b) x its expression copy (+0x4c) x its own (+0x16) >> 14.
void voiceLevels(Call &c, uint32_t v)
{
const uint32_t ch = c.r32(v + 0x54);
c.w16(v + 0x18, (c.r8(ch + 0x2b) * c.r8(v + 0x4c) * static_cast<uint32_t>(c.r16(v + 0x16))) >> 14);
}
// A key-follow amount (0x52038): the velocity curve (0x5447c) for the voice's velocity (+0x40) through two
// points, scaled by amount / 65536 (rounded toward zero).
constexpr uint32_t kCurve = 0x5447c;
uint32_t keyFollow(Call &c, uint32_t v, uint32_t amount, uint32_t a, uint32_t b)
{
const uint32_t state = c.r32(c.r32(v + 0x54) + 0x1c);
const uint32_t r = c.call(kCurve, {c.r8(v + 0x40), a & 0xff, b & 0xff, c.r32(state + 0x10), c.r32(state + 0xc)});
return static_cast<uint32_t>(shiftDown(static_cast<int32_t>(r * static_cast<uint32_t>(static_cast<int32_t>(static_cast<int8_t>(amount)))), 16));
}
int32_t clampTo(int32_t v, int32_t lo, int32_t hi) { return v > hi ? hi : v < lo ? lo : v; }
// The amplitude LFO depths (+0x47 up, +0x48 down: the split's +0x22 / +0x23 x value / 128, plus the voice's
// +0x49 / +0x4a, 8-bit) and the pitch LFO depths (+0x20 / +0x22: split +0x1c / +0x1e x value / 128 plus +0x24 /
// +0x26, 16-bit) for a channel level of value.
void voiceAmplitudeDepth(Call &c, uint32_t v, uint32_t value)
{
const uint32_t split = c.r32(v + 0x58);
const int32_t up = shiftDown(static_cast<int32_t>((value & 0xff) * static_cast<uint32_t>(static_cast<int32_t>(c.s8(split + 0x22)))), 7) + c.s8(v + 0x49);
c.w8(v + 0x47, static_cast<uint32_t>(clampTo(up, -0x80, 0x7f)));
const int32_t down = shiftDown(static_cast<int32_t>((value & 0xff) * static_cast<uint32_t>(static_cast<int32_t>(c.s8(split + 0x23)))), 7) + c.s8(v + 0x4a);
c.w8(v + 0x48, static_cast<uint32_t>(clampTo(down, -0x80, 0x7f)));
}
void voicePitchDepth(Call &c, uint32_t v, uint32_t value)
{
const uint32_t split = c.r32(v + 0x58);
const int32_t up = shiftDown(static_cast<int32_t>((value & 0xff) * static_cast<uint32_t>(static_cast<int32_t>(c.s16(split + 0x1c)))), 7) + c.s16(v + 0x24);
c.w16(v + 0x20, static_cast<uint32_t>(clampTo(up, -0x8000, 0x7fff)));
const int32_t down = shiftDown(static_cast<int32_t>((value & 0xff) * static_cast<uint32_t>(static_cast<int32_t>(c.s16(split + 0x1e)))), 7) + c.s16(v + 0x26);
c.w16(v + 0x22, static_cast<uint32_t>(clampTo(down, -0x8000, 0x7fff)));
}
// The voice's pan position (+0x1e: the split's +7, sample set's +0x11 and program's +0xd plus the key-follow
// offset, centred on 0x80 - 0x80): wrapped around the circle when the split surrounds (+0x46), else its
// magnitudes summed and clamped to 0..127 (the voice then in phase, +0x4b = 1).
void voicePanPosition(Call &c, uint32_t v, uint32_t offset)
{
int32_t splitPan = c.s8(c.r32(v + 0x58) + 7), setPan = c.s8(c.r32(v + 0x5c) + 0x11),
programPan = c.s8(c.r32(v + 0x60) + 0xd);
int32_t p;
if (c.r8(v + 0x46))
{
p = static_cast<int32_t>(static_cast<uint32_t>(splitPan - 0x80) + static_cast<uint32_t>(setPan + programPan) + offset);
while (p >= 0x80)
p -= 0xff;
while (p < -0x80)
p += 0xff;
}
else
{
if (splitPan < 0)
splitPan = -splitPan;
if (setPan < 0)
setPan = -setPan;
if (programPan < 0)
programPan = -programPan;
p = static_cast<int32_t>(static_cast<uint32_t>(splitPan - 0x80) + static_cast<uint32_t>(setPan + programPan) + offset);
if (p < 0)
p = 0;
else if (p >= 0x80)
p = 0x7f;
c.w8(v + 0x4b, 1);
}
c.w16(v + 0x1e, static_cast<uint32_t>(p));
}
// The voice's pan (0x522c8): each side's level (+0x44 left, +0x45 right, 0x80 each, centred) from its position
// (+0x1e) and the channel's offset (-63..63); none in mono (0x584f0). Surround splits flip the phase (+0x4b) when
// the sum crosses an edge.
void voicePan(Call &c, uint32_t v, uint32_t channelOffset)
{
c.w8(v + 0x45, 0x80);
c.w8(v + 0x44, 0x80);
if (c.r8(0x584f0))
return;
int32_t offset = static_cast<int8_t>(channelOffset);
if (offset >= 0x40)
offset = 0x3f;
else if (offset < -0x3f)
offset = -0x3f;
int32_t p;
if (c.r8(v + 0x46))
{
int32_t a = c.s16(v + 0x1e);
uint32_t phase = 1;
if (a < 0)
{
a = -a;
phase = 0;
}
a += static_cast<int8_t>(offset);
if (a < 0)
{
phase ^= 1;
a = -a;
}
else if (a >= 0x80)
{
phase ^= 1;
a = 0x100 - a;
}
c.w8(v + 0x4b, phase);
p = a - 0x40;
}
else
p = static_cast<int8_t>(offset) + c.s16(v + 0x1e) - 0x40;
auto per63 = [](int32_t x) { return x / 63; }; // (the module's 0x82082083 product: truncated)
if (p > 0)
c.w8(v + 0x44, p >= 0x3f ? 0 : static_cast<uint32_t>(per63((0x3f - p) << 7)));
else if (p < 0)
c.w8(v + 0x45, p < -0x3e ? 0 : static_cast<uint32_t>(per63((p + 0x3f) << 7)));
}
// The voice's gain from where its velocity (+0x40) lies in the program's velocity range (+2 low fade start,
// +3, +4 high fade end: bit 7 enables each fade) and its note (+0x3e) in the sample set's key range (the same
// three bytes): 0x10000 at full, fading linearly to the edges.
uint32_t divideTrap(uint32_t a, uint32_t b, uint32_t pc)
{
if (!b)
throw std::runtime_error("native IOP trap: division by zero in MODHSYN at " + std::to_string(pc));
return a / b;
}
uint32_t voiceGain(Call &c, uint32_t v)
{
const uint32_t program = c.r32(v + 0x60), set = c.r32(v + 0x5c);
uint32_t gain = 0x10000;
const uint32_t velocity = c.r8(v + 0x40);
if (velocity < c.r8(program + 3))
{
if (c.r8(program + 2) & 0x80)
{
uint32_t lo = c.r8(program + 2) & 0x7f;
const uint32_t hi = c.r8(program + 3);
if (lo != hi)
{
lo -= 1;
gain = divideTrap((velocity - lo) << 16, hi - lo, 0x524b0);
}
}
}
else if (c.r8(program + 4) & 0x80)
{
const uint32_t mid = c.r8(program + 3);
uint32_t hi = c.r8(program + 4) & 0x7f;
if (hi != mid)
{
hi += 1;
gain = divideTrap((hi - velocity) << 16, hi - mid, 0x524b0);
}
}
const uint32_t note = c.r8(v + 0x3e);
if (note < c.r8(set + 3))
{
if (c.r8(set + 2) & 0x80)
{
uint32_t lo = c.r8(set + 2) & 0x7f;
const uint32_t hi = c.r8(set + 3);
if (lo != hi)
{
lo -= 1;
gain = divideTrap(gain * (note - lo), hi - lo, 0x5255c);
}
}
}
else if (c.r8(set + 4) & 0x80)
{
const uint32_t mid = c.r8(set + 3);
uint32_t hi = c.r8(set + 4) & 0x7f;
if (hi != mid)
{
hi += 1;
gain = divideTrap(gain * (hi - note), hi - mid, 0x5255c);
}
}
return gain;
}
// The envelope pass (0x5256c) over each core's sounding voices (0x56130 + 8 x core, from the tail): a voice keyed
// off or released (+0x10 & 0x300) waits out its key-on delay (+0x43, counted down while ENDX shows it) or ends
// when ENDX has it and its envelope (ENVX) is 0; one still keyed ends at an envelope of 0. An ended voice is
// freed (0x53240) and its stat cleared; the others' envelope goes to the stat buffer, their portamento (+0x34 by
// +0x2a toward 0: +0x10 bit 2 up, bit 4 down) and LFOs (pitch +0x6c, amplitude +0x94) move, their pitch and
// volume are set again. The count of voices still sounding.
constexpr uint32_t kVoiceFree = 0x53240, kLfoTick = 0x547bc;
uint32_t envelopePass(Call &c)
{
uint32_t sounding = 0;
for (uint32_t core = 0; core < 2; ++core)
{
const uint32_t ended = c.call(kGetSwitch, {(core | 0x1700) & 0xffff});
const uint32_t head = 0x56130 + 8 * core;
for (uint32_t v = c.r32(head + 4); v != head; v = c.r32(v + 4))
{
const uint32_t flags = c.r16(v + 0x10);
if (c.r32(0x56144))
{
const uint32_t number = c.r8(v + 0x3d), bit = 1u << number;
uint32_t envelope = c.call(kGetParam, {((core | (number << 1)) | 0x500) & 0xffff});
bool finished;
if (flags & 0x300)
{
if (const uint32_t delay = c.r8(v + 0x43))
{
c.w8(v + 0x43, (ended & bit) ? delay - 1 : 0);
envelope = 0x7fff;
finished = false;
}
else
finished = (ended & bit) && !(envelope & 0xffff);
}
else
finished = !(envelope & 0xffff);
if (finished)
{
const uint32_t next = c.r32(v);
c.call(kVoiceFree, {v});
if (const uint32_t stats = c.r32(0x56140))
{
const uint32_t a = stats + c.r8(v + 0x3c) * 24 + 8 + c.r8(v + 0x3d);
c.w8(a, c.r8(a) & 0x8f);
}
v = next;
continue;
}
if (const uint32_t stats = c.r32(0x56140))
c.w16(stats + c.r8(v + 0x3d) * 2 + c.r8(v + 0x3c) * 48 + 0x38, envelope);
}
if (c.r32(v + 0x64))
{
if (flags & 6)
{
const uint32_t speed = c.r16(v + 0x2a);
if (flags & 2)
{
const int32_t offset = static_cast<int32_t>(c.r32(v + 0x34) + speed);
c.w32(v + 0x34, static_cast<uint32_t>(offset));
if (offset >= 0)
{
c.w32(v + 0x34, 0);
c.w16(v + 0x10, c.r16(v + 0x10) & 0xfffd);
}
}
else
{
const int32_t offset = static_cast<int32_t>(c.r32(v + 0x34) - speed);
c.w32(v + 0x34, static_cast<uint32_t>(offset));
if (offset <= 0)
{
c.w32(v + 0x34, 0);
c.w16(v + 0x10, c.r16(v + 0x10) & 0xfffb);
}
}
}
c.call(kLfoTick, {v + 0x6c});
updatePitch(c, v);
}
c.call(kLfoTick, {v + 0x94});
updateVolume(c, v);
++sounding;
}
}
c.w32(0x56144, 1);
return sounding;
}
// The voices' envelopes read into each voice (+0x12) and the finished ones freed (0x527f0, as the envelope pass
// decides it): 1 when one was freed.
uint32_t collectEnvelopes(Call &c)
{
uint32_t freed = 0;
for (uint32_t core = 0; core < 2; ++core)
{
const uint32_t ended = c.call(kGetSwitch, {(core | 0x1700) & 0xffff});
const uint32_t head = 0x56130 + 8 * core;
for (uint32_t v = c.r32(head + 4); v != head; v = c.r32(v + 4))
{
const uint32_t flags = c.r16(v + 0x10);
const uint32_t number = c.r8(v + 0x3d), bit = 1u << number;
const uint32_t envelope = c.call(kGetParam, {((core | (number << 1)) | 0x500) & 0xffff});
c.w16(v + 0x12, envelope);
bool finished;
if (flags & 0x300)
{
if (const uint32_t delay = c.r8(v + 0x43))
{
c.w8(v + 0x43, (ended & bit) ? delay - 1 : 0);
c.w16(v + 0x12, 0x7fff);
continue;
}
finished = (ended & bit) && !(envelope & 0xffff);
}
else
finished = !(envelope & 0xffff);
if (!finished)
continue;
const uint32_t next = c.r32(v);
c.call(kVoiceFree, {v});
if (const uint32_t stats = c.r32(0x56140))
{
const uint32_t a = stats + c.r8(v + 0x3c) * 24 + 8 + c.r8(v + 0x3d);
c.w8(a, c.r8(a) & 0x8f);
}
v = next;
freed = 1;
}
}
return freed;
}
// A portamento's start (0x5299c): the distance from the channel's portamento note (+0x33) to the voice's,
// in 1/128 semitones (+0x34; +0x10 bit 2 when it rises, bit 4 when it falls), and its speed (+0x2a): the
// distance x the tick / the channel's portamento time (+0x24), / 256.
void portamentoStart(Call &c, uint32_t v)
{
const uint32_t ch = c.r32(v + 0x54);
int32_t d = static_cast<int32_t>((c.r8(ch + 0x33) - c.r8(v + 0x3e)) << 7);
c.w32(v + 0x34, static_cast<uint32_t>(d));
if (d < 0)
{
d = -d;
c.w16(v + 0x10, c.r16(v + 0x10) | 2);
}
else
c.w16(v + 0x10, c.r16(v + 0x10) | 4);
const uint32_t x = divideTrap(static_cast<uint32_t>(d) * c.r32(kTickMsValue), c.r16(ch + 0x24), 0x52a04);
c.w16(v + 0x2a, static_cast<uint32_t>(shiftDown(static_cast<int32_t>(x), 8)));
}
// Whether 21 microseconds have passed since the last key on (0x584d0 seconds, 0x56120 microseconds), for a
// change of output mode (Sony's test: a key on exactly one second before counts as passed).
uint32_t keyOnSettled(Call &c)
{
iop::Locals t(c, 0x10);
c.call(kGetSystemTime, {t.base});
c.call(kSysClock2USec, {t.base, t.base + 8, t.base + 0xc});
const uint32_t seconds = c.r32(t.base + 8), saved = c.r32(0x584d0);
int32_t elapsed;
if (seconds == saved)
elapsed = static_cast<int32_t>(c.r32(t.base + 0xc) - c.r32(0x56120));
else
{
c.w32(t.base + 8, seconds - 1);
if (seconds - 1 == saved)
return 1;
elapsed = static_cast<int32_t>(c.r32(t.base + 0xc) + 1000000u - c.r32(0x56120));
}
c.w32(t.base + 0xc, static_cast<uint32_t>(elapsed));
return elapsed < 0x15 ? 0 : 1;
}
// A voice that never started given back (0x52cec): its flags cleared, the port's voice count (+0x50 -> +2) down,
// back on its core's free list (0x56100 + 8 x core, the free count 0x584d4 + core up), its stat cleared.
void dropVoice(Call &c, uint32_t v)
{
const uint32_t owner = c.r32(v + 0x50);
c.w16(v + 0x10, 0);
if (const uint32_t n = c.r8(owner + 2))
c.w8(owner + 2, n - 1);
listRemove(c, v);
const uint32_t core = c.r8(v + 0x3c);
c.w8(0x584d4 + core, c.r8(0x584d4 + core) + 1);
listInsertAfter(c, v, 0x56100 + 8 * c.r8(v + 0x3c));
if (const uint32_t stats = c.r32(0x56140))
{
const uint32_t a = stats + c.r8(v + 0x3c) * 24 + 8 + c.r8(v + 0x3d);
c.w8(a, c.r8(a) & 0x8f);
}
}
// A note off on a channel (0x52ad4: note, id; 0xff any): the portamento note follows it (while unset), then
// each of the channel's playing voices for that note (list -8, from its tail): held by the sustain pedal (+0x32:
// moved to the sustained list, +0x10 |= 0x100), dropped when it never started (+0x10 bit 8), else released: its
// LFOs' release, KOFF (0x56148 + 4 x core), moved to the released list (+0x10), its stat 0x30. A key off of the
// voice keyed on last waits until the key on has settled (21 microseconds, 0x52a30).
constexpr uint32_t kLfoRelease = 0x54d08;
void noteOff(Call &c, uint32_t ch, uint32_t note, uint32_t id)
{
if (c.r8(ch + 0x33) < 0x81)
c.w8(ch + 0x33, note);
id &= 0xff;
const uint32_t head = ch - 8;
for (uint32_t v = c.r32(head + 0xc); v != head; v = c.r32(v + 0xc))
{
if (c.r8(v + 0x3e) != (note & 0xff) || (c.r8(v + 0x3f) != id && id != 0xff))
continue;
const uint32_t next = c.r32(v + 8);
channelListRemove(c, v);
const uint32_t flags = c.r16(v + 0x10);
c.w16(v + 0x10, flags & 0xfcff);
if (c.r8(ch + 0x32))
{
c.w16(v + 0x10, (flags & 0xfcff) | 0x100);
channelListPush(c, v, ch + 8);
}
else if (flags & 8)
dropVoice(c, v);
else
{
c.call(kLfoRelease, {v + 0x6c});
c.call(kLfoRelease, {v + 0x94});
if (c.r8(0x56170) == c.r8(v + 0x3c) && c.r8(0x56171) == c.r8(v + 0x3d))
{
// (The module polls; the IOP's clock does not move inside a call, so a first "not yet" would
// poll forever: the host's work budget stops the compiled module there.)
if (!keyOnSettled(c))
throw std::runtime_error("MODHSYN: a key off waits for a key on that cannot settle");
c.w8(0x56171, 0xff);
}
const uint32_t off = 0x56148 + 4 * c.r8(v + 0x3c);
c.w32(off, c.r32(off) | (1u << c.r8(v + 0x3d)));
c.call(kSetSwitchWrap, {c.r8(v + 0x3c) | 0x1600, c.r32(0x56148 + 4 * c.r8(v + 0x3c))});
channelListPush(c, v, ch + 0x10);
if (const uint32_t stats = c.r32(0x56140))
{
const uint32_t a = stats + c.r8(v + 0x3c) * 24 + 8 + c.r8(v + 0x3d);
c.w8(a, (c.r8(a) & 0x8f) | 0x30);
}
}
v = next;
}
}
// A voice keyed off (after its LFOs' release where the caller does it): the key on settled first when it was the
// last one, KOFF, onto the channel's released list (ch + 0x10), its stat 0x30.
void keyOffVoice(Call &c, uint32_t v, uint32_t ch)
{
if (c.r8(0x56170) == c.r8(v + 0x3c) && c.r8(0x56171) == c.r8(v + 0x3d))
{
if (!keyOnSettled(c))
throw std::runtime_error("MODHSYN: a key off waits for a key on that cannot settle");
c.w8(0x56171, 0xff);
}
const uint32_t off = 0x56148 + 4 * c.r8(v + 0x3c);
c.w32(off, c.r32(off) | (1u << c.r8(v + 0x3d)));
c.call(kSetSwitchWrap, {c.r8(v + 0x3c) | 0x1600, c.r32(0x56148 + 4 * c.r8(v + 0x3c))});
channelListPush(c, v, ch + 0x10);
c.w16(v + 0x10, c.r16(v + 0x10) & 0xfcff);
if (const uint32_t stats = c.r32(0x56140))
{
const uint32_t a = stats + c.r8(v + 0x3c) * 24 + 8 + c.r8(v + 0x3d);
c.w8(a, (c.r8(a) & 0x8f) | 0x30);
}
}
// The sustain pedal (CC 64): on, kept; off, every sustained voice (list +0) released (or dropped when it never
// started).
void sustain(Call &c, uint32_t ch, uint32_t value)
{
if (value & 0xff)
{
c.w8(ch + 0x32, value);
return;
}
if (!c.r8(ch + 0x32))
return;
c.w8(ch + 0x32, value);
for (uint32_t v = c.r32(ch + 8); v != ch; v = c.r32(ch + 8))
{
channelListRemove(c, v);
if (c.r16(v + 0x10) & 8)
{
dropVoice(c, v);
continue;
}
c.call(kLfoRelease, {v + 0x6c});
c.call(kLfoRelease, {v + 0x94});
keyOffVoice(c, v, ch);
}
}
// All notes off (CC 123): every playing and sustained voice of the channel keyed off (no LFO release), those
// that never started dropped.
void allNotesOff(Call &c, uint32_t ch)
{
for (const uint32_t head : {ch - 8, ch})
for (uint32_t v = c.r32(head + 0xc); v != head; v = c.r32(head + 0xc))
{
channelListRemove(c, v);
const uint32_t flags = c.r16(v + 0x10);
if (flags & 8)
{
dropVoice(c, v);
continue;
}
c.w16(v + 0x10, flags & 0xfcff);
keyOffVoice(c, v, ch);
}
}
// A voice freed (0x53240): off its channel list, then as dropVoice.
void voiceFree(Call &c, uint32_t v)
{
channelListRemove(c, v);
dropVoice(c, v);
}
// A voice cut at once: the key on settled when it was the last, KOFF, ADSR2 0xc021 (the fastest release),
// freed.
void cutVoice(Call &c, uint32_t v)
{
if (c.r8(0x56170) == c.r8(v + 0x3c) && c.r8(0x56171) == c.r8(v + 0x3d))
{
if (!keyOnSettled(c))
throw std::runtime_error("MODHSYN: a key off waits for a key on that cannot settle");
c.w8(0x56171, 0xff);
}
const uint32_t off = 0x56148 + 4 * c.r8(v + 0x3c);
c.w32(off, c.r32(off) | (1u << c.r8(v + 0x3d)));
c.call(kSetSwitchWrap, {c.r8(v + 0x3c) | 0x1600, c.r32(0x56148 + 4 * c.r8(v + 0x3c))});
c.call(kSetParamWrap, {(c.r8(v + 0x3c) | (c.r8(v + 0x3d) << 1)) | 0x400, 0xc021});
voiceFree(c, v);
}
// All sound off (CC 120): every voice of the channel's three lists cut.
void allSoundOff(Call &c, uint32_t ch)
{
for (const uint32_t head : {ch - 8, ch, ch + 8})
for (uint32_t v = c.r32(head + 0xc); v != head; v = c.r32(head + 0xc))
cutVoice(c, v);
}
// A voice stolen for a new note of priority (0x53310): the lowest priority (+0x14) at most priority, among the
// released voices of every channel first, then the playing and sustained ones; cut. 1 when one was.
uint32_t stealVoice(Call &c, uint32_t banks, uint32_t priority)
{
uint32_t chosen = 0;
auto scan = [&](uint32_t head) {
for (uint32_t v = c.r32(head + 8); v != head; v = c.r32(v + 8))
{
const uint32_t p = c.r16(v + 0x14);
if (!((priority & 0xffff) < p))
{
chosen = v;
priority = p;
}
}
};
for (uint32_t ch = 0; ch < 16; ++ch)
scan(banks + 4 + 0x38 * ch + 8);
if (!chosen)
{
for (uint32_t ch = 0; ch < 16; ++ch)
{
scan(banks + 4 + 0x38 * ch - 8);
scan(banks + 4 + 0x38 * ch);
}
if (!chosen)
return 0;
}
cutVoice(c, chosen);
return 1;
}
// The voices of an exclusive group (+0x41) cut on a channel (0x53dd0): those that never started dropped, the
// playing and sustained ones keyed off, every one given the fastest release and freed.
void exclusiveCut(Call &c, uint32_t ch, uint32_t group)
{
const uint32_t heads[3] = {ch - 8, ch, ch + 8};
for (uint32_t list = 0; list < 3; ++list)
{
const uint32_t head = heads[list];
for (uint32_t v = c.r32(head + 0xc); v != head; v = c.r32(v + 0xc))
{
if (c.r8(v + 0x41) != (group & 0xff))
continue;
const uint32_t next = c.r32(v + 8);
if (c.r16(v + 0x10) & 8)
{
channelListRemove(c, v);
dropVoice(c, v);
v = next;
continue;
}
if (list < 2)
{
if (c.r8(0x56170) == c.r8(v + 0x3c) && c.r8(0x56171) == c.r8(v + 0x3d))
{
if (!keyOnSettled(c))
throw std::runtime_error("MODHSYN: a key off waits for a key on that cannot settle");
c.w8(0x56171, 0xff);
}
const uint32_t off = 0x56148 + 4 * c.r8(v + 0x3c);
c.w32(off, c.r32(off) | (1u << c.r8(v + 0x3d)));
c.call(kSetSwitchWrap, {c.r8(v + 0x3c) | 0x1600, c.r32(0x56148 + 4 * c.r8(v + 0x3c))});
}
c.call(kSetParamWrap, {(c.r8(v + 0x3c) | (c.r8(v + 0x3d) << 1)) | 0x400, 0xc021});
voiceFree(c, v);
v = next;
}
}
}
// --- The velocity curves (0x5447c: Sony's eleven, through an optional remapping table of the port's). Each
// maps a value and a point (both 1..127) to a signed 16.16 amount: linear up / down (0, 1), squared (2..5),
// linear (6, 7) or squared (8, 9) from the point, the value itself (10).
int32_t sdivTrap(int32_t a, int32_t b, uint32_t pc)
{
if (!b)
throw std::runtime_error("native IOP trap: division by zero in MODHSYN at " + std::to_string(pc));
if (b == -1 && a == INT32_MIN)
throw std::runtime_error("native IOP trap: division overflow in MODHSYN at " + std::to_string(pc));
return a / b;
}
int32_t per126(int32_t x) { return x / 126; } // (the 0x82082083 product, >> 6: truncated)
int32_t sq14(int32_t x) { return shiftDown(static_cast<int32_t>(static_cast<uint32_t>(x) * static_cast<uint32_t>(x)), 14); }
uint32_t curveValue(uint32_t kind, uint32_t a, uint32_t b)
{
const int32_t va = static_cast<int32_t>(a & 0xff), vb = static_cast<int32_t>(b & 0xff);
switch (kind)
{
case 0: return static_cast<uint32_t>(per126((va - vb) << 16));
case 1: return static_cast<uint32_t>(per126((vb - va) << 16));
case 2:
{
const int32_t x = per126((va - 1) << 15), y = per126((vb - 1) << 15);
return static_cast<uint32_t>(shiftDown(static_cast<int32_t>(static_cast<uint32_t>(x) * static_cast<uint32_t>(x) - static_cast<uint32_t>(y) * static_cast<uint32_t>(y)), 14));
}
case 3:
{
const int32_t x = sq14(per126((va - 1) << 15)), y = sq14(per126((vb - 1) << 15));
return static_cast<uint32_t>(0x10000 - x) - static_cast<uint32_t>(0x10000 - y);
}
case 4:
{
const int32_t x = sq14(0x10000 - per126((va - 1) << 15)), y = sq14(0x10000 - per126((vb - 1) << 15));
return static_cast<uint32_t>(0x10000 - x) - static_cast<uint32_t>(0x10000 - y);
}
case 5:
{
const int32_t x = sq14(0x10000 - per126((va - 1) << 15)), y = sq14(0x10000 - per126((vb - 1) << 15));
return static_cast<uint32_t>(x) - static_cast<uint32_t>(y);
}
case 6:
case 7:
{
if (va == vb)
return 0;
const int32_t d = vb < va ? 0x7f - vb : vb - 1;
return static_cast<uint32_t>(sdivTrap(kind == 6 ? (va - vb) << 16 : (vb - va) << 16, d, kind == 6 ? 0x54244 : 0x542b0));
}
case 8:
{
if (va == vb)
return 0;
const int32_t x = (va - vb) << 15;
if (vb < va)
return static_cast<uint32_t>(sq14(sdivTrap(x, 0x7f - vb, 0x5430c)));
return static_cast<uint32_t>(-sq14(sdivTrap(x, vb - 1, 0x5435c)));
}
case 9:
{
if (va == vb)
return 0;
const int32_t x = (va - vb) << 15;
if (vb < va)
return static_cast<uint32_t>(0x10000 - sq14(0x8000 - sdivTrap(x, 0x7f - vb, 0x543d8)));
return static_cast<uint32_t>(sq14(sdivTrap(x, vb - 1, 0x54430) + 0x8000)) + 0xffff0000u;
}
default: return a & 0xff;
}
}
uint32_t curve(Call &c, uint32_t value, uint32_t point, uint32_t type, uint32_t tables, uint32_t count)
{
const int32_t table = static_cast<int32_t>((type & 0xff) >> 4) - 1;
if (table != -1 && table < static_cast<int32_t>(count))
{
const uint32_t t = tables + static_cast<uint32_t>(table) * 126;
value = c.r8(t + (value & 0xff) - 1);
point = c.r8(t + (point & 0xff) - 1);
}
uint32_t kind = type & 0xf;
if (kind >= 11)
kind = 0;
return curveValue(kind, value & 0xff, point & 0xff);
}
// --- libsd through the module's command log (export 16 sets it, 0x560f0: +0 its entries, +8 the next one, 28
// bytes each from +0xc: +0 the command | 0x80000000 while running, +4 the result, +8.. its arguments).
constexpr uint32_t kCommandLog = 0x560f0;
void setCommandLog(Call &c)
{
const uint32_t log = c.arg(0);
c.w32(kCommandLog, log);
if (log)
{
c.w32(log + 8, 0);
c.w32(log + 4, 0);
}
c.ret(0);
}
uint32_t logNext(Call &c)
{
const uint32_t log = c.r32(kCommandLog);
if (!log)
return 0;
const uint32_t i = c.r32(log + 8), entry = log + i * 28 + 0xc;
c.w32(log + 8, i + 1);
if (!(i + 1 < c.r32(log)))
c.w32(log + 8, 0);
return entry;
}
void logWrite(Call &c, uint32_t entry, uint32_t command, uint32_t a, uint32_t b, uint32_t d, uint32_t e, uint32_t f)
{
if (!entry)
return;
c.w32(entry, command | 0x80000000u);
c.w32(entry + 8, a);
c.w32(entry + 0xc, b);
c.w32(entry + 0x10, d);
c.w32(entry + 0x14, e);
c.w32(entry + 0x18, f);
}
void logDone(Call &c, uint32_t entry, uint32_t result)
{
if (!entry)
return;
c.w32(entry + 4, result);
c.w32(entry, c.r32(entry) & 0x7fffffff);
}
// A libsd call logged: command, the stub, its arguments (as the module masks them), the result kept or 0.
uint32_t sd(Call &c, uint32_t command, uint32_t stub, std::initializer_list<uint32_t> args, bool keepResult,
uint32_t mask = 0xffffffffu)
{
uint32_t logged[5] = {0, 0, 0, 0, 0};
uint32_t n = 0;
for (const uint32_t v : args)
if (n < 5)
logged[n++] = v;
const uint32_t entry = logNext(c);
logWrite(c, entry, command, logged[0], logged[1], logged[2], logged[3], logged[4]);
const uint32_t r = c.call(stub, args) & mask;
logDone(c, entry, keepResult ? r : 0);
return r;
}
uint32_t s16arg(uint32_t v) { return static_cast<uint32_t>(static_cast<int32_t>(static_cast<int16_t>(v))); }
constexpr uint32_t kSdInit = 0x55a44, kSdProcBatch = 0x55a94, kSdProcBatchEx = 0x55a9c, kSdVoiceTrans = 0x55aa4,
kSdBlockTrans = 0x55aac, kSdVoiceTransStatus = 0x55ab4, kSdBlockTransStatus = 0x55abc,
kSdSetTransCallback = 0x55ac4, kSdSetIRQCallback = 0x55acc, kSdSetEffectAttr = 0x55ad4,
kSdGetEffectAttr = 0x55adc, kSdClearEffectWorkArea = 0x55ae4, kSdPitch2Note = 0x55a8c;
void bindLibsdWrappers(bozo::native::irx::CompiledLibraries &host)
{
iop::bind(host, 0x54df0, setCommandLog);
iop::bind(host, 0x54e0c, [](Call &c) { c.ret(logNext(c)); });
iop::bind(host, 0x54e64, [](Call &c) { logWrite(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3), c.arg(4), c.arg(5), c.arg(6)); c.ret(0); });
iop::bind(host, 0x54ea8, [](Call &c) { logDone(c, c.arg(0), c.arg(1)); c.ret(0); });
iop::bind(host, 0x54ecc, [](Call &c) { c.ret(sd(c, 0, kSdInit, {c.arg(0)}, true)); });
iop::bind(host, 0x54f38, [](Call &c) { sd(c, 1, kSdSetParam, {c.arg(0) & 0xffff, c.arg(1) & 0xffff}, false); c.ret(0); });
iop::bind(host, 0x54fb4, [](Call &c) { c.ret(sd(c, 2, kSdGetParam, {c.arg(0) & 0xffff}, true, 0xffff)); });
iop::bind(host, 0x55024, [](Call &c) { sd(c, 3, kSdSetSwitch, {c.arg(0) & 0xffff, c.arg(1)}, false); c.ret(0); });
iop::bind(host, 0x5509c, [](Call &c) { c.ret(sd(c, 4, kSdGetSwitch, {c.arg(0) & 0xffff}, true)); });
iop::bind(host, 0x5510c, [](Call &c) { sd(c, 5, kSdSetAddr, {c.arg(0) & 0xffff, c.arg(1)}, false); c.ret(0); });
iop::bind(host, 0x55184, [](Call &c) { c.ret(sd(c, 6, kSdGetAddr, {c.arg(0) & 0xffff}, true)); });
iop::bind(host, 0x551f4, [](Call &c) { sd(c, 7, kSdSetCoreAttr, {c.arg(0) & 0xffff, c.arg(1) & 0xffff}, false); c.ret(0); });
iop::bind(host, 0x55270, [](Call &c) { c.ret(sd(c, 9, kSdNote2Pitch, {c.arg(0) & 0xffff, c.arg(1) & 0xffff, c.arg(2) & 0xffff, s16arg(c.arg(3))}, true, 0xffff)); });
iop::bind(host, 0x55320, [](Call &c) { c.ret(sd(c, 0xa, kSdPitch2Note, {c.arg(0) & 0xffff, c.arg(1) & 0xffff, c.arg(2) & 0xffff}, true, 0xffff)); });
iop::bind(host, 0x553b8, [](Call &c) { c.ret(sd(c, 0xb, kSdProcBatch, {c.arg(0), c.arg(1), c.arg(2)}, true)); });
iop::bind(host, 0x55444, [](Call &c) { c.ret(sd(c, 0xc, kSdProcBatchEx, {c.arg(0), c.arg(1), c.arg(2), c.arg(3)}, true)); });
iop::bind(host, 0x554e0, [](Call &c) { c.ret(sd(c, 0xd, kSdVoiceTrans, {s16arg(c.arg(0)), c.arg(1) & 0xffff, c.arg(2), c.arg(3), c.arg(4)}, true)); });
iop::bind(host, 0x55598, [](Call &c) { c.ret(sd(c, 0xe, kSdBlockTrans, {s16arg(c.arg(0)), c.arg(1) & 0xffff, c.arg(2), c.arg(3)}, true)); });
iop::bind(host, 0x55640, [](Call &c) { c.ret(sd(c, 0xf, kSdVoiceTransStatus, {s16arg(c.arg(0)), s16arg(c.arg(1))}, true)); });
iop::bind(host, 0x556cc, [](Call &c) { c.ret(sd(c, 0x10, kSdBlockTransStatus, {s16arg(c.arg(0)), s16arg(c.arg(1))}, true)); });
iop::bind(host, 0x55758, [](Call &c) { c.ret(sd(c, 0x11, kSdSetTransCallback, {c.arg(0) & 0xffff, c.arg(1)}, true)); });
iop::bind(host, 0x557d8, [](Call &c) { c.ret(sd(c, 0x12, kSdSetIRQCallback, {c.arg(0)}, true)); });
iop::bind(host, 0x55844, [](Call &c) { c.ret(sd(c, 0x13, kSdSetEffectAttr, {c.arg(0), c.arg(1)}, true)); });
iop::bind(host, 0x558c0, [](Call &c) { sd(c, 0x14, kSdGetEffectAttr, {c.arg(0), c.arg(1)}, false); c.ret(0); });
iop::bind(host, 0x55934, [](Call &c) { c.ret(sd(c, 0x15, kSdClearEffectWorkArea, {c.arg(0), c.arg(1), c.arg(2)}, true)); });
}
// --- Velocity: Sony's curves (0x545f4: through an optional remapping table, then one of: as is, inverted,
// squared, inverted square, and their mirrors).
uint32_t velocitySquare(uint32_t a)
{
const uint32_t x = (a & 0xff) * (a & 0xff);
const uint32_t q = x / 127; // (the 0x02040811 product)
return q ? q & 0xff : 1;
}
uint32_t velocityKind(uint32_t kind, uint32_t a)
{
switch (kind)
{
case 0: case 6: return a & 0xff;
case 1: case 7: return (0x80 - a) & 0xff;
case 2: case 8: return velocitySquare(a);
case 3: case 9: return (0x80 - velocitySquare(a & 0xff)) & 0xff;
case 4: return (0x80 - velocitySquare((0x80 - a) & 0xff)) & 0xff;
case 5: return velocitySquare((0x80 - a) & 0xff) & 0xff;
default: throw std::runtime_error("MODHSYN: velocity curve 10 points at data (0x4050000)");
}
}
uint32_t velocity(Call &c, uint32_t value, uint32_t type, uint32_t tables, uint32_t count)
{
const int32_t table = static_cast<int32_t>((type & 0xff) >> 4) - 1;
if (table != -1 && table < static_cast<int32_t>(count))
value = c.r8(tables + static_cast<uint32_t>(table) * 126 + (value & 0xff) - 1);
uint32_t kind = type & 0xf;
if (kind >= 11)
kind = 0;
return velocityKind(kind, value & 0xff) & 0xff;
}
// --- LFOs (0x28 bytes: +0 state (0 off, 1 delay, 2 fade in, 3 running, 4 release delay, 5 fade out, 6 done),
// +1 releases with the note, +2 the delay in ticks, +4 its counter, +8 the fade level, +0xc its step, +0x10 the
// output, +0x14 the wave (its function's IOP address), +0x18 the phase (16 bits), +0x1c its step, +0x20 /
// +0x24 a wave table and its size).
constexpr uint32_t kRandomSeed = 0x560e0;
constexpr uint32_t kWaveSawUp = 0x54680, kWaveSawDown = 0x54698, kWaveTriangle = 0x546b8, kWaveSquare = 0x546f4,
kWaveTable = 0x54714, kWaveRandom = 0x54788, kSineTable = 0x55f50;
uint32_t lfoRandom(Call &c)
{
const uint32_t seed = c.r32(kRandomSeed) * 0x41c64e6du + 0x3039;
c.w32(kRandomSeed, seed);
return static_cast<uint32_t>(static_cast<int32_t>(seed) >> 16);
}
uint32_t s16of(uint32_t x) { return static_cast<uint32_t>(static_cast<int32_t>(static_cast<int16_t>(x))); }
uint32_t lfoWaveAt(Call &c, uint32_t fn, uint32_t lfo, uint32_t wrapped)
{
const uint32_t phase = c.r32(lfo + 0x18);
switch (fn)
{
case kWaveSawUp: return static_cast<uint32_t>(static_cast<int32_t>((phase - 0x8000) << 15) >> 16);
case kWaveSawDown: return static_cast<uint32_t>(static_cast<int32_t>((0xffff - phase - 0x8000) << 15) >> 16);
case kWaveTriangle:
{
int32_t p = static_cast<int32_t>(phase);
if (p > 0xbfff)
p -= 0x10000;
else if (p > 0x4000)
p = 0x8000 - p;
return s16of(static_cast<uint32_t>(p));
}
case kWaveSquare: return static_cast<int32_t>(phase) > 0x7fff ? 0x4000 : static_cast<uint32_t>(-0x4000);
case kWaveRandom:
return wrapped ? static_cast<uint32_t>(static_cast<int32_t>(lfoRandom(c) << 16) >> 18) : s16of(c.r16(lfo + 0x10));
case kWaveTable:
{
const uint32_t size = c.r16(lfo + 0x24), table = c.r32(lfo + 0x20);
const uint32_t x = phase * size;
uint32_t i = x >> 16;
int32_t a = c.s16(table + 2 * i);
++i;
if (!(i < size))
i = 0;
const int32_t d = c.s16(table + 2 * i) - a;
a += static_cast<int32_t>(static_cast<uint32_t>(d) * (x & 0xffff)) >> 16;
return static_cast<uint32_t>(static_cast<int32_t>((static_cast<uint32_t>(a) + (static_cast<uint32_t>(a) >> 31)) << 15) >> 16);
}
default: return c.call(fn, {lfo, wrapped});
}
}
uint32_t lfoWave(Call &c, uint32_t lfo, uint32_t wrapped) { return lfoWaveAt(c, c.r32(lfo + 0x14), lfo, wrapped); }
// The LFO's start (0x5497c): its delay, else its fade in, else running.
void lfoStart(Call &c, uint32_t lfo)
{
if (const uint32_t delay = c.r16(lfo + 2))
{
c.w8(lfo, 1);
c.w16(lfo + 4, delay);
return;
}
if (c.r32(lfo + 0xc))
{
c.w8(lfo, 2);
c.w32(lfo + 8, 0);
}
else
c.w8(lfo, 3);
}
// A tick (0x547bc): the phase moves (wrapping at 0x10000) once past the delay, the wave read, the delay and
// fades counted; the output kept (+0x10) and returned (16-bit).
uint32_t lfoTick(Call &c, uint32_t lfo)
{
const uint32_t state = c.r8(lfo);
uint32_t out = 0;
if (state && state < 6)
{
if (state >= 2)
{
uint32_t phase = c.r32(lfo + 0x18) + c.r32(lfo + 0x1c), wrapped = 0;
if (static_cast<int32_t>(phase) > 0xffff)
{
phase -= 0x10000;
wrapped = 1;
}
c.w32(lfo + 0x18, phase);
out = lfoWave(c, lfo, wrapped);
}
switch (state)
{
case 1:
{
const uint32_t n = (c.r16(lfo + 4) - 1) & 0xffff;
c.w16(lfo + 4, n);
if (n)
break;
if (c.r32(lfo + 0xc))
{
c.w32(lfo + 8, 0);
c.w8(lfo, 2);
}
else
c.w8(lfo, 3);
break;
}
case 2:
{
const uint32_t level = c.r32(lfo + 8);
int32_t x = static_cast<int32_t>(s16of(out) * level);
if (x < 0)
x += 0xffff;
out = static_cast<uint32_t>(x) >> 16;
const uint32_t next = level + c.r32(lfo + 0xc);
c.w32(lfo + 8, next);
if (static_cast<int32_t>(next) > 0xffff)
c.w8(lfo, 3);
break;
}
case 4:
{
const uint32_t n = (c.r16(lfo + 4) - 1) & 0xffff;
c.w16(lfo + 4, n);
if (n)
break;
if (c.r32(lfo + 0xc))
{
c.w32(lfo + 8, 0x10000);
c.w8(lfo, 5);
}
else
c.w8(lfo, 6);
break;
}
case 5:
{
const uint32_t level = c.r32(lfo + 8);
int32_t x = static_cast<int32_t>(s16of(out) * level);
if (x < 0)
x += 0xffff;
out = static_cast<uint32_t>(x) >> 16;
const int32_t next = static_cast<int32_t>(level - c.r32(lfo + 0xc));
c.w32(lfo + 8, static_cast<uint32_t>(next));
if (next <= 0)
c.w8(lfo, 6);
break;
}
default: break;
}
}
c.w16(lfo + 0x10, out);
return s16of(out);
}
// The note's release (0x54d08) for an LFO that releases: before its delay ends it is done; while it fades in
// or runs, its release delay, else its fade out, else done; not started yet, it starts.
void lfoRelease(Call &c, uint32_t lfo)
{
if (!c.r8(lfo + 1))
return;
const uint32_t state = c.r8(lfo);
if (state == 1 || state >= 4)
{
c.w8(lfo, 6);
return;
}
if (state == 0)
{
lfoStart(c, lfo);
return;
}
if (const uint32_t delay = c.r16(lfo + 2))
{
c.w8(lfo, 4);
c.w16(lfo + 4, delay);
}
else if (c.r32(lfo + 0xc))
{
c.w8(lfo, 5);
c.w32(lfo + 8, 0x10000);
}
else
c.w8(lfo, 6);
}
// An LFO set up for a note (0x549c4: lfo, program, the port's wave tables and their count, type (the
// program's +0x28 bits: 0x77 any, 0x66 releases, 0x55 starts now), delay and fade times (milliseconds x 256 in
// ticks), the tick, 0 pitch / 1 amplitude): its wave (the program's +0xe / +0xf: built in 1..6, bit 7 a table
// of the port's with id 0x80), its phase (+0x10 / +0x11 plus a random part of +0x12 / +0x13), its rate (+0x14
// / +0x16).
void lfoInit(Call &c, uint32_t lfo, uint32_t program, uint32_t tables, uint32_t count, uint32_t type,
uint32_t delayMs, uint32_t fadeMs, uint32_t tick, uint32_t amplitude)
{
c.w8(lfo + 1, 0);
c.w8(lfo, 0);
c.w16(lfo + 0x10, 0);
c.w32(lfo + 0x20, 0);
if (!(type & 0x77))
return;
const uint32_t wave = c.r8(program + (amplitude ? 0xf : 0xe));
if (wave & 0x80)
{
for (int32_t i = 0; i < static_cast<int32_t>(count); ++i, tables += 8)
if (c.r8(tables) == 0x80)
{
c.w32(lfo + 0x20, c.r32(tables + 4));
c.w32(lfo + 0x14, kWaveTable);
c.w16(lfo + 0x24, c.r16(tables + 2));
break;
}
if (!c.r32(lfo + 0x20) && !c.r16(lfo + 0x24))
return;
}
else
{
switch (wave)
{
case 1: c.w32(lfo + 0x14, kWaveSawUp); break;
case 2: c.w32(lfo + 0x14, kWaveSawDown); break;
case 3: c.w32(lfo + 0x14, kWaveTriangle); break;
case 4: c.w32(lfo + 0x14, kWaveSquare); break;
case 5: c.w32(lfo + 0x14, kWaveRandom); break;
case 6:
c.w32(lfo + 0x20, kSineTable);
c.w16(lfo + 0x24, 200);
c.w32(lfo + 0x14, kWaveTable);
break;
default: return;
}
}
const uint32_t half = tick >> 1;
c.w16(lfo + 2, divideTrap(((delayMs & 0xffff) << 8) + half, tick, 0x54b5c));
uint32_t fade = divideTrap(((fadeMs & 0xffff) << 8) + half, tick, 0x54b78);
if (fade)
{
fade = divideTrap(0x10000, fade, 0x54b94);
if (!fade)
fade = 1;
}
c.w32(lfo + 0xc, fade);
const uint32_t base = amplitude ? 0x11 : 0x10, spread = amplitude ? 0x13 : 0x12, rate = amplitude ? 0x16 : 0x14;
c.w32(lfo + 0x18, c.r8(program + spread) << 8);
const uint32_t r = lfoRandom(c);
const int32_t x = static_cast<int32_t>(c.r32(lfo + 0x18) * s16of(r));
int32_t phase = shiftDown(x, 16);
c.w32(lfo + 0x18, static_cast<uint32_t>(phase));
phase = static_cast<int32_t>(static_cast<uint32_t>(phase) + (c.r8(program + base) << 8));
c.w32(lfo + 0x18, static_cast<uint32_t>(phase));
const uint32_t cycle = static_cast<uint32_t>(c.r16(program + rate)) << 8;
if (phase < 0)
c.w32(lfo + 0x18, static_cast<uint32_t>(phase) + 0x10000);
else if (phase > 0xffff)
c.w32(lfo + 0x18, static_cast<uint32_t>(phase) + 0xffff0000u);
const uint32_t ticks = divideTrap(cycle + half, tick, 0x54ca8);
c.w32(lfo + 0x1c, divideTrap(0x10000, ticks, 0x54cbc));
if (type & 0x66)
c.w8(lfo + 1, 1);
if (type & 0x55)
lfoStart(c, lfo);
}
// A free voice (0x53a50) on the core the sample's mixing asks (+0x29 & 0x30: 0x10 core 0, 0x20 core 1, else the
// core with more free voices, core 0 on a tie): the tail of its free list, the free count down. 0 with none.
uint32_t takeFree(Call &c, uint32_t mix)
{
const uint32_t m = mix & 0x30;
const uint32_t core = m == 0x10 ? 0 : m == 0x20 ? 1 : (c.r8(0x584d4) < c.r8(0x584d5) ? 1 : 0);
const uint32_t count = c.r8(0x584d4 + core);
if (!count)
return 0;
c.w8(0x584d4 + core, count - 1);
const uint32_t v = c.r32(0x56104 + 8 * core);
listRemove(c, v);
return v;
}
// The cores a sample's mixing allows (0x55ef0: 0x10 core 0, 0x20 core 1; none given: both).
bool coreAllowed(Call &c, uint32_t mix, uint32_t core)
{
const uint32_t m = (mix & 0x30) ? mix : 0x30;
return (m & c.r8(0x55ef0 + core)) != 0;
}
// A sounding voice taken over (0x53af4): among the allowed cores' sounding voices (core 1 first, each list from
// its tail), the least held (flags & 0x300: released before sustained before playing) of priority at most the
// note's, then the lowest priority, then the lowest envelope; keyed off and taken off its lists.
uint32_t takeReleased(Call &c, uint32_t priority, uint32_t mix)
{
uint32_t best = 0, bestHeld = 0x300, bestEnvelope = 0xffff, running = priority;
const uint32_t limit = priority & 0xffff;
for (int32_t core = 1; core >= 0; --core)
{
if (!coreAllowed(c, mix, static_cast<uint32_t>(core)))
continue;
const uint32_t head = 0x56130 + 8 * static_cast<uint32_t>(core);
for (uint32_t v = c.r32(head + 4); v != head; v = c.r32(v + 4))
{
const uint32_t held = c.r16(v + 0x10) & 0x300;
if (held < (bestHeld & 0xffff))
{
const uint32_t p = c.r16(v + 0x14);
if (limit < p)
continue;
running = p;
bestHeld = held;
bestEnvelope = c.r16(v + 0x12);
best = v;
}
else if (held == (bestHeld & 0xffff))
{
const uint32_t p = c.r16(v + 0x14), current = running & 0xffff;
if (p == current)
{
const uint32_t envelope = c.r16(v + 0x12);
if (!(envelope < bestEnvelope))
continue;
bestEnvelope = envelope;
best = v;
}
else if (p < current)
{
running = p;
bestEnvelope = c.r16(v + 0x12);
best = v;
}
}
}
}
if (!best)
return 0;
if (c.r8(0x56170) == c.r8(best + 0x3c) && c.r8(0x56171) == c.r8(best + 0x3d))
{
if (!keyOnSettled(c))
throw std::runtime_error("MODHSYN: a key off waits for a key on that cannot settle");
c.w8(0x56171, 0xff);
}
const uint32_t off = 0x56148 + 4 * c.r8(best + 0x3c);
c.w32(off, c.r32(off) | (1u << c.r8(best + 0x3d)));
c.call(kSetSwitchWrap, {c.r8(best + 0x3c) | 0x1600, c.r32(0x56148 + 4 * c.r8(best + 0x3c))});
const uint32_t owner = c.r32(best + 0x50);
if (const uint32_t n = c.r8(owner + 2))
c.w8(owner + 2, n - 1);
channelListRemove(c, best);
listRemove(c, best);
c.w16(best + 0x10, 0);
return best;
}
// A pending voice taken over (0x53ce8): the lowest priority below the note's + 1 among the allowed cores'
// pending voices (0x584c0 + 8 x core, core 1 first, from the tail); taken off its lists.
uint32_t takePending(Call &c, uint32_t priority, uint32_t mix)
{
uint32_t best = 0, limit = priority + 1;
for (int32_t core = 1; core >= 0; --core)
{
if (!coreAllowed(c, mix, static_cast<uint32_t>(core)))
continue;
const uint32_t head = 0x584c0 + 8 * static_cast<uint32_t>(core);
for (uint32_t v = c.r32(head + 4); v != head; v = c.r32(v + 4))
{
const uint32_t p = c.r16(v + 0x14);
if (p < (limit & 0xffff))
{
limit = p;
best = v;
}
}
}
if (!best)
return 0;
const uint32_t owner = c.r32(best + 0x50);
if (const uint32_t n = c.r8(owner + 2))
c.w8(owner + 2, n - 1);
channelListRemove(c, best);
listRemove(c, best);
c.w16(best + 0x10, 0);
return best;
}
// A note on (0x534f8: state, channel, port, note, velocity, id): every split of the channel's program whose key
// range holds the note (0x14-byte splits after the program's header, +2 / +4 the key range, +0 the sample set),
// whose sample set's velocity range holds the velocity, then every sample of the set (+4.. their numbers) whose
// velocity range holds it: a voice for each, at the sample's priority (+0xf) plus the port's; the exclusive
// group (+0xe) cut first (0x53dd0); a voice stolen when the port is at its maximum; a free voice taken
// (0x53a50, after collecting the ended ones), else a sounding one (0x53af4) or a pending one (0x53ce8) not
// started at once. The voice is set from the program, split, sample, VAG and the channel's one-shot values
// (bend +0x28, level +0x2f, pan +0x30: then reset), put on the channel's playing list, its portamento started,
// keyed on (or left pending, 0x584c0) and its stat set (0xa0 / 0x90 | port).
void noteOn(Call &c, uint32_t state, uint32_t ch, uint32_t port, uint32_t note, uint32_t velocity, uint32_t id)
{
note &= 0xff;
velocity &= 0xff;
id &= 0xff;
const uint32_t banks = state + 0x14;
const uint32_t program = c.r32(ch + 0x18), slot = c.r32(ch + 0x20);
if (!program)
return;
uint32_t split = program + c.r32(program);
int32_t remaining = static_cast<int32_t>(c.r8(program + 4)) - 1;
const uint32_t sets = c.r32(slot + 8), samples = c.r32(slot + 0xc), vags = c.r32(slot + 0x10);
if (remaining == -1)
return;
const uint32_t startedStat = (port | 0xffffffa0u) & 0xff, pendingStat = (port | 0xffffff90u) & 0xff;
for (; remaining != -1; --remaining, split += 0x14)
{
if (static_cast<int32_t>(note) < static_cast<int32_t>(c.r8(split + 2) & 0x7f) ||
static_cast<int32_t>(c.r8(split + 4) & 0x7f) < static_cast<int32_t>(note))
continue;
const uint32_t setIndex = c.r16(split);
if (c.r32(sets + 0xc) < setIndex)
continue;
const uint32_t set = sets + c.r32(sets + 0x10 + 4 * setIndex);
if (velocity < c.r8(set + 1) || c.r8(set + 2) < velocity || !c.r8(set + 3))
continue;
for (uint32_t k = 0; static_cast<int32_t>(k) < static_cast<int32_t>(c.r8(set + 3)); ++k)
{
const uint32_t sampleIndex = c.r16(set + 4 + 2 * k);
if (c.r32(samples + 0xc) < sampleIndex)
continue;
const uint32_t sample = samples + c.r32(samples + 0x10 + 4 * sampleIndex);
if (static_cast<int32_t>(velocity) < static_cast<int32_t>(c.r8(sample + 2) & 0x7f) ||
static_cast<int32_t>(c.r8(sample + 4) & 0x7f) < static_cast<int32_t>(velocity))
continue;
bool startNow = true;
uint32_t vag = 0;
const uint32_t vagIndex = c.r16(sample);
if (!(c.r32(vags + 0xc) < vagIndex))
{
vag = vags + c.r32(vags + 0x10 + 4 * vagIndex);
if (c.s32(vag) == -1)
continue;
}
if (const uint32_t group = c.r8(sample + 0xe))
exclusiveCut(c, ch, group);
const uint32_t priority = c.r8(sample + 0xf) + c.r8(state);
if (!(c.r8(banks + 2) < c.r8(state + 1)) && !stealVoice(c, banks, priority))
return;
uint32_t v = takeFree(c, c.r8(sample + 0x29));
if (!v && c.r32(0x56144))
{
c.w32(0x56144, 0);
if (collectEnvelopes(c))
v = takeFree(c, c.r8(sample + 0x29));
}
if (!v)
{
startNow = false;
v = takeReleased(c, priority & 0xffff, c.r8(sample + 0x29));
if (!v)
v = takePending(c, priority & 0xffff, c.r8(sample + 0x29));
if (!v)
continue;
}
c.w16(v + 0x10, 0x208);
c.w32(v + 0x50, banks);
c.w32(v + 0x54, ch);
c.w32(v + 0x58, program);
c.w32(v + 0x60, sample);
c.w32(v + 0x64, vag);
c.w32(v + 0x5c, split);
c.w16(v + 0x12, 0);
c.w32(v + 0x34, 0);
c.w16(v + 0x14, priority);
c.w8(v + 0x3e, note);
c.w8(v + 0x3f, id);
c.w32(v + 0x68, c.r32(slot + 0x14));
c.w8(v + 0x40, velocity);
c.w8(v + 0x41, c.r8(sample + 0xe));
c.w8(v + 0x42, c.r8(set));
c.w16(v + 0x28, c.r16(ch + 0x28));
c.w8(v + 0x4d, c.r8(ch + 0x2f));
c.w8(v + 0x4e, c.r8(ch + 0x30));
c.w16(ch + 0x28, 0xffff);
c.w8(ch + 0x2f, 0xff);
c.w8(ch + 0x30, 0xff);
c.w8(banks + 2, c.r8(banks + 2) + 1);
channelListPush(c, v, ch);
if (!(c.r8(ch + 0x33) & 0x80) && c.r16(ch + 0x24))
portamentoStart(c, v);
if (startNow)
{
keyOn(c, v);
listInsertAfter(c, v, 0x56130 + 8 * c.r8(v + 0x3c));
}
else
listInsertAfter(c, v, 0x584c0 + 8 * c.r8(v + 0x3c));
if (const uint32_t stats = c.r32(0x56140))
c.w8(stats + c.r8(v + 0x3c) * 24 + c.r8(v + 0x3d) + 8, startNow ? startedStat : pendingStat);
}
}
}
// Export 17 (env, port, out): each channel's lists in use, a byte a channel (1, 2, 4: its three lists).
void channelStatus(Call &c)
{
uint32_t state;
if (!portState(c, c.arg(0), c.arg(1), state))
return c.ret(static_cast<uint32_t>(-1));
for (uint32_t ch = 0; ch < 16; ++ch)
{
const uint32_t p = state + 0x18 + 0x38 * ch;
uint32_t flags = c.r32(p) != p - 8 ? 1 : 0;
if (c.r32(p + 8) != p)
flags |= 2;
if (c.r32(p + 0x10) != p + 8)
flags |= 4;
c.w8(c.arg(2) + ch, flags);
}
c.ret(0);
}
// sceHSyn_SetOutputMode(mode): 0 (0x584f0 = 1) or 1 (0); -1 for anything else.
void setOutputMode(Call &c)
{
if (c.arg(0) == 0)
c.w8(0x584f0, 1);
else if (c.arg(0) == 1)
c.w8(0x584f0, 0);
else
return c.ret(static_cast<uint32_t>(-1));
c.ret(0);
}
// Export 7 (Sony's direct voice access): voices locked (0x55640), the call made (0x554e0), unlocked: 0 when it
// returned the count asked, -1 otherwise.
constexpr uint32_t kLock = 0x55640, kDirect = 0x554e0;
void direct(Call &c)
{
const uint32_t id = static_cast<uint32_t>(static_cast<int32_t>(static_cast<int16_t>(c.arg(0))));
c.call(kLock, {id, 1});
const uint32_t r = c.call(kDirect, {id, 0, c.arg(1), c.arg(2), c.arg(3)});
c.call(kLock, {id, 1});
c.ret(r == c.arg(3) ? 0 : static_cast<uint32_t>(-1));
}
// sceHSyn_Init(env, unit): the tick in milliseconds (unit << 8 / 1000, hi of the 0x10624dd3 product >> 6),
// each port's state given its defaults (master 0x100, banks empty, channels reset), the voices reset; -1 for an
// environment without ports.
constexpr uint32_t kTickMs = 0x56110;
int32_t init(Call &c, uint32_t env, uint32_t unit)
{
c.w32(kVoiceStatBuffer, 0);
c.w32(kTickMs, static_cast<uint32_t>((static_cast<uint64_t>(unit << 8) * 0x10624dd3u) >> 32) >> 6);
if (!env || !c.r32(env))
return -1;
const uint32_t info = c.r32(env + 4);
if (!info || c.s32(info) < 2)
return -1;
const uint32_t ports = c.r32(info + 4);
if (!ports)
return -1;
int32_t count = 0;
for (int32_t k = 1; k < c.s32(info); k += 2, ++count)
{
const uint32_t entry = ports + 16 * count;
if (!c.r32(entry + 4))
return -1;
const uint32_t state = c.r32(entry + 0xc);
if (!state)
return -1;
c.w8(state + 1, 0x30);
c.w8(state + 0, 0);
for (uint32_t off = 4; off <= 0x10; off += 4)
c.w32(state + off, 0);
c.w16(state + 0x14, 0x100);
c.w8(state + 0x16, 0);
for (uint32_t bank = 0; bank < 16; ++bank)
for (uint32_t w = 0; w < 6; ++w)
c.w32(state + 0x398 + 24 * bank + 4 * w, 0);
for (uint32_t ch = 0; ch < 16; ++ch)
channelInit(c, state + 0x18 + 0x38 * ch, state);
}
if (!count)
return -1;
voiceReset(c);
c.w8(0x56171, 0xff);
c.w32(0x5612c, 0);
c.w32(0x56128, 0);
c.w8(0x584f0, 0);
c.w32(0x56144, 1);
return 0;
}
void initFn(Call &c) { c.ret(init(c, c.arg(0), c.arg(1))); }
} // namespace
void bindModhsyn(bozo::native::irx::CompiledLibraries &host)
{
using iop::Call;
bindLibsdWrappers(host);
iop::bind(host, 0x4f100, start);
iop::bind(host, 0x4f130, initFn);
iop::bind(host, 0x4f390, chunkIsFn);
iop::bind(host, 0x4f4c4, loadFn);
iop::bind(host, 0x4f6d0, channelInitFn);
iop::bind(host, 0x4f74c, selectProgramFn);
iop::bind(host, 0x4f304, direct);
iop::bind(host, 0x4f7e0, [](Call &c) { pitchBend(c, c.arg(0), c.arg(1), c.arg(2)); c.ret(0); });
iop::bind(host, 0x4f8a4, updateLevelsFn);
iop::bind(host, 0x4f960, [](Call &c) { setVolume(c, c.arg(0), c.arg(1)); c.ret(0); });
iop::bind(host, 0x4f9bc, [](Call &c) { setExpression(c, c.arg(0), c.arg(1)); c.ret(0); });
iop::bind(host, 0x4fa18, [](Call &c) { setLevel2d(c, c.arg(0), c.arg(1)); c.ret(0); });
iop::bind(host, 0x4fae0, [](Call &c) { setLevel2e(c, c.arg(0), c.arg(1)); c.ret(0); });
iop::bind(host, 0x4fba8, setPortamentoTime);
iop::bind(host, 0x4fbc0, [](Call &c) { c.ret(setPortamento(c, c.arg(0), c.arg(1))); });
iop::bind(host, 0x4fbf8, setPortamentoNote);
iop::bind(host, 0x4fc00, [](Call &c) { setPan(c, c.arg(0), c.arg(1)); c.ret(0); });
iop::bind(host, 0x4fccc, [](Call &c) { notePan(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3)); c.ret(0); });
iop::bind(host, 0x4fde4, [](Call &c) { noteLevel(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3)); c.ret(0); });
iop::bind(host, 0x4ff00, [](Call &c) { noteBend(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3), c.arg(4)); c.ret(0); });
iop::bind(host, 0x50370, [](Call &c) { c.ret(c.e.cpu.gpr[2]); }); // a system exclusive message: nothing
iop::bind(host, 0x50378, [](Call &c) { synthMessage(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3)); c.ret(0); });
iop::bind(host, 0x5052c, [](Call &c) { parseStream(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3)); c.ret(0); });
iop::bind(host, 0x5069c, [](Call &c) { readPorts(c, c.arg(0), c.sarg(1)); c.ret(0); });
iop::bind(host, 0x50734, tick);
iop::bind(host, 0x50900, setReservedVoices);
iop::bind(host, 0x50a54, portStateFn);
iop::bind(host, 0x50aa4, setMasterVolume);
iop::bind(host, 0x50ae8, getMasterVolume);
iop::bind(host, 0x50b20, eachChannel<0>);
iop::bind(host, 0x50b84, eachChannel<1>);
iop::bind(host, 0x50be8, eachChannel<2>);
iop::bind(host, 0x50c4c, setEffect);
iop::bind(host, 0x50d30, setVoiceStatBuffer);
iop::bind(host, 0x50d40, effectMode);
iop::bind(host, 0x50db0, effectParam);
iop::bind(host, 0x50e88, channelStatus);
iop::bind(host, 0x50f24, setOutputMode);
iop::bind(host, 0x50f70, [](Call &c) { c.ret(voiceReset(c)); });
iop::bind(host, 0x510f0, [](Call &c) { c.ret(listInsertAfter(c, c.arg(0), c.arg(1))); });
iop::bind(host, 0x5110c, [](Call &c) { c.ret(listAppend(c, c.arg(0), c.arg(1))); });
iop::bind(host, 0x51128, [](Call &c) { c.ret(listRemove(c, c.arg(0))); });
iop::bind(host, 0x51148, [](Call &c) { c.ret(channelListPush(c, c.arg(0), c.arg(1))); });
iop::bind(host, 0x51188, [](Call &c) { c.ret(channelListRemove(c, c.arg(0))); });
iop::bind(host, 0x511a8, [](Call &c) { c.ret(voiceWork(c)); });
iop::bind(host, 0x512bc, [](Call &c) { c.ret(add16(c.arg(0), c.arg(1), c.arg(2))); });
iop::bind(host, 0x51300, [](Call &c) { c.ret(add8(c.arg(0), c.arg(1), c.arg(2))); });
iop::bind(host, 0x51340, [](Call &c) { c.ret(follow(c.arg(0), c.arg(1), c.arg(2), c.arg(3), c.arg(4))); });
iop::bind(host, 0x513c8, [](Call &c) { c.ret(adsr1(c, c.arg(0), c.arg(1))); });
iop::bind(host, 0x5146c, [](Call &c) { c.ret(adsr2(c, c.arg(0), c.arg(1))); });
iop::bind(host, 0x514e8, [](Call &c) { keyOn(c, c.arg(0)); c.ret(0); });
iop::bind(host, 0x51d10, [](Call &c) { updatePitch(c, c.arg(0)); c.ret(0); });
iop::bind(host, 0x51e34, [](Call &c) { voiceBend(c, c.arg(0), c.arg(1)); c.ret(0); });
iop::bind(host, 0x51e8c, [](Call &c) { updateVolume(c, c.arg(0)); c.ret(0); });
iop::bind(host, 0x52000, [](Call &c) { voiceLevels(c, c.arg(0)); c.ret(0); });
iop::bind(host, 0x52038, [](Call &c) { c.ret(keyFollow(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3))); });
iop::bind(host, 0x5209c, [](Call &c) { voiceAmplitudeDepth(c, c.arg(0), c.arg(1)); c.ret(0); });
iop::bind(host, 0x52144, [](Call &c) { voicePitchDepth(c, c.arg(0), c.arg(1)); c.ret(0); });
iop::bind(host, 0x521f4, [](Call &c) { voicePanPosition(c, c.arg(0), c.arg(1)); c.ret(0); });
iop::bind(host, 0x522c8, [](Call &c) { voicePan(c, c.arg(0), c.arg(1)); c.ret(0); });
iop::bind(host, 0x52414, [](Call &c) { c.ret(voiceGain(c, c.arg(0))); });
iop::bind(host, 0x5256c, [](Call &c) { c.ret(envelopePass(c)); });
iop::bind(host, 0x527f0, [](Call &c) { c.ret(collectEnvelopes(c)); });
iop::bind(host, 0x5299c, [](Call &c) { portamentoStart(c, c.arg(0)); c.ret(0); });
iop::bind(host, 0x52a30, [](Call &c) { c.ret(keyOnSettled(c)); });
iop::bind(host, 0x52ad4, [](Call &c) { noteOff(c, c.arg(0), c.arg(1), c.arg(2)); c.ret(0); });
iop::bind(host, 0x52cec, [](Call &c) { dropVoice(c, c.arg(0)); c.ret(0); });
iop::bind(host, 0x52db8, [](Call &c) { sustain(c, c.arg(0), c.arg(1)); c.ret(0); });
iop::bind(host, 0x52f50, [](Call &c) { allNotesOff(c, c.arg(0)); c.ret(0); });
iop::bind(host, 0x530e8, [](Call &c) { allSoundOff(c, c.arg(0)); c.ret(0); });
iop::bind(host, 0x53240, [](Call &c) { voiceFree(c, c.arg(0)); c.ret(0); });
iop::bind(host, 0x53310, [](Call &c) { c.ret(stealVoice(c, c.arg(0), c.arg(1))); });
iop::bind(host, 0x53dd0, [](Call &c) { exclusiveCut(c, c.arg(0), c.arg(1)); c.ret(0); });
iop::bind(host, 0x53f70, [](Call &c) { c.ret(curveValue(0, c.arg(0), c.arg(1))); });
iop::bind(host, 0x53fa4, [](Call &c) { c.ret(curveValue(1, c.arg(0), c.arg(1))); });
iop::bind(host, 0x53fd8, [](Call &c) { c.ret(curveValue(2, c.arg(0), c.arg(1))); });
iop::bind(host, 0x54050, [](Call &c) { c.ret(curveValue(3, c.arg(0), c.arg(1))); });
iop::bind(host, 0x540dc, [](Call &c) { c.ret(curveValue(4, c.arg(0), c.arg(1))); });
iop::bind(host, 0x54170, [](Call &c) { c.ret(curveValue(5, c.arg(0), c.arg(1))); });
iop::bind(host, 0x54200, [](Call &c) { c.ret(curveValue(6, c.arg(0), c.arg(1))); });
iop::bind(host, 0x5426c, [](Call &c) { c.ret(curveValue(7, c.arg(0), c.arg(1))); });
iop::bind(host, 0x542d8, [](Call &c) { c.ret(curveValue(8, c.arg(0), c.arg(1))); });
iop::bind(host, 0x543a4, [](Call &c) { c.ret(curveValue(9, c.arg(0), c.arg(1))); });
iop::bind(host, 0x54514, [](Call &c) { c.ret(curveValue(10, c.arg(0), c.arg(1))); });
iop::bind(host, 0x5451c, [](Call &c) { c.ret((0x80 - c.arg(0)) & 0xff); });
iop::bind(host, 0x5447c, [](Call &c) { c.ret(curve(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3), c.arg(4))); });
iop::bind(host, 0x54680, [](Call &c) { c.ret(lfoWaveAt(c, 0x54680, c.arg(0), c.arg(1))); });
iop::bind(host, 0x54698, [](Call &c) { c.ret(lfoWaveAt(c, 0x54698, c.arg(0), c.arg(1))); });
iop::bind(host, 0x546b8, [](Call &c) { c.ret(lfoWaveAt(c, 0x546b8, c.arg(0), c.arg(1))); });
iop::bind(host, 0x546f4, [](Call &c) { c.ret(lfoWaveAt(c, 0x546f4, c.arg(0), c.arg(1))); });
iop::bind(host, 0x54714, [](Call &c) { c.ret(lfoWaveAt(c, 0x54714, c.arg(0), c.arg(1))); });
iop::bind(host, 0x54788, [](Call &c) { c.ret(lfoWaveAt(c, 0x54788, c.arg(0), c.arg(1))); });
iop::bind(host, 0x5452c, [](Call &c) { c.ret(velocitySquare(c.arg(0))); });
iop::bind(host, 0x54570, [](Call &c) { c.ret(velocityKind(3, c.arg(0) & 0xff)); });
iop::bind(host, 0x54598, [](Call &c) { c.ret(velocityKind(4, c.arg(0))); });
iop::bind(host, 0x545cc, [](Call &c) { c.ret(velocityKind(5, c.arg(0))); });
iop::bind(host, 0x545f4, [](Call &c) { c.ret(velocity(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3))); });
iop::bind(host, 0x547bc, [](Call &c) { c.ret(lfoTick(c, c.arg(0))); });
iop::bind(host, 0x5497c, [](Call &c) { lfoStart(c, c.arg(0)); c.ret(0); });
iop::bind(host, 0x549c4, [](Call &c) { lfoInit(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3), c.arg(4), c.arg(5), c.arg(6), c.arg(7), c.arg(8)); c.ret(0); });
iop::bind(host, 0x54d08, [](Call &c) { lfoRelease(c, c.arg(0)); c.ret(0); });
iop::bind(host, 0x54dc0, [](Call &c) { c.ret(lfoRandom(c)); });
iop::bind(host, 0x53a50, [](Call &c) { c.ret(takeFree(c, c.arg(0))); });
iop::bind(host, 0x53af4, [](Call &c) { c.ret(takeReleased(c, c.arg(0), c.arg(1))); });
iop::bind(host, 0x53ce8, [](Call &c) { c.ret(takePending(c, c.arg(0), c.arg(1))); });
iop::bind(host, 0x534f8, [](Call &c) { noteOn(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3), c.arg(4), c.arg(5)); c.ret(0); });
iop::bind(host, 0x50010, [](Call &c) { c.ret(resetControllers(c, c.arg(0))); });
iop::bind(host, 0x50090, [](Call &c) { controlChange(c, c.arg(1), c.arg(2), c.arg(3)); c.ret(0); });
iop::bind(host, 0x50248, [](Call &c) { channelMessage(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3), c.arg(4)); c.ret(0); });
}
} // namespace oni::drivers