Tutorial: a counter from scratch

In this tutorial you write a 4-bit counter yourself, prove that it works, and turn it into a chip layout. It takes about fifteen minutes. You type two short Verilog files, and Vulkos writes the configuration and runs the tools.

What you will build#

A counter is a small circuit that holds a number and adds one to it on every tick of a clock. Yours holds 4 bits, so it counts from 0 to 15 and then wraps back to 0. It has two controls:

  • rst (reset) puts the count back to 0.
  • en (enable) decides whether it counts. While en is low the count holds still.

You will go through the same loop you use on every design:

  • Write the design in Verilog.
  • Test it in a simulator, with a second file called a testbench that drives the inputs and checks the outputs.
  • Build it with LibreLane, the tool that turns Verilog into a physical layout.
  • Read the result: is it fast enough, and does the layout match the design?

Rather not type?

The Quickstart does the same journey from a ready-made template, with no typing. This page starts from an empty project so you see where every file comes from.

Part 1: a blank project#

  1. 1

    Create a blank chip project

    Go to My Work and choose Create project, then Chip project. Name it Counter, leave Start with on Blank, and press Create project.
The New chip project page with Counter typed in the Name field and Blank chosen under Start with. Cancel and a green Create project button are at the top right.
A blank project is an empty workspace. You add the files.

The IDE opens with an empty file list on the left and an empty editor in the middle. The green button at the top right is the Run button. It runs whichever tool is chosen in the Tools menu, and its label changes to say what it will do.

Part 2: write the counter#

  1. 1

    Add the design file

    Press the New file button above the file list, type src/counter_en.v and press Enter. Typing the folder as part of the name creates the src folder too. Paste this in, or use the Copy button on the block:
src/counter_en.v
1module counter_en (
2    input  wire       clk,
3    input  wire       rst,
4    input  wire       en,
5    output reg  [3:0] count
6);
7
8    always @(posedge clk) begin
9        if (rst)
10            count <= 4'd0;
11        else if (en)
12            count <= count + 4'd1;
13    end
14
15endmodule

How to read it:

  • module counter_en ( ... ); names the circuit and lists its pins. Three are inputs. One, count, is a 4-bit output: [3:0] means bits 3 down to 0.
  • always @(posedge clk) means "do this on every rising edge of the clock". Everything inside happens once per tick, which is what makes this a register (a circuit that remembers).
  • The if gives reset the first say. If rst is high the count becomes 0. Otherwise, if en is high, it goes up by one. If neither is true nothing is assigned, so the count keeps its value.
  • 4'd1 is the number 1 written as 4 bits. Adding 1 to 15 in 4 bits gives 0, which is the wrap.

Your work saves on its own. The file list tags the file top: Vulkos has worked out that counter_en is the top module, the outermost one that the tools start from.

Part 3: write the testbench#

A testbench is a second Verilog module that is never built into the chip. It creates your counter, wiggles its inputs, and checks what comes out. This one checks itself and prints a verdict, so you do not have to read a waveform to know whether the design works.

  1. 1

    Add the testbench file

    Press New file again, type tb/counter_en_tb.v and press Enter. Paste this in:
tb/counter_en_tb.v
1`timescale 1ns/1ps
2
3module counter_en_tb;
4
5    reg        clk = 0;
6    reg        rst = 1;
7    reg        en  = 0;
8    wire [3:0] count;
9    integer    errors = 0;
10
11    counter_en dut (.clk(clk), .rst(rst), .en(en), .count(count));
12
13    // A clock with a 10 ns period.
14    always #5 clk = ~clk;
15
16    // Record every signal in a waveform file.
17    initial begin
18        $dumpfile("counter_en_tb.vcd");
19        $dumpvars(0, counter_en_tb);
20    end
21
22    // Compare count with what we expect and say which check it was.
23    task check(input [3:0] expected, input [127:0] label);
24        begin
25            if (count !== expected) begin
26                $display("ERROR %0s: expected %0d, got %0d at %0t", label, expected, count, $time);
27                errors = errors + 1;
28            end else begin
29                $display("ok    %0s: count = %0d", label, count);
30            end
31        end
32    endtask
33
34    initial begin
35        // Hold reset across two rising edges, then release it on a falling edge.
36        repeat (2) @(posedge clk);
37        @(negedge clk) rst = 0;
38
39        @(posedge clk); #1 check(4'd0, "idle, en=0");
40
41        en = 1;
42        repeat (3) @(posedge clk);
43        #1 check(4'd3, "after 3 clocks");
44
45        en = 0;
46        repeat (2) @(posedge clk);
47        #1 check(4'd3, "held, en=0");
48
49        en = 1;
50        repeat (13) @(posedge clk);
51        #1 check(4'd0, "wrapped past 15");
52
53        rst = 1;
54        @(posedge clk);
55        #1 check(4'd0, "reset to 0");
56
57        if (errors == 0) $display("VULKOS_RESULT: PASS");
58        else             $display("VULKOS_RESULT: FAIL (%0d checks failed)", errors);
59        $finish;
60    end
61
62endmodule

The parts that matter:

  • counter_en dut (...) creates one counter and wires it to the testbench's own signals. DUT stands for design under test.
  • always #5 clk = ~clk; flips the clock every 5 ns, which makes a 10 ns clock.
  • $dumpfile and $dumpvars record every signal to a waveform file, which you open in Part 5.
  • The check task compares count with the value you expect and prints ok or ERROR.
  • The last lines print VULKOS_RESULT: PASS or VULKOS_RESULT: FAIL. Vulkos looks for that line to decide what the run badge says.

The file list tags this one testbench, and the Run button now reads Simulate. With no testbench in the project it read Lint, because there was nothing to simulate with.

Part 4: simulate it#

  1. 1

    Press Simulate

    The Terminal tab opens at the bottom. After a few seconds the output appears under the heading VERILATOR OUTPUT. Verilator is the simulator a new project uses.
Terminal
ok    idle, en=0: count = 0
ok    after 3 clocks: count = 3
ok    held, en=0: count = 3
ok    wrapped past 15: count = 0
ok    reset to 0: count = 0
VULKOS_RESULT: PASS
- tb/counter_en_tb.v:59: Verilog $finish

Five checks, five ok lines, and a pass. The badge beside the Inspect menu reads Passed.

The IDE after Simulate. The file list on the left shows src/counter_en.v tagged top and tb/counter_en_tb.v tagged testbench. The editor shows the counter. The Terminal tab below shows five ok lines and VULKOS_RESULT: PASS in green, and the badge in the top bar reads Passed.
A passing simulation: five ok lines in the Terminal and a Passed badge in the top bar.

Part 5: look at the waveform#

The verdict tells you it works. The waveform shows you how: every signal drawn against time.

  1. 1

    Open the waves

    Open Inspect and choose Open waves in panel. A Waves tab opens beside the Terminal.
  2. 2

    Add the signals

    Under Scopes, click the arrow beside TOP, then click counter_en_tb. Press the + beside the Filter box to add every signal in it.
The Waves tab after simulating counter_en_tb. The signals clk, count, en, errors and rst are drawn over time: count steps to 3, holds while en is low, then counts up and wraps to 0.
Read it left to right. count only moves while en is high, and it stops at 3 while en is low.

Follow count along the row. It sits at 0 while rst is high. It goes 1, 2, 3 once en rises, holds at 3 for the two clocks where en drops, then runs up through f (15 in hexadecimal) and wraps to 0. That is the behaviour you wrote.

Part 6: break it on purpose#

A testbench earns its keep when something is wrong. Make a mistake and watch it get caught.

  1. 1

    Add a bug

    In src/counter_en.v, change count + 4'd1 to count + 4'd2, so the counter goes up in twos.
  2. 2

    Press Simulate again

    The Terminal now shows:
Terminal
ok    idle, en=0: count = 0
ERROR after 3 clocks: expected 3, got 6 at 56000
ERROR held, en=0: expected 3, got 6 at 76000
ok    wrapped past 15: count = 0
ok    reset to 0: count = 0
VULKOS_RESULT: FAIL (2 checks failed)
The IDE after simulating the counter with the bug. Line 12 of the editor reads count plus 4'd2. The Terminal shows two red ERROR lines, expected 3, got 6, and VULKOS_RESULT: FAIL (2 checks failed). The badge in the top bar reads Tests failed in red.
Two checks fail, the lines say what was expected, and the badge reads Tests failed.

Each error names the check, the value it wanted, the value it got and the time. After three clocks the count is 6, not 3, which points straight at the adding.

A check can pass by luck

Look at wrapped past 15. It still says ok, because counting in twos from 6 for 13 clocks also lands on 0. One passing check does not prove a design. Several checks that look at it from different sides do.
  1. 3

    Put it right

    Change 4'd2 back to 4'd1 and press Simulate once more. The badge returns to Passed.

Part 7: build the chip#

Now turn the working Verilog into a layout. LibreLane does this in stages: it maps your code to real logic gates (synthesis), places those gates on the silicon, builds the clock wiring, routes every wire, and then checks the result.

  1. 1

    Choose LibreLane

    Open Tools and choose LibreLane. The Run button now reads Run LibreLane.
  2. 2

    Press Run LibreLane, then Generate config.json

    LibreLane needs a settings file called config.json, and the project does not have one yet. A dialog says so. Press Generate config.json.
A dialog titled config.json required. It says LibreLane needs a config.json before it can run your design through the flow, and offers Not now and a green Generate config.json button.
Vulkos writes the config for you from the design.

Vulkos reads your design and writes the file. These are the lines that describe your counter. The rest are defaults you can leave alone:

config.json (the lines that matter)
1"DESIGN_NAME": "counter_en",
2"VERILOG_FILES": [
3  "dir::src/counter_en.v"
4],
5"CLOCK_PORT": "clk",
6"CLOCK_PERIOD": 20,
7"DIE_AREA": "0 0 161.00 111.52",
  • DESIGN_NAME is your top module, and VERILOG_FILES lists the design files. The testbench is left out, because it is not part of the chip.
  • CLOCK_PORT is the clock pin. CLOCK_PERIOD is the time between ticks you are asking for, in nanoseconds: 20 ns is 50 MHz.
  • DIE_AREA is the size of the silicon in microns: 161 by 111.52, the smallest Tiny Tapeout tile.
  1. 3

    Press Run LibreLane again

    The Terminal lists each stage as it finishes: Lint, Synthesis, Floorplanning, Placement, Cts, Routing, Antenna, Timing and the final checks. The badge counts the steps. This run took about a minute and a half. You can keep reading or editing while it goes.

Part 8: read the result#

When the badge reads Completed, new tabs appear beside the Terminal: Problems, Schematic, Layout, Timing, Power and Signoff. Three of them answer the questions that matter.

Is it fast enough? Open Timing.

The Timing tab. The headline reads Meets timing at SS 100°C 1.60V (max). Three blocks follow: Setup, Met, with a worst slack of 14.17 ns; Hold, Met, with 0.91 ns; and Design rules, Met, with no violations.
The Timing tab on the counter. Every block says Met.
  • Meets timing is the verdict, given for the slowest conditions the chip must work in.
  • Setup asks whether a signal gets from one register to the next before the next tick. Slack is the time to spare. A worst slack of 14.17 ns out of a 20 ns period means the slowest path needs under 6 ns, so this counter could run much faster than you asked.
  • Hold asks the opposite: that a signal does not change too soon after a tick. It also has time to spare.

Is the layout the circuit you wrote? Open Signoff. It reads Circuits match. That check, layout versus schematic, extracts a circuit back out of the finished layout and compares it with the gates from synthesis.

What does it look like? Open Layout, then press Full screen.

The layout of the counter, full screen. The die is a large empty rectangle with a small cluster of coloured cells and wires at the bottom centre and one long wire running to a pin at the top edge. The panel on the right reads counter_en, 19 cells, 22 nets, 423 µm of wire, with a Things to look at list that includes Clock tree: 3 buffers feed 4 registers and Your logic fills 2% of the cell rows.
The whole chip. Your counter is the small cluster at the bottom.

The big rectangle is the die. The cluster at the bottom is your counter: 19 cells joined by 22 nets (a net is one electrical connection). Four of those cells are the registers that hold count, and the panel tells you three more are buffers that carry the clock to them. The logic fills about 2% of the space, which is why most of the die looks empty. Click a wire to light up its whole net, or a cell to see what it does.

That is a chip design

You wrote a circuit, proved it with a testbench, caught a bug with it, and produced a layout that meets timing and matches the design. Every bigger project is the same loop with more Verilog in it.

Where to go next#

Try one change and run the loop again:

  • Make the counter 8 bits wide: change [3:0] to [7:0] in both files and 4'd to 8'd. The testbench will tell you which of its checks no longer hold, and why.
  • Lower CLOCK_PERIOD in config.json and see how far the slack shrinks.