1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-05 06:05:27 +00:00
CLK/Machines/Vic-20/Vic20.hpp

123 lines
3.3 KiB
C++
Raw Normal View History

//
// Vic20.hpp
// Clock Signal
//
// Created by Thomas Harte on 04/06/2016.
// Copyright © 2016 Thomas Harte. All rights reserved.
//
#ifndef Vic20_hpp
#define Vic20_hpp
#include "../../Processors/6502/CPU6502.hpp"
#include "../../Components/6560/6560.hpp"
#include "../../Components/6522/6522.hpp"
#include "../CRTMachine.hpp"
namespace Vic20 {
enum ROMSlot {
ROMSlotKernel,
ROMSlotBASIC,
ROMSlotCharacters,
};
#define key(line, mask) (((mask) << 3) | (line))
enum Key: uint16_t {
Key2 = key(7, 0x80), Key4 = key(7, 0x40)
};
class UserPortVIA: public MOS::MOS6522<UserPortVIA> {
};
class KeyboardVIA: public MOS::MOS6522<KeyboardVIA> {
public:
void set_key_state(Key key, bool isPressed) {
if(isPressed)
_columns[key & 0x07] &= ~(key >> 3);
else
_columns[key & 0x07] |= (key >> 3);
}
// to satisfy MOS::MOS6522
uint8_t get_port_input(int port) {
if(!port) {
uint8_t result = 0xff;
for(int c = 0; c < 8; c++)
{
if(!(_activation_mask&(1 << c))) result &= _columns[c];
}
return result;
}
return 0xff;
}
void set_port_output(int port, uint8_t value) {
if(port) _activation_mask = value;
}
KeyboardVIA() : _columns{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} {}
private:
uint8_t _columns[8];
uint8_t _activation_mask;
};
class Machine: public CPU6502::Processor<Machine>, public CRTMachine::Machine, public MOS::MOS6522Delegate {
public:
Machine();
void set_rom(ROMSlot slot, size_t length, const uint8_t *data);
void add_prg(size_t length, const uint8_t *data);
void set_key_state(Key key, bool isPressed) { _keyboardVIA->set_key_state(key, isPressed); }
// to satisfy CPU6502::Processor
unsigned int perform_bus_operation(CPU6502::BusOperation operation, uint16_t address, uint8_t *value);
void synchronise() {}
// to satisfy CRTMachine::Machine
virtual void setup_output(float aspect_ratio);
virtual void close_output() {}
virtual Outputs::CRT::CRT *get_crt() { return _mos6560->get_crt(); }
virtual Outputs::Speaker *get_speaker() { return nullptr; } // TODO
virtual void run_for_cycles(int number_of_cycles) { CPU6502::Processor<Machine>::run_for_cycles(number_of_cycles); }
// to satisfy MOS::MOS6522::Delegate
virtual void mos6522_did_change_interrupt_status(void *mos6522);
private:
uint8_t _characterROM[0x1000];
uint8_t _basicROM[0x2000];
uint8_t _kernelROM[0x2000];
uint8_t _userBASICMemory[0x0400];
uint8_t _screenMemory[0x1000];
uint8_t _colorMemory[0x0400];
inline uint8_t *ram_pointer(uint16_t address) {
if(address < sizeof(_userBASICMemory)) return &_userBASICMemory[address];
if(address >= 0x1000 && address < 0x2000) return &_screenMemory[address&0x0fff];
if(address >= 0x9400 && address < 0x9800) return &_colorMemory[0x03ff]; // TODO: make this 4-bit
return nullptr;
}
inline uint8_t read_memory(uint16_t address) {
uint8_t *ram = ram_pointer(address);
if(ram) return *ram;
else if(address >= 0x8000 && address < 0x9000) return _characterROM[address&0x0fff];
else if(address >= 0xc000 && address < 0xe000) return _basicROM[address&0x1fff];
else if(address >= 0xe000) return _kernelROM[address&0x1fff];
return 0xff;
}
std::unique_ptr<MOS::MOS6560> _mos6560;
std::unique_ptr<UserPortVIA> _userPortVIA;
std::unique_ptr<KeyboardVIA> _keyboardVIA;
};
}
#endif /* Vic20_hpp */