blib/lib/CPU/Z80/Disassembler/Memory.pm

filter: Statementstmt 100% 131 / 131 Branchbran 100% 48 / 48 Conditioncond 96.0% 24 / 25 MC/DCmcdc 87.5% 14 / 16 Subroutinesub 100% 34 / 34 Podpod 100% 19 / 19 totaltotal 98.9% 270 / 273 SCAR 35.3
CC
34
Cov
100%
load_file
4.0
peek_str
4.0
_check_addr
3.0
CRAP 34.0
« blib/lib/CPU/Z80/Disassembler/Instruction.pm ↑ Summary blib/lib/CPU/Z80/Disassembler/Labels.pm »
1 package CPU::Z80::Disassembler::Memory;
2  
3 #------------------------------------------------------------------------------
4  
5 =head1 NAME
6  
7 CPU::Z80::Disassembler::Memory - Memory representation for Z80 disassembler
8  
9 =cut
10  
11 #------------------------------------------------------------------------------
12  
13 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 ▶use Carp; our @CARP_NOT; # do not report errors in this package
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use Bit::Vector;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 12 ▶use CPU::Z80::Disassembler::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 12 our $VERSION = '1.00';
23  
24 #------------------------------------------------------------------------------
25  
26 =head1 SYNOPSIS
27  
28 use CPU::Z80::Disassembler::Memory;
29 $mem = CPU::Z80::Disassembler::Memory->new;
30
31 $mem->load_file($file_name, $addr, $opt_skip_bytes, $opt_length);
32 $it = $mem->loaded_iter(); while (($min,$max) = $it->()) {}
33
34 $byte = $mem->peek8u($addr); $byte = $mem->peek($addr);
35 $byte = $mem->peek8s($addr);
36
37 $word = $mem->peek16u($addr);
38 $word = $mem->peek16s($addr);
39
40 $str = $mem->peek_str( $addr, $length);
41 $str = $mem->peek_strz($addr);
42 $str = $mem->peek_str7($addr);
43
44 $mem->poke8u($addr, $byte); $mem->poke($addr, $byte);
45 $mem->poke8s($addr, $byte);
46
47 $mem->poke16u($addr, $word);
48 $mem->poke16s($addr, $word);
49
50 $mem->poke_str( $addr, $str);
51 $mem->poke_strz($addr, $str);
52 $mem->poke_str7($addr, $str);
53  
54 =head1 DESCRIPTION
55  
56 This module represents a memory segment being diassembled.
57  
58 =head1 FUNCTIONS
59  
60 =head2 new
61  
62 Creates a new empty object.
63  
64 =cut
65  
66 #------------------------------------------------------------------------------
67 12 ▶use base 'Class::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 12 __PACKAGE__->mk_accessors(
69 '_mem', # string of 64 Kbytes
70 '_loaded', # Bit::Vector, one bit per address, 1 if byte loaded
71 );
72  
73 sub new {
74 59 ▶ my($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
75 59 my $loaded = Bit::Vector->new(0x10000);
76 59 my $mem = "\0" x $loaded->Size;
77 59 return bless { _mem => $mem, _loaded => $loaded }, $class;
78 }
79  
80 #------------------------------------------------------------------------------
81 # check ranges
82  
83 sub _check_addr {
84 969461 ▶ my($self, $addr) = @_;
Subroutine _check_addr: called CC=3 SCAR=11.0
85 969461 ▶ croak("address ".format_hex($addr)." out of range")
Branch: 1 branch
BranchTrueFalseTotal
if $addr < 0 or $addr >= $self->_loaded->Size 37 969424 969461
Condition100%3/3
$addr < 0 or $addr >= $self->_loaded->Size
l!l&&r!l&&!r
1819969424
MC/DC100%2/2
$addr < 0 or $addr >= $self->_loaded->Size
$addr < 0 $addr >= $self->_loaded->Size
Truth table100%3/3
A or B
A= $addr < 0B= $addr >= $self->_loaded->Size
ABresult
1X1
011
000
86 if ($addr < 0 || $addr >= $self->_loaded->Size);
87 }
88  
89 sub _check_value8u {
90 41101 ▶ my($self, $byte) = @_;
Subroutine _check_value8u: called CC=3 SCAR=11.0
91 41101 ▶ croak("unsigned byte ".format_hex($byte)." out of range")
Branch: 1 branch
BranchTrueFalseTotal
if $byte < 0 or $byte > 255 4 41097 41101
Condition100%3/3
$byte < 0 or $byte > 255
l!l&&r!l&&!r
2241097
MC/DC100%2/2
$byte < 0 or $byte > 255
$byte < 0 $byte > 255
Truth table100%3/3
A or B
A= $byte < 0B= $byte > 255
ABresult
1X1
011
000
92 if ($byte < 0 || $byte > 0xFF);
93 }
94  
95 sub _check_value8s {
96 9 ▶ my($self, $byte) = @_;
Subroutine _check_value8s: called CC=3 SCAR=11.0
97 9 ▶ croak("signed byte ".format_hex($byte)." out of range")
Branch: 1 branch
BranchTrueFalseTotal
if $byte < -128 or $byte > 127 2 7 9
Condition100%3/3
$byte < -128 or $byte > 127
l!l&&r!l&&!r
117
MC/DC100%2/2
$byte < -128 or $byte > 127
$byte < -128 $byte > 127
Truth table100%3/3
A or B
A= $byte < -128B= $byte > 127
ABresult
1X1
011
000
98 if ($byte < -0x80 || $byte > 0x7F);
99 }
100  
101 sub _check_value16u {
102 12 ▶ my($self, $word) = @_;
Subroutine _check_value16u: called CC=3 SCAR=11.0
103 12 ▶ croak("unsigned word ".format_hex($word)." out of range")
Branch: 1 branch
BranchTrueFalseTotal
if $word < 0 or $word > 65535 2 10 12
Condition100%3/3
$word < 0 or $word > 65535
l!l&&r!l&&!r
1110
MC/DC100%2/2
$word < 0 or $word > 65535
$word < 0 $word > 65535
Truth table100%3/3
A or B
A= $word < 0B= $word > 65535
ABresult
1X1
011
000
104 if ($word < 0 || $word > 0xFFFF);
105 }
106  
107 sub _check_value16s {
108 9 ▶ my($self, $word) = @_;
Subroutine _check_value16s: called CC=3 SCAR=11.0
109 9 ▶ croak("signed word ".format_hex($word)." out of range")
Branch: 1 branch
BranchTrueFalseTotal
if $word < -32768 or $word > 32767 2 7 9
Condition100%3/3
$word < -32768 or $word > 32767
l!l&&r!l&&!r
117
MC/DC100%2/2
$word < -32768 or $word > 32767
$word < -32768 $word > 32767
Truth table100%3/3
A or B
A= $word < -32768B= $word > 32767
ABresult
1X1
011
000
110 if ($word < -0x8000 || $word > 0x7FFF);
111 }
112  
113 sub _check_strz {
114 7 ▶ my($self, $str) = @_;
Subroutine _check_strz: called CC=2 SCAR=6.9
115 7 ▶ croak("invalid zero character in string")
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /\0/ 1 6 7
116 if $str =~ /\0/;
117 }
118  
119 sub _check_str7 {
120 8 ▶ my($self, $str) = @_;
Subroutine _check_str7: called CC=3 SCAR=11.0
121 8 ▶ croak("invalid empty string") if length($str) < 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $str < 1 1 7 8
122 7 ▶ croak("invalid bit-7 set character in string")
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /[\x80-\xFF]/ 1 6 7
123 if $str =~ /[\x80-\xFF]/;
124 }
125  
126 #------------------------------------------------------------------------------
127  
128 =head2 load_file
129  
130 Loads a binary file to the memory.
131 The argument C<$addr> indicates where in the memory to load the file, and defaults to 0.
132 The argument C<$opt_skip_bytes> indicates how many bytes to skip from the start
133 of the binary file and defaults to 0.
134 This is useful to read C<.SNA> ZX Spectrum Snapshot Files which have a header of 27 bytes.
135 The argument C<$opt_length> limits the number of bytes to read to memory and
136 defaults to all the file after the header.
137  
138 =cut
139  
140 #------------------------------------------------------------------------------
141 sub load_file {
142 30 ▶ my($self, $file_name, $addr, $opt_skip_bytes, $opt_length) = @_;
Subroutine load_file: called CC=4 SCAR=13.9
Pod documented
143
144 30 my $bytes = read_file($file_name, binmode => ':raw');
145 29 ▶ $addr ||= 0;
Condition100%2/2
$addr ||= 0
l!l
623
MC/DC100%1/1
$addr ||= 0
$addr
Truth table100%2/2
A
A= $addr
Aresult
11
00
146 29 ▶ $opt_skip_bytes ||= 0;
Condition100%2/2
$opt_skip_bytes ||= 0
l!l
227
MC/DC100%1/1
$opt_skip_bytes ||= 0
$opt_skip_bytes
Truth table100%2/2
A
A= $opt_skip_bytes
Aresult
11
00
147 29 ▶ $opt_length ||= length($bytes) - $opt_skip_bytes;
Condition66%2/3
$opt_length ||= length($bytes) - $opt_skip_bytes
l!l&&r!l&&!r
2270
MC/DC0%0/2
$opt_length ||= length($bytes) - $opt_skip_bytes
$opt_length length($bytes) - $opt_skip_bytes
Truth table66%2/3
A ||= B
A= $opt_lengthB= length($bytes) - $opt_skip_bytes
ABresult
1X1
011
000
148
149 29 $self->poke_str($addr, substr($bytes, $opt_skip_bytes, $opt_length));
150 }
151 #------------------------------------------------------------------------------
152  
153 =head2 loaded_iter
154  
155 Returns an iterator to return each block of consecutive loaded addresses.
156 C<$min> is the first address of the consecutive block, C<$max> is last address
157 of the block.
158  
159 =cut
160  
161 #------------------------------------------------------------------------------
162 sub loaded_iter {
163 49 ▶ my($self) = @_;
Subroutine loaded_iter: called CC=1 SCAR=0.0
Pod documented
164 49 my $loaded = $self->_loaded;
165 49 my $start = 0;
166
167 return sub {
168 91 ▶ while ( $start < $loaded->Size &&
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if ($start < $loaded->Size and () = $loaded->Interval_Scan_inc($start)) 42 49 91
Condition100%3/3
$start < $loaded->Size and () = $loaded->Interval_Scan_inc($start)
!ll&&!rl&&r
24742
MC/DC100%2/2
$start < $loaded->Size and () = $loaded->Interval_Scan_inc($start)
$start < $loaded->Size () = $loaded->Interval_Scan_inc($start)
Truth table100%3/3
A and B
A= $start < $loaded->SizeB= () = $loaded->Interval_Scan_inc($start)
ABresult
0X0
100
111
169 (my($min,$max) = $loaded->Interval_Scan_inc($start)) ) {
170 42 $start = $max + 2; # start after the 0 after $max
171 42 return ($min, $max);
172 }
173 49 return (); # no more blocks
174 49 };
175 }
176 #------------------------------------------------------------------------------
177  
178 =head2 peek, peek8u
179  
180 Retrieves the byte (0 .. 255) from the given address.
181 Returns C<undef> if the memory at that address was not loaded.
182  
183 =cut
184  
185 #------------------------------------------------------------------------------
186 sub peek8u {
187 928242 ▶ my($self, $addr) = @_;
Subroutine peek8u: called CC=2 SCAR=6.9
Pod documented
188 928242 $self->_check_addr($addr);
189 return $self->_loaded->bit_test($addr) ?
190 928226 ▶ ord(substr($self->{_mem}, $addr, 1)) :
Branch: 1 branch
BranchTrueFalseTotal
$self->_loaded->bit_test($addr) ? : 802974 125252 928226
191 undef;
192 }
193 860744 ▶sub peek { goto &peek8u }
Subroutine peek: called CC=1 SCAR=0.0
Pod documented
194 #------------------------------------------------------------------------------
195  
196 =head2 peek8s
197  
198 Same as C<peek8u>, but treats byte as signed (-128 .. 127).
199  
200 =cut
201  
202 #------------------------------------------------------------------------------
203 sub peek8s {
204 9528 ▶ my($self, $addr) = @_;
Subroutine peek8s: called CC=3 SCAR=11.0
Pod documented
205 9528 my $byte = $self->peek8u($addr);
206 9526 ▶ return undef unless defined $byte;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $byte 3 9523 9526
207 9523 ▶ $byte -= 0x100 if $byte & 0x80;
Branch: 1 branch
BranchTrueFalseTotal
if $byte & 128 2252 7271 9523
208 9523 return $byte;
209 }
210 #------------------------------------------------------------------------------
211  
212 =head2 peek16u
213  
214 Retrieves the two-byte word (0 .. 65535) from the given address, least
215 significant first (little-endian).
216 Returns C<undef> if the memory at any of the two addresses was not loaded.
217  
218 =cut
219  
220 #------------------------------------------------------------------------------
221 sub peek16u {
222 12459 ▶ my($self, $addr) = @_;
Subroutine peek16u: called CC=3 SCAR=11.0
Pod documented
223 12459 ▶ my $lo = $self->peek($addr++); return undef unless defined $lo;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lo 5 12450 12455
224 12450 ▶ my $hi = $self->peek($addr++); return undef unless defined $hi;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hi 2 12448 12450
225 12448 return ($hi << 8) | $lo;
226 }
227 #------------------------------------------------------------------------------
228  
229 =head2 peek16s
230  
231 Same as C<peek16u>, but treats word as signed (-32768 .. 32767).
232  
233 =cut
234  
235 #------------------------------------------------------------------------------
236 sub peek16s {
237 6 ▶ my($self, $addr) = @_;
Subroutine peek16s: called CC=3 SCAR=11.0
Pod documented
238 6 my $word = $self->peek16u($addr);
239 4 ▶ return undef unless defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 2 2 4
240 2 ▶ $word -= 0x10000 if $word & 0x8000;
Branch: 1 branch
BranchTrueFalseTotal
if $word & 32768 1 1 2
241 2 return $word;
242 }
243 #------------------------------------------------------------------------------
244  
245 =head2 peek_str
246  
247 Retrieves a string from the given address with the given length.
248 Returns C<undef> if the memory at any of the addresses was not loaded.
249  
250 =cut
251  
252 #------------------------------------------------------------------------------
253 sub peek_str {
254 99 ▶ my($self, $addr, $length) = @_;
Subroutine peek_str: called CC=4 SCAR=13.9
Pod documented
255 99 ▶ croak("invalid length $length") if $length < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $length < 1 1 98 99
256 98 my $str = "";
257 98 ▶ while ($length-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length-- > 0) 167 91 258
258 167 my $byte = $self->peek8u($addr++);
259 165 ▶ return undef unless defined $byte;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $byte 5 160 165
260 160 $str .= chr($byte);
261 }
262 91 return $str;
263 }
264 #------------------------------------------------------------------------------
265  
266 =head2 peek_strz
267  
268 Retrieves a zero-terminated string from the given address. The returned string
269 does not include the final zero byte.
270 Returns C<undef> if the memory at any of the addresses was not loaded.
271  
272 =cut
273  
274 #------------------------------------------------------------------------------
275 sub peek_strz {
276 9 ▶ my($self, $addr) = @_;
Subroutine peek_strz: called CC=3 SCAR=11.0
Pod documented
277 9 my $str = "";
278 9 while (1) {
279 55 my $byte = $self->peek8u($addr++);
280 53 ▶ return undef unless defined $byte;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $byte 5 48 53
281 48 ▶ return $str if $byte == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $byte == 0 2 46 48
282 46 $str .= chr($byte);
283 }
284 }
285 #------------------------------------------------------------------------------
286  
287 =head2 peek_str7
288  
289 Retrieves a bit-7-set-terminated string from the given address.
290 This string has all characters with bit 7 reset, execept the last character,
291 where bit 7 is set. The returned string has bit 7 reset in all characters.
292 Returns C<undef> if the memory at any of the addresses was not loaded.
293  
294 =cut
295  
296 #------------------------------------------------------------------------------
297 sub peek_str7 {
298 137 ▶ my($self, $addr) = @_;
Subroutine peek_str7: called CC=3 SCAR=11.0
Pod documented
299 137 my $str = "";
300 137 while (1) {
301 964 my $byte = $self->peek8u($addr++);
302 962 ▶ return undef unless defined $byte;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $byte 5 957 962
303 957 $str .= chr($byte & 0x7F); # clear bit 7
304 957 ▶ return $str if $byte & 0x80; # bit 7 set
Branch: 1 branch
BranchTrueFalseTotal
if $byte & 128 130 827 957
305 }
306 }
307 #------------------------------------------------------------------------------
308  
309 =head2 poke, poke8u
310  
311 Stores the unsigned byte (0 .. 255) at the given address,
312 and signals that the address was loaded.
313  
314 =cut
315  
316 #------------------------------------------------------------------------------
317 sub poke8u {
318 41107 ▶ my($self, $addr, $byte) = @_;
Subroutine poke8u: called CC=1 SCAR=0.0
Pod documented
319 41107 $self->_check_addr($addr);
320 41101 $self->_check_value8u($byte);
321 41097 substr($self->{_mem}, $addr, 1) = chr($byte);
322 41097 $self->_loaded->Bit_On($addr);
323 }
324 41071 ▶sub poke { goto &poke8u }
Subroutine poke: called CC=1 SCAR=0.0
Pod documented
325 #------------------------------------------------------------------------------
326  
327 =head2 poke8s
328  
329 Same as C<poke8u>, but treats byte as signed (-128 .. 127).
330  
331 =cut
332  
333 #------------------------------------------------------------------------------
334 sub poke8s {
335 9 ▶ my($self, $addr, $byte) = @_;
Subroutine poke8s: called CC=1 SCAR=0.0
Pod documented
336 9 $self->_check_value8s($byte);
337 7 $self->poke8u($addr, $byte & 0xFF);
338 }
339 #------------------------------------------------------------------------------
340  
341 =head2 poke16u
342  
343 Stores the two-byte word (0 .. 65535) at the given address, least
344 significant first (little-endian),
345 and signals that the address was loaded.
346  
347 =cut
348  
349 #------------------------------------------------------------------------------
350 sub poke16u {
351 16 ▶ my($self, $addr, $word) = @_;
Subroutine poke16u: called CC=1 SCAR=0.0
Pod documented
352 16 $self->_check_addr($addr);
353 12 $self->_check_value16u($word);
354 10 $self->poke8u($addr++, $word & 0xFF);
355 10 $self->poke8u($addr++, ($word >> 8) & 0xFF);
356 }
357 #------------------------------------------------------------------------------
358  
359 =head2 poke16s
360  
361 Same as C<poke16u>, but treats word as signed (-32768 .. 32767).
362  
363 =cut
364  
365 #------------------------------------------------------------------------------
366 sub poke16s {
367 9 ▶ my($self, $addr, $word) = @_;
Subroutine poke16s: called CC=1 SCAR=0.0
Pod documented
368 9 $self->_check_value16s($word);
369 7 $self->poke16u($addr, $word & 0xFFFF);
370 }
371 #------------------------------------------------------------------------------
372  
373 =head2 poke_str
374  
375 Stores the string at the given start address,
376 and signals that the addresser were loaded.
377  
378 =cut
379  
380 #------------------------------------------------------------------------------
381 sub poke_str {
382 53 ▶ my($self, $addr, $str) = @_;
Subroutine poke_str: called CC=2 SCAR=6.9
Pod documented
383 53 $self->_check_addr($addr);
384  
385 44 ▶ if (length($str) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str > 0) 43 1 44
386 43 my $end_addr = $addr + length($str) - 1;
387 43 $self->_check_addr($end_addr);
388
389 41 substr($self->{_mem}, $addr, length($str)) = $str;
390 41 $self->_loaded->Interval_Fill($addr, $end_addr);
391 }
392 }
393 #------------------------------------------------------------------------------
394  
395 =head2 poke_strz
396  
397 Stores the string at the given start address, and adds a zero byte,
398 and signals that the addresses were loaded.
399  
400 =cut
401  
402 #------------------------------------------------------------------------------
403 sub poke_strz {
404 7 ▶ my($self, $addr, $str) = @_;
Subroutine poke_strz: called CC=1 SCAR=0.0
Pod documented
405 7 $self->_check_strz($str);
406 6 $self->poke_str($addr, $str.chr(0));
407 }
408 #------------------------------------------------------------------------------
409  
410 =head2 poke_str7
411  
412 Stores the string at the given start address and sets the bit 7 of the
413 last character,
414 and signals that the addresses were loaded.
415  
416 =cut
417  
418 #------------------------------------------------------------------------------
419 sub poke_str7 {
420 8 ▶ my($self, $addr, $str) = @_;
Subroutine poke_str7: called CC=1 SCAR=0.0
Pod documented
421 8 $self->_check_str7($str);
422 6 substr($str, -1, 1) = chr(ord(substr($str, -1, 1)) | 0x80); # set bit 7
423 6 $self->poke_str($addr, $str);
424 }
425 #------------------------------------------------------------------------------
426  
427 =head1 AUTHOR, BUGS, FEEDBACK, LICENSE AND COPYRIGHT
428  
429 See L<CPU::Z80::Disassembler|CPU::Z80::Disassembler>.
430  
431 =cut
432  
433 #------------------------------------------------------------------------------
434  
435 12 1;
« blib/lib/CPU/Z80/Disassembler/Instruction.pm ↑ Summary blib/lib/CPU/Z80/Disassembler/Labels.pm »