112 lines
4.6 KiB
C++
112 lines
4.6 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#pragma once
|
|
// Real ROM boot, CMOC development launch, PIA input and guest audio.
|
|
#include "../../../src/core/MachineSession.h"
|
|
#include "../../../src/devices/FD502/Fd502Device.h"
|
|
#include "../../../src/devices/SpeechSoundDevice.h"
|
|
#include "../../../src/devices/Orchestra90Device.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
|
|
using namespace VCC::Mac;
|
|
using namespace VCC::Devices;
|
|
namespace fs = std::filesystem;
|
|
using Bytes = std::vector<std::uint8_t>;
|
|
|
|
static Bytes readFile(const fs::path& path) {
|
|
std::ifstream file(path, std::ios::binary);
|
|
if (!file) throw std::runtime_error("Cannot open " + path.string());
|
|
return {std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
|
|
}
|
|
|
|
static void require(bool condition, const std::string& message) {
|
|
if (!condition) throw std::runtime_error(message);
|
|
}
|
|
|
|
struct Session {
|
|
MachineSession machine;
|
|
std::shared_ptr<Fd502Device> floppy;
|
|
std::shared_ptr<SpeechSoundDevice> ssc;
|
|
std::vector<StereoSample> audio;
|
|
|
|
Session(Processor cpu, const Bytes& system, const Bytes& diskRom,
|
|
std::shared_ptr<SpeechSoundDevice> cartridge, bool useOrchestra = false,
|
|
std::shared_ptr<CartridgeDevice> observer = nullptr)
|
|
: floppy(std::make_shared<Fd502Device>(diskRom)), ssc(std::move(cartridge)) {
|
|
auto orchestra = useOrchestra ? std::make_shared<Orchestra90Device>() : nullptr;
|
|
machine.cartridges().configure({nullptr, ssc, orchestra, observer, floppy}, true, 4);
|
|
machine.load(system, cpu);
|
|
machine.resume();
|
|
for (unsigned n = 0; n < 180; ++n) machine.advance();
|
|
machine.takeAudio();
|
|
require(contains("DISK EXTENDED"), "FD-502 in startup slot 4 did not boot Disk BASIC");
|
|
}
|
|
|
|
bool contains(const std::string& text) { return machine.copyScreenText().find(text) != std::string::npos; }
|
|
void tick() {
|
|
machine.advance();
|
|
auto samples = machine.takeAudio();
|
|
audio.insert(audio.end(), samples.begin(), samples.end());
|
|
}
|
|
void until(const std::string& text, unsigned maxFrames = 360) {
|
|
for (unsigned i = 0; i < maxFrames; ++i) {
|
|
if (contains(text)) return;
|
|
tick();
|
|
}
|
|
throw std::runtime_error("Timed out waiting for " + text + "\n" + machine.copyScreenText());
|
|
}
|
|
void launch(const Bytes& binary) {
|
|
require(binary.size() >= 5, "Incomplete binary");
|
|
auto entry = std::uint16_t(binary[binary.size() - 2] << 8 | binary.back());
|
|
machine.debuggerLoadBinary(binary, entry);
|
|
machine.debuggerClearDevelopmentBreakpoints();
|
|
machine.resume();
|
|
}
|
|
void press(unsigned bit, bool shift = false) {
|
|
machine.setKeyboard((std::uint64_t{1} << bit) | (std::uint64_t(shift) << 55));
|
|
for (int i = 0; i < 6; ++i) tick();
|
|
machine.setKeyboard(0);
|
|
for (int i = 0; i < 6; ++i) tick();
|
|
}
|
|
void diskExec(const Bytes& disk, const std::string& basename) {
|
|
floppy->mount(0, DiskImage::load(disk, true));
|
|
for (char c : "LOADM\"" + basename + "\":EXEC\r") {
|
|
if (c == '"') press(34, true);
|
|
else if (c == ':') press(42);
|
|
else if (c == '\r') press(48);
|
|
else if (c >= 'A' && c <= 'Z') press(c - '@');
|
|
else throw std::logic_error("Unsupported fixture key");
|
|
}
|
|
}
|
|
void checkSignal(std::size_t from) const {
|
|
require(from < audio.size(), "Missing PCM samples");
|
|
auto range = std::minmax_element(audio.begin() + from, audio.end(),
|
|
[](auto a, auto b) { return a.left < b.left; });
|
|
require(range.second->left - range.first->left > 0.02f, "Expected audible output");
|
|
}
|
|
};
|
|
|
|
|
|
static void saveWav(const fs::path& path, const std::vector<StereoSample>& pcm) {
|
|
std::ofstream file(path, std::ios::binary);
|
|
auto little = [&](unsigned value, unsigned bytes) {
|
|
while (bytes--) { file.put(char(value)); value >>= 8; }
|
|
};
|
|
const unsigned size = unsigned(pcm.size()) * 4;
|
|
file.write("RIFF", 4); little(36 + size, 4); file.write("WAVEfmt ", 8);
|
|
little(16, 4); little(1, 2); little(2, 2); little(44100, 4); little(176400, 4);
|
|
little(4, 2); little(16, 2); file.write("data", 4); little(size, 4);
|
|
for (auto sample : pcm) {
|
|
for (float value : {sample.left, sample.right}) {
|
|
const auto converted = std::lround(value * 32767.0f);
|
|
little(static_cast<std::uint16_t>(std::clamp(converted, -32768l, 32767l)), 2);
|
|
}
|
|
}
|
|
if (!file) throw std::runtime_error("Cannot save " + path.string());
|
|
}
|