-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop_jmp.c
162 lines (134 loc) · 3.28 KB
/
op_jmp.c
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
/* 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 [email protected]
*/
#include "nitfol.h"
N_INLINE static void skip_branch(zbyte branch)
{
if(!(branch & b01000000)) /* Bit 6 clear means 'branch occupies two bytes' */
PC++;
}
N_INLINE static void take_branch(zbyte branch)
{
int o = branch & b00111111;
if(!(branch & b01000000)) {/* Bit 6 clear means 'branch occupies two bytes'*/
o = (o << 8) + HIBYTE(PC);
PC++;
if(branch & b00100000)
o = -((1 << 14) - o);
}
if(o == 0)
mop_func_return(0);
else if(o == 1)
mop_func_return(1);
else
PC += o - 2;
#ifndef FAST
if(PC > game_size) {
n_show_error(E_INSTR, "attempt to conditionally jump outside of story", o - 2);
PC -= o - 2;
return;
}
#endif
/* printf("cjmp %x -> %x\n", oldPC, PC); */
}
void mop_skip_branch(void)
{
zbyte branch = HIBYTE(PC);
PC++;
if(branch & b10000000) /* Bit 7 set means 'branch when true' */
skip_branch(branch);
else
take_branch(branch);
}
void mop_take_branch(void)
{
zbyte branch = HIBYTE(PC);
PC++;
if(branch & b10000000) /* Bit 7 set means 'branch when true' */
take_branch(branch);
else
skip_branch(branch);
}
void mop_cond_branch(BOOL cond)
{
zbyte branch = HIBYTE(PC);
PC++;
if((branch >> 7) ^ cond)
skip_branch(branch);
else
take_branch(branch);
}
void op_je(void)
{
int i;
for(i = 1; i < numoperands; i++)
if(operand[0] == operand[i]) {
mop_take_branch();
return;
}
mop_skip_branch();
}
void op_jg(void)
{
if(is_greaterthan(operand[0], operand[1]))
mop_take_branch();
else
mop_skip_branch();
}
void op_jl(void)
{
if(is_lessthan(operand[0], operand[1]))
mop_take_branch();
else
mop_skip_branch();
}
void op_jump(void)
{
operand[0] -= 2; /* not documented in zspec */
if(is_neg(operand[0])) {
#ifndef FAST
if(neg(operand[0]) > PC) {
n_show_error(E_INSTR, "attempt to jump before beginning of story", -neg(operand[0]));
return;
}
#endif
PC -= neg(operand[0]);
}
else {
PC += operand[0];
if(PC > game_size) {
n_show_error(E_INSTR, "attempt to jump past end of story", operand[0]);
PC -= operand[0];
return;
}
}
/* printf("jump %x -> %x\n", oldPC, PC); */
}
void op_jz(void)
{
mop_cond_branch(operand[0] == 0);
}
void op_test(void)
{
mop_cond_branch((operand[0] & operand[1]) == operand[1]);
}
void op_verify(void)
{
mop_cond_branch(LOWORD(HD_CHECKSUM) == z_checksum);
}
void op_piracy(void)
{
mop_cond_branch(aye_matey);
}