Skip to content

Commit

Permalink
Fix starting t-state when loading Z80 snaps
Browse files Browse the repository at this point in the history
  • Loading branch information
MartianGirl committed Jun 22, 2024
1 parent 4f3e33b commit 6b5cb81
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
24 changes: 13 additions & 11 deletions source/src/Spectrum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1178,28 +1178,30 @@ void Spectrum::loadState(SaveState const& state) {
z80.startInstruction();
z80.state = Z80State::ST_OCF_T4L_RFSH2;

ula.scan = 0;
ula.pixel = 1;
ula.z80Clock = true;
while (ula.scan != ula.vSyncStart || ula.pixel != ula.interruptEnd) {
ula.clock();
}
} else if (state.type == SnapType::SNA_128) {
ula.scan = 0;
ula.pixel = 1;
ula.z80Clock = true;
while (ula.scan != ula.vSyncStart || ula.pixel != ula.interruptEnd) {
ula.clock();
}

z80.pc.w = state.pc;
z80.state = Z80State::ST_OCF_T1H_ADDRWR;
} else {

uint32_t tStatesHi = (state.tStates / 0x10000) % 4;
uint32_t tStatesLo = state.tStates % 0x10000;
uint32_t cyclesPerArea = (ula.maxScan * ula.checkPointValues[ula.ulaVersion][5]) / 8;

ula.scan = ula.vSyncStart;
ula.pixel = ula.interruptStart | 1;
ula.z80Clock = true;
// First, advance the ULA until "just after the ULA generates its once-in-every-20-ms interrupt".
while (ula.scan != ula.vSyncStart || ula.pixel != ula.interruptEnd) {
ula.clock();
}

// If tStatesLo has a valid value (not higher than the number to total T-states divided by 4)...
if (tStatesLo <= cyclesPerArea) {
uint32_t cycles = cyclesPerArea * (3 - tStatesHi) + 2 * (cyclesPerArea - tStatesLo) - 1;
// We have to advance the ULA this number of cycles
uint32_t cycles = cyclesPerArea * ((tStatesHi + 1) % 4) + 2 * (cyclesPerArea - tStatesLo);
for (uint32_t ii = 0; ii < cycles; ++ii) {
ula.clock();
}
Expand Down
4 changes: 2 additions & 2 deletions source/src/Z80File.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ bool Z80File::parseHeader() {
} else {
state.pc = fileData[32] + 0x100 * fileData[33];
}

state.sp = fileData[8] + 0x100 * fileData[9];
state.ir = ((fileData[11] & 0x7F) | ((fileData[12] & 0x1) << 7)) + 0x100 * fileData[10];

Expand Down Expand Up @@ -153,7 +153,7 @@ bool Z80File::parseHeader() {
case 0: // fall-through
case 1: // fall-through
case 2: // fall-through
case 3:
case 3:
state.model = state.issue2 ? SnapshotModel::ZX_48K_ISSUE2 : SnapshotModel::ZX_48K_ISSUE3;
break;
case 4: // fall-through
Expand Down

0 comments on commit 6b5cb81

Please sign in to comment.