2021-07-18 15:36:13 +00:00
|
|
|
//
|
|
|
|
// 6526Implementation.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 18/07/2021.
|
|
|
|
// Copyright © 2021 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef _526Implementation_h
|
|
|
|
#define _526Implementation_h
|
|
|
|
|
2021-07-18 16:13:56 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdio>
|
|
|
|
|
2021-07-18 15:36:13 +00:00
|
|
|
namespace MOS {
|
|
|
|
namespace MOS6526 {
|
|
|
|
|
2021-07-18 21:17:41 +00:00
|
|
|
template <typename BusHandlerT, Personality personality>
|
|
|
|
template <int port> void MOS6526<BusHandlerT, personality>::set_port_output() {
|
2021-07-24 01:24:07 +00:00
|
|
|
const uint8_t output = output_[port] | (~data_direction_[port]);
|
2021-07-18 21:17:41 +00:00
|
|
|
port_handler_.set_port_output(Port(port), output);
|
|
|
|
}
|
|
|
|
|
2021-07-19 00:25:04 +00:00
|
|
|
template <typename BusHandlerT, Personality personality>
|
|
|
|
template <int port> uint8_t MOS6526<BusHandlerT, personality>::get_port_input() {
|
|
|
|
const uint8_t input = port_handler_.get_port_input(Port(port));
|
2021-07-24 01:24:07 +00:00
|
|
|
return (input & ~data_direction_[port]) | (output_[port] & data_direction_[port]);
|
2021-07-19 00:25:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 01:58:52 +00:00
|
|
|
template <typename BusHandlerT, Personality personality>
|
|
|
|
void MOS6526<BusHandlerT, personality>::posit_interrupt(uint8_t mask) {
|
2021-08-02 01:09:02 +00:00
|
|
|
if(!mask) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-24 01:58:52 +00:00
|
|
|
interrupt_state_ |= mask;
|
|
|
|
update_interrupts();
|
|
|
|
}
|
|
|
|
|
2021-07-22 20:09:32 +00:00
|
|
|
template <typename BusHandlerT, Personality personality>
|
|
|
|
void MOS6526<BusHandlerT, personality>::update_interrupts() {
|
2021-07-24 01:58:52 +00:00
|
|
|
if(interrupt_state_ & interrupt_control_) {
|
2021-08-02 01:09:02 +00:00
|
|
|
pending_ |= InterruptInOne;
|
2021-07-24 01:58:52 +00:00
|
|
|
}
|
2021-07-28 23:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename BusHandlerT, Personality personality>
|
|
|
|
bool MOS6526<BusHandlerT, personality>::get_interrupt_line() {
|
|
|
|
return interrupt_state_ & 0x80;
|
2021-07-22 20:09:32 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 15:36:13 +00:00
|
|
|
template <typename BusHandlerT, Personality personality>
|
|
|
|
void MOS6526<BusHandlerT, personality>::write(int address, uint8_t value) {
|
2021-07-18 16:13:56 +00:00
|
|
|
address &= 0xf;
|
2021-07-18 16:23:47 +00:00
|
|
|
switch(address) {
|
2021-07-18 21:17:41 +00:00
|
|
|
// Port output.
|
|
|
|
case 0:
|
2021-07-24 01:24:07 +00:00
|
|
|
output_[0] = value;
|
2021-07-18 21:17:41 +00:00
|
|
|
set_port_output<0>();
|
|
|
|
break;
|
|
|
|
case 1:
|
2021-07-24 01:24:07 +00:00
|
|
|
output_[1] = value;
|
2021-07-18 21:17:41 +00:00
|
|
|
set_port_output<1>();
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Port direction.
|
|
|
|
case 2:
|
2021-07-24 01:24:07 +00:00
|
|
|
data_direction_[0] = value;
|
2021-07-18 21:17:41 +00:00
|
|
|
set_port_output<0>();
|
|
|
|
break;
|
|
|
|
case 3:
|
2021-07-24 01:24:07 +00:00
|
|
|
data_direction_[1] = value;
|
2021-07-18 21:17:41 +00:00
|
|
|
set_port_output<1>();
|
2021-07-18 16:23:47 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-24 01:24:07 +00:00
|
|
|
// Counters; writes set the reload values.
|
2021-07-24 20:06:49 +00:00
|
|
|
case 4: counter_[0].template set_reload<0>(value); break;
|
|
|
|
case 5: counter_[0].template set_reload<8>(value); break;
|
|
|
|
case 6: counter_[1].template set_reload<0>(value); break;
|
|
|
|
case 7: counter_[1].template set_reload<8>(value); break;
|
2021-07-24 01:24:07 +00:00
|
|
|
|
|
|
|
// Time-of-day clock.
|
|
|
|
//
|
|
|
|
// 8520: a binary counter; stopped on any write, restarted
|
|
|
|
// upon a write to the LSB.
|
|
|
|
case 8:
|
|
|
|
if constexpr (personality == Personality::P8250) {
|
2021-07-24 02:43:47 +00:00
|
|
|
if(counter_[1].control & 0x80) {
|
2021-07-24 01:58:52 +00:00
|
|
|
tod_alarm_ = (tod_alarm_ & 0xffff00) | uint32_t(value);
|
|
|
|
} else {
|
|
|
|
tod_ = (tod_ & 0xffff00) | uint32_t(value);
|
|
|
|
tod_increment_mask_ = uint32_t(~0);
|
|
|
|
}
|
2021-07-24 01:24:07 +00:00
|
|
|
} else {
|
|
|
|
printf("6526 TOD clock not implemented\n");
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
if constexpr (personality == Personality::P8250) {
|
2021-07-24 02:43:47 +00:00
|
|
|
if(counter_[1].control & 0x80) {
|
2021-07-24 01:58:52 +00:00
|
|
|
tod_alarm_ = (tod_alarm_ & 0xff00ff) | uint32_t(value << 8);
|
|
|
|
} else {
|
|
|
|
tod_ = (tod_ & 0xff00ff) | uint32_t(value << 8);
|
|
|
|
tod_increment_mask_ = 0;
|
|
|
|
}
|
2021-07-24 01:24:07 +00:00
|
|
|
} else {
|
|
|
|
printf("6526 TOD clock not implemented\n");
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
if constexpr (personality == Personality::P8250) {
|
2021-07-24 02:43:47 +00:00
|
|
|
if(counter_[1].control & 0x80) {
|
2021-07-24 01:58:52 +00:00
|
|
|
tod_alarm_ = (tod_alarm_ & 0x00ffff) | uint32_t(value << 16);
|
|
|
|
} else {
|
|
|
|
tod_ = (tod_ & 0x00ffff) | uint32_t(value << 16);
|
|
|
|
tod_increment_mask_ = 0;
|
|
|
|
}
|
2021-07-24 01:24:07 +00:00
|
|
|
} else {
|
|
|
|
printf("6526 TOD clock not implemented\n");
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 11:
|
|
|
|
if constexpr (personality != Personality::P8250) {
|
|
|
|
printf("6526 TOD clock not implemented\n");
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-07-22 20:09:32 +00:00
|
|
|
// Interrupt control.
|
2021-07-24 01:58:52 +00:00
|
|
|
case 13: {
|
2021-07-28 23:26:02 +00:00
|
|
|
if(value & 0x80) {
|
2021-07-24 01:58:52 +00:00
|
|
|
interrupt_control_ |= value & 0x7f;
|
|
|
|
} else {
|
|
|
|
interrupt_control_ &= ~(value & 0x7f);
|
|
|
|
}
|
2021-07-22 20:09:32 +00:00
|
|
|
update_interrupts();
|
2021-07-24 01:58:52 +00:00
|
|
|
} break;
|
2021-07-22 20:09:32 +00:00
|
|
|
|
2021-07-22 23:28:01 +00:00
|
|
|
// Control.
|
2021-07-24 20:06:49 +00:00
|
|
|
case 14: counter_[0].template set_control<false>(value); break;
|
|
|
|
case 15: counter_[1].template set_control<true>(value); break;
|
2021-07-22 23:28:01 +00:00
|
|
|
|
2021-07-18 16:23:47 +00:00
|
|
|
default:
|
|
|
|
printf("Unhandled 6526 write: %02x to %d\n", value, address);
|
2021-07-24 01:24:07 +00:00
|
|
|
assert(false);
|
2021-07-18 16:23:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-07-18 15:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename BusHandlerT, Personality personality>
|
|
|
|
uint8_t MOS6526<BusHandlerT, personality>::read(int address) {
|
2021-07-18 16:13:56 +00:00
|
|
|
address &= 0xf;
|
2021-07-18 16:23:47 +00:00
|
|
|
switch(address) {
|
2021-07-22 20:09:32 +00:00
|
|
|
case 0: return get_port_input<0>();
|
|
|
|
case 1: return get_port_input<1>();
|
2021-07-18 21:17:41 +00:00
|
|
|
|
2021-07-18 16:23:47 +00:00
|
|
|
case 2: case 3:
|
2021-07-24 01:24:07 +00:00
|
|
|
return data_direction_[address - 2];
|
|
|
|
|
|
|
|
// Counters; reads obtain the current values.
|
2021-07-24 02:43:47 +00:00
|
|
|
case 4: return uint8_t(counter_[0].value >> 0);
|
|
|
|
case 5: return uint8_t(counter_[0].value >> 8);
|
|
|
|
case 6: return uint8_t(counter_[1].value >> 0);
|
2021-07-26 00:41:15 +00:00
|
|
|
case 7: return uint8_t(counter_[1].value >> 8);
|
2021-07-22 20:09:32 +00:00
|
|
|
|
2021-07-24 01:58:52 +00:00
|
|
|
// Interrupt state.
|
|
|
|
case 13: {
|
|
|
|
const uint8_t result = interrupt_state_;
|
|
|
|
interrupt_state_ = 0;
|
2021-08-02 11:47:08 +00:00
|
|
|
pending_ &= ~(InterruptNow | InterruptInOne);
|
2021-07-24 01:58:52 +00:00
|
|
|
update_interrupts();
|
|
|
|
return result;
|
|
|
|
} break;
|
|
|
|
|
2021-07-22 23:28:01 +00:00
|
|
|
case 14: case 15:
|
2021-07-24 02:43:47 +00:00
|
|
|
return counter_[address - 14].control;
|
2021-07-24 01:24:07 +00:00
|
|
|
|
|
|
|
// Time-of-day clock.
|
|
|
|
//
|
|
|
|
// 8250: Latch on MSB. Unlatch on LSB. Read raw if not latched.
|
|
|
|
case 8:
|
|
|
|
if constexpr (personality == Personality::P8250) {
|
|
|
|
if(tod_latch_) {
|
|
|
|
const uint8_t result = tod_latch_ & 0xff;
|
|
|
|
tod_latch_ = 0;
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return tod_ & 0xff;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("6526 TOD clock not implemented\n");
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
if constexpr (personality == Personality::P8250) {
|
|
|
|
if(tod_latch_) {
|
|
|
|
return (tod_latch_ >> 8) & 0xff;
|
|
|
|
} else {
|
|
|
|
return (tod_ >> 8) & 0xff;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("6526 TOD clock not implemented\n");
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
if constexpr (personality == Personality::P8250) {
|
|
|
|
tod_latch_ = tod_ | 0xff00'0000;
|
|
|
|
return (tod_ >> 16) & 0xff;
|
|
|
|
} else {
|
|
|
|
printf("6526 TOD clock not implemented\n");
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 11:
|
|
|
|
if constexpr (personality == Personality::P8250) {
|
|
|
|
return 0x00; // Assumed. Just a guss.
|
|
|
|
} else {
|
|
|
|
printf("6526 TOD clock not implemented\n");
|
|
|
|
assert(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
2021-07-18 16:23:47 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
printf("Unhandled 6526 read from %d\n", address);
|
2021-07-24 01:24:07 +00:00
|
|
|
assert(false);
|
2021-07-18 16:23:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-07-18 15:36:13 +00:00
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
|
2021-07-18 15:49:10 +00:00
|
|
|
template <typename BusHandlerT, Personality personality>
|
|
|
|
void MOS6526<BusHandlerT, personality>::run_for(const HalfCycles half_cycles) {
|
2021-07-24 01:24:07 +00:00
|
|
|
half_divider_ += half_cycles;
|
2021-08-01 22:14:10 +00:00
|
|
|
int sub = half_divider_.divide_cycles().template as<int>();
|
2021-07-24 01:24:07 +00:00
|
|
|
|
2021-08-01 22:14:10 +00:00
|
|
|
while(sub--) {
|
2021-08-02 01:09:02 +00:00
|
|
|
pending_ <<= 1;
|
|
|
|
if(pending_ & InterruptNow) {
|
|
|
|
interrupt_state_ |= 0x80;
|
|
|
|
}
|
|
|
|
pending_ &= PendingClearMask;
|
|
|
|
|
|
|
|
// TODO: use CNT potentially to clock timer A, elimiante conditional above.
|
2021-08-03 00:14:01 +00:00
|
|
|
const bool timer1_did_reload = counter_[0].advance(false);
|
2021-08-03 00:17:37 +00:00
|
|
|
|
|
|
|
const bool timer1_carry = timer1_did_reload && (counter_[1].control & 0x60) == 0x40;
|
|
|
|
const bool timer2_did_reload = counter_[1].advance(timer1_carry);
|
2021-08-03 00:14:01 +00:00
|
|
|
posit_interrupt((timer1_did_reload ? 0x01 : 0x00) | (timer2_did_reload ? 0x02 : 0x00));
|
2021-08-01 22:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 01:24:07 +00:00
|
|
|
template <typename BusHandlerT, Personality personality>
|
|
|
|
void MOS6526<BusHandlerT, personality>::advance_tod(int count) {
|
2021-07-26 21:02:30 +00:00
|
|
|
if(!count) return;
|
|
|
|
|
2021-07-24 01:24:07 +00:00
|
|
|
if constexpr(personality == Personality::P8250) {
|
|
|
|
// The 8250 uses a simple binary counter to replace the
|
|
|
|
// 6526's time-of-day clock. So this is easy.
|
2021-07-24 01:58:52 +00:00
|
|
|
const uint32_t distance_to_alarm_ = (tod_alarm_ - tod_) & 0xffffff;
|
2021-07-24 01:24:07 +00:00
|
|
|
tod_ += uint32_t(count) & tod_increment_mask_;
|
2021-07-24 01:58:52 +00:00
|
|
|
if(distance_to_alarm_ <= uint32_t(count)) {
|
|
|
|
posit_interrupt(0x04);
|
|
|
|
}
|
2021-07-24 01:24:07 +00:00
|
|
|
} else {
|
|
|
|
// The 6526 uses a time-of-day clock. This may or may not
|
|
|
|
// be accurate.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 15:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _526Implementation_h */
|