-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch.cpp
91 lines (77 loc) · 2.13 KB
/
branch.cpp
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
// Copyright 2010-2021 Chris Spiegel.
//
// This file is part of Bocfel.
//
// Bocfel is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version
// 2 or 3, as published by the Free Software Foundation.
//
// Bocfel 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 Bocfel. If not, see <http://www.gnu.org/licenses/>.
#include "branch.h"
#include "memory.h"
#include "process.h"
#include "stack.h"
#include "types.h"
#include "util.h"
void branch_if(bool do_branch)
{
uint8_t branch;
uint16_t offset;
branch = byte(pc++);
if (!do_branch) {
branch ^= 0x80;
}
offset = branch & 0x3f;
if ((branch & 0x40) == 0) {
offset = (offset << 8) | byte(pc++);
// Get the sign right.
if ((offset & 0x2000) == 0x2000) {
offset |= 0xc000;
}
}
if ((branch & 0x80) == 0x80) {
if (offset > 1) {
pc += as_signed(offset) - 2;
ZASSERT(pc < memory_size, "branch to invalid address 0x%lx", static_cast<unsigned long>(pc));
} else {
do_return(offset);
}
}
}
void zjump()
{
// -= 2 because pc has been advanced past the jump instruction.
pc += as_signed(zargs[0]);
pc -= 2;
ZASSERT(pc < memory_size, "@jump to invalid address 0x%lx", static_cast<unsigned long>(pc));
}
void zjz()
{
branch_if(zargs[0] == 0);
}
void zje()
{
if (znargs == 1) {
branch_if(false);
} else if (znargs == 2) {
branch_if(zargs[0] == zargs[1]);
} else if (znargs == 3) {
branch_if(zargs[0] == zargs[1] || zargs[0] == zargs[2]);
} else {
branch_if(zargs[0] == zargs[1] || zargs[0] == zargs[2] || zargs[0] == zargs[3]);
}
}
void zjl()
{
branch_if(as_signed(zargs[0]) < as_signed(zargs[1]));
}
void zjg()
{
branch_if(as_signed(zargs[0]) > as_signed(zargs[1]));
}