Inspired by #17, compared our Nitfol source to the source from its last
[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., 59 Temple Place, Suite 330, Boston, MA 02111, 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 "glk.h"
27 #define GLK_EOF ((glsi32) -1)
28
29 #define NITFOL_MAJOR 0
30 #define NITFOL_MINOR 5
31
32 /* Change these next few typedefs depending on your compiler */
33 #include <stdint.h>
34 typedef uint8_t zbyte;
35
36 #ifdef FAST_SHORT
37 typedef uint16_t zword;
38 typedef uint32_t offset;
39
40 #ifdef TWOS16SHORT
41 #define FAST_TWOS16SHORT
42 #endif
43
44 #else
45
46 #ifdef FAST_SIGNED
47 typedef int32_t zword;
48 typedef int32_t offset;
49 #else
50 typedef uint32_t zword;  /* Needs to be >= real zword */
51 typedef uint32_t offset;
52 #endif
53
54 #endif
55
56 #ifndef BOOL
57 #define BOOL int
58 #endif
59 #ifndef TRUE
60 #define TRUE 1
61 #endif
62 #ifndef FALSE
63 #define FALSE 0
64 #endif
65
66 #ifndef MAX
67 #define MAX(a, b) (a > b ? a : b)
68 #endif
69 #ifndef MIN
70 #define MIN(a, b) (a < b ? a : b)
71 #endif
72
73 #ifdef PASTE
74 #undef PASTE
75 #endif
76 #ifdef XPASTE
77 #undef XPASTE
78 #endif
79 #define PASTE(a, b) a##b
80 #define XPASTE(a, b) PASTE(a, b)
81
82
83 #if defined(__cplusplus) || defined(USE_INLINE)
84 #define N_INLINE inline
85 #elif defined(INLINE)
86 #define N_INLINE INLINE
87 #elif defined(__GNUC__)
88 #define N_INLINE __inline__
89 #else
90 #define N_INLINE
91 #endif
92
93
94
95 #ifdef ZVERSION_GRAHAM_9
96
97 #define ZWORD_SIZE 4
98 #define ZWORD_MAX  0x7fffffffL
99 #define ZWORD_MASK 0xffffffffL
100 #define ZWORD_WRAP 0x100000000L
101 #define ZWORD_TOPBITMASK 0x80000000L
102
103 #else
104
105 #define ZWORD_SIZE 2
106 #define ZWORD_MAX  0x7fff
107 #define ZWORD_MASK 0xffff
108 #define ZWORD_WRAP 0x10000L
109 #define ZWORD_TOPBITMASK 0x8000
110
111 #endif
112
113
114 #ifdef FAST_TWOS16SHORT
115
116 #define ARITHMASK(n) (n)
117 #define is_neg(a)     ((short) (a) < 0)
118 #define neg(a)       (-((short) (a)))
119
120 #else
121
122 #define ARITHMASK(n) ((n) & ZWORD_MASK)
123 #define is_neg(a) ((a) > ZWORD_MAX)
124 #define neg(a) ((ZWORD_WRAP - (a)) & ZWORD_MASK)
125
126 #endif
127
128
129 #ifdef TWOS16SHORT
130
131 #define is_greaterthan(a, b) (((short) (a)) > ((short) (b)))
132 #define is_lessthan(a, b)    (((short) (a)) < ((short) (b)))
133
134 #else
135
136 #define is_greaterthan(a, b) (((b) - (a)) & ZWORD_TOPBITMASK)
137 #define is_lessthan(a, b)    (((a) - (b)) & ZWORD_TOPBITMASK)
138
139 #endif
140
141
142 #ifdef FAST
143
144 #define LOBYTE(p)         z_memory[p]
145 #define LOBYTEcopy(d, s)  z_memory[d] = LOBYTE(s)
146 #define LOBYTEwrite(p, n) z_memory[p] = (n)
147 #define LOWORD(p)         MSBdecodeZ(z_memory + (p))
148 #define LOWORDcopy(d, s)  BYTEcopyZ(z_memory + (d), z_memory + (s))
149 #define LOWORDwrite(p, n) MSBencodeZ(z_memory + (p), n)
150
151 /* If you have a segmented memory model or need to implement virtual memory,
152    you can change the next three lines, and the corresponding three in the
153    not FAST section. */
154 #define HIBYTE(p)         z_memory[p]
155 #define HIWORD(p)         MSBdecodeZ(z_memory + (p))
156 #define HISTRWORD(p)      HIWORD(p)
157
158 #else /* not FAST */
159
160 /* FIXME: these tests may not work on 16 bit machines */
161
162 #define LOBYTE(p)         ((p) >= ZWORD_WRAP ? z_range_error(p) : \
163                            z_memory[p])
164 #define LOBYTEcopy(a, b)  ((void) \
165                            ((a) >= dynamic_size ? z_range_error(a) : \
166                            (z_memory[a] = LOBYTE(b))))
167 #define LOBYTEwrite(p, n) ((void) \
168                            ((p) >= dynamic_size ? z_range_error(p) : \
169                            (z_memory[p] = (n))))
170 #define LOWORD(p)         ((p) + ZWORD_SIZE > ZWORD_WRAP ? z_range_error(p) : \
171                            MSBdecodeZ(z_memory + (p)))
172 #define LOWORDcopy(d, s)  ((void)  \
173                           ((d) + ZWORD_SIZE > dynamic_size ? z_range_error(d) : \
174                            BYTEcopyZ(z_memory + (d), z_memory + (s))))
175 #define LOWORDwrite(p, n) ((void) \
176                           ((p) + ZWORD_SIZE > dynamic_size ? z_range_error(p) : \
177                            MSBencodeZ(z_memory + (p), n)))
178
179 #define HIBYTE(p)         ((p) >= game_size ? z_range_error(p) : z_memory[p])
180 #define HIWORD(p)         ((p) + ZWORD_SIZE > total_size ? z_range_error(p) : \
181                            MSBdecodeZ(z_memory + (p)))
182 #define HISTRWORD(p)      HIWORD(p)
183
184
185 #endif /* not FAST */
186
187
188
189 /* Probably your system has more efficient ways of reading/writing MSB values,
190    so go ahead and plop it in here if you crave that extra bit of speed */
191
192 #define MSBdecode1(v) ((v)[0])
193 #define MSBdecode2(v) ((((zword) (v)[0]) << 8) | (v)[1])
194 #define MSBdecode3(v) ((((offset) (v)[0]) << 16) | (((offset) (v)[1]) << 8) \
195                        | (v)[2])
196 #define MSBdecode4(v) ((((offset) (v)[0]) << 24) | (((offset) (v)[1]) << 16) \
197                      | (((offset) (v)[2]) <<  8) | (v)[3])
198
199 #define MSBencode1(v, n) ((v)[0] = (char) (n))
200 #define MSBencode2(v, n) (((v)[0] = (char) ((n) >> 8)), ((v)[1] = (char) (n)))
201 #define MSBencode3(v, n) (((v)[0] = (char) ((n) >> 16)), \
202                           ((v)[1] = (char) ((n) >> 8)), \
203                           ((v)[2] = (char) (n)))
204 #define MSBencode4(v, n) (((v)[0] = (char) ((n) >> 24)), \
205                           ((v)[1] = (char) ((n) >> 16)), \
206                           ((v)[2] = (char) ((n) >>  8)), \
207                           ((v)[3] = (char) (n)))
208
209 #define BYTEcopy1(d, s) ((d)[0] = (s)[0])
210 #define BYTEcopy2(d, s) ((d)[0] = (s)[0], (d)[1] = (s)[1])
211 #define BYTEcopy3(d, s) ((d)[0] = (s)[0], (d)[1] = (s)[1], (d)[2] = (s)[2])
212 #define BYTEcopy4(d, s) ((d)[0] = (s)[0], (d)[1] = (s)[1], \
213                          (d)[2] = (s)[2], (d)[3] = (s)[3])
214
215
216 #define MSBdecodeZ(v)    XPASTE(MSBdecode, ZWORD_SIZE)(v)
217 #define MSBencodeZ(v, n) XPASTE(MSBencode, ZWORD_SIZE)(v, n)
218 #define BYTEcopyZ(d, s)  XPASTE(BYTEcopy, ZWORD_SIZE)(d, s)
219
220
221
222 #define UNPACKR(paddr) (paddr * granularity + rstart)
223 #define UNPACKS(paddr) (paddr * granularity + sstart)
224
225 #define PACKR(addr) ((addr - rstart) / granularity)
226 #define PACKS(addr) ((addr - sstart) / granularity)
227
228 /* Byte offsets into the header */
229 #define HD_ZVERSION   0x00
230 #define HD_FLAGS1     0x01
231 #define HD_RELNUM     0x02
232 #define HD_HIMEM      0x04
233 #define HD_INITPC     0x06
234 #define HD_DICT       0x08
235 #define HD_OBJTABLE   0x0a
236 #define HD_GLOBVAR    0x0c
237 #define HD_STATMEM    0x0e
238 #define HD_FLAGS2     0x10
239 #define HD_SERNUM     0x12
240 #define HD_ABBREV     0x18
241 #define HD_LENGTH     0x1a
242 #define HD_CHECKSUM   0x1c
243 #define HD_TERPNUM    0x1e
244 #define HD_TERPVER    0x1f
245 #define HD_SCR_HEIGHT 0x20
246 #define HD_SCR_WIDTH  0x21
247 #define HD_SCR_WUNIT  0x22
248 #define HD_SCR_HUNIT  0x24
249 #define HD_FNT_WIDTH  0x26
250 #define HD_FNT_HEIGHT 0x27
251 #define HD_RTN_OFFSET 0x28
252 #define HD_STR_OFFSET 0x2a
253 #define HD_DEF_BACK   0x2c
254 #define HD_DEF_FORE   0x2d
255 #define HD_TERM_CHAR  0x2e
256 #define HD_STR3_WIDTH 0x30
257 #define HD_STD_REV    0x32
258 #define HD_ALPHABET   0x34
259 #define HD_HEADER_EXT 0x36
260 #define HD_USERID     0x38
261 #define HD_INFORMVER  0x3c
262
263
264 enum zversions { v1 = 1, v2, v3, v4, v5, v6, v7, v8, vM };
265 enum opcodeinfoflags { opNONE = 0, opSTORES = 1, opBRANCHES = 2, opJUMPS = 4,
266                        opTEXTINLINE = 8 };
267
268 typedef struct {
269   const char *name;
270   int minversion, maxversion;
271   int minargs, maxargs;
272   int flags;
273   int watchlevel;
274 } opcodeinfo;
275
276
277 typedef enum { OBJ_GET_INFO, OBJ_RECEIVE, OBJ_MOVE } watchinfo;
278
279
280 #include "portfunc.h"    /* For miscellaneous string functions, etc. */
281 #include "hash.h"
282 #include "linkevil.h"
283 #include "struct.h"
284 #include "globals.h"
285 #include "binary.h"
286 #include "errmesg.h"
287 #include "iff.h"
288 #include "init.h"
289 #include "decode.h"
290 #include "main.h"
291
292 #include "io.h"
293 #include "z_io.h"
294
295 #include "no_snd.h"
296 #include "gi_blorb.h"
297 #include "no_graph.h"
298 #include "no_blorb.h"
299
300 #include "infix.h"
301 #include "debug.h"
302 #include "inform.h"
303 #include "copying.h"
304 #include "solve.h"
305 #include "automap.h"
306
307 #include "zscii.h"
308 #include "tokenise.h"
309 #include "op_call.h"
310 #include "op_jmp.h"
311 #include "op_math.h"
312 #include "quetzal.h"
313 #include "undo.h"
314 #include "op_save.h"
315 #include "op_table.h"
316 #include "op_v6.h"
317 #include "objects.h"
318 #include "stack.h"
319 #include "oplist.h"
320
321
322 strid_t startup_findfile(void);
323
324 strid_t intd_filehandle_open(strid_t savefile, glui32 operating_id,
325                              glui32 contents_id, glui32 interp_id,
326                              glui32 length);
327
328 void intd_filehandle_make(strid_t savefile);
329
330 glui32 intd_get_size(void);
331
332 strid_t startup_open(const char *name);
333
334
335 #endif
336