You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Verilog连续赋值中wire变量引发非法左值问题求助

Alright, let's figure out why you're hitting that "Illegal left-hand side in continuous assignment" error and fix your Hamming code module properly. There are two main issues in your code that we need to address:

1. Why assign Y[b] = ~Y[b]; is invalid

In Verilog, continuous assignments (assign) require the left-hand side to be a static, synthesizable target. You can't use a variable like b to dynamically index a wire bit as the left-hand side—this is a software-style operation that doesn't map directly to hardware. When you replace b with a hardcoded number (like 3), Y[3] is a static, known bit position, so the compiler accepts it. But using a variable index here isn't allowed in continuous assignment.

2. Your b assignment logic is broken

Your three assign b = ... statements run concurrently, which means the last one will overwrite the previous two completely—this isn't how hardware works. The correct way to calculate the error bit position b is to use the XOR of your parity checks: each S[n] != P[n] tells you whether the corresponding bit position (weight 2^(n-1)) is part of the error. We can directly construct b from these XOR results.

Fixed Hamming Code Module

Here's the corrected code with explanations of key changes:

// Hamming code 1-bit error correction
module HCG(I,e,O);
input [4:1] I; // input BCD
input [7:1] e; // noise simulation
wire [7:1] X; // Hamming code
wire [7:1] Y; // Hamming code after addition of noise
wire [3:1] P; // Parity at start
wire [3:1] S; // Parity at end
wire [2:0] b; // Updated to 3-bit wire (covers error positions 0-7)
output [4:1] O; // corrected output
wire [7:1] Y_corrected; // New wire to hold corrected Hamming code

// Hamming code generator (unchanged)
assign X[1]=I[1]^I[2]^I[4]; 
assign X[2]=I[1]^I[3]^I[4];
assign X[3]=I[1];
assign X[4]=I[2]^I[3]^I[4];
assign X[5]=I[2];
assign X[6]=I[3];
assign X[7]=I[4];

// Parity at start (unchanged)
assign P[1]=X[1]; 
assign P[2]=X[2];
assign P[3]=X[4];

// Add noise to Hamming code (unchanged)
assign Y[1]=e[1]^X[1]; 
assign Y[2]=e[2]^X[2];
assign Y[3]=e[3]^X[3];
assign Y[4]=e[4]^X[4];
assign Y[5]=e[5]^X[5];
assign Y[6]=e[6]^X[6];
assign Y[7]=e[7]^X[7];

// Calculate end parity (unchanged)
assign S[1]=Y[3]^Y[5]^Y[7]; 
assign S[2]=Y[3]^Y[6]^Y[7];
assign S[3]=Y[5]^Y[6]^Y[7];

// Correctly compute error bit position: XOR S and P, then combine into binary
assign b = {S[3]^P[3], S[2]^P[2], S[1]^P[1]};

// Generate corrected Hamming code: flip the error bit if b != 0, else keep Y
assign Y_corrected = (b == 3'd1) ? {Y[7:2], ~Y[1]} :
                     (b == 3'd2) ? {Y[7:3], ~Y[2], Y[1]} :
                     (b == 3'd3) ? {Y[7:4], ~Y[3], Y[2:1]} :
                     (b == 3'd4) ? {Y[7:5], ~Y[4], Y[3:1]} :
                     (b == 3'd5) ? {Y[7:6], ~Y[5], Y[4:1]} :
                     (b == 3'd6) ? {Y[7], ~Y[6], Y[5:1]} :
                     (b == 3'd7) ? {~Y[7], Y[6:1]} :
                     Y; // No error: output original Y

// Assign corrected outputs using Y_corrected
assign O[1]=Y_corrected[3]; 
assign O[2]=Y_corrected[5];
assign O[3]=Y_corrected[6];
assign O[4]=Y_corrected[7];
endmodule

Key Changes Explained

  • Updated b to 3-bit wire: Error positions range from 0 (no error) to 7, so we need 3 bits to represent this.
  • Fixed b calculation: We directly construct b from the XOR of S and P—this correctly maps parity mismatches to the error bit position.
  • Added Y_corrected: Instead of trying to dynamically modify Y, we create a new wire that conditionally flips the correct bit based on b. This is synthesizable and follows Verilog hardware rules.
  • Switched outputs to Y_corrected: Ensures we're using the error-corrected Hamming code for the final output.

内容的提问来源于stack exchange,提问作者Joe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:08:56