From 8d74e2b1f3f7a59d569386506034393cac6c8396 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 5 Apr 2012 19:54:01 -0500 Subject: add script to execute binary files --- bin/dcpu16-execute | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 bin/dcpu16-execute diff --git a/bin/dcpu16-execute b/bin/dcpu16-execute new file mode 100644 index 0000000..43a24b3 --- /dev/null +++ b/bin/dcpu16-execute @@ -0,0 +1,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: $!"; +} -- cgit v1.2.3