This is first (hopefully not the last) on Hardcaml1 which seems like interesting project by Janesteet which seem into Ocaml for both hardware and software (well, based on the public github repos)
Install Link to heading
Just in case opam is not install, we need to install opam at ~/.opam
sudo apt install opam
opam init
eval $(opam env)
Then install hardcaml stuff
opam install hardcaml hardcaml_waveterm ppx_hardcaml
Hello world Link to heading
Getting the example for hardcaml
repo for half adder to work wasn’t easy. I kept getting error about Hardcaml module not found. It took some time to figure out use
and require
at the ttop of file.
#use "topfind";;
#require "hardcaml";;
open Hardcaml
open Signal
(* Define a Half Adder *)
let half_adder a b =
let sum = a ^: b in (* XOR for sum *)
let carry = a &: b in (* AND for carry *)
sum, carry
(* Define the circuit *)
let circuit =
let a = input "a" 1 in
let b = input "b" 1 in
let sum, carry = half_adder a b in
Circuit.create_exn ~name:"half_adder"
[ output "sum" sum; output "carry" carry ]
(* Generate Verilog *)
let () = Rtl.print Verilog circuit
$ ocaml half_adder.ml
module half_adder (
b,
a,
sum,
carry
);
input b;
input a;
output sum;
output carry;
wire _5;
wire _6;
assign _5 = a & b;
assign _6 = a ^ b;
assign sum = _6;
assign carry = _5;
endmodule