Skip to content

Commit

Permalink
Add support for USER port switch.
Browse files Browse the repository at this point in the history
Also initialises all VIA registers to zero, per data sheet.
Introduces alt-1 through alt-8 for switches (currently enabled on but
will need configuration later). Also alt-R reloads.

Addresses #160 - though configuration of this is still needed.
  • Loading branch information
mattgodbolt committed Feb 10, 2018
1 parent 5b9135f commit c77e85b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
21 changes: 19 additions & 2 deletions 6502.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,30 @@ define(['./utils', './6502.opcodes', './via', './acia', './serial', './tube', '.
};
}

return function Cpu6502(model, dbgr, video_, soundChip_, ddNoise_, cmos, config) {
function FakeUserPort() {
return {
write: function (val) {
},
read: function () {
return 0xff;
}
};
}

function fixUpConfig(config) {
if (config === undefined) config = {};
if (!config.keyLayout)
config.keyLayout = "physical";
if (!config.cpuMultiplier)
config.cpuMultiplier = 1;
if (!config.userPort)
config.userPort = new FakeUserPort();
config.extraRoms = config.extraRoms || [];
return config;
}

return function Cpu6502(model, dbgr, video_, soundChip_, ddNoise_, cmos, config) {
config = fixUpConfig(config);

base6502(this, model);

Expand Down Expand Up @@ -846,7 +863,7 @@ define(['./utils', './6502.opcodes', './via', './acia', './serial', './tube', '.
this.scheduler = new scheduler.Scheduler();
this.soundChip.setScheduler(this.scheduler);
this.sysvia = via.SysVia(this, this.video, this.soundChip, cmos, model.isMaster, config.keyLayout);
this.uservia = via.UserVia(this, model.isMaster);
this.uservia = via.UserVia(this, model.isMaster, config.userPort);
this.touchScreen = new TouchScreen(this.scheduler);
this.acia = new Acia(this, this.soundChip.toneGenerator, this.scheduler, this.touchScreen);
this.serial = new Serial(this.acia);
Expand Down
59 changes: 55 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require(['jquery', 'utils', 'video', 'soundchip', 'ddnoise', 'debug', '6502', 'c

var BBC = utils.BBC;
var keyCodes = utils.keyCodes;
var emuKeyHandlers = {};
var cpuMultiplier = 1;

if (queryString) {
Expand Down Expand Up @@ -276,7 +277,8 @@ require(['jquery', 'utils', 'video', 'soundchip', 'ddnoise', 'debug', '6502', 'c

function keyPress(evt) {
if (running || !dbgr.enabled()) return;
if (keyCode(evt) === 103 /* lower case g */) {
var code = keyCode(evt);
if (code === 103 /* lower case g */) {
dbgr.hide();
go();
return;
Expand All @@ -285,10 +287,27 @@ require(['jquery', 'utils', 'video', 'soundchip', 'ddnoise', 'debug', '6502', 'c
if (handled) evt.preventDefault();
}

emuKeyHandlers[utils.keyCodes.S] = function (down, code) {
if (down) {
utils.noteEvent('keyboard', 'press', 'S');
stop(true);
}
};
emuKeyHandlers[utils.keyCodes.R] = function (down, code) {
if (down)
window.location.reload();
};

function keyDown(evt) {
if (!running) return;
var code = keyCode(evt);
if (code === utils.keyCodes.HOME && evt.ctrlKey) {
if (evt.altKey) {
var handler = emuKeyHandlers[code];
if (handler) {
handler(true, code);
evt.preventDefault();
}
} else if (code === utils.keyCodes.HOME && evt.ctrlKey) {
utils.noteEvent('keyboard', 'press', 'home');
stop(true);
} else if (code == utils.keyCodes.F12 || code == utils.keyCodes.BREAK) {
Expand All @@ -306,7 +325,13 @@ require(['jquery', 'utils', 'video', 'soundchip', 'ddnoise', 'debug', '6502', 'c
var code = keyCode(evt);
processor.sysvia.keyUp(code);
if (!running) return;
if (code == utils.keyCodes.F12 || code == utils.keyCodes.BREAK) {
if (evt.altKey) {
var handler = emuKeyHandlers[code];
if (handler) {
handler(false, code);
evt.preventDefault();
}
} else if (code == utils.keyCodes.F12 || code == utils.keyCodes.BREAK) {
processor.setReset(false);
}
evt.preventDefault();
Expand Down Expand Up @@ -347,11 +372,37 @@ require(['jquery', 'utils', 'video', 'soundchip', 'ddnoise', 'debug', '6502', 'c
window.localStorage.cmosRam = JSON.stringify(data);
}
});

var userPort = null;
if (true /* keyswitch */) {
var switchState = 0xff;

function switchKey(down, code) {
var bit = 1 << (code - utils.keyCodes.K1);
if (down)
switchState &= (0xff ^ bit);
else
switchState |= bit;
}

for (var idx = utils.keyCodes.K1; idx <= utils.keyCodes.K8; ++idx) {
emuKeyHandlers[idx] = switchKey;
}
userPort = {
write: function (val) {
},
read: function () {
return switchState;
}
};
}

var emulationConfig = {
keyLayout: keyLayout,
cpuMultiplier: cpuMultiplier,
videoCyclesBatch: parsedQuery.videoCyclesBatch,
extraRoms: extraRoms
extraRoms: extraRoms,
userPort: userPort
};
processor = new Cpu6502(model, dbgr, video, soundChip, ddNoise, cmos, emulationConfig);

Expand Down
12 changes: 7 additions & 5 deletions via.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ define(['./utils'], function (utils) {
justhit: 0,

reset: function (hard) {
self.ora = self.orb = 0xff;
self.ddra = self.ddrb = 0xff;
// http://archive.6502.org/datasheets/mos_6522_preliminary_nov_1977.pdf
// "Reset sets all registers to zero except t1 t2 and sr"
self.ora = self.orb = 0x00;
self.ddra = self.ddrb = 0x00;
self.ifr = self.ier = 0x00;
self.t1c = self.t1l = self.t2c = self.t2l = 0x1fffe;
self.t1hit = self.t2hit = true;
Expand Down Expand Up @@ -528,7 +530,7 @@ define(['./utils'], function (utils) {
return self;
}

function uservia(cpu, isMaster) {
function uservia(cpu, isMaster, userPortPeripheral) {
"use strict";
var self = via(cpu, 0x02);

Expand All @@ -537,15 +539,15 @@ define(['./utils'], function (utils) {
};

self.writePortB = function (val) {
// user port
userPortPeripheral.write(val);
};

self.readPortA = function () {
return 0xff; // printer port
};

self.readPortB = function () {
return 0xff; // user port (TODO: mouse, compact joystick)
return userPortPeripheral.read();
};
self.reset();
return self;
Expand Down

0 comments on commit c77e85b

Please sign in to comment.