logicsimulator/components/dflipflop.ts

53 lines
1.8 KiB
TypeScript

// D-Flip-Flop component 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 DFlipFlop extends BaseComponent {
private d = false;
private CLK = false;
private oldCLK = false;
private q = false;
constructor(simulator: Simulator, id: string, param: BaseComponentParam) {
super(simulator, id, param);
this.pins.set('d', new TriState(this, -30, -10));
this.pins.set('clk', new TriState(this, -30, 10));
this.pins.set('q', new TriState(this, 30, -10));
this.getPin('q').setBool(false);
this.pins.set('notq', new TriState(this, 30, 10));
this.getPin('notq').setBool(true);
}
setup(canvas: SVGElement) {
super.doSetup('dflipflop', canvas);
}
io() {
this.d = this.binary(this.getPin('d').getAndReset(), true);
this.CLK = this.binary(this.getPin('clk').getAndReset(), true);
}
update(): boolean {
const oldstate = this.q;
if (this.oldCLK === false && this.CLK === true) {
this.q = this.d;
}
this.getPin('q').setBool(this.q);
this.getPin('notq').setBool(this.q === false);
this.oldCLK = this.CLK;
return oldstate !== this.q;
}
}