2016-06-05 01:43:50 +00:00
|
|
|
//
|
|
|
|
// Vic20.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 04/06/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Vic20.hpp"
|
2016-06-05 02:00:50 +00:00
|
|
|
|
2016-06-05 14:51:07 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2016-06-05 02:00:50 +00:00
|
|
|
using namespace Vic20;
|
|
|
|
|
2016-06-09 02:15:24 +00:00
|
|
|
Machine::Machine() :
|
|
|
|
_userPortVIA(new UserPortVIA()),
|
2016-06-11 18:00:12 +00:00
|
|
|
_keyboardVIA(new KeyboardVIA()),
|
|
|
|
_rom(nullptr)
|
2016-06-05 15:44:29 +00:00
|
|
|
{
|
2016-06-10 02:37:59 +00:00
|
|
|
_userPortVIA->set_delegate(this);
|
|
|
|
_keyboardVIA->set_delegate(this);
|
2016-06-05 15:44:29 +00:00
|
|
|
set_reset_line(true);
|
|
|
|
}
|
2016-06-05 14:51:07 +00:00
|
|
|
|
2016-06-11 18:00:12 +00:00
|
|
|
Machine::~Machine()
|
|
|
|
{
|
|
|
|
delete[] _rom;
|
|
|
|
}
|
|
|
|
|
2016-06-05 02:00:50 +00:00
|
|
|
unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uint16_t address, uint8_t *value)
|
|
|
|
{
|
2016-06-05 15:44:29 +00:00
|
|
|
set_reset_line(false);
|
|
|
|
|
2016-06-05 16:11:12 +00:00
|
|
|
// run the phase-1 part of this cycle, in which the VIC accesses memory
|
2016-06-05 20:28:06 +00:00
|
|
|
uint16_t video_address = _mos6560->get_address();
|
2016-06-10 22:12:21 +00:00
|
|
|
uint8_t video_value = 0xff; // TODO
|
|
|
|
if(!(video_address&0x2000))
|
|
|
|
{
|
|
|
|
video_value = _characterROM[video_address & 0x0fff];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
video_address &= 0x1fff;
|
|
|
|
if(video_address < sizeof(_userBASICMemory)) video_value = _userBASICMemory[video_address];
|
|
|
|
else if(video_address >= 0x1000 && video_address < 0x2000) video_value = _screenMemory[video_address&0x0fff];
|
|
|
|
}
|
|
|
|
_mos6560->set_graphics_value(video_value, _colorMemory[video_address & 0x03ff]);
|
2016-06-05 16:11:12 +00:00
|
|
|
|
|
|
|
// run the phase-2 part of the cycle, which is whatever the 6502 said it should be
|
2016-06-05 15:44:29 +00:00
|
|
|
if(isReadOperation(operation))
|
|
|
|
{
|
2016-06-05 16:11:12 +00:00
|
|
|
*value = read_memory(address);
|
2016-06-07 00:29:39 +00:00
|
|
|
if((address&0xfff0) == 0x9000)
|
|
|
|
{
|
|
|
|
*value = _mos6560->get_register(address - 0x9000);
|
|
|
|
}
|
2016-06-07 23:15:18 +00:00
|
|
|
else if((address&0xfff0) == 0x9110)
|
|
|
|
{
|
|
|
|
*value = _userPortVIA->get_register(address - 0x9110);
|
|
|
|
}
|
|
|
|
else if((address&0xfff0) == 0x9120)
|
|
|
|
{
|
|
|
|
*value = _keyboardVIA->get_register(address - 0x9120);
|
|
|
|
}
|
2016-06-05 15:44:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-05 20:00:35 +00:00
|
|
|
uint8_t *ram = ram_pointer(address);
|
|
|
|
if(ram) *ram = *value;
|
2016-06-05 16:11:12 +00:00
|
|
|
else if((address&0xfff0) == 0x9000)
|
|
|
|
{
|
|
|
|
_mos6560->set_register(address - 0x9000, *value);
|
|
|
|
}
|
2016-06-07 23:15:18 +00:00
|
|
|
else if((address&0xfff0) == 0x9110)
|
|
|
|
{
|
|
|
|
_userPortVIA->set_register(address - 0x9110, *value);
|
|
|
|
}
|
|
|
|
else if((address&0xfff0) == 0x9120)
|
|
|
|
{
|
|
|
|
_keyboardVIA->set_register(address - 0x9120, *value);
|
|
|
|
}
|
2016-06-05 15:44:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-09 02:15:24 +00:00
|
|
|
_userPortVIA->run_for_cycles(1);
|
|
|
|
_keyboardVIA->run_for_cycles(1);
|
2016-06-05 02:00:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-06-05 13:06:59 +00:00
|
|
|
|
2016-06-10 02:37:59 +00:00
|
|
|
#pragma mark - 6522 delegate
|
|
|
|
|
|
|
|
void Machine::mos6522_did_change_interrupt_status(void *mos6522)
|
|
|
|
{
|
|
|
|
bool irq = _userPortVIA->get_interrupt_line() || _keyboardVIA->get_interrupt_line();
|
|
|
|
set_irq_line(irq);
|
|
|
|
}
|
|
|
|
|
2016-06-05 15:20:05 +00:00
|
|
|
#pragma mark - Setup
|
|
|
|
|
2016-06-05 14:51:07 +00:00
|
|
|
void Machine::setup_output(float aspect_ratio)
|
|
|
|
{
|
|
|
|
_mos6560 = std::unique_ptr<MOS::MOS6560>(new MOS::MOS6560());
|
|
|
|
}
|
|
|
|
|
2016-06-05 13:06:59 +00:00
|
|
|
void Machine::set_rom(ROMSlot slot, size_t length, const uint8_t *data)
|
|
|
|
{
|
2016-06-05 14:51:07 +00:00
|
|
|
uint8_t *target = nullptr;
|
2016-06-05 15:20:05 +00:00
|
|
|
size_t max_length = 0x2000;
|
2016-06-05 14:51:07 +00:00
|
|
|
switch(slot)
|
|
|
|
{
|
2016-06-05 15:20:05 +00:00
|
|
|
case ROMSlotKernel: target = _kernelROM; break;
|
|
|
|
case ROMSlotCharacters: target = _characterROM; max_length = 0x1000; break;
|
|
|
|
case ROMSlotBASIC: target = _basicROM; break;
|
2016-06-05 14:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(target)
|
|
|
|
{
|
2016-06-05 15:20:05 +00:00
|
|
|
size_t length_to_copy = std::min(max_length, length);
|
2016-06-05 14:51:07 +00:00
|
|
|
memcpy(target, data, length_to_copy);
|
|
|
|
}
|
2016-06-05 13:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Machine::add_prg(size_t length, const uint8_t *data)
|
|
|
|
{
|
2016-06-11 18:00:12 +00:00
|
|
|
if(length > 2)
|
|
|
|
{
|
|
|
|
_rom_address = (uint16_t)(data[0] | (data[1] << 8));
|
|
|
|
_rom_length = (uint16_t)(length - 2);
|
|
|
|
_rom = new uint8_t[length - 2];
|
|
|
|
memcpy(_rom, &data[2], length - 2);
|
|
|
|
}
|
2016-06-05 13:06:59 +00:00
|
|
|
}
|