upsilon/firmware/generate_csr_locations.py

73 lines
1.7 KiB
Python
Raw Normal View History

2022-07-12 15:36:50 -04:00
#!/usr/bin/python3
import json
2023-03-16 14:53:37 -04:00
import sys
class CSRGenerator:
2023-03-20 15:07:52 -04:00
def __init__(self, json_file, registers, f):
2023-03-16 14:53:37 -04:00
self.registers = registers
2023-03-20 15:07:52 -04:00
self.j = json.load(open(json_file))
2023-03-16 14:53:37 -04:00
self.file = f
def get_reg(self, name, num):
if num is None:
regname = f"base_{name}"
else:
regname = f"base_{name}_{num}"
return f'{self.j["csr_registers"][regname]["addr"]}'
2023-03-16 14:53:37 -04:00
def print(self, *args):
2023-03-20 15:07:52 -04:00
print(*args, end='', file=self.file)
2023-03-16 14:53:37 -04:00
def print_array(self, name, num):
if num == 1:
self.print(f'static const uintptr_t {name} = {self.get_reg(name, None)};\n')
2023-03-16 14:53:37 -04:00
else:
self.print(f'static const uintptr_t {name}[{num}] = {{', self.get_reg(name, 0))
2023-04-04 12:04:06 -04:00
for i in range(1,num):
2023-03-16 14:53:37 -04:00
self.print(',', self.get_reg(name, i))
2023-03-20 15:07:52 -04:00
self.print('};\n\n')
2023-03-16 14:53:37 -04:00
def print_registers(self):
for name,num in self.registers:
self.print_array(name, num)
def print_file(self):
2023-03-20 15:07:52 -04:00
self.print(f'''#pragma once
#define ADC_MAX {adc_num}
#define DAC_MAX {dac_num}
''')
2023-03-16 14:53:37 -04:00
self.print_registers()
if __name__ == "__main__":
dac_num = 8
adc_num = 8
registers = [
("dac_sel", dac_num),
("dac_finished", dac_num),
("dac_arm", dac_num),
("from_dac", dac_num),
("to_dac", dac_num),
("wf_arm", dac_num),
("wf_halt_on_finish", dac_num),
("wf_finished", dac_num),
("wf_running", dac_num),
2023-03-16 14:53:37 -04:00
("wf_time_to_wait", dac_num),
("wf_refresh_start", dac_num),
("wf_refresh_finished", dac_num),
("wf_start_addr", dac_num),
("adc_finished", adc_num),
("adc_arm", adc_num),
("from_adc", adc_num),
("adc_sel_0", 1),
("cl_in_loop", 1),
("cl_cmd", 1),
("cl_word_in", 1),
("cl_word_out", 1),
("cl_start_cmd", 1),
("cl_finish_cmd", 1),
]
2023-03-20 15:07:52 -04:00
CSRGenerator("csr.json", registers, sys.stdout).print_file()
2022-07-12 15:36:50 -04:00