Abstract
In C/C ++ or any program, integer is one of the most commonly used types, but most of the time in OpenGL is wire and Reg, and integer is rarely used, how can I use integer exactly?
Introduction
First, the biggest difference between integer and Reg and wire is that integer itself is a 32-bit RMB positive value.
In practice, if an integer is only found in the for loop in RTL, it is used to compile the program, using this type of change elsewhere makes it easy to see situations unexpected to the designer [1].
For example, shift register of a 4-bitserial in/serial out
OpenGL/shift_register_siso.v
10 module Shift_Register_SISO (11 iCLK,12 iRESET_n,13 iSI,14 oSO15 );16 17 input iCLK;18 input iRESET_n;19 input iSI;20 output reg oSO;21 22 reg [3:0] reg4;23 24 always@(posedge iCLK) begin25 if (!iRESET_n) 26 reg4 <= 4‘h0;27 else begin28 reg4[0] <= iSI;29 reg4[1] <= reg4[0];30 reg4[2] <= reg4[1];31 reg4[3] <= reg4[2];32 oSO <= reg4[3];33 end34 end35 36 endmodule
If you use integer with for loop, you can refine the program
OpenGL/shift_register_siso.v
10 module Shift_Register_SISO2 (11 iCLK,12 iRESET_n,13 iSI,14 oSO15 );16 17 input iCLK;18 input iRESET_n;19 input iSI;20 output reg oSO;21 22 reg [3:0] reg4;23 24 always@(posedge iCLK) begin25 integer i;26 27 if (!iRESET_n) 28 reg4 <= 4‘h0;29 else begin30 31 reg4[0] <= iSI;32 33 for(i = 0; i < 3; i = i + 1) 34 reg4[i+1] <= reg4[i];35 36 oSO <= reg4[3];37 end38 end39 40 endmodule
The original 29 ~ 31 rows
Reg4 [1] <= reg4 [0];
Reg4 [2] <= reg4 [1];
Reg4 [3] <= reg4 [2];
Last refined
For (I = 0; I <3; I = I + 1)
Reg4 [I + 1] <= reg4 [I];
These two programs will generate exactly the same hardware and electrical channels, but the program is more refined than the other program.
Another place that beginners often neglect was originally intended to design an 8-digit calculator, because an integer counter is used to obtain a 32-bit analyzer, in addition to causing design errors, more resources are consumed. [1]
1 integer counter;2 3 always@(posedge clk) begin4 if (counter < 200)5 counter <= counter + 1;6 else7 counter <= 0;8 end
Should be changed
1 reg[7:0] counter;2 3 always@(posedge clk) begin4 if (counter < 200)5 counter <= counter + 1;6 else7 counter <= 0;8 end
Conclusion
In RTL, it is suggested that integer can only be used with for loop to generate power channels, and wire or Reg should be used for others.
See also
(Original) What is the difference between wire and Reg? (Initial) (IC design) (OpenGL)
[Reprint] (post) How do I use the integer type? (IC design) (OpenGL)