/
Examples.hs
633 lines (545 loc) · 15.9 KB
/
Examples.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
{-|
Copyright : © 2015-2016, Christiaan Baaij,
2017 , Google Inc.
Licence : Creative Commons 4.0 (CC BY 4.0) (http://creativecommons.org/licenses/by/4.0/)
-}
{-# LANGUAGE NoImplicitPrelude, CPP, TemplateHaskell, DataKinds, BinaryLiterals,
FlexibleContexts, GADTs, TypeOperators, TypeApplications,
RecordWildCards #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}
module Clash.Examples (
-- * Decoders and Encoders
-- $decoders_and_encoders
-- * Counters
-- $counters
-- * Parity and CRC
-- $parity_and_crc
-- * UART model
-- $uart
)
where
import Clash.Prelude hiding (feedback)
import Control.Lens
import Control.Monad
import Control.Monad.Trans.State
decoderCase :: Bool -> BitVector 4 -> BitVector 16
decoderCase enable binaryIn | enable =
case binaryIn of
0x0 -> 0x0001
0x1 -> 0x0002
0x2 -> 0x0004
0x3 -> 0x0008
0x4 -> 0x0010
0x5 -> 0x0020
0x6 -> 0x0040
0x7 -> 0x0080
0x8 -> 0x0100
0x9 -> 0x0200
0xA -> 0x0400
0xB -> 0x0800
0xC -> 0x1000
0xD -> 0x2000
0xE -> 0x4000
0xF -> 0x8000
decoderCase _ _ = 0
decoderShift :: Bool -> BitVector 4 -> BitVector 16
decoderShift enable binaryIn =
if enable
then 1 `shiftL` (fromIntegral binaryIn)
else 0
encoderCase :: Bool -> BitVector 16 -> BitVector 4
encoderCase enable binaryIn | enable =
case binaryIn of
0x0001 -> 0x0
0x0002 -> 0x1
0x0004 -> 0x2
0x0008 -> 0x3
0x0010 -> 0x4
0x0020 -> 0x5
0x0040 -> 0x6
0x0080 -> 0x7
0x0100 -> 0x8
0x0200 -> 0x9
0x0400 -> 0xA
0x0800 -> 0xB
0x1000 -> 0xC
0x2000 -> 0xD
0x4000 -> 0xE
0x8000 -> 0xF
encoderCase _ _ = 0
upCounter :: HasClockReset domain gated synchronous
=> Signal domain Bool -> Signal domain (Unsigned 8)
upCounter enable = s
where
s = register 0 (mux enable (s + 1) s)
upCounterLdT
:: Num a => a -> (Bool, Bool, a) -> (a,a)
upCounterLdT s (ld,en,dIn) = (s',s)
where
s' | ld = dIn
| en = s + 1
| otherwise = s
upCounterLd :: HasClockReset domain gated synchronous
=> Signal domain (Bool,Bool,Unsigned 8) -> Signal domain (Unsigned 8)
upCounterLd = mealy upCounterLdT 0
upDownCounter :: HasClockReset domain gated synchronous
=> Signal domain Bool -> Signal domain (Unsigned 8)
upDownCounter upDown = s
where
s = register 0 (mux upDown (s + 1) (s - 1))
lfsrF' :: BitVector 16 -> BitVector 16
lfsrF' s = feedback ++# slice d15 d1 s
where
feedback = s!5 `xor` s!3 `xor` s!2 `xor` s!0
lfsrF :: HasClockReset domain gated synchronous
=> BitVector 16 -> Signal domain Bit
lfsrF seed = msb <$> r
where r = register seed (lfsrF' <$> r)
lfsrGP
:: (KnownNat (n + 1), Bits a)
=> Vec (n + 1) Bool
-> Vec (n + 1) a
-> Vec (n + 1) a
lfsrGP taps regs = zipWith xorM taps (fb +>> regs)
where
fb = last regs
xorM i x | i = x `xor` fb
| otherwise = x
lfsrG :: HasClockReset domain gated synchronous => BitVector 16 -> Signal domain Bit
lfsrG seed = last (unbundle r)
where r = register (unpack seed) (lfsrGP (unpack 0b0011010000000000) <$> r)
grayCounter :: HasClockReset domain gated synchronous
=> Signal domain Bool -> Signal domain (BitVector 8)
grayCounter en = gray <$> upCounter en
where gray xs = msb xs ++# xor (slice d7 d1 xs) (slice d6 d0 xs)
oneHotCounter :: HasClockReset domain gated synchronous
=> Signal domain Bool -> Signal domain (BitVector 8)
oneHotCounter enable = s
where
s = register 1 (mux enable (rotateL <$> s <*> 1) s)
crcT
:: (Bits a, KnownNat (BitSize a), BitPack a)
=> a -> Bit -> a
crcT bv dIn = replaceBit 0 dInXor
$ replaceBit 5 (bv!4 `xor` dInXor)
$ replaceBit 12 (bv!11 `xor` dInXor)
rotated
where
dInXor = dIn `xor` fb
rotated = rotateL bv 1
fb = msb bv
crc :: HasClockReset domain gated synchronous
=> Signal domain Bool -> Signal domain Bool -> Signal domain Bit -> Signal domain (BitVector 16)
crc enable ld dIn = s
where
s = register 0xFFFF (mux enable (mux ld 0xFFFF (crcT <$> s <*> dIn)) s)
data RxReg
= RxReg
{ _rx_reg :: BitVector 8
, _rx_data :: BitVector 8
, _rx_sample_cnt :: Unsigned 4
, _rx_cnt :: Unsigned 4
, _rx_frame_err :: Bool
, _rx_over_run :: Bool
, _rx_empty :: Bool
, _rx_d1 :: Bit
, _rx_d2 :: Bit
, _rx_busy :: Bool
}
makeLenses ''RxReg
data TxReg
= TxReg
{ _tx_reg :: BitVector 8
, _tx_empty :: Bool
, _tx_over_run :: Bool
, _tx_out :: Bit
, _tx_cnt :: Unsigned 4
}
makeLenses ''TxReg
uartTX t@(TxReg {..}) ld_tx_data tx_data tx_enable = flip execState t $ do
when ld_tx_data $ do
if not _tx_empty then
tx_over_run .= False
else do
tx_reg .= tx_data
tx_empty .= False
when (tx_enable && not _tx_empty) $ do
tx_cnt += 1
when (_tx_cnt == 0) $
tx_out .= 0
when (_tx_cnt > 0 && _tx_cnt < 9) $
tx_out .= _tx_reg ! (_tx_cnt - 1)
when (_tx_cnt == 9) $ do
tx_out .= 1
tx_cnt .= 0
tx_empty .= True
unless tx_enable $
tx_cnt .= 0
uartRX r@(RxReg {..}) rx_in uld_rx_data rx_enable = flip execState r $ do
-- Synchronise the async signal
rx_d1 .= rx_in
rx_d2 .= _rx_d1
-- Uload the rx data
when uld_rx_data $ do
rx_data .= _rx_reg
rx_empty .= True
-- Receive data only when rx is enabled
if rx_enable then do
-- Check if just received start of frame
when (not _rx_busy && _rx_d2 == 0) $ do
rx_busy .= True
rx_sample_cnt .= 1
rx_cnt .= 0
-- Star of frame detected, Proceed with rest of data
when _rx_busy $ do
rx_sample_cnt += 1
-- Logic to sample at middle of data
when (_rx_sample_cnt == 7) $ do
if _rx_d1 == 1 && _rx_cnt == 0 then
rx_busy .= False
else do
rx_cnt += 1
-- start storing the rx data
when (_rx_cnt > 0 && _rx_cnt < 9) $ do
rx_reg %= replaceBit (_rx_cnt - 1) _rx_d2
when (_rx_cnt == 9) $ do
rx_busy .= False
-- Check if End of frame received correctly
if _rx_d2 == 0 then
rx_frame_err .= True
else do
rx_empty .= False
rx_frame_err .= False
-- Check if last rx data was not unloaded
rx_over_run .= not _rx_empty
else do
rx_busy .= False
uart ld_tx_data tx_data tx_enable rx_in uld_rx_data rx_enable =
( _tx_out <$> txReg
, _tx_empty <$> txReg
, _rx_data <$> rxReg
, _rx_empty <$> rxReg
)
where
rxReg = register rxRegInit (uartRX <$> rxReg <*> rx_in <*> uld_rx_data
<*> rx_enable)
rxRegInit = RxReg { _rx_reg = 0
, _rx_data = 0
, _rx_sample_cnt = 0
, _rx_cnt = 0
, _rx_frame_err = False
, _rx_over_run = False
, _rx_empty = True
, _rx_d1 = 1
, _rx_d2 = 1
, _rx_busy = False
}
txReg = register txRegInit (uartTX <$> txReg <*> ld_tx_data <*> tx_data
<*> tx_enable)
txRegInit = TxReg { _tx_reg = 0
, _tx_empty = True
, _tx_over_run = False
, _tx_out = 1
, _tx_cnt = 0
}
{- $setup
>>> :set -XDataKinds
>>> import Clash.Prelude
>>> import Test.QuickCheck
-}
{- $decoders_and_encoders
= Decoder
Using a @case@ statement:
@
decoderCase :: Bool -> BitVector 4 -> BitVector 16
decoderCase enable binaryIn | enable =
case binaryIn of
0x0 -> 0x0001
0x1 -> 0x0002
0x2 -> 0x0004
0x3 -> 0x0008
0x4 -> 0x0010
0x5 -> 0x0020
0x6 -> 0x0040
0x7 -> 0x0080
0x8 -> 0x0100
0x9 -> 0x0200
0xA -> 0x0400
0xB -> 0x0800
0xC -> 0x1000
0xD -> 0x2000
0xE -> 0x4000
0xF -> 0x8000
decoderCase _ _ = 0
@
Using the `shiftL` function:
@
decoderShift :: Bool -> BitVector 4 -> BitVector 16
decoderShift enable binaryIn =
if enable
then 1 ``shiftL`` ('fromIntegral' binaryIn)
else 0
@
Examples:
>>> decoderCase True 3
0000_0000_0000_1000
>>> decoderShift True 7
0000_0000_1000_0000
The following property holds:
prop> \enable binaryIn -> decoderShift enable binaryIn === decoderCase enable binaryIn
= Encoder
Using a @case@ statement:
@
encoderCase :: Bool -> BitVector 16 -> BitVector 4
encoderCase enable binaryIn | enable =
case binaryIn of
0x0001 -> 0x0
0x0002 -> 0x1
0x0004 -> 0x2
0x0008 -> 0x3
0x0010 -> 0x4
0x0020 -> 0x5
0x0040 -> 0x6
0x0080 -> 0x7
0x0100 -> 0x8
0x0200 -> 0x9
0x0400 -> 0xA
0x0800 -> 0xB
0x1000 -> 0xC
0x2000 -> 0xD
0x4000 -> 0xE
0x8000 -> 0xF
encoderCase _ _ = 0
@
The following property holds:
prop> \en decIn -> en ==> (encoderCase en (decoderCase en decIn) === decIn)
-}
{- $counters
= 8-bit Simple Up Counter
Using `register`:
@
upCounter :: Signal Bool -> Signal (Unsigned 8)
upCounter enable = s
where
s = `register` 0 (`mux` enable (s + 1) s)
@
= 8-bit Up Counter With Load
Using `mealy`:
@
upCounterLd :: Signal (Bool,Bool,Unsigned 8) -> Unsigned 8
upCounterLd = `mealy` upCounterLdT 0
upCounterLdT s (ld,en,dIn) = (s',s)
where
s' | ld = dIn
| en = s + 1
| otherwise = s
@
= 8-bit Up-Down counter
Using `register` and `mux`:
@
upDownCounter :: Signal Bool -> Signal (Unsigned 8)
upDownCounter upDown = s
where
s = `register` 0 (`mux` upDown (s + 1) (s - 1))
@
The following property holds:
prop> \en -> en ==> testFor 1000 (upCounter (pure en) .==. upDownCounter (pure en))
= LFSR
External/Fibonacci LFSR, for @n=16@ and using the primitive polynominal @1 + x^11 + x^13 + x^14 + x^16@
@
lfsrF' :: BitVector 16 -> BitVector 16
lfsrF' s = feedback '++#' 'slice' d15 d1 s
where
feedback = s'!'5 ``xor`` s'!'3 ``xor`` s'!'2 ``xor`` s'!'0
lfsrF :: BitVector 16 -> Signal Bit
lfsrF seed = 'msb' '<$>' r
where r = 'register' seed (lfsrF' '<$>' r)
@
We can also build a internal/Galois LFSR which has better timing characteristics.
We first define a Galois LFSR parametrizable in its filter taps:
@
lfsrGP taps regs = 'zipWith' xorM taps (fb '+>>' regs)
where
fb = 'last' regs
xorM i x | i = x ``xor`` fb
| otherwise = x
@
Then we can instantiate a 16-bit LFSR as follows:
@
lfsrG :: BitVector 16 -> Signal Bit
lfsrG seed = 'last' ('unbundle' r)
where r = 'register' ('unpack' seed) (lfsrGP ('unpack' 0b0011010000000000) '<$>' r)
@
The following property holds:
prop> testFor 100 (lfsrF 0xACE1 .==. lfsrG 0x4645)
= Gray counter
Using the previously defined @upCounter@:
@
grayCounter :: Signal Bool -> Signal (BitVector 8)
grayCounter en = gray '<$>' upCounter en
where gray xs = 'msb' xs '++#' 'xor' ('slice' d7 d1 xs) ('slice' d6 d0 xs)
@
= One-hot counter
Basically a barrel-shifter:
@
oneHotCounter :: Signal Bool -> Signal (BitVector 8)
oneHotCounter enable = s
where
s = 'register' 1 ('mux' enable ('rotateL' '<$>' s '<*>' 1) s)
@
-}
{- $parity_and_crc
= Parity
Just 'reduceXor':
@
parity :: Unsigned 8 -> Bit
parity data_in = `reduceXor` data_in
@
= Serial CRC
* Width = 16 bits
* Truncated polynomial = 0x1021
* Initial value = 0xFFFF
* Input data is NOT reflected
* Output CRC is NOT reflected
* No XOR is performed on the output CRC
@
crcT bv dIn = 'replaceBit' 0 dInXor
$ 'replaceBit' 5 (bv'!'4 ``xor`` dInXor)
$ 'replaceBit' 12 (bv'!'11 ``xor`` dInXor)
rotated
where
dInXor = dIn ``xor`` fb
rotated = 'rotateL' bv 1
fb = 'msb' bv
crc :: Signal Bool -> Signal Bool -> Signal Bit -> Signal (BitVector 16)
crc enable ld dIn = s
where
s = 'register' 0xFFFF ('mux' enable ('mux' ld 0xFFFF (crcT '<$>' s '<*>' dIn)) s)
@
-}
{- $uart
@
{\-\# LANGUAGE RecordWildCards \#-\}
module UART (uart) where
import Clash.Prelude
import Control.Lens
import Control.Monad
import Control.Monad.Trans.State
-- UART RX Logic
data RxReg
= RxReg
{ _rx_reg :: BitVector 8
, _rx_data :: BitVector 8
, _rx_sample_cnt :: Unsigned 4
, _rx_cnt :: Unsigned 4
, _rx_frame_err :: Bool
, _rx_over_run :: Bool
, _rx_empty :: Bool
, _rx_d1 :: Bit
, _rx_d2 :: Bit
, _rx_busy :: Bool
}
makeLenses ''RxReg
uartRX r\@(RxReg {..}) rx_in uld_rx_data rx_enable = 'flip' 'execState' r $ do
-- Synchronise the async signal
rx_d1 '.=' rx_in
rx_d2 '.=' _rx_d1
-- Uload the rx data
'when' uld_rx_data $ do
rx_data '.=' _rx_reg
rx_empty '.=' True
-- Receive data only when rx is enabled
if rx_enable then do
-- Check if just received start of frame
'when' (not _rx_busy && _rx_d2 == 0) $ do
rx_busy '.=' True
rx_sample_cnt '.=' 1
rx_cnt '.=' 0
-- Star of frame detected, Proceed with rest of data
'when' _rx_busy $ do
rx_sample_cnt '+=' 1
-- Logic to sample at middle of data
'when' (_rx_sample_cnt == 7) $ do
if _rx_d1 == 1 && _rx_cnt == 0 then
rx_busy '.=' False
else do
rx_cnt '+=' 1
-- start storing the rx data
'when' (_rx_cnt > 0 && _rx_cnt < 9) $ do
rx_reg '%=' 'replaceBit' (_rx_cnt - 1) _rx_d2
'when' (_rx_cnt == 9) $ do
rx_busy .= False
-- Check if End of frame received correctly
if _rx_d2 == 0 then
rx_frame_err '.=' True
else do
rx_empty '.=' False
rx_frame_err '.=' False
-- Check if last rx data was not unloaded
rx_over_run '.=' not _rx_empty
else do
rx_busy .= False
-- UART TX Logic
data TxReg
= TxReg
{ _tx_reg :: BitVector 8
, _tx_empty :: Bool
, _tx_over_run :: Bool
, _tx_out :: Bit
, _tx_cnt :: Unsigned 4
}
makeLenses ''TxReg
uartTX t\@(TxReg {..}) ld_tx_data tx_data tx_enable = 'flip' 'execState' t $ do
'when' ld_tx_data $ do
if not _tx_empty then
tx_over_run '.=' False
else do
tx_reg '.=' tx_data
tx_empty '.=' False
'when' (tx_enable && not _tx_empty) $ do
tx_cnt '+=' 1
'when' (_tx_cnt == 0) $
tx_out '.=' 0
'when' (_tx_cnt > 0 && _tx_cnt < 9) $
tx_out '.=' _tx_reg '!' (_tx_cnt - 1)
'when' (_tx_cnt == 9) $ do
tx_out '.=' 1
tx_cnt '.=' 0
tx_empty '.=' True
'unless' tx_enable $
tx_cnt '.=' 0
-- Combine RX and TX logic
uart ld_tx_data tx_data tx_enable rx_in uld_rx_data rx_enable =
( _tx_out '<$>' txReg
, _tx_empty '<$>' txReg
, _rx_data '<$>' rxReg
, _rx_empty '<$>' rxReg
)
where
rxReg = register rxRegInit (uartRX '<$>' rxReg '<*>' rx_in '<*>' uld_rx_data
'<*>' rx_enable)
rxRegInit = RxReg { _rx_reg = 0
, _rx_data = 0
, _rx_sample_cnt = 0
, _rx_cnt = 0
, _rx_frame_err = False
, _rx_over_run = False
, _rx_empty = True
, _rx_d1 = 1
, _rx_d2 = 1
, _rx_busy = False
}
txReg = register txRegInit (uartTX '<$>' txReg '<*>' ld_tx_data '<*>' tx_data
'<*>' tx_enable)
txRegInit = TxReg { _tx_reg = 0
, _tx_empty = True
, _tx_over_run = False
, _tx_out = 1
, _tx_cnt = 0
}
@
-}