diff --git a/apple/mouse.cpp b/apple/mouse.cpp index 7d56609..f6e43ba 100644 --- a/apple/mouse.cpp +++ b/apple/mouse.cpp @@ -66,21 +66,25 @@ uint8_t Mouse::readSwitches(uint8_t s) void Mouse::writeSwitches(uint8_t s, uint8_t v) { switch (s) { + /* Many of these were designed to be reads, because they don't have to + * modify any state inside the VM directly -- but it's important (per docs) + * that we return A, X, and Y as they were when we were called. So these + * are all now writes, which don't modify A/X/Y. */ case SW_R_HOMEMOUSE: - g_mouse->setPosition( (g_ram.readByte(0x578) << 8) | g_ram.readByte(0x478), - (g_ram.readByte(0x5F8) << 8) | g_ram.readByte(0x4F8) + g_mouse->setPosition( (g_vm->getMMU()->read(0x578) << 8) | g_vm->getMMU()->read(0x478), + (g_vm->getMMU()->read(0x5F8) << 8) | g_vm->getMMU()->read(0x4F8) ); break; case SW_R_POSMOUSE: - g_mouse->setPosition( (g_ram.readByte(0x578+4) << 8) | g_ram.readByte(0x478+4), - (g_ram.readByte(0x5F8+4) << 8) | g_ram.readByte(0x4F8+4) + g_mouse->setPosition( (g_vm->getMMU()->read(0x578+4) << 8) | g_vm->getMMU()->read(0x478+4), + (g_vm->getMMU()->read(0x5F8+4) << 8) | g_vm->getMMU()->read(0x4F8+4) ); break; case SW_R_CLEARMOUSE: - g_ram.writeByte(0x578+4, 0); - g_ram.writeByte(0x478+4, 0); - g_ram.writeByte(0x5F8+4, 0); - g_ram.writeByte(0x4F8+4, 0); + g_vm->getMMU()->write(0x578+4, 0); + g_vm->getMMU()->write(0x478+4, 0); + g_vm->getMMU()->write(0x5F8+4, 0); + g_vm->getMMU()->write(0x4F8+4, 0); g_mouse->setPosition(0,0); break; case SW_R_READMOUSE: @@ -92,25 +96,22 @@ void Mouse::writeSwitches(uint8_t s, uint8_t v) lastX = xpos; lastY = ypos; } curButton = g_mouse->getButton(); - uint8_t newStatus = g_ram.readByte(0x778+4) & ~0xC0; + uint8_t newStatus = g_vm->getMMU()->read(0x778+4) & ~0xC0; if (curButton) { newStatus |= 0x80; }; if (lastButton) { newStatus |= 0x40; }; - uint16_t xv = xpos >> 8; xv &= 0xFF; - printf("XPOS: %d => 0x%X 0x%X\n", xpos, xv, xpos & 0xFF); - - g_ram.writeByte(0x578+4, xv); // high X - g_ram.writeByte(0x478+4, xpos & 0xFF); // low X - g_ram.writeByte(0x5F8+4, (ypos >> 8) & 0xFF); // high Y - g_ram.writeByte(0x4F8+4, ypos); // low Y + g_vm->getMMU()->write(0x578+4, (xpos >> 8) & 0xFF); // high X + g_vm->getMMU()->write(0x478+4, xpos & 0xFF); // low X + g_vm->getMMU()->write(0x5F8+4, (ypos >> 8) & 0xFF); // high Y + g_vm->getMMU()->write(0x4F8+4, ypos); // low Y } break; case SW_R_INITMOUSE: // Set clamp to (0,0) - (1023,1023) - g_ram.writeByte(0x578, 0); // high of lowclamp - g_ram.writeByte(0x478, 0); // low of lowclamp - g_ram.writeByte(0x5F8, 0x03); // high of highclamp - g_ram.writeByte(0x4F8, 0xFF); // low of highclamp + g_vm->getMMU()->write(0x578, 0); // high of lowclamp + g_vm->getMMU()->write(0x478, 0); // low of lowclamp + g_vm->getMMU()->write(0x5F8, 0x03); // high of highclamp + g_vm->getMMU()->write(0x4F8, 0xFF); // low of highclamp g_mouse->setClamp(XCLAMP, 0, 1023); g_mouse->setClamp(YCLAMP, 0, 1023); break; @@ -120,20 +121,20 @@ void Mouse::writeSwitches(uint8_t s, uint8_t v) interruptsTriggered |= 0x80; lastButton = curButton; } - g_ram.writeByte(0x778+4, interruptsTriggered); - g_ram.writeByte(0x6B8+4, interruptsTriggered); // hack to appease ROM + g_vm->getMMU()->write(0x778+4, interruptsTriggered); + g_vm->getMMU()->write(0x6B8+4, interruptsTriggered); // hack to appease ROM interruptsTriggered = 0; break; case SW_W_INIT: v &= 0x03; // just the low 3 bits apparently? printf("Simple init: value is 0x%X\n", v); status = v; - g_ram.writeByte(0x7f8 + 4, v); + g_vm->getMMU()->write(0x7f8 + 4, v); break; case SW_W_CLAMPMOUSE: { - uint16_t lowval = (g_ram.readByte(0x578) << 8) | (g_ram.readByte(0x478)); - uint16_t highval = (g_ram.readByte(0x5F8) << 8) | (g_ram.readByte(0x4F8)); + uint16_t lowval = (g_vm->getMMU()->read(0x578) << 8) | (g_vm->getMMU()->read(0x478)); + uint16_t highval = (g_vm->getMMU()->read(0x5F8) << 8) | (g_vm->getMMU()->read(0x4F8)); if (v) { g_mouse->setClamp(YCLAMP, lowval, highval); } else { @@ -144,7 +145,7 @@ void Mouse::writeSwitches(uint8_t s, uint8_t v) break; case SW_W_SETMOUSE: status = v; - g_ram.writeByte(0x7f8 + 4, v); + g_vm->getMMU()->write(0x7f8 + 4, v); break; default: printf("mouse: unknown switch write 0x%X = 0x%2X\n", s, v); @@ -154,250 +155,121 @@ void Mouse::writeSwitches(uint8_t s, uint8_t v) void Mouse::loadROM(uint8_t *toWhere) { - /* Actual mouse ROM Disassembly: -C400- 2C 58 FF BIT $FF58 ; test bits in FF58 w/ A -C403- 70 1B BVS $C420 ; V will contain bit 6 from $FF58, which should be $60 on boot-up (RTS), which has $40 set, so should branch here -C405- 38 SEC -C406- 90 18 BCC $C420 ; no-op; unless called @ $C406 directly? - -C408- B8 CLV ; clear overflow -C409- 50 15 BVC $C420 ; always branches - -; unused... ? more lookup table... ? -C40B- 01 20 ORA ($20,X) -C40D- AE AE AE LDX $AEAE - -; This is the lookup table for entry addresses -C410- AE -C411- 00 -C412- 6D ;(setmouse @ c46d) -C413- 75 ;(servemouse @ C475) -C414- 8E ;(readmouse @ C48E) -C415- 9F ;(clearmouse @ C49F) -C416- A4 ;(posmouse @ C4A4) -C417- 86 ;(clampmouse @ C486) -C418- A9 ;(homemouse @ C4A9) -C419- 97 ;(initmouse @ C497) -C41A- AE -C41B- AE -C41C- AE ;(semi-documented: sets mouse frequency handler to 60 or 50 hz) -C41D- AE -C41E- AE -C41F- AE - -; Main (installs KSW for IN# handling) -C420- 48 PHA ; push accumulator to stack -C421- 98 TYA -C422- 48 PHA ; push Y to stack -C423- 8A TXA -C424- 48 PHA ; push X to stack -C425- 08 PHP ; push status to stack -C426- 78 SEI ; disable interrupts - -; This JSR $FF58 is a trick to get the address we're calling from. By -; default, $FF58 contains a RTS (until patched as the DOS '&' vector, -; or ProDOS does something similar). As long as this executes before -; the DOS takes over, it's safe; but if we're doing this after ProDOS -; loads, a 64k machine might have garbage after the language card RAM -; is loaded here. A safer alternative would be something like -; LDA #$60 ; Write an RTS ($60) to a temporary memory -; STA WORK ; address, and then -; JSR WORK ; jump to it -; TSX -; LDA $100, X ; grab the return address off the stack - -C427- 20 58 FF JSR $FF58 ; call something that will RTS -C42A- BA TSX ; pull stack pointer to X -C42B- BD 00 01 LDA $0100,X ; grab A from the current stack pointer, which has our return addr -C42E- AA TAX ; X = return addr -C42F- 0A ASL -C430- 0A ASL -C431- 0A ASL -C432- 0A ASL -C433- A8 TAY ; Y = (return addr) << 4 -C434- 28 PLP ; restore status from stack (includes V,C) -C435- 50 0F BVC $C446 ; overflow is clear from when we were called? -C437- A5 38 LDA $38 ; >> $38 is the IN# vector ("KSW") low byte -C439- D0 0D BNE $C448 ; restore stack & return -C43B- 8A TXA -C43C- 45 39 EOR $39 ; >> KSW high byte -C43E- D0 08 BNE $C448 ; restore stack & return -C440- A9 05 LDA #$05 -C442- 85 38 STA $38 ; ($38) = $05 -C444- D0 0B BNE $C451 - -;; we wind up here when the entry vector $c405 is called directly (from $9eba) -C446- B0 09 BCS $C451 ; carry set if ... ? - -;; entry point when called as PR# so BASIC/DOS can init the mouse -C448- 68 PLA ; pull X from stack -C449- AA TAX -C44A- 68 PLA ; pull Y from stack, but -C44B- EA NOP ; throw it away -C44C- 68 PLA ; pull A from stack -C44D- 99 80 C0 STA $C080,Y ; LC RAM bank2, read and wr-protect -C450- 60 RTS - -C451- 99 81 C0 STA $C081,Y ; LC RAM bank 2, read ROM -C454- 68 PLA ; -C455- BD 38 06 LDA $0638,X -C458- AA TAX -C459- 68 PLA -C45A- A8 TAY -C45B- 68 PLA -C45C- BD 00 02 LDA $0200,X ; keyboard character buffer? -C45F- 60 RTS - -C460- 00 BRK -C461- 00 BRK -C462- 00 BRK -C463- 00 BRK -C464- 00 BRK -C465- 00 BRK -C466- 00 BRK -C467- 00 BRK -C468- 00 BRK -C469- 00 BRK -C46A- 00 BRK -C46B- 00 BRK -C46C- 00 BRK - -; SetMouse -C46D- C9 10 CMP #$10 ; interrupt on VBL + interrupt on mouse move? -C46F- B0 3F BCS $C4B0 ; RTS if A >= 0x10 -C471- 99 82 C0 STA $C082,Y ; LC RAM bank 2, read ROM, wr-protect RAM -C474- 60 RTS -; ServeMouse -C475- 48 PHA ; save A -C476- 18 CLC -C477- 90 39 BCC $C4B2 ; always branches -; ServeMouse cleanup and exit -C479- 99 83 C0 STA $C083,Y ; LC RAM bank 2, read RAM -C47C- BD B8 06 LDA $06B8,X ; apparently this is where the new status wound up, also $7f8 + -C47F- 29 0E AND #$0E -C481- D0 01 BNE $C484 ; if the interrupt bits don't show it was a mouse interrupt, set error (C) before returning -C483- 38 SEC -C484- 68 PLA ; restore A -C485- 60 RTS -; ClampMouse -C486- C9 02 CMP #$02 -C488- B0 26 BCS $C4B0 ; RTS if 2 <= the A register -C48A- 99 83 C0 STA $C083,Y ; LC RAM bank 2, read RAM -C48D- 60 RTS -; ReadMouse -C48E- A9 04 LDA #$04 -C490- 99 83 C0 STA $C083,Y ; LC RAM bank 2, read RAM -C493- 18 CLC -C494- EA NOP -C495- EA NOP -C496- 60 RTS -; InitMouse -C497- EA NOP -C498- A9 02 LDA #$02 -C49A- 99 83 C0 STA $C083,Y ; LC RAM bank 2, read RAM -C49D- 18 CLC -C49E- 60 RTS -; ClearMouse -C49F- EA NOP -C4A0- A9 05 LDA #$05 -C4A2- D0 F6 BNE $C49A -; PosMouse -C4A4- EA NOP -C4A5- A9 06 LDA #$06 -C4A7- D0 F1 BNE $C49A -; HomeMouse -C4A9- EA NOP -C4AA- A9 07 LDA #$07 -C4AC- D0 EC BNE $C49A -; "TimeData" to set freq before init to 50/60 Hz? Doesn't look like it... -C4AE- A2 03 LDX #$03 -C4B0- 38 SEC -C4B1- 60 RTS -; ServeMouse main worker -C4B2- 08 PHP -C4B3- A5 00 LDA $00 -C4B5- 48 PHA -C4B6- A9 60 LDA #$60 -C4B8- 85 00 STA $00 -C4BA- 78 SEI -C4BB- 20 00 00 JSR $0000 -C4BE- BA TSX -C4BF- 68 PLA -C4C0- 85 00 STA $00 -C4C2- BD 00 01 LDA $0100,X -C4C5- 28 PLP -C4C6- AA TAX -C4C7- 0A ASL -C4C8- 0A ASL -C4C9- 0A ASL -C4CA- 0A ASL -C4CB- A8 TAY -C4CC- A9 03 LDA #$03 -C4CE- 18 CLC -C4CF- 90 A8 BCC $C479 ; always branches - -... but the version below is patched so it uses soft switches to call -back here. - -SETMOUSE: -C471- 8D CF C0 STA $C0CF ; use soft switch 0xF to handle it - 60 RTS - -Patch: C471 from 99 82 C0 60 => 8D CF C0 60 - -SERVEMOUSE @ $C4B2: - 78 SEI - AD CE C0 LDA $C0CE ; use soft switch 0xE (read) to trigger this - expect new value to be placed in $7f8+4 and $6b8+4 - A2 04 LDX #$04 ; our slot number, for cleanup code - 18 CLC - 90 C1 BCC $C47C - -Patch: C4B2 from 08 A5 00 48 A9 60 85 00 78 => - 78 AD CE C0 A2 04 18 90 C1 - -READMOUSE -C48E- AD CB C0 LDA $C0CB ; soft switch 0x0B for readmouse - 18 CLC - 60 RTS - -Patch: from A9 04 99 83 C0 => - AD CB C0 18 60 - -CLEARMOUSE -C49F- AD CA C0 LDA $C0CA ; soft switch 0x0A for clearmouse - 18 CLC - 60 RTS - -Patch: from EA A9 05 D0 F6 => - AD CA C0 18 60 -POSMOUSE -C4a4- AD C9 C0 LDA $C0C9 ; soft switch 0x09 for posmouse - 18 CLC - 60 RTS - -Patch: from EA A9 06 D0 F1 => - AD C9 C0 18 60 - -CLAMPMOUSE -C48A- 8D CD C0 STA $C04D ; use write to soft switch 0x0D to trigger clamp - 60 RTS - -Patch: from 99 83 C0 60 => 8D CD C0 60 - -HOMEMOUSE -C4A9- AD C8 C0 LDA $c048 ; soft switch 0x08 for homemouse (read) - 18 CLC - 60 RTS - -Patch: from EA A9 07 D0 EC => AD C8 C0 18 60 - -INITMOUSE -C498- AD CC C0 LDA $C04C ; soft switch 0x0C read for initmouse - 18 CLC - 60 RTS - -Patch: from A9 02 99 83 C0 => AD CC C0 18 60 - */ + /* This is a custom-built ROM which hands off control to the C++ code via + * soft switch writes. It's hard-coded to work with the mouse in Slot 4. + * + * ; $c400 is the entry point for PR#4 + * $C400 2C 58 FF BIT $FF58 + * $C403 70 1B BVS $C420 + * ; $c405 is the entry point for IN#4 when we set KSW + * $C405 38 SEC + * $C406 90 18 BCC $C420 + * $C408 B8 CLV + * $C409 50 15 BVC $C420 ; always branch + * $C40B 01 20 8D 8D 8D ; data (ID bytes & such) + * $C410 8D 00 60 68 76 7B 80 85 ; data (lookup table of entrypoints) + * $C418 8F 94 8D 8D 8D 8D 8D 8D ; data (lookup table of entrypoints) + * ; $c420 is the I/O handler + * $C420 48 PHA + * $C421 98 TYA + * $C422 48 PHA + * $C423 8A TXA + * $C424 48 PHA + * $C425 08 PHP + * $C426 78 SEI + * $C427 20 58 FF JSR $FF58 + * $C42A BA TSX + * $C42B BD 00 01 LDA $100,X + * $C42E AA TAX + * $C42F 0A ASL + * $C430 0A ASL + * $C431 0A ASL + * $C432 0A ASL + * $C433 A8 TAY + * $C434 28 PLP + * $C435 50 0F BVC $C446 + * $C437 A5 38 LDA $0 + * $C439 D0 0D BNE $C448 + * $C43B 8A TXA + * $C43C 45 39 EOR $0 + * $C43E D0 08 BNE $C448 + * $C440 A9 05 LDA #$0 + * $C442 85 38 STA $0 + * $C444 D0 0B BNE $C451 + * $C446 B0 09 BCS $C451 + * $C448 68 PLA + * $C449 AA TAX + * $C44A 68 PLA + * $C44B EA NOP + * $C44C 68 PLA + * $C44D EA NOP + * $C44E EA NOP + * $C44F EA NOP + * $C450 60 RTS + * $C451 EA NOP + * $C452 EA NOP + * $C453 EA NOP + * $C454 68 PLA + * $C455 BD 38 06 LDA $638,X + * $C458 AA TAX + * $C459 68 PLA + * $C45A A8 TAY + * $C45B 68 PLA + * $C45C BD 00 02 LDA $200,X + * $C45F 60 RTS + * ; $c460 is SetMouse + * $C460 C9 10 CMP #$0 + * $C462 B0 29 BCS $C48D + * $C464 8D CF C0 STA $C0CF + * $C467 60 RTS + * ; $c468 is ServeMouse + * $C468 48 PHA + * $C469 18 CLC + * $C46A 90 2D BCC $C499 ; jump to ServeMouseWorker + * ; $c46c is ServeMouseExit + * $C46C BD B8 06 LDA $6B8,X + * $C46F 29 0E AND #$0 + * $C471 D0 01 BNE $C474 + * $C473 38 SEC + * $C474 68 PLA + * $C475 60 RTS + * ; $c476 is ReadMouse + * $C476 8D CB C0 STA $C0CB + * $C479 18 CLC + * $C47A 60 RTS + * ; $c47b is ClearMouse + * $C47B 8D CA C0 STA $C0CA + * $C47E 18 CLC + * $C47F 60 RTS + * ; $c480 is PosMouse + * $C480 8D C9 C0 STA $C0C9 + * $C483 18 CLC + * $C484 60 RTS + * ; $c485 is ClampMouse + * $C485 C9 02 CMP #$0 + * $C487 B0 04 BCS $C48D + * $C489 8D CD C0 STA $C0CD + * $C48C 60 RTS + * ; $c48d is an error exit point + * $C48D 38 SEC + * $C48E 60 RTS + * ; $c48f is HomeMouse + * $C48F 8D C8 C0 STA $C0C8 + * $C492 18 CLC + * $C493 60 RTS + * ; $c494 is InitMouse + * $C494 8D CC C0 STA $C0CC + * $C497 18 CLC + * $C498 60 RTS + * ; $c499 is ServeMouse + * $C499 78 SEI + * $C49A 8D CE C0 STA $C0CE + * $C49D A2 04 LDX #$0 + * $C49F 18 CLC + * $C4A0 90 CA BCC $C46C + * ; $C4A2..C4FA is dead space (all $FF) + * $C4FB D6 FF FF FF 01 ; data (ID bytes) + */ uint8_t rom[256] = { 0x2c, 0x58, 0xff, 0x70, 0x1B, 0x38, 0x90, 0x18, // C400 0xb8, 0x50, 0x15, 0x01, 0x20, 0x8d, 0x8d, 0x8d, @@ -442,12 +314,8 @@ bool Mouse::hasExtendedRom() void Mouse::loadExtendedRom(uint8_t *toWhere, uint16_t byteOffset) { -#if 0 - printf("loading extended rom for the mouse\n"); - for (int i=0; i<256; i++) { - toWhere[i] = romData[i + byteOffset]; - } -#endif + // There's no extended ROM needed, b/c we do the extended ROM work + // directly in C++ } void Mouse::maintainMouse(int64_t cycleCount)