blib/lib/ZX81/Tape.pm

filter: Statementstmt 96.3% 822 / 853 Branchbran 91.4% 355 / 388 Conditioncond 81.9% 77 / 94 MC/DCmcdc 68.5% 37 / 54 Subroutinesub 95.2% 40 / 42 Podpod 15.7% 3 / 19 totaltotal 92.0% 1334 / 1450 CC 265 SCAR 30.4
CC
265
Cov
94%
as_bytes
39.0
list_p
31.6
parse_file
31.0
CRAP 20.9
↑ Summary bin/zx81_make_p »
1 package ZX81::Tape;
2  
3 117 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 117 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 117 ▶use CPU::Z80::Assembler;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 117 ▶use ZX81::Tokenize qw( tokenize_basic encode_chars encode_float );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 117 our $VERSION = '0.006';
9  
10 117 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 117 our @EXPORT_OK = qw(
13 make_p
14 dump_p
15 list_p
16 );
17  
18 #------------------------------------------------------------------------------
19 # Memory area
20 #------------------------------------------------------------------------------
21 {
22  
23 package ZX81::Tape::Memory;
24 117 ▶ use Object::Tiny::RW qw( start data );
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 362 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
27 362 my ( $class, $start, @data ) = @_;
28 362 return $class->SUPER::new( start => $start, data => \@data );
29 }
30  
31 348464 ▶ sub peek {
Subroutine peek: called CC=3 SCAR=12.3
32 348464 my ( $self, $addr ) = @_;
33 348464 my $p = $addr - $self->start;
34 348464 ▶ if ( $p < 0 || $p >= @{ $self->data } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < 0 or $p >= @{$self->data;}) 0 348464 348464
Condition33%1/3
$p < 0 or $p >= @{$self->data;}
l!l&&r!l&&!r
00348464
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
35 0 return 0;
36 }
37 348464 return $self->data->[$p];
38 }
39  
40 4476 ▶ sub dpeek {
Subroutine dpeek: called CC=1 SCAR=0.0
41 4476 my ( $self, $addr ) = @_;
42 4476 return $self->peek($addr) | ( $self->peek( $addr + 1 ) << 8 );
43 }
44  
45 681 ▶ sub dpeek_be {
Subroutine dpeek_be: called CC=1 SCAR=0.0
46 681 my ( $self, $addr ) = @_;
47 681 return ( $self->peek($addr) << 8 ) | $self->peek( $addr + 1 );
48 }
49  
50 270 ▶ sub peek_bytes {
Subroutine peek_bytes: called CC=2 SCAR=6.9
51 270 my ( $self, $addr, $length ) = @_;
52 270 my @bytes;
53 270 for my $i ( 0 .. $length - 1 ) {
54 31320 push @bytes, $self->peek( $addr + $i );
55 }
56 270 return @bytes;
57 }
58  
59 16356 ▶ sub poke {
Subroutine poke: called CC=2 SCAR=7.4
60 16356 my ( $self, $addr, $value ) = @_;
61 16356 my $p = $addr - $self->start;
62 16356 ▶ if ( $p < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < 0) 0 16356 16356
63 0 return;
64 }
65 16356 $self->data->[$p] = $value & 0xFF;
66 }
67  
68 2820 ▶ sub dpoke {
Subroutine dpoke: called CC=1 SCAR=0.0
69 2820 my ( $self, $addr, $value ) = @_;
70 2820 $self->poke( $addr, $value & 0xFF );
71 2820 $self->poke( $addr + 1, ( $value >> 8 ) & 0xFF );
72 }
73  
74 0 ▶ sub dpoke_be {
Subroutine dpoke_be: not called CC=1 SCAR=6.9
75 0 my ( $self, $addr, $value ) = @_;
76 0 $self->poke( $addr, ( $value >> 8 ) & 0xFF );
77 0 $self->poke( $addr + 1, $value & 0xFF );
78 }
79  
80 0 ▶ sub poke_bytes {
Subroutine poke_bytes: not called CC=2 SCAR=17.9
81 0 my ( $self, $addr, @bytes ) = @_;
82 0 for my $i ( 0 .. $#bytes ) {
83 0 $self->poke( $addr + $i, $bytes[$i] );
84 }
85 }
86 }
87  
88 #------------------------------------------------------------------------------
89 # zx81 system variables and character mappings
90 #------------------------------------------------------------------------------
91 117 my %sysvars = (
92 ERR_NO => 0x4000,
93 FLAGS => 0x4001,
94 ERR_SP => 0x4002,
95 RAMTOP => 0x4004,
96 MODE => 0x4006,
97 PPC => 0x4007,
98 VERSN => 0x4009,
99 E_PPC => 0x400a,
100 D_FILE => 0x400c,
101 DF_CC => 0x400e,
102 VARS => 0x4010,
103 DEST => 0x4012,
104 E_LINE => 0x4014,
105 CH_ADD => 0x4016,
106 X_PTR => 0x4018,
107 STKBOT => 0x401a,
108 STKEND => 0x401c,
109 BREG => 0x401e,
110 MEM => 0x401f,
111 FREE1 => 0x4021,
112 DF_SZ => 0x4022,
113 S_TOP => 0x4023,
114 LAST_K => 0x4025,
115 DEBOUNCE => 0x4027,
116 MARGIN => 0x4028,
117 NXTLIN => 0x4029,
118 OLDPPC => 0x402b,
119 FLAGX => 0x402d,
120 STRLEN => 0x402e,
121 T_ADDR => 0x4030,
122 SEED => 0x4032,
123 FRAMES => 0x4034,
124 COORDS_X => 0x4036,
125 COORDS_Y => 0x4037,
126 PR_CC => 0x4038,
127 S_POSN_COL => 0x4039,
128 S_POSN_ROW => 0x403a,
129 CDFLAG => 0x403b,
130 PRBUFF => 0x403c,
131 MEMBOT => 0x405d,
132 FREE2 => 0x407b,
133 PROG => 0x407d,
134 );
135  
136 117 my %zx81chars = (
137 0x00 => ' ',
138 0x0b => '"',
139 0x0c => '#',
140 0x0d => '$',
141 0x0e => ':',
142 0x0f => '?',
143 0x10 => '(',
144 0x11 => ')',
145 0x12 => '>',
146 0x13 => '<',
147 0x14 => '=',
148 0x15 => '+',
149 0x16 => '-',
150 0x17 => '*',
151 0x18 => '/',
152 0x19 => ';',
153 0x1a => ',',
154 0x1b => '.',
155 0x1c => '0',
156 0x1d => '1',
157 0x1e => '2',
158 0x1f => '3',
159 0x20 => '4',
160 0x21 => '5',
161 0x22 => '6',
162 0x23 => '7',
163 0x24 => '8',
164 0x25 => '9',
165 0x26 => 'A',
166 0x27 => 'B',
167 0x28 => 'C',
168 0x29 => 'D',
169 0x2a => 'E',
170 0x2b => 'F',
171 0x2c => 'G',
172 0x2d => 'H',
173 0x2e => 'I',
174 0x2f => 'J',
175 0x30 => 'K',
176 0x31 => 'L',
177 0x32 => 'M',
178 0x33 => 'N',
179 0x34 => 'O',
180 0x35 => 'P',
181 0x36 => 'Q',
182 0x37 => 'R',
183 0x38 => 'S',
184 0x39 => 'T',
185 0x3a => 'U',
186 0x3b => 'V',
187 0x3c => 'W',
188 0x3d => 'X',
189 0x3e => 'Y',
190 0x3f => 'Z',
191 0x40 => 'RND',
192 0x41 => 'INKEY$',
193 0x42 => 'PI',
194 0x80 => '% ',
195 0x8b => '%"',
196 0x8c => '%#',
197 0x8d => '%$',
198 0x8e => '%:',
199 0x8f => '%?',
200 0x90 => '%(',
201 0x91 => '%)',
202 0x92 => '%>',
203 0x93 => '%<',
204 0x94 => '%=',
205 0x95 => '%+',
206 0x96 => '%-',
207 0x97 => '%*',
208 0x98 => '%/',
209 0x99 => '%;',
210 0x9a => '%,',
211 0x9b => '%.',
212 0x9c => '%0',
213 0x9d => '%1',
214 0x9e => '%2',
215 0x9f => '%3',
216 0xa0 => '%4',
217 0xa1 => '%5',
218 0xa2 => '%6',
219 0xa3 => '%7',
220 0xa4 => '%8',
221 0xa5 => '%9',
222 0xa6 => '%A',
223 0xa7 => '%B',
224 0xa8 => '%C',
225 0xa9 => '%D',
226 0xaa => '%E',
227 0xab => '%F',
228 0xac => '%G',
229 0xad => '%H',
230 0xae => '%I',
231 0xaf => '%J',
232 0xb0 => '%K',
233 0xb1 => '%L',
234 0xb2 => '%M',
235 0xb3 => '%N',
236 0xb4 => '%O',
237 0xb5 => '%P',
238 0xb6 => '%Q',
239 0xb7 => '%R',
240 0xb8 => '%S',
241 0xb9 => '%T',
242 0xba => '%U',
243 0xbb => '%V',
244 0xbc => '%W',
245 0xbd => '%X',
246 0xbe => '%Y',
247 0xbf => '%Z',
248 0xc0 => '\\"',
249 0xc1 => 'AT',
250 0xc2 => 'TAB',
251 0xc4 => 'CODE',
252 0xc5 => 'VAL',
253 0xc6 => 'LEN',
254 0xc7 => 'SIN',
255 0xc8 => 'COS',
256 0xc9 => 'TAN',
257 0xca => 'ASN',
258 0xcb => 'ACS',
259 0xcc => 'ATN',
260 0xcd => 'LN',
261 0xce => 'EXP',
262 0xcf => 'INT',
263 0xd0 => 'SQR',
264 0xd1 => 'SGN',
265 0xd2 => 'ABS',
266 0xd3 => 'PEEK',
267 0xd4 => 'USR',
268 0xd5 => 'STR$',
269 0xd6 => 'CHR$',
270 0xd7 => 'NOT',
271 0xd8 => '**',
272 0xd9 => 'OR',
273 0xda => 'AND',
274 0xdb => '<=',
275 0xdc => '>=',
276 0xdd => '<>',
277 0xde => 'THEN',
278 0xdf => 'TO',
279 0xe0 => 'STEP',
280 0xe1 => 'LPRINT',
281 0xe2 => 'LLIST',
282 0xe3 => 'STOP',
283 0xe4 => 'SLOW',
284 0xe5 => 'FAST',
285 0xe6 => 'NEW',
286 0xe7 => 'SCROLL',
287 0xe8 => 'CONT',
288 0xe9 => 'DIM',
289 0xea => 'REM',
290 0xeb => 'FOR',
291 0xec => 'GOTO',
292 0xed => 'GOSUB',
293 0xee => 'INPUT',
294 0xef => 'LOAD',
295 0xf0 => 'LIST',
296 0xf1 => 'LET',
297 0xf2 => 'PAUSE',
298 0xf3 => 'NEXT',
299 0xf4 => 'POKE',
300 0xf5 => 'PRINT',
301 0xf6 => 'PLOT',
302 0xf7 => 'RUN',
303 0xf8 => 'SAVE',
304 0xf9 => 'RAND',
305 0xfa => 'IF',
306 0xfb => 'CLS',
307 0xfc => 'UNPLOT',
308 0xfd => 'CLEAR',
309 0xfe => 'RETURN',
310 0xff => 'COPY',
311 );
312  
313 # bytes is a list of 5 bytes, representing a ZX81 floating point number
314 1559 ▶sub zx81_to_float {
Subroutine zx81_to_float: called CC=7 SCAR=19.8
Pod undocumented
315 1559 my (@bytes) = @_;
316  
317 # Zero special case
318 1559 ▶ 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) 557 1002 1559
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
10020557
$bytes[0] == 0 and $bytes[1] == 0 and $bytes[2] == 0 and $bytes[3] == 0
!ll&&!rl&&r
10020557
$bytes[0] == 0 and $bytes[1] == 0 and $bytes[2] == 0
!ll&&!rl&&r
10020557
$bytes[0] == 0 and $bytes[1] == 0
!ll&&!rl&&r
10020557
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
319 && $bytes[1] == 0
320 && $bytes[2] == 0
321 && $bytes[3] == 0
322 && $bytes[4] == 0 )
323 {
324  
325 557 return 0.0;
326 }
327  
328 1002 my $exp = $bytes[0] - 128;
329 1002 ▶ my $sign = ( $bytes[1] & 0x80 ) ? -1.0 : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
$bytes[1] & 128 ? : 9 993 1002
330  
331 1002 my $mant =
332 ( ( $bytes[1] | 0x80 ) << 24 ) | ( $bytes[2] << 16 ) |
333 ( $bytes[3] << 8 ) | ( $bytes[4] );
334  
335 1002 my $value = $sign * $mant * ( 2**( $exp - 32 ) );
336 1002 return $value;
337 }
338  
339 #------------------------------------------------------------------------------
340 # make_p driver
341 #------------------------------------------------------------------------------
342 147 ▶sub make_p {
Subroutine make_p: called CC=1 SCAR=0.0
Pod documented
343 147 my ( $basic_filename, $p_filename ) = @_;
344  
345 147 my $tape = ZX81::Tape->new();
346 147 $tape->parse_file($basic_filename);
347 143 $tape->compile();
348 141 my $bytes = $tape->as_bytes();
349 141 path($p_filename)->spew_raw($bytes);
350 141 return;
351 }
352  
353 #------------------------------------------------------------------------------
354 # dump_p
355 #------------------------------------------------------------------------------
356 92 ▶sub dump_p {
Subroutine dump_p: called CC=14 SCAR=26.4
Pod documented
357 92 my ( $p_filename, $dump_filename ) = @_;
358 92 my $dump_fh;
359 92 ▶ if ( defined $dump_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dump_filename) { } 92 0 92
360 92 ▶ open( $dump_fh, ">", $dump_filename )
Branch: 1 branch
BranchTrueFalseTotal
open $dump_fh, ">", $dump_filename or die "error: open ${dump_filename}: $!\n" 92 0 92
361 or die "error: open $dump_filename: $!\n";
362 }
363 else {
364 0 $dump_fh = *STDOUT;
365 }
366  
367 # load memory
368 92 my @bytes = ( map { ord } split //, path($p_filename)->slurp_raw );
369 92 my $mem = ZX81::Tape::Memory->new( $sysvars{VERSN}, @bytes );
370  
371 # memory access helpers
372 86080 ▶ my $peek_text = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
373 86080 my ( $addr, $size ) = @_;
374 86080 my $text = "";
375 86080 ▶ for ( my $i = 0 ; $i < $size ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 89610 86080 175690
376 89610 my $byte = $mem->peek( $addr + $i );
377 89610 ▶ if ( $byte == 0x7E && $size > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($byte == 126 and $size > 1) { } 165 89445 89610
elsif (exists $zx81chars{$byte}) { } 85582 3863 89445
Condition100%3/3
$byte == 126 and $size > 1
!ll&&!rl&&r
89278167165
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
378  
379 # floating point number
380 165 my @bytes;
381 165 ▶ for ( my $j = 1 ; $j < 6 ; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 6) 825 165 990
382 825 push @bytes, $mem->peek( $addr + $i + $j );
383 }
384 165 my $float = zx81_to_float(@bytes);
385 165 $text .= "{$float}";
386  
387 165 $i += 6 - 1;
388 }
389 elsif ( exists $zx81chars{$byte} ) {
390 85582 my $code = $zx81chars{$byte};
391 85582 ▶ if ( $code =~ /\w\w$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\w\w$/u) 767 84815 85582
392 767 $code .= " ";
393 }
394 85582 $text .= $code;
395 }
396 else {
397 3863 $text .= sprintf( "\\%02X", $byte );
398 }
399 }
400 86080 return $text;
401 92 };
402 1342 ▶ my $dump = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
403 1342 my ( $addr, $size, $text ) = @_;
404  
405 1342 printf $dump_fh "%04X %s\n", $addr, $text;
406 1342 ▶ for ( my $i = 0 ; $i < $size ; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 13200 1342 14542
407 13200 my $bytes_dump = "";
408 13200 my $text_dump = "";
409 13200 my $num_dumpped = 0;
410 13200 ▶ for ( my $j = 0 ; $j < 8 && $i + $j < $size ; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 8 and $i + $j < $size) 85804 10528 96332
Condition100%3/3
$j < 8 and $i + $j < $size
!ll&&!rl&&r
986466485804
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
411 85804 my $byte = $mem->peek( $addr + $i + $j );
412 85804 $num_dumpped++;
413 85804 $bytes_dump .= sprintf( "%02X ", $byte );
414 85804 $text_dump .= $peek_text->( $addr + $i + $j, 1 ) . " ";
415 85804 ▶ last if $byte == 0x76;
Branch: 1 branch
BranchTrueFalseTotal
if $byte == 118 2672 83132 85804
416 }
417 13200 printf $dump_fh "%04X %-26s %s\n",
418 $addr + $i, $bytes_dump, $text_dump;
419 13200 $i += $num_dumpped;
420 }
421 92 };
422  
423 92 my $prog = $sysvars{PROG};
424 92 my $d_file = $mem->dpeek( $sysvars{D_FILE} );
425 92 my $vars = $mem->dpeek( $sysvars{VARS} );
426 92 my $e_line = $mem->dpeek( $sysvars{E_LINE} );
427  
428 # show system variables
429 92 print $dump_fh "System Variables:\n";
430 92 my %rev_sysvars;
431 92 ▶ while ( my ( $name, $addr ) = each %sysvars ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %sysvars) 3864 92 3956
432 3864 $rev_sysvars{$addr} = $name;
433 }
434 92 ▶ for ( my $addr = $sysvars{VERSN} ; $addr < $prog ; $addr++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr < $prog) 3220 92 3312
435 3220 ▶ if ( $addr == $sysvars{PRBUFF} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($addr == $sysvars{'PRBUFF'}) { } 92 3128 3220
elsif ($addr == $sysvars{'MEMBOT'}) { } 92 3036 3128
elsif (exists $rev_sysvars{$addr + 1}) { } 1196 1840 3036
436  
437 # printer buffer
438 92 $dump->( $addr, 33, "PRBUFF" );
439 92 $addr += 33 - 1;
440 }
441 elsif ( $addr == $sysvars{MEMBOT} ) {
442  
443 # memory calculator area
444 92 ▶ for ( my $i = 0 ; $i < 30 / 5 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 6) 552 92 644
445 552 my @bytes;
446 552 for my $j ( 0 .. 4 ) {
447 2760 push @bytes, $mem->peek( $addr + $i * 5 + $j );
448 }
449 552 my $float = zx81_to_float(@bytes);
450 552 $dump->( $addr + 5 * $i, 5, "MEM[$i] = $float" );
451 }
452  
453 92 $addr += 30 - 1;
454 }
455 elsif ( exists $rev_sysvars{ $addr + 1 } ) {
456  
457 # byte variable
458 printf $dump_fh "%04X %02X %s\n", $addr, $mem->peek($addr),
459 1196 ▶ $rev_sysvars{$addr} // "";
Condition50%1/2
$rev_sysvars{$addr} // ""
l!l
11960
MC/DC0%0/1
$rev_sysvars{$addr} // ""
$rev_sysvars{$addr}
Truth table50%1/2
A
A= $rev_sysvars{$addr}
Aresult
11
00
460 }
461 else {
462 # word variable
463 printf $dump_fh "%04X %04X %s\n", $addr, $mem->dpeek($addr),
464 1840 ▶ $rev_sysvars{$addr} // "";
Condition50%1/2
$rev_sysvars{$addr} // ""
l!l
18400
MC/DC0%0/1
$rev_sysvars{$addr} // ""
$rev_sysvars{$addr}
Truth table50%1/2
A
A= $rev_sysvars{$addr}
Aresult
11
00
465 1840 $addr++;
466 }
467 }
468  
469 # show BASIC program
470 92 print $dump_fh "\nBASIC Program:\n";
471 92 ▶ for ( my $addr = $prog ; $addr < $d_file ; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr < $d_file) 276 92 368
472 276 my $line_num = $mem->dpeek_be($addr);
473 276 my $size = $mem->dpeek( $addr + 2 );
474 276 $dump->(
475 $addr, 4 + $size,
476 "[$size] $line_num " . $peek_text->( $addr + 4, $size - 1 )
477 );
478 276 print $dump_fh "\n";
479  
480 # move to the next line (2 bytes for line number, 2 bytes for size)
481 276 $addr += $size + 4;
482 }
483  
484 # show D_FILE section
485 92 print $dump_fh "\nD_FILE Section:\n";
486 92 $dump->( $d_file, $vars - $d_file, "D_FILE" );
487  
488 # Show VARS section
489 92 print $dump_fh "\nVARS Section:\n";
490 92 my $addr = $vars;
491 92 ▶ while ( $addr < $e_line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr < $e_line) 330 92 422
492 330 my ( $size, $text ) = decode_var( $mem, $addr );
493 330 $dump->( $addr, $size, $text );
494 330 $addr += $size;
495 }
496 }
497  
498 #------------------------------------------------------------------------------
499 # list basic program
500 #------------------------------------------------------------------------------
501 129 ▶sub list_p {
Subroutine list_p: called CC=31 SCAR=34.5
Pod documented
502 129 my ( $p_filename, $bas_filename ) = @_;
503  
504 129 my $bas_fh;
505 129 ▶ if ( defined $bas_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bas_filename) { } 129 0 129
506 129 ▶ open( $bas_fh, ">", $bas_filename )
Branch: 1 branch
BranchTrueFalseTotal
open $bas_fh, ">", $bas_filename or die "error: open ${bas_filename}: $!\n" 129 0 129
507 or die "error: open $bas_filename: $!\n";
508 }
509 else {
510 0 $bas_fh = *STDOUT;
511 }
512  
513 # load memory
514 129 my @bytes = ( map { ord } split //, path($p_filename)->slurp_raw );
515 129 my $mem = ZX81::Tape::Memory->new( $sysvars{VERSN}, @bytes );
516  
517 # dump pragmas
518  
519 # dump AUTOSTART, AUTOSTART_LINE
520 129 my $d_file = $mem->dpeek( $sysvars{D_FILE} );
521 129 my $nxtlin = $mem->dpeek( $sysvars{NXTLIN} );
522 129 ▶ if ( $nxtlin < $d_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nxtlin < $d_file) 21 108 129
523 21 print $bas_fh "#AUTOSTART=1\n";
524 21 print $bas_fh "#AUTOSTART_LINE=" . $mem->dpeek_be($nxtlin) . "\n";
525 }
526  
527 # dump FAST
528 129 ▶ if ( ( $mem->peek( $sysvars{CDFLAG} ) & 0x40 ) == 0x00 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($mem->peek($sysvars{"CDFLAG"}) & 64) == 0) 4 125 129
529 4 print $bas_fh "#FAST=1\n";
530 }
531  
532 # dump VARS
533 129 my $vars = $mem->dpeek( $sysvars{VARS} );
534 129 my $e_line = $mem->dpeek( $sysvars{E_LINE} );
535 129 my $addr = $vars;
536 129 ▶ while ( $addr < $e_line - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr < $e_line - 1) 353 129 482
537 353 my ( $size, $text ) = decode_var( $mem, $addr );
538 353 print $bas_fh "#VARS $text\n";
539 353 $addr += $size;
540 }
541  
542 # dump DFILE, DFILE_COLLAPSED
543 129 my $dfile_collapsed = 0;
544 129 ▶ if ( $vars - $d_file < 1 + 33 * 24 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars - $d_file < 793) 2 127 129
545 2 print $bas_fh "#DFILE_COLLAPSED=1\n";
546 2 $dfile_collapsed = 1;
547 }
548 129 my @dfile_lines = decode_dfile($mem);
549 129 ▶ if ( !$dfile_collapsed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dfile_collapsed) 127 2 129
550 127 for (@dfile_lines) {
551 3048 s/\s+$//; # trim trailing spaces
552 }
553 127 ▶ while ( @dfile_lines && $dfile_lines[-1] eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dfile_lines and $dfile_lines[-1] eq "") 2997 127 3124
Condition100%3/3
@dfile_lines and $dfile_lines[-1] eq ""
!ll&&!rl&&r
12342997
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
554 2997 pop @dfile_lines;
555 }
556 }
557 129 for my $line (@dfile_lines) {
558 75 print $bas_fh "#DFILE=\"$line\"\n";
559 }
560  
561 # dump SYSVARS
562 my @sysvars =
563 129 $mem->peek_bytes( $sysvars{VERSN}, $sysvars{PROG} - $sysvars{VERSN} );
564 print $bas_fh "#SYSVARS="
565 129 . join( ",", map { sprintf "0x%02X", $_ } @sysvars ) . "\n";
566  
567 129 print $bas_fh "\n";
568  
569 # dump BASIC program
570 129 my $prog = $sysvars{PROG};
571 129 ▶ for ( my $addr = $prog ; $addr < $d_file ; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr < $d_file) 384 128 512
572 384 my $line_num = $mem->dpeek_be($addr);
573 384 my $size = $mem->dpeek( $addr + 2 );
574 384 printf $bas_fh "%5d ", $line_num;
575  
576 # rem line with possible binary data
577 384 ▶ if ( $mem->peek( $addr + 4 ) == 0xEA ) { # REM
Branch: 1 branch
BranchTrueFalseTotal
if ($mem->peek($addr + 4) == 234) 52 332 384
578 52 my $has_binary_data = 0;
579 52 ▶ for ( my $i = 1 ; $i < $size - 1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size - 1) 430 24 454
580 430 ▶ if ( ( $mem->peek( $addr + 4 + $i ) & 0x40 ) == 0x40 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($mem->peek($addr + 4 + $i) & 64) == 64) 28 402 430
581 28 $has_binary_data = 1;
582 28 last;
583 }
584 }
585 52 ▶ if ($has_binary_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_binary_data) { } 28 24 52
586 28 print $bas_fh " REM ";
587 28 ▶ for ( my $i = 1 ; $i < $size - 1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size - 1) 171 28 199
588 171 printf $bas_fh "\\%02X", $mem->peek( $addr + 4 + $i );
589 }
590 28 print $bas_fh "\n";
591 }
592 else {
593 24 print $bas_fh " REM ";
594 24 ▶ for ( my $i = 1 ; $i < $size - 1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size - 1) 351 24 375
595 351 my $byte = $mem->peek( $addr + 4 + $i );
596 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
597 351 print $bas_fh $zx81chars{$byte};
598 }
599 else {
600 0 printf $bas_fh "\\%02X", $byte;
601 }
602 }
603 24 print $bas_fh "\n";
604 }
605  
606 52 $addr += 4 + $size;
607 52 next;
608 }
609  
610 # BASIC text
611 332 check_numbers( $mem, $addr + 4, $size - 1 );
612  
613 331 my $in_quote = 0;
614 331 ▶ for ( my $i = 0 ; $i < $size - 1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size - 1) 4897 331 5228
615 4897 my $byte = $mem->peek( $addr + 4 + $i );
616 4897 ▶ if ( $byte == 0x0B ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte == 11) 810 4087 4897
617 810 $in_quote = !$in_quote;
618 }
619  
620 4897 ▶ if ( $byte == 0xC0 ) { # double quote
Branch: 5 branches
BranchTrueFalseTotal
if ($byte == 192) { } 6 4891 4897
elsif ($byte == 126) { } 221 4670 4891
elsif ($in_quote and ($byte & 64) == 64) { } 0 4670 4670
elsif (not $in_quote and $byte == 0) { } 9 4661 4670
elsif (exists $zx81chars{$byte}) { } 4661 0 4661
621 6 print $bas_fh "\\\"";
622 }
623 elsif ( $byte == 0x7E ) { # number
624 221 $i += 5;
625 }
626 ▶ elsif ( $in_quote && ( $byte & 0x40 ) == 0x40 ) {
Condition66%2/3
$in_quote and ($byte & 64) == 64
!ll&&!rl&&r
340212680
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
627 0 printf $bas_fh "\\%02X", $byte;
628 }
629 ▶ elsif ( !$in_quote && $byte == 0 ) {
Condition100%3/3
not $in_quote and $byte == 0
!ll&&!rl&&r
126833939
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
630 9 printf $bas_fh "\\%02X", $byte; # space
631 }
632 elsif ( exists $zx81chars{$byte} ) {
633 4661 my $code = $zx81chars{$byte};
634 4661 ▶ if ( $code =~ /^\w\w/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /^\w\w/u) 405 4256 4661
635 405 $code = " $code ";
636 }
637 4661 print $bas_fh $code;
638 }
639 else {
640 0 printf $bas_fh "\\%02X", $byte;
641 }
642 }
643 331 print $bas_fh "\n";
644  
645 331 $addr += 4 + $size;
646 331 next;
647 }
648 }
649  
650 # check that numbers have the correct floating point representation
651 332 ▶sub check_numbers {
Subroutine check_numbers: called CC=22 SCAR=31.0
Pod undocumented
652 332 my ( $mem, $addr, $size ) = @_;
653  
654 # REM line
655 332 ▶ if ( $mem->peek($addr) == 0xEA ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mem->peek($addr) == 234) 0 332 332
656 0 return;
657 }
658  
659 # scan the line for numbers
660 332 my $p = $addr;
661 332 ▶ while ( $p < $addr + $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $addr + $size) 1930 331 2261
662 1930 my $byte = $mem->peek($p);
663  
664 # quoted string
665 1930 ▶ if ( $byte == 0x0B ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte == 11) 810 1120 1930
666 810 $p++;
667 810 ▶ while ( $p < $addr + $size && $mem->peek($p) != 0x0B ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $addr + $size and $mem->peek($p) != 11) 2467 810 3277
Condition100%3/3
$p < $addr + $size and $mem->peek($p) != 11
!ll&&!rl&&r
967142467
MC/DC100%2/2
$p < $addr + $size and $mem->peek($p) != 11
$p < $addr + $size $mem->peek($p) != 11
Truth table100%3/3
A and B
A= $p < $addr + $sizeB= $mem->peek($p) != 11
ABresult
0X0
100
111
668 2467 $p++;
669 }
670 810 next;
671 }
672  
673 # number
674 1120 ▶ if ( $byte >= 0x1B && $byte <= 0x25 ) { # '.', '0'..'9'
Branch: 1 branch
BranchTrueFalseTotal
if ($byte >= 27 and $byte <= 37) 240 880 1120
Condition100%3/3
$byte >= 27 and $byte <= 37
!ll&&!rl&&r
237643240
MC/DC100%2/2
$byte >= 27 and $byte <= 37
$byte >= 27 $byte <= 37
Truth table100%3/3
A and B
A= $byte >= 27B= $byte <= 37
ABresult
0X0
100
111
675 # collect mantissa
676 240 my $digits = "";
677 240 ▶ while ($p < $addr + $size
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $addr + $size and $mem->peek($p) >= 27 and $mem->peek($p) <= 37) 485 240 725
Condition100%6/6
$p < $addr + $size and $mem->peek($p) >= 27 and $mem->peek($p) <= 37
!ll&&!rl&&r
18222485
$p < $addr + $size and $mem->peek($p) >= 27
!ll&&!rl&&r
315707
MC/DC100%3/3
$p < $addr + $size and $mem->peek($p) >= 27 and $mem->peek($p) <= 37
$p < $addr + $size $mem->peek($p) >= 27 $mem->peek($p) <= 37
Truth table100%4/4
A and B and C
A= $p < $addr + $sizeB= $mem->peek($p) >= 27C= $mem->peek($p) <= 37
ABCresult
0XX0
10X0
1100
1111
678 && $mem->peek($p) >= 0x1B
679 && $mem->peek($p) <= 0x25 )
680 {
681 485 $digits .= $zx81chars{ $mem->peek($p) };
682 485 $p++;
683 }
684  
685 # collect exponent
686 240 ▶ if ( $mem->peek($p) == 0x2A ) { # 'E'
Branch: 1 branch
BranchTrueFalseTotal
if ($mem->peek($p) == 42) 16 224 240
687 16 $digits .= $zx81chars{ $mem->peek($p) };
688 16 $p++;
689  
690 # sign
691 16 ▶ if ( $p < $addr + $size
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $addr + $size and $mem->peek($p) == 21 || $mem->peek($p) == 22) 6 10 16
Condition83%5/6
$p < $addr + $size and $mem->peek($p) == 21 || $mem->peek($p) == 22
!ll&&!rl&&r
0106
$mem->peek($p) == 21 or $mem->peek($p) == 22
l!l&&r!l&&!r
3310
MC/DC66%2/3
$p < $addr + $size and $mem->peek($p) == 21 || $mem->peek($p) == 22
$p < $addr + $size $mem->peek($p) == 21 $mem->peek($p) == 22
Truth table75%3/4
A and B or C
A= $p < $addr + $sizeB= $mem->peek($p) == 21C= $mem->peek($p) == 22
ABCresult
0XX0
1000
11X1
1011
692 && ( $mem->peek($p) == 0x15 || $mem->peek($p) == 0x16 ) )
693 {
694 6 $digits .= $zx81chars{ $mem->peek($p) };
695 6 $p++;
696 }
697 16 ▶ while ($p < $addr + $size
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $addr + $size and $mem->peek($p) >= 28 and $mem->peek($p) <= 37) 16 16 32
Condition50%3/6
$p < $addr + $size and $mem->peek($p) >= 28 and $mem->peek($p) <= 37
!ll&&!rl&&r
01616
$p < $addr + $size and $mem->peek($p) >= 28
!ll&&!rl&&r
0032
MC/DC33%1/3
$p < $addr + $size and $mem->peek($p) >= 28 and $mem->peek($p) <= 37
$p < $addr + $size $mem->peek($p) >= 28 $mem->peek($p) <= 37
Truth table50%2/4
A and B and C
A= $p < $addr + $sizeB= $mem->peek($p) >= 28C= $mem->peek($p) <= 37
ABCresult
0XX0
10X0
1100
1111
698 && $mem->peek($p) >= 0x1C
699 && $mem->peek($p) <= 0x25 )
700 {
701 16 $digits .= $zx81chars{ $mem->peek($p) };
702 16 $p++;
703 }
704 }
705  
706 # collect floating point form
707 240 ▶ if ( $mem->peek($p) == 0x7E ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mem->peek($p) == 126) 222 18 240
708 222 my @fp_bytes;
709 222 for my $i ( 1 .. 5 ) {
710 1110 push @fp_bytes, $mem->peek( $p + $i );
711 }
712  
713 222 $digits = lc($digits);
714 222 my $value1 = eval($digits);
715 222 ▶ $@ and die "invalid number '$digits'";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "invalid number '${digits}'" 0 222 222
716 222 my $value2 = zx81_to_float(@fp_bytes);
717  
718 222 ▶ if ( abs( $value1 - $value2 ) > 1e-10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $value1 - $value2 > "1e-10") 1 221 222
719 1 die
720 "error: wrong floating point representation at address ",
721 sprintf( "0x%04X", $p + 1 ), ": $value1 != $value2\n";
722 }
723  
724 221 $p += 6;
725 }
726 239 next;
727 }
728  
729 # anything else
730 880 $p++;
731 }
732 }
733  
734 #------------------------------------------------------------------------------
735 # decode BASIC display
736 #------------------------------------------------------------------------------
737 129 ▶sub decode_dfile {
Subroutine decode_dfile: called CC=7 SCAR=19.5
Pod undocumented
738 129 my ($mem) = @_;
739 129 my $d_file = $mem->dpeek( $sysvars{D_FILE} );
740 129 my $vars = $mem->dpeek( $sysvars{VARS} );
741  
742 # collect dfile lines
743 129 my @dfile;
744 129 ▶ for ( my $addr = $d_file + 1 ; $addr < $vars ; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr < $vars) 3096 129 3225
745 3096 my $line = "";
746 3096 ▶ for ( my $col = 0 ; $col < 33 ; $col++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < 33) 101067 0 101067
747 101067 my $byte = $mem->peek( $addr++ );
748 101067 ▶ if ( $byte == 0x76 ) { # end of line
Branch: 2 branches
BranchTrueFalseTotal
if ($byte == 118) { } 3096 97971 101067
elsif (exists $zx81chars{$byte}) { } 97971 0 97971
749 3096 last;
750 }
751 elsif ( exists $zx81chars{$byte} ) {
752 97971 $line .= $zx81chars{$byte};
753 }
754 else {
755 0 die "decode_dfile: unexpected byte $byte at $addr";
756 }
757 }
758 3096 push @dfile, $line;
759 }
760  
761 # trim trailing empty lines
762 129 ▶ while ( @dfile && $dfile[-1] eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dfile and $dfile[-1] eq "") 24 129 153
Condition100%3/3
@dfile and $dfile[-1] eq ""
!ll&&!rl&&r
112824
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
763 24 pop @dfile;
764 }
765  
766 129 return @dfile;
767 }
768  
769 #------------------------------------------------------------------------------
770 # decode BASIC vars
771 #------------------------------------------------------------------------------
772 683 ▶sub decode_var {
Subroutine decode_var: called CC=23 SCAR=31.4
Pod undocumented
773 683 my ( $mem, $addr ) = @_;
774 683 my $start_addr = $addr;
775  
776 683 my $byte = $mem->peek( $addr++ );
777 683 ▶ if ( $byte == 0x80 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte == 128) 92 591 683
778  
779 # end marker
780 92 return ( 1, "end marker" );
781 }
782  
783 591 ▶ if ( ( $byte & 0xE0 ) == 0x60 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($byte & 224) == 96) 7 584 591
784  
785 # single letter variable
786 7 $byte = ( $byte & 0x3F ) | 0x20;
787 7 my $name = $zx81chars{$byte};
788  
789 7 my @bytes;
790 7 ▶ for ( my $i = 0 ; $i < 5 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5) 35 7 42
791 35 push @bytes, $mem->peek( $addr++ );
792 }
793 7 my $float = zx81_to_float(@bytes);
794 7 return ( $addr - $start_addr, $name . "=$float" );
795 }
796  
797 584 ▶ if ( ( $byte & 0xE0 ) == 0xA0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($byte & 224) == 160) 557 27 584
798  
799 # multiple letter variable
800 557 $byte = $byte & 0x3F;
801 557 my $name = $zx81chars{$byte}; # first letter
802  
803 557 ▶ while ( ( $mem->peek($addr) & 0xC0 ) == 0x00 )
Branch: 1 branch
BranchTrueFalseTotal
if (($mem->peek($addr) & 192) == 0) 547 557 1104
804 { # second..before-last letters
805 547 $byte = $mem->peek( $addr++ );
806 547 $name .= $zx81chars{$byte}; # second..before-last letters
807 }
808  
809 557 $byte = $mem->peek( $addr++ );
810 557 ▶ ( $byte & 0xC0 ) == 0x80
Branch: 1 branch
BranchTrueFalseTotal
($byte & 192) == 128 or die "decode_var: unexpected byte $byte at $addr" 557 0 557
811 or die "decode_var: unexpected byte $byte at $addr";
812 557 $byte = $byte & 0x3F;
813 557 $name .= $zx81chars{$byte}; # last letter
814  
815 557 my @bytes;
816 557 ▶ for ( my $i = 0 ; $i < 5 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5) 2785 557 3342
817 2785 push @bytes, $mem->peek( $addr++ );
818 }
819 557 my $float = zx81_to_float(@bytes);
820 557 return ( $addr - $start_addr, $name . "=$float" );
821 }
822  
823 27 ▶ if ( ( $byte & 0xE0 ) == 0x80 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($byte & 224) == 128) 7 20 27
824  
825 # numeric array
826 7 $byte = ( $byte & 0x3F ) | 0x20;
827 7 my $name = $zx81chars{$byte};
828  
829 # array size
830 7 my $size = $mem->dpeek($addr);
831 7 $addr += 2;
832  
833 # number of dimensions
834 7 my $num_dims = $mem->peek( $addr++ );
835  
836 # dimensions
837 7 my $num_elements = 1;
838 7 my @dims;
839 7 for ( 1 .. $num_dims ) {
840 14 my $dim = $mem->dpeek($addr);
841 14 $addr += 2;
842 14 push @dims, $dim;
843 14 $num_elements *= $dim;
844 }
845  
846 # values
847 7 my @values;
848 7 for ( 1 .. $num_elements ) {
849 38 my @bytes;
850 38 ▶ for ( my $i = 0 ; $i < 5 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5) 190 38 228
851 190 push @bytes, $mem->peek( $addr++ );
852 }
853 38 my $float = zx81_to_float(@bytes);
854 38 push @values, $float;
855 }
856  
857 # text representation
858 7 my $text =
859 $name . "(" . join( ",", @dims ) . ")=" . join( ",", @values );
860 7 return ( $addr - $start_addr, $text );
861 }
862  
863 20 ▶ if ( ( $byte & 0xE0 ) == 0xE0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($byte & 224) == 224) 6 14 20
864  
865 # loop variable
866 6 $byte = ( $byte & 0x3F ) | 0x20;
867 6 my $name = $zx81chars{$byte};
868  
869 # value, limit, step
870 6 my @values;
871 6 for ( 1 .. 3 ) {
872 18 my @bytes;
873 18 ▶ for ( my $i = 0 ; $i < 5 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5) 90 18 108
874 90 push @bytes, $mem->peek( $addr++ );
875 }
876 18 my $float = zx81_to_float(@bytes);
877 18 push @values, $float;
878 }
879  
880 # line number
881 6 my $line_num = $mem->dpeek($addr);
882 6 $addr += 2;
883 6 push @values, $line_num;
884  
885 # text representation
886 6 my $text = $name . "=" . join( ",", @values );
887  
888 6 return ( $addr - $start_addr, $text );
889 }
890  
891 14 ▶ if ( ( $byte & 0xE0 ) == 0x40 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($byte & 224) == 64) 7 7 14
892  
893 # string variable
894 7 $byte = ( $byte & 0x3F ) | 0x20;
895 7 my $name = $zx81chars{$byte};
896  
897 # string length
898 7 my $length = $mem->dpeek($addr);
899 7 $addr += 2;
900  
901 # string value
902 7 my $value = '"';
903 7 ▶ for ( my $i = 0 ; $i < $length ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length) 35 7 42
904 35 my $byte = $mem->peek( $addr++ );
905 35 ▶ if ( exists $zx81chars{$byte} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $zx81chars{$byte}) { } 35 0 35
906 35 $value .= $zx81chars{$byte};
907 }
908 else {
909 0 die "decode_var: unexpected byte $byte at $addr";
910 }
911 }
912 7 $value .= '"';
913  
914 # text representation
915 7 my $text = $name . '$' . '=' . $value;
916 7 return ( $addr - $start_addr, $text );
917 }
918  
919 7 ▶ if ( ( $byte & 0xE0 ) == 0xC0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($byte & 224) == 192) 7 0 7
920  
921 # string array
922 7 $byte = ( $byte & 0x3F ) | 0x20;
923 7 my $name = $zx81chars{$byte};
924  
925 # array size
926 7 my $size = $mem->dpeek($addr);
927 7 $addr += 2;
928  
929 # number of dimensions
930 7 my $num_dims = $mem->peek( $addr++ );
931  
932 # dimensions
933 7 my $num_elements = 1;
934 7 my @dims;
935 7 for ( 1 .. $num_dims ) {
936 21 my $dim = $mem->dpeek($addr);
937 21 $addr += 2;
938 21 push @dims, $dim;
939 21 $num_elements *= $dim;
940 }
941 7 my $last_dim = $dims[-1];
942 7 $num_elements /= $last_dim;
943  
944 # values
945 7 my @values;
946 7 for ( 1 .. $num_elements ) {
947 38 my $value = '"';
948 38 ▶ for ( my $i = 0 ; $i < $last_dim ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $last_dim) 92 38 130
949 92 my $byte = $mem->peek( $addr++ );
950 92 ▶ if ( exists $zx81chars{$byte} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $zx81chars{$byte}) { } 92 0 92
951 92 $value .= $zx81chars{$byte};
952 }
953 else {
954 0 die "decode_var: unexpected byte $byte at $addr";
955 }
956 }
957 38 $value .= '"';
958 38 push @values, $value;
959 }
960  
961 # text representation
962 7 my $text =
963 $name . '$' . "(" . join( ",", @dims ) . ")=" . join( ",", @values );
964  
965 7 return ( $addr - $start_addr, $text );
966 }
967  
968 0 die "invalid byte $byte at ", $addr - 1;
969 }
970  
971 #------------------------------------------------------------------------------
972 # input BASIC line
973 #------------------------------------------------------------------------------
974 {
975  
976 package ZX81::Tape::Line;
977 117 ▶ use Object::Tiny::RW qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
978 src_file src_line_num
979 line_num label text
980 reminvert
981 bytes
982 117 );
983  
984 1214 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
985 1214 my ($class) = @_;
986 1214 return bless {
987 src_file => '',
988 src_line_num => 0,
989 line_num => -1,
990 label => '',
991 text => '',
992 reminvert => 0,
993 bytes => '',
994 }, $class;
995 }
996  
997 6 ▶ sub error {
Subroutine error: called CC=1 SCAR=0.0
998 6 my ( $self, $message ) = @_;
999 6 die $self->src_file, ":", $self->src_line_num, ": error: ",
1000 $message, "\n";
1001 }
1002 }
1003  
1004 #------------------------------------------------------------------------------
1005 # BASIC variable
1006 #------------------------------------------------------------------------------
1007 {
1008  
1009 117 package ZX81::Tape::Var;
1010 117 ▶ use Object::Tiny::RW qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
1011 name is_string is_array is_loop
1012 dims
1013 value limit step line_num
1014 117 );
1015  
1016 354 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
1017 354 my ($class) = @_;
1018 354 return bless {
1019 name => '',
1020 is_string => 0,
1021 is_array => 0,
1022 is_loop => 0,
1023 dims => [],
1024 value => [0],
1025 limit => 0,
1026 step => 0,
1027 line_num => -1,
1028 }, $class;
1029 }
1030 }
1031  
1032 #------------------------------------------------------------------------------
1033 # Tape class
1034 #------------------------------------------------------------------------------
1035 117 ▶use Object::Tiny::RW qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
1036 lines
1037 labels
1038 asm_lines
1039 asm_labels
1040 asm_bytes
1041 sysvars
1042 vars
1043 dfile
1044 dfile_collapsed
1045 last_line_num
1046 autostart autostart_line
1047 increment
1048 reminvert
1049 fast
1050 117 );
1051  
1052 147 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
1053 147 my ($class) = @_;
1054 147 my $self = bless {
1055 lines => [],
1056 labels => {},
1057 asm_lines => [],
1058 asm_labels => {},
1059 asm_bytes => '',
1060 sysvars => [],
1061 vars => [],
1062 dfile => [],
1063 dfile_collapsed => 0,
1064 last_line_num => -1,
1065 autostart => 0,
1066 autostart_line => 0,
1067 increment => 10,
1068 reminvert => 0,
1069 fast => 0,
1070 }, $class;
1071 147 return $self;
1072 }
1073  
1074 147 ▶sub parse_file {
Subroutine parse_file: called CC=31 SCAR=34.3
Pod undocumented
1075 147 my ( $self, $filename ) = @_;
1076  
1077 # read lines from file
1078 147 my @lines = path($filename)->lines( { chomp => 1 } );
1079  
1080 # add source line numbers
1081 147 for my $i ( 0 .. $#lines ) {
1082 1214 my $line = ZX81::Tape::Line->new();
1083 1214 $line->src_file($filename);
1084 1214 $line->src_line_num( $i + 1 );
1085 1214 $line->text( $lines[$i] );
1086 1214 $lines[$i] = $line;
1087 }
1088  
1089 # join continuation lines
1090 147 ▶ for ( my $i = 0 ; $i < @lines ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines) 1204 147 1351
1091 1204 ▶ while ( $lines[$i]->{text} =~ s/\\$// && $i + 1 < @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[$i]{"text"} =~ s/\\$//u and $i + 1 < @lines) 10 1204 1214
Condition66%2/3
$lines[$i]{"text"} =~ s/\\$//u and $i + 1 < @lines
!ll&&!rl&&r
1204010
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
1092 10 $lines[$i]->text( $lines[$i]->text . $lines[ $i + 1 ]->text );
1093 10 splice( @lines, $i + 1, 1 );
1094 }
1095 }
1096  
1097 # separate ASM and BASIC source
1098 147 my @new_lines;
1099 147 my $in_asm = 0;
1100 147 for my $line (@lines) {
1101 1204 ▶ if ( $line->text =~ /^\s*\#\s*ASM\b/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*ASM\b/u) { } 8 1196 1204
elsif ($line->text =~ /^\s*\#\s*ENDASM\b/u) { } 8 1188 1196
elsif ($in_asm) { } 73 1115 1188
1102 8 $in_asm = 1;
1103 }
1104 elsif ( $line->text =~ /^\s*\#\s*ENDASM\b/ ) {
1105 8 $in_asm = 0;
1106 }
1107 elsif ($in_asm) {
1108 73 push @{ $self->asm_lines }, $line;
1109 }
1110 else {
1111 1115 push @new_lines, $line;
1112 }
1113 }
1114 147 @lines = @new_lines;
1115  
1116 # parse labels and line numbers
1117 147 for my $line (@lines) {
1118 1115 $self->parse_label_line_num($line);
1119 }
1120  
1121 # merge lines with only label or line number with next line
1122 147 ▶ for ( my $i = 0 ; $i < @lines - 1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines - 1) 970 147 1117
1123 970 my $this = $lines[$i];
1124 970 my $next = $lines[ $i + 1 ];
1125 970 ▶ if ( $this->text =~ /^\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->text =~ /^\s*$/u) 81 889 970
1126 81 ▶ my $num_labels =
Branch: 2 branches
BranchTrueFalseTotal
$this->label ne '' ? : 14 67 81
$next->label ne '' ? : 2 79 81
1127 ( $this->label ne '' ? 1 : 0 ) + ( $next->label ne '' ? 1 : 0 );
1128 81 ▶ my $num_line_nums =
Branch: 2 branches
BranchTrueFalseTotal
$this->line_num >= 0 ? : 16 65 81
$next->line_num >= 0 ? : 57 24 81
1129 ( $this->line_num >= 0 ? 1 : 0 ) +
1130 ( $next->line_num >= 0 ? 1 : 0 );
1131 81 ▶ if ( $num_labels <= 1 && $num_line_nums <= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_labels <= 1 and $num_line_nums <= 1) 77 4 81
Condition66%2/3
$num_labels <= 1 and $num_line_nums <= 1
!ll&&!rl&&r
0477
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
1132 77 ▶ $this->label( $next->label ) if $next->label ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $next->label ne "" 2 75 77
1133 77 ▶ $this->line_num( $next->line_num ) if $next->line_num >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $next->line_num >= 0 53 24 77
1134 77 $this->text( $next->text );
1135 77 splice( @lines, $i + 1, 1 );
1136 77 $i--;
1137 }
1138 }
1139 }
1140  
1141 # parse lines
1142 147 ▶ for ( my $i = 0 ; $i < @lines ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines) 1038 143 1181
1143 1038 my $line = $lines[$i];
1144  
1145 # skip blank lines
1146 1038 ▶ if ( $line->text =~ /^\s*$/
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*$/u and $line->label eq "" and $line->line_num < 0) 9 1029 1038
Condition100%6/6
$line->text =~ /^\s*$/u and $line->label eq "" and $line->line_num < 0
!ll&&!rl&&r
102189
$line->text =~ /^\s*$/u and $line->label eq ""
!ll&&!rl&&r
1019217
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
1147 && $line->label eq ''
1148 && $line->line_num < 0 )
1149 {
1150 9 next;
1151 }
1152  
1153 # handle pragmas
1154 1029 ▶ if ( $self->parse_pragma($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parse_pragma($line)) 614 415 1029
1155 614 next;
1156 }
1157  
1158 # add reminvert tag
1159 415 $line->reminvert( $self->reminvert );
1160  
1161 # number the line
1162 415 ▶ if ( $line->line_num >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->line_num >= 0) { } 258 157 415
1163 258 ▶ if ( $line->line_num <= $self->last_line_num ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->line_num <= $self->last_line_num) 1 257 258
1164 1 $line->error( "BASIC lines out of order: "
1165 . $line->line_num
1166 . " after "
1167 . $self->last_line_num );
1168 }
1169 257 $self->last_line_num( $line->line_num );
1170 }
1171 else {
1172 157 ▶ my $new_line_num =
Branch: 1 branch
BranchTrueFalseTotal
$self->last_line_num < 0 ? : 41 116 157
1173 $self->last_line_num < 0
1174 ? $self->increment
1175 : $self->last_line_num + $self->increment;
1176 157 $line->line_num($new_line_num);
1177 157 $self->last_line_num($new_line_num);
1178 }
1179  
1180 414 ▶ if ( $line->line_num > 9999 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->line_num > 9999) 2 412 414
1181 2 $line->error(
1182 "BASIC line number exceeds 9999: " . $line->line_num );
1183 }
1184  
1185 # set the label if present
1186 412 ▶ if ( $line->label ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->label ne "") 22 390 412
1187 22 ▶ if ( exists $self->labels->{ $line->label } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->labels->{$line->label}) 1 21 22
1188 1 $line->error( "duplicate label: " . $line->label );
1189 }
1190 21 $self->labels->{ $line->label } = $line->line_num;
1191 }
1192  
1193 # add line to tape
1194 411 push @{ $self->lines }, $line;
1195 }
1196 }
1197  
1198 1029 ▶sub parse_pragma {
Subroutine parse_pragma: called CC=20 SCAR=30.7
Pod undocumented
1199 1029 my ( $self, $line ) = @_;
1200  
1201 1029 ▶ if ( $line->text =~ /^\s*\#\s*AUTOSTART\s*=\s*(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*AUTOSTART\s*=\s*(\d+)/u) 24 1005 1029
1202 24 ▶ $self->autostart( $1 == 0 ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$1 == 0 ? : 0 24 24
1203 24 return 1;
1204 }
1205 1005 ▶ 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) 13 992 1005
1206 13 $self->autostart_line( 0 + $1 );
1207 13 return 1;
1208 }
1209 992 ▶ if ( $line->text =~ /^\s*\#\s*INCREMENT\s*=\s*(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*INCREMENT\s*=\s*(\d+)/u) 4 988 992
1210 4 $self->increment( 0 + $1 );
1211 4 return 1;
1212 }
1213 988 ▶ if ( $line->text =~ /^\s*\#\s*REMINVERT\s*=\s*(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*REMINVERT\s*=\s*(\d+)/u) 4 984 988
1214 4 ▶ $self->reminvert( $1 == 0 ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$1 == 0 ? : 2 2 4
1215 4 return 1;
1216 }
1217 984 ▶ if ( $line->text =~ /^\s*\#\s*FAST\s*=\s*(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*FAST\s*=\s*(\d+)/u) 6 978 984
1218 6 ▶ $self->fast( $1 == 0 ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$1 == 0 ? : 2 4 6
1219 6 return 1;
1220 }
1221 978 ▶ if ( $line->text =~ /^\s*\#\s*VARS\s*(.*?)\s*=\s*(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*VARS\s*(.*?)\s*=\s*(.*)/u) 354 624 978
1222 354 my $var = $self->parse_var( $line, $1, $2 );
1223 354 ▶ push @{ $self->vars }, $var if defined $var;
Branch: 1 branch
BranchTrueFalseTotal
if defined $var 354 0 354
1224 354 return 1;
1225 }
1226 624 ▶ if ( $line->text =~ /^\s*\#\s*DFILE\s*=\s*\"(.*)\"/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*DFILE\s*=\s*\"(.*)\"/u) 149 475 624
1227 149 push @{ $self->dfile }, $1;
1228 149 return 1;
1229 }
1230 475 ▶ 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 471 475
1231 4 ▶ $self->dfile_collapsed( $1 == 0 ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$1 == 0 ? : 0 4 4
1232 4 return 1;
1233 }
1234 471 ▶ if ( $line->text =~ /^\s*\#\s*SYSVARS\s*=\s*(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*\#\s*SYSVARS\s*=\s*(.*)/u) 54 417 471
1235 54 my $data = $1;
1236 54 my @data = split( /\s*,\s*/, $data );
1237 54 for (@data) {
1238 6264 ▶ /^\$([0-9a-f]+)$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/^\$([0-9a-f]+)$/iu and do { $_ = hex $1; next } 0 6264 6264
1239 0 $_ = hex($1);
1240 0 next;
1241 };
1242 6264 ▶ /^0x([0-9a-f]+)$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/^0x([0-9a-f]+)$/iu and do { $_ = hex $1; next } 6264 0 6264
1243 6264 $_ = hex($1);
1244 6264 next;
1245 };
1246 0 ▶ /^\d+$/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^\d+$/u and do { $_ = 0 + $_; next } 0 0 0
1247 0 $_ = 0 + $_;
1248 0 next;
1249 };
1250 0 $line->error( "invalid sysvar value: " . $_ );
1251 }
1252 54 $self->sysvars( \@data );
1253 54 return 1;
1254 }
1255 417 ▶ if ( $line->text =~ /^\s*#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->text =~ /^\s*#/u) 2 415 417
1256  
1257 # unrecognized pragma, handle as comment
1258 2 return 1;
1259 }
1260  
1261 415 return 0;
1262 }
1263  
1264 354 ▶sub parse_var {
Subroutine parse_var: called CC=23 SCAR=32.2
Pod undocumented
1265 354 my ( $self, $line, $name, $value ) = @_;
1266  
1267 354 my $var = ZX81::Tape::Var->new();
1268  
1269 # collect variable name
1270 354 my $text = $name;
1271 354 ▶ $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) 354 0 354
1272 or $line->error( "invalid variable name: " . $name );
1273 354 $var->name($1);
1274  
1275 # collect '$' for string variable
1276 354 ▶ if ( $text =~ s/^\s*\$// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^\s*\$//u) 8 346 354
1277 8 $var->is_string(1);
1278 }
1279  
1280 # collect array dimensions
1281 354 ▶ if ( $text =~ s/^\s*\((.*)\)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^\s*\((.*)\)//u) 8 346 354
1282 8 $var->is_array(1);
1283 8 my @dims = split( /\s*,\s*/, $1 );
1284 8 for my $dim (@dims) {
1285 20 ▶ if ( $dim !~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dim =~ /^\d+$/u) 0 20 20
1286 0 $line->error( "invalid array dimension: " . $dim );
1287 }
1288 20 push @{ $var->dims }, 0 + $dim;
1289 }
1290 }
1291  
1292 # check for any remaining text in the variable name
1293 354 ▶ if ( $text =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\S/u) 0 354 354
1294 0 $line->error( "invalid variable name: " . $name );
1295 }
1296  
1297 # check variable name length
1298 354 ▶ if ( $var->is_string || $var->is_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->is_string or $var->is_array) 12 342 354
Condition100%3/3
$var->is_string or $var->is_array
l!l&&r!l&&!r
84342
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
1299 12 ▶ if ( length( $var->name ) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $var->name > 1) 0 12 12
1300 0 $line->error( "invalid variable name: " . $name );
1301 }
1302 }
1303  
1304 # compute number of elements needed
1305 354 my $num_elements = 1;
1306 354 my $size_string_element = 0;
1307 354 ▶ if ( $var->is_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->is_array) 8 346 354
1308 8 for my $dim ( @{ $var->dims } ) {
1309 20 $num_elements *= $dim;
1310 }
1311 8 ▶ if ( $var->is_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->is_string) 4 4 8
1312 4 $size_string_element = $var->dims->[-1];
1313 4 $num_elements /= $size_string_element;
1314 }
1315 }
1316  
1317 # collect variable values
1318 354 ▶ if ( !$var->is_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $var->is_string) { } 346 8 354
1319 346 my @values = split( /\s*,\s*/, $value );
1320 346 ▶ if ( @values == 4 && !$var->is_array ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@values == 4 and not $var->is_array) { } 4 342 346
elsif (@values != $num_elements) { } 0 342 342
Condition100%3/3
@values == 4 and not $var->is_array
!ll&&!rl&&r
34114
MC/DC100%2/2
@values == 4 and not $var->is_array
@values == 4 not $var->is_array
Truth table100%3/3
A and not(B)
A= @values == 4B= not $var->is_array
ABresult
0X0
100
111
1321 4 $var->is_loop(1);
1322 }
1323 elsif ( @values != $num_elements ) {
1324 0 $line->error(
1325 "number of values does not match array dimensions: " . $value );
1326 }
1327 346 for (@values) {
1328 376 ▶ if ( $_ !~ /^-?(\d+|\d*\.\d+|\d+\.\d*)(e[-+]?\d+)?$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_ =~ /^-?(\d+|\d*\.\d+|\d+\.\d*)(e[-+]?\d+)?$/iu) 0 376 376
1329 0 $line->error( "invalid variable value: " . $_ );
1330 }
1331 }
1332 346 ▶ if ( $var->is_loop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->is_loop) { } 4 342 346
1333 4 $var->value( [ 0 + $values[0] ] );
1334 4 $var->limit( 0 + $values[1] );
1335 4 $var->step( 0 + $values[2] );
1336 4 $var->line_num( 0 + $values[3] );
1337 }
1338 else {
1339 342 $var->value( [ map { 0 + $_ } @values ] );
1340 }
1341 }
1342 else {
1343 8 $text = $value;
1344 8 my @values;
1345 8 ▶ while ( $text =~ s/^\s*("((?:[^"\\]|\\.)*)")\s*,?// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^\s*("((?:[^"\\]|\\.)*)")\s*,?//u) 26 8 34
1346 26 push @values, $2;
1347 }
1348 8 ▶ if ( @values != $num_elements ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values != $num_elements) 0 8 8
1349 0 $line->error(
1350 "number of values does not match array dimensions: " . $value );
1351 }
1352 8 ▶ if ( $text =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\S/u) 0 8 8
1353 0 $line->error( "invalid string variable value: " . $text );
1354 }
1355 8 $var->value( [@values] );
1356 }
1357  
1358 354 return $var;
1359 }
1360  
1361 1115 ▶sub parse_label_line_num {
Subroutine parse_label_line_num: called CC=5 SCAR=16.1
Pod undocumented
1362 1115 my ( $self, $line ) = @_;
1363  
1364 1115 my $got_any;
1365 1115 while (1) {
1366 1395 ▶ if ( $line->label eq ''
Branch: 2 branches
BranchTrueFalseTotal
if ($line->label eq '' and $line->{'text'} =~ s/^\s*\@([a-z_]\w+)\s*:\s*//iu) { } 22 1373 1395
elsif ($line->line_num < 0 and $line->{'text'} =~ s/^\s*(\d+)\s*//iu) { } 258 1115 1373
Condition100%3/3
$line->label eq "" and $line->{"text"} =~ s/^\s*\@([a-z_]\w+)\s*:\s*//iu
!ll&&!rl&&r
24134922
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
1367 && $line->{text} =~ s/^\s*\@([a-z_]\w+)\s*:\s*//i )
1368 {
1369 22 $line->label($1);
1370 22 $got_any = 1;
1371 }
1372 ▶ 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
258857258
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
1373 258 $line->line_num($1);
1374 258 $got_any = 1;
1375 }
1376 else {
1377 1115 last;
1378 }
1379 }
1380 1115 $line->{text} =~ s/^\s+//;
1381  
1382 1115 return $got_any;
1383 }
1384  
1385 143 ▶sub compile {
Subroutine compile: called CC=14 SCAR=26.4
Pod undocumented
1386 143 my ($self) = @_;
1387  
1388 # remove empty lines
1389 143 @{ $self->lines } = grep { $_->text !~ /^\s*$/ } @{ $self->lines };
1390  
1391 # replace all @labels with line numbers
1392 143 for my $line ( @{ $self->lines } ) {
1393 398 ▶ while ( $line->{text} =~ /\@([a-z_]\w+)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{"text"} =~ /\@([a-z_]\w+)/iu) 15 397 412
1394 15 my $label = $1;
1395 15 ▶ if ( !exists $self->labels->{$label} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->labels->{$label}) 1 14 15
1396 1 $line->error( "undefined label: " . $label );
1397 }
1398 14 my $line_num = $self->labels->{$label};
1399 14 $line->{text} =~ s/\@$label/$line_num/g;
1400 }
1401 }
1402  
1403 # replace &label with line address
1404 142 my %line_addr;
1405 142 my $old_size = 0;
1406 142 ▶ for ( my $pass = 1 ; $pass <= 2 ; $pass++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass <= 2) 414 141 555
1407 414 my $bin = '';
1408  
1409 # assemble ASM code
1410 414 ▶ if ( @{ $self->asm_lines } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->asm_lines;}) 21 393 414
1411 21 my $asm_bytes = $self->assemble_asm(%line_addr);
1412 21 $self->asm_bytes($asm_bytes);
1413 21 $bin .= $asm_bytes;
1414 }
1415  
1416 414 for my $line ( @{ $self->lines } ) {
1417  
1418 # calculate the address of the line
1419 1193 ▶ if ( $line->label ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->label ne "") 56 1137 1193
1420 56 $line_addr{ $line->label } = 0x407D + length($bin);
1421 }
1422  
1423 # replace &label with line address or asm label value
1424 1193 my $text = $line->line_num . " " . $line->text;
1425 1193 ▶ while ( $text =~ /\&([a-z_]\w+)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\&([a-z_]\w+)/iu) 30 1192 1222
1426 30 my $label = $1;
1427 30 ▶ if ( exists $self->labels->{$label} ) { # BASIC label
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->labels->{$label}) { } 14 16 30
elsif (exists $self->asm_labels->{$label}) { } 15 1 16
1428 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
1429 14 $text =~ s/\&$label/$addr/g;
1430 }
1431 elsif ( exists $self->asm_labels->{$label} ) { # ASM label
1432 15 my $value = $self->asm_labels->{$label};
1433 15 $text =~ s/\&$label/$value/g;
1434 }
1435 else {
1436 1 $line->error( "undefined label: " . $label );
1437 }
1438 }
1439  
1440 # apply reminvert if requested
1441 1192 ▶ if ( $line->reminvert ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->reminvert) 24 1168 1192
1442 24 $text = $self->invert_rem($text);
1443 }
1444  
1445 # convert hex notation to decimal
1446 1192 $text =~ s/0x([0-9A-Fa-f]+)/hex($1)/ge;
1447 1192 $text =~ s/\$([0-9A-Fa-f]+)/hex($1)/ge;
1448  
1449 # evaluate expressions in {}
1450 1192 $text =~ s/\{(.*?)\}/eval_expr($1)/ge;
1451  
1452 # convert BASIC to ZX81 tokens
1453 1192 my $line_bytes = tokenize_basic($text);
1454 1192 $line->bytes($line_bytes);
1455 1192 $bin .= $line_bytes;
1456 }
1457  
1458 # check if size changed; recompute if yes
1459 413 my $new_size = length($bin);
1460 413 ▶ if ( $old_size != $new_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_size != $new_size) 131 282 413
1461 131 $old_size = $new_size;
1462 131 $pass = 0; # repeat if size changed
1463 }
1464 }
1465 }
1466  
1467 6 ▶sub eval_expr {
Subroutine eval_expr: called CC=2 SCAR=7.0
Pod undocumented
1468 6 my ($expr) = @_;
1469 6 my $value = eval($expr);
1470 6 ▶ $@ and die "eval '$expr' failed: $@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "eval '${expr}' failed: $@" 0 6 6
1471 6 return $value;
1472 }
1473  
1474 24 ▶sub invert_rem {
Subroutine invert_rem: called CC=4 SCAR=13.9
Pod undocumented
1475 24 my ( $self, $text ) = @_;
1476 24 ▶ if ( $text =~ /^(\s*\d+\s+REM ?)(.*)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^(\s*\d+\s+REM ?)(.*)$/iu) 18 6 24
1477 18 my ( $rem_statement, $rem_text ) = ( $1, $2 );
1478 18 ▶ if ( $rem_text =~ /\\/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rem_text =~ /\\/u) 6 12 18
1479  
1480 # don't invert if there are escape sequences
1481 6 return $text;
1482 }
1483  
1484 # invert (prepend %) to all not inverted characters
1485 12 my @chars = $rem_text =~ /(\%?[^\%])/g;
1486 12 for (@chars) { s/^([^\%])/\%$1/; } # invert C to %C
1487 12 $rem_text = join( '', @chars );
1488  
1489 12 return $rem_statement . $rem_text;
1490 }
1491 6 return $text;
1492 }
1493  
1494 21 ▶sub assemble_asm {
Subroutine assemble_asm: called CC=6 SCAR=17.9
Pod undocumented
1495 21 my ( $self, %line_addr ) = @_;
1496  
1497 21 my @asm_lines;
1498  
1499 # add ORG
1500 21 push @asm_lines, " org 0x407D";
1501  
1502 # add EQU for all BASIC labels
1503 21 for my $label ( sort keys %{ $self->labels } ) {
1504 6 ▶ my $addr = $line_addr{$label} // 0;
Condition100%2/2
$line_addr{$label} // 0
l!l
42
MC/DC100%1/1
$line_addr{$label} // 0
$line_addr{$label}
Truth table100%2/2
A
A= $line_addr{$label}
Aresult
11
00
1505 6 push @asm_lines, "$label equ $addr";
1506 }
1507  
1508 # add 0 REM line
1509 21 push @asm_lines, " defw 0"; # line number
1510 21 push @asm_lines, " defw end_rem0 - start_rem0"; # size
1511 21 push @asm_lines, "start_rem0:";
1512 21 push @asm_lines, " defb 0xEA"; # REM
1513  
1514 # add assembly lines
1515 21 for my $line ( @{ $self->asm_lines } ) {
1516 219 my $text = $line->text;
1517  
1518 # convert strings to zx81 characters
1519 219 ▶ while ( $text =~ /( (['"]) ( (?: [^\\] | \\.)* ) \2 )/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /( (['"]) ( (?: [^\\] | \\.)* ) \2 )/ux) 24 219 243
1520 24 my ( $match, $char ) = ( $1, $3 );
1521 24 my $encoded = join ",", map { ord } split //, encode_chars($char);
1522 24 $text =~ s/$match/$encoded/g;
1523 }
1524  
1525 219 push @asm_lines, $text;
1526 }
1527  
1528 # add newline and end label
1529 21 push @asm_lines, " defb 0x76";
1530 21 push @asm_lines, "end_rem0:";
1531  
1532 # assemble
1533 21 my $program = CPU::Z80::Assembler::Program->new();
1534 21 my $token_stream = z80lexer(@asm_lines);
1535 21 $program->parse($token_stream);
1536 21 my $bytes = $program->bytes();
1537  
1538 # extract asm labels
1539 21 for my $label ( sort keys %{ $program->symbols } ) {
1540 81 my $value = $program->symbols->{$label}->evaluate;
1541 81 $self->asm_labels->{$label} = $value;
1542 }
1543  
1544 21 return $bytes;
1545 }
1546  
1547 141 ▶sub as_bytes {
Subroutine as_bytes: called CC=39 SCAR=36.6
Pod undocumented
1548 141 my ($self) = @_;
1549  
1550 # Convert the stored BASIC lines to a byte string
1551 141 my $prog = $self->asm_bytes;
1552 141 my @line_addr;
1553 141 for my $line ( @{ $self->lines } ) {
1554 396 push @line_addr, [ $line->line_num, $sysvars{PROG} + length($prog) ];
1555 396 $prog .= $line->bytes;
1556 }
1557 141 push @line_addr, [ 10000, $sysvars{PROG} + length($prog) ];
1558  
1559 141 my $prog_start = $sysvars{PROG};
1560  
1561 # Create display file
1562 141 my $dfile = $self->dfile_as_bytes();
1563 141 my $dfile_start = $prog_start + length($prog);
1564  
1565 # Create vars area
1566 141 my $vars = $self->vars_as_bytes();
1567 141 my $vars_start = $dfile_start + length($dfile);
1568 141 my $mem_end = $vars_start + length($vars);
1569  
1570 # init sysvars from defaults
1571 my $sysvars =
1572 141 ZX81::Tape::Memory->new( $sysvars{VERSN}, @{ $self->sysvars } );
1573  
1574 # VERSN
1575 141 $sysvars->poke( $sysvars{VERSN}, 0 );
1576  
1577 # E_PPC
1578 141 ▶ my $e_ppc = $prog eq '' ? 0 : unpack( "n", substr( $prog, 0, 2 ) );
Branch: 1 branch
BranchTrueFalseTotal
$prog eq '' ? : 12 129 141
1579 $sysvars->dpoke( $sysvars{E_PPC},
1580 141 ▶ @{ $self->sysvars } ? $sysvars->dpeek( $sysvars{E_PPC} ) : $e_ppc );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1581  
1582 # D_FILE and DF_CC
1583 141 $sysvars->dpoke( $sysvars{D_FILE}, $dfile_start );
1584 $sysvars->dpoke( $sysvars{DF_CC},
1585 141 @{ $self->sysvars }
1586 ? $sysvars->dpeek( $sysvars{DF_CC} )
1587 141 ▶ : $dfile_start + 1 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1588  
1589 # VARS
1590 141 $sysvars->dpoke( $sysvars{VARS}, $vars_start );
1591  
1592 # DEST
1593 $sysvars->dpoke( $sysvars{DEST},
1594 141 ▶ @{ $self->sysvars } ? $sysvars->dpeek( $sysvars{DEST} ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1595  
1596 # E_LINE
1597 141 $sysvars->dpoke( $sysvars{E_LINE}, $mem_end );
1598  
1599 # CH_ADD
1600 $sysvars->dpoke( $sysvars{CH_ADD},
1601 141 @{ $self->sysvars }
1602 ? $sysvars->dpeek( $sysvars{CH_ADD} )
1603 141 ▶ : $mem_end + 1 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1604  
1605 # X_PTR
1606 $sysvars->dpoke( $sysvars{X_PTR},
1607 141 ▶ @{ $self->sysvars } ? $sysvars->dpeek( $sysvars{X_PTR} ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1608  
1609 # STKBOT and STKEND
1610 $sysvars->dpoke( $sysvars{STKBOT},
1611 141 @{ $self->sysvars }
1612 ? $sysvars->dpeek( $sysvars{STKBOT} )
1613 141 ▶ : $mem_end + 2 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1614 $sysvars->dpoke( $sysvars{STKEND},
1615 141 @{ $self->sysvars }
1616 ? $sysvars->dpeek( $sysvars{STKEND} )
1617 141 ▶ : $mem_end + 2 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1618  
1619 # BREG
1620 $sysvars->poke( $sysvars{BREG},
1621 141 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{BREG} ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1622  
1623 # MEM
1624 $sysvars->dpoke( $sysvars{MEM},
1625 141 @{ $self->sysvars }
1626 ? $sysvars->dpeek( $sysvars{MEM} )
1627 141 ▶ : $sysvars{MEMBOT} );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1628  
1629 # FREE1
1630 $sysvars->poke( $sysvars{FREE1},
1631 141 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{FREE1} ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1632  
1633 # DF_SZ
1634 $sysvars->poke( $sysvars{DF_SZ},
1635 141 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{DF_SZ} ) : 2 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1636  
1637 # S_TOP
1638 $sysvars->dpoke( $sysvars{S_TOP},
1639 141 ▶ @{ $self->sysvars } ? $sysvars->dpeek( $sysvars{S_TOP} ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1640  
1641 # LAST_K and DEBOUNCE
1642 $sysvars->dpoke( $sysvars{LAST_K},
1643 141 ▶ @{ $self->sysvars } ? $sysvars->dpeek( $sysvars{LAST_K} ) : 0xFFFF );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1644 $sysvars->poke( $sysvars{DEBOUNCE},
1645 141 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{DEBOUNCE} ) : 0x0F );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1646  
1647 # MARGIN
1648 $sysvars->poke( $sysvars{MARGIN},
1649 141 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{MARGIN} ) : 55 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1650  
1651 # NXTLIN - define autostart
1652 141 my $nxtlin = $dfile_start;
1653 141 ▶ if ( $self->autostart ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->autostart) 24 117 141
1654  
1655 # get address of first line at or after autostart line
1656 24 my $autostart_line = $self->autostart_line;
1657 24 for my $line_addr (@line_addr) {
1658 34 ▶ if ( $line_addr->[0] >= $autostart_line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_addr->[0] >= $autostart_line) 24 10 34
1659 24 $nxtlin = $line_addr->[1];
1660 24 last;
1661 }
1662 }
1663 }
1664 141 $sysvars->dpoke( $sysvars{NXTLIN}, $nxtlin );
1665  
1666 # OLDPPC
1667 $sysvars->dpoke( $sysvars{OLDPPC},
1668 141 ▶ @{ $self->sysvars } ? $sysvars->dpeek( $sysvars{OLDPPC} ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1669  
1670 # FLAGX
1671 $sysvars->poke( $sysvars{FLAGX},
1672 141 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{FLAGX} ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1673  
1674 # STRLEN
1675 $sysvars->dpoke( $sysvars{STRLEN},
1676 141 ▶ @{ $self->sysvars } ? $sysvars->dpeek( $sysvars{STRLEN} ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1677  
1678 # T_ADDR
1679 $sysvars->dpoke( $sysvars{T_ADDR},
1680 141 ▶ @{ $self->sysvars } ? $sysvars->dpeek( $sysvars{T_ADDR} ) : 0x0C6B );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1681  
1682 # SEED
1683 $sysvars->dpoke( $sysvars{SEED},
1684 141 ▶ @{ $self->sysvars } ? $sysvars->dpeek( $sysvars{SEED} ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1685  
1686 # FRAMES
1687 $sysvars->dpoke( $sysvars{FRAMES},
1688 141 ▶ @{ $self->sysvars } ? $sysvars->dpeek( $sysvars{FRAMES} ) : 0xFFFF );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1689  
1690 # COORDS
1691 $sysvars->poke( $sysvars{COORDS_X},
1692 141 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{COORDS_X} ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1693 $sysvars->poke( $sysvars{COORDS_Y},
1694 141 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{COORDS_Y} ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1695  
1696 # PR_CC
1697 $sysvars->poke( $sysvars{PR_CC},
1698 141 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{PR_CC} ) : 0xBC );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1699  
1700 # S_POSN
1701 $sysvars->poke( $sysvars{S_POSN_COL},
1702 141 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{S_POSN_COL} ) : 33 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1703 $sysvars->poke( $sysvars{S_POSN_ROW},
1704 141 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{S_POSN_ROW} ) : 24 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1705  
1706 # CDFLAG - FAST mode flag
1707 141 ▶ my $cdflag = $self->fast ? 0x00 : 0x40; # SLOW if not fast
Branch: 1 branch
BranchTrueFalseTotal
$self->fast ? : 4 137 141
1708 141 $sysvars->poke( $sysvars{CDFLAG}, $cdflag );
1709  
1710 # PRBUFF
1711 141 for my $i ( 0 .. 31 ) {
1712 $sysvars->poke( $sysvars{PRBUFF} + $i,
1713 4512 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{PRBUFF} + $i ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 1728 2784 4512
1714 }
1715 $sysvars->poke( $sysvars{PRBUFF} + 32,
1716 141 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{PRBUFF} + 32 ) : 0x76 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1717  
1718 # MEMBOT
1719 141 for my $i ( 0 .. 29 ) {
1720 $sysvars->poke( $sysvars{MEMBOT} + $i,
1721 4230 ▶ @{ $self->sysvars } ? $sysvars->peek( $sysvars{MEMBOT} + $i ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 1620 2610 4230
1722 }
1723  
1724 # FREE2
1725 $sysvars->dpoke( $sysvars{FREE2},
1726 141 ▶ @{ $self->sysvars } ? $sysvars->dpeek( $sysvars{FREE2} ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@{$self->sysvars;} ? : 54 87 141
1727  
1728 # Create memory image
1729 141 my $bytes = '';
1730 $bytes .= pack(
1731 "C*",
1732 $sysvars->peek_bytes(
1733 $sysvars{VERSN}, $sysvars{PROG} - $sysvars{VERSN}
1734 )
1735 141 );
1736 141 $bytes .= $prog;
1737 141 $bytes .= $dfile;
1738 141 $bytes .= $vars;
1739  
1740 141 return $bytes;
1741 }
1742  
1743 141 ▶sub dfile_as_bytes {
Subroutine dfile_as_bytes: called CC=4 SCAR=13.9
Pod undocumented
1744 141 my ($self) = @_;
1745  
1746 141 my @lines = @{ $self->dfile };
1747 141 my $bytes = pack( "C", 0x76 );
1748 141 for ( 1 .. 24 ) {
1749 3384 ▶ my $line = shift @lines // '';
Condition100%2/2
shift @lines // ""
l!l
1473237
MC/DC100%1/1
shift @lines // ""
shift @lines
Truth table100%2/2
A
A= shift @lines
Aresult
11
00
1750 3384 ▶ if ( $self->dfile_collapsed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dfile_collapsed) { } 96 3288 3384
1751 96 $line =~ s/\s+$//;
1752 }
1753 else {
1754 3288 $line .= ' ' x 32;
1755 }
1756 3384 $line = substr( $line, 0, 32 );
1757  
1758 3384 $bytes .= pack( "C*", map { ord } split //, encode_chars($line) );
1759 3384 $bytes .= pack( "C", 0x76 );
1760 }
1761  
1762 141 return $bytes;
1763 }
1764  
1765 141 ▶sub vars_as_bytes {
Subroutine vars_as_bytes: called CC=14 SCAR=26.4
Pod undocumented
1766 141 my ($self) = @_;
1767  
1768 141 my $bytes = '';
1769 141 for my $var ( @{ $self->vars } ) {
1770 354 my @name_bytes = map { ord } split //, encode_chars( $var->name );
1771 354 ▶ if ( $var->is_loop ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($var->is_loop) { } 4 350 354
elsif ($var->is_string) { } 8 342 350
1772  
1773 # loop variable
1774 # 111-letter
1775 4 $bytes .= pack( "C", ( $name_bytes[0] & 0x3F ) | 0xE0 );
1776  
1777 # value
1778 4 $bytes .= encode_float( $var->value->[0] );
1779  
1780 # limit
1781 4 $bytes .= encode_float( $var->limit );
1782  
1783 # step
1784 4 $bytes .= encode_float( $var->step );
1785  
1786 # line number
1787 4 $bytes .= pack( "v", $var->line_num );
1788 }
1789 elsif ( $var->is_string ) {
1790 8 ▶ if ( $var->is_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->is_array) { } 4 4 8
1791  
1792 # string array variable
1793 4 my $num_values = 1;
1794 4 for my $dim ( @{ $var->dims } ) {
1795 12 $num_values *= $dim;
1796 }
1797 4 my $last_dim = $var->dims->[-1];
1798 4 $num_values /= $last_dim;
1799  
1800 # 110-letter
1801 4 $bytes .= pack( "C", ( $name_bytes[0] & 0x1F ) | 0xC0 );
1802  
1803 # total size of array in bytes
1804 $bytes .= pack( "v",
1805 4 1 + 2 * scalar( @{ $var->dims } ) +
1806 $last_dim * $num_values );
1807  
1808 # number of dimensions
1809 4 $bytes .= pack( "C", scalar( @{ $var->dims } ) );
1810  
1811 # dimensions
1812 4 for my $dim ( @{ $var->dims } ) {
1813 12 $bytes .= pack( "v", $dim );
1814 }
1815  
1816 # values
1817 4 for ( 0 .. $num_values - 1 ) {
1818 22 my $value = $var->value->[$_];
1819  
1820 # make fixed length
1821 22 $value .= ' ' x $last_dim;
1822 22 $value = substr( $value, 0, $last_dim );
1823  
1824 # convert to bytes
1825 22 my @value_bytes = map { ord } split //,
1826 encode_chars($value);
1827 22 $bytes .= pack( "C*", @value_bytes );
1828 }
1829 }
1830 else {
1831 # string variable
1832 4 my @value_bytes = map { ord } split //,
1833 encode_chars( $var->value->[0] );
1834  
1835 # 010-letter
1836 4 $bytes .= pack( "C", ( $name_bytes[0] & 0x1F ) | 0x40 );
1837  
1838 # length of string
1839 4 $bytes .= pack( "v", scalar(@value_bytes) );
1840  
1841 # string bytes
1842 4 $bytes .= pack( "C*", @value_bytes );
1843 }
1844 }
1845 else {
1846 342 ▶ if ( $var->is_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->is_array) { } 4 338 342
1847  
1848 # numeric array variable
1849 4 my $num_values = 1;
1850 4 for my $dim ( @{ $var->dims } ) {
1851 8 $num_values *= $dim;
1852 }
1853  
1854 # 100-letter
1855 4 $bytes .= pack( "C", ( $name_bytes[0] & 0x1F ) | 0x80 );
1856  
1857 # total size of array in bytes
1858 $bytes .= pack( "v",
1859 4 1 + 2 * scalar( @{ $var->dims } ) + 5 * $num_values );
1860  
1861 # number of dimensions
1862 4 $bytes .= pack( "C", scalar( @{ $var->dims } ) );
1863  
1864 # dimensions
1865 4 for my $dim ( @{ $var->dims } ) {
1866 8 $bytes .= pack( "v", $dim );
1867 }
1868  
1869 # values
1870 4 for ( 0 .. $num_values - 1 ) {
1871 22 $bytes .= encode_float( $var->value->[$_] );
1872 }
1873 }
1874 else {
1875 # numeric variable
1876 338 ▶ if ( @name_bytes == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@name_bytes == 1) { } 4 334 338
1877  
1878 # 011-letter
1879 4 $bytes .= pack( "C", ( $name_bytes[0] & 0x3F ) | 0x60 );
1880 }
1881 else {
1882 # 101-letter, 001-letter, 101-letter
1883 334 $bytes .= pack( "C", ( $name_bytes[0] & 0x3F ) | 0xA0 );
1884 334 for ( 1 .. $#name_bytes - 1 ) {
1885 328 $bytes .= pack( "C", $name_bytes[$_] );
1886 }
1887 334 $bytes .= pack( "C", $name_bytes[-1] | 0x80 );
1888 }
1889  
1890 # pack value
1891 338 $bytes .= encode_float( $var->value->[0] );
1892 }
1893 }
1894 }
1895  
1896 141 $bytes .= pack( "C", 0x80 ); # end of vars
1897 141 return $bytes;
1898 }
1899  
1900 117 1;
1901  
1902 __END__
↑ Summary bin/zx81_make_p »