| 1 | package CPU::Z80::Disassembler::Instruction; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | CPU::Z80::Disassembler::Instruction - One Z80 disassembled instruction | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 11 | ▶ | use Asm::Z80::Table; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use CPU::Z80::Disassembler::Memory; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 11 | ▶ | use CPU::Z80::Disassembler::Format; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 11 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | use CPU::Z80::Disassembler::Instruction; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | $instr = CPU::Z80::Disassembler::Instruction->disassemble( | ||||||||||||||||||||||||||||||||||||||||||
| 28 | $memory, $addr, $limit_addr); | ||||||||||||||||||||||||||||||||||||||||||
| 29 | $instr = CPU::Z80::Disassembler::Instruction->defb($memory, $addr, $count); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | $instr = CPU::Z80::Disassembler::Instruction->defb2($memory, $addr, $count); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | $instr = CPU::Z80::Disassembler::Instruction->defw($memory, $addr, $count); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | $instr = CPU::Z80::Disassembler::Instruction->defm($memory, $addr, $length); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | $instr = CPU::Z80::Disassembler::Instruction->defmz($memory, $addr); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | $instr = CPU::Z80::Disassembler::Instruction->defm7($memory, $addr); | ||||||||||||||||||||||||||||||||||||||||||
| 35 | $instr = CPU::Z80::Disassembler::Instruction->org($memory, $addr); | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | $instr->addr; $instr->next_addr; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | $instr->bytes; $instr->opcode; $instr->N; $instr->NN; $instr->DIS; $instr->STR; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | $instr->comment; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | print $instr->dump; | ||||||||||||||||||||||||||||||||||||||||||
| 41 | print $instr->asm; | ||||||||||||||||||||||||||||||||||||||||||
| 42 | print $instr->as_string, "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | This module represents one disassembled instruction. The object is | ||||||||||||||||||||||||||||||||||||||||||
| 47 | constructed by one of the factory methods, and has attributes to ease the | ||||||||||||||||||||||||||||||||||||||||||
| 48 | interpretation of the instruction. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | =head1 CONSTRUCTORS | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | =head2 disassemble | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | Factory method to create a new object by disassembling the given | ||||||||||||||||||||||||||||||||||||||||||
| 55 | L<CPU::Z80::Disassembler::Memory|CPU::Z80::Disassembler::Memory> object | ||||||||||||||||||||||||||||||||||||||||||
| 56 | at the given address. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | The C<$limit_addr> argument, if defined, tells the disassembler to select | ||||||||||||||||||||||||||||||||||||||||||
| 59 | the longest possible instruction, that does not use the byte at C<$limit_add>. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | The default is to select the shortest possible instruction. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | For example, the sequence of bytes C<62 6B> is decoded as C<ld h,d> if | ||||||||||||||||||||||||||||||||||||||||||
| 63 | C<$limit_addr> is undef. | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | If C<$limit_addr> is defined with any value different from C<$addr + 1>, where | ||||||||||||||||||||||||||||||||||||||||||
| 66 | the second byte is stored, then the same sequence of bytes is decoded as | ||||||||||||||||||||||||||||||||||||||||||
| 67 | C<ld hl,de>. | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | To decode standard Z80 instructions, do not pass the C<$limit_addr> argument. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | To decode extended Z80 instructions, pass the address of the next label after | ||||||||||||||||||||||||||||||||||||||||||
| 72 | C<$addr>, or 0x10000 to get always the longest instruction. | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | If the instruction at the given address is an invalid opcode, or if there | ||||||||||||||||||||||||||||||||||||||||||
| 75 | are no loaded bytes at the given address, the instrution object is not | ||||||||||||||||||||||||||||||||||||||||||
| 76 | constructed and the factory returns C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =head2 defb | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | Factory method to create a new object by disassembling a C<defb> instruction | ||||||||||||||||||||||||||||||||||||||||||
| 81 | at the given address, reading one or C<$count> byte(s) from memory. | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =head2 defb2 | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | Same as defb but shows binary data. | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | =head2 defw | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | Factory method to create a new object by disassembling a C<defw> instruction | ||||||||||||||||||||||||||||||||||||||||||
| 90 | at the given address, reading one or C<$count> word(s) from memory. | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | =head2 defm | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | Factory method to create a new object by disassembling a C<defm> instruction | ||||||||||||||||||||||||||||||||||||||||||
| 95 | at the given address, reading C<$length> character(s) from memory. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | =head2 defmz | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | Factory method to create a new object by disassembling a C<defmz> instruction | ||||||||||||||||||||||||||||||||||||||||||
| 100 | at the given address, reading character(s) from memory until a zero terminator | ||||||||||||||||||||||||||||||||||||||||||
| 101 | is found. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | =head2 defm7 | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | Factory method to create a new object by disassembling a C<defm7> instruction | ||||||||||||||||||||||||||||||||||||||||||
| 106 | at the given address, reading character(s) from memory until a character | ||||||||||||||||||||||||||||||||||||||||||
| 107 | with bit 7 set is found. | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | =head2 org | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | Factory method to create a new ORG instruction. | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | =head2 memory | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | Point to the memory object from where this instruction was disassembled. | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | =head2 addr | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | Address of the instruction. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | =head2 size | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | Size of the instruction, in bytes. | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | =head2 next_addr | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | Returns the address that follows this instruction. | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | =head2 next_code | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | Returns the list with the next possible addresses where the code flow can continue. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | For an instruction that does not branch, this is the same as C<next_addr>. | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | For a decision-branch instruction, these are the C<next_addr> and the C<NN>. | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | For an instruction that breaks the flow (e.g. C<ret>), this is an empty list. | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | A C<call> or C<rst> instruction is considered as breaking the flow, because | ||||||||||||||||||||||||||||||||||||||||||
| 146 | the called routine might manipulate the return pointer in the stack, and the | ||||||||||||||||||||||||||||||||||||||||||
| 147 | bytes after the C<call> or C<rst> instruction can be data bytes. | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | =head2 bytes | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | Reference to a list of the instruction bytes. The bytes are retrieved | ||||||||||||||||||||||||||||||||||||||||||
| 152 | from the L<CPU::Z80::Disassembler::Memory|CPU::Z80::Disassembler::Memory> | ||||||||||||||||||||||||||||||||||||||||||
| 153 | object. | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | =head2 opcode | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | Canonical assembly instruction, e.g. 'ld a,(NN)'. | ||||||||||||||||||||||||||||||||||||||||||
| 158 | The possible argument types are N, NN, DIS and STR. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | There is one method to get/set each of the argument types. | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | =head2 N | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 8-bit data used by the instruction. | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | =head2 N2 | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 8-bit data used by the instruction, to be shown in base 2. | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | =head2 NN | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 16-bit data used by the instruction. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | =head2 DIS | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | Offset for index register. | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | =head2 STR | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | String for defm* instructions. | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | =head2 comment | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | Comment to be written after a '; ' at the end of the line. | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | =head2 format | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | Returs the hash of special formating functions for each type of argument. These | ||||||||||||||||||||||||||||||||||||||||||
| 188 | functions, if defined, are called instead of the ones in the | ||||||||||||||||||||||||||||||||||||||||||
| 189 | L<CPU::Z80::Disassembler::Format|CPU::Z80::Disassembler::Format> module to format | ||||||||||||||||||||||||||||||||||||||||||
| 190 | each type of argument. | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | For example, to format the 8-bit argument of an instruction as decimal: | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | $instr->format->{N} = sub { my $v = shift; return "$v" }; | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =head1 PREDICATES | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | =head2 is_code | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | Return TRUE if the instruction is a Z80 assembly opcode, FALSE if it is one | ||||||||||||||||||||||||||||||||||||||||||
| 205 | of the data definition or org instructions. | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | =head2 is_call | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | Return TRUE if the instruction is a call instruction, i.e. C<call> or C<rst>. | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | =head2 is_branch | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | Return TRUE if the instruction may branch to another address, the address is | ||||||||||||||||||||||||||||||||||||||||||
| 214 | stored in the C<NN> attribute. This is either a jump or a call instruction. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | =head2 is_break_flow | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | Return TRUE if the instruction breaks the flow at this point and jumps to some | ||||||||||||||||||||||||||||||||||||||||||
| 219 | other part of the code. A call instruction is considered as breaking the flow, | ||||||||||||||||||||||||||||||||||||||||||
| 220 | see C<next_code> above. | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 11 | ▶ | use base 'Class::Accessor'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 11 | __PACKAGE__->mk_accessors( | |||||||||||||||||||||||||||||||||||||||||
| 227 | 'memory', # point to whole memory | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 'addr', # start address | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 'size', # number of bytes of instruction | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 'opcode', # canonical opcode, e.g. 'ld a,(NN)' | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 'N', # 8-bit data | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 'N2', # 8-bit data in binary | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 'NN', # 16-bit data | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 'DIS', # offset for index | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 'STR', # unquoted string for defm* | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 'comment', # comment after instruction | ||||||||||||||||||||||||||||||||||||||||||
| 237 | '_format', # hash of (N, NN, DIS, STR) => custom function to | ||||||||||||||||||||||||||||||||||||||||||
| 238 | # format each type of argument | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 'is_code', # true for a Z80 assembly instruction, | ||||||||||||||||||||||||||||||||||||||||||
| 240 | # false for def*, org, ... | ||||||||||||||||||||||||||||||||||||||||||
| 241 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 244 | sub format { | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 25593 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 25593 | ▶ | $self->_format({}) unless $self->_format; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 25593 | $self->_format; | |||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 11 | my %default_format = ( | |||||||||||||||||||||||||||||||||||||||||
| 252 | N => \&format_hex2, | ||||||||||||||||||||||||||||||||||||||||||
| 253 | N2 => \&format_bin8, | ||||||||||||||||||||||||||||||||||||||||||
| 254 | NN => \&format_hex4, | ||||||||||||||||||||||||||||||||||||||||||
| 255 | DIS => \&format_dis, | ||||||||||||||||||||||||||||||||||||||||||
| 256 | STR => \&format_str, | ||||||||||||||||||||||||||||||||||||||||||
| 257 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 260 | sub next_addr { | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 374867 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
next_addr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 374867 | $self->addr + $self->size; | |||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 266 | sub next_code { | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 16119 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
next_code: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 16119 | my @ret; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 16119 | ▶ | push @ret, $self->NN if $self->is_branch; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 16119 | ▶ | push @ret, $self->next_addr unless $self->is_break_flow; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 16119 | @ret; | |||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 275 | sub bytes { | ||||||||||||||||||||||||||||||||||||||||||
| 276 | 51044 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bytes: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 51044 | my @bytes; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 51044 | for my $addr ($self->addr .. $self->next_addr - 1) { | |||||||||||||||||||||||||||||||||||||||||
| 279 | 80474 | push @bytes, $self->memory->peek($addr); | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 51044 | \@bytes; | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 285 | # predicates | ||||||||||||||||||||||||||||||||||||||||||
| 286 | 251451 | ▶ | sub is_call { shift->opcode =~ /call|rst/ } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_call: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 234389 | ▶ | sub is_branch { shift->opcode =~ /jp .*NN|jr|djnz|call|rst/ } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_branch: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 272001 | ▶ | sub is_break_flow { shift->opcode =~ /ret$|reti|retn|call NN|rst|jr NN|jp NN|jp \(\w+\)|org/ } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_break_flow: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 291 | sub disassemble { | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 73216 | ▶ | my($class, $memory, $addr, $limit_addr) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
disassemble: called CC=22 SCAR=31.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | 73216 | my $self = bless { memory => $memory, | |||||||||||||||||||||||||||||||||||||||||
| 295 | addr => $addr, | ||||||||||||||||||||||||||||||||||||||||||
| 296 | is_code => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 297 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | # save bytes of all decoded instructions | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 73216 | my @found; # other instructions found | |||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 73216 | my $table = Asm::Z80::Table->disasm_table; | |||||||||||||||||||||||||||||||||||||||||
| 303 | 73216 | ▶ | for ( ; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $limit_addr and @found and $addr == $limit_addr
defined $limit_addr and @found
MC/DC100%3/3
defined $limit_addr and @found and $addr == $limit_addr
defined $limit_addr
@found
$addr == $limit_addr
Truth table100%4/4
A and B and C
A= defined $limit_addrB= @foundC= $addr == $limit_addr
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | # exit if second instruction goes over limit, e.g. label | ||||||||||||||||||||||||||||||||||||||||||
| 305 | ! (defined($limit_addr) && @found && $addr == $limit_addr) ; | ||||||||||||||||||||||||||||||||||||||||||
| 306 | $addr++ | ||||||||||||||||||||||||||||||||||||||||||
| 307 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 308 | # fetch | ||||||||||||||||||||||||||||||||||||||||||
| 309 | 113183 | my $byte = $memory->peek($addr); | |||||||||||||||||||||||||||||||||||||||||
| 310 | 113183 | ▶ | last unless defined $byte; # unloaded memory | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | # lookup in table | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 113181 | ▶ | if (exists $table->{N}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 6722 | ▶ | die if defined $self->N; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | 6722 | $self->N( $byte ); | |||||||||||||||||||||||||||||||||||||||||
| 316 | 6722 | $table = $table->{N}; | |||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||
| 318 | elsif (exists $table->{NNl}) { | ||||||||||||||||||||||||||||||||||||||||||
| 319 | 11802 | ▶ | die if defined $self->NN; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 11802 | $self->NN( $memory->peek16u($addr++) ); | |||||||||||||||||||||||||||||||||||||||||
| 321 | 11802 | $table = $table->{NNl}{NNh}; | |||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | elsif (exists $table->{NNo}) { | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 7600 | ▶ | die if defined $self->NN; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 7600 | $self->NN( $addr + 1 + $memory->peek8s($addr) ); | |||||||||||||||||||||||||||||||||||||||||
| 326 | 7600 | $table = $table->{NNo}; | |||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | elsif (exists $table->{DIS}) { | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 1879 | ▶ | die if defined $self->DIS; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | 1879 | $self->DIS( $memory->peek8s($addr) ); | |||||||||||||||||||||||||||||||||||||||||
| 331 | 1879 | $table = $table->{DIS}; | |||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||
| 333 | elsif (exists $table->{'DIS+1'}) { | ||||||||||||||||||||||||||||||||||||||||||
| 334 | 21 | ▶ | die unless defined $self->DIS; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | 21 | ▶ | if ( $self->DIS + 1 != $memory->peek8s($addr) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | last; # abort search | |||||||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||||||
| 338 | 21 | $table = $table->{'DIS+1'}; | |||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||
| 340 | elsif (! exists $table->{$byte}) { | ||||||||||||||||||||||||||||||||||||||||||
| 341 | 5636 | last; # abort search | |||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||
| 343 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 344 | 79521 | $table = $table->{$byte}; | |||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | # check for end | ||||||||||||||||||||||||||||||||||||||||||
| 348 | 107545 | ▶ | if (exists $table->{''}) { # possible finish | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 73409 | push @found, [ [@{$table->{''}}], $addr + 1 ]; | |||||||||||||||||||||||||||||||||||||||||
| 350 | # save this instance, copy | ||||||||||||||||||||||||||||||||||||||||||
| 351 | 73409 | ▶ | last unless defined $limit_addr; # no limit -> shortest instr | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | # continue for composite instruction | ||||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | # return undef if no instrution found | ||||||||||||||||||||||||||||||||||||||||||
| 358 | 73216 | ▶ | return undef unless @found; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | # collect last complete instruction found | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 73201 | my($opcode, @args) = @{$found[-1][0]}; | |||||||||||||||||||||||||||||||||||||||||
| 362 | 73201 | ▶ | $opcode .= ' '.join('', @args) if @args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 73201 | $opcode =~ s/,\s*/, /g; | |||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | 73201 | $self->opcode($opcode); | |||||||||||||||||||||||||||||||||||||||||
| 366 | 73201 | $self->size($found[-1][1] - $self->addr); | |||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | # special case: rst -> show address in hex | ||||||||||||||||||||||||||||||||||||||||||
| 369 | 73201 | ▶ | if ($opcode =~ /rst (\d+)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | 5886 | $self->N($1); # set N for display | |||||||||||||||||||||||||||||||||||||||||
| 371 | 5886 | $self->NN($1); # set NN for analysis | |||||||||||||||||||||||||||||||||||||||||
| 372 | 5886 | $self->opcode('rst N'); | |||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | 73201 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 379 | sub _def_value { | ||||||||||||||||||||||||||||||||||||||||||
| 380 | 11035 | ▶ | my($class, $peek, $size, $def, $N, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_def_value: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 381 | $memory, $addr, $count) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | 11035 | ▶ | $count ||= 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$count ||= 1
MC/DC100%1/1
$count ||= 1
$count
Truth table100%2/2
A
A= $count
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | 11035 | my $values = []; | |||||||||||||||||||||||||||||||||||||||||
| 385 | 11035 | for my $i (0 .. $count - 1) { | |||||||||||||||||||||||||||||||||||||||||
| 386 | 57309 | my $value = $memory->$peek($addr + $size * $i); # read values | |||||||||||||||||||||||||||||||||||||||||
| 387 | 57309 | ▶ | return undef unless defined $value; # unloaded memory | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | 57304 | $values->[$i] = $value; | |||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | 11030 | return bless { memory => $memory, | |||||||||||||||||||||||||||||||||||||||||
| 393 | addr => $addr, | ||||||||||||||||||||||||||||||||||||||||||
| 394 | size => $size * $count, | ||||||||||||||||||||||||||||||||||||||||||
| 395 | opcode => "$def $N", | ||||||||||||||||||||||||||||||||||||||||||
| 396 | $N => $values, | ||||||||||||||||||||||||||||||||||||||||||
| 397 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 10511 | ▶ | sub defb { shift->_def_value('peek8u', 1, 'defb', 'N', @_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defb: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | ▶ | sub defb2 { shift->_def_value('peek8u', 1, 'defb', 'N2', @_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defb2: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 403 | 524 | ▶ | sub defw { shift->_def_value('peek16u', 2, 'defw', 'NN', @_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||
| 405 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 406 | sub _def_str { | ||||||||||||||||||||||||||||||||||||||||||
| 407 | 216 | ▶ | my($class, $peek, $eos_length, $def, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_def_str: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 408 | $memory, $addr, $length) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | 216 | my $str = $memory->$peek($addr, $length); | |||||||||||||||||||||||||||||||||||||||||
| 411 | 216 | ▶ | return undef unless defined $str; # unloaded memory | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | 210 | return $class->new({memory => $memory, | |||||||||||||||||||||||||||||||||||||||||
| 414 | addr => $addr, | ||||||||||||||||||||||||||||||||||||||||||
| 415 | size => length($str) + $eos_length, | ||||||||||||||||||||||||||||||||||||||||||
| 416 | opcode => "$def STR", | ||||||||||||||||||||||||||||||||||||||||||
| 417 | STR => $str}); | ||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 421 | 82 | ▶ | sub defm { shift->_def_str('peek_str', 0, 'defm', @_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defm: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 422 | 3 | ▶ | sub defmz { shift->_def_str('peek_strz', 1, 'defmz', @_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defmz: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 423 | 131 | ▶ | sub defm7 { shift->_def_str('peek_str7', 0, 'defm7', @_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defm7: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 426 | sub org { | ||||||||||||||||||||||||||||||||||||||||||
| 427 | 10 | ▶ | my($class, $memory, $addr) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
org: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | 10 | return bless { memory => $memory, | |||||||||||||||||||||||||||||||||||||||||
| 430 | addr => $addr, | ||||||||||||||||||||||||||||||||||||||||||
| 431 | size => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 432 | opcode => "org NN", | ||||||||||||||||||||||||||||||||||||||||||
| 433 | NN => $addr, | ||||||||||||||||||||||||||||||||||||||||||
| 434 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | =head2 as_string | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | Returns the disassembled instruction opcode and arguments. | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 448 | # Format of the disassembled output | ||||||||||||||||||||||||||||||||||||||||||
| 449 | # 1 2 3 4 5 6 7 | ||||||||||||||||||||||||||||||||||||||||||
| 450 | # 0123456789012345678901234567890123456789012345678901234567890123456789012 | ||||||||||||||||||||||||||||||||||||||||||
| 451 | # # # # # # # # # # # | ||||||||||||||||||||||||||||||||||||||||||
| 452 | # AAAA H1H2H3H4H5 INSTR ; COMMENT | ||||||||||||||||||||||||||||||||||||||||||
| 453 | # | ||||||||||||||||||||||||||||||||||||||||||
| 454 | sub as_string { | ||||||||||||||||||||||||||||||||||||||||||
| 455 | 74369 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | # decode opcode | ||||||||||||||||||||||||||||||||||||||||||
| 458 | 74369 | my $opcode = $self->opcode; | |||||||||||||||||||||||||||||||||||||||||
| 459 | 74369 | $opcode =~ s{\b ( N | N2 | NN | \+(DIS) | STR ) \b | |||||||||||||||||||||||||||||||||||||||||
| 460 | }{ | ||||||||||||||||||||||||||||||||||||||||||
| 461 | 38703 | ▶ | $self->_format_arg($2 || $1) | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$2 || $1
MC/DC0%0/2
$2 || $1
$2
$1
Truth table66%2/3
A || B
A= $2B= $1
| |||||||||||||||||||||||||||||||||||||||||||
| 462 | }gex; | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | 74369 | my $comment = $self->comment; | |||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | 74369 | ▶ | if (defined $comment) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 467 | 16898 | $comment =~ s/\n/ "\n" . " " x 32 . "; " /ge; # multi-line comment | |||||||||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | 74369 | ▶ | return !defined($comment) ? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 471 | $opcode : | ||||||||||||||||||||||||||||||||||||||||||
| 472 | length($opcode) >= 24 ? | ||||||||||||||||||||||||||||||||||||||||||
| 473 | $opcode . "\n" . " " x 32 . "; " . $comment : | ||||||||||||||||||||||||||||||||||||||||||
| 474 | sprintf("%-24s; %s", $opcode, $comment); | ||||||||||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | sub _format_arg { | ||||||||||||||||||||||||||||||||||||||||||
| 478 | 38703 | ▶ | my($self, $arg) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_format_arg: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | my $ffunc = ( $self->_format && $self->format->{$arg} ? | ||||||||||||||||||||||||||||||||||||||||||
| 481 | $self->format->{$arg} : | ||||||||||||||||||||||||||||||||||||||||||
| 482 | 38703 | ▶ | $default_format{$arg} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->_format and $self->format->{$arg}
MC/DC100%2/2
$self->_format and $self->format->{$arg}
$self->_format
$self->format->{$arg}
Truth table100%3/3
A and B
A= $self->_formatB= $self->format->{$arg}
| |||||||||||||||||||||||||||||||||||||||||||
| 483 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 484 | 38703 | my $value = $self->$arg; | |||||||||||||||||||||||||||||||||||||||||
| 485 | 38703 | ▶ | $value = [$value] unless ref($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | 38703 | return join(", ", map {$ffunc->($_)} @$value) | |||||||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||
| 490 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | =head2 dump | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | Returns the disassembly dump ready to print, containing address, bytes and | ||||||||||||||||||||||||||||||||||||||||||
| 495 | instruction, followed by newline. | ||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||
| 497 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 500 | 11 | ▶ | use constant BPL => 5; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | sub dump { | ||||||||||||||||||||||||||||||||||||||||||
| 503 | 51005 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dump: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | # address | ||||||||||||||||||||||||||||||||||||||||||
| 506 | 51005 | my $ret = sprintf("%04X ", $self->addr); | |||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | # bytes | ||||||||||||||||||||||||||||||||||||||||||
| 509 | 51005 | my $bytes = ''; | |||||||||||||||||||||||||||||||||||||||||
| 510 | 51005 | for (@{$self->bytes}) { | |||||||||||||||||||||||||||||||||||||||||
| 511 | 80296 | $bytes .= sprintf("%02X", $_); | |||||||||||||||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||
| 514 | # first line of bytes | ||||||||||||||||||||||||||||||||||||||||||
| 515 | 51005 | $ret .= sprintf("%-*s ", BPL*2, substr($bytes, 0, BPL*2)); | |||||||||||||||||||||||||||||||||||||||||
| 516 | 51005 | ▶ | $bytes = length($bytes) < BPL*2 ? '' : substr($bytes, BPL*2); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | # opcode | ||||||||||||||||||||||||||||||||||||||||||
| 519 | 51005 | $ret .= $self->as_string . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | # next lines of bytes | ||||||||||||||||||||||||||||||||||||||||||
| 522 | 51005 | ▶ | while ($bytes ne '') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | 20 | $ret .= " " x 5 . sprintf("%-*s \n", BPL*2, substr($bytes, 0, BPL*2)); | |||||||||||||||||||||||||||||||||||||||||
| 524 | 20 | ▶ | $bytes = length($bytes) < BPL*2 ? '' : substr($bytes, BPL*2); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | 51005 | $ret; | |||||||||||||||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||
| 530 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | =head2 asm | ||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | Returns the disassembly asm line ready to print, containing | ||||||||||||||||||||||||||||||||||||||||||
| 535 | instruction and comments, followed by newline. | ||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||
| 537 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 540 | sub asm { | ||||||||||||||||||||||||||||||||||||||||||
| 541 | 23290 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
asm: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | 23290 | ▶ | sprintf("%-7s %s\n%s", '', | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->is_break_flow and not $self->is_call
MC/DC100%2/2
$self->is_break_flow and not $self->is_call
$self->is_break_flow
not $self->is_call
Truth table100%3/3
A and not(B)
A= $self->is_break_flowB= not $self->is_call
| |||||||||||||||||||||||||||||||||||||||||||
| 544 | $self->as_string, | ||||||||||||||||||||||||||||||||||||||||||
| 545 | ($self->is_break_flow && ! $self->is_call) ? "\n" : ""); | ||||||||||||||||||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | =head1 AUTHOR, BUGS, FEEDBACK, LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||||||||
| 552 | See L<CPU::Z80::Disassembler|CPU::Z80::Disassembler>. | ||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||
| 554 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||