all repos — mgba @ 6d18b9aea53e8d510d24fc8412ca8dc00b2e738c

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 (*readCPSR)(struct ARMCore* cpu);
125
126	void (*hitStub)(struct ARMCore* cpu, uint32_t opcode);
127};
128
129struct ARMComponent {
130	uint32_t id;
131	void (*init)(struct ARMCore* cpu, struct ARMComponent* component);
132	void (*deinit)(struct ARMComponent* component);
133};
134
135struct ARMCore {
136	int32_t gprs[16];
137	union PSR cpsr;
138	union PSR spsr;
139
140	int32_t cycles;
141	int32_t nextEvent;
142	int halted;
143
144	int32_t bankedRegisters[6][7];
145	int32_t bankedSPSRs[6];
146
147	int32_t shifterOperand;
148	int32_t shifterCarryOut;
149
150	uint32_t prefetch[2];
151	enum ExecutionMode executionMode;
152	enum PrivilegeMode privilegeMode;
153
154	struct ARMMemory memory;
155	struct ARMInterruptHandler irqh;
156
157	struct ARMComponent* master;
158
159	int numComponents;
160	struct ARMComponent** components;
161};
162
163void ARMInit(struct ARMCore* cpu);
164void ARMDeinit(struct ARMCore* cpu);
165void ARMSetComponents(struct ARMCore* cpu, struct ARMComponent* master, int extra, struct ARMComponent** extras);
166void ARMHotplugAttach(struct ARMCore* cpu, int slot);
167void ARMHotplugDetach(struct ARMCore* cpu, int slot);
168
169void ARMReset(struct ARMCore* cpu);
170void ARMSetPrivilegeMode(struct ARMCore*, enum PrivilegeMode);
171void ARMRaiseIRQ(struct ARMCore*);
172void ARMRaiseSWI(struct ARMCore*);
173
174void ARMRun(struct ARMCore* cpu);
175void ARMRunLoop(struct ARMCore* cpu);
176
177#endif