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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! The branch-and-call family: B*/PB*, GO/GOI, PUSHGO/PUSHGOI, JMP/JMPB,
//! PUSHJ/PUSHJB, GETA/GETAB and POP.
use super::super::{MMix, PopFrame, SpecialReg};
use crate::mmixal::Opcode;
impl MMix {
/// Conditional branch forward: if cond, PC = PC + 4*YZ.
#[inline]
fn branch_forward(&mut self, cond: bool, y: u8, z: u8) {
if cond {
let yz = ((y as u16) << 8) | (z as u16);
self.pc = self.pc.wrapping_add((yz as u64) * 4);
} else {
self.advance_pc();
}
}
/// Conditional branch backward: if cond, PC = PC + 4*(YZ - 65536).
#[inline]
fn branch_backward(&mut self, cond: bool, y: u8, z: u8) {
if cond {
// YZ is unsigned in both directions; the backward opcode carries the
// sign, so YZ = 0 is -65536 tetras and YZ = 65535 is -1.
let yz = ((y as u16) << 8) | (z as u16);
let offset = yz as i64 - 65536;
self.pc = self.pc.wrapping_add((offset * 4) as u64);
} else {
self.advance_pc();
}
}
/// Dispatches the branch-and-call family; `dispatch` routes only these
/// opcodes here.
pub(super) fn dispatch_control(
&mut self,
opcode: Opcode,
_op_byte: u8,
x: u8,
y: u8,
z: u8,
) -> bool {
match opcode {
// Special Load/Store instructions (0x9E-0x9F): GO, GOI
Opcode::GO => {
// GO $X, $Y, $Z - Go to location. u($X) <- @+4 mod 2^64,
// per the Instruction Reference's GO page.
let addr = self.get_register(y).wrapping_add(self.get_register(z));
self.set_register(x, self.pc.wrapping_add(4));
self.pc = addr;
true
}
Opcode::GOI => {
// GOI $X, $Y, Z - Go to location immediate. Same rule as GO.
let addr = self.get_register(y).wrapping_add(z as u64);
self.set_register(x, self.pc.wrapping_add(4));
self.pc = addr;
true
}
// Store-range call instructions (0xBE-0xBF): PUSHGO, PUSHGOI
Opcode::PUSHGO => {
// PUSHGO $X, $Y, $Z - Push registers and go (absolute target $Y+$Z)
let target = self.get_register(y).wrapping_add(self.get_register(z));
self.push_frame(x);
self.set_pc(target);
true
}
Opcode::PUSHGOI => {
// PUSHGOI $X, $Y, Z - Push registers and go (absolute target $Y+Z)
let target = self.get_register(y).wrapping_add(z as u64);
self.push_frame(x);
self.set_pc(target);
true
}
// Branch instructions - opcodes 0x40-0x5F
Opcode::BN => {
// BN $X, $Y, Z - Branch if negative
let cond = (self.get_register(x) as i64) < 0;
self.branch_forward(cond, y, z);
true
}
Opcode::BNB => {
// BNB $X, $Y, Z - Branch if negative (backward)
let cond = (self.get_register(x) as i64) < 0;
self.branch_backward(cond, y, z);
true
}
Opcode::BZ => {
// BZ $X, $Y, Z - Branch if zero
let cond = self.get_register(x) == 0;
self.branch_forward(cond, y, z);
true
}
Opcode::BZB => {
// BZB $X, $Y, Z - Branch if zero (backward)
let cond = self.get_register(x) == 0;
self.branch_backward(cond, y, z);
true
}
Opcode::BP => {
// BP $X, $Y, Z - Branch if positive
let cond = (self.get_register(x) as i64) > 0;
self.branch_forward(cond, y, z);
true
}
Opcode::BPB => {
// BPB $X, $Y, Z - Branch if positive (backward)
let cond = (self.get_register(x) as i64) > 0;
self.branch_backward(cond, y, z);
true
}
Opcode::BOD => {
// BOD $X, $Y, Z - Branch if odd
let cond = (self.get_register(x) & 1) != 0;
self.branch_forward(cond, y, z);
true
}
Opcode::BODB => {
// BODB $X, $Y, Z - Branch if odd (backward)
let cond = (self.get_register(x) & 1) != 0;
self.branch_backward(cond, y, z);
true
}
Opcode::BNN => {
// BNN $X, $Y, Z - Branch if non-negative
let cond = (self.get_register(x) as i64) >= 0;
self.branch_forward(cond, y, z);
true
}
Opcode::BNNB => {
// BNNB $X, $Y, Z - Branch if non-negative (backward)
let cond = (self.get_register(x) as i64) >= 0;
self.branch_backward(cond, y, z);
true
}
Opcode::BNZ => {
// BNZ $X, $Y, Z - Branch if non-zero
let cond = self.get_register(x) != 0;
self.branch_forward(cond, y, z);
true
}
Opcode::BNZB => {
// BNZB $X, $Y, Z - Branch if non-zero (backward)
let cond = self.get_register(x) != 0;
self.branch_backward(cond, y, z);
true
}
Opcode::BNP => {
// BNP $X, $Y, Z - Branch if non-positive
let cond = (self.get_register(x) as i64) <= 0;
self.branch_forward(cond, y, z);
true
}
Opcode::BNPB => {
// BNPB $X, $Y, Z - Branch if non-positive (backward)
let cond = (self.get_register(x) as i64) <= 0;
self.branch_backward(cond, y, z);
true
}
Opcode::BEV => {
// BEV $X, $Y, Z - Branch if even
let cond = (self.get_register(x) & 1) == 0;
self.branch_forward(cond, y, z);
true
}
Opcode::BEVB => {
// BEVB $X, $Y, Z - Branch if even (backward)
let cond = (self.get_register(x) & 1) == 0;
self.branch_backward(cond, y, z);
true
}
Opcode::PBN => {
// PBN $X, $Y, Z - Probable branch if negative
let cond = (self.get_register(x) as i64) < 0;
self.branch_forward(cond, y, z);
true
}
Opcode::PBNB => {
// PBNB $X, $Y, Z - Probable branch if negative (backward)
let cond = (self.get_register(x) as i64) < 0;
self.branch_backward(cond, y, z);
true
}
Opcode::PBZ => {
// PBZ $X, $Y, Z - Probable branch if zero
let cond = self.get_register(x) == 0;
self.branch_forward(cond, y, z);
true
}
Opcode::PBZB => {
// PBZB $X, $Y, Z - Probable branch if zero (backward)
let cond = self.get_register(x) == 0;
self.branch_backward(cond, y, z);
true
}
Opcode::PBP => {
// PBP $X, $Y, Z - Probable branch if positive
let cond = (self.get_register(x) as i64) > 0;
self.branch_forward(cond, y, z);
true
}
Opcode::PBPB => {
// PBPB $X, $Y, Z - Probable branch if positive (backward)
let cond = (self.get_register(x) as i64) > 0;
self.branch_backward(cond, y, z);
true
}
Opcode::PBOD => {
// PBOD $X, $Y, Z - Probable branch if odd
let cond = (self.get_register(x) & 1) != 0;
self.branch_forward(cond, y, z);
true
}
Opcode::PBODB => {
// PBODB $X, $Y, Z - Probable branch if odd (backward)
let cond = (self.get_register(x) & 1) != 0;
self.branch_backward(cond, y, z);
true
}
Opcode::PBNN => {
// PBNN $X, $Y, Z - Probable branch if non-negative
let cond = (self.get_register(x) as i64) >= 0;
self.branch_forward(cond, y, z);
true
}
Opcode::PBNNB => {
// PBNNB $X, $Y, Z - Probable branch if non-negative (backward)
let cond = (self.get_register(x) as i64) >= 0;
self.branch_backward(cond, y, z);
true
}
Opcode::PBNZ => {
// PBNZ $X, $Y, Z - Probable branch if non-zero
let cond = self.get_register(x) != 0;
self.branch_forward(cond, y, z);
true
}
Opcode::PBNZB => {
// PBNZB $X, $Y, Z - Probable branch if non-zero (backward)
let cond = self.get_register(x) != 0;
self.branch_backward(cond, y, z);
true
}
Opcode::PBNP => {
// PBNP $X, $Y, Z - Probable branch if non-positive
let cond = (self.get_register(x) as i64) <= 0;
self.branch_forward(cond, y, z);
true
}
Opcode::PBNPB => {
// PBNPB $X, $Y, Z - Probable branch if non-positive (backward)
let cond = (self.get_register(x) as i64) <= 0;
self.branch_backward(cond, y, z);
true
}
Opcode::PBEV => {
// PBEV $X, $Y, Z - Probable branch if even
let cond = (self.get_register(x) & 1) == 0;
self.branch_forward(cond, y, z);
true
}
Opcode::PBEVB => {
// PBEVB $X, $Y, Z - Probable branch if even (backward)
let cond = (self.get_register(x) & 1) == 0;
self.branch_backward(cond, y, z);
true
}
// Jump/Stack instructions - opcodes 0xF0-0xF5, 0xF8
Opcode::JMP => {
// JMP XYZ - Jump to PC + 4*XYZ.
let xyz = ((x as u32) << 16) | ((y as u32) << 8) | (z as u32);
self.pc = self.pc.wrapping_add((xyz as u64) * 4);
true
}
Opcode::JMPB => {
// JMPB XYZ - Jump to PC + 4*(XYZ - 2^24).
let xyz = ((x as u32) << 16) | ((y as u32) << 8) | (z as u32);
let offset = xyz as i64 - (1 << 24);
self.pc = self.pc.wrapping_add((offset * 4) as u64);
true
}
Opcode::PUSHJ => {
// PUSHJ $X, YZ - Push registers and jump to PC + 4*YZ.
let pc_at_inst = self.pc;
self.push_frame(x);
let yz = ((y as u16) << 8) | z as u16;
self.pc = pc_at_inst.wrapping_add((yz as u64) * 4);
true
}
Opcode::PUSHJB => {
// PUSHJB $X, YZ - Push registers and jump to PC + 4*(YZ - 65536).
let pc_at_inst = self.pc;
self.push_frame(x);
let yz = ((y as u16) << 8) | z as u16;
let offset = yz as i64 - 65536;
self.pc = pc_at_inst.wrapping_add((offset * 4) as u64);
true
}
Opcode::GETA => {
// GETA $X, YZ - Address PC + 4*YZ.
let yz = ((y as u16) << 8) | z as u16;
let addr = self.pc.wrapping_add((yz as u64) * 4);
self.set_register(x, addr);
self.advance_pc();
true
}
Opcode::GETAB => {
// GETAB $X, YZ - Address PC + 4*(YZ - 65536).
let yz = ((y as u16) << 8) | z as u16;
let offset = yz as i64 - 65536;
let addr = self.pc.wrapping_add((offset * 4) as u64);
self.set_register(x, addr);
self.advance_pc();
true
}
Opcode::POP => {
// POP X, YZ - Pop frame and return.
// Puts the callee's last output in the hole and the rest
// above it in order, restores caller's locals below the
// hole, and branches to rJ + 4·YZ. rJ itself is untouched;
// a subroutine that calls another must save and restore it.
let yz = ((y as u16) << 8) | z as u16;
match self.pop_frame(x, yz) {
PopFrame::Frame(target) => {
self.pc = target;
true
}
PopFrame::NoFrame => {
// No frame to pop: branch via current rJ as a defensive fallback.
self.pc = self
.get_special(SpecialReg::RJ)
.wrapping_add((yz as u64) * 4);
true
}
PopFrame::Rejected => false,
}
}
_ => unreachable!("dispatch routes only branch-and-call opcodes to dispatch_control"),
}
}
}