2020-11-01 01:00:15 +00:00
|
|
|
|
//
|
|
|
|
|
// ADB.cpp
|
|
|
|
|
// Clock Signal
|
|
|
|
|
//
|
|
|
|
|
// Created by Thomas Harte on 31/10/2020.
|
|
|
|
|
// Copyright © 2020 Thomas Harte. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "ADB.hpp"
|
|
|
|
|
|
2021-01-24 23:07:05 +00:00
|
|
|
|
#include <cassert>
|
2020-11-01 01:00:15 +00:00
|
|
|
|
#include <cstdio>
|
2021-01-27 23:31:11 +00:00
|
|
|
|
#include <iostream>
|
2020-11-01 01:00:15 +00:00
|
|
|
|
|
2021-01-27 00:52:30 +00:00
|
|
|
|
// TEST.
|
|
|
|
|
#include "../../../InstructionSets/M50740/Parser.hpp"
|
|
|
|
|
#include "../../../InstructionSets/Disassembler.hpp"
|
|
|
|
|
|
2021-02-15 03:20:58 +00:00
|
|
|
|
#define LOG_PREFIX "[ADB GLU] "
|
|
|
|
|
#include "../../../Outputs/Log.hpp"
|
|
|
|
|
|
2020-11-01 01:00:15 +00:00
|
|
|
|
using namespace Apple::IIgs::ADB;
|
|
|
|
|
|
2021-01-30 22:53:27 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// Flags affecting the CPU-visible status register.
|
|
|
|
|
enum class CPUFlags: uint8_t {
|
|
|
|
|
MouseDataFull = 0x80,
|
|
|
|
|
MouseInterruptEnabled = 0x40,
|
|
|
|
|
CommandDataIsValid = 0x20,
|
|
|
|
|
CommandDataInterruptEnabled = 0x10,
|
|
|
|
|
KeyboardDataFull = 0x08,
|
|
|
|
|
KeyboardDataInterruptEnabled = 0x04,
|
|
|
|
|
MouseXIsAvailable = 0x02,
|
|
|
|
|
CommandRegisterFull = 0x01,
|
|
|
|
|
};
|
2021-01-24 23:07:05 +00:00
|
|
|
|
|
2021-01-30 22:53:27 +00:00
|
|
|
|
// Flags affecting the microcontroller-visible register.
|
|
|
|
|
enum class MicrocontrollerFlags: uint8_t {
|
|
|
|
|
CommandRegisterFull = 0x40,
|
|
|
|
|
};
|
2020-11-10 00:05:48 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 04:16:45 +00:00
|
|
|
|
GLU::GLU() :
|
|
|
|
|
executor_(*this),
|
|
|
|
|
bus_(HalfCycles(1'789'772)),
|
|
|
|
|
controller_id_(bus_.add_device()),
|
|
|
|
|
mouse_(bus_),
|
|
|
|
|
keyboard_(bus_) {}
|
2021-01-30 22:53:27 +00:00
|
|
|
|
|
2020-11-10 00:05:48 +00:00
|
|
|
|
// MARK: - External interface.
|
|
|
|
|
|
2020-11-01 01:00:15 +00:00
|
|
|
|
uint8_t GLU::get_keyboard_data() {
|
2020-11-01 19:49:04 +00:00
|
|
|
|
// The classic Apple II serial keyboard register:
|
|
|
|
|
// b7: key strobe.
|
|
|
|
|
// b6–b0: ASCII code.
|
2021-02-20 02:55:06 +00:00
|
|
|
|
return (registers_[0] & 0x7f) | ((status_ & uint8_t(CPUFlags::KeyboardDataFull)) ? 0x80 : 0x00);
|
2020-11-01 01:00:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 02:35:11 +00:00
|
|
|
|
void GLU::clear_key_strobe() {
|
|
|
|
|
// Clears the key strobe of the classic Apple II serial keyboard register.
|
2021-02-20 01:06:12 +00:00
|
|
|
|
status_ &= ~uint8_t(CPUFlags::KeyboardDataFull);
|
2020-11-05 02:35:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t GLU::get_any_key_down() {
|
|
|
|
|
// The Apple IIe check-for-any-key-down bit.
|
2021-01-30 22:53:27 +00:00
|
|
|
|
return registers_[5];
|
2020-11-05 02:35:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 01:00:15 +00:00
|
|
|
|
uint8_t GLU::get_mouse_data() {
|
2020-11-01 19:49:04 +00:00
|
|
|
|
// Alternates between returning x and y values.
|
|
|
|
|
//
|
2023-05-12 18:14:45 +00:00
|
|
|
|
// b7: 1 = button is up; 0 = button is down.
|
2020-11-01 19:49:04 +00:00
|
|
|
|
// b6: delta sign bit; 1 = negative.
|
|
|
|
|
// b5–b0: mouse delta.
|
2021-02-20 03:48:15 +00:00
|
|
|
|
|
|
|
|
|
const uint8_t result = registers_[visible_mouse_register_];
|
2023-08-20 19:02:25 +00:00
|
|
|
|
if(visible_mouse_register_ == 3) {
|
2021-02-24 02:57:02 +00:00
|
|
|
|
status_ &= ~uint8_t(CPUFlags::MouseDataFull);
|
|
|
|
|
}
|
2023-08-20 19:02:25 +00:00
|
|
|
|
|
|
|
|
|
// Spelt out at tedious length because Clang has trust issues.
|
|
|
|
|
static constexpr int first_register = 2;
|
|
|
|
|
static constexpr int second_register = 3;
|
|
|
|
|
static constexpr int flip_mask = first_register ^ second_register;
|
|
|
|
|
visible_mouse_register_ ^= flip_mask;
|
2021-02-20 03:48:15 +00:00
|
|
|
|
return result;
|
2020-11-01 01:00:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t GLU::get_modifier_status() {
|
2020-11-01 19:49:04 +00:00
|
|
|
|
// b7: 1 = command key pressed; 0 = not.
|
|
|
|
|
// b6: option key.
|
|
|
|
|
// b5: 1 = modifier key latch has been updated, no key has been pressed; 0 = not.
|
|
|
|
|
// b4: any numeric keypad key.
|
|
|
|
|
// b3: a key is down.
|
|
|
|
|
// b2: caps lock is pressed.
|
|
|
|
|
// b1: control key.
|
|
|
|
|
// b0: shift key.
|
2021-01-30 22:53:27 +00:00
|
|
|
|
return registers_[6];
|
2020-11-01 01:00:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t GLU::get_data() {
|
2020-11-01 19:49:04 +00:00
|
|
|
|
// b0–2: number of data bytes to be returned.
|
|
|
|
|
// b3: 1 = a valid service request is pending; 0 = no request pending.
|
|
|
|
|
// b4: 1 = control, command and delete keys have been pressed simultaneously; 0 = they haven't.
|
|
|
|
|
// b5: 1 = control, command and reset have all been pressed together; 0 = they haven't.
|
|
|
|
|
// b6: 1 = ADB controller encountered an error and reset itself; 0 = no error.
|
|
|
|
|
// b7: 1 = ADB has received a response from the addressed ADB device; 0 = no respone.
|
2021-02-20 03:22:14 +00:00
|
|
|
|
status_ &= ~uint8_t(CPUFlags::CommandDataIsValid);
|
2021-01-30 22:53:27 +00:00
|
|
|
|
return registers_[7];
|
2020-11-01 01:00:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t GLU::get_status() {
|
2020-11-01 19:49:04 +00:00
|
|
|
|
// b7: 1 = mouse data register is full; 0 = empty.
|
|
|
|
|
// b6: 1 = mouse interrupt is enabled.
|
|
|
|
|
// b5: 1 = command/data has valid data.
|
|
|
|
|
// b4: 1 = command/data interrupt is enabled.
|
|
|
|
|
// b3: 1 = keyboard data is full.
|
|
|
|
|
// b2: 1 = keyboard data interrupt is enabled.
|
|
|
|
|
// b1: 1 = mouse x-data is available; 0 = y.
|
|
|
|
|
// b0: 1 = command register is full (set when command is written); 0 = empty (cleared when data is read).
|
2023-08-20 19:02:25 +00:00
|
|
|
|
return status_ | ((visible_mouse_register_ == 2) ? 0 : uint8_t(CPUFlags::MouseXIsAvailable));
|
2020-11-01 01:00:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-15 02:04:20 +00:00
|
|
|
|
void GLU::set_status(uint8_t status) {
|
|
|
|
|
// This permits only the interrupt flags to be set.
|
|
|
|
|
constexpr uint8_t interrupt_flags =
|
|
|
|
|
uint8_t(CPUFlags::MouseInterruptEnabled) |
|
|
|
|
|
uint8_t(CPUFlags::CommandDataInterruptEnabled) |
|
|
|
|
|
uint8_t(CPUFlags::KeyboardDataInterruptEnabled);
|
|
|
|
|
status_ = (status_ & ~interrupt_flags) | (status & interrupt_flags);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 19:49:04 +00:00
|
|
|
|
void GLU::set_command(uint8_t command) {
|
2021-01-30 22:53:27 +00:00
|
|
|
|
registers_[1] = command;
|
|
|
|
|
registers_[4] |= uint8_t(MicrocontrollerFlags::CommandRegisterFull);
|
|
|
|
|
status_ |= uint8_t(CPUFlags::CommandRegisterFull);
|
2020-11-01 01:00:15 +00:00
|
|
|
|
}
|
2020-11-10 00:05:48 +00:00
|
|
|
|
|
2021-01-30 22:53:27 +00:00
|
|
|
|
// MARK: - Setup and run.
|
2021-01-18 21:59:49 +00:00
|
|
|
|
|
|
|
|
|
void GLU::set_microcontroller_rom(const std::vector<uint8_t> &rom) {
|
|
|
|
|
executor_.set_rom(rom);
|
2021-01-27 00:52:30 +00:00
|
|
|
|
|
2021-01-27 02:29:17 +00:00
|
|
|
|
// TEST invocation.
|
2021-01-30 22:53:27 +00:00
|
|
|
|
/* InstructionSet::Disassembler<InstructionSet::M50740::Parser, 0x1fff, InstructionSet::M50740::Instruction, uint8_t, uint16_t> disassembler;
|
2021-01-27 00:52:30 +00:00
|
|
|
|
disassembler.disassemble(rom.data(), 0x1000, uint16_t(rom.size()), 0x1000);
|
2021-01-27 02:29:17 +00:00
|
|
|
|
|
|
|
|
|
const auto instructions = disassembler.instructions();
|
|
|
|
|
const auto entry_points = disassembler.entry_points();
|
|
|
|
|
for(const auto &pair : instructions) {
|
|
|
|
|
std::cout << std::hex << pair.first << "\t\t";
|
|
|
|
|
if(entry_points.find(pair.first) != entry_points.end()) {
|
|
|
|
|
std::cout << "L" << pair.first << "\t";
|
|
|
|
|
} else {
|
|
|
|
|
std::cout << "\t\t";
|
|
|
|
|
}
|
|
|
|
|
std::cout << operation_name(pair.second.operation) << " ";
|
|
|
|
|
std::cout << address(pair.second.addressing_mode, &rom[pair.first - 0x1000], pair.first);
|
|
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
2021-01-30 22:53:27 +00:00
|
|
|
|
}*/
|
2021-01-18 21:59:49 +00:00
|
|
|
|
}
|
2021-01-24 03:55:12 +00:00
|
|
|
|
|
|
|
|
|
void GLU::run_for(Cycles cycles) {
|
|
|
|
|
executor_.run_for(cycles);
|
|
|
|
|
}
|
2021-01-24 23:07:05 +00:00
|
|
|
|
|
|
|
|
|
// MARK: - M50470 port handler
|
|
|
|
|
|
|
|
|
|
void GLU::set_port_output(int port, uint8_t value) {
|
|
|
|
|
switch(port) {
|
|
|
|
|
case 0:
|
2021-02-13 22:38:42 +00:00
|
|
|
|
register_latch_ = value;
|
2021-01-24 23:07:05 +00:00
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
// printf("Keyboard write: %02x???\n", value);
|
|
|
|
|
break;
|
2021-02-13 22:38:42 +00:00
|
|
|
|
case 2: {
|
2021-01-24 23:07:05 +00:00
|
|
|
|
// printf("ADB data line input: %d???\n", value >> 7);
|
|
|
|
|
// printf("IIe keyboard reset line: %d\n", (value >> 6)&1);
|
|
|
|
|
// printf("IIgs reset line: %d\n", (value >> 5)&1);
|
|
|
|
|
// printf("GLU strobe: %d\n", (value >> 4)&1);
|
2021-01-25 22:43:22 +00:00
|
|
|
|
// printf("Select GLU register: %d [%02x]\n", value & 0xf, value);
|
2021-02-13 22:38:42 +00:00
|
|
|
|
|
2021-01-24 23:07:05 +00:00
|
|
|
|
register_address_ = value & 0xf;
|
2021-02-13 22:38:42 +00:00
|
|
|
|
|
2021-02-20 02:46:18 +00:00
|
|
|
|
// This is an ugly hack, I think. Per Neil Parker's Inside the Apple IIGS ADB Controller
|
|
|
|
|
// http://nparker.llx.com/a2/adb.html#external:
|
2021-02-13 22:38:42 +00:00
|
|
|
|
//
|
2021-02-20 02:46:18 +00:00
|
|
|
|
// The protocol for reading an ADB GLU register is as follows:
|
|
|
|
|
//
|
|
|
|
|
// 1. Put the register number of the ADB GLU register in port P2 bits 0-3.
|
|
|
|
|
// 2. Clear bit 4 of port P2, read the data from P0, and set bit 4 of P0.
|
|
|
|
|
//
|
|
|
|
|
// The protocol for writing a GLU register is similar:
|
|
|
|
|
//
|
|
|
|
|
// 1. Write the register number to port P2 bits 0-3.
|
|
|
|
|
// 2. Write the data to port P0.
|
|
|
|
|
// 3. Configure port P0 for output by writing $FF to $E1.
|
|
|
|
|
// 4. Clear bit 4 of P2, and immediately set it again.
|
|
|
|
|
// 5. Configure port P0 for input by writing 0 to $E1.
|
|
|
|
|
//
|
|
|
|
|
// ---
|
|
|
|
|
//
|
|
|
|
|
// I tried: linking a read or write to rising or falling edges of the strobe.
|
|
|
|
|
// Including with hysteresis as per the "immediately" (which, in practice, seems
|
|
|
|
|
// to mean "in the very next instruction", i.e. 5 cycles later). That didn't seem
|
|
|
|
|
// properly to differentiate.
|
|
|
|
|
//
|
|
|
|
|
// So I'm focussing on the "configure port P0 for output" bit. Which I don't see
|
|
|
|
|
// would be visible here unless it is actually an exposed signal, which is unlikely.
|
|
|
|
|
//
|
|
|
|
|
// Ergo: ugly. HACK.
|
2021-02-13 22:38:42 +00:00
|
|
|
|
const bool strobe = value & 0x10;
|
|
|
|
|
if(strobe != register_strobe_) {
|
|
|
|
|
register_strobe_ = strobe;
|
|
|
|
|
|
2021-02-20 02:46:18 +00:00
|
|
|
|
if(!register_strobe_) {
|
|
|
|
|
if(executor_.get_output_mask(0)) {
|
|
|
|
|
registers_[register_address_] = register_latch_;
|
|
|
|
|
switch(register_address_) {
|
|
|
|
|
default: break;
|
|
|
|
|
case 0: status_ |= uint8_t(CPUFlags::KeyboardDataFull); break;
|
2021-02-20 03:48:15 +00:00
|
|
|
|
case 2:
|
|
|
|
|
case 3:
|
|
|
|
|
status_ |= uint8_t(CPUFlags::MouseDataFull);
|
|
|
|
|
visible_mouse_register_ = 2;
|
|
|
|
|
break;
|
2021-02-20 02:46:18 +00:00
|
|
|
|
case 7: status_ |= uint8_t(CPUFlags::CommandDataIsValid); break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
register_latch_ = registers_[register_address_];
|
|
|
|
|
switch(register_address_) {
|
|
|
|
|
default: break;
|
|
|
|
|
case 1:
|
|
|
|
|
registers_[4] &= ~uint8_t(MicrocontrollerFlags::CommandRegisterFull);
|
|
|
|
|
status_ &= ~uint8_t(CPUFlags::CommandRegisterFull);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-02-13 22:38:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} break;
|
2021-02-11 02:24:31 +00:00
|
|
|
|
case 3:
|
2021-02-15 03:20:58 +00:00
|
|
|
|
if(modifier_state_ != (value & 0x30)) {
|
|
|
|
|
modifier_state_ = value & 0x30;
|
|
|
|
|
LOG("Modifier state: " << int(value & 0x30));
|
|
|
|
|
}
|
2021-01-25 22:43:22 +00:00
|
|
|
|
|
2021-02-09 03:08:49 +00:00
|
|
|
|
// Output is inverted respective to input; the microcontroller
|
|
|
|
|
// sets a value of '1' in order to pull the ADB bus low.
|
2021-02-11 02:24:31 +00:00
|
|
|
|
bus_.set_device_output(controller_id_, !(value & 0x08));
|
|
|
|
|
break;
|
2021-01-24 23:07:05 +00:00
|
|
|
|
|
|
|
|
|
default: assert(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-15 02:15:31 +00:00
|
|
|
|
bool GLU::get_command_button() const {
|
|
|
|
|
return modifier_state_ & 0x20;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GLU::get_option_button() const {
|
|
|
|
|
return modifier_state_ & 0x10;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-24 23:07:05 +00:00
|
|
|
|
uint8_t GLU::get_port_input(int port) {
|
|
|
|
|
switch(port) {
|
2021-02-13 22:38:42 +00:00
|
|
|
|
case 0: return register_latch_;
|
2021-01-24 23:07:05 +00:00
|
|
|
|
case 1:
|
|
|
|
|
// printf("IIe keyboard read\n");
|
|
|
|
|
return 0x06;
|
|
|
|
|
case 2:
|
2021-01-25 22:43:22 +00:00
|
|
|
|
// printf("ADB data line input, etc\n");
|
2021-02-11 02:24:31 +00:00
|
|
|
|
return bus_.get_state() ? 0x80 : 0x00;
|
2021-01-24 23:07:05 +00:00
|
|
|
|
case 3:
|
|
|
|
|
// printf("ADB data line output, etc\n");
|
2021-02-08 04:05:01 +00:00
|
|
|
|
return 0x00;
|
2021-01-24 23:07:05 +00:00
|
|
|
|
|
|
|
|
|
default: assert(false);
|
|
|
|
|
}
|
|
|
|
|
return 0xff;
|
|
|
|
|
}
|
2021-01-25 22:43:22 +00:00
|
|
|
|
|
|
|
|
|
void GLU::run_ports_for(Cycles cycles) {
|
2021-02-11 02:24:31 +00:00
|
|
|
|
bus_.run_for(cycles);
|
2021-01-25 22:43:22 +00:00
|
|
|
|
}
|
2021-02-15 03:20:58 +00:00
|
|
|
|
|
|
|
|
|
void GLU::set_vertical_blank(bool is_blank) {
|
2023-08-21 19:35:13 +00:00
|
|
|
|
vertical_blank_ = is_blank;
|
2021-02-15 03:20:58 +00:00
|
|
|
|
executor_.set_interrupt_line(is_blank);
|
|
|
|
|
}
|