8-bit-computer/eightbitalu.ts

84 lines
3.3 KiB
TypeScript

// Breadboard 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 EightBitALU extends BaseComponent {
private write: boolean | string;
private sub: boolean | string;
private ret: number;
private a = 0;
private b = 0;
private r = 0;
constructor(simulator: Simulator, id: string, param: BaseComponentParam) {
super(simulator, id, param);
this.pins.set('sub', new TriState(this, -15, 90));
this.sub = 'float';
this.pins.set('write', new TriState(this, 20, 90));
this.write = 'float';
this.ret = 0;
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.pins.set('D' + (7 - i), new TriState(this, 40, i * 20 - 70));
this.stateMapBool.set('A' + (7 - i), true);
this.stateMapBool.set('B' + (7 - i), true);
this.stateMapBool.set('D' + (7 - i), true);
}
}
setup(canvas: SVGElement) {
super.doSetup('eightbitalu', canvas);
}
io() {
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));
this.stateMapBool.set('D' + i, this.binary(this.getPin('D' + i).getAndReset(), true));
}
this.sub = this.binary(this.getPin('sub').getAndReset(), true);
this.write = this.binary(this.getPin('write').getAndReset(), true);
this.a =
this.asint(this.getStateBool('A7'), 128) +
this.asint(this.getStateBool('A6'), 64) +
this.asint(this.getStateBool('A5'), 32) +
this.asint(this.getStateBool('A4'), 16) +
this.asint(this.getStateBool('A3'), 8) +
this.asint(this.getStateBool('A2'), 4) +
this.asint(this.getStateBool('A1'), 2) +
this.asint(this.getStateBool('A0'), 1);
this.b =
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);
}
update(): boolean {
const oldret = this.ret;
this.ret = (this.sub ? this.a - this.b : this.a + this.b) & 0xff;
for (let i = 0; i < 8; ++i) {
const v = (this.ret >> i) & 1 ? WireState.high : WireState.low;
this.getPin('D' + i).setState(this.write ? v : WireState.float);
}
return oldret !== this.ret;
}
}