2016-06-19 22:11:37 +00:00
|
|
|
//
|
|
|
|
// 6532.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 19/06/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef _532_hpp
|
|
|
|
#define _532_hpp
|
|
|
|
|
2016-06-19 22:57:40 +00:00
|
|
|
#include <cstdint>
|
2016-06-20 22:57:35 +00:00
|
|
|
#include <cstdio>
|
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.
|
|
|
|
*/
|
|
|
|
template <class T> class MOS6532 {
|
|
|
|
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
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline void set_register(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
|
2017-03-04 22:00:28 +00:00
|
|
|
timer_.value = ((unsigned int)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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline uint8_t get_register(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: {
|
2016-12-03 15:51:09 +00:00
|
|
|
uint8_t value = (uint8_t)(timer_.value >> timer_.activeShift);
|
|
|
|
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-03-26 18:34:47 +00:00
|
|
|
inline void run_for_cycles(unsigned int number_of_cycles) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOS6532() :
|
2016-12-03 15:51:09 +00:00
|
|
|
interrupt_status_(0),
|
|
|
|
port_{{.output_mask = 0, .output = 0}, {.output_mask = 0, .output = 0}},
|
|
|
|
a7_interrupt_({.last_port_value = 0, .enabled = false}),
|
2017-03-18 21:34:34 +00:00
|
|
|
interrupt_line_(false),
|
2017-03-26 18:34:47 +00:00
|
|
|
timer_{.value = (unsigned int)((rand() & 0xff) << 10), .activeShift = 10, .writtenShift = 10, .interrupt_enabled = false} {}
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline bool get_inerrupt_line() {
|
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;
|
|
|
|
unsigned int activeShift, writtenShift;
|
2016-06-20 22:57:35 +00:00
|
|
|
bool interrupt_enabled;
|
2016-12-03 15:51:09 +00:00
|
|
|
} timer_;
|
2016-06-19 22:57:40 +00:00
|
|
|
|
|
|
|
struct {
|
2016-06-20 22:57:35 +00:00
|
|
|
bool enabled;
|
|
|
|
bool active_on_positive;
|
|
|
|
uint8_t last_port_value;
|
2016-12-03 15:51:09 +00:00
|
|
|
} a7_interrupt_;
|
2016-06-20 22:57:35 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
uint8_t output_mask, output;
|
2016-12-03 15:51:09 +00:00
|
|
|
} port_[2];
|
2016-06-19 22:57:40 +00:00
|
|
|
|
2016-12-03 15:51:09 +00:00
|
|
|
uint8_t interrupt_status_;
|
2016-06-20 22:57:35 +00:00
|
|
|
enum InterruptFlag: uint8_t {
|
|
|
|
Timer = 0x80,
|
|
|
|
PA7 = 0x40
|
|
|
|
};
|
2016-12-03 15:51:09 +00:00
|
|
|
bool interrupt_line_;
|
2016-06-20 22:57:35 +00:00
|
|
|
|
2016-06-19 22:57:40 +00:00
|
|
|
// expected to be overridden
|
|
|
|
uint8_t get_port_input(int port) { return 0xff; }
|
2016-06-20 22:57:35 +00:00
|
|
|
void set_port_output(int port, uint8_t value, uint8_t output_mask) {}
|
|
|
|
void set_irq_line(bool new_value) {}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2016-06-19 22:11:37 +00:00
|
|
|
|
|
|
|
#endif /* _532_hpp */
|