From Haskell to Hardware

Star Build Status Hackage

Fork me on GitHub


FIR filter

fir coeffs x = dotp coeffs (window x)
    dotp as bs = sum (zipWith (*) as bs)
  Parametric in the number of filter taps
  Parametric in the data type
  Strongly-typed, yet free from type annotations

Why use CλaSH

CλaSH (pronounced ‘clash’) is a functional hardware description language that borrows both its syntax and semantics from the functional programming language Haskell. It provides a familiar structural design approach to both combinational and synchronous sequential circuits. The CλaSH compiler transforms these high-level descriptions to low-level synthesizable VHDL, Verilog, or SystemVerilog.

Features of CλaSH:

  • Strongly typed (like VHDL), yet with a very high degree of type inference, enabling both safe and fast prototying using concise descriptions (like Verilog).
  • Interactive REPL: load your designs in an interpreter and easily test all your component without needing to setup a test bench.
  • Compile your designs for fast simulation.
  • Higher-order functions, in combination with type inference, result in designs that are fully parametric by default.
  • Synchronous sequential circuit design based on streams of values, called Signals, lead to natural descriptions of feedback loops.
  • Multiple clock domains, with type safe clock domain crossing.
  • Template language for introducing new VHDL/(System)Verilog primitives.
setup details

How do I install CλaSH?

Please file a bug report if the following installation instructions do not work for you.

The CλaSH compiler is written in Haskell and is compiled from source using the the GHC Haskell compiler version 7.10 (higher or lower versions of GHC are not supported):

  1. Install GHC 7.10

    • Download GHC for your platform. Unix users can use ./configure prefix=<LOCATION> to set the installation location
    • Make sure the bin directory of GHC is in your PATH

    Next follows a list of alternative installation instructions, in case you cannot find what you are looking for on https://www.haskell.org/ghc/download_ghc_7_10_3

    • Ubuntu:
      • sudo add-apt-repository -y ppa:hvr/ghc
      • sudo apt-get update
      • sudo apt-get install cabal-install-1.24 ghc-7.10.3 libtinfo-dev
      • update your PATH with: /opt/ghc/7.10.3/bin, /opt/cabal/1.24/bin and $HOME/.cabal/bin
      • Run cabal update
      • Skip step 2.
    • OS X:
    • Windows:
      • Follow the instructions on: MinGHC
      • Run cabal update
      • Skip step 2.
  2. Install Cabal (version 1.24)

    • Binary, when available:
      • Download the binary for cabal-install
      • Put the binary in a location mentioned in your PATH
      • Add cabal’s bin directory to your PATH:
        • Windows: %appdata%\cabal\bin
        • Unix: $HOME/.cabal/bin
    • Source:
      • Download the sources for cabal-install
      • Unpack (tar xf) the archive and cd to the directory
      • Run sh bootstrap.sh
      • Follow the instructions to add cabal to your PATH
    • Run cabal update
  3. Install CλaSH

    • Run:
      • i386 Linux: cabal install clash-ghc --enable-documentation --enable-executable-dynamic
      • Other: cabal install clash-ghc --enable-documentation
      • If you get the error can't load .so/.DLL for: libtinfo.so (libtinfo.so: cannot open shared object file: No such file or directory), add a symlink from libtinfo.5.so to libtinfo.so, and retry the cabal install process.
    • This is going to take awhile, so have a refreshment xkcd

Alright, you’ve got the compiler installed, now it’s time for some testing:

  • Download the FIR.hs example
  • Run clash --interactive FIR.hs
  • Execute, in the interpreter, the :vhdl command.
  • Execute, in the interpreter, the :verilog command.
  • Execute, in the interpreter, the :systemverilog command.
  • Exit the interpreter using :q
  • Examine the VHDL code in the vhdl directory
  • Examine the Verilog code in the verilog directory
  • Examine the SystemVerilog code in the systemverilog directory

Tutorial and Reference material

  Reference documentation
  More examples
  Blog post: Programming an FPGA with CλaSH

Blog posts:

  6502 in Clash
  An FPGA-based SKI calculus evaluator written in Haskell/Cλash
  Functional hardware
  Remote fpga call