1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-17 10:06:21 +00:00

Use less branchy inner loop.

This commit is contained in:
Thomas Harte 2023-11-21 22:42:53 -05:00
parent bcd4a2216a
commit 1828a10885

View File

@ -578,10 +578,6 @@ class ConcreteMachine:
public MachineTypes::ScanProducer public MachineTypes::ScanProducer
{ {
public: public:
// i.e. CPU clock rate is 1/3 * ~1.19Mhz ~= 0.4 MIPS.
static constexpr int CPUMultiplier = 1;
static constexpr int CPUDivisor = 3;
ConcreteMachine( ConcreteMachine(
[[maybe_unused]] const Analyser::Static::Target &target, [[maybe_unused]] const Analyser::Static::Target &target,
const ROMMachine::ROMFetcher &rom_fetcher const ROMMachine::ROMFetcher &rom_fetcher
@ -618,27 +614,31 @@ class ConcreteMachine:
// bool log = false; // bool log = false;
// std::string previous; // std::string previous;
void run_for(const Cycles duration) override { void run_for(const Cycles duration) override {
auto pit_ticks = duration.as_integral(); const auto pit_ticks = duration.as_integral();
while(pit_ticks--) { cpu_divisor_ += pit_ticks;
int ticks = cpu_divisor_ / 3;
cpu_divisor_ %= 3;
while(ticks--) {
// //
// First draft: all hardware runs in lockstep, as a multiple or divisor of the PIT frequency. // First draft: all hardware runs in lockstep, as a multiple or divisor of the PIT frequency.
// //
// Advance the PIT. //
// Advance the PIT and audio.
//
pit_.run_for(1);
++speaker_.cycles_since_update;
pit_.run_for(1);
++speaker_.cycles_since_update;
pit_.run_for(1); pit_.run_for(1);
// Advance audio clock.
++speaker_.cycles_since_update; ++speaker_.cycles_since_update;
// Advance the CPU. //
cpu_divisor_ += CPUMultiplier; // Perform one CPU instruction every three PIT cycles.
int cycles = cpu_divisor_ / CPUDivisor; // i.e. CPU instruction rate is 1/3 * ~1.19Mhz ~= 0.4 MIPS.
cycles %= CPUDivisor; //
// To consider: a Duff-esque switch table calling into a function templated on clock phase
// might alleviate a large part of the conditionality here?
while(cycles--) {
// Query for interrupts and apply if pending. // Query for interrupts and apply if pending.
if(pic_.pending() && context.flags.flag<InstructionSet::x86::Flag::Interrupt>()) { if(pic_.pending() && context.flags.flag<InstructionSet::x86::Flag::Interrupt>()) {
// Regress the IP if a REP is in-progress so as to resume it later. // Regress the IP if a REP is in-progress so as to resume it later.
@ -690,7 +690,6 @@ class ConcreteMachine:
); );
} }
} }
}
// MARK: - ScanProducer. // MARK: - ScanProducer.
void set_scan_target([[maybe_unused]] Outputs::Display::ScanTarget *scan_target) override {} void set_scan_target([[maybe_unused]] Outputs::Display::ScanTarget *scan_target) override {}