// SPDX-License-Identifier: GPL-3.0-or-later #include "CoreChecks.h" #include "../../src/core/JoystickState.h" #include "../../src/core/MachineSession.h" #include "../../src/core/MachineHost.h" #include "../../src/core/mc6821.h" #include "../../src/core/CpuMemory.h" #include "../../src/core/mc6809.h" #include "../../src/core/hd6309.h" extern int JS_Ramp_Clock; #include using namespace VCC::Mac; namespace { void require(bool condition) { if (!condition) throw std::runtime_error("Joystick input contract failed"); } } void CheckJoystickComparator() { JoystickState state; state.set(0, 0, 16383, 3, 0); state.set(1, 1000, 2000, 3, 0); require(state.scan(0x7f, 0, 0, 0, false) == 0x70); // zero never compares high require(state.scan(0x7f, 1, 16383, 0, false) == 0xf0); require(state.scan(0x7f, 2, 1001, 0, false) == 0x70); require(state.scan(0x7f, 3, 2000, 0, false) == 0xf0); state.release(); require(state.scan(0x7f, 0, 8192, 0, false) == 0x7f); require(state.scan(0x7e, 0, 8191, 0, false) == 0xfe); // retain keyboard row state.set(0, 99999, 99999, 255, 99); require(state.ports[0].x == 16383 && state.ports[0].mode == 3 && state.ports[0].buttons == 3); } void CheckJoystickRamps() { for (bool fast : {false, true}) { JoystickState state; int clock = 123; state.set(0, 0, 16383, 0, 2); state.start(true, 1, 0, clock); require(clock == 123); state.start(true, 2, 0, clock); require(clock == 0); const int threshold = fast ? 1200 : 600; require(state.scan(0x7f, 0, 0, threshold, fast) == 0x7f); require(state.scan(0x7f, 0, 0, threshold + 1, fast) == 0xff); state.start(true, 2, 1, clock); const int max = (1200 + ((16383 * 37) >> 6)) / (fast ? 1 : 2); require(state.scan(0x7f, 1, 0, max, fast) == 0x7f); require(state.scan(0x7f, 1, 0, max + 1, fast) == 0xff); state.set(1, 0, 0, 0, 3); state.start(false, 0, 2, clock); require(state.scan(0x7f, 2, 0, 800, fast) == 0x7f); require(state.scan(0x7f, 2, 0, 801, fast) == 0xff); } } void CheckJoystickSession() { for (auto cpu : {Processor::MC6809, Processor::HD6309}) { MachineSession machine; machine.loadDiagnostic(cpu); machine.resume(); machine.setJoystick(0, 0, 0, 3, 0); machine.setJoystick(1, 0, 0, 3, 0); require(VCC::Machine().KeyboardScan(0xff) == 0x70); // Exercise the real PIA start/comparator and retained CPU cycle counter. MemWrite8(4, 0xff01); MemWrite8(4, 0xff03); MemWrite8(0xff, 0xff02); MemWrite8(4, 0xff21); // PA2..PA7 alone set the threshold, including at an exact six-bit boundary. machine.setJoystick(0, 26u << 8, 0, 0, 0); for (unsigned lowBits = 0; lowBits < 4; ++lowBits) { MemWrite8((26 << 2) | lowBits, 0xff20); require((MemRead8(0xff00) & 0x80) != 0); MemWrite8((27 << 2) | lowBits, 0xff20); require((MemRead8(0xff00) & 0x80) == 0); } machine.setJoystick(0, 0, 0, 0, 2); MemWrite8(2, 0xff20); require(JS_Ramp_Clock == 0); require((MemRead8(0xff00) & 0x80) == 0); // Authored RAM loop; neither CPU needs a proprietary ROM for this check. MemWrite8(0x20, 0x2000); MemWrite8(0xfe, 0x2001); if (cpu == Processor::MC6809) { MC6809ForcePC(0x2000); MC6809Exec(700); } else { HD6309ForcePC(0x2000); HD6309Exec(700); } require(JS_Ramp_Clock > 600 && (MemRead8(0xff00) & 0x80) != 0); machine.setJoystick(0, 0, 0, 0, 3); MemWrite8(0, 0xff00); require(JS_Ramp_Clock == 0 && (MemRead8(0xff00) & 0x80) == 0); JS_Ramp_Clock = 801; require((MemRead8(0xff00) & 0x80) != 0); machine.pause(); require((VCC::Machine().KeyboardScan(0xff) & 15) == 15); machine.setJoystick(0, 0, 0, 3, 0); require((VCC::Machine().KeyboardScan(0xff) & 15) == 15); machine.resume(); machine.setJoystick(0, 0, 0, 3, 0); machine.softReset(); require((VCC::Machine().KeyboardScan(0xff) & 15) == 15); machine.setJoystick(0, 0, 0, 3, 0); machine.reset(); require((VCC::Machine().KeyboardScan(0xff) & 15) == 15); // Normal guest instructions configure the PIA and stop immediately // before reading it. Resume must apply the complete host input before // that first instruction, not one CPU slice later. const std::vector code = { 0x86,0x04, 0xb7,0xff,0x01, 0xb7,0xff,0x03, 0xb7,0xff,0x21, 0x86,0xff, 0xb7,0xff,0x02, 0x86,0x80, 0xb7,0xff,0x20, 0xb6,0xff,0x00, 0xb7,0x30,0x00, 0x20,0xfe }; std::vector binary = {0, 0, uint8_t(code.size()), 0x20, 0}; binary.insert(binary.end(), code.begin(), code.end()); binary.insert(binary.end(), {0xff, 0, 0, 0x20, 0}); machine.debuggerLoadBinary(binary, std::vector{0x2015, 0x201b}); machine.advance(); require(machine.snapshot().state == RunState::Paused && machine.snapshot().cpu.PC == 0x2015); const std::array inputs = {{{16383, 1234, 1, 0}, {0, 8191, 2, 0}}}; machine.resumeWithInput(1, inputs); const auto held = machine.snapshot(); require(held.keyboardHeld && held.joysticks[0].x == 16383 && held.joysticks[0].y == 1234 && held.joysticks[0].buttons == 1 && held.joysticks[1].buttons == 2); machine.advance(); const auto stopped = machine.snapshot(); require(stopped.state == RunState::Paused && stopped.cpu.PC == 0x201b); const auto readback = machine.debuggerSnapshot(0x3000, 1); require(readback.memoryAvailable[0] && (readback.memory[0] & 0x81) == 0x80); machine.releaseInputs(); const auto released = machine.snapshot(); require(!released.keyboardHeld && released.state == stopped.state && released.cpu.PC == stopped.cpu.PC && released.frames == stopped.frames && released.cycles == stopped.cycles && released.generation == stopped.generation); for (const auto& port : released.joysticks) require(port.x == 8191 && port.y == 8191 && port.buttons == 0); require(held.keyboardHeld && held.joysticks[0].buttons == 1); // The prior snapshot is an immutable value. auto invalid = inputs; invalid[1].x = 16384; try { machine.resumeWithInput(0, invalid); require(false); } catch (const std::invalid_argument&) {} require(machine.snapshot().state == RunState::Paused && machine.snapshot().cycles == stopped.cycles); // A pending interrupt can stop at a different breakpoint inside the // first resumed slice. Preserve the actual input applied before that // slice even though pausing correctly neutralizes the live controls. std::vector rom(32768, 0x12); rom[0x7ff8] = 0x21; rom[0x7ff9] = 0; // IRQ vector -> authored RAM handler rom[0x7ffe] = 0x20; rom[0x7fff] = 0; machine.load(rom, cpu); const std::vector interruptBinary = { 0,0,9,0x20,0, 0x10,0xce,0x70,0,0x1c,0xaf,0x12,0x20,0xfe, 0,0,1,0x21,0, 0x3b, 0xff,0,0,0x20,0 }; machine.debuggerLoadBinary(interruptBinary, std::vector{0x2006, 0x2100}); machine.advance(); require(machine.snapshot().state == RunState::Paused && machine.snapshot().cpu.PC == 0x2006); VCC::Machine().AssertInterrupt(IS_GIME, INT_IRQ); const auto staged = machine.resumeWithInput(1, inputs); const auto interrupted = machine.snapshot(); require(staged.state == RunState::Running && staged.keyboardHeld && staged.joysticks[0].buttons == 1 && staged.joysticks[0].x == 16383 && staged.joysticks[1].buttons == 2); require(interrupted.state == RunState::Paused && interrupted.cpu.PC == 0x2100 && !interrupted.keyboardHeld); for (const auto& port : interrupted.joysticks) require(port.x == 8191 && port.y == 8191 && port.buttons == 0); VCC::Machine().ClearInterrupt(IS_GIME, INT_IRQ); } }