-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Bayeux.RgbCounter | ||
( prog | ||
, compile | ||
) where | ||
|
||
import Bayeux.Rtlil | ||
import Control.Monad.Writer | ||
|
||
class Monad m => MonadRgb m where | ||
ctr :: m SigSpec | ||
at :: SigSpec -> Integer -> m SigSpec | ||
rgb :: SigSpec -> SigSpec -> SigSpec -> m (SigSpec, SigSpec, SigSpec) | ||
|
||
prog :: MonadRgb m => m (SigSpec, SigSpec, SigSpec) | ||
prog = do | ||
c <- ctr | ||
r <- c `at` 24 | ||
g <- c `at` 23 | ||
b <- c `at` 22 | ||
rgb r g b | ||
|
||
newtype Rgb a = Rgb{ unRgb :: Writer [ModuleBody] a } | ||
deriving (Functor, Applicative, Monad, MonadWriter [ModuleBody]) | ||
|
||
instance MonadRgb Rgb where | ||
ctr = do | ||
tell $ [ModuleBodyWire $ Wire [] $ WireStmt [WireOptionInput 1] "\\clk"] <> counter 32 "$my_counter" "\\unused" | ||
return $ SigSpecWireId "\\$my_counter" | ||
|
||
at sigSpec ix = do | ||
tell | ||
[ ModuleBodyWire $ Wire [] $ WireStmt [WireOptionWidth 1] n | ||
, ModuleBodyConnStmt $ ConnStmt (SigSpecWireId n) (SigSpecSlice sigSpec ix Nothing) | ||
] | ||
return $ SigSpecWireId n | ||
where | ||
n | ix == 24 = "\\pwm_r" | ||
| ix == 23 = "\\pwm_g" | ||
| otherwise = "\\pwm_b" | ||
|
||
rgb r g b = do | ||
Check warning on line 44 in src/Bayeux/RgbCounter.hs
|
||
tell [ ModuleBodyWire $ Wire [] $ WireStmt [WireOptionOutput 2] "\\red" | ||
, ModuleBodyWire $ Wire [] $ WireStmt [WireOptionOutput 3] "\\green" | ||
, ModuleBodyWire $ Wire [] $ WireStmt [WireOptionOutput 4] "\\blue" | ||
, ModuleBodyCell sbRgbaDrv | ||
] | ||
return ( SigSpecWireId "\\red" | ||
, SigSpecWireId "\\green" | ||
, SigSpecWireId "\\blue" | ||
) | ||
|
||
compile :: Rgb (SigSpec, SigSpec, SigSpec) -> File | ||
compile = top . execWriter . unRgb | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
module Main (main) where | ||
|
||
import qualified Test.Bayeux.Lp | ||
import qualified Test.Bayeux.RgbCounter | ||
import qualified Test.Bayeux.Rtlil | ||
import Test.Tasty | ||
|
||
main :: IO () | ||
main = defaultMain $ testGroup "Test.Bayeux" | ||
[ testGroup "Lp" Test.Bayeux.Lp.tests | ||
, testGroup "Rtlil" Test.Bayeux.Rtlil.tests | ||
[ testGroup "Lp" Test.Bayeux.Lp.tests | ||
, testGroup "RgbCounter" Test.Bayeux.RgbCounter.tests | ||
, testGroup "Rtlil" Test.Bayeux.Rtlil.tests | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module Test.Bayeux.RgbCounter | ||
( tests | ||
) where | ||
|
||
import Test.Tasty | ||
import Bayeux.RgbCounter | ||
import Bayeux.Rtlil | ||
import Data.String | ||
import Data.Text (Text) | ||
import qualified Data.Text as T | ||
import qualified Data.Text.IO as TIO | ||
import Prettyprinter | ||
import Prettyprinter.Render.Text | ||
import System.Exit | ||
import System.FilePath | ||
import System.IO.Extra | ||
import System.Process | ||
import Test.Tasty | ||
Check warning on line 18 in test/Test/Bayeux/RgbCounter.hs
|
||
import Test.Tasty.HUnit | ||
import Test.Tasty.Golden | ||
|
||
|
||
tests :: [TestTree] | ||
tests = | ||
[ testGroup "pretty" | ||
[ prettyTest "rgbcounter" $ compile prog | ||
] | ||
, testGroup "synth" | ||
[ synthTest "rgbcounter" $ compile prog | ||
] | ||
] | ||
|
||
prettyTest :: Pretty a => TestName -> a -> TestTree | ||
prettyTest n = goldenVsString n (curDir </> n <.> "pretty") | ||
. return . fromString . T.unpack . render . pretty | ||
|
||
synthTest :: TestName -> File -> TestTree | ||
synthTest n rtl = testCase n $ withTempFile $ \t -> do | ||
TIO.writeFile t $ render $ pretty rtl | ||
let c = "yosys -q -p \"synth_ice40\" -f rtlil " <> t | ||
(ExitSuccess @=?) =<< waitForProcess =<< spawnCommand c | ||
|
||
curDir :: FilePath | ||
curDir = "test" </> "Test" </> "Bayeux" </> "RgbCounter" </> "golden" | ||
|
||
render :: Doc ann -> Text | ||
render = renderStrict . layoutSmart defaultLayoutOptions |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
autoidx 0 | ||
|
||
module \top | ||
|
||
wire input 1 \clk | ||
|
||
wire width 32 \$my_counter | ||
|
||
wire width 32 \unused | ||
|
||
cell $add $my_counter | ||
|
||
parameter \A_SIGNED 0 | ||
parameter \A_WIDTH 32 | ||
parameter \B_SIGNED 0 | ||
parameter \B_WIDTH 32 | ||
parameter \Y_WIDTH 32 | ||
connect \A \$my_counter | ||
connect \B 1 | ||
connect \Y \unused | ||
|
||
end | ||
|
||
process $procStmt | ||
|
||
|
||
|
||
|
||
sync posedge \clk | ||
|
||
update \$my_counter \unused | ||
|
||
end | ||
|
||
wire width 1 \pwm_r | ||
|
||
connect \pwm_r \$my_counter [24] | ||
|
||
wire width 1 \pwm_g | ||
|
||
connect \pwm_g \$my_counter [23] | ||
|
||
wire width 1 \pwm_b | ||
|
||
connect \pwm_b \$my_counter [22] | ||
|
||
wire output 2 \red | ||
|
||
wire output 3 \green | ||
|
||
wire output 4 \blue | ||
|
||
attribute \module_not_derived 1 | ||
cell \SB_RGBA_DRV \RGBA_DRIVER | ||
|
||
parameter \CURRENT_MODE "0b1" | ||
parameter \RGB0_CURRENT "0b111111" | ||
parameter \RGB1_CURRENT "0b111111" | ||
parameter \RGB2_CURRENT "0b111111" | ||
connect \CURREN 1'1 | ||
connect \RGB0 \red | ||
connect \RGB0PWM \pwm_r | ||
connect \RGB1 \green | ||
connect \RGB1PWM \pwm_g | ||
connect \RGB2 \blue | ||
connect \RGB2PWM \pwm_b | ||
connect \RGBLEDEN 1'1 | ||
|
||
end | ||
|
||
end |