2021-07-18 16:23:47 +00:00
|
|
|
//
|
|
|
|
// 6526Storage.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 18/07/2021.
|
|
|
|
// Copyright © 2021 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef _526Storage_h
|
|
|
|
#define _526Storage_h
|
|
|
|
|
2021-08-04 01:13:08 +00:00
|
|
|
#include <array>
|
|
|
|
|
2021-07-18 16:23:47 +00:00
|
|
|
namespace MOS {
|
|
|
|
namespace MOS6526 {
|
|
|
|
|
2021-08-03 23:10:09 +00:00
|
|
|
class TODBase {
|
|
|
|
public:
|
|
|
|
template <bool is_timer2> void set_control(uint8_t value) {
|
|
|
|
if constexpr (is_timer2) {
|
|
|
|
write_alarm = value & 0x80;
|
|
|
|
} else {
|
|
|
|
is_50Hz = value & 0x80;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool write_alarm = false, is_50Hz = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <bool is_8250> class TODStorage {};
|
2021-08-04 01:13:08 +00:00
|
|
|
|
2021-08-03 23:10:09 +00:00
|
|
|
template <> class TODStorage<false>: public TODBase {
|
2021-08-04 01:13:08 +00:00
|
|
|
private:
|
|
|
|
bool increment_ = true, latched_ = false;
|
|
|
|
int divider_ = 0;
|
|
|
|
std::array<uint8_t, 4> value_;
|
|
|
|
std::array<uint8_t, 4> latch_;
|
|
|
|
std::array<uint8_t, 4> alarm_;
|
|
|
|
|
|
|
|
static constexpr uint8_t masks[4] = {0xf, 0x3f, 0x3f, 0x1f};
|
|
|
|
|
|
|
|
void bcd_increment(uint8_t &value) {
|
|
|
|
++value;
|
|
|
|
if((value&0x0f) > 0x09) value += 0x06;
|
|
|
|
}
|
|
|
|
|
2021-08-03 23:10:09 +00:00
|
|
|
public:
|
2021-08-04 01:13:08 +00:00
|
|
|
template <int byte> void write(uint8_t v) {
|
|
|
|
if(write_alarm) {
|
|
|
|
alarm_[byte] = v & masks[byte];
|
|
|
|
} else {
|
|
|
|
value_[byte] = v & masks[byte];
|
|
|
|
|
|
|
|
if constexpr (byte == 0) {
|
|
|
|
increment_ = true;
|
|
|
|
}
|
|
|
|
if constexpr (byte == 3) {
|
|
|
|
increment_ = false;
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 23:10:09 +00:00
|
|
|
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <int byte> uint8_t read() {
|
2021-08-04 01:13:08 +00:00
|
|
|
if(latched_) {
|
|
|
|
const uint8_t result = latch_[byte];
|
|
|
|
if constexpr (byte == 0) {
|
|
|
|
latched_ = false;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if constexpr (byte == 3) {
|
|
|
|
latched_ = true;
|
|
|
|
latch_ = value_;
|
|
|
|
}
|
|
|
|
return value_[byte];
|
2021-08-03 23:10:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 01:13:08 +00:00
|
|
|
bool advance(int count) {
|
|
|
|
if(!increment_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(count--) {
|
|
|
|
// Increment the pre-10ths divider.
|
|
|
|
++divider_;
|
|
|
|
if(divider_ < 5) continue;
|
|
|
|
if(divider_ < 6 && !is_50Hz) continue;
|
|
|
|
divider_ = 0;
|
|
|
|
|
|
|
|
// Increments 10ths of a second. One BCD digit.
|
|
|
|
++value_[0];
|
|
|
|
if(value_[0] < 10) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment seconds. Actual BCD needed from here onwards.
|
|
|
|
bcd_increment(value_[1]);
|
|
|
|
if(value_[1] != 60) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
value_[1] = 0;
|
|
|
|
|
|
|
|
// Increment minutes.
|
|
|
|
bcd_increment(value_[2]);
|
|
|
|
if(value_[2] != 60) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
value_[2] = 0;
|
|
|
|
|
|
|
|
// TODO: increment hours, keeping AM/PM separate?
|
|
|
|
}
|
|
|
|
|
|
|
|
return false; // TODO: test against alarm.
|
2021-08-03 23:10:09 +00:00
|
|
|
}
|
2021-08-03 22:50:58 +00:00
|
|
|
};
|
2021-08-04 01:13:08 +00:00
|
|
|
|
2021-08-03 23:10:09 +00:00
|
|
|
template <> class TODStorage<true>: public TODBase {
|
|
|
|
private:
|
|
|
|
uint32_t increment_mask_ = uint32_t(~0);
|
|
|
|
uint32_t latch_ = 0;
|
|
|
|
uint32_t value_ = 0;
|
|
|
|
uint32_t alarm_ = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <int byte> void write(uint8_t v) {
|
|
|
|
if constexpr (byte == 3) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
constexpr int shift = byte << 3;
|
|
|
|
|
|
|
|
// Write to either the alarm or the current value as directed;
|
|
|
|
// writing to any part of the current value other than the LSB
|
|
|
|
// pauses incrementing until the LSB is written.
|
|
|
|
if(write_alarm) {
|
|
|
|
alarm_ = (alarm_ & (0x00ff'ffff >> (24 - shift))) | uint32_t(v << shift);
|
|
|
|
} else {
|
|
|
|
value_ = (alarm_ & (0x00ff'ffff >> (24 - shift))) | uint32_t(v << shift);
|
|
|
|
increment_mask_ = (shift == 0) ? uint32_t(~0) : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <int byte> uint8_t read() {
|
|
|
|
if constexpr (byte == 3) {
|
|
|
|
return 0xff; // Assumed. Just a guss.
|
|
|
|
}
|
|
|
|
constexpr int shift = byte << 3;
|
|
|
|
|
|
|
|
if(latch_) {
|
|
|
|
// Latching: if this is a latched read from the LSB,
|
|
|
|
// empty the latch.
|
|
|
|
const uint8_t result = uint8_t((latch_ >> shift) & 0xff);
|
|
|
|
if constexpr (shift == 0) {
|
|
|
|
latch_ = 0;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
// Latching: if this is a read from the MSB, latch now.
|
|
|
|
if constexpr (shift == 16) {
|
|
|
|
latch_ = value_ | 0xff00'0000;
|
|
|
|
}
|
|
|
|
return uint8_t((value_ >> shift) & 0xff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool advance(int count) {
|
|
|
|
// The 8250 uses a simple binary counter to replace the
|
|
|
|
// 6526's time-of-day clock. So this is easy.
|
|
|
|
const uint32_t distance_to_alarm = (alarm_ - value_) & 0xffffff;
|
|
|
|
value_ += uint32_t(count) & increment_mask_;
|
|
|
|
return distance_to_alarm <= uint32_t(count);
|
|
|
|
}
|
2021-08-03 22:50:58 +00:00
|
|
|
};
|
|
|
|
|
2021-07-18 16:23:47 +00:00
|
|
|
struct MOS6526Storage {
|
2021-07-24 01:24:07 +00:00
|
|
|
HalfCycles half_divider_;
|
2021-07-18 16:23:47 +00:00
|
|
|
|
2021-07-24 01:24:07 +00:00
|
|
|
uint8_t output_[2] = {0, 0};
|
|
|
|
uint8_t data_direction_[2] = {0, 0};
|
2021-07-24 01:58:52 +00:00
|
|
|
|
2021-07-24 01:24:07 +00:00
|
|
|
uint8_t interrupt_control_ = 0;
|
2021-07-24 01:58:52 +00:00
|
|
|
uint8_t interrupt_state_ = 0;
|
|
|
|
|
2021-07-24 01:24:07 +00:00
|
|
|
struct Counter {
|
|
|
|
uint16_t reload = 0;
|
|
|
|
uint16_t value = 0;
|
2021-07-24 02:43:47 +00:00
|
|
|
uint8_t control = 0;
|
|
|
|
|
2021-07-24 20:06:49 +00:00
|
|
|
template <int shift> void set_reload(uint8_t v) {
|
|
|
|
reload = (reload & (0xff00 >> shift)) | uint16_t(v << shift);
|
|
|
|
|
2021-08-02 01:09:02 +00:00
|
|
|
if constexpr (shift == 8) {
|
|
|
|
if(!(control&1)) {
|
|
|
|
pending |= ReloadInOne;
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 20:57:05 +00:00
|
|
|
|
|
|
|
// If this write has hit during a reload cycle, reload.
|
|
|
|
if(pending & ReloadNow) {
|
|
|
|
value = reload;
|
|
|
|
}
|
2021-07-24 20:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool is_counter_2> void set_control(uint8_t v) {
|
2021-08-02 01:09:02 +00:00
|
|
|
control = v;
|
2021-08-01 22:14:10 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 01:04:00 +00:00
|
|
|
template <bool is_counter_2> bool advance(bool chained_input) {
|
2021-08-03 20:57:05 +00:00
|
|
|
// TODO: remove most of the conditionals here in favour of bit shuffling.
|
2021-07-26 21:02:30 +00:00
|
|
|
|
2021-08-03 20:57:05 +00:00
|
|
|
pending = (pending & PendingClearMask) << 1;
|
2021-08-01 22:14:10 +00:00
|
|
|
|
2021-08-03 00:14:01 +00:00
|
|
|
//
|
|
|
|
// Apply feeder states inputs: anything that
|
|
|
|
// will take effect in the future.
|
|
|
|
//
|
2021-08-03 01:04:00 +00:00
|
|
|
|
|
|
|
// Schedule a force reload if requested.
|
2021-08-02 01:09:02 +00:00
|
|
|
if(control & 0x10) {
|
2021-08-02 01:35:08 +00:00
|
|
|
pending |= ReloadInOne;
|
2021-08-02 01:09:02 +00:00
|
|
|
control &= ~0x10;
|
2021-07-26 21:02:30 +00:00
|
|
|
}
|
2021-08-03 01:04:00 +00:00
|
|
|
|
|
|
|
// Determine whether an input clock is applicable.
|
|
|
|
if constexpr(is_counter_2) {
|
|
|
|
switch(control&0x60) {
|
2021-08-03 21:03:58 +00:00
|
|
|
case 0x00: // Count Phi2 pulses.
|
|
|
|
pending |= TestInputNow;
|
2021-08-03 01:04:00 +00:00
|
|
|
break;
|
2021-08-03 21:12:08 +00:00
|
|
|
case 0x40: // Count timer A reloads.
|
2021-08-03 21:03:58 +00:00
|
|
|
pending |= chained_input ? TestInputNow : 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x20: // Count negative CNTs.
|
|
|
|
case 0x60: // Count timer A transitions when CNT is low.
|
2021-08-03 01:04:00 +00:00
|
|
|
default:
|
|
|
|
// TODO: all other forms of input.
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(!(control&0x20)) {
|
|
|
|
pending |= TestInputNow;
|
|
|
|
} else if (chained_input) { // TODO: check CNT directly, probably?
|
|
|
|
pending |= TestInputInOne;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(pending&TestInputNow && control&1) {
|
2021-08-01 22:14:10 +00:00
|
|
|
pending |= ApplyClockInTwo;
|
|
|
|
}
|
2021-08-03 01:04:00 +00:00
|
|
|
|
|
|
|
// Keep a history of the one-shot bit.
|
2021-08-01 22:14:10 +00:00
|
|
|
if(control & 0x08) {
|
2021-08-03 01:04:00 +00:00
|
|
|
pending |= OneShotInOne | OneShotNow;
|
2021-08-01 22:14:10 +00:00
|
|
|
}
|
2021-07-24 02:43:47 +00:00
|
|
|
|
2021-08-01 22:14:10 +00:00
|
|
|
|
2021-08-03 00:14:01 +00:00
|
|
|
//
|
|
|
|
// Perform a timer tick and decide whether a reload is prompted.
|
|
|
|
//
|
2021-08-01 22:14:10 +00:00
|
|
|
if(pending & ApplyClockNow) {
|
|
|
|
--value;
|
2021-08-03 00:14:01 +00:00
|
|
|
}
|
2021-08-03 01:04:00 +00:00
|
|
|
|
2021-08-03 00:14:01 +00:00
|
|
|
const bool should_reload = !value && (pending & ApplyClockInOne);
|
|
|
|
|
|
|
|
// Schedule a reload if so ordered.
|
|
|
|
if(should_reload) {
|
|
|
|
pending |= ReloadNow; // Combine this decision with a deferred
|
|
|
|
// input from the force-reoad test above.
|
|
|
|
|
|
|
|
// If this was one-shot, stop.
|
|
|
|
if(pending&(OneShotInOne | OneShotNow)) {
|
|
|
|
control &= ~1;
|
2021-08-03 01:04:00 +00:00
|
|
|
pending &= ~(ApplyClockInOne|ApplyClockInTwo); // Cancel scheculed ticks.
|
2021-08-03 00:14:01 +00:00
|
|
|
}
|
2021-07-24 02:43:47 +00:00
|
|
|
}
|
2021-08-02 01:09:02 +00:00
|
|
|
|
2021-08-03 00:14:01 +00:00
|
|
|
// Reload if scheduled.
|
|
|
|
if(pending & ReloadNow) {
|
|
|
|
value = reload;
|
2021-08-03 01:04:00 +00:00
|
|
|
pending &= ~ApplyClockInOne; // Skip next decrement.
|
2021-08-02 01:09:02 +00:00
|
|
|
}
|
2021-08-02 01:35:08 +00:00
|
|
|
|
2021-08-03 00:14:01 +00:00
|
|
|
|
|
|
|
return should_reload;
|
2021-07-24 02:43:47 +00:00
|
|
|
}
|
2021-08-01 22:14:10 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int pending = 0;
|
|
|
|
|
2021-08-03 01:04:00 +00:00
|
|
|
static constexpr int ReloadInOne = 1 << 0;
|
|
|
|
static constexpr int ReloadNow = 1 << 1;
|
|
|
|
|
|
|
|
static constexpr int OneShotInOne = 1 << 2;
|
|
|
|
static constexpr int OneShotNow = 1 << 3;
|
2021-08-01 22:14:10 +00:00
|
|
|
|
2021-08-03 01:04:00 +00:00
|
|
|
static constexpr int ApplyClockInTwo = 1 << 4;
|
|
|
|
static constexpr int ApplyClockInOne = 1 << 5;
|
|
|
|
static constexpr int ApplyClockNow = 1 << 6;
|
2021-08-01 22:14:10 +00:00
|
|
|
|
2021-08-03 01:04:00 +00:00
|
|
|
static constexpr int TestInputInOne = 1 << 7;
|
|
|
|
static constexpr int TestInputNow = 1 << 8;
|
2021-08-01 22:14:10 +00:00
|
|
|
|
2021-08-02 01:09:02 +00:00
|
|
|
static constexpr int PendingClearMask = ~(ReloadNow | OneShotNow | ApplyClockNow);
|
2021-08-01 22:14:10 +00:00
|
|
|
|
|
|
|
bool active_ = false;
|
2021-07-24 02:43:47 +00:00
|
|
|
} counter_[2];
|
2021-08-02 01:09:02 +00:00
|
|
|
|
|
|
|
static constexpr int InterruptInOne = 1 << 0;
|
|
|
|
static constexpr int InterruptNow = 1 << 1;
|
|
|
|
static constexpr int PendingClearMask = ~(InterruptNow);
|
|
|
|
int pending_ = 0;
|
2021-07-18 16:23:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _526Storage_h */
|