Update interpreters to latest Garglk codebase
[projects/chimara/chimara.git] / interpreters / frotz / table.c
1 /* table.c - Table handling 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "frotz.h"
22
23 /*
24  * z_copy_table, copy a table or fill it with zeroes.
25  *
26  *      zargs[0] = address of table
27  *      zargs[1] = destination address or 0 for fill
28  *      zargs[2] = size of table
29  *
30  * Note: Copying is safe even when source and destination overlap; but
31  *       if zargs[1] is negative the table _must_ be copied forwards.
32  *
33  */
34
35 void z_copy_table (void)
36 {
37     zword addr;
38     zword size = zargs[2];
39     zbyte value;
40     int i;
41
42     if (zargs[1] == 0)                                  /* zero table */
43
44         for (i = 0; i < size; i++)
45             storeb ((zword) (zargs[0] + i), 0);
46
47     else if ((short) size < 0 || zargs[0] > zargs[1])   /* copy forwards */
48
49         for (i = 0; i < (((short) size < 0) ? - (short) size : size); i++) {
50             addr = zargs[0] + i;
51             LOW_BYTE (addr, value)
52             storeb ((zword) (zargs[1] + i), value);
53         }
54
55     else                                                /* copy backwards */
56
57         for (i = size - 1; i >= 0; i--) {
58             addr = zargs[0] + i;
59             LOW_BYTE (addr, value)
60             storeb ((zword) (zargs[1] + i), value);
61         }
62
63 }/* z_copy_table */
64
65 /*
66  * z_loadb, store a value from a table of bytes.
67  *
68  *      zargs[0] = address of table
69  *      zargs[1] = index of table entry to store
70  *
71  */
72
73 void z_loadb (void)
74 {
75     zword addr = zargs[0] + zargs[1];
76     zbyte value;
77
78     LOW_BYTE (addr, value)
79
80     store (value);
81
82 }/* z_loadb */
83
84 /*
85  * z_loadw, store a value from a table of words.
86  *
87  *      zargs[0] = address of table
88  *      zargs[1] = index of table entry to store
89  *
90  */
91
92 void z_loadw (void)
93 {
94     zword addr = zargs[0] + 2 * zargs[1];
95     zword value;
96
97     LOW_WORD (addr, value)
98
99     store (value);
100
101 }/* z_loadw */
102
103 /*
104  * z_scan_table, find and store the address of a target within a table.
105  *
106  *      zargs[0] = target value to be searched for
107  *      zargs[1] = address of table
108  *      zargs[2] = number of table entries to check value against
109  *      zargs[3] = type of table (optional, defaults to 0x82)
110  *
111  * Note: The table is a word array if bit 7 of zargs[3] is set; otherwise
112  *       it's a byte array. The lower bits hold the address step.
113  *
114  */
115
116 void z_scan_table (void)
117 {
118     zword addr = zargs[1];
119     int i;
120
121     /* Supply default arguments */
122
123     if (zargc < 4)
124         zargs[3] = 0x82;
125
126     /* Scan byte or word array */
127
128     for (i = 0; i < zargs[2]; i++) {
129
130         if (zargs[3] & 0x80) {  /* scan word array */
131
132             zword wvalue;
133
134             LOW_WORD (addr, wvalue)
135
136             if (wvalue == zargs[0])
137                 goto finished;
138
139         } else {                /* scan byte array */
140
141             zbyte bvalue;
142
143             LOW_BYTE (addr, bvalue)
144
145             if (bvalue == zargs[0])
146                 goto finished;
147
148         }
149
150         addr += zargs[3] & 0x7f;
151
152     }
153
154     addr = 0;
155
156 finished:
157
158     store (addr);
159     branch (addr);
160
161 }/* z_scan_table */
162
163 /*
164  * z_storeb, write a byte into a table of bytes.
165  *
166  *      zargs[0] = address of table
167  *      zargs[1] = index of table entry
168  *      zargs[2] = value to be written
169  *
170  */
171
172 void z_storeb (void)
173 {
174
175     storeb ((zword) (zargs[0] + zargs[1]), zargs[2]);
176
177 }/* z_storeb */
178
179 /*
180  * z_storew, write a word into a table of words.
181  *
182  *      zargs[0] = address of table
183  *      zargs[1] = index of table entry
184  *      zargs[2] = value to be written
185  *
186  */
187
188 void z_storew (void)
189 {
190
191     storew ((zword) (zargs[0] + 2 * zargs[1]), zargs[2]);
192
193 }/* z_storew */