Refactor CPU clock and instruction handling: improve variable naming for clarity and consistency

This commit is contained in:
scawful
2024-12-30 07:50:18 -05:00
parent 0e81df57fd
commit b904cfd8a4
3 changed files with 20 additions and 23 deletions

View File

@@ -7,7 +7,7 @@ namespace yaze {
namespace emu { namespace emu {
class Clock { class Clock {
public: public:
virtual ~Clock() = default; virtual ~Clock() = default;
virtual void UpdateClock(double delta) = 0; virtual void UpdateClock(double delta) = 0;
virtual unsigned long long GetCycleCount() const = 0; virtual unsigned long long GetCycleCount() const = 0;
@@ -17,23 +17,23 @@ class Clock {
}; };
class ClockImpl : public Clock { class ClockImpl : public Clock {
public: public:
ClockImpl() = default; ClockImpl() = default;
virtual ~ClockImpl() = default; virtual ~ClockImpl() = default;
void UpdateCycleCount(double deltaTime) { void UpdateCycleCount(double delta_time) {
accumulatedTime += deltaTime; accumulated_time += delta_time;
double cycleTime = 1.0 / frequency; double cycle_time = 1.0 / frequency;
while (accumulatedTime >= cycleTime) { while (accumulated_time >= cycle_time) {
Cycle(); Cycle();
accumulatedTime -= cycleTime; accumulated_time -= cycle_time;
} }
} }
void Cycle() { void Cycle() {
cycle++; cycle++;
cycleCount++; cycle_count++;
} }
void UpdateClock(double delta) override { void UpdateClock(double delta) override {
@@ -41,22 +41,21 @@ class ClockImpl : public Clock {
ResetAccumulatedTime(); ResetAccumulatedTime();
} }
void ResetAccumulatedTime() override { accumulatedTime = 0.0; } void ResetAccumulatedTime() override { accumulated_time = 0.0; }
unsigned long long GetCycleCount() const override { return cycleCount; } unsigned long long GetCycleCount() const override { return cycle_count; }
float GetFrequency() const override { return frequency; } float GetFrequency() const override { return frequency; }
void SetFrequency(float new_frequency) override { void SetFrequency(float new_frequency) override {
this->frequency = new_frequency; this->frequency = new_frequency;
} }
private: private:
uint64_t cycle = 0; // Current cycle uint64_t cycle = 0; // Current cycle
float frequency = 0.0; // Frequency of the clock in Hz float frequency = 0.0; // Frequency of the clock in Hz
unsigned long long cycleCount = 0; // Total number of cycles executed unsigned long long cycle_count = 0; // Total number of cycles executed
double accumulatedTime = 0.0; // Accumulated time since the last cycle update double accumulated_time = 0.0; // Accumulated time since the last cycle update
}; };
} // namespace emu } // namespace emu
} // namespace yaze
} // namespace yaze #endif // YAZE_APP_EMU_CLOCK_H_
#endif // YAZE_APP_EMU_CLOCK_H_

View File

@@ -2259,5 +2259,4 @@ uint8_t Cpu::GetInstructionLength(uint8_t opcode) {
*/ */
} // namespace emu } // namespace emu
} // namespace yaze
} // namespace yaze

View File

@@ -36,8 +36,7 @@ class InstructionEntry {
class Cpu : public core::ExperimentFlags { class Cpu : public core::ExperimentFlags {
public: public:
explicit Cpu(memory::Memory& mem, Clock& vclock, explicit Cpu(Memory& mem, Clock& vclock, CpuCallbacks& callbacks)
memory::CpuCallbacks& callbacks)
: memory(mem), clock(vclock), callbacks_(callbacks) {} : memory(mem), clock(vclock), callbacks_(callbacks) {}
void Reset(bool hard = false); void Reset(bool hard = false);
@@ -792,8 +791,8 @@ class Cpu : public core::ExperimentFlags {
bool int_wanted_ = false; bool int_wanted_ = false;
bool int_delay_ = false; bool int_delay_ = false;
memory::CpuCallbacks callbacks_; CpuCallbacks callbacks_;
memory::Memory& memory; Memory& memory;
Clock& clock; Clock& clock;
}; };