-
Notifications
You must be signed in to change notification settings - Fork 153
added verilog module for LSB #621
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
Open
092vk
wants to merge
1
commit into
CircuitVerse:main
Choose a base branch
from
092vk:LSB
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.
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
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Critical logic error: Verilog implementation finds MSB instead of LSB.
The Verilog implementation has two critical issues:
Logic Error: The for-loop iterates from MSB to LSB (
WIDTH-1
down to0
), which finds the Most Significant Bit, not the Least Significant Bit. This contradicts the JavaScript implementation that correctly finds the LSB.Synthesis Issue: The
break
statement is not synthesizable in Verilog and will cause synthesis failures.Apply this diff to fix both issues:
This ensures the module correctly finds the least significant set bit and uses synthesizable Verilog constructs.
🤖 Prompt for AI Agents
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.
can you resolve this
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.
@niladrix719, I'll resolve this issue by fixing the Verilog implementation to correctly find the LSB and use synthesizable code.
Task: Fix LSB Verilog Implementation
break
statement with synthesizable loop controlThe fix addresses both critical issues:
i = 0; i < WIDTH; i = i + 1
) to find the LSB correctlybreak
statement withi = WIDTH
to exit the loop in a synthesizable wayThis ensures the module correctly finds the least significant set bit position and can be synthesized properly.
🧠 Learnings used
⏳ Preparing changes...
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.
A summary of the changes CodeRabbit can apply:
A summary of the context that CodeRabbit is considering across the codebase:
Show context details
Generated Script:
67: resolve() {
68- const inp = dec2bin(this.inp1.value)
69- let out = 0
70- for (let i = inp.length - 1; i >= 0; i--) {
71- if (inp[i] === '1') {
72- out = inp.length - 1 - i
73- break
74- }
75- }
76- this.output1.value = out
77- simulationArea.simulationQueue.add(this.output1)
78- if (inp != 0) {
79- this.enable.value = 1
80- } else {
81- this.enable.value = 0
82- }
83- simulationArea.simulationQueue.add(this.enable)
84- }
grep -n -A 15 "static moduleVerilog" src/simulator/src/modules/LSB.js
124: static moduleVerilog() {
125- return
126- module LSB(out, en, inp); 127- parameter WIDTH = 1; 128- output reg [WIDTH-1:0] out; 129- output reg en; 130- input [WIDTH-1:0] inp; 131- 132- integer i; 133- 134- always @(*) begin 135- out = 0; 136- en = 0; 137- for (i = WIDTH-1; i >=0; i = i - 1) begin 138- if (inp[i]) begin 139- out = WIDTH-1-i; 140- en = 1; 141- break; 142- end 143- end 144- end 145- endmodule
146- }
rg -A 15 -B 5 "for.*i.*WIDTH" src/simulator/src/modules/
src/simulator/src/modules/LSB.js- integer i;
src/simulator/src/modules/LSB.js-
src/simulator/src/modules/LSB.js- always @(*) begin
src/simulator/src/modules/LSB.js- out = 0;
src/simulator/src/modules/LSB.js- en = 0;
src/simulator/src/modules/LSB.js: for (i = WIDTH-1; i >=0; i = i - 1) begin
src/simulator/src/modules/LSB.js- if (inp[i]) begin
src/simulator/src/modules/LSB.js- out = WIDTH-1-i;
src/simulator/src/modules/LSB.js- en = 1;
src/simulator/src/modules/LSB.js- break;
src/simulator/src/modules/LSB.js- end
src/simulator/src/modules/LSB.js- end
src/simulator/src/modules/LSB.js- end
src/simulator/src/modules/LSB.js- endmodule`
src/simulator/src/modules/LSB.js- }
src/simulator/src/modules/LSB.js-}
src/simulator/src/modules/LSB.js-
src/simulator/src/modules/LSB.js-/**
src/simulator/src/modules/LSB.js- * @memberof LSB
src/simulator/src/modules/LSB.js- * Help Tip
src/simulator/src/modules/LSB.js- * @type {string}
</generated_script>
Let me verify the current Verilog implementation structure to ensure my fix targets the correct lines. ```shell grep -n -A 15 "static moduleVerilog" src/simulator/src/modules/LSB.js ``` 124: static moduleVerilog() { 125- return ` 126- module LSB(out, en, inp); 127- parameter WIDTH = 1; 128- output reg [WIDTH-1:0] out; 129- output reg en; 130- input [WIDTH-1:0] inp; 131- 132- integer i; 133- 134- always @(*) begin 135- out = 0; 136- en = 0; 137- for (i = WIDTH-1; i >=0; i = i - 1) begin 138- if (inp[i]) begin 139- out = WIDTH-1-i; 140- en = 1; 141- break; 142- end 143- end 144- end 145- endmodule` 146- } I should check if there are any other Verilog modules that use similar loop patterns to understand the synthesizable coding style used in this project. ```shell rg -A 15 -B 5 "for.*i.*WIDTH" src/simulator/src/modules/ ``` src/simulator/src/modules/LSB.js- integer i; src/simulator/src/modules/LSB.js- src/simulator/src/modules/LSB.js- always @(*) begin src/simulator/src/modules/LSB.js- out = 0; src/simulator/src/modules/LSB.js- en = 0; src/simulator/src/modules/LSB.js: for (i = WIDTH-1; i >=0; i = i - 1) begin src/simulator/src/modules/LSB.js- if (inp[i]) begin src/simulator/src/modules/LSB.js- out = WIDTH-1-i; src/simulator/src/modules/LSB.js- en = 1; src/simulator/src/modules/LSB.js- break; src/simulator/src/modules/LSB.js- end src/simulator/src/modules/LSB.js- end src/simulator/src/modules/LSB.js- end src/simulator/src/modules/LSB.js- endmodule` src/simulator/src/modules/LSB.js- } src/simulator/src/modules/LSB.js-} src/simulator/src/modules/LSB.js- src/simulator/src/modules/LSB.js-/** src/simulator/src/modules/LSB.js- * @memberof LSB src/simulator/src/modules/LSB.js- * Help Tip src/simulator/src/modules/LSB.js- * @type {string}<shell_result>
67: resolve() {
68- const inp = dec2bin(this.inp1.value)
69- let out = 0
70- for (let i = inp.length - 1; i >= 0; i--) {
71- if (inp[i] === '1') {
72- out = inp.length - 1 - i
73- break
74- }
75- }
76- this.output1.value = out
77- simulationArea.simulationQueue.add(this.output1)
78- if (inp != 0) {
79- this.enable.value = 1
80- } else {
81- this.enable.value = 0
82- }
83- simulationArea.simulationQueue.add(this.enable)
84- }
</shell_result>
Now I understand the issue completely. The JavaScript implementation correctly finds the LSB by iterating from MSB to LSB and calculating the position as
inp.length - 1 - i
, but the Verilog implementation incorrectly mimics this approach without accounting for the different indexing logic needed in Verilog.The fix should:
i
directly instead of calculatingWIDTH-1-i
break
withi = WIDTH
to exit the loop