Hardcaml MIPS CPU Learning Project and Blog

Tl;dr: I’m writing a blog about making a MIPS CPU in Hardcaml.

Hi! My name is Sasha, and I’m a student at Penn State majoring in CS and Math. Last semester, I took a computer engineering class where we built a pipelined MIPS CPU in Verilog as a semester-long project. I enjoyed the class, but a lot of frustration came from Verilog itself.

A few months ago, I came across the Signals and Threads Programmable Hardware episode. I really liked the idea of Hardcaml: a library to write and test hardware designs in OCaml. Representing circuits as functions felt like a good abstraction, and I’ve been wanting to learn OCaml for a while.

So this summer, a friend and I are rewriting the Verilog MIPS CPU we made last semester into Hardcaml. We’re still working on the project, but have made some good progress and wanted to share it in case anyone finds it interesting / useful. If anyone wants to take a look, it’s up on GitHub.

We’ve written some blog posts about our project:

  1. Some more background on what we’re doing and why
  2. An ELI5 overview of how hardware, and pipelined CPUs in particular, work
  3. Another high-level overview of Verilog, hardware design, FPGAs, and why I think OCaml might be a great fit for hardware design
  4. How to set up a Hardcaml project, including testing and Verilog generation
  5. How to split Hardcaml circuits into multiple modules

There’s also a few more that we’ve written code for, but are still drafting blog posts about:

  • How to work with memory in Hardcaml
  • How to design stateful, sequential circuits in Hardcaml
  • A safer design pattern for Hardcaml circuits

I’m new to both OCaml and blogging, and this has definitely been a fun experience so far! Would love to hear any feedback / comments.

36 Likes

Hi,
I just want to say it’s a great project. There doesn’t seem to be many resources on hardcaml so I look forward to reading the blog!

1 Like

This is a really wonderful series of blogs! Can we convince you to have a go at a simple RISC-V CPU once you have MIPS working? :slight_smile:

3 Likes

I’m glad the episode struck a chord, and I’m excited to see what comes of your experiment. Good luck!

2 Likes

Very cool - really enjoying the blog posts!

If you have any questions, suggestions or feedback for Hardcaml, please do open issues on Github.

And here’s a deal - if you can run a compiled program in the Hardcaml simulator, I’ll buy you and your colleague an Arty A7 board to run it on.

4 Likes

Thank you all for the kind words and support! This has been a fun project so far, and now that we’re starting to get comfortable with Hardcaml, I’m hoping to have more updates soon.

That’s a great idea! Once we get the core parts in place, there’s a few things I’d like to improve/add to the basic version we made in class. For instance, we’re initially implementing instruction memory as a mux, which is simple but not that realistic. I’d like to play around with the Xilinx memory library and try to get it working with actual RAM. Rewriting this project into a RISC-V CPU could be a fun challenge!

Absolutely! We’ve been keeping track of some “Hardcaml observations” in each post. Once we’ve gotten a bit more comfortable with the library and ecosystem, we’ll try and distill that into something useful.

Wow, thanks! With the magic powers of Hardcaml behind us, it should be doable :smile:

1 Like

Nice blog posts!

If you want an example of Hardcaml on Arty A7, do have a look at this example

Regardless of whether you use the Arty A7, src/infrastructure/uart.ml contains some ~non-trivial code examples for state machines.

2 Likes

:+1: Does your website have an RSS feed?

It does now! https://ceramichacker.com/rss.
Also https://ceramichacker.com/atom for Atom users.

On a side note, we’ve published two more posts:

  1. Memory in Hardcaml
  2. Registers and Stateful Design

More coming soon!

3 Likes