Files

237 lines
8.6 KiB
C++

// Host for TBG's SPC700 sound driver: replays an op list from re/tools/snd_build_spc.py against blargg's
// snes_spc, doing exactly the CPU-side port handshakes of the bank-$88 primitives, then renders audio.
// The IPL boot is skipped: the driver block is placed in RAM and the SPC starts at the IPL jump target,
// which is the state the IPL ROM leaves (so no IPL ROM image is needed).
//
// build: g++ -O2 -std=c++17 -Ithird_party/snes_spc re/tools/snd_play.cpp third_party/snes_spc/snes_spc/{SNES_SPC,SNES_SPC_misc,SNES_SPC_state,SPC_DSP,SPC_Filter}.cpp -o re/runs/snd/snd_play
// usage: snd_play OPS OUT_PREFIX -> OUT_PREFIX.wav (all "play" segments), OUT_PREFIX_N.spc after each play,
// OUT_PREFIX_N.ram (64 KiB SPC RAM) + .dsp (128 B) after each play
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "snes_spc/SNES_SPC.h"
namespace {
std::vector<uint8_t> unhex(const std::string& s) {
std::vector<uint8_t> v(s.size() / 2);
for (size_t i = 0; i < v.size(); ++i) v[i] = static_cast<uint8_t>(std::stoul(s.substr(i * 2, 2), nullptr, 16));
return v;
}
struct Host {
SNES_SPC spc;
long t = 0;
int last0 = 0; // WRAM $76: last command value written to port 0 (88:8589)
std::vector<short> buf = std::vector<short>(1 << 20);
std::vector<short> wav;
bool capture = false;
int rd(int p) {
t += 24;
return spc.read_port(t, p);
}
int rd16(int p) {
const int lo = rd(p);
return lo | (spc.read_port(t, p + 1) << 8);
}
void wr(int p, int v) {
t += 24;
spc.write_port(t, p, v & 0xFF);
}
void wr16(int p, int v) {
wr(p, v);
spc.write_port(t, p + 1, (v >> 8) & 0xFF);
}
void flush() {
spc.end_frame(t);
const int n = spc.sample_count();
if (capture) wav.insert(wav.end(), buf.begin(), buf.begin() + n);
spc.set_output(buf.data(), static_cast<int>(buf.size()));
t = 0;
}
void maybe_flush() {
if (t > 200000) flush();
}
template <class F>
void wait(F cond, const char* what) {
long guard = 0;
while (!cond()) {
maybe_flush();
if (++guard > 20'000'000) {
std::fprintf(stderr, "timeout waiting for %s\n", what);
std::exit(1);
}
}
}
void play_frames(int frames) {
flush();
capture = true;
for (int f = 0; f < frames; ++f) {
t += 1024000 / 60;
flush();
}
capture = false;
}
};
void write_wav(const std::string& path, const std::vector<short>& s) {
std::ofstream f(path, std::ios::binary);
auto u32 = [&](uint32_t v) { f.write(reinterpret_cast<const char*>(&v), 4); };
auto u16 = [&](uint16_t v) { f.write(reinterpret_cast<const char*>(&v), 2); };
f.write("RIFF", 4);
u32(36 + static_cast<uint32_t>(s.size() * 2));
f.write("WAVEfmt ", 8);
u32(16);
u16(1);
u16(2);
u32(32000);
u32(32000 * 4);
u16(4);
u16(16);
f.write("data", 4);
u32(static_cast<uint32_t>(s.size() * 2));
f.write(reinterpret_cast<const char*>(s.data()), static_cast<std::streamsize>(s.size() * 2));
}
} // namespace
int main(int argc, char** argv) {
if (argc < 3) {
std::fprintf(stderr, "usage: %s OPS OUT_PREFIX\n", argv[0]);
return 2;
}
std::ifstream in(argv[1]);
const std::string prefix = argv[2];
std::vector<std::string> lines;
for (std::string l; std::getline(in, l);)
if (!l.empty()) lines.push_back(l);
// Initial state = what the IPL leaves after uploading the driver: RAM blocks + PC at the jump target.
std::vector<uint8_t> file(0x10200, 0);
std::memcpy(file.data(), "SNES-SPC700 Sound File Data v0.30", 33);
file[0x21] = 26;
file[0x22] = 26;
file[0x23] = 27; // no ID666 tag
file[0x24] = 30;
uint8_t* ram = &file[0x100];
uint8_t* dsp = &file[0x10100];
dsp[0x6C] = 0xE0; // FLG after reset: soft reset, mute, echo writes off
int pc = 0;
size_t first_op = 0;
for (; first_op < lines.size(); ++first_op) {
std::istringstream ls(lines[first_op]);
std::string k;
ls >> k;
if (k == "ipl") {
std::string a, h;
ls >> a >> h;
const auto data = unhex(h);
std::memcpy(ram + std::stoul(a, nullptr, 16), data.data(), data.size());
} else if (k == "iplend") {
std::string a;
ls >> a;
pc = static_cast<int>(std::stoul(a, nullptr, 16));
} else {
break;
}
}
file[0x25] = static_cast<uint8_t>(pc);
file[0x26] = static_cast<uint8_t>(pc >> 8);
file[0x2B] = 0xEF; // SP left by the IPL
ram[0xF1] = 0x00; // IPL ROM off, timers off
Host h;
static const uint8_t no_rom[SNES_SPC::rom_size] = {};
if (h.spc.init()) return 1;
h.spc.init_rom(no_rom);
if (const char* err = h.spc.load_spc(file.data(), static_cast<long>(file.size()))) {
std::fprintf(stderr, "load_spc: %s\n", err);
return 1;
}
h.spc.clear_echo();
h.spc.set_output(h.buf.data(), static_cast<int>(h.buf.size()));
int snapshot = 0;
for (size_t i = first_op; i < lines.size(); ++i) {
std::istringstream ls(lines[i]);
std::string k;
ls >> k;
auto num = [&]() {
std::string v;
ls >> v;
return static_cast<int>(std::stoul(v, nullptr, 16));
};
if (k == "enter-transfer") { // 88:8570 (16-bit stores)
h.wr16(2, 0x0000);
h.wait([&] { return h.rd16(2) == 0x0265; }, "transfer ack $0265");
h.wr16(0, 0x0000);
h.wr16(2, 0x0265);
} else if (k == "end-transfer") { // 88:8561
h.wait([&] { return h.rd16(2) == 0x0968; }, "driver ready $0968");
h.last0 = 0;
} else if (k == "cmd0") { // 88:8589
const int c = num();
h.wait([&] { return h.rd(0) == h.last0; }, "port0 idle");
const int v = ((h.rd(0) ^ 0x80) & 0x80) | c;
h.wr(0, v);
h.last0 = v;
} else if (k == "cmd1") { // 88:85A5 (no wait)
const int c = num();
h.wr(1, ((h.rd(1) ^ 0x80) & 0x80) | c);
} else if (k == "addr") { // 88:8301
h.wr16(2, num());
const int v = (h.rd(0) ^ 0x80) | 0x40;
h.wr(0, v);
h.wait([&] { return h.rd(0) == (v & 0xFF); }, "addr ack");
} else if (k == "word") { // 88:831A
h.wr16(2, num());
const int v = (h.rd(0) ^ 0x80) & 0x80;
h.wr(0, v);
h.wait([&] { return h.rd(0) == v; }, "word ack");
} else if (k == "stream") { // 88:827A = 8301 + words
const int dest = num();
std::string hx;
ls >> hx;
const auto data = unhex(hx);
h.wr16(2, dest);
int v = (h.rd(0) ^ 0x80) | 0x40;
h.wr(0, v);
h.wait([&] { return h.rd(0) == (v & 0xFF); }, "stream addr ack");
for (size_t j = 0; j + 1 < data.size() + 1; j += 2) {
h.wr16(2, data[j] | ((j + 1 < data.size() ? data[j + 1] : 0) << 8));
v = (h.rd(0) ^ 0x80) & 0x80;
h.wr(0, v);
h.wait([&] { return h.rd(0) == v; }, "stream ack");
}
} else if (k == "kick") { // 88:8A38
h.wr(0, (h.rd(0) ^ 0x80) | 0x01);
} else if (k == "port") {
const int p = num();
h.wr(p, num());
} else if (k == "play") {
h.play_frames(num());
++snapshot;
std::vector<uint8_t> out(SNES_SPC::spc_file_size);
h.spc.save_spc(out.data());
const std::string base = prefix + "_" + std::to_string(snapshot);
std::ofstream(base + ".spc", std::ios::binary).write(reinterpret_cast<const char*>(out.data()),
static_cast<std::streamsize>(out.size()));
std::ofstream(base + ".ram", std::ios::binary).write(reinterpret_cast<const char*>(out.data() + 0x100), 0x10000);
std::ofstream(base + ".dsp", std::ios::binary).write(reinterpret_cast<const char*>(out.data() + 0x10100), 128);
} else {
std::fprintf(stderr, "unknown op '%s'\n", k.c_str());
return 1;
}
h.maybe_flush();
}
write_wav(prefix + ".wav", h.wav);
std::printf("%zu samples (%.1f s)\n", h.wav.size() / 2, static_cast<double>(h.wav.size()) / 64000.0);
return 0;
}