1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-26 09:29:45 +00:00

Gave the 6522 a full and grouped register set.

This commit is contained in:
Thomas Harte 2016-06-11 07:12:55 -04:00
parent 12d1cac751
commit a71259dec3

View File

@ -38,66 +38,106 @@ template <class T> class MOS6522 {
}
MOS6522() :
_data_direction{0, 0},
_timer_is_running{false, false}
{}
void set_register(int address, uint8_t value)
{
printf("6522: %d <- %02x\n", address, value);
address &= 0xf;
switch(address)
{
case 0x00:
_registers.output[1] = value;
break;
case 0x01:
_registers.output[0] = value;
break;
case 0xf:
_registers.output[0] = value;
break;
case 0x02:
_registers.data_direction[1] = value;
break;
case 0x03:
_registers.data_direction[0] = value;
break;
// Timer 1
case 0x06: case 0x04: _interval_timer_latch[0] = (_interval_timer_latch[0]&0xff00) | value; break;
case 0x06: case 0x04: _registers.timer_latch[0] = (_registers.timer_latch[0]&0xff00) | value; break;
case 0x05: case 0x07:
_interval_timer_latch[0] = (_interval_timer_latch[0]&0x00ff) | (uint16_t)(value << 8);
_interrupt_flags &= ~InterruptFlag::Timer1;
_registers.timer_latch[0] = (_registers.timer_latch[0]&0x00ff) | (uint16_t)(value << 8);
_registers.interrupt_flags &= ~InterruptFlag::Timer1;
if(address == 0x05)
{
_interval_timer[0] = _interval_timer_latch[0];
_registers.timer[0] = _registers.timer_latch[0];
_timer_is_running[0] = true;
}
reevaluate_interrupts();
break;
// Timer 2
case 0x08: _interval_timer_latch[1] = value; break;
case 0x08: _registers.timer_latch[1] = value; break;
case 0x09:
_interrupt_flags &= ~InterruptFlag::Timer2;
_interval_timer[1] = _interval_timer_latch[1] | (uint16_t)(value << 8);
_registers.interrupt_flags &= ~InterruptFlag::Timer2;
_registers.timer[1] = _registers.timer_latch[1] | (uint16_t)(value << 8);
_timer_is_running[1] = true;
reevaluate_interrupts();
break;
case 11: _auxiliary_control_register = value; break;
case 13:
if(!(value&0x80)) value = ~value;
_interrupt_mask = value;
// Shift
case 0xa: _registers.shift = value; break;
// Control
case 0xb: _registers.auxiliary_control = value; break;
case 0xc: _registers.peripheral_control = value; break;
// Interrupt control
case 0xd:
_registers.interrupt_flags = value; // TODO: really?
reevaluate_interrupts();
break;
case 14:
case 0xe:
if(!(value&0x80)) value = ~value;
_registers.interrupt_enable = value;
reevaluate_interrupts();
break;
}
}
uint8_t get_register(int address)
{
printf("6522: %d\n", address);
address &= 0xf;
switch(address)
{
case 0x00: return _registers.input[1];
case 0x01: return _registers.input[0];
case 0x02: return _registers.data_direction[1];
case 0x03: return _registers.data_direction[0];
case 0x0f: return _registers.input[1];
// Timer 1
case 0x04:
_interrupt_flags &= ~InterruptFlag::Timer1;
_registers.interrupt_flags &= ~InterruptFlag::Timer1;
reevaluate_interrupts();
return _interval_timer[0] & 0x00ff;
case 0x05: return _interval_timer[0] >> 8;
case 0x06: return _interval_timer_latch[0] & 0x00ff;
case 0x07: return _interval_timer_latch[0] >> 8;
return _registers.timer[0] & 0x00ff;
case 0x05: return _registers.timer[0] >> 8;
case 0x06: return _registers.timer_latch[0] & 0x00ff;
case 0x07: return _registers.timer_latch[0] >> 8;
case 11: return _auxiliary_control_register;
case 13: return _interrupt_flags;
case 14: return _interrupt_mask;
// Timer 2
case 0x08:
_registers.interrupt_flags &= ~InterruptFlag::Timer2;
reevaluate_interrupts();
return _registers.timer[1] & 0x00ff;
case 0x09: return _registers.timer[1] >> 8;
case 0x0a: return _registers.shift;
case 0x0b: return _registers.auxiliary_control;
case 0x0c: return _registers.peripheral_control;
case 0x0d: return _registers.interrupt_flags;
case 0x0e: return _registers.interrupt_enable;
}
return 0xff;
@ -105,25 +145,25 @@ template <class T> class MOS6522 {
void run_for_cycles(unsigned int number_of_cycles)
{
_interval_timer[0] -= number_of_cycles;
_interval_timer[1] -= number_of_cycles;
_registers.timer[0] -= number_of_cycles;
_registers.timer[1] -= number_of_cycles;
if(!_interval_timer[1] && _timer_is_running[1])
if(!_registers.timer[1] && _timer_is_running[1])
{
_timer_is_running[1] = false;
_interrupt_flags |= InterruptFlag::Timer2;
_registers.interrupt_flags |= InterruptFlag::Timer2;
reevaluate_interrupts();
}
if(!_interval_timer[0] && _timer_is_running[0])
if(!_registers.timer[0] && _timer_is_running[0])
{
_interrupt_flags |= InterruptFlag::Timer1;
_registers.interrupt_flags |= InterruptFlag::Timer1;
reevaluate_interrupts();
// TODO: reload shouldn't occur for a further 1.5 cycles
if(_auxiliary_control_register&0x40)
if(_registers.auxiliary_control&0x40)
{
_interval_timer[0] = _interval_timer_latch[0];
_registers.timer[0] = _registers.timer_latch[0];
}
else
_timer_is_running[0] = false;
@ -133,7 +173,7 @@ template <class T> class MOS6522 {
bool get_interrupt_line()
{
uint8_t interrupt_status = _interrupt_flags & (~_interrupt_mask) & 0x7f;
uint8_t interrupt_status = _registers.interrupt_flags & (~_registers.interrupt_enable) & 0x7f;
return !!interrupt_status;
}
@ -144,16 +184,18 @@ template <class T> class MOS6522 {
}
MOS6522Delegate *_delegate;
uint16_t _interval_timer[2], _interval_timer_latch[2];
uint8_t _shift_register;
uint8_t _input_latch[2];
uint8_t _data_direction[2];
uint8_t _interrupt_flags, _interrupt_mask;
struct Registers {
uint8_t output[2], input[2], data_direction[2];
uint16_t timer[2], timer_latch[2];
uint8_t shift;
uint8_t auxiliary_control, peripheral_control;
uint8_t interrupt_flags, interrupt_enable;
Registers() : data_direction{0, 0} {}
} _registers;
bool _timer_is_running[2];
uint8_t _interrupt_flag_register, _interrupt_enable_register;
uint8_t _peripheral_control_register, _auxiliary_control_register;
};
}