-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathop_table.c
More file actions
170 lines (148 loc) · 3.94 KB
/
op_table.c
File metadata and controls
170 lines (148 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Nitfol - z-machine interpreter using Glk for output.
Copyright (C) 1999 Evin Robertson
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
The author can be reached at nitfol@deja.com
*/
#include "nitfol.h"
void op_copy_table(void)
{
offset i;
zword first = operand[0];
zword second = operand[1];
zword size = operand[2];
if(second == 0) { /* zero 'first' bytes */
for(i = 0; i < size; i++)
LOBYTEwrite(first + i, 0);
} else {
if(first > second || is_neg(size)) {
if(is_neg(size))
size = neg(size);
for(i = 0; i < size; i++) /* copy forward */
LOBYTEcopy(second + i, first + i);
}
else {
for(i = 0; i < size; i++) /* copy backward */
LOBYTEcopy(second + size - i - 1, first + size - i - 1);
}
}
}
void op_scan_table(void)
{
zword i;
int form = operand[3];
zword address = operand[1];
if(numoperands < 4)
form = 0x82; /* default form - scan for words, increment by two bytes */
if(form & b10000000) { /* Bit 8 set means scan for words */
for(i = 0; i < operand[2]; i++) {
if(LOWORD(address) == operand[0]) {
mop_store_result(address);
mop_take_branch();
return;
}
address += form & b01111111; /* Bottom 7 bits give amount to increment */
}
mop_store_result(0);
mop_skip_branch();
} else { /* Bit 8 clear means scan for bytes */
for(i = 0; i < operand[2]; i++) {
if(LOBYTE(address) == operand[0]) {
mop_store_result(address);
mop_take_branch();
return;
}
address += form & b01111111;
}
mop_store_result(0);
mop_skip_branch();
}
}
void op_loadb(void)
{
mop_store_result(LOBYTE(operand[0] + operand[1]));
}
void op_loadw(void)
{
mop_store_result(LOWORD(operand[0] + operand[1] * ZWORD_SIZE));
}
static void z_write_header(zword i, zbyte val)
{
zbyte diff = LOBYTE(i) ^ val;
if(diff == 0)
return;
if(i >= 0x40) {
LOBYTEwrite(i, val);
return;
}
if(i != HD_FLAGS2 + 1) {
n_show_error(E_MEMORY, "attempt to write to non-dynamic byte in header", i);
return;
}
if(diff > b00000111) {
n_show_error(E_MEMORY, "attempt to change non-dynamic bits in flags2", val);
return;
}
LOBYTEwrite(i, val);
if(diff & b00000001) {
if(val & b00000001) {
operand[0] = 2;
op_output_stream();
} else {
operand[0] = neg(2);
op_output_stream();
}
}
if(diff & b00000010) {
if(val & b00000010) {
set_fixed(TRUE);
} else {
set_fixed(FALSE);
}
}
}
void op_storeb(void)
{
zword addr = operand[0] + operand[1];
if(addr < 0x40)
z_write_header(addr, (zbyte) operand[2]);
else
LOBYTEwrite(addr, operand[2]);
}
void op_storew(void)
{
zword addr = operand[0] + operand[1] * ZWORD_SIZE;
if(addr < 0x40) {
z_write_header(addr, (zbyte) (operand[2] >> 8));
z_write_header(addr + 1, (zbyte) (operand[2] & 0xff));
} else {
LOWORDwrite(addr, operand[2]);
}
}
void header_extension_write(zword w, zword val)
{
w *= ZWORD_SIZE;
if(z_headerext == 0)
return;
if(LOWORD(z_headerext) < w)
return;
LOWORDwrite(z_headerext + w, val);
}
zword header_extension_read(unsigned w)
{
w *= ZWORD_SIZE;
if(z_headerext == 0)
return 0;
if(LOWORD(z_headerext) < w)
return 0;
return LOWORD(z_headerext + w);
}