SM83: Simplify register pair access on big endian

This commit is contained in:
Vicki Pfau 2020-07-26 18:12:20 -07:00
parent a786be5bdf
commit 89f9a0b8b1
3 changed files with 46 additions and 46 deletions

View File

@ -49,6 +49,7 @@ Other fixes:
- Qt: Fix Italian RTC translation (fixes mgba.io/i/1798)
- Qt: Add missing option for Wisdom Tree in overrides list
- Util: Fix crash if PNG header fails to write
- SM83: Simplify register pair access on big endian
Misc:
- Debugger: Keep track of global cycle count
- FFmpeg: Add looping option for GIF/APNG

View File

@ -74,37 +74,42 @@ struct SM83InterruptHandler {
void (*hitIllegal)(struct SM83Core* cpu);
};
#ifdef __BIG_ENDIAN__
#define SM83_REGISTER_PAIR(HIGH, LOW) union { \
struct { \
uint8_t HIGH; \
uint8_t LOW; \
}; \
uint16_t HIGH ## LOW; \
}
#else
#define SM83_REGISTER_PAIR(HIGH, LOW) union { \
struct { \
uint8_t LOW; \
uint8_t HIGH; \
}; \
uint16_t HIGH ## LOW; \
}
#endif
struct SM83Core {
#pragma pack(push, 1)
union {
struct {
#ifdef __BIG_ENDIAN__
uint8_t a;
union FlagRegister f;
#else
union FlagRegister f;
uint8_t a;
#endif
};
uint16_t af;
};
#pragma pack(pop)
union {
struct {
uint8_t c;
uint8_t b;
};
uint16_t bc;
};
union {
struct {
uint8_t e;
uint8_t d;
};
uint16_t de;
};
union {
struct {
uint8_t l;
uint8_t h;
};
uint16_t hl;
};
SM83_REGISTER_PAIR(b, c);
SM83_REGISTER_PAIR(d, e);
SM83_REGISTER_PAIR(h, l);
uint16_t sp;
uint16_t pc;

View File

@ -9,33 +9,27 @@
#include <mgba/internal/sm83/sm83.h>
static inline uint16_t SM83ReadHL(struct SM83Core* cpu) {
uint16_t hl;
LOAD_16LE(hl, 0, &cpu->hl);
return hl;
return cpu->hl;
}
static inline void SM83WriteHL(struct SM83Core* cpu, uint16_t hl) {
STORE_16LE(hl, 0, &cpu->hl);
cpu->hl = hl;
}
static inline uint16_t SM83ReadBC(struct SM83Core* cpu) {
uint16_t bc;
LOAD_16LE(bc, 0, &cpu->bc);
return bc;
return cpu->bc;
}
static inline void SM83WriteBC(struct SM83Core* cpu, uint16_t bc) {
STORE_16LE(bc, 0, &cpu->bc);
cpu->bc = bc;
}
static inline uint16_t SM83ReadDE(struct SM83Core* cpu) {
uint16_t de;
LOAD_16LE(de, 0, &cpu->de);
return de;
return cpu->de;
}
static inline void SM83WriteDE(struct SM83Core* cpu, uint16_t de) {
STORE_16LE(de, 0, &cpu->de);
cpu->de = de;
}
#define DEFINE_INSTRUCTION_SM83(NAME, BODY) \
@ -77,7 +71,7 @@ DEFINE_INSTRUCTION_SM83(JPDelay,
DEFINE_CONDITIONAL_INSTRUCTION_SM83(JP);
DEFINE_INSTRUCTION_SM83(JPHL,
cpu->pc = SM83ReadHL(cpu);
cpu->pc = cpu->hl;
cpu->memory.setActiveRegion(cpu, cpu->pc);)
DEFINE_INSTRUCTION_SM83(JRFinish,
@ -226,7 +220,7 @@ DEFINE_CONDITIONAL_ONLY_INSTRUCTION_SM83(RET)
#define DEFINE_LDHL__INSTRUCTION_SM83(NAME, OPERAND) \
DEFINE_INSTRUCTION_SM83(LDHL_ ## NAME, \
cpu->bus = OPERAND; \
cpu->index = SM83ReadHL(cpu); \
cpu->index = cpu->hl; \
cpu->executionState = SM83_CORE_MEMORY_STORE; \
cpu->instruction = _SM83InstructionNOP;)
@ -244,7 +238,7 @@ DEFINE_CONDITIONAL_ONLY_INSTRUCTION_SM83(RET)
DEFINE_ ## NAME ## _INSTRUCTION_SM83(L, cpu->l);
DEFINE_INSTRUCTION_SM83(LDHL_Bus, \
cpu->index = SM83ReadHL(cpu); \
cpu->index = cpu->hl; \
cpu->executionState = SM83_CORE_MEMORY_STORE; \
cpu->instruction = _SM83InstructionNOP;)
@ -267,7 +261,7 @@ DEFINE_INSTRUCTION_SM83(LDHL_SP,
cpu->instruction = _SM83InstructionLDHL_SPDelay;)
DEFINE_INSTRUCTION_SM83(LDSP_HL,
cpu->sp = SM83ReadHL(cpu);
cpu->sp = cpu->hl;
cpu->executionState = SM83_CORE_STALL;)
#define DEFINE_ALU_INSTRUCTION_SM83_MEM(NAME, REG) \
@ -378,7 +372,7 @@ DEFINE_INSTRUCTION_SM83(LDBC, \
cpu->instruction = _SM83InstructionLDBCDelay;)
DEFINE_INSTRUCTION_SM83(LDBC_A, \
cpu->index = SM83ReadBC(cpu); \
cpu->index = cpu->bc; \
cpu->bus = cpu->a; \
cpu->executionState = SM83_CORE_MEMORY_STORE; \
cpu->instruction = _SM83InstructionNOP;)
@ -393,7 +387,7 @@ DEFINE_INSTRUCTION_SM83(LDDE, \
cpu->instruction = _SM83InstructionLDDEDelay;)
DEFINE_INSTRUCTION_SM83(LDDE_A, \
cpu->index = SM83ReadDE(cpu); \
cpu->index = cpu->de; \
cpu->bus = cpu->a; \
cpu->executionState = SM83_CORE_MEMORY_STORE; \
cpu->instruction = _SM83InstructionNOP;)
@ -419,27 +413,27 @@ DEFINE_INSTRUCTION_SM83(LDSP, \
cpu->instruction = _SM83InstructionLDSPDelay;)
DEFINE_INSTRUCTION_SM83(LDIHLA, \
cpu->index = SM83ReadHL(cpu); \
cpu->index = cpu->hl; \
SM83WriteHL(cpu, cpu->index + 1); \
cpu->bus = cpu->a; \
cpu->executionState = SM83_CORE_MEMORY_STORE; \
cpu->instruction = _SM83InstructionNOP;)
DEFINE_INSTRUCTION_SM83(LDDHLA, \
cpu->index = SM83ReadHL(cpu); \
cpu->index = cpu->hl; \
SM83WriteHL(cpu, cpu->index - 1); \
cpu->bus = cpu->a; \
cpu->executionState = SM83_CORE_MEMORY_STORE; \
cpu->instruction = _SM83InstructionNOP;)
DEFINE_INSTRUCTION_SM83(LDA_IHL, \
cpu->index = SM83ReadHL(cpu); \
cpu->index = cpu->hl; \
SM83WriteHL(cpu, cpu->index + 1); \
cpu->executionState = SM83_CORE_MEMORY_LOAD; \
cpu->instruction = _SM83InstructionLDA_Bus;)
DEFINE_INSTRUCTION_SM83(LDA_DHL, \
cpu->index = SM83ReadHL(cpu); \
cpu->index = cpu->hl; \
SM83WriteHL(cpu, cpu->index - 1); \
cpu->executionState = SM83_CORE_MEMORY_LOAD; \
cpu->instruction = _SM83InstructionLDA_Bus;)
@ -587,7 +581,7 @@ DEFINE_INSTRUCTION_SM83(INC_HLDelay,
cpu->executionState = SM83_CORE_MEMORY_STORE;)
DEFINE_INSTRUCTION_SM83(INC_HL,
cpu->index = SM83ReadHL(cpu);
cpu->index = cpu->hl;
cpu->instruction = _SM83InstructionINC_HLDelay;
cpu->executionState = SM83_CORE_MEMORY_LOAD;)
@ -601,7 +595,7 @@ DEFINE_INSTRUCTION_SM83(DEC_HLDelay,
cpu->executionState = SM83_CORE_MEMORY_STORE;)
DEFINE_INSTRUCTION_SM83(DEC_HL,
cpu->index = SM83ReadHL(cpu);
cpu->index = cpu->hl;
cpu->instruction = _SM83InstructionDEC_HLDelay;
cpu->executionState = SM83_CORE_MEMORY_LOAD;)
@ -699,7 +693,7 @@ DEFINE_POPPUSH_INSTRUCTION_SM83(AF, A, a, f.packed);
cpu->executionState = WB; \
cpu->instruction = _SM83InstructionNOP;) \
DEFINE_INSTRUCTION_SM83(NAME ## HL, \
cpu->index = SM83ReadHL(cpu); \
cpu->index = cpu->hl; \
cpu->executionState = SM83_CORE_MEMORY_LOAD; \
cpu->instruction = _SM83Instruction ## NAME ## HLDelay;) \
DEFINE_INSTRUCTION_SM83(NAME ## A, uint8_t reg = cpu->a; BODY; cpu->a = reg)