-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample5.hs
More file actions
42 lines (34 loc) · 991 Bytes
/
Copy pathExample5.hs
File metadata and controls
42 lines (34 loc) · 991 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module Example5 where
import CLaSH.Prelude
type Value = Signed 16
type SizeInBits = 3
type StackDepth = 2^SizeInBits
type SP = Unsigned SizeInBits
type SMem = Vec StackDepth Value
data SInstr = Push Value
| Pop
| PopPush Value
deriving (Show)
stack5 (mem, sp) instr = ((mem', sp'), o)
where
(mem', sp') = case instr of
Push val -> (replace sp val mem, sp + 1)
Pop -> (mem, sp - 1)
PopPush val -> (replace (sp - 1) val mem, sp)
o = case instr of
Pop -> mem !! sp'
PopPush _ -> mem !! (sp - 1)
_ -> 0
topEntity
:: (SMem, SP)
-> SInstr
-> ((SMem, SP), Value)
topEntity = stack5
-- Push 3 on the stack
x = topEntity (repeat 0, 0) (Push 3)
-- Push 5 on the stack
y = topEntity (fst x) (Push 5)
-- Pop the last value off of the stack
z = topEntity (fst y) Pop
-- Pop the last value off of the stack and replace it with 42
w = topEntity (fst z) (PopPush 42)