Skip to content

Commit 9baa469

Browse files
mattgodboltclaude
andcommitted
Fix VIA port B data direction register handling for joystick fire buttons
- Correctly handle DDR in recalculatePortBPins(): output pins use ORB values, input pins default high - Fix inverted button logic: clear pin bits when buttons are pressed (active low) - Remove redundant DDR masking that was preventing proper input pin reads - This fixes ADVAL(0) not detecting mouse joystick fire button presses The original code wasn't respecting the Data Direction Register, causing joystick fire button states to not be readable by BBC BASIC's ADVAL(0) function. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 232f96d commit 9baa469

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/via.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ class Via {
369369
portBUpdated() {}
370370

371371
getJoysticks() {
372-
return { button1: true, button2: true };
372+
return { button1: false, button2: false };
373373
}
374374

375375
recalculatePortAPins() {
@@ -381,18 +381,16 @@ class Via {
381381

382382
recalculatePortBPins() {
383383
const prevPb6 = !!(this.portbpins & 0x40);
384-
this.portbpins = this.orb & this.ddrb;
385-
const buttons = this.getJoysticks();
384+
// Start with output pins, and inputs all high
385+
this.portbpins = (this.orb & this.ddrb) | (~this.ddrb & 0xff);
386386

387387
// AUG p418
388388
// ### PB4 and PB5 inputs
389-
// These are the inputs from the joystick FIRE buttons. They are
390-
// normally at logic 1 with no button pressed and change to 0 when a
391-
// button is pressed
392-
if (!buttons.button1) this.portbpins |= 1 << 4;
393-
if (!buttons.button2) this.portbpins |= 1 << 5;
389+
// These are the inputs from the joystick FIRE buttons. They are active low.
390+
const buttons = this.getJoysticks();
391+
if (buttons.button1) this.portbpins &= ~(1 << 4); // Clear PB4 if button1 pressed
392+
if (buttons.button2) this.portbpins &= ~(1 << 5); // Clear PB5 if button2 pressed
394393

395-
this.portbpins |= ~this.ddrb & 0xff;
396394
this.drivePortB();
397395
if (prevPb6 && !(this.portbpins & 0x40)) {
398396
// If we see a high to low transition on pb6, and we are in timer2 pulse counting mode, count a pulse.

0 commit comments

Comments
 (0)