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.* or higher:

  1. Install GHC (version 7.10.* or higher)

    • 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

    Alternatives, if you cannot find what you are looking for on http://www.haskell.org/ghc/download

    • Ubuntu:
      • sudo add-apt-repository -y ppa:hvr/ghc
      • sudo apt-get update
      • sudo apt-get install cabal-install-1.22 ghc-7.10.2 libtinfo-dev
      • update your PATH with: /opt/ghc/7.10.2/bin, /opt/cabal/1.22/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.22.* or higher)

    • 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
    • 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