8-bit-computer/addrregister.ts

97 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-07-21 19:55:40 +02:00
// Address Register component for the LogicSimulator used on the 8-bit-computer
// Copyright (C) 2022 Sascha Nitsch
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class AddrRegister extends BaseComponent {
private r = 0;
private R = false;
private W = false;
private CLK = false;
private oldCLK = false;
constructor(simulator: Simulator, id: string, param: BaseComponentParam) {
super(simulator, id, param);
for (let i = 0; i < 8; ++i) {
this.pins.set('A' + (7 - i), new TriState(this, -40, i * 10 - 80));
this.pins.set('B' + (7 - i), new TriState(this, -40, i * 10 + 10));
this.stateMapBool.set('A' + (7 - i), false);
this.stateMapBool.set('B' + (7 - i), false);
}
this.pins.set('R', new TriState(this, 40, 100));
this.pins.set('W', new TriState(this, 40, 110));
this.pins.set('CLK', new TriState(this, 40, 80));
this.pins.set('invCLK', new TriState(this, 40, 90));
for (let i = 0; i < 16; ++i) {
this.pins.set('O' + (15 - i), new TriState(this, 40, i * 10 - 80));
this.stateMapBool.set('O' + (15 - i), false);
}
}
setup(canvas: SVGElement) {
super.doSetup('addrregister', canvas);
}
update(): boolean {
let i;
let value;
// is R set ? then send out to addrbus
if (this.R) {
// which register is selected via R3...R0
value = this.r;
for (i = 0; i < 16; ++i) {
this.getPin('O' + i).setBool((value & (1 << i)) > 0 ? true : false);
}
}
const rising = this.CLK && this.oldCLK === false;
this.oldCLK = this.CLK;
// is W set ? read from bus on raising clock edge
if (this.W && rising) {
this.r =
this.asint(this.getStateBool('A7'), 32768) +
this.asint(this.getStateBool('A6'), 16384) +
this.asint(this.getStateBool('A5'), 8192) +
this.asint(this.getStateBool('A4'), 4096) +
this.asint(this.getStateBool('A3'), 2048) +
this.asint(this.getStateBool('A2'), 1024) +
this.asint(this.getStateBool('A1'), 512) +
this.asint(this.getStateBool('A0'), 256) +
this.asint(this.getStateBool('B7'), 128) +
this.asint(this.getStateBool('B6'), 64) +
this.asint(this.getStateBool('B5'), 32) +
this.asint(this.getStateBool('B4'), 16) +
this.asint(this.getStateBool('B3'), 8) +
this.asint(this.getStateBool('B2'), 4) +
this.asint(this.getStateBool('B1'), 2) +
this.asint(this.getStateBool('B0'), 1);
}
return false;
}
io() {
this.CLK = this.binary(this.getPin('CLK').getAndReset(), true);
this.getPin('invCLK').getAndReset();
this.R = this.binary(this.getPin('R').getAndReset(), true);
this.W = this.binary(this.getPin('W').getAndReset(), true);
for (let i = 0; i < 8; ++i) {
this.stateMapBool.set('A' + i, this.binary(this.getPin('A' + i).getAndReset(), true));
this.stateMapBool.set('B' + i, this.binary(this.getPin('B' + i).getAndReset(), true));
}
for (let i = 0; i < 16; ++i) {
this.stateMapBool.set('O' + i, this.binary(this.getPin('O' + i).getAndReset(), true));
}
}
}