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
11#include "core/cpu.h"
12
13enum {
14 ARM_SP = 13,
15 ARM_LR = 14,
16 ARM_PC = 15
17};
18
19enum ExecutionMode {
20 MODE_ARM = 0,
21 MODE_THUMB = 1
22};
23
24enum PrivilegeMode {
25 MODE_USER = 0x10,
26 MODE_FIQ = 0x11,
27 MODE_IRQ = 0x12,
28 MODE_SUPERVISOR = 0x13,
29 MODE_ABORT = 0x17,
30 MODE_UNDEFINED = 0x1B,
31 MODE_SYSTEM = 0x1F
32};
33
34enum WordSize {
35 WORD_SIZE_ARM = 4,
36 WORD_SIZE_THUMB = 2
37};
38
39enum ExecutionVector {
40 BASE_RESET = 0x00000000,
41 BASE_UNDEF = 0x00000004,
42 BASE_SWI = 0x00000008,
43 BASE_PABT = 0x0000000C,
44 BASE_DABT = 0x00000010,
45 BASE_IRQ = 0x00000018,
46 BASE_FIQ = 0x0000001C
47};
48
49enum RegisterBank {
50 BANK_NONE = 0,
51 BANK_FIQ = 1,
52 BANK_IRQ = 2,
53 BANK_SUPERVISOR = 3,
54 BANK_ABORT = 4,
55 BANK_UNDEFINED = 5
56};
57
58enum LSMDirection {
59 LSM_B = 1,
60 LSM_D = 2,
61 LSM_IA = 0,
62 LSM_IB = 1,
63 LSM_DA = 2,
64 LSM_DB = 3
65};
66
67struct ARMCore;
68
69union PSR {
70 struct {
71#if defined(__POWERPC__) || defined(__PPC__)
72 unsigned n : 1;
73 unsigned z : 1;
74 unsigned c : 1;
75 unsigned v : 1;
76 unsigned : 20;
77 unsigned i : 1;
78 unsigned f : 1;
79 unsigned t : 1;
80 unsigned priv : 5;
81#else
82 unsigned priv : 5;
83 unsigned t : 1;
84 unsigned f : 1;
85 unsigned i : 1;
86 unsigned : 20;
87 unsigned v : 1;
88 unsigned c : 1;
89 unsigned z : 1;
90 unsigned n : 1;
91#endif
92 };
93
94 int32_t packed;
95};
96
97struct ARMMemory {
98 uint32_t (*load32)(struct ARMCore*, uint32_t address, int* cycleCounter);
99 uint32_t (*load16)(struct ARMCore*, uint32_t address, int* cycleCounter);
100 uint32_t (*load8)(struct ARMCore*, uint32_t address, int* cycleCounter);
101
102 void (*store32)(struct ARMCore*, uint32_t address, int32_t value, int* cycleCounter);
103 void (*store16)(struct ARMCore*, uint32_t address, int16_t value, int* cycleCounter);
104 void (*store8)(struct ARMCore*, uint32_t address, int8_t value, int* cycleCounter);
105
106 uint32_t (*loadMultiple)(struct ARMCore*, uint32_t baseAddress, int mask, enum LSMDirection direction,
107 int* cycleCounter);
108 uint32_t (*storeMultiple)(struct ARMCore*, uint32_t baseAddress, int mask, enum LSMDirection direction,
109 int* cycleCounter);
110
111 uint32_t* activeRegion;
112 uint32_t activeMask;
113 uint32_t activeSeqCycles32;
114 uint32_t activeSeqCycles16;
115 uint32_t activeNonseqCycles32;
116 uint32_t activeNonseqCycles16;
117 int32_t (*stall)(struct ARMCore*, int32_t wait);
118 void (*setActiveRegion)(struct ARMCore*, uint32_t address);
119};
120
121struct ARMInterruptHandler {
122 void (*reset)(struct ARMCore* cpu);
123 void (*processEvents)(struct ARMCore* cpu);
124 void (*swi16)(struct ARMCore* cpu, int immediate);
125 void (*swi32)(struct ARMCore* cpu, int immediate);
126 void (*hitIllegal)(struct ARMCore* cpu, uint32_t opcode);
127 void (*bkpt16)(struct ARMCore* cpu, int immediate);
128 void (*bkpt32)(struct ARMCore* cpu, int immediate);
129 void (*readCPSR)(struct ARMCore* cpu);
130
131 void (*hitStub)(struct ARMCore* cpu, uint32_t opcode);
132};
133
134DECL_BITFIELD(ARMCPUID, uint32_t);
135DECL_BITFIELD(ARMCacheType, uint32_t);
136DECL_BITFIELD(ARMTCMType, uint32_t);
137DECL_BITFIELD(ARMTLBType, uint32_t);
138DECL_BITFIELD(ARMMPUType, uint32_t);
139
140DECL_BITFIELD(ARMControlReg, uint32_t);
141DECL_BIT(ARMControlReg, M, 0);
142DECL_BIT(ARMControlReg, A, 1);
143DECL_BIT(ARMControlReg, C, 2);
144DECL_BIT(ARMControlReg, W, 3);
145DECL_BIT(ARMControlReg, P, 4);
146DECL_BIT(ARMControlReg, D, 5);
147DECL_BIT(ARMControlReg, L, 6);
148DECL_BIT(ARMControlReg, B, 7);
149DECL_BIT(ARMControlReg, S, 8);
150DECL_BIT(ARMControlReg, R, 9);
151DECL_BIT(ARMControlReg, F, 10);
152DECL_BIT(ARMControlReg, Z, 11);
153DECL_BIT(ARMControlReg, I, 12);
154DECL_BIT(ARMControlReg, V, 13);
155DECL_BIT(ARMControlReg, RR, 14);
156DECL_BIT(ARMControlReg, L4, 15);
157DECL_BIT(ARMControlReg, FI, 21);
158DECL_BIT(ARMControlReg, U, 22);
159DECL_BIT(ARMControlReg, XP, 23);
160DECL_BIT(ARMControlReg, VE, 24);
161DECL_BIT(ARMControlReg, EE, 25);
162DECL_BIT(ARMControlReg, L2, 26);
163
164DECL_BITFIELD(ARMCoprocessorAccess, uint32_t);
165
166struct ARMCP15 {
167 struct {
168 ARMCPUID cpuid;
169 ARMCacheType cachetype;
170 ARMTCMType tcmtype;
171 ARMTLBType tlbtype;
172 ARMMPUType mputype;
173 } r0;
174 struct {
175 ARMControlReg c0;
176 uint32_t c1;
177 ARMCoprocessorAccess cpAccess;
178 } r1;
179
180 uint32_t (*write)(struct ARMCore*, int crn, int crm, int opcode1, int opcode2, uint32_t value);
181};
182
183struct ARMCore {
184 int32_t gprs[16];
185 union PSR cpsr;
186 union PSR spsr;
187
188 int32_t cycles;
189 int32_t nextEvent;
190 int halted;
191
192 int32_t bankedRegisters[6][7];
193 int32_t bankedSPSRs[6];
194
195 int32_t shifterOperand;
196 int32_t shifterCarryOut;
197
198 uint32_t prefetch[2];
199 enum ExecutionMode executionMode;
200 enum PrivilegeMode privilegeMode;
201
202 struct ARMMemory memory;
203 struct ARMInterruptHandler irqh;
204 struct ARMCP15 cp15;
205
206 struct mCPUComponent* master;
207
208 size_t numComponents;
209 struct mCPUComponent** components;
210};
211
212void ARMInit(struct ARMCore* cpu);
213void ARMDeinit(struct ARMCore* cpu);
214void ARMSetComponents(struct ARMCore* cpu, struct mCPUComponent* master, int extra, struct mCPUComponent** extras);
215void ARMHotplugAttach(struct ARMCore* cpu, size_t slot);
216void ARMHotplugDetach(struct ARMCore* cpu, size_t slot);
217
218void ARMReset(struct ARMCore* cpu);
219void ARMSetPrivilegeMode(struct ARMCore*, enum PrivilegeMode);
220void ARMRaiseIRQ(struct ARMCore*);
221void ARMRaiseSWI(struct ARMCore*);
222void ARMRaiseUndefined(struct ARMCore*);
223
224void ARMRun(struct ARMCore* cpu);
225void ARMRunLoop(struct ARMCore* cpu);
226void ARMRunFake(struct ARMCore* cpu, uint32_t opcode);
227
228#endif