diff --git a/Components/6526/Implementation/6526Implementation.hpp b/Components/6526/Implementation/6526Implementation.hpp index c23dad6c0..8415425db 100644 --- a/Components/6526/Implementation/6526Implementation.hpp +++ b/Components/6526/Implementation/6526Implementation.hpp @@ -69,9 +69,15 @@ void MOS6526::write(int address, uint8_t value) { // Counters; writes set the reload values. case 4: counter_[0].reload = (counter_[0].reload & 0xff00) | uint16_t(value << 0); break; - case 5: counter_[0].reload = (counter_[0].reload & 0x00ff) | uint16_t(value << 8); break; + case 5: + counter_[0].reload = (counter_[0].reload & 0x00ff) | uint16_t(value << 8); + counter_[0].is_counting = true; + break; case 6: counter_[1].reload = (counter_[1].reload & 0xff00) | uint16_t(value << 0); break; - case 7: counter_[1].reload = (counter_[1].reload & 0x00ff) | uint16_t(value << 8); break; + case 7: + counter_[1].reload = (counter_[1].reload & 0x00ff) | uint16_t(value << 8); + counter_[1].is_counting = true; + break; // Time-of-day clock. // @@ -138,6 +144,7 @@ void MOS6526::write(int address, uint8_t value) { case 14: if(value & 0x10) { counter_[0].value = counter_[0].reload; + counter_[0].is_counting = true; } counter_[0].control = value & 0xef; printf("Ignoring control A write: %02x\n", value); @@ -146,6 +153,7 @@ void MOS6526::write(int address, uint8_t value) { case 15: if(value & 0x10) { counter_[1].value = counter_[1].reload; + counter_[1].is_counting = true; } counter_[1].control = value; printf("Ignoring control B write: %02x\n", value); diff --git a/Components/6526/Implementation/6526Storage.hpp b/Components/6526/Implementation/6526Storage.hpp index 154ee9efb..efaa0638a 100644 --- a/Components/6526/Implementation/6526Storage.hpp +++ b/Components/6526/Implementation/6526Storage.hpp @@ -30,18 +30,20 @@ struct MOS6526Storage { uint16_t reload = 0; uint16_t value = 0; uint8_t control = 0; -// int one_shot_mask = 0; + bool is_counting = false; int subtract(int count) { if(control & 8) { // One-shot. -// value -= count; -// if(value < 0) { -// const int underflows = one_shot_mask; -// value = 0; -// one_shot_mask = 0; -// return underflows; -// } + if(is_counting) { + if(value < count) { + value = reload; + is_counting = false; + return 1; + } else { + value -= count; + } + } return 0; } else { // Continuous.