Verilog Data Representation
Verilog uses a standardized format for representing numbers: n'bv, where n is the bit width, 'b is the base (e.g., binary, decimal, hexadecimal), and v is the value. This format allows for precise control over how data is stored and interpreted.
For instance, the declaration 8'hFF specifies an 8-bit hexadecimal number with a value of FF (255 in decimal). The bit width, n, defines the memory allocation. The base, such as b (binary), d (decimal), or h (hexadecimal), dictates the number system. The actual value, v, is the numerical content.
Consider the following examples:
// 8-bit binary number
8'b10101010;
// 16-bit decimal number
16'd65535;
// 4-bit hexadecimal number
4'hA;
// 12-bit number with an explicit binary base
12'b0000_1111_0000;
If the bit width is omitted, Verilog compilers typically default to a 32-bit representation. For example, the number 42 is treated as 32'd42. This implicit sizing is common but can sometimes lead to unexpected behavior if the intended width is smaller, as the compiler will not automatically truncate or extend the value in a way that might be desired for hardware synthesis.
Practical Encodings
Beyond simple numeric representation, Verilog supports specific encodings tailored for hardware design, such as ASCII and Binary-Coded Decimal (BCD).
ASCII Encoding: The American Standard Code for Information Interchange (ASCII) is an 8-bit encoding for text characters. In Verilog, a character can be represented directly using double quotes, like "A", which is equivalent to the 8-bit hexadecimal value 8'h41.
Binary-Coded Decimal (BCD): BCD is a class of binary encodings where each decimal digit is represented by a fixed number of bits, usually four. The most common is 8421 BCD, where each decimal digit (0-9) is represented by its 4-bit binary equivalent. This encodnig is "unweighted" in the sense that the position of a digit does not corrrespond to a power of the base, unlike standard binary.
BCD is particularly useful when interfacing with decimal-based systems, as it simplifies digit-wise operations. For example, adding two BCD numbers involves adding each 4-bit digit pair and handling any resulting carry, similar to manual decimal addition. However, BCD is not efficient for complex arithmetic, as it requires conversion to binary for most calculations.
The following example demonstrates a BCD addition. The numbers 57 and 28 are represented in BCD, and their sum, 85, is also in BCD format.
// BCD representation of 57
reg [7:0] bcd_57 = {4'd5, 4'd7};
// BCD representation of 28
reg [7:0] bcd_28 = {4'd2, 4'd8};
// BCD addition (57 + 28 = 85)
reg [7:0] bcd_sum;
assign bcd_sum = bcd_57 + bcd_28; // Result: {4'd8, 4'd5}
The primary advantage of BCD is its ease of use for display and input/output operations, as it maps directly to decimal digits. Its main disadvantage is the increased circuit complexity for arithmetic operations compared to pure binary.