Skip to content

Commit

Permalink
add, adc, sub, sbc, inc, dec
Browse files Browse the repository at this point in the history
  • Loading branch information
Prof9 committed Sep 20, 2020
1 parent dfae289 commit 170da32
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
32 changes: 32 additions & 0 deletions Archs/GB/CGameboyInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ bool CGameboyInstruction::Validate(const ValidateState& state)
Vars.WriteImmediate16 = true;
}

// add <-> sub
if ((Opcode.flags & (GB_ADD_IMMEDIATE | GB_SUB_IMMEDIATE)) && Vars.Immediate < 0)
{
// Change opcode
Vars.Encoding ^= 0x10;
Vars.Immediate = -Vars.Immediate;
}
if (Opcode.flags & GB_NEGATE_IMM)
{
Vars.Immediate = -Vars.Immediate;
}
// add a,1 -> inc a
if ((Opcode.flags & GB_ADD_IMMEDIATE) && Vars.LeftParam.num == GB_REG8_A && Vars.Immediate == 1)
{
// Change opcode
Vars.Encoding = 0x3C;
Vars.Length = 1;
Vars.LeftParam.num = 0;
Vars.WriteImmediate8 = false;
Vars.WriteImmediate16 = false;
}
// sub a,1 -> dec a
if ((Opcode.flags & GB_SUB_IMMEDIATE) && Vars.LeftParam.num == GB_REG8_A && Vars.Immediate == 1)
{
// Change opcode
Vars.Encoding = 0x3D;
Vars.Length = 1;
Vars.LeftParam.num = 0;
Vars.WriteImmediate8 = false;
Vars.WriteImmediate16 = false;
}

// Special loads in range 0xFF00 - 0xFFFF
if (Vars.RightParam.num == GB_REG8_A && Vars.Immediate >= 0xFF00)
{
Expand Down
21 changes: 15 additions & 6 deletions Archs/GB/GameboyOpcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,25 @@ const tGameboyOpcode GameboyOpcodes[] = {
{ L"ldd", 1, 0x3A, GB_PARAM_A, GB_PARAM_MEMHL, -1, -1, 0 },
{ L"push", 1, 0xC5, GB_PARAM_REG16_AF, GB_PARAM_NONE, 4, -1, 0 },
{ L"pop", 1, 0xC1, GB_PARAM_REG16_AF, GB_PARAM_NONE, 4, -1, 0 },
// { L"add", 1, 0x00, GB_PARAM_NONE, GB_PARAM_NONE, -1, -1, 0 },
// { L"adc", 1, 0x00, GB_PARAM_NONE, GB_PARAM_NONE, -1, -1, 0 },
// { L"sub", 1, 0x00, GB_PARAM_NONE, GB_PARAM_NONE, -1, -1, 0 },
// { L"sbc", 1, 0x00, GB_PARAM_NONE, GB_PARAM_NONE, -1, -1, 0 },
{ L"add", 1, 0x09, GB_PARAM_HL, GB_PARAM_REG16_SP, -1, 4, 0 },
{ L"add", 1, 0x80, GB_PARAM_A, GB_PARAM_REG8_MEMHL, -1, 0, 0 },
{ L"add", 2, 0xC6, GB_PARAM_A, GB_PARAM_IMMEDIATE, -1, -1, GB_IMMEDIATE_U8 | GB_ADD_IMMEDIATE },
{ L"add", 2, 0xE8, GB_PARAM_SP, GB_PARAM_IMMEDIATE, -1, -1, GB_IMMEDIATE_S8 },
{ L"adc", 1, 0x88, GB_PARAM_A, GB_PARAM_REG8_MEMHL, -1, 0, 0 },
{ L"adc", 2, 0xCE, GB_PARAM_A, GB_PARAM_IMMEDIATE, -1, -1, GB_IMMEDIATE_U8 | GB_ADD_IMMEDIATE },
{ L"sub", 1, 0x90, GB_PARAM_A, GB_PARAM_REG8_MEMHL, -1, 0, 0 },
{ L"sub", 2, 0xD6, GB_PARAM_A, GB_PARAM_IMMEDIATE, -1, -1, GB_IMMEDIATE_U8 | GB_SUB_IMMEDIATE },
{ L"sub", 2, 0xE8, GB_PARAM_SP, GB_PARAM_IMMEDIATE, -1, -1, GB_IMMEDIATE_S8 | GB_NEGATE_IMM },
{ L"sbc", 1, 0x98, GB_PARAM_A, GB_PARAM_REG8_MEMHL, -1, 0, 0 },
{ L"sbc", 2, 0xDE, GB_PARAM_A, GB_PARAM_IMMEDIATE, -1, -1, GB_IMMEDIATE_U8 | GB_SUB_IMMEDIATE },
// { L"and", 1, 0x00, GB_PARAM_NONE, GB_PARAM_NONE, -1, -1, 0 },
// { L"xor", 1, 0x00, GB_PARAM_NONE, GB_PARAM_NONE, -1, -1, 0 },
// { L"or", 1, 0x00, GB_PARAM_NONE, GB_PARAM_NONE, -1, -1, 0 },
// { L"cp", 1, 0x00, GB_PARAM_NONE, GB_PARAM_NONE, -1, -1, 0 },
// { L"inc", 1, 0x00, GB_PARAM_NONE, GB_PARAM_NONE, -1, -1, 0 },
// { L"dec", 1, 0x00, GB_PARAM_NONE, GB_PARAM_NONE, -1, -1, 0 },
{ L"inc", 1, 0x04, GB_PARAM_REG8_MEMHL, GB_PARAM_NONE, 3, -1, 0 },
{ L"inc", 1, 0x03, GB_PARAM_REG16_SP, GB_PARAM_NONE, 4, -1, 0 },
{ L"dec", 1, 0x05, GB_PARAM_REG8_MEMHL, GB_PARAM_NONE, 3, -1, 0 },
{ L"dec", 1, 0x0B, GB_PARAM_REG16_SP, GB_PARAM_NONE, 4, -1, 0 },
{ L"daa", 1, 0x27, GB_PARAM_NONE, GB_PARAM_NONE, -1, -1, 0 },
{ L"cpl", 1, 0x2F, GB_PARAM_NONE, GB_PARAM_NONE, -1, -1, 0 },
{ L"rlca", 1, 0x07, GB_PARAM_NONE, GB_PARAM_NONE, -1, -1, 0 },
Expand Down
3 changes: 3 additions & 0 deletions Archs/GB/GameboyOpcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
#define GB_IMMEDIATE_U16 0x00000008
#define GB_STOP 0x00000010
#define GB_LOAD_REG8_REG8 0x00000020
#define GB_ADD_IMMEDIATE 0x00000040
#define GB_SUB_IMMEDIATE 0x00000080
#define GB_NEGATE_IMM 0x00000100

#define GB_REG_BIT(reg) (1 << reg)

Expand Down

0 comments on commit 170da32

Please sign in to comment.