8-bit-computer/logicrisingedge.ts

56 lines
1.8 KiB
TypeScript

// Logic Rising Edge 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/>.
interface LogicRisingEdgeParam extends BaseComponentParam {
delay?: number;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class LogicRisingEdge extends BaseComponent {
private a = false;
private olda = false;
private q = false;
private delay: number;
private activeDelay = 0;
constructor(simulator: Simulator, id: string, param: LogicRisingEdgeParam) {
super(simulator, id, param);
this.pins.set('a', new TriState(this, -30, 0));
this.pins.set('q', new TriState(this, 30, 0));
this.delay = param.delay || 0;
}
setup(canvas: SVGElement) {
super.doSetup('logicraisingedge', canvas);
}
io() {
this.a = this.binary(this.getPin('a').getAndReset(), true);
}
update(): boolean {
const oldstate = this.q;
if (this.olda === false && this.a === true) {
this.activeDelay = this.delay;
}
this.q = this.activeDelay > 0;
this.getPin('q').setBool(this.q);
this.olda = this.a;
if (this.activeDelay > 0) {
this.activeDelay--;
}
return oldstate !== this.q || this.q === true;
}
}