-
Notifications
You must be signed in to change notification settings - Fork 145
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
shivamsahu-tech
wants to merge
1
commit into
CircuitVerse:main
Choose a base branch
from
shivamsahu-tech:add-subtractor-module
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+227
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
💡 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:
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 inv0/src/simulator/src/modules/Subtractor.js
(lines 109-111) points to: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.
v0/src/simulator/src/modules/Subtractor.js