Files
Thomas MartinandClaude Opus 5.5 076b9e8bdb WIP: MODMIDI's song API written natively
Song select, block start, repeat marks, the event player, stop/pause/go on,
the song tick, search-play and the setters. Onimusha never selects a song,
so these are checked by building only; every sound module is now native.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-29 19:15:18 +01:00

1493 lines
66 KiB
C++

// MODMIDI.IRX natively: Sony's MIDI sequencer of the modular synthesizer. It plays a sequence file's ("SQ": the
// chunks "SCEI" "Vers", "SCEI" "Sequ", then "SCEI" "Song" and "SCEI" "Midi") MIDI data blocks at its tick and writes
// the messages to the output streams the synthesizer (modhsyn) reads.
//
// The environment the caller gives (sceMidi_Init's ctx): +0 its entry count (at least 2), +4 the info: +0 the number
// of words that follow in pairs (inputs then outputs: 2 per port), +4 the port table (16 bytes a port: +4 the sequence
// data, +0xc the port's player state), +8 / +0xc the output streams' count and table (8 bytes each: +4 the stream).
// A player state (the "MIDI environment"): +0 / +4 the selected MIDI block / song (-1 none), +0xc its flags (1 a
// sequence loaded, 2 playing, 8 ..., 0x100 a song), +0x10.. 16 channel volumes (16-bit), +0x30 the master volume,
// +0x54.. the loaded chunks (+0x54 Sequ, +0x58 Midi, +0x5c Song), +0x8a .., +0x86 / +0x9c.. the 16 channels' ... (see
// the functions).
#include "oni_iop.h"
#include "oni_sound_native.h"
#include <climits>
#include <cstdint>
#include <stdexcept>
#include <string>
namespace oni::drivers
{
namespace
{
using iop::Call;
constexpr uint32_t kRegisterLibraryEntries = 0x4e924, kPrintf = 0x4e948;
constexpr uint32_t kExports = 0x4e890;
constexpr uint32_t kTickUnit = 0x4f010; // sceMidi_Init's second argument << 7
constexpr uint32_t kChunkFormat = 0x4e9a4; // "[%c%c%c%c] [%c%c%c%c]\n"
constexpr uint32_t kSCEI = 0x4e9bc, kVers = 0x4e9c4, kSequ = 0x4e9cc, kSong = 0x4e9d4, kMidi = 0x4e9dc;
// module start: its export table registered, resident (0) when it was.
void start(Call &c) { c.ret(c.call(kRegisterLibraryEntries, {kExports}) == 0 ? 0 : 1); }
// A chunk's 8-byte tag (two words, each four characters stored backwards) printed, then checked against two
// expected names: 1 when both match.
uint32_t chunkIs(Call &c, uint32_t w0, uint32_t w1, uint32_t name0, uint32_t name1)
{
auto b = [](uint32_t w, int k) { return static_cast<uint32_t>(static_cast<int32_t>(static_cast<int8_t>(w >> (8 * k)))); };
c.call(kPrintf, {kChunkFormat, b(w0, 3), b(w0, 2), b(w0, 1), b(w0, 0), b(w1, 3), b(w1, 2), b(w1, 1), b(w1, 0)});
for (int k = 0; k < 4; ++k)
{
if (static_cast<int8_t>(w0 >> (8 * k)) != c.s8(name0 + 3 - k))
return 0;
if (static_cast<int8_t>(w1 >> (8 * k)) != c.s8(name1 + 3 - k))
return 0;
}
return 1;
}
void chunkIsFn(Call &c) { c.ret(chunkIs(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3))); }
// sceMidi_Load(env, port): the port's sequence data checked ("SCEI" "Vers", then the "Sequ" chunk at its offset,
// the optional "Song" chunk, the "Midi" chunk) and the port's player reset: -1 when a chunk is missing.
int32_t load(Call &c, uint32_t env, uint32_t port)
{
if (!env)
return -1;
const uint32_t info = c.r32(env + 4);
if (!(port * 2 + 1 < c.r32(info)))
return -1;
const uint32_t entry = c.r32(info + 4) + port * 16;
const uint32_t player = c.r32(entry + 0xc);
const uint32_t chunks = player + 0x54;
c.w32(player + 0, 0xffffffffu);
c.w32(player + 4, 0xffffffffu);
c.w32(player + 8, 0);
c.w32(player + 0xc, 0);
c.w32(chunks + 0, 0);
c.w32(chunks + 4, 0);
c.w32(chunks + 8, 0);
c.w16(chunks + 0x36, 0);
for (int ch = 15; ch >= 0; --ch)
c.w8(player + 0x63 + 0x48 - (15 - ch), 0x80); // 0x9c..0xab: each channel's pan, centred
c.w8(chunks + 0x32, 0x80);
const uint32_t data = c.r32(c.r32(info + 4) + port * 16 + 4);
if (!data)
return -1;
if (!chunkIs(c, c.r32(data), c.r32(data + 4), kSCEI, kVers))
return -1;
const uint32_t sequ = data + c.r32(data + 8);
if (!chunkIs(c, c.r32(sequ), c.r32(sequ + 4), kSCEI, kSequ))
return -1;
uint32_t song = 0;
if (c.s32(sequ + 0x10) != -1)
{
song = data + c.r32(sequ + 0x10);
if (!chunkIs(c, c.r32(song), c.r32(song + 4), kSCEI, kSong))
song = 0;
}
if (c.s32(sequ + 0x14) == -1)
return -1;
const uint32_t midi = data + c.r32(sequ + 0x14);
if (!chunkIs(c, c.r32(midi), c.r32(midi + 4), kSCEI, kMidi))
return -1;
c.w32(chunks + 0, sequ);
c.w32(chunks + 4, midi);
c.w32(chunks + 8, song);
c.w32(player + 0xc, 1);
return 0;
}
void loadFn(Call &c) { c.ret(load(c, c.arg(0), c.arg(1))); }
// sceMidi_Init(env, unit): the tick unit kept (unit << 7); each port's player given its defaults (channel
// volumes and master volume 1 ... as Sony sets them: 16-bit 1, the rest 0) and its sequence loaded; each output
// stream emptied. -1 for an environment without its entries, ports or streams.
int32_t init(Call &c, uint32_t env, uint32_t unit)
{
c.w32(kTickUnit, unit << 7);
if (!env || c.s32(env) < 2)
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;
for (int32_t k = 1, port = 0; k < c.s32(info); k += 2, ++port)
{
const uint32_t player = c.r32(ports + 16 * port + 0xc);
if (!player)
return -1;
for (int ch = 0; ch < 16; ++ch)
c.w16(player + 0x10 + 2 * ch, 1);
c.w16(player + 0x30, 1);
c.w32(player + 0x34, 0);
c.w32(player + 0x3c, 0);
c.w32(player + 0x44, 0);
c.w32(player + 0x4c, 0);
load(c, env, static_cast<uint32_t>(port));
}
const int32_t outputs = c.s32(info + 8);
const uint32_t streams = c.r32(info + 0xc);
if (!outputs || !streams)
return -1;
if (outputs <= 0)
return 0;
for (int32_t k = 1;; ++k)
{
const uint32_t stream = c.r32(streams + 8 * (k - 1) + 4);
if (!stream)
return -1;
c.w32(stream + 4, 0);
if (!(k < c.s32(info + 8)))
break;
}
return 0;
}
void initFn(Call &c) { c.ret(init(c, c.arg(0), c.arg(1))); }
// --- The tracks. A track (the playing MIDI block's): +0x14 the read pointer, +0x18 its time, +0x20 the ticks a
// tick advances, +0x24 / +0x2c the time base, +0x28 the tempo (microseconds a quarter), +0x30 the running status,
// +0x31 1 when the next delta is 0 (after a loop jump), +0x33 / +0x34 the NRPN selected and its value, +0x36 the
// channels used (a bit each), +0x174 the loop table (8 entries of 12 bytes: +0 the loop's id (0xff free), +1 its
// count (0xff not started), +2 / +3 the running status, +4 the time, +8 the read pointer).
uint32_t divideOrTrap(uint32_t a, uint32_t b, uint32_t pc)
{
if (!b)
throw std::runtime_error("native IOP trap: division by zero in MODMIDI at " + std::to_string(pc));
return a / b;
}
// The ticks each sceMidi tick advances: (tempo << 7) / time base, << 8, / tick unit (+0x2c).
uint32_t tickRate(Call &c, uint32_t track)
{
const uint32_t v = divideOrTrap(c.r32(track + 0x28) << 7, c.r32(track + 0x24), 0x4c010);
const uint32_t rate = divideOrTrap(v << 8, c.r32(track + 0x2c), 0x4c028);
c.w32(track + 0x20, rate);
return rate;
}
void tickRateFn(Call &c) { c.ret(tickRate(c, c.arg(0))); }
// A variable-length number at the read pointer (7 bits a byte, high bit: more).
uint32_t readNumber(Call &c, uint32_t track)
{
uint32_t p = c.r32(track + 0x14), v = 0, b;
do
{
b = c.r8(p++);
v = (v << 7) + (b & 0x7f);
} while (b & 0x80);
c.w32(track + 0x14, p);
return v;
}
void readNumberFn(Call &c) { c.ret(readNumber(c, c.arg(0))); }
// The next event's delta time, added to the track's time: 0 once after a loop jump.
uint32_t readDelta(Call &c, uint32_t track)
{
if (c.r8(track + 0x31))
{
c.w8(track + 0x31, 0);
return 0;
}
const uint32_t delta = readNumber(c, track);
c.w32(track + 0x18, c.r32(track + 0x18) + delta);
return delta;
}
void readDeltaFn(Call &c) { c.ret(readDelta(c, c.arg(0))); }
// Back one byte, a length read and skipped.
uint32_t skipLength(Call &c, uint32_t track)
{
c.w32(track + 0x14, c.r32(track + 0x14) - 1);
const uint32_t length = readNumber(c, track);
c.w32(track + 0x14, c.r32(track + 0x14) + length);
return length;
}
void skipLengthFn(Call &c) { c.ret(skipLength(c, c.arg(0))); }
constexpr uint32_t kTempoLengthError = 0x4ea3c, kTempoFormat = 0x4ea64, kExclusiveOverrun = 0x4ea70,
kMessageOverrun = 0x4ea90, kLoopDuplicate = 0x4e9f0, kLoopOverflow = 0x4ea08,
kLoopNotFound = 0x4ea20;
constexpr uint32_t kChannelBits = 0x4efd0; // 16 halfwords: 1 << channel
constexpr uint32_t kLoopMessage = 0x4eff0; // the loop callback's message: +1 id, +2 the pass, +4 the loop
// A meta event (0xFF type length data): the player's meta callback (+0x3c, its data +0x40) first (0: skipped
// by the callback, then 1 unless it was the end of track); end of track (0x2f) marks the player (+0xc bit 4)
// and returns 0; a tempo (0x51, 3 bytes) sets the track's tempo and rate; anything else is skipped. 1 to go on.
uint32_t metaEvent(Call &c, uint32_t player, uint32_t track, uint32_t type)
{
const uint32_t length = readNumber(c, track);
const uint32_t callback = c.r32(player + 0x3c);
if (callback)
{
const uint32_t t = type & 0xff;
if (c.call(callback, {t, c.r32(track + 0x14), length, c.r32(player + 0x40)}) == 0)
{
c.w32(track + 0x14, c.r32(track + 0x14) + length);
return t != 0x2f ? 1 : 0;
}
}
type &= 0xff;
if (type == 0x2f)
{
c.w32(player + 0xc, c.r32(player + 0xc) | 4);
return 0;
}
if (type != 0x51)
{
c.w32(track + 0x14, c.r32(track + 0x14) + length);
return 1;
}
if (length != 3)
{
c.call(kPrintf, {kTempoLengthError, length});
return 0;
}
const uint32_t p = c.r32(track + 0x14);
const uint32_t tempo = (c.r8(p) << 16) + (c.r8(p + 1) << 8) + c.r8(p + 2);
c.w32(track + 0x28, tempo);
c.call(kPrintf, {kTempoFormat, divideOrTrap(60000000u, tempo, 0x4c1f8)});
c.w32(track + 0x14, c.r32(track + 0x14) + 3);
tickRate(c, track);
return 1;
}
void metaEventFn(Call &c) { c.ret(metaEvent(c, c.arg(0), c.arg(1), c.arg(2))); }
// The output stream entry index of the streams table (8 bytes each, +4 the buffer), 0 past its end.
uint32_t stream(Call &c, uint32_t index, uint32_t streams)
{
return index < c.r32(streams) ? c.r32(streams + 4) + index * 8 : 0;
}
void streamFn(Call &c) { c.ret(stream(c, c.arg(0), c.arg(1))); }
// A system exclusive event (0xF0 length data): the player's callback (+0x44, its data +0x48: 0 skips it), then
// 0xF0 and the data appended to each output the player's mask (+0x30) selects. The read pointer then passes
// the data; it is the result. (Sony's mask walk here skips 4 outputs when the low nibble has bits, 1 when it has
// none: the reverse of the channel messages'. The game's masks are 1.)
uint32_t exclusiveEvent(Call &c, uint32_t player, uint32_t track, uint32_t streams)
{
const uint32_t length = readNumber(c, track);
const uint32_t callback = c.r32(player + 0x44);
bool send = true;
if (callback && c.call(callback, {c.r32(track + 0x14), length, c.r32(player + 0x48)}) == 0)
send = false;
if (send && streams)
{
uint32_t mask = c.r16(player + 0x30), out = 0;
while (mask)
{
uint32_t step = 1;
if (mask & 1)
{
if (const uint32_t entry = stream(c, out, streams))
{
const uint32_t buffer = c.r32(entry + 4);
const uint32_t used = c.r32(buffer + 4);
if (c.r32(buffer) < used + length + 9)
c.call(kPrintf, {kExclusiveOverrun});
else
{
const uint32_t from = c.r32(track + 0x14);
c.w32(buffer + 4, used + 1 + length);
c.w8(buffer + 8 + used, 0xf0);
for (uint32_t k = 0; k < length; ++k)
c.w8(buffer + 9 + used + k, c.r8(from + k));
}
}
}
else if (mask & 0xf)
step = 4;
mask >>= step;
out += step;
}
}
const uint32_t next = c.r32(track + 0x14) + length;
c.w32(track + 0x14, next);
return next;
}
void exclusiveEventFn(Call &c) { c.ret(exclusiveEvent(c, c.arg(0), c.arg(1), c.arg(2))); }
// A channel message (status | data1 << 8 | data2 << 16, length bytes): the player's message callback (+0x34,
// its data +0x38) may change it (0 drops it); the channel marked used on the track; the message appended to
// each output the channel's mask (+0x10 + 2 x channel) selects.
void channelMessage(Call &c, uint32_t player, uint32_t track, uint32_t streams, uint32_t message, uint32_t length)
{
message &= 0x7f7fff;
if (const uint32_t callback = c.r32(player + 0x34))
{
message = c.call(callback, {message, c.r32(player + 0x38)});
if (!message)
return;
}
const uint32_t channel = message & 0xf;
c.w16(track + 0x36, c.r16(track + 0x36) | c.r16(kChannelBits + 2 * channel));
if (!streams)
return;
uint32_t mask = c.r16(player + 0x10 + 2 * channel), out = 0;
while (mask)
{
uint32_t step = 1;
if (mask & 1)
{
if (const uint32_t entry = stream(c, out, streams))
{
const uint32_t buffer = c.r32(entry + 4);
const uint32_t used = c.r32(buffer + 4);
if (c.r32(buffer) < used + length + 8)
c.call(kPrintf, {kMessageOverrun});
else
{
c.w32(buffer + 4, used + length);
uint32_t m = message;
for (uint32_t k = 0; k < length; ++k, m >>= 8)
c.w8(buffer + 8 + used + k, m);
}
}
}
else if (!(mask & 0xf))
step = 4;
mask >>= step;
out += step;
}
}
void channelMessageFn(Call &c)
{
channelMessage(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3), c.arg(4));
c.ret(0);
}
// A channel's level: its volume (+0x38) x expression (+0x48) x the master (+0x32) >> 14, at most 127.
uint32_t channelLevel(Call &c, uint32_t state, uint32_t channel)
{
const uint32_t s = state + (channel & 0xff);
const uint32_t v = (c.r8(s + 0x38) * c.r8(s + 0x48) * c.r8(state + 0x32)) >> 14;
return v < 0x80 ? v : 0x7f;
}
void channelLevelFn(Call &c) { c.ret(channelLevel(c, c.arg(0), c.arg(1))); }
// The loop table entry with this id, 0 when none.
uint32_t findLoop(Call &c, uint32_t table, uint32_t id)
{
for (int k = 0; k < 8; ++k, table += 12)
if (c.r8(table) == (id & 0xff))
return table;
return 0;
}
void findLoopFn(Call &c) { c.ret(findLoop(c, c.arg(0), c.arg(1))); }
// A control change's loop markers (Sony's NRPN loops): 0x63 selects an NRPN (its value then unset); data entry
// 0x06 on NRPN 0 starts a loop (the id: the entry's place in the table, the running status, time and read
// pointer kept); data entry 0x26 on NRPN 1 ends the loop with that id: back to its start the count times (0
// forever, through the player's loop callback +0x4c when it has one), unless the player is not looping (+0xc
// bit 8) or jumping is not allowed (jump 0).
void loopControl(Call &c, uint32_t player, uint32_t track, uint32_t control, uint32_t value, uint32_t jump)
{
control &= 0xff;
if (control == 0x63)
{
c.w8(track + 0x33, value);
c.w8(track + 0x34, 0xff);
return;
}
if (control == 6)
{
c.w8(track + 0x34, value);
if (c.r8(track + 0x33))
return;
const uint32_t table = track + 0x174, id = value & 0xff;
uint32_t entry = findLoop(c, table, id);
if (!entry)
{
// The first free entry; with none free, the one after the table (as the module does).
entry = table;
for (int k = 0; k < 8 && c.r8(entry) != 0xff; ++k)
entry += 12;
}
else if (c.r8(entry + 1) != 0xff)
c.call(kPrintf, {kLoopDuplicate, id});
if (!entry)
{
c.call(kPrintf, {kLoopOverflow});
return;
}
c.w8(entry + 0, value);
c.w8(entry + 1, 0xff);
c.w8(entry + 2, c.r8(track + 0x30));
c.w8(entry + 3, c.r8(track + 0x31));
c.w32(entry + 4, c.r32(track + 0x18));
c.w32(entry + 8, c.r32(track + 0x14));
return;
}
if (control != 0x26)
return;
if (c.r8(track + 0x33) != 1 || c.r8(track + 0x34) == 0xff || (c.r32(player + 0xc) & 8) || !jump)
return;
const uint32_t entry = findLoop(c, track + 0x174, c.r8(track + 0x34));
if (!entry)
{
c.call(kPrintf, {kLoopNotFound, c.r8(track + 0x34)});
return;
}
if (c.r8(entry + 1) == 0xff)
c.w8(entry + 1, value);
const uint32_t count = value & 0xff;
if (count && !c.r8(entry + 1))
{
c.w8(entry + 1, 0xff);
return;
}
if (const uint32_t callback = c.r32(player + 0x4c))
{
c.w8(kLoopMessage + 1, value);
c.w8(kLoopMessage + 2, value - c.r8(entry + 1) + 1);
c.w32(kLoopMessage + 4, c.r8(entry));
if (c.call(callback, {kLoopMessage, c.r32(player + 0x50)}) == 0)
{
c.w8(entry + 1, 0xff);
return;
}
}
c.w32(track + 0x14, c.r32(entry + 8));
c.w32(player + 8, c.r32(entry + 4));
c.w32(track + 0x18, c.r32(entry + 4));
c.w8(track + 0x31, c.r8(entry + 3));
c.w8(track + 0x30, c.r8(entry + 2));
if (count)
c.w8(entry + 1, c.r8(entry + 1) - 1);
}
void loopControlFn(Call &c)
{
loopControl(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3), c.arg(4));
c.ret(0);
}
constexpr uint32_t kRunningStatusError = 0x4eaac, kUnknownStatus = 0x4ead4;
// The track's events up to the player's time (+8), each sent to the outputs (streams): Sony's SQ encoding (a
// note off has one data byte; a data byte with its high bit set means the next event has no delta time, +0x31).
// Per channel (12 bytes from +0x58): +0 the program, +1 bank (CC 0), +2 modulation (CC 1), +3 CC 2, +4
// portamento time (CC 5), +5 volume (CC 7, also +0x38 + channel), +6 pan (CC 10), +7 expression (CC 11), +8 /
// +9 CC 64 / 65, +0xa the pitch bend (16-bit). CC 32 sets the channel's output mask (+0x10 + 2 x channel = 1 <<
// value); CC 6, 38, 98, 99 are the loop markers; a volume (CC 7) goes out at the channel's level. With a
// conversion table (+0xc), polyphonic aftertouch becomes the table's two bytes and a third. 1 when the track
// went on, 0 at its end or an error.
uint32_t playEvents(Call &c, uint32_t player, uint32_t track, uint32_t streams)
{
while (!(c.r32(player + 8) < c.r32(track + 0x18)))
{
uint32_t p = c.r32(track + 0x14);
uint32_t status = c.r8(p);
c.w32(track + 0x14, p + 1);
uint32_t data1;
if (status & 0x80)
{
data1 = c.r8(p + 1);
c.w32(track + 0x14, p + 2);
}
else
{
const uint32_t running = c.r8(track + 0x30);
data1 = status;
if (!running)
{
c.call(kPrintf, {kRunningStatusError, status & 0xff, running & 0xff});
return 0;
}
status = running;
}
status &= 0xff;
if (status < 0xf0)
{
const uint32_t channel = status & 0xf, kind = status & 0xf0;
const uint32_t chan = track + channel * 12;
uint32_t message = status | (data1 & 0xff) << 8;
uint32_t data2 = 0, length = 0;
c.w8(track + 0x30, status);
auto readData2 = [&] {
const uint32_t q = c.r32(track + 0x14);
data2 = c.r8(q);
c.w32(track + 0x14, q + 1);
message |= (data2 & 0xff) << 16;
};
switch (kind)
{
case 0x80:
case 0xd0:
length = 2;
break;
case 0xc0:
c.w8(chan + 0x58, data1 & 0x7f);
length = 2;
break;
case 0x90:
case 0xe0:
readData2();
length = 3;
if (kind == 0xe0)
c.w16(chan + 0x62, message >> 8);
break;
case 0xa0:
if (const uint32_t table = c.r32(track + 0xc))
{
const uint32_t index = ((channel | (data1 & 0xf0)) << 1);
if (index < c.r16(table + 2))
{
message = c.r8(table + index + 4) | c.r8(table + index + 5) << 8 |
((((data1 & 0xf) << 3) | 7) << 16);
length = 3;
}
}
else
{
readData2();
length = 3;
}
break;
case 0xb0:
{
readData2();
length = 3;
const uint32_t value = data2 & 0x7f;
switch (data1 & 0xff)
{
case 0x00: c.w8(chan + 0x59, value); break;
case 0x01: c.w8(chan + 0x5a, value); break;
case 0x02: c.w8(chan + 0x5b, value); break;
case 0x05: c.w8(chan + 0x5c, value); break;
case 0x07:
c.w8(track + 0x38 + channel, value);
c.w8(chan + 0x5d, value);
data2 = (data2 & 0x80) | channelLevel(c, track, channel);
message = (message & 0xffff) | (data2 & 0xff) << 16;
break;
case 0x0a: c.w8(chan + 0x5e, value); break;
case 0x0b: c.w8(chan + 0x5f, value); break;
case 0x40: c.w8(chan + 0x60, value); break;
case 0x41: c.w8(chan + 0x61, value); break;
case 0x20: c.w16(player + 0x10 + 2 * channel, 1u << (data2 & 0xf)); break;
case 0x06: case 0x26: case 0x62: case 0x63:
loopControl(c, player, track, data1 & 0xff, value, streams);
break;
default: break;
}
break;
}
default:
c.call(kPrintf, {kUnknownStatus, status});
return 0;
}
if (length)
channelMessage(c, player, track, streams, message, length);
c.w8(track + 0x31, (data1 | data2) & 0x80);
}
else
{
c.w8(track + 0x31, 0);
if (status == 0xf7)
skipLength(c, track);
else if (status == 0xf0)
exclusiveEvent(c, player, track, streams);
else if (status == 0xff)
{
if (!metaEvent(c, player, track, data1 & 0xff))
return 0;
}
else
{
c.call(kPrintf, {kUnknownStatus, status});
return 0;
}
}
readDelta(c, track);
}
return 1;
}
void playEventsFn(Call &c) { c.ret(playEvents(c, c.arg(0), c.arg(1), c.arg(2))); }
// Every channel the track used: sustain off (CC 64 = 0), all notes off (CC 123); none used after.
void allNotesOff(Call &c, uint32_t player, uint32_t streams)
{
const uint32_t track = player + 0x54;
for (uint32_t ch = 0; ch < 16; ++ch)
if (c.r16(track + 0x36) & c.r16(kChannelBits + 2 * ch))
{
channelMessage(c, player, track, streams, ch | 0x40b0, 3);
channelMessage(c, player, track, streams, ch | 0x7bb0, 3);
}
c.w16(track + 0x36, 0);
}
void allNotesOffFn(Call &c)
{
allNotesOff(c, c.arg(0), c.arg(1));
c.ret(0);
}
// A tick of the player's block: the tick unit added to the track's remainder (+0x1c); each whole step of the
// track's rate (+0x20) moves the player's time (+8) on and plays the events up to it. At the track's end the
// player stops (flags bit 2 cleared) and its notes are released.
void playBlock(Call &c, uint32_t player, uint32_t streams)
{
const uint32_t track = player + 0x54;
uint32_t remainder = c.r32(track + 0x1c) + c.r32(kTickUnit);
for (;;)
{
if (!playEvents(c, player, track, streams))
{
c.w32(player + 0xc, c.r32(player + 0xc) & ~2u);
allNotesOff(c, player, streams);
remainder = 0;
break;
}
const uint32_t rate = c.r32(track + 0x20);
if (remainder < rate)
break;
c.w32(player + 8, c.r32(player + 8) + 1);
remainder -= rate;
}
c.w32(track + 0x1c, remainder);
}
void playBlockFn(Call &c)
{
playBlock(c, c.arg(0), c.arg(1));
c.ret(0);
}
// A volume message (CC 7) at the channel's level, while the player plays.
void sendVolume(Call &c, uint32_t player, uint32_t track, uint32_t streams, uint32_t channel)
{
if (!(c.r32(player + 0xc) & 2))
return;
channelMessage(c, player, track, streams, channel | (channelLevel(c, track, channel & 0xff) << 16) | 0x7b0, 3);
}
void sendVolumeFn(Call &c)
{
sendVolume(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3));
c.ret(0);
}
// Each channel's state sent again (the values set: bit 7 clear): bank, program, modulation, CC 2, portamento
// time, volume (at its level), pan, expression, CC 64, CC 65, pitch bend.
void restoreChannels(Call &c, uint32_t player, uint32_t streams)
{
const uint32_t track = player + 0x54;
for (uint32_t ch = 0; ch < 16; ++ch)
{
const uint32_t s = track + 12 * ch;
auto control = [&](uint32_t offset, uint32_t number) {
const uint32_t v = c.r8(s + offset);
if (!(v & 0x80))
channelMessage(c, player, track, streams, ch | (v << 16) | (number << 8) | 0xb0, 3);
};
control(0x59, 0x00);
if (!(c.r8(s + 0x58) & 0x80))
channelMessage(c, player, track, streams, ch | (c.r8(s + 0x58) << 8) | 0xc0, 2);
control(0x5a, 0x01);
control(0x5b, 0x02);
control(0x5c, 0x05);
if (!(c.r8(s + 0x5d) & 0x80))
{
c.w8(track + ch + 0x38, c.r8(s + 0x5d));
sendVolume(c, player, track, streams, ch);
}
control(0x5e, 0x0a);
control(0x5f, 0x0b);
control(0x60, 0x40);
control(0x61, 0x41);
if (!(c.r16(s + 0x62) & 0x8080))
channelMessage(c, player, track, streams, ch | (c.r16(s + 0x62) << 8) | 0xe0, 3);
}
}
void restoreChannelsFn(Call &c)
{
restoreChannels(c, c.arg(0), c.arg(1));
c.ret(0);
}
// The port's player and its track (player + 0x54): 0 for a port the environment does not have.
bool player(Call &c, uint32_t env, uint32_t port, uint32_t &p)
{
if (!env)
return false;
const uint32_t info = c.r32(env + 4);
if (!(port * 2 + 1 < c.r32(info)))
return false;
p = c.r32(c.r32(info + 4) + (port * 2 + 1) * 8 + 4);
return true;
}
void playerFn(Call &c)
{
uint32_t p = 0;
if (!player(c, c.arg(0), c.arg(1), p))
return c.ret(0);
c.w32(c.arg(2), p);
c.w32(c.arg(3), p + 0x54);
c.ret(1);
}
// sceMidi_MidiPlaySwitch(env, port, on): 1 starts the selected block (loaded, not ended, a block chosen: the
// channels' state sent again), 0 stops it (its notes released).
int32_t playSwitch(Call &c, uint32_t env, uint32_t port, uint32_t on)
{
if (!env)
return -1;
const uint32_t info = c.r32(env + 4);
if (!(port * 2 + 1 < c.r32(info)))
return -1;
const uint32_t p = c.r32(c.r32(info + 4) + (port * 2 + 1) * 8 + 4), streams = info + 8;
if (on == 0)
{
if (!(c.r32(p + 0xc) & 2))
return 0;
allNotesOff(c, p, streams);
c.w32(p + 0xc, c.r32(p + 0xc) & ~2u);
return 0;
}
if (on != 1)
return 0;
if ((c.r32(p + 0xc) & 5) != 1 || c.s32(p + 4) == -1)
return -1;
restoreChannels(c, p, streams);
c.w32(p + 0xc, c.r32(p + 0xc) | 2);
return 0;
}
void playSwitchFn(Call &c) { c.ret(playSwitch(c, c.arg(0), c.arg(1), c.arg(2))); }
// The block from its start: time 0, the running status and NRPN cleared, each channel's volume 127 and its
// state unset (0xff), the rate and the first delta read.
uint32_t rewind(Call &c, uint32_t p)
{
const uint32_t track = p + 0x54;
c.w32(p + 8, 0);
c.w32(track + 0x18, 0);
c.w32(track + 0x1c, 0);
c.w32(track + 0x20, 0);
c.w8(track + 0x30, 0);
c.w8(track + 0x31, 0);
c.w8(track + 0x34, 0xff);
c.w8(track + 0x33, 0xff);
c.w32(track + 0x14, c.r32(track + 0x10));
for (uint32_t ch = 0; ch < 16; ++ch)
{
c.w8(track + 0x38 + ch, 0x7f);
for (uint32_t k = 0x58; k <= 0x61; ++k)
c.w8(track + 12 * ch + k, 0xff);
c.w16(track + 12 * ch + 0x62, 0xffff);
}
tickRate(c, track);
readDelta(c, track);
return 1;
}
void rewindFn(Call &c) { c.ret(rewind(c, c.arg(0))); }
// sceMidi_SelectMidi(env, port, block): the sequence's MIDI block chosen (stopping what played): its data
// (+0x10 from its offset in the Midi chunk's table), its conversion table (a block of type 1 with entries and
// a header of at least 12 bytes), the loop table emptied, 500000 microseconds a quarter, its division, rewound.
int32_t selectMidi(Call &c, uint32_t env, uint32_t port, uint32_t block)
{
uint32_t p;
if (!player(c, env, port, p))
return -1;
const uint32_t track = p + 0x54;
const uint32_t midi = c.r32(track + 4);
if (c.r32(p + 0xc) & 2)
playSwitch(c, env, port, 0);
const uint32_t flags = c.r32(p + 0xc) & ~6u;
c.w32(p + 0xc, flags);
c.w32(p + 4, 0xffffffffu);
if (!(flags & 1) || !midi || c.r32(midi + 0xc) < block)
return -1;
const uint32_t data = midi + c.r32(midi + 0x10 + 4 * block);
c.w32(track + 0xc, data + 6);
if (c.r16(data + 6) != 1 || c.r16(data + 8) == 0 || c.r32(data) < 0xc)
c.w32(track + 0xc, 0);
for (uint32_t k = 0; k < 8; ++k)
{
c.w8(track + 0x174 + 12 * k, 0xff);
c.w8(track + 0x175 + 12 * k, 0xff);
}
c.w32(track + 0x10, data + c.r32(data));
c.w32(track + 0x28, 500000);
c.w32(track + 0x2c, 0x100);
c.w32(track + 0x24, c.r16(data + 4));
if (!rewind(c, p))
return -1;
c.w32(p + 4, block);
return 0;
}
void selectMidiFn(Call &c) { c.ret(selectMidi(c, c.arg(0), c.arg(1), c.arg(2))); }
// sceMidi_MidiSetLocation(env, port, time): the block played silently up to time (rewound first when time is
// earlier): 0 when it got there, -1 at its end or an error.
int32_t setLocation(Call &c, uint32_t env, uint32_t port, uint32_t time)
{
uint32_t p;
if (!player(c, env, port, p))
return -1;
const uint32_t track = p + 0x54;
if (time < c.r32(p + 8) && !rewind(c, p))
return -1;
while (c.r32(p + 8) < time)
{
if (c.r32(p + 0xc) & 4)
return -1;
const uint32_t next = c.r32(track + 0x18);
if (!(next < time))
{
c.w32(p + 8, time);
break;
}
c.w32(p + 8, next);
if (!playEvents(c, p, track, 0))
break;
}
return c.r32(p + 8) == time ? 0 : -1;
}
void setLocationFn(Call &c) { c.ret(setLocation(c, c.arg(0), c.arg(1), c.arg(2))); }
// A song's state reset (flags 0x700 cleared, its position back to its start, its 8 slots' +0x1c cleared).
void songReset(Call &c)
{
const uint32_t p = c.arg(0), song = c.arg(1);
c.w32(p + 0xc, c.r32(p + 0xc) & ~0x700u);
const uint32_t start = c.r32(song);
c.w32(song + 4, start);
for (uint32_t k = 0; k < 8; ++k)
c.w32(song + 0x54 - 8 * k, 0);
c.ret(start);
}
// sceMidi_MidiSetVolume(env, port, channel, volume): a channel's relative volume (+0x48 + channel), or with
// channel 0xff the master (+0x32, every channel sent again while playing); the volumes go out at once.
int32_t setVolume(Call &c, uint32_t env, uint32_t port, uint32_t channel, uint32_t volume)
{
uint32_t p;
if (!player(c, env, port, p))
return -1;
const uint32_t track = p + 0x54, streams = c.r32(env + 4) + 8;
channel &= 0xff;
if (channel < 16)
{
c.w8(track + 0x48 + channel, volume);
sendVolume(c, p, track, streams, channel);
return 0;
}
if (channel != 0xff)
return -1;
c.w8(track + 0x32, volume);
if (c.r32(p + 0xc) & 2)
for (uint32_t ch = 0; ch < 16; ++ch)
sendVolume(c, p, track, streams, ch);
return 0;
}
void setVolumeFn(Call &c) { c.ret(setVolume(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3))); }
// sceMidi_MidiVolumeChange(env, port, channel, volume): a channel's volume (CC 7: +0x38 + channel and its state
// +0x5d), every channel's with 0xff; a value with bit 7 set means 127.
int32_t volumeChange(Call &c, uint32_t env, uint32_t port, uint32_t channel, uint32_t volume)
{
uint32_t p;
if (!player(c, env, port, p))
return -1;
const uint32_t track = p + 0x54, streams = c.r32(env + 4) + 8;
if (volume & 0x80)
volume = 0x7f;
channel &= 0xff;
auto set = [&](uint32_t ch) {
c.w8(track + 0x38 + ch, volume);
c.w8(track + 12 * ch + 0x5d, volume);
sendVolume(c, p, track, streams, ch);
};
if (channel < 16)
{
set(channel);
return 0;
}
if (channel != 0xff)
return -1;
for (uint32_t ch = 0; ch < 16; ++ch)
set(ch);
return 0;
}
void volumeChangeFn(Call &c) { c.ret(volumeChange(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3))); }
// sceMidi_MidiSetAbsoluteTempo(env, port, bpm): the tempo as beats a minute (60000000 / bpm microseconds).
void setAbsoluteTempo(Call &c)
{
uint32_t p;
if (!player(c, c.arg(0), c.arg(1), p))
return c.ret(-1);
const uint32_t track = p + 0x54;
c.w32(track + 0x28, divideOrTrap(60000000u, c.arg(2) & 0xff, 0x4d5ac));
tickRate(c, track);
c.ret(0);
}
// sceMidi_MidiGetAbsoluteTempo(env, port): beats a minute, 20 to 255 (0 with no block or tempo).
void getAbsoluteTempo(Call &c)
{
uint32_t p;
if (!player(c, c.arg(0), c.arg(1), p) || c.s32(p + 4) == -1)
return c.ret(0);
const uint32_t tempo = c.r32(p + 0x54 + 0x28);
if (!tempo)
return c.ret(0);
const uint32_t bpm = divideOrTrap(60000000u, tempo, 0x4d63c);
c.ret(bpm < 0x14 ? 0x14 : bpm < 0x100 ? bpm & 0xff : 0xff);
}
// sceMidi_MidiSetRelativeTempo(env, port, relative): the tick unit (+0x2c, 0x100 = as written).
void setRelativeTempo(Call &c)
{
uint32_t p;
if (!player(c, c.arg(0), c.arg(1), p))
return c.ret(-1);
const uint32_t track = p + 0x54;
c.w32(track + 0x2c, c.arg(2) & 0xffff);
tickRate(c, track);
c.ret(0);
}
// sceMidi_MidiGetRelativeTempo(env, port).
void getRelativeTempo(Call &c)
{
uint32_t p;
if (!player(c, c.arg(0), c.arg(1), p) || c.s32(p + 4) == -1)
return c.ret(0);
c.ret(c.r16(p + 0x54 + 0x2c));
}
// A channel message to all 16 channels of a port (its channel bits replaced).
void allChannels(Call &c)
{
const uint32_t info = c.r32(c.arg(0) + 4);
const uint32_t p = c.r32(c.r32(info + 4) + ((c.arg(1) << 4) | 8) + 4);
const uint32_t message = c.arg(2) & 0xfffff0, length = c.arg(3);
for (uint32_t ch = 0; ch < 16; ++ch)
channelMessage(c, p, p + 0x54, info + 8, message | ch, length);
c.ret(0);
}
// A song's fade: the volume between its start (+0x128) and target (+0x129) at its step (+0x124) of its length
// (+0x120), scaled by +0x12a / 128, set as the port's master volume when it changed (+0x12b).
void songFade(Call &c, uint32_t env, uint32_t port, uint32_t song)
{
int32_t level;
if (const int32_t step = c.s32(song + 0x124))
{
const int32_t span = (c.r8(song + 0x129) - c.r8(song + 0x128)) * step, length = c.s32(song + 0x120);
if (!length)
throw std::runtime_error("native IOP trap: division by zero in MODMIDI at 0x4d80c");
if (length == -1 && span == INT32_MIN)
throw std::runtime_error("native IOP trap: division overflow in MODMIDI at 0x4d824");
level = span / length + c.r8(song + 0x128);
}
else
level = c.r8(song + 0x129);
int32_t scaled = level * c.r8(song + 0x12a);
if (scaled < 0)
scaled += 0x7f;
level = scaled >> 7;
if (static_cast<int32_t>(c.r8(song + 0x12b)) != level)
{
setVolume(c, env, port, 0xff, level & 0xff);
c.w8(song + 0x12b, level);
}
}
void songFadeFn(Call &c)
{
songFade(c, c.arg(0), c.arg(1), c.arg(2));
c.ret(0);
}
// sceMidi_ATick(env): each port's player: its MIDI block played (flags bit 2) and its song (bit 0x100).
// --- The song API (exports 7-13): a song is a list of 3-byte events (0xa0 op value) choosing MIDI blocks to
// play and setting the song's volume, pan and tempo, with repeat marks (0xa1 offset) and an end. A player's
// song state is at +0x54: +0x118 the song's start, +0x11c the next event, +0x120 the fade step, +0x124 the
// fade count, +0x128 the fade's target, +0x129 volume, +0x12a, +0x12b, +0x12c flags (1 seek, 2 keep, 4 in a
// seek), +0x12d the seek's block count, +0x12e pan, +0x12f tempo, +0x130 relative tempo; +0x188 eight repeat
// marks (the event, the passes left). Onimusha never selects a song, so none of this runs in the game.
constexpr uint32_t kSongSelect = 0x4d90c, kSongVolume = 0x4da58, kSongPan = 0x4da7c, kSongTempo = 0x4daa0,
kSongBlock = 0x4dac8, kSongRepeat = 0x4db90, kSongEvents = 0x4dda8, kSongStop = 0x4e134,
kSongSearch = 0x4e5dc;
constexpr uint32_t kPlayer = 0x4d884, kSongReset = 0x4d8d4, kSongFade = 0x4d7c0, kAllChannels = 0x4d714,
kPlaySwitch = 0x4ced4, kSelectMidi = 0x4d05c, kSetAbsoluteTempo = 0x4d578,
kSetRelativeTempo = 0x4d678;
constexpr uint32_t kRepeatMessage = 0x4f000; // the repeat callback's message: +1 the set, +2 the pass, +4 where
constexpr uint32_t kRepeatStatusError = 0x4ec90, kRepeatOverflow = 0x4ecb0, kSongUnknownStatus = 0x4ecd4,
kSongLoopError = 0x4ecf4, kSongUnknownControl = 0x4ed1c, kSongUnknownOperation = 0x4ed3c,
kSearchUnknownStatus = 0x4ef60, kSearchLoopError = 0x4ef80;
// The port's player context (+0 the song, -1 none; +0xc flags: 0x100 song playing, 0x200 its block playing,
// 0x400 at its end) and song state; false if there is no such port.
bool songPlayer(Call &c, uint32_t env, uint32_t port, uint32_t &context, uint32_t &song)
{
iop::Locals out(c, 8);
if (!c.call(kPlayer, {env, port, out.base, out.base + 4}))
return false;
context = c.r32(out.base);
song = c.r32(out.base + 4);
return true;
}
// sceMidi_SongSetLocation(env, port, song): the song chosen (the playing one stopped), its state reset.
void songSelect(Call &c)
{
const uint32_t env = c.arg(0), port = c.arg(1), number = c.arg(2);
uint32_t context, song;
if (!songPlayer(c, env, port, context, song))
return c.ret(0xffffffffu);
const uint32_t songs = c.r32(song + 8);
if (songs == 0 || c.r32(songs + 0xc) < number)
return c.ret(0xffffffffu);
if (c.r32(songs + 0x10 + 4 * number) == 0xffffffffu)
return c.ret(0xffffffffu);
if (c.r32(context + 0xc) & 0x100)
c.call(kSongStop, {env, port, 0});
c.w32(song + 0x120, 0);
c.w32(song + 0x124, 0);
c.w8(song + 0x129, 0x80);
c.w32(song + 0x118, c.r32(song + 8) + c.r32(c.r32(song + 8) + 0x10 + 4 * number));
c.w8(song + 0x12a, 0x80);
c.w8(song + 0x12b, 0xff);
c.w8(song + 0x12c, 0);
c.w8(song + 0x12e, 0x40);
c.w8(song + 0x12f, 0x78);
c.w16(song + 0x130, 0x100);
c.w32(context, number);
c.call(kSongReset, {context, song + 0x118});
c.ret(0);
}
// The song's volume (0..0x80), pan (0..0x7f), tempo (10..0xff) kept.
void songVolume(Call &c)
{
const int32_t v = c.sarg(1);
c.w8(c.arg(0) + 0x129, v < 0 ? 0 : v < 0x81 ? v : 0x80);
c.ret(v < 0x81 ? 1 : 0);
}
void songPan(Call &c)
{
const int32_t v = c.sarg(1);
c.w8(c.arg(0) + 0x12e, v < 0 ? 0 : v < 0x80 ? v : 0x7f);
c.ret(v < 0x80 ? 1 : 0);
}
void songTempo(Call &c)
{
const int32_t v = c.sarg(1);
c.w8(c.arg(0) + 0x12f, v < 10 ? 10 : v < 0x100 ? v : 0xff);
c.ret(v < 0x100 ? 1 : 0);
}
// (env, port, player, block): the block selected and started at the song's tempo, volume and pan. 1 started.
void songBlock(Call &c)
{
const uint32_t env = c.arg(0), port = c.arg(1), player = c.arg(2), block = c.arg(3);
const uint32_t song = player + 0x54;
if (c.call(kSelectMidi, {env, port, block}))
return c.ret(0);
c.call(kSetRelativeTempo, {env, port, c.r16(song + 0x130)});
c.call(kSetAbsoluteTempo, {env, port, c.r8(song + 0x12f)});
if (c.call(kPlaySwitch, {env, port, 1}))
return c.ret(0);
c.call(kSongFade, {env, port, song});
c.call(kAllChannels, {env, port, (uint32_t(c.r8(song + 0x12e)) << 16) | 0xab0, 3});
c.w32(player + 0xc, c.r32(player + 0xc) | 0x200);
c.ret(1);
}
// (player, passes): a repeat mark (0xa1 hi lo: back by that offset from the song's start) taken `passes`
// times (0: forever), the player's callback (+0x4c, argument +0x50) told each pass and able to stop it.
// 1 go on, 0 an error.
void songRepeat(Call &c)
{
const uint32_t player = c.arg(0), passes = c.arg(1);
const uint32_t song = player + 0x54;
const uint32_t at = c.r32(song + 0x11c);
if (c.r8(at) != 0xa1)
{
c.call(kPrintf, {kRepeatStatusError, c.r8(at)});
return c.ret(0);
}
const uint32_t offset = (uint32_t(c.r8(at + 1)) << 8) + c.r8(at + 2);
c.w32(song + 0x11c, at + 3);
c.w8(kRepeatMessage + 1, passes);
c.w32(kRepeatMessage + 4, c.r32(song + 0x11c));
auto back = [&] {
c.w32(song + 0x11c, c.r32(song + 0x118) + offset);
c.ret(1);
};
auto told = [&] { return c.call(c.r32(player + 0x4c), {kRepeatMessage, c.r32(player + 0x50)}) != 0; };
if ((passes & 0xff) == 0)
{
c.w8(kRepeatMessage + 2, 0);
if (c.r32(player + 0x4c) && !told())
return c.ret(1);
return back();
}
const uint32_t mark = c.r32(song + 0x11c);
for (uint32_t k = 0; k < 8; ++k)
{
const uint32_t entry = player + 0x188 + 8 * k;
if (c.r32(entry) != mark)
continue;
const uint32_t left = c.r8(entry + 4) - 1;
c.w8(entry + 4, left);
if ((left & 0xff) != 0)
{
if (c.r32(player + 0x4c) == 0)
return back();
c.w8(kRepeatMessage + 2, passes - left + 1);
if (told())
return back();
}
c.w32(entry, 0);
return c.ret(1);
}
for (uint32_t k = 0; k < 8; ++k)
{
const uint32_t entry = player + 0x188 + 8 * k;
if (c.r32(entry) != 0)
continue;
c.w8(kRepeatMessage + 2, 1);
if (c.r32(player + 0x4c) && !told())
return c.ret(1);
c.w32(entry, mark);
c.w8(entry + 4, passes);
return back();
}
c.call(kPrintf, {kRepeatOverflow});
c.ret(0);
}
// (env, port, player): the song's events played until one starts a block (or the song stops).
void songEvents(Call &c)
{
const uint32_t env = c.arg(0), port = c.arg(1), player = c.arg(2);
const uint32_t song = player + 0x54;
uint32_t v = c.r32(player + 0xc) & 0x100;
auto fail = [&](uint32_t format, uint32_t value) {
c.call(kPrintf, {format, value});
c.ret(c.call(kSongStop, {env, port, 0}));
};
while (c.r32(player + 0xc) & 0x100)
{
const uint32_t at = c.r32(song + 0x11c);
c.w32(song + 0x11c, at + 1);
if (c.r8(at) != 0xa0)
return fail(kSongUnknownStatus, c.r8(at));
const uint32_t op = c.r8(at + 1), value = c.r8(at + 2);
c.w32(song + 0x11c, at + 3);
switch (op)
{
case 0:
v = c.call(kSongBlock, {env, port, player, value});
if (v)
return c.ret(v);
c.w8(song + 0x12c, c.r8(song + 0x12c) & 0xfb);
break;
case 1: c.call(kSongVolume, {song, value}); break;
case 2: c.call(kSongVolume, {song, value + c.r8(song + 0x129)}); break;
case 3: c.call(kSongVolume, {song, c.r8(song + 0x129) - value}); break;
case 4: c.call(kSongPan, {song, value}); break;
case 5: c.call(kSongPan, {song, value + c.r8(song + 0x12e)}); break;
case 6: c.call(kSongPan, {song, c.r8(song + 0x12e) - value}); break;
case 7:
if (c.r32(kTickUnit) == 0)
throw std::runtime_error("MODMIDI: a song's fade time divided by a zero tick unit");
c.w32(song + 0x120, (value * 3125u << 12) / c.r32(kTickUnit));
break;
case 8:
c.w32(song + 0x124, c.r8(song + 0x129) == value ? 0 : c.r32(song + 0x120));
if (c.r32(song + 0x124))
c.w8(song + 0x128, value);
else
c.call(kSongVolume, {song, value});
break;
case 0x11:
if (c.r8(song + 0x12c) & 4)
{
const uint32_t next = c.r32(song + 0x11c);
if (c.r8(next) != 0xa1)
return fail(kSongLoopError, c.r8(next));
c.w32(song + 0x11c, next + 3);
}
else if (c.call(kSongRepeat, {player, value}) == 0)
return c.ret(c.call(kSongStop, {env, port, 0}));
break;
case 0x21: c.call(kSongTempo, {song, value}); break;
case 0x22: c.call(kSongTempo, {song, value + c.r8(song + 0x12f)}); break;
case 0x23: c.call(kSongTempo, {song, c.r8(song + 0x12f) - value}); break;
case 0x7f:
if (value == 1)
c.call(kPlaySwitch, {env, port, 0});
else if (value == 0)
{
c.call(kPlaySwitch, {env, port, 0});
c.call(kAllChannels, {env, port, 0x78b0, 3});
}
else if (value == 0x7f)
c.w32(player + 0xc, (c.r32(player + 0xc) & ~0x301u) | 0x400);
else
return fail(kSongUnknownControl, value);
break;
default: return fail(kSongUnknownOperation, op);
}
v = c.r32(player + 0xc) & 0x100;
}
c.ret(v);
}
// sceMidi_SongPlaySwitch(env, port, mode): 0 stop (back to the start), 1 pause, 2 go on. -1 no song.
void songStop(Call &c)
{
const uint32_t env = c.arg(0), port = c.arg(1);
const int32_t mode = c.sarg(2);
uint32_t context, song;
if (!songPlayer(c, env, port, context, song) || c.r32(context) == 0xffffffffu)
return c.ret(0xffffffffu);
if (mode == 1)
{
c.w32(context + 0xc, c.r32(context + 0xc) & ~0x101u);
c.w8(song + 0x12c, 0);
c.call(kPlaySwitch, {env, port, 0});
return c.ret(0);
}
if (mode == 0)
{
c.w32(context + 0xc, c.r32(context + 0xc) & ~0x701u);
c.w8(song + 0x12c, 0);
c.call(kPlaySwitch, {env, port, 0});
if (c.r32(song + 0x124))
{
c.w32(song + 0x124, 0);
c.w8(song + 0x129, c.r8(song + 0x128));
}
c.call(kSongReset, {context, song + 0x118});
return c.ret(0);
}
if (mode != 2)
return c.ret(0xffffffffu);
if (c.r32(context + 0xc) & 0x100)
return c.ret(0);
c.w8(song + 0x12c, c.r8(song + 0x12c) & 0xfc);
const uint32_t flags = c.r32(context + 0xc);
if (flags & 0x400)
c.w32(context + 0xc, flags & ~0x301u);
else
{
c.w32(context + 0xc, flags | 0x100);
if (flags & 0x200)
c.call(kPlaySwitch, {env, port, 1});
}
c.ret(0);
}
// (env, port, player), each tick while the song plays: its fade stepped, a pending seek done, its events.
void songTick(Call &c)
{
const uint32_t env = c.arg(0), port = c.arg(1), player = c.arg(2);
if (!(c.r32(player + 0xc) & 0x100))
return c.ret(0);
const uint32_t song = player + 0x54;
if (c.r32(song + 0x124))
{
c.w32(song + 0x124, c.r32(song + 0x124) - 1);
if (c.r32(song + 0x124) == 0)
c.w8(song + 0x129, c.r8(song + 0x128));
c.call(kSongFade, {env, port, song});
}
const uint32_t flags = c.r32(player + 0xc);
if ((flags & 0x202) == 0x202)
return c.ret(0);
if (c.r8(song + 0x12c) & 1)
{
if (!c.call(kSongSearch, {player, song, c.r8(song + 0x12d), c.r8(song + 0x12c) & 2u}))
return c.ret(0);
c.w32(player + 0xc, c.r32(player + 0xc) | (flags & 0x700));
}
c.ret(c.call(kSongEvents, {env, port, player}));
}
// (context, song, blocks, keep): from the song's start, past `blocks` block events (repeat marks skipped);
// the next event is the one after (keep: the block's own). 1, or 0 at the song's end or an error (the song
// marked ended).
void songSearch(Call &c)
{
const uint32_t context = c.arg(0), song = c.arg(1), blocks = c.arg(2), keep = c.arg(3);
c.call(kSongReset, {context, song + 0x118});
uint32_t after = c.r32(song + 0x11c);
c.w8(song + 0x12c, 4);
auto ended = [&] {
c.w32(context + 0xc, (c.r32(context + 0xc) & ~0x301u) | 0x400);
c.w8(song + 0x12c, 0);
c.ret(0);
};
for (uint32_t seen = 0;;)
{
const uint32_t at = c.r32(song + 0x11c);
c.w32(song + 0x11c, at + 1);
if (c.r8(at) != 0xa0)
{
c.call(kPrintf, {kSearchUnknownStatus, c.r8(at)});
return ended();
}
const uint32_t op = c.r8(at + 1), value = c.r8(at + 2);
c.w32(song + 0x11c, at + 3);
if (op == 0x11)
{
if (c.r8(at + 3) != 0xa1)
{
c.call(kPrintf, {kSearchLoopError, c.r8(at + 3)});
return ended();
}
c.w32(song + 0x11c, at + 6);
}
else if (op == 0)
{
if (blocks < ++seen)
{
c.w32(song + 0x11c, keep ? after : at);
c.w8(song + 0x12c, 0);
return c.ret(1);
}
after = at + 3;
}
else if (op == 0x7f && value == 0x7f)
return ended();
}
}
// sceMidi_SongSetVolume / SetPan... (env, port, value): the song's level (+0x12a), volume, tempo, relative
// tempo kept and applied if its block plays. -1 no such port.
void songSetLevel(Call &c)
{
uint32_t context, song;
if (!songPlayer(c, c.arg(0), c.arg(1), context, song))
return c.ret(0xffffffffu);
c.w8(song + 0x12a, c.arg(2));
if (c.r32(context + 0xc) & 0x200)
c.call(kSongFade, {c.arg(0), c.arg(1), song});
c.ret(0);
}
void songSetVolume(Call &c)
{
uint32_t context, song;
if (!songPlayer(c, c.arg(0), c.arg(1), context, song))
return c.ret(0xffffffffu);
c.call(kSongVolume, {song, c.arg(2) & 0xff});
if (c.r32(context + 0xc) & 0x200)
c.call(kSongFade, {c.arg(0), c.arg(1), song});
c.ret(0);
}
void songSetTempo(Call &c)
{
uint32_t context, song;
if (!songPlayer(c, c.arg(0), c.arg(1), context, song))
return c.ret(0xffffffffu);
c.call(kSongTempo, {song, c.arg(2) & 0xff});
if (c.r32(context + 0xc) & 0x200)
c.call(kSetAbsoluteTempo, {c.arg(0), c.arg(1), c.r8(song + 0x12f)});
c.ret(0);
}
void songSetRelativeTempo(Call &c)
{
uint32_t context, song;
if (!songPlayer(c, c.arg(0), c.arg(1), context, song))
return c.ret(0xffffffffu);
c.w16(song + 0x130, c.arg(2));
if (c.r32(context + 0xc) & 0x200)
c.call(kSetRelativeTempo, {c.arg(0), c.arg(1), c.arg(2) & 0xffff});
c.ret(0);
}
// sceMidi_SongSearchPlay(env, port, blocks, flags): the song played from past `blocks` block events (flags 1
// while it plays: on its next tick; 2: from that block itself). -1 no song or none so far.
void songSearchPlay(Call &c)
{
const uint32_t env = c.arg(0), port = c.arg(1), blocks = c.arg(2), flags = c.arg(3);
uint32_t context, song;
if (!songPlayer(c, env, port, context, song) || c.r32(context) == 0xffffffffu)
return c.ret(0xffffffffu);
const uint32_t state = c.r32(context + 0xc) & 0x700;
if (c.r32(context + 0xc) & 0x100)
{
if (flags & 1)
{
c.w8(song + 0x12c, (flags & 2) ? 1 : 3);
c.w8(song + 0x12d, blocks);
return c.ret(0);
}
c.call(kPlaySwitch, {env, port, 0});
if (c.r32(song + 0x124))
{
c.w32(song + 0x124, 0);
c.w8(song + 0x129, c.r8(song + 0x128));
}
}
if (!c.call(kSongSearch, {context, song, blocks, ((flags >> 1) ^ 1) & 1}))
return c.ret(0xffffffffu);
if (!(state & 0x100))
return c.ret(0);
c.ret(c.call(kSongStop, {env, port, 2}));
}
constexpr uint32_t kPlaySong = 0x4e2e0;
void tick(Call &c)
{
const uint32_t env = c.arg(0), info = c.r32(env + 4);
for (int32_t k = 1; k < c.s32(info); k += 2)
{
const uint32_t player = c.r32(c.r32(info + 4) + 8 * k + 4);
if (c.r32(player + 0xc) & 2)
playBlock(c, player, info + 8);
if (c.r32(player + 0xc) & 0x100)
c.call(kPlaySong, {env, static_cast<uint32_t>(k / 2), player});
}
c.ret(0);
}
} // namespace
void bindModmidi(bozo::native::irx::CompiledLibraries &host)
{
iop::bind(host, 0x4b800, start);
iop::bind(host, 0x4b830, initFn);
iop::bind(host, 0x4b99c, chunkIsFn);
iop::bind(host, 0x4ba88, loadFn);
iop::bind(host, 0x4bc40, tick);
iop::bind(host, 0x4bd10, findLoopFn);
iop::bind(host, 0x4bd44, loopControlFn);
iop::bind(host, 0x4bffc, tickRateFn);
iop::bind(host, 0x4c038, readNumberFn);
iop::bind(host, 0x4c06c, readDeltaFn);
iop::bind(host, 0x4c0c4, skipLengthFn);
iop::bind(host, 0x4c108, metaEventFn);
iop::bind(host, 0x4c274, streamFn);
iop::bind(host, 0x4c29c, exclusiveEventFn);
iop::bind(host, 0x4c420, channelMessageFn);
iop::bind(host, 0x4c598, channelLevelFn);
iop::bind(host, 0x4c5e0, playEventsFn);
iop::bind(host, 0x4cacc, allNotesOffFn);
iop::bind(host, 0x4cb80, playBlockFn);
iop::bind(host, 0x4cc30, restoreChannelsFn);
iop::bind(host, 0x4ced4, playSwitchFn);
iop::bind(host, 0x4cfa4, rewindFn);
iop::bind(host, 0x4d05c, selectMidiFn);
iop::bind(host, 0x4d200, setLocationFn);
iop::bind(host, 0x4d2f0, sendVolumeFn);
iop::bind(host, 0x4d884, playerFn);
iop::bind(host, 0x4d8d4, songReset);
iop::bind(host, 0x4d374, setVolumeFn);
iop::bind(host, 0x4d464, volumeChangeFn);
iop::bind(host, 0x4d578, setAbsoluteTempo);
iop::bind(host, 0x4d5dc, getAbsoluteTempo);
iop::bind(host, 0x4d678, setRelativeTempo);
iop::bind(host, 0x4d6c4, getRelativeTempo);
iop::bind(host, 0x4d714, allChannels);
iop::bind(host, 0x4d7c0, songFadeFn);
iop::bind(host, kSongSelect, songSelect);
iop::bind(host, kSongVolume, songVolume);
iop::bind(host, kSongPan, songPan);
iop::bind(host, kSongTempo, songTempo);
iop::bind(host, kSongBlock, songBlock);
iop::bind(host, kSongRepeat, songRepeat);
iop::bind(host, kSongEvents, songEvents);
iop::bind(host, kSongStop, songStop);
iop::bind(host, kPlaySong, songTick);
iop::bind(host, 0x4e3cc, songSetLevel);
iop::bind(host, 0x4e450, songSetVolume);
iop::bind(host, 0x4e4d4, songSetTempo);
iop::bind(host, 0x4e560, songSetRelativeTempo);
iop::bind(host, kSongSearch, songSearch);
iop::bind(host, 0x4e750, songSearchPlay);
}
} // namespace oni::drivers