logicsimulator/components/logic74xx273.ts

118 lines
4.4 KiB
TypeScript

// Logic simulation for the 74xx273 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 Logic74xx273 extends BaseComponent {
private clock = false;
private internalstate: boolean[];
private d0 = true;
private d1 = true;
private d2 = true;
private d3 = true;
private d4 = true;
private d5 = true;
private d6 = true;
private d7 = true;
private cp = true;
private mr = true;
constructor(simulator: Simulator, id: string, param: BaseComponentParam) {
super(simulator, id, param);
this.pins.set('d0', new TriState(this, -40, -75));
this.pins.set('d1', new TriState(this, -40, -60));
this.pins.set('d2', new TriState(this, -40, -45));
this.pins.set('d3', new TriState(this, -40, -30));
this.pins.set('d4', new TriState(this, -40, -15));
this.pins.set('d5', new TriState(this, -40, 0));
this.pins.set('d6', new TriState(this, -40, 15));
this.pins.set('d7', new TriState(this, -40, 30));
this.pins.set('cp', new TriState(this, -40, 60));
this.pins.set('mr', new TriState(this, -40, 75));
this.pins.set('q0', new TriState(this, 40, -75));
this.pins.set('q1', new TriState(this, 40, -60));
this.pins.set('q2', new TriState(this, 40, -45));
this.pins.set('q3', new TriState(this, 40, -30));
this.pins.set('q4', new TriState(this, 40, -15));
this.pins.set('q5', new TriState(this, 40, 0));
this.pins.set('q6', new TriState(this, 40, 15));
this.pins.set('q7', new TriState(this, 40, 30));
this.internalstate = [];
for (let i = 0; i < 8; ++i) {
this.internalstate[i] = false;
}
}
setup(canvas: SVGElement) {
super.doSetup('logic74xx273', canvas);
}
io() {
this.d0 = this.binary(this.getPin('d0').getAndReset(), true);
this.d1 = this.binary(this.getPin('d1').getAndReset(), true);
this.d2 = this.binary(this.getPin('d2').getAndReset(), true);
this.d3 = this.binary(this.getPin('d3').getAndReset(), true);
this.d4 = this.binary(this.getPin('d4').getAndReset(), true);
this.d5 = this.binary(this.getPin('d5').getAndReset(), true);
this.d6 = this.binary(this.getPin('d6').getAndReset(), true);
this.d7 = this.binary(this.getPin('d7').getAndReset(), true);
this.cp = this.binary(this.getPin('cp').getAndReset(), true);
this.mr = this.binary(this.getPin('mr').getAndReset(), true);
}
update(): boolean {
if (this.startdelay) {
--this.startdelay;
return false;
}
if (this.mr === false) {
// reset
for (let i = 0; i < 8; ++i) {
this.internalstate[i] = false;
}
}
let mod = false;
if (this.cp && this.clock === false) {
mod ||= this.internalstate[0] !== this.d0;
this.internalstate[0] = this.d0;
mod ||= this.internalstate[1] !== this.d1;
this.internalstate[1] = this.d1;
mod ||= this.internalstate[2] !== this.d2;
this.internalstate[2] = this.d2;
mod ||= this.internalstate[3] !== this.d3;
this.internalstate[3] = this.d3;
mod ||= this.internalstate[4] !== this.d4;
this.internalstate[4] = this.d4;
mod ||= this.internalstate[5] !== this.d5;
this.internalstate[5] = this.d5;
mod ||= this.internalstate[6] !== this.d6;
this.internalstate[6] = this.d6;
mod ||= this.internalstate[7] !== this.d7;
this.internalstate[7] = this.d7;
}
this.getPin('q0').setBool(this.internalstate[0]);
this.getPin('q1').setBool(this.internalstate[1]);
this.getPin('q2').setBool(this.internalstate[2]);
this.getPin('q3').setBool(this.internalstate[3]);
this.getPin('q4').setBool(this.internalstate[4]);
this.getPin('q5').setBool(this.internalstate[5]);
this.getPin('q6').setBool(this.internalstate[6]);
this.getPin('q7').setBool(this.internalstate[7]);
this.clock = this.cp;
return mod;
}
}