DEV Community

Javad
Javad

Posted on

Embedded Systems & IoT: The first journey into Verilog!

Hey There, Future Chip Designer! πŸ‘‹

What's up?! If you've ever wondered how the chips inside your phone, laptop, or even your graphics card are designed, you're in the right place! Today, we're going to learn Verilog together - the language that lets you program the actual brain of hardware!

πŸ“– What You'll Learn in This Article:

  • What the heck is Verilog and why is it so important?
  • How to think like a hardware engineer
  • Building your first Verilog module
  • Commenting and debugging code
  • Installing and setting up the simulator (IVerilog) on all major operating systems
  • The process of turning code into real chips
  • And finally, you'll be able to design actual digital circuits!

πŸ” What Exactly is Verilog?

Verilog is a Hardware Description Language (HDL) - not your typical programming language! What's the difference?

// Software programming (like C):
// "Do this" => Processor executes it

// Hardware programming with Verilog:
// "Build this circuit" => Turns into wires and transistors
Enter fullscreen mode Exit fullscreen mode

Key Difference: Software runs on processors, but Verilog DESCRIBES the processor itself! Mind blown yet? 🀯

Real-World Examples:

  • Intel's Core i9 processors: Designed with Verilog
  • NVIDIA's RTX GPUs: Verilog at the core
  • Your router's networking chip: Probably Verilog
  • Smartphone processors (Apple's A-series, Snapdragon): Verilog designs

🎯 What We Expect From You (Prerequisites)

Don't worry, we're keeping it accessible! Here's what you need:

Essential:

  1. Basic programming logic (if-else, loops concepts)
  2. Binary/Hexadecimal numbers understanding
  3. Patience - hardware thinking is different!

Nice to Have:

  • Understanding of logic gates (AND, OR, NOT)
  • Basic electronics knowledge (voltage, current)
  • Some Python/C experience (but not required)

πŸ“š Deep Dive into Prerequisites (Don't Get Stuck!)

Let me break it down so you're 100% ready:

1. Programming Logic:

You know how in games you have conditions like:

if (player_health > 0) {
    continue_game();
} else {
    game_over();
}
Enter fullscreen mode Exit fullscreen mode

That same logic applies here! Verilog just expresses it as hardware.

2. Binary/Hex Numbers:

Think of it like this:

  • Binary (0,1) = Light switch (ON/OFF)
  • Hex = Shorthand for binary (easier to read)

Example: 1010 in binary = A in hex = 10 in decimal

3. Hardware Mindset Shift:

This is the big one! In software:

x = 5;
y = x + 3;  // Happens one after another
Enter fullscreen mode Exit fullscreen mode

In hardware (Verilog):

// Everything happens AT THE SAME TIME (parallel)!
// Think of it as building multiple roads, not walking one path
Enter fullscreen mode Exit fullscreen mode

If you understand traffic flowing through multiple streets simultaneously, you understand parallel processing! πŸš—πŸš•πŸš™

πŸ’¬ How to Comment in Verilog? (Practical Code)

Comments are crucial for understanding hardware designs!

// Single-line comment (like C++/Java)
// This comment goes until the end of the line

/*
 Multi-line comment block
 Everything between these symbols is a comment
 Just like in C!
*/

module example (
    input wire clk,    // Clock signal comment
    input wire reset,  // Reset signal comment
    output reg [7:0] data  // 8-bit data output
);
    // Good practice: ALWAYS comment your hardware modules!

    // Parameters (constants) should be documented
    parameter WIDTH = 8;  // Data width is 8 bits

    // Important: Verilog comments DON'T affect synthesis
    // They're only for humans to read
endmodule
Enter fullscreen mode Exit fullscreen mode

🧱 What is a Module? (The Building Block of Hardware)

A module in Verilog is like a LEGO block of your circuit. It's a reusable component!

// Basic module structure
module module_name (
    // Port declarations (inputs/outputs)
    input wire port1,
    input wire port2,
    output reg port3
);
    // Internal logic goes here
endmodule

// Real-world example: A simple AND gate
module and_gate (
    input wire a,      // First input
    input wire b,      // Second input
    output wire result // Output
);
    // Continuous assignment
    // 'result' is ALWAYS (a & b)
    assign result = a & b;

    /* How this looks as hardware:
        a ───┐
             β”œβ”€β”€ AND ── result
        b β”€β”€β”€β”˜
    */
endmodule

// More complex example: 8-bit adder
module adder_8bit (
    input wire [7:0] a,    // 8-bit input A
    input wire [7:0] b,    // 8-bit input B
    input wire carry_in,   // Carry input
    output wire [7:0] sum, // 8-bit sum output
    output wire carry_out  // Carry output
);
    // This describes an 8-bit adder circuit
    assign {carry_out, sum} = a + b + carry_in;

    /* Hardware equivalent:
        Takes 8 wires for A, 8 for B
        Adds them together
        Produces 8-bit sum + carry flag
    */
endmodule
Enter fullscreen mode Exit fullscreen mode

⏱️ What is initial? (Your Simulation Starting Point)

The initial block runs ONCE at the beginning of simulation. It's for TESTING, not for real hardware!

module testbench;
    // Initial block - runs once when simulation starts
    initial begin
        // This is like the 'main()' function for simulation
        $display("Simulation started!");

        // Initialize signals
        test_signal = 0;
        #10;  // Wait 10 time units

        // Change signal
        test_signal = 1;
        #10;

        $display("Simulation finished!");
        $finish;
    end

    // Important: Initial blocks are NOT synthesizable!
    // They only exist in simulation/testbenches
    // Real chips don't have "initial" states in this way
endmodule
Enter fullscreen mode Exit fullscreen mode

πŸ”„ What is initial begin CODES end? (The Complete Structure)

This is how you group multiple simulation commands together:

module full_example;
    // Declare some registers for testing
    reg [3:0] counter = 0;  // 4-bit counter
    reg clk = 0;            // Clock signal

    // Initial block with multiple statements
    initial begin
        // Begin marks the start of the block

        $display("=== Test Sequence Starting ===");

        // Test case 1: Reset test
        $display("Test 1: Resetting counter...");
        counter = 0;
        #5;
        $display("Counter value: %d", counter);

        // Test case 2: Increment test
        $display("Test 2: Incrementing counter...");
        for (integer i = 0; i < 5; i = i + 1) begin
            counter = counter + 1;
            #10;
            $display("  Step %0d: Counter = %d", i, counter);
        end

        // Test case 3: Clock generation
        $display("Test 3: Generating clock pulses...");
        repeat (10) begin
            clk = ~clk;  // Toggle clock
            #5;
            $display("  Clock edge at time %0d", $time);
        end

        $display("=== All Tests Completed ===");

        // Always end with finish!
        $finish;
    end  // End marks the end of the block

    /*
    Structure breakdown:
    initial           -> Run once at start
        begin         -> Start of block
            ...codes... -> Your simulation commands
        end           -> End of block
    */
endmodule
Enter fullscreen mode Exit fullscreen mode

πŸ“ What are $display and $finish? (Your Debugging Friends)

These are system tasks - built-in Verilog commands for simulation:

module system_tasks_demo;
    reg [7:0] data = 8'hA5;  // Hex A5 = 165 decimal

    initial begin
        // $display: Print to console (like printf)
        $display("Basic message");

        // With formatting (similar to C)
        $display("Data value: %h (hex), %d (decimal)", data, data);

        // Multiple values
        reg [3:0] a = 4'b1010, b = 4'b0101;
        $display("A=%b, B=%b, A&B=%b", a, b, a & b);

        // $time gives current simulation time
        #10;
        $display("Time is now: %0d", $time);

        // Monitoring changes automatically
        $monitor("Data changed! New value: %h at time %0d", data, $time);

        // Trigger some changes
        #5 data = 8'hFF;
        #5 data = 8'h00;

        // $finish: Ends the simulation
        $display("=== Ending simulation ===");
        $finish;  // Stops the simulation

        // Nothing after $finish executes!
        $display("This never prints");
    end

    /*
    Common format specifiers:
    %d, %D - Decimal
    %b, %B - Binary
    %h, %H - Hexadecimal
    %o, %O - Octal
    %s, %S - String
    %t, %T - Time
    %m, %M - Module instance name
    */
endmodule
Enter fullscreen mode Exit fullscreen mode

πŸ› οΈ Installing & Using Icarus Verilog (IVerilog) - The Emulator

Icarus Verilog is the most popular open-source Verilog simulator. Let's install it everywhere!

Linux Installation:

# Debian/Ubuntu/Kali Linux:
sudo apt update
sudo apt install iverilog gtkwave

# Arch Linux/Manjaro:
sudo pacman -S iverilog gtkwave

# Fedora/RHEL/CentOS:
sudo dnf install iverilog gtkwave

# Verify installation:
iverilog -v  # Should show version info
Enter fullscreen mode Exit fullscreen mode

Windows Installation:

  1. Option A: Using Windows Subsystem for Linux (WSL)
# Install WSL (in PowerShell as Admin):
wsl --install

# Then install like Debian above
sudo apt install iverilog gtkwave
Enter fullscreen mode Exit fullscreen mode
  1. Option B: Native Windows Binary

macOS Installation:

# Using Homebrew (recommended):
brew install icarus-verilog gtkwave

# Using MacPorts:
sudo port install iverilog

# Verify:
iverilog -v
Enter fullscreen mode Exit fullscreen mode

BSD Systems:

# FreeBSD:
sudo pkg install iverilog

# OpenBSD:
doas pkg_add icarus-verilog

# NetBSD:
pkgin install icarus-verilog

# Note: GTKWave might need separate installation
Enter fullscreen mode Exit fullscreen mode

πŸ“ .v Files - The Magic Behind Every Verilog Source

What's a .v file?

It's a plain text file containing Verilog code, just like .c for C or .py for Python.

The Complete Workflow:

// Example: counter.v
module counter (
    input wire clk,
    input wire reset,
    output reg [3:0] count
);
    always @(posedge clk or posedge reset) begin
        if (reset) begin
            count <= 0;
        end else begin
            count <= count + 1;
        end
    end
endmodule
Enter fullscreen mode Exit fullscreen mode

How to Simulate (Emulate):

# 1. Compile your Verilog code
iverilog -o counter.vvp counter.v

# 2. Run the simulation (creates VCD file)
vvp counter.vvp

# 3. View waveforms (if you generated VCD)
gtkwave dump.vcd
Enter fullscreen mode Exit fullscreen mode

How Synthesis Works (Turning Code into Hardware):

Your Verilog Code (.v)
          ↓
   Synthesis Tool
 (Quartus, Vivado, Yosys)
          ↓
   Gate-level Netlist
 (Actual logic gates)
          ↓
   Place & Route
 (Physical layout)
          ↓
      FPGA Bitstream
   or ASIC Mask Layout
          ↓
   Program onto FPGA
   or Fabricate ASIC
Enter fullscreen mode Exit fullscreen mode

Real Example - Complete Flow:

# 1. Create your design (my_design.v)
# 2. Create testbench (testbench.v)
# 3. Compile both together
iverilog -o sim my_design.v testbench.v

# 4. Run simulation
vvp sim

# 5. Output:
# VCD file for waveforms
# Console output from $display

# For FPGA synthesis (example with open-source tools):
yosys -p "synth_ice40 -blif chip.blif" my_design.v
arachne-pnr -d 8k -p pinmap.pcf chip.blif -o chip.asc
icepack chip.asc chip.bin
# Now chip.bin can be loaded onto an FPGA!
Enter fullscreen mode Exit fullscreen mode

🎯 Call to Action: Your First Real Hardware Design!

THE CHALLENGE:

Build a 4-bit binary counter that blinks LEDs on an FPGA. You now have ALL the knowledge you need!

Your Mission (Step by Step):

  1. Create blinky_counter.v:
module blinky_counter(
    input wire clk_12mhz,      // 12MHz clock from board
    input wire reset_button,    // Reset button
    output reg [3:0] leds       // 4 LEDs
);
    reg [23:0] counter = 0;    // 24-bit counter for slowing down

    always @(posedge clk_12mhz or posedge reset_button) begin
        if (reset_button) begin
            counter <= 0;
            leds <= 0;
        end else begin
            counter <= counter + 1;

            // Update LEDs every ~0.1 seconds
            if (counter == 1200000) begin
                counter <= 0;
                leds <= leds + 1;  // Binary count on LEDs
            end
        end
    end
endmodule
Enter fullscreen mode Exit fullscreen mode
  1. Simulate it:
echo "Compiling your first hardware design..."
iverilog -o blinky blinky_counter.v testbench.v
vvp blinky
Enter fullscreen mode Exit fullscreen mode
  1. Take it to REAL hardware:
    • Buy a $20 FPGA board (Lattice iCE40, TinyFPGA)
    • Install open-source toolchain
    • Synthesize your code
    • Watch PHYSICAL LEDs blink in binary!

Why This Matters:

You're not just writing code anymore. You're designing ACTUAL ELECTRONIC CIRCUITS that could end up in real products!

Join the Hardware Revolution:

  1. Fork a GitHub repo with beginner Verilog projects
  2. Build a portfolio - even simple designs impress employers
  3. Contribute to open-source hardware (RISC-V chips need you!)
  4. Consider a career in semiconductors - the industry is BOOMING!

REMEMBER: Every tech giant (Apple, NVIDIA, Intel, AMD) started with someone writing Verilog. That someone could be YOU.

πŸ‘‹ Final Words & Farewell

Congratulations, future chip architect! πŸŽ‰

You've just taken your first steps into the incredible world of hardware design. From this point forward, you'll never look at your electronics the same way again. Every time your phone responds, your game renders, or your car's sensors detect something - you'll know there's Verilog code running the show.

Your Journey Continues Here:

  • Next Steps: Learn SystemVerilog (enhanced Verilog)
  • Project Ideas: Design a simple CPU, create custom graphics chip
  • Community: Join r/FPGA on Reddit, follow OpenCores.org
  • Career Path: Digital Design Engineer, FPGA Developer, ASIC Designer

Parting Wisdom:

"Software is limited by imagination. Hardware is limited by physics.

Verilog is where imagination meets physics." - Anonymous Chip Designer

Keep experimenting, keep building, and most importantly - have fun creating the technology of tomorrow!

Stay curious, keep coding, and go make some chips! πŸš€


Need help? Found a bug in the tutorial? Want to show off your first Verilog design?

Drop a comment below or reach out on Twitter! Let's build the future together.

Top comments (0)