blib/lib/D64/File/PRG.pm

filter: Statementstmt 76.6% 226 / 295 Branchbran 44.8% 52 / 116 Conditioncond 41.1% 7 / 17 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 85.7% 24 / 28 Podpod 100% 7 / 7 totaltotal 67.0% 317 / 473 CC 68 SCAR 28.1
CC
68
Cov
67%
write_file
72.0
read_file
42.0
_write_raw_contents_to_file
30.0
CRAP 16.6
↑ Summary
1 package D64::File::PRG;
2
3 =head1 NAME
4
5 D64::File::PRG - Handling individual C64's PRG files
6
7 =head1 SYNOPSIS
8
9 use D64::File::PRG;
10
11 my $prg = D64::File::PRG->new('FILE' => $file);
12 my $prg = D64::File::PRG->new('RAW_DATA' => \$data, 'LOADING_ADDRESS' => 0x0801);
13
14 $prg->change_loading_address('LOADING_ADDRESS' => 0x6400);
15
16 my $data = $prg->get_data();
17 my $data = $prg->get_data('FORMAT' => 'ASM', 'ROW_LENGTH' => 10);
18
19 $prg->set_data('RAW_DATA' => \$data, 'LOADING_ADDRESS' => 0x1000);
20 $prg->set_data('RAW_DATA' => \$data);
21 $prg->set_file_data('FILE_DATA' => \$file_data);
22
23 $prg->write_file('FILE' => $file);
24
25 =head1 DESCRIPTION
26
27 D64::File::PRG is a Perl module providing the set of methods for handling individual C64's PRG files. It enables an easy access to the raw contents of any PRG file, manipulation of its loading address, and transforming binary data into assembly code understood by tools like "Dreamass" or "Turbo Assembler".
28
29 =head1 METHODS
30
31 =cut
32
33 2 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36
37 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use IO::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
41
42 2 our $VERSION = '0.03';
43 2 our @ISA = qw(Exporter);
44 2 our @EXPORT = ();
45 2 our @EXPORT_OK = qw();
46 2 our %EXPORT_TAGS = (default => [qw()]);
47
48 2 local $| = 1;
49
50 =head2 new
51
52 A new D64::File::PRG object instance is created either by providing the path to an existing file, which is then being read into a memory, or by providing a scalar reference to the raw binary data with an accompanying loading address. This is illustrated by the following examples:
53
54 my $prg = D64::File::PRG->new('FILE' => $file);
55 my $prg = D64::File::PRG->new('RAW_DATA' => \$data, 'LOADING_ADDRESS' => 0x0801);
56
57 Constructor will die upon one of the following conditions:
58
59 1. File size is less than two bytes.
60 2. Loading address provided is outside of a 16-bit range.
61 3. Any character within a raw data is not a single byte.
62
63 There is an additional optional boolean "VERBOSE" available (defaulting to 0), which indicates that the extensive debugging messages should be printed out to the standard output. By default module acts silently, reporting error messages only.
64
65 =cut
66
67 15 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
68 15 my $this = shift;
69 15 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0150
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
70 15 my $self = {};
71 15 bless $self, $class;
72 15 $self->_initialize(@_);
73 15 return $self;
74 }
75
76 15 ▶sub _initialize {
Subroutine _initialize: called CC=5 SCAR=16.8
77 15 my $self = shift;
78 15 my $params = {@_};
79 15 my $verbose = $params->{'VERBOSE'}; # display diagnostic messages
80 15 my $file = $params->{'FILE'}; # get data from file
81 15 my $loading_address = $params->{'LOADING_ADDRESS'}; # externally provided loading address
82 15 my $raw_data_ref = $params->{'RAW_DATA'}; # externally provided raw data
83 15 my ($package) = (caller(0))[0];
84 15 $self->{'VERBOSE'} = $verbose;
85 15 ▶ $self->_verbose_message('MESSAGE' => "Initializing new \"${package}\" object instance", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 15 15
86 # When "FILE" parameter is defined, it takes the precedence over "LOAD/DATA" parameters:
87 15 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) { } 0 15 15
88 0 $self->read_file('FILE' => $file);
89 }
90 else {
91 # LOADING_ADDRESS: loading address (must be a valid value)
92 15 ▶ unless (defined $loading_address) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $loading_address) 0 15 15
93 0 $self->_verbose_message('MESSAGE' => "an undefined loading address has been provided to the constructor", 'ERROR' => 1);
94 }
95 15 $self->_get_loading_address_from_scalar('LOADING_ADDRESS' => $loading_address, 'VERBOSE' => $verbose);
96 # RAW_DATA: binary data provided as a raw data scalar
97 15 $self->_get_raw_contents_from_scalarref('RAW_DATA' => $raw_data_ref, 'VERBOSE' => $verbose);
98 }
99 15 ▶ $self->_verbose_message('MESSAGE' => "Returning new object instance upon successful init", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 15 15
100 15 return;
101 }
102
103 18 ▶sub _get_loading_address_from_scalar {
Subroutine _get_loading_address_from_scalar: called CC=6 SCAR=18.9
104 18 my $self = shift;
105 18 my $params = {@_};
106 18 my $loading_address = $params->{'LOADING_ADDRESS'}; # externally provided loading address
107 18 my $verbose = $params->{'VERBOSE'}; # display diagnostic messages
108 18 ▶ unless (looks_like_number $loading_address) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($loading_address)) 1 17 18
109 1 $self->_verbose_message('MESSAGE' => "a non-numeric scalar value cannot be converted into loading address", 'ERROR' => 1);
110 }
111 17 my $loading_address_readable = uc sprintf "\$%04x", $loading_address;
112 17 ▶ $self->_verbose_message('MESSAGE' => "Validating value of provided loading address: ${loading_address_readable}", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 17 17
113 17 ▶ if ($loading_address < 0x0000 or $loading_address > 0xffff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loading_address < 0 or $loading_address > 65535) 0 17 17
Condition33%1/3
$loading_address < 0 or $loading_address > 65535
l!l&&r!l&&!r
0017
MC/DC0%0/2
$loading_address < 0 or $loading_address > 65535
$loading_address < 0 $loading_address > 65535
Truth table33%1/3
A or B
A= $loading_address < 0B= $loading_address > 65535
ABresult
1X1
011
000
114 0 $self->_verbose_message('MESSAGE' => "invalid loading address provided (${loading_address_readable})", 'ERROR' => 1);
115 }
116 17 ▶ $self->_verbose_message('MESSAGE' => "Received the correct file loading address: ${loading_address_readable}", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 17 17
117 17 $self->{'LOADING_ADDRESS'} = $loading_address;
118 }
119
120 =head2 read_file
121
122 While operating an existing D64::File::PRG object instance, there is no need to create a new one when you simply want to replace it with the contents of another file, that is if you only want to load a new data (however you need to create a new object instance if you want to provide raw data through a scalar reference - this limitation should be patched with the next release of this module). The example follows:
123
124 $prg->read_file('FILE' => $file);
125
126 =cut
127
128 0 ▶sub read_file {
Subroutine read_file: not called CC=6 SCAR=37.4
Pod documented
129 0 my $self = shift;
130 0 my $params = {@_};
131 0 my $file = $params->{'FILE'}; # get data from file
132 0 my $verbose = $self->{'VERBOSE'}; # display diagnostic messages
133 # Verify if file exists:
134 0 ▶ unless (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
135 0 $self->_verbose_message('MESSAGE' => "file \"${file}\" does not exist", 'ERROR' => 1);
136 }
137 0 $self->{'FILENAME'} = $file;
138 # Read data from file:
139 0 ▶ $self->_verbose_message('MESSAGE' => "Opening file \"${file}\" for reading", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
140 0 ▶ open my $fh, '<', $file or $self->_verbose_message('MESSAGE' => "could not open filehandle for \"${file}\" file", 'ERROR' => 1);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or $self->_verbose_message("MESSAGE", qq[could not open filehandle for "$file" file], "ERROR", 1) 0 0 0
141 0 binmode $fh, ':bytes';
142 0 $self->_read_file('FILEHANDLE' => $fh, 'VERBOSE' => $verbose);
143 0 ▶ close $fh or $self->_verbose_message('MESSAGE' => "could not close opened filehandle for \"${file}\" file", 'ERROR' => 1);
Branch: 1 branch
BranchTrueFalseTotal
close $fh or $self->_verbose_message("MESSAGE", qq[could not close opened filehandle for "$file" file], "ERROR", 1) 0 0 0
144 0 ▶ $self->_verbose_message('MESSAGE' => "Closing file \"${file}\" upon successful read", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
145 }
146
147 4 ▶sub _read_file {
Subroutine _read_file: called CC=1 SCAR=0.0
148 4 my ($self, %params) = @_;
149
150 4 my $fh = $params{FILEHANDLE};
151 4 my $verbose = $params{VERBOSE};
152
153 4 $self->_get_loading_address_from_file(FILEHANDLE => $fh, VERBOSE => $verbose);
154 2 $self->_get_raw_contents_from_file(FILEHANDLE => $fh, VERBOSE => $verbose);
155
156 2 return;
157 }
158
159 20 ▶sub _get_raw_contents_from_scalarref {
Subroutine _get_raw_contents_from_scalarref: called CC=8 SCAR=21.5
160 20 my $self = shift;
161 20 my $params = {@_};
162 20 my $raw_data_ref = $params->{'RAW_DATA'}; # externally provided raw data
163 20 my $verbose = $params->{'VERBOSE'}; # display diagnostic messages
164 20 ▶ unless (ref $raw_data_ref eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $raw_data_ref eq "SCALAR") 1 19 20
165 1 ▶ my $raw_data_reftype = ref ($raw_data_ref) ? ( ref ($raw_data_ref) . ' reference' ) : 'SCALAR itself';
Branch: 1 branch
BranchTrueFalseTotal
ref $raw_data_ref ? : 0 1 1
166 1 $self->_verbose_message('MESSAGE' => "raw data has to be a SCALAR reference (but is a ${raw_data_reftype})", 'ERROR' => 1);
167 }
168 19 $self->{'RAW_DATA'} = []; # empty all previously stored raw file contents
169 19 my ($bytes_count, $byte) = (0);
170 19 ▶ $self->_verbose_message('MESSAGE' => "Retrieving raw file contents from a SCALAR reference", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 19 19
171 19 my $raw_data_length = length ${$raw_data_ref};
172 19 ▶ while ($bytes_count < $raw_data_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_count < $raw_data_length) 116 19 135
173 116 my $byte = substr ${$raw_data_ref}, $bytes_count, 1;
174 116 my $byte_value = ord $byte; # get byte numeric value
175 116 ▶ if ($byte_value < 0x00 or $byte_value > 0xff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte_value < 0 or $byte_value > 255) 0 116 116
Condition33%1/3
$byte_value < 0 or $byte_value > 255
l!l&&r!l&&!r
00116
MC/DC0%0/2
$byte_value < 0 or $byte_value > 255
$byte_value < 0 $byte_value > 255
Truth table33%1/3
A or B
A= $byte_value < 0B= $byte_value > 255
ABresult
1X1
011
000
176 0 my $byte_value_readable = sprintf "\$%02x", $byte_value;
177 0 $self->_verbose_message('MESSAGE' => "invalid byte value (${byte_value_readable}) in raw data at offset ${bytes_count}", 'ERROR' => 1);
178 }
179 116 push @{$self->{'RAW_DATA'}}, $byte_value;
180 116 $bytes_count++;
181 }
182 19 ▶ $self->_verbose_message('MESSAGE' => "Received ${bytes_count} bytes of the raw file contents", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 19 19
183 }
184
185 2 ▶sub _get_raw_contents_from_file {
Subroutine _get_raw_contents_from_file: called CC=4 SCAR=13.9
186 2 my $self = shift;
187 2 my $params = {@_};
188 2 my $fh = $params->{'FILEHANDLE'}; # already opened filehandle
189 2 my $verbose = $params->{'VERBOSE'}; # display diagnostic messages
190 2 my $file = $self->{'FILENAME'}; # filename associated with "$fh" filehandle
191 2 my ($bytes_count, $byte) = (0);
192 2 $self->{'RAW_DATA'} = []; # empty all previously stored raw file contents
193 2 ▶ $self->_verbose_message('MESSAGE' => "Retrieving raw file contents from an opened filehandle", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
194 2 ▶ while ( sysread $fh, $byte, 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread $fh, $byte, 1) 9 2 11
195 9 $bytes_count++;
196 9 push @{$self->{'RAW_DATA'}}, ord $byte; # get byte numeric value
197 }
198 2 ▶ $self->_verbose_message('MESSAGE' => "Received ${bytes_count} bytes of the raw file contents", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
199 }
200
201 4 ▶sub _get_loading_address_from_file {
Subroutine _get_loading_address_from_file: called CC=6 SCAR=18.0
202 4 my $self = shift;
203 4 my $params = {@_};
204 4 my $fh = $params->{'FILEHANDLE'}; # already opened filehandle
205 4 my $verbose = $params->{'VERBOSE'}; # display diagnostic messages
206 4 my $file = $self->{'FILENAME'}; # filename associated with "$fh" filehandle
207 4 my ($load_addr_lo, $load_addr_hi, $bytes_count);
208 4 ▶ $self->_verbose_message('MESSAGE' => "Retrieving loading address from an opened filehandle", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 4 4
209 4 $bytes_count = sysread $fh, $load_addr_lo, 1;
210 4 ▶ my $filename = defined $file ? qq{"${file}"} : q{IO::Scalar};
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 0 4 4
211 4 ▶ if ($bytes_count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_count != 1) 1 3 4
212 1 $self->_verbose_message('MESSAGE' => "unexpected end of file while reading loading address from $filename filehandle", 'ERROR' => 1);
213 }
214 3 $bytes_count = sysread $fh, $load_addr_hi, 1;
215 3 ▶ if ($bytes_count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_count != 1) 1 2 3
216 1 $self->_verbose_message('MESSAGE' => "unexpected end of file while reading loading address from $filename filehandle", 'ERROR' => 1);
217 }
218 2 my $loading_address = ord ($load_addr_lo) + 0x100 * ord ($load_addr_hi);
219 2 my $loading_address_readable = uc sprintf "\$%04x", $loading_address;
220 2 ▶ $self->_verbose_message('MESSAGE' => "Received the correct file loading address: ${loading_address_readable}", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
221 2 $self->{'LOADING_ADDRESS'} = $loading_address;
222 }
223
224 =head2 get_data
225
226 All raw data can be accessed through this method. You might explicitly want to request the format of a data retrieved. By default the raw content is collected unless you otherwise specify to get an assembly formatted source code. In both cases a scalar value is returned. In the latter case you are able to provide an additional parameter indicating how many byte values will be returned on a single line (these are 8 bytes by default). Here are a couple of examples:
227
228 my $raw_data = $prg->get_data('FORMAT' => 'RAW', 'LOAD_ADDR_INCL' => 0);
229 my $asm_data = $prg->get_data('FORMAT' => 'ASM', 'LOAD_ADDR_INCL' => 1, 'ROW_LENGTH' => 4);
230
231 There is an additional optional boolean "LOAD_ADDR_INCL", which indicates if a loading address should be included in the output string. For raw contents it defaults to 0, while for assembly source code format it defaults to 1. This is reasonable, as you usually don't want loading address included in a raw data, but it becomes quite useful when compiling a source code.
232
233 =cut
234
235 16 ▶sub get_data {
Subroutine get_data: called CC=13 SCAR=27.1
Pod documented
236 16 my $self = shift;
237 16 my $params = {@_};
238 16 my $verbose = $self->{'VERBOSE'};
239 16 my $format = $params->{'FORMAT'}; # format of data returned from this method (defaults to "RAW")
240 16 ▶ $format = 'RAW' unless defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 4 12 16
241 # LOAD_ADDR_INCL: a boolean indicating if loading address should be included to output
242 # It defaults to 0 for RAW format, and to 1 for ASM format
243 16 my $loading_address_included = $params->{'LOAD_ADDR_INCL'};
244 16 ▶ if ($format eq 'RAW') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'RAW') { } 14 2 16
elsif ($format eq 'ASM') { } 2 0 2
245 14 ▶ $self->_verbose_message('MESSAGE' => "Getting raw data contents into a scalar value", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 14 14
246 14 my $data = ''; # prepare scalar value with the whole RAW contents
247 14 ▶ $self->_add_loading_address_to_scalarref('RAW_DATA' => \$data, 'VERBOSE' => $verbose) if $loading_address_included;
Branch: 1 branch
BranchTrueFalseTotal
if $loading_address_included 9 5 14
248 14 $self->_add_raw_data_to_scalarref('RAW_DATA' => \$data, 'VERBOSE' => $verbose);
249 14 return $data;
250 } elsif ($format eq 'ASM') {
251 2 ▶ $loading_address_included = 1 unless defined $loading_address_included;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $loading_address_included 2 0 2
252 2 ▶ $self->_verbose_message('MESSAGE' => "Getting raw data composed as an assembly source code", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
253 2 ▶ my $row_length = $params->{'ROW_LENGTH'} || 8;
Condition100%2/2
$params->{'ROW_LENGTH'} || 8
l!l
11
MC/DC100%1/1
$params->{'ROW_LENGTH'} || 8
$params->{'ROW_LENGTH'}
Truth table100%2/2
A
A= $params->{'ROW_LENGTH'}
Aresult
11
00
254 2 ▶ if ($row_length !~ m/^\d+$/ or $row_length < 0x01 or $row_length > 0xff) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $row_length =~ /^\d+$/ or $row_length < 1 or $row_length > 255) 0 2 2
Condition33%2/6
not $row_length =~ /^\d+$/ or $row_length < 1 or $row_length > 255
l!l&&r!l&&!r
002
not $row_length =~ /^\d+$/ or $row_length < 1
l!l&&r!l&&!r
002
MC/DC0%0/3
not $row_length =~ /^\d+$/ or $row_length < 1 or $row_length > 255
not $row_length =~ /^\d+$/ $row_length < 1 $row_length > 255
Truth table25%1/4
not(A) or B or C
A= not $row_length =~ /^\d+$/B= $row_length < 1C= $row_length > 255
ABCresult
1XX1
01X1
0011
0000
255 0 $self->_verbose_message('MESSAGE' => "invalid row length (\"${row_length}\") request for assembly raw data composition", 'ERROR' => 1);
256 }
257 2 my $data = ''; # prepare scalar value with the whole ASSEMBLY contents
258 2 ▶ if ($loading_address_included) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loading_address_included) 2 0 2
259 2 $self->_compose_comment_line_to_scalarref('RAW_DATA' => \$data, 'ROW_LENGTH' => $row_length, 'VERBOSE' => $verbose);
260 2 $self->_compose_loading_address_to_scalarref('RAW_DATA' => \$data, 'VERBOSE' => $verbose);
261 }
262 2 $self->_compose_comment_line_to_scalarref('RAW_DATA' => \$data, 'ROW_LENGTH' => $row_length, 'VERBOSE' => $verbose);
263 2 $self->_compose_raw_data_to_scalarref('RAW_DATA' => \$data, 'ROW_LENGTH' => $row_length, 'VERBOSE' => $verbose);
264 2 $self->_compose_comment_line_to_scalarref('RAW_DATA' => \$data, 'ROW_LENGTH' => $row_length, 'VERBOSE' => $verbose);
265 2 return $data;
266 }
267 else {
268 0 $self->_verbose_message('MESSAGE' => "unrecognized data format (\"${format}\")", 'ERROR' => 1);
269 }
270 }
271
272 6 ▶sub _compose_comment_line_to_scalarref {
Subroutine _compose_comment_line_to_scalarref: called CC=1 SCAR=0.0
273 6 my $self = shift;
274 6 my $params = {@_};
275 6 my $data_ref = $params->{'RAW_DATA'}; # scalar value with the whole RAW contents
276 6 my $row_length = $params->{'ROW_LENGTH'}; # number of byte values per single line
277 6 my $verbose = $params->{'VERBOSE'}; # display diagnostic messages
278 6 ${$data_ref} .= ';' . '-' x (19 + 5 * $row_length) . "\n";
279 }
280
281 2 ▶sub _compose_raw_data_to_scalarref {
Subroutine _compose_raw_data_to_scalarref: called CC=5 SCAR=16.1
282 2 my $self = shift;
283 2 my $params = {@_};
284 2 my $data_ref = $params->{'RAW_DATA'}; # scalar value with the whole RAW contents
285 2 my $row_length = $params->{'ROW_LENGTH'}; # number of byte values per single line
286 2 my $verbose = $params->{'VERBOSE'}; # display diagnostic messages
287 2 my $line;
288 2 my $offset = 0;
289 2 foreach my $byte (@{$self->{'RAW_DATA'}}) {
290 6 ▶ if ($offset % $row_length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset % $row_length == 0) 3 3 6
291 3 ▶ if ($offset != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset != 0) 1 2 3
292 1 $line =~ s/, $//;
293 1 ${$data_ref} .= "${line}\n";
294 }
295 3 $line = ' ' x 16 . '.byte ';
296 }
297 6 my $byte_value = sprintf "\$%02x, ", $byte;
298 6 $line .= $byte_value;
299 6 $offset++;
300 }
301 2 ▶ ${$data_ref} .= "${line}\n" if $line =~ m/, $/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /, $/i 2 0 2
302 2 ${$data_ref} =~ s/, $//;
303 }
304
305 14 ▶sub _add_raw_data_to_scalarref {
Subroutine _add_raw_data_to_scalarref: called CC=2 SCAR=6.9
306 14 my $self = shift;
307 14 my $params = {@_};
308 14 my $data_ref = $params->{'RAW_DATA'}; # scalar value with the whole RAW contents
309 14 my $verbose = $params->{'VERBOSE'}; # display diagnostic messages
310 14 foreach my $byte (@{$self->{'RAW_DATA'}}) {
311 79 my $raw_byte = chr $byte;
312 79 ${$data_ref} .= $raw_byte;
313 }
314 }
315
316 2 ▶sub _compose_loading_address_to_scalarref {
Subroutine _compose_loading_address_to_scalarref: called CC=1 SCAR=0.0
317 2 my $self = shift;
318 2 my $params = {@_};
319 2 my $data_ref = $params->{'RAW_DATA'}; # scalar value with the whole RAW contents
320 2 my $verbose = $params->{'VERBOSE'}; # display diagnostic messages
321 2 my $loading_address = sprintf "%04x", hex $self->{'LOADING_ADDRESS'};
322 2 ${$data_ref} .= ' ' x 16;
323 2 ${$data_ref} .= sprintf "*= \$%04x\n", $loading_address;
324 }
325
326 9 ▶sub _add_loading_address_to_scalarref {
Subroutine _add_loading_address_to_scalarref: called CC=1 SCAR=0.0
327 9 my $self = shift;
328 9 my $params = {@_};
329 9 my $data_ref = $params->{'RAW_DATA'}; # scalar value with the whole RAW contents
330 9 my $verbose = $params->{'VERBOSE'}; # display diagnostic messages
331 9 my $loading_address = sprintf "%04x", $self->{'LOADING_ADDRESS'};
332 9 my ($addr_hi, $addr_lo) = ( $loading_address =~ m/([0-9a-f]{2})/ig );
333 9 ${$data_ref} .= chr (hex $addr_lo);
334 9 ${$data_ref} .= chr (hex $addr_hi);
335 }
336
337 =head2 change_loading_address
338
339 You can modify original file loading address by performing the following operation:
340
341 $prg->change_loading_address('LOADING_ADDRESS' => 0x6400);
342
343 =cut
344
345 4 ▶sub change_loading_address {
Subroutine change_loading_address: called CC=3 SCAR=11.0
Pod documented
346 4 my $self = shift;
347 4 my $params = {@_};
348 4 my $loading_address = $params->{'LOADING_ADDRESS'}; # new loading address
349 4 my $verbose = $self->{'VERBOSE'}; # display diagnostic messages
350 # Verify if provided loading address is correct:
351 4 ▶ unless (defined $loading_address) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $loading_address) 1 3 4
352 1 $self->_verbose_message('MESSAGE' => "an undefined loading address has been provided to the method that was supposed to change its value", 'ERROR' => 1);
353 }
354 # Update loading address if correct value provided:
355 3 $self->_get_loading_address_from_scalar('LOADING_ADDRESS' => $loading_address, 'VERBOSE' => $verbose);
356 2 ▶ $self->_verbose_message('MESSAGE' => "File loading address has been succesfully updated", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
357 }
358
359 =head2 set_data
360
361 You can update raw program data and its loading address by performing the following operation:
362
363 $prg->set_data('RAW_DATA' => \$data, 'LOADING_ADDRESS' => 0x1000);
364
365 You can update raw program data without modifying its loading address by performing the following operation:
366
367 $prg->set_data('RAW_DATA' => \$data);
368
369 =cut
370
371 5 ▶sub set_data {
Subroutine set_data: called CC=2 SCAR=6.9
Pod documented
372 5 my ($self, %params) = @_;
373
374 5 $self->_get_raw_contents_from_scalarref(%params);
375 4 ▶ $self->change_loading_address(%params) if exists $params{LOADING_ADDRESS};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"LOADING_ADDRESS"} 3 1 4
376
377 2 return;
378 }
379
380 =head2 set_file_data
381
382 You can replace original program data assuming that its loading address is included within the first two bytes of provided file data by performing the following operation:
383
384 $prg->set_file_data('FILE_DATA' => \$file_data);
385
386 =cut
387
388 4 ▶sub set_file_data {
Subroutine set_file_data: called CC=1 SCAR=0.0
Pod documented
389 4 my ($self, %params) = @_;
390
391 4 my $file_data = $params{FILE_DATA};
392 4 my $verbose = $params{VERBOSE};
393
394 4 my $fh = new IO::Scalar $file_data;
395 4 $self->_read_file(FILEHANDLE => $fh, VERBOSE => $verbose);
396 2 $fh->close;
397
398 2 return;
399 }
400
401 =head2 write_file
402
403 There is a command allowing you to save the whole contents into a disk file:
404
405 $prg->write_file('FILE' => $file, 'OVERWRITE' => 1);
406
407 Note that when you specify any value evaluating to true for 'OVERWRITE' parameter, any existing file will be replaced (overwriting is disabled by default).
408
409 =cut
410
411 0 ▶sub write_file {
Subroutine write_file: not called CC=8 SCAR=42.8
Pod documented
412 0 my $self = shift;
413 0 my $params = {@_};
414 0 my $file = $params->{'FILE'}; # write data to file
415 0 my $overwrite = $params->{'OVERWRITE'}; # a boolean indicating if any existing file should be overwritten (no files will be overwritten by default)
416 0 my $verbose = $self->{'VERBOSE'}; # display diagnostic messages
417 0 $self->{'TARGET_FILENAME'} = $file;
418 # Write data to file:
419 0 ▶ $self->_verbose_message('MESSAGE' => "Opening file \"${file}\" for writing", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
420 # Verify if file exists:
421 0 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
422 0 ▶ unless ($overwrite) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overwrite) { } 0 0 0
423 0 $self->_verbose_message('MESSAGE' => "file \"${file}\" already exists", 'ERROR' => 1);
424 }
425 else {
426 0 ▶ $self->_verbose_message('MESSAGE' => "File exists (overwriting the existing content)", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
427 }
428 }
429 0 ▶ open my $fh, '>', $file or $self->_verbose_message('MESSAGE' => "could not open filehandle for \"${file}\" file", 'ERROR' => 1);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or $self->_verbose_message("MESSAGE", qq[could not open filehandle for "$file" file], "ERROR", 1) 0 0 0
430 0 binmode $fh, ':bytes';
431 0 $self->_write_loading_address_to_file('FILEHANDLE' => $fh, 'VERBOSE' => $verbose);
432 0 $self->_write_raw_contents_to_file('FILEHANDLE' => $fh, 'VERBOSE' => $verbose);
433 0 ▶ close $fh or $self->_verbose_message('MESSAGE' => "could not close opened filehandle for \"${file}\" file", 'ERROR' => 1);
Branch: 1 branch
BranchTrueFalseTotal
close $fh or $self->_verbose_message("MESSAGE", qq[could not close opened filehandle for "$file" file], "ERROR", 1) 0 0 0
434 0 ▶ $self->_verbose_message('MESSAGE' => "Closing file \"${file}\" upon successful write", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
435 }
436
437 0 ▶sub _write_raw_contents_to_file {
Subroutine _write_raw_contents_to_file: not called CC=5 SCAR=34.0
438 0 my $self = shift;
439 0 my $params = {@_};
440 0 my $fh = $params->{'FILEHANDLE'}; # already opened filehandle
441 0 my $verbose = $params->{'VERBOSE'}; # display diagnostic messages
442 0 my $file = $self->{'TARGET_FILENAME'}; # filename associated with "$fh" filehandle
443 0 my ($bytes_count, $byte) = (0);
444 0 ▶ $self->_verbose_message('MESSAGE' => "Writing raw file contents into an opened filehandle", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
445 0 foreach my $byte (@{$self->{'RAW_DATA'}}) {
446 0 my $bytes_written = syswrite $fh, chr (hex $byte), 1;
447 0 ▶ if ($bytes_written != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_written != 1) 0 0 0
448 0 $self->_verbose_message('MESSAGE' => "unexpected difficulties writing raw file contents to \"${file}\" filehandle at offset ${bytes_count} ($!)", 'ERROR' => 1);
449 }
450 0 $bytes_count++;
451 }
452 0 ▶ $self->_verbose_message('MESSAGE' => "Written ${bytes_count} bytes of the raw file contents", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
453 }
454
455 0 ▶sub _write_loading_address_to_file {
Subroutine _write_loading_address_to_file: not called CC=5 SCAR=34.0
456 0 my $self = shift;
457 0 my $params = {@_};
458 0 my $fh = $params->{'FILEHANDLE'}; # already opened filehandle
459 0 my $verbose = $params->{'VERBOSE'}; # display diagnostic messages
460 0 my $file = $self->{'TARGET_FILENAME'}; # filename associated with "$fh" filehandle
461 0 my $loading_address = sprintf "%04x", $self->{'LOADING_ADDRESS'};
462 0 my ($addr_hi, $addr_lo) = ( $loading_address =~ m/([0-9a-f]{2})/ig );
463 0 my $bytes_count;
464 0 ▶ $self->_verbose_message('MESSAGE' => "Writing loading address into an opened filehandle", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
465 0 $bytes_count = syswrite $fh, chr (hex $addr_lo), 1;
466 0 ▶ if ($bytes_count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_count != 1) 0 0 0
467 0 $self->_verbose_message('MESSAGE' => "unexpected difficulties writing loading address to \"${file}\" filehandle ($!)", 'ERROR' => 1);
468 }
469 0 $bytes_count = syswrite $fh, chr (hex $addr_hi), 1;
470 0 ▶ if ($bytes_count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_count != 1) 0 0 0
471 0 $self->_verbose_message('MESSAGE' => "unexpected difficulties writing loading address to \"${file}\" filehandle ($!)", 'ERROR' => 1);
472 }
473 0 my $loading_address_readable = uc sprintf "\$%04x", $self->{'LOADING_ADDRESS'};
474 0 ▶ $self->_verbose_message('MESSAGE' => "Written the following file loading address: ${loading_address_readable}", 'ERROR' => 0) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
475 }
476
477 5 ▶sub _verbose_message {
Subroutine _verbose_message: called CC=3 SCAR=11.3
478 5 my $self = shift;
479 5 my $params = {@_};
480 5 my $message = $params->{'MESSAGE'};
481 5 my $error = $params->{'ERROR'};
482 5 my ($package, $line, $subroutine) = (caller(1))[0,2,3];
483 5 ▶ ($package, $line, $subroutine) = (caller(0))[0,2,3] if $package eq 'main';
Branch: 1 branch
BranchTrueFalseTotal
if $package eq "main" 0 5 5
484 5 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 5 0 5
485 5 croak "[${package}][ERROR] ${subroutine} subroutine error at line ${line}: ${message}";
486 }
487 else {
488 0 print "[${package}][INFO] ${message}\n";
489 }
490 }
491
492 =head1 EXAMPLES
493
494 Retrieving raw data as an assembly formatted source code can be expressed using the following few lines of Perl code:
495
496 use D64::File::PRG;
497 my $data = join ('', map {chr} (1,2,3,4,5));
498 my $prg = D64::File::PRG->new('RAW_DATA' => \$data, 'LOADING_ADDRESS' => 0x0801);
499 my $src = $prg->get_data('FORMAT' => 'ASM', 'ROW_LENGTH' => 4);
500 print $src;
501
502 When executed, it prints out the source code that is ready for compilation:
503
504 ;---------------------------------------
505 *= $0801
506 ;---------------------------------------
507 .byte $01, $02, $03, $04
508 .byte $05
509 ;---------------------------------------
510
511 =head1 BUGS
512
513 There are no known bugs at the moment. Please report any bugs or feature requests.
514
515 =head1 EXPORT
516
517 None. No method is exported into the caller's namespace either by default or explicitly.
518
519 =head1 SEE ALSO
520
521 I am working on the set of modules providing an easy way to access and manipulate the contents of D64 disk images and T64 tape images. D64::File::PRG is the first module of this set, as it provides operations necessary for handling individual C64's PRG files, which are the smallest building blocks for those images. Upon completion I am going to successively upload all my new modules into the CPAN.
522
523 =head1 AUTHOR
524
525 Pawel Krol, E<lt>pawelkrol@cpan.orgE<gt>.
526
527 =head1 VERSION
528
529 Version 0.03 (2013-01-19)
530
531 =head1 COPYRIGHT AND LICENSE
532
533 Copyright (C) 2010, 2013 by Pawel Krol.
534
535 This library is free open source software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.
536
537 PLEASE NOTE THAT IT COMES WITHOUT A WARRANTY OF ANY KIND!
538
539 =cut
540
541 2 1;
↑ Summary