Skip to content

Instantly share code, notes, and snippets.

@jbarrett
Last active March 13, 2025 13:58
Show Gist options
  • Save jbarrett/4f6d57170290cd600205d62ad2b75f0c to your computer and use it in GitHub Desktop.
Save jbarrett/4f6d57170290cd600205d62ad2b75f0c to your computer and use it in GitHub Desktop.
A headless and non-interactive ZX Spectrum emulator.
#!/usr/bin/env perl
use v5.40;
use autodie;
use CPU::Emulator::Z80;
my $sna = $ARGV[0];
open my $fh, '<:raw', $sna;
read $fh, my $header, 27;
read $fh, my $ram, 49152;
open $fh, '<:raw', 'spec48.rom';
read $fh, my $rom, 16384;
my (
$I,
$HL_, $D_, $E_, $B_, $C_, $A_, $F_,
$HL, $D, $E, $B, $C, $IY, $IX,
$Int,
$R,
$A, $F, $SP,
$IntMode,
$Border
) = unpack '
C
S C C C C C C
S C C C C S S
C
C
C C S
C
C
', $header;
my $cpu = CPU::Emulator::Z80->new(
memory => $rom . $ram,
ports => 65536,
init_A => $A,
init_B => $B,
init_C => $C,
init_D => $D,
init_E => $E,
init_F => $F,
init_R => $R,
init_HL => $HL,
init_IX => $IX,
init_IY => $IY,
init_A_ => $A_,
init_B_ => $B_,
init_C_ => $C_,
init_D_ => $D_,
init_E_ => $E_,
init_F_ => $F_,
init_HL_ => $HL_,
init_SP => $SP,
init_PC => 0x72,
);
# Dummy devices to prevent crashing - keyboard handling for keys 1-5
$cpu->add_input_device( address => 0xf7fe, function => sub { } );
$cpu->add_output_device( address => 0xf7fe, function => sub { } );
sub screenshot( $filename = 'screen.scr' ) {
open $fh, '>:raw', $filename;
print $fh join '', map { chr( $cpu->memory->peek8( $_ ) ) } 16384..16384+6911;
}
screenshot( 'tge1.scr' );
$cpu->run( 5_000_000 );
screenshot( 'tge2.scr' );
$cpu->run( 5_000_000 );
screenshot( 'tge3.scr' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment