all repos — mgba @ 849f80e7a1959923c9d3d47191d0a162f0887bb9

mGBA Game Boy Advance Emulator

include/mgba/internal/arm/arm.h (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#ifndef ARM_H
  7#define ARM_H
  8
  9#include <mgba-util/common.h>
 10
 11CXX_GUARD_START
 12
 13#include <mgba/core/cpu.h>
 14
 15enum {
 16	ARM_SP = 13,
 17	ARM_LR = 14,
 18	ARM_PC = 15
 19};
 20
 21enum ExecutionMode {
 22	MODE_ARM = 0,
 23	MODE_THUMB = 1
 24};
 25
 26enum PrivilegeMode {
 27	MODE_USER = 0x10,
 28	MODE_FIQ = 0x11,
 29	MODE_IRQ = 0x12,
 30	MODE_SUPERVISOR = 0x13,
 31	MODE_ABORT = 0x17,
 32	MODE_UNDEFINED = 0x1B,
 33	MODE_SYSTEM = 0x1F
 34};
 35
 36enum WordSize {
 37	WORD_SIZE_ARM = 4,
 38	WORD_SIZE_THUMB = 2
 39};
 40
 41enum ExecutionVector {
 42	BASE_RESET = 0x00000000,
 43	BASE_UNDEF = 0x00000004,
 44	BASE_SWI = 0x00000008,
 45	BASE_PABT = 0x0000000C,
 46	BASE_DABT = 0x00000010,
 47	BASE_IRQ = 0x00000018,
 48	BASE_FIQ = 0x0000001C
 49};
 50
 51enum RegisterBank {
 52	BANK_NONE = 0,
 53	BANK_FIQ = 1,
 54	BANK_IRQ = 2,
 55	BANK_SUPERVISOR = 3,
 56	BANK_ABORT = 4,
 57	BANK_UNDEFINED = 5
 58};
 59
 60enum LSMDirection {
 61	LSM_B = 1,
 62	LSM_D = 2,
 63	LSM_IA = 0,
 64	LSM_IB = 1,
 65	LSM_DA = 2,
 66	LSM_DB = 3
 67};
 68
 69struct ARMCore;
 70
 71union PSR {
 72	struct {
 73#if defined(__POWERPC__) || defined(__PPC__)
 74		unsigned n : 1;
 75		unsigned z : 1;
 76		unsigned c : 1;
 77		unsigned v : 1;
 78		unsigned unused : 20;
 79		unsigned i : 1;
 80		unsigned f : 1;
 81		unsigned t : 1;
 82		unsigned priv : 5;
 83#else
 84		unsigned priv : 5;
 85		unsigned t : 1;
 86		unsigned f : 1;
 87		unsigned i : 1;
 88		unsigned unused : 20;
 89		unsigned v : 1;
 90		unsigned c : 1;
 91		unsigned z : 1;
 92		unsigned n : 1;
 93#endif
 94	};
 95
 96	int32_t packed;
 97};
 98
 99struct ARMMemory {
100	uint32_t (*load32)(struct ARMCore*, uint32_t address, int* cycleCounter);
101	uint32_t (*load16)(struct ARMCore*, uint32_t address, int* cycleCounter);
102	uint32_t (*load8)(struct ARMCore*, uint32_t address, int* cycleCounter);
103
104	void (*store32)(struct ARMCore*, uint32_t address, int32_t value, int* cycleCounter);
105	void (*store16)(struct ARMCore*, uint32_t address, int16_t value, int* cycleCounter);
106	void (*store8)(struct ARMCore*, uint32_t address, int8_t value, int* cycleCounter);
107
108	uint32_t (*loadMultiple)(struct ARMCore*, uint32_t baseAddress, int mask, enum LSMDirection direction,
109	                         int* cycleCounter);
110	uint32_t (*storeMultiple)(struct ARMCore*, uint32_t baseAddress, int mask, enum LSMDirection direction,
111	                          int* cycleCounter);
112
113	uint32_t* activeRegion;
114	uint32_t activeMask;
115	uint32_t activeSeqCycles32;
116	uint32_t activeSeqCycles16;
117	uint32_t activeNonseqCycles32;
118	uint32_t activeNonseqCycles16;
119	int32_t (*stall)(struct ARMCore*, int32_t wait);
120	void (*setActiveRegion)(struct ARMCore*, uint32_t address);
121};
122
123struct ARMInterruptHandler {
124	void (*reset)(struct ARMCore* cpu);
125	void (*processEvents)(struct ARMCore* cpu);
126	void (*swi16)(struct ARMCore* cpu, int immediate);
127	void (*swi32)(struct ARMCore* cpu, int immediate);
128	void (*hitIllegal)(struct ARMCore* cpu, uint32_t opcode);
129	void (*bkpt16)(struct ARMCore* cpu, int immediate);
130	void (*bkpt32)(struct ARMCore* cpu, int immediate);
131	void (*readCPSR)(struct ARMCore* cpu);
132
133	void (*hitStub)(struct ARMCore* cpu, uint32_t opcode);
134};
135
136struct ARMCore {
137	int32_t gprs[16];
138	union PSR cpsr;
139	union PSR spsr;
140
141	int32_t cycles;
142	int32_t nextEvent;
143	int halted;
144
145	int32_t bankedRegisters[6][7];
146	int32_t bankedSPSRs[6];
147
148	int32_t shifterOperand;
149	int32_t shifterCarryOut;
150
151	uint32_t prefetch[2];
152	enum ExecutionMode executionMode;
153	enum PrivilegeMode privilegeMode;
154
155	struct ARMMemory memory;
156	struct ARMInterruptHandler irqh;
157
158	struct mCPUComponent* master;
159
160	size_t numComponents;
161	struct mCPUComponent** components;
162};
163
164void ARMInit(struct ARMCore* cpu);
165void ARMDeinit(struct ARMCore* cpu);
166void ARMSetComponents(struct ARMCore* cpu, struct mCPUComponent* master, int extra, struct mCPUComponent** extras);
167void ARMHotplugAttach(struct ARMCore* cpu, size_t slot);
168void ARMHotplugDetach(struct ARMCore* cpu, size_t slot);
169
170void ARMReset(struct ARMCore* cpu);
171void ARMSetPrivilegeMode(struct ARMCore*, enum PrivilegeMode);
172void ARMRaiseIRQ(struct ARMCore*);
173void ARMRaiseSWI(struct ARMCore*);
174void ARMRaiseUndefined(struct ARMCore*);
175
176void ARMRun(struct ARMCore* cpu);
177void ARMRunLoop(struct ARMCore* cpu);
178void ARMRunFake(struct ARMCore* cpu, uint32_t opcode);
179
180CXX_GUARD_END
181
182#endif