Skip to content

Commit

Permalink
Main clock
Browse files Browse the repository at this point in the history
  • Loading branch information
drhelius committed Jul 21, 2024
1 parent 54f30d2 commit 31ca673
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 84 deletions.
5 changes: 3 additions & 2 deletions platforms/shared/desktop/gui_debug_huc6280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ void gui_debug_window_huc6280(void)
ImGui::Text(BYTE_TO_BINARY_PATTERN_SPACED, BYTE_TO_BINARY(input->GetIORegister()));

ImGui::TableNextColumn();
u8 tim = (*proc_state->TIMER) ? 0x01 : 0x00;
ImGui::TextColored(blue, "TIM "); ImGui::SameLine();
ImGui::Text("$%02X", *proc_state->TIMER ? 1 : 0);
ImGui::Text(BYTE_TO_BINARY_PATTERN_SPACED, BYTE_TO_BINARY(*proc_state->TIMER ? 1 : 0));
ImGui::Text("$%02X", tim);
ImGui::Text(BYTE_TO_BINARY_PATTERN_SPACED, BYTE_TO_BINARY(tim));

ImGui::TableNextColumn();
ImGui::TextColored(blue, "TIMC"); ImGui::SameLine();
Expand Down
1 change: 0 additions & 1 deletion platforms/shared/desktop/makefiles/Makefile.sources
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ SOURCES_CXX := \
$(SRC_DIR)/huc6280_functors.cpp \
$(SRC_DIR)/huc6280_opcodes.cpp \
$(SRC_DIR)/huc6280_psg.cpp \
$(SRC_DIR)/huc6280_timer.cpp \
$(SRC_DIR)/input.cpp \
$(SRC_DIR)/memory.cpp \
6 changes: 3 additions & 3 deletions src/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Audio
void Init();
void Reset();
void Mute(bool mute);
void Tick(unsigned int cycles);
void Clock();
void EndFrame(s16* sample_buffer, int* sample_count);
// void SaveState(std::ostream& stream);
// void LoadState(std::istream& stream);
Expand All @@ -42,7 +42,7 @@ class Audio
bool m_mute;
};

inline void Audio::Tick(unsigned int cycles)
inline void Audio::Clock()
{
m_elapsed_cycles += cycles;
m_elapsed_cycles++;
}
2 changes: 1 addition & 1 deletion src/geargrafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#include "audio.h"
#include "cartridge.h"
#include "memory.h"
#include "huc6260.h"
#include "huc6270.h"
#include "huc6280.h"
#include "huc6280_psg.h"
#include "huc6280_timer.h"

#endif /* GEARGRAFX_H */
64 changes: 38 additions & 26 deletions src/geargrafx_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,23 @@ GeargrafxCore::GeargrafxCore()
{
InitPointer(m_memory);
InitPointer(m_huc6260);
InitPointer(m_huc6270);
InitPointer(m_huc6280);
InitPointer(m_audio);
// InitPointer(m_huc6270);
InitPointer(m_input);
InitPointer(m_cartridge);
m_paused = true;
m_clock = 0;
m_pixel_format = GG_PIXEL_RGB888;
}

GeargrafxCore::~GeargrafxCore()
{
SafeDelete(m_cartridge);
SafeDelete(m_input);
// SafeDelete(m_huc6270);
SafeDelete(m_audio);
SafeDelete(m_huc6280);
SafeDelete(m_huc6270);
SafeDelete(m_huc6260);
SafeDelete(m_memory);
}
Expand All @@ -63,18 +64,18 @@ void GeargrafxCore::Init(GG_Pixel_Format pixel_format)

m_cartridge = new Cartridge();
m_huc6260 = new HuC6260();
m_huc6270 = new HuC6270();
m_huc6280 = new HuC6280();
m_input = new Input();
m_memory = new Memory(m_huc6260, m_huc6280, m_cartridge, m_input);
m_audio = new Audio();
// m_huc6270 = new HuC6270(m_memory, m_huc6280);

m_cartridge->Init();
m_memory->Init();
m_huc6260->Init();
m_huc6270->Init();
m_huc6280->Init(m_memory);
m_audio->Init();
// m_huc6270->Init();
m_input->Init();
}

Expand All @@ -85,34 +86,44 @@ bool GeargrafxCore::RunToVBlank(u8* frame_buffer, s16* sample_buffer, int* sampl
if (m_paused || !m_cartridge->IsReady())
return false;

bool stop = step_debugger;
int clocks = 0;
bool high_speed = m_huc6280->IsHighSpeed();
bool stop = false;

int huc6280_divider = m_huc6280->IsHighSpeed() ? 3 : 12;
int huc6260_divider = m_huc6260->GetClockDivider();
const int timer_divider = 3;
const int audio_divider = 6;

do
{
unsigned int cpu_clocks = m_huc6280->Tick();
unsigned int timer_clocks = high_speed ? cpu_clocks : cpu_clocks << 2;
unsigned int video_clocks = high_speed ? cpu_clocks : cpu_clocks << 2;
unsigned int audio_clocks = high_speed ? cpu_clocks << 1 : cpu_clocks >> 1;
m_clock++;
bool instruction_completed = false;

if (m_clock % huc6280_divider == 0)
instruction_completed = m_huc6280->Clock();

m_huc6280->TickTimer(timer_clocks);
// stop = m_huc6270->Tick(clockCycles);
m_audio->Tick(audio_clocks);
if (m_clock % timer_divider == 0)
m_huc6280->ClockTimer();

// if (m_processor->BreakpointHit())
// breakpoint = true;
if (m_clock % huc6260_divider == 0)
m_huc6260->Clock();

clocks += video_clocks;
if (m_clock % audio_divider == 0)
m_audio->Clock();

if (clocks > 72240)
if (step_debugger && instruction_completed)
stop = true;

m_audio->EndFrame(sample_buffer, sample_count);
RenderFrameBuffer(frame_buffer);
if (m_clock >= 72240)
{
m_clock -= 72240;
stop = true;
}
}
while (!stop);

m_audio->EndFrame(sample_buffer, sample_count);
RenderFrameBuffer(frame_buffer);

return breakpoint;
}

Expand Down Expand Up @@ -176,6 +187,11 @@ HuC6260* GeargrafxCore::GetHuC6260()
return m_huc6260;
}

HuC6270* GeargrafxCore::GetHuC6270()
{
return m_huc6270;
}

HuC6280* GeargrafxCore::GetHuC6280()
{
return m_huc6280;
Expand All @@ -191,11 +207,6 @@ Input* GeargrafxCore::GetInput()
return m_input;
}

// Video* GeargrafxCore::GetVideo()
// {
// return m_video;
// }

void GeargrafxCore::KeyPressed(GG_Controllers controller, GG_Keys key)
{
m_input->KeyPressed(controller, key);
Expand Down Expand Up @@ -506,11 +517,12 @@ void GeargrafxCore::ResetSound()

void GeargrafxCore::Reset()
{
m_clock = 0;
m_memory->Reset();
m_huc6260->Reset();
m_huc6270->Reset();
m_huc6280->Reset();
m_audio->Reset();
// m_video->Reset(m_cartridge->IsPAL());
m_input->Reset();
m_paused = false;
}
Expand Down
8 changes: 5 additions & 3 deletions src/geargrafx_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
class Audio;
class Input;
class HuC6260;
class HuC6270;
class HuC6280;
class Memory;
class Cartridge;
Expand Down Expand Up @@ -61,10 +62,10 @@ class GeargrafxCore
Memory* GetMemory();
Cartridge* GetCartridge();
HuC6260* GetHuC6260();
HuC6270* GetHuC6270();
HuC6280* GetHuC6280();
Audio* GetAudio();
Input* GetInput();
// HuC6270* GetHuC6270();

private:
void Reset();
Expand All @@ -73,13 +74,14 @@ class GeargrafxCore
private:
Memory* m_memory;
HuC6260* m_huc6260;
HuC6270* m_huc6270;
HuC6280* m_huc6280;
Audio* m_audio;
// Video* m_huc6270;
Audio* m_audio;
Input* m_input;
Cartridge* m_cartridge;
bool m_paused;
GG_Pixel_Format m_pixel_format;
int m_clock;
};

#endif /* GEARGRAFX_CORE_H */
13 changes: 10 additions & 3 deletions src/huc6260.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ HuC6260::HuC6260()
m_control_register = 0;
m_color_table_address = 0;
m_speed = HuC6260_SPEED_5_36_MHZ;
m_clock_divider = 4;
InitPointer(m_color_table);
state.CR = &m_control_register;
state.CTA = &m_color_table_address;
m_state.CR = &m_control_register;
m_state.CTA = &m_color_table_address;
}

HuC6260::~HuC6260()
Expand All @@ -45,6 +46,7 @@ void HuC6260::Reset()
m_control_register = 0;
m_color_table_address = 0;
m_speed = HuC6260_SPEED_5_36_MHZ;
m_clock_divider = 4;
for (int i = 0; i < 512; i++)
{
m_color_table[i] = 0;
Expand All @@ -53,14 +55,19 @@ void HuC6260::Reset()

HuC6260::HuC6260_State* HuC6260::GetState()
{
return &state;
return &m_state;
}

HuC6260::HuC6260_Speed HuC6260::GetSpeed()
{
return m_speed;
}

int HuC6260::GetClockDivider()
{
return m_clock_divider;
}

u16* HuC6260::GetColorTable()
{
return m_color_table;
Expand Down
4 changes: 3 additions & 1 deletion src/huc6260.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ class HuC6260
void WriteRegister(u32 address, u8 value);
HuC6260_State* GetState();
HuC6260_Speed GetSpeed();
int GetClockDivider();
u16* GetColorTable();

private:
HuC6260_State state;
HuC6260_State m_state;
u8 m_control_register;
u16 m_color_table_address;
HuC6260_Speed m_speed;
int m_clock_divider;
u16* m_color_table;
};

Expand Down
9 changes: 4 additions & 5 deletions src/huc6260_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,17 @@ inline void HuC6260::WriteRegister(u32 address, u8 value)
case 0:
Debug("HuC6260 Speed: 5.36 MHz");
m_speed = HuC6260_SPEED_5_36_MHZ;
m_clock_divider = 4;
break;
case 1:
Debug("HuC6260 Speed: 7.16 MHz");
m_speed = HuC6260_SPEED_7_16_MHZ;
m_clock_divider = 3;
break;
case 2:
Debug("HuC6260 Speed: 10.8 MHz");
m_speed = HuC6260_SPEED_10_8_MHZ;
break;
case 3:
default:
Debug("HuC6260 Speed: 10.8 MHz");
m_speed = HuC6260_SPEED_10_8_MHZ;
m_clock_divider = 2;
break;
}
break;
Expand Down
27 changes: 26 additions & 1 deletion src/huc6270.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,29 @@
*
*/

#include "huc6270.h"
#include "huc6270.h"

HuC6270::HuC6270()
{

}

HuC6270::~HuC6270()
{

}

void HuC6270::Init()
{
Reset();
}

void HuC6270::Reset()
{

}

HuC6270::HuC6270_State* HuC6270::GetState()
{
return &m_state;
}
25 changes: 25 additions & 0 deletions src/huc6270.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,32 @@
#ifndef HUC6270_H
#define HUC6270_H

#include "common.h"

#define GG_MAX_RESOLUTION_WIDTH 256
#define GG_MAX_RESOLUTION_HEIGHT 192

class HuC6270
{
public:
struct HuC6270_State
{

};

public:
HuC6270();
~HuC6270();
void Init();
void Reset();
u8 ReadRegister(u32 address);
void WriteRegister(u32 address, u8 value);
HuC6270_State* GetState();

private:
HuC6270_State m_state;
};

#include "huc6270_inline.h"

#endif /* HUC6270_H */
9 changes: 6 additions & 3 deletions src/huc6280_timer.h → src/huc6270_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*
*/

#ifndef HUC6280_TIMER_H
#define HUC6280_TIMER_H
#ifndef HUC6270_INLINE_H
#define HUC6270_INLINE_H

#endif /* HUC6280_TIMER_H */
#include "huc6270.h"


#endif /* HUC6270_INLINE_H */
Loading

0 comments on commit 31ca673

Please sign in to comment.