blib/lib/ZX81/Tape.pm

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