blib/lib/ZX81/Tape.pm

filter: Statementstmt 97.5% 668 / 685 Branchbran 90.7% 256 / 282 Conditioncond 79.4% 54 / 68 MC/DCmcdc 62.5% 25 / 40 Subroutinesub 100% 33 / 33 Podpod 18.7% 3 / 16 totaltotal 92.4% 1039 / 1124 CC 193 SCAR 28.7
CC
193
Cov
94%
list_p
32.6
parse_file
27.0
parse_var
25.6
CRAP 17.6
↑ Summary bin/zx81_make_p »
1 package ZX81::Tape;
2  
3 106 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 106 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 106 ▶use ZX81::Tokenize qw( tokenize_basic encode_chars encode_float );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 106 our $VERSION = '0.004';
8  
9 106 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 106 our @EXPORT_OK = qw(
12 make_p
13 dump_p
14 list_p
15 );
16  
17 #------------------------------------------------------------------------------
18 # Memory area
19 #------------------------------------------------------------------------------
20 {
21  
22 package ZX81::Tape::Memory;
23 106 ▶ use Object::Tiny::RW qw( start data );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 180 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
26 180 my ( $class, $start, @data ) = @_;
27 180 return $class->SUPER::new( start => $start, data => \@data );
28 }
29  
30 228863 ▶ sub peek {
Subroutine peek: called CC=3 SCAR=12.3
31 228863 my ( $self, $addr ) = @_;
32 228863 my $p = $addr - $self->start;
33 228863 ▶ if ( $p < 0 || $p >= @{ $self->data } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < 0 or $p >= @{$self->data;}) 0 228863 228863
Condition33%1/3
$p < 0 or $p >= @{$self->data;}
l!l&&r!l&&!r
00228863
MC/DC0%0/2
$p < 0 or $p >= @{$self->data;}
$p < 0 $p >= @{$self->data;}
Truth table33%1/3
A or B
A= $p < 0B= $p >= @{$self->data;}
ABresult
1X1
011
000
34 0 return 0;
35 }
36 228863 return $self->data->[$p];
37 }
38  
39 2869 ▶ sub dpeek {
Subroutine dpeek: called CC=1 SCAR=0.0
40 2869 my ( $self, $addr ) = @_;
41 2869 return $self->peek($addr) | ( $self->peek( $addr + 1 ) << 8 );
42 }
43  
44 522 ▶ sub dpeek_be {
Subroutine dpeek_be: called CC=1 SCAR=0.0
45 522 my ( $self, $addr ) = @_;
46 522 return ( $self->peek($addr) << 8 ) | $self->peek( $addr + 1 );
47 }
48 }
49  
50 #------------------------------------------------------------------------------
51 # zx81 system variables and character mappings
52 #------------------------------------------------------------------------------
53 106 my %sysvars = (
54 ERR_NO => 0x4000,
55 FLAGS => 0x4001,
56 ERR_SP => 0x4002,
57 RAMTOP => 0x4004,
58 MODE => 0x4006,
59 PPC => 0x4007,
60 VERSN => 0x4009,
61 E_PPC => 0x400a,
62 D_FILE => 0x400c,
63 DF_CC => 0x400e,
64 VARS => 0x4010,
65 DEST => 0x4012,
66 E_LINE => 0x4014,
67 CH_ADD => 0x4016,
68 X_PTR => 0x4018,
69 STKBOT => 0x401a,
70 STKEND => 0x401c,
71 BREG => 0x401e,
72 MEM => 0x401f,
73 FREE1 => 0x4021,
74 DF_SZ => 0x4022,
75 S_TOP => 0x4023,
76 LAST_K => 0x4025,
77 DEBOUNCE => 0x4027,
78 MARGIN => 0x4028,
79 NXTLIN => 0x4029,
80 OLDPPC => 0x402b,
81 FLAGX => 0x402d,
82 STRLEN => 0x402e,
83 T_ADDR => 0x4030,
84 SEED => 0x4032,
85 FRAMES => 0x4034,
86 COORDS_X => 0x4036,
87 COORDS_Y => 0x4037,
88 PR_CC => 0x4038,
89 S_POSN_COL => 0x4039,
90 S_POSN_ROW => 0x403a,
91 CDFLAG => 0x403b,
92 PRBUFF => 0x403c,
93 MEMBOT => 0x405d,
94 FREE2 => 0x407b,
95 PROG => 0x407d,
96 );
97  
98 106 my %zx81chars = (
99 0x00 => ' ',
100 0x0b => '"',
101 0x0c => '#',
102 0x0d => '$',
103 0x0e => ':',
104 0x0f => '?',
105 0x10 => '(',
106 0x11 => ')',
107 0x12 => '>',
108 0x13 => '<',
109 0x14 => '=',
110 0x15 => '+',
111 0x16 => '-',
112 0x17 => '*',
113 0x18 => '/',
114 0x19 => ';',
115 0x1a => ',',
116 0x1b => '.',
117 0x1c => '0',
118 0x1d => '1',
119 0x1e => '2',
120 0x1f => '3',
121 0x20 => '4',
122 0x21 => '5',
123 0x22 => '6',
124 0x23 => '7',
125 0x24 => '8',
126 0x25 => '9',
127 0x26 => 'A',
128 0x27 => 'B',
129 0x28 => 'C',
130 0x29 => 'D',
131 0x2a => 'E',
132 0x2b => 'F',
133 0x2c => 'G',
134 0x2d => 'H',
135 0x2e => 'I',
136 0x2f => 'J',
137 0x30 => 'K',
138 0x31 => 'L',
139 0x32 => 'M',
140 0x33 => 'N',
141 0x34 => 'O',
142 0x35 => 'P',
143 0x36 => 'Q',
144 0x37 => 'R',
145 0x38 => 'S',
146 0x39 => 'T',
147 0x3a => 'U',
148 0x3b => 'V',
149 0x3c => 'W',
150 0x3d => 'X',
151 0x3e => 'Y',
152 0x3f => 'Z',
153 0x40 => 'RND',
154 0x41 => 'INKEY$',
155 0x42 => 'PI',
156 0x80 => '% ',
157 0x8b => '%"',
158 0x8c => '%#',
159 0x8d => '%$',
160 0x8e => '%:',
161 0x8f => '%?',
162 0x90 => '%(',
163 0x91 => '%)',
164 0x92 => '%>',
165 0x93 => '%<',
166 0x94 => '%=',
167 0x95 => '%+',
168 0x96 => '%-',
169 0x97 => '%*',
170 0x98 => '%/',
171 0x99 => '%;',
172 0x9a => '%,',
173 0x9b => '%.',
174 0x9c => '%0',
175 0x9d => '%1',
176 0x9e => '%2',
177 0x9f => '%3',
178 0xa0 => '%4',
179 0xa1 => '%5',
180 0xa2 => '%6',
181 0xa3 => '%7',
182 0xa4 => '%8',
183 0xa5 => '%9',
184 0xa6 => '%A',
185 0xa7 => '%B',
186 0xa8 => '%C',
187 0xa9 => '%D',
188 0xaa => '%E',
189 0xab => '%F',
190 0xac => '%G',
191 0xad => '%H',
192 0xae => '%I',
193 0xaf => '%J',
194 0xb0 => '%K',
195 0xb1 => '%L',
196 0xb2 => '%M',
197 0xb3 => '%N',
198 0xb4 => '%O',
199 0xb5 => '%P',
200 0xb6 => '%Q',
201 0xb7 => '%R',
202 0xb8 => '%S',
203 0xb9 => '%T',
204 0xba => '%U',
205 0xbb => '%V',
206 0xbc => '%W',
207 0xbd => '%X',
208 0xbe => '%Y',
209 0xbf => '%Z',
210 0xc0 => '\\"',
211 0xc1 => 'AT',
212 0xc2 => 'TAB',
213 0xc4 => 'CODE',
214 0xc5 => 'VAL',
215 0xc6 => 'LEN',
216 0xc7 => 'SIN',
217 0xc8 => 'COS',
218 0xc9 => 'TAN',
219 0xca => 'ASN',
220 0xcb => 'ACS',
221 0xcc => 'ATN',
222 0xcd => 'LN',
223 0xce => 'EXP',
224 0xcf => 'INT',
225 0xd0 => 'SQR',
226 0xd1 => 'SGN',
227 0xd2 => 'ABS',
228 0xd3 => 'PEEK',
229 0xd4 => 'USR',
230 0xd5 => 'STR$',
231 0xd6 => 'CHR$',
232 0xd7 => 'NOT',
233 0xd8 => '**',
234 0xd9 => 'OR',
235 0xda => 'AND',
236 0xdb => '<=',
237 0xdc => '>=',
238 0xdd => '<>',
239 0xde => 'THEN',
240 0xdf => 'TO',
241 0xe0 => 'STEP',
242 0xe1 => 'LPRINT',
243 0xe2 => 'LLIST',
244 0xe3 => 'STOP',
245 0xe4 => 'SLOW',
246 0xe5 => 'FAST',
247 0xe6 => 'NEW',
248 0xe7 => 'SCROLL',
249 0xe8 => 'CONT',
250 0xe9 => 'DIM',
251 0xea => 'REM',
252 0xeb => 'FOR',
253 0xec => 'GOTO',
254 0xed => 'GOSUB',
255 0xee => 'INPUT',
256 0xef => 'LOAD',
257 0xf0 => 'LIST',
258 0xf1 => 'LET',
259 0xf2 => 'PAUSE',
260 0xf3 => 'NEXT',
261 0xf4 => 'POKE',
262 0xf5 => 'PRINT',
263 0xf6 => 'PLOT',
264 0xf7 => 'RUN',
265 0xf8 => 'SAVE',
266 0xf9 => 'RAND',
267 0xfa => 'IF',
268 0xfb => 'CLS',
269 0xfc => 'UNPLOT',
270 0xfd => 'CLEAR',
271 0xfe => 'RETURN',
272 0xff => 'COPY',
273 );
274  
275 # bytes is a list of 5 bytes, representing a ZX81 floating point number
276 605 ▶sub zx81_to_float {
Subroutine zx81_to_float: called CC=7 SCAR=20.1
Pod undocumented
277 605 my (@bytes) = @_;
278  
279 # Zero special case
280 605 ▶ if ( $bytes[0] == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes[0] == 0 and $bytes[1] == 0 and $bytes[2] == 0 and $bytes[3] == 0 and $bytes[4] == 0) 442 163 605
Condition66%8/12
$bytes[0] == 0 and $bytes[1] == 0 and $bytes[2] == 0 and $bytes[3] == 0 and $bytes[4] == 0
!ll&&!rl&&r
1630442
$bytes[0] == 0 and $bytes[1] == 0 and $bytes[2] == 0 and $bytes[3] == 0
!ll&&!rl&&r
1630442
$bytes[0] == 0 and $bytes[1] == 0 and $bytes[2] == 0
!ll&&!rl&&r
1630442
$bytes[0] == 0 and $bytes[1] == 0
!ll&&!rl&&r
1630442
MC/DC20%1/5
$bytes[0] == 0 and $bytes[1] == 0 and $bytes[2] == 0 and $bytes[3] == 0 and $bytes[4] == 0
$bytes[0] == 0 $bytes[1] == 0 $bytes[2] == 0 $bytes[3] == 0 $bytes[4] == 0
Truth table33%2/6
A and B and C and D and E
A= $bytes[0] == 0B= $bytes[1] == 0C= $bytes[2] == 0D= $bytes[3] == 0E= $bytes[4] == 0
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
281 && $bytes[1] == 0
282 && $bytes[2] == 0
283 && $bytes[3] == 0
284 && $bytes[4] == 0 )
285 {
286  
287 442 return 0.0;
288 }
289  
290 163 my $exp = $bytes[0] - 128;
291 163 ▶ my $sign = ( $bytes[1] & 0x80 ) ? -1.0 : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
$bytes[1] & 128 ? : 0 163 163
292  
293 163 my $mant =
294 ( ( $bytes[1] | 0x80 ) << 24 ) | ( $bytes[2] << 16 ) |
295 ( $bytes[3] << 8 ) | ( $bytes[4] );
296  
297 163 my $value = $sign * $mant * ( 2**( $exp - 32 ) );
298 163 return $value;
299 }
300  
301 #------------------------------------------------------------------------------
302 # make_p driver
303 #------------------------------------------------------------------------------
304 118 ▶sub make_p {
Subroutine make_p: called CC=1 SCAR=0.0
Pod documented
305 118 my ( $basic_filename, $p_filename ) = @_;
306  
307 118 my $tape = ZX81::Tape->new();
308 118 $tape->parse_file($basic_filename);
309 114 $tape->compile();
310 112 my $bytes = $tape->as_bytes();
311 112 path($p_filename)->spew_raw($bytes);
312 112 return;
313 }
314  
315 #------------------------------------------------------------------------------
316 # dump_p
317 #------------------------------------------------------------------------------
318 73 ▶sub dump_p {
Subroutine dump_p: called CC=14 SCAR=26.4
Pod documented
319 73 my ( $p_filename, $dump_filename ) = @_;
320 73 my $dump_fh;
321 73 ▶ if ( defined $dump_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dump_filename) { } 73 0 73
322 73 ▶ open( $dump_fh, ">", $dump_filename )
Branch: 1 branch
BranchTrueFalseTotal
open $dump_fh, ">", $dump_filename or die "error: open ${dump_filename}: $!\n" 73 0 73
323 or die "error: open $dump_filename: $!\n";
324 }
325 else {
326 0 $dump_fh = *STDOUT;
327 }
328  
329 # load memory
330 73 my @bytes = ( map { ord } split //, path($p_filename)->slurp_raw );
331 73 my $mem = ZX81::Tape::Memory->new( 0x4009, @bytes );
332  
333 # memory access helpers
334 65003 ▶ my $peek_text = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
335 65003 my ( $addr, $size ) = @_;
336 65003 my $text = "";
337 65003 ▶ for ( my $i = 0 ; $i < $size ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 66349 65003 131352
338 66349 my $byte = $mem->peek( $addr + $i );
339 66349 ▶ if ( $byte == 0x7E && $size > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($byte == 126 and $size > 1) { } 116 66233 66349
elsif (exists $zx81chars{$byte}) { } 63548 2685 66233
Condition100%3/3
$byte == 126 and $size > 1
!ll&&!rl&&r
66117116116
MC/DC100%2/2
$byte == 126 and $size > 1
$byte == 126 $size > 1
Truth table100%3/3
A and B
A= $byte == 126B= $size > 1
ABresult
0X0
100
111
340  
341 # floating point number
342 116 my @bytes;
343 116 ▶ for ( my $j = 1 ; $j < 6 ; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 6) 580 116 696
344 580 push @bytes, $mem->peek( $addr + $i + $j );
345 }
346 116 my $float = zx81_to_float(@bytes);
347 116 $text .= "{$float}";
348  
349 116 $i += 6 - 1;
350 }
351 elsif ( exists $zx81chars{$byte} ) {
352 63548 my $code = $zx81chars{$byte};
353 63548 ▶ if ( $code =~ /\w\w$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\w\w$/u) 509 63039 63548
354 509 $code .= " ";
355 }
356 63548 $text .= $code;
357 }
358 else {
359 2685 $text .= sprintf( "\\%02X", $byte );
360 }
361 }
362 65003 return $text;
363 73 };
364 873 ▶ my $dump = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
365 873 my ( $addr, $size, $text ) = @_;
366  
367 873 printf $dump_fh "%04X %s\n", $addr, $text;
368 873 ▶ for ( my $i = 0 ; $i < $size ; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 10044 873 10917
369 10044 my $bytes_dump = "";
370 10044 my $text_dump = "";
371 10044 my $num_dumpped = 0;
372 10044 ▶ for ( my $j = 0 ; $j < 8 && $i + $j < $size ; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 8 and $i + $j < $size) 64800 7943 72743
Condition100%3/3
$j < 8 and $i + $j < $size
!ll&&!rl&&r
742352064800
MC/DC100%2/2
$j < 8 and $i + $j < $size
$j < 8 $i + $j < $size
Truth table100%3/3
A and B
A= $j < 8B= $i + $j < $size
ABresult
0X0
100
111
373 64800 my $byte = $mem->peek( $addr + $i + $j );
374 64800 $num_dumpped++;
375 64800 $bytes_dump .= sprintf( "%02X ", $byte );
376 64800 $text_dump .= $peek_text->( $addr + $i + $j, 1 ) . " ";
377 64800 ▶ last if $byte == 0x76;
Branch: 1 branch
BranchTrueFalseTotal
if $byte == 118 2101 62699 64800
378 }
379 10044 printf $dump_fh "%04X %-26s %s\n",
380 $addr + $i, $bytes_dump, $text_dump;
381 10044 $i += $num_dumpped;
382 }
383 73 };
384  
385 73 my $prog = $sysvars{PROG};
386 73 my $d_file = $mem->dpeek( $sysvars{D_FILE} );
387 73 my $vars = $mem->dpeek( $sysvars{VARS} );
388 73 my $e_line = $mem->dpeek( $sysvars{E_LINE} );
389  
390 # show system variables
391 73 print $dump_fh "System Variables:\n";
392 73 my %rev_sysvars;
393 73 ▶ while ( my ( $name, $addr ) = each %sysvars ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %sysvars) 3066 73 3139
394 3066 $rev_sysvars{$addr} = $name;
395 }
396 73 ▶ for ( my $addr = $sysvars{VERSN} ; $addr < $prog ; $addr++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr < $prog) 2555 73 2628
397 2555 ▶ if ( $addr == $sysvars{PRBUFF} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($addr == $sysvars{'PRBUFF'}) { } 73 2482 2555
elsif ($addr == $sysvars{'MEMBOT'}) { } 73 2409 2482
elsif (exists $rev_sysvars{$addr + 1}) { } 949 1460 2409
398  
399 # printer buffer
400 73 $dump->( $addr, 33, "PRBUFF" );
401 73 $addr += 33 - 1;
402 }
403 elsif ( $addr == $sysvars{MEMBOT} ) {
404  
405 # memory calculator area
406 73 ▶ for ( my $i = 0 ; $i < 30 / 5 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 6) 438 73 511
407 438 my @bytes;
408 438 for my $j ( 0 .. 4 ) {
409 2190 push @bytes, $mem->peek( $addr + $i * 5 + $j );
410 }
411 438 my $float = zx81_to_float(@bytes);
412 438 $dump->( $addr + 5 * $i, 5, "MEM[$i] = $float" );
413 }
414  
415 73 $addr += 30 - 1;
416 }
417 elsif ( exists $rev_sysvars{ $addr + 1 } ) {
418  
419 # byte variable
420 printf $dump_fh "%04X %02X %s\n", $addr, $mem->peek($addr),
421 949 ▶ $rev_sysvars{$addr} // "";
Condition50%1/2
$rev_sysvars{$addr} // ""
l!l
9490
MC/DC0%0/1
$rev_sysvars{$addr} // ""
$rev_sysvars{$addr}
Truth table50%1/2
A
A= $rev_sysvars{$addr}
Aresult
11
00
422 }
423 else {
424 # word variable
425 printf $dump_fh "%04X %04X %s\n", $addr, $mem->dpeek($addr),
426 1460 ▶ $rev_sysvars{$addr} // "";
Condition50%1/2
$rev_sysvars{$addr} // ""
l!l
14600
MC/DC0%0/1
$rev_sysvars{$addr} // ""
$rev_sysvars{$addr}
Truth table50%1/2
A
A= $rev_sysvars{$addr}
Aresult
11
00
427 1460 $addr++;
428 }
429 }
430  
431 # show BASIC program
432 73 print $dump_fh "\nBASIC Program:\n";
433 73 ▶ for ( my $addr = $prog ; $addr < $d_file ; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr < $d_file) 203 73 276
434 203 my $line_num = $mem->dpeek_be($addr);
435 203 my $size = $mem->dpeek( $addr + 2 );
436 203 $dump->(
437 $addr, 4 + $size,
438 "[$size] $line_num " . $peek_text->( $addr + 4, $size - 1 )
439 );
440 203 print $dump_fh "\n";
441  
442 # move to the next line (2 bytes for line number, 2 bytes for size)
443 203 $addr += $size + 4;
444 }
445  
446 # show D_FILE section
447 73 print $dump_fh "\nD_FILE Section:\n";
448 73 $dump->( $d_file, $vars - $d_file, "D_FILE" );
449  
450 # Show VARS section
451 73 print $dump_fh "\nVARS Section:\n";
452 73 my $addr = $vars;
453 73 ▶ while ( $addr < $e_line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr < $e_line) 86 73 159
454 86 my ( $size, $text ) = decode_var( $mem, $addr );
455 86 $dump->( $addr, $size, $text );
456 86 $addr += $size;
457 }
458 }
459  
460 #------------------------------------------------------------------------------
461 # list basic program
462 #------------------------------------------------------------------------------
463 107 ▶sub list_p {
Subroutine list_p: called CC=32 SCAR=34.8
Pod documented
464 107 my ( $p_filename, $bas_filename ) = @_;
465  
466 107 my $bas_fh;
467 107 ▶ if ( defined $bas_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bas_filename) { } 107 0 107
468 107 ▶ open( $bas_fh, ">", $bas_filename )
Branch: 1 branch
BranchTrueFalseTotal
open $bas_fh, ">", $bas_filename or die "error: open ${bas_filename}: $!\n" 107 0 107
469 or die "error: open $bas_filename: $!\n";
470 }
471 else {
472 0 $bas_fh = *STDOUT;
473 }
474  
475 # load memory
476 107 my @bytes = ( map { ord } split //, path($p_filename)->slurp_raw );
477 107 my $mem = ZX81::Tape::Memory->new( 0x4009, @bytes );
478  
479 # dump pragmas
480 107 my $need_newline = 0;
481  
482 # dump AUTOSTART, AUTOSTART_LINE
483 107 my $d_file = $mem->dpeek( $sysvars{D_FILE} );
484 107 my $nxtlin = $mem->dpeek( $sysvars{NXTLIN} );
485 107 ▶ if ( $nxtlin < $d_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nxtlin < $d_file) 16 91 107
486 16 print $bas_fh "#AUTOSTART=1\n";
487 16 print $bas_fh "#AUTOSTART_LINE=" . $mem->dpeek_be($nxtlin) . "\n";
488 16 $need_newline = 1;
489 }
490  
491 # dump FAST
492 107 ▶ if ( ( $mem->peek( $sysvars{CDFLAG} ) & 0x40 ) == 0x00 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($mem->peek($sysvars{"CDFLAG"}) & 64) == 0) 3 104 107
493 3 print $bas_fh "#FAST=1\n";
494 3 $need_newline = 1;
495 }
496  
497 # dump VARS
498 107 my $vars = $mem->dpeek( $sysvars{VARS} );
499 107 my $e_line = $mem->dpeek( $sysvars{E_LINE} );
500 107 my $addr = $vars;
501 107 ▶ while ( $addr < $e_line - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr < $e_line - 1) 19 107 126
502 19 my ( $size, $text ) = decode_var( $mem, $addr );
503 19 print $bas_fh "#VARS $text\n";
504 19 $addr += $size;
505 19 $need_newline = 1;
506 }
507  
508 # dump DFILE, DFILE_COLLAPSED
509 107 my $dfile_collapsed = 0;
510 107 ▶ if ( $vars - $d_file < 1 + 33 * 24 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars - $d_file < 793) 2 105 107
511 2 print $bas_fh "#DFILE_COLLAPSED=1\n";
512 2 $dfile_collapsed = 1;
513 2 $need_newline = 1;
514 }
515 107 my @dfile_lines = decode_dfile($mem);
516 107 ▶ if ( !$dfile_collapsed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dfile_collapsed) 105 2 107
517 105 for (@dfile_lines) {
518 2520 s/\s+$//; # trim trailing spaces
519 }
520 105 ▶ while ( @dfile_lines && $dfile_lines[-1] eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dfile_lines and $dfile_lines[-1] eq "") 2472 105 2577
Condition100%3/3
@dfile_lines and $dfile_lines[-1] eq ""
!ll&&!rl&&r
10322472
MC/DC100%2/2
@dfile_lines and $dfile_lines[-1] eq ""
@dfile_lines $dfile_lines[-1] eq ""
Truth table100%3/3
A and B
A= @dfile_linesB= $dfile_lines[-1] eq ""
ABresult
0X0
100
111
521 2472 pop @dfile_lines;
522 }
523 }
524 107 for my $line (@dfile_lines) {
525 72 print $bas_fh "#DFILE=\"$line\"\n";
526 72 $need_newline = 1;
527 }
528  
529 107 ▶ if ($need_newline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_newline) 20 87 107
530 20 print $bas_fh "\n";
531 }
532  
533 # dump BASIC program
534 107 my $prog = $sysvars{PROG};
535 107 ▶ for ( my $addr = $prog ; $addr < $d_file ; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr < $d_file) 303 107 410
536 303 my $line_num = $mem->dpeek_be($addr);
537 303 my $size = $mem->dpeek( $addr + 2 );
538 303 printf $bas_fh "%5d ", $line_num;
539  
540 # rem line with possible binary data
541 303 ▶ if ( $mem->peek( $addr + 4 ) == 0xEA ) { # REM
Branch: 1 branch
BranchTrueFalseTotal
if ($mem->peek($addr + 4) == 234) 42 261 303
542 42 my $has_binary_data = 0;
543 42 ▶ for ( my $i = 1 ; $i < $size - 1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size - 1) 399 24 423
544 399 ▶ if ( ( $mem->peek( $addr + 4 + $i ) & 0x40 ) == 0x40 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($mem->peek($addr + 4 + $i) & 64) == 64) 18 381 399
545 18 $has_binary_data = 1;
546 18 last;
547 }
548 }
549 42 ▶ if ($has_binary_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_binary_data) { } 18 24 42
550 18 print $bas_fh " REM ";
551 18 ▶ for ( my $i = 1 ; $i < $size - 1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size - 1) 54 18 72
552 54 printf $bas_fh "\\%02X", $mem->peek( $addr + 4 + $i );
553 }
554 18 print $bas_fh "\n";
555 }
556 else {
557 24 print $bas_fh " REM ";
558 24 ▶ for ( my $i = 1 ; $i < $size - 1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size - 1) 351 24 375
559 351 my $byte = $mem->peek( $addr + 4 + $i );
560 351 ▶ if ( ( $byte & 0x40 ) == 0 && exists $zx81chars{$byte} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($byte & 64) == 0 and exists $zx81chars{$byte}) { } 351 0 351
Condition33%1/3
($byte & 64) == 0 and exists $zx81chars{$byte}
!ll&&!rl&&r
00351
MC/DC0%0/2
($byte & 64) == 0 and exists $zx81chars{$byte}
($byte & 64) == 0 exists $zx81chars{$byte}
Truth table33%1/3
A and B
A= ($byte & 64) == 0B= exists $zx81chars{$byte}
ABresult
0X0
100
111
561 351 print $bas_fh $zx81chars{$byte};
562 }
563 else {
564 0 printf $bas_fh "\\%02X", $byte;
565 }
566 }
567 24 print $bas_fh "\n";
568 }
569  
570 42 $addr += 4 + $size;
571 42 next;
572 }
573  
574 # BASIC text
575 261 my $in_quote = 0;
576 261 ▶ for ( my $i = 0 ; $i < $size - 1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size - 1) 1871 261 2132
577 1871 my $byte = $mem->peek( $addr + 4 + $i );
578 1871 ▶ if ( $byte == 0x0B ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte == 11) 152 1719 1871
579 152 $in_quote = !$in_quote;
580 }
581  
582 1871 ▶ if ( $byte == 0xC0 ) { # double quote
Branch: 5 branches
BranchTrueFalseTotal
if ($byte == 192) { } 6 1865 1871
elsif ($byte == 126) { } 174 1691 1865
elsif ($in_quote and ($byte & 64) == 64) { } 0 1691 1691
elsif (not $in_quote and $byte == 0) { } 9 1682 1691
elsif (exists $zx81chars{$byte}) { } 1682 0 1682
583 6 print $bas_fh "\\\"";
584 }
585 elsif ( $byte == 0x7E ) { # number
586 174 $i += 5;
587 }
588 ▶ elsif ( $in_quote && ( $byte & 0x40 ) == 0x40 ) {
Condition66%2/3
$in_quote and ($byte & 64) == 64
!ll&&!rl&&r
11245670
MC/DC0%0/2
$in_quote and ($byte & 64) == 64
$in_quote ($byte & 64) == 64
Truth table66%2/3
A and B
A= $in_quoteB= ($byte & 64) == 64
ABresult
0X0
100
111
589 0 printf $bas_fh "\\%02X", $byte;
590 }
591 ▶ elsif ( !$in_quote && $byte == 0 ) {
Condition100%3/3
not $in_quote and $byte == 0
!ll&&!rl&&r
56711159
MC/DC100%2/2
not $in_quote and $byte == 0
not $in_quote $byte == 0
Truth table100%3/3
not(A) and B
A= not $in_quoteB= $byte == 0
ABresult
0X0
100
111
592 9 printf $bas_fh "\\%02X", $byte; # space
593 }
594 elsif ( exists $zx81chars{$byte} ) {
595 1682 my $code = $zx81chars{$byte};
596 1682 ▶ if ( $code =~ /^\w\w/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /^\w\w/u) 309 1373 1682
597 309 $code = " $code ";
598 }
599 1682 printf $bas_fh $code;
600 }
601 else {
602 0 printf $bas_fh "\\%02X", $byte;
603 }
604 }
605 261 print $bas_fh "\n";
606  
607 261 $addr += 4 + $size;
608 261 next;
609 }
610 }
611  
612 #------------------------------------------------------------------------------
613 # decode BASIC display
614 #------------------------------------------------------------------------------
615 107 ▶sub decode_dfile {
Subroutine decode_dfile: called CC=7 SCAR=19.5
Pod undocumented
616 107 my ($mem) = @_;
617 107 my $d_file = $mem->dpeek( $sysvars{D_FILE} );
618 107 my $vars = $mem->dpeek( $sysvars{VARS} );
619  
620 # collect dfile lines
621 107 my @dfile;
622 107 ▶ for ( my $addr = $d_file + 1 ; $addr < $vars ; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr < $vars) 2568 107 2675
623 2568 my $line = "";
624 2568 ▶ for ( my $col = 0 ; $col < 33 ; $col++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < 33) 83643 0 83643
625 83643 my $byte = $mem->peek( $addr++ );
626 83643 ▶ if ( $byte == 0x76 ) { # end of line
Branch: 2 branches
BranchTrueFalseTotal
if ($byte == 118) { } 2568 81075 83643
elsif (exists $zx81chars{$byte}) { } 81075 0 81075
627 2568 last;
628 }
629 elsif ( exists $zx81chars{$byte} ) {
630 81075 $line .= $zx81chars{$byte};
631 }
632 else {
633 0 die "decode_dfile: unexpected byte $byte at $addr";
634 }
635 }
636 2568 push @dfile, $line;
637 }
638  
639 # trim trailing empty lines
640 107 ▶ while ( @dfile && $dfile[-1] eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dfile and $dfile[-1] eq "") 24 107 131
Condition100%3/3
@dfile and $dfile[-1] eq ""
!ll&&!rl&&r
110624
MC/DC100%2/2
@dfile and $dfile[-1] eq ""
@dfile $dfile[-1] eq ""
Truth table100%3/3
A and B
A= @dfileB= $dfile[-1] eq ""
ABresult
0X0
100
111
641 24 pop @dfile;
642 }
643  
644 107 return @dfile;
645 }
646  
647 #------------------------------------------------------------------------------
648 # decode BASIC vars
649 #------------------------------------------------------------------------------
650 105 ▶sub decode_var {
Subroutine decode_var: called CC=23 SCAR=31.4
Pod undocumented
651 105 my ( $mem, $addr ) = @_;
652 105 my $start_addr = $addr;
653  
654 105 my $byte = $mem->peek( $addr++ );
655 105 ▶ if ( $byte == 0x80 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte == 128) 73 32 105
656  
657 # end marker
658 73 return ( 1, "end marker" );
659 }
660  
661 32 ▶ if ( ( $byte & 0xE0 ) == 0x60 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($byte & 224) == 96) 5 27 32
662  
663 # single letter variable
664 5 $byte = ( $byte & 0x3F ) | 0x20;
665 5 my $name = $zx81chars{$byte};
666  
667 5 my @bytes;
668 5 ▶ for ( my $i = 0 ; $i < 5 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5) 25 5 30
669 25 push @bytes, $mem->peek( $addr++ );
670 }
671 5 my $float = zx81_to_float(@bytes);
672 5 return ( $addr - $start_addr, $name . "=$float" );
673 }
674  
675 27 ▶ if ( ( $byte & 0xE0 ) == 0xA0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($byte & 224) == 160) 10 17 27
676  
677 # multiple letter variable
678 10 $byte = ( $byte & 0x3F ) | 0x20;
679 10 my $name = $zx81chars{$byte}; # first letter
680  
681 10 ▶ while ( ( $mem->peek($addr) & 0xC0 ) == 0x00 )
Branch: 1 branch
BranchTrueFalseTotal
if (($mem->peek($addr) & 192) == 0) 5 10 15
682 { # second..before-last letters
683 5 $byte = $mem->peek( $addr++ );
684 5 $byte = ( $byte & 0x3F ) | 0x20;
685 5 $name .= $zx81chars{$byte}; # second..before-last letters
686 }
687  
688 10 $byte = $mem->peek( $addr++ );
689 10 ▶ ( $byte & 0xC0 ) == 0x80
Branch: 1 branch
BranchTrueFalseTotal
($byte & 192) == 128 or die "decode_var: unexpected byte $byte at $addr" 10 0 10
690 or die "decode_var: unexpected byte $byte at $addr";
691 10 $byte = ( $byte & 0x3F ) | 0x20;
692 10 $name .= $zx81chars{$byte}; # last letter
693  
694 10 my @bytes;
695 10 ▶ for ( my $i = 0 ; $i < 5 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5) 50 10 60
696 50 push @bytes, $mem->peek( $addr++ );
697 }
698 10 my $float = zx81_to_float(@bytes);
699 10 return ( $addr - $start_addr, $name . "=$float" );
700 }
701  
702 17 ▶ if ( ( $byte & 0xE0 ) == 0x80 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($byte & 224) == 128) 5 12 17
703  
704 # numeric array
705 5 $byte = ( $byte & 0x3F ) | 0x20;
706 5 my $name = $zx81chars{$byte};
707  
708 # array size
709 5 my $size = $mem->dpeek($addr);
710 5 $addr += 2;
711  
712 # number of dimensions
713 5 my $num_dims = $mem->peek( $addr++ );
714  
715 # dimensions
716 5 my $num_elements = 1;
717 5 my @dims;
718 5 for ( 1 .. $num_dims ) {
719 10 my $dim = $mem->dpeek($addr);
720 10 $addr += 2;
721 10 push @dims, $dim;
722 10 $num_elements *= $dim;
723 }
724  
725 # values
726 5 my @values;
727 5 for ( 1 .. $num_elements ) {
728 30 my @bytes;
729 30 ▶ for ( my $i = 0 ; $i < 5 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5) 150 30 180
730 150 push @bytes, $mem->peek( $addr++ );
731 }
732 30 my $float = zx81_to_float(@bytes);
733 30 push @values, $float;
734 }
735  
736 # text representation
737 5 my $text =
738 $name . "(" . join( ",", @dims ) . ")=" . join( ",", @values );
739 5 return ( $addr - $start_addr, $text );
740 }
741  
742 12 ▶ if ( ( $byte & 0xE0 ) == 0xE0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($byte & 224) == 224) 2 10 12
743  
744 # loop variable
745 2 $byte = ( $byte & 0x3F ) | 0x20;
746 2 my $name = $zx81chars{$byte};
747  
748 # value, limit, step
749 2 my @values;
750 2 for ( 1 .. 3 ) {
751 6 my @bytes;
752 6 ▶ for ( my $i = 0 ; $i < 5 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5) 30 6 36
753 30 push @bytes, $mem->peek( $addr++ );
754 }
755 6 my $float = zx81_to_float(@bytes);
756 6 push @values, $float;
757 }
758  
759 # line number
760 2 my $line_num = $mem->dpeek($addr);
761 2 $addr += 2;
762 2 push @values, $line_num;
763  
764 # text representation
765 2 my $text = $name . "=" . join( ",", @values );
766  
767 2 return ( $addr - $start_addr, $text );
768 }
769  
770 10 ▶ if ( ( $byte & 0xE0 ) == 0x40 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($byte & 224) == 64) 5 5 10
771  
772 # string variable
773 5 $byte = ( $byte & 0x3F ) | 0x20;
774 5 my $name = $zx81chars{$byte};
775  
776 # string length
777 5 my $length = $mem->dpeek($addr);
778 5 $addr += 2;
779  
780 # string value
781 5 my $value = '"';
782 5 ▶ for ( my $i = 0 ; $i < $length ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length) 25 5 30
783 25 my $byte = $mem->peek( $addr++ );
784 25 ▶ if ( exists $zx81chars{$byte} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $zx81chars{$byte}) { } 25 0 25
785 25 $value .= $zx81chars{$byte};
786 }
787 else {
788 0 die "decode_var: unexpected byte $byte at $addr";
789 }
790 }
791 5 $value .= '"';
792  
793 # text representation
794 5 my $text = $name . '$' . '=' . $value;
795 5 return ( $addr - $start_addr, $text );
796 }
797  
798 5 ▶ if ( ( $byte & 0xE0 ) == 0xC0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($byte & 224) == 192) 5 0 5
799  
800 # string array
801 5 $byte = ( $byte & 0x3F ) | 0x20;
802 5 my $name = $zx81chars{$byte};
803  
804 # array size
805 5 my $size = $mem->dpeek($addr);
806 5 $addr += 2;
807  
808 # number of dimensions
809 5 my $num_dims = $mem->peek( $addr++ );
810  
811 # dimensions
812 5 my $num_elements = 1;
813 5 my @dims;
814 5 for ( 1 .. $num_dims ) {
815 15 my $dim = $mem->dpeek($addr);
816 15 $addr += 2;
817 15 push @dims, $dim;
818 15 $num_elements *= $dim;
819 }
820 5 my $last_dim = $dims[-1];
821 5 $num_elements /= $last_dim;
822  
823 # values
824 5 my @values;
825 5 for ( 1 .. $num_elements ) {
826 30 my $value = '"';
827 30 ▶ for ( my $i = 0 ; $i < $last_dim ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $last_dim) 60 30 90
828 60 my $byte = $mem->peek( $addr++ );
829 60 ▶ if ( exists $zx81chars{$byte} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $zx81chars{$byte}) { } 60 0 60
830 60 $value .= $zx81chars{$byte};
831 }
832 else {
833 0 die "decode_var: unexpected byte $byte at $addr";
834 }
835 }
836 30 $value .= '"';
837 30 push @values, $value;
838 }
839  
840 # text representation
841 5 my $text =
842 $name . '$' . "(" . join( ",", @dims ) . ")=" . join( ",", @values );
843  
844 5 return ( $addr - $start_addr, $text );
845 }
846  
847 0 die "invalid byte $byte at ", $addr - 1;
848 }
849  
850 #------------------------------------------------------------------------------
851 # input BASIC line
852 #------------------------------------------------------------------------------
853 {
854  
855 package ZX81::Tape::Line;
856 106 ▶ use Object::Tiny::RW qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
857 src_file src_line_num
858 line_num label text
859 reminvert
860 bytes
861 106 );
862  
863 586 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
864 586 my ($class) = @_;
865 586 return bless {
866 src_file => '',
867 src_line_num => 0,
868 line_num => -1,
869 label => '',
870 text => '',
871 reminvert => 0,
872 bytes => '',
873 }, $class;
874 }
875  
876 6 ▶ sub error {
Subroutine error: called CC=1 SCAR=0.0
877 6 my ( $self, $message ) = @_;
878 6 die $self->src_file, ":", $self->src_line_num, ": error: ",
879 $message, "\n";
880 }
881 }
882  
883 #------------------------------------------------------------------------------
884 # BASIC variable
885 #------------------------------------------------------------------------------
886 {
887  
888 106 package ZX81::Tape::Var;
889 106 ▶ use Object::Tiny::RW qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
890 name is_string is_array is_loop
891 dims
892 value limit step line_num
893 106 );
894  
895 20 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
896 20 my ($class) = @_;
897 20 return bless {
898 name => '',
899 is_string => 0,
900 is_array => 0,
901 is_loop => 0,
902 dims => [],
903 value => [0],
904 limit => 0,
905 step => 0,
906 line_num => -1,
907 }, $class;
908 }
909 }
910  
911 #------------------------------------------------------------------------------
912 # Tape class
913 #------------------------------------------------------------------------------
914 106 ▶use Object::Tiny::RW qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
915 lines
916 vars
917 dfile
918 dfile_collapsed
919 last_line_num
920 labels
921 autostart autostart_line
922 increment
923 reminvert
924 fast
925 106 );
926  
927 118 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
928 118 my ($class) = @_;
929 118 my $self = bless {
930 lines => [],
931 vars => [],
932 dfile => [],
933 dfile_collapsed => 0,
934 last_line_num => -1,
935 labels => {},
936 autostart => 0,
937 autostart_line => 0,
938 increment => 10,
939 reminvert => 0,
940 fast => 0,
941 }, $class;
942 118 return $self;
943 }
944  
945 118 ▶sub parse_file {
Subroutine parse_file: called CC=27 SCAR=33.0
Pod undocumented
946 118 my ( $self, $filename ) = @_;
947  
948 # read lines from file
949 118 my @lines = path($filename)->lines( { chomp => 1 } );
950  
951 # add source line numbers
952 118 for my $i ( 0 .. $#lines ) {
953 586 my $line = ZX81::Tape::Line->new();
954 586 $line->src_file($filename);
955 586 $line->src_line_num( $i + 1 );
956 586 $line->text( $lines[$i] );
957 586 $lines[$i] = $line;
958 }
959  
960 # join continuation lines
961 118 ▶ for ( my $i = 0 ; $i < @lines ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines) 576 118 694
962 576 ▶ while ( $lines[$i]->{text} =~ s/\\$// && $i + 1 < @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[$i]{"text"} =~ s/\\$//u and $i + 1 < @lines) 10 576 586
Condition66%2/3
$lines[$i]{"text"} =~ s/\\$//u and $i + 1 < @lines
!ll&&!rl&&r
576010
MC/DC50%1/2
$lines[$i]{"text"} =~ s/\\$//u and $i + 1 < @lines
$lines[$i]{"text"} =~ s/\\$//u $i + 1 < @lines
Truth table66%2/3
A and B
A= $lines[$i]{"text"} =~ s/\\$//uB= $i + 1 < @lines
ABresult
0X0
100
111
963 10 $lines[$i]->text( $lines[$i]->text . $lines[ $i + 1 ]->text );
964 10 splice( @lines, $i + 1, 1 );
965 }
966 }
967  
968 # parse labels and line numbers
969 118 for my $line (@lines) {
970 576 $self->parse_label_line_num($line);
971 }
972  
973 # merge lines with only label or line number with next line
974 118 ▶ for ( my $i = 0 ; $i < @lines - 1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines - 1) 461 118 579
975 461 my $this = $lines[$i];
976 461 my $next = $lines[ $i + 1 ];
977 461 ▶ if ( $this->text =~ /^\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->text =~ /^\s*$/u) 35 426 461
978 35 ▶ my $num_labels =
Branch: 2 branches
BranchTrueFalseTotal
$this->label ne '' ? : 12 23 35
$next->label ne '' ? : 2 33 35
979 ( $this->label ne '' ? 1 : 0 ) + ( $next->label ne '' ? 1 : 0 );
980 35 ▶ my $num_line_nums =
Branch: 2 branches
BranchTrueFalseTotal
$this->line_num >= 0 ? : 16 19 35
$next->line_num >= 0 ? : 15 20 35
981 ( $this->line_num >= 0 ? 1 : 0 ) +
982 ( $next->line_num >= 0 ? 1 : 0 );
983 35 ▶ if ( $num_labels <= 1 && $num_line_nums <= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_labels <= 1 and $num_line_nums <= 1) 31 4 35
Condition66%2/3
$num_labels <= 1 and $num_line_nums <= 1
!ll&&!rl&&r
0431
MC/DC50%1/2
$num_labels <= 1 and $num_line_nums <= 1
$num_labels <= 1 $num_line_nums <= 1
Truth table66%2/3
A and B
A= $num_labels <= 1B= $num_line_nums <= 1
ABresult
0X0
100
111
984 31 ▶ $this->label( $next->label ) if $next->label ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $next->label ne "" 2 29 31
985 31 ▶ $this->line_num( $next->line_num ) if $next->line_num >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $next->line_num >= 0 11 20 31
986 31 $this->text( $next->text );
987 31 splice( @lines, $i + 1, 1 );
988 31 $i--;
989 }
990 }
991 }
992  
993 # parse lines
994 118 ▶ for ( my $i = 0 ; $i < @lines ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines) 545 114 659
995 545 my $line = $lines[$i];
996  
997 # skip blank lines
998 545 ▶ if ( $line->text =~ /^\s*$/
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*$/u and $line->label eq "" and $line->line_num < 0) 6 539 545
Condition100%6/6
$line->text =~ /^\s*$/u and $line->label eq "" and $line->line_num < 0
!ll&&!rl&&r
53186
$line->text =~ /^\s*$/u and $line->label eq ""
!ll&&!rl&&r
529214
MC/DC100%3/3
$line->text =~ /^\s*$/u and $line->label eq "" and $line->line_num < 0
$line->text =~ /^\s*$/u $line->label eq "" $line->line_num < 0
Truth table100%4/4
A and B and C
A= $line->text =~ /^\s*$/uB= $line->label eq ""C= $line->line_num < 0
ABCresult
0XX0
10X0
1100
1111
999 && $line->label eq ''
1000 && $line->line_num < 0 )
1001 {
1002 6 next;
1003 }
1004  
1005 # handle pragmas
1006 539 ▶ if ( $self->parse_pragma($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parse_pragma($line)) 214 325 539
1007 214 next;
1008 }
1009  
1010 # add reminvert tag
1011 325 $line->reminvert( $self->reminvert );
1012  
1013 # number the line
1014 325 ▶ if ( $line->line_num >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->line_num >= 0) { } 194 131 325
1015 194 ▶ if ( $line->line_num <= $self->last_line_num ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->line_num <= $self->last_line_num) 1 193 194
1016 1 $line->error( "BASIC lines out of order: "
1017 . $line->line_num
1018 . " after "
1019 . $self->last_line_num );
1020 }
1021 193 $self->last_line_num( $line->line_num );
1022 }
1023 else {
1024 131 ▶ my $new_line_num =
Branch: 1 branch
BranchTrueFalseTotal
$self->last_line_num < 0 ? : 27 104 131
1025 $self->last_line_num < 0
1026 ? $self->increment
1027 : $self->last_line_num + $self->increment;
1028 131 $line->line_num($new_line_num);
1029 131 $self->last_line_num($new_line_num);
1030 }
1031  
1032 324 ▶ if ( $line->line_num > 9999 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->line_num > 9999) 2 322 324
1033 2 $line->error(
1034 "BASIC line number exceeds 9999: " . $line->line_num );
1035 }
1036  
1037 # set the label if present
1038 322 ▶ if ( $line->label ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->label ne "") 20 302 322
1039 20 ▶ if ( exists $self->labels->{ $line->label } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->labels->{$line->label}) 1 19 20
1040 1 $line->error( "duplicate label: " . $line->label );
1041 }
1042 19 $self->labels->{ $line->label } = $line->line_num;
1043 }
1044  
1045 # add line to tape
1046 321 push @{ $self->lines }, $line;
1047 }
1048 }
1049  
1050 539 ▶sub parse_pragma {
Subroutine parse_pragma: called CC=15 SCAR=27.1
Pod undocumented
1051 539 my ( $self, $line ) = @_;
1052  
1053 539 ▶ if ( $line->text =~ /^\s*\#\s*AUTOSTART\s*=\s*(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*AUTOSTART\s*=\s*(\d+)/u) 19 520 539
1054 19 ▶ $self->autostart( $1 == 0 ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$1 == 0 ? : 0 19 19
1055 19 return 1;
1056 }
1057 520 ▶ if ( $line->text =~ /^\s*\#\s*AUTOSTART_LINE\s*=\s*(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*AUTOSTART_LINE\s*=\s*(\d+)/u) 10 510 520
1058 10 $self->autostart_line( 0 + $1 );
1059 10 return 1;
1060 }
1061 510 ▶ if ( $line->text =~ /^\s*\#\s*INCREMENT\s*=\s*(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*INCREMENT\s*=\s*(\d+)/u) 4 506 510
1062 4 $self->increment( 0 + $1 );
1063 4 return 1;
1064 }
1065 506 ▶ if ( $line->text =~ /^\s*\#\s*REMINVERT\s*=\s*(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*REMINVERT\s*=\s*(\d+)/u) 4 502 506
1066 4 ▶ $self->reminvert( $1 == 0 ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$1 == 0 ? : 2 2 4
1067 4 return 1;
1068 }
1069 502 ▶ if ( $line->text =~ /^\s*\#\s*FAST\s*=\s*(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*FAST\s*=\s*(\d+)/u) 5 497 502
1070 5 ▶ $self->fast( $1 == 0 ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$1 == 0 ? : 2 3 5
1071 5 return 1;
1072 }
1073 497 ▶ if ( $line->text =~ /^\s*\#\s*VARS\s*(.*?)\s*=\s*(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*VARS\s*(.*?)\s*=\s*(.*)/u) 20 477 497
1074 20 my $var = $self->parse_var( $line, $1, $2 );
1075 20 ▶ push @{ $self->vars }, $var if defined $var;
Branch: 1 branch
BranchTrueFalseTotal
if defined $var 20 0 20
1076 20 return 1;
1077 }
1078 477 ▶ if ( $line->text =~ /^\s*\#\s*DFILE\s*=\s*\"(.*)\"/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*DFILE\s*=\s*\"(.*)\"/u) 146 331 477
1079 146 push @{ $self->dfile }, $1;
1080 146 return 1;
1081 }
1082 331 ▶ if ( $line->text =~ /^\s*\#\s*DFILE_COLLAPSED\s*=\s*(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*DFILE_COLLAPSED\s*=\s*(\d+)/u) 4 327 331
1083 4 ▶ $self->dfile_collapsed( $1 == 0 ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$1 == 0 ? : 0 4 4
1084 4 return 1;
1085 }
1086 327 ▶ if ( $line->text =~ /^\s*#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*#/u) 2 325 327
1087  
1088 # unrecognized pragma, handle as comment
1089 2 return 1;
1090 }
1091  
1092 325 return 0;
1093 }
1094  
1095 20 ▶sub parse_var {
Subroutine parse_var: called CC=23 SCAR=32.4
Pod undocumented
1096 20 my ( $self, $line, $name, $value ) = @_;
1097  
1098 20 my $var = ZX81::Tape::Var->new();
1099  
1100 # collect variable name
1101 20 my $text = $name;
1102 20 ▶ $text =~ s/^\s*([a-z_]\w*)//i
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/^\s*([a-z_]\w*)//iu or $line->error('invalid variable name: ' . $name) 20 0 20
1103 or $line->error( "invalid variable name: " . $name );
1104 20 $var->name($1);
1105  
1106 # collect '$' for string variable
1107 20 ▶ if ( $text =~ s/^\s*\$// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^\s*\$//u) 6 14 20
1108 6 $var->is_string(1);
1109 }
1110  
1111 # collect array dimensions
1112 20 ▶ if ( $text =~ s/^\s*\((.*)\)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^\s*\((.*)\)//u) 6 14 20
1113 6 $var->is_array(1);
1114 6 my @dims = split( /\s*,\s*/, $1 );
1115 6 for my $dim (@dims) {
1116 15 ▶ if ( $dim !~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dim =~ /^\d+$/u) 0 15 15
1117 0 $line->error( "invalid array dimension: " . $dim );
1118 }
1119 15 push @{ $var->dims }, 0 + $dim;
1120 }
1121 }
1122  
1123 # check for any remaining text in the variable name
1124 20 ▶ if ( $text =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\S/u) 0 20 20
1125 0 $line->error( "invalid variable name: " . $name );
1126 }
1127  
1128 # check variable name length
1129 20 ▶ if ( $var->is_string || $var->is_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->is_string or $var->is_array) 9 11 20
Condition100%3/3
$var->is_string or $var->is_array
l!l&&r!l&&!r
6311
MC/DC100%2/2
$var->is_string or $var->is_array
$var->is_string $var->is_array
Truth table100%3/3
A or B
A= $var->is_stringB= $var->is_array
ABresult
1X1
011
000
1130 9 ▶ if ( length( $var->name ) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $var->name > 1) 0 9 9
1131 0 $line->error( "invalid variable name: " . $name );
1132 }
1133 }
1134  
1135 # compute number of elements needed
1136 20 my $num_elements = 1;
1137 20 my $size_string_element = 0;
1138 20 ▶ if ( $var->is_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->is_array) 6 14 20
1139 6 for my $dim ( @{ $var->dims } ) {
1140 15 $num_elements *= $dim;
1141 }
1142 6 ▶ if ( $var->is_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->is_string) 3 3 6
1143 3 $size_string_element = $var->dims->[-1];
1144 3 $num_elements /= $size_string_element;
1145 }
1146 }
1147  
1148 # collect variable values
1149 20 ▶ if ( !$var->is_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $var->is_string) { } 14 6 20
1150 14 my @values = split( /\s*,\s*/, $value );
1151 14 ▶ if ( @values == 4 && !$var->is_array ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@values == 4 and not $var->is_array) { } 2 12 14
elsif (@values != $num_elements) { } 0 12 12
Condition66%2/3
@values == 4 and not $var->is_array
!ll&&!rl&&r
1202
MC/DC50%1/2
@values == 4 and not $var->is_array
@values == 4 not $var->is_array
Truth table66%2/3
A and not(B)
A= @values == 4B= not $var->is_array
ABresult
0X0
100
111
1152 2 $var->is_loop(1);
1153 }
1154 elsif ( @values != $num_elements ) {
1155 0 $line->error(
1156 "number of values does not match array dimensions: " . $value );
1157 }
1158 14 for (@values) {
1159 35 ▶ if ( $_ !~ /^-?(\d+|\d*\.\d+|\d+\.\d*)(e[-+]?\d+)?$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_ =~ /^-?(\d+|\d*\.\d+|\d+\.\d*)(e[-+]?\d+)?$/iu) 0 35 35
1160 0 $line->error( "invalid variable value: " . $_ );
1161 }
1162 }
1163 14 ▶ if ( $var->is_loop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->is_loop) { } 2 12 14
1164 2 $var->value( [ 0 + $values[0] ] );
1165 2 $var->limit( 0 + $values[1] );
1166 2 $var->step( 0 + $values[2] );
1167 2 $var->line_num( 0 + $values[3] );
1168 }
1169 else {
1170 12 $var->value( [ map { 0 + $_ } @values ] );
1171 }
1172 }
1173 else {
1174 6 $text = $value;
1175 6 my @values;
1176 6 ▶ while ( $text =~ s/^\s*("((?:[^"\\]|\\.)*)")\s*,?// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^\s*("((?:[^"\\]|\\.)*)")\s*,?//u) 21 6 27
1177 21 push @values, $2;
1178 }
1179 6 ▶ if ( @values != $num_elements ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values != $num_elements) 0 6 6
1180 0 $line->error(
1181 "number of values does not match array dimensions: " . $value );
1182 }
1183 6 ▶ if ( $text =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\S/u) 0 6 6
1184 0 $line->error( "invalid string variable value: " . $text );
1185 }
1186 6 $var->value( [@values] );
1187 }
1188  
1189 20 return $var;
1190 }
1191  
1192 576 ▶sub parse_label_line_num {
Subroutine parse_label_line_num: called CC=5 SCAR=16.1
Pod undocumented
1193 576 my ( $self, $line ) = @_;
1194  
1195 576 my $got_any;
1196 576 while (1) {
1197 790 ▶ if ( $line->label eq ''
Branch: 2 branches
BranchTrueFalseTotal
if ($line->label eq '' and $line->{'text'} =~ s/^\s*\@([a-z_]\w+)\s*:\s*//iu) { } 20 770 790
elsif ($line->line_num < 0 and $line->{'text'} =~ s/^\s*(\d+)\s*//iu) { } 194 576 770
Condition100%3/3
$line->label eq "" and $line->{"text"} =~ s/^\s*\@([a-z_]\w+)\s*:\s*//iu
!ll&&!rl&&r
2274820
MC/DC100%2/2
$line->label eq "" and $line->{"text"} =~ s/^\s*\@([a-z_]\w+)\s*:\s*//iu
$line->label eq "" $line->{"text"} =~ s/^\s*\@([a-z_]\w+)\s*:\s*//iu
Truth table100%3/3
A and B
A= $line->label eq ""B= $line->{"text"} =~ s/^\s*\@([a-z_]\w+)\s*:\s*//iu
ABresult
0X0
100
111
1198 && $line->{text} =~ s/^\s*\@([a-z_]\w+)\s*:\s*//i )
1199 {
1200 20 $line->label($1);
1201 20 $got_any = 1;
1202 }
1203 ▶ elsif ( $line->line_num < 0 && $line->{text} =~ s/^\s*(\d+)\s*//i ) {
Condition100%3/3
$line->line_num < 0 and $line->{"text"} =~ s/^\s*(\d+)\s*//iu
!ll&&!rl&&r
194382194
MC/DC100%2/2
$line->line_num < 0 and $line->{"text"} =~ s/^\s*(\d+)\s*//iu
$line->line_num < 0 $line->{"text"} =~ s/^\s*(\d+)\s*//iu
Truth table100%3/3
A and B
A= $line->line_num < 0B= $line->{"text"} =~ s/^\s*(\d+)\s*//iu
ABresult
0X0
100
111
1204 194 $line->line_num($1);
1205 194 $got_any = 1;
1206 }
1207 else {
1208 576 last;
1209 }
1210 }
1211 576 $line->{text} =~ s/^\s+//;
1212  
1213 576 return $got_any;
1214 }
1215  
1216 114 ▶sub compile {
Subroutine compile: called CC=12 SCAR=24.8
Pod undocumented
1217 114 my ($self) = @_;
1218  
1219 # remove empty lines
1220 114 @{ $self->lines } = grep { $_->text !~ /^\s*$/ } @{ $self->lines };
1221  
1222 # replace all labels with line numbers
1223 114 for my $line ( @{ $self->lines } ) {
1224 308 ▶ while ( $line->{text} =~ /\@([a-z_]\w+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{"text"} =~ /\@([a-z_]\w+)/u) 15 307 322
1225 15 my $label = $1;
1226 15 ▶ if ( !exists $self->labels->{$label} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->labels->{$label}) 1 14 15
1227 1 $line->error( "undefined label: " . $label );
1228 }
1229 14 my $line_num = $self->labels->{$label};
1230 14 $line->{text} =~ s/\@$label/$line_num/g;
1231 }
1232 }
1233  
1234 # replace &label with line address
1235 113 my %line_addr;
1236 113 my $old_size = 0;
1237 113 ▶ for ( my $pass = 1 ; $pass <= 2 ; $pass++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass <= 2) 328 112 440
1238 328 my $bin = '';
1239 328 for my $line ( @{ $self->lines } ) {
1240  
1241 # calculate the address of the line
1242 923 ▶ if ( $line->label ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->label ne "") 50 873 923
1243 50 $line_addr{ $line->label } = 0x407D + length($bin);
1244 }
1245  
1246 # replace &label with line address
1247 923 my $text = $line->line_num . " " . $line->text;
1248 923 ▶ while ( $text =~ /\&([a-z_]\w+)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\&([a-z_]\w+)/iu) 15 922 937
1249 15 my $label = $1;
1250 15 ▶ if ( !exists $self->labels->{$label} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->labels->{$label}) 1 14 15
1251 1 $line->error( "undefined label: " . $label );
1252 }
1253 14 ▶ my $addr = $line_addr{$label} // 0;
Condition100%2/2
$line_addr{$label} // 0
l!l
122
MC/DC100%1/1
$line_addr{$label} // 0
$line_addr{$label}
Truth table100%2/2
A
A= $line_addr{$label}
Aresult
11
00
1254 14 $text =~ s/\&$label/$addr/g;
1255 }
1256  
1257 # apply reminvert if requested
1258 922 ▶ if ( $line->reminvert ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->reminvert) 24 898 922
1259 24 $text = $self->invert_rem($text);
1260 }
1261  
1262 922 my $line_bytes = tokenize_basic($text);
1263 922 $line->bytes($line_bytes);
1264 922 $bin .= $line_bytes;
1265 }
1266  
1267 327 my $new_size = length($bin);
1268 327 ▶ if ( $old_size != $new_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_size != $new_size) 103 224 327
1269 103 $old_size = $new_size;
1270 103 $pass = 0; # repeat if size changed
1271 }
1272 }
1273 }
1274  
1275 24 ▶sub invert_rem {
Subroutine invert_rem: called CC=4 SCAR=13.9
Pod undocumented
1276 24 my ( $self, $text ) = @_;
1277 24 ▶ if ( $text =~ /^(\s*\d+\s+REM ?)(.*)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^(\s*\d+\s+REM ?)(.*)$/iu) 18 6 24
1278 18 my ( $rem_statement, $rem_text ) = ( $1, $2 );
1279 18 ▶ if ( $rem_text =~ /\\/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rem_text =~ /\\/u) 6 12 18
1280  
1281 # don't invert if there are escape sequences
1282 6 return $text;
1283 }
1284  
1285 # invert (prepend %) to all not inverted characters
1286 12 my @chars = $rem_text =~ /(\%?[^\%])/g;
1287 12 for (@chars) { s/^([^\%])/\%$1/; } # invert C to %C
1288 12 $rem_text = join( '', @chars );
1289  
1290 12 return $rem_statement . $rem_text;
1291 }
1292 6 return $text;
1293 }
1294  
1295 112 ▶sub as_bytes {
Subroutine as_bytes: called CC=7 SCAR=19.5
Pod undocumented
1296 112 my ($self) = @_;
1297  
1298 # Convert the stored BASIC lines to a byte string
1299 112 my $prog = '';
1300 112 my @line_addr;
1301 112 for my $line ( @{ $self->lines } ) {
1302 306 push @line_addr, [ $line->line_num, $sysvars{PROG} + length($prog) ];
1303 306 $prog .= $line->bytes;
1304 }
1305 112 push @line_addr, [ 10000, $sysvars{PROG} + length($prog) ];
1306  
1307 112 my $prog_start = $sysvars{PROG};
1308 112 ▶ my $e_ppc = $prog eq '' ? 0 : unpack( "n", substr( $prog, 0, 2 ) );
Branch: 1 branch
BranchTrueFalseTotal
$prog eq '' ? : 11 101 112
1309  
1310 # Create an empty display file
1311 112 my $dfile = $self->dfile_as_bytes();
1312 112 my $dfile_start = $prog_start + length($prog);
1313  
1314 # Create an empty vars area
1315 112 my $vars = $self->vars_as_bytes();
1316 112 my $vars_start = $dfile_start + length($dfile);
1317 112 my $mem_end = $vars_start + length($vars);
1318  
1319 # define autostart
1320 112 my $nxtlin = $dfile_start;
1321 112 ▶ if ( $self->autostart ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->autostart) 19 93 112
1322  
1323 # get address of first line at or after autostart line
1324 19 my $autostart_line = $self->autostart_line;
1325 19 for my $line_addr (@line_addr) {
1326 26 ▶ if ( $line_addr->[0] >= $autostart_line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_addr->[0] >= $autostart_line) 19 7 26
1327 19 $nxtlin = $line_addr->[1];
1328 19 last;
1329 }
1330 }
1331 }
1332  
1333 # define fast mode
1334 112 ▶ my $cdflag = $self->fast ? 0x00 : 0x40; # SLOW if not fast
Branch: 1 branch
BranchTrueFalseTotal
$self->fast ? : 3 109 112
1335  
1336 # Create the system variables
1337 112 my $bytes = '';
1338 112 $bytes .= pack( "C", 0 ); # VERSN
1339 112 $bytes .= pack( "v", $e_ppc ); # E_PPC
1340 112 $bytes .= pack( "v", $dfile_start ); # D_FILE
1341 112 $bytes .= pack( "v", $dfile_start + 1 ); # DF_CC
1342 112 $bytes .= pack( "v", $vars_start ); # VARS
1343 112 $bytes .= pack( "v", 0 ); # DEST
1344 112 $bytes .= pack( "v", $mem_end ); # E_LINE
1345 112 $bytes .= pack( "v", $mem_end + 1 ); # CH_ADD
1346 112 $bytes .= pack( "v", 0 ); # X_PTR
1347 112 $bytes .= pack( "v", $mem_end + 2 ); # STKBOT
1348 112 $bytes .= pack( "v", $mem_end + 2 ); # STKEND
1349 112 $bytes .= pack( "C", 0 ); # BREG
1350 112 $bytes .= pack( "v", 0x405d ); # MEM
1351 112 $bytes .= pack( "C", 0 ); # not used
1352 112 $bytes .= pack( "C", 2 ); # DF_SZ
1353 112 $bytes .= pack( "v", 0 ); # S_TOP
1354 112 $bytes .= pack( "v", 0xFFFF ); # LAST_K
1355 112 $bytes .= pack( "C", 0x0F ); # DEBOUNCE
1356 112 $bytes .= pack( "C", 55 ); # MARGIN
1357 112 $bytes .= pack( "v", $nxtlin ); # NXTLIN
1358 112 $bytes .= pack( "v", 0 ); # OLDPPC
1359 112 $bytes .= pack( "C", 0 ); # FLAGX
1360 112 $bytes .= pack( "v", 0 ); # STRLEN
1361 112 $bytes .= pack( "v", 0x0C6B ); # T_ADDR
1362 112 $bytes .= pack( "v", 0 ); # SEED
1363 112 $bytes .= pack( "v", 0xFFFF ); # FRAMES
1364 112 $bytes .= pack( "C*", 0, 0 ); # COORDS
1365 112 $bytes .= pack( "C", 0xBC ); # PR_CC
1366 112 $bytes .= pack( "C*", 33, 24 ); # S_POSN
1367 112 $bytes .= pack( "C", $cdflag ); # CDFLAG
1368 112 $bytes .= pack( "C*", (0) x 32, 0x76 ); # Print buffer
1369 112 $bytes .= pack( "C*", (0) x 30 ); # Calculator's memory area
1370 112 $bytes .= pack( "C*", (0) x 2 ); # not used
1371  
1372 # Pack the BASIC program, dfile and vars areas
1373 112 $bytes .= $prog;
1374 112 $bytes .= $dfile;
1375 112 $bytes .= $vars;
1376  
1377 112 return $bytes;
1378 }
1379  
1380 112 ▶sub dfile_as_bytes {
Subroutine dfile_as_bytes: called CC=4 SCAR=13.9
Pod undocumented
1381 112 my ($self) = @_;
1382  
1383 112 my @lines = @{ $self->dfile };
1384 112 my $bytes = pack( "C", 0x76 );
1385 112 for ( 1 .. 24 ) {
1386 2688 ▶ my $line = shift @lines // '';
Condition100%2/2
shift @lines // ""
l!l
1442544
MC/DC100%1/1
shift @lines // ""
shift @lines
Truth table100%2/2
A
A= shift @lines
Aresult
11
00
1387 2688 ▶ if ( $self->dfile_collapsed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dfile_collapsed) { } 96 2592 2688
1388 96 $line =~ s/\s+$//;
1389 }
1390 else {
1391 2592 $line .= ' ' x 32;
1392 }
1393 2688 $line = substr( $line, 0, 32 );
1394  
1395 2688 $bytes .= pack( "C*", map { ord } split //, encode_chars($line) );
1396 2688 $bytes .= pack( "C", 0x76 );
1397 }
1398  
1399 112 return $bytes;
1400 }
1401  
1402 112 ▶sub vars_as_bytes {
Subroutine vars_as_bytes: called CC=14 SCAR=26.4
Pod undocumented
1403 112 my ($self) = @_;
1404  
1405 112 my $bytes = '';
1406 112 for my $var ( @{ $self->vars } ) {
1407 20 my @name_bytes = map { ord } split //, encode_chars( $var->name );
1408 20 ▶ if ( $var->is_loop ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($var->is_loop) { } 2 18 20
elsif ($var->is_string) { } 6 12 18
1409  
1410 # loop variable
1411 # 111-letter
1412 2 $bytes .= pack( "C", ( $name_bytes[0] & 0x3F ) | 0xE0 );
1413  
1414 # value
1415 2 $bytes .= encode_float( $var->value->[0] );
1416  
1417 # limit
1418 2 $bytes .= encode_float( $var->limit );
1419  
1420 # step
1421 2 $bytes .= encode_float( $var->step );
1422  
1423 # line number
1424 2 $bytes .= pack( "v", $var->line_num );
1425 }
1426 elsif ( $var->is_string ) {
1427 6 ▶ if ( $var->is_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->is_array) { } 3 3 6
1428  
1429 # string array variable
1430 3 my $num_values = 1;
1431 3 for my $dim ( @{ $var->dims } ) {
1432 9 $num_values *= $dim;
1433 }
1434 3 my $last_dim = $var->dims->[-1];
1435 3 $num_values /= $last_dim;
1436  
1437 # 110-letter
1438 3 $bytes .= pack( "C", ( $name_bytes[0] & 0x1F ) | 0xC0 );
1439  
1440 # total size of array in bytes
1441 $bytes .= pack( "v",
1442 3 1 + 2 * scalar( @{ $var->dims } ) +
1443 $last_dim * $num_values );
1444  
1445 # number of dimensions
1446 3 $bytes .= pack( "C", scalar( @{ $var->dims } ) );
1447  
1448 # dimensions
1449 3 for my $dim ( @{ $var->dims } ) {
1450 9 $bytes .= pack( "v", $dim );
1451 }
1452  
1453 # values
1454 3 for ( 0 .. $num_values - 1 ) {
1455 18 my $value = $var->value->[$_];
1456  
1457 # make fixed length
1458 18 $value .= ' ' x $last_dim;
1459 18 $value = substr( $value, 0, $last_dim );
1460  
1461 # convert to bytes
1462 18 my @value_bytes = map { ord } split //,
1463 encode_chars($value);
1464 18 $bytes .= pack( "C*", @value_bytes );
1465 }
1466 }
1467 else {
1468 # string variable
1469 3 my @value_bytes = map { ord } split //,
1470 encode_chars( $var->value->[0] );
1471  
1472 # 010-letter
1473 3 $bytes .= pack( "C", ( $name_bytes[0] & 0x1F ) | 0x40 );
1474  
1475 # length of string
1476 3 $bytes .= pack( "v", scalar(@value_bytes) );
1477  
1478 # string bytes
1479 3 $bytes .= pack( "C*", @value_bytes );
1480 }
1481 }
1482 else {
1483 12 ▶ if ( $var->is_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->is_array) { } 3 9 12
1484  
1485 # numeric array variable
1486 3 my $num_values = 1;
1487 3 for my $dim ( @{ $var->dims } ) {
1488 6 $num_values *= $dim;
1489 }
1490  
1491 # 100-letter
1492 3 $bytes .= pack( "C", ( $name_bytes[0] & 0x1F ) | 0x80 );
1493  
1494 # total size of array in bytes
1495 $bytes .= pack( "v",
1496 3 1 + 2 * scalar( @{ $var->dims } ) + 5 * $num_values );
1497  
1498 # number of dimensions
1499 3 $bytes .= pack( "C", scalar( @{ $var->dims } ) );
1500  
1501 # dimensions
1502 3 for my $dim ( @{ $var->dims } ) {
1503 6 $bytes .= pack( "v", $dim );
1504 }
1505  
1506 # values
1507 3 for ( 0 .. $num_values - 1 ) {
1508 18 $bytes .= encode_float( $var->value->[$_] );
1509 }
1510 }
1511 else {
1512 # numeric variable
1513 9 ▶ if ( @name_bytes == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@name_bytes == 1) { } 3 6 9
1514  
1515 # 011-letter
1516 3 $bytes .= pack( "C", ( $name_bytes[0] & 0x3F ) | 0x60 );
1517 }
1518 else {
1519 # 101-letter, 001-letter, 101-letter
1520 6 $bytes .= pack( "C", ( $name_bytes[0] & 0x3F ) | 0xA0 );
1521 6 for ( 1 .. $#name_bytes - 1 ) {
1522 3 $bytes .= pack( "C", $name_bytes[$_] & 0x3F );
1523 }
1524 6 $bytes .= pack( "C", ( $name_bytes[-1] & 0x3F ) | 0x80 );
1525 }
1526  
1527 # pack value
1528 9 $bytes .= encode_float( $var->value->[0] );
1529 }
1530 }
1531 }
1532  
1533 112 $bytes .= pack( "C", 0x80 ); # end of vars
1534 112 return $bytes;
1535 }
1536  
1537 106 1;
1538  
1539 __END__
↑ Summary bin/zx81_make_p »