Integer And Real
Part of the Fundamentals section of Coddy's Verilog journey — lesson 8 of 90.
In addition to wire and reg, Verilog has other data types for numbers with different purposes.
Integer
integer is a general-purpose variable for counting and calculations.
- Default size is 32 bits
- Signed (can store negative numbers)
- Used mainly in testbenches and for loops
Declaring an integer:
integer i; // 32-bit signed variable
integer count; // Can store negative numbersUsing integer:
integer i;
initial begin
i = 0; // Start at 0
i = i + 1; // Count up
i = -5; // Can store negative
$display("i = %d", i); // Prints: i = -5
endReal
real is for floating-point numbers (numbers with decimals).
- Stores fractional values
- Used in testbenches for calculations
- Cannot be synthesized to hardware
Declaring a real:
real pi; // Floating-point variable
real voltage;Using real:
real pi;
initial begin
pi = 3.14159; // Decimal value
pi = 22.0 / 7.0; // Division result
$display("pi = %f", pi); // Prints: pi = 3.142857
endWhere to Use Them
In testbenches: Integers and reals are commonly used for loop counters, calculations, and checking expected values. They make test code easier to write and understand.
In hardware: These types are usually not synthesizable—meaning they cannot be turned into actual circuits. For hardware that will go on an FPGA or chip, use reg and wire instead.
Challenge
Complete the code to make the testbench work.
What to do:
- Declare an integer called i (for loop counter)
- Declare a real called value (for calculation)
Cheat sheet
integer – 32-bit signed variable for counting/calculations (testbenches, loops):
integer i;
integer count;
initial begin
i = 0;
i = i + 1;
i = -5; // can store negatives
endreal – floating-point variable for fractional values (testbenches only, not synthesizable):
real pi;
initial begin
pi = 3.14159;
pi = 22.0 / 7.0;
$display("pi = %f", pi);
endBoth integer and real are used in testbenches only. For synthesizable hardware, use reg and wire.
Try it yourself
module test;
// Declare an integer called i
// Declare a real called value
initial begin
i = 7;
value = i * 0.5;
$display("i = %0d, value = %f", i, value);
$finish;
end
endmoduleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic