module Language.Copilot.Examples.Examples where
import Data.Word
import Prelude (($))
import qualified Prelude as Prelude
import qualified Prelude as P
import Data.Map (fromList)
import Language.Copilot
fib :: Streams
fib = do
let f = varW64 "f"
t = varB "t"
f .= [0,1] ++ f + (drop 1 f)
t .= even f
where even :: Spec Word64 -> Spec Bool
even w' = w' `mod` 2 == 0
t1 :: Streams
t1 = do
let x = varI32 "x"
y = varB "y"
z = varB "z"
w = varB "w"
x .= [0, 1, 2] ++ x (drop 1 x)
y .= [True, False] ++ y ^ z
z .= x <= drop 1 x
w .= 3 == x
t3 :: Streams
t3 = do
let a = varW32 "a"
b = varB "b"
ext8 = extW32 "ext"
ext1 = extW32 "ext"
a .= [0,1] ++ a + ext8 + ext8 + ext1
b .= [True, False] ++ 2 + a < 5 + ext1
t4 :: Streams
t4 = do
let a = varB "a"
b = varB "b"
a .= [True,False] ++ not a
b .= drop 1 a
t5 :: Streams
t5 = do
let x = varW16 "x"
y = varB "y"
z = varB "z"
x .= [0] ++ x + 1
y .= [True, True] ++ y
z .= [False] ++ not z
trigger y "y_trigger" void
trigger z "z0_trigger" x
trigger z "z1_trigger" (y <> constW16 3 <> x)
yy :: Streams
yy = do
let a = varW32 "a"
a .= [3,5] ++ a + 1
maj1 :: Streams
maj1 = do
let v0 = varW32 "v0"
ans2 = varW32 "ans2"
v0 .= [3,7] ++ v0 + 1
ans2 .= v0
zz :: Streams
zz = do
let a = varW32 "a"
b = varW32 "b"
a .= a + 1
b .= drop 3 a
xx :: Streams
xx = do
let a = varW32 "a"
b = varW32 "b"
c = varW32 "c"
e = extW32 "ext"
d = varB "d"
f = extW32 (fun "fun1" void)
h = extArrW16 (fun "g" b) a
g = extW16 (fun "fun2" (true <> b <> constW16 3))
w = varB "w"
a .= e + f
b .= [3] ++ a
c .= [0, 1, 3, 4] ++ drop 1 b
d .= h < g
trigger d "y_trigger" void
trigger w "z0_trigger" (a <> b <> true)
trigger w "z1_trigger" (d <> constW16 3 <> w)
distrib :: Streams
distrib = do
let a = varW8 "a"
b = varB "b"
a .= [0,1] ++ a + 1
b .= mod a 2 == 0
trigger true "portA" a
trigger true "portB" b
monitor :: Streams
monitor = do
let word = extW32 "rx"
arbiter = extB "arbiter"
words = varW32 "words"
arbiters = varB "arbiters"
words .= [0] ++ mux arbiter word words
arbiters .= [False] ++ mux arbiter false arbiter
gcd :: Word16 -> Word16 -> Streams
gcd n0 n1 = do
let a = varW16 "a"
b = varW16 "b"
a .= alg n0 a b
b .= alg n1 b a
let ans = varB "ans"
ans .= a == b
where alg x0 x1 x2 = [x0] ++ mux (x1 > x2) (x1 x2) x1
gcd' :: Streams
gcd' = do
let n = extW16 "n"
m = extW16 "m"
a = varW16 "a"
b = varW16 "b"
init = varB "init"
ans = varB "ans"
a .= alg n (sub a b) init
b .= alg m (sub b a) init
ans .= a == b && not init
init .= [True] ++ false
where sub hi lo = mux (hi > lo) (hi lo) hi
alg ext ex init = [0] ++ mux init ext ex
testCoercions :: Streams
testCoercions = do
let word = varW8 "word"
int = varI16 "int"
word .= [1] ++ word * (2)
int .= 1 + cast word
testCoercions2 :: Streams
testCoercions2 = do
let b = varB "b"
i = varI16 "i"
j = varI16 "j"
b .= [True] ++ not b
i .= cast j
j .= 3
testCoercions3 :: Streams
testCoercions3 = do
let x = varB "x"
y = varI32 "y"
z = varI32 "z"
x .= [True, True] ++ not x
y .= cast x + cast x
z .= cast (not true)
i8 :: Streams
i8 = let v = varI8 "v" in v .= [0, 1] ++ v + 1
trap :: Streams
trap = do
let target = varW32 "target"
target .= [0] ++ target + 1
let x = varW32 "x"
let y = varW32 "y"
x .= [0,0] ++ y + target
y .= [0,0] ++ x + target
testArr :: Streams
testArr = do
let e = varW16 "e"
e .= [6,7,8] ++ 3
let g = varB "g"
let gg = extArrW16 "gg" e
g .= gg < 13
t99 :: Streams
t99 = do
let ext = extW32 "ext"
a = varW32 "a"
a .= [0,1] ++ a + ext + ext + ext
let b = varB "b"
b .= [True, False] ++ 2 + a < 5 + ext
t11 :: Streams
t11 = do
let v = varB "v"
v .= [False, True, False] ++ v
tdiv :: Streams
tdiv = do
let a = varW16 "a"
b = varW16 "b"
a .= [3] ++ a `div` 5
b .= [3] ++ a `mod` 5
extT :: Streams
extT = do
let x = extW16 "x"
let y = varW16 "y"
y .= x
interpretExtT :: Prelude.IO ()
interpretExtT =
interpret extT 10 $
setE (emptySM {w16Map = fromList [("x", [8,9..])]})
baseOpts
compileExtT :: P.IO ()
compileExtT =
compile extT "extT" $ setCode (P.Just "#include \"mylib.h\"", P.Nothing) baseOpts
extT2 :: Streams
extT2 = do
let y = varW16 "y"
x = extW16 (fun "f" y)
y .= x
extT3 :: Streams
extT3 = do
let y = varW16 "y"
x = extW16 (fun "f" y)
y .= [3] ++ x
extT4 :: Streams
extT4 = do
let y = varW16 "y"
x = extArrW16 "arr" y
z = extW16 "z"
w = varW16 "w"
y .= z
w .= x
extT5 :: Streams
extT5 = do
let y = varW16 "y"
x = extW16 (fun "f" y)
z = extW16 "z"
w = varW16 "w"
y .= z
w .= x
extT6 :: Streams
extT6 = do
let y = varW16 "y"
x = extArrW16 "arr" y
z = extW16 "z"
w = varW16 "w"
y .= [7] ++ z
w .= x