all repos — mgba @ c8d5219212667aebe4797171665b361830e653bd

mGBA Game Boy Advance Emulator

src/arm/decoder.h (view raw)

  1#ifndef ARM_DECODER_H
  2#define ARM_DECODER_H
  3
  4#include "arm.h"
  5
  6// Bit 0: a register is involved with this operand
  7// Bit 1: an immediate is invovled with this operand
  8// Bit 2: a memory access is invovled with this operand
  9// Bit 3: the destination of this operand is affected by this opcode
 10// Bit 4: this operand is shifted by a register
 11// Bit 5: this operand is shifted by an immediate
 12#define ARM_OPERAND_NONE                0x00000000
 13#define ARM_OPERAND_REGISTER_1          0x00000001
 14#define ARM_OPERAND_IMMEDIATE_1         0x00000002
 15#define ARM_OPERAND_MEMORY_1            0x00000004
 16#define ARM_OPERAND_AFFECTED_1          0x00000008
 17#define ARM_OPERAND_SHIFT_REGISTER_1    0x00000010
 18#define ARM_OPERAND_SHIFT_IMMEDIATE_1   0x00000020
 19#define ARM_OPERAND_1                   0x000000FF
 20
 21#define ARM_OPERAND_REGISTER_2          0x00000100
 22#define ARM_OPERAND_IMMEDIATE_2         0x00000200
 23#define ARM_OPERAND_MEMORY_2            0x00000400
 24#define ARM_OPERAND_AFFECTED_2          0x00000800
 25#define ARM_OPERAND_SHIFT_REGISTER_2    0x00001000
 26#define ARM_OPERAND_SHIFT_IMMEDIATE_2   0x00002000
 27#define ARM_OPERAND_2                   0x0000FF00
 28
 29#define ARM_OPERAND_REGISTER_3          0x00010000
 30#define ARM_OPERAND_IMMEDIATE_3         0x00020000
 31#define ARM_OPERAND_MEMORY_3            0x00040000
 32#define ARM_OPERAND_AFFECTED_3          0x00080000
 33#define ARM_OPERAND_SHIFT_REGISTER_3    0x00100000
 34#define ARM_OPERAND_SHIFT_IMMEDIATE_3   0x00200000
 35#define ARM_OPERAND_3                   0x00FF0000
 36
 37#define ARM_OPERAND_REGISTER_4          0x01000000
 38#define ARM_OPERAND_IMMEDIATE_4         0x02000000
 39#define ARM_OPERAND_MEMORY_4            0x04000000
 40#define ARM_OPERAND_AFFECTED_4          0x08000000
 41#define ARM_OPERAND_SHIFT_REGISTER_4    0x10000000
 42#define ARM_OPERAND_SHIFT_IMMEDIATE_4   0x20000000
 43#define ARM_OPERAND_4                   0xFF000000
 44
 45
 46#define ARM_MEMORY_REGISTER_BASE     0x0001
 47#define ARM_MEMORY_IMMEDIATE_OFFSET  0x0002
 48#define ARM_MEMORY_REGISTER_OFFSET   0x0004
 49#define ARM_MEMORY_SHIFTED_OFFSET    0x0008
 50#define ARM_MEMORY_PRE_INCREMENT     0x0010
 51#define ARM_MEMORY_POST_INCREMENT    0x0020
 52#define ARM_MEMORY_OFFSET_SUBTRACT   0x0040
 53#define ARM_MEMORY_WRITEBACK         0x0080
 54#define ARM_MEMORY_DECREMENT_AFTER   0x0000
 55#define ARM_MEMORY_INCREMENT_AFTER   0x0100
 56#define ARM_MEMORY_DECREMENT_BEFORE  0x0200
 57#define ARM_MEMORY_INCREMENT_BEFORE  0x0300
 58
 59#define MEMORY_FORMAT_TO_DIRECTION(F) (((F) >> 8) & 0x7)
 60
 61enum ARMCondition {
 62	ARM_CONDITION_EQ = 0x0,
 63	ARM_CONDITION_NE = 0x1,
 64	ARM_CONDITION_CS = 0x2,
 65	ARM_CONDITION_CC = 0x3,
 66	ARM_CONDITION_MI = 0x4,
 67	ARM_CONDITION_PL = 0x5,
 68	ARM_CONDITION_VS = 0x6,
 69	ARM_CONDITION_VC = 0x7,
 70	ARM_CONDITION_HI = 0x8,
 71	ARM_CONDITION_LS = 0x9,
 72	ARM_CONDITION_GE = 0xA,
 73	ARM_CONDITION_LT = 0xB,
 74	ARM_CONDITION_GT = 0xC,
 75	ARM_CONDITION_LE = 0xD,
 76	ARM_CONDITION_AL = 0xE,
 77	ARM_CONDITION_NV = 0xF
 78};
 79
 80enum ARMShifterOperation {
 81	ARM_SHIFT_NONE = 0,
 82	ARM_SHIFT_LSL,
 83	ARM_SHIFT_LSR,
 84	ARM_SHIFT_ASR,
 85	ARM_SHIFT_ROR,
 86	ARM_SHIFT_RRX
 87};
 88
 89union ARMOperand {
 90	struct {
 91		uint8_t reg;
 92		uint8_t shifterOp;
 93		union {
 94			uint8_t shifterReg;
 95			uint8_t shifterImm;
 96		};
 97	};
 98	int32_t immediate;
 99};
100
101enum ARMMemoryAccessType {
102	ARM_ACCESS_WORD = 4,
103	ARM_ACCESS_HALFWORD = 2,
104	ARM_ACCESS_SIGNED_HALFWORD = 10,
105	ARM_ACCESS_BYTE = 1,
106	ARM_ACCESS_SIGNED_BYTE = 9,
107	ARM_ACCESS_TRANSLATED_WORD = 20,
108	ARM_ACCESS_TRANSLATED_BYTE = 17
109};
110
111enum ARMBranchType {
112	ARM_BRANCH_NONE = 0,
113	ARM_BRANCH = 1,
114	ARM_BRANCH_INDIRECT = 2,
115	ARM_BRANCH_LINKED = 4
116};
117
118struct ARMMemoryAccess {
119	uint8_t baseReg;
120	uint8_t width;
121	uint16_t format;
122	union ARMOperand offset;
123};
124
125enum ARMMnemonic {
126	ARM_MN_ILL = 0,
127	ARM_MN_ADC,
128	ARM_MN_ADD,
129	ARM_MN_AND,
130	ARM_MN_ASR,
131	ARM_MN_B,
132	ARM_MN_BIC,
133	ARM_MN_BKPT,
134	ARM_MN_BL,
135	ARM_MN_BLH,
136	ARM_MN_BX,
137	ARM_MN_CMN,
138	ARM_MN_CMP,
139	ARM_MN_EOR,
140	ARM_MN_LDM,
141	ARM_MN_LDR,
142	ARM_MN_LSL,
143	ARM_MN_LSR,
144	ARM_MN_MLA,
145	ARM_MN_MOV,
146	ARM_MN_MRS,
147	ARM_MN_MSR,
148	ARM_MN_MUL,
149	ARM_MN_MVN,
150	ARM_MN_NEG,
151	ARM_MN_ORR,
152	ARM_MN_ROR,
153	ARM_MN_RSB,
154	ARM_MN_RSC,
155	ARM_MN_SBC,
156	ARM_MN_SMLAL,
157	ARM_MN_SMULL,
158	ARM_MN_STM,
159	ARM_MN_STR,
160	ARM_MN_SUB,
161	ARM_MN_SWI,
162	ARM_MN_SWP,
163	ARM_MN_TEQ,
164	ARM_MN_TST,
165	ARM_MN_UMLAL,
166	ARM_MN_UMULL,
167
168	ARM_MN_MAX
169};
170
171enum {
172	ARM_CPSR = 16,
173	ARM_SPSR = 17
174};
175
176struct ARMInstructionInfo {
177	uint32_t opcode;
178	union ARMOperand op1;
179	union ARMOperand op2;
180	union ARMOperand op3;
181	union ARMOperand op4;
182	struct ARMMemoryAccess memory;
183	int operandFormat;
184	unsigned execMode : 1;
185	bool traps : 1;
186	bool affectsCPSR : 1;
187	unsigned branchType : 3;
188	unsigned condition : 4;
189	unsigned mnemonic : 6;
190	unsigned iCycles : 3;
191	unsigned cCycles : 4;
192	unsigned sInstructionCycles : 4;
193	unsigned nInstructionCycles : 4;
194	unsigned sDataCycles : 10;
195	unsigned nDataCycles : 10;
196};
197
198void ARMDecodeARM(uint32_t opcode, struct ARMInstructionInfo* info);
199void ARMDecodeThumb(uint16_t opcode, struct ARMInstructionInfo* info);
200int ARMDisassemble(struct ARMInstructionInfo* info, uint32_t pc, char* buffer, int blen);
201
202#endif