2016-07-05 17:28:27 +00:00
|
|
|
|
//
|
|
|
|
|
// Commodore1540.cpp
|
|
|
|
|
// Clock Signal
|
|
|
|
|
//
|
|
|
|
|
// Created by Thomas Harte on 05/07/2016.
|
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2016-07-10 11:46:20 +00:00
|
|
|
|
#include "C1540.hpp"
|
2016-07-29 15:03:09 +00:00
|
|
|
|
#include <string>
|
|
|
|
|
#include "../../../Storage/Disk/Encodings/CommodoreGCR.hpp"
|
2016-07-05 20:39:18 +00:00
|
|
|
|
|
|
|
|
|
using namespace Commodore::C1540;
|
|
|
|
|
|
2016-07-29 15:03:09 +00:00
|
|
|
|
Machine::Machine() :
|
|
|
|
|
_shift_register(0),
|
2016-07-31 17:32:30 +00:00
|
|
|
|
Storage::DiskDrive(1000000, 16, 300)
|
2016-07-05 21:27:02 +00:00
|
|
|
|
{
|
2016-07-10 12:01:16 +00:00
|
|
|
|
// create a serial port and a VIA to run it
|
2016-07-05 21:27:02 +00:00
|
|
|
|
_serialPortVIA.reset(new SerialPortVIA);
|
|
|
|
|
_serialPort.reset(new SerialPort);
|
2016-07-05 23:12:43 +00:00
|
|
|
|
|
2016-07-10 12:01:16 +00:00
|
|
|
|
// attach the serial port to its VIA and vice versa
|
2016-07-05 21:27:02 +00:00
|
|
|
|
_serialPort->set_serial_port_via(_serialPortVIA);
|
2016-07-05 23:12:43 +00:00
|
|
|
|
_serialPortVIA->set_serial_port(_serialPort);
|
2016-07-10 12:01:16 +00:00
|
|
|
|
|
|
|
|
|
// set this instance as the delegate to receive interrupt requests from both VIAs
|
2016-07-06 02:22:09 +00:00
|
|
|
|
_serialPortVIA->set_delegate(this);
|
|
|
|
|
_driveVIA.set_delegate(this);
|
2016-07-29 15:03:09 +00:00
|
|
|
|
|
|
|
|
|
// set a bit rate
|
|
|
|
|
set_expected_bit_length(Storage::Encodings::CommodoreGCR::length_of_a_bit_in_time_zone(3));
|
2016-07-05 21:27:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Machine::set_serial_bus(std::shared_ptr<::Commodore::Serial::Bus> serial_bus)
|
|
|
|
|
{
|
2016-07-10 12:01:16 +00:00
|
|
|
|
Commodore::Serial::AttachPortAndBus(_serialPort, serial_bus);
|
2016-07-05 21:27:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-05 20:39:18 +00:00
|
|
|
|
unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uint16_t address, uint8_t *value)
|
|
|
|
|
{
|
2016-07-31 23:33:18 +00:00
|
|
|
|
// static bool log = false;
|
|
|
|
|
// if(operation == CPU6502::BusOperation::ReadOpcode) printf("%04x\n", address);
|
|
|
|
|
// if(operation == CPU6502::BusOperation::ReadOpcode && (address == 0xf3be)) log = true;
|
|
|
|
|
// if(operation == CPU6502::BusOperation::ReadOpcode && log) printf("%04x\n", address);
|
2016-07-10 02:29:11 +00:00
|
|
|
|
// if(operation == CPU6502::BusOperation::ReadOpcode && (address >= 0xF556 && address <= 0xF56D)) printf("%04x\n", address);
|
2016-07-09 22:06:49 +00:00
|
|
|
|
// if(operation == CPU6502::BusOperation::ReadOpcode && (address == 0xE887)) printf("A: %02x\n", get_value_of_register(CPU6502::Register::A));
|
|
|
|
|
|
2016-07-09 19:40:25 +00:00
|
|
|
|
/* static bool log = false;
|
2016-07-08 23:00:39 +00:00
|
|
|
|
|
|
|
|
|
if(operation == CPU6502::BusOperation::ReadOpcode)
|
|
|
|
|
{
|
|
|
|
|
log = (address >= 0xE85B && address <= 0xE907) || (address >= 0xE9C9 && address <= 0xEA2D);
|
|
|
|
|
if(log) printf("\n%04x: ", address);
|
|
|
|
|
}
|
2016-07-09 19:40:25 +00:00
|
|
|
|
if(log) printf("[%c %04x] ", isReadOperation(operation) ? 'r' : 'w', address);*/
|
2016-07-08 02:13:18 +00:00
|
|
|
|
|
2016-07-10 12:01:16 +00:00
|
|
|
|
/*
|
|
|
|
|
Memory map (given that I'm unsure yet on any potential mirroring):
|
|
|
|
|
|
|
|
|
|
0x0000–0x07ff RAM
|
|
|
|
|
0x1800–0x180f the serial-port VIA
|
|
|
|
|
0x1c00–0x1c0f the drive VIA
|
|
|
|
|
0xc000–0xffff ROM
|
|
|
|
|
*/
|
2016-07-05 20:39:18 +00:00
|
|
|
|
if(address < 0x800)
|
|
|
|
|
{
|
|
|
|
|
if(isReadOperation(operation))
|
|
|
|
|
*value = _ram[address];
|
|
|
|
|
else
|
|
|
|
|
_ram[address] = *value;
|
|
|
|
|
}
|
|
|
|
|
else if(address >= 0xc000)
|
|
|
|
|
{
|
|
|
|
|
if(isReadOperation(operation))
|
|
|
|
|
*value = _rom[address & 0x3fff];
|
|
|
|
|
}
|
2016-07-05 21:27:02 +00:00
|
|
|
|
else if(address >= 0x1800 && address <= 0x180f)
|
|
|
|
|
{
|
|
|
|
|
if(isReadOperation(operation))
|
|
|
|
|
*value = _serialPortVIA->get_register(address);
|
|
|
|
|
else
|
|
|
|
|
_serialPortVIA->set_register(address, *value);
|
|
|
|
|
}
|
2016-07-06 02:22:09 +00:00
|
|
|
|
else if(address >= 0x1c00 && address <= 0x1c0f)
|
|
|
|
|
{
|
|
|
|
|
if(isReadOperation(operation))
|
|
|
|
|
*value = _driveVIA.get_register(address);
|
|
|
|
|
else
|
|
|
|
|
_driveVIA.set_register(address, *value);
|
|
|
|
|
}
|
2016-07-05 21:27:02 +00:00
|
|
|
|
|
|
|
|
|
_serialPortVIA->run_for_half_cycles(2);
|
2016-07-06 02:22:09 +00:00
|
|
|
|
_driveVIA.run_for_half_cycles(2);
|
2016-07-05 20:39:18 +00:00
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-05 20:54:25 +00:00
|
|
|
|
void Machine::set_rom(const uint8_t *rom)
|
2016-07-05 20:39:18 +00:00
|
|
|
|
{
|
|
|
|
|
memcpy(_rom, rom, sizeof(_rom));
|
|
|
|
|
}
|
2016-07-05 23:12:43 +00:00
|
|
|
|
|
2016-07-29 15:03:09 +00:00
|
|
|
|
void Machine::run_for_cycles(int number_of_cycles)
|
2016-07-10 20:24:46 +00:00
|
|
|
|
{
|
2016-07-29 15:03:09 +00:00
|
|
|
|
CPU6502::Processor<Machine>::run_for_cycles(number_of_cycles);
|
2016-07-31 23:38:51 +00:00
|
|
|
|
if(_driveVIA.get_motor_enabled()) // TODO: motor speed up/down
|
|
|
|
|
Storage::DiskDrive::run_for_cycles(number_of_cycles);
|
2016-07-10 20:24:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-05 23:12:43 +00:00
|
|
|
|
#pragma mark - 6522 delegate
|
|
|
|
|
|
|
|
|
|
void Machine::mos6522_did_change_interrupt_status(void *mos6522)
|
|
|
|
|
{
|
2016-07-10 12:01:16 +00:00
|
|
|
|
// both VIAs are connected to the IRQ line
|
2016-07-07 00:22:46 +00:00
|
|
|
|
set_irq_line(_serialPortVIA->get_interrupt_line() || _driveVIA.get_interrupt_line());
|
2016-07-05 23:12:43 +00:00
|
|
|
|
}
|
2016-07-29 15:03:09 +00:00
|
|
|
|
|
|
|
|
|
#pragma mark - Disk drive
|
|
|
|
|
|
|
|
|
|
void Machine::process_input_bit(int value, unsigned int cycles_since_index_hole)
|
|
|
|
|
{
|
2016-07-31 17:32:30 +00:00
|
|
|
|
_shift_register = (_shift_register << 1) | value;
|
2016-07-31 22:29:44 +00:00
|
|
|
|
_driveVIA.set_sync_detected((_shift_register & 0x3ff) == 0x3ff);
|
|
|
|
|
if((_shift_register & 0x7ff) == 0x7fe) _bit_window_offset = 0;
|
|
|
|
|
_bit_window_offset++;
|
|
|
|
|
if(_bit_window_offset == 8)
|
|
|
|
|
{
|
|
|
|
|
_driveVIA.set_data_input((uint8_t)_shift_register);
|
|
|
|
|
_bit_window_offset = 0;
|
2016-07-31 23:33:18 +00:00
|
|
|
|
if(_driveVIA.get_should_set_overflow())
|
|
|
|
|
{
|
|
|
|
|
set_overflow_line(true);
|
|
|
|
|
}
|
2016-07-31 22:29:44 +00:00
|
|
|
|
}
|
2016-07-31 23:33:18 +00:00
|
|
|
|
else
|
|
|
|
|
set_overflow_line(false);
|
2016-07-29 15:03:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// the 1540 does not recognise index holes
|
|
|
|
|
void Machine::process_index_hole() {}
|