IT練習ノート

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

UART

UARTについて

http://www.altima.jp/column/fpga_edison/uart.html

UART / USART

https://www.youtube.com/watch?v=FQpbIvhY7es

UART = Universal Asyncronus Receiver-Transmitter

USART = Universal Syncronus / Asyncronus Receiver-Transmitter

RS-232

https://www.youtube.com/watch?v=XVEnxipCIJ0

RS-232 Recommended Standard

Tx <--> Rx

Tx = Transmmit Data Rx = Receive Data

DTR <--> DSR

DTR = Data Terminal Ready DSR = Data Set Ready

RST <--> CTS

RTS = Request To Send CTS = Carrier To Send

DCD = Data Carrier Detect RI = ??

FPGAプログラミング大全 Xilinx編 10章

FPGAプログラミング大全 Xilinx編

FPGAプログラミング大全 Xilinx編

手元の環境Vivado 2017.3と10章で書籍と少し違っていた点

P.503 図10-28 (a) IPの登録

f:id:naotoogawa:20180116171905p:plain

P.504 図10-29 乗加算回路IPを追加したかいあダイアグラムが完成

f:id:naotoogawa:20180116172303p:plain

少し嵌った点

10章の最初のサンプルはFPGA\XilinxFPGA\commonにある。(XilinxFPGA\Zybo\dai10sho\muladd_zqではない)

hetero-mapの使い方

hackage.haskell.org

hetero-mapパッケージを使おうと思ったのですが、サンプルが見つけられず、使い方に悩みました。

このようにして、型の整合はとれても、undefinedをつかっているので実行時にエラーになります。

*Main HeteroMap.Map> let k1 = undefined :: Key Char Char
*Main HeteroMap.Map> let k2 = undefined :: Key Int Int
*Main HeteroMap.Map> let m = insert k2 2 $ insert k1 'a' empty
*Main HeteroMap.Map> :t m
m :: Map
       (Int HeteroMap.Map.:* (Char HeteroMap.Map.:* HeteroMap.Map.Z))
*Main HeteroMap.Map> let v = lookup k1 m
*Main HeteroMap.Map> v
*** Exception: Prelude.undefined
CallStack (from HasCallStack):
  error, called at libraries/base/GHC/Err.hs:79:14 in base:GHC.Err
  undefined, called at <interactive>:19:10 in interactive:Ghci2
*Main HeteroMap.Map>

Keyの実装は

data Key x a where
    Key :: Key a a

となっていますが、データコンストラクタは公開されていません。

keyを作る実装も関数ccを与えるだけになっており、関数を与えるけれど、その関数の第1引数はKey固定になっています。これはどのように使うのでしょうか?

-- | Allocate a new key, which is only valid within the passed function
-- (and will be a type error if it tries to escape).
newKey :: (forall x. Key x a -> b) -> b
newKey cc = cc Key

検索しても本当にこのパッケージの情報がなくて、途方に暮れていたところ、HMapexampleがヒントになりました。

Data.HMap

ポイントとしては、

1つの関数の中で閉じて使う。別の関数とMapを共有できない。(<-この理解であってる?)

ということのようです。

import HeteroMap.Map 
import Prelude hiding (lookup)

example :: Key aaa String -> Key bbb Double -> Key ccc Bool -> String
example name salary female =
  format a ++ "\n" ++ format b ++ "\n"
  where a = insert name "Edsger" $
            insert salary 4450.0 $
            insert female False empty
        b = insert name "Ada"    $
            insert salary 5000.0 $
            insert female True empty
        format x = lookup name x ++ ": salary=" ++ show (lookup salary x) ++ ", female="  ++ show (lookup female x)

keyLocal :: String
keyLocal = newKey $ newKey $ newKey example

main = do print "local"
          putStr keyLocal

HaskellでFPGAの最小のサンプル

入力を反転するだけのコード

Not gate

コンパイル

$ stack exec -- clash --interactive
CLaSHi, version 0.99 (using clash-lib, version 0.99):
http://www.clash-lang.org/  :? for help
Clash.Prelude> :cd ../worka
Clash.Prelude> :! ls
Clash01.hs
Clash.Prelude> :l Clash01.hs
[1 of 1] Compiling Clash01          ( Clash01.hs, interpreted )
Ok, modules loaded: Clash01.
*Clash01>

Verilog生成

*Clash01> :verilog
Loading dependencies took 2.500923s
Compiling: Clash01.topEntity
Applied 64 transformations
Normalisation took 1.056631s
Netlist generation took 0.006391s
Compiling: Clash01.testBench
Applied 219 transformations
Testbench normalisation took 0.550645s
Testbench netlist generation took 0.030401s
Total compilation took 4.170923s
*Clash01> :! ls
Clash01.dyn_hi  Clash01.dyn_o   Clash01.hi  Clash01.hs  Clash01.o   verilog
*Clash01> :! tree
.
├── Clash01.dyn_hi
├── Clash01.dyn_o
├── Clash01.hi
├── Clash01.hs
├── Clash01.o
└── verilog
    └── Clash01
        ├── Clash01_testBench
        │   ├── Clash01_outputVerifier.v
        │   ├── Clash01_stimuliGenerator.v
        │   └── Clash01_testBench.v
        ├── Clash01_testBench.vcd
        ├── Clash01_topEntity.manifest
        ├── Clash01_topEntity.v
        └── main.exe

3 directories, 12 files
*Clash01>

波形ファイルを作るためにテストベンチを修正

A Testbench

波形ファイル生成

$ find . -name "*.v" | xargs iverilog -o main.exe
$ ./main.exe
VCD info: dumpfile Clash01_testBench.vcd opened for output.

波形ファイルの確認

f:id:naotoogawa:20180114173923p:plain

ターゲット指定のcabalのrepl

cabalファイルで複数のターゲットを作り、cabal replすると起動ができなくなります。

$ cabal repl
cabal: The 'repl' command does not support multiple targets at once.
$

また、ターゲットの名前が重複すると起動しません。

$ cabal repl  myprotobuf
cabal: Ambiguous build target 'myprotobuf'. It could be:
exe:myprotobuf (component)
lib:myprotobuf (component)

この場合、cabal repl 種別:名称で指定します。

$ cabal repl exe:myprotobuf
$ cabal repl lib:myprotobuf
$ cabal repl test:MyTestSuite01

cabalファイルで記載する場所の例は下記です。

name:                myprotobuf
version:             0.1.0.0
...
executable myprotobuf    ### ここの名称
  main-is:             TCP01.hs
  other-extensions:    BangPatterns, DeriveDataTypeable, DeriveGeneric, FlexibleInstances, MultiParamTypeClasses
...
library lib-xproto   ### ここの名称
...
  exposed-modules:      DataBase.MySQLX.CRUD
...
Test-Suite MyTestSuite01   ### ここの名称
  type: exitcode-stdio-1.0

Haskellのドキュメント生成

haddockコマンド自体はディレクトリを指定してその配下にあるhsファイルを再帰的に処理してくれるオプションはないようです。xargsを使うのが手っ取り早いようです。

haskell - how to generate documentations with haddock? - Stack Overflow

$ find . -name '*.hs' | xargs haddock --html -o doc_dir

Implicit Parameters

Clashのプログラミングでサンプルを作り始めたところ、?変数名 :: 型というシグネチャが出てきました。

*> :t notM
notM
  :: (?rst::Reset domain synchronous, ?clk::Clock domain gated) =>
     Signal domain Bool -> Signal domain Bool

Implicit Paremtersという機能でした。

https://www.haskell.org/hugs/pages/users_guide/implicit-parameters.html

Readerモナドに似た挙動をするようです。

まずは、このような関数を考えてみます。

*> let foo = (*2) . (+2)
*> :t foo
foo :: Num c => c -> c
*> foo 1
6
*> foo 2
8
*> foo 3
10

次に、上の関数の実装内で2つあった関数((*2)(+2))の1番目の関数を、パラメータとして外出しします。

*> let foo' z = z . (+2)
*> :t foo'
foo' :: Num b => (b -> c) -> b -> c
*> foo' (*2) 1
6
*> foo' (*2) 2
8
*> foo' (*2) 3
10

さらに、上の関数で外出ししたパラメータを暗黙化します。

*> let foo'' = undefined :: (Num a, ?z::a->a) => a -> a
*> let foo'' = let ?z=(*2) in ?z . (+2)  :: (Num a, ?z::a->a) => a -> a
*> foo'' 1
6
*> foo'' 2
8
*> foo'' 3
10

参考情報

24 Days of GHC Extensions: Implicit Parameters