logicsimulator/components/logic74xx161.ts

84 lines
3.2 KiB
TypeScript

// Logic simulation for the 74xx161 IC for the LogicSimulator
// 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 Logic74xx161 extends BaseComponent {
private o = 0;
private i = 0;
private mr = false;
private cet = false;
private cep = false;
private clk = false;
private oldclk = false;
private pe = false;
constructor(simulator: Simulator, id: string, param: BaseComponentParam) {
super(simulator, id, param);
this.pins.set('clk', new TriState(this, -45, 30));
this.pins.set('i0', new TriState(this, -45, -30));
this.pins.set('i1', new TriState(this, -45, -15));
this.pins.set('i2', new TriState(this, -45, 0));
this.pins.set('i3', new TriState(this, -45, 15));
this.pins.set('o0', new TriState(this, 45, -30));
this.pins.set('o1', new TriState(this, 45, -15));
this.pins.set('o2', new TriState(this, 45, 0));
this.pins.set('o3', new TriState(this, 45, 15));
this.pins.set('cet', new TriState(this, -45, 60));
this.pins.set('cep', new TriState(this, -45, 45));
this.pins.set('mr', new TriState(this, -45, -60));
this.pins.set('pe', new TriState(this, -45, -45));
this.pins.set('tc', new TriState(this, 45, 60));
}
setup(canvas: SVGElement) {
super.doSetup('logic74xx161', canvas);
}
io() {
this.clk = this.binary(this.getPin('clk').getAndReset(), true);
this.mr = this.binary(this.getPin('mr').getAndReset(), true);
this.cet = this.binary(this.getPin('cet').getAndReset(), true);
this.cep = this.binary(this.getPin('cep').getAndReset(), true);
this.i =
this.asint(this.binary(this.getPin('i0').getAndReset(), true), 1) +
this.asint(this.binary(this.getPin('i1').getAndReset(), true), 2) +
this.asint(this.binary(this.getPin('i2').getAndReset(), true), 4) +
this.asint(this.binary(this.getPin('i3').getAndReset(), true), 8);
this.pe = this.binary(this.getPin('pe').getAndReset(), true);
}
update(): boolean {
let mod = false;
if (this.mr === false) {
mod = this.o !== 0;
this.o = 0;
}
const rising = this.clk && this.oldclk === false;
this.oldclk = this.clk;
if (this.pe === false && rising) {
// load
this.o = this.i;
} else if (rising && this.cet && this.cep) {
this.o = (this.o + 1) % 16;
}
this.getPin('o0').setBool((this.o & 1) !== 0);
this.getPin('o1').setBool((this.o & 2) !== 0);
this.getPin('o2').setBool((this.o & 4) !== 0);
this.getPin('o3').setBool((this.o & 8) !== 0);
this.getPin('tc').setBool(this.o === 15);
return mod;
}
}