mirror of
https://github.com/TomHarte/CLK.git
synced 2026-04-19 19:16:34 +00:00
Factored out the 6532, eventually to make it testable.
This commit is contained in:
@@ -15,15 +15,15 @@
|
||||
namespace MOS {
|
||||
|
||||
/*!
|
||||
Implements the template for an emulation of the MOS 6522 Versatile Interface Adaptor ('VIA').
|
||||
Implements a template for emulation of the MOS 6522 Versatile Interface Adaptor ('VIA').
|
||||
|
||||
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.
|
||||
|
||||
Consumers should derive their own curiously-recurring-template-pattern subclass of MOS6522,
|
||||
implementing bus communications as required for the specific machine.
|
||||
Consumers should derive their own curiously-recurring-template-pattern subclass,
|
||||
implementing bus communications as required.
|
||||
*/
|
||||
template <class T> class MOS6522 {
|
||||
private:
|
||||
@@ -39,7 +39,7 @@ template <class T> class MOS6522 {
|
||||
|
||||
public:
|
||||
/*! Sets a register value. */
|
||||
void set_register(int address, uint8_t value)
|
||||
inline void set_register(int address, uint8_t value)
|
||||
{
|
||||
address &= 0xf;
|
||||
// printf("6522 %p: %d <- %02x\n", this, address, value);
|
||||
@@ -111,7 +111,7 @@ template <class T> class MOS6522 {
|
||||
}
|
||||
|
||||
/*! Gets a register value. */
|
||||
uint8_t get_register(int address)
|
||||
inline uint8_t get_register(int address)
|
||||
{
|
||||
address &= 0xf;
|
||||
// printf("6522 %p: %d\n", this, address);
|
||||
@@ -152,7 +152,7 @@ template <class T> class MOS6522 {
|
||||
return 0xff;
|
||||
}
|
||||
|
||||
void set_control_line_input(int port, int line, bool value)
|
||||
inline void set_control_line_input(int port, int line, bool value)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -166,7 +166,7 @@ template <class T> class MOS6522 {
|
||||
next rising edge. So it should align with a full system's phase-1. The next emulated half-cycle will be
|
||||
that which occurs during phase-2.
|
||||
*/
|
||||
void run_for_half_cycles(unsigned int number_of_cycles)
|
||||
inline void run_for_half_cycles(unsigned int number_of_cycles)
|
||||
{
|
||||
while(number_of_cycles--)
|
||||
{
|
||||
@@ -212,7 +212,7 @@ template <class T> class MOS6522 {
|
||||
}
|
||||
|
||||
/*! @returns @c true if the IRQ line is currently active; @c false otherwise. */
|
||||
bool get_interrupt_line()
|
||||
inline bool get_interrupt_line()
|
||||
{
|
||||
uint8_t interrupt_status = _registers.interrupt_flags & _registers.interrupt_enable & 0x7f;
|
||||
return !!interrupt_status;
|
||||
|
||||
+123
-1
@@ -9,6 +9,128 @@
|
||||
#ifndef _532_hpp
|
||||
#define _532_hpp
|
||||
|
||||
#include <stdio.h>
|
||||
#include <cstdint>
|
||||
|
||||
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:
|
||||
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]; }
|
||||
|
||||
inline void set_register(int address, uint8_t value)
|
||||
{
|
||||
const uint8_t decodedAddress = address & 0x0f;
|
||||
switch(decodedAddress) {
|
||||
case 0x00:
|
||||
case 0x02:
|
||||
static_cast<T *>(this)->set_port_output(decodedAddress / 2, value, _port[decodedAddress / 2].direction);
|
||||
_port[decodedAddress/2].output = value;
|
||||
break;
|
||||
|
||||
case 0x01:
|
||||
case 0x03:
|
||||
_port[decodedAddress / 2].direction = value;
|
||||
break;
|
||||
|
||||
case 0x04:
|
||||
case 0x05:
|
||||
case 0x06:
|
||||
case 0x07:
|
||||
_timer.writtenShift = _timer.activeShift = (decodedAddress - 0x04) * 3 + (decodedAddress / 0x07); // i.e. 0, 3, 6, 10
|
||||
_timer.value = ((unsigned int)(value) << _timer.activeShift) | ((1 << _timer.activeShift)-1);
|
||||
_timer.status &= ~0x40;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
inline uint8_t get_register(int address)
|
||||
{
|
||||
const uint8_t decodedAddress = address & 0xf;
|
||||
switch(decodedAddress) {
|
||||
case 0x00:
|
||||
case 0x02:
|
||||
{
|
||||
const int port = decodedAddress / 2;
|
||||
uint8_t input = static_cast<T *>(this)->get_port_input(port);
|
||||
return (input & ~_port[port].direction) | (_port[port].output & _port[port].direction);
|
||||
}
|
||||
break;
|
||||
case 0x01:
|
||||
case 0x03:
|
||||
return _port[decodedAddress / 2].direction;
|
||||
break;
|
||||
case 0x04:
|
||||
case 0x06:
|
||||
{
|
||||
uint8_t value = (uint8_t)(_timer.value >> _timer.activeShift);
|
||||
|
||||
if(_timer.activeShift != _timer.writtenShift) {
|
||||
unsigned int shift = _timer.writtenShift - _timer.activeShift;
|
||||
_timer.value = (_timer.value << shift) | ((1 << shift) - 1);
|
||||
_timer.activeShift = _timer.writtenShift;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
break;
|
||||
case 0x05:
|
||||
case 0x07:
|
||||
{
|
||||
uint8_t value = _timer.status;
|
||||
_timer.status &= ~0x80;
|
||||
return value;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return 0xff;
|
||||
}
|
||||
|
||||
inline void run_for_cycles(unsigned int number_of_cycles)
|
||||
{
|
||||
if(_timer.value >= number_of_cycles) {
|
||||
_timer.value -= number_of_cycles;
|
||||
} else {
|
||||
_timer.value = 0x100 + ((_timer.value - (number_of_cycles / 3)) >> _timer.activeShift);
|
||||
_timer.activeShift = 0;
|
||||
_timer.status |= 0xc0;
|
||||
}
|
||||
}
|
||||
|
||||
MOS6532() :
|
||||
_timer({.status = 0})
|
||||
{}
|
||||
|
||||
private:
|
||||
uint8_t _ram[128];
|
||||
|
||||
struct {
|
||||
unsigned int value;
|
||||
unsigned int activeShift, writtenShift;
|
||||
uint8_t status;
|
||||
} _timer;
|
||||
|
||||
struct {
|
||||
uint8_t direction, output;
|
||||
} _port[2];
|
||||
|
||||
// expected to be overridden
|
||||
uint8_t get_port_input(int port) { return 0xff; }
|
||||
void set_port_output(int port, uint8_t value, uint8_t direction_mask) {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* _532_hpp */
|
||||
|
||||
Reference in New Issue
Block a user