-
Notifications
You must be signed in to change notification settings - Fork 147
/
Signal.hs
2302 lines (2025 loc) · 67.1 KB
/
Signal.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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-|
Copyright : (C) 2013-2016, University of Twente,
2016-2019, Myrtle Software Ltd,
2017 , Google Inc.,
2021-2023, QBayLogic B.V.
License : BSD2 (see the file LICENSE)
Maintainer : QBayLogic B.V. <devops@qbaylogic.com>
Clash has synchronous 'Signal's in the form of:
@
'Signal' (dom :: 'Domain') a
@
Where /a/ is the type of the value of the 'Signal', for example /Int/ or /Bool/,
and /dom/ is the /clock-/ (and /reset-/) domain to which the memory elements
manipulating these 'Signal's belong.
The type-parameter, /dom/, is of the kind 'Domain' - a simple string. That
string refers to a single /synthesis domain/. A synthesis domain describes the
behavior of certain aspects of memory elements in it. More specifically, a
domain looks like:
@
'DomainConfiguration'
{ _name :: 'Domain'
-- ^ Domain name
, _period :: 'Clash.Promoted.Nat.Nat'
-- ^ Clock period in /ps/
, _activeEdge :: 'ActiveEdge'
-- ^ Active edge of the clock
, _resetKind :: 'ResetKind'
-- ^ Whether resets are synchronous (edge-sensitive) or asynchronous (level-sensitive)
, _initBehavior :: 'InitBehavior'
-- ^ Whether the initial (or "power up") value of memory elements is
-- unknown/undefined, or configurable to a specific value
, _resetPolarity :: ResetPolarity
-- ^ Whether resets are active high or active low
}
@
Check the documentation of each of the types to see the various options Clash
provides. In order to specify a domain, an instance of 'KnownDomain' should be
made. Clash provides an implementation 'System' with some common options
chosen:
@
instance KnownDomain "System" where
type KnownConf "System" = 'DomainConfiguration "System" 10000 'Rising 'Asynchronous 'Defined 'ActiveHigh
knownDomain = SDomainConfiguration SSymbol SNat SRising SAsynchronous SDefined SActiveHigh
@
In words, \"System\" is a synthesis domain with a clock running with a period
of 10000 /ps/. Memory elements respond to the rising edge of the clock,
asynchronously to changes in their resets, and have defined power up values
if applicable.
In order to create a new domain, you don't have to instantiate it explicitly.
Instead, you can have 'createDomain' create a domain for you. You can also use
the same function to subclass existing domains.
* __NB__: \"Bad things\"™ happen when you actually use a clock period of @0@,
so do __not__ do that!
* __NB__: You should be judicious using a clock with period of @1@ as you can
never create a clock that goes any faster!
* __NB__: Whether 'System' has good defaults depends on your target platform.
Check out 'IntelSystem' and 'XilinxSystem' too!
-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Trustworthy #-}
{-# OPTIONS_HADDOCK show-extensions #-}
module Clash.Signal
( -- * Synchronous signals
Signal
, BiSignalIn
, BiSignalOut
, BiSignalDefault(..)
-- * Domain
, Domain
, sameDomain
, KnownDomain(..)
, KnownConfiguration
, ActiveEdge(..)
, SActiveEdge(..)
, InitBehavior(..)
, SInitBehavior(..)
, ResetKind(..)
, SResetKind(..)
, ResetPolarity(..)
, SResetPolarity(..)
, DomainConfiguration(..)
, SDomainConfiguration(..)
-- ** Configuration type families
, DomainPeriod
, DomainActiveEdge
, DomainResetKind
, DomainInitBehavior
, DomainResetPolarity
-- ** Default domains
, System
, XilinxSystem
, IntelSystem
, vSystem
, vIntelSystem
, vXilinxSystem
-- ** Domain utilities
, VDomainConfiguration(..)
, vDomain
, createDomain
, knownVDomain
, clockPeriod
, activeEdge
, resetKind
, initBehavior
, resetPolarity
-- * Clock
, Clock
, DiffClock
, periodToHz
, hzToPeriod
#ifdef CLASH_MULTIPLE_HIDDEN
-- ** Synchronization primitive
, unsafeSynchronizer
#endif
-- * Reset
, Reset
, unsafeToReset
, unsafeFromReset
, unsafeToHighPolarity
, unsafeToLowPolarity
, unsafeFromActiveHigh
, unsafeFromActiveLow
#ifdef CLASH_MULTIPLE_HIDDEN
, convertReset
#endif
, resetSynchronizer
, resetGlitchFilter
, holdReset
-- * Enabling
, Enable
, toEnable
, fromEnable
, E.enableGen
-- * Hidden clock, reset, and enable arguments
-- $hiddenclockandreset
-- ** Monomorphism restriction leads to surprising behavior
-- $monomorphism
-- ** Hidden clock
, HiddenClock
, hideClock
, exposeClock
, withClock
#ifdef CLASH_MULTIPLE_HIDDEN
, exposeSpecificClock
, withSpecificClock
#endif
, hasClock
-- ** Hidden reset
, HiddenReset
, hideReset
, exposeReset
, withReset
#ifdef CLASH_MULTIPLE_HIDDEN
, exposeSpecificReset
, withSpecificReset
#endif
, hasReset
-- ** Hidden enable
, HiddenEnable
, hideEnable
, exposeEnable
, withEnable
#ifdef CLASH_MULTIPLE_HIDDEN
, exposeSpecificEnable
, withSpecificEnable
#endif
, hasEnable
-- ** Hidden clock, reset, and enable
, HiddenClockResetEnable
, hideClockResetEnable
, exposeClockResetEnable
, withClockResetEnable
#ifdef CLASH_MULTIPLE_HIDDEN
, exposeSpecificClockResetEnable
, withSpecificClockResetEnable
#endif
, SystemClockResetEnable
-- * Basic circuit functions
, andEnable
#ifdef CLASH_MULTIPLE_HIDDEN
, andSpecificEnable
#endif
, dflipflop
, delay
, delayMaybe
, delayEn
, register
, regMaybe
, regEn
, mux
-- * Simulation and testbench functions
, clockGen
, resetGen
, resetGenN
, systemClockGen
, systemResetGen
-- * Boolean connectives
, (.&&.), (.||.)
-- * Product/Signal isomorphism
, Bundle(..)
, EmptyTuple(..)
, TaggedEmptyTuple(..)
-- * Simulation functions (not synthesizable)
, simulate
, simulateB
, simulateN
, simulateWithReset
, simulateWithResetN
, runUntil
-- ** lazy versions
, simulate_lazy
, simulateB_lazy
-- ** Automaton
, signalAutomaton
-- * List \<-\> Signal conversion (not synthesizable)
, sample
, sampleN
, sampleWithReset
, sampleWithResetN
, fromList
, fromListWithReset
-- ** lazy versions
, sample_lazy
, sampleN_lazy
, fromList_lazy
-- * QuickCheck combinators
, testFor
-- * Type classes
-- ** 'Eq'-like
, (.==.), (./=.)
-- ** 'Ord'-like
, (.<.), (.<=.), (.>=.), (.>.)
-- * Bisignal functions
, veryUnsafeToBiSignalIn
, readFromBiSignal
, writeToBiSignal
, mergeBiSignalOuts
-- * Internals
, HiddenClockName
, HiddenResetName
, HiddenEnableName
-- * Deprecated
, unsafeFromHighPolarity
, unsafeFromLowPolarity
)
where
import Control.Arrow.Transformer.Automaton (Automaton)
import GHC.TypeLits (type (<=))
import Data.Proxy (Proxy(..))
import Prelude
import Test.QuickCheck (Property, property)
#ifdef CLASH_MULTIPLE_HIDDEN
import GHC.TypeLits (AppendSymbol)
import Clash.Class.HasDomain (WithSingleDomain)
#endif
import Clash.Class.HasDomain (WithSpecificDomain)
import qualified Clash.Explicit.Signal as E
import qualified Clash.Explicit.Reset as E
import Clash.Explicit.Reset (resetSynchronizer, resetGlitchFilter)
import Clash.Explicit.Signal (systemClockGen, systemResetGen)
import Clash.Hidden
import Clash.Promoted.Nat (SNat (..), snatToNum)
import Clash.Signal.Bundle
(Bundle (..), EmptyTuple(..), TaggedEmptyTuple(..))
import Clash.Signal.BiSignal --(BisignalIn, BisignalOut, )
import Clash.Signal.Internal hiding
(sample, sample_lazy, sampleN, sampleN_lazy, simulate, simulate_lazy, testFor,
signalAutomaton)
import Clash.Signal.Internal.Ambiguous
(knownVDomain, clockPeriod, activeEdge, resetKind, initBehavior, resetPolarity)
import Clash.XException (NFDataX, ShowX)
{- $setup
>>> :set -XFlexibleContexts -XTypeApplications
>>> :m -Prelude
>>> import Clash.Prelude
>>> import Clash.Promoted.Nat (SNat(..))
>>> import Clash.XException (printX)
>>> import Control.Applicative (liftA2)
>>> let oscillate = register False (not <$> oscillate)
>>> let count = regEn 0 oscillate (count + 1)
>>> :{
let sometimes1 = s where
s = register Nothing (switch <$> s)
switch Nothing = Just 1
switch _ = Nothing
:}
>>> :{
let countSometimes = s where
s = regMaybe 0 (plusM (pure <$> s) sometimes1)
plusM = liftA2 (liftA2 (+))
:}
-}
{- $hiddenclockandreset #hiddenclockandreset#
Clocks, resets and enables are by default implicitly routed to their components.
You can see from the type of a component whether it has hidden clock, reset or
enable arguments:
It has a hidden clock when it has a:
@
f :: 'HiddenClock' dom => ...
@
Constraint.
Or it has a hidden reset when it has a:
@
g :: 'HiddenReset' dom => ...
@
Constraint.
Or it has a hidden enable when it has a:
@
g :: 'HiddenEnable' dom => ...
@
Constraint.
Or it has a hidden clock argument, a hidden reset argument and a hidden enable
argument when it has a:
@
h :: 'HiddenClockResetEnable' dom => ..
@
Constraint.
Given a component with explicit clock, reset and enable arguments, you can turn
them into hidden arguments using 'hideClock', 'hideReset', and 'hideEnable'. So
given a:
@
f :: Clock dom -> Reset dom -> Enable dom -> Signal dom a -> ...
@
You hide the clock and reset arguments by:
@
-- g :: 'HiddenClockResetEnable' dom => Signal dom a -> ...
g = 'hideClockResetEnable' f
@
Or, alternatively, by:
@
-- h :: 'HiddenClockResetEnable' dom => Signal dom a -> ...
h = f 'hasClock' 'hasReset' 'hasEnable'
@
== Assigning explicit clock, reset and enable arguments to hidden clocks, resets and enables
Given a component:
@
f :: 'HiddenClockResetEnable' dom
=> Signal dom Int
-> Signal dom Int
@
which has hidden clock, reset and enable arguments, we expose those hidden
arguments so that we can explicitly apply them:
@
-- g :: Clock dom -> Reset dom -> Enable dom -> Signal dom Int -> Signal dom Int
g = 'exposeClockResetEnable' f
@
or, alternatively, by:
@
-- h :: Clock dom -> Reset dom -> Enable dom -> Signal dom Int -> Signal dom Int
h clk rst en = 'withClockResetEnable' clk rst en f
@
Similarly, there are 'exposeClock', 'exposeReset' and 'exposeEnable' to just
expose the hidden clock, the hidden reset or the hidden enable argument.
You will need to explicitly apply clocks and resets when you want to use
components such as PLLs and 'resetSynchronizer':
@
topEntity
:: Clock System
-> Reset System
-> Signal System Bit
-> Signal System (BitVector 8)
topEntity clk rst key1 =
let (pllOut,pllStable) = 'Clash.Intel.ClockGen.altpll' (SSymbol \@\"altpll50\") clk rst
rstSync = 'resetSynchronizer' pllOut (unsafeToHighPolarity pllStable)
in 'exposeClockResetEnable' leds pllOut rstSync enableGen
where
key1R = isRising 1 key1
leds = mealy blinkerT (1, False, 0) key1R
@
or, using the alternative method:
@
topEntity
:: Clock System
-> Reset System
-> Signal System Bit
-> Signal System (BitVector 8)
topEntity clk rst key1 =
let (pllOut,pllStable) = 'Clash.Intel.ClockGen.altpll' (SSymbol \@\"altpll50\") clk rst
rstSync = 'resetSynchronizer' pllOut (unsafeToHighPolarity pllStable)
in 'withClockResetEnable' pllOut rstSync enableGen leds
where
key1R = isRising 1 key1
leds = mealy blinkerT (1, False, 0) key1R
@
-}
{- $monomorphism #monomorphism#
If you don't provide a type signature for a function, Haskell will infer one for
you. Sometimes this inferred type is less general than you would expect. This
can be due to the monomorphism restriction, which is a rather intricate
technical aspect of Haskell's type system. You don't need to understand it to
avoid the problems it creates with hidden parameters, though.
The @expose...@ and @with...@ functions for hidden clocks, resets, and enables
are intended to be used to resolve a function with hidden parameters into a
function without that hidden parameter. Put differently, 'exposeClock' and
'withClock' are not themselves used in a 'HiddenClock' context, and so on for
resets and enables. If the rule that they are not themselves in a @Hidden...@
context is observed, they will function as expected. No specific consideration
is needed in these cases.
However, the function 'andEnable' is explicitly designed to be used within a
'HiddenEnable' context. In such a situation, it is important to provide a type
signature for the component that is given to `andEnable` as an argument, and not
let Haskell infer one.
The use of 'andEnable' has an unfortunate interaction with Haskells monomorphism
restriction that can lead to very surprising behavior. All of the following also
applies to using 'exposeClock' and 'withClock' inside a 'HiddenClock' context,
and so on for resets and enables.
When you write a function
@
f :: HiddenClockResetEnable dom
=> Signal dom Bool
-> Signal dom Int
-> Signal dom Int
f en i = andEnable en g i -- BROKEN
where
g = register 0
@
you would intuitively think this has the following type for the local function @g@:
@
f :: forall dom
. HiddenClockResetEnable dom
=> Signal dom Bool
-> Signal dom Int
-> Signal dom Int
f en i = andEnable en g i
where
g :: HiddenClockResetEnable dom => Signal dom Int -> Signal dom Int
g = register 0
@
but instead, the monomorphism restriction will cause the following type to be inferred:
@
f :: forall dom
. HiddenClockResetEnable dom
=> Signal dom Bool
-> Signal dom Int
-> Signal dom Int
f en i = andEnable en g i -- BROKEN
where
g :: Signal dom Int -> Signal dom Int
g = register 0
@
The monomorphism restriction essentially misqualifies the implicit parameter as
polymorphism, and tries to remove the implicit parameter from the context of the
function's type. It /can/ do that because the outer scope already has a
'HiddenEnable' context. But by getting that implicit parameter of the enclosing
function as context, it also gets the value of the parameter of the enclosing
function. So the Enable line for @g@ is the Enable line of @f@, and the Enable
line produced by 'andEnable' that was intended to be connected to @g@ is not
connected to anything!
When using 'andEnable', you should always explicitly provide the type signature
for the component given to 'andEnable' as an argument, thereby avoiding
surprising inferred types. We don't advise you to turn off the monomorphism
restriction, as this may have undesirable consequences.
Note that the inferred type is not always incorrect. The following variant works
correctly:
@
f :: HiddenClockResetEnable dom
=> Signal dom Bool
-> Signal dom Int
-> Signal dom Int
f en i = andEnable en g i
where
g i = register 0 i
@
This is an instance of the very first example on
<https://wiki.haskell.org/Monomorphism_restriction HaskellWiki>, @f1@ (as
opposed to @f4@). The monomorphism restriction works differently for function
bindings and pattern bindings. Since @g@ here has a formal parameter, it is a
function binding, and the monomorphish restriction does not kick in. The code
works as expected. If a later code change removes the formal parameter, all of a
sudden the code silently disregards the @en@ signal! Adhering to the rule that
you should always explicitly provide the type signature for the component given
to 'andEnable' as an argument would have avoided this hard to debug problem.
-}
#ifdef CLASH_MULTIPLE_HIDDEN
type HiddenClockName dom = AppendSymbol dom "_clk"
type HiddenResetName dom = AppendSymbol dom "_rst"
type HiddenEnableName dom = AppendSymbol dom "_en"
#else
type HiddenClockName (dom :: Domain) = "clock"
type HiddenResetName (dom :: Domain) = "reset"
type HiddenEnableName (dom :: Domain) = "enable"
#endif
-- | A /constraint/ that indicates the component has a hidden 'Clock'
--
-- <Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
type HiddenClock dom =
( Hidden (HiddenClockName dom) (Clock dom)
, KnownDomain dom )
-- | A /constraint/ that indicates the component needs a 'Reset'
--
-- <Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
type HiddenReset dom =
( Hidden (HiddenResetName dom) (Reset dom)
, KnownDomain dom )
-- | A /constraint/ that indicates the component needs an 'Enable'
--
-- <Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
type HiddenEnable dom =
( Hidden (HiddenEnableName dom) (Enable dom)
, KnownDomain dom )
-- | A /constraint/ that indicates the component needs a 'Clock', a 'Reset',
-- and an 'Enable' belonging to the same @dom@.
--
-- <Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
type HiddenClockResetEnable dom =
( HiddenClock dom
, HiddenReset dom
, HiddenEnable dom
)
-- | A /constraint/ that indicates the component needs a 'Clock', a 'Reset',
-- and an 'Enable' belonging to the 'System' domain.
--
-- <Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
type SystemClockResetEnable =
( Hidden (HiddenClockName System) (Clock System)
, Hidden (HiddenResetName System) (Reset System)
, Hidden (HiddenEnableName System) (Enable System)
)
{- | Expose a hidden 'Clock' argument of a component, so it can be applied
explicitly.
#ifdef CLASH_MULTIPLE_HIDDEN
This function can only be used on components with a single
domain. For example, this function will refuse when:
@
r ~ HiddenClock dom1 => Signal dom1 a -> Signal dom2 a
@
But will work when:
@
r ~ HiddenClock dom => Signal dom a -> Signal dom a
@
If you want to expose a clock of a component working on multiple domains
(such as the first example), use 'exposeSpecificClock'.
#endif
<Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
=== __Example__
Usage with a /polymorphic/ domain:
>>> reg = register 5 (reg + 1)
>>> sig = exposeClock reg clockGen
>>> sampleN @System 10 sig
[5,5,6,7,8,9,10,11,12,13]
Force 'exposeClock' to work on 'System' (hence 'sampleN' not needing an explicit
domain later):
>>> reg = register 5 (reg + 1)
>>> sig = exposeClock @System reg clockGen
>>> sampleN 10 sig
[5,5,6,7,8,9,10,11,12,13]
-}
exposeClock
:: forall dom r
.
#ifdef CLASH_MULTIPLE_HIDDEN
WithSingleDomain dom r =>
#endif
(HiddenClock dom => r)
-- ^ The component with a hidden clock
-> (KnownDomain dom => Clock dom -> r)
-- ^ The component with its clock argument exposed
exposeClock = \f clk -> exposeSpecificClock (const f) clk (Proxy @dom)
-- See Note [Going from WithSingleDomain to WithSpecificDomain]
{-# INLINE exposeClock #-}
-- Note [Going from WithSingleDomain to WithSpecificDomain]
--
-- Functions like 'exposeSpecificClock' have a 'WithSpecificDomain dom r'
-- constraint on the component with type 'r' that's passed to them. This
-- requires 'dom' to be present in 'r' at the time the function is used,
-- otherwise it will not type-check.
--
-- Functions like 'exposeClock' have a 'WithSingleDomain dom r' constraint, so
-- it is known that the domain 'dom' is indeed in 'r'. So we can safely
-- introduce 'dom' into the type passed to 'exposeSpecificClock'. By introducing
-- 'dom' into the type, the type checker can find the 'dom' when it type-checks
-- that the use of 'exposeSpecificClock' in 'exposeClock' satisies the
-- 'WithSpecificDomain dom r' constraint.
--
-- So given:
--
-- exposeClock
-- :: forall dom r
-- . WithSingleDomain dom r
-- => (HiddenClock dom => r)
-- -> (KnownDomain dom => Clock dom -> r)
-- exposeClock = \f clk -> exposeSpecificClock (const f) clk (Proxy @dom)
--
-- The type of 'exposeSpecificClock' as called from 'exposeClock' could be
-- written something like:
--
-- exposeSpecificClock
-- :: ( WithSpecificDomain dom s
-- , s ~ (Proxy dom -> r)
-- )
-- => (HiddenClock dom => Proxy dom -> r)
-- -> (KnownDomain dom => Clock dom -> Proxy dom -> r)
--
-- The type-checker can now find 'dom' in the 'Proxy', so it type-checks.
--
-- The argument
--
-- (HiddenClock dom => Proxy dom -> r)
--
-- is filled in as 'const f', consuming the 'Proxy' before passing on to 'f'.
--
-- In the resulting
--
-- (KnownDomain dom => Clock dom -> Proxy dom -> r)
--
-- the filled in values are 'clk' and 'Proxy @dom', leaving 'r'.
{- | Expose a hidden 'Clock' argument of a component, so it can be applied
explicitly. This function can be used on components with multiple domains.
As opposed to 'exposeClock', callers should explicitly state what the clock
domain is. See the examples for more information.
<Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
#ifdef CLASH_MULTIPLE_HIDDEN
=== __Example__
'exposeSpecificClock' can only be used when it can find the specified domain
in /r/:
>>> reg = register @System 5 (reg + 1)
>>> sig = exposeSpecificClock @System reg clockGen
>>> sampleN 10 sig
[5,5,6,7,8,9,10,11,12,13]
Type variables work too, if they are in scope. For example:
@
reg = 'register' @@dom 5 (reg + 1)
sig = 'exposeSpecificClock' @@dom reg 'clockGen'
@
#endif
-}
exposeSpecificClock
:: forall dom r
. WithSpecificDomain dom r
=> (HiddenClock dom => r)
-- ^ The component with a hidden clock
-> (KnownDomain dom => Clock dom -> r)
-- ^ The component with its clock argument exposed
exposeSpecificClock = \f clk -> expose @(HiddenClockName dom) f clk
{-# INLINE exposeSpecificClock #-}
-- | Hide the 'Clock' argument of a component, so it can be routed implicitly.
--
-- <Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
hideClock
:: forall dom r
. HiddenClock dom
=> (Clock dom -> r)
-- ^ Function whose clock argument you want to hide
-> r
hideClock = \f -> f (fromLabel @(HiddenClockName dom))
{-# INLINE hideClock #-}
{- | Connect an explicit 'Clock' to a function with a hidden 'Clock'.
#ifdef CLASH_MULTIPLE_HIDDEN
This function can only be used on components with a single domain. For
example, this function will refuse when:
@
r ~ HiddenClock dom1 => Signal dom1 a -> Signal dom2 a
@
But will work when:
@
r ~ HiddenClock dom => Signal dom a -> Signal dom a
@
If you want to connect a clock to a component working on multiple domains
(such as the first example), use 'withSpecificClock'.
#endif
<Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
=== __Example__
Usage with a /polymorphic/ domain:
>>> reg = register 5 (reg + 1)
>>> sig = withClock clockGen reg
>>> sampleN @System 10 sig
[5,5,6,7,8,9,10,11,12,13]
Force 'withClock' to work on 'System' (hence 'sampleN' not needing an explicit
domain later):
>>> reg = register 5 (reg + 1)
>>> sig = withClock @System clockGen reg
>>> sampleN 10 sig
[5,5,6,7,8,9,10,11,12,13]
-}
withClock
:: forall dom r
.
#ifdef CLASH_MULTIPLE_HIDDEN
WithSingleDomain dom r =>
#endif
KnownDomain dom
=> Clock dom
-- ^ The 'Clock' we want to connect
-> (HiddenClock dom => r)
-- ^ The function with a hidden 'Clock' argument
-> r
withClock clk f = withSpecificClock clk (const f) (Proxy @dom)
-- See Note [Going from WithSingleDomain to WithSpecificDomain]
{-# INLINE withClock #-}
{- | Connect an explicit 'Clock' to a function with a hidden 'Clock'. This
function can be used on components with multiple domains. As opposed to
'withClock', callers should explicitly state what the clock domain is. See
the examples for more information.
<Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
#ifdef CLASH_MULTIPLE_HIDDEN
=== __Example__
'withSpecificClock' can only be used when it can find the specified domain
in /r/:
>>> reg = register @System 5 (reg + 1)
>>> sig = withSpecificClock @System clockGen reg
>>> sampleN 10 sig
[5,5,6,7,8,9,10,11,12,13]
Type variables work too, if they are in scope. For example:
@
reg = 'register' @@dom 5 (reg + 1)
sig = 'withSpecificClock' @@dom 'clockGen' reg
@
#endif
-}
withSpecificClock
:: forall dom r
. (KnownDomain dom, WithSpecificDomain dom r)
=> Clock dom
-- ^ The 'Clock' we want to connect
-> (HiddenClock dom => r)
-- ^ The function with a hidden 'Clock' argument
-> r
withSpecificClock = \clk f -> expose @(HiddenClockName dom) f clk
{-# INLINE withSpecificClock #-}
-- | Connect a hidden 'Clock' to an argument where a normal 'Clock' argument
-- was expected.
--
-- <Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
hasClock
:: forall dom
. HiddenClock dom
=> Clock dom
hasClock = fromLabel @(HiddenClockName dom)
{-# INLINE hasClock #-}
{- | Expose a hidden 'Reset' argument of a component, so it can be applied
explicitly.
#ifdef CLASH_MULTIPLE_HIDDEN
This function can only be used on components with a single domain. For
example, this function will refuse when:
@
r ~ HiddenReset dom1 => Signal dom1 a -> Signal dom2 a
@
But will work when:
@
r ~ HiddenReset dom => Signal dom a -> Signal dom a
@
If you want to expose a reset of a component working on multiple domains
(such as the first example), use 'exposeSpecificReset'.
<Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
#endif
=== __Example__
Usage with a /polymorphic/ domain:
>>> reg = register 5 (reg + 1)
>>> sig = exposeReset reg resetGen
>>> sampleN @System 10 sig
[5,5,6,7,8,9,10,11,12,13]
Force 'exposeReset' to work on 'System' (hence 'sampleN' not needing an explicit
domain later):
>>> reg = register 5 (reg + 1)
>>> sig = exposeReset @System reg resetGen
>>> sampleN 10 sig
[5,5,6,7,8,9,10,11,12,13]
-}
exposeReset
:: forall dom r
.
#ifdef CLASH_MULTIPLE_HIDDEN
WithSingleDomain dom r =>
#endif
(HiddenReset dom => r)
-- ^ The component with a hidden reset
-> (KnownDomain dom => Reset dom -> r)
-- ^ The component with its reset argument exposed
exposeReset = \f rst -> exposeSpecificReset (const f) rst (Proxy @dom)
-- See Note [Going from WithSingleDomain to WithSpecificDomain]
{-# INLINE exposeReset #-}
{- | Expose a hidden 'Reset' argument of a component, so it can be applied
explicitly. This function can be used on components with multiple domains.
As opposed to 'exposeReset', callers should explicitly state what the reset
domain is. See the examples for more information.
<Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
#ifdef CLASH_MULTIPLE_HIDDEN
=== __Example__
'exposeSpecificReset' can only be used when it can find the specified domain
in /r/:
>>> reg = register @System 5 (reg + 1)
>>> sig = exposeSpecificReset @System reg resetGen
>>> sampleN 10 sig
[5,5,6,7,8,9,10,11,12,13]
Type variables work too, if they are in scope. For example:
@
reg = 'register' @@dom 5 (reg + 1)
sig = 'exposeSpecificReset' @@dom reg 'resetGen'
@
#endif
-}
exposeSpecificReset
:: forall dom r
. WithSpecificDomain dom r
=> (HiddenReset dom => r)
-- ^ The component with a hidden reset
-> (KnownDomain dom => Reset dom -> r)
-- ^ The component with its reset argument exposed
exposeSpecificReset = \f rst -> expose @(HiddenResetName dom) f rst
{-# INLINE exposeSpecificReset #-}
-- | Hide the 'Reset' argument of a component, so it can be routed implicitly.
--
-- <Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
hideReset
:: forall dom r
. HiddenReset dom
=> (Reset dom -> r)
-- ^ Component whose reset argument you want to hide
-> r
hideReset = \f -> f (fromLabel @(HiddenResetName dom))
{-# INLINE hideReset #-}
{- | Connect an explicit 'Reset' to a function with a hidden 'Reset'.
#ifdef CLASH_MULTIPLE_HIDDEN
This function can only be used on components with a single domain. For
example, this function will refuse when:
@
r ~ HiddenReset dom1 => Signal dom1 a -> Signal dom2 a
@
But will work when:
@
r ~ HiddenReset dom => Signal dom a -> Signal dom a
@
If you want to connect a reset to a component working on multiple domains
(such as the first example), use 'withSpecificReset'.
<Clash-Signal.html#hiddenclockandreset Click here to read more about hidden clocks, resets, and enables>
#endif
=== __Example__
Usage with a /polymorphic/ domain:
>>> reg = register 5 (reg + 1)
>>> sig = withReset resetGen reg
>>> sampleN @System 10 sig
[5,5,6,7,8,9,10,11,12,13]
Force 'withReset' to work on 'System' (hence 'sampleN' not needing an explicit
domain later):
>>> reg = register 5 (reg + 1)
>>> sig = withReset @System resetGen reg
>>> sampleN 10 sig
[5,5,6,7,8,9,10,11,12,13]
-}
withReset
:: forall dom r
.
#ifdef CLASH_MULTIPLE_HIDDEN
WithSingleDomain dom r =>
#endif
KnownDomain dom
=> Reset dom
-- ^ The 'Reset' we want to connect
-> (HiddenReset dom => r)