-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.c
367 lines (300 loc) · 10.9 KB
/
processor.c
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
// Created by kosmas on 5/12/21.
//
#include <stdint.h>
#include <pico/printf.h>
#include "processor.h"
uint8_t getBit(uint8_t byte, uint8_t bitToGet) {
return (byte >> bitToGet) & 1;
}
void setBit(uint8_t *byte, uint8_t bitToSet, uint8_t value) {
if (value == 1) {
*byte |= value << bitToSet;
}
else if (value == 0) {
*byte &= ~(1 << bitToSet);
}
}
uint8_t fetchRealOperand(AddressingMode addressingMode, uint8_t operand) {
switch (addressingMode) {
case ADDRESSING_MODE_IMMEDIATE:
return operand;
case ADDRESSING_MODE_MEMORY:
return memory[operand];
case ADDRESSING_MODE_INDIRECT:
return memory[memory[operand]];
case ADDRESSING_MODE_INDEXED:
return memory[operand + memory[X_REGISTER_ADDRESS]];
case ADDRESSING_MODE_INDIRECT_INDEXED:
return memory[memory[operand] + memory[X_REGISTER_ADDRESS]];
default:
break;
}
}
uint8_t determineRegisterToUse(uint8_t instruction) {
uint8_t mostSignificant2bits = instruction & 0xC0;
switch (mostSignificant2bits) {
case 0:
return A_REGISTER_ADDRESS;
case 0x40:
return B_REGISTER_ADDRESS;
case 0x80:
return X_REGISTER_ADDRESS;
default:
// Defying the rules of binary everybody
return 0;
}
}
AddressingMode determineAddressingMode(uint8_t instruction) {
uint8_t leastSignificant3bits = instruction & 0x7;
switch (leastSignificant3bits) {
case 0x3:
return ADDRESSING_MODE_IMMEDIATE;
case 0x4:
return ADDRESSING_MODE_MEMORY;
case 0x5:
return ADDRESSING_MODE_INDIRECT;
case 0x6:
return ADDRESSING_MODE_INDEXED;
case 0x7:
return ADDRESSING_MODE_INDIRECT_INDEXED;
default:
// How did we even get here?
return 0;
}
}
void add(uint8_t instruction) {
uint8_t registerToAddTo = determineRegisterToUse(instruction);
AddressingMode addressingMode = determineAddressingMode(instruction);
uint8_t operand = memory[PROGRAM_COUNTER_VALUE++];
uint8_t numberToAdd = fetchRealOperand(addressingMode, operand);
printf("Adding %d to %d", registerToAddTo, memory[registerToAddTo]);
uint16_t result = memory[registerToAddTo] + numberToAdd;
memory[registerToAddTo] += numberToAdd;
// The Overflow and Carry address for a register can be found by adding 0x81
setBit(&memory[registerToAddTo + 0x81], CARRY_BIT, result > 0xFF);
setBit(&memory[registerToAddTo + 0x81], OVERFLOW_BIT, result > 0x7F);
printf("0x%X is now %d from addition\n", registerToAddTo, memory[registerToAddTo]);
}
void sub(uint8_t instruction) {
uint8_t registerToSubtractFrom = determineRegisterToUse(instruction);
AddressingMode addressingMode = determineAddressingMode(instruction);
uint8_t operand = memory[PROGRAM_COUNTER_VALUE++];
uint8_t numberToSubtract = fetchRealOperand(addressingMode, operand);
uint16_t result = memory[registerToSubtractFrom] + numberToSubtract;
memory[registerToSubtractFrom] -= numberToSubtract;
// The Overflow and Carry address for a register can be found by adding 0x81
setBit(&memory[registerToSubtractFrom + 0x81], CARRY_BIT, result > 0xFF);
setBit(&memory[registerToSubtractFrom + 0x81], OVERFLOW_BIT, result > 0x7F);
}
void load(uint8_t instruction) {
uint8_t registerToLoadTo = determineRegisterToUse(instruction);
AddressingMode addressingMode = determineAddressingMode(instruction);
uint8_t operand = memory[PROGRAM_COUNTER_VALUE++];
uint8_t valueToLoad = fetchRealOperand(addressingMode, operand);
memory[registerToLoadTo] = valueToLoad;
printf("0x%X is now %d\n", registerToLoadTo, memory[registerToLoadTo]);
}
void store(uint8_t instruction) {
uint8_t registerToStore = determineRegisterToUse(instruction);
AddressingMode addressingMode = determineAddressingMode(instruction);
uint8_t operand = memory[PROGRAM_COUNTER_VALUE++];
uint8_t addressOfValueToStore = fetchRealOperand(addressingMode, operand);
memory[addressOfValueToStore] = memory[registerToStore];
printf("0x%X is now %d\n", addressOfValueToStore, memory[addressOfValueToStore]);
}
void logicalAnd(uint8_t instruction) {
AddressingMode addressingMode = determineAddressingMode(instruction);
uint8_t operand = memory[PROGRAM_COUNTER_VALUE++];
uint8_t numberToAnd = fetchRealOperand(addressingMode, operand);
memory[A_REGISTER_ADDRESS] &= numberToAnd;
}
void logicalOr(uint8_t instruction) {
AddressingMode addressingMode = determineAddressingMode(instruction);
uint8_t operand = memory[PROGRAM_COUNTER_VALUE++];
uint8_t numberToOr = fetchRealOperand(addressingMode, operand);
memory[A_REGISTER_ADDRESS] |= numberToOr;
}
void loadComplement(uint8_t instruction) {
AddressingMode addressingMode = determineAddressingMode(instruction);
uint8_t operand = memory[PROGRAM_COUNTER_VALUE++];
uint8_t numberToGetComplementOf = fetchRealOperand(addressingMode, operand);
memory[A_REGISTER_ADDRESS] = 0 - numberToGetComplementOf;
}
uint8_t checkForJumpCondition(uint8_t registerToCheck, JumpCondition condition) {
switch (condition) {
case JUMP_CONDITION_NON_ZERO:
return (registerToCheck != 0);
case JUMP_CONDITION_ZERO:
return (registerToCheck == 0);
case JUMP_CONDITION_NEGATIVE:
return (registerToCheck < 0);
case JUMP_CONDITION_POSITIVE:
return (registerToCheck >= 0);
case JUMP_CONDITION_POSITIVE_NON_ZERO:
return (registerToCheck > 0);
case JUMP_CONDITION_UNCONDITIONAL:
return 1;
default:
// Invalid?????
return 0;
}
}
uint8_t getRegisterToCheckForJump(uint8_t instruction) {
uint8_t twoMostSignificantBits = (instruction & 0xC0) >> 6;
if (twoMostSignificantBits == 0) {
//printf("A ");
return A_REGISTER_ADDRESS;
}
else if (twoMostSignificantBits == 1) {
//printf("B is %d\n", memory[B_REGISTER_ADDRESS]);
return B_REGISTER_ADDRESS;
}
else if (twoMostSignificantBits == 0b10) {
//printf("X ");
return X_REGISTER_ADDRESS;
}
//printf("Unconditional ");
return 0;
}
JumpCondition getJumpCondition(uint8_t instruction) {
uint8_t leastSignificant3bits = instruction & 0x7;
switch (leastSignificant3bits) {
case 0x3:
//printf("Non-zero\n");
return JUMP_CONDITION_NON_ZERO;
case 0x4:
//printf("Zero\n");
return JUMP_CONDITION_ZERO;
case 0x5:
//printf("Negative\n");
return JUMP_CONDITION_NEGATIVE;
case 0x6:
//printf("Positive\n");
return JUMP_CONDITION_POSITIVE;
case 0x7:
//printf("Positive Non-zero\n");
return JUMP_CONDITION_POSITIVE_NON_ZERO;
default:
// Invalid jump
//printf("Invalid\n");
return 0xFF;
}
}
void jump(uint8_t instruction) {
printf("Instruction: 0x%X", instruction);
uint8_t operand = memory[PROGRAM_COUNTER_VALUE++];
uint8_t addressToJumpTo = operand;
printf("Operand is %d\n", addressToJumpTo);
uint8_t registerToCheck = getRegisterToCheckForJump(instruction);
JumpCondition condition = getJumpCondition(instruction);
// Invalid jump
if (condition == 0xFF) {
return;
}
// The function to get the register returns 0 for an unconditional jump
if (registerToCheck == 0) {
condition = JUMP_CONDITION_UNCONDITIONAL;
}
uint8_t mark = getBit(instruction, 4);
uint8_t indirect = getBit(instruction, 3);
uint8_t shouldJump = checkForJumpCondition(memory[registerToCheck], condition);
if(shouldJump) {
if (indirect) {
//printf("Indirect\n");
addressToJumpTo = memory[operand];
}
if (mark) {
//printf("Mark\n");
memory[addressToJumpTo++] = operand;
}
//printf("Executing jump to address %d\n", addressToJumpTo);
PROGRAM_COUNTER_VALUE = addressToJumpTo;
//printf("Jumped to address %d\n", PROGRAM_COUNTER_VALUE);
}
}
void skipOnZero(uint8_t instruction) {
uint8_t bitToCheck = (instruction & 0x38) >> 3;
uint8_t operand = memory[PROGRAM_COUNTER_VALUE++];
if (!getBit(operand, bitToCheck)) {
PROGRAM_COUNTER_VALUE += 2;
}
}
void skipOnOne(uint8_t instruction) {
uint8_t bitToCheck = (instruction & 0x38) >> 3;
uint8_t operand = memory[PROGRAM_COUNTER_VALUE++];
if (getBit(operand, bitToCheck)) {
PROGRAM_COUNTER_VALUE += 2;
}
}
// This is what the emulator calls for both instructions
void skip(uint8_t instruction) {
// The instruction changes depending on bit 6
if (getBit(instruction, 6)) {
skipOnOne(instruction);
}
else {
skipOnZero(instruction);
}
}
void setZero(uint8_t instruction) {
uint8_t bitToSet = (instruction & 0x38) >> 3;
uint8_t operand = memory[PROGRAM_COUNTER_VALUE++];
setBit(&memory[operand], bitToSet, 0);
}
void setOne(uint8_t instruction) {
uint8_t bitToSet = (instruction & 0x38) >> 3;
uint8_t operand = memory[PROGRAM_COUNTER_VALUE++];
setBit(&(memory[operand]), bitToSet, 1);
}
// This is what the emulator calls for both instructions
void set(uint8_t instruction) {
// The instruction changes depending on bit 6
if (getBit(instruction, 6)) {
setOne(instruction);
}
else {
setZero(instruction);
}
}
uint8_t rotateLeft(uint8_t value, uint8_t places) {
// First, shift the bits. Then, the new bits that are 0 can be assigned the old bits that
// were supposed to end up there by shifting the value to the opposite direction of the rotation,
// accordingly.
return (value << places) | (value >> ((sizeof(uint8_t) * 8) - places));
}
uint8_t rotateRight(uint8_t value, uint8_t places) {
return (value >> places) | (value << ((sizeof(uint8_t) * 8) - places));
}
// These have similar opcodes, so I decided to group them together
void shiftRotate(uint8_t instruction) {
// 0 for Right, 1 for Left
uint8_t direction = (instruction & 0x80) >> 7;
// 0 for Shift, 1 for Rotate
uint8_t operation = (instruction & 0x40) >> 6;
uint8_t registerToWorkOn = (instruction & 0x20) >> 5;
uint8_t places = (instruction & 0x18) >> 3;
// 0 actually means 4 places
if (places == 0) {
places = 4;
}
if (!operation) {
if (direction) {
memory[registerToWorkOn] <<= places;
}
else {
memory[registerToWorkOn] >>= places;
}
}
else {
if (direction) {
memory[registerToWorkOn] = rotateLeft(memory[registerToWorkOn], places);
}
else {
memory[registerToWorkOn] = rotateRight(memory[registerToWorkOn], places);
}
}
}
void nop() {
++PROGRAM_COUNTER_VALUE;
}