all repos — mgba @ 8eeaa11256c9ac4a16eb4bc92188db631a9332f4

mGBA Game Boy Advance Emulator

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