Knowledge / Electronics and Communication Engineering / Verilog and HDL basics

Verilog and HDL basics

Electronics and Communication Engineering (B.E./B.Tech) · Digital electronics · free study notes for exams, placements and interviews

In short

A hardware description language (HDL) describes digital hardware as text so that it can be simulated, verified and then automatically synthesised into gates on an ASIC or FPGA. Verilog (IEEE 1364, now largely superseded by SystemVerilog IEEE 1800) is the dominant HDL in Indian industry; VHDL (IEEE 1076) is the other. The central skill examined is writing *synthesizable* RTL - code whose meaning in simulation matches the hardware the tool builds - and knowing where simulation-only constructs stop. It appears in GATE EC/EE digital questions, in every university digital-design and VLSI lab, and in the first technical round at Intel India, Qualcomm, Texas Instruments Bengaluru, Samsung SRI-B, NXP, Micron, Nvidia, Synopsys, Cadence, BEL, ISRO, SCL Mohali and VLSI services firms such as Tessolve, L&T Technology Services and Sankalp.

Explained

What an HDL is, and the Verilog design flow

Lexical elements, data types, nets versus variables

Operators, expression width and precedence

Modelling styles

Procedural blocks, blocking versus non-blocking, inference rules

Tasks, functions, testbenches and the simulation model

Synthesizable subset, guidelines, and Verilog versus VHDL/SystemVerilog

VerilogVHDL
Origin / standardGateway 1984, IEEE 1364US DoD 1980s, IEEE 1076
StyleC-like, terseAda-like, verbose
Case sensitivityCase sensitiveNot case sensitive
Type checkingWeak (silent truncation)Strong, explicit conversions
Data typesFew, built-in, 4-stateRich, user-defined, std_logic
Learning curveEasier to startStricter, catches more at compile
Typical useASIC/front-end, verification, most Indian product firmsDefence, aerospace, European and some PSU/FPGA work
Aspectwirereg
Assigned byassign, module output, primitiveProcedural = / <=
Holds valueNo (needs a driver)Yes, until reassigned
Multiple driversAllowed (resolved)Not allowed
Hardware inferredNet / combinational logicFlop, latch or just wiring

Key points

Formulas and examples

Bits for N distinct values: n = ⌈log₂ N⌉ ; maximum unsigned value = 2ⁿ − 1 (n in bits)
Two's-complement range for n bits: −2n−1 to +2n−1 − 1
Value of a sized literal s'bB = Σ bᵢ·2ⁱ, then truncated or zero-extended to s bits
Memory bits for reg [W-1:0] mem [0:D-1] = D × W bits (D = depth, W = word width in bits)
Unsigned wrap-around: result = (exact value) mod 2ⁿ
Clock divider by toggling: terminal count = f_in / (2 × f_out) − 1 (f in Hz)
Simulated delay = (#value × time_unit), rounded to the nearest precision step of timescale

1. Literal width, truncation and replication

2. The unsigned-subtraction trap

reg [7:0] y;  // 8-bit unsigned
y = 3'd2 - 3'd5;        // operands widen to 8 bits, unsigned

3. Blocking versus non-blocking - trace it Start with a = 1, b = 2.

always @(posedge clk) begin a = b; b = a; end   // blocking
always @(posedge clk) begin a <= b; b <= a; end // non-blocking
StyleStepabHardware
Blockingafter a = b22one flop; b feeds a, then a feeds b
Blockingafter b = a22swap fails
Non-blockingRHS sampled12two flops, cross-coupled
Non-blockingNBA update21swap succeeds

4. Parameterised counter and clock divider on a 50 MHz FPGA board

module clkdiv #(parameter integer TC = 24_999_999, parameter integer W = 25)
  (input wire clk, input wire rst_n, output reg slow);
  reg [W-1:0] cnt;
  always @(posedge clk or negedge rst_n)
    if (!rst_n)      begin cnt <= {W{1'b0}}; slow <= 1'b0; end
    else if (cnt == TC) begin cnt <= {W{1'b0}}; slow <= ~slow; end
    else                cnt <= cnt + 1'b1;
endmodule

5. Timescale rounding and memory size

6. Latch versus multiplexer - the same intent, two results

// infers a LATCH on y: no else branch
always @(*) if (sel) y = a;

// clean 2:1 MUX: default assignment covers every path
always @(*) begin y = b; if (sel) y = a; end

Where it is used

Interview questions

Common mistakes

Related topics

Combinational circuits · Sequential circuits: flip-flops, counters and registers · Finite state machine design · Programmable logic: PLDs, CPLDs and FPGAs · Number systems and Boolean algebra · Microprocessors and embedded system design

Open in the Knowledge reader →Find jobs →