Skip to content

Commit

Permalink
executing opcodes with names
Browse files Browse the repository at this point in the history
  • Loading branch information
drhelius committed Jul 6, 2024
1 parent 920a638 commit b4bac66
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 70 deletions.
63 changes: 39 additions & 24 deletions src/geargrafx_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ void GeargrafxCore::RunToVBlank(u8* frame_buffer, s16* sample_buffer, int* sampl
int totalClocks = 0;
while (!vblank)
{
//unsigned int clockCycles = m_huc6280->Tick();
unsigned int clockCycles = 1;
unsigned int clockCycles = m_huc6280->Tick();
// vblank = m_huc6270->Tick(clockCycles);
m_audio->Tick(clockCycles);

Expand All @@ -96,33 +95,49 @@ void GeargrafxCore::RunToVBlank(u8* frame_buffer, s16* sample_buffer, int* sampl
m_audio->EndFrame(sample_buffer, sample_count);
RenderFrameBuffer(frame_buffer);
}
}

// if (!m_paused && m_cartridge->IsReady())
// {
// bool vblank = false;
// int totalClocks = 0;
// while (!vblank)
// {
// unsigned int clockCycles = m_processor->RunFor(1);
// // vblank = m_video->Tick(clockCycles);
// m_audio->Tick(clockCycles);
bool GeargrafxCore::DebugRun(u8* frame_buffer, s16* sample_buffer, int* sample_count, GG_Debugger_Command command)
{
bool breakpoint = false;

if (!m_paused && m_cartridge->IsReady())
{
bool vblank = false;
int totalClocks = 0;
while (!vblank)
{
unsigned int clockCycles = m_huc6280->Tick();
// vblank = m_huc6270->Tick(clockCycles);
m_audio->Tick(clockCycles);
totalClocks += clockCycles;

// totalClocks += clockCycles;
switch (command)
{
case GG_Debugger_Command_Continue:
break;
case GG_Debugger_Command_StepInto:
vblank = true;
break;
case GG_Debugger_Command_StepOver:
vblank = true;
break;
case GG_Debugger_Command_StepOut:
break;
}

// if (m_processor->BreakpointHit())
// breakpoint = true;

// // if ((step || (stopOnBreakpoints && m_processor->BreakpointHit())) && !m_processor->DuringInputOpcode())
// // {
// // vblank = true;
// // if (m_processor->BreakpointHit())
// // breakpoint = true;
// // }
if (totalClocks > 702240)
vblank = true;
}

// if (totalClocks > 702240)
// vblank = true;
// }
m_audio->EndFrame(sample_buffer, sample_count);
RenderFrameBuffer(frame_buffer);
}

// m_audio->EndFrame(sample_buffer, sample_count);
// RenderFrameBuffer(frame_buffer);
// }
return breakpoint;
}

bool GeargrafxCore::LoadROM(const char* file_path)
Expand Down
1 change: 1 addition & 0 deletions src/geargrafx_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class GeargrafxCore
~GeargrafxCore();
void Init(GG_Pixel_Format pixel_format = GG_PIXEL_RGB888);
void RunToVBlank(u8* frame_buffer, s16* sample_buffer, int* sample_count);
bool DebugRun(u8* frame_buffer, s16* sample_buffer, int* sample_count, GG_Debugger_Command command);
bool LoadROM(const char* file_path);
bool LoadROMFromBuffer(const u8* buffer, int size);
void ResetROM(bool preserve_ram);
Expand Down
126 changes: 107 additions & 19 deletions src/huc6280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "huc6280_names.h"
#include "memory.h"
#include <stdlib.h>
#include <string.h>

HuC6280::HuC6280(Memory* memory)
{
Expand Down Expand Up @@ -50,8 +51,8 @@ void HuC6280::Init()

void HuC6280::Reset()
{
m_PC.SetLow(m_memory->Read(0x1FFE));
m_PC.SetHigh(m_memory->Read(0x1FFF));
m_PC.SetLow(m_memory->Read(0xFFFE));
m_PC.SetHigh(m_memory->Read(0xFFFF));
m_A.SetValue(0x00);
m_X.SetValue(0x00);
m_Y.SetValue(0x00);
Expand Down Expand Up @@ -89,6 +90,7 @@ unsigned int HuC6280::Tick()
m_PC.SetLow(m_memory->Read(0xFFFA));
m_PC.SetHigh(m_memory->Read(0xFFFB));
m_t_states += 7;
DisassembleNextOPCode();
return m_t_states;
}
else if (!IsSetFlag(FLAG_IRQ) && m_interrupt_asserted)
Expand All @@ -100,37 +102,123 @@ unsigned int HuC6280::Tick()
m_PC.SetLow(m_memory->Read(0xFFFE));
m_PC.SetHigh(m_memory->Read(0xFFFF));
m_t_states += 7;
DisassembleNextOPCode();
return m_t_states;
}

u8 opcode = Fetch8();
(this->*m_opcodes[opcode])();
DisassembleNextOPCode();

m_t_states += k_opcode_tstates[opcode];

return m_t_states;
}

HuC6280::Processor_State* HuC6280::GetState()
{
return &m_processor_state;
}

void HuC6280::DisassembleNextOPCode()
{
#ifndef GG_DISABLE_DISASSEMBLER

u16 address = m_PC.GetValue();
Memory::GG_Disassembler_Record* record = m_memory->GetOrCreatDisassemblerRecord(address);

#ifdef HuC6280_DISASM
if (!IsValidPointer(record))
{
u16 opcode_address = m_PC.GetValue() - 1;
return;
}

u8 opcode = m_memory->Read(address);
u8 opcode_size = k_opcode_sizes[opcode];

if (!m_memory->IsDisassembled(opcode_address))
bool changed = false;

for (int i = 0; i < opcode_size; i++)
{
u8 mem_byte = m_memory->Read(address + i);

if (record->opcodes[i] != mem_byte)
{
m_memory->Disassemble(opcode_address, kOPCodeNames[opcode]);
changed = true;
record->opcodes[i] = mem_byte;
}
}
#endif

#ifdef HuC6280_DEBUG
if (changed || record->size == 0)
{
u16 opcode_address = m_PC.GetValue() - 1;
printf("HuC6280 --> $%.4X %s\n", opcode_address, kOPCodeNames[opcode]);
}
#endif
record->size = opcode_size;
record->name[0] = 0;
record->bytes[0] = 0;
record->jump = false;
record->jump_address = 0;

(this->*m_opcodes[opcode])();
for (int i = 0; i < opcode_size; i++)
{
char value[4];
snprintf(value, 4, "%02X", record->opcodes[i]);
strncat(record->bytes, value, 20);
strncat(record->bytes, " ", 20);
}

m_t_states += k_opcode_tstates[opcode];
switch (k_opcode_names[opcode].type)
{
case GG_OPCode_Type_Implied:
{
snprintf(record->name, 64, "%s", k_opcode_names[opcode].name);
break;
}
case GG_OPCode_Type_1b:
{
snprintf(record->name, 64, k_opcode_names[opcode].name, m_memory->Read(address + 1));
break;
}
case GG_OPCode_Type_1b_1b:
{
snprintf(record->name, 64, k_opcode_names[opcode].name, m_memory->Read(address + 1), m_memory->Read(address + 2));
break;
}
case GG_OPCode_Type_1b_2b:
{
snprintf(record->name, 64, k_opcode_names[opcode].name, m_memory->Read(address + 1), m_memory->Read(address + 2) | (m_memory->Read(address + 3) << 8));
break;
}
case GG_OPCode_Type_2b:
{
snprintf(record->name, 64, k_opcode_names[opcode].name, m_memory->Read(address + 1) | (m_memory->Read(address + 2) << 8));
break;
}
case GG_OPCode_Type_2b_2b_2b:
{
snprintf(record->name, 64, k_opcode_names[opcode].name, m_memory->Read(address + 1) | (m_memory->Read(address + 2) << 8), m_memory->Read(address + 3) | (m_memory->Read(address + 4) << 8), m_memory->Read(address + 5) | (m_memory->Read(address + 6) << 8));
break;
}
case GG_OPCode_Type_1b_Relative:
{
s8 rel = m_memory->Read(address + 1);
u16 jump_address = address + 2 + rel;
snprintf(record->name, 64, k_opcode_names[opcode].name, jump_address, rel);
break;
}
case GG_OPCode_Type_1b_1b_Relative:
{
u8 zero_page = m_memory->Read(address + 1);
s8 rel = m_memory->Read(address + 2);
u16 jump_address = address + 3 + rel;
snprintf(record->name, 64, k_opcode_names[opcode].name, zero_page, jump_address, rel);
break;
}
default:
{
break;
}
}
}

return m_t_states;
}
Debug("--> PC: %04X %s %s", address, record->bytes, record->name);

HuC6280::Processor_State* HuC6280::GetState()
{
return &m_processor_state;
#endif
}
1 change: 1 addition & 0 deletions src/huc6280.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class HuC6280
void SetHighSpeed(bool high_speed);
bool IsHighSpeed();
Processor_State* GetState();
void DisassembleNextOPCode();

private:
typedef void (HuC6280::*opcodeptr) (void);
Expand Down
Loading

0 comments on commit b4bac66

Please sign in to comment.