blib/lib/CPU/Z80/Disassembler.pm

filter: Statementstmt 91.7% 421 / 459 Branchbran 85.4% 229 / 268 Conditioncond 88.4% 46 / 52 MC/DCmcdc 77.4% 24 / 31 Subroutinesub 94.8% 55 / 58 Podpod 100% 21 / 21 totaltotal 89.5% 796 / 889 SCAR 53.0
CC
167
Cov
89%
load_control_file
85.0
__ANON__
30.0
_check_call
23.4
CRAP 200.7
↑ Summary blib/lib/CPU/Z80/Disassembler/Instruction.pm »
1 package CPU::Z80::Disassembler;
2  
3 #------------------------------------------------------------------------------
4  
5 =head1 NAME
6  
7 CPU::Z80::Disassembler - Disassemble the flow of a Z80 program
8  
9 =cut
10  
11 #------------------------------------------------------------------------------
12  
13 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 ▶use Carp; our @CARP_NOT; # do not report errors in this package
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 ▶use CPU::Z80::Disassembler::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use CPU::Z80::Disassembler::Instruction;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use CPU::Z80::Disassembler::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use CPU::Z80::Disassembler::Labels;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 10 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 10 our $VERSION = '1.00';
26  
27 #------------------------------------------------------------------------------
28  
29 =head1 SYNOPSIS
30  
31 use CPU::Z80::Disassembler;
32 $dis = CPU::Z80::Disassembler->new;
33 $dis->memory->load_file($file_name, $addr, $opt_skip_bytes, $opt_length);
34 $dis->write_dump; $dis->write_dump($file);
35 $dis->analyse;
36 $dis->write_asm; $dis->write_asm($file);
37  
38 $dis->get_type($addr);
39 $dis->set_type_code($addr [,$count]);
40 $dis->set_type_byte($addr [,$count]);
41 $dis->set_type_word($addr [,$count]);
42  
43 $dis->set_call($addr, 1); # this may be called
44 $dis->set_call($addr, $sub); # @next_code = $sub->($self, $next_addr) will be called
45  
46 $dis->code($addr [, $label]);
47 $dis->defb($addr [, $count][, $label]);
48 $dis->defw($addr [, $count][, $label]);
49 $dis->defm($addr, $size [, $label]);
50 $dis->defmz($addr [, $count][, $label]);
51 $dis->defm7($addr [, $count][, $label]);
52  
53 $dis->block_comment($addr, $block_comment);
54 $dis->line_comments($addr, @line_comments);
55  
56 $dis->relative_arg($addr, $label_name);
57 $dis->ix_base($addr);
58 $dis->iy_base($addr);
59
60 $dis->create_control_file($ctl_file, $bin_file, $addr, $arch);
61 $dis->load_control_file($ctl_file);
62  
63 =head1 DESCRIPTION
64  
65 Implements a Z80 disassembler. Loads a binary file into memory and dumps
66 an unprocessed disassembly listing (see C<write_dump>).
67  
68 Alternatively there are functions to tell the disassembler where there are
69 data bytes and what are code entry points and labels. The disassembler will
70 follow the code by simulating a Z80 processor, to find out where the code region
71 finishes.
72  
73 As a C<call> instruction may be followed by data, the disassembler tries to find
74 out if the called routine manipulates the return stack. If it does not, and ends
75 with a C<ret>, then the routine is considered safe, and the disassembly continues
76 after the C<call> instruction. If the routine is not considered safe, a message is
77 written at the end of the disassembled file asking the used to check the
78 routines manually; the C<set_call> method should then be used to tell how to
79 handle calls to that routine on the next iteration.
80  
81 The C<analyse> function can be called just before dumping the output to try to find
82 higher level constructs in the assembly listing. For example, it transforms the
83 sequence C<ld b,h:ld c,l> into C<ld bc,hl>.
84  
85 The C<write_asm> dumps an assembly listing that can be re-assembled to obtain the
86 starting binary file. All the unknown region bytes are disassembled as C<defb>
87 instructions, and a map is shown at the end of the file with the code regions (C<C>),
88 byte regions (C<B>), word regions (C<W>) and unknown regions (C<->).
89  
90 =head1 FUNCTIONS
91  
92 =head2 new
93  
94 Creates the object.
95  
96 =head2 memory
97  
98 L<CPU::Z80::Disassembler::Memory|CPU::Z80::Disassembler::Memory> object
99 containing the memory being analysed.
100  
101 =head2 instr
102  
103 Reference to an array that contains all the disassembled instructions
104 as L<CPU::Z80::Disassembler::Intruction|CPU::Z80::Disassembler::Intruction>
105 objects, indexed
106 by the address of the instruction. The entry is C<undef> if there is no
107 disassembled instruction at that address (either not known, or pointing to the second,
108 etc, bytes of a multi-byte instruction).
109  
110 =head2 labels
111  
112 Returns the L<CPU::Z80::Disassembler::Labels|CPU::Z80::Disassembler::Labels>
113 object that contains all the defined labels.
114  
115 =head2 header, footer
116  
117 Attributes containing blocks of text to dump before and after the assembly listing.
118 They are used by C<write_asm>.
119  
120 =head2 ix_base, iy_base
121  
122 Base addess for (IX+DIS) and (IY+DIS) instructions, if constant in all the code.
123 Causes the disassembly to dump:
124  
125 IY0 equ 0xHHHH ; 0xHHHH is iy_base
126 ...
127 ld a,(iy+0xHHHH-IY0) ; 0xHHHH is the absolute address
128  
129 =cut
130  
131 #------------------------------------------------------------------------------
132 # Hold a disassembly session
133 10 ▶use base 'Class::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
134 10 __PACKAGE__->mk_accessors(
135 'memory', # memory to disassemble
136 '_type', # identified type of each memory address, TYPE_xxx
137 'instr', # array of Instruction objects at each address
138 'labels', # all defined labels
139 '_call_instr', # hash of all call instructions where we are blocked
140 '_can_call', # hash of all subroutines we may call:
141 # 1 : can be called, no stack impact
142 # 0 : has stack impact, needs to be checked manually
143 # sub {} : call sub->($self, $next_addr) to handle
144 # stack impact and return next code addresses
145 # to continue disassembly after call
146 '_block_comments',
147 # array of block comment string at each address, printed before
148 # the address
149 'header', 'footer',
150 # header and footer sections of disassembled file
151 'ix_base', 'iy_base',
152 # base addess for (IX+DIS) and (IY+DIS)
153 );
154  
155 10 ▶use constant TYPE_UNKNOWN => '-';
Subroutine BEGIN: called CC=1 SCAR=0.0
156 10 ▶use constant TYPE_CODE => 'C';
Subroutine BEGIN: called CC=1 SCAR=0.0
157 10 ▶use constant TYPE_BYTE => 'B';
Subroutine BEGIN: called CC=1 SCAR=0.0
158 10 ▶use constant TYPE_WORD => 'W';
Subroutine BEGIN: called CC=1 SCAR=0.0
159 10 my $TYPES_RE = qr/^[-CBW]$/;
160  
161 10 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
162 10 our @EXPORT = qw( TYPE_UNKNOWN TYPE_CODE TYPE_BYTE TYPE_WORD );
163  
164  
165 sub new {
166 21 ▶ my($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
167 21 my $memory = CPU::Z80::Disassembler::Memory->new;
168 21 my $type = CPU::Z80::Disassembler::Memory->new;
169 21 my $labels = CPU::Z80::Disassembler::Labels->new;
170 21 return bless { memory => $memory,
171 _type => $type,
172 instr => [],
173 labels => $labels,
174 _call_instr => {},
175 _can_call => {},
176 _block_comments => [],
177 }, $class;
178 }
179 #------------------------------------------------------------------------------
180  
181 =head2 write_dump
182  
183 Outputs a disassembly dump on the given file, or standard output if no file
184 provided.
185  
186 The disassembly dump shows the address and bytes of each instruction with
187 the disassembled instruction.
188  
189 =cut
190  
191 #------------------------------------------------------------------------------
192  
193 sub write_dump {
194 10 ▶ my($self, $file) = @_;
Subroutine write_dump: called CC=4 SCAR=13.9
Pod documented
195  
196 10 my $fh = _opt_output_fh($file);
197
198 10 my $it = $self->memory->loaded_iter;
199 10 my $instr;
200
201 10 ▶ while (my($min, $max) = $it->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$it()) 8 10 18
202 8 ▶ for (my $addr = $min; $addr <= $max; $addr = $instr->next_addr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr <= $max) 47213 8 47221
203 # either a Z80 instruction, or, if not found, a defb
204 47213 ▶ $instr = CPU::Z80::Disassembler::Instruction->disassemble(
Condition66%2/3
'CPU::Z80::Disassembler::Instruction'->disassemble($self->memory, $addr) || 'CPU::Z80::Disassembler::Instruction'->defb($self->memory, $addr)
l!l&&r!l&&!r
47201120
MC/DC0%0/2
'CPU::Z80::Disassembler::Instruction'->disassemble($self->memory, $addr) || 'CPU::Z80::Disassembler::Instruction'->defb($self->memory, $addr)
'CPU::Z80::Disassembler::Instruction'->disassemble($self->memory, $addr) 'CPU::Z80::Disassembler::Instruction'->defb($self->memory, $addr)
Truth table66%2/3
A || B
A= 'CPU::Z80::Disassembler::Instruction'->disassemble($self->memory, $addr)B= 'CPU::Z80::Disassembler::Instruction'->defb($self->memory, $addr)
ABresult
1X1
011
000
205 $self->memory, $addr)
206 || CPU::Z80::Disassembler::Instruction->defb(
207 $self->memory, $addr);
208 47213 print $fh $instr->dump;
209 }
210 }
211 }
212  
213 #------------------------------------------------------------------------------
214  
215 =head2 analyse
216  
217 Analyse the disassembled information looking for higher level constructs.
218 For example, it replaces 'ld c,(hl):inc hl' by 'ldi c,(hl)'.
219  
220 Should be called immediately before C<write_asm>.
221  
222 =cut
223  
224 #------------------------------------------------------------------------------
225 sub analyse {
226 1 ▶ my($self) = @_;
Subroutine analyse: called CC=7 SCAR=19.5
Pod documented
227
228 # search for composed instructions
229 1 my $it = $self->memory->loaded_iter;
230 1 my $limit_addr = $self->_limit_addr(0);
231 1 ▶ while (my($min, $max) = $it->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$it()) 1 1 2
232 1 ▶ for (my $addr = $min; $addr <= $max; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr <= $max) 8954 1 8955
233 8954 my $instr = $self->instr->[$addr];
234 8954 ▶ if (defined $instr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $instr) { } 8954 0 8954
235 8954 ▶ if ($instr->is_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instr->is_code) 6598 2356 8954
236
237 # get address of next label
238 6598 ▶ if ($addr >= $limit_addr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr >= $limit_addr) 1036 5562 6598
239 1036 $limit_addr = $self->_limit_addr($addr + 1);
240 }
241
242 # disassemble long instruction
243 6598 my $long_instr = CPU::Z80::Disassembler::Instruction
244 ->disassemble($self->memory,
245 $addr, $limit_addr);
246 6598 ▶ if ($instr->opcode ne $long_instr->opcode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instr->opcode ne $long_instr->opcode) 193 6405 6598
247 193 $instr = $self->_merge_instr($long_instr);
248 }
249 }
250 8954 $addr += $instr->size; # both code and data
251 }
252 else {
253 0 $addr++; # undefined
254 }
255 }
256 }
257 }
258  
259 sub _merge_instr {
260 193 ▶ my($self, $new_instr) = @_;
Subroutine _merge_instr: called CC=8 SCAR=21.4
261
262 193 my @comments;
263 193 ▶ push @comments, $new_instr->comment if defined $new_instr->comment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_instr->comment 0 193 193
264 193 for my $addr ($new_instr->addr .. $new_instr->next_addr - 1) {
265 511 my $old_instr = $self->instr->[$addr];
266 511 ▶ if ($old_instr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_instr) 406 105 511
267 # copy comments
268 406 ▶ push @comments, $old_instr->comment if defined $old_instr->comment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $old_instr->comment 315 91 406
269
270 # copy formats
271 406 ▶ if (defined $old_instr->_format) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_instr->_format) 0 406 406
272 0 for my $key (keys %{$old_instr->_format}) {
273 $new_instr->format->{$key} =
274 0 $old_instr->format->{$key};
275 }
276 }
277
278 # delete old
279 406 $self->instr->[$addr] = undef;
280 }
281 }
282 193 ▶ $new_instr->comment(join("\n", @comments)) if @comments;
Branch: 1 branch
BranchTrueFalseTotal
if @comments 160 33 193
283 193 $self->instr->[$new_instr->addr] = $new_instr;
284
285 193 return $new_instr;
286 }
287  
288 sub _limit_addr {
289 1037 ▶ my($self, $addr) = @_;
Subroutine _limit_addr: called CC=2 SCAR=7.0
290 1037 my $label = $self->labels->next_label($addr);
291 1037 ▶ my $limit_addr = (defined $label) ? $label->addr : 0x10000;
Branch: 1 branch
BranchTrueFalseTotal
defined $label ? : 1037 0 1037
292 1037 return $limit_addr;
293 }
294  
295 #------------------------------------------------------------------------------
296  
297 =head2 write_asm
298  
299 Outputs a disassembly listing on the given file, or standard output if no file
300 provided.
301  
302 The disassembly listing can be assembled to obtain the original binary file.
303  
304 =cut
305  
306 #------------------------------------------------------------------------------
307 sub write_asm {
308 11 ▶ my($self, $file) = @_;
Subroutine write_asm: called CC=4 SCAR=13.9
Pod documented
309  
310 11 my $fh = _opt_output_fh($file);
311  
312 11 $self->_write_header($fh);
313
314 11 my $comment_it = $self->_block_comments_iter;
315 11 my $it = $self->memory->loaded_iter;
316 11 ▶ while (my($min, $max) = $it->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$it()) 9 11 20
317 9 my $instr = CPU::Z80::Disassembler::Instruction
318 ->org($self->memory, $min);
319 9 print $fh $instr->asm;
320
321 9 ▶ for (my $addr = $min; $addr <= $max; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr <= $max) 20274 9 20283
322 # block comments
323 20274 print $fh $comment_it->($addr);
324
325 20274 $addr = $self->_write_instr($fh, $addr, $max);
326 }
327
328 9 print $fh "\n";
329 }
330
331 # final comments
332 11 print $fh $comment_it->();
333
334 11 ▶ print $fh $self->footer if defined $self->footer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->footer 1 10 11
335  
336 11 $self->_write_map($fh);
337 11 $self->_write_labels($fh);
338 11 $self->_write_check_calls($fh);
339 }
340  
341 #------------------------------------------------------------------------------
342 # iterator to return block comments up to given address
343 sub _block_comments_iter {
344 11 ▶ my($self) = @_;
Subroutine _block_comments_iter: called CC=1 SCAR=0.0
345 11 my $i = 0;
346 return sub {
347 20285 ▶ my($addr) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
348 20285 my $max = $#{$self->_block_comments};
349 20285 ▶ $addr = $max unless defined $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr 11 20274 20285
350
351 20285 my $return = "";
352 20285 ▶ while ($i <= $addr && $i <= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $addr and $i <= $max) 32747 20285 53032
Condition100%3/3
$i <= $addr and $i <= $max
!ll&&!rl&&r
1937690932747
MC/DC100%2/2
$i <= $addr and $i <= $max
$i <= $addr $i <= $max
Truth table100%3/3
A and B
A= $i <= $addrB= $i <= $max
ABresult
0X0
100
111
353 32747 my $comment = $self->_block_comments->[$i++];
354 32747 ▶ $return .= $comment if defined $comment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $comment 4454 28293 32747
355 }
356 20285 $return;
357 11 };
358 }
359  
360 #------------------------------------------------------------------------------
361 10 ▶use constant BPL => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
362  
363 #------------------------------------------------------------------------------
364 # write the file header and the label equates
365 sub _write_header {
366 11 ▶ my($self, $fh) = @_;
Subroutine _write_header: called CC=9 SCAR=22.0
367
368 11 my $label_width = $self->labels->max_length + 1;
369
370 11 ▶ print $fh $self->header if defined $self->header;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->header 3 8 11
371
372 11 my @labels = sort { $a->addr <=> $b->addr } $self->labels->search_all;
373 11 for my $label (@labels) {
374 2745 ▶ next if defined $self->instr->[$label->addr]; # no need for EQU
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->instr->[$label->addr] 2585 160 2745
375 160 print $fh $label->equ_string($label_width);
376 }
377 11 ▶ print $fh "\n" if @labels;
Branch: 1 branch
BranchTrueFalseTotal
if @labels 7 4 11
378
379 # create IX0 / IY0 base
380 11 my $printed_base;
381 11 for (['IX0', 'ix_base'], ['IY0', 'iy_base']) {
382 22 my($base, $func) = @$_;
383 22 my $addr = $self->$func;
384 22 ▶ if (defined $addr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr) 3 19 22
385 3 my $label = $self->labels->search_addr($addr);
386 3 ▶ if (defined $label) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $label) { } 3 0 3
387 3 $addr = $label->name;
388 }
389 else {
390 0 $addr = format_hex4($addr);
391 }
392
393 3 print $fh sprintf("%-*s equ %s\n", $label_width-1, $base, $addr);
394 3 $printed_base++;
395 }
396 }
397  
398 11 ▶ print $fh "\n" if $printed_base;
Branch: 1 branch
BranchTrueFalseTotal
if $printed_base 3 8 11
399 }
400
401 #------------------------------------------------------------------------------
402 # write one instruction
403 sub _write_instr {
404 20274 ▶ my($self, $fh, $addr, $max) = @_;
Subroutine _write_instr: called CC=19 SCAR=29.4
405
406 # label
407 20274 my $label = $self->labels->search_addr($addr);
408 20274 ▶ print $fh "\n", $label->label_string if (defined $label);
Branch: 1 branch
BranchTrueFalseTotal
if defined $label 2585 17689 20274
409  
410 20274 my $instr = $self->instr->[$addr];
411 20274 ▶ if (defined $instr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $instr) { } 20234 40 20274
412 # instruction
413 20234 ▶ if (defined($instr->NN) && !defined($instr->format->{NN})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $instr->NN and not defined $instr->format->{'NN'}) { } 1540 18694 20234
elsif (defined $instr->DIS and not defined $instr->format->{'DIS'}) { } 465 36458 36923
Condition100%3/3
defined $instr->NN and not defined $instr->format->{"NN"}
!ll&&!rl&&r
1474839461540
MC/DC100%2/2
defined $instr->NN and not defined $instr->format->{"NN"}
defined $instr->NN not defined $instr->format->{"NN"}
Truth table100%3/3
A and not(B)
A= defined $instr->NNB= not defined $instr->format->{"NN"}
ABresult
0X0
100
111
414 # nac the special case of 16-bit (defw) values which can
415 # nac potentially be converted to a label
416 1540 ▶ if (ref($instr->NN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $instr->NN) { } 316 1224 1540
417 316 my $max = scalar(@{$instr->NN});
418 316 ▶ for (my $i=0; $i<$max; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max) 320 316 636
419 320 my $NN = $instr->NN->[$i];
420 320 my $ref_label = $self->labels->search_addr($NN);
421 320 ▶ if (defined($ref_label)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref_label) 317 3 320
422 317 $instr->NN->[$i] = $ref_label->name;
423 $instr->format->{NN} =
424 320 ▶ sub { my $foo=shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
425 320 ▶ if (/^\d+$/) {return format_hex4($foo)}
Branch: 1 branch
BranchTrueFalseTotal
if (/^\d+$/) { } 3 317 320
426 317 else {return $foo}
427 317 };
428 }
429 }
430 }
431 else {
432 1224 my $NN = $instr->NN;
433 1224 my $ref_label = $self->labels->search_addr($NN);
434 1224 ▶ if (defined($ref_label)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref_label) 867 357 1224
435 867 ▶ $instr->format->{NN} = sub { $ref_label->name };
Subroutine __ANON__: called CC=1 SCAR=0.0
436 }
437 }
438 }
439 ▶ elsif (defined($instr->DIS) && !defined($instr->format->{DIS})) {
Condition66%2/3
defined $instr->DIS and not defined $instr->format->{"DIS"}
!ll&&!rl&&r
182290465
MC/DC50%1/2
defined $instr->DIS and not defined $instr->format->{"DIS"}
defined $instr->DIS not defined $instr->format->{"DIS"}
Truth table66%2/3
A and not(B)
A= defined $instr->DISB= not defined $instr->format->{"DIS"}
ABresult
0X0
100
111
440 465 for (['ix', 'ix_base'], ['iy', 'iy_base']) {
441 930 my($reg, $func) = @$_;
442 930 ▶ if ($instr->opcode =~ /$reg/ && defined(my $base = $self->$func)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instr->opcode =~ /$reg/ and defined(my $base = $self->$func)) 378 552 930
Condition100%3/3
$instr->opcode =~ /$reg/ and defined(my $base = $self->$func)
!ll&&!rl&&r
46587378
MC/DC100%2/2
$instr->opcode =~ /$reg/ and defined(my $base = $self->$func)
$instr->opcode =~ /$reg/ defined(my $base = $self->$func)
Truth table100%3/3
A and B
A= $instr->opcode =~ /$reg/B= defined(my $base = $self->$func)
ABresult
0X0
100
111
443 378 my $addr = $base + $instr->DIS;
444 378 my $ref_label = $self->labels->search_addr($addr);
445 378 ▶ if (defined $ref_label) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref_label) 348 30 378
446 $instr->format->{DIS} =
447 348 ▶ sub { '+'.$ref_label->name.'-'.uc($reg).'0' };
Subroutine __ANON__: called CC=1 SCAR=0.0
448 }
449 }
450 }
451 }
452 20234 print $fh $instr->asm;
453
454 20234 return $instr->next_addr;
455 }
456 else {
457 # block of defb
458  
459 # search for next defined instr
460 40 my $p;
461 40 ▶ for ($p = $addr; $p <= $max && ! defined($self->instr->[$p]) ; $p++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p <= $max and not defined $self->instr->[$p]) 47544 40 47584
Condition100%3/3
$p <= $max and not defined $self->instr->[$p]
!ll&&!rl&&r
43647544
MC/DC100%2/2
$p <= $max and not defined $self->instr->[$p]
$p <= $max not defined $self->instr->[$p]
Truth table100%3/3
A and not(B)
A= $p <= $maxB= not defined $self->instr->[$p]
ABresult
0X0
100
111
462 ;
463 }
464  
465 40 my $comment = "unknown area ".format_hex4($addr)." to ".format_hex4($p-1);
466 40 print $fh "\n", " " x 8, "; Start of $comment\n";
467
468 # print for $addr in blocks of 16
469 40 ▶ while ($addr < $p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr < $p) 3008 40 3048
470 3008 my $max_count = $p - $addr;
471 3008 my $count = BPL - ($addr % BPL); # until end of addr block
472 3008 ▶ $count = $max_count if $count > $max_count; # until $p
Branch: 1 branch
BranchTrueFalseTotal
if $count > $max_count 32 2976 3008
473
474 3008 my $instr = CPU::Z80::Disassembler::Instruction
475 ->defb($self->memory, $addr, $count);
476 3008 print $fh $instr->asm;
477 3008 $addr += $count;
478 }
479  
480 40 print $fh " " x 8, "; End of $comment\n\n";
481
482 40 return $addr;
483 }
484 }
485  
486 #------------------------------------------------------------------------------
487 sub _write_map {
488 11 ▶ my($self, $fh) = @_;
Subroutine _write_map: called CC=5 SCAR=16.1
489
490 11 my $it = $self->memory->loaded_iter;
491 11 ▶ while (my($min, $max) = $it->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$it()) 9 11 20
492 9 for my $addr ($min .. $max-1) {
493 81935 ▶ if ($addr == $min || ($addr % 0x50) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr == $min or $addr % 80 == 0) 1029 80906 81935
Condition100%3/3
$addr == $min or $addr % 80 == 0
l!l&&r!l&&!r
9102080906
MC/DC100%2/2
$addr == $min or $addr % 80 == 0
$addr == $min $addr % 80 == 0
Truth table100%3/3
A or B
A= $addr == $minB= $addr % 80 == 0
ABresult
1X1
011
000
494 1029 print $fh "\n; ", format_hex4($addr), " ";
495 }
496 81935 print $fh $self->get_type($addr);
497 }
498 9 print $fh "\n";
499 }
500 }
501  
502 #------------------------------------------------------------------------------
503 sub _write_labels {
504 11 ▶ my($self, $fh) = @_;
Subroutine _write_labels: called CC=3 SCAR=11.0
505
506 11 my @labels = $self->labels->search_all;
507 11 ▶ return unless @labels;
Branch: 1 branch
BranchTrueFalseTotal
unless @labels 4 7 11
508
509 7 my $len = $self->labels->max_length;
510
511 7 my @by_name = sort { lc($a->name) cmp lc($b->name) } @labels;
512 7 my @by_addr = sort { $a->addr <=> $b->addr } @labels;
513  
514 7 print $fh "\n; Labels\n;\n";
515 7 for (0 .. $#labels) {
516 2745 print $fh "; ", format_hex4($by_addr[$_]->addr), " => ",
517 sprintf("%-${len}s", $by_addr[$_]->name),
518 " " x 8,
519 sprintf("%-${len}s", $by_name[$_]->name), " => ",
520 format_hex4($by_name[$_]->addr), "\n";
521 }
522 }
523  
524 #------------------------------------------------------------------------------
525 sub _write_check_calls {
526 11 ▶ my($self, $fh) = @_;
Subroutine _write_check_calls: called CC=5 SCAR=16.1
527  
528 11 my %unknown_calls;
529 11 for my $addr (keys %{$self->_can_call}) {
530 488 ▶ $unknown_calls{$addr}++ unless $self->_can_call->{$addr};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_can_call->{$addr} 22 466 488
531 }
532 11 for my $addr (keys %{$self->_call_instr}) {
533 10 my $instr = $self->_get_instr($addr);
534 10 $unknown_calls{$instr->NN}++;
535 }
536
537 11 ▶ if (%unknown_calls) {
Branch: 1 branch
BranchTrueFalseTotal
if (%unknown_calls) 4 7 11
538 print $fh "\n\n; Check these calls manualy: ",
539 4 join(", ", sort map {format_hex4($_)} keys %unknown_calls),
540 "\n\n";
541 }
542 }
543  
544 #------------------------------------------------------------------------------
545 sub _opt_output_fh {
546 21 ▶ my($file) = @_;
Subroutine _opt_output_fh: called CC=3 SCAR=11.0
547
548 # open file
549 21 my $fh;
550 21 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) { } 18 3 21
551 18 ▶ open($fh, ">", $file) or croak("write $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $file or croak("write ${file}: $!") 0 18 18
552 }
553 else {
554 3 $fh = \*STDOUT;
555 }
556  
557 21 $fh;
558 }
559  
560 #------------------------------------------------------------------------------
561  
562 =head2 set_type_code, set_type_byte, set_type_word
563  
564 Sets the type of the given address. An optional count allows the definitions of
565 the type of consecutive memory locations.
566  
567 It is an error to set a type of a not-defined memory location,
568 or to redefine a type.
569  
570 =cut
571  
572 #------------------------------------------------------------------------------
573 sub _set_type {
574 24758 ▶ my($self, $type, $addr, $count) = @_;
Subroutine _set_type: called CC=7 SCAR=19.5
575 24758 ▶ $count ||= 1;
Condition100%2/2
$count ||= 1
l!l
247553
MC/DC100%1/1
$count ||= 1
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
576
577 24758 ▶ croak("Invalid type $type") unless $type =~ /$TYPES_RE/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /$TYPES_RE/ 0 24758 24758
578
579 24758 ▶ for ( ; $count > 0 ; $count--, $addr++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 41060 24756 65816
580 41060 my $current_type = $self->get_type($addr);
581
582 41059 ▶ croak("Changing type of address ".format_hex4($addr)." from ".
Branch: 1 branch
BranchTrueFalseTotal
if $current_type ne "-" and $type ne "-" and $current_type ne $type 1 41058 41059
Condition100%6/6
$current_type ne "-" and $type ne "-" and $current_type ne $type
!ll&&!rl&&r
3792931291
$current_type ne "-" and $type ne "-"
!ll&&!rl&&r
3659213373130
MC/DC100%3/3
$current_type ne "-" and $type ne "-" and $current_type ne $type
$current_type ne "-" $type ne "-" $current_type ne $type
Truth table100%4/4
A and B and C
A= $current_type ne "-"B= $type ne "-"C= $current_type ne $type
ABCresult
0XX0
10X0
1100
1111
583 "$current_type to $type")
584 if ($current_type ne TYPE_UNKNOWN &&
585 $type ne TYPE_UNKNOWN &&
586 $current_type ne $type);
587
588 41058 $self->_type->poke($addr, ord($type));
589 }
590 }
591 16083 ▶sub set_type_code { shift->_set_type( TYPE_CODE, @_ ) }
Subroutine set_type_code: called CC=1 SCAR=0.0
Pod documented
592 7692 ▶sub set_type_byte { shift->_set_type( TYPE_BYTE, @_ ) }
Subroutine set_type_byte: called CC=1 SCAR=0.0
Pod documented
593 518 ▶sub set_type_word { shift->_set_type( TYPE_WORD, @_ ) }
Subroutine set_type_word: called CC=1 SCAR=0.0
Pod documented
594
595 #------------------------------------------------------------------------------
596  
597 =head2 get_type
598  
599 Gets the type at the given address, one of TYPE_UNKNOWN, TYPE_CODE, TYPE_BYTE or
600 TYPE_WORD constants.
601  
602 It is an error to set a type of a not-defined memory location.
603  
604 =cut
605  
606 #------------------------------------------------------------------------------
607 sub get_type {
608 185353 ▶ my($self, $addr) = @_;
Subroutine get_type: called CC=4 SCAR=13.9
Pod documented
609
610 185353 ▶ croak("Getting type of unloaded memory at ".format_hex4($addr))
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->memory->peek($addr) 1 185352 185353
611 unless defined $self->memory->peek($addr);
612
613 185352 my $current_type = $self->_type->peek($addr);
614 185352 ▶ $current_type = defined($current_type) ? chr($current_type) : TYPE_UNKNOWN;
Branch: 1 branch
BranchTrueFalseTotal
defined $current_type ? : 60147 125205 185352
615
616 185352 ▶ croak("Invalid type $current_type") unless $current_type =~ /$TYPES_RE/;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_type =~ /$TYPES_RE/ 0 185352 185352
617
618 185352 return $current_type;
619 }
620  
621 #------------------------------------------------------------------------------
622  
623 =head2 set_call
624  
625 Declares a subroutine at the given address, either with no stack impact
626 (if 1 is passed as argument) or with a stack impact to be computed by the
627 given code reference. This function is called with $self and the address
628 after the call instruction as arguments and should return the next address(es)
629 where the code stream shall continue.
630  
631 =cut
632  
633 #------------------------------------------------------------------------------
634 sub set_call {
635 33 ▶ my($self, $addr, $can_call) = @_;
Subroutine set_call: called CC=1 SCAR=0.0
Pod documented
636 33 $self->_can_call->{$addr} = $can_call;
637 }
638  
639 #------------------------------------------------------------------------------
640  
641 =head2 code
642  
643 Declares the given address and all following instructions up to an unconditional
644 jump as a block of code, with an optional label.
645  
646 =cut
647  
648 #------------------------------------------------------------------------------
649 sub _get_instr {
650 302664 ▶ my($self, $addr) = @_;
Subroutine _get_instr: called CC=2 SCAR=7.1
651  
652 # read from cache or disassemble
653 302664 ▶ $self->instr->[$addr] ||=
Condition66%2/3
$self->instr->[$addr] ||= "CPU::Z80::Disassembler::Instruction"->disassemble($self->memory, $addr)
l!l&&r!l&&!r
287049156150
MC/DC0%0/2
$self->instr->[$addr] ||= "CPU::Z80::Disassembler::Instruction"->disassemble($self->memory, $addr)
$self->instr->[$addr] "CPU::Z80::Disassembler::Instruction"->disassemble($self->memory, $addr)
Truth table66%2/3
A ||= B
A= $self->instr->[$addr]B= "CPU::Z80::Disassembler::Instruction"->disassemble($self->memory, $addr)
ABresult
1X1
011
000
654 CPU::Z80::Disassembler::Instruction->disassemble($self->memory, $addr);
655 }
656
657 sub code {
658 16099 ▶ my($self, $addr, $label) = @_;
Subroutine code: called CC=12 SCAR=24.9
Pod documented
659  
660 16099 ▶ defined($label) and $self->labels->add($addr, $label);
Branch: 1 branch
BranchTrueFalseTotal
defined $label and $self->labels->add($addr, $label) 2267 13832 16099
661
662 16099 my @stack = ($addr); # all addresses to investigate
663
664 # check calls
665 16099 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 16368 16099 32467
666 # follow all streams of code
667 16368 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 35481 16368 51849
668 35481 my $addr = pop @stack;
669
670 # if address is not loaded, assume a ROM entry point
671 35481 ▶ if (!defined $self->memory->peek($addr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->memory->peek($addr)) 2 35479 35481
672 2 ▶ if (!$self->labels->search_addr($addr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->labels->search_addr($addr)) 0 2 2
673 0 my $instr = $self->labels->add($addr);
674 }
675 2 next;
676 }
677
678 # skip if already checked
679 35479 ▶ next if $self->get_type($addr) eq TYPE_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_type($addr) eq "C" 19399 16080 35479
680
681 # get instruction and mark as code
682 16080 my $instr = $self->_get_instr($addr);
683 16080 $self->set_type_code($addr, $instr->size);
684
685 # create labels for branches (jump or call)
686 16080 ▶ if ($instr->is_branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instr->is_branch) 4445 11635 16080
687 4445 my $branch_addr = $instr->NN;
688 4445 my $label = $self->labels->add($branch_addr, undef, $addr);
689 4445 ▶ $instr->format->{NN} = sub { $label->name };
Subroutine __ANON__: called CC=1 SCAR=0.0
690 }
691
692 # check call / rst addresses
693 16080 ▶ if ($instr->is_call) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instr->is_call) 2327 13753 16080
694 2327 my $call_addr = $instr->NN;
695 2327 my $can_call = $self->_can_call->{$call_addr};
696 2327 ▶ if (! defined $can_call) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $can_call) { } 548 1779 2327
elsif (ref $can_call) { } 116 1663 1779
elsif ($can_call) { } 1514 149 1663
697 548 $self->_call_instr->{$addr}++; # mark road block
698 }
699 elsif (ref $can_call) {
700 116 push @stack, $can_call->($self, $instr->next_addr);
701 # call sub to handle impact
702 }
703 elsif ($can_call) {
704 1514 push @stack, $instr->next_addr; # can continue
705 }
706 }
707
708 # continue on next addresses
709 16080 push @stack, $instr->next_code;
710 }
711
712 # check if we can unwind any blocked calls, after all paths without calls are
713 # exhausted
714 16368 push @stack, $self->_check_call_instr;
715 }
716 }
717  
718 #------------------------------------------------------------------------------
719 sub _check_call_instr {
720 16368 ▶ my($self) = @_;
Subroutine _check_call_instr: called CC=6 SCAR=17.9
721  
722 16368 my @stack;
723
724 # check simple call instructions where we blocked
725 16368 for my $addr (keys %{$self->_call_instr}) {
726 26859 my $instr = $self->_get_instr($addr);
727 26859 my $call_addr = $instr->NN;
728
729 26859 ▶ if ( # if any of the calls is conditional, then _can_call
Branch: 1 branch
BranchTrueFalseTotal
if ($instr->opcode =~ /call \w+,NN/ or $self->get_type($instr->next_addr) eq "C") 68 26791 26859
Condition66%2/3
$instr->opcode =~ /call \w+,NN/ or $self->get_type($instr->next_addr) eq "C"
l!l&&r!l&&!r
06826791
MC/DC50%1/2
$instr->opcode =~ /call \w+,NN/ or $self->get_type($instr->next_addr) eq "C"
$instr->opcode =~ /call \w+,NN/ $self->get_type($instr->next_addr) eq "C"
Truth table66%2/3
A or B
A= $instr->opcode =~ /call \w+,NN/B= $self->get_type($instr->next_addr) eq "C"
ABresult
1X1
011
000
730 $instr->opcode =~ /call \w+,NN/
731 ||
732 # if address after the call is CODE, then _can_call
733 $self->get_type($instr->next_addr) eq TYPE_CODE
734 ) {
735
736 # mark for later; do not call code() directly because we are
737 # iterating over _call_instr that might be changed by code()
738 68 $self->_can_call->{$call_addr} = 1;
739 68 push @stack, $instr->next_addr; # code from here
740 68 delete $self->_call_instr->{$addr}; # processed
741 }
742 }
743
744 # check remaining by following code flow
745 16368 for my $addr (keys %{$self->_call_instr}) {
746 26791 my $instr = $self->_get_instr($addr);
747 26791 my $call_addr = $instr->NN;
748
749 # if call flow in called subroutine
750 # does not pop return address, than _can_call
751 26791 my $can_call = $self->_check_call($call_addr);
752 26791 ▶ if (defined $can_call) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $can_call) 465 26326 26791
753 465 $self->_can_call->{$call_addr} = $can_call;
754 465 push @stack, $addr; # re-check call to call can_call
755 465 $self->_set_type(TYPE_UNKNOWN, $addr, $instr->size);
756 # allow recheck to happen
757 465 delete $self->_call_instr->{$addr}; # processed
758 }
759 }
760
761 16368 return @stack;
762 }
763  
764 #------------------------------------------------------------------------------
765 sub _check_call {
766 26791 ▶ my($self, $call_addr) = @_;
Subroutine _check_call: called CC=23 SCAR=31.5
767
768 26791 my %seen; # addresses we have checked
769 26791 my($addr, $sp_level) = ($call_addr, 0);
770 26791 my @stack = ([$addr, $sp_level]); # all addresses to investigate
771
772 # follow code
773 26791 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 262631 26325 288956
774 262631 ($addr, $sp_level) = @{pop @stack};
775 262631 ▶ next if $seen{$addr}++; # prevent loops
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$addr}++ 29612 233019 262631
776
777 # run into some known code
778 233019 my $can_call = $self->_can_call->{$addr};
779 233019 ▶ if (defined $can_call) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $can_call) 120 232899 233019
780 120 ▶ return $can_call if $sp_level == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sp_level == 0 119 1 120
781 }
782  
783 # if address is not loaded, return "dont know"
784 232900 ▶ if (!defined $self->memory->peek($addr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->memory->peek($addr)) 1 232899 232900
785 1 return undef;
786 }
787  
788 # get the instruction
789 232899 my $instr = $self->_get_instr($addr);
790 232899 local $_ = $instr->opcode;
791
792 # check stack impact
793 232899 ▶ if (/ret/) {
Branch: 7 branches
BranchTrueFalseTotal
if (/ret/) { } 326 232573 232899
elsif (/push/) { } 30475 202098 232573
elsif (/pop/) { } 4494 197604 202098
elsif (/dec sp/) { } 0 197604 197604
elsif (/inc sp/) { } 0 197604 197604
elsif (/ex \(sp\),/) { } 36 197568 197604
elsif (/ld sp/) { } 2 197566 197568
794 326 ▶ return 1 if $sp_level == 0; # can call if stack empty
Branch: 1 branch
BranchTrueFalseTotal
if $sp_level == 0 323 3 326
795 }
796 elsif (/push/) {
797 30475 $sp_level += 2;
798 }
799 elsif (/pop/) {
800 4494 $sp_level -= 2;
801 4494 ▶ return 0 if $sp_level < 0; # STACK IMPACT!
Branch: 1 branch
BranchTrueFalseTotal
if $sp_level < 0 15 4479 4494
802 }
803 elsif (/dec sp/) {
804 0 $sp_level++;
805 }
806 elsif (/inc sp/) {
807 0 $sp_level--;
808 0 ▶ return 0 if $sp_level < 0; # STACK IMPACT!
Branch: 1 branch
BranchTrueFalseTotal
if $sp_level < 0 0 0 0
809 }
810 elsif (/ex \(sp\),/) {
811 36 ▶ return 0 if $sp_level < 2; # STACK IMPACT!
Branch: 1 branch
BranchTrueFalseTotal
if $sp_level < 2 6 30 36
812 }
813 elsif (/ld sp/) {
814 2 return 0; # STACK IMPACT!
815 }
816
817 # continue on next address, but dont follow calls
818 232553 ▶ if ($instr->is_call) {
Branch: 2 branches
BranchTrueFalseTotal
if ($instr->is_call) { } 30402 202151 232553
elsif ($instr->is_branch) { } 34419 167732 202151
819 30402 my $can_call = $self->_can_call->{$instr->NN};
820 30402 ▶ if (defined($can_call) && !ref($can_call) && $can_call) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $can_call and not ref $can_call and $can_call) 4685 25717 30402
Condition100%6/6
defined $can_call and not ref $can_call and $can_call
!ll&&!rl&&r
6601191164685
defined $can_call and not ref $can_call
!ll&&!rl&&r
6594723801
MC/DC100%3/3
defined $can_call and not ref $can_call and $can_call
defined $can_call not ref $can_call $can_call
Truth table100%4/4
A and not(B) and C
A= defined $can_callB= not ref $can_callC= $can_call
ABCresult
0XX0
10X0
1100
1111
821 4685 push @stack, [$instr->next_addr, $sp_level]; # continue after call
822 }
823 }
824 elsif ($instr->is_branch) {
825 34419 push @stack, [$instr->NN, $sp_level];
826 }
827
828 232553 ▶ push @stack, [$instr->next_addr, $sp_level] unless $instr->is_break_flow;
Branch: 1 branch
BranchTrueFalseTotal
unless $instr->is_break_flow 197116 35437 232553
829 }
830
831 26325 return undef; # don't know
832 }
833  
834 #------------------------------------------------------------------------------
835  
836 =head2 defb, defb2, defw, defm, defmz, defm7
837  
838 Declares the given address as a def* instruction
839 with an optional label.
840  
841 =cut
842  
843 #------------------------------------------------------------------------------
844 sub _def {
845 8209 ▶ my($self, $factory, $set_type,
Subroutine _def: called CC=2 SCAR=6.9
846 $addr, $count, $label) = @_;
847  
848 8209 ▶ defined($label) and $self->labels->add($addr, $label);
Branch: 1 branch
BranchTrueFalseTotal
defined $label and $self->labels->add($addr, $label) 7 8202 8209
849
850 8209 my $instr = CPU::Z80::Disassembler::Instruction
851 ->$factory($self->memory, $addr, $count);
852 8209 $self->instr->[$addr] = $instr;
853 8209 $self->$set_type($addr, $instr->size);
854
855 8209 return $instr;
856 }
857  
858 sub defb {
859 7485 ▶ my($self, $addr, $count, $label) = @_;
Subroutine defb: called CC=1 SCAR=0.0
Pod documented
860 7485 $self->_def('defb', 'set_type_byte', $addr, $count, $label);
861 }
862  
863 sub defb2 {
864 0 ▶ my($self, $addr, $count, $label) = @_;
Subroutine defb2: not called CC=1 SCAR=6.9
Pod documented
865 0 $self->_def('defb2', 'set_type_byte', $addr, $count, $label);
866 }
867  
868 sub defw {
869 518 ▶ my($self, $addr, $count, $label) = @_;
Subroutine defw: called CC=1 SCAR=0.0
Pod documented
870 518 $self->_def('defw', 'set_type_word', $addr, $count, $label);
871 }
872  
873 sub defm {
874 78 ▶ my($self, $addr, $length, $label) = @_;
Subroutine defm: called CC=1 SCAR=0.0
Pod documented
875 78 $self->_def('defm', 'set_type_byte', $addr, $length, $label);
876 }
877  
878 sub defmz {
879 0 ▶ my($self, $addr, $count, $label) = @_;
Subroutine defmz: not called CC=1 SCAR=6.9
Pod documented
880 0 $self->_def('defmz', 'set_type_byte', $addr, $count, $label);
881 }
882  
883 sub defm7 {
884 128 ▶ my($self, $addr, $count, $label) = @_;
Subroutine defm7: called CC=1 SCAR=0.0
Pod documented
885 128 $self->_def('defm7', 'set_type_byte', $addr, $count, $label);
886 }
887  
888 #------------------------------------------------------------------------------
889  
890 =head2 block_comment
891  
892 Creates a block comment to insert before the given address.
893  
894 =cut
895  
896 #------------------------------------------------------------------------------
897 sub block_comment {
898 15795 ▶ my($self, $addr, $block_comment) = @_;
Subroutine block_comment: called CC=3 SCAR=11.0
Pod documented
899
900 15795 ▶ if (defined $block_comment) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $block_comment) 9330 6465 15795
901 9330 chomp($block_comment);
902 9330 ▶ $self->_block_comments->[$addr] ||= "";
Condition100%2/2
$self->_block_comments->[$addr] ||= ""
l!l
48764454
MC/DC100%1/1
$self->_block_comments->[$addr] ||= ""
$self->_block_comments->[$addr]
Truth table100%2/2
A
A= $self->_block_comments->[$addr]
Aresult
11
00
903 9330 $self->_block_comments->[$addr] .= "$block_comment\n";
904 }
905 }
906  
907 #------------------------------------------------------------------------------
908  
909 =head2 line_comments
910  
911 Appends each of the given line comments to the instrutions starting at
912 the given address, one comment per instruction.
913  
914 =cut
915  
916 #------------------------------------------------------------------------------
917 sub line_comments {
918 13493 ▶ my($self, $addr, @line_comments) = @_;
Subroutine line_comments: called CC=5 SCAR=16.1
Pod documented
919
920 13493 for (@line_comments) {
921 13493 my $instr = $self->instr->[$addr];
922 13493 ▶ croak("Cannot set comment of unknown instruction at ".format_hex4($addr))
Branch: 1 branch
BranchTrueFalseTotal
unless $instr 1 13492 13493
923 unless $instr;
924 13492 ▶ my $old_comment = $instr->comment // "";
Condition100%2/2
$instr->comment // ""
l!l
39569536
MC/DC100%1/1
$instr->comment // ""
$instr->comment
Truth table100%2/2
A
A= $instr->comment
Aresult
11
00
925 13492 ▶ $old_comment .= "\n" if $old_comment;
Branch: 1 branch
BranchTrueFalseTotal
if $old_comment 3520 9972 13492
926 13492 $instr->comment($old_comment . $_);
927 13492 $addr += $instr->size;
928 }
929 }
930  
931 #------------------------------------------------------------------------------
932  
933 =head2 relative_arg
934  
935 Shows the instruction argument (NN or N) relative to a given label name.
936 Label name can be '$' for a value relative to the instruction pointer.
937  
938 =cut
939  
940 #------------------------------------------------------------------------------
941  
942 =head2 create_control_file
943  
944 $dis->create_control_file($ctl_file, $bin_file, $addr, $arch);
945  
946 Creates a new control file for the given input binary file, starting at the given address
947 and for the given architecture.
948  
949 The address defaults to zero, and the architecture to undefined. The architecture may be
950 implemented in the future, for example to define system variable equates for the given
951 architecture.
952  
953 It is an error to overwrite a control file.
954  
955 The Control File is the input file for a disassembly run in an interactive disassembly
956 session, and the outout is the <bin_file>.asm. After each run, the user studies the output
957 .asm file, and includes new commands in the control file to add information to the
958 .asm file on the next run.
959  
960 This function creates a template control file that contains just the hex dump of the
961 binary file and the decoded assembly instruction at each address, e.g.
962  
963 0000 :F <bin_file>
964 0000 D3FD out ($FD),a
965 0002 01FF7F ld bc,$7FFF
966 0005 C3CB03 jp $03CB
967  
968 The control file commands start with a ':' and refer to the hexadecimal address at the
969 start of the line.
970  
971 Some commands operate on a range of addresses and accept the inclusive range limits separated
972 by a single '-'.
973  
974 A line starting with a blank uses the same address as the previous command.
975  
976 A semicolon starts a comment in the control file.
977  
978 0000 :; define next address as 0x0000
979 :<cmd> ; <cmd> at the same address 0x0000
980 0000-001F :B ; define a range address of bytes
981  
982 The dump between the address and the ':' is ignored and is helpfull as a guide while adding
983 information to the control file.
984  
985 =head2 load_control_file
986  
987 $dis->load_control_file($ctl_file);
988  
989 Load the control file created by <create_control_file> and subsequently edited by the user
990 and create a new .asm disassembly file.
991  
992 =head1 Control File commands
993  
994 =head2 Include
995  
996 Include another control file at the current location.
997  
998 #include vars.ctl
999  
1000 =head2 File
1001  
1002 Load a binary file at the given address.
1003  
1004 0000 :F zx81.rom
1005  
1006 =head2 Code
1007  
1008 Define the start of a code routine, with an optional label. The code is not known to be
1009 stack-safe, i.e. not to have data bytes following the call instruction. The disassembler
1010 stops disassembly when it cannot determine if the bytes after a call instruction are
1011 data or code.
1012  
1013 0000 :C START
1014  
1015 =head2 Procedure
1016  
1017 Define the start of a procedure with a possible list of arguments following the call
1018 instruction.
1019  
1020 The signature is a list of {'B','W','C'}+, identifing each of the following items
1021 after the call instruction (Byte, Word or Code). In the following example the call
1022 istruction is followed by one byte and one word, and the procedure returns
1023 to the address after the word.
1024  
1025 0000 P proc B,W,C
1026  
1027 The signature defaults to a single 'C', meaning the procedure returns to the point after call.
1028  
1029 A signature without a 'C' means that the call never returns.
1030  
1031 =head2 Bytes and Words
1032  
1033 Define data bytes and words in the given address range.
1034  
1035 0000-0003 :B label
1036 0000-0003 :B label
1037 0000-0003 :B2[1] label ; one byte per line, binary data
1038 0000-0003 :W label
1039  
1040 =head2 Define a symbol
1041  
1042 Define the name of a symbol.
1043  
1044 4000 := ERR_NO comment\nline 2 of comment
1045  
1046 =head2 IX and IY base
1047  
1048 Define base address for IX and IY indexed mode.
1049  
1050 4000 :IX
1051 4000 :IY
1052  
1053 =head2 Header block
1054  
1055 Define a text block to be output before the given address. The block is inserted vervbatin,
1056 so include ';' if a comment is intended.
1057  
1058 0000 :# ; header
1059 :# ; continuation
1060 :# abc EQU 23
1061  
1062 =head2 Line comment
1063  
1064 Define a line comment to show at the given address.
1065  
1066 0000 :; comment
1067  
1068 =head2 Header and Footer
1069  
1070 Define a text block to be output at the top and the bottom of the assembly file.
1071 The block is inserted vervbatin, so include ';' if a comment is intended.
1072  
1073 0000 :< ; header
1074 :< ; continuation
1075 :> ; footer
1076  
1077 =cut
1078  
1079 #------------------------------------------------------------------------------
1080  
1081 sub _find_file {
1082 8 ▶ my($self, $from_file, $include_file) = @_;
Subroutine _find_file: called CC=3 SCAR=11.3
1083
1084 8 ▶ return $include_file if -f $include_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $include_file 6 2 8
1085
1086 # test relative to parent
1087 2 my $relative = path(path($from_file)->parent, path($include_file)->basename);
1088 2 ▶ return $relative if -f $relative;
Branch: 1 branch
BranchTrueFalseTotal
if -f $relative 2 0 2
1089
1090 0 return $from_file;
1091 }
1092  
1093 #------------------------------------------------------------------------------
1094  
1095 sub create_control_file {
1096 6 ▶ my($class, $ctl_file, $bin_file, $addr, $arch) = @_;
Subroutine create_control_file: called CC=2 SCAR=6.9
Pod documented
1097
1098 6 ▶ -f $ctl_file and die "Error: $ctl_file exists\n";
Branch: 1 branch
BranchTrueFalseTotal
-f $ctl_file and die "Error: $ctl_file exists\n" 1 5 6
1099
1100 5 my $dis = $class->new;
1101 5 $dis->memory->load_file($bin_file, $addr);
1102 5 $dis->write_dump($ctl_file);
1103 5 my @lines = ( <<END,
1104 ;------------------------------------------------------------------------------
1105 ; CPU::Z80::Disassembler control file
1106 ;------------------------------------------------------------------------------
1107  
1108 END
1109 sprintf("%04X :F $bin_file\n\n", $addr),
1110 path($ctl_file)->lines
1111 );
1112 5 path($ctl_file)->spew(@lines);
1113 }
1114  
1115 #------------------------------------------------------------------------------
1116  
1117 sub load_control_file {
1118 8 ▶ my($self, $file) = @_;
Subroutine load_control_file: called CC=36 SCAR=44.4
Pod documented
1119
1120 8 my $addr = 0; my $end_addr = 0;
1121 8 ▶ open(my $fh, $file) or die "cannot open $file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $file or die "cannot open $file\n" 0 8 8
1122 8 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 72516 8 72524
1123 72516 chomp;
1124 72516 s/^\s*;.*$//; # remove comments
1125 72516 s/\s+$//;
1126 72516 ▶ next unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 10470 62046 72516
1127  
1128 62046 ▶ if (/^ \#include \s+ (\S+) /ix) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^ \#include \s+ (\S+) /xi) { } 2 62044 62046
1129 2 $self->load_control_file($self->_find_file($file, $1));
1130 }
1131 else {
1132 # decode start address
1133 62044 ▶ if (s/^ ([0-9a-f]+) //ix) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^ ([0-9a-f]+) //xi) 41902 20142 62044
1134 41902 $addr = hex($1);
1135 }
1136  
1137 # decode end address
1138 62044 $end_addr = $addr;
1139 62044 ▶ if (s/^ -([0-9a-f]+) //ix) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^ -([0-9a-f]+) //xi) 2926 59118 62044
1140 2926 $end_addr = hex($1);
1141 }
1142  
1143 # remove all chars up to ':', ignore lines without ':'
1144 62044 ▶ /:\s*/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/:\s*/ or next 20948 41096 62044
1145 41096 $_ = $';
1146 41096 ▶ next unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 0 41096 41096
1147
1148 # decode command
1149 41096 my($include_file, $label, $comment, $signature, $type);
1150
1151 # File
1152 41096 ▶ if (($include_file) = /^ F \s+ (\S+) /ix) {
Branch: 11 branches
BranchTrueFalseTotal
if (($include_file) = /^ F \s+ (\S+) /xi) { } 6 41090 41096
elsif (($label) = /^ C \s* (\w+)? /xi) { } 15012 26078 41090
elsif (() = /^ = \s+ (\S+) \s* ;? \s*(.*)/xi) { } 80 25998 26078
elsif (($comment) = /^ \# \s? (.*)/xi) { } 6540 19458 25998
elsif (($comment) = /^ \< \s? (.*)/xi) { } 112 19346 19458
elsif (($comment) = /^ \> \s? (.*)/xi) { } 0 19346 19346
elsif (($comment) = /^ \; [\s;]* (.*)/xi) { } 13492 5854 19346
elsif (($label, $signature) = /^ P \s+ (\w+) \s* (.*)/xi) { } 0 5854 5854
elsif (() = /^ (B2 | B | W | M) (?: \[ (\d+) \] )? \s* (\w+)?/xi) { } 5852 2 5854
elsif (/^ IX /xi) { } 0 2 2
elsif (/^ IY /xi) { } 2 0 2
1153 6 $self->memory->load_file($self->_find_file($file, $include_file), $addr);
1154 }
1155
1156 # Code
1157 elsif (($label) = /^ C \s* (\w+)? /ix) {
1158 15012 $self->code($addr, $label);
1159 }
1160
1161 # Define label
1162 elsif (($label, $comment) = /^ = \s+ (\S+) \s* ;? \s*(.*)/ix) {
1163 80 $comment =~ s/ \\ n /\n/gx;
1164 80 my $instr = $self->labels->add($addr, $label);
1165 80 ▶ $instr->comment($comment) if $comment;
Branch: 1 branch
BranchTrueFalseTotal
if $comment 80 0 80
1166 }
1167
1168 # Block comment
1169 elsif (($comment) = /^ \# \s? (.*)/ix) {
1170 6540 $self->block_comment($addr, $comment);
1171 }
1172
1173 # Header
1174 elsif (($comment) = /^ \< \s? (.*)/ix) {
1175 112 ▶ my $header = $self->header // "";
Condition100%2/2
$self->header // ""
l!l
1102
MC/DC100%1/1
$self->header // ""
$self->header
Truth table100%2/2
A
A= $self->header
Aresult
11
00
1176 112 ▶ $header .= "\n" if $header;
Branch: 1 branch
BranchTrueFalseTotal
if $header 110 2 112
1177 112 $self->header($header.$comment);
1178 }
1179
1180 # Footer
1181 elsif (($comment) = /^ \> \s? (.*)/ix) {
1182 0 ▶ my $footer = $self->footer // "";
Condition0%0/2
$self->footer // ""
l!l
00
MC/DC0%0/1
$self->footer // ""
$self->footer
Truth table0%0/2
A
A= $self->footer
Aresult
11
00
1183 0 ▶ $footer .= "\n" if $footer;
Branch: 1 branch
BranchTrueFalseTotal
if $footer 0 0 0
1184 0 $self->footer($footer.$comment);
1185 }
1186
1187 # Line comment
1188 elsif (($comment) = /^ \; [\s;]* (.*)/ix) {
1189 13492 $self->line_comments($addr, $comment);
1190 }
1191
1192 # Procedure
1193 elsif (($label, $signature) = /^ P \s+ (\w+) \s* (.*)/ix) {
1194 0 $self->code($addr, $label);
1195 0 $signature =~ s/,/ /g;
1196 0 my @types = split(' ', $signature);
1197 0 ▶ @types = ('C') if !@types;
Branch: 1 branch
BranchTrueFalseTotal
unless @types 0 0 0
1198 $self->set_call($addr, sub {
1199 0 ▶ my($self, $addr) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
1200 0 for (@types) {
1201 0 ▶ if ($_ eq 'B') {
Branch: 3 branches
BranchTrueFalseTotal
if ($_ eq 'B') { } 0 0 0
elsif ($_ eq 'W') { } 0 0 0
elsif ($_ eq 'C') { } 0 0 0
1202 0 $self->defb($addr);
1203 0 $addr++
1204 }
1205 elsif ($_ eq 'W') {
1206 0 $self->defW($addr);
1207 0 $addr += 2;
1208 }
1209 elsif ($_ eq 'C') {
1210 0 return $addr;
1211 }
1212 else {
1213 0 die "procedure argument type $_ unknown";
1214 }
1215 }
1216 0 return;
1217 0 });
1218 }
1219
1220 # Byte | Word
1221 elsif (my($type, $ipl, $label) = /^ (B2 | B | W | M) (?: \[ (\d+) \] )? \s* (\w+)?/ix) {
1222 5852 ▶ $self->labels->add($addr, $label) if defined $label;
Branch: 1 branch
BranchTrueFalseTotal
if defined $label 140 5712 5852
1223 5852 ▶ $ipl = 16 unless $ipl;
Branch: 1 branch
BranchTrueFalseTotal
unless $ipl 5852 0 5852
1224  
1225 5852 my($func, $size);
1226 5852 ▶ if ($type eq 'B') { ($func, $size) = ('defb', 1); }
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'B') { } 5448 404 5852
elsif ($type eq 'B2') { } 0 404 404
elsif ($type eq 'W') { } 404 0 404
elsif ($type eq 'M') { } 0 0 0
1227 0 elsif ($type eq 'B2') { ($func, $size) = ('defb2', 1); }
1228 404 elsif ($type eq 'W') { ($func, $size) = ('defw', 2); }
1229 0 elsif ($type eq 'M') { ($func, $size) = ('defm', 1); $ipl = 32; }
1230 0 else { die "type $type unknown"; }
1231
1232 5852 ▶ if ($size == 2 && $addr == $end_addr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size == 2 and $addr == $end_addr) 202 5650 5852
Condition100%3/3
$size == 2 and $addr == $end_addr
!ll&&!rl&&r
5448202202
MC/DC100%2/2
$size == 2 and $addr == $end_addr
$size == 2 $addr == $end_addr
Truth table100%3/3
A and B
A= $size == 2B= $addr == $end_addr
ABresult
0X0
100
111
1233 202 $end_addr++; # a word uses two addresses
1234 }
1235
1236 5852 ▶ for (my $a = $addr; $a <= $end_addr; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a <= $end_addr) 5852 5852 11704
1237 5852 my $items = int(($end_addr - $a + 1) / $size);
1238 5852 ▶ $items = $ipl if $items > $ipl;
Branch: 1 branch
BranchTrueFalseTotal
if $items > $ipl 0 5852 5852
1239
1240 5852 $self->$func($a, $items);
1241 5852 $a += $size * $items;
1242 }
1243 }
1244
1245 # IX
1246 elsif (/^ IX /ix) {
1247 0 $self->ix_base($addr);
1248 }
1249
1250 # IY
1251 elsif (/^ IY /ix) {
1252 2 $self->iy_base($addr);
1253 }
1254
1255 # undefined
1256 else {
1257 0 die "Load '$file': cannot parse '$_'";
1258 }
1259 }
1260 }
1261 }
1262
1263 #------------------------------------------------------------------------------
1264 sub relative_arg {
1265 25 ▶ my($self, $addr, $label_name) = @_;
Subroutine relative_arg: called CC=7 SCAR=19.5
Pod documented
1266  
1267 # disassemble from here, if needed
1268 25 $self->code($addr);
1269 25 ▶ my $instr = $self->_get_instr($addr) or die;
Branch: 1 branch
BranchTrueFalseTotal
my $instr = $self->_get_instr($addr) or die 0 25 25
1270
1271 25 my $label_addr;
1272 25 ▶ if ($label_name eq '$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($label_name eq '$') { } 3 22 25
1273 3 $label_addr = $instr->addr;
1274 }
1275 else {
1276 22 ▶ my $label = $self->labels->search_name($label_name)
Branch: 1 branch
BranchTrueFalseTotal
my $label = $self->labels->search_name($label_name) or croak("Label '${label_name}' not found") 1 21 22
1277 or croak("Label '$label_name' not found");
1278 21 $label_addr = $label->addr;
1279 }
1280
1281 24 ▶ my $NN = defined($instr->NN) ? 'NN' :
Branch: 2 branches
BranchTrueFalseTotal
defined $instr->NN ? : 19 5 24
defined $instr->N ? : 4 1 5
1282 defined($instr->N ) ? 'N' :
1283 croak("Instruction at address ".format_hex4($addr).
1284 " has no arguments");
1285 23 my $arg = $instr->$NN;
1286 23 ▶ $arg = [$arg] unless ref $arg; # defb stores as [N]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg 23 0 23
1287
1288 23 my $delta = $arg->[0] - $label_addr;
1289 23 my $expr = $label_name . format_dis($delta);
1290 23 ▶ $instr->format->{$NN} = sub { $expr };
Subroutine __ANON__: called CC=1 SCAR=0.0
1291 }
1292  
1293 #------------------------------------------------------------------------------
1294  
1295 =head1 ACKNOWLEDGEMENTS
1296  
1297 =head1 AUTHOR
1298  
1299 Paulo Custodio, C<< <pscust at cpan.org> >>
1300  
1301 =head1 BUGS and FEEDBACK
1302  
1303 Please report any bugs or feature requests through
1304 the web interface at
1305 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CPU-Z80-Disassembler>.
1306  
1307 =head1 LICENSE AND COPYRIGHT
1308  
1309 Copyright 2010 Paulo Custodio.
1310  
1311 This program is free software; you can redistribute it and/or modify it
1312 under the terms of either: the GNU General Public License as published
1313 by the Free Software Foundation; or the Artistic License.
1314  
1315 See http://dev.perl.org/licenses/ for more information.
1316  
1317 The Spectrum 48K ROM used in the test scripts is Copyright by Amstrad.
1318 Amstrad have kindly given their permission for the
1319 redistribution of their copyrighted material but retain that copyright
1320 (see L<http://www.worldofspectrum.org/permits/amstrad-roms.txt>).
1321  
1322 =cut
1323  
1324 10 1;
↑ Summary blib/lib/CPU/Z80/Disassembler/Instruction.pm »