2016-06-19 22:11:37 +00:00
|
|
|
//
|
|
|
|
// 6532.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 19/06/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-06-19 22:11:37 +00:00
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2016-06-19 22:11:37 +00:00
|
|
|
|
2016-06-19 22:57:40 +00:00
|
|
|
#include <cstdint>
|
2016-06-20 22:57:35 +00:00
|
|
|
#include <cstdio>
|
2017-07-26 00:20:55 +00:00
|
|
|
|
2017-07-27 12:05:14 +00:00
|
|
|
#include "../../ClockReceiver/ClockReceiver.hpp"
|
|
|
|
|
2016-06-19 22:57:40 +00:00
|
|
|
namespace MOS {
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Implements a template for emulation of the MOS 6532 RAM-I/O-Timer ('RIOT').
|
|
|
|
|
|
|
|
The RIOT provides:
|
|
|
|
* 128 bytes of static RAM;
|
|
|
|
* an interval timer; and
|
|
|
|
* two digital input/output ports.
|
|
|
|
|
|
|
|
Consumers should derive their own curiously-recurring-template-pattern subclass,
|
|
|
|
implementing bus communications as required.
|
|
|
|
*/
|
2017-07-27 11:40:02 +00:00
|
|
|
template <class T> class MOS6532 {
|
2016-06-19 22:57:40 +00:00
|
|
|
public:
|
2016-12-03 15:51:09 +00:00
|
|
|
inline void set_ram(uint16_t address, uint8_t value) { ram_[address&0x7f] = value; }
|
|
|
|
inline uint8_t get_ram(uint16_t address) { return ram_[address & 0x7f]; }
|
2016-06-19 22:57:40 +00:00
|
|
|
|
2020-01-05 18:40:02 +00:00
|
|
|
inline void write(int address, uint8_t value) {
|
2016-06-20 11:27:38 +00:00
|
|
|
const uint8_t decodedAddress = address & 0x07;
|
2016-06-19 22:57:40 +00:00
|
|
|
switch(decodedAddress) {
|
2016-06-20 22:57:35 +00:00
|
|
|
// Port output
|
|
|
|
case 0x00: case 0x02:
|
2016-12-03 15:51:09 +00:00
|
|
|
port_[decodedAddress / 2].output = value;
|
|
|
|
static_cast<T *>(this)->set_port_output(decodedAddress / 2, port_[decodedAddress/2].output, port_[decodedAddress / 2].output_mask);
|
2016-06-21 01:02:42 +00:00
|
|
|
set_port_did_change(decodedAddress / 2);
|
2016-06-19 22:57:40 +00:00
|
|
|
break;
|
2016-06-20 22:57:35 +00:00
|
|
|
case 0x01: case 0x03:
|
2016-12-03 15:51:09 +00:00
|
|
|
port_[decodedAddress / 2].output_mask = value;
|
|
|
|
static_cast<T *>(this)->set_port_output(decodedAddress / 2, port_[decodedAddress/2].output, port_[decodedAddress / 2].output_mask);
|
2016-06-21 01:02:42 +00:00
|
|
|
set_port_did_change(decodedAddress / 2);
|
2016-06-19 22:57:40 +00:00
|
|
|
break;
|
|
|
|
|
2016-06-20 22:57:35 +00:00
|
|
|
// The timer and edge detect control
|
|
|
|
case 0x04: case 0x05: case 0x06: case 0x07:
|
2017-03-26 18:34:47 +00:00
|
|
|
if(address & 0x10) {
|
2016-12-03 15:51:09 +00:00
|
|
|
timer_.writtenShift = timer_.activeShift = (decodedAddress - 0x04) * 3 + (decodedAddress / 0x07); // i.e. 0, 3, 6, 10
|
2020-05-10 03:00:39 +00:00
|
|
|
timer_.value = (unsigned(value) << timer_.activeShift) ;
|
2016-12-03 15:51:09 +00:00
|
|
|
timer_.interrupt_enabled = !!(address&0x08);
|
|
|
|
interrupt_status_ &= ~InterruptFlag::Timer;
|
2016-06-20 22:57:35 +00:00
|
|
|
evaluate_interrupts();
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-03 15:51:09 +00:00
|
|
|
a7_interrupt_.enabled = !!(address&0x2);
|
|
|
|
a7_interrupt_.active_on_positive = !!(address & 0x01);
|
2016-06-20 22:57:35 +00:00
|
|
|
}
|
2016-06-19 22:57:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 18:40:02 +00:00
|
|
|
inline uint8_t read(int address) {
|
2016-06-20 11:27:38 +00:00
|
|
|
const uint8_t decodedAddress = address & 0x7;
|
2016-06-19 22:57:40 +00:00
|
|
|
switch(decodedAddress) {
|
2016-06-20 22:57:35 +00:00
|
|
|
// Port input
|
2017-03-26 18:34:47 +00:00
|
|
|
case 0x00: case 0x02: {
|
2016-06-19 22:57:40 +00:00
|
|
|
const int port = decodedAddress / 2;
|
|
|
|
uint8_t input = static_cast<T *>(this)->get_port_input(port);
|
2016-12-03 15:51:09 +00:00
|
|
|
return (input & ~port_[port].output_mask) | (port_[port].output & port_[port].output_mask);
|
2016-06-19 22:57:40 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-06-20 22:57:35 +00:00
|
|
|
case 0x01: case 0x03:
|
2016-12-03 15:51:09 +00:00
|
|
|
return port_[decodedAddress / 2].output_mask;
|
2016-06-19 22:57:40 +00:00
|
|
|
break;
|
2016-06-20 22:57:35 +00:00
|
|
|
|
|
|
|
// Timer and interrupt control
|
2017-03-26 18:34:47 +00:00
|
|
|
case 0x04: case 0x06: {
|
2020-05-10 03:00:39 +00:00
|
|
|
uint8_t value = uint8_t(timer_.value >> timer_.activeShift);
|
2016-12-03 15:51:09 +00:00
|
|
|
timer_.interrupt_enabled = !!(address&0x08);
|
|
|
|
interrupt_status_ &= ~InterruptFlag::Timer;
|
2016-06-20 22:57:35 +00:00
|
|
|
evaluate_interrupts();
|
2016-06-19 22:57:40 +00:00
|
|
|
|
2016-12-03 15:51:09 +00:00
|
|
|
if(timer_.activeShift != timer_.writtenShift) {
|
|
|
|
unsigned int shift = timer_.writtenShift - timer_.activeShift;
|
|
|
|
timer_.value = (timer_.value << shift) | ((1 << shift) - 1);
|
|
|
|
timer_.activeShift = timer_.writtenShift;
|
2016-06-19 22:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
break;
|
2016-06-20 22:57:35 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
case 0x05: case 0x07: {
|
2016-12-03 15:51:09 +00:00
|
|
|
uint8_t value = interrupt_status_;
|
|
|
|
interrupt_status_ &= ~InterruptFlag::PA7;
|
2016-06-20 22:57:35 +00:00
|
|
|
evaluate_interrupts();
|
2016-06-19 22:57:40 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
inline void run_for(const Cycles cycles) {
|
2020-05-10 03:00:39 +00:00
|
|
|
unsigned int number_of_cycles = unsigned(cycles.as_integral());
|
2017-07-25 01:57:24 +00:00
|
|
|
|
2016-06-20 22:57:35 +00:00
|
|
|
// permit counting _to_ zero; counting _through_ zero initiates the other behaviour
|
2016-12-03 15:51:09 +00:00
|
|
|
if(timer_.value >= number_of_cycles) {
|
|
|
|
timer_.value -= number_of_cycles;
|
2016-06-19 22:57:40 +00:00
|
|
|
} else {
|
2016-12-03 15:51:09 +00:00
|
|
|
number_of_cycles -= timer_.value;
|
2017-03-04 22:00:28 +00:00
|
|
|
timer_.value = (0x100 - number_of_cycles) & 0xff;
|
2016-12-03 15:51:09 +00:00
|
|
|
timer_.activeShift = 0;
|
|
|
|
interrupt_status_ |= InterruptFlag::Timer;
|
2016-06-20 22:57:35 +00:00
|
|
|
evaluate_interrupts();
|
2016-06-19 22:57:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-11 00:14:19 +00:00
|
|
|
MOS6532() {
|
2020-05-10 03:00:39 +00:00
|
|
|
timer_.value = unsigned((rand() & 0xff) << 10);
|
2017-11-11 00:14:19 +00:00
|
|
|
}
|
2016-06-19 22:57:40 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline void set_port_did_change(int port) {
|
|
|
|
if(!port) {
|
2016-12-03 15:51:09 +00:00
|
|
|
uint8_t new_port_a_value = (get_port_input(0) & ~port_[0].output_mask) | (port_[0].output & port_[0].output_mask);
|
|
|
|
uint8_t difference = new_port_a_value ^ a7_interrupt_.last_port_value;
|
|
|
|
a7_interrupt_.last_port_value = new_port_a_value;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(difference&0x80) {
|
2016-06-20 22:57:35 +00:00
|
|
|
if(
|
2016-12-03 15:51:09 +00:00
|
|
|
((new_port_a_value&0x80) && a7_interrupt_.active_on_positive) ||
|
|
|
|
(!(new_port_a_value&0x80) && !a7_interrupt_.active_on_positive)
|
2017-03-26 18:34:47 +00:00
|
|
|
) {
|
2016-12-03 15:51:09 +00:00
|
|
|
interrupt_status_ |= InterruptFlag::PA7;
|
2016-06-20 22:57:35 +00:00
|
|
|
evaluate_interrupts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 01:22:51 +00:00
|
|
|
inline bool get_inerrupt_line() const {
|
2016-12-03 15:51:09 +00:00
|
|
|
return interrupt_line_;
|
2016-06-21 01:02:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 22:57:40 +00:00
|
|
|
private:
|
2016-12-03 15:51:09 +00:00
|
|
|
uint8_t ram_[128];
|
2016-06-19 22:57:40 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
unsigned int value;
|
2017-11-11 00:14:19 +00:00
|
|
|
unsigned int activeShift = 10, writtenShift = 10;
|
|
|
|
bool interrupt_enabled = false;
|
2016-12-03 15:51:09 +00:00
|
|
|
} timer_;
|
2016-06-19 22:57:40 +00:00
|
|
|
|
|
|
|
struct {
|
2017-11-11 00:14:19 +00:00
|
|
|
bool enabled = false;
|
|
|
|
bool active_on_positive = false;
|
|
|
|
uint8_t last_port_value = 0;
|
2016-12-03 15:51:09 +00:00
|
|
|
} a7_interrupt_;
|
2016-06-20 22:57:35 +00:00
|
|
|
|
|
|
|
struct {
|
2017-11-11 00:14:19 +00:00
|
|
|
uint8_t output_mask = 0, output = 0;
|
2016-12-03 15:51:09 +00:00
|
|
|
} port_[2];
|
2016-06-19 22:57:40 +00:00
|
|
|
|
2017-11-11 00:14:19 +00:00
|
|
|
uint8_t interrupt_status_ = 0;
|
2016-06-20 22:57:35 +00:00
|
|
|
enum InterruptFlag: uint8_t {
|
|
|
|
Timer = 0x80,
|
|
|
|
PA7 = 0x40
|
|
|
|
};
|
2017-11-11 00:14:19 +00:00
|
|
|
bool interrupt_line_ = false;
|
2016-06-20 22:57:35 +00:00
|
|
|
|
2016-06-19 22:57:40 +00:00
|
|
|
// expected to be overridden
|
2020-05-30 04:37:06 +00:00
|
|
|
void set_port_output([[maybe_unused]] int port, [[maybe_unused]] uint8_t value, [[maybe_unused]] uint8_t output_mask) {}
|
|
|
|
uint8_t get_port_input([[maybe_unused]] int port) {
|
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
void set_irq_line(bool) {}
|
2016-06-20 22:57:35 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline void evaluate_interrupts() {
|
2016-12-03 15:51:09 +00:00
|
|
|
interrupt_line_ =
|
|
|
|
((interrupt_status_&InterruptFlag::Timer) && timer_.interrupt_enabled) ||
|
|
|
|
((interrupt_status_&InterruptFlag::PA7) && a7_interrupt_.enabled);
|
|
|
|
set_irq_line(interrupt_line_);
|
2016-06-20 22:57:35 +00:00
|
|
|
}
|
2016-06-19 22:57:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|