2016-07-05 17:28:27 +00:00
|
|
|
//
|
|
|
|
// Commodore1540.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 05/07/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-07-05 17:28:27 +00:00
|
|
|
//
|
|
|
|
|
2017-11-11 02:56:53 +00:00
|
|
|
#include "../C1540.hpp"
|
2017-09-05 00:58:00 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
2017-11-11 02:56:53 +00:00
|
|
|
#include <cstring>
|
2017-11-11 03:02:02 +00:00
|
|
|
#include <string>
|
2017-09-05 00:58:00 +00:00
|
|
|
|
|
|
|
#include "../../../../Storage/Disk/Encodings/CommodoreGCR.hpp"
|
2016-07-05 20:39:18 +00:00
|
|
|
|
|
|
|
using namespace Commodore::C1540;
|
|
|
|
|
2017-09-05 00:58:00 +00:00
|
|
|
MachineBase::MachineBase() :
|
2017-09-11 02:56:05 +00:00
|
|
|
Storage::Disk::Controller(1000000),
|
2017-11-11 03:31:27 +00:00
|
|
|
m6502_(*this),
|
|
|
|
drive_(new Storage::Disk::Drive(1000000, 300, 2)),
|
2017-09-04 18:26:04 +00:00
|
|
|
serial_port_VIA_port_handler_(new SerialPortVIA(serial_port_VIA_)),
|
2017-11-11 03:31:27 +00:00
|
|
|
serial_port_(new SerialPort),
|
2017-09-04 18:26:04 +00:00
|
|
|
drive_VIA_(drive_VIA_port_handler_),
|
2017-11-11 03:31:27 +00:00
|
|
|
serial_port_VIA_(*serial_port_VIA_port_handler_) {
|
2016-07-10 12:01:16 +00:00
|
|
|
// attach the serial port to its VIA and vice versa
|
2017-09-04 18:26:04 +00:00
|
|
|
serial_port_->set_serial_port_via(serial_port_VIA_port_handler_);
|
|
|
|
serial_port_VIA_port_handler_->set_serial_port(serial_port_);
|
2016-07-10 12:01:16 +00:00
|
|
|
|
|
|
|
// set this instance as the delegate to receive interrupt requests from both VIAs
|
2017-09-04 18:26:04 +00:00
|
|
|
serial_port_VIA_port_handler_->set_interrupt_delegate(this);
|
|
|
|
drive_VIA_port_handler_.set_interrupt_delegate(this);
|
|
|
|
drive_VIA_port_handler_.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));
|
2017-09-12 02:08:10 +00:00
|
|
|
|
|
|
|
// attach the only drive there is
|
|
|
|
set_drive(drive_);
|
2016-07-05 21:27:02 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 02:19:51 +00:00
|
|
|
Machine::Machine(Commodore::C1540::Machine::Personality personality) : personality_(personality) {}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Machine::set_serial_bus(std::shared_ptr<::Commodore::Serial::Bus> serial_bus) {
|
2016-12-03 18:14:03 +00:00
|
|
|
Commodore::Serial::AttachPortAndBus(serial_port_, serial_bus);
|
2016-07-05 21:27:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 00:58:00 +00:00
|
|
|
Cycles MachineBase::perform_bus_operation(CPU::MOS6502::BusOperation operation, uint16_t address, uint8_t *value) {
|
2016-07-10 12:01:16 +00:00
|
|
|
/*
|
|
|
|
Memory map (given that I'm unsure yet on any potential mirroring):
|
|
|
|
|
2018-05-13 19:43:03 +00:00
|
|
|
0x0000-0x07ff RAM
|
|
|
|
0x1800-0x180f the serial-port VIA
|
|
|
|
0x1c00-0x1c0f the drive VIA
|
|
|
|
0xc000-0xffff ROM
|
2016-07-10 12:01:16 +00:00
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
if(address < 0x800) {
|
2016-07-05 20:39:18 +00:00
|
|
|
if(isReadOperation(operation))
|
2016-12-03 18:14:03 +00:00
|
|
|
*value = ram_[address];
|
2016-07-05 20:39:18 +00:00
|
|
|
else
|
2016-12-03 18:14:03 +00:00
|
|
|
ram_[address] = *value;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else if(address >= 0xc000) {
|
2017-09-06 01:19:56 +00:00
|
|
|
if(isReadOperation(operation)) {
|
2016-12-03 18:14:03 +00:00
|
|
|
*value = rom_[address & 0x3fff];
|
2017-09-06 01:19:56 +00:00
|
|
|
}
|
2017-03-26 18:34:47 +00:00
|
|
|
} else if(address >= 0x1800 && address <= 0x180f) {
|
2016-07-05 21:27:02 +00:00
|
|
|
if(isReadOperation(operation))
|
2017-09-04 18:26:04 +00:00
|
|
|
*value = serial_port_VIA_.get_register(address);
|
2016-07-05 21:27:02 +00:00
|
|
|
else
|
2017-09-04 18:26:04 +00:00
|
|
|
serial_port_VIA_.set_register(address, *value);
|
2017-03-26 18:34:47 +00:00
|
|
|
} else if(address >= 0x1c00 && address <= 0x1c0f) {
|
2016-07-06 02:22:09 +00:00
|
|
|
if(isReadOperation(operation))
|
2016-12-03 18:14:03 +00:00
|
|
|
*value = drive_VIA_.get_register(address);
|
2016-07-06 02:22:09 +00:00
|
|
|
else
|
2016-12-03 18:14:03 +00:00
|
|
|
drive_VIA_.set_register(address, *value);
|
2016-07-06 02:22:09 +00:00
|
|
|
}
|
2016-07-05 21:27:02 +00:00
|
|
|
|
2017-09-04 18:26:04 +00:00
|
|
|
serial_port_VIA_.run_for(Cycles(1));
|
2017-07-25 02:29:09 +00:00
|
|
|
drive_VIA_.run_for(Cycles(1));
|
2016-07-05 20:39:18 +00:00
|
|
|
|
2017-07-26 02:21:09 +00:00
|
|
|
return Cycles(1);
|
2016-07-05 20:39:18 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 03:20:14 +00:00
|
|
|
bool Machine::set_rom_fetcher(const ROMMachine::ROMFetcher &roms_with_names) {
|
2017-11-08 02:19:51 +00:00
|
|
|
std::string rom_name;
|
|
|
|
switch(personality_) {
|
|
|
|
case Personality::C1540: rom_name = "1540.bin"; break;
|
|
|
|
case Personality::C1541: rom_name = "1541.bin"; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto roms = roms_with_names("Commodore1540", {rom_name});
|
|
|
|
if(!roms[0]) return false;
|
2017-11-11 20:28:40 +00:00
|
|
|
std::memcpy(rom_, roms[0]->data(), std::min(sizeof(rom_), roms[0]->size()));
|
2017-11-08 02:19:51 +00:00
|
|
|
return true;
|
2016-07-05 20:39:18 +00:00
|
|
|
}
|
2016-07-05 23:12:43 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Machine::set_disk(std::shared_ptr<Storage::Disk::Disk> disk) {
|
2017-09-12 02:08:10 +00:00
|
|
|
drive_->set_disk(disk);
|
2016-09-26 01:24:16 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
void Machine::run_for(const Cycles cycles) {
|
2017-08-16 15:56:52 +00:00
|
|
|
m6502_.run_for(cycles);
|
2017-09-05 00:58:00 +00:00
|
|
|
|
|
|
|
bool drive_motor = drive_VIA_port_handler_.get_motor_enabled();
|
2017-09-12 02:08:10 +00:00
|
|
|
drive_->set_motor_on(drive_motor);
|
2017-09-05 00:58:00 +00:00
|
|
|
if(drive_motor)
|
2017-07-25 02:32:41 +00:00
|
|
|
Storage::Disk::Controller::run_for(cycles);
|
2016-07-10 20:24:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 02:24:33 +00:00
|
|
|
void MachineBase::set_activity_observer(Activity::Observer *observer) {
|
|
|
|
drive_VIA_.bus_handler().set_activity_observer(observer);
|
|
|
|
drive_->set_activity_observer(observer, "Drive", false);
|
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - 6522 delegate
|
2016-07-05 23:12:43 +00:00
|
|
|
|
2017-09-05 00:58:00 +00:00
|
|
|
void MachineBase::mos6522_did_change_interrupt_status(void *mos6522) {
|
2016-07-10 12:01:16 +00:00
|
|
|
// both VIAs are connected to the IRQ line
|
2017-09-04 18:26:04 +00:00
|
|
|
m6502_.set_irq_line(serial_port_VIA_.get_interrupt_line() || drive_VIA_.get_interrupt_line());
|
2016-07-05 23:12:43 +00:00
|
|
|
}
|
2016-07-29 15:03:09 +00:00
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Disk drive
|
2016-07-29 15:03:09 +00:00
|
|
|
|
2017-09-10 23:23:23 +00:00
|
|
|
void MachineBase::process_input_bit(int value) {
|
2016-12-03 18:14:03 +00:00
|
|
|
shift_register_ = (shift_register_ << 1) | value;
|
2017-03-26 18:34:47 +00:00
|
|
|
if((shift_register_ & 0x3ff) == 0x3ff) {
|
2017-09-04 18:26:04 +00:00
|
|
|
drive_VIA_port_handler_.set_sync_detected(true);
|
2016-12-03 18:14:03 +00:00
|
|
|
bit_window_offset_ = -1; // i.e. this bit isn't the first within a data window, but the next might be
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2017-09-04 18:26:04 +00:00
|
|
|
drive_VIA_port_handler_.set_sync_detected(false);
|
2016-08-01 08:25:11 +00:00
|
|
|
}
|
2016-12-03 18:14:03 +00:00
|
|
|
bit_window_offset_++;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(bit_window_offset_ == 8) {
|
2017-10-22 01:50:53 +00:00
|
|
|
drive_VIA_port_handler_.set_data_input(static_cast<uint8_t>(shift_register_));
|
2016-12-03 18:14:03 +00:00
|
|
|
bit_window_offset_ = 0;
|
2017-09-04 18:26:04 +00:00
|
|
|
if(drive_VIA_port_handler_.get_should_set_overflow()) {
|
2017-08-16 15:56:52 +00:00
|
|
|
m6502_.set_overflow_line(true);
|
2016-07-31 23:33:18 +00:00
|
|
|
}
|
2016-07-31 22:29:44 +00:00
|
|
|
}
|
2017-08-16 15:56:52 +00:00
|
|
|
else m6502_.set_overflow_line(false);
|
2016-07-29 15:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the 1540 does not recognise index holes
|
2017-09-05 00:58:00 +00:00
|
|
|
void MachineBase::process_index_hole() {}
|
2016-08-01 08:25:11 +00:00
|
|
|
|
2017-11-12 21:11:24 +00:00
|
|
|
// MARK: - Drive VIA delegate
|
2016-08-01 08:25:11 +00:00
|
|
|
|
2017-09-05 00:58:00 +00:00
|
|
|
void MachineBase::drive_via_did_step_head(void *driveVIA, int direction) {
|
2018-05-07 03:17:36 +00:00
|
|
|
drive_->step(Storage::Disk::HeadPosition(direction, 2));
|
2016-08-01 08:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 00:58:00 +00:00
|
|
|
void MachineBase::drive_via_did_set_data_density(void *driveVIA, int density) {
|
2017-10-21 23:49:04 +00:00
|
|
|
set_expected_bit_length(Storage::Encodings::CommodoreGCR::length_of_a_bit_in_time_zone(static_cast<unsigned int>(density)));
|
2016-08-01 08:25:11 +00:00
|
|
|
}
|
2016-10-21 01:15:21 +00:00
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - SerialPortVIA
|
2016-10-21 01:15:21 +00:00
|
|
|
|
2017-11-11 17:35:51 +00:00
|
|
|
SerialPortVIA::SerialPortVIA(MOS::MOS6522::MOS6522<SerialPortVIA> &via) : via_(via) {}
|
2016-10-21 01:15:21 +00:00
|
|
|
|
2017-09-04 18:26:04 +00:00
|
|
|
uint8_t SerialPortVIA::get_port_input(MOS::MOS6522::Port port) {
|
2016-12-03 18:14:03 +00:00
|
|
|
if(port) return port_b_;
|
2016-10-21 01:15:21 +00:00
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
|
2017-09-04 18:26:04 +00:00
|
|
|
void SerialPortVIA::set_port_output(MOS::MOS6522::Port port, uint8_t value, uint8_t mask) {
|
2017-03-26 18:34:47 +00:00
|
|
|
if(port) {
|
2016-12-03 18:14:03 +00:00
|
|
|
std::shared_ptr<::Commodore::Serial::Port> serialPort = serial_port_.lock();
|
2016-10-21 01:15:21 +00:00
|
|
|
if(serialPort) {
|
2016-12-03 18:14:03 +00:00
|
|
|
attention_acknowledge_level_ = !(value&0x10);
|
|
|
|
data_level_output_ = (value&0x02);
|
2016-10-21 01:15:21 +00:00
|
|
|
|
2018-03-27 01:06:07 +00:00
|
|
|
// printf("[C1540] %s output is %s\n", StringForLine(::Commodore::Serial::Line::Clock), value ? "high" : "low");
|
|
|
|
serialPort->set_output(::Commodore::Serial::Line::Clock, static_cast<::Commodore::Serial::LineLevel>(!(value&0x08)));
|
2016-10-21 01:15:21 +00:00
|
|
|
update_data_line();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void SerialPortVIA::set_serial_line_state(::Commodore::Serial::Line line, bool value) {
|
2018-03-27 01:06:07 +00:00
|
|
|
// printf("[C1540] %s input is %s\n", StringForLine(line), value ? "high" : "low");
|
2017-09-06 01:19:56 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(line) {
|
2016-10-21 01:15:21 +00:00
|
|
|
default: break;
|
2016-12-03 18:14:03 +00:00
|
|
|
case ::Commodore::Serial::Line::Data: port_b_ = (port_b_ & ~0x01) | (value ? 0x00 : 0x01); break;
|
|
|
|
case ::Commodore::Serial::Line::Clock: port_b_ = (port_b_ & ~0x04) | (value ? 0x00 : 0x04); break;
|
2016-10-21 01:15:21 +00:00
|
|
|
case ::Commodore::Serial::Line::Attention:
|
2016-12-03 18:14:03 +00:00
|
|
|
attention_level_input_ = !value;
|
|
|
|
port_b_ = (port_b_ & ~0x80) | (value ? 0x00 : 0x80);
|
2017-09-04 18:26:04 +00:00
|
|
|
via_.set_control_line_input(MOS::MOS6522::Port::A, MOS::MOS6522::Line::One, !value);
|
2016-10-21 01:15:21 +00:00
|
|
|
update_data_line();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void SerialPortVIA::set_serial_port(const std::shared_ptr<::Commodore::Serial::Port> &serialPort) {
|
2016-12-03 18:14:03 +00:00
|
|
|
serial_port_ = serialPort;
|
2016-10-21 01:15:21 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void SerialPortVIA::update_data_line() {
|
2016-12-03 18:14:03 +00:00
|
|
|
std::shared_ptr<::Commodore::Serial::Port> serialPort = serial_port_.lock();
|
2017-03-26 18:34:47 +00:00
|
|
|
if(serialPort) {
|
2018-03-27 01:06:07 +00:00
|
|
|
// printf("[C1540] %s output is %s\n", StringForLine(::Commodore::Serial::Line::Data), (!data_level_output_ && (attention_level_input_ != attention_acknowledge_level_)) ? "high" : "low");
|
2016-10-21 01:15:21 +00:00
|
|
|
// "ATN (Attention) is an input on pin 3 of P2 and P3 that is sensed at PB7 and CA1 of UC3 after being inverted by UA1"
|
|
|
|
serialPort->set_output(::Commodore::Serial::Line::Data,
|
2018-03-27 01:06:07 +00:00
|
|
|
static_cast<::Commodore::Serial::LineLevel>(!data_level_output_ && (attention_level_input_ != attention_acknowledge_level_)));
|
2016-10-21 01:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - DriveVIA
|
2016-10-21 01:15:21 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void DriveVIA::set_delegate(Delegate *delegate) {
|
2016-12-03 18:14:03 +00:00
|
|
|
delegate_ = delegate;
|
2016-10-21 01:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write protect tab uncovered
|
2017-09-04 18:26:04 +00:00
|
|
|
uint8_t DriveVIA::get_port_input(MOS::MOS6522::Port port) {
|
2016-12-03 18:14:03 +00:00
|
|
|
return port ? port_b_ : port_a_;
|
2016-10-21 01:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DriveVIA::set_sync_detected(bool sync_detected) {
|
2016-12-03 18:14:03 +00:00
|
|
|
port_b_ = (port_b_ & 0x7f) | (sync_detected ? 0x00 : 0x80);
|
2016-10-21 01:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DriveVIA::set_data_input(uint8_t value) {
|
2016-12-03 18:14:03 +00:00
|
|
|
port_a_ = value;
|
2016-10-21 01:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DriveVIA::get_should_set_overflow() {
|
2016-12-03 18:14:03 +00:00
|
|
|
return should_set_overflow_;
|
2016-10-21 01:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DriveVIA::get_motor_enabled() {
|
2016-12-03 18:14:03 +00:00
|
|
|
return drive_motor_;
|
2016-10-21 01:15:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 18:26:04 +00:00
|
|
|
void DriveVIA::set_control_line_output(MOS::MOS6522::Port port, MOS::MOS6522::Line line, bool value) {
|
|
|
|
if(port == MOS::MOS6522::Port::A && line == MOS::MOS6522::Line::Two) {
|
2016-12-03 18:14:03 +00:00
|
|
|
should_set_overflow_ = value;
|
2016-10-21 01:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 18:26:04 +00:00
|
|
|
void DriveVIA::set_port_output(MOS::MOS6522::Port port, uint8_t value, uint8_t direction_mask) {
|
2017-03-26 18:34:47 +00:00
|
|
|
if(port) {
|
2017-09-06 01:19:56 +00:00
|
|
|
if(previous_port_b_output_ != value) {
|
|
|
|
// record drive motor state
|
|
|
|
drive_motor_ = !!(value&4);
|
|
|
|
|
|
|
|
// check for a head step
|
|
|
|
int step_difference = ((value&3) - (previous_port_b_output_&3))&3;
|
|
|
|
if(step_difference) {
|
|
|
|
if(delegate_) delegate_->drive_via_did_step_head(this, (step_difference == 1) ? 1 : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for a change in density
|
|
|
|
int density_difference = (previous_port_b_output_^value) & (3 << 5);
|
|
|
|
if(density_difference && delegate_) {
|
|
|
|
delegate_->drive_via_did_set_data_density(this, (value >> 5)&3);
|
|
|
|
}
|
|
|
|
|
2018-05-12 02:24:33 +00:00
|
|
|
// post the LED status
|
|
|
|
if(observer_) observer_->set_led_status("Drive", !!(value&8));
|
2017-09-06 01:19:56 +00:00
|
|
|
|
|
|
|
previous_port_b_output_ = value;
|
2016-10-21 01:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 02:24:33 +00:00
|
|
|
void DriveVIA::set_activity_observer(Activity::Observer *observer) {
|
|
|
|
observer_ = observer;
|
|
|
|
if(observer) {
|
|
|
|
observer->register_led("Drive");
|
|
|
|
observer->set_led_status("Drive", !!(previous_port_b_output_&8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - SerialPort
|
2016-10-21 01:15:21 +00:00
|
|
|
|
|
|
|
void SerialPort::set_input(::Commodore::Serial::Line line, ::Commodore::Serial::LineLevel level) {
|
2016-12-03 18:14:03 +00:00
|
|
|
std::shared_ptr<SerialPortVIA> serialPortVIA = serial_port_VIA_.lock();
|
2016-10-21 01:15:21 +00:00
|
|
|
if(serialPortVIA) serialPortVIA->set_serial_line_state(line, (bool)level);
|
|
|
|
}
|
|
|
|
|
2016-12-03 18:14:03 +00:00
|
|
|
void SerialPort::set_serial_port_via(const std::shared_ptr<SerialPortVIA> &serialPortVIA) {
|
|
|
|
serial_port_VIA_ = serialPortVIA;
|
2016-10-21 01:15:21 +00:00
|
|
|
}
|