Let's Play with Hardcaml!

Building Digital Circuits in an OCaml REPL

Kiwamu Okabe
kiwamu@metasepi.org
https://metasepi.org/

What Is Hardcaml?

A library for describing digital circuits (RTL) in OCaml

  • Build circuits with ordinary OCaml functions
  • Simulate a circuit cycle by cycle
  • Generate Verilog and run it on an FPGA

Today we will use neither an FPGA nor a Verilog simulator.

We will play with just the OCaml REPL!

8th Homebrew CPU Meetup, 2026-11-28

Let's Install It

On Debian / Ubuntu, use opam, the OCaml package manager.

$ sudo apt install opam
$ opam init
$ opam switch create hardcaml-5.3.0 ocaml-base-compiler.5.3.0
$ eval "$(opam env --switch=hardcaml-5.3.0)"
$ opam install hardcaml ppx_hardcaml hardcaml_waveterm utop
8th Homebrew CPU Meetup, 2026-11-28

Start the OCaml REPL

$ utop
# #require "hardcaml";;
# #require "hardcaml_waveterm";;
# open Hardcaml;;
# open Signal;;
  • hardcaml: describing and simulating digital circuits
  • hardcaml_waveterm: recording and displaying waveforms

You can start without creating a project or source files.

8th Homebrew CPU Meetup, 2026-11-28

Let's Build a 5-bit Counter!

# module Counter = struct
    let clock = input "clock" 1
    let count = reg_fb (Reg_spec.create ~clock ())
                  ~width:5 ~f:(fun d -> d +:. 1)
    let circuit = Circuit.create_exn ~name:"top"
                    [ output "count" count ]
  end;;

reg_fb is a register that feeds its own value back as the next input.

~f:(fun d -> d +:. 1) adds 1 on every clock.

That is all it takes to make a digital circuit!

8th Homebrew CPU Meetup, 2026-11-28

What If We Wrote It in Verilog?

# Hardcaml.Rtl.print Verilog Counter.circuit;;

Rewriting the generated Verilog in a more readable form gives:

module top (
    input wire clock,
    output reg [4:0] count
);
    always @(posedge clock)
        count <= count + 5'd1;
endmodule

We were writing RTL in OCaml all along.

8th Homebrew CPU Meetup, 2026-11-28

Let's Run the Circuit Right There!

# let waves, sim =
    Hardcaml_waveterm.Waveform.create
      (Cyclesim.create Counter.circuit);;
# for _ = 1 to 34 do
    Cyclesim.cycle sim;
    Bits.to_int !(Cyclesim.out_port sim "count") |> print_int;
    print_string " "
  done;;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
31 0 1 2 - : unit = ()

An ordinary OCaml for loop can drive a circuit!

8th Homebrew CPU Meetup, 2026-11-28

We Can Even See Waveforms!

# Hardcaml_waveterm.Waveform.print
    ~display_width:80 ~display_height:10 ~wave_width:1 waves;;
+Signals-----------+ +Waves-----------------------------------------------------+
|clock             | |+-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-|
|                  | |  +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ |
|                  | |----+---+---+---+---+---+---+---+---+---+---+---+---+---+-|
|count             | | 00 |01 |02 |03 |04 |05 |06 |07 |08 |09 |0A |0B |0C |0D |0|
|                  | |----+---+---+---+---+---+---+---+---+---+---+---+---+---+-|
|                  | |                                                          |
|                  | |                                                          |
|                  | |                                                          |
+------------------+ +----------------------------------------------------------+

We could do everything inside utop!

8th Homebrew CPU Meetup, 2026-11-28

And Then, Linux Booted!

While playing with Hardcaml,
I ended up building a RISC-V CPU together with AI.

  • RV32IMA / Sv32 MMU / 2 cores / 50 MHz
  • Colorlight i9 v7.2 (ECP5 FPGA, 8 MiB SDRAM)
  • Linux 6.18 SMP boots on real hardware

github.com/takibi-lang/takibi-hardware

8th Homebrew CPU Meetup, 2026-11-28

Timing: 15 s (total 0:15) I usually write software. Recently I have been playing with digital circuits built in OCaml. Today I will show you a simple way to try it yourself right away.

Timing: 25 s (total 0:40) Hardcaml is a library developed by Jane Street. It is not HLS that turns an OCaml program directly into hardware; you describe RTL structure with OCaml functions. Today I will show how to try it without an FPGA.

Timing: 25 s (total 1:05) First install opam and create an OCaml 5.3.0 environment. Then install the required libraries and utop together. Installation takes time, so I will not run it live; this is a recipe to take home and try later. I do not pin the Docker image or exact library versions here. Some environments may need additional build dependencies.

Timing: 20 s (total 1:25) utop is OCaml's interactive REPL. We load the libraries so that the circuit-building functions become available. From here on we use the same utop session.

Timing: 55 s (total 2:20) Create a 1-bit input signal called clock. reg_fb builds a register with feedback. It is 5 bits wide and stores d + 1 at the next clock. Finally, connect count to an output port to create the Circuit. The first fun part is that calling OCaml functions creates a circuit. I only wrapped long lines from the original log; the circuit definition is unchanged.

Timing: 35 s (total 2:55) Hardcaml can generate Verilog. Here I omit the generated code with many intermediate signals and show concise Verilog with the same behavior. The 5-bit register updates on every rising clock edge. This example has no reset or explicit initial value. The initial value of 0 in Cyclesim does not guarantee the power-up value of this Verilog on real hardware.

Timing: 55 s (total 3:50) Cyclesim.create builds a simulator, and Waveform.create records waveforms as well. Each call to cycle advances one clock. out_port reads the count output, Bits.to_int converts it to an integer, and we print it. Because it is 5 bits, 31 is followed by 0. There is no need to generate Verilog or start another simulator. The numeric output is wrapped for projection.

Timing: 40 s (total 4:30) Because the earlier loop also recorded the waveform, no re-simulation is needed. The waveform is shown in hex and starts at 00. The values printed earlier are outputs after advancing a clock, so they start at 1. In this way we described, ran, and observed a circuit in the REPL. That is today's introduction. The real utop output uses box-drawing characters; this slide shows an ASCII redrawing to keep the repository ASCII-only.

Timing: 30 s (total 5:00) That is how I built and tested circuits with Hardcaml and had fun with AI. Then I built a RISC-V CPU in Hardcaml and got Linux running. If you are interested, please look at GitHub. Thank you. The specifications and REPL output strings follow README.md in the repository. When presenting, you may add a board photo or boot log if needed, but keep this slide self-contained.