2016-07-10 12:05:05 +00:00
|
|
|
//
|
2016-06-07 01:56:02 +00:00
|
|
|
// 6522.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 06/06/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef _522_hpp
|
|
|
|
#define _522_hpp
|
|
|
|
|
|
|
|
#include <cstdint>
|
2016-07-07 02:17:32 +00:00
|
|
|
#include <typeinfo>
|
2016-06-08 02:01:14 +00:00
|
|
|
#include <cstdio>
|
2016-06-07 01:56:02 +00:00
|
|
|
|
2017-07-27 12:05:14 +00:00
|
|
|
#include "../../ClockReceiver/ClockReceiver.hpp"
|
|
|
|
|
2016-06-07 01:56:02 +00:00
|
|
|
namespace MOS {
|
|
|
|
|
2016-06-19 22:11:37 +00:00
|
|
|
/*!
|
2016-06-19 22:57:40 +00:00
|
|
|
Implements a template for emulation of the MOS 6522 Versatile Interface Adaptor ('VIA').
|
2016-06-19 22:11:37 +00:00
|
|
|
|
|
|
|
The VIA provides:
|
|
|
|
* two timers, each of which may trigger interrupts and one of which may repeat;
|
|
|
|
* two digial input/output ports; and
|
|
|
|
* a serial-to-parallel shifter.
|
|
|
|
|
2016-06-19 22:57:40 +00:00
|
|
|
Consumers should derive their own curiously-recurring-template-pattern subclass,
|
|
|
|
implementing bus communications as required.
|
2016-06-19 22:11:37 +00:00
|
|
|
*/
|
2017-07-27 11:40:02 +00:00
|
|
|
template <class T> class MOS6522 {
|
2016-06-10 02:37:59 +00:00
|
|
|
private:
|
|
|
|
enum InterruptFlag: uint8_t {
|
|
|
|
CA2ActiveEdge = 1 << 0,
|
|
|
|
CA1ActiveEdge = 1 << 1,
|
|
|
|
ShiftRegister = 1 << 2,
|
|
|
|
CB2ActiveEdge = 1 << 3,
|
|
|
|
CB1ActiveEdge = 1 << 4,
|
|
|
|
Timer2 = 1 << 5,
|
|
|
|
Timer1 = 1 << 6,
|
|
|
|
};
|
|
|
|
|
2016-06-07 01:56:02 +00:00
|
|
|
public:
|
2016-06-26 16:30:01 +00:00
|
|
|
enum Port {
|
|
|
|
A = 0,
|
|
|
|
B = 1
|
|
|
|
};
|
|
|
|
|
2016-06-26 20:32:27 +00:00
|
|
|
enum Line {
|
|
|
|
One = 0,
|
|
|
|
Two = 1
|
|
|
|
};
|
|
|
|
|
2016-06-19 22:11:37 +00:00
|
|
|
/*! Sets a register value. */
|
2017-03-26 18:34:47 +00:00
|
|
|
inline void set_register(int address, uint8_t value) {
|
2016-06-09 02:15:24 +00:00
|
|
|
address &= 0xf;
|
2016-07-07 02:17:32 +00:00
|
|
|
// printf("6522 [%s]: %0x <- %02x\n", typeid(*this).name(), address, value);
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(address) {
|
2016-06-11 17:06:01 +00:00
|
|
|
case 0x0:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.output[1] = value;
|
|
|
|
static_cast<T *>(this)->set_port_output(Port::B, value, registers_.data_direction[1]); // TODO: handshake
|
2016-06-27 01:30:06 +00:00
|
|
|
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interrupt_flags &= ~(InterruptFlag::CB1ActiveEdge | ((registers_.peripheral_control&0x20) ? 0 : InterruptFlag::CB2ActiveEdge));
|
2016-06-27 01:30:06 +00:00
|
|
|
reevaluate_interrupts();
|
2016-06-11 11:12:55 +00:00
|
|
|
break;
|
|
|
|
case 0xf:
|
2016-06-18 21:17:03 +00:00
|
|
|
case 0x1:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.output[0] = value;
|
|
|
|
static_cast<T *>(this)->set_port_output(Port::A, value, registers_.data_direction[0]); // TODO: handshake
|
2016-06-27 01:30:06 +00:00
|
|
|
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interrupt_flags &= ~(InterruptFlag::CA1ActiveEdge | ((registers_.peripheral_control&0x02) ? 0 : InterruptFlag::CB2ActiveEdge));
|
2016-06-27 01:30:06 +00:00
|
|
|
reevaluate_interrupts();
|
2016-06-11 11:12:55 +00:00
|
|
|
break;
|
2016-06-18 21:17:03 +00:00
|
|
|
// // No handshake, so write directly
|
2016-12-03 15:51:09 +00:00
|
|
|
// registers_.output[0] = value;
|
2016-06-18 21:17:03 +00:00
|
|
|
// static_cast<T *>(this)->set_port_output(0, value);
|
|
|
|
// break;
|
2016-06-11 15:34:39 +00:00
|
|
|
|
2016-06-11 17:06:01 +00:00
|
|
|
case 0x2:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.data_direction[1] = value;
|
2016-06-11 11:12:55 +00:00
|
|
|
break;
|
2016-06-11 17:06:01 +00:00
|
|
|
case 0x3:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.data_direction[0] = value;
|
2016-06-11 11:12:55 +00:00
|
|
|
break;
|
|
|
|
|
2016-06-09 02:15:24 +00:00
|
|
|
// Timer 1
|
2016-12-03 15:51:09 +00:00
|
|
|
case 0x6: case 0x4: registers_.timer_latch[0] = (registers_.timer_latch[0]&0xff00) | value; break;
|
2016-06-11 17:06:01 +00:00
|
|
|
case 0x5: case 0x7:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.timer_latch[0] = (registers_.timer_latch[0]&0x00ff) | (uint16_t)(value << 8);
|
|
|
|
registers_.interrupt_flags &= ~InterruptFlag::Timer1;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(address == 0x05) {
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.next_timer[0] = registers_.timer_latch[0];
|
|
|
|
timer_is_running_[0] = true;
|
2016-06-09 02:15:24 +00:00
|
|
|
}
|
2016-06-10 02:37:59 +00:00
|
|
|
reevaluate_interrupts();
|
2016-06-09 02:15:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Timer 2
|
2016-12-03 15:51:09 +00:00
|
|
|
case 0x8: registers_.timer_latch[1] = value; break;
|
2016-06-11 17:06:01 +00:00
|
|
|
case 0x9:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interrupt_flags &= ~InterruptFlag::Timer2;
|
|
|
|
registers_.next_timer[1] = registers_.timer_latch[1] | (uint16_t)(value << 8);
|
|
|
|
timer_is_running_[1] = true;
|
2016-06-10 02:37:59 +00:00
|
|
|
reevaluate_interrupts();
|
2016-06-09 02:15:24 +00:00
|
|
|
break;
|
|
|
|
|
2016-06-11 11:12:55 +00:00
|
|
|
// Shift
|
2016-12-03 15:51:09 +00:00
|
|
|
case 0xa: registers_.shift = value; break;
|
2016-06-11 11:12:55 +00:00
|
|
|
|
|
|
|
// Control
|
2016-07-01 23:01:22 +00:00
|
|
|
case 0xb:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.auxiliary_control = value;
|
2016-07-01 23:01:22 +00:00
|
|
|
break;
|
2016-06-26 20:32:27 +00:00
|
|
|
case 0xc:
|
2016-07-06 00:39:15 +00:00
|
|
|
// printf("Peripheral control %02x\n", value);
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.peripheral_control = value;
|
2016-07-06 01:15:29 +00:00
|
|
|
|
2016-07-10 02:29:11 +00:00
|
|
|
// TODO: simplify below; trying to avoid improper logging of unimplemented warnings in input mode
|
2017-03-26 18:34:47 +00:00
|
|
|
if(value & 0x08) {
|
|
|
|
switch(value & 0x0e) {
|
2016-07-06 01:15:29 +00:00
|
|
|
default: printf("Unimplemented control line mode %d\n", (value >> 1)&7); break;
|
|
|
|
case 0x0c: static_cast<T *>(this)->set_control_line_output(Port::A, Line::Two, false); break;
|
|
|
|
case 0x0e: static_cast<T *>(this)->set_control_line_output(Port::A, Line::Two, true); break;
|
|
|
|
}
|
2016-07-10 02:29:11 +00:00
|
|
|
}
|
2017-03-26 18:34:47 +00:00
|
|
|
if(value & 0x80) {
|
|
|
|
switch(value & 0xe0) {
|
2016-07-06 01:15:29 +00:00
|
|
|
default: printf("Unimplemented control line mode %d\n", (value >> 5)&7); break;
|
|
|
|
case 0xc0: static_cast<T *>(this)->set_control_line_output(Port::B, Line::Two, false); break;
|
|
|
|
case 0xe0: static_cast<T *>(this)->set_control_line_output(Port::B, Line::Two, true); break;
|
|
|
|
}
|
2016-07-01 23:01:22 +00:00
|
|
|
}
|
2016-06-26 20:32:27 +00:00
|
|
|
break;
|
2016-06-11 11:12:55 +00:00
|
|
|
|
|
|
|
// Interrupt control
|
|
|
|
case 0xd:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interrupt_flags &= ~value;
|
2016-06-11 11:12:55 +00:00
|
|
|
reevaluate_interrupts();
|
2016-06-10 02:37:59 +00:00
|
|
|
break;
|
2016-06-11 11:12:55 +00:00
|
|
|
case 0xe:
|
2016-06-11 11:57:04 +00:00
|
|
|
if(value&0x80)
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interrupt_enable |= value;
|
2016-06-11 11:57:04 +00:00
|
|
|
else
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interrupt_enable &= ~value;
|
2016-06-11 11:12:55 +00:00
|
|
|
reevaluate_interrupts();
|
2016-06-10 02:37:59 +00:00
|
|
|
break;
|
2016-06-09 02:15:24 +00:00
|
|
|
}
|
2016-06-08 02:01:14 +00:00
|
|
|
}
|
2016-06-09 02:15:24 +00:00
|
|
|
|
2016-06-19 22:11:37 +00:00
|
|
|
/*! Gets a register value. */
|
2017-03-26 18:34:47 +00:00
|
|
|
inline uint8_t get_register(int address) {
|
2016-06-09 02:15:24 +00:00
|
|
|
address &= 0xf;
|
2016-06-11 11:57:04 +00:00
|
|
|
// printf("6522 %p: %d\n", this, address);
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(address) {
|
2016-06-27 01:30:06 +00:00
|
|
|
case 0x0:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interrupt_flags &= ~(InterruptFlag::CB1ActiveEdge | InterruptFlag::CB2ActiveEdge);
|
2016-06-27 01:30:06 +00:00
|
|
|
reevaluate_interrupts();
|
2016-12-03 15:51:09 +00:00
|
|
|
return get_port_input(Port::B, registers_.data_direction[1], registers_.output[1]);
|
2016-06-11 17:06:01 +00:00
|
|
|
case 0xf: // TODO: handshake, latching
|
2016-06-27 01:30:06 +00:00
|
|
|
case 0x1:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interrupt_flags &= ~(InterruptFlag::CA1ActiveEdge | InterruptFlag::CA2ActiveEdge);
|
2016-06-27 01:30:06 +00:00
|
|
|
reevaluate_interrupts();
|
2016-12-03 15:51:09 +00:00
|
|
|
return get_port_input(Port::A, registers_.data_direction[0], registers_.output[0]);
|
2016-06-11 15:34:39 +00:00
|
|
|
|
2016-12-03 15:51:09 +00:00
|
|
|
case 0x2: return registers_.data_direction[1];
|
|
|
|
case 0x3: return registers_.data_direction[0];
|
2016-06-11 11:12:55 +00:00
|
|
|
|
2016-06-09 02:15:24 +00:00
|
|
|
// Timer 1
|
2016-06-11 17:06:01 +00:00
|
|
|
case 0x4:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interrupt_flags &= ~InterruptFlag::Timer1;
|
2016-06-11 11:12:55 +00:00
|
|
|
reevaluate_interrupts();
|
2016-12-03 15:51:09 +00:00
|
|
|
return registers_.timer[0] & 0x00ff;
|
|
|
|
case 0x5: return registers_.timer[0] >> 8;
|
|
|
|
case 0x6: return registers_.timer_latch[0] & 0x00ff;
|
|
|
|
case 0x7: return registers_.timer_latch[0] >> 8;
|
2016-06-11 11:12:55 +00:00
|
|
|
|
|
|
|
// Timer 2
|
2016-06-11 17:06:01 +00:00
|
|
|
case 0x8:
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interrupt_flags &= ~InterruptFlag::Timer2;
|
2016-06-10 02:37:59 +00:00
|
|
|
reevaluate_interrupts();
|
2016-12-03 15:51:09 +00:00
|
|
|
return registers_.timer[1] & 0x00ff;
|
|
|
|
case 0x9: return registers_.timer[1] >> 8;
|
2016-06-11 11:12:55 +00:00
|
|
|
|
2016-12-03 15:51:09 +00:00
|
|
|
case 0xa: return registers_.shift;
|
2016-06-11 11:12:55 +00:00
|
|
|
|
2016-12-03 15:51:09 +00:00
|
|
|
case 0xb: return registers_.auxiliary_control;
|
|
|
|
case 0xc: return registers_.peripheral_control;
|
2016-06-11 11:12:55 +00:00
|
|
|
|
2016-12-03 15:51:09 +00:00
|
|
|
case 0xd: return registers_.interrupt_flags | (get_interrupt_line() ? 0x80 : 0x00);
|
|
|
|
case 0xe: return registers_.interrupt_enable | 0x80;
|
2016-06-09 02:15:24 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 02:01:14 +00:00
|
|
|
return 0xff;
|
|
|
|
}
|
2016-06-07 23:15:18 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline void set_control_line_input(Port port, Line line, bool value) {
|
|
|
|
switch(line) {
|
2016-06-26 20:32:27 +00:00
|
|
|
case Line::One:
|
2016-12-03 15:51:09 +00:00
|
|
|
if( value != control_inputs_[port].line_one &&
|
|
|
|
value == !!(registers_.peripheral_control & (port ? 0x10 : 0x01))
|
2017-03-26 18:34:47 +00:00
|
|
|
) {
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interrupt_flags |= port ? InterruptFlag::CB1ActiveEdge : InterruptFlag::CA1ActiveEdge;
|
2016-06-26 20:32:27 +00:00
|
|
|
reevaluate_interrupts();
|
|
|
|
}
|
2016-12-03 15:51:09 +00:00
|
|
|
control_inputs_[port].line_one = value;
|
2016-06-26 20:32:27 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Line::Two:
|
2016-07-06 01:11:51 +00:00
|
|
|
// TODO: output modes, but probably elsewhere?
|
2016-12-03 15:51:09 +00:00
|
|
|
if( value != control_inputs_[port].line_two && // i.e. value has changed ...
|
|
|
|
!(registers_.peripheral_control & (port ? 0x80 : 0x08)) && // ... and line is input ...
|
|
|
|
value == !!(registers_.peripheral_control & (port ? 0x40 : 0x04)) // ... and it's either high or low, as required
|
2017-03-26 18:34:47 +00:00
|
|
|
) {
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interrupt_flags |= port ? InterruptFlag::CB2ActiveEdge : InterruptFlag::CA2ActiveEdge;
|
2016-07-06 00:39:15 +00:00
|
|
|
reevaluate_interrupts();
|
|
|
|
}
|
2016-12-03 15:51:09 +00:00
|
|
|
control_inputs_[port].line_two = value;
|
2016-06-26 20:32:27 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-06-11 15:34:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-28 01:06:31 +00:00
|
|
|
#define phase2() \
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.last_timer[0] = registers_.timer[0];\
|
|
|
|
registers_.last_timer[1] = registers_.timer[1];\
|
2016-10-28 01:06:31 +00:00
|
|
|
\
|
2017-03-26 18:34:47 +00:00
|
|
|
if(registers_.timer_needs_reload) {\
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.timer_needs_reload = false;\
|
|
|
|
registers_.timer[0] = registers_.timer_latch[0];\
|
2016-10-28 01:06:31 +00:00
|
|
|
}\
|
|
|
|
else\
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.timer[0] --;\
|
2016-10-28 01:06:31 +00:00
|
|
|
\
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.timer[1] --; \
|
|
|
|
if(registers_.next_timer[0] >= 0) { registers_.timer[0] = (uint16_t)registers_.next_timer[0]; registers_.next_timer[0] = -1; }\
|
|
|
|
if(registers_.next_timer[1] >= 0) { registers_.timer[1] = (uint16_t)registers_.next_timer[1]; registers_.next_timer[1] = -1; }\
|
2016-10-28 01:06:31 +00:00
|
|
|
|
|
|
|
// IRQ is raised on the half cycle after overflow
|
|
|
|
#define phase1() \
|
2017-03-26 18:34:47 +00:00
|
|
|
if((registers_.timer[1] == 0xffff) && !registers_.last_timer[1] && timer_is_running_[1]) {\
|
2016-12-03 15:51:09 +00:00
|
|
|
timer_is_running_[1] = false;\
|
|
|
|
registers_.interrupt_flags |= InterruptFlag::Timer2;\
|
2016-10-28 01:06:31 +00:00
|
|
|
reevaluate_interrupts();\
|
|
|
|
}\
|
|
|
|
\
|
2017-03-26 18:34:47 +00:00
|
|
|
if((registers_.timer[0] == 0xffff) && !registers_.last_timer[0] && timer_is_running_[0]) {\
|
2016-12-03 15:51:09 +00:00
|
|
|
registers_.interrupt_flags |= InterruptFlag::Timer1;\
|
2016-10-28 01:06:31 +00:00
|
|
|
reevaluate_interrupts();\
|
|
|
|
\
|
2016-12-03 15:51:09 +00:00
|
|
|
if(registers_.auxiliary_control&0x40)\
|
|
|
|
registers_.timer_needs_reload = true;\
|
2016-10-28 01:06:31 +00:00
|
|
|
else\
|
2016-12-03 15:51:09 +00:00
|
|
|
timer_is_running_[0] = false;\
|
2016-10-28 01:06:31 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 02:29:09 +00:00
|
|
|
/*! Runs for a specified number of half cycles. */
|
|
|
|
inline void run_for(const HalfCycles &half_cycles) {
|
|
|
|
int number_of_half_cycles = half_cycles.as_int();
|
2016-10-28 01:13:25 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(is_phase2_) {
|
2016-10-28 01:06:31 +00:00
|
|
|
phase2();
|
2017-07-25 02:29:09 +00:00
|
|
|
number_of_half_cycles--;
|
2016-10-28 01:06:31 +00:00
|
|
|
}
|
2016-06-18 17:57:10 +00:00
|
|
|
|
2017-07-25 02:29:09 +00:00
|
|
|
while(number_of_half_cycles >= 2) {
|
2016-10-28 01:06:31 +00:00
|
|
|
phase1();
|
|
|
|
phase2();
|
2017-07-25 02:29:09 +00:00
|
|
|
number_of_half_cycles -= 2;
|
2016-10-28 01:06:31 +00:00
|
|
|
}
|
2016-06-18 17:57:10 +00:00
|
|
|
|
2017-07-25 02:29:09 +00:00
|
|
|
if(number_of_half_cycles) {
|
2016-10-28 01:06:31 +00:00
|
|
|
phase1();
|
2016-12-03 15:51:09 +00:00
|
|
|
is_phase2_ = true;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-03 15:51:09 +00:00
|
|
|
is_phase2_ = false;
|
2016-06-10 02:37:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 02:29:09 +00:00
|
|
|
/*! Runs for a specified number of cycles. */
|
|
|
|
inline void run_for(const Cycles &cycles) {
|
|
|
|
int number_of_cycles = cycles.as_int();
|
2017-03-26 18:34:47 +00:00
|
|
|
while(number_of_cycles--) {
|
2016-10-28 01:13:25 +00:00
|
|
|
phase1();
|
|
|
|
phase2();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef phase1
|
|
|
|
#undef phase2
|
|
|
|
|
2016-06-19 22:11:37 +00:00
|
|
|
/*! @returns @c true if the IRQ line is currently active; @c false otherwise. */
|
2017-03-26 18:34:47 +00:00
|
|
|
inline bool get_interrupt_line() {
|
2016-12-03 15:51:09 +00:00
|
|
|
uint8_t interrupt_status = registers_.interrupt_flags & registers_.interrupt_enable & 0x7f;
|
2016-06-10 02:37:59 +00:00
|
|
|
return !!interrupt_status;
|
2016-06-09 02:15:24 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 11:49:07 +00:00
|
|
|
MOS6522() :
|
2016-12-03 15:51:09 +00:00
|
|
|
timer_is_running_{false, false},
|
|
|
|
last_posted_interrupt_status_(false),
|
2017-03-26 18:34:47 +00:00
|
|
|
is_phase2_(false) {}
|
2016-06-11 11:49:07 +00:00
|
|
|
|
2016-06-07 23:15:18 +00:00
|
|
|
private:
|
2016-06-18 21:17:03 +00:00
|
|
|
// Expected to be overridden
|
2016-06-26 16:30:01 +00:00
|
|
|
uint8_t get_port_input(Port port) { return 0xff; }
|
|
|
|
void set_port_output(Port port, uint8_t value, uint8_t direction_mask) {}
|
2016-07-01 23:01:22 +00:00
|
|
|
void set_control_line_output(Port port, Line line, bool value) {}
|
2016-07-06 01:15:29 +00:00
|
|
|
void set_interrupt_status(bool status) {}
|
2016-06-18 17:57:10 +00:00
|
|
|
|
2016-06-18 21:17:03 +00:00
|
|
|
// Input/output multiplexer
|
2017-03-26 18:34:47 +00:00
|
|
|
uint8_t get_port_input(Port port, uint8_t output_mask, uint8_t output) {
|
2016-06-18 21:17:03 +00:00
|
|
|
uint8_t input = static_cast<T *>(this)->get_port_input(port);
|
|
|
|
return (input & ~output_mask) | (output & output_mask);
|
|
|
|
}
|
|
|
|
|
2016-06-18 17:57:10 +00:00
|
|
|
// Phase toggle
|
2016-12-03 15:51:09 +00:00
|
|
|
bool is_phase2_;
|
2016-06-11 15:34:39 +00:00
|
|
|
|
|
|
|
// Delegate and communications
|
2016-12-03 15:51:09 +00:00
|
|
|
bool last_posted_interrupt_status_;
|
2017-03-26 18:34:47 +00:00
|
|
|
inline void reevaluate_interrupts() {
|
2016-06-11 11:49:07 +00:00
|
|
|
bool new_interrupt_status = get_interrupt_line();
|
2017-03-26 18:34:47 +00:00
|
|
|
if(new_interrupt_status != last_posted_interrupt_status_) {
|
2016-12-03 15:51:09 +00:00
|
|
|
last_posted_interrupt_status_ = new_interrupt_status;
|
2016-06-18 12:51:18 +00:00
|
|
|
static_cast<T *>(this)->set_interrupt_status(new_interrupt_status);
|
2016-06-11 11:49:07 +00:00
|
|
|
}
|
2016-06-10 02:37:59 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 15:34:39 +00:00
|
|
|
// The registers
|
2016-06-11 11:12:55 +00:00
|
|
|
struct Registers {
|
|
|
|
uint8_t output[2], input[2], data_direction[2];
|
2016-06-18 18:30:23 +00:00
|
|
|
uint16_t timer[2], timer_latch[2], last_timer[2];
|
2016-11-05 01:30:18 +00:00
|
|
|
int next_timer[2];
|
2016-06-11 11:12:55 +00:00
|
|
|
uint8_t shift;
|
|
|
|
uint8_t auxiliary_control, peripheral_control;
|
|
|
|
uint8_t interrupt_flags, interrupt_enable;
|
2016-06-18 18:30:23 +00:00
|
|
|
bool timer_needs_reload;
|
2016-06-09 02:15:24 +00:00
|
|
|
|
2016-06-11 15:34:39 +00:00
|
|
|
// "A low reset (RES) input clears all R6522 internal registers to logic 0"
|
2016-06-11 11:57:04 +00:00
|
|
|
Registers() :
|
2016-06-11 15:34:39 +00:00
|
|
|
output{0, 0}, input{0, 0}, data_direction{0, 0},
|
|
|
|
auxiliary_control(0), peripheral_control(0),
|
2016-06-18 18:30:23 +00:00
|
|
|
interrupt_flags(0), interrupt_enable(0),
|
2016-11-05 01:30:18 +00:00
|
|
|
last_timer{0, 0}, timer_needs_reload(false),
|
|
|
|
next_timer{-1, -1} {}
|
2016-12-03 15:51:09 +00:00
|
|
|
} registers_;
|
2016-06-11 11:12:55 +00:00
|
|
|
|
2016-06-26 20:32:27 +00:00
|
|
|
// control state
|
|
|
|
struct {
|
|
|
|
bool line_one, line_two;
|
2016-12-03 15:51:09 +00:00
|
|
|
} control_inputs_[2];
|
2016-06-26 20:32:27 +00:00
|
|
|
|
2016-06-11 15:34:39 +00:00
|
|
|
// Internal state other than the registers
|
2016-12-03 15:51:09 +00:00
|
|
|
bool timer_is_running_[2];
|
2016-06-07 01:56:02 +00:00
|
|
|
};
|
|
|
|
|
2016-06-19 22:11:37 +00:00
|
|
|
/*!
|
|
|
|
Provided for optional composition with @c MOS6522, @c MOS6522IRQDelegate provides for a delegate
|
|
|
|
that will receive IRQ line change notifications.
|
|
|
|
*/
|
2016-06-18 12:51:18 +00:00
|
|
|
class MOS6522IRQDelegate {
|
|
|
|
public:
|
|
|
|
class Delegate {
|
|
|
|
public:
|
|
|
|
virtual void mos6522_did_change_interrupt_status(void *mos6522) = 0;
|
|
|
|
};
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline void set_interrupt_delegate(Delegate *delegate) {
|
2016-12-03 15:51:09 +00:00
|
|
|
delegate_ = delegate;
|
2016-06-18 12:51:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline void set_interrupt_status(bool new_status) {
|
2016-12-03 15:51:09 +00:00
|
|
|
if(delegate_) delegate_->mos6522_did_change_interrupt_status(this);
|
2016-06-18 12:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-12-03 15:51:09 +00:00
|
|
|
Delegate *delegate_;
|
2016-06-18 12:51:18 +00:00
|
|
|
};
|
|
|
|
|
2016-06-07 01:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _522_hpp */
|