diff options
author | Jesse Luehrs <doy@tozt.net> | 2012-04-05 19:53:20 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2012-04-05 19:53:20 -0500 |
commit | 48775888ab7747a4502de5be6e4efbf910c33022 (patch) | |
tree | c12e76140d9b5dc9dfc49896910c0a745a475e08 /lib/Games/Emulation/DCPU16 | |
parent | 2e07d46bcac1879ed39452688a3dca344ea6acf5 (diff) | |
download | games-emulation-dcpu16-48775888ab7747a4502de5be6e4efbf910c33022.tar.gz games-emulation-dcpu16-48775888ab7747a4502de5be6e4efbf910c33022.zip |
add some pretty printers
Diffstat (limited to 'lib/Games/Emulation/DCPU16')
-rw-r--r-- | lib/Games/Emulation/DCPU16/Util.pm | 48 |
1 files changed, 48 insertions, 0 deletions
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; |