/
bmstack.go
228 lines (200 loc) · 5.64 KB
/
bmstack.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package bmstack
import (
"bytes"
"encoding/json"
"os"
"sort"
"strconv"
"text/template"
)
type Push struct {
Agent string // The name of the agent that is pushing
Tick uint64 // The tick at which the push occurs
Value string // The value that is pushed
}
type Pop struct {
Agent string // The name of the agent that is popping
Tick uint64 // The tick at which the pop occurs
}
type TestBenchData struct {
Pops []Pop // List of pops
Pushes []Push // List of pushes
TestSequence []string // Pushes and pops in order
}
type BmStack struct {
ModuleName string // The name of the module
DataSize int // The size of the data bus
Depth int // The depth of the stack
Senders []string // The names of the agents that can send data to the stack
Receivers []string // The names of the agents that can receive data from the stack
MemType string // "LIFO" for a stack or "FIFO" for a queue
funcMap template.FuncMap
// TestBench data
TestBenchData
}
func CreateBasicStack() *BmStack {
result := new(BmStack)
funcMap := template.FuncMap{
"inc": func(i int) int {
return i + 1
},
"dec": func(i int) int {
return i - 1
},
"next": func(i int, max int) int {
if i < max-1 {
return i + 1
} else {
return 0
}
},
"bits": func(i int) int {
return NeededBits(i)
},
}
result.funcMap = funcMap
// Default values
result.ModuleName = "bmstack"
return result
}
func (s *BmStack) WriteHDL() (string, error) {
var f bytes.Buffer
t, err := template.New("stack").Funcs(s.funcMap).Parse(stack)
if err != nil {
return "", err
}
err = t.Execute(&f, *s)
if err != nil {
return "", err
}
return f.String(), nil
}
func (s *BmStack) WriteTestBench() (string, error) {
var f bytes.Buffer
// Sort the pushes and pops by tick
sort.Slice(s.Pushes, func(i, j int) bool {
return s.Pushes[i].Tick < s.Pushes[j].Tick
})
sort.Slice(s.Pops, func(i, j int) bool {
return s.Pops[i].Tick < s.Pops[j].Tick
})
s.TestSequence = make([]string, 0)
var subSeq []string
var absTick uint64
var oldAbsTick uint64
for i, j := 0, 0; i < len(s.Pushes) || j < len(s.Pops); {
if i == len(s.Pushes) {
absTick = s.Pops[j].Tick
if absTick != oldAbsTick {
relTick := absTick - oldAbsTick
s.TestSequence = append(s.TestSequence, subSeq...)
s.TestSequence = append(s.TestSequence, "")
s.TestSequence = append(s.TestSequence, "#"+strconv.Itoa(int(relTick))+";")
subSeq = make([]string, 0)
oldAbsTick += relTick
}
agent := s.Pops[j].Agent
tick := strconv.Itoa(int(absTick))
subSeq = append(subSeq, "// Pop agent "+agent+" at tick "+tick)
subSeq = append(subSeq, agent+"Impulse=1;")
subSeq = append(subSeq, "#2;")
subSeq = append(subSeq, agent+"Impulse=0;")
j++
} else if j == len(s.Pops) {
absTick = s.Pushes[i].Tick
if absTick != oldAbsTick {
relTick := absTick - oldAbsTick
s.TestSequence = append(s.TestSequence, subSeq...)
s.TestSequence = append(s.TestSequence, "")
s.TestSequence = append(s.TestSequence, "#"+strconv.Itoa(int(relTick))+";")
subSeq = make([]string, 0)
oldAbsTick += relTick
}
agent := s.Pushes[i].Agent
value := s.Pushes[i].Value
tick := strconv.Itoa(int(absTick))
subSeq = append(subSeq, "// Push agent "+agent+" at tick "+tick+" with value "+value)
subSeq = append(subSeq, agent+"Data="+value+";")
subSeq = append(subSeq, agent+"Impulse=1;")
subSeq = append(subSeq, "#2;")
subSeq = append(subSeq, agent+"Impulse=0;")
i++
} else {
if s.Pushes[i].Tick <= s.Pops[j].Tick {
absTick = s.Pushes[i].Tick
if absTick != oldAbsTick {
relTick := absTick - oldAbsTick
s.TestSequence = append(s.TestSequence, subSeq...)
s.TestSequence = append(s.TestSequence, "")
s.TestSequence = append(s.TestSequence, "#"+strconv.Itoa(int(relTick))+";")
subSeq = make([]string, 0)
oldAbsTick += relTick
}
agent := s.Pushes[i].Agent
value := s.Pushes[i].Value
tick := strconv.Itoa(int(absTick))
subSeq = append(subSeq, "// Push agent "+agent+" at tick "+tick+" with value "+value)
subSeq = append(subSeq, agent+"Data="+value+";")
subSeq = append(subSeq, agent+"Impulse=1;")
subSeq = append(subSeq, "#2;")
subSeq = append(subSeq, agent+"Impulse=0;")
i++
} else {
absTick = s.Pops[j].Tick
if absTick != oldAbsTick {
relTick := absTick - oldAbsTick
s.TestSequence = append(s.TestSequence, subSeq...)
s.TestSequence = append(s.TestSequence, "")
s.TestSequence = append(s.TestSequence, "#"+strconv.Itoa(int(relTick))+";")
subSeq = make([]string, 0)
oldAbsTick += relTick
}
agent := s.Pops[j].Agent
tick := strconv.Itoa(int(absTick))
subSeq = append(subSeq, "// Pop agent "+agent+" at tick "+tick)
subSeq = append(subSeq, agent+"Impulse=1;")
subSeq = append(subSeq, "#2;")
subSeq = append(subSeq, agent+"Impulse=0;")
j++
}
}
}
s.TestSequence = append(s.TestSequence, subSeq...)
t, err := template.New("testbench").Funcs(s.funcMap).Parse(testbench)
if err != nil {
return "", err
}
err = t.Execute(&f, *s)
if err != nil {
return "", err
}
return f.String(), nil
}
func NeededBits(num int) int {
if num > 0 {
for bits := 1; true; bits++ {
if 1<<uint8(bits) >= num {
return bits
}
}
}
return 0
}
func (td *TestBenchData) SaveJSON(filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
enc := json.NewEncoder(f)
return enc.Encode(td)
}
func (td *TestBenchData) LoadJSON(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
dec := json.NewDecoder(f)
return dec.Decode(td)
}