aboutsummaryrefslogtreecommitdiffstats
path: root/bin/dcpu16-execute
blob: 43a24b3c1d4a5522678f48b385fb90443a805e41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env perl
use strict;
use warnings;

use Games::Emulation::DCPU16;
use Games::Emulation::DCPU16::Util 'format_registers', 'format_memory';
use Getopt::Long;
use Time::HiRes;

my $iterations;
my $dump_file = 'dcpu16.dump';
GetOptions(
    'iterations=i' => \$iterations,
    'dump=s'       => \$dump_file,
);

my $bin = $ARGV[0];
open my $fh, '<', $bin or die "Couldn't open $bin for reading: $!";

my $bytecode = do { local $/; <$fh> };

my $cpu = Games::Emulation::DCPU16->new;
$cpu->load($bytecode);

my $time = Time::HiRes::time;
if (defined $iterations) {
    $cpu->step for 1..$iterations;
}
else {
    $cpu->run;
}

END {
    my $duration = Time::HiRes::time - $time;
    open my $dump_fh, '>', $dump_file
        or die "Couldn't open $dump_file for writing: $!";

    print { $dump_fh } "Executed for " . $cpu->clock
                     . " cycles in $duration seconds\n\n";

    print { $dump_fh } "Registers:\n"
                     . format_registers($cpu->registers,
                                        $cpu->SP, $cpu->PC, $cpu->O)
                     . "\n";

    print { $dump_fh } "Memory:\n"
                     . format_memory($cpu->memory);

    close($dump_fh) or die "Couldn't close $dump_file: $!";
}