lib/Language/Zcode/Runtime/Opcodes.pm

filter: Statementstmt 5.3% 22 / 408 Branchbran 0.0% 0 / 190 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 13.1% 5 / 38 Podpod 3.0% 1 / 33 totaltotal 3.7% 28 / 740 CC 118 SCAR 46.8
CC
118
Cov
3%
decode_text
306.0
bracket_var
272.0
get_dictionary_address
182.0
CRAP 107.4
« lib/Language/Zcode/Runtime/Quetzal.pm ↑ Summary lib/Language/Zcode/Runtime/Input.pm »
1 package Language::Zcode::Runtime::Opcodes;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw(@EXPORT @ISA $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 @ISA=qw(Exporter);
8 # Export subs called by external programs
9 1 @EXPORT = qw (
10 decode_text
11 print_addr print_paddr
12 z_read z_tokenise
13 get_parent get_child get_sibling
14 insert_obj remove_obj
15 get_prop put_prop get_next_prop get_prop_addr get_prop_len
16 set_attr clear_attr test_attr
17 z_random
18 z_verify
19
20 bracket_var
21 global_var
22
23 get_object
24 thing_location
25
26 $DEBUG
27 );
28
29 =head1 NAME
30
31 Language::Zcode::Runtime::Opcodes
32
33 =head1 DESCRIPTION
34
35 This module is included by Perl translations of Z files.
36 It includes functionality that'll be the same for every game,
37 namely the implementations of the Z opcodes.
38 Simpler opcode translations are translated into native Perl.
39 sub names are (usually) opcode names prefixed by "z_"
40
41 It also contains other useful subs, like decode_text
42
43 =cut
44
45 =head2 Z_machine
46
47 Build and run the Z-machine
48
49 =cut
50
51 # XXX This should probably be in Language::Zcode::State
52 0 ▶sub Z_machine {
Subroutine Z_machine: not called CC=4 SCAR=30.0
Pod documented
53 0 my %opts = @_;
54 0 $DEBUG = defined $opts{d};
55 0 my ($rows, $columns, $terminal) = @opts{qw(r c t)};
56
57 # read memory (into @PlotzMemory::Memory) and store original dynamic memory
58 0 PlotzMemory::read_memory();
59
60 # Create objects for IO. (Most IO setup happens in setup_IO, which gets
61 # called again for restart/restore))
62 0 Language::Zcode::Runtime::IO::start_IO($rows, $columns, $terminal);
63
64 ###############
65 # The below loop allows the Z-machine to "reboot" if we hit a
66 # restart or restore opcode.
67 0 my ($rebooting, $Z_Result) = (0, 0, "");
68
69 RESTART:
70 0 ▶ do { # 'do {BLOCK} while $foo' runs once before checking loop condition
Branch: 1 branch
BranchTrueFalseTotal
$rebooting and do { do { PlotzMemory::reset_dynamic_memory($Z_Result eq "Restore"); Language::Zcode::Runtime::IO::update_header(); Language::Zcode::Runtime::IO::setup_IO(); my $addr = $main::Constants{"first_instruction_address"} - 1; eval { do { &Language::Zcode::Runtime::State::z_call($addr, scalar [], scalar [], 0, undef); die "Call stack underflow\n" } }; $rebooting = 0; if (($Z_Result = $@) =~ /^Rest(art|ore)$/) { chomp $Z_Result; $rebooting = 1; } elsif ($Z_Result eq "Quit\n") { $Z_Result = ""; } } } 0 0 0
71
72 # Load RAM
73 0 PlotzMemory::reset_dynamic_memory($Z_Result eq "Restore");
74 # Change flags and write them back to memory
75 0 Language::Zcode::Runtime::IO::update_header();
76 # Reset screen etc.
77 0 Language::Zcode::Runtime::IO::setup_IO();
78
79 #####################
80 # Until now we've been "building/booting" the Z-machine. Now run it!
81 # 'main' sub has one byte (num_locals) before first instruction address
82 0 my $addr = $main::Constants{first_instruction_address} - 1;
83 # Empty args will generate the dummy frame in the call stack
84 0 eval {
85 0 &Language::Zcode::Runtime::State::z_call(
86 $addr, [], [], 0, undef);
87 # We should never return from main sub
88 0 die "Call stack underflow\n";
89 };
90
91 #####################
92 # Now decide if we're rebooting or exiting
93 # exit after running program unless we hit a restart or restore opcode
94 0 $rebooting = 0;
95
96 # Catch any "die"s, which we use for restart/restore AND for real errors.
97 0 ▶ if (($Z_Result = $@) =~ /^Rest(art|ore)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (($Z_Result = $@) =~ /^Rest(art|ore)$/) { } 0 0 0
elsif ($Z_Result eq "Quit\n") { } 0 0 0
98 0 chomp($Z_Result);
99 0 $rebooting = 1; # Loop!
100
101 } elsif ($Z_Result eq "Quit\n") { # 'quit' opcode
102 0 $Z_Result = ""; # so we don't die() later
103
104 }
105 # Any other error is a real error; we'll die after cleaning up IO
106 # if main sub ends w/out a 'quit', $Z_Result should be empty
107
108 } while $rebooting;
109
110 ###############
111 # Close fancy IO windows etc.
112 # (But don't allow die() to overwrite the error message we were storing.)
113 0 eval {Language::Zcode::Runtime::IO::clean_IO()}; $Z_Result .= $@;
114
115 0 return $Z_Result;
116
117 }
118
119 # Random number OR seed random number generator
120 # return a random number between 1 and specified number.
121 # With arg 0, seed random number generator, return 0
122 # With arg < 0, seed with that value, return 0
123 # XXX TODO - OR give non-random sequence
124 # XXX TODO - Note that "restart" yields regular randomness again.
125 0 ▶sub z_random {
Subroutine z_random: not called CC=3 SCAR=24.8
Pod undocumented
126 0 my $value = shift;
127 0 my $result = 0;
128 0 ▶ if ($value == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value == 0) { } 0 0 0
elsif ($value < 0) { } 0 0 0
129 # seed the random number generator
130 0 srand();
131 } elsif ($value < 0) {
132 # use specified value as a seed
133 0 srand($value);
134 } else {
135 0 $result = int(rand($value)) + 1;
136 }
137 0 return $result;
138 }
139
140 # verify game image
141 # This is supposed to be a checksum on the file itself, not the current
142 # memory state, so use the original dynamic memory.
143 0 ▶sub z_verify {
Subroutine z_verify: not called CC=1 SCAR=6.9
Pod undocumented
144 0 my $checksum = $main::Constants{file_checksum};
145 0 my $sum = &PlotzMemory::checksum();
146 0 return ($sum == $checksum);
147 }
148
149 # Read and tokenize a command.
150 0 ▶sub z_read {
Subroutine z_read: not called CC=7 SCAR=40.3
Pod undocumented
151 0 my ($text_address, $parse_address, $time, $routine) = @_;
152 0 my $version = $main::Constants{version};
153
154 # sect15.html#read
155 0 my $max_text_length = get_byte_at($text_address);
156 0 ▶ $max_text_length++ if ($version <= 4);
Branch: 1 branch
BranchTrueFalseTotal
if $version <= 4 0 0 0
157
158 # sect15.html#read
159 # XXX ADK confirm this is only v5+
160 0 my $initial_buf;
161 0 ▶ if ($version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version >= 5) 0 0 0
162 # there may be some text already displayed as if we had typed it
163 0 my $initial = get_byte_at($text_address + 1);
164 0 ▶ $initial_buf = get_string_at($text_address + 2, $initial) if $initial;
Branch: 1 branch
BranchTrueFalseTotal
if $initial 0 0 0
165 }
166
167 0 my $s = &Language::Zcode::Runtime::IO::read_command(
168 $max_text_length, $time, $routine, $initial_buf);
169
170 # truncate input if necessary
171 0 $s = substr($s, 0, $max_text_length);
172
173 # Put the string into a buffer
174 0 save_buffer($s, $text_address);
175
176 # Tokenize the string
177 0 ▶ if ($version < 5 || $parse_address) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version < 5 or $parse_address) 0 0 0
Condition0%0/3
$version < 5 or $parse_address
l!l&&r!l&&!r
000
MC/DC0%0/2
$version < 5 or $parse_address
$version < 5 $parse_address
Truth table0%0/3
A or B
A= $version < 5B= $parse_address
ABresult
1X1
011
000
178 # Tell tokenise to use standard dictionary & don't set special flag.
179 0 z_tokenise($text_address, $parse_address, 0, 0, length $s);
180 }
181 # XXX else { print STDERR "Skipping tokenization; test this!\n";}
182
183 # sect15.html#sread; store terminating char ("newline")
184 0 ▶ return(10) if $version >= 5;
Branch: 1 branch
BranchTrueFalseTotal
if $version >= 5 0 0 0
185 }
186
187 0 ▶sub save_buffer {
Subroutine save_buffer: not called CC=3 SCAR=24.8
Pod undocumented
188 # copy the input buffer to story memory.
189 0 my ($buf, $text_address) = @_;
190 0 my $mem_offset;
191 0 $text_address++; # skip max-input-letters byte
192 0 my $len = length $buf;
193 0 my $version = $main::Constants{version};
194 0 ▶ if ($version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version >= 5) 0 0 0
195 0 $PlotzMemory::Memory[$text_address++] = $len;
196 }
197
198 # copy the buffer to memory
199 @PlotzMemory::Memory[$text_address .. $text_address + length($buf) - 1] =
200 0 map {ord} split //, lc $buf;
201
202 # terminate the line
203 0 ▶ $PlotzMemory::Memory[$text_address + length $buf] = 0 if $version <= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $version <= 4 0 0 0
204 }
205
206 0 ▶sub z_tokenise {
Subroutine z_tokenise: not called CC=11 SCAR=48.8
Pod undocumented
207
208 # XXX Spec 'tokenise': If the number of entries is given as -n, then the
209 # interpreter reads this as "n entries unsorted" (for user dictionaries)
210
211 # text_len gets passed in if this sub is called from read()
212 0 my ($text_address, $parse_address, $dict, $flag, $text_len) = @_;
213
214 0 ▶ $dict ||= $main::Constants{dictionary_address};
Condition0%0/3
$dict ||= $main::Constants{"dictionary_address"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$dict ||= $main::Constants{"dictionary_address"}
$dict $main::Constants{"dictionary_address"}
Truth table0%0/3
A ||= B
A= $dictB= $main::Constants{"dictionary_address"}
ABresult
1X1
011
000
215
216 # get token separators
217 0 my $sep_count = $PlotzMemory::Memory[$dict++];
218 0 my %separators;
219 0 for (1..$sep_count) {
220 # Spec 13.2: XXX IRL these are ZSCII not ASCII
221 0 $separators{chr($PlotzMemory::Memory[$dict++])} = 1;
222 }
223
224 # number of bytes for each encoded word
225 # Spec 13.3: this includes the word itself PLUS some data
226 0 my $entry_length = $PlotzMemory::Memory[$dict++];
227 # number of words in the dictionary
228 0 my $entry_count = get_word_at($dict);
229 0 $dict +=2;
230 # Where the entries in the dictionary actually start
231 0 my $word_start = $dict;
232
233 # my $b1 = new Benchmark();
234 0 my $max_tokens = $PlotzMemory::Memory[$parse_address];
235 # pointer to location where token data will be written
236 0 my $token_p = $parse_address + 2;
237
238 #
239 # Step 1: parse out the tokens
240 #
241 0 my $text_p = $text_address + 1;
242 # skip past max bytes enterable
243 0 ▶ if ($main::Constants{version} >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Constants{"version"} >= 5) 0 0 0
244 # needed if called from tokenize opcode (VAR 0x1b)
245 0 ▶ $text_len = $PlotzMemory::Memory[$text_p] unless defined $text_len;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text_len 0 0 0
246 # move pointer past length of entered text.
247 0 $text_p++;
248 }
249 # Number of bytes between start of text_buffer and start of input bytes
250 0 my $offset = $text_p - $text_address;
251
252 # Get entered text from memory
253 my $raw_input = join "",
254 0 map {chr} @PlotzMemory::Memory[$text_p .. $text_p + $text_len - 1];
255 0 my @tokens;
256 # Separate into tokens based on separators or spaces
257 0 my $sep = join "", keys %separators;
258 0 ▶ while ($raw_input =~ /[$sep]|[^ $sep]+/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_input =~ /[$sep]|[^ $sep]+/g) 0 0 0
259 0 push @tokens, [$&, $-[0] + $offset];
260 }
261
262 # printf STDERR "tokens: %s\n", join "/", map {$_->[0]} @tokens;
263
264 #
265 # Step 2: store dictionary addresses for words
266 #
267 0 my $encoded_length = $main::Constants{encoded_word_length};
268 0 my $wrote_tokens = 0;
269 0 my $untrunc_token;
270 0 for (@tokens) {
271 0 my ($token, $offset) = @{$_};
272 # $offset = position in string, but there's 1/2 extra bytes at
273 # beginning of text_buffer
274 0 ▶ if ($wrote_tokens++ < $max_tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrote_tokens++ < $max_tokens) { } 0 0 0
275 0 $untrunc_token = lc($token);
276 0 ▶ $token = substr($token,0,$encoded_length)
Branch: 1 branch
BranchTrueFalseTotal
if length $token > $encoded_length 0 0 0
277 if length($token) > $encoded_length;
278 0 my $addr = get_dictionary_address(
279 $token, $word_start, $entry_count, $entry_length);
280
281 # sect15.html#tokenise:
282 # when $flag is set, don't touch entries not in the dictionary.
283 #print "address is $addr\n";
284 0 ▶ if ($addr || !$flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr or not $flag) 0 0 0
Condition0%0/3
$addr or not $flag
l!l&&r!l&&!r
000
MC/DC0%0/2
$addr or not $flag
$addr not $flag
Truth table0%0/3
A or not(B)
A= $addrB= not $flag
ABresult
1X1
011
000
285 0 $PlotzMemory::Memory[$token_p++] = $addr >> 8;
286 0 $PlotzMemory::Memory[$token_p++] = $addr & 0xff;
287 0 $PlotzMemory::Memory[$token_p++] = length $untrunc_token;
288 0 $PlotzMemory::Memory[$token_p++] = $offset;
289 # printf "Wrote %x %x %d %d\n", @PlotzMemory::Memory[$token_p -4 .. $token_p -1];
290 }
291 } else {
292 0 warn "Too many tokens: ignoring $token\n";
293 # $self->write_text("Too many tokens; ignoring $token");
294 #$self->newline();
295 }
296 }
297
298 # record number of tokens written
299 0 $PlotzMemory::Memory[$parse_address + 1] = $wrote_tokens;
300
301 # my $b2 = new Benchmark();
302 # my $td = timediff($b2, $b1);
303 # printf STDERR "took: %s\n", timestr($td, 'all');
304
305 }
306
307 # Get the dictionary address for the given token.
308 # Spec 13.5: "Words must be in numerical order of the encoded text (when the
309 # encoded text is regarded as a 32 or 48-bit binary number with
310 # most-significant byte first)"
311 0 ▶sub get_dictionary_address {
Subroutine get_dictionary_address: not called CC=13 SCAR=52.0
Pod undocumented
312 0 my ($token, $dict_start, $num_words, $entry_length) = @_;
313 # Contains ugly hacks for non-alphanumeric "words".
314
315 # make sure token is truncated to max length
316 # Note: this is number of Zchars, not characters
317 0 my $word_length = $main::Constants{encoded_word_length};
318
319 # find the word
320 # XXX z_encode_text should call encode_text, too.
321 0 my @encoded = &encode_text($token, $word_length);
322 0 $word_length = $word_length / 3; # encode 3 Z-chars per two-byte word
323 # print map(sprintf("%x ",$_), @encoded), " is $token\n";
324 0 ▶ die "Word @encoded is the wrong length" unless @encoded == $word_length;
Branch: 1 branch
BranchTrueFalseTotal
unless @encoded == $word_length 0 0 0
325
326 0 my $first = ($encoded[0] & 0x7c00) >> 10; # first Z-char
327 # print "First is $first\n";
328 0 my $search_index;
329 my $linear_search;
330 # XXX This'll probably break for special alphabets
331 # space or shift: something special's going on, but we know this
332 # special word should be one of the first few in the dictionary
333 # because all regular (i.e., A0) words have first char 6-31
334 0 ▶ if ($first < 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first < 6) { } 0 0 0
335 0 $search_index = 0;
336 0 $linear_search = 1;
337 } else {
338 # Sneakily guess where to start looking from.
339 # In half the cases, a good first guess can save several binary chops
340 # OTOH, in a number of cases it won't help at all!
341 # XXX $search_index = int(($num_words - 1) * ($first - 6) / 26);
342 0 $search_index = int($num_words/2);
343 0 $linear_search = 0;
344 }
345
346 # In theory, we could optimize by caching dictionary hits.
347 # But since this happens only once per turn, and we've already
348 # got a binary search, and there shouldn't be more than a couple
349 # thousand words (i.e., < 15 searches), it's probably not worth it.
350 # Another option would be caching locations of first letters.
351 0 my ($address, @word, $delta_mult, $delta, $next);
352 0 my $behind = -1;
353 0 my $ahead = $num_words;
354 0 while (1) {
355 0 $address = $dict_start + ($search_index * $entry_length);
356 # printf"Trying %s at $search_index %x\n", decode_text($address),$address;
357 0 my $diff;
358 0 my $p = $address;
359 0 for (0 .. $word_length - 1) {
360 #$word = 256*$PlotzMemory::Memory[$p++] +PlotzMemory::Memory[$p++];
361 # printf'%x_', get_word_at($p);
362 0 ▶ $diff = $encoded[$_] <=> get_word_at($p) and last;
Branch: 1 branch
BranchTrueFalseTotal
$diff = $encoded[$_] <=> get_word_at($p) and last 0 0 0
363 0 $p+=2;
364 }
365 # print "Found ",decode_text($address),"\n" if !$diff;
366 # found the word we're looking for: done
367 0 ▶ return $address unless $diff;
Branch: 1 branch
BranchTrueFalseTotal
unless $diff 0 0 0
368
369 # missed: search further
370 0 ▶ if ($linear_search) {
Branch: 1 branch
BranchTrueFalseTotal
if ($linear_search) { } 0 0 0
371 0 $next = $search_index + 1; # try next word
372 } else {
373 # determine direction we need to search
374 0 ▶ if ($diff < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff < 0) { } 0 0 0
375 # ahead; need to search back
376 0 $delta = int(($search_index - $behind) / 2);
377 0 $ahead = $search_index;
378 } else {
379 # behind; need to search ahead
380 0 $delta = int(($ahead - $search_index) / 2);
381 0 $behind = $search_index;
382 }
383 0 ▶ $delta = 1 if $delta == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delta == 0 0 0 0
384 0 $next = $search_index + ($delta * $diff);
385 }
386
387 0 ▶ if ($next < 0 or $next >= $num_words) {
Branch: 2 branches
BranchTrueFalseTotal
if ($next < 0 or $next >= $num_words) { } 0 0 0
elsif ($next == $ahead or $next == $behind) { } 0 0 0
Condition0%0/3
$next < 0 or $next >= $num_words
l!l&&r!l&&!r
000
MC/DC0%0/2
$next < 0 or $next >= $num_words
$next < 0 $next >= $num_words
Truth table0%0/3
A or B
A= $next < 0B= $next >= $num_words
ABresult
1X1
011
000
388 # out of range
389 0 return 0;
390 ▶ } elsif ($next == $ahead or $next == $behind) {
Condition0%0/3
$next == $ahead or $next == $behind
l!l&&r!l&&!r
000
MC/DC0%0/2
$next == $ahead or $next == $behind
$next == $ahead $next == $behind
Truth table0%0/3
A or B
A= $next == $aheadB= $next == $behind
ABresult
1X1
011
000
391 # word does not exist between flanking words
392 0 return 0;
393 } else {
394 0 $search_index = $next;
395 }
396 }
397 0 die "Finished a while(1) loop in dictionary search?!";
398 }
399
400 ################################################################################
401 # Variables
402
403 # 1 arg - get, 2 args - set to value
404 # Special hack: if second arg is ++ or --, inc/dec the variable.
405 0 ▶sub global_var {
Subroutine global_var: not called CC=5 SCAR=34.0
Pod undocumented
406 0 my ($index, $value) = @_;
407 0 my $i = $main::Constants{global_variable_address} + $index * 2;
408 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
409 0 ▶ if ($value eq "++" || $value eq "--") {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "++" or $value eq "--") 0 0 0
Condition0%0/3
$value eq "++" or $value eq "--"
l!l&&r!l&&!r
000
MC/DC0%0/2
$value eq "++" or $value eq "--"
$value eq "++" $value eq "--"
Truth table0%0/3
A or B
A= $value eq "++"B= $value eq "--"
ABresult
1X1
011
000
410 0 ▶ my $delta = $value eq "++" ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$value eq '++' ? : 0 0 0
411 # XXX could do this without a sub call
412 # (Don't need to &0xffff cuz it's done below)
413 0 $value = global_var($index) + $delta;
414 }
415 # printf("g%x (%d) -> %d\n", $index, $index, $value);
416 0 $PlotzMemory::Memory[$i] = ($value >> 8) & 0xff;
417 0 $PlotzMemory::Memory[$i+1] = $value & 0xff;
418 0 return $value; # may be needed by caller
419 } else {
420 0 return 256*$PlotzMemory::Memory[$i] + $PlotzMemory::Memory[$i + 1];
421 }
422 }
423
424 # Deal with bracketed indirect variables, that can't be computed at compile time
425 # (E.g., load [local1] -> result. See notes to Spec 14)
426 #
427 # Args: number representing a stack/local/global variable,
428 # references to the current local vars & stack,
429 # (optionally) a value
430 # If final arg is given, set the variable to the value (or the stack).
431 # (Special case: if rval is '++' or '--', inc or dec the value/top of stack
432 # Otherwise, return the value of the variable (or top of stack).
433 #
434 # Spec v1.1draft7: indirect variables. If the bracketed thing refers
435 # to the stack, read/write to top of stack; do NOT push/pop the stack.
436 # (Any time we get to this sub, we're guaranteed to be an indirect variable.)
437
438 0 ▶sub bracket_var {
Subroutine bracket_var: not called CC=16 SCAR=56.1
Pod undocumented
439 0 my ($var_num, $locv_ref, $stack_ref, $rval) = @_;
440 0 ▶ die "Non-number passed to bracket_var: '$var_num'"
Branch: 1 branch
BranchTrueFalseTotal
unless $var_num =~ /^\d+$/ 0 0 0
441 unless $var_num =~ /^\d+$/;
442
443 # 0 means the stack
444 0 ▶ if ($var_num == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($var_num == 0) { } 0 0 0
elsif ($var_num >= 1 and $var_num <= 15) { } 0 0 0
elsif ($var_num >= 16 and $var_num <= 255) { } 0 0 0
445 0 ▶ die "Empty stack in bracket_var" unless @$stack_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless @$stack_ref 0 0 0
446 0 ▶ if (!defined $rval) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $rval) { } 0 0 0
elsif ($rval eq '++' or $rval eq '--') { } 0 0 0
447 0 return $$stack_ref[-1];
448 ▶ } elsif ($rval eq "++" || $rval eq "--") {
Condition0%0/3
$rval eq "++" or $rval eq "--"
l!l&&r!l&&!r
000
MC/DC0%0/2
$rval eq "++" or $rval eq "--"
$rval eq "++" $rval eq "--"
Truth table0%0/3
A or B
A= $rval eq "++"B= $rval eq "--"
ABresult
1X1
011
000
449 0 ▶ my $delta = $rval eq "++" ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$rval eq '++' ? : 0 0 0
450 0 $stack_ref->[-1] = ($stack_ref->[-1] + $delta) & 0xffff;
451 } else {
452 0 $stack_ref->[-1] = $rval;
453 }
454
455 # 1-15 means local variables
456 ▶ } elsif ($var_num >=1 && $var_num <=15) {
Condition0%0/3
$var_num >= 1 and $var_num <= 15
!ll&&!rl&&r
000
MC/DC0%0/2
$var_num >= 1 and $var_num <= 15
$var_num >= 1 $var_num <= 15
Truth table0%0/3
A and B
A= $var_num >= 1B= $var_num <= 15
ABresult
0X0
100
111
457 0 $var_num--; # var 1 refers to locv[0]
458 0 ▶ if (!defined $rval) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $rval) { } 0 0 0
elsif ($rval eq '++' or $rval eq '--') { } 0 0 0
459 0 return $locv_ref->[$var_num];
460 ▶ } elsif ($rval eq "++" || $rval eq "--") {
Condition0%0/3
$rval eq "++" or $rval eq "--"
l!l&&r!l&&!r
000
MC/DC0%0/2
$rval eq "++" or $rval eq "--"
$rval eq "++" $rval eq "--"
Truth table0%0/3
A or B
A= $rval eq "++"B= $rval eq "--"
ABresult
1X1
011
000
461 0 ▶ my $delta = $rval eq "++" ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$rval eq '++' ? : 0 0 0
462 0 $locv_ref->[$var_num] = ($locv_ref->[$var_num] + $delta) & 0xffff;
463 } else {
464 0 $locv_ref->[$var_num] = $rval;
465 }
466
467 # 16-255 means global variables
468 ▶ } elsif ($var_num >= 16 && $var_num <= 255) {
Condition0%0/3
$var_num >= 16 and $var_num <= 255
!ll&&!rl&&r
000
MC/DC0%0/2
$var_num >= 16 and $var_num <= 255
$var_num >= 16 $var_num <= 255
Truth table0%0/3
A and B
A= $var_num >= 16B= $var_num <= 255
ABresult
0X0
100
111
469 # If $rval is defined, global_var will set, otherwise, just return value
470 # If $rval is "++" or "--", global_var will handle correctly
471 0 global_var($var_num - 16, $rval);
472
473 } else {
474 0 die "Illegal value '$var_num' passed to indirect_var";
475 }
476 }
477
478 ################################################################################
479 # Objects
480 #
481 # Object table starts with max_properties words of default properties
482 # Then each object has:
483 # - attribute_bytes bytes of attributes
484 # - 1 object pointer parent
485 # - 1 object pointer sibling
486 # - 1 object pointer child
487 # - 1 object pointer prop offset
488
489 # set_ptr/get_ptr uses bytes or words, depending on version
490 1 my $zstrict_warn = 0;
491 0 ▶sub get_object {
Subroutine get_object: not called CC=4 SCAR=30.0
Pod undocumented
492 0 my ($arr) = @_;
493 0 ▶ if (!defined $arr) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arr) 0 0 0
494 0 ▶ warn "\nCalled get/set parent/child/sibling with object 0"
Branch: 1 branch
BranchTrueFalseTotal
unless $zstrict_warn++ 0 0 0
495 unless $zstrict_warn++;
496 0 return 0;
497 }
498 0 ▶ if ($main::Constants{pointer_size} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Constants{'pointer_size'} == 1) { } 0 0 0
499 0 return $PlotzMemory::Memory[$arr];
500 } else {
501 0 return 256*$PlotzMemory::Memory[$arr] +
502 $PlotzMemory::Memory[$arr+1];
503 }
504 }
505
506 0 ▶sub set_object {
Subroutine set_object: not called CC=2 SCAR=17.9
Pod undocumented
507 0 my ($arr, $value) = @_;
508 0 ▶ if ($main::Constants{pointer_size} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Constants{'pointer_size'} == 1) { } 0 0 0
509 0 $PlotzMemory::Memory[$arr] = $value;
510 } else {
511 0 $PlotzMemory::Memory[$arr] = ($value >> 8) & 0xff;
512 0 $PlotzMemory::Memory[$arr+1] = $value & 0xff;
513 }
514 }
515
516 # XXX In theory I could inline this
517 0 ▶sub thing_location {
Subroutine thing_location: not called CC=6 SCAR=37.4
Pod undocumented
518 0 my ($object_id, $thing) = @_;
519 # XXX untested handling for object 0
520 0 ▶ return undef if $object_id == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $object_id == 0 0 0 0
521 # XXX Some way to test for object_id > num_objects?
522 # name & prop come after child, so first do child offset, then do more
523 0 my %ind = (parent => 0, sibling => 1, child => 2, name => 3, prop => 3);
524 0 my %prop = (name => 1, prop => 1);
525 # Start of all objects
526 0 my %c = %main::Constants;
527 # Where object table's attributes start
528 0 my $address = $c{object_table_address} + $c{max_properties} * 2;
529 0 $address += $c{object_bytes}*($object_id-1);
530 0 ▶ return $address if $thing =~ /^attr(ibute)?s?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $thing =~ /^attr(ibute)?s?$/ 0 0 0
531
532 0 ▶ if (exists $ind{$thing}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ind{$thing}) { } 0 0 0
533 0 $address += $c{attribute_bytes};
534 0 $address += $c{pointer_size} * $ind{$thing};
535 #print "address is $address\n";
536 0 ▶ if (exists $prop{$thing}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prop{$thing}) 0 0 0
537 # we're currently at address of properties address, so go there
538 0 $address = 256*$PlotzMemory::Memory[$address] +
539 $PlotzMemory::Memory[$address+1];
540 # name-length-byte, name-length words, properties
541 0 ▶ if ($thing eq "name") {
Branch: 1 branch
BranchTrueFalseTotal
if ($thing eq 'name') { } 0 0 0
542 0 $address++; # address where Z-string starts
543 } else { # "prop"
544 0 my $text_words = $PlotzMemory::Memory[$address];
545 0 $address += $text_words*2 + 1; # address where properties start
546 }
547 }
548 } else {
549 0 die "Bad arg '$thing' to thing_location";
550 }
551 0 return $address;
552 }
553
554 0 ▶sub get_parent { get_object(&thing_location($_[0], 'parent')) }
Subroutine get_parent: not called CC=1 SCAR=6.9
Pod undocumented
555 0 ▶sub get_child { get_object(&thing_location($_[0], 'child')) }
Subroutine get_child: not called CC=1 SCAR=6.9
Pod undocumented
556 0 ▶sub get_sibling { get_object(&thing_location($_[0], 'sibling')) }
Subroutine get_sibling: not called CC=1 SCAR=6.9
Pod undocumented
557
558 0 ▶sub set_parent { set_object(&thing_location($_[0], 'parent'), $_[1]) }
Subroutine set_parent: not called CC=1 SCAR=6.9
Pod undocumented
559 0 ▶sub set_child { set_object(&thing_location($_[0], 'child'), $_[1]) }
Subroutine set_child: not called CC=1 SCAR=6.9
Pod undocumented
560 0 ▶sub set_sibling { set_object(&thing_location($_[0], 'sibling'), $_[1]) }
Subroutine set_sibling: not called CC=1 SCAR=6.9
Pod undocumented
561
562 0 ▶sub remove_obj {
Subroutine remove_obj: not called CC=5 SCAR=34.0
Pod undocumented
563 # remove this object from its parent/sibling chain.
564 0 my ($object) = @_;
565 # TODO change vec(thing_location) in ProtzTranslate to get_parent
566 # since we use it here too? Or use vec here? Same for child/sib
567 # No parent? don't need to remove from anything
568 0 ▶ my $parent = get_parent($object) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = get_parent($object) or return 0 0 0
569
570 0 my $parent_child = get_child($parent);
571 0 my $sibling = get_sibling($object);
572 # get child of old parent
573 0 ▶ if ($parent_child == $object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_child == $object) { } 0 0 0
574 # first child matches: set child of old parent to first sibling
575 # of the object being removed
576 0 set_child($parent, $sibling);
577 } else {
578 # set the "next sibling" pointer of the previous
579 # sibling in the chain to the next sibling of this
580 # object (the one we're removing).
581 # We'd better find the object before the end of the sibling chain
582 0 my $prev_sib = $parent_child;
583 0 my $this_sib = $prev_sib;
584 0 ▶ while ($this_sib != $object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_sib != $object) 0 0 0
585 0 $prev_sib = $this_sib;
586 0 ▶ $this_sib = get_sibling($this_sib) or die
Branch: 1 branch
BranchTrueFalseTotal
$this_sib = get_sibling($this_sib) or die "remove_obj: Failed for object '${object}'. Bad sibling chain?" 0 0 0
587 "remove_obj: Failed for object '$object'. Bad sibling chain?";
588 }
589 0 set_sibling($prev_sib, get_sibling($this_sib));
590 }
591
592 0 set_parent($object, 0);
593 0 set_sibling($object, 0);
594 }
595
596
597 # move object to become the first child of the destination object.
598 0 ▶sub insert_obj {
Subroutine insert_obj: not called CC=4 SCAR=30.0
Pod undocumented
599 0 my ($object, $new_parent) = @_;
600
601 # if object being moved is ID 0, do nothing (bogus object)
602 0 ▶ return unless $object;
Branch: 1 branch
BranchTrueFalseTotal
unless $object 0 0 0
603
604 # unlink o from its parent and siblings
605 0 remove_obj($object);
606
607 # set new o's parent to d
608 0 set_parent($object, $new_parent);
609
610 # Only set children if new parent is not zero
611 0 ▶ if ($new_parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_parent) 0 0 0
612 0 my $old_child = get_child($new_parent);
613 0 set_child($new_parent, $object);
614
615 # If new parent already had children, make them the new siblings of o,
616 # which is now d's "firstborn".
617 0 ▶ set_sibling($object, $old_child) if $old_child;
Branch: 1 branch
BranchTrueFalseTotal
if $old_child 0 0 0
618 }
619 }
620
621 ############################
622 # Attributes
623
624 # return true of specified attribute of this object is set,
625 # false otherwise.
626 # spec 12.3.1.
627 # attributes are numbered from 0 to 31, and stored in 4 bytes,
628 # bits starting at "most significant" and ending at "least".
629 # ie attrib 0 is high bit of first byte, attrib 31 is low bit of
630 # last byte.
631 0 ▶sub test_attr {
Subroutine test_attr: not called CC=1 SCAR=6.9
Pod undocumented
632 0 my ($object_id, $attribute) = @_;
633
634 # which of the 4 or 6 bytes the attribute is in
635 0 my $byte_offset = $attribute / 8;
636 # which bit in the byte (starting at high bit, counted as #0)
637 0 my $bit_position = ($attribute % 8);
638 0 my $bit_shift = 7 - $bit_position;
639 0 my $attr_start = &thing_location($object_id, "attribute");
640 0 my $the_byte = $PlotzMemory::Memory[$attr_start + $byte_offset];
641 # move target bit into least significant byte
642 0 my $the_bit = ($the_byte >> $bit_shift) & 0x01;
643
644 0 return ($the_bit == 1);
645 }
646
647 # set an attribute for an object; spec 12.3.1
648 0 ▶sub set_attr {
Subroutine set_attr: not called CC=1 SCAR=6.9
Pod undocumented
649 0 my ($object_id, $attribute) = @_;
650 0 my $byte_offset = $attribute / 8;
651 0 my $bit_position = $attribute % 8;
652 0 my $bit_shift = 7 - $bit_position;
653 0 my $mask = 1 << $bit_shift;
654 0 my $attr_start = &thing_location($object_id, "attribute");
655 0 $PlotzMemory::Memory[$attr_start + $byte_offset] |= $mask;
656 }
657
658 # clear an attribute for an object; spec 12.3.1
659 0 ▶sub clear_attr {
Subroutine clear_attr: not called CC=1 SCAR=6.9
Pod undocumented
660 0 my ($object_id, $attribute) = @_;
661 0 my $byte_offset = $attribute / 8;
662 0 my $bit_position = ($attribute % 8);
663 0 my $bit_shift = 7 - $bit_position;
664 0 my $mask = ~(1 << $bit_shift); # all 1's except a zero at our bit
665 0 my $attr_start = &thing_location($object_id, "attribute");
666 0 $PlotzMemory::Memory[$attr_start + $byte_offset] &= $mask;
667 }
668
669 ###########################
670 # Properties
671
672 0 ▶sub get_prop_addr {
Subroutine get_prop_addr: not called CC=2 SCAR=17.9
Pod undocumented
673 # Return address of prop data, or 0 if no such data
674 # (Also returns zero for object 0 -- is that spec?)
675 0 my ($object, $prop_num) = @_;
676 0 ▶ return 0 if $prop_num == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $prop_num == 0 0 0 0
677
678 0 my $prop_table = &thing_location($object, 'prop');
679 0 my ($pnum, $paddr, $plen) = &get_prop_info($prop_table, $prop_num);
680 0 return $paddr;
681 }
682
683 0 ▶sub get_prop_len {
Subroutine get_prop_len: not called CC=6 SCAR=37.4
Pod undocumented
684 0 my ($prop_addr) = @_;
685 # Spec version 1.1draft7
686 0 ▶ return 0 if $prop_addr == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $prop_addr == 0 0 0 0
687
688 # Note if the size is a word instead of a byte, we'll get the second
689 # byte first!
690 0 $prop_addr--;
691 0 my $size_byte = $PlotzMemory::Memory[$prop_addr];
692 0 my $plen;
693 # Stolen from get_prop_info - see comments there
694 0 ▶ if ($main::Constants{version} < 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($main::Constants{'version'} < 4) { } 0 0 0
elsif ($size_byte & 128) { } 0 0 0
695 0 $plen = ($size_byte >> 5) + 1;
696 } elsif ($size_byte & 0x80) {
697 # Inform compiles length 64 as length 0. Whatever.
698 0 ▶ $plen = $size_byte & 0x3f || 64;
Condition0%0/2
$size_byte & 63 || 64
l!l
00
MC/DC0%0/1
$size_byte & 63 || 64
$size_byte & 63
Truth table0%0/2
A
A= $size_byte & 63
Aresult
11
00
699 } else {
700 0 ▶ $plen = ($size_byte & 0x40) ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$size_byte & 64 ? : 0 0 0
701 }
702 0 return $plen;
703 }
704
705 # Return the property after prop_num, or first property if prop_num == 0
706 0 ▶sub get_next_prop {
Subroutine get_next_prop: not called CC=3 SCAR=24.8
Pod undocumented
707 0 my ($object, $prop_num) = @_;
708 0 my $prop_table = &thing_location($object, 'prop');
709 # First get property prop_num
710 0 my ($pnum, $paddr, $plen) = &get_prop_info($prop_table, $prop_num);
711 0 ▶ die "Called get_next_prop on non-existent prop '$prop_num', " .
Branch: 1 branch
BranchTrueFalseTotal
unless $pnum 0 0 0
712 "object '$object'\n" unless $pnum;
713 0 ▶ return $pnum if $prop_num == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $prop_num == 0 0 0 0
714
715 # go to end of property prop_num, and call get_prop_info(0) to get next prop
716 0 $paddr += $plen;
717 0 ($pnum, $paddr, $plen) = &get_prop_info($paddr, 0);
718 0 return $pnum;
719 }
720
721 # return this value for this property - default value if this property
722 # doesn't exist in this object
723 0 ▶sub get_prop {
Subroutine get_prop: not called CC=4 SCAR=30.0
Pod undocumented
724 0 my ($object, $prop_num) = @_;
725 0 my $prop_table = &thing_location($object, 'prop');
726 # First get property prop_num
727 0 my ($pnum, $paddr, $plen) = &get_prop_info($prop_table, $prop_num);
728 0 my $value;
729 0 ▶ if ($pnum == 0) { # get word stored at beginnning of object table
Branch: 1 branch
BranchTrueFalseTotal
if ($pnum == 0) 0 0 0
730 0 $plen = 2;
731 # Spec 12.1, 12.2: props are numbered from 1
732 0 $paddr = $main::Constants{object_table_address} + 2 * ($prop_num - 1);
733 }
734
735 0 ▶ if ($plen == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($plen == 1) { } 0 0 0
elsif ($plen == 2) { } 0 0 0
736 0 $value = $PlotzMemory::Memory[$paddr];
737 } elsif ($plen == 2) {
738 0 $value = 256 * $PlotzMemory::Memory[$paddr] +
739 $PlotzMemory::Memory[$paddr+1];
740 } else {
741 0 die "get_prop: Illegal length $plen for obj $object, prop $prop_num\n";
742 }
743
744 0 return $value;
745 }
746
747 # set this property in this object to specified value
748 0 ▶sub put_prop {
Subroutine put_prop: not called CC=4 SCAR=30.0
Pod undocumented
749 0 my ($object, $prop_num, $value) = @_;
750 0 my $prop_table = &thing_location($object, 'prop');
751 # First get property prop_num
752 0 my ($pnum, $paddr, $plen) = &get_prop_info($prop_table, $prop_num);
753 0 ▶ die "put_prop: nonexistent property $prop_num in object $object"
Branch: 1 branch
BranchTrueFalseTotal
unless $pnum 0 0 0
754 unless $pnum;
755 0 ▶ if ($plen == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($plen == 1) { } 0 0 0
elsif ($plen == 2) { } 0 0 0
756 0 $PlotzMemory::Memory[$paddr] = $value & 0xff;
757 } elsif ($plen == 2) {
758 0 $PlotzMemory::Memory[$paddr] = ($value >>8) & 0xff;
759 0 $PlotzMemory::Memory[$paddr +1] = $value & 0xff;
760 } else {
761 0 die "put_prop: Illegal length $plen for obj $object, prop $prop_num\n";
762 }
763 }
764
765 # search in properties starting at arg0 for a specific property arg1
766 # If called with property 0, get next property, whatever number it is
767 # Otherwise, return the requested property, or 0 if it doesn't exist
768 0 ▶sub get_prop_info {
Subroutine get_prop_info: not called CC=10 SCAR=47.0
Pod undocumented
769 # Spec 12.4: properties are stored in descending numerical order
770 # So search until you get property <= search id (if any)
771 0 my ($start_address, $search_id) = @_;
772 0 my $address = $start_address;
773
774 0 my $property_number = 0;
775 0 my $property_length = 0;
776 0 my $property_address = 0;
777 0 my $exists = 0; # did we find something?
778 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$search_id and $search_id < $property_number and do { do { my $size_byte = $PlotzMemory::Memory[$address++]; return 0, 0, 0 if $size_byte == 0; if ($main::Constants{"version"} < 4) { $property_number = $size_byte & 31; $property_length = ($size_byte >> 5) + 1; } else { $property_number = $size_byte & 63; if ($size_byte & 128) { $property_length = $PlotzMemory::Memory[$address++] & 63 || 64; } else { $property_length = $size_byte & 64 ? 2 : 1; } ; } ; $property_address = $address; $address += $property_length } } 0 0 0
Condition0%0/3
$search_id and $search_id < $property_number
!ll&&!rl&&r
000
MC/DC0%0/2
$search_id and $search_id < $property_number
$search_id $search_id < $property_number
Truth table0%0/3
A and B
A= $search_idB= $search_id < $property_number
ABresult
0X0
100
111
779 0 my $size_byte = $PlotzMemory::Memory[$address++];
780 0 ▶ return (0, 0, 0) if $size_byte == 0; # end of property list
Branch: 1 branch
BranchTrueFalseTotal
if $size_byte == 0 0 0 0
781
782 0 ▶ if ($main::Constants{version} < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Constants{'version'} < 4) { } 0 0 0
783 # spec 12.4.1:
784 # 32 times the number of data bytes minus one, plus property number
785 0 $property_number = $size_byte & 0x1f;
786 0 $property_length = ($size_byte >> 5) + 1;
787 } else {
788 # spec 12.4.2:
789 # property number in bottom 6 bits
790 0 $property_number = $size_byte & 0x3f;
791 0 ▶ if ($size_byte & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size_byte & 128) { } 0 0 0
792 # top bit is set, there is a second size byte
793 # length in bottom 6 bits
794 # (Inform compiles length 64 as length 0!)
795 0 ▶ $property_length =
Condition0%0/2
$PlotzMemory::Memory[$address++] & 63 || 64
l!l
00
MC/DC0%0/1
$PlotzMemory::Memory[$address++] & 63 || 64
$PlotzMemory::Memory[$address++] & 63
Truth table0%0/2
A
A= $PlotzMemory::Memory[$address++] & 63
Aresult
11
00
796 $PlotzMemory::Memory[$address++] & 0x3f || 64;
797 } else {
798 0 ▶ $property_length = ($size_byte & 0x40) ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$size_byte & 64 ? : 0 0 0
799 }
800 }
801 0 $property_address = $address;
802 0 $address += $property_length;
803
804 # This was in ME's code. Don't know why it's needed
805 #if ($last_id and $property_number > $last_id) {
806 # this means we are past the end
807 # ...need example case here!
808 #last; }
809 } while ($search_id && $search_id < $property_number);
810
811 0 ▶ if (!$search_id || $property_number == $search_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $search_id or $property_number == $search_id) { } 0 0 0
Condition0%0/3
not $search_id or $property_number == $search_id
l!l&&r!l&&!r
000
MC/DC0%0/2
not $search_id or $property_number == $search_id
not $search_id $property_number == $search_id
Truth table0%0/3
not(A) or B
A= not $search_idB= $property_number == $search_id
ABresult
1X1
011
000
812 0 return ($property_number, $property_address, $property_length);
813 } else {
814 0 return (0,0,0);
815 }
816 }
817
818 ################################################################################
819 # Text
820 # Move this to LZ::Runtime::IO, then we don't need this.
821 1 ▶use constant Z_NEWLINE => 13;
Subroutine BEGIN: called CC=1 SCAR=0.0
822
823 # These are entries 6-31 in the 3 ZSCII alphabets
824 # XXX Versions 5+ may have different alphabet table.
825 # Note that in that case, alpha_table should hold not characters but integers.
826 # Those integers will be ZSCII values, so e.g. if alpha_table[0][0]=155,
827 # then use the first extra character (defined in the Unicode translation table)
828 # when you get Z-character 6
829 1 my @alpha_table = (
830 [ 'a' .. 'z' ],
831 [ 'A' .. 'Z' ],
832 # char 6 means 10-bit ZSCII follows, so we should never translate it
833 [ 'PLOTZ ERROR!', chr(Z_NEWLINE),
834 split//,qq{0123456789.,!?_#'"/\\-:()} ]
835 );
836
837 # decode and return text at this address; see spec section 3
838 # Note that this sub only reads things in low memory!
839 # XXX Differences for versions 1,2
840 0 ▶sub decode_text {
Subroutine decode_text: not called CC=17 SCAR=57.2
Pod undocumented
841 0 my ($address) = @_;
842 0 my $buffer = "";
843
844 0 my ($word, $zshift, $zchar);
845 0 my $alphabet = 0;
846 0 my $abbreviation = 0;
847 0 my $two_bit_code = 0;
848 0 my $two_bit_flag = 0;
849 # XXX HACK!
850 0 my $flen = @PlotzMemory::Memory;
851
852 0 while (1) {
853 0 ▶ last if $address >= $flen;
Branch: 1 branch
BranchTrueFalseTotal
if $address >= $flen 0 0 0
854 # Deal with translation problems of Advent.z5
855 0 ▶ if (!defined $PlotzMemory::Memory[$address]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $PlotzMemory::Memory[$address]) 0 0 0
856 0 warn "Bad address $address to decode_text";
857 return
858 0 }
859 0 $word = 256*$PlotzMemory::Memory[$address++] +
860 $PlotzMemory::Memory[$address++];
861 # spec 3.2
862 0 ▶ for ($zshift = 10; $zshift >= 0; $zshift -= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zshift >= 0) 0 0 0
863 # break word into 3 zcharacters of 5 bytes each
864 0 $zchar = ($word >> $zshift) & 0x1f;
865 0 ▶ if ($two_bit_flag > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($two_bit_flag > 0) { } 0 0 0
elsif ($abbreviation) { } 0 0 0
elsif ($zchar < 6) { } 0 0 0
866 # Ten-bit ZSCII character. spec 3.4
867 0 ▶ if ($two_bit_flag++ == 1) { # middle of char
Branch: 1 branch
BranchTrueFalseTotal
if ($two_bit_flag++ == 1) { } 0 0 0
868 0 $two_bit_code = $zchar << 5; # first 5 bits
869 } else { # end of char
870 0 $two_bit_code |= $zchar; # last 5
871 0 $buffer .= chr($two_bit_code);
872 0 $two_bit_code = $two_bit_flag = 0;
873 }
874 } elsif ($abbreviation) {
875 # synonym/abbreviation; spec 3.3
876 0 my $entry = (32 * ($abbreviation - 1)) + $zchar;
877 # Spec 3.3, 1.2.2: fetch and convert the "word address" of the
878 # given entry in the abbreviations table.
879 # "word address"; only used for abbreviations (packed address
880 # rules do not apply here)
881 my $abbrev_addr = $main::Constants{abbrev_table_address} +
882 0 $entry * 2;
883 0 my $addr = get_word_at($abbrev_addr) * 2;
884 0 my $expanded = decode_text($addr);
885 0 $buffer .= $expanded;
886 #print STDERR "abbrev $abbreviation $expanded\n";
887 0 $abbreviation = 0;
888 } elsif ($zchar < 6) {
889 0 ▶ if ($zchar == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($zchar == 0) { } 0 0 0
elsif ($zchar == 4) { } 0 0 0
elsif ($zchar == 5) { } 0 0 0
elsif ($zchar >= 1 and $zchar <= 3) { } 0 0 0
890 0 $buffer .= " ";
891 } elsif ($zchar == 4) {
892 # spec 3.2.3: shift character; alphabet 1
893 0 $alphabet = 1;
894 } elsif ($zchar == 5) {
895 # spec 3.2.3: shift character; alphabet 2
896 0 $alphabet = 2;
897 ▶ } elsif ($zchar >= 1 && $zchar <= 3) {
Condition0%0/3
$zchar >= 1 and $zchar <= 3
!ll&&!rl&&r
000
MC/DC0%0/2
$zchar >= 1 and $zchar <= 3
$zchar >= 1 $zchar <= 3
Truth table0%0/3
A and B
A= $zchar >= 1B= $zchar <= 3
ABresult
0X0
100
111
898 # spec 3.3: next zchar is an abbreviation code
899 0 $abbreviation = $zchar;
900 }
901 } else {
902 # spec 3.5: convert remaining chars from alpha table
903 0 $zchar -= 6;
904 # convert to string index
905 0 ▶ if ($alphabet != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alphabet != 2) { } 0 0 0
906 0 $buffer .= $alpha_table[$alphabet]->[$zchar];
907 } else {
908 # alphabet 2; some special cases (3.5.3)
909 0 ▶ if ($zchar == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($zchar == 0) { } 0 0 0
elsif ($zchar == 1) { } 0 0 0
910 0 $two_bit_flag = 1;
911 } elsif ($zchar == 1) {
912 # XXX from rezrov
913 0 $buffer .= chr(Z_NEWLINE);
914 # $buffer .= "\n";
915 } else {
916 0 $buffer .= $alpha_table[$alphabet]->[$zchar];
917 }
918 }
919 # XXX applies to this character for version > 2 (3.2.3)
920 0 $alphabet = 0; # turn "Shift" off
921 }
922 }
923 # Last bit set
924 0 ▶ last if $word & 0x8000;
Branch: 1 branch
BranchTrueFalseTotal
if $word & 32768 0 0 0
925 }
926
927 0 return $buffer;
928 }
929
930 # Spec 3.7: When an interpreter is encrypting typed-in text to match against
931 # dictionary words, the following restrictions apply. Text should be converted
932 # to lower case (as a result A1 will not be needed unless the game provides its
933 # own alphabet table). Abbreviations may not be used. The pad character, if
934 # needed, must be 5. The total string length must be 6 Z-characters (in
935 # Versions 1 to 3) or 9 (Versions 4 and later): any multi-Z-character
936 # constructions should be left incomplete (rather than omitted) if there's no
937 # room to finish them.
938
939 # Default encoding (not counting alphabet table). ZSCII is mostly ASCII
940 # '5' means A2, '6' in A2 means next two 5-bit numbers are 10-bit ZSCII char.
941 my %encode_table = (
942 1 (map {chr, [5, 6, $_>>5, $_&0x1f]} 32..126),
943 " " => [0],
944 "\n" => [13],
945 );
946
947 # Return an array that encodes the given string in exactly the given length
948 # (I.e., chop if too long, pad with shift chars (5) if too short
949 0 ▶sub encode_text {
Subroutine encode_text: not called CC=7 SCAR=40.3
Pod undocumented
950 # Length of end-product in bytes (not number of bytes to encode)
951 0 my ($text, $entry_length) = @_;
952 0 $text = lc $text;
953 0 my $c = 6;
954 # Create the encoding table
955 0 for (@{$alpha_table[0]}) {
956 0 $encode_table{$_} = [$c++];
957 }
958 0 $c = 6;
959 # shift to alphabet 1, letter
960 0 for (@{$alpha_table[1]}) {
961 0 $encode_table{$_} = [4, $c++];
962 }
963 # shift to alphabet 2, letter
964 0 $c = 6;
965 0 for (@{$alpha_table[2]}) {
966 0 $encode_table{$_} = [5, $c++];
967 }
968
969 # Spec 3.8.4:
970 # Defined for input only
971 # 129: cursor up 130: cursor down 131: cursor left 132: cursor right
972 # 133: f1 134: f2 .... 144: f12
973 # 145: keypad 0 146: keypad 1 .... 154: keypad 9
974 # XXX TODO
975
976 # Spec 3.8.5
977 # Special characters 155-251
978 # XXX TODO
979
980 # Spec 3.8.6:
981 # ZSCII codes 252 to 254 are defined for input only:
982 # 252: menu click 253: mouse double-click 254: mouse single-click
983 #
984 # Menu clicks are available only in Version 6. In Versions 5 and later it
985 # is recommended that an interpreter should only send code 254, whether the
986 # mouse is clicked once or twice.
987 # TODO
988
989 die "Illegal to create a Z-char > 31" if
990 0 ▶ grep {$_ > 31} map {@$_} values %encode_table;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ > 31;} map({@$_;} values %encode_table) 0 0 0
991
992 # Encode each letter
993 # XXX die if unknown char?
994 0 my @zchar = map {@{$encode_table{$_}}} split //, $text;
995 # pad w/ shift chars as necessary
996 0 push @zchar, (5) x $entry_length;
997 # print "Chars @zchar\n";
998 # Cut to correct length, breaking multi-byte chars in the middle.
999 0 $#zchar = $entry_length - 1;
1000
1001 # Turn into 5-bit chars
1002 0 ▶ die "Need a length that's a multiple of 3: $entry_length"
Branch: 1 branch
BranchTrueFalseTotal
if $entry_length % 3 0 0 0
1003 if $entry_length %3;
1004 0 my @words = ();
1005 0 my $n = 0;
1006 0 for (0 .. ($entry_length/3) - 1) {
1007 0 my $word = ($zchar[$n++] << 10) + ($zchar[$n++] << 5) + $zchar[$n++];
1008 0 push @words, $word;
1009 }
1010 0 $words[-1] |= (1 << 15);
1011
1012 0 return @words;
1013 }
1014
1015 0 ▶sub get_byte_at {
Subroutine get_byte_at: not called CC=2 SCAR=17.9
Pod undocumented
1016 0 my ($arr, $index) = @_;
1017 0 ▶ $index = 0 if !defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 0 0
1018 0 return $PlotzMemory::Memory[$arr + $index];
1019 }
1020
1021 0 ▶sub get_word_at {
Subroutine get_word_at: not called CC=2 SCAR=17.9
Pod undocumented
1022 0 my ($arr, $index) = @_;
1023 0 ▶ $index = 0 if !defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 0 0
1024 0 return 256*$PlotzMemory::Memory[$arr + $index] +
1025 $PlotzMemory::Memory[$arr + $index + 1];
1026 }
1027
1028 1 1;
« lib/Language/Zcode/Runtime/Quetzal.pm ↑ Summary lib/Language/Zcode/Runtime/Input.pm »