aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2012-04-05 19:54:01 -0500
committerJesse Luehrs <doy@tozt.net>2012-04-05 19:54:01 -0500
commit8d74e2b1f3f7a59d569386506034393cac6c8396 (patch)
tree85d08f64b557be125f92d97d576a94aa9362bd47
parent48775888ab7747a4502de5be6e4efbf910c33022 (diff)
downloadgames-emulation-dcpu16-8d74e2b1f3f7a59d569386506034393cac6c8396.tar.gz
games-emulation-dcpu16-8d74e2b1f3f7a59d569386506034393cac6c8396.zip
add script to execute binary files
-rw-r--r--bin/dcpu16-execute50
1 files changed, 50 insertions, 0 deletions
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: $!";
+}