Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/assets/img/ParityGenerator.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
162 changes: 162 additions & 0 deletions src/simulator/spec/parityGenerator.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/**
* @vitest-environment jsdom
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';

vi.mock('../src/themer/themer', () => ({
colors: {
'stroke': '#000000',
'fill': '#FFFFFF',
'hover_select': '#CCCCCC',
'text': '#000000'
},
updateThemeForStyle: vi.fn(),
getThemeCardSvg: vi.fn()
}));

vi.mock('../src/engine', () => ({
scheduleUpdate: vi.fn(),
wireToBeCheckedSet: vi.fn(),
updateCanvasSet: vi.fn(),
forceResetNodesSet: vi.fn(),
updateSimulationSet: vi.fn(),
renderCanvas: vi.fn(),
canvasMessageData: {},
errorDetectedGet: vi.fn().mockReturnValue(false),
errorDetectedSet: vi.fn()
}));

vi.mock('../src/ux', () => ({
fillSubcircuitElements: vi.fn()
}));

vi.mock('../src/modules', () => ({
changeInputSize: vi.fn(),
default: {}
}));

vi.mock('../src/subcircuit', () => ({
default: class SubCircuit {}
}));

vi.mock('../src/data/load', () => ({
loadScope: vi.fn(),
default: vi.fn()
}));

vi.mock('../src/data', () => ({
default: {
save: vi.fn(),
load: vi.fn(),
createSaveAsImgPrompt: vi.fn(),
clearProject: vi.fn(),
newProject: vi.fn(),
saveOffline: vi.fn(),
createOpenLocalPrompt: vi.fn(),
recoverProject: vi.fn(),
createSubCircuitPrompt: vi.fn(),
createCombinationalAnalysisPrompt: vi.fn(),
fullViewOption: vi.fn(),
colorThemes: vi.fn(),
showTourGuide: vi.fn(),
newVerilogModule: vi.fn(),
generateVerilog: vi.fn(),
bitconverter: vi.fn(),
createNewCircuitScope: vi.fn(),
customShortcut: vi.fn(),
ExportProject: {},
ImportProject: {}
}
}));

vi.mock('../src/combinationalAnalysis', () => ({
performCombinationalAnalysis: vi.fn(),
GenerateCircuit: vi.fn(),
createBooleanPrompt: vi.fn()
}));

vi.mock('../src/layoutMode', () => ({
layoutModeGet: vi.fn().mockReturnValue(false),
layoutModeSet: vi.fn(),
tempBuffer: {},
determineLabel: vi.fn(),
paneLayout: vi.fn(),
layoutUpdate: vi.fn()
}));

import ParityGenerator from '../src/modules/ParityGenerator';
import { simulationArea } from '../src/simulationArea';

describe('ParityGenerator', () => {
let parityGen;

beforeEach(() => {
global.globalScope = {
ParityGenerator: [],
scale: 1,
ox: 0,
oy: 0,
allNodes: [],
nodes: []
};

simulationArea.simulationQueue = {
add: vi.fn()
};
simulationArea.context = {
beginPath: vi.fn(),
fillStyle: '',
textAlign: '',
fill: vi.fn(),
stroke: vi.fn(),
moveTo: vi.fn(),
lineTo: vi.fn(),
bezierCurveTo: vi.fn(),
closePath: vi.fn(),
arc: vi.fn(),
fillText: vi.fn(),
measureText: vi.fn(() => ({ width: 0 }))
};
});

it('should initialize with 3 inputs by default', () => {
parityGen = new ParityGenerator(0, 0, global.globalScope, 'RIGHT', 3);
expect(parityGen.inputSize).toBe(3);
expect(parityGen.inp.length).toBe(3);
});

it('should calculate parity correctly (Odd 1s -> 1)', () => {
parityGen = new ParityGenerator(0, 0, global.globalScope, 'RIGHT', 3);
parityGen.inp[0].value = 1;
parityGen.inp[1].value = 0;
parityGen.inp[2].value = 0;
parityGen.resolve();
expect(parityGen.output1.value).toBe(1);
});

it('should calculate parity correctly (Even 1s -> 0)', () => {
parityGen = new ParityGenerator(0, 0, global.globalScope, 'RIGHT', 3);
parityGen.inp[0].value = 1;
parityGen.inp[1].value = 1;
parityGen.inp[2].value = 0;
parityGen.resolve();
expect(parityGen.output1.value).toBe(0);
});

it('should work with 4 inputs', () => {
parityGen = new ParityGenerator(0, 0, global.globalScope, 'RIGHT', 4);
expect(parityGen.inputSize).toBe(4);
expect(parityGen.inp.length).toBe(4);

parityGen.inp[0].value = 1;
parityGen.inp[1].value = 1;
parityGen.inp[2].value = 1;
parityGen.inp[3].value = 0;
parityGen.resolve();
expect(parityGen.output1.value).toBe(1);

parityGen.inp[3].value = 1;
parityGen.resolve();
expect(parityGen.output1.value).toBe(0);
});
});
4 changes: 3 additions & 1 deletion src/simulator/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export const circuitElementList = [
"Dlatch",
"TB_Input",
"TB_Output",
"ForceGate"
"ForceGate",
"ParityGenerator"
]
const annotationList = ["Text", "Rectangle", "Arrow", "ImageAnnotation"]
export const moduleList = [...circuitElementList, ...annotationList]
Expand Down Expand Up @@ -171,6 +172,7 @@ export const elementHierarchy: Record<string, NameLabel[]> = {
],
"Misc": [
{ name: "TwoComplement", label: "Two Complement" },
{ name: "ParityGenerator", label: "Parity Generator" },
{ name: "Flag", label: "Flag" },
{ name: "Splitter", label: "Splitter" },
{ name: "Adder", label: "Adder" },
Expand Down
2 changes: 2 additions & 0 deletions src/simulator/src/moduleSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import TwoComplement from './modules/TwoComplement'
import VariableLed from './modules/VariableLed'
import XnorGate from './modules/XnorGate'
import XorGate from './modules/XorGate'
import ParityGenerator from './modules/ParityGenerator'
import Clock from './sequential/Clock'
import DflipFlop from './sequential/DflipFlop'
import Dlatch from './sequential/Dlatch'
Expand Down Expand Up @@ -72,6 +73,7 @@ export default function setupModules() {
Counter,
Multiplexer,
XorGate,
ParityGenerator,
XnorGate,
SevenSegDisplay,
SixteenSegDisplay,
Expand Down
91 changes: 91 additions & 0 deletions src/simulator/src/modules/ParityGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import CircuitElement from '../circuitElement'
import Node, { findNode } from '../node'
import { simulationArea } from '../simulationArea'
import { fillText } from '../canvasApi'
import { changeInputSize } from '../modules'
import { gateGenerateVerilog } from '../utils'
import { colors } from '../themer/themer'

export default class ParityGenerator extends CircuitElement {
constructor(
x,
y,
scope = globalScope,
dir = 'RIGHT',
inputs = 3,
bitWidth = 1
) {
super(x, y, scope, dir, bitWidth)
this.rectangleObject = true
this.setDimensions(20, 20)
this.inp = []
this.inputSize = inputs
if (inputs % 2 === 1) {
for (let i = 0; i < inputs / 2 - 1; i++) {
const a = new Node(-20, -10 * (i + 1), 0, this)
this.inp.push(a)
}
let a = new Node(-20, 0, 0, this)
this.inp.push(a)
for (let i = inputs / 2 + 1; i < inputs; i++) {
a = new Node(-20, 10 * (i + 1 - inputs / 2 - 1), 0, this)
this.inp.push(a)
}
} else {
for (let i = 0; i < inputs / 2; i++) {
const a = new Node(-20, -10 * (i + 1), 0, this)
this.inp.push(a)
}
for (let i = inputs / 2; i < inputs; i++) {
const a = new Node(-20, 10 * (i + 1 - inputs / 2), 0, this)
this.inp.push(a)
}
}
this.output1 = new Node(20, 0, 1, this)
}

customSave() {
const data = {
constructorParamaters: [
this.direction,
this.inputSize,
this.bitWidth,
],
nodes: {
inp: this.inp.map(findNode),
output1: findNode(this.output1),
},
}
return data
}

resolve() {
let result = this.inp[0].value || 0
if (this.isResolvable() === false) {
return
}
for (let i = 1; i < this.inputSize; i++)
result ^= this.inp[i].value || 0
this.output1.value = result
simulationArea.simulationQueue.add(this.output1)
}

customDraw() {
var ctx = simulationArea.context
ctx.beginPath()
ctx.fillStyle = colors['text']
ctx.textAlign = 'center'
fillText(ctx, 'Parity', this.x, this.y + 2, 10)
ctx.fill()
}

generateVerilog() {
return gateGenerateVerilog.call(this, '^')
}
}

ParityGenerator.prototype.tooltipText = 'Parity Generator: Outputs 1 if odd number of 1s.'
ParityGenerator.prototype.alwaysResolve = true
ParityGenerator.prototype.changeInputSize = changeInputSize
ParityGenerator.prototype.verilogType = 'xor'
ParityGenerator.prototype.objectType = 'ParityGenerator'
Loading