-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathulx3s.py
59 lines (47 loc) · 1.84 KB
/
ulx3s.py
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
import argparse
import subprocess
import sys
from amaranth import *
from amaranth_boards.ulx3s import *
from soc import SoC
if __name__ == "__main__":
variants = {
'12F': ULX3S_12F_Platform,
'25F': ULX3S_25F_Platform,
'45F': ULX3S_45F_Platform,
'85F': ULX3S_85F_Platform
}
# Figure out which FPGA variant we want to target...
parser = argparse.ArgumentParser()
parser.add_argument('variant', choices=variants.keys())
parser.add_argument('program')
args = parser.parse_args()
# Select the correct platform
platform = variants[args.variant]()
# Assemble the program and extract the hex representation
subprocess.run(["riscv64-elf-as", "-c", "programs/%s.s" % args.program, "-o", "programs/a.out"])
subprocess.run(["riscv64-elf-objdump", "-d", "programs/a.out"])
subprocess.run(["riscv64-elf-objcopy", "-O", "binary", "programs/a.out", "programs/hex.bin"])
# Fill the data list with the instructions (as hex)
data = []
with open("programs/hex.bin", "rb") as f:
bindata = f.read()
for i in range(len(bindata) // 4):
w = bindata[4*i : 4*i+4]
data.append(int("%02x%02x%02x%02x" % (w[3], w[2], w[1], w[0]), 16))
# Build the SoC
top = Module()
top.submodules.soc = soc = SoC(imem_init=data)
# Connect the low 8-bit outputs of the GPIO
# to the LEDs on the ULX3S.
leds = [platform.request("led", 0),
platform.request("led", 1),
platform.request("led", 2),
platform.request("led", 3),
platform.request("led", 4),
platform.request("led", 5),
platform.request("led", 6),
platform.request("led", 7)]
for i in range(len(leds)):
top.d.comb += leds[i].eq(soc.o_gpio[i])
platform.build(top, do_program=True, nextpnr_opts="--timing-allow-fail")