From 76f966845d1eedfa645482006c245f961080d038 Mon Sep 17 00:00:00 2001 From: joevt Date: Mon, 22 Apr 2024 17:10:40 -0700 Subject: [PATCH] viacuda: Use timer ids instead of timer flags. --- devices/common/viacuda.cpp | 40 ++++++++++++++++++-------------------- devices/common/viacuda.h | 3 --- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/devices/common/viacuda.cpp b/devices/common/viacuda.cpp index 1d84f8e..996e802 100644 --- a/devices/common/viacuda.cpp +++ b/devices/common/viacuda.cpp @@ -68,9 +68,7 @@ ViaCuda::ViaCuda() { // intialize counters/timers this->t1_counter = 0xFFFF; - this->t1_active = false; this->t2_counter = 0xFFFF; - this->t2_active = false; // calculate VIA clock duration in ns this->via_clk_dur = 1.0f / VIA_CLOCK_HZ * NS_PER_SEC; @@ -102,17 +100,17 @@ ViaCuda::ViaCuda() { ViaCuda::~ViaCuda() { - if (this->sr_timer_on) { + if (this->sr_timer_id) { TimerManager::get_instance()->cancel_timer(this->sr_timer_id); - this->sr_timer_on = false; + this->sr_timer_id = 0; } - if (this->t1_active) { + if (this->t1_timer_id) { TimerManager::get_instance()->cancel_timer(this->t1_timer_id); - this->t1_active = false; + this->t1_timer_id = 0; } - if (this->t2_active) { + if (this->t2_timer_id) { TimerManager::get_instance()->cancel_timer(this->t2_timer_id); - this->t2_active = false; + this->t2_timer_id = 0; } if (this->treq_timer_id) { TimerManager::get_instance()->cancel_timer(this->treq_timer_id); @@ -226,9 +224,9 @@ void ViaCuda::write(int reg, uint8_t value) { ABORT_F("Unsupported VIA T1 mode, ACR=0x%X", this->via_regs[VIA_ACR]); } // cancel active T1 timer task - if (this->t1_active) { + if (this->t1_timer_id) { TimerManager::get_instance()->cancel_timer(this->t1_timer_id); - this->t1_active = false; + this->t1_timer_id = 0; } // clear T1 flag in IFR this->_via_ifr &= ~VIA_IF_T1; @@ -242,19 +240,19 @@ void ViaCuda::write(int reg, uint8_t value) { this->t1_timer_id = TimerManager::get_instance()->add_oneshot_timer( static_cast(this->via_clk_dur * (this->t1_counter + 3) + 0.5f), [this]() { + this->t1_timer_id = 0; this->assert_t1_int(); } ); - this->t1_active = true; break; case VIA_T2CH: if (this->via_regs[VIA_ACR] & 0x20) { ABORT_F("VIA T2 pulse count mode not supported!"); } // cancel active T2 timer task - if (this->t2_active) { + if (this->t2_timer_id) { TimerManager::get_instance()->cancel_timer(this->t2_timer_id); - this->t2_active = false; + this->t2_timer_id = 0; } // clear T2 flag in IFR this->_via_ifr &= ~VIA_IF_T2; @@ -268,10 +266,10 @@ void ViaCuda::write(int reg, uint8_t value) { this->t2_timer_id = TimerManager::get_instance()->add_oneshot_timer( static_cast(this->via_clk_dur * (this->t2_counter + 3) + 0.5f), [this]() { + this->t2_timer_id = 0; this->assert_t2_int(); } ); - this->t2_active = true; break; case VIA_SR: this->_via_ifr &= ~VIA_IF_SR; @@ -310,20 +308,17 @@ void ViaCuda::update_irq() } void ViaCuda::assert_sr_int() { - this->sr_timer_on = false; this->_via_ifr |= VIA_IF_SR; update_irq(); } void ViaCuda::assert_t1_int() { this->_via_ifr |= VIA_IF_T1; - this->t1_active = false; update_irq(); } void ViaCuda::assert_t2_int() { this->_via_ifr |= VIA_IF_T2; - this->t2_active = false; update_irq(); } @@ -356,14 +351,17 @@ void ViaCuda::assert_ctrl_line(ViaLine line) } void ViaCuda::schedule_sr_int(uint64_t timeout_ns) { - if (this->sr_timer_on) { + if (this->sr_timer_id) { TimerManager::get_instance()->cancel_timer(this->sr_timer_id); - this->sr_timer_on = false; + this->sr_timer_id = 0; } this->sr_timer_id = TimerManager::get_instance()->add_oneshot_timer( timeout_ns, - [this]() { this->assert_sr_int(); }); - this->sr_timer_on = true; + [this]() { + this->sr_timer_id = 0; + this->assert_sr_int(); + } + ); } void ViaCuda::write(uint8_t new_state) { diff --git a/devices/common/viacuda.h b/devices/common/viacuda.h index a36397c..4577aa6 100644 --- a/devices/common/viacuda.h +++ b/devices/common/viacuda.h @@ -182,16 +182,13 @@ private: // VIA internal state uint32_t sr_timer_id = 0; - bool sr_timer_on = false; // timer 1 state - bool t1_active; uint16_t t1_counter; uint32_t t1_timer_id = 0; uint64_t t1_start_time = 0; // timer 2 state - bool t2_active; uint16_t t2_counter; uint32_t t2_timer_id = 0; uint64_t t2_start_time = 0;