lib/Language/Zcode/Runtime/Quetzal.pm

filter: Statementstmt 26.2% 21 / 80 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 70.0% 7 / 10 Podpod n/a totaltotal 23.3% 28 / 120 CC 16 SCAR 47.8
CC
16
Cov
19%
parse_quetzal
210.0
read_stacks
12.0
read_header
2.0
CRAP 119.4
« lib/Language/Zcode/Translator/PIR.pm ↑ Summary lib/Language/Zcode/Runtime/Opcodes.pm »
1 package Language::Zcode::Runtime::Quetzal;
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
6 =head1 NAME
7
8 Language::Zcode::Runtime::Quetzal - Save/restore Z-machine state using the Quetzal standard
9
10 =cut
11
12 1 ▶use constant QUETZAL_SUB_ID => "IFZS";
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use constant QUETZAL_STACK => "Stks";
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant QUETZAL_HEADER => "IFhd";
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant QUETZAL_COMPRESSED_MEMORY => "CMem";
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant QUETZAL_UNCOMPRESSED_MEMORY => "UMem";
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 0 ▶sub parse_quetzal {
Subroutine parse_quetzal: not called CC=14 SCAR=53.5
19 0 my $st = shift; # string containing entire quetzal file
20 0 my $pos = 0; # position in $st
21
22 # Stuff we'll be returning
23 0 my ($memory_ref, @call_stack, %header);
24
25 # Read very beginning of save file
26 0 ▶ my $t = substr($st, 0, 4); die "Not an IFF file!\n" unless $t eq "FORM";
Branch: 1 branch
BranchTrueFalseTotal
unless $t eq "FORM" 0 0 0
27 0 $t = substr($st, 4, 4);
28 0 my $size = unpack "N", $t; print "size $size\n";
29 0 ▶ die "Not a simple IFF file!\n" unless $size == length($st)-8;
Branch: 1 branch
BranchTrueFalseTotal
unless $size == length($st) - 8 0 0 0
30 0 $t = substr($st, 8, 4);
31 0 ▶ die "Not a Quetzal save!" unless $t eq QUETZAL_SUB_ID;
Branch: 1 branch
BranchTrueFalseTotal
unless $t eq "IFZS" 0 0 0
32
33 # Read a set of chunks and parse them
34 0 $pos = 12;
35 0 my $did_header = 0;
36 0 my ($name, $data);
37 0 ▶ while ($pos < $size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $size) 0 0 0
38 # Read the chunk
39 0 my $len;
40 #print "Pos $pos\n";
41 0 ($name, $len) = unpack "A4 N", substr($st, $pos, 8); $pos += 8;
42 0 my $data = substr($st, $pos, $len); $pos += $len;
43 # print "length $len - ",length $data," - ", length $st,"\n";
44 0 ▶ if ($len % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len % 2) 0 0 0
45 0 ▶ substr($st, $pos, 1) eq "\0" or die "expecting 0 at $pos!\n"; $pos++
Branch: 1 branch
BranchTrueFalseTotal
substr($st, $pos, 1) eq "\0" or die "expecting 0 at $pos!\n" 0 0 0
46 }
47 0 print "$name, ",length($data),"\n";
48
49 # Do stuff based on chunk
50 # Quetzal Spec 5.4: Header info MUST come before Mem/Stacks
51 0 ▶ if ($name eq QUETZAL_HEADER) {
Branch: 4 branches
BranchTrueFalseTotal
if ($name eq 'IFhd') { } 0 0 0
elsif ($name eq 'Stks') { } 0 0 0
elsif ($name eq 'CMem') { } 0 0 0
elsif ($name eq 'UMem') { } 0 0 0
52 0 %header = read_header($data);
53 0 $did_header = 1;
54
55 } elsif ($name eq QUETZAL_STACK) {
56 0 ▶ die QUETZAL_HEADER . " chunk must come before stack chunk " .
Branch: 1 branch
BranchTrueFalseTotal
unless $did_header 0 0 0
57 "in save file\n" unless $did_header;
58 0 @call_stack = read_stacks($data);
59
60 } elsif ($name eq QUETZAL_COMPRESSED_MEMORY) {
61 0 ▶ die QUETZAL_HEADER . " chunk must come before memory chunk " .
Branch: 1 branch
BranchTrueFalseTotal
unless $did_header 0 0 0
62 "in save file\n" unless $did_header;
63 0 $memory_ref = read_cmem($data);
64 } elsif ($name eq QUETZAL_UNCOMPRESSED_MEMORY) {
65 0 ▶ die QUETZAL_HEADER . " chunk must come before memory chunk " .
Branch: 1 branch
BranchTrueFalseTotal
unless $did_header 0 0 0
66 "in save file\n" unless $did_header;
67 0 $memory_ref = read_umem($data);
68 0 } else { print "Ignoring $name chunk\n"; }
69 }
70
71 0 return ($memory_ref, \@call_stack, \%header);
72 }
73
74 ######################
75
76 0 ▶sub read_header {
Subroutine read_header: not called CC=1 SCAR=6.9
77 0 my $data = shift;
78 0 my ($release, $serial, $checksum, $PC1, $PC2) = unpack
79 "n A6 n n C",
80 substr($data, 0, 13);
81 0 my $PC = 256 * $PC1 + $PC2;
82 0 my %header = (
83 release => $release,
84 serial => $serial,
85 checksum => $checksum,
86 restore_PC => $PC,
87 );
88 0 return %header;
89 }
90
91 0 ▶sub read_stacks {
Subroutine read_stacks: not called CC=3 SCAR=24.8
92 0 my $data = shift;
93 0 my @call_stack = (); # stack of frames
94 # print join " ", unpack "C*", $data;
95 0 my $p = 0;
96 0 ▶ while ($p < length $data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < length $data) 0 0 0
97 0 my ($PC1, $PC2, $args, $stack_size, $store, $flags);
98 0 ($PC1, $PC2, $flags, $store, $args, $stack_size) = unpack
99 "n C C C B8 n",
100 0 substr($data, $p, 8); $p += 8;
101 0 my @args = split//,$args;
102 0 my $PC = 256 * $PC1 + $PC2;
103 0 my $discard_result = int($flags >> 4);
104 0 my $num_locals = $flags & 0xf; # just last four bits
105 0 ▶ die "Bad result-discard flag $discard_result" unless $discard_result<2;
Branch: 1 branch
BranchTrueFalseTotal
unless $discard_result < 2 0 0 0
106 0 my @locv = unpack "n*", substr($data, $p, $num_locals*2);
107 0 $p += $num_locals*2;
108 0 my @stack = unpack "n*", substr($data, $p, $stack_size*2);
109 0 $p += $stack_size*2;
110
111 0 my $frame_ref = {
112 next_PC => $PC,
113 discard_bit => $discard_result,
114 store_var => $store,
115 args => join("",@args),
116 locals => \@locv,
117 eval_stack => \@stack,
118 };
119 0 push @call_stack, $frame_ref;
120 }
121 0 return @call_stack;
122 }
123
124 sub read_cmem {
125 my $data = shift;
126 # "0 n" means n PLUS ONE zeros
127 (my $diff = $data) =~ s/(\0)(\C)/$1 . $1 x ord $2/ge;
128 # bitwise xor with original memory
129 # ("" says they're really strings, not nums, so do char-by-char or'ing)
130 my $dynamic_orig = pack "C*", @{&PlotzMemory::get_orig_dynamic_memory};
131 my $memory = "$diff" ^ "$dynamic_orig";
132
133 # $diff =~ s/\C/sprintf("%3d", ord $&)/ge;
134 # print join "\n", $diff =~ /(.{48})/g,$',"";
135 # Note: memory size may be smaller than dynamic memory read in.
136 # In that case, Quetzal 3.4, the rest of @mem is all zeros
137 my @mem = unpack "C*", $memory;
138
139 return \@mem;
140 }
141
142 sub read_umem {
143 my $data = shift;
144 my @mem = unpack "C*", $data;
145 return \@mem;
146 }
147
148 ######################
149 # Input: header, stack, memory for quetzal
150 # Output: string that will be a quetzal file
151 sub build_quetzal {
152 my ($href, $sref, $mref) = @_;
153 my $collect = QUETZAL_SUB_ID;
154 $collect .= write_header(QUETZAL_HEADER, $href);
155 $collect .= write_memory(QUETZAL_COMPRESSED_MEMORY, $mref);
156 $collect .= write_stacks(QUETZAL_STACK, $sref);
157
158 #print "length collect is ",length $collect,"\n";
159 # Add overall header, which includes the size of the rest of the file
160 my $IFF = "FORM" . pack("N", length $collect);
161 $IFF .= $collect;
162 return $IFF;
163 }
164
165 sub write_chunk {
166 my ($name, $data) = @_;
167 my $len = length $data;
168 #print "Pos $pos\n";
169 my $str = pack "A4 N", $name, $len;
170 $str .= $data;
171 $str .= "\0" if $len % 2;
172 return $str;
173 }
174
175 sub write_header {
176 my ($name, $href) = @_;
177 my %header = %$href;
178 printf "Rel: %s, Ser#: %s, Check: %s, RestorePC: %s (%x)\n",
179 @header{qw(release serial checksum restore_PC restore_PC)}
180 if $main::DEBUG;
181 my ($release, $serial, $checksum, $PC) =
182 @header{qw(release serial checksum restore_PC)};
183 my $PC1 = $PC >> 8;
184 my $PC2 = $PC & 0xFF;
185 my $data = pack
186 "n A6 n n C",
187 $release, $serial, $checksum, $PC1, $PC2;
188 my $str = write_chunk($name, $data);
189 return $str;
190 }
191
192 sub write_stacks {
193 my ($name, $sref) = @_;
194 my $data = "";
195 foreach my $frame (@$sref) {
196 my %f = %$frame;
197 printf "PC %s (%x), call_*n? %s, store %s, args %s, %s locals, stack size %s
198 Locals @{$f{locals}}
199 Stack @{$f{eval_stack}}\n",
200 @f{qw(next_PC next_PC discard_bit store_var args)},
201 $#{$f{locals}} +1, $#{$f{eval_stack}} +1
202 if $main::DEBUG;
203
204 my ($PC, $discard_bit, $store, $args, $locref, $stackref) =
205 @f{qw(next_PC discard_bit store_var args locals eval_stack)};
206 die "Bad result-discard flag $discard_bit" unless $discard_bit<2;
207 my @split_args = split//,$args; # we don't actually need this
208 my @locv = @$locref;
209 my @stack = @$stackref;
210
211 my ($PC1, $PC2, $flags, $stack_size, $num_locals);
212 $PC1 = $PC >> 8;
213 $PC2 = $PC & 0xFF;
214 $stack_size = @stack;
215 $num_locals = @locv;
216 die "Number of locals must be < 16" unless $num_locals < 16;
217 $flags = ($discard_bit << 4) | $num_locals;
218
219 my $frame_data = pack
220 "n C C C B8 n",
221 $PC1, $PC2, $flags, $store, $args, $stack_size;
222 $frame_data .= pack "n*", @locv, @stack;
223 $data .= $frame_data;
224 }
225 my $str = write_chunk($name, $data);
226 return $str;
227 }
228
229 # write COMPRESSED memory
230 sub write_memory {
231 my ($name, $mem_ref) = @_;
232 # memory at time of save
233 my $memory = pack "C*", @$mem_ref;
234 # bitwise xor with original memory
235 # ("" to specify they're really strings, not nums)
236 my $dynamic_orig = pack "C*", @{&PlotzMemory::get_orig_dynamic_memory};
237 my $diff = "$memory" ^ "$dynamic_orig";
238 # Remove trailing zeros (which show no change from orig memory
239 # beyond a certain point)
240 $diff =~ s/\0+$//;
241
242 # Compress
243 (my $data = $diff) =~ s/\0{1,256}/"\0" . chr(length($&)-1)/ge;
244
245 # $memory =~ s/\C/sprintf("%3d", ord $&)/ge;
246 # print join "\n", $memory =~ /(.{48})/g,$',"";
247 # for (my $i=0; $i < @mem; $i+=16) {
248 # my $m = $i>@mem-16 ? $#mem : $i+15;
249 # printf "%3d"x($m-$i +1) ."\n", @mem[$i..$m];
250 # }
251 # print "@mem\n";
252
253 my $str = write_chunk($name, $data);
254 return $str;
255 }
256
257 1;
« lib/Language/Zcode/Translator/PIR.pm ↑ Summary lib/Language/Zcode/Runtime/Opcodes.pm »