zxspectrum 0.1.0 zxspectrum: ^0.1.0 copied to clipboard
Emulator for a Sinclair ZX Spectrum, a popular 8-bit home computer of the 1980s
import 'dart:io';
import 'dart:math' as math;
import 'package:zxspectrum/zxspectrum.dart';
// Good breakpoint in 48K ROM that represents a time when the machine has booted
const breakpoint = 0x15e6;
const testRuns = 500;
double median(List<int> list) {
final copy = List<int>.from(list);
copy.sort((a, b) => a.compareTo(b));
final middle = copy.length ~/ 2;
if (copy.length % 2 == 1) {
return copy[middle].toDouble();
} else {
return (copy[middle - 1] + copy[middle]) / 2.0;
}
}
int runTest(Spectrum spectrum) {
// Boots spectrum and runs through to breakpoint
final stopwatch = Stopwatch()..start();
final start = stopwatch.elapsedMicroseconds;
while (spectrum.z80.pc != breakpoint) {
spectrum.z80.executeNextInstruction();
}
final end = stopwatch.elapsedMicroseconds;
return end - start;
}
void resetTest(Spectrum spectrum) => spectrum.reset();
void main() async {
final rom = File('roms/48.rom').readAsBytesSync();
final spectrum = Spectrum(rom);
final runTimes = <int>[];
print('Warming up...');
for (var i = 0; i < 10; i++) {
runTest(spectrum);
resetTest(spectrum);
}
print('Measuring the speed of $testRuns boot times.');
for (var i = 0; i < testRuns; i++) {
runTimes.add(runTest(spectrum));
resetTest(spectrum);
}
final maxMillisecs = runTimes.reduce(math.max) / 1000;
final minMillisecs = runTimes.reduce(math.min) / 1000;
final medianMillisecs = median(runTimes) / 1000;
print('Median boot time: ${medianMillisecs.toStringAsFixed(1)}ms.');
print('Maximum boot time: ${maxMillisecs.toStringAsFixed(1)}ms.');
print('Minimum boot time: ${minMillisecs.toStringAsFixed(1)}ms.');
}