IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

2018/01時点のClash

Githubから取得してclashをインストールした場合、2018/01時点では、hackageにあるチュートリアルはのコード動かないようです。

CLaSH.Tutorial

tutorialにあるソースをコンパイルするとパッケージがないとのエラーメッセージがでます。

Clash.Prelude> :l MAC.hs
[1 of 1] Compiling MAC              ( MAC.hs, interpreted )

MAC.hs:3:1: error:
    Could not find module ‘CLaSH.Prelude’
    Perhaps you meant Clash.Prelude (from clash-prelude-0.99)
    Use -v to see a list of the files searched for.
  |
3 | import CLaSH.Prelude
  | ^^^^^^^^^^^^^^^^^^^^
Failed, modules loaded: none.

CLasH.PreludeClash.Preludeに変更してコンパイルしてもエラーになります。

Clash.Prelude> :l MAC.hs
[1 of 1] Compiling MAC              ( MAC.hs, interpreted )

MAC.hs:14:14: error:
    • Expecting one more argument to ‘Signal (Signed 9, Signed 9)’
      Expected a type, but
      ‘Signal (Signed 9, Signed 9)’ has kind
      ‘* -> *’
    • In the type signature:
        topEntity :: Signal (Signed 9, Signed 9) -> Signal (Signed 9)
   |
14 | topEntity :: Signal (Signed 9, Signed 9) -> Signal (Signed 9)
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^

MAC.hs:14:21: error:
    • Expected kind ‘Domain’, but ‘(Signed 9, Signed 9)’ has kind ‘*’
    • In the first argument of ‘Signal’, namely ‘(Signed 9, Signed 9)’
      In the type signature:
        topEntity :: Signal (Signed 9, Signed 9) -> Signal (Signed 9)
   |
14 | topEntity :: Signal (Signed 9, Signed 9) -> Signal (Signed 9)
   |                     ^^^^^^^^^^^^^^^^^^^^

MAC.hs:14:45: error:
    • Expecting one more argument to ‘Signal (Signed 9)’
      Expected a type, but ‘Signal (Signed 9)’ has kind ‘* -> *’
    • In the type signature:
        topEntity :: Signal (Signed 9, Signed 9) -> Signal (Signed 9)
   |
14 | topEntity :: Signal (Signed 9, Signed 9) -> Signal (Signed 9)
   |                                             ^^^^^^^^^^^^^^^^^

MAC.hs:14:53: error:
    • Expected kind ‘Domain’, but ‘Signed 9’ has kind ‘*’
    • In the first argument of ‘Signal’, namely ‘(Signed 9)’
      In the type signature:
        topEntity :: Signal (Signed 9, Signed 9) -> Signal (Signed 9)
   |
14 | topEntity :: Signal (Signed 9, Signed 9) -> Signal (Signed 9)
   |                                                     ^^^^^^^^
Failed, modules loaded: none.

どうやら、諸々APIが変わっているようです。

サンプルは、リポジトリから取得した中にexampleがあるのでそれを使います。

clash-compiler/examples at master · clash-lang/clash-compiler · GitHub

起動

$ stack exec -- clash --interactive
CLaSHi, version 0.99 (using clash-lib, version 0.99):
http://www.clash-lang.org/  :? for help

サンプルの確認

Clash.Prelude> :! ls
MAC.hs
*MAC> :! cat MAC.hs
module MAC where

import Clash.Prelude

ma acc (x,y) = acc + x * y

macT acc (x,y) = (acc',o)
  where
    acc' = ma acc (x,y)
    o    = acc

mac = macT `mealy` 0

topEntity
  :: SystemClockReset
  => Signal System (Signed 9,Signed 9)
  -> Signal System (Signed 9)
topEntity = mac
{-# NOINLINE topEntity #-}

testBench :: Signal System Bool
testBench = done'
  where
    testInput      = stimuliGenerator $(listToVecTH [(1,1) :: (Signed 9,Signed 9),(2,2),(3,3),(4,4)])
    expectedOutput = outputVerifier $(listToVecTH [0 :: Signed 9,1,5,14])
    done           = expectedOutput (topEntity testInput)
    done' = withClockReset (tbSystemClockGen (not <$> done')) systemResetGen done
*MAC>

ロード(コンパイル)

Clash.Prelude> :l MAC.h
MAC.hi  MAC.hs
Clash.Prelude> :l MAC.hs
[1 of 1] Compiling MAC              ( MAC.hs, interpreted ) [flags changed]
Ok, modules loaded: MAC.

verilog生成

*MAC> :verilog
Loading dependencies took 2.235453s
Compiling: MAC.topEntity
Applied 133 transformations
Normalisation took 1.062433s
Netlist generation took 0.011033s
Compiling: MAC.testBench
Applied 425 transformations
Testbench normalisation took 0.557544s
Testbench netlist generation took 0.013983s
Total compilation took 3.906076s
*MAC>

生成内容の確認

*MAC> :! tree verilog/
verilog/
└── MAC
    ├── MAC_testBench
    │   ├── MAC_outputVerifier.v
    │   ├── MAC_stimuliGenerator.v
    │   └── MAC_testBench.v
    ├── MAC_topEntity.manifest
    └── MAC_topEntity.v

2 directories, 5 files
*MAC>