@@ -57,15 +57,15 @@ import Text.Read (readEither)
57
57
import Text.Trifecta.Result hiding (Err )
58
58
59
59
import Clash.Backend
60
- (Backend (.. ), Usage (.. ), AggressiveXOptBB (.. ), RenderEnums (.. ))
60
+ (Backend (.. ), DomainMap , Usage (.. ), AggressiveXOptBB (.. ), RenderEnums (.. ))
61
61
import Clash.Netlist.BlackBox.Parser
62
62
import Clash.Netlist.BlackBox.Types
63
63
import Clash.Netlist.Types
64
64
(BlackBoxContext (.. ), Expr (.. ), HWType (.. ), Literal (.. ), Modifier (.. ),
65
65
Declaration (BlackBoxD ))
66
66
import qualified Clash.Netlist.Id as Id
67
67
import qualified Clash.Netlist.Types as N
68
- import Clash.Netlist.Util (typeSize , isVoid , stripVoid )
68
+ import Clash.Netlist.Util (typeSize , isVoid , stripAttributes , stripVoid )
69
69
import Clash.Signal.Internal
70
70
(ResetKind (.. ), ResetPolarity (.. ), InitBehavior (.. ), VDomainConfiguration (.. ))
71
71
import Clash.Util
@@ -493,20 +493,20 @@ renderElem b (IF c t f) = do
493
493
syn <- hdlSyn
494
494
enums <- renderEnums
495
495
xOpt <- aggressiveXOptBB
496
- let c' = check (coerce xOpt) iw hdl syn enums c
496
+ c' <- check (coerce xOpt) iw hdl syn enums c
497
497
if c' > 0 then renderTemplate b t else renderTemplate b f
498
498
where
499
- check :: Bool -> Int -> HDL -> HdlSyn -> RenderEnums -> Element -> Int
499
+ check :: Backend backend => Bool -> Int -> HDL -> HdlSyn -> RenderEnums -> Element -> State backend Int
500
500
check xOpt iw hdl syn enums c' = case c' of
501
- (Size e) -> typeSize (lineToType b [e])
502
- (Length e) -> case lineToType b [e] of
501
+ (Size e) -> pure $ typeSize (lineToType b [e])
502
+ (Length e) -> pure $ case lineToType b [e] of
503
503
(Vector n _) -> n
504
504
Void (Just (Vector n _)) -> n
505
505
(MemBlob n _) -> n
506
506
Void (Just (MemBlob n _)) -> n
507
507
_ -> 0 -- HACK: So we can test in splitAt if one of the
508
508
-- vectors in the tuple had a zero length
509
- (Lit n) -> case bbInputs b !! n of
509
+ (Lit n) -> pure $ case bbInputs b !! n of
510
510
(l,_,_)
511
511
| Literal _ l' <- l ->
512
512
case l' of
@@ -534,16 +534,16 @@ renderElem b (IF c t f) = do
534
534
, [Literal _ (NumLit j)] <- extractLiterals bbCtx
535
535
-> fromInteger j
536
536
k -> error $ $ (curLoc) ++ (" IF: LIT must be a numeric lit:" ++ show k)
537
- (Depth e) -> case lineToType b [e] of
537
+ (Depth e) -> pure $ case lineToType b [e] of
538
538
(RTree n _) -> n
539
539
_ -> error $ $ (curLoc) ++ " IF: treedepth of non-tree type"
540
- IW64 -> if iw == 64 then 1 else 0
541
- (HdlSyn s) -> if s == syn then 1 else 0
542
- (IsVar n) -> let (e,_,_) = bbInputs b !! n
540
+ IW64 -> pure $ if iw == 64 then 1 else 0
541
+ (HdlSyn s) -> pure $ if s == syn then 1 else 0
542
+ (IsVar n) -> pure $ let (e,_,_) = bbInputs b !! n
543
543
in case e of
544
544
Identifier _ Nothing -> 1
545
545
_ -> 0
546
- (IsLit n) -> let (e,_,_) = bbInputs b !! n
546
+ (IsLit n) -> pure $ let (e,_,_) = bbInputs b !! n
547
547
in case e of
548
548
DataCon {} -> 1
549
549
Literal {} -> 1
@@ -557,13 +557,13 @@ renderElem b (IF c t f) = do
557
557
RenderEnums True -> 1
558
558
RenderEnums False -> 0
559
559
isScalar _ _ = 0
560
- in isScalar hdl ty
560
+ in pure $ isScalar hdl ty
561
561
562
- (IsUndefined n) ->
562
+ (IsUndefined n) -> pure $
563
563
let (e, _, _) = bbInputs b !! n
564
564
in if xOpt && checkUndefined e then 1 else 0
565
565
566
- (IsActiveEnable n) ->
566
+ (IsActiveEnable n) -> pure $
567
567
let (e, ty, _) = bbInputs b !! n in
568
568
case ty of
569
569
Enable _ ->
@@ -585,52 +585,80 @@ renderElem b (IF c t f) = do
585
585
_ ->
586
586
error $ $ (curLoc) ++ " IsActiveEnable: Expected Bool or Enable, not: " ++ show ty
587
587
588
- (ActiveEdge edgeRequested n) ->
589
- let (_, ty, _) = bbInputs b !! n in
590
- case stripVoid ty of
591
- KnownDomain _ _ edgeActual _ _ _ ->
588
+ (ActiveEdge edgeRequested n) -> do
589
+ let (_, ty, _) = bbInputs b !! n
590
+ domConf <- getDomainConf ty
591
+ case domConf of
592
+ VDomainConfiguration _ _ edgeActual _ _ _ -> pure $
592
593
if edgeRequested == edgeActual then 1 else 0
593
- _ ->
594
- error $ $ (curLoc) ++ " ActiveEdge: Expected `KnownDomain` or `KnownConfiguration`, not: " ++ show ty
595
-
596
- (IsSync n) ->
597
- let (_, ty, _) = bbInputs b !! n in
598
- case stripVoid ty of
599
- KnownDomain _ _ _ Synchronous _ _ -> 1
600
- KnownDomain _ _ _ Asynchronous _ _ -> 0
601
- _ -> error $ $ (curLoc) ++ " IsSync: Expected `KnownDomain` or `KnownConfiguration`, not: " ++ show ty
602
-
603
- (IsInitDefined n) ->
604
- let (_, ty, _) = bbInputs b !! n in
605
- case stripVoid ty of
606
- KnownDomain _ _ _ _ Defined _ -> 1
607
- KnownDomain _ _ _ _ Unknown _ -> 0
608
- _ -> error $ $ (curLoc) ++ " IsInitDefined: Expected `KnownDomain` or `KnownConfiguration`, not: " ++ show ty
609
-
610
- (IsActiveHigh n) ->
611
- let (_, ty, _) = bbInputs b !! n in
612
- case stripVoid ty of
613
- KnownDomain _ _ _ _ _ ActiveHigh -> 1
614
- KnownDomain _ _ _ _ _ ActiveLow -> 0
615
- _ -> error $ $ (curLoc) ++ " IsActiveHigh: Expected `KnownDomain` or `KnownConfiguration`, not: " ++ show ty
616
-
617
- (StrCmp [Text t1] n) ->
594
+
595
+ (IsSync n) -> do
596
+ let (_, ty, _) = bbInputs b !! n
597
+ domConf <- getDomainConf ty
598
+ case domConf of
599
+ VDomainConfiguration _ _ _ Synchronous _ _ -> pure 1
600
+ VDomainConfiguration _ _ _ Asynchronous _ _ -> pure 0
601
+
602
+ (IsInitDefined n) -> do
603
+ let (_, ty, _) = bbInputs b !! n
604
+ domConf <- getDomainConf ty
605
+ case domConf of
606
+ VDomainConfiguration _ _ _ _ Defined _ -> pure 1
607
+ VDomainConfiguration _ _ _ _ Unknown _ -> pure 0
608
+
609
+ (IsActiveHigh n) -> do
610
+ let (_, ty, _) = bbInputs b !! n
611
+ domConf <- getDomainConf ty
612
+ case domConf of
613
+ VDomainConfiguration _ _ _ _ _ ActiveHigh -> pure 1
614
+ VDomainConfiguration _ _ _ _ _ ActiveLow -> pure 0
615
+
616
+ (StrCmp [Text t1] n) -> pure $
618
617
let (e,_,_) = bbInputs b !! n
619
618
in case exprToString e of
620
619
Just t2
621
620
| t1 == Text. pack t2 -> 1
622
621
| otherwise -> 0
623
622
Nothing -> error $ $ (curLoc) ++ " Expected a string literal: " ++ show e
624
- (And es) -> if all (/= 0 ) (map (check xOpt iw hdl syn enums) es)
623
+ (And es) -> do
624
+ es' <- mapM (check xOpt iw hdl syn enums) es
625
+ pure $ if all (/= 0 ) es'
625
626
then 1
626
627
else 0
627
- CmpLE e1 e2 -> if check xOpt iw hdl syn enums e1 <= check xOpt iw hdl syn enums e2
628
- then 1
629
- else 0
628
+ CmpLE e1 e2 -> do
629
+ v1 <- check xOpt iw hdl syn enums e1
630
+ v2 <- check xOpt iw hdl syn enums e2
631
+ if v1 <= v2
632
+ then pure 1
633
+ else pure 0
630
634
_ -> error $ $ (curLoc) ++ " IF: condition must be: SIZE, LENGTH, LIT, DEPTH, IW64, VIVADO, OTHERSYN, ISVAR, ISLIT, ISUNDEFINED, ISACTIVEENABLE, ACTIVEEDGE, ISSYNC, ISINITDEFINED, ISACTIVEHIGH, STRCMP, AND, ISSCALAR or CMPLE."
631
635
++ " \n Got: " ++ show c'
632
636
renderElem b e = fmap const (renderTag b e)
633
637
638
+ getDomainConf :: (Backend backend , HasCallStack ) => HWType -> State backend VDomainConfiguration
639
+ getDomainConf = generalGetDomainConf domainConfigurations
640
+
641
+ generalGetDomainConf
642
+ :: (Monad m , HasCallStack )
643
+ => (m DomainMap ) -- ^ a way to get the `DomainMap`
644
+ -> HWType -> m VDomainConfiguration
645
+ generalGetDomainConf getDomainMap ty = case (snd . stripAttributes . stripVoid) ty of
646
+ KnownDomain dom period activeEdge resetKind initBehavior resetPolarity ->
647
+ pure $ VDomainConfiguration (Data.Text. unpack dom) (fromIntegral period) activeEdge resetKind initBehavior resetPolarity
648
+
649
+ Clock dom -> go dom
650
+ ClockN dom -> go dom
651
+ Reset dom -> go dom
652
+ Enable dom -> go dom
653
+ Product _DiffClock _ [Clock dom,_clkN] -> go dom
654
+ t -> error $ $ (curLoc) ++ " Don't know how to get a Domain out of HWType: " <> show t
655
+ where
656
+ go dom = do
657
+ doms <- getDomainMap
658
+ case HashMap. lookup dom doms of
659
+ Nothing -> error $ " Can't find domain " <> show dom
660
+ Just conf -> pure conf
661
+
634
662
parseFail :: Text -> BlackBoxTemplate
635
663
parseFail t = case runParse t of
636
664
Failure errInfo ->
0 commit comments