From 48775888ab7747a4502de5be6e4efbf910c33022 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 5 Apr 2012 19:53:20 -0500 Subject: add some pretty printers --- lib/Games/Emulation/DCPU16/Util.pm | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 lib/Games/Emulation/DCPU16/Util.pm diff --git a/lib/Games/Emulation/DCPU16/Util.pm b/lib/Games/Emulation/DCPU16/Util.pm new file mode 100644 index 0000000..3b5235e --- /dev/null +++ b/lib/Games/Emulation/DCPU16/Util.pm @@ -0,0 +1,48 @@ +package Games::Emulation::DCPU16::Util; +use strict; +use warnings; + +use Exporter 'import'; + +our @EXPORT_OK = ('format_registers', 'format_memory'); + +sub format_registers { + my ($registers, $sp, $pc, $o) = @_; + + return join("\n", + sprintf("A: 0x%04x X: 0x%04x I: 0x%04x", + $registers->[0], $registers->[3], $registers->[6]), + sprintf("B: 0x%04x Y: 0x%04x J: 0x%04x", + $registers->[1], $registers->[4], $registers->[7]), + sprintf("C: 0x%04x Z: 0x%04x", + $registers->[2], $registers->[5]), + "", + sprintf("SP: 0x%04x PC: 0x%04x O: 0x%04x", $sp, $pc, $o), + "", + ); +} + +sub format_memory { + my ($memory) = @_; + + my $out = ''; + my $eliding = 0; + for my $addr (0..int(@$memory / 8)) { + $addr *= 8; + my @values = @{ $memory }[$addr..($addr + 7)]; + if (grep { $_ } @values) { + $out .= sprintf("%04x:" . (" %04x" x 8), $addr, @values) . "\n"; + $eliding = 0; + } + else { + if (!$eliding) { + $out .= "...\n"; + $eliding = 1; + } + } + } + + return $out; +} + +1; -- cgit v1.2.3