Skip to content

Commit

Permalink
Count pulses on PB6 (#451)
Browse files Browse the repository at this point in the history
Fixes #268
  • Loading branch information
mattgodbolt authored Sep 20, 2024
1 parent e035db5 commit 7d38da1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
61 changes: 60 additions & 1 deletion tests/integration/via.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const resultAddress = 0x100;
function expectArray(testMachine, array) {
let addr = resultAddress;
for (const expected of array) {
assert.equal(testMachine.readbyte(addr), expected);
assert.equal(testMachine.readbyte(addr), expected, `mismatch at 0x${addr.toString(16)}`);
addr++;
}
}
Expand Down Expand Up @@ -1063,4 +1063,63 @@ PRINT ?(R%)
PRINT ?(R%+1)`);
expectArray(testMachine, [251, 249]);
});

// @tom-seddon reported this one:
it("T.VIA.PB6 - VIA T2 doesn't count manually-induced PB6 pulses", async function () {
const testMachine = await runViaProgram(`
REM DISABLE IRQS
N%=0
R%=${resultAddress}
?&FE6E=&7F
?&FE6D=&7F
:
REM T2 PULSE COUNTING MODE
?&FE6B=?&FE6B OR&20
:
REM RESET T2
?&FE68=100
?&FE69=0
:
REM PB OUTPUTS
:
PROCP
PRINT"TOGGLE IN OUTPUT MODE"
?&FE62=0
?&FE6F=&40
PROCP
?&FE62=&FF
PROCP
?&FE6F=&40
PROCP
?&FE6F=&00
PROCP
PRINT"TOGGLE VIA DDRB"
?&FE62=0
PROCP
?&FE62=&FF
PROCP
?&FE62=0
PROCP
END
:
DEFPROCP:LOCALT2%
T2%=?&FE69*256+?&FE68
R%!N%=T2%
N%=N%+2
PRINT"T2=";T2%" (&";~T2%")"
ENDPROC`);
// Expected output:
// T2=100 (&64)
// TOGGLE IN OUTPUT MODE
// T2=100 (&64)
// T2=99 (&63)
// T2=99 (&63)
// T2=99 (&63)
// TOGGLE VIA DDRB
// T2=99 (&63)
// T2=98 (&62)
// T2=98 (&62)
expectArray(testMachine, [100, 0, 100, 0, 99, 0, 99, 0, 99, 0, 99, 0, 98, 0, 98, 0]);
});
// TODO: write more pb6 pulse tests, e.g. interrupt behaviour and underflow, and run on a real bbc.
});
16 changes: 16 additions & 0 deletions via.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ class Via {
}

recalculatePortBPins() {
const prevPb6 = !!(this.portbpins & 0x40);
this.portbpins = this.orb & this.ddrb;
const buttons = this.getJoysticks();

Expand All @@ -363,6 +364,21 @@ class Via {

this.portbpins |= ~this.ddrb & 0xff;
this.drivePortB();
if (prevPb6 && !(this.portbpins & 0x40)) {
// If we see a high to low transition on pb6, and we are in timer2 pulse counting mode, count a pulse.
if (this.acr & 0x20) {
this.t2c -= 2;
// Not clear what happens here. Docs say:
// "When the T2 counter reaches a count of zero, IFR5 is set and the counter continues to decrement with
// each pulse on PB6. To enable IFR5 for subsequent countdowns, it is necessary to reload high order T2
// counter."
if (this.t2c < 0) {
this.t2c = 0xffff;
this.ifr |= TIMER2INT;
this.updateIFR();
}
}
}
this.portBUpdated();
}

Expand Down

0 comments on commit 7d38da1

Please sign in to comment.