2024-08-08 13:12:37 -07:00
|
|
|
#include "zsm_backend.hpp"
|
|
|
|
extern "C" {
|
2024-10-14 21:27:16 -07:00
|
|
|
#include "x16emu/glue.h"
|
2024-08-08 13:12:37 -07:00
|
|
|
#include "x16emu/vera_pcm.h"
|
|
|
|
#include "x16emu/vera_psg.h"
|
|
|
|
#include "x16emu/ymglue.h"
|
|
|
|
}
|
|
|
|
#include <exception>
|
|
|
|
#include <filesystem>
|
|
|
|
#include "file_backend.hpp"
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <file_backend.hpp>
|
2024-10-14 21:27:16 -07:00
|
|
|
#define HZ (AUDIO_SAMPLERATE)
|
|
|
|
#define BUFFERS 32
|
2024-08-08 13:12:37 -07:00
|
|
|
void ZsmBackend::load(const char *filename) {
|
2024-09-28 10:31:06 -07:00
|
|
|
memset(&spec, 0, sizeof(spec));
|
2024-08-08 13:12:37 -07:00
|
|
|
spec.format = AUDIO_S16SYS;
|
2024-10-14 21:27:16 -07:00
|
|
|
spec.samples = 100;
|
2024-09-16 15:05:53 -07:00
|
|
|
spec.channels = 2;
|
2024-10-14 21:27:16 -07:00
|
|
|
spec.freq = PSG_FREQ;
|
|
|
|
spec.size = 100 * 2 * sizeof(int16_t);
|
2024-08-08 13:12:37 -07:00
|
|
|
file = open_file(filename);
|
|
|
|
char magic[2];
|
2024-09-16 15:05:53 -07:00
|
|
|
file->read(magic, 2, 1);
|
2024-08-08 13:12:37 -07:00
|
|
|
if (magic[0] != 0x7a || magic[1] != 0x6d) {
|
|
|
|
throw std::exception();
|
|
|
|
}
|
|
|
|
uint8_t version;
|
2024-09-16 15:05:53 -07:00
|
|
|
file->read(&version, 1, 1);
|
2024-08-08 13:12:37 -07:00
|
|
|
uint8_t loop_point[3];
|
2024-09-16 15:05:53 -07:00
|
|
|
file->read(loop_point, 3, 1);
|
2024-08-08 13:12:37 -07:00
|
|
|
this->loop_point = loop_point[0] | ((uint32_t)(loop_point[1]) << 8) | ((uint32_t)(loop_point[2]) << 16);
|
2024-09-16 15:05:53 -07:00
|
|
|
file->read(loop_point, 3, 1);
|
2024-08-08 13:12:37 -07:00
|
|
|
this->pcm_offset = loop_point[0] | ((uint32_t)(loop_point[1]) << 8) | ((uint32_t)(loop_point[2]) << 16);
|
2024-10-14 21:27:16 -07:00
|
|
|
pcm_offset += 3;
|
2024-09-16 15:05:53 -07:00
|
|
|
file->read(&fm_mask, 1, 1);
|
|
|
|
file->read(loop_point, 2, 1);
|
2024-08-08 13:12:37 -07:00
|
|
|
this->psg_channel_mask = loop_point[0] | ((uint16_t)(loop_point[1]) << 8);
|
2024-09-16 15:05:53 -07:00
|
|
|
file->read(loop_point, 2, 1);
|
2024-08-08 13:12:37 -07:00
|
|
|
this->tick_rate = loop_point[0] | ((uint16_t)(loop_point[1]) << 8);
|
2024-09-16 15:05:53 -07:00
|
|
|
file->read(loop_point, 2, 1); // Reserved.
|
2024-08-08 13:12:37 -07:00
|
|
|
music_data_start = file->get_pos();
|
2024-10-14 21:27:16 -07:00
|
|
|
this->loop_point += music_data_start;
|
|
|
|
file->seek(pcm_offset, SeekType::SET);
|
|
|
|
file->read(loop_point, 1, 1);
|
|
|
|
pcm_offset++;
|
|
|
|
pcm_data_offs = (loop_point[0] * 16) + pcm_offset;
|
|
|
|
file->seek(music_data_start, SeekType::SET);
|
|
|
|
double time = 0.0;
|
|
|
|
double tmpDelayTicks = 0.0;
|
2024-08-08 13:12:37 -07:00
|
|
|
while (true) {
|
2024-10-14 21:27:16 -07:00
|
|
|
tmpDelayTicks -= get_delay_per_frame();
|
|
|
|
if (tmpDelayTicks < 0.0) {
|
|
|
|
ZsmCommand cmd = get_command();
|
|
|
|
if (cmd.id == ZsmEOF) {
|
|
|
|
break;
|
|
|
|
} else if (cmd.id == Delay) {
|
|
|
|
time += ((double)cmd.delay) / ((double)(tick_rate));
|
|
|
|
tmpDelayTicks += cmd.delay;
|
|
|
|
}
|
2024-08-08 13:12:37 -07:00
|
|
|
}
|
|
|
|
}
|
2024-10-14 21:27:16 -07:00
|
|
|
length = time;
|
2024-08-08 13:12:37 -07:00
|
|
|
music_data_len = file->get_pos();
|
|
|
|
switch_stream(0);
|
2024-10-14 21:27:16 -07:00
|
|
|
loop_end = length;
|
|
|
|
loop_start = ((double)this->loop_point) / ((double)tick_rate);
|
|
|
|
fm_stream = SDL_NewAudioStream(AUDIO_S16SYS, 2, YM_FREQ, AUDIO_S16SYS, 2, PSG_FREQ);
|
|
|
|
DEBUG.writefln("fm_stream: %ld -> %ld", YM_FREQ, PSG_FREQ);
|
2024-08-08 13:12:37 -07:00
|
|
|
}
|
|
|
|
extern SDL_AudioSpec obtained;
|
|
|
|
void ZsmBackend::switch_stream(int idx) {
|
2024-10-14 21:27:16 -07:00
|
|
|
YM_Create(YM_FREQ);
|
|
|
|
YM_init(YM_FREQ/64, 60);
|
2024-08-08 13:12:37 -07:00
|
|
|
psg_reset();
|
2024-10-14 21:27:16 -07:00
|
|
|
for (uint8_t i = 0; i < 16; i++) {
|
|
|
|
psg_writereg(i * 4 + 2, 0);
|
|
|
|
}
|
|
|
|
this->cpuClocks = 0.0;
|
|
|
|
this->delayTicks = 0.0;
|
|
|
|
this->ticks = 0.0;
|
2024-08-08 13:12:37 -07:00
|
|
|
}
|
|
|
|
void ZsmBackend::cleanup() {
|
|
|
|
delete file;
|
|
|
|
file = nullptr;
|
2024-10-14 21:27:16 -07:00
|
|
|
audio_buf.clear();
|
|
|
|
SDL_FreeAudioStream(fm_stream);
|
|
|
|
fm_stream = nullptr;
|
2024-08-08 13:12:37 -07:00
|
|
|
}
|
2024-10-14 21:27:16 -07:00
|
|
|
void ZsmBackend::tick(bool step) {
|
|
|
|
delayTicks -= 1;
|
|
|
|
const double ClocksPerTick = ((double)HZ) / ((double)tick_rate);
|
|
|
|
ssize_t ticks_remaining = ClocksPerTick;
|
|
|
|
while (delayTicks <= 0) {
|
|
|
|
ZsmCommand cmd = get_command();
|
|
|
|
switch (cmd.id) {
|
|
|
|
case ZsmEOF: {
|
|
|
|
if (step) {
|
|
|
|
file->seek(this->loop_point, SeekType::SET);
|
|
|
|
this->position = this->loop_pos;
|
|
|
|
} else {
|
|
|
|
throw std::exception();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case PsgWrite: {
|
|
|
|
psg_writereg(cmd.psg_write.reg, cmd.psg_write.val);
|
|
|
|
} break;
|
|
|
|
case FmWrite: {
|
|
|
|
for (uint8_t i = 0; i < cmd.fm_write.len; i++) {
|
|
|
|
YM_write_reg(cmd.fm_write.regs[i].reg, cmd.fm_write.regs[i].val);
|
|
|
|
while (YM_read_status()) {
|
|
|
|
size_t clocksToAddForYm = 64;
|
|
|
|
ticks_remaining -= clocksToAddForYm;
|
|
|
|
if (ticks_remaining < 0) {
|
|
|
|
delayTicks -= 1;
|
|
|
|
cpuClocks += ClocksPerTick;
|
|
|
|
ticks_remaining += ClocksPerTick;
|
|
|
|
}
|
|
|
|
audio_step(clocksToAddForYm);
|
2024-08-08 13:12:37 -07:00
|
|
|
}
|
2024-10-14 21:27:16 -07:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case Delay: {
|
|
|
|
delayTicks += cmd.delay;
|
|
|
|
position += ((double)cmd.delay) / ((double)(tick_rate));
|
|
|
|
} break;
|
|
|
|
case ExtCmd: {
|
|
|
|
//cmd.extcmd.channel
|
|
|
|
switch (cmd.extcmd.channel) {
|
|
|
|
case 0: {
|
|
|
|
for (size_t i = 0; i < cmd.extcmd.bytes; i += 2) {
|
|
|
|
switch (cmd.extcmd.pcm[i]) {
|
|
|
|
case 0: { // ctrl
|
|
|
|
pcm_write_ctrl(cmd.extcmd.pcm[i + 1]);
|
|
|
|
} break;
|
|
|
|
case 1: { // rate
|
|
|
|
pcm_write_rate(cmd.extcmd.pcm[i + 1]);
|
|
|
|
} break;
|
|
|
|
default: { // trigger
|
|
|
|
size_t file_pos = file->get_pos();
|
|
|
|
uint8_t ctrl = pcm_read_ctrl();
|
|
|
|
pcm_write_ctrl(ctrl | 0x80);
|
|
|
|
uint16_t pcm_idx = cmd.extcmd.pcm[i + 1];
|
|
|
|
uint16_t instdef = pcm_idx * 16;
|
|
|
|
file->seek(pcm_offset + instdef, SeekType::SET);
|
|
|
|
uint8_t geom;
|
|
|
|
file->read(&geom, 1, 1);
|
|
|
|
ctrl = pcm_read_ctrl() & 0x0F;
|
|
|
|
ctrl |= geom & 0x30;
|
|
|
|
pcm_write_ctrl(ctrl);
|
|
|
|
uint8_t bytes[10];
|
|
|
|
file->read(bytes, 10, 1);
|
|
|
|
loop_rem = bytes[9];
|
|
|
|
loop_rem <<= 8;
|
|
|
|
loop_rem |= bytes[8];
|
|
|
|
loop_rem <<= 8;
|
|
|
|
loop_rem |= bytes[7];
|
|
|
|
loop = loop_rem & 0xFFFF;
|
|
|
|
islooped = bytes[6];
|
|
|
|
remain = bytes[5];
|
|
|
|
remain <<= 8;
|
|
|
|
remain |= bytes[4];
|
|
|
|
remain <<= 8;
|
|
|
|
remain |= bytes[3];
|
|
|
|
cur = bytes[2];
|
|
|
|
cur <<= 8;
|
|
|
|
cur |= bytes[1];
|
|
|
|
cur <<= 8;
|
|
|
|
cur |= bytes[0];
|
|
|
|
pcm_loop_point = cur + loop;
|
|
|
|
rem_point = remain - loop_rem;
|
|
|
|
file->seek(file_pos, SeekType::SET);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
//cmd.extcmd.pcm
|
|
|
|
audio_step(0);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
// Nothing handled yet.
|
|
|
|
}
|
|
|
|
} break;
|
2024-08-08 13:12:37 -07:00
|
|
|
}
|
|
|
|
}
|
2024-10-14 21:27:16 -07:00
|
|
|
audio_step(ticks_remaining);
|
|
|
|
cpuClocks += ClocksPerTick;
|
|
|
|
}
|
|
|
|
void ZsmBackend::add_clocks(double amount, bool step) {
|
|
|
|
const double ClocksPerTick = ((double)HZ) / ((double)tick_rate);
|
|
|
|
cpuClocks = std::fmod(cpuClocks, ClocksPerTick);
|
|
|
|
double prevCpuClocks = cpuClocks;
|
|
|
|
size_t prevIntCpuClocks = prevCpuClocks;
|
|
|
|
double tickDelta = amount / ClocksPerTick;
|
|
|
|
double prevTicks = ticks;
|
|
|
|
ticks += tickDelta;
|
|
|
|
size_t prevIntTicks = prevTicks;
|
|
|
|
size_t intTicks = ticks;
|
|
|
|
size_t intTicksDelta = intTicks - prevIntTicks;
|
|
|
|
cpuClocks += intTicks * ClocksPerTick;
|
|
|
|
double remainder = amount - (intTicksDelta * ClocksPerTick);
|
|
|
|
size_t intCpuClocks = cpuClocks;
|
|
|
|
size_t intCpuClockDelta = intCpuClocks - prevIntCpuClocks;
|
|
|
|
double initialTicks = prevCpuClocks / ClocksPerTick;
|
|
|
|
for (size_t i = 0; i < intTicksDelta; i++) {
|
|
|
|
double preTickCpuClocks = cpuClocks;
|
|
|
|
delayTicks--;
|
|
|
|
tick(step);
|
|
|
|
double neededCpuClocks = preTickCpuClocks + ClocksPerTick;
|
|
|
|
if (cpuClocks < neededCpuClocks) {
|
|
|
|
cpuClocks = neededCpuClocks;
|
|
|
|
}
|
2024-09-28 10:31:06 -07:00
|
|
|
}
|
2024-10-14 21:27:16 -07:00
|
|
|
if (remainder >= 0) {
|
|
|
|
cpuClocks += remainder;
|
|
|
|
audio_step(remainder);
|
2024-09-16 15:05:53 -07:00
|
|
|
}
|
2024-10-14 21:27:16 -07:00
|
|
|
}
|
|
|
|
size_t ZsmBackend::render(void *buf, size_t maxlen) {
|
|
|
|
size_t sample_type_len = 2;
|
|
|
|
maxlen /= sample_type_len;
|
|
|
|
while (audio_buf.size() <= maxlen) {
|
|
|
|
tick(true);
|
|
|
|
}
|
|
|
|
size_t copied = copy_out(buf, maxlen) * sample_type_len;
|
|
|
|
maxlen *= sample_type_len;
|
|
|
|
return copied;
|
|
|
|
}
|
|
|
|
uint64_t ZsmBackend::get_min_samples() {
|
|
|
|
return spec.size;
|
|
|
|
}
|
|
|
|
std::optional<uint64_t> ZsmBackend::get_max_samples() {
|
|
|
|
return get_min_samples();
|
2024-08-08 13:12:37 -07:00
|
|
|
}
|
|
|
|
ZsmCommand ZsmBackend::get_command() {
|
|
|
|
ZsmCommandId cmdid;
|
|
|
|
uint8_t cmd_byte;
|
2024-09-16 15:05:53 -07:00
|
|
|
file->read(&cmd_byte, 1, 1);
|
2024-08-08 13:12:37 -07:00
|
|
|
if (cmd_byte == 0x80) {
|
|
|
|
cmdid = ZsmEOF;
|
|
|
|
} else {
|
2024-09-28 10:31:06 -07:00
|
|
|
if ((cmd_byte >> 6) == 0) {
|
2024-08-08 13:12:37 -07:00
|
|
|
cmdid = PsgWrite;
|
2024-09-28 10:31:06 -07:00
|
|
|
} else if ((cmd_byte >> 6) == 0b01) {
|
2024-08-08 13:12:37 -07:00
|
|
|
if (cmd_byte == 0b01000000) {
|
|
|
|
cmdid = ExtCmd;
|
|
|
|
} else {
|
|
|
|
cmdid = FmWrite;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cmdid = Delay;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ZsmCommand output;
|
|
|
|
output.id = cmdid;
|
|
|
|
if (cmdid == ZsmEOF) {
|
|
|
|
return output;
|
|
|
|
} else if (cmdid == PsgWrite) {
|
|
|
|
uint8_t value;
|
2024-09-16 15:05:53 -07:00
|
|
|
file->read(&value, 1, 1);
|
2024-08-08 13:12:37 -07:00
|
|
|
output.psg_write.reg = cmd_byte & 0x3F;
|
|
|
|
output.psg_write.val = value;
|
|
|
|
} else if (cmdid == FmWrite) {
|
2024-09-28 10:31:06 -07:00
|
|
|
uint16_t _value;
|
|
|
|
uint8_t *value = (uint8_t*)(void*)(&_value);
|
2024-08-08 13:12:37 -07:00
|
|
|
uint8_t pairs = cmd_byte & 0b111111;
|
|
|
|
output.fm_write.len = pairs;
|
2024-09-28 10:31:06 -07:00
|
|
|
output.fm_write.regs = (reg_pair*)malloc((sizeof(reg_pair))*pairs);
|
2024-08-08 13:12:37 -07:00
|
|
|
for (uint8_t i = 0; i < pairs; i++) {
|
2024-09-28 10:31:06 -07:00
|
|
|
file->read(value, 2, 1);
|
2024-08-08 13:12:37 -07:00
|
|
|
output.fm_write.regs[i].reg = value[0];
|
|
|
|
output.fm_write.regs[i].val = value[1];
|
|
|
|
}
|
|
|
|
} else if (cmdid == ExtCmd) {
|
|
|
|
uint8_t ext_cmd_byte;
|
2024-09-16 15:05:53 -07:00
|
|
|
file->read(&ext_cmd_byte, 1, 1);
|
2024-08-08 13:12:37 -07:00
|
|
|
uint8_t bytes = ext_cmd_byte & 0x3F;
|
|
|
|
uint8_t ch = ext_cmd_byte >> 6;
|
|
|
|
output.extcmd.channel = ch;
|
|
|
|
output.extcmd.bytes = bytes;
|
|
|
|
if (ch == 1) {
|
2024-09-28 10:31:06 -07:00
|
|
|
output.extcmd.expansion.write_bytes = NULL;
|
2024-08-08 13:12:37 -07:00
|
|
|
} else {
|
|
|
|
output.extcmd.pcm = (uint8_t*)malloc(bytes); // Handles all other cases due to them being in a union, and each having the same type.
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < bytes; i++) {
|
|
|
|
uint8_t byte;
|
2024-09-16 15:05:53 -07:00
|
|
|
file->read(&byte, 1, 1);
|
2024-08-08 13:12:37 -07:00
|
|
|
switch (ch) {
|
|
|
|
case 0: {
|
|
|
|
output.extcmd.pcm[i] = byte;
|
|
|
|
} break;
|
|
|
|
case 1: {
|
|
|
|
if (i == 0) {
|
|
|
|
output.extcmd.expansion.chip_id = byte;
|
|
|
|
} else if (i == 1) {
|
|
|
|
output.extcmd.expansion.writes = byte;
|
2024-09-28 10:31:06 -07:00
|
|
|
output.extcmd.expansion.write_bytes = (uint8_t*)malloc(byte);
|
2024-08-08 13:12:37 -07:00
|
|
|
} else {
|
|
|
|
output.extcmd.expansion.write_bytes[i - 2] = byte;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case 2: {
|
|
|
|
output.extcmd.sync[i] = byte;
|
|
|
|
} break;
|
|
|
|
case 3: {
|
|
|
|
output.extcmd.custom[i] = byte;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
2024-10-14 21:27:16 -07:00
|
|
|
} else if (cmdid == Delay) {
|
|
|
|
output.delay = cmd_byte & 0x7F;
|
2024-08-08 13:12:37 -07:00
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
ZsmCommand::~ZsmCommand() {
|
|
|
|
switch (id) {
|
|
|
|
case ExtCmd: {
|
|
|
|
if (extcmd.channel == 1) {
|
2024-09-28 10:31:06 -07:00
|
|
|
if (extcmd.expansion.write_bytes != NULL) {
|
|
|
|
free(extcmd.expansion.write_bytes);
|
|
|
|
}
|
2024-08-08 13:12:37 -07:00
|
|
|
} else {
|
|
|
|
free(extcmd.pcm);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case FmWrite: {
|
|
|
|
free(fm_write.regs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-14 21:27:16 -07:00
|
|
|
void ZsmBackend::seek_internal(double position, bool loop) {
|
|
|
|
switch_stream(0);
|
2024-08-08 13:12:37 -07:00
|
|
|
file->seek(music_data_start, SeekType::SET);
|
2024-10-14 21:27:16 -07:00
|
|
|
this->cpuClocks = 0.0;
|
|
|
|
this->delayTicks = 0;
|
|
|
|
this->ticks = 0.0;
|
|
|
|
this->position = 0.0;
|
|
|
|
while (this->position < position) {
|
|
|
|
audio_buf.clear();
|
|
|
|
try {
|
|
|
|
tick(false);
|
|
|
|
} catch (std::exception) {
|
|
|
|
switch_stream(0);
|
2024-08-08 13:12:37 -07:00
|
|
|
file->seek(music_data_start, SeekType::SET);
|
2024-10-14 21:27:16 -07:00
|
|
|
this->cpuClocks = 0.0;
|
|
|
|
this->delayTicks = 0;
|
|
|
|
this->ticks = 0.0;
|
|
|
|
this->position = 0.0;
|
|
|
|
return;
|
2024-08-08 13:12:37 -07:00
|
|
|
}
|
|
|
|
}
|
2024-10-14 21:27:16 -07:00
|
|
|
size_t samples = (this->position - position) * PSG_FREQ;
|
|
|
|
while (samples--) {
|
|
|
|
audio_buf.pop();
|
|
|
|
}
|
|
|
|
this->position = std::floor(position * PSG_FREQ) / PSG_FREQ;
|
2024-08-08 13:12:37 -07:00
|
|
|
}
|
|
|
|
void ZsmBackend::seek(double position) {
|
2024-10-14 21:27:16 -07:00
|
|
|
seek_internal(position, false);
|
2024-08-08 13:12:37 -07:00
|
|
|
}
|
|
|
|
double ZsmBackend::get_position() {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
int ZsmBackend::get_stream_idx() {
|
|
|
|
return 0;
|
|
|
|
}
|