-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruction.h
153 lines (140 loc) · 2.47 KB
/
instruction.h
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
#ifndef INSTRUCTION_H
#define INSTRUCTION_H
#include <cstdint>
enum RegName
{
R0, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15,
SP = R14,
PC = R15,
MaxReg,
};
enum OperandSize
{
Op8,
Op16,
Op32,
};
class Register
{
public:
Register(uint32_t v = 0) : value(v) {}
uint32_t& Value() { return value; }
void Value(uint32_t v) { value = v; }
Register& operator+=(uint32_t v) { value += v; return *this; }
Register& operator-=(uint32_t v) { value -= v; return *this; }
private:
uint32_t value;
};
class FlagRegister
{
public:
union
{
struct
{
bool n:1;
bool z:1;
bool v:1;
bool c:1;
};
uint8_t word;
};
};
enum AddrMode
{
Direct, /* Rn */
Indir, /* [Rn] */
IndirAutoInc, /* [Rn]+ */
AutoDecIndir, /* -[Rn] */
};
enum InstrKind
{
/* Regular two operand instructions */
NOP = 0, /* Ignored size, source, dest. */
MOV,
CMP,
ADD,
ADC,
SUB,
SBC,
MUL,
DIV,
AND,
OR,
XOR,
NEG,
COM, /* Complement = NOT = ~ */
ASR, /* Arithmetic shift */
ASL, /* Arithmetic shift */
LSR, /* Logic shift */
LSL,
ROR, /* Rotate */
ROL,
CLC,
CLV,
CLN,
CLZ,
SEC,
SEV,
SEN,
SEZ,
/* Flow control unconditional - ignores dest operands and operand size */
JSR = 32,
RET,
JMP,
HLT, /* Stop execution */
BPT, /* Breakpoint */
/* Branch instructions */
BEQ = 48,
BNE,
BLT,
BGT,
BGE,
BLE,
BHI, /* Higher than (unsigned) */
BLOS, /* Lower or same */
BCC, /* Carry clear */
BHIS = BCC, /* Higher or same (unsigned) */
BCS, /* Carry set */
BLO = BCS,
BMI, /* Negative */
BPL, /* Positive */
BVC, /* Overflow clear */
BVS, /* Overflow set */
BR,
/* Special type instructions */
EMT = 64, /* Emulation trap - call OS */
MAX_INST = 255
};
class Instruction
{
public:
Instruction() { value.word = 0; }
union Instr
{
struct
{
union
{
struct /* Regular two operands */
{
RegName source:8;
RegName dest:8;
AddrMode srcMode:2;
AddrMode destMode:2;
uint32_t unused:2;
OperandSize size:2;
InstrKind op:8;
};
struct /* Branch instructions */
{
int32_t branch:24;
InstrKind dummy_op:8;
};
};
};
uint32_t word;
};
Instr value;
};
#endif