all repos — mgba @ c8e1a9cd59ba20c1187368e5e9f68bab47b885ea

mGBA Game Boy Advance Emulator

src/arm/decoder.h (view raw)

  1#ifndef ARM_DECODER_H
  2#define ARM_DECODER_H
  3
  4#include <stdint.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// Bit 6: this operand is added or subtracted to the base register
 13enum ARMOperandFormat {
 14	ARM_OPERAND_NONE =               0x00000000,
 15	ARM_OPERAND_REGISTER_1 =         0x00000001,
 16	ARM_OPERAND_IMMEDIATE_1 =        0x00000002,
 17	ARM_OPERAND_MEMORY_1 =           0x00000004,
 18	ARM_OPERAND_AFFECTED_1 =         0x00000008,
 19	ARM_OPERAND_SHIFT_REGISTER_1 =   0x00000010,
 20	ARM_OPERAND_SHIFT_IMMEDIATE_1 =  0x00000020,
 21	ARM_OPERAND_1 =                  0x000000FF,
 22
 23	ARM_OPERAND_REGISTER_2 =         0x00000100,
 24	ARM_OPERAND_IMMEDIATE_2 =        0x00000200,
 25	ARM_OPERAND_MEMORY_2 =           0x00000400,
 26	ARM_OPERAND_AFFECTED_2 =         0x00000800,
 27	ARM_OPERAND_SHIFT_REGISTER_2 =   0x00001000,
 28	ARM_OPERAND_SHIFT_IMMEDIATE_2 =  0x00002000,
 29	ARM_OPERAND_2 =                  0x0000FF00,
 30
 31	ARM_OPERAND_REGISTER_3 =         0x00010000,
 32	ARM_OPERAND_IMMEDIATE_3 =        0x00020000,
 33	ARM_OPERAND_MEMORY_3 =           0x00040000,
 34	ARM_OPERAND_AFFECTED_3 =         0x00080000,
 35	ARM_OPERAND_SHIFT_REGISTER_3 =   0x00100000,
 36	ARM_OPERAND_SHIFT_IMMEDIATE_3 =  0x00200000,
 37	ARM_OPERAND_3 =                  0x00FF0000
 38};
 39
 40enum ARMMemoryFormat {
 41	ARM_MEMORY_REGISTER_BASE =    0x0001,
 42	ARM_MEMORY_IMMEDIATE_OFFSET = 0x0002,
 43	ARM_MEMORY_REGISTER_OFFSET  = 0x0004,
 44	ARM_MEMORY_SHIFTED_OFFSET =   0x0008,
 45	ARM_MEMORY_PRE_INCREMENT =    0x0010,
 46	ARM_MEMORY_POST_INCREMENT =   0x0020,
 47	ARM_MEMORY_OFFSET_SUBTRACT =  0x0040
 48};
 49
 50enum ARMCondition {
 51	ARM_CONDITION_EQ = 0x0,
 52	ARM_CONDITION_NE = 0x1,
 53	ARM_CONDITION_CS = 0x2,
 54	ARM_CONDITION_CC = 0x3,
 55	ARM_CONDITION_MI = 0x4,
 56	ARM_CONDITION_PL = 0x5,
 57	ARM_CONDITION_VS = 0x6,
 58	ARM_CONDITION_VC = 0x7,
 59	ARM_CONDITION_HI = 0x8,
 60	ARM_CONDITION_LS = 0x9,
 61	ARM_CONDITION_GE = 0xA,
 62	ARM_CONDITION_LT = 0xB,
 63	ARM_CONDITION_GT = 0xC,
 64	ARM_CONDITION_LE = 0xD,
 65	ARM_CONDITION_AL = 0xE,
 66	ARM_CONDITION_NV = 0xF
 67};
 68
 69union ARMOperand {
 70	struct {
 71		uint8_t reg;
 72		uint8_t shifterOp;
 73		union {
 74			uint8_t shifterReg;
 75			uint8_t shifterImm;
 76		};
 77	};
 78	int32_t immediate;
 79};
 80
 81struct ARMMemoryAccess {
 82	uint8_t baseReg;
 83	uint16_t format;
 84	union ARMOperand offset;
 85};
 86
 87enum ThumbMnemonic {
 88	THUMB_MN_ILL = 0,
 89	THUMB_MN_ADC,
 90	THUMB_MN_ADD,
 91	THUMB_MN_AND,
 92	THUMB_MN_ASR,
 93	THUMB_MN_B,
 94	THUMB_MN_BIC,
 95	THUMB_MN_BKPT,
 96	THUMB_MN_BL,
 97	THUMB_MN_BLH,
 98	THUMB_MN_BX,
 99	THUMB_MN_CMN,
100	THUMB_MN_CMP,
101	THUMB_MN_EOR,
102	THUMB_MN_LDMIA,
103	THUMB_MN_LDR,
104	THUMB_MN_LDRB,
105	THUMB_MN_LDRH,
106	THUMB_MN_LDRSB,
107	THUMB_MN_LDRSH,
108	THUMB_MN_LSL,
109	THUMB_MN_LSR,
110	THUMB_MN_MOV,
111	THUMB_MN_MUL,
112	THUMB_MN_MVN,
113	THUMB_MN_NEG,
114	THUMB_MN_ORR,
115	THUMB_MN_POP,
116	THUMB_MN_PUSH,
117	THUMB_MN_ROR,
118	THUMB_MN_SBC,
119	THUMB_MN_STMIA,
120	THUMB_MN_STR,
121	THUMB_MN_STRB,
122	THUMB_MN_STRH,
123	THUMB_MN_SUB,
124	THUMB_MN_SWI,
125	THUMB_MN_TST,
126
127	THUMB_MN_MAX
128};
129
130struct ThumbInstructionInfo {
131	uint16_t opcode;
132	enum ThumbMnemonic mnemonic;
133	union ARMOperand op1;
134	union ARMOperand op2;
135	union ARMOperand op3;
136	struct ARMMemoryAccess memory;
137	int operandFormat;
138	int branches;
139	int traps;
140	int accessesSpecialRegisters;
141	int affectsCPSR;
142	int condition;
143};
144
145void ARMDecodeThumb(uint16_t opcode, struct ThumbInstructionInfo* info);
146int ARMDisassembleThumb(uint16_t opcode, uint32_t pc, char* buffer, int blen);
147
148#endif