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

filter: Statementstmt 100% 30 / 30 Branchbran 100% 12 / 12 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 6 / 6 totaltotal 100% 62 / 62 SCAR 20.8
CC
8
Cov
100%
format_bin8
5.0
format_dis
3.0
format_hex
2.0
CRAP 8.0
« blib/lib/CPU/Z80/Disassembler/Label.pm ↑ Summary bin/z80dis »
1 package CPU::Z80::Disassembler::Format;
2  
3 #------------------------------------------------------------------------------
4  
5 =head1 NAME
6  
7 CPU::Z80::Disassembler::Format - Format output of disassembler
8  
9 =cut
10  
11 #------------------------------------------------------------------------------
12  
13 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 15 our $VERSION = '1.00';
17  
18 #------------------------------------------------------------------------------
19  
20 =head1 SYNOPSYS
21  
22 use CPU::Z80::Disassembler::Format;
23 print format_hex($x), format_hex2($x), format_hex4($x); format_bin8($x);
24 print format_dis($x), format_str($x);
25  
26 =head1 DESCRIPTION
27  
28 Exports functions to format output values in the disassembler listing.
29  
30 =head1 EXPORTS
31  
32 Exports all functions by default.
33  
34 =head1 FUNCTIONS
35  
36 =cut
37 #------------------------------------------------------------------------------
38 15 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 15 our @EXPORT = qw( format_hex format_hex2 format_hex4
40 format_bin8
41 format_dis format_str );
42 #------------------------------------------------------------------------------
43  
44 =head2 format_hex
45  
46 Returns the string representation of a value in hexadecimal..
47  
48 =cut
49  
50 #------------------------------------------------------------------------------
51 sub format_hex {
52 1244 ▶ $_[0] < 0 ? sprintf("-\$%02X", -$_[0]) : sprintf("\$%02X", $_[0]);
Subroutine format_hex: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < 0 ? : 24 1220 1244
53 }
54 #------------------------------------------------------------------------------
55  
56 =head2 format_hex2
57  
58 Returns the string representation of a byte in hexadecimal as $HH.
59  
60 =cut
61  
62 #------------------------------------------------------------------------------
63 sub format_hex2 {
64 64743 ▶ sprintf("\$%02X", $_[0] & 0xFF)
Subroutine format_hex2: called CC=1 SCAR=0.0
Pod documented
65 }
66 #------------------------------------------------------------------------------
67  
68 =head2 format_hex4
69  
70 Returns the string representation of a word in hexadecimal as $HHHH.
71  
72 =cut
73  
74 #------------------------------------------------------------------------------
75 sub format_hex4 {
76 19782 ▶ sprintf("\$%04X", $_[0] & 0xFFFF)
Subroutine format_hex4: called CC=1 SCAR=0.0
Pod documented
77 }
78 #------------------------------------------------------------------------------
79  
80 =head2 format_bin8
81  
82 Returns the string representation of a word in binary as %01010101.
83  
84 =cut
85  
86 #------------------------------------------------------------------------------
87 sub format_bin8 {
88 773 ▶ my($val) = @_;
Subroutine format_bin8: called CC=5 SCAR=16.1
Pod documented
89  
90 773 my $sign = '';
91 773 ▶ if ($val < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < 0) 1 772 773
92 1 $val = -$val;
93 1 $sign = '-';
94 }
95  
96 773 my $digits = '';
97 773 ▶ while ($val != 0 || length($digits) < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val != 0 or length $digits < 8) 6185 773 6958
Condition100%3/3
$val != 0 or length $digits < 8
l!l&&r!l&&!r
30103175773
MC/DC100%2/2
$val != 0 or length $digits < 8
$val != 0 length $digits < 8
Truth table100%3/3
A or B
A= $val != 0B= length $digits < 8
ABresult
1X1
011
000
98 6185 ▶ $digits = (($val & 1) ? '1' : '0') . $digits;
Branch: 1 branch
BranchTrueFalseTotal
$val & 1 ? : 1167 5018 6185
99 6185 $val >>= 1;
100 }
101
102 773 return $sign.'%'.$digits;
103 }
104 #------------------------------------------------------------------------------
105  
106 =head2 format_dis
107  
108 Returns the string representation of a signed byte in hexadecimal as +$HH, -$HH or
109 empty string for zero.
110  
111 =cut
112  
113 #------------------------------------------------------------------------------
114 sub format_dis {
115 1297 ▶ my($arg) = @_;
Subroutine format_dis: called CC=3 SCAR=11.0
Pod documented
116 1297 ▶ $arg < 0 ? '-'.format_hex(-$arg) :
Branch: 2 branches
BranchTrueFalseTotal
$arg < 0 ? : 65 1232 1297
$arg > 0 ? : 1129 103 1232
117 $arg > 0 ? '+'.format_hex( $arg) :
118 '';
119 }
120 #------------------------------------------------------------------------------
121  
122 =head2 format_str
123  
124 Returns the string representation of an assembly string: double-quoted, all
125 double-quotes inside are escaped.
126  
127 =cut
128  
129 #------------------------------------------------------------------------------
130 sub format_str {
131 220 ▶ my($str) = @_;
Subroutine format_str: called CC=1 SCAR=0.0
Pod documented
132 220 $str =~ s/(["\\])/\\$1/g;
133 220 return '"'.$str.'"';
134 }
135 #------------------------------------------------------------------------------
136  
137 =head1 BUGS, FEEDBACK, AUTHORS, COPYRIGHT and LICENCE
138  
139 See L<CPU::Z80::Disassembler|CPU::Z80::Disassembler>.
140  
141 =cut
142  
143 15 1;
« blib/lib/CPU/Z80/Disassembler/Label.pm ↑ Summary bin/z80dis »