Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / plugins / frotz / variable.c
blob3e5c6e0c0980cfb6e39d133093f1a0a057257f43
1 /* variable.c - Variable and stack related opcodes
2 * Copyright (c) 1995-1997 Stefan Jokisch
4 * This file is part of Frotz.
6 * Frotz is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Frotz is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include "frotz.h"
24 * z_dec, decrement a variable.
26 * zargs[0] = variable to decrement
30 void z_dec (void)
32 zword value;
34 if (zargs[0] == 0)
35 (*sp)--;
36 else if (zargs[0] < 16)
37 (*(fp - zargs[0]))--;
38 else {
39 zword addr = h_globals + 2 * (zargs[0] - 16);
40 LOW_WORD (addr, value)
41 value--;
42 SET_WORD (addr, value)
45 }/* z_dec */
48 * z_dec_chk, decrement a variable and branch if now less than value.
50 * zargs[0] = variable to decrement
51 * zargs[1] = value to check variable against
55 void z_dec_chk (void)
57 zword value;
59 if (zargs[0] == 0)
60 value = --(*sp);
61 else if (zargs[0] < 16)
62 value = --(*(fp - zargs[0]));
63 else {
64 zword addr = h_globals + 2 * (zargs[0] - 16);
65 LOW_WORD (addr, value)
66 value--;
67 SET_WORD (addr, value)
70 branch ((short) value < (short) zargs[1]);
72 }/* z_dec_chk */
75 * z_inc, increment a variable.
77 * zargs[0] = variable to increment
81 void z_inc (void)
83 zword value;
85 if (zargs[0] == 0)
86 (*sp)++;
87 else if (zargs[0] < 16)
88 (*(fp - zargs[0]))++;
89 else {
90 zword addr = h_globals + 2 * (zargs[0] - 16);
91 LOW_WORD (addr, value)
92 value++;
93 SET_WORD (addr, value)
96 }/* z_inc */
99 * z_inc_chk, increment a variable and branch if now greater than value.
101 * zargs[0] = variable to increment
102 * zargs[1] = value to check variable against
106 void z_inc_chk (void)
108 zword value;
110 if (zargs[0] == 0)
111 value = ++(*sp);
112 else if (zargs[0] < 16)
113 value = ++(*(fp - zargs[0]));
114 else {
115 zword addr = h_globals + 2 * (zargs[0] - 16);
116 LOW_WORD (addr, value)
117 value++;
118 SET_WORD (addr, value)
121 branch ((short) value > (short) zargs[1]);
123 }/* z_inc_chk */
126 * z_load, store the value of a variable.
128 * zargs[0] = variable to store
132 void z_load (void)
134 zword value;
136 if (zargs[0] == 0)
137 value = *sp;
138 else if (zargs[0] < 16)
139 value = *(fp - zargs[0]);
140 else {
141 zword addr = h_globals + 2 * (zargs[0] - 16);
142 LOW_WORD (addr, value)
145 store (value);
147 }/* z_load */
150 * z_pop, pop a value off the game stack and discard it.
152 * no zargs used
156 void z_pop (void)
159 sp++;
161 }/* z_pop */
164 * z_pop_stack, pop n values off the game or user stack and discard them.
166 * zargs[0] = number of values to discard
167 * zargs[1] = address of user stack (optional)
171 void z_pop_stack (void)
174 if (zargc == 2) { /* it's a user stack */
176 zword size;
177 zword addr = zargs[1];
179 LOW_WORD (addr, size)
181 size += zargs[0];
182 storew (addr, size);
184 } else sp += zargs[0]; /* it's the game stack */
186 }/* z_pop_stack */
189 * z_pull, pop a value off...
191 * a) ...the game or a user stack and store it (V6)
193 * zargs[0] = address of user stack (optional)
195 * b) ...the game stack and write it to a variable (other than V6)
197 * zargs[0] = variable to write value to
201 void z_pull (void)
203 zword value;
205 if (h_version != V6) { /* not a V6 game, pop stack and write */
207 value = *sp++;
209 if (zargs[0] == 0)
210 *sp = value;
211 else if (zargs[0] < 16)
212 *(fp - zargs[0]) = value;
213 else {
214 zword addr = h_globals + 2 * (zargs[0] - 16);
215 SET_WORD (addr, value)
218 } else { /* it's V6, but is there a user stack? */
220 if (zargc == 1) { /* it's a user stack */
222 zword size;
223 zword addr = zargs[0];
225 LOW_WORD (addr, size)
227 size++;
228 storew (addr, size);
230 addr += 2 * size;
231 LOW_WORD (addr, value)
233 } else value = *sp++; /* it's the game stack */
235 store (value);
239 }/* z_pull */
242 * z_push, push a value onto the game stack.
244 * zargs[0] = value to push onto the stack
248 void z_push (void)
251 *--sp = zargs[0];
253 }/* z_push */
256 * z_push_stack, push a value onto a user stack then branch if successful.
258 * zargs[0] = value to push onto the stack
259 * zargs[1] = address of user stack
263 void z_push_stack (void)
265 zword size;
266 zword addr = zargs[1];
268 LOW_WORD (addr, size)
270 if (size != 0) {
272 storew ((zword) (addr + 2 * size), zargs[0]);
274 size--;
275 storew (addr, size);
279 branch (size);
281 }/* z_push_stack */
284 * z_store, write a value to a variable.
286 * zargs[0] = variable to be written to
287 * zargs[1] = value to write
291 void z_store (void)
293 zword value = zargs[1];
295 if (zargs[0] == 0)
296 *sp = value;
297 else if (zargs[0] < 16)
298 *(fp - zargs[0]) = value;
299 else {
300 zword addr = h_globals + 2 * (zargs[0] - 16);
301 SET_WORD (addr, value)
304 }/* z_store */