all repos — mgba @ f62c068a2c59ca2750b6589e1983844c37137c01

mGBA Game Boy Advance Emulator

src/arm/arm.c (view raw)

  1/* Copyright (c) 2013-2014 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "arm.h"
  7
  8#include "isa-arm.h"
  9#include "isa-inlines.h"
 10#include "isa-thumb.h"
 11
 12static inline enum RegisterBank _ARMSelectBank(enum PrivilegeMode);
 13
 14void ARMSetPrivilegeMode(struct ARMCore* cpu, enum PrivilegeMode mode) {
 15	if (mode == cpu->privilegeMode) {
 16		// Not switching modes after all
 17		return;
 18	}
 19
 20	enum RegisterBank newBank = _ARMSelectBank(mode);
 21	enum RegisterBank oldBank = _ARMSelectBank(cpu->privilegeMode);
 22	if (newBank != oldBank) {
 23		// Switch banked registers
 24		if (mode == MODE_FIQ || cpu->privilegeMode == MODE_FIQ) {
 25			int oldFIQBank = oldBank == BANK_FIQ;
 26			int newFIQBank = newBank == BANK_FIQ;
 27			cpu->bankedRegisters[oldFIQBank][2] = cpu->gprs[8];
 28			cpu->bankedRegisters[oldFIQBank][3] = cpu->gprs[9];
 29			cpu->bankedRegisters[oldFIQBank][4] = cpu->gprs[10];
 30			cpu->bankedRegisters[oldFIQBank][5] = cpu->gprs[11];
 31			cpu->bankedRegisters[oldFIQBank][6] = cpu->gprs[12];
 32			cpu->gprs[8] = cpu->bankedRegisters[newFIQBank][2];
 33			cpu->gprs[9] = cpu->bankedRegisters[newFIQBank][3];
 34			cpu->gprs[10] = cpu->bankedRegisters[newFIQBank][4];
 35			cpu->gprs[11] = cpu->bankedRegisters[newFIQBank][5];
 36			cpu->gprs[12] = cpu->bankedRegisters[newFIQBank][6];
 37		}
 38		cpu->bankedRegisters[oldBank][0] = cpu->gprs[ARM_SP];
 39		cpu->bankedRegisters[oldBank][1] = cpu->gprs[ARM_LR];
 40		cpu->gprs[ARM_SP] = cpu->bankedRegisters[newBank][0];
 41		cpu->gprs[ARM_LR] = cpu->bankedRegisters[newBank][1];
 42
 43		cpu->bankedSPSRs[oldBank] = cpu->spsr.packed;
 44		cpu->spsr.packed = cpu->bankedSPSRs[newBank];
 45
 46	}
 47	cpu->privilegeMode = mode;
 48}
 49
 50static inline enum RegisterBank _ARMSelectBank(enum PrivilegeMode mode) {
 51	switch (mode) {
 52		case MODE_USER:
 53		case MODE_SYSTEM:
 54			// No banked registers
 55			return BANK_NONE;
 56		case MODE_FIQ:
 57			return BANK_FIQ;
 58		case MODE_IRQ:
 59			return BANK_IRQ;
 60		case MODE_SUPERVISOR:
 61			return BANK_SUPERVISOR;
 62		case MODE_ABORT:
 63			return BANK_ABORT;
 64		case MODE_UNDEFINED:
 65			return BANK_UNDEFINED;
 66		default:
 67			// This should be unreached
 68			return BANK_NONE;
 69	}
 70}
 71
 72void ARMInit(struct ARMCore* cpu) {
 73	cpu->master->init(cpu, cpu->master);
 74	int i;
 75	for (i = 0; i < cpu->numComponents; ++i) {
 76		if (cpu->components[i] && cpu->components[i]->init) {
 77			cpu->components[i]->init(cpu, cpu->components[i]);
 78		}
 79	}
 80}
 81
 82void ARMDeinit(struct ARMCore* cpu) {
 83	if (cpu->master->deinit) {
 84		cpu->master->deinit(cpu->master);
 85	}
 86	int i;
 87	for (i = 0; i < cpu->numComponents; ++i) {
 88		if (cpu->components[i] && cpu->components[i]->deinit) {
 89			cpu->components[i]->deinit(cpu->components[i]);
 90		}
 91	}
 92}
 93
 94void ARMSetComponents(struct ARMCore* cpu, struct ARMComponent* master, int extra, struct ARMComponent** extras) {
 95	cpu->master = master;
 96	cpu->numComponents = extra;
 97	cpu->components = extras;
 98}
 99
100void ARMHotplugAttach(struct ARMCore* cpu, int slot) {
101	if (slot >= cpu->numComponents) {
102		return;
103	}
104	cpu->components[slot]->init(cpu, cpu->components[slot]);
105}
106
107void ARMHotplugDetach(struct ARMCore* cpu, int slot) {
108	if (slot >= cpu->numComponents) {
109		return;
110	}
111	cpu->components[slot]->init(cpu, cpu->components[slot]);
112}
113
114void ARMReset(struct ARMCore* cpu) {
115	int i;
116	for (i = 0; i < 16; ++i) {
117		cpu->gprs[i] = 0;
118	}
119	for (i = 0; i < 6; ++i) {
120		cpu->bankedRegisters[i][0] = 0;
121		cpu->bankedRegisters[i][1] = 0;
122		cpu->bankedRegisters[i][2] = 0;
123		cpu->bankedRegisters[i][3] = 0;
124		cpu->bankedRegisters[i][4] = 0;
125		cpu->bankedRegisters[i][5] = 0;
126		cpu->bankedRegisters[i][6] = 0;
127		cpu->bankedSPSRs[i] = 0;
128	}
129
130	cpu->privilegeMode = MODE_SYSTEM;
131	cpu->cpsr.packed = MODE_SYSTEM;
132	cpu->spsr.packed = 0;
133
134	cpu->shifterOperand = 0;
135	cpu->shifterCarryOut = 0;
136
137	cpu->executionMode = MODE_THUMB;
138	_ARMSetMode(cpu, MODE_ARM);
139
140	int currentCycles = 0;
141	ARM_WRITE_PC;
142
143	cpu->cycles = 0;
144	cpu->nextEvent = 0;
145	cpu->halted = 0;
146
147	cpu->irqh.reset(cpu);
148}
149
150void ARMRaiseIRQ(struct ARMCore* cpu) {
151	if (cpu->cpsr.i) {
152		return;
153	}
154	union PSR cpsr = cpu->cpsr;
155	int instructionWidth;
156	if (cpu->executionMode == MODE_THUMB) {
157		instructionWidth = WORD_SIZE_THUMB;
158	} else {
159		instructionWidth = WORD_SIZE_ARM;
160	}
161	ARMSetPrivilegeMode(cpu, MODE_IRQ);
162	cpu->cpsr.priv = MODE_IRQ;
163	cpu->gprs[ARM_LR] = cpu->gprs[ARM_PC] - instructionWidth + WORD_SIZE_ARM;
164	cpu->gprs[ARM_PC] = BASE_IRQ;
165	int currentCycles = 0;
166	ARM_WRITE_PC;
167	cpu->memory.setActiveRegion(cpu, cpu->gprs[ARM_PC]);
168	_ARMSetMode(cpu, MODE_ARM);
169	cpu->spsr = cpsr;
170	cpu->cpsr.i = 1;
171}
172
173void ARMRaiseSWI(struct ARMCore* cpu) {
174	union PSR cpsr = cpu->cpsr;
175	int instructionWidth;
176	if (cpu->executionMode == MODE_THUMB) {
177		instructionWidth = WORD_SIZE_THUMB;
178	} else {
179		instructionWidth = WORD_SIZE_ARM;
180	}
181	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
182	cpu->cpsr.priv = MODE_SUPERVISOR;
183	cpu->gprs[ARM_LR] = cpu->gprs[ARM_PC] - instructionWidth;
184	cpu->gprs[ARM_PC] = BASE_SWI;
185	int currentCycles = 0;
186	ARM_WRITE_PC;
187	cpu->memory.setActiveRegion(cpu, cpu->gprs[ARM_PC]);
188	_ARMSetMode(cpu, MODE_ARM);
189	cpu->spsr = cpsr;
190	cpu->cpsr.i = 1;
191}
192
193static inline void ARMStep(struct ARMCore* cpu) {
194	uint32_t opcode = cpu->prefetch[0];
195	cpu->prefetch[0] = cpu->prefetch[1];
196	cpu->gprs[ARM_PC] += WORD_SIZE_ARM;
197	LOAD_32(cpu->prefetch[1], cpu->gprs[ARM_PC] & cpu->memory.activeMask, cpu->memory.activeRegion);
198
199	unsigned condition = opcode >> 28;
200	if (condition != 0xE) {
201		bool conditionMet = false;
202		switch (condition) {
203		case 0x0:
204			conditionMet = ARM_COND_EQ;
205			break;
206		case 0x1:
207			conditionMet = ARM_COND_NE;
208			break;
209		case 0x2:
210			conditionMet = ARM_COND_CS;
211			break;
212		case 0x3:
213			conditionMet = ARM_COND_CC;
214			break;
215		case 0x4:
216			conditionMet = ARM_COND_MI;
217			break;
218		case 0x5:
219			conditionMet = ARM_COND_PL;
220			break;
221		case 0x6:
222			conditionMet = ARM_COND_VS;
223			break;
224		case 0x7:
225			conditionMet = ARM_COND_VC;
226			break;
227		case 0x8:
228			conditionMet = ARM_COND_HI;
229			break;
230		case 0x9:
231			conditionMet = ARM_COND_LS;
232			break;
233		case 0xA:
234			conditionMet = ARM_COND_GE;
235			break;
236		case 0xB:
237			conditionMet = ARM_COND_LT;
238			break;
239		case 0xC:
240			conditionMet = ARM_COND_GT;
241			break;
242		case 0xD:
243			conditionMet = ARM_COND_LE;
244			break;
245		default:
246			break;
247		}
248		if (!conditionMet) {
249			cpu->cycles += ARM_PREFETCH_CYCLES;
250			return;
251		}
252	}
253	ARMInstruction instruction = _armTable[((opcode >> 16) & 0xFF0) | ((opcode >> 4) & 0x00F)];
254	instruction(cpu, opcode);
255}
256
257static inline void ThumbStep(struct ARMCore* cpu) {
258	uint32_t opcode = cpu->prefetch[0];
259	cpu->prefetch[0] = cpu->prefetch[1];
260	cpu->gprs[ARM_PC] += WORD_SIZE_THUMB;
261	LOAD_16(cpu->prefetch[1], cpu->gprs[ARM_PC] & cpu->memory.activeMask, cpu->memory.activeRegion);
262	ThumbInstruction instruction = _thumbTable[opcode >> 6];
263	instruction(cpu, opcode);
264}
265
266void ARMRun(struct ARMCore* cpu) {
267	if (cpu->executionMode == MODE_THUMB) {
268		ThumbStep(cpu);
269	} else {
270		ARMStep(cpu);
271	}
272	if (cpu->cycles >= cpu->nextEvent) {
273		cpu->irqh.processEvents(cpu);
274	}
275}
276
277void ARMRunLoop(struct ARMCore* cpu) {
278	if (cpu->executionMode == MODE_THUMB) {
279		while (cpu->cycles < cpu->nextEvent) {
280			ThumbStep(cpu);
281		}
282	} else {
283		while (cpu->cycles < cpu->nextEvent) {
284			ARMStep(cpu);
285		}
286	}
287	cpu->irqh.processEvents(cpu);
288}