5ff5163230bd4680e55b438dc6168b2c37eb2312
[projects/chimara/chimara.git] / interpreters / frotz / math.c
1 /* math.c - Arithmetic, compare and logical opcodes
2  *      Copyright (c) 1995-1997 Stefan Jokisch
3  *
4  * This file is part of Frotz.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "frotz.h"
22
23 /*
24  * z_add, 16bit addition.
25  *
26  *      zargs[0] = first value
27  *      zargs[1] = second value
28  *
29  */
30
31 void z_add (void)
32 {
33
34     store ((zword) ((short) zargs[0] + (short) zargs[1]));
35
36 }/* z_add */
37
38 /*
39  * z_and, bitwise AND operation.
40  *
41  *      zargs[0] = first value
42  *      zargs[1] = second value
43  *
44  */
45
46 void z_and (void)
47 {
48
49     store ((zword) (zargs[0] & zargs[1]));
50
51 }/* z_and */
52
53 /*
54  * z_art_shift, arithmetic SHIFT operation.
55  *
56  *      zargs[0] = value
57  *      zargs[1] = #positions to shift left (positive) or right
58  *
59  */
60
61 void z_art_shift (void)
62 {
63
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]));
68
69 }/* z_art_shift */
70
71 /*
72  * z_div, signed 16bit division.
73  *
74  *      zargs[0] = first value
75  *      zargs[1] = second value
76  *
77  */
78
79 void z_div (void)
80 {
81
82     if (zargs[1] == 0)
83         runtime_error (ERR_DIV_ZERO);
84
85     store ((zword) ((short) zargs[0] / (short) zargs[1]));
86
87 }/* z_div */
88
89 /*
90  * z_je, branch if the first value equals any of the following.
91  *
92  *      zargs[0] = first value
93  *      zargs[1] = second value (optional)
94  *      ...
95  *      zargs[3] = fourth value (optional)
96  *
97  */
98
99 void z_je (void)
100 {
101
102     branch (
103         zargc > 1 && (zargs[0] == zargs[1] || (
104         zargc > 2 && (zargs[0] == zargs[2] || (
105         zargc > 3 && (zargs[0] == zargs[3]))))));
106
107 }/* z_je */
108
109 /*
110  * z_jg, branch if the first value is greater than the second.
111  *
112  *      zargs[0] = first value
113  *      zargs[1] = second value
114  *
115  */
116
117 void z_jg (void)
118 {
119
120     branch ((short) zargs[0] > (short) zargs[1]);
121
122 }/* z_jg */
123
124 /*
125  * z_jl, branch if the first value is less than the second.
126  *
127  *      zargs[0] = first value
128  *      zargs[1] = second value
129  *
130  */
131
132 void z_jl (void)
133 {
134
135     branch ((short) zargs[0] < (short) zargs[1]);
136
137 }/* z_jl */
138
139 /*
140  * z_jz, branch if value is zero.
141  *
142  *      zargs[0] = value
143  *
144  */
145
146 void z_jz (void)
147 {
148
149     branch ((short) zargs[0] == 0);
150
151 }/* z_jz */
152
153 /*
154  * z_log_shift, logical SHIFT operation.
155  *
156  *      zargs[0] = value
157  *      zargs[1] = #positions to shift left (positive) or right (negative)
158  *
159  */
160
161 void z_log_shift (void)
162 {
163
164     if ((short) zargs[1] > 0)
165         store ((zword) (zargs[0] << (short) zargs[1]));
166     else
167         store ((zword) (zargs[0] >> - (short) zargs[1]));
168
169 }/* z_log_shift */
170
171 /*
172  * z_mod, remainder after signed 16bit division.
173  *
174  *      zargs[0] = first value
175  *      zargs[1] = second value
176  *
177  */
178
179 void z_mod (void)
180 {
181
182     if (zargs[1] == 0)
183         runtime_error (ERR_DIV_ZERO);
184
185     store ((zword) ((short) zargs[0] % (short) zargs[1]));
186
187 }/* z_mod */
188
189 /*
190  * z_mul, 16bit multiplication.
191  *
192  *      zargs[0] = first value
193  *      zargs[1] = second value
194  *
195  */
196
197 void z_mul (void)
198 {
199
200     store ((zword) ((short) zargs[0] * (short) zargs[1]));
201
202 }/* z_mul */
203
204 /*
205  * z_not, bitwise NOT operation.
206  *
207  *      zargs[0] = value
208  *
209  */
210
211 void z_not (void)
212 {
213
214     store ((zword) ~zargs[0]);
215
216 }/* z_not */
217
218 /*
219  * z_or, bitwise OR operation.
220  *
221  *      zargs[0] = first value
222  *      zargs[1] = second value
223  *
224  */
225
226 void z_or (void)
227 {
228
229     store ((zword) (zargs[0] | zargs[1]));
230
231 }/* z_or */
232
233 /*
234  * z_sub, 16bit substraction.
235  *
236  *      zargs[0] = first value
237  *      zargs[1] = second value
238  *
239  */
240
241 void z_sub (void)
242 {
243
244     store ((zword) ((short) zargs[0] - (short) zargs[1]));
245
246 }/* z_sub */
247
248 /*
249  * z_test, branch if all the flags of a bit mask are set in a value.
250  *
251  *      zargs[0] = value to be examined
252  *      zargs[1] = bit mask
253  *
254  */
255
256 void z_test (void)
257 {
258
259     branch ((zargs[0] & zargs[1]) == zargs[1]);
260
261 }/* z_test */