Update interpreters to latest Garglk codebase
[projects/chimara/chimara.git] / interpreters / nitfol / nitfol.h
1 /*  Nitfol - z-machine interpreter using Glk for output.
2     Copyright (C) 1999  Evin Robertson
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18     The author can be reached at nitfol@deja.com
19 */
20 #ifndef NITFOL_H
21 #define NITFOL_H
22
23 #include <stdlib.h>      /* For NULL, rand, srand */
24 #include <time.h>        /* For time() */
25 #include <ctype.h>       /* for isspace, isgraph, etc. */
26 #include <limits.h>
27 #include "glk.h"
28 #define GLK_EOF ((glsi32) -1)
29
30 #define NITFOL_MAJOR 0
31 #define NITFOL_MINOR 5
32
33 /* Change these next few typedefs depending on your compiler */
34 #if UCHAR_MAX==0xff
35 typedef unsigned char zbyte;
36 #else
37 #error "Can't find an 8-bit integer type"
38 #endif
39
40 #ifdef FAST_SHORT
41
42 #if SHRT_MAX==0x7fff
43 typedef unsigned short zword;
44 #elif INT_MAX==0x7fff
45 typedef unsigned int zword;
46 #else
47 #error "Can't find a 16-bit integer type"
48 #endif
49
50 #if INT_MAX==0x7fffffff
51 typedef unsigned int offset;
52 #elif LONG_MAX==0x7fffffff
53 typedef unsigned long offset;
54 #else
55 #error "Can't find a 32-bit integer type"
56 #endif
57
58 #ifdef TWOS16SHORT
59 #define FAST_TWOS16SHORT
60 #endif
61
62 #else
63
64 #ifdef FAST_SIGNED
65 #if INT_MAX==0x7fffffff
66 typedef int zword;
67 typedef int offset;
68 #elif LONG_MAX==0x7fffffff
69 typedef long zword;
70 typedef long offset;
71 #else
72 #error "Can't find a 32-bit integer type"
73 #endif
74
75 #else
76
77 #if INT_MAX==0x7fffffff
78 typedef unsigned int zword;
79 typedef unsigned int offset;
80 #elif LONG_MAX==0x7fffffff
81 typedef unsigned long zword;
82 typedef unsigned long offset;
83 #else
84 #error "Can't find a 32-bit integer type"
85 #endif
86
87 #endif
88
89 #endif
90
91 #ifndef BOOL
92 #define BOOL int
93 #endif
94 #ifndef TRUE
95 #define TRUE 1
96 #endif
97 #ifndef FALSE
98 #define FALSE 0
99 #endif
100
101 #ifndef MAX
102 #define MAX(a, b) (a > b ? a : b)
103 #endif
104 #ifndef MIN
105 #define MIN(a, b) (a < b ? a : b)
106 #endif
107
108 #ifdef PASTE
109 #undef PASTE
110 #endif
111 #ifdef XPASTE
112 #undef XPASTE
113 #endif
114 #define PASTE(a, b) a##b
115 #define XPASTE(a, b) PASTE(a, b)
116
117
118 #if defined(__cplusplus) || defined(USE_INLINE)
119 #define N_INLINE inline
120 #elif defined(INLINE)
121 #define N_INLINE INLINE
122 #elif defined(__GNUC__)
123 #define N_INLINE __inline__
124 #else
125 #define N_INLINE
126 #endif
127
128
129
130 #ifdef ZVERSION_GRAHAM_9
131
132 #define ZWORD_SIZE 4
133 #define ZWORD_MAX  0x7fffffffL
134 #define ZWORD_MASK 0xffffffffL
135 #define ZWORD_WRAP 0x100000000L
136 #define ZWORD_TOPBITMASK 0x80000000L
137
138 #else
139
140 #define ZWORD_SIZE 2
141 #define ZWORD_MAX  0x7fff
142 #define ZWORD_MASK 0xffff
143 #define ZWORD_WRAP 0x10000L
144 #define ZWORD_TOPBITMASK 0x8000
145
146 #endif
147
148
149 #ifdef FAST_TWOS16SHORT
150
151 #define ARITHMASK(n) (n)
152 #define is_neg(a)     ((short) (a) < 0)
153 #define neg(a)       (-((short) (a)))
154
155 #else
156
157 #define ARITHMASK(n) ((n) & ZWORD_MASK)
158 #define is_neg(a) ((a) > ZWORD_MAX)
159 #define neg(a) ((ZWORD_WRAP - (a)) & ZWORD_MASK)
160
161 #endif
162
163
164 #ifdef TWOS16SHORT
165
166 #define is_greaterthan(a, b) (((short) (a)) > ((short) (b)))
167 #define is_lessthan(a, b)    (((short) (a)) < ((short) (b)))
168
169 #else
170
171 #define is_greaterthan(a, b) (((b) - (a)) & ZWORD_TOPBITMASK)
172 #define is_lessthan(a, b)    (((a) - (b)) & ZWORD_TOPBITMASK)
173
174 #endif
175
176
177 #ifdef FAST
178
179 #define LOBYTE(p)         z_memory[p]
180 #define LOBYTEcopy(d, s)  z_memory[d] = LOBYTE(s)
181 #define LOBYTEwrite(p, n) z_memory[p] = (n)
182 #define LOWORD(p)         MSBdecodeZ(z_memory + (p))
183 #define LOWORDcopy(d, s)  BYTEcopyZ(z_memory + (d), z_memory + (s))
184 #define LOWORDwrite(p, n) MSBencodeZ(z_memory + (p), n)
185
186 /* If you have a segmented memory model or need to implement virtual memory,
187    you can change the next three lines, and the corresponding three in the
188    not FAST section. */
189 #define HIBYTE(p)         z_memory[p]
190 #define HIWORD(p)         MSBdecodeZ(z_memory + (p))
191 #define HISTRWORD(p)      HIWORD(p)
192
193 #else /* not FAST */
194
195 /* FIXME: these tests may not work on 16 bit machines */
196
197 #define LOBYTE(p)         ((p) >= ZWORD_WRAP ? z_range_error(p) : \
198                            z_memory[p])
199 #define LOBYTEcopy(a, b)  ((void) \
200                            ((a) >= dynamic_size ? z_range_error(a) : \
201                            (z_memory[a] = LOBYTE(b))))
202 #define LOBYTEwrite(p, n) ((void) \
203                            ((p) >= dynamic_size ? z_range_error(p) : \
204                            (z_memory[p] = (n))))
205 #define LOWORD(p)         ((p) + ZWORD_SIZE > ZWORD_WRAP ? z_range_error(p) : \
206                            MSBdecodeZ(z_memory + (p)))
207 #define LOWORDcopy(d, s)  ((void)  \
208                           ((d) + ZWORD_SIZE > dynamic_size ? z_range_error(d) : \
209                            BYTEcopyZ(z_memory + (d), z_memory + (s))))
210 #define LOWORDwrite(p, n) ((void) \
211                           ((p) + ZWORD_SIZE > dynamic_size ? z_range_error(p) : \
212                            MSBencodeZ(z_memory + (p), n)))
213
214 #define HIBYTE(p)         ((p) >= game_size ? z_range_error(p) : z_memory[p])
215 #define HIWORD(p)         ((p) + ZWORD_SIZE > total_size ? z_range_error(p) : \
216                            MSBdecodeZ(z_memory + (p)))
217 #define HISTRWORD(p)      HIWORD(p)
218
219
220 #endif /* not FAST */
221
222
223
224 /* Probably your system has more efficient ways of reading/writing MSB values,
225    so go ahead and plop it in here if you crave that extra bit of speed */
226
227 #define MSBdecode1(v) ((v)[0])
228 #define MSBdecode2(v) ((((zword) (v)[0]) << 8) | (v)[1])
229 #define MSBdecode3(v) ((((offset) (v)[0]) << 16) | (((offset) (v)[1]) << 8) \
230                        | (v)[2])
231 #define MSBdecode4(v) ((((offset) (v)[0]) << 24) | (((offset) (v)[1]) << 16) \
232                      | (((offset) (v)[2]) <<  8) | (v)[3])
233
234 #define MSBencode1(v, n) ((v)[0] = (char) (n))
235 #define MSBencode2(v, n) (((v)[0] = (char) ((n) >> 8)), ((v)[1] = (char) (n)))
236 #define MSBencode3(v, n) (((v)[0] = (char) ((n) >> 16)), \
237                           ((v)[1] = (char) ((n) >> 8)), \
238                           ((v)[2] = (char) (n)))
239 #define MSBencode4(v, n) (((v)[0] = (char) ((n) >> 24)), \
240                           ((v)[1] = (char) ((n) >> 16)), \
241                           ((v)[2] = (char) ((n) >>  8)), \
242                           ((v)[3] = (char) (n)))
243
244 #define BYTEcopy1(d, s) ((d)[0] = (s)[0])
245 #define BYTEcopy2(d, s) ((d)[0] = (s)[0], (d)[1] = (s)[1])
246 #define BYTEcopy3(d, s) ((d)[0] = (s)[0], (d)[1] = (s)[1], (d)[2] = (s)[2])
247 #define BYTEcopy4(d, s) ((d)[0] = (s)[0], (d)[1] = (s)[1], \
248                          (d)[2] = (s)[2], (d)[3] = (s)[3])
249
250
251 #define MSBdecodeZ(v)    XPASTE(MSBdecode, ZWORD_SIZE)(v)
252 #define MSBencodeZ(v, n) XPASTE(MSBencode, ZWORD_SIZE)(v, n)
253 #define BYTEcopyZ(d, s)  XPASTE(BYTEcopy, ZWORD_SIZE)(d, s)
254
255
256
257 #define UNPACKR(paddr) (paddr * granularity + rstart)
258 #define UNPACKS(paddr) (paddr * granularity + sstart)
259
260 #define PACKR(addr) ((addr - rstart) / granularity)
261 #define PACKS(addr) ((addr - sstart) / granularity)
262
263 /* Byte offsets into the header */
264 #define HD_ZVERSION   0x00
265 #define HD_FLAGS1     0x01
266 #define HD_RELNUM     0x02
267 #define HD_HIMEM      0x04
268 #define HD_INITPC     0x06
269 #define HD_DICT       0x08
270 #define HD_OBJTABLE   0x0a
271 #define HD_GLOBVAR    0x0c
272 #define HD_STATMEM    0x0e
273 #define HD_FLAGS2     0x10
274 #define HD_SERNUM     0x12
275 #define HD_ABBREV     0x18
276 #define HD_LENGTH     0x1a
277 #define HD_CHECKSUM   0x1c
278 #define HD_TERPNUM    0x1e
279 #define HD_TERPVER    0x1f
280 #define HD_SCR_HEIGHT 0x20
281 #define HD_SCR_WIDTH  0x21
282 #define HD_SCR_WUNIT  0x22
283 #define HD_SCR_HUNIT  0x24
284 #define HD_FNT_WIDTH  0x26
285 #define HD_FNT_HEIGHT 0x27
286 #define HD_RTN_OFFSET 0x28
287 #define HD_STR_OFFSET 0x2a
288 #define HD_DEF_BACK   0x2c
289 #define HD_DEF_FORE   0x2d
290 #define HD_TERM_CHAR  0x2e
291 #define HD_STR3_WIDTH 0x30
292 #define HD_STD_REV    0x32
293 #define HD_ALPHABET   0x34
294 #define HD_HEADER_EXT 0x36
295 #define HD_USERID     0x38
296 #define HD_INFORMVER  0x3c
297
298
299 enum zversions { v1 = 1, v2, v3, v4, v5, v6, v7, v8, vM };
300 enum opcodeinfoflags { opNONE = 0, opSTORES = 1, opBRANCHES = 2, opJUMPS = 4,
301                        opTEXTINLINE = 8 };
302
303 typedef struct {
304   const char *name;
305   int minversion, maxversion;
306   int minargs, maxargs;
307   int flags;
308   int watchlevel;
309 } opcodeinfo;
310
311
312 typedef enum { OBJ_GET_INFO, OBJ_RECEIVE, OBJ_MOVE } watchinfo;
313
314
315 #include "portfunc.h"    /* For miscellaneous string functions, etc. */
316 #include "hash.h"
317 #include "linkevil.h"
318 #include "struct.h"
319 #include "globals.h"
320 #include "binary.h"
321 #include "errmesg.h"
322 #include "iff.h"
323 #include "init.h"
324 #include "decode.h"
325 #include "main.h"
326
327 #include "nio.h"
328 #include "z_io.h"
329
330 #include "no_snd.h"
331 #include "gi_blorb.h"
332 #include "no_graph.h"
333 #include "no_blorb.h"
334
335 #include "infix.h"
336 #include "debug.h"
337 #include "inform.h"
338 #include "copying.h"
339 #include "solve.h"
340 #include "automap.h"
341
342 #include "zscii.h"
343 #include "tokenise.h"
344 #include "op_call.h"
345 #include "op_jmp.h"
346 #include "op_math.h"
347 #include "quetzal.h"
348 #include "undo.h"
349 #include "op_save.h"
350 #include "op_table.h"
351 #include "op_v6.h"
352 #include "objects.h"
353 #include "stack.h"
354 #include "oplist.h"
355
356
357 strid_t startup_findfile(void);
358
359 strid_t intd_filehandle_open(strid_t savefile, glui32 operating_id,
360                              glui32 contents_id, glui32 interp_id,
361                              glui32 length);
362
363 void intd_filehandle_make(strid_t savefile);
364
365 glui32 intd_get_size(void);
366
367 strid_t startup_open(const char *name);
368
369
370 #endif
371