防止下溢和上溢

Preventing Underflow and Overflow

  module threshold(input[7:0] oLCD_R1,
                  input[7:0] oLCD_G1,
                  input[7:0] oLCD_B1,
                  input[7:0] Rcapture1,
                  input[7:0] Gcapture1,
                  input[7:0] Bcapture1,
                  input oDEN1,
                  output reg[7:0] oLCD_R2,
                  output reg[7:0] oLCD_G2,
                  output reg[7:0] oLCD_B2,
                  output oDEN2,
                  output  [7:0] Rlower,
                  output  [7:0] Rupper,
                  output  [7:0] Glower,
                  output  [7:0] Gupper,
                  output  [7:0] Blower,
                  output  [7:0] Bupper
                );
assign Rlower = Rcapture1;
assign Rupper = Rcapture;
assign oDEN2 = oDEN1;

always @(*)
begin
     if (Rcaputre1 < 30)
     begin
     Rlower = 30;
     end
     if (Rcaputre1 > 225)
       begin
       Rupper = 225;
      end
end 
  begin
  if (
      ( ( Rcapture1 - 30  < oLCD_R1)  &&  (oLCD_R1 < Rcapture1 + 30 ) ) && 
      ( ( Gcapture1 - 30  < oLCD_G1)  &&  (oLCD_G1 < Gcapture1 + 30 ) ) &&
      ( ( Bcapture1 - 30  < oLCD_B1)  &&  (oLCD_B1 < Bcapture1 + 30 ) )
    )   

  begin
 oLCD_R2 = 255;
 oLCD_G2 = 192;
 oLCD_B2 = 0;
 end 
    else 
    begin   
     oLCD_R2 = oLCD_R1;
     oLCD_G2 = oLCD_G1;
     oLCD_B2 = oLCD_B1;
     end 
  end
 endmodule

我试图在我的 Rcapture 的减法和加法过程中防止下溢和溢出,但由于赋值左侧的对象 'Rlower' 的错误,它似乎无法工作必须具有网络类型?我将其声明为 output reg 为什么 RlowerRupper

仍然出现此错误

所以通过更改语句

assign Rlower = Rcapture1 ? Rcapture1 < 30  : Rlower == 30;
assign Rupper = Rcapture1 ? Rcapture1 < 225 : Rupper == 225;

assign Glower = Gcapture1 ? Gcapture1 < 30  : Glower == 30;
assign Gupper = Gcapture1 ? Gcapture1 < 225 : Gupper == 225;

assign Blower = Bcapture1 ? Bcapture1 < 30  : Blower == 30;
assign Bupper = Bcapture1 ? Bcapture1 < 225 : Bupper == 225;

这会解决下溢和上溢问题吗?

regs(和 output regs)在 always 块中被赋值。声明为 wireoutput 的信号被认为是 "net-type",并使用 assign 语句为它们赋值。

要么将 Rlower 移动到 always 块中,要么从声明中删除 reg。同样适用于 Rupper.