2019-03-01 05:21:18 +00:00
|
|
|
/* Copyright 2010-2019 Will Scullin <scullin@scullinsteel.com>
|
2013-10-10 18:03:07 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and its
|
|
|
|
* documentation for any purpose is hereby granted without fee, provided that
|
|
|
|
* the above copyright notice appear in all copies and that both that
|
|
|
|
* copyright notice and this permission notice appear in supporting
|
|
|
|
* documentation. No representations are made about the suitability of this
|
|
|
|
* software for any purpose. It is provided "as is" without express or
|
|
|
|
* implied warranty.
|
|
|
|
*/
|
|
|
|
|
2019-12-01 17:52:14 +00:00
|
|
|
import { debug, toHex } from '../util';
|
2019-11-25 00:52:01 +00:00
|
|
|
import { rom } from '../roms/cards/thunderclock';
|
2013-10-10 18:03:07 +00:00
|
|
|
|
2019-12-27 23:04:07 +00:00
|
|
|
export default function Thunderclock()
|
2013-10-10 18:03:07 +00:00
|
|
|
{
|
|
|
|
var LOC = {
|
2014-07-30 19:01:57 +00:00
|
|
|
CONTROL: 0x80,
|
|
|
|
AUX: 0x88
|
2013-10-10 18:03:07 +00:00
|
|
|
};
|
|
|
|
|
2019-12-01 17:52:14 +00:00
|
|
|
var COMMANDS = {
|
|
|
|
MASK: 0x18,
|
|
|
|
REGHOLD: 0x00,
|
|
|
|
REGSHIFT: 0x08,
|
|
|
|
TIMED: 0x18
|
|
|
|
};
|
|
|
|
|
2013-10-10 18:03:07 +00:00
|
|
|
var FLAGS = {
|
|
|
|
DATA: 0x01,
|
|
|
|
CLOCK: 0x02,
|
|
|
|
STROBE: 0x04
|
|
|
|
};
|
|
|
|
|
|
|
|
function _init() {
|
2019-12-27 23:04:07 +00:00
|
|
|
debug('Thunderclock');
|
2013-10-10 18:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 17:52:14 +00:00
|
|
|
var _clock = false;
|
|
|
|
var _strobe = false;
|
|
|
|
var _shiftMode = false;
|
|
|
|
var _register = 0;
|
2013-10-10 18:03:07 +00:00
|
|
|
var _bits = [];
|
2019-12-01 17:52:14 +00:00
|
|
|
var _command = COMMANDS.HOLD;
|
|
|
|
|
|
|
|
function _debug() {
|
|
|
|
// debug.apply(this, arguments);
|
|
|
|
}
|
2013-10-10 18:03:07 +00:00
|
|
|
|
2019-12-01 17:52:14 +00:00
|
|
|
function _calcBits() {
|
2013-10-10 18:03:07 +00:00
|
|
|
function shift(val) {
|
|
|
|
for (var idx = 0; idx < 4; idx++) {
|
|
|
|
_bits.push((val & 0x08) !== 0);
|
|
|
|
val <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function shiftBCD(val) {
|
|
|
|
shift(parseInt(val / 10, 10));
|
|
|
|
shift(parseInt(val % 10, 10));
|
|
|
|
}
|
|
|
|
|
|
|
|
var now = new Date();
|
|
|
|
var day = now.getDate();
|
|
|
|
var weekday = now.getDay();
|
|
|
|
var month = now.getMonth() + 1;
|
|
|
|
var hour = now.getHours();
|
|
|
|
var minutes = now.getMinutes();
|
|
|
|
var seconds = now.getSeconds();
|
|
|
|
|
|
|
|
_bits = [];
|
|
|
|
shift(month);
|
|
|
|
shift(weekday);
|
|
|
|
shiftBCD(day);
|
|
|
|
shiftBCD(hour);
|
|
|
|
shiftBCD(minutes);
|
|
|
|
shiftBCD(seconds);
|
|
|
|
}
|
|
|
|
|
2019-12-01 17:52:14 +00:00
|
|
|
function _shift() {
|
|
|
|
if (_shiftMode) {
|
|
|
|
if (_bits.pop()) {
|
|
|
|
_debug('shifting 1');
|
|
|
|
_register |= 0x80;
|
|
|
|
} else {
|
|
|
|
_debug('shifting 0');
|
|
|
|
_register &= 0x7f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-10 18:03:07 +00:00
|
|
|
function _access(off, val) {
|
2016-12-06 05:16:21 +00:00
|
|
|
switch (off & 0x8F) {
|
2013-10-10 18:03:07 +00:00
|
|
|
case LOC.CONTROL:
|
|
|
|
if (val !== undefined) {
|
2019-12-01 17:52:14 +00:00
|
|
|
var strobe = val & FLAGS.STROBE ? true : false;
|
|
|
|
if (strobe !== _strobe) {
|
|
|
|
_debug('strobe', _strobe ? 'high' : 'low');
|
|
|
|
if (strobe) {
|
|
|
|
_command = val & COMMANDS.MASK;
|
|
|
|
switch (_command) {
|
|
|
|
case COMMANDS.TIMED:
|
|
|
|
_debug('TIMED');
|
|
|
|
_calcBits();
|
|
|
|
break;
|
|
|
|
case COMMANDS.REGSHIFT:
|
|
|
|
_debug('REGSHIFT');
|
|
|
|
_shiftMode = true;
|
|
|
|
_shift();
|
|
|
|
break;
|
|
|
|
case COMMANDS.REGHOLD:
|
|
|
|
_debug('REGHOLD');
|
|
|
|
_shiftMode = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
_debug('Unknown command', toHex(_command));
|
|
|
|
}
|
2013-10-10 18:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-01 17:52:14 +00:00
|
|
|
|
|
|
|
var clock = val & FLAGS.CLOCK ? true : false;
|
|
|
|
|
|
|
|
if (clock !== _clock) {
|
|
|
|
_clock = clock;
|
|
|
|
_debug('clock', _clock ? 'high' : 'low');
|
|
|
|
if (clock) {
|
|
|
|
_shift();
|
|
|
|
}
|
2013-10-10 18:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-07-30 19:01:57 +00:00
|
|
|
case LOC.AUX:
|
|
|
|
break;
|
|
|
|
}
|
2019-12-01 17:52:14 +00:00
|
|
|
return _register;
|
2013-10-10 18:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_init();
|
|
|
|
|
|
|
|
return {
|
|
|
|
read: function thunderclock_read(page, off) {
|
2016-11-28 01:28:49 +00:00
|
|
|
var result;
|
|
|
|
if (page < 0xc8) {
|
|
|
|
result = rom[off];
|
|
|
|
} else {
|
2016-12-06 05:16:21 +00:00
|
|
|
result = rom[(page - 0xc8) << 8 | off];
|
2016-11-28 01:28:49 +00:00
|
|
|
}
|
|
|
|
return result;
|
2013-10-10 18:03:07 +00:00
|
|
|
},
|
|
|
|
write: function thunderclock_write() {
|
|
|
|
},
|
|
|
|
ioSwitch: function thunderclock_ioSwitch(off, val) {
|
|
|
|
return _access(off, val);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|