-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample5_sim.hs
More file actions
77 lines (64 loc) · 1.82 KB
/
Example5_sim.hs
File metadata and controls
77 lines (64 loc) · 1.82 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module Example5_sim where
import CLaSH.Prelude
--
-- Configuration
--
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)
--
-- Logic
--
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
--
-- Simulation
--
instrs = [Push 3, Push 5, PopPush 6, Pop, Pop]
-- Simulation function that just stores the output
sim_o f s [] = []
sim_o f s (x:xs) = o:sim_o f s' xs
where
(s', o) = f s x
-- Simulation function that stores the internal state and the output
sim_full f s [] = []
sim_full f s (x:xs) = (s', o):sim_full f s' xs
where
(s', o) = f s x
-- Simulation function that only stores the state of the memory
sim_mem f s [] = []
sim_mem f s (x:xs) = mem:sim_mem f s' xs
where
(s'@(mem, sp), o) = f s x
-- Simulation function that only stores the state of the stack pointer
sim_sp f s [] = []
sim_sp f s (x:xs) = sp:sim_sp f s' xs
where
(s'@(_, sp), o) = f s x
-- Will contain just the output
test_o = sim_o topEntity (repeat 0 :: SMem, 0 :: SP) instrs
-- Will also contain the internal state
test_full = sim_full topEntity (repeat 0 :: SMem, 0 :: SP) instrs
-- Will only contain the memory state
test_mem = sim_mem topEntity (repeat 0 :: SMem, 0 :: SP) instrs
-- Will only contain the stack pointer state
test_sp = sim_sp topEntity (repeat 0 :: SMem, 0 :: SP) instrs