2021-02-11 02:24:31 +00:00
|
|
|
//
|
|
|
|
// Bus.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 10/02/2021.
|
|
|
|
// Copyright © 2021 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2021-02-11 02:24:31 +00:00
|
|
|
|
|
|
|
#include "../../../ClockReceiver/ClockReceiver.hpp"
|
|
|
|
|
|
|
|
#include <bitset>
|
|
|
|
#include <cstddef>
|
2021-02-13 22:38:42 +00:00
|
|
|
#include <ostream>
|
2021-02-11 02:24:31 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-05-10 21:02:18 +00:00
|
|
|
namespace Apple::ADB {
|
2021-02-11 02:24:31 +00:00
|
|
|
|
2021-02-11 02:39:33 +00:00
|
|
|
struct Command {
|
|
|
|
enum class Type {
|
|
|
|
Reset,
|
|
|
|
Flush,
|
|
|
|
Reserved,
|
2021-02-13 02:50:24 +00:00
|
|
|
/// The host wishes the device to store register contents.
|
2021-02-11 02:39:33 +00:00
|
|
|
Listen,
|
2021-02-13 02:50:24 +00:00
|
|
|
/// The host wishes the device to broadcast register contents.
|
2021-02-11 02:39:33 +00:00
|
|
|
Talk
|
|
|
|
};
|
2021-02-15 01:37:33 +00:00
|
|
|
static constexpr uint8_t AllDevices = 0xff;
|
|
|
|
static constexpr uint8_t NoRegister = 0xff;
|
|
|
|
|
|
|
|
Type type = Type::Reserved;
|
|
|
|
uint8_t device = AllDevices;
|
|
|
|
uint8_t reg = NoRegister;
|
2021-02-11 02:39:33 +00:00
|
|
|
|
2024-02-17 02:47:23 +00:00
|
|
|
Command() = default;
|
2021-02-11 02:39:33 +00:00
|
|
|
Command(Type type) : type(type) {}
|
|
|
|
Command(Type type, uint8_t device) : type(type), device(device) {}
|
|
|
|
Command(Type type, uint8_t device, uint8_t reg) : type(type), device(device), reg(reg) {}
|
|
|
|
};
|
|
|
|
|
2021-02-13 22:38:42 +00:00
|
|
|
inline std::ostream &operator <<(std::ostream &stream, Command::Type type) {
|
|
|
|
switch(type) {
|
|
|
|
case Command::Type::Reset: stream << "reset"; break;
|
|
|
|
case Command::Type::Flush: stream << "flush"; break;
|
|
|
|
case Command::Type::Listen: stream << "listen"; break;
|
|
|
|
case Command::Type::Talk: stream << "talk"; break;
|
2023-05-12 18:14:45 +00:00
|
|
|
default: stream << "reserved"; break;
|
2021-02-13 22:38:42 +00:00
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::ostream &operator <<(std::ostream &stream, Command command) {
|
|
|
|
stream << "Command {";
|
|
|
|
if(command.device != 0xff) stream << "device " << int(command.device) << ", ";
|
|
|
|
if(command.reg != 0xff) stream << "register " << int(command.reg) << ", ";
|
|
|
|
stream << command.type;
|
|
|
|
stream << "}";
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2021-02-11 02:39:33 +00:00
|
|
|
/*!
|
|
|
|
@returns The @c Command encoded in @c code.
|
|
|
|
*/
|
|
|
|
inline Command decode_command(uint8_t code) {
|
|
|
|
switch(code & 0x0f) {
|
|
|
|
default: return Command();
|
|
|
|
|
|
|
|
case 0: return Command(Command::Type::Reset);
|
|
|
|
case 1: return Command(Command::Type::Flush, code >> 4);
|
|
|
|
|
|
|
|
case 8: case 9: case 10: case 11:
|
|
|
|
return Command(Command::Type::Listen, code >> 4, code & 3);
|
|
|
|
|
|
|
|
case 12: case 13: case 14: case 15:
|
|
|
|
return Command(Command::Type::Talk, code >> 4, code & 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 02:24:31 +00:00
|
|
|
/*!
|
|
|
|
The ADB bus models the data line of the ADB bus; it allows multiple devices to
|
|
|
|
post their current data level, or read the current level, and also offers a tokenised
|
|
|
|
version of all activity on the bus.
|
2021-02-12 23:42:12 +00:00
|
|
|
|
|
|
|
In implementation terms, two types of device are envisaged:
|
|
|
|
|
|
|
|
* proactive devices, which use @c add_device() and then merely @c set_device_output
|
|
|
|
and @c get_state() as required, according to their own tracking of time; and
|
|
|
|
|
|
|
|
* reactive devices, which use @c add_device(Device*) and then merely react to
|
|
|
|
@c adb_bus_did_observe_event and @c advance_state in order to
|
|
|
|
update @c set_device_output.
|
2021-02-11 02:24:31 +00:00
|
|
|
*/
|
|
|
|
class Bus {
|
|
|
|
public:
|
|
|
|
Bus(HalfCycles clock_speed);
|
|
|
|
|
2021-02-11 02:28:32 +00:00
|
|
|
/*!
|
|
|
|
Advances time; ADB is a clocked serial signal.
|
|
|
|
*/
|
2021-02-11 02:24:31 +00:00
|
|
|
void run_for(HalfCycles);
|
|
|
|
|
2021-02-11 02:28:32 +00:00
|
|
|
/*!
|
|
|
|
Adds a device to the bus, returning the index it should use
|
|
|
|
to refer to itself in subsequent calls to set_device_output.
|
|
|
|
*/
|
2021-02-11 02:24:31 +00:00
|
|
|
size_t add_device();
|
|
|
|
|
2021-02-11 02:28:32 +00:00
|
|
|
/*!
|
|
|
|
Sets the current data line output for @c device.
|
|
|
|
*/
|
2021-02-12 23:42:12 +00:00
|
|
|
void set_device_output(size_t device_id, bool output);
|
2021-02-11 02:24:31 +00:00
|
|
|
|
2021-02-11 02:28:32 +00:00
|
|
|
/*!
|
|
|
|
@returns The current state of the ADB data line.
|
|
|
|
*/
|
2021-02-11 02:24:31 +00:00
|
|
|
bool get_state() const;
|
|
|
|
|
|
|
|
enum class Event {
|
|
|
|
Reset,
|
|
|
|
Attention,
|
|
|
|
Byte,
|
|
|
|
ServiceRequest,
|
|
|
|
|
|
|
|
Unrecognised
|
|
|
|
};
|
|
|
|
|
2021-02-12 23:42:12 +00:00
|
|
|
struct Device {
|
2021-02-11 02:24:31 +00:00
|
|
|
/// Reports to an observer that @c event was observed in the activity
|
2021-02-11 02:28:32 +00:00
|
|
|
/// observed on this bus. If this was a byte event, that byte's value is given as @c value.
|
2021-02-12 23:42:12 +00:00
|
|
|
virtual void adb_bus_did_observe_event(Event event, uint8_t value = 0xff) = 0;
|
|
|
|
|
|
|
|
/// Requests that the device update itself @c microseconds and, if necessary, post a
|
|
|
|
/// new value ot @c set_device_output. This will be called only when the bus needs
|
|
|
|
/// to reevaluate its current level. It cannot reliably be used to track the timing between
|
|
|
|
/// observed events.
|
2021-02-14 23:54:22 +00:00
|
|
|
virtual void advance_state(double microseconds, bool current_level) = 0;
|
2021-02-11 02:24:31 +00:00
|
|
|
};
|
|
|
|
/*!
|
2021-02-12 23:42:12 +00:00
|
|
|
Adds a device.
|
2021-02-11 02:24:31 +00:00
|
|
|
*/
|
2021-02-12 23:42:12 +00:00
|
|
|
size_t add_device(Device *);
|
2021-02-11 02:24:31 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
HalfCycles time_in_state_;
|
2021-02-12 23:42:12 +00:00
|
|
|
mutable HalfCycles time_since_get_state_;
|
|
|
|
|
2021-02-11 02:24:31 +00:00
|
|
|
double half_cycles_to_microseconds_ = 1.0;
|
2021-02-12 23:42:12 +00:00
|
|
|
std::vector<Device *> devices_;
|
2021-02-11 02:24:31 +00:00
|
|
|
unsigned int shift_register_ = 0;
|
2021-02-16 01:33:10 +00:00
|
|
|
unsigned int start_target_ = 8;
|
2021-02-11 02:24:31 +00:00
|
|
|
bool data_level_ = true;
|
|
|
|
|
|
|
|
// ADB addressing supports at most 16 devices but that doesn't include
|
|
|
|
// the controller. So assume a maximum of 17 connected devices.
|
|
|
|
std::bitset<17> bus_state_{0xffffffff};
|
|
|
|
size_t next_device_id_ = 0;
|
|
|
|
|
|
|
|
inline void shift(unsigned int);
|
2021-02-15 01:41:05 +00:00
|
|
|
enum class Phase {
|
|
|
|
PacketCapture,
|
|
|
|
AttentionCapture
|
|
|
|
} phase_ = Phase::AttentionCapture;
|
2021-02-11 02:24:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|