Files

116 lines
4.9 KiB
C++

// MODMSIN.IRX natively: Sony's MIDI stream input module of the modular synthesizer. A context (sceMSIn's: +0 the
// number of its entries, at least 2; +4 its info) names ports (info +8 their count, +0xc an array of 8-byte entries,
// +4 each port's stream buffer); a buffer is {+0 its size, +4 the bytes used, +8 the bytes}. Messages are appended
// to a port's buffer for the module it feeds (modmidi, modhsyn) to take at its tick.
#include "oni_iop.h"
#include "oni_sound_native.h"
namespace oni::drivers
{
namespace
{
using iop::Call;
constexpr uint32_t kBase = 0x58500;
// module start: its export table registered (loadcore RegisterLibraryEntries), resident (0) when it was.
constexpr uint32_t kExports = 0x58b50, kRegisterLibraryEntries = 0x58bb4;
void start(Call &c) { c.ret(c.call(kRegisterLibraryEntries, {kExports}) == 0 ? 0 : 1); }
// sceMSIn_Init(ctx): -1 unless the context has its two entries, its info, ports, and each port a buffer of at
// least 8 bytes.
void init(Call &c)
{
const uint32_t ctx = c.arg(0);
if (!ctx)
return c.ret(-1);
const uint32_t info = c.r32(ctx + 4);
if (c.s32(ctx) < 2 || !info)
return c.ret(-1);
const uint32_t ports = c.r32(info + 0xc);
if (!c.r32(info + 8) || !ports)
return c.ret(-1);
for (int32_t i = 0; i < c.s32(info + 8); ++i)
{
const uint32_t buffer = c.r32(ports + 8 * i + 4);
if (!buffer || c.r32(buffer) < 8)
return c.ret(-1);
}
c.ret(0);
}
// sceMSIn's ticks: nothing to do (the buffers are read by their consumers).
void tick(Call &c) { c.ret(0); }
// The port's buffer gets length bytes from data: -1 for a port it does not have or a buffer they do not fit.
int32_t put(Call &c, uint32_t ctx, uint32_t port, uint32_t data, uint32_t length)
{
const uint32_t info = c.r32(ctx + 4);
if (port >= c.r32(info + 8))
return -1;
const uint32_t buffer = c.r32(c.r32(info + 0xc) + 8 * port + 4);
const uint32_t used = c.r32(buffer + 4);
if (c.r32(buffer) < used + length + 8)
return -1;
c.w32(buffer + 4, used + length);
for (uint32_t k = 0; k < length; ++k)
c.w8(buffer + 8 + used + k, c.r8(data + k));
return 0;
}
void putBytes(Call &c) { c.ret(put(c, c.arg(0), c.arg(1), c.arg(2), c.arg(3))); }
// sceMSIn_PutMsg(ctx, port, message): a channel message, its status in the low byte: 0x80 (as Sony counts it),
// 0xC0, 0xD0 two bytes; 0x90, 0xA0, 0xB0, 0xE0 three; anything else -1. The bytes are the message word's as it
// lies in memory (the caller's argument home).
void putMessage(Call &c)
{
const uint32_t message = c.arg(2);
uint32_t length;
switch (message & 0xf0)
{
case 0x80: case 0xc0: case 0xd0: length = 2; break;
case 0x90: case 0xa0: case 0xb0: case 0xe0: length = 3; break;
default: return c.ret(-1);
}
// The compiled code passes the address of its argument's home slot (sp + 8 of its own frame).
const uint32_t home = c.e.cpu.gpr[29] + 8;
c.w32(home, message);
c.ret(put(c, c.arg(0), c.arg(1), home, length));
}
// sceMSIn_PutExcMsg(ctx, port, data, size): a system exclusive message (0xF0 ... 0xF7) of at most size bytes.
void putExclusive(Call &c)
{
const uint32_t data = c.arg(2), size = c.arg(3);
if (c.r8(data) != 0xf0)
return c.ret(-1);
for (uint32_t length = 2; length <= size; ++length)
if (c.r8(data + length - 1) == 0xf7)
return c.ret(put(c, c.arg(0), c.arg(1), data, length));
c.ret(-1);
}
// sceMSIn_PutHsMsg(ctx, port, data): the synthesizer's own messages, 0xF9 (5 bytes) and 0xFD (7 bytes).
void putSynth(Call &c)
{
const uint32_t data = c.arg(2);
const uint32_t status = c.r8(data);
const uint32_t length = status == 0xf9 ? 5 : status == 0xfd ? 7 : 0;
if (!length)
return c.ret(-1);
c.ret(put(c, c.arg(0), c.arg(1), data, length));
}
} // namespace
void bindModmsin(bozo::native::irx::CompiledLibraries &host)
{
iop::bind(host, kBase + 0x000, start);
iop::bind(host, 0x58550, init);
iop::bind(host, 0x586bc, tick);
iop::bind(host, 0x586e4, tick);
iop::bind(host, 0x5870c, putBytes);
iop::bind(host, 0x5889c, putMessage);
iop::bind(host, 0x589b8, putExclusive);
iop::bind(host, 0x58aac, putSynth);
}
} // namespace oni::drivers