blarney-lang / blarney

Haskell library for hardware description
Other
98 stars 11 forks source link

Have `when` work also not within `always` #11

Open gameboo opened 5 years ago

gameboo commented 5 years ago

I think it would be nice to have when work as a when :: Bit 1 -> Action () -> Module () on top of just when :: Bit 1 -> Action () -> Action (). I'd like to write something like

top = do
  cycleCount :: Reg (Bit 8) <- makeReg 0
  -- Increment cycleCount on every cycle
  always do
    cycleCount <== cycleCount.val + 1
  -- Terminate simulation when count reaches 20
  when (cycleCount.val .==. 20) do
    display "Finished"
    finish

instead of

top = do
  cycleCount :: Reg (Bit 8) <- makeReg 0
  always do
    -- Increment cycleCount on every cycle
    cycleCount <== cycleCount.val + 1
    -- Terminate simulation when count reaches 20
    when (cycleCount.val .==. 20) do
      display "Finished"
      finish

or

top = do
  cycleCount :: Reg (Bit 8) <- makeReg 0
  -- Increment cycleCount on every cycle
  always do
    cycleCount <== cycleCount.val + 1
  -- Terminate simulation when count reaches 20
  always do
    when (cycleCount.val .==. 20) do
      display "Finished"
      finish
mn416 commented 5 years ago

Yeah, library seems happy if we introduce

class When m where
  when :: Bit 1 -> Action () -> m ()

with instances for Module and Action.

Just wondering if there are any annoying cases when the type is no longer inferred. The other option is to have a new function, e.g. on, or whenever.

gameboo commented 5 years ago

I think I prefer the same name but I don't know how likely it would be for the type not to be inferred correctly... <troll>if only Action and Module where the same</troll>