Skip to content

Clash gets stuck on this design #2882

@pieter-bos

Description

@pieter-bos

The below self-contained example makes clash not terminate:

Unfold for original example
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Top where

import Clash.Prelude
import Clash.Annotations.TH

class KnownNat (UsbSize a) => UsbSerialize a where
  type UsbSize a :: Nat
  usbDeserialize :: BitVector (UsbSize a) -> a

newtype Packed a = Packed { unPacked :: a }

instance BitPack a => UsbSerialize (Packed a) where
  type UsbSize (Packed a) = BitSize a
  usbDeserialize = Packed . unpack

data PID
  = Out
  | In
  deriving (Generic, NFDataX, BitPack)

instance UsbSerialize PID where
  type UsbSize PID = UsbSize (Packed PID)
  usbDeserialize = unPacked . usbDeserialize

baseCrc ::
  HiddenClockResetEnable dom =>
  Signal dom (Maybe Bit) ->
  Signal dom (Maybe Bit)
baseCrc = mealy (\() _ -> deepErrorX "Q") ()

bufferFirst ::
  HiddenClockResetEnable dom =>
  Signal dom (Maybe Bit) ->
  Signal dom (Maybe PID)
bufferFirst =
  mealy step 0
 where
  step buf (Just 0) = (0, Just $ usbDeserialize buf)
  step buf _ = (buf, Just (usbDeserialize (buf .<<+ 0)))

holdPID ::
  (KnownDomain dom, HiddenClockResetEnable dom) =>
  Signal dom (Maybe Bit) ->
  Signal dom (Maybe PID)
holdPID inp =
  bufferFirst $ baseCrc $ mealy (\() _ -> deepErrorX "QQ") () inp

usb ::
  forall dom .
  (KnownDomain dom, HiddenClockResetEnable dom) =>
  Signal dom Bool
usb = goodPid <$> holdPID (deepErrorX "QQ")
 where
  goodPid (Just In) = True
  goodPid _ = False

{-# OPAQUE usb #-}

topEntity ::
  "CLK" ::: Clock System ->
  "BTN" ::: Reset System ->
  "o" ::: Signal System Bool
topEntity clk rst =
  withClockResetEnable clk rst enableGen $ usb

$(makeTopEntity 'topEntity)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions