Skip to content

Commit

Permalink
Experimental and non-working VIA implementation using the scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgodbolt committed Aug 11, 2024
1 parent 971eca7 commit 2c67629
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 28 deletions.
7 changes: 4 additions & 3 deletions 6502.js
Original file line number Diff line number Diff line change
Expand Up @@ -1155,14 +1155,15 @@ export class Cpu6502 extends Base6502 {
this.soundChip.setScheduler(this.scheduler);
this.sysvia = new via.SysVia(
this,
this.scheduler,
this.video,
this.soundChip,
this.cmos,
this.model.isMaster,
this.config.keyLayout,
this.config.getGamepads,
);
this.uservia = new via.UserVia(this, this.model.isMaster, this.config.userPort);
this.uservia = new via.UserVia(this, this.scheduler, this.model.isMaster, this.config.userPort);
if (this.config.printerPort) this.uservia.ca2changecallback = this.config.printerPort.outputStrobe;
this.touchScreen = new TouchScreen(this.scheduler);
this.acia = new Acia(this, this.soundChip.toneGenerator, this.scheduler, this.touchScreen);
Expand Down Expand Up @@ -1215,8 +1216,8 @@ export class Cpu6502 extends Base6502 {

// Common between polltimeSlow and polltimeFast
polltimeCommon(cycles) {
this.sysvia.polltime(cycles);
this.uservia.polltime(cycles);
// this.sysvia.polltime(cycles);
// this.uservia.polltime(cycles);
this.scheduler.polltime(cycles);
this.tube.execute(cycles);
if (this.teletextAdaptor) this.teletextAdaptor.polltime(cycles);
Expand Down
79 changes: 54 additions & 25 deletions via.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ const ORB = 0x0,
INT_CB2 = 0x08;

class Via {
constructor(cpu, irq) {
constructor(cpu, scheduler, irq) {
this.cpu = cpu;
this.irq = irq;
this.scheduler = scheduler;

this.ora = 0;
this.orb = 0;
Expand Down Expand Up @@ -56,6 +57,9 @@ class Via {
this.cb2changecallback = null;
this.justhit = 0;
this.t1_pb7 = 0;

this.task = this.scheduler.newTask(() => this._onTimeout());
this.lastPolltime = 0;
}

reset() {
Expand All @@ -68,41 +72,62 @@ class Via {
this.t1hit = this.t2hit = true;
this.acr = this.pcr = 0;
this.t1_pb7 = 1;
this.updateNextTime();
}

updateNextTime() {
let nextTimer = this.t1c;
if (!(this.acr & 0x20)) nextTimer = Math.min(this.t2c, nextTimer);
this.task.reschedule(Math.max(1, nextTimer));
}

_onTimeout() {
this._catchUp();
}

polltime(cycles) {
_catchUp() {
this._polltime(this.scheduler.epoch - this.lastPolltime);
this.lastPolltime = this.scheduler.epoch;
this.updateNextTime();
}

_polltime(cycles) {
cycles |= 0;
this.justhit = 0;
let newT1c = this.t1c - cycles;
const newT1c = this.t1c - cycles;
if (newT1c < -2) this.t1c = this._handleT1c(newT1c);
else this.t1c = newT1c;

if (!(this.acr & 0x20)) {
const newT2c = this.t2c - cycles;
if (newT2c < -2) this.t2c = this._handleT2c(newT2c);
else this.t2c = newT2c;
}
}

_handleT1c(newT1c) {
if (newT1c < -2 && this.t1c > -3) {
if (!this.t1hit) {
this.ifr |= TIMER1INT;
this.updateIFR();
if (newT1c === -3) {
this.justhit |= 1;
}
if (newT1c === -3) this.justhit |= 1;
this.t1_pb7 = !this.t1_pb7;
}
if (!(this.acr & 0x40)) this.t1hit = true;
}
while (newT1c < -3) newT1c += this.t1l + 4;
this.t1c = newT1c;
return newT1c;
}

if (!(this.acr & 0x20)) {
let newT2c = this.t2c - cycles;
if (newT2c < -2) {
if (!this.t2hit) {
this.ifr |= TIMER2INT;
this.updateIFR();
if (newT2c === -3) {
this.justhit |= 2;
}
this.t2hit = true;
}
newT2c += 0x20000;
}
this.t2c = newT2c;
_handleT2c(newT2c) {
if (!this.t2hit) {
this.ifr |= TIMER2INT;
this.updateIFR();
if (newT2c === -3) this.justhit |= 2;
this.t2hit = true;
}
newT2c += 0x20000;
return newT2c;
}

updateIFR() {
Expand All @@ -116,6 +141,7 @@ class Via {
}

write(addr, val) {
this._catchUp();
let mode;
val |= 0;
switch (addr & 0xf) {
Expand Down Expand Up @@ -216,6 +242,7 @@ class Via {
this.updateIFR();
}
this.t1_pb7 = 0;
this.updateNextTime();
break;

case T2CL:
Expand All @@ -233,6 +260,7 @@ class Via {
this.updateIFR();
}
this.t2hit = false;
this.updateNextTime();
break;

case IER:
Expand All @@ -251,6 +279,7 @@ class Via {
}

read(addr) {
this._catchUp();
switch (addr & 0xf) {
case ORA:
this.ifr &= ~INT_CA1;
Expand Down Expand Up @@ -431,8 +460,8 @@ class Via {
}

export class SysVia extends Via {
constructor(cpu, video, soundChip, cmos, isMaster, initialLayout, getGamepads) {
super(cpu, 0x01);
constructor(cpu, scheduler, video, soundChip, cmos, isMaster, initialLayout, getGamepads) {
super(cpu, scheduler, 0x01);

this.IC32 = 0;
this.capsLockLight = false;
Expand Down Expand Up @@ -626,8 +655,8 @@ export class SysVia extends Via {
}

export class UserVia extends Via {
constructor(cpu, isMaster, userPortPeripheral) {
super(cpu, 0x02);
constructor(cpu, scheduler, isMaster, userPortPeripheral) {
super(cpu, scheduler, 0x02);
this.isMaster = isMaster;
this.userPortPeripheral = userPortPeripheral;
this.reset();
Expand Down

0 comments on commit 2c67629

Please sign in to comment.