Skip to content

Commit

Permalink
ps1: separate /ack and transfer delays into two counters
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeUsher committed Aug 14, 2023
1 parent 543dec4 commit 631085f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
16 changes: 5 additions & 11 deletions ares/ps1/peripheral/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,30 @@ auto Peripheral::transmit(u8 data) -> void {

//Calculate the number of cycles required to transfer a byte at the current baud rate
//This is added to the /ACK delay to determine the total duration until /ACK is asserted
//NOTE: transfer time is only emulated for memory cards for now, not controllers
//enabling it for controllers causes games to drop input; needs further investigation
u8 factors[4] = {1, 1, 16, 64};
auto transferCycles = ((io.baudrateReloadValue * factors[io.baudrateReloadFactor])) * 8;
io.transferCounter = ((io.baudrateReloadValue * factors[io.baudrateReloadFactor])) * 8;

if(io.slotNumber == 0) {
if(!memoryCardPort1.active()) {
io.receiveSize = 1;
io.receiveData = controllerPort1.bus(data);
if(controllerPort1.acknowledge()) io.counter = /*transferCycles*/ + 338; // approx 9.98us
if(controllerPort1.acknowledge()) io.ackCounter = 338; // approx 9.98us
}

if(!controllerPort1.active()) {
io.receiveSize = 1;
io.receiveData = memoryCardPort1.bus(data);
if(memoryCardPort1.acknowledge()) io.counter = transferCycles + 170; // approx 5us
if(memoryCardPort1.acknowledge()) io.ackCounter = 170; // approx 5us
}
}

if(io.slotNumber == 1) {
if(!memoryCardPort2.active()) {
io.receiveSize = 1;
io.receiveData = controllerPort2.bus(data);
if(controllerPort2.acknowledge()) io.counter = /*transferCycles*/ + 338; // approx 9.98us
if(controllerPort2.acknowledge()) io.ackCounter = 338; // approx 9.98us
}

if(!controllerPort2.active()) {
io.receiveSize = 1;
io.receiveData = memoryCardPort2.bus(data);
if(memoryCardPort2.acknowledge()) io.counter = transferCycles + 170; // approx 5us
if(memoryCardPort2.acknowledge()) io.ackCounter = 170; // approx 5us
}
}
}
Expand Down
17 changes: 12 additions & 5 deletions ares/ps1/peripheral/peripheral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@ auto Peripheral::unload() -> void {
}

auto Peripheral::main() -> void {
if(io.counter > 0) {
if(--io.counter == 0) {
if(!io.acknowledgeAsserted) {
if(io.transferCounter > 0) {
if(--io.transferCounter == 0) {
//transfer complete, set receive size to 1 byte
io.receiveSize = 1;
}
}

if(io.transferCounter == 0 && io.ackCounter > 0) {
if(--io.ackCounter == 0) {
if (!io.acknowledgeAsserted) {
//Assert /ACK and fire the IRQ
io.interruptRequest = 1;
io.acknowledgeAsserted = 1;
if(io.acknowledgeInterruptEnable) {
interrupt.raise(Interrupt::Peripheral);
}

io.counter = 96; // ACK duration is approx 96 cycles (2.84us)
io.ackCounter = 96; // ACK duration is approx 96 cycles (2.84us)
} else {
//Deassert /ACK
//De-assert /ACK
io.acknowledgeAsserted = 0;
}
}
Expand Down
3 changes: 2 additions & 1 deletion ares/ps1/peripheral/peripheral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ struct Peripheral : Thread, Memory::Interface {
n16 baudrateReloadValue;

//internal
i32 counter;
i32 transferCounter;
i32 ackCounter;
} io;
};

Expand Down
3 changes: 2 additions & 1 deletion ares/ps1/peripheral/serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ auto Peripheral::serialize(serializer& s) -> void {
s(io.slotNumber);
s(io.unknownCtrl_14_15);
s(io.baudrateReloadValue);
s(io.counter);
s(io.transferCounter);
s(io.ackCounter);
}
2 changes: 1 addition & 1 deletion ares/ps1/system/serialization.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
static const string SerializerVersion = "v131";
static const string SerializerVersion = "v133";

auto System::serialize(bool synchronize) -> serializer {
serializer s;
Expand Down

0 comments on commit 631085f

Please sign in to comment.