-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake.py
More file actions
executable file
·278 lines (230 loc) · 6.24 KB
/
make.py
File metadata and controls
executable file
·278 lines (230 loc) · 6.24 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/python3
import argparse
import os
import subprocess
import sys
# Runs a subcommand with the appropriate pipes
# (a wrapper for use with tqdm)
def run_subcommand(args, env={}):
# A simple hack to add new env variables
# on top of the old ones
for i, k in env.items():
os.environ[i] = k
rv = subprocess.run(args=args, check=True)
print(rv.args)
# Removes generated files after build
def clean():
try:
os.remove("stepper.json")
os.remove("ulx3s_out.config")
os.remove("ulx3s.bit")
except FileNotFoundError:
print("Some files where already deleted or are missing!")
def fill_0s(val):
for _ in range(4 - (len(val) % 4)):
val.append(0)
def reverse_byte_order(non):
output = bytearray()
fill_0s(non)
for i in range(0, len(non), 4):
output.append(non[i + 3])
output.append(non[i + 2])
output.append(non[i + 1])
output.append(non[i])
fill_0s(output)
return output
def compile_firmware():
wd = os.getcwd()
os.chdir("firmware")
# Build the firmware
run_subcommand(
["cargo", "build", "--release"],
)
# Copy sections to bin for use with rom initialisation
run_subcommand(
[
"cargo",
"objcopy",
"--release",
"--",
"-O",
"binary",
"stepper.bin",
],
)
# Format output to work with readmemh
counter = 1
with open("stepper.mem", "w") as firm_out:
with open("stepper.bin", "rb") as firm_in:
firm = bytearray(firm_in.read())
for i in reverse_byte_order(firm):
firm_out.write("{:02x}".format(i))
if counter % 4 == 0:
firm_out.write("\n")
counter += 1
os.chdir(wd)
# Builds the bitstream which can be loaded onto the FPGA
def build():
# Generate the intermediate netlist as a .json
run_subcommand(
[
"yosys",
"-s",
"synth_flow.txt",
],
)
# Route the netlist in the specific FPGA
run_subcommand(
[
"nextpnr-ecp5",
"--12k",
"--package",
"CABGA381",
"--json",
"stepper.json",
"--lpf",
"ulx3s_v20.lpf",
"--speed",
"6",
"--freq",
"25",
"--textcfg",
"ulx3s_out.config",
],
)
# Generates the bitstream using the packed netlist
run_subcommand(
[
"ecppack",
"ulx3s_out.config",
"ulx3s.bit",
"--idcode",
"0x21111043",
],
)
# Runs one test with the name test_name which is in the tests
# directory
def test(test_name):
wd = os.getcwd()
os.chdir("tests")
# Test commands are run inside the tests folder!!!
src_dir = "../src/"
output_dir = "sim_output/"
# Generates a temporary hacky verilog file for simulation variables
with open(output_dir + "iverilog_dump.v", "w", encoding="utf-8") as dump:
dump.write(
"`timescale 1ns / 100ps\n\n"
+ "module iverilog_dump();\n"
+ "initial begin\n"
+ '\t$dumpfile("'
+ output_dir
+ test_name
+ '.vcd");\n'
+ "\t$dumpvars();\n"
+ "end\n"
+ "endmodule\n"
)
# Gets together a list of source files
print("Current source list:")
with open(output_dir + "srclist.txt", "w", encoding="utf-8") as sources:
sources.write(output_dir + "iverilog_dump.v\n")
for i in os.listdir(src_dir):
sources.write(src_dir + i + "\n")
print(src_dir + i)
# Uses iverilog to compile all of the source files
# into an intermediate format
run_subcommand(
[
"iverilog",
"-o",
output_dir + test_name + ".vvp",
"-g",
"2005",
"-D",
"COCOTB_SIM=1",
"-s",
test_name,
"-s",
"iverilog_dump",
"-I",
src_dir,
"-v",
"-f",
output_dir + "srclist.txt",
],
)
# Run the compiled test
run_subcommand(
[
"vvp",
"-M",
# "/usr/lib/python3.10/site-packages/cocotb/libs",
"/home/fuckaduck/.local/lib/python3.10/site-packages/cocotb/libs",
"-m",
"libcocotbvpi_icarus",
output_dir + test_name + ".vvp",
],
env={
"MODULE": "test_" + test_name,
"TOPLEVEL": test_name,
"TOPLEVEL_LANG": "verilog",
"COCOTB_HDL_TIMEUNIT": "1ns",
"COCOTB_HDL_TIMEPRECISION": "1ns",
},
)
# Switch back to original working directory
os.chdir(wd)
# Load the FPGA temporarily with the generated bitstream
def load():
run_subcommand(
[
"openFPGALoader",
"-b",
"ulx3s",
"-v",
"-m",
"ulx3s.bit",
],
)
def flash():
run_subcommand(
[
"openFPGALoader",
"-b",
"ulx3s",
"-f",
"-v",
"-m",
"ulx3s.bit",
],
)
parser = argparse.ArgumentParser(description="Main makefile for the stepper project")
parser.add_argument(
"--clean", help="Removes build output", dest="clean", action="store_true"
)
parser.add_argument(
"--build", help="Build the hole project", dest="build", action="store_true"
)
parser.add_argument("--test", help="Run a test", dest="test", action="store")
parser.add_argument(
"--load", help="Load the bitstream into SRAM", dest="load", action="store_true"
)
parser.add_argument(
"--flash", help="Flash the bitstream into EEPROM", dest="flash", action="store_true"
)
parser.add_argument(
"--compile", help="Compile the firmware", dest="compile", action="store_true"
)
prog_args = parser.parse_args()
if prog_args.clean:
clean()
if prog_args.build:
build()
if prog_args.test:
test(prog_args.test)
if prog_args.load:
load()
if prog_args.flash:
flash()
if prog_args.compile:
compile_firmware()