Advertisement
Sidsh

Haskell blinker.

Jun 26th, 2022
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# OPTIONS_GHC -fno-warn-orphans #-}
  2. module Blinker where
  3.  
  4. import Clash.Prelude
  5. import Clash.Intel.ClockGen
  6. import Clash.Annotations.SynthesisAttributes
  7. createDomain vSystem{vName="Input", vPeriod=20000}
  8. createDomain vSystem{vName="Dom20MHz", vPeriod=50000}
  9.  
  10. topEntity ::
  11.   Clock Input
  12.     `Annotate` 'StringAttr "chip_pin" "R8"
  13.    `Annotate` 'StringAttr
  14.                 "altera_attribute" "-name IO_STANDARD \"3.3-V LVTTL\"" ->
  15.   Signal Input Bool
  16.     `Annotate` 'StringAttr "chip_pin" "J15"
  17.    `Annotate` 'StringAttr
  18.                 "altera_attribute" "-name IO_STANDARD \"3.3-V LVTTL\"" ->
  19.   Signal Dom20MHz Bit
  20.     `Annotate` 'StringAttr "chip_pin" "E1"
  21.    `Annotate` 'StringAttr
  22.                 "altera_attribute" "-name IO_STANDARD \"3.3-V LVTTL\"" ->
  23.   Signal Dom20MHz (BitVector 8)
  24.     `Annotate` 'StringAttr
  25.                "chip_pin" "L3, B1, F3, D1, A11, B13, A13, A15"
  26.    `Annotate` 'StringAttr
  27.                 "altera_attribute" "-name IO_STANDARD \"3.3-V LVTTL\""
  28. topEntity clk50Mhz rstBtn modeBtn =
  29.   exposeClockResetEnable
  30.     (mealy blinkerT initialStateBlinkerT . isRising 1)
  31.     clk20Mhz
  32.     rstSync
  33.     en
  34.     modeBtn
  35.   where
  36.   en = enableGen
  37.   initialStateBlinkerT = (1, Rotate, 0)
  38.   rst = unsafeFromLowPolarity rstBtn
  39.   (clk20Mhz, pllStable) =
  40.     altpll
  41.       @Dom20MHz
  42.       (SSymbol @"altpll20")
  43.       clk50Mhz
  44.       rst
  45.   rstSync =
  46.     resetSynchronizer
  47.       clk20Mhz
  48.       (unsafeFromLowPolarity pllStable)
  49. data LedMode
  50.   = Rotate
  51.   | Complement
  52.   deriving (Generic, NFDataX)
  53.  
  54. flipMode :: LedMode -> LedMode
  55. flipMode Rotate = Complement
  56. flipMode Complement = Rotate
  57.  
  58. -- Finally, the actual behavior of the circuit
  59. blinkerT ::
  60.   (BitVector 8, LedMode, Index 6660000) ->
  61.   Bool ->
  62.   ((BitVector 8, LedMode, Index 6660000), BitVector 8)
  63. blinkerT (leds, mode, cntr) key1R = ((ledsN, modeN, cntrN), leds)
  64.   where
  65.     -- clock frequency = 20e6  (20 MHz)
  66.     -- led update rate = 333e-3 (every 333ms)
  67.     cnt_max = maxBound :: Index 6660000 -- 20e6 * 333e-3
  68.  
  69.     cntrN | cntr == cnt_max = 0
  70.           | otherwise       = cntr + 1
  71.  
  72.     modeN | key1R     = flipMode mode
  73.           | otherwise = mode
  74.  
  75.     ledsN | cntr == 0 =
  76.               case mode of
  77.                 Rotate -> rotateL leds 1
  78.                 Complement -> complement leds
  79.           | otherwise = leds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement