| 1 | package CPU::Emulator::DCPU16::Disassembler; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use CPU::Emulator::DCPU16; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | CPU::Emulator::DCPU16::Disassembler - a disassembler for DCPU-16 bytecode | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # Disassemble a single instruction | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | my $instruction = CPU::Emulator::DCPU16::Disassembler->disassemble($pc, @memory); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # Dump a whole program | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | my $asm = CPU::Emulator::DCPU16::Disassembler->dump($bytes); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 5 | our @OPCODES = qw(NOOP SET ADD SUB MUL DIV MOD SHL SHR AND BOR XOR IFE IFN IFG IFB); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | our @REGISTERS = qw(A B C X Y Z I J); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 13 | ▶ | sub _get_operand { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_operand: called CC=12 SCAR=28.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 13 | my $n = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 13 | my $pc = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 13 | my @mem = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 13 | ▶ | if ($n < 0x08) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 11 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | sprintf("%s", $REGISTERS[$n & 7]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | } elsif ($n < 0x10) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | sprintf("[%s]", $REGISTERS[$n & 7]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | } elsif ($n < 0x18) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | sprintf("[0x%04x+%s]", $mem[$$pc++], $REGISTERS[$n & 7]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } elsif ($n == 0x18) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | "POP" | |||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } elsif ($n == 0x19) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | "PEEK" | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } elsif ($n == 0x1A) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | "PUSH" | |||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | } elsif ($n == 0x1B) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | "SP" | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } elsif ($n == 0x1C) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | "PC" | |||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } elsif ($n == 0x1D) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | "O" | |||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } elsif ($n == 0x1E) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | sprintf("[0x%04x]", $mem[$$pc++]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } elsif ($n == 0x1F) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | sprintf("0x%04x", $mem[$$pc++]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 2 | ($n - 0x20); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | =head2 disassemble <pc> <memory> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | Given a program counter and an array of memory words will dissassemble the current instruction. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 7 | ▶ | sub disassemble { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
disassemble: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 7 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 7 | my $pc = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 7 | my @mem = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 7 | my $word = $mem[$pc++]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 7 | my $op = $word & 0xF; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 7 | my $a = ($word >> 4) & 0x3F; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 7 | my $b = ($word >> 10); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | my $ret = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 7 | ▶ | if ($op > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 6 | $ret .= $OPCODES[$op]." "; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 6 | $ret .= _get_operand($a, \$pc, @mem); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 6 | $ret .= ", "; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 6 | $ret .= _get_operand($b, \$pc, @mem); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } elsif ($a == 0x01) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | $ret .= "JSR "._get_operand($b, \$pc, @mem); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $ret .= sprintf("UNK[%02x] ", $a)._get_operand($b, \$pc, @mem); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 7 | ▶ | wantarray ? ($ret, $pc) : $ret; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | =head2 dump <words> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | Given an scalar containing program bytecode will return a string representing the assembler. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 5 | our $CODE_INDENT = 10; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | sub dump { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dump: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | my $bytes = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | my @words = CPU::Emulator::DCPU16->bytes_to_array($bytes); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | my $pc = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | my %labels = (); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | my %lines = (); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | ▶ | while ($pc < scalar(@words)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 3 | my ($tmp, $new_pc) = $class->disassemble($pc, @words); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | ▶ | if ($tmp =~ /^(JSR|SET PC,)\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | my $addr = "$2"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # TODO potentially replace faux address labels with generated ones | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | $labels{hex($addr)} = $addr if $addr =~ /^0x/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 3 | $lines{$pc} = $tmp; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | $pc = $new_pc; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 1 | my $indent = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 1 | my $ret = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | foreach $pc (sort { $a <=> $b } keys %lines) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | my $line = $lines{$pc}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | #$ret .= sprintf "%d (0x%04x) ", $pc, $pc; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | ▶ | if ($labels{$pc}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | $ret .= ":".$labels{$pc} . " " ." "x ($CODE_INDENT-length($labels{$pc})-2); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 2 | $ret .= " "x$CODE_INDENT; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | $ret .= " " x $indent; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | $ret .= "$line\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 3 | ▶ | if ($line =~ /^IF/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | $indent++; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } elsif ($indent) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | $indent--; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | return $ret; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||