如何在 Verilog 中获取信号幅度

How to get magnitude of signals in Verilog

我有一个模块Magnitude:

module Magnitude(
  input [31:0] re,
  input [31:0] im,
  output [31:0] out
);
  assign out = re * re + im * im;
endmodule

现在,对于 128 个信号,我需要找出幅度。也就是说,我需要一种数到 128 的方法。我该怎么做?

此外,这是我编写的第一个 Verilog 代码。欢迎任何关于提高效率的建议。

根据我对你的问题的理解,你可能打算做以下事情:

module Magnitude(
  input [31:0] re,
  input clk;
  input reset;
  input [31:0] im,
  output reg [31:0] out
);

reg [6:0] counter;

 always @(posedge clk or negedge reset)
 begin
  if(!reset)
   begin
   counter<=0;
   end
  else 
    begin
     if (counter==7'd128)
       counter<=0;
     else
       counter<=counter+1;
    end
 end

 always @(posedge clk or negedge reset)
 begin
   if(!reset)
     out<=0;
   else 
    begin
     if (counter==7'd127)
       out<=0;            // As soon as counter counts to 128, it will become zero.
     else
       out <= out + re * re + im * im;
    end
 end
endmodule