ZHeader.pm

filter: Statementstmt 77.5% 180 / 232 Branchbran 22.0% 11 / 50 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 89.5% 43 / 48 Podpod 0.0% 0 / 7 totaltotal 65.6% 237 / 361 CC 31 SCAR 38.1
CC
31
Cov
65%
new
134.9
set_columns
12.0
set_rows
12.0
CRAP 45.0
« ZIO_Win32.pm ↑ Summary ZInterpreter.pm »
1 # ZHeader: version-specific information and settings
2 # for each game
3  
4 package Games::Rezrov::ZHeader;
5  
6 1 ▶use Games::Rezrov::ZConst;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Games::Rezrov::Inliner;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use constant FLAGS_1 => 0x01; # one byte
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant FLAGS_2 => 0x10; # TWO BYTES
Subroutine BEGIN: called CC=1 SCAR=0.0
13 # location of various flags in the header
14  
15 # see spec section 11:
16 1 ▶use constant RELEASE_NUMBER => 0x02;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant PAGED_MEMORY_ADDRESS => 0x04;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant FIRST_INSTRUCTION_ADDRESS => 0x06;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant DICTIONARY_ADDRESS => 0x08;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant OBJECT_TABLE_ADDRESS => 0x0a;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use constant GLOBAL_VARIABLE_ADDRESS => 0x0c;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use constant STATIC_MEMORY_ADDRESS => 0x0e;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant SERIAL_CODE => 0x12;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use constant ABBREV_TABLE_ADDRESS => 0x18;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use constant FILE_LENGTH => 0x1a;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use constant CHECKSUM => 0x1c;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶use constant STATUS_NOT_AVAILABLE => 0x10; # bit 4 (#5)
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use constant SCREEN_SPLITTING_AVAILABLE => 0x20; # bit 5 (#6)
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # Flags 1
32 1 ▶use constant TANDY => 0x08;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # Flags 2
35 1 ▶use constant TRANSCRIPT_ON => 0x01; # bit 0
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant FORCE_FIXED => 0x02; # bit 1
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use constant REQUEST_STATUS_REDRAW => 0x04; # bit 2
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # Flags 2, v5+:
40 1 ▶use constant WANTS_PICTURES => 0x08;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use constant WANTS_UNDO => 0x10;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use constant WANTS_MOUSE => 0x20;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use constant WANTS_COLOR => 0x40;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use constant WANTS_SOUND => 0x80;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 # Flags 2, v6+:
46 1 ▶use constant WANTS_MENUS => 0x0100; # ??
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 1 ▶use constant BACKGROUND_COLOR => 0x2c;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use constant FOREGROUND_COLOR => 0x2d;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 # 8.3.2, 8.3.3
51  
52 1 ▶use constant SCREEN_HEIGHT_LINES => 0x20;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use constant SCREEN_WIDTH_CHARS => 0x21;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use constant SCREEN_WIDTH_UNITS => 0x22;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use constant SCREEN_HEIGHT_UNITS => 0x24;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 1 ▶use constant FONT_WIDTH_UNITS_V5 => 0x26;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use constant FONT_WIDTH_UNITS_V6 => 0x27;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 1 ▶use constant FONT_HEIGHT_UNITS_V5 => 0x27;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use constant FONT_HEIGHT_UNITS_V6 => 0x26;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 1 ▶use constant ROUTINES_OFFSET => 0x28;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use constant STRINGS_OFFSET => 0x2a;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 1 ▶use Games::Rezrov::MethodMaker ([],
Subroutine BEGIN: called CC=1 SCAR=0.0
67 qw(
68 abbrev_table_address
69 file_checksum
70 release_number
71 paged_memory_address
72 object_table_address
73 global_variable_address
74 static_memory_address
75 first_instruction_address
76 dictionary_address
77 serial_code
78 file_length
79 story
80 version
81 object_bytes
82 attribute_bytes
83 pointer_size
84 max_properties
85 max_objects
86 attribute_starter
87 object_count
88 encoded_word_length
89 is_time_game
90 strings_offset
91 routines_offset
92 1 ));
93  
94 #use SelfLoader;
95  
96 1 1;
97  
98 1 my $INLINE_CODE = '
99 sub new {
100 my ($type, $zio) = @_;
101 my $self = [];
102 bless $self, $type;
103
104 my $version = GET_BYTE_AT(0);
105 if ($version < 1 or $version > 8) {
106 die "\nThis does not appear to be a valid game file.\n";
107 } elsif (($version < 3 or $version > 5) and $version != 8) {
108 # } elsif ($version < 3 or $version > 8) {
109 # die "Sorry, only z-code versions 3-8 are supported at present...\nAnd even those need work! :)\n"
110 die "Sorry, only z-code versions 3,4,5 and 8 are supported at present...\nAnd even those need work! :)\n"
111 } else {
112 $self->version($version);
113 }
114  
115 my $f1 = GET_BYTE_AT(FLAGS_1);
116 $self->is_time_game($f1 & 0x02 ? 1 : 0);
117 # a "time" game: 8.2.3.2
118  
119 my $start_rows = Games::Rezrov::StoryFile::rows();
120 my $start_columns = Games::Rezrov::StoryFile::columns();
121  
122 $f1 |= TANDY if Games::Rezrov::ZOptions::TANDY_BIT();
123 # turn on the "tandy bit"
124
125 if ($version <= 3) {
126 $self->encoded_word_length(6);
127 # 13.3, 13.4
128  
129 # set bits 4 (status line) and 5 (screen splitting) appropriately
130 # depending on the ability of the ZIO implementation
131 if ($zio->can_split()) {
132 # yes
133 $f1 |= SCREEN_SPLITTING_AVAILABLE;
134 $f1 &= ~ STATUS_NOT_AVAILABLE;
135 } else {
136 # no
137 $f1 &= ~ SCREEN_SPLITTING_AVAILABLE;
138 $f1 |= STATUS_NOT_AVAILABLE;
139 }
140  
141 # "bit 6" (#7): variable-pitch font is default?
142 if ($zio->fixed_font_default()) {
143 $f1 |= 0x40;
144 } else {
145 $f1 &= ~0x40;
146 }
147 } else {
148 #
149 # versions 4+
150 #
151 $self->encoded_word_length(9);
152 # 13.3, 13.4
153  
154 if ($version >= 4) {
155 $f1 |= 0x04;
156 # "bit 2" (#3): boldface available
157 $f1 |= 0x08;
158 # "bit 3" (#4): italic available
159 $f1 |= 0x10;
160 # "bit 4" (#5): fixed-font available
161  
162 # $f1 |= 0x80;
163 $f1 &= ~0x80;
164 # "bit 7" (#8): timed input NOT available
165  
166 Games::Rezrov::StoryFile::set_byte_at(30, Games::Rezrov::ZOptions::INTERPRETER_ID());
167 # interpreter number
168 Games::Rezrov::StoryFile::set_byte_at(31, ord "R");
169 # interpreter version; "R" for rezrov
170
171 $self->set_columns($start_columns);
172 $self->set_rows($start_rows);
173 }
174 if ($version >= 5) {
175 if ($zio->can_use_color()) {
176 # "bit 0" (#1): colors available
177 $f1 |= 0x01;
178 }
179  
180 Games::Rezrov::StoryFile::set_byte_at(BACKGROUND_COLOR, Games::Rezrov::ZConst::COLOR_BLACK);
181 Games::Rezrov::StoryFile::set_byte_at(FOREGROUND_COLOR, Games::Rezrov::ZConst::COLOR_WHITE);
182 # 8.3.3: default foreground and background
183 # FIX ME!
184  
185 my $f2 = Games::Rezrov::StoryFile::get_word_at(FLAGS_2);
186 if ($zio->groks_font_3() and
187 !Games::Rezrov::StoryFile::font_3_disabled()) {
188 # ZIO can decode font 3 characters
189 $f2 |= WANTS_PICTURES;
190 } else {
191 # nope
192 $f2 &= ~ WANTS_PICTURES;
193 }
194
195 # $f2 |= WANTS_UNDO;
196 $f2 &= ~ WANTS_UNDO;
197 # FIX ME: should we never use this???
198  
199 if ($f2 & WANTS_COLOR) {
200 # 8.3.4: the game wants to use colors
201 # print "wants color!\n";
202 }
203 Games::Rezrov::StoryFile::set_word_at(FLAGS_2, $f2);
204 }
205 if ($version >= 6) {
206 # more unimplemented: see 8.3.2, etc
207 $self->routines_offset(Games::Rezrov::StoryFile::get_word_at(ROUTINES_OFFSET));
208 $self->strings_offset(Games::Rezrov::StoryFile::get_word_at(STRINGS_OFFSET));
209 }
210 }
211  
212 Games::Rezrov::StoryFile::set_byte_at(FLAGS_1, $f1);
213 # write back the header flags
214  
215 $self->release_number(Games::Rezrov::StoryFile::get_word_at(RELEASE_NUMBER));
216 $self->paged_memory_address(Games::Rezrov::StoryFile::get_word_at(PAGED_MEMORY_ADDRESS));
217 $self->first_instruction_address(Games::Rezrov::StoryFile::get_word_at(FIRST_INSTRUCTION_ADDRESS));
218 $self->dictionary_address(Games::Rezrov::StoryFile::get_word_at(DICTIONARY_ADDRESS));
219 $self->object_table_address(Games::Rezrov::StoryFile::get_word_at(OBJECT_TABLE_ADDRESS));
220 $self->global_variable_address(Games::Rezrov::StoryFile::get_word_at(GLOBAL_VARIABLE_ADDRESS));
221 $self->static_memory_address(Games::Rezrov::StoryFile::get_word_at(STATIC_MEMORY_ADDRESS));
222 $self->serial_code(Games::Rezrov::StoryFile::get_string_at(SERIAL_CODE, 6));
223 # see zmach06e.txt
224 $self->abbrev_table_address(Games::Rezrov::StoryFile::get_word_at(ABBREV_TABLE_ADDRESS));
225 $self->file_checksum(Games::Rezrov::StoryFile::get_word_at(CHECKSUM));
226  
227 my $flen = Games::Rezrov::StoryFile::get_word_at(FILE_LENGTH);
228 if ($version <= 3) {
229 # see 11.1.6
230 $flen *= 2;
231 } elsif ($version == 4 || $version == 5) {
232 $flen *= 4;
233 } else {
234 $flen *= 8;
235 }
236 $self->file_length($flen);
237
238 #
239 # set object "constants" for this version...
240 #
241 if ($version <= 3) {
242 # 12.3.1
243 $self->object_bytes(9);
244 $self->attribute_bytes(4);
245 $self->pointer_size(1);
246 $self->max_properties(31); # 12.2
247 $self->max_objects(255); # 12.3.1
248 } else {
249 # 12.3.2
250 $self->object_bytes(14);
251 $self->attribute_bytes(6);
252 $self->pointer_size(2);
253 $self->max_properties(63); # 12.2
254 $self->max_objects(65535); # 12.3.2
255 }
256 die("check your math!")
257 if (($self->attribute_bytes() + ($self->pointer_size() * 3) + 2)
258 != $self->object_bytes());
259
260 $self->attribute_starter($self->object_table_address() +
261 ($self->max_properties() * 2));
262
263 my $obj_space = $self->global_variable_address() - $self->attribute_starter();
264 # how many bytes exist between the start of the object area and
265 # the beginning of the global variable block?
266 my $object_count;
267 if ($obj_space > 0) {
268 # hack:
269 # guess approximate object count; most useful for games later than v3
270 # FIX ME: is this _way_ off? Better to check validity of each object
271 # sequentially, stopping w/invalid pointers, etc?
272 $object_count = $obj_space / $self->object_bytes();
273 $object_count = $self->max_objects()
274 if $object_count > $self->max_objects();
275 } else {
276 # header data not arranged the way we expect; oh well.
277 $object_count = $self->max_objects();
278 }
279 $self->object_count($object_count);
280 # die sprintf "objects: %s\n", $object_count;
281
282 return $self;
283 }
284  
285 sub get_colors {
286 return (GET_BYTE_AT(FOREGROUND_COLOR),
287 GET_BYTE_AT(BACKGROUND_COLOR));
288 }
289  
290 ';
291  
292 1 Games::Rezrov::Inliner::inline(\$INLINE_CODE);
293 0 ▶eval $INLINE_CODE;
Subroutine get_colors: not called CC=1 SCAR=1.6
Subroutine new: called CC=25 SCAR=49.0
Pod undocumented
Pod undocumented
Branch: 19 branches
BranchTrueFalseTotal
if ($version < 1 or $version > 8) { } 0 1 1
elsif ($version < 3 || $version > 5 and $version != 8) { } 0 1 1
$f1 & 2 ? : 0 1 1
if Games::Rezrov::ZOptions::TANDY_BIT() 0 1 1
if ($version <= 3) { } 1 0 1
if ($zio->can_split) { } 0 1 1
if ($zio->fixed_font_default) { } 1 0 1
if ($version >= 4) 0 0 0
if ($version >= 5) 0 0 0
if ($zio->can_use_color) 0 0 0
if ($zio->groks_font_3 and not Games::Rezrov::StoryFile::font_3_disabled()) { } 0 0 0
if ($f2 & 64) 0 0 0
if ($version >= 6) 0 0 0
if ($version <= 3) { } 1 0 1
elsif ($version == 4 or $version == 5) { } 0 0 0
if ($version <= 3) { } 1 0 1
if $self->attribute_bytes + $self->pointer_size * 3 + 2 != $self->object_bytes 0 1 1
if ($obj_space > 0) { } 0 1 1
if $object_count > $self->max_objects 0 0 0
Condition20%3/15
$version < 1 or $version > 8
l!l&&r!l&&!r
001
$version < 3 || $version > 5 and $version != 8
!ll&&!rl&&r
100
$version < 3 or $version > 5
l!l&&r!l&&!r
001
$zio->groks_font_3 and not Games::Rezrov::StoryFile::font_3_disabled()
!ll&&!rl&&r
000
$version == 4 or $version == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
$version < 1 or $version > 8
$version < 1 $version > 8
MC/DC0%0/3
$version < 3 || $version > 5 and $version != 8
$version < 3 $version > 5 $version != 8
MC/DC0%0/2
$zio->groks_font_3 and not Games::Rezrov::StoryFile::font_3_disabled()
$zio->groks_font_3 not Games::Rezrov::StoryFile::font_3_disabled()
MC/DC0%0/2
$version == 4 or $version == 5
$version == 4 $version == 5
Truth table33%1/3
A or B
A= $version < 1B= $version > 8
ABresult
1X1
011
000
Truth table20%1/5
A or B and C
A= $version < 3B= $version > 5C= $version != 8
ABCresult
00X0
1X00
0100
1X11
0111
Truth table0%0/3
A and not(B)
A= $zio->groks_font_3B= not Games::Rezrov::StoryFile::font_3_disabled()
ABresult
0X0
100
111
Truth table0%0/3
A or B
A= $version == 4B= $version == 5
ABresult
1X1
011
000
294 #print $INLINE_CODE;
295 #die "oof";
296 1 undef $INLINE_CODE;
297  
298 1 1;
299  
300 #__DATA__
301  
302 190 ▶sub get_abbreviation_addr {
Subroutine get_abbreviation_addr: called CC=1 SCAR=0.0
Pod undocumented
303 190 my ($self, $entry) = @_;
304 # Spec 3.3: fetch and convert the "word address" of the given entry
305 # in the abbreviations table.
306 # print STDERR "gaa\n";
307 190 my $abbrev_addr = $self->abbrev_table_address() + ($entry * 2);
308 190 return Games::Rezrov::StoryFile::get_word_at($abbrev_addr) * 2;
309 # "word address"; only used for abbreviations (packed address
310 # rules do not apply here)
311 }
312  
313 0 ▶sub set_columns {
Subroutine set_columns: not called CC=3 SCAR=24.8
Pod undocumented
314 # 8.4: set the dimensions of the screen.
315 # only needed in v4+
316 # arg: number of columns
317 0 Games::Rezrov::StoryFile::set_byte_at(SCREEN_WIDTH_CHARS, $_[1]);
318 0 ▶ if ($_[0]->version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->version >= 5) 0 0 0
319 0 ▶ Games::Rezrov::StoryFile::set_byte_at($_[0]->version >= 6 ?
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->version >= 6 ? : 0 0 0
320 FONT_WIDTH_UNITS_V6 : FONT_WIDTH_UNITS_V5, 1);
321 0 Games::Rezrov::StoryFile::set_word_at(SCREEN_WIDTH_UNITS, $_[1]);
322 # ?
323 }
324 }
325  
326 0 ▶sub set_rows {
Subroutine set_rows: not called CC=3 SCAR=24.8
Pod undocumented
327 # arg: number of rows
328 0 Games::Rezrov::StoryFile::set_byte_at(SCREEN_HEIGHT_LINES, $_[1]);
329 0 ▶ if ($_[0]->version >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->version >= 5) 0 0 0
330 0 ▶ Games::Rezrov::StoryFile::set_byte_at($_[0]->version >= 6 ?
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->version >= 6 ? : 0 0 0
331 FONT_HEIGHT_UNITS_V6 : FONT_HEIGHT_UNITS_V5, 1);
332 0 Games::Rezrov::StoryFile::set_word_at(SCREEN_HEIGHT_UNITS, $_[1]);
333 }
334 }
335  
336 0 ▶sub wants_color {
Subroutine wants_color: not called CC=2 SCAR=17.9
Pod undocumented
337 # 8.3.4: does the game want to use colors?
338 0 ▶ return Games::Rezrov::StoryFile::get_word_at(FLAGS_2) & WANTS_COLOR ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
Games::Rezrov::StoryFile::get_word_at(16) & 64 ? : 0 0 0
339 }
340  
341 0 ▶sub fixed_font_forced {
Subroutine fixed_font_forced: not called CC=2 SCAR=17.9
Pod undocumented
342 # 8.1: fixed-font printing may be forced by the game
343 0 ▶ if ($_[0]->version >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->version >= 3) { } 0 0 0
344 # see section 10
345 0 return Games::Rezrov::StoryFile::get_word_at(FLAGS_2) & FORCE_FIXED;
346 } else {
347 0 return 0;
348 }
349 }
350  
351  
352 1 1;
« ZIO_Win32.pm ↑ Summary ZInterpreter.pm »