2016-08-21 22:11:24 +00:00
|
|
|
#include <md/md.hpp>
|
|
|
|
|
|
|
|
namespace MegaDrive {
|
|
|
|
|
2017-06-27 01:18:28 +00:00
|
|
|
ControllerPort controllerPort1;
|
|
|
|
ControllerPort controllerPort2;
|
|
|
|
ControllerPort extensionPort;
|
2016-08-21 22:11:24 +00:00
|
|
|
#include "gamepad/gamepad.cpp"
|
|
|
|
|
|
|
|
Controller::Controller(uint port) : port(port) {
|
2017-06-30 04:17:23 +00:00
|
|
|
if(!handle()) create(Controller::Enter, 1);
|
2016-08-21 22:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Controller::~Controller() {
|
2017-01-22 00:33:36 +00:00
|
|
|
scheduler.remove(*this);
|
2016-08-21 22:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto Controller::Enter() -> void {
|
|
|
|
while(true) {
|
|
|
|
scheduler.synchronize();
|
2017-06-30 04:17:23 +00:00
|
|
|
if(controllerPort1.device->active()) controllerPort1.device->main();
|
|
|
|
if(controllerPort2.device->active()) controllerPort2.device->main();
|
|
|
|
if(extensionPort.device->active()) extensionPort.device->main();
|
2016-08-21 22:11:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Controller::main() -> void {
|
|
|
|
step(1);
|
|
|
|
synchronize(cpu);
|
|
|
|
}
|
|
|
|
|
2017-06-27 01:18:28 +00:00
|
|
|
//
|
|
|
|
|
2017-06-30 04:17:23 +00:00
|
|
|
auto ControllerPort::connect(uint deviceID) -> void {
|
2017-06-27 01:18:28 +00:00
|
|
|
if(!system.loaded()) return;
|
2017-06-30 04:17:23 +00:00
|
|
|
delete device;
|
2017-06-27 01:18:28 +00:00
|
|
|
|
2017-06-30 04:17:23 +00:00
|
|
|
switch(deviceID) { default:
|
|
|
|
case ID::Device::None: device = new Controller(port); break;
|
|
|
|
case ID::Device::Gamepad: device = new Gamepad(port); break;
|
2017-06-27 01:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cpu.peripherals.reset();
|
2017-06-30 04:17:23 +00:00
|
|
|
if(auto device = controllerPort1.device) cpu.peripherals.append(device);
|
|
|
|
if(auto device = controllerPort2.device) cpu.peripherals.append(device);
|
|
|
|
if(auto device = extensionPort.device) cpu.peripherals.append(device);
|
2017-06-27 01:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-06-30 04:17:23 +00:00
|
|
|
delete device;
|
|
|
|
device = nullptr;
|
2017-06-27 01:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ControllerPort::serialize(serializer& s) -> void {
|
|
|
|
s.integer(control);
|
|
|
|
}
|
|
|
|
|
2016-08-21 22:11:24 +00:00
|
|
|
}
|