2022-07-12 15:36:50 -04:00
|
|
|
#!/usr/bin/python3
|
|
|
|
import json
|
2023-03-16 14:53:37 -04:00
|
|
|
import sys
|
|
|
|
|
2023-04-08 12:47:59 -04:00
|
|
|
"""
|
2023-04-18 15:47:57 -04:00
|
|
|
This file uses csr.json and csr_bitwidth.json and writes functions
|
|
|
|
that handle reads and writes to MMIO.
|
2023-04-08 12:47:59 -04:00
|
|
|
"""
|
|
|
|
|
2023-03-16 14:53:37 -04:00
|
|
|
class CSRGenerator:
|
2023-04-18 15:47:57 -04:00
|
|
|
def __init__(self, csrjson, bitwidthjson, registers, outf, dacmax, adcmax):
|
2023-03-16 14:53:37 -04:00
|
|
|
self.registers = registers
|
2023-04-18 15:47:57 -04:00
|
|
|
self.csrs = json.load(open(csrjson))
|
|
|
|
self.bws = json.load(open(bitwidthjson))
|
|
|
|
self.file = outf
|
|
|
|
self.dacmax = dacmax
|
|
|
|
self.adcmax = adcmax
|
2023-03-16 14:53:37 -04:00
|
|
|
|
|
|
|
def get_reg(self, name, num):
|
|
|
|
if num is None:
|
|
|
|
regname = f"base_{name}"
|
|
|
|
else:
|
|
|
|
regname = f"base_{name}_{num}"
|
2023-04-18 15:47:57 -04:00
|
|
|
return self.csrs["csr_registers"][regname]["addr"]
|
|
|
|
|
|
|
|
def get_bitwidth_type(self, name):
|
|
|
|
b = self.bws[name]
|
|
|
|
if b <= 8:
|
|
|
|
return 8
|
|
|
|
elif b <= 16:
|
|
|
|
return 16
|
|
|
|
elif b <= 32:
|
|
|
|
return 32
|
|
|
|
elif b <= 64:
|
|
|
|
return 64
|
|
|
|
else:
|
|
|
|
raise Exception('unsupported width', b)
|
|
|
|
|
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
|
|
|
|
2023-04-18 15:47:57 -04:00
|
|
|
def print_write_fun(self, name, regnum):
|
|
|
|
typ = self.get_bitwidth_type(name)
|
|
|
|
self.print('static inline void\n')
|
|
|
|
self.print(f'write_{name}(uint{typ}_t v')
|
|
|
|
|
|
|
|
if regnum != 1:
|
|
|
|
self.print(f', int num')
|
|
|
|
self.print(')\n{\n')
|
|
|
|
|
|
|
|
if regnum != 1:
|
|
|
|
self.print('\t', f'static const uintptr_t loc[{regnum}]', '= {\n')
|
|
|
|
self.print('\t\t', self.get_reg(name,0), '\n')
|
|
|
|
for i in range(1,regnum):
|
|
|
|
self.print('\t\t,', self.get_reg(name, i), '\n')
|
|
|
|
self.print('\t};\n')
|
|
|
|
self.print('''
|
|
|
|
if (num < 0 || num >= ARRAY_SIZE(loc)) {
|
|
|
|
LOG_ERR("invalid location %d", num);
|
|
|
|
k_fatal_halt(K_ERR_KERNEL_OOPS);
|
|
|
|
}
|
|
|
|
''')
|
|
|
|
self.print('\t', f'litex_write{typ}(v, {"loc[num]" if regnum != 1 else self.get_reg(name, None)});', '\n}\n\n')
|
|
|
|
|
|
|
|
def print_read_fun(self, name, regnum):
|
|
|
|
typ = self.get_bitwidth_type(name)
|
|
|
|
self.print(f'static inline uint{typ}_t\nread_{name}')
|
|
|
|
|
|
|
|
if regnum != 1:
|
|
|
|
self.print(f'(int num)', '\n{\n')
|
2023-03-16 14:53:37 -04:00
|
|
|
else:
|
2023-04-18 15:47:57 -04:00
|
|
|
self.print('(void)\n{\n')
|
|
|
|
|
|
|
|
if regnum != 1:
|
|
|
|
self.print('\t', f'static const uintptr_t loc[{regnum}]', '= {\n')
|
|
|
|
self.print('\t\t', self.get_reg(name,0), '\n')
|
|
|
|
for i in range(1,regnum):
|
|
|
|
self.print('\t\t,', self.get_reg(name, i), '\n')
|
|
|
|
self.print('\t};\n')
|
|
|
|
self.print('''
|
|
|
|
if (num < 0 || num >= ARRAY_SIZE(loc)) {
|
|
|
|
LOG_ERR("invalid location %d", num);
|
|
|
|
k_fatal_halt(K_ERR_KERNEL_OOPS);
|
|
|
|
}
|
|
|
|
''')
|
|
|
|
self.print('\t', f'return litex_read{typ}({"loc[num]" if regnum != 1 else self.get_reg(name, None)}', ');\n}\n\n')
|
|
|
|
|
2023-03-16 14:53:37 -04:00
|
|
|
def print_file(self):
|
2023-04-18 15:47:57 -04:00
|
|
|
self.print('''
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
static inline void litex_write64(uint64_t value, unsigned long addr)
|
|
|
|
{
|
|
|
|
#if CONFIG_LITEX_CSR_DATA_WIDTH >= 32
|
|
|
|
sys_write32(value >> 32, addr);
|
|
|
|
sys_write32(value, addr + 0x4);
|
|
|
|
#else
|
|
|
|
# error Unsupported CSR data width
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-03-20 15:07:52 -04:00
|
|
|
''')
|
2023-04-18 15:47:57 -04:00
|
|
|
self.print('#define DAC_MAX', self.dacmax, '\n')
|
|
|
|
self.print('#define ADC_MAX', self.adcmax, '\n')
|
|
|
|
for reg in self.registers:
|
|
|
|
self.print_read_fun(reg[1],reg[2])
|
|
|
|
if not reg[0]: #read only
|
|
|
|
self.print_write_fun(reg[1],reg[2])
|
2023-03-16 14:53:37 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
dac_num = 8
|
|
|
|
adc_num = 8
|
|
|
|
|
|
|
|
registers = [
|
2023-04-18 15:47:57 -04:00
|
|
|
# Read-only, name, number
|
|
|
|
(False, "dac_sel", dac_num),
|
|
|
|
(True, "dac_finished", dac_num),
|
|
|
|
(False, "dac_arm", dac_num),
|
|
|
|
(True, "from_dac", dac_num),
|
|
|
|
(False, "to_dac", dac_num),
|
|
|
|
(False, "wf_arm", dac_num),
|
|
|
|
(False, "wf_halt_on_finish", dac_num),
|
|
|
|
(True, "wf_finished", dac_num),
|
|
|
|
(True, "wf_running", dac_num),
|
|
|
|
(False, "wf_time_to_wait", dac_num),
|
|
|
|
(False, "wf_refresh_start", dac_num),
|
|
|
|
(True, "wf_refresh_finished", dac_num),
|
|
|
|
(False, "wf_start_addr", dac_num),
|
|
|
|
|
|
|
|
(True, "adc_finished", adc_num),
|
|
|
|
(False, "adc_arm", adc_num),
|
|
|
|
(True, "from_adc", adc_num),
|
|
|
|
|
2023-05-16 15:02:05 -04:00
|
|
|
(False, "adc_sel", adc_num),
|
2023-04-18 15:47:57 -04:00
|
|
|
(True, "cl_in_loop", 1),
|
|
|
|
(False, "cl_cmd", 1),
|
|
|
|
(False, "cl_word_in", 1),
|
|
|
|
(False, "cl_word_out", 1),
|
|
|
|
(False, "cl_start_cmd", 1),
|
|
|
|
(True, "cl_finish_cmd", 1),
|
2023-03-16 14:53:37 -04:00
|
|
|
]
|
|
|
|
|
2023-04-18 15:47:57 -04:00
|
|
|
CSRGenerator("csr.json", "csr_bitwidth.json", registers, sys.stdout, dac_num, adc_num).print_file()
|
2022-07-12 15:36:50 -04:00
|
|
|
|