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
|
#include "core.h"
#include <stdint.h>
#define MSP430_SR_C (1 << 0)
#define MSP430_SR_Z (1 << 1)
#define MSP430_SR_N (1 << 2)
#define MSP430_SR_V (1 << 8)
// r0 = pc
// r1 = sp
// r2 = sr
static int msp430_do_cycle_jump(msp430_t *state, uint16_t opcode);
int msp430_do_cycle_single_operand(msp430_t *state, uint16_t opcode);
int msp430_do_cycle_dual_operand(msp430_t *state, uint16_t opcode);
int msp430_do_cycle_single_operand_byte(msp430_t *state, uint16_t opcode);
int msp430_do_cycle_dual_operand_byte(msp430_t *state, uint16_t opcode);
uint16_t *msp430_do_cycle_get_single_operand(msp430_t *state, uint16_t opcode);
uint16_t *msp430_do_cycle_get_source_operand(msp430_t *state, uint16_t opcode);
uint16_t *msp430_do_cycle_get_dest_operand(msp430_t *state, uint16_t opcode);
static inline int msp430_get_opcode_bw(uint16_t opcode);
void msp430_init_state(msp430_t *state, void *mem)
{
for (int i = 0; i < 16; ++i)
state->reg[i] = 0;
state->mem = mem;
}
int msp430_do_cycle(msp430_t *state)
{
uint16_t pc = state->reg[0];
uint16_t opcode = *((uint16_t *)(state->mem + pc));
// Check for program end...
if (opcode == 0x4130 && state->reg[1] == 0)
return 1;
state->reg[0] = pc + 2;
int ret;
if ((opcode & 0xE000) == 0x2000) {
ret = msp430_do_cycle_jump(state, opcode);
} else {
int bw = msp430_get_opcode_bw(opcode);
if (bw == 0) {
if ((opcode & 0xFC00) == 0x1000)
ret = msp430_do_cycle_single_operand(state, opcode);
else
ret = msp430_do_cycle_dual_operand(state, opcode);
} else {
if ((opcode & 0xFC00) == 0x1000)
ret = msp430_do_cycle_single_operand_byte(state, opcode);
else
ret = msp430_do_cycle_dual_operand_byte(state, opcode);
}
}
return ret;
}
static void msp430_jump_to_offset(msp430_t *state, uint16_t opcode)
{
uint16_t addr = opcode & 0x3FF;
if (addr & (1 << 9))
addr |= 0xFC00;
int16_t saddr = (int16_t)addr * 2;
state->reg[0] += saddr;
}
int msp430_do_cycle_jump(msp430_t *state, uint16_t opcode)
{
// PC to become PC + ((opcode & 0x3FF) << 1)
switch ((opcode & 0x01C0) >> 10) {
case 0:
// JNE/JZ
if ((state->reg[2] & MSP430_SR_Z) == 0)
msp430_jump_to_offset(state, opcode);
break;
case 1:
// JEQ/JZ
if ((state->reg[2] & MSP430_SR_Z) == MSP430_SR_Z)
msp430_jump_to_offset(state, opcode);
break;
case 2:
// JNC/JLO
if ((state->reg[2] & MSP430_SR_C) == 0)
msp430_jump_to_offset(state, opcode);
break;
case 3:
// JC/JHS
if ((state->reg[2] & MSP430_SR_C) == MSP430_SR_C)
msp430_jump_to_offset(state, opcode);
break;
case 4:
// JN
if ((state->reg[2] & MSP430_SR_N) == MSP430_SR_N)
msp430_jump_to_offset(state, opcode);
break;
case 5: {
// JGE
uint16_t flags = state->reg[2] & (MSP430_SR_N | MSP430_SR_V);
if (flags == 0 || flags == (MSP430_SR_N | MSP430_SR_V))
msp430_jump_to_offset(state, opcode);
break; }
case 6: {
// JL
uint16_t flags = state->reg[2] & (MSP430_SR_N | MSP430_SR_V);
if (flags == MSP430_SR_N || flags == MSP430_SR_V)
msp430_jump_to_offset(state, opcode);
break; }
case 7:
// JMP
msp430_jump_to_offset(state, opcode);
break;
default:
return -1;
}
return 0;
}
uint16_t *msp430_do_cycle_get_operand(msp430_t *state, int AS, int BW, int SOURCE)
{
static uint16_t constants[6] = {
0, 1, 2, -1, 4, 8
};
if (SOURCE == 0) {
if (AS == 1) {
// Operand at PC + X
// X = word after PC
uint16_t offset = *((uint16_t *)(state->mem + state->reg[0]));
uint16_t *ret = (uint16_t *)(state->mem + state->reg[0] + offset);
state->reg[0] += 2;
return ret;
} else if (AS == 3) {
// Operand is X
uint16_t *ret = (uint16_t *)(state->mem + state->reg[0]);
state->reg[0] += 2;
return ret;
}
}
if (SOURCE == 2) {
switch (AS) {
case 0:
// R2
return &state->reg[2];
break;
case 1: {
// Operand at X
uint16_t offset = *((uint16_t *)(state->mem + state->reg[0]));
uint16_t *ret = (uint16_t *)(state->mem + offset);
state->reg[0] += 2;
return ret;
break; }
case 2:
return &constants[4];
break;
case 3:
return &constants[5];
break;
}
} else if (SOURCE == 3) {
// Constants
return &constants[AS];
} else {
switch (AS) {
case 0:
// Register
return &state->reg[SOURCE];
break;
case 1: {
// Operand at Rn + X
uint16_t offset = *((uint16_t *)(state->mem + state->reg[0]));
uint16_t *ret = (uint16_t *)(state->mem + state->reg[SOURCE] + offset);
state->reg[0] += 2;
return ret;
break; }
case 2:
// Operand at Rn
return (uint16_t *)(state->mem + state->reg[SOURCE]);
break;
case 3: {
// Operand at Rn, increment Rn
uint16_t *ret = (uint16_t *)(state->mem + state->reg[SOURCE]);
state->reg[SOURCE] += BW ? 1 : 2;
return ret;
break; }
}
}
return 0; // Failed...
}
inline int msp430_get_opcode_bw(uint16_t opcode)
{
return opcode & (1 << 6);
}
uint16_t *msp430_do_cycle_get_single_operand(msp430_t *state, uint16_t opcode)
{
return msp430_do_cycle_get_operand(state,
/* AS */ (opcode & 0x30) >> 4,
/* BW */ msp430_get_opcode_bw(opcode),
/* SOURCE */ (opcode & 0xF));
}
uint16_t *msp430_do_cycle_get_source_operand(msp430_t *state, uint16_t opcode)
{
return msp430_do_cycle_get_operand(state,
/* AS */ (opcode & 0x30) >> 4,
/* BW */ msp430_get_opcode_bw(opcode),
/* SOURCE */ (opcode & 0xF00) >> 8);
}
uint16_t *msp430_do_cycle_get_dest_operand(msp430_t *state, uint16_t opcode)
{
int AD = opcode & 0x80;
int DEST = opcode & 0xF;
if (AD == 0) {
// Byte operation? Clear MSB.
if (msp430_get_opcode_bw(opcode) == 0) {
return &state->reg[DEST];
} else {
uint16_t *ret = &state->reg[DEST];
*ret &= 0xFF;
return ret;
}
} else {
if (DEST == 0) {
// dest at PC + X
uint16_t offset = *((uint16_t *)(state->mem + state->reg[0]));
uint16_t *ret = (uint16_t *)(state->mem + state->reg[0] + offset);
state->reg[0] += 2;
return ret;
} else if (DEST == 2) {
// dest at X
uint16_t offset = *((uint16_t *)(state->mem + state->reg[0]));
uint16_t *ret = (uint16_t *)(state->mem + offset);
state->reg[0] += 2;
return ret;
} else {
// dest at Rn + X
uint16_t offset = *((uint16_t *)(state->mem + state->reg[0]));
uint16_t *ret = (uint16_t *)(state->mem + state->reg[DEST] + offset);
state->reg[0] += 2;
return ret;
}
}
return 0;
}
|