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-10-04 12:26:38 +00:00
|
|
|
#include "../../../ClockReceiver/ClockReceiver.hpp"
|
|
|
|
|
2023-05-10 21:02:18 +00:00
|
|
|
namespace MOS::MOS6526 {
|
2021-07-18 16:23:47 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-10-30 19:02:43 +00:00
|
|
|
uint32_t alarm_ = 0xff'ffff;
|
2021-08-03 23:10:09 +00:00
|
|
|
|
|
|
|
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.
|
2021-10-30 19:02:43 +00:00
|
|
|
const uint32_t mask = uint32_t(~(0xff << shift));
|
2021-08-03 23:10:09 +00:00
|
|
|
if(write_alarm) {
|
2021-10-30 19:02:43 +00:00
|
|
|
alarm_ = (alarm_ & mask) | uint32_t(v << shift);
|
2021-08-03 23:10:09 +00:00
|
|
|
} else {
|
2021-10-30 19:02:43 +00:00
|
|
|
value_ = (value_ & mask) | uint32_t(v << shift);
|
|
|
|
increment_mask_ = (byte == 0) ? uint32_t(~0) : 0;
|
2021-08-03 23:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <int byte> uint8_t read() {
|
|
|
|
if constexpr (byte == 3) {
|
2021-10-30 19:02:43 +00:00
|
|
|
return 0xff; // Assumed. Just a guess.
|
2021-08-03 23:10:09 +00:00
|
|
|
}
|
|
|
|
constexpr int shift = byte << 3;
|
|
|
|
|
2021-10-30 19:02:43 +00:00
|
|
|
if constexpr (byte == 2) {
|
|
|
|
latch_ = value_ | 0xff00'0000;
|
2021-08-03 23:10:09 +00:00
|
|
|
}
|
2021-10-30 19:02:43 +00:00
|
|
|
|
|
|
|
const uint32_t source = latch_ ? latch_ : value_;
|
|
|
|
const uint8_t result = uint8_t((source >> shift) & 0xff);
|
|
|
|
|
|
|
|
if constexpr (byte == 0) {
|
|
|
|
latch_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2021-08-03 23:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool advance(int count) {
|
|
|
|
// The 8250 uses a simple binary counter to replace the
|
|
|
|
// 6526's time-of-day clock. So this is easy.
|
2021-10-30 19:02:43 +00:00
|
|
|
const uint32_t distance_to_alarm = (alarm_ - value_) & 0xff'ffff;
|
|
|
|
const auto increment = uint32_t(count) & increment_mask_;
|
|
|
|
value_ = (value_ + increment) & 0xff'ffff;
|
|
|
|
return distance_to_alarm <= increment;
|
2021-08-03 23:10:09 +00:00
|
|
|
}
|
2021-08-03 22:50:58 +00:00
|
|
|
};
|
|
|
|
|
2021-07-18 16:23:47 +00:00
|
|
|
struct MOS6526Storage {
|
2021-08-04 02:19:41 +00:00
|
|
|
bool cnt_state_ = false; // Inactive by default.
|
|
|
|
bool cnt_edge_ = false;
|
2021-10-09 11:08:59 +00:00
|
|
|
bool flag_state_ = false;
|
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-11-04 02:26:45 +00:00
|
|
|
uint8_t shift_data_ = 0;
|
|
|
|
uint8_t shift_register_ = 0;
|
|
|
|
int shift_bits_ = 0;
|
2021-11-07 13:18:54 +00:00
|
|
|
bool shifter_is_output_ = false;
|
2021-11-04 02:26:45 +00:00
|
|
|
|
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-08-09 02:32:41 +00:00
|
|
|
template <int shift, bool is_8250> void set_reload(uint8_t v) {
|
2021-07-24 20:06:49 +00:00
|
|
|
reload = (reload & (0xff00 >> shift)) | uint16_t(v << shift);
|
|
|
|
|
2021-08-02 01:09:02 +00:00
|
|
|
if constexpr (shift == 8) {
|
2021-08-09 02:32:41 +00:00
|
|
|
// This seems to be a special 8250 feature per the Amiga
|
|
|
|
// Hardware Reference Manual; cf. Appendix F.
|
|
|
|
if(is_8250) {
|
|
|
|
control |= 1;
|
2021-08-02 01:09:02 +00:00
|
|
|
pending |= ReloadInOne;
|
2021-08-09 02:32:41 +00:00
|
|
|
} else {
|
|
|
|
if(!(control&1)) {
|
|
|
|
pending |= ReloadInOne;
|
|
|
|
}
|
2021-08-02 01:09:02 +00:00
|
|
|
}
|
|
|
|
}
|
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-07 03:16:06 +00:00
|
|
|
|
|
|
|
if(v&2) {
|
|
|
|
printf("UNIMPLEMENTED: PB strobe\n");
|
|
|
|
}
|
2021-08-01 22:14:10 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 02:19:41 +00:00
|
|
|
template <bool is_counter_2> bool advance(bool chained_input, bool cnt_state, bool cnt_edge) {
|
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
|
|
|
|
2021-08-04 02:19:41 +00:00
|
|
|
// Keep a history of the one-shot bit.
|
|
|
|
if(control & 0x08) {
|
|
|
|
pending |= OneShotInOne;
|
|
|
|
}
|
|
|
|
|
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-04 02:19:41 +00:00
|
|
|
case 0x20: // Count negative CNTs, with an extra cycle of delay.
|
|
|
|
pending |= cnt_edge ? TestInputInOne : 0;
|
|
|
|
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 0x60: // Count timer A transitions when CNT is low.
|
2021-08-04 02:27:23 +00:00
|
|
|
pending |= chained_input && cnt_state ? TestInputNow : 0;
|
2021-08-03 01:04:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(!(control&0x20)) {
|
|
|
|
pending |= TestInputNow;
|
2021-08-04 02:19:41 +00:00
|
|
|
} else if (cnt_edge) {
|
2021-08-03 01:04:00 +00:00
|
|
|
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
|
|
|
|
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-04 01:45:44 +00:00
|
|
|
pending &= ~(ApplyClockInOne|ApplyClockInTwo); // Cancel scheduled 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 */
|