2016-08-22 08:11:24 +10:00
|
|
|
#include <md/md.hpp>
|
|
|
|
|
|
|
|
namespace MegaDrive {
|
|
|
|
|
2017-06-27 11:18:28 +10:00
|
|
|
ControllerPort controllerPort1;
|
|
|
|
ControllerPort controllerPort2;
|
|
|
|
ControllerPort extensionPort;
|
2016-08-22 08:11:24 +10:00
|
|
|
#include "gamepad/gamepad.cpp"
|
|
|
|
|
|
|
|
Controller::Controller(uint port) : port(port) {
|
|
|
|
if(!handle()) create(Controller::Enter, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
Controller::~Controller() {
|
2017-01-22 11:33:36 +11:00
|
|
|
scheduler.remove(*this);
|
2016-08-22 08:11:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
auto Controller::Enter() -> void {
|
|
|
|
while(true) {
|
|
|
|
scheduler.synchronize();
|
2017-06-27 11:18:28 +10:00
|
|
|
if(controllerPort1.controller->active()) controllerPort1.controller->main();
|
|
|
|
if(controllerPort2.controller->active()) controllerPort2.controller->main();
|
|
|
|
if(extensionPort.controller->active()) extensionPort.controller->main();
|
2016-08-22 08:11:24 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Controller::main() -> void {
|
|
|
|
step(1);
|
|
|
|
synchronize(cpu);
|
|
|
|
}
|
|
|
|
|
2017-06-27 11:18:28 +10:00
|
|
|
//
|
|
|
|
|
|
|
|
auto ControllerPort::connect(uint device) -> void {
|
|
|
|
if(!system.loaded()) return;
|
|
|
|
delete controller;
|
|
|
|
|
|
|
|
switch(device) { default:
|
|
|
|
case ID::Device::None: controller = new Controller(port); break;
|
|
|
|
case ID::Device::Gamepad: controller = new Gamepad(port); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cpu.peripherals.reset();
|
|
|
|
cpu.peripherals.append(controllerPort1.controller);
|
|
|
|
cpu.peripherals.append(controllerPort2.controller);
|
|
|
|
cpu.peripherals.append(extensionPort.controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ControllerPort::readData() -> uint8 {
|
|
|
|
return controller->readData();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ControllerPort::writeData(uint8 data) -> void {
|
|
|
|
return controller->writeData(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ControllerPort::readControl() -> uint8 {
|
|
|
|
return control;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ControllerPort::writeControl(uint8 data) -> void {
|
|
|
|
control = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ControllerPort::power(uint port) -> void {
|
|
|
|
this->port = port;
|
|
|
|
control = 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ControllerPort::unload() -> void {
|
|
|
|
delete controller;
|
|
|
|
controller = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ControllerPort::serialize(serializer& s) -> void {
|
|
|
|
s.integer(control);
|
|
|
|
}
|
|
|
|
|
2016-08-22 08:11:24 +10:00
|
|
|
}
|