Skip to content

Conversation

@092vk
Copy link
Member

@092vk 092vk commented Jun 29, 2025

Fixes #561

Describe the changes you have made in this PR -

  1. Added the pre condiiton logic in the verilog module
  2. Made the verilog module consistent to the circuit simulation logic

Note: Please check Allow edits from maintainers. if you would like us to assist in the PR.

Summary by CodeRabbit

  • Bug Fixes
    • Improved the T flip-flop’s asynchronous reset so preset/unknown cases are handled more accurately, reducing simulation inaccuracies.
    • Refined enable and toggle semantics so the flip-flop now reliably holds or toggles only when enabled and t is high, while maintaining complementary q/q_inv outputs after reset.

@netlify
Copy link

netlify bot commented Jun 29, 2025

Deploy Preview for circuitverse ready!

Name Link
🔨 Latest commit c9f1518
🔍 Latest deploy log https://app.netlify.com/projects/circuitverse/deploys/68aaa8e3f696ff0008831760
😎 Deploy Preview https://deploy-preview-620--circuitverse.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 46 (🔴 down 1 from production)
Accessibility: 72 (🔴 down 1 from production)
Best Practices: 92 (no change from production)
SEO: 80 (🔴 down 2 from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 29, 2025

Walkthrough

The Verilog string in TflipFlop.js was updated to change reset and toggle semantics: asynchronous reset now assigns outputs based on pre, enable gating replaces previous en==0 path, and the always block and port declarations were restructured to match simulator logic.

Changes

Cohort / File(s) Change Summary
T flip-flop Verilog update
src/simulator/src/sequential/TflipFlop.js
Rewrote the generated Verilog module string in moduleVerilog(): updated port declarations (input clk, a_rst, en; input [WIDTH-1:0] t, pre;), changed async reset to assign q = pre and q_inv = ~pre, replaced previous enable/toggle paths with explicit en-gated toggle and hold branches, wrapped always block with begin...end, and added inline comments.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Assessment against linked issues

Objective Addressed Explanation
Fix errors in Verilog modules and ensure consistency with simulator logic (#561)
Implement preset (pre) handling in flip-flop Verilog modules (#561)
Ensure flip-flop enable and toggle semantics match simulator behavior (#561)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/simulator/src/sequential/TflipFlop.js (1)

154-160: Good alignment with simulation logic, consider multi-bit syntax improvement.

The conditional preset logic correctly aligns the Verilog behavior with the JavaScript simulation logic (lines 72-74), where reset uses this.preset.value || 0. This change successfully addresses the PR objective of adding precondition logic.

For better multi-bit signal handling, consider using width-specific syntax:

- if (pre == 'bx) begin
-   q <= 'b0;
-   q_inv <= 'b1;
+ if (pre === {WIDTH{1'bx}}) begin
+   q <= {WIDTH{1'b0}};
+   q_inv <= {WIDTH{1'b1}};
 end else begin
   q <= pre;
   q_inv <= ~pre;
 end

However, since the original code used 'b0 and 'b1, maintaining consistency with the existing style is also reasonable.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8d8ca00 and e6d9ae8.

📒 Files selected for processing (1)
  • src/simulator/src/sequential/TflipFlop.js (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: 092vk
PR: CircuitVerse/cv-frontend-vue#591
File: v1/src/simulator/src/sequential/JKflipFlop.js:159-188
Timestamp: 2025-06-10T11:26:38.102Z
Learning: In the CircuitVerse project, Verilog modules for circuit elements like JKflipFlop are designed to be consistent with the circuit simulation design, which means reset and preset signals are implemented synchronously (triggered on clock edge) rather than asynchronously, even when the signals are named as asynchronous reset/preset.
src/simulator/src/sequential/TflipFlop.js (1)
Learnt from: 092vk
PR: CircuitVerse/cv-frontend-vue#591
File: v1/src/simulator/src/sequential/JKflipFlop.js:159-188
Timestamp: 2025-06-10T11:26:38.102Z
Learning: In the CircuitVerse project, Verilog modules for circuit elements like JKflipFlop are designed to be consistent with the circuit simulation design, which means reset and preset signals are implemented synchronously (triggered on clock edge) rather than asynchronously, even when the signals are named as asynchronous reset/preset.

Copy link
Member

@niladrix719 niladrix719 left a comment

Choose a reason for hiding this comment

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

lgtm

@niladrix719 niladrix719 requested a review from JoshVarga July 4, 2025 18:35
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
src/simulator/src/sequential/TflipFlop.js (3)

152-156: Asynchronous vs. synchronous reset: confirm project-wide convention.

The Verilog uses an async reset (always @(posedge clk or posedge a_rst)), while prior learnings for CircuitVerse indicate resets/presets are implemented synchronously for some modules. Your simulator-side resolve path does look asynchronous for T-FF, so this may be intentional. Please confirm whether T-FF is the intended exception or whether we should switch to synchronous reset for consistency.

If synchronous reset is desired, the change would be:

-          always @ (posedge clk or posedge a_rst) begin
-            if (a_rst) begin
+          always @ (posedge clk) begin
+            if (a_rst) begin
               q <= pre;
-              q_inv <= ~pre;
             end else if (en) begin
-              // toggle logic...
+              // toggle logic...
             end
           end

Note: If you adopt this, also ensure simulator resolve() semantics match.


154-156: Derive q_inv combinationally from q to prevent divergence.

Optional simplification: drive q_inv as ~q via a continuous assignment. This guarantees complementarity without having to remember to update q_inv on every path.

-          output reg [WIDTH-1:0] q, q_inv;
+          output reg [WIDTH-1:0] q;
+          output      [WIDTH-1:0] q_inv;
@@
-          input [WIDTH-1:0] t, pre;
+          input [WIDTH-1:0] t, pre;
+          assign q_inv = ~q;
@@
-            if (a_rst) begin
-              q <= pre;
-              q_inv <= ~pre;
+            if (a_rst) begin
+              q <= pre;
             end else if (en) begin
-              q <= q ^ t;
-              q_inv <= ~q ^ t; // == ~(q ^ t)
+              q <= q ^ t;
             end

11-17: Doc nits: terminology and typos.

  • “data input” → “T input” for T flip-flop.
  • Spelling: “ciruit” → “circuit”; “direcion” → “direction”.

Proposed adjustments:

  • Line 11: “clock, T input, preset, reset, enable.”
  • Lines 15–16: fix spelling in JSDoc params.
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between e6d9ae8 and c9f1518.

📒 Files selected for processing (1)
  • src/simulator/src/sequential/TflipFlop.js (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: 092vk
PR: CircuitVerse/cv-frontend-vue#591
File: v1/src/simulator/src/sequential/JKflipFlop.js:159-188
Timestamp: 2025-06-10T11:26:38.102Z
Learning: In the CircuitVerse project, Verilog modules for circuit elements like JKflipFlop are designed to be consistent with the circuit simulation design, which means reset and preset signals are implemented synchronously (triggered on clock edge) rather than asynchronously, even when the signals are named as asynchronous reset/preset.
📚 Learning: 2025-06-10T11:26:38.102Z
Learnt from: 092vk
PR: CircuitVerse/cv-frontend-vue#591
File: v1/src/simulator/src/sequential/JKflipFlop.js:159-188
Timestamp: 2025-06-10T11:26:38.102Z
Learning: In the CircuitVerse project, Verilog modules for circuit elements like JKflipFlop are designed to be consistent with the circuit simulation design, which means reset and preset signals are implemented synchronously (triggered on clock edge) rather than asynchronously, even when the signals are named as asynchronous reset/preset.

Applied to files:

  • src/simulator/src/sequential/TflipFlop.js
🔇 Additional comments (2)
src/simulator/src/sequential/TflipFlop.js (2)

156-166: Unconnected en/pre semantics may not match simulator defaults.

In the simulator, en unconnected behaves like 1 and pre unconnected defaults to 0 (Line 74 uses preset.value || 0). In generated Verilog, unconnected inputs become Z/X, which leads to holding state (for en) or X-propagation (for pre). Ensure the codegen ties:

  • en to 1’b1 when not wired,
  • pre to {WIDTH{1’b0}} when not wired.

I can update the exporter to auto-insert these ties at instantiation sites; confirm if desired.


149-151: Double-check port ordering consistency across modules and codegen.

You changed the declaration grouping to separate scalar and vector inputs and the module port list is (..., clk, t, a_rst, pre, en). If other elements (e.g., JK/D flip-flops) or the exporter assume a specific positional order, this could break instantiations. If named port connections are always used, we’re fine.

Run this quick scan to compare port lists across sequential modules:

Comment on lines +156 to 163
end else if (en) begin
if (t) begin
// Toggle when T input is high
q <= ~q;
q_inv <= q;
end
// When t == 0, hold current state
end
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

⚠️ Potential issue

Fix multi-bit T semantics: use per-bit XOR instead of toggling all bits.

When WIDTH > 1, if (t) q <= ~q; flips all bits whenever any bit of t is 1. Simulator logic (Line 83) uses per-bit XOR (masterState = T ^ slaveState), so the Verilog must mirror that: q <= q ^ t; and keep q_inv complementary of the new q. This is a functional mismatch for vector widths and breaks correctness.

Apply this minimal fix:

-            end else if (en) begin
-              if (t) begin
-                // Toggle when T input is high
-                q <= ~q;
-                q_inv <= q;
-              end
-              // When t == 0, hold current state
-            end
+            end else if (en) begin
+              // Per-bit toggle: only bits with T=1 flip (matches simulator XOR semantics)
+              q     <= q ^ t;
+              q_inv <= ~q ^ t; // == ~(q ^ t), ensures q_inv remains the complement of new q
+            end
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
end else if (en) begin
if (t) begin
// Toggle when T input is high
q <= ~q;
q_inv <= q;
end
// When t == 0, hold current state
end
end else if (en) begin
// Per‐bit toggle: only bits with T=1 flip (matches simulator XOR semantics)
q <= q ^ t;
q_inv <= ~q ^ t; // == ~(q ^ t), ensures q_inv remains the complement of new q
end
🤖 Prompt for AI Agents
In src/simulator/src/sequential/TflipFlop.js around lines 156-163, the code
toggles the entire vector when any bit of T is high; replace the scalar toggle
with per-bit XOR so multi-bit T works: set q <= q ^ t; and set q_inv to be the
bitwise complement of the resulting q (q_inv <= ~(q ^ t)) so q_inv remains the
complement of the updated q.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix the errors in the Verilog module of the circuit elements

2 participants