Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Subtractor Component to misc menu #517

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
28 changes: 27 additions & 1 deletion v0/src/assets/img/Adder.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions v0/src/assets/img/Subtractor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 27 additions & 1 deletion v0/src/simulator/src/img/Adder.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions v0/src/simulator/src/img/Subtractor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions v0/src/simulator/src/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"ControlledInverter",
"TriState",
"Adder",
"Subtractor",
"verilogMultiplier",
"verilogDivider",
"verilogPower",
Expand Down Expand Up @@ -161,6 +162,7 @@
{ "name": "Flag", "label": "Flag" },
{ "name": "Splitter", "label": "Splitter" },
{ "name": "Adder", "label": "Adder" },
{ "name": "Subtractor", "label": "Subtractor" },
{ "name": "ALU", "label": "ALU(Arithmetic and Logical Unit)" },
{ "name": "TriState", "label": "TriState Flip Flop" },
{ "name": "Tunnel", "label": "Tunnel" },
Expand Down
2 changes: 2 additions & 0 deletions v0/src/simulator/src/moduleSetup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import modules from './modules'
import Adder from './modules/Adder'
import Subtractor from './modules/Subtractor'
import ALU from './modules/ALU'
import AndGate from './modules/AndGate'
import Arrow from './modules/Arrow'
Expand Down Expand Up @@ -84,6 +85,7 @@ export default function setupModules() {
Buffer,
ControlledInverter,
Adder,
Subtractor,
verilogMultiplier,
verilogDivider,
verilogPower,
Expand Down
111 changes: 111 additions & 0 deletions v0/src/simulator/src/modules/Subtractor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* eslint-disable no-bitwise */
import CircuitElement from '../circuitElement';
import Node, { findNode } from '../node';
import simulationArea from '../simulationArea';

/**
* @class
* Subtractor
* @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 modules
*/
export default class Subtractor extends CircuitElement {
constructor(x, y, scope = globalScope, dir = 'RIGHT', bitWidth = 1) {
super(x, y, scope, dir, bitWidth);
/* this is done in this.baseSetup() now
this.scope['Subtractor'].push(this);
*/
this.setDimensions(20, 20);

this.inpA = new Node(-20, -10, 0, this, this.bitWidth, 'A');
this.inpB = new Node(-20, 0, 0, this, this.bitWidth, 'B');
this.borrowIn = new Node(-20, 10, 0, this, 1, 'Bin');
this.diff = new Node(20, 0, 1, this, this.bitWidth, 'Diff');
this.borrowOut = new Node(20, 10, 1, this, 1, 'Bout');
}

/**
* @memberof Subtractor
* fn to create save Json Data of object
* @return {JSON}
*/
customSave() {
const data = {
constructorParamaters: [this.direction, this.bitWidth],
nodes: {
inpA: findNode(this.inpA),
inpB: findNode(this.inpB),
borrowIn: findNode(this.borrowIn),
borrowOut: findNode(this.borrowOut),
diff: findNode(this.diff),
},
};
return data;
}

/**
* @memberof Subtractor
* Checks if the element is resolvable
* @return {boolean}
*/
isResolvable() {
return this.inpA.value !== undefined && this.inpB.value !== undefined;
}

/**
* @memberof Subtractor
* function to change bitwidth of the element
* @param {number} bitWidth - new bitwidth
*/
newBitWidth(bitWidth) {
this.bitWidth = bitWidth;
this.inpA.bitWidth = bitWidth;
this.inpB.bitWidth = bitWidth;
this.diff.bitWidth = bitWidth;
}

/**
* @memberof Subtractor
* resolve output values based on inputData
*/
resolve() {
if (this.isResolvable() === false) {
return;
}
let borrowIn = this.borrowIn.value;
if (borrowIn === undefined) borrowIn = 0;

// Calculate difference
let diff = this.inpA.value - this.inpB.value - borrowIn;

// Handle borrow out
this.borrowOut.value = +(diff < 0);

// Ensure the result fits within bitWidth
this.diff.value = ((diff) << (32 - this.bitWidth)) >>> (32 - this.bitWidth);
simulationArea.simulationQueue.add(this.borrowOut);
simulationArea.simulationQueue.add(this.diff);
}

generateVerilog() {
if (this.borrowIn.verilogLabel) {
return `assign ${this.diff.verilogLabel} = ${this.inpA.verilogLabel} - ${this.inpB.verilogLabel} - ${this.borrowIn.verilogLabel};`;
}
return `assign ${this.diff.verilogLabel} = ${this.inpA.verilogLabel} - ${this.inpB.verilogLabel};`;
}
}

/**
* @memberof Subtractor
* Help Tip
* @type {string}
* @category modules
*/
Subtractor.prototype.tooltipText = 'Subtractor ToolTip : Performs subtraction of numbers.';
Subtractor.prototype.helplink = 'https://docs.circuitverse.org/#/chapter4/8misc?id=subtractor';
Subtractor.prototype.objectType = 'Subtractor';
Comment on lines +109 to +111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify documentation link

The helplink points to a specific URL that might not exist yet. Please ensure the documentation for the Subtractor component exists at this location or update the URL accordingly.


🌐 Web query:

Does the page https://docs.circuitverse.org/#/chapter4/8misc?id=subtractor exist and contain documentation for the Subtractor component?

💡 Result:

The page https://docs.circuitverse.org/#/chapter4/8misc?id=subtractor does not appear to exist in the provided documentation. While CircuitVerse's documentation ([2]) covers general usage of the simulator and digital circuit concepts, none of the search results reference this specific URL or confirm the existence of a dedicated "Subtractor" component documentation section at this address.

However, multiple CircuitVerse projects ([1][3][4][5][9][11]) demonstrate subtractor implementations (half/full subtractors, 2-bit to 8-bit variants) with detailed explanations of their logic equations, truth tables, and circuit designs. For example:

  • Full subtractors use SOP forms like Diff=xy' z'+x' y' z+xyz+x'yz' and Borrow=x' z+x' y+yz [1][4]
  • 4-bit adder-subtractor circuits implement two's complement subtraction through XOR gates and controlled inversion [3][5]

For official documentation, the main CircuitVerse docs ([2][6]) remain the primary resource, though subtractor-specific guidance would need to be inferred from general circuit design principles and community project examples.

Citations:


Action Required: Update the Subtractor Documentation URL

The current helplink property in v0/src/simulator/src/modules/Subtractor.js (lines 109-111) points to:

https://docs.circuitverse.org/#/chapter4/8misc?id=subtractor

However, our verification indicates that this URL does not exist in the CircuitVerse documentation. Please update the URL to point to valid and relevant documentation for the Subtractor component, or remove it if no dedicated documentation is available.

  • File: v0/src/simulator/src/modules/Subtractor.js
  • Lines: 109-111
  • Issue: Invalid documentation link for the Subtractor component