Skip to content

Commit

Permalink
Huc6280 improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
drhelius committed Jul 25, 2024
1 parent e3b436c commit eae030d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 39 deletions.
6 changes: 3 additions & 3 deletions src/huc6280.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ class HuC6280
void OPCodes_LSR_Accumulator();
void OPCodes_LSR_Memory(u16 address);
void OPCodes_ORA(u8 value);
void OPCodes_RMB(int bit, u16 address);
void OPCodes_RMB(u8 bit, u16 address);
void OPCodes_ROL_Accumulator();
void OPCodes_ROL_Memory(u16 address);
void OPCodes_ROR_Accumulator();
void OPCodes_ROR_Memory(u16 address);
void OPCodes_SBC(u8 value);
void OPCodes_SMB(int bit, u16 address);
void OPCodes_SMB(u8 bit, u16 address);
void OPCodes_Store(EightBitRegister* reg, u16 address);
void OPCodes_STN(int reg, u8 value);
void OPCodes_STN(u8 reg, u8 value);
void OPCodes_STZ(u16 address);
void OPCodes_Swap(EightBitRegister* reg1, EightBitRegister* reg2);
void OPCodes_TAM();
Expand Down
4 changes: 2 additions & 2 deletions src/huc6280_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ inline u16 HuC6280::ZeroPageAddressing()

inline u16 HuC6280::ZeroPageAddressing(EightBitRegister* reg)
{
return (0x2000 | (Fetch8() + reg->GetValue())) & 0x20FF;
return 0x2000 | ((Fetch8() + reg->GetValue()) & 0xFF);
}

inline u16 HuC6280::ZeroPageRelativeAddressing()
Expand All @@ -266,7 +266,7 @@ inline u16 HuC6280::ZeroPageIndirectAddressing()
{
u16 address = ZeroPageAddressing();
u8 l = m_memory->Read(address);
u8 h = m_memory->Read(address + 1);
u8 h = m_memory->Read((address + 1) & 0x20FF);
return Address16(h, l);
}

Expand Down
66 changes: 34 additions & 32 deletions src/huc6280_opcodes_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ inline void HuC6280::OPCodes_ADC(u8 value)

if (IsSetFlag(FLAG_DECIMAL))
{
m_cycles++;
int low_nibble = (a & 0x0F) + (value & 0x0F) + (IsSetFlag(FLAG_CARRY) ? 1 : 0);
if (low_nibble > 9) low_nibble += 6;
int high_nibble = (a >> 4) + (value >> 4) + (low_nibble > 15 ? 1 : 0);
Expand Down Expand Up @@ -323,7 +324,7 @@ inline void HuC6280::OPCodes_ORA(u8 value)
SetNegativeFlagFromResult(result);
}

inline void HuC6280::OPCodes_RMB(int bit, u16 address)
inline void HuC6280::OPCodes_RMB(u8 bit, u16 address)
{
ClearFlag(FLAG_TRANSFER);
u8 result = UnsetBit(m_memory->Read(address), bit);
Expand Down Expand Up @@ -395,6 +396,7 @@ inline void HuC6280::OPCodes_SBC(u8 value)
// TODO
if (IsSetFlag(FLAG_DECIMAL))
{
m_cycles++;
int carry = IsSetFlag(FLAG_CARRY) ? 0 : 1;
int low_nibble = (m_A.GetValue() & 0x0F) - (value & 0x0F) - carry;
int high_nibble = (m_A.GetValue() >> 4) - (value >> 4) - ((low_nibble < 0) ? 1 : 0);
Expand Down Expand Up @@ -444,7 +446,7 @@ inline void HuC6280::OPCodes_SBC(u8 value)
}
}

inline void HuC6280::OPCodes_SMB(int bit, u16 address)
inline void HuC6280::OPCodes_SMB(u8 bit, u16 address)
{
ClearFlag(FLAG_TRANSFER);
u8 result = SetBit(m_memory->Read(address), bit);
Expand All @@ -458,10 +460,10 @@ inline void HuC6280::OPCodes_Store(EightBitRegister* reg, u16 address)
m_memory->Write(address, value);
}

inline void HuC6280::OPCodes_STN(int reg, u8 value)
inline void HuC6280::OPCodes_STN(u8 reg, u8 value)
{
ClearFlag(FLAG_TRANSFER);
m_huc6270->DirectWrite(0x1FE000 | (reg & 0x03), value);
m_huc6270->WriteRegister(0x1FE000 | reg, value);
}

inline void HuC6280::OPCodes_STZ(u16 address)
Expand Down Expand Up @@ -490,11 +492,10 @@ inline void HuC6280::OPCodes_TAM()

for (int i = 0; i < 8; i++)
{
if ((bits & 0x01) != 0)
if ((bits & (0x01 << i)) != 0)
{
m_memory->SetMpr(i, m_A.GetValue());
}
bits >>= 1;
}
}

Expand All @@ -510,11 +511,10 @@ inline void HuC6280::OPCodes_TMA()

for (int i = 0; i < 8; i++)
{
if ((bits & 0x01) != 0)
if ((bits & (0x01 << i)) != 0)
{
m_A.SetValue(m_memory->GetMpr(i));
}
bits >>= 1;
}
}

Expand Down Expand Up @@ -564,83 +564,85 @@ inline void HuC6280::OPCodes_TAI()
ClearFlag(FLAG_TRANSFER);
u16 source = Fetch16();
u16 dest = Fetch16();
int length = Fetch16();
length = (length == 0) ? 0x10000 : length;

for (int i = 0; i < length; i++)
u16 length = Fetch16();
u16 alternate = 0;
do
{
m_memory->Write(dest, m_memory->Read(source, true));
source += (i & 1) ? -1 : 1;
m_memory->Write(dest, m_memory->Read(source + alternate, true));
alternate ^= 1;
dest++;
length--;
m_cycles += 6;
}
while (length);
}

inline void HuC6280::OPCodes_TDD()
{
ClearFlag(FLAG_TRANSFER);
u16 source = Fetch16();
u16 dest = Fetch16();
int length = Fetch16();
length = (length == 0) ? 0x10000 : length;

for (int i = 0; i < length; i++)
u16 length = Fetch16();
do
{
m_memory->Write(dest, m_memory->Read(source, true));
source--;
dest--;
length--;
m_cycles += 6;
}
while (length);
}

inline void HuC6280::OPCodes_TIA()
{
ClearFlag(FLAG_TRANSFER);
u16 source = Fetch16();
u16 dest = Fetch16();
int length = Fetch16();
length = (length == 0) ? 0x10000 : length;

for (int i = 0; i < length; i++)
u16 length = Fetch16();
u16 alternate = 0;
do
{
m_memory->Write(dest, m_memory->Read(source, true));
m_memory->Write(dest + alternate, m_memory->Read(source, true));
source++;
dest += (i & 1) ? -1 : 1;
alternate ^= 1;
length--;
m_cycles += 6;
}
while (length);
}

inline void HuC6280::OPCodes_TII()
{
ClearFlag(FLAG_TRANSFER);
u16 source = Fetch16();
u16 dest = Fetch16();
int length = Fetch16();
length = (length == 0) ? 0x10000 : length;

for (int i = 0; i < length; i++)
u16 length = Fetch16();
do
{
m_memory->Write(dest, m_memory->Read(source, true));
source++;
dest++;
length--;
m_cycles += 6;
}
while (length);
}

inline void HuC6280::OPCodes_TIN()
{
ClearFlag(FLAG_TRANSFER);
u16 source = Fetch16();
u16 dest = Fetch16();
int length = Fetch16();
length = (length == 0) ? 0x10000 : length;

for (int i = 0; i < length; i++)
u16 length = Fetch16();
do
{
m_memory->Write(dest, m_memory->Read(source, true));
source++;
length--;
m_cycles += 6;
}
while (length);
}

inline void HuC6280::UnofficialOPCode()
Expand Down
3 changes: 2 additions & 1 deletion src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ Memory::GG_Disassembler_Record* Memory::GetOrCreateDisassemblerRecord(u16 addres
record->name[0] = 0;
record->bytes[0] = 0;
record->size = 0;
for (int i = 0; i < 7; i++) record->opcodes[i] = 0;
for (int i = 0; i < 7; i++)
record->opcodes[i] = 0;
record->jump = false;
record->jump_address = 0;
m_disassemblerMemoryMap[physical_address] = record;
Expand Down
8 changes: 7 additions & 1 deletion src/memory_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ inline u8 Memory::Read(u16 address, bool block_transfer)
// HuCard ROM
u8* rom = m_cartridge->GetROM();
int rom_size = m_cartridge->GetROMSize();
return rom[physical_address & (rom_size - 1)];
if (physical_address >= rom_size)
{
Debug("Attempted read out of ROM bounds at %06X", physical_address);
return 0xFF;
}
else
return rom[physical_address];
}
// 0xF7
else if (mpr_value < 0xF8)
Expand Down

0 comments on commit eae030d

Please sign in to comment.