fix(ALU): correct borrow flag for subtraction and handle CTR=3 stale …#846
fix(ALU): correct borrow flag for subtraction and handle CTR=3 stale …#846Me-Priyank wants to merge 1 commit intoCircuitVerse:mainfrom
Conversation
✅ Deploy Preview for circuitverse ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
WalkthroughThe ALU.js module is updated to fix two operational bugs. When control signal is 3 (no-operation), the module now sets output and carryOut to 0 and enqueues both values instead of only updating the display message. When control signal is 6 (subtraction A-B), the module now correctly sets carryOut to indicate a borrow by computing whether inp1 is less than inp2, using arithmetic coercion to convert the result to 1 or 0, rather than always setting carryOut to 0. These modifications ensure proper output handling for the no-operation mode and accurate borrow flag representation during subtraction operations. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@Nihal4777 @tachyons kindly review |
|
@Nihal4777 can you please review |

Fixes #845
Describe the changes you have made in this PR -
This PR fixes two bugs in the ALU component:
Bug 1: Subtraction Borrow Flag Always Zero
When performing subtraction (
A-B, Control Signal = 6), the Carry/Borrow output was hardcoded to0. In real hardware, the borrow flag should be1whenB > Ato indicate that a borrow occurred.Fix: Changed from
to
Bug 2: Control Signal 3 Outputs Stale Data
When Control Signal = 3, the ALU only updated the display message to "ALU" but did not update the output value. This caused the output to retain its previous value (stale data).
Fix: Added proper output handling that sets output to
0and adds both output and carry to the simulation queue.Files Modified:
src/simulator/src/modules/ALU.jsv1/src/simulator/src/modules/ALU.jsScreenshots of the UI changes (If any) -
Bug 1 fixed :
Bug 2 fixed :
Code Understanding and AI Usage
Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?
If you used AI assistance:
Explain your implementation approach:
Problem: The ALU component had two issues affecting simulation correctness:
Alternative approaches considered:
this.inp2.value > this.inp1.valuebut+(a < b)is more idiomatic JavaScriptundefined, but0is more consistent with hardware behavior (no operation = zero output)Why this implementation:
Key changes:
this.carryOut.value = +(this.inp1.value < this.inp2.value)- Sets borrow flag to 1 when B > AsimulationQueue.add()calls for CTR=3 to properly propagate output changesChecklist before requesting a review
Note: Please check Allow edits from maintainers if you would like us to assist in the PR.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.