Rebase to SVN, and do a bit of cleanup as well as minor fixes.
[kugel-rb.git] / apps / plugins / frotz / math.c
blob5ff5163230bd4680e55b438dc6168b2c37eb2312
1 /* math.c - Arithmetic, compare and logical 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_add, 16bit addition.
26 * zargs[0] = first value
27 * zargs[1] = second value
31 void z_add (void)
34 store ((zword) ((short) zargs[0] + (short) zargs[1]));
36 }/* z_add */
39 * z_and, bitwise AND operation.
41 * zargs[0] = first value
42 * zargs[1] = second value
46 void z_and (void)
49 store ((zword) (zargs[0] & zargs[1]));
51 }/* z_and */
54 * z_art_shift, arithmetic SHIFT operation.
56 * zargs[0] = value
57 * zargs[1] = #positions to shift left (positive) or right
61 void z_art_shift (void)
64 if ((short) zargs[1] > 0)
65 store ((zword) ((short) zargs[0] << (short) zargs[1]));
66 else
67 store ((zword) ((short) zargs[0] >> - (short) zargs[1]));
69 }/* z_art_shift */
72 * z_div, signed 16bit division.
74 * zargs[0] = first value
75 * zargs[1] = second value
79 void z_div (void)
82 if (zargs[1] == 0)
83 runtime_error (ERR_DIV_ZERO);
85 store ((zword) ((short) zargs[0] / (short) zargs[1]));
87 }/* z_div */
90 * z_je, branch if the first value equals any of the following.
92 * zargs[0] = first value
93 * zargs[1] = second value (optional)
94 * ...
95 * zargs[3] = fourth value (optional)
99 void z_je (void)
102 branch (
103 zargc > 1 && (zargs[0] == zargs[1] || (
104 zargc > 2 && (zargs[0] == zargs[2] || (
105 zargc > 3 && (zargs[0] == zargs[3]))))));
107 }/* z_je */
110 * z_jg, branch if the first value is greater than the second.
112 * zargs[0] = first value
113 * zargs[1] = second value
117 void z_jg (void)
120 branch ((short) zargs[0] > (short) zargs[1]);
122 }/* z_jg */
125 * z_jl, branch if the first value is less than the second.
127 * zargs[0] = first value
128 * zargs[1] = second value
132 void z_jl (void)
135 branch ((short) zargs[0] < (short) zargs[1]);
137 }/* z_jl */
140 * z_jz, branch if value is zero.
142 * zargs[0] = value
146 void z_jz (void)
149 branch ((short) zargs[0] == 0);
151 }/* z_jz */
154 * z_log_shift, logical SHIFT operation.
156 * zargs[0] = value
157 * zargs[1] = #positions to shift left (positive) or right (negative)
161 void z_log_shift (void)
164 if ((short) zargs[1] > 0)
165 store ((zword) (zargs[0] << (short) zargs[1]));
166 else
167 store ((zword) (zargs[0] >> - (short) zargs[1]));
169 }/* z_log_shift */
172 * z_mod, remainder after signed 16bit division.
174 * zargs[0] = first value
175 * zargs[1] = second value
179 void z_mod (void)
182 if (zargs[1] == 0)
183 runtime_error (ERR_DIV_ZERO);
185 store ((zword) ((short) zargs[0] % (short) zargs[1]));
187 }/* z_mod */
190 * z_mul, 16bit multiplication.
192 * zargs[0] = first value
193 * zargs[1] = second value
197 void z_mul (void)
200 store ((zword) ((short) zargs[0] * (short) zargs[1]));
202 }/* z_mul */
205 * z_not, bitwise NOT operation.
207 * zargs[0] = value
211 void z_not (void)
214 store ((zword) ~zargs[0]);
216 }/* z_not */
219 * z_or, bitwise OR operation.
221 * zargs[0] = first value
222 * zargs[1] = second value
226 void z_or (void)
229 store ((zword) (zargs[0] | zargs[1]));
231 }/* z_or */
234 * z_sub, 16bit substraction.
236 * zargs[0] = first value
237 * zargs[1] = second value
241 void z_sub (void)
244 store ((zword) ((short) zargs[0] - (short) zargs[1]));
246 }/* z_sub */
249 * z_test, branch if all the flags of a bit mask are set in a value.
251 * zargs[0] = value to be examined
252 * zargs[1] = bit mask
256 void z_test (void)
259 branch ((zargs[0] & zargs[1]) == zargs[1]);
261 }/* z_test */