blob: 247d3b4da85b170b0dc4af9134499e01261b2ce4 (
plain)
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
|
#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
typedef struct {
uint16_t reg[16];
uint8_t *mem;
} msp430_t;
extern uint16_t *msp430_do_cycle_get_single_operand(msp430_t *state, uint16_t opcode);
extern uint16_t *msp430_do_cycle_get_source_operand(msp430_t *state, uint16_t opcode);
extern uint16_t *msp430_do_cycle_get_dest_operand(msp430_t *state, uint16_t opcode);
int msp430_do_cycle_single_operand_byte(msp430_t *state, uint16_t opcode)
{
uint8_t *operand = (uint8_t *)msp430_do_cycle_get_single_operand(state, opcode);
if (operand == 0)
return -1;
switch ((opcode & 0x0380) >> 7) {
case 0: {
// RRC
uint8_t res = *operand;
uint16_t sr = 0;
if (res & 1)
sr |= MSP430_SR_C;
res >>= 1;
if (state->reg[2] & MSP430_SR_C)
res |= 0x80;
if (res == 0)
sr |= MSP430_SR_Z;
if ((int8_t)res < 0)
sr |= MSP430_SR_N;
*operand = res;
state->reg[2] = sr;
break; }
case 2: {
// RRA
uint8_t res = *operand;
uint16_t sr = 0;
if (res & 1)
sr |= MSP430_SR_C;
res >>= 1;
if (res == 0)
sr |= MSP430_SR_Z;
if ((int8_t)res < 0)
sr |= MSP430_SR_N;
*operand = res;
state->reg[2] = sr;
break; }
case 4: {
// PUSH
uint16_t sp = state->reg[1] - 2;
*((uint16_t *)(state->mem + sp)) = *operand;
state->reg[1] = sp;
break; }
default:
return -1;
break;
}
return 0;
}
int msp430_do_cycle_dual_operand_byte(msp430_t *state, uint16_t opcode)
{
uint8_t *src = (uint8_t *)msp430_do_cycle_get_source_operand(state, opcode);
uint8_t *dst = (uint8_t *)msp430_do_cycle_get_dest_operand(state, opcode);
if (src == 0 || dst == 0)
return -1;
switch ((opcode & 0xF000) >> 12) {
case 4:
// MOV
*dst = *src;
break;
case 5: {
// ADD
uint16_t res = *src + *dst;
uint16_t sr = 0;
if ((int8_t)res < 0)
sr |= MSP430_SR_N;
if (res == 0)
sr |= MSP430_SR_Z;
if ((res & 0xFF00) != 0)
sr |= MSP430_SR_C;
if (((*src & 0x80) ^ (*src & 0x80)) == 0 && (*src & 0x80) != (res & 0x80))
sr |= MSP430_SR_V;
*dst = res & 0xFF;
state->reg[2] = sr;
break; }
case 6: {
// ADDC
uint16_t res = *src + *dst + (state->reg[2] & MSP430_SR_C);
uint16_t sr = 0;
if ((int8_t)res < 0)
sr |= MSP430_SR_N;
if (res == 0)
sr |= MSP430_SR_Z;
if ((res & 0xFF00) != 0)
sr |= MSP430_SR_C;
if (((*src & 0x80) ^ (*src & 0x80)) == 0 && (*src & 0x80) != (res & 0x80))
sr |= MSP430_SR_V;
*dst = res & 0xFF;
state->reg[2] = sr;
break; }
case 7: {
// SUBC
uint16_t res = *dst + ~(*src) + (state->reg[2] & MSP430_SR_C);
uint16_t sr = 0;
if ((int8_t)res < 0)
sr |= MSP430_SR_N;
if (res == 0)
sr |= MSP430_SR_Z;
if ((res & 0xFF00) != 0)
sr |= MSP430_SR_C;
if (((*src & 0x80) ^ (*src & 0x80)) == 0 && (*src & 0x80) != (res & 0x80))
sr |= MSP430_SR_V;
*dst = res & 0xFF;
state->reg[2] = sr;
break; }
case 8: {
// SUB
uint16_t res = *dst + ~(*src) + 1;
uint16_t sr = 0;
if ((int8_t)res < 0)
sr |= MSP430_SR_N;
if (res == 0)
sr |= MSP430_SR_Z;
if ((res & 0xFF00) != 0) // TODO confirm
sr |= MSP430_SR_C;
if (((*src & 0x80) ^ (*src & 0x80)) == 0 && (*src & 0x80) != (res & 0x80))
sr |= MSP430_SR_V;
*dst = res & 0xFF;
state->reg[2] = sr;
break; }
case 9: {
// CMP
uint16_t res = *dst + ~(*src) + 1;
uint16_t sr = 0;
if ((int8_t)res < 0)
sr |= MSP430_SR_N;
if (res == 0)
sr |= MSP430_SR_Z;
if ((res & 0xFF00) != 0)
sr |= MSP430_SR_C;
if (((*src & 0x80) ^ (*src & 0x80)) == 0 && (*src & 0x80) != (res & 0x80))
sr |= MSP430_SR_V;
state->reg[2] = sr;
break; }
case 10:
// DADD TODO
break;
case 11: {
// BIT
uint8_t res = *dst & *src;
uint16_t sr = 0;
if (res & 0x80)
sr |= MSP430_SR_N;
if (res == 0)
sr |= MSP430_SR_Z;
if (res != 0)
sr |= MSP430_SR_C;
state->reg[2] = sr;
break; }
case 12:
// BIC
*dst &= ~(*src);
break;
case 13:
// BIS
*dst |= *src;
break;
case 14: {
// XOR
uint8_t res = *dst ^ *src;
uint16_t sr = 0;
if (res & 0x80)
sr |= MSP430_SR_N;
if (res == 0)
sr |= MSP430_SR_Z;
if (res != 0)
sr |= MSP430_SR_C;
if ((*dst & 0x80) && (*src & 0x80))
sr |= MSP430_SR_V;
*dst = res;
state->reg[2] = sr;
break; }
case 15: {
// AND
uint8_t res = *dst & *src;
uint16_t sr = 0;
if (res & 0x80)
sr |= MSP430_SR_N;
if (res == 0)
sr |= MSP430_SR_Z;
if (res != 0)
sr |= MSP430_SR_C;
*dst = res;
state->reg[2] = sr;
break; }
default:
return -1;
}
return 0;
}
|