把前端从CircuitVerse中拆了出来

This commit is contained in:
zhbaor 2022-01-23 17:33:42 +08:00
commit 5bf1284599
2182 changed files with 189323 additions and 0 deletions

View file

@ -0,0 +1,88 @@
import CircuitElement from '../circuitElement';
import Node, { findNode } from '../node';
import simulationArea from '../simulationArea';
import { fillText4 } from '../canvasApi';
/**
* @class
* ForceGate
* @extends CircuitElement
* @param {number} x - x coordinate of element.
* @param {number} y - y coordinate of element.
* @param {Scope=} scope - Cirucit on which element is drawn
* @param {string=} dir - direction of element
* @param {number=} bitWidth - bit width per node.
* @category testbench
*/
export default class ForceGate extends CircuitElement {
constructor(x, y, scope = globalScope, dir = 'RIGHT', bitWidth = 1) {
super(x, y, scope, dir, bitWidth);
this.setDimensions(20, 10);
this.objectType = 'ForceGate';
this.scope.ForceGate.push(this);
this.inp1 = new Node(-20, 0, 0, this);
this.inp2 = new Node(0, 0, 0, this);
this.output1 = new Node(20, 0, 1, this);
}
/**
* @memberof ForceGate
* Checks if the element is resolvable
* @return {boolean}
*/
isResolvable() {
return (this.inp1.value !== undefined || this.inp2.value !== undefined);
}
/**
* @memberof ForceGate
* fn to create save Json Data of object
* @return {JSON}
*/
customSave() {
const data = {
constructorParamaters: [this.direction, this.bitWidth],
nodes: {
output1: findNode(this.output1),
inp1: findNode(this.inp1),
inp2: findNode(this.inp2),
},
};
return data;
}
/**
* @memberof ForceGate
* resolve output values based on inputData
*/
resolve() {
if (this.inp2.value !== undefined) { this.output1.value = this.inp2.value; } else { this.output1.value = this.inp1.value; }
simulationArea.simulationQueue.add(this.output1);
}
/**
* @memberof ForceGate
* function to draw element
*/
customDraw() {
var ctx = simulationArea.context;
const xx = this.x;
const yy = this.y;
ctx.beginPath();
ctx.fillStyle = 'Black';
ctx.textAlign = 'center';
fillText4(ctx, 'I', -10, 0, xx, yy, this.direction, 10);
fillText4(ctx, 'O', 10, 0, xx, yy, this.direction, 10);
ctx.fill();
}
}
/**
* @memberof ForceGate
* Help Tip
* @type {string}
* @category testbench
*/
ForceGate.prototype.tooltipText = 'Force Gate ToolTip : ForceGate Selected.';
ForceGate.prototype.objectType = 'ForceGate';

View file

@ -0,0 +1,289 @@
import CircuitElement from '../circuitElement';
import simulationArea from '../simulationArea';
import { correctWidth, lineTo, moveTo, fillText } from '../canvasApi';
import Node, { findNode } from '../node';
import plotArea from '../plotArea';
/**
* TestBench Input has a node for it's clock input.
* this.testData - the data of all test cases.
* Every testbench has a uniq identifier.
* @class
* @extends CircuitElement
* @param {number} x - the x coord of TB
* @param {number} y - the y coord of TB
* @param {Scope=} scope - the circuit on which TB is drawn
* @param {string} dir - direction
* @param {string} identifier - id to identify tests
* @param {JSON=} testData - input, output and number of tests
* @category testbench
*/
export default class TB_Input extends CircuitElement {
constructor(x, y, scope = globalScope, dir = 'RIGHT', identifier, testData) {
super(x, y, scope, dir, 1);
this.objectType = 'TB_Input';
this.scope.TB_Input.push(this);
this.setIdentifier(identifier || 'Test1');
this.testData = testData || { inputs: [], outputs: [], n: 0 };
this.clockInp = new Node(0, 20, 0, this, 1);
this.outputs = [];
this.running = false; // if tests are undergo
this.iteration = 0;
this.setup();
}
/**
* @memberof TB_Input
* Takes iput when double clicked. For help on generation of input refer to TB_Input.helplink
*/
dblclick() {
this.testData = JSON.parse(prompt('Enter TestBench Json'));
this.setup();
}
setDimensions() {
this.leftDimensionX = 0;
this.rightDimensionX = 120;
this.upDimensionY = 0;
this.downDimensionY = 40 + this.testData.inputs.length * 20;
}
/**
* @memberof TB_Input
* setups the Test by parsing through the testbench data.
*/
setup() {
this.iteration = 0;
this.running = false;
this.nodeList.clean(this.clockInp);
this.deleteNodes();
this.nodeList = [];
this.nodeList.push(this.clockInp);
this.testData = this.testData || { inputs: [], outputs: [], n: 0 };
// this.clockInp = new Node(0,20, 0,this,1);
this.setDimensions();
this.prevClockState = 0;
this.outputs = [];
for (var i = 0; i < this.testData.inputs.length; i++) {
this.outputs.push(new Node(this.rightDimensionX, 30 + i * 20, 1, this, this.testData.inputs[i].bitWidth, this.testData.inputs[i].label));
}
for (var i = 0; i < this.scope.TB_Output.length; i++) {
if (this.scope.TB_Output[i].identifier == this.identifier) { this.scope.TB_Output[i].setup(); }
}
}
/**
* @memberof TB_Input
* toggles state by simply negating this.running so that test cases stop
*/
toggleState() {
this.running = !this.running;
this.prevClockState = 0;
}
/**
* @memberof TB_Input
* function to run from test case 0 again
*/
resetIterations() {
this.iteration = 0;
this.prevClockState = 0;
}
/**
* @memberof TB_Input
* function to resolve the testbench input adds
*/
resolve() {
if (this.clockInp.value != this.prevClockState) {
this.prevClockState = this.clockInp.value;
if (this.clockInp.value == 1 && this.running) {
if (this.iteration < this.testData.n) {
this.iteration++;
} else {
this.running = false;
}
}
}
if (this.running && this.iteration) {
for (var i = 0; i < this.testData.inputs.length; i++) {
this.outputs[i].value = parseInt(this.testData.inputs[i].values[this.iteration - 1], 2);
simulationArea.simulationQueue.add(this.outputs[i]);
}
}
}
/**
* @memberof TB_Input
* was a function to plot values incase any flag used as output to this element
*/
setPlotValue() {
return;
var time = plotArea.stopWatch.ElapsedMilliseconds;
if (this.plotValues.length && this.plotValues[this.plotValues.length - 1][0] == time) { this.plotValues.pop(); }
if (this.plotValues.length == 0) {
this.plotValues.push([time, this.inp1.value]);
return;
}
if (this.plotValues[this.plotValues.length - 1][1] == this.inp1.value) { return; }
this.plotValues.push([time, this.inp1.value]);
}
customSave() {
var data = {
constructorParamaters: [this.direction, this.identifier, this.testData],
nodes: {
outputs: this.outputs.map(findNode),
clockInp: findNode(this.clockInp),
},
};
return data;
}
/**
* This function is used to set a uniq identifier to every testbench
* @memberof TB_Input
*/
setIdentifier(id = '') {
if (id.length == 0 || id == this.identifier) return;
for (var i = 0; i < this.scope.TB_Output.length; i++) {
this.scope.TB_Output[i].checkPairing();
}
for (var i = 0; i < this.scope.TB_Output.length; i++) {
if (this.scope.TB_Output[i].identifier == this.identifier) { this.scope.TB_Output[i].identifier = id; }
}
this.identifier = id;
this.checkPaired();
}
/**
* Check if there is a output tester paired with input TB.
* @memberof TB_Input
*/
checkPaired() {
for (var i = 0; i < this.scope.TB_Output.length; i++) {
if (this.scope.TB_Output[i].identifier == this.identifier) { this.scope.TB_Output[i].checkPairing(); }
}
}
delete() {
super.delete();
this.checkPaired();
}
customDraw() {
var ctx = simulationArea.context;
ctx.beginPath();
ctx.strokeStyle = 'grey';
ctx.fillStyle = '#fcfcfc';
ctx.lineWidth = correctWidth(1);
var xx = this.x;
var yy = this.y;
var xRotate = 0;
var yRotate = 0;
if (this.direction == 'LEFT') {
xRotate = 0;
yRotate = 0;
} else if (this.direction == 'RIGHT') {
xRotate = 120 - this.xSize;
yRotate = 0;
} else if (this.direction == 'UP') {
xRotate = 60 - this.xSize / 2;
yRotate = -20;
} else {
xRotate = 60 - this.xSize / 2;
yRotate = 20;
}
ctx.beginPath();
ctx.textAlign = 'center';
ctx.fillStyle = 'black';
fillText(ctx, `${this.identifier} [INPUT]`, xx + this.rightDimensionX / 2, yy + 14, 10);
fillText(ctx, ['Not Running', 'Running'][+this.running], xx + this.rightDimensionX / 2, yy + 14 + 10 + 20 * this.testData.inputs.length, 10);
fillText(ctx, `Case: ${this.iteration}`, xx + this.rightDimensionX / 2, yy + 14 + 20 + 20 * this.testData.inputs.length, 10);
// fillText(ctx, "Case: "+this.iteration, xx , yy + 20+14, 10);
ctx.fill();
ctx.font = '30px Raleway';
ctx.textAlign = 'right';
ctx.fillStyle = 'blue';
ctx.beginPath();
for (var i = 0; i < this.testData.inputs.length; i++) {
// ctx.beginPath();
fillText(ctx, this.testData.inputs[i].label, this.rightDimensionX - 5 + xx, 30 + i * 20 + yy + 4, 10);
}
ctx.fill();
if (this.running && this.iteration) {
ctx.font = '30px Raleway';
ctx.textAlign = 'left';
ctx.fillStyle = 'blue';
ctx.beginPath();
for (var i = 0; i < this.testData.inputs.length; i++) {
fillText(ctx, this.testData.inputs[i].values[this.iteration - 1], 5 + xx, 30 + i * 20 + yy + 4, 10);
}
ctx.fill();
}
ctx.beginPath();
ctx.strokeStyle = ('rgba(0,0,0,1)');
ctx.lineWidth = correctWidth(3);
var xx = this.x;
var yy = this.y;
// rect(ctx, xx - 20, yy - 20, 40, 40);
moveTo(ctx, 0, 15, xx, yy, this.direction);
lineTo(ctx, 5, 20, xx, yy, this.direction);
lineTo(ctx, 0, 25, xx, yy, this.direction);
ctx.stroke();
}
}
TB_Input.prototype.tooltipText = 'Test Bench Input Selected';
/**
* @memberof TB_Input
* different algo for drawing center elements
* @category testbench
*/
TB_Input.prototype.centerElement = true;
TB_Input.prototype.helplink = 'https://docs.circuitverse.org/#/testbench';
TB_Input.prototype.mutableProperties = {
identifier: {
name: 'TestBench Name:',
type: 'text',
maxlength: '10',
func: 'setIdentifier',
},
iteration: {
name: 'Reset Iterations',
type: 'button',
func: 'resetIterations',
},
toggleState: {
name: 'Toggle State',
type: 'button',
func: 'toggleState',
},
};
TB_Input.prototype.objectType = 'TB_Input';

View file

@ -0,0 +1,232 @@
import CircuitElement from '../circuitElement';
import simulationArea from '../simulationArea';
import { correctWidth, fillText } from '../canvasApi';
import Node, { findNode } from '../node';
// helper function to convert decimal to binary
function dec2bin(dec, bitWidth = undefined) {
// only for positive nos
var bin = (dec).toString(2);
if (bitWidth == undefined) return bin;
return '0'.repeat(bitWidth - bin.length) + bin;
}
/**
* TestBench Output has a node for it's input which is
* compared to desired output according tp testData of
* input TB Every TB_output has a uniq identifier matching
* it's TB_Input
* @class
* @extends CircuitElement
* @param {number} x - the x coord of TB
* @param {number} y - the y coord of TB
* @param {Scope=} scope - the circuit on which TB is drawn
* @param {string} dir - direction
* @param {string} identifier - id to identify tests
* @category testbench
*/
export default class TB_Output extends CircuitElement {
constructor(x, y, scope = globalScope, dir = 'RIGHT', identifier) {
super(x, y, scope, dir, 1);
// this.setDimensions(60,20);
this.objectType = 'TB_Output';
this.scope.TB_Output.push(this);
// this.xSize=10;
// this.plotValues = [];
// this.inp1 = new Node(0, 0, 0, this);
// this.inp1 = new Node(100, 100, 0, this);
this.setIdentifier(identifier || 'Test1');
this.inputs = [];
this.testBenchInput = undefined;
this.setup();
}
// TB_Output.prototype.dblclick=function(){
// this.testData=JSON.parse(prompt("Enter TestBench Json"));
// this.setup();
// }
setDimensions() {
this.leftDimensionX = 0;
this.rightDimensionX = 160;
this.upDimensionY = 0;
this.downDimensionY = 40;
if (this.testBenchInput) { this.downDimensionY = 40 + this.testBenchInput.testData.outputs.length * 20; }
}
setup() {
// this.iteration = 0;
// this.running = false;
// this.nodeList.clean(this.clockInp);
this.deleteNodes(); // deletes all nodes whenever setup is called.
this.nodeList = [];
this.inputs = [];
this.testBenchInput = undefined;
// find it's pair input
for (var i = 0; i < this.scope.TB_Input.length; i++) {
if (this.scope.TB_Input[i].identifier == this.identifier) {
this.testBenchInput = this.scope.TB_Input[i];
break;
}
}
this.setDimensions();
if (this.testBenchInput) {
for (var i = 0; i < this.testBenchInput.testData.outputs.length; i++) {
this.inputs.push(new Node(0, 30 + i * 20, NODE_INPUT, this, this.testBenchInput.testData.outputs[i].bitWidth, this.testBenchInput.testData.outputs[i].label));
}
}
}
customSave() {
var data = {
constructorParamaters: [this.direction, this.identifier],
nodes: {
inputs: this.inputs.map(findNode),
},
};
return data;
}
/**
* @memberof TB_output
* set identifier for this testbench
*/
setIdentifier(id = '') {
if (id.length == 0 || id == this.identifier) return;
this.identifier = id;
this.setup();
}
/**
* @memberof TB_output
* Function to check if the input for this TB exist
*/
checkPairing(id = '') {
if (this.testBenchInput) {
if (this.testBenchInput.deleted || this.testBenchInput.identifier != this.identifier) {
this.setup();
}
} else {
this.setup();
}
}
customDraw() {
var ctx = simulationArea.context;
ctx.beginPath();
ctx.strokeStyle = 'grey';
ctx.fillStyle = '#fcfcfc';
ctx.lineWidth = correctWidth(1);
var xx = this.x;
var yy = this.y;
var xRotate = 0;
var yRotate = 0;
if (this.direction == 'LEFT') {
xRotate = 0;
yRotate = 0;
} else if (this.direction == 'RIGHT') {
xRotate = 120 - this.xSize;
yRotate = 0;
} else if (this.direction == 'UP') {
xRotate = 60 - this.xSize / 2;
yRotate = -20;
} else {
xRotate = 60 - this.xSize / 2;
yRotate = 20;
}
// rect2(ctx, -120+xRotate+this.xSize, -20+yRotate, 120-this.xSize, 40, xx, yy, "RIGHT");
// if ((this.hover && !simulationArea.shiftDown) || simulationArea.lastSelected == this || simulationArea.multipleObjectSelections.contains(this))
// ctx.fillStyle = "rgba(255, 255, 32,0.8)";
// ctx.fill();
// ctx.stroke();
//
// ctx.font = "14px Raleway";
// this.xOff = ctx.measureText(this.identifier).width;
// ctx.beginPath();
// rect2(ctx, -105+xRotate+this.xSize, -11+yRotate,this.xOff + 10, 23, xx, yy, "RIGHT");
// ctx.fillStyle = "#eee"
// ctx.strokeStyle = "#ccc";
// ctx.fill();
// ctx.stroke();
//
ctx.beginPath();
ctx.textAlign = 'center';
ctx.fillStyle = 'black';
fillText(ctx, `${this.identifier} [OUTPUT]`, xx + this.rightDimensionX / 2, yy + 14, 10);
// fillText(ctx, ["Not Running","Running"][+this.running], xx + this.rightDimensionX/ 2 , yy + 14 + 10 + 20*this.testData.inputs.length, 10);
// fillText(ctx, "Case: "+(this.iteration), xx + this.rightDimensionX/ 2 , yy + 14 + 20 + 20*this.testData.inputs.length, 10);
fillText(ctx, ['Unpaired', 'Paired'][+(this.testBenchInput != undefined)], xx + this.rightDimensionX / 2, yy + this.downDimensionY - 5, 10);
ctx.fill();
if (this.testBenchInput) {
ctx.beginPath();
ctx.font = '30px Raleway';
ctx.textAlign = 'left';
ctx.fillStyle = 'blue';
for (var i = 0; i < this.testBenchInput.testData.outputs.length; i++) {
// ctx.beginPath();
fillText(ctx, this.testBenchInput.testData.outputs[i].label, 5 + xx, 30 + i * 20 + yy + 4, 10);
}
ctx.fill();
if (this.testBenchInput.running && this.testBenchInput.iteration) {
ctx.beginPath();
ctx.font = '30px Raleway';
ctx.textAlign = 'right';
ctx.fillStyle = 'blue';
ctx.beginPath();
for (var i = 0; i < this.testBenchInput.testData.outputs.length; i++) {
fillText(ctx, this.testBenchInput.testData.outputs[i].values[this.testBenchInput.iteration - 1], xx + this.rightDimensionX - 5, 30 + i * 20 + yy + 4, 10);
}
ctx.fill();
}
if (this.testBenchInput.running && this.testBenchInput.iteration) {
ctx.beginPath();
ctx.font = '30px Raleway';
ctx.textAlign = 'center';
ctx.fillStyle = 'blue';
for (var i = 0; i < this.testBenchInput.testData.outputs.length; i++) {
if (this.inputs[i].value != undefined) {
ctx.beginPath();
if (this.testBenchInput.testData.outputs[i].values[this.testBenchInput.iteration - 1] == 'x' || parseInt(this.testBenchInput.testData.outputs[i].values[this.testBenchInput.iteration - 1], 2) == this.inputs[i].value) { ctx.fillStyle = 'green'; } else { ctx.fillStyle = 'red'; }
fillText(ctx, dec2bin(this.inputs[i].value, this.inputs[i].bitWidth), xx + this.rightDimensionX / 2, 30 + i * 20 + yy + 4, 10);
ctx.fill();
} else {
ctx.beginPath();
if (this.testBenchInput.testData.outputs[i].values[this.testBenchInput.iteration - 1] == 'x') { ctx.fillStyle = 'green'; } else { ctx.fillStyle = 'red'; }
fillText(ctx, 'X', xx + this.rightDimensionX / 2, 30 + i * 20 + yy + 4, 10);
ctx.fill();
}
}
}
}
}
}
TB_Output.prototype.tooltipText = 'Test Bench Output Selected';
TB_Output.prototype.helplink = 'https://docs.circuitverse.org/#/testbench';
TB_Output.prototype.centerElement = true;
TB_Output.prototype.mutableProperties = {
identifier: {
name: 'TestBench Name:',
type: 'text',
maxlength: '10',
func: 'setIdentifier',
},
};
TB_Output.prototype.objectType = 'TB_Output';