2 from shutil
import which
6 from litex
.soc
.interconnect
.csr
import CSRStatus
9 # select between clang and gcc
10 clang
= os
.getenv("CLANG", "")
12 clang
= bool(int(clang
))
15 if not hasattr(cpu
, "clang_triple"):
17 raise ValueError(cpu
.name
+ "not supported with clang.")
21 # Default to clang unless told otherwise
24 assert isinstance(clang
, bool)
26 triple
= cpu
.clang_triple
27 flags
= cpu
.clang_flags
29 triple
= cpu
.gcc_triple
32 # select triple when more than one
33 def select_triple(triple
):
35 if not isinstance(triple
, tuple):
37 for i
in range(len(triple
)):
43 msg
= "Unable to find any of the cross compilation toolchains:\n"
44 for i
in range(len(triple
)):
45 msg
+= "- " + triple
[i
] + "\n"
51 ("TRIPLE", select_triple(triple
)),
54 ("CPUENDIANNESS", cpu
.endianness
),
55 ("CLANG", str(int(clang
)))
59 def get_linker_output_format(cpu
):
60 return "OUTPUT_FORMAT(\"" + cpu
.linker_output_format
+ "\")\n"
63 def get_linker_regions(regions
):
65 for name
, origin
, length
in regions
:
66 r
+= "\t{} : ORIGIN = 0x{:08x}, LENGTH = 0x{:08x}\n".format(name
, origin
, length
)
71 def get_mem_header(regions
, flash_boot_address
):
72 r
= "#ifndef __GENERATED_MEM_H\n#define __GENERATED_MEM_H\n\n"
73 for name
, base
, size
in regions
:
74 r
+= "#define {name}_BASE 0x{base:08x}\n#define {name}_SIZE 0x{size:08x}\n\n".format(name
=name
.upper(), base
=base
, size
=size
)
75 if flash_boot_address
is not None:
76 r
+= "#define FLASH_BOOT_ADDRESS 0x{:08x}\n\n".format(flash_boot_address
)
81 def _get_rw_functions_c(reg_name
, reg_base
, nwords
, busword
, read_only
, with_access_functions
):
84 r
+= "#define CSR_"+reg_name
.upper()+"_ADDR "+hex(reg_base
)+"\n"
85 r
+= "#define CSR_"+reg_name
.upper()+"_SIZE "+str(nwords
)+"\n"
91 ctype
= "unsigned long long int"
93 ctype
= "unsigned int"
95 ctype
= "unsigned short int"
97 ctype
= "unsigned char"
99 if with_access_functions
:
100 r
+= "static inline "+ctype
+" "+reg_name
+"_read(void) {\n"
102 r
+= "\t"+ctype
+" r = csr_readl("+hex(reg_base
)+");\n"
103 for byte
in range(1, nwords
):
104 r
+= "\tr <<= "+str(busword
)+";\n\tr |= csr_readl("+hex(reg_base
+4*byte
)+");\n"
105 r
+= "\treturn r;\n}\n"
107 r
+= "\treturn csr_readl("+hex(reg_base
)+");\n}\n"
110 r
+= "static inline void "+reg_name
+"_write("+ctype
+" value) {\n"
111 for word
in range(nwords
):
112 shift
= (nwords
-word
-1)*busword
114 value_shifted
= "value >> "+str(shift
)
116 value_shifted
= "value"
117 r
+= "\tcsr_writel("+value_shifted
+", "+hex(reg_base
+4*word
)+");\n"
122 def get_csr_header(regions
, constants
, with_access_functions
=True, with_shadow_base
=True, shadow_base
=0x80000000):
123 r
= "#ifndef __GENERATED_CSR_H\n#define __GENERATED_CSR_H\n"
124 if with_access_functions
:
125 r
+= "#include <stdint.h>\n"
126 r
+= "#ifdef CSR_ACCESSORS_DEFINED\n"
127 r
+= "extern void csr_writeb(uint8_t value, uint32_t addr);\n"
128 r
+= "extern uint8_t csr_readb(uint32_t addr);\n"
129 r
+= "extern void csr_writew(uint16_t value, uint32_t addr);\n"
130 r
+= "extern uint16_t csr_readw(uint32_t addr);\n"
131 r
+= "extern void csr_writel(uint32_t value, uint32_t addr);\n"
132 r
+= "extern uint32_t csr_readl(uint32_t addr);\n"
133 r
+= "#else /* ! CSR_ACCESSORS_DEFINED */\n"
134 r
+= "#include <hw/common.h>\n"
135 r
+= "#endif /* ! CSR_ACCESSORS_DEFINED */\n"
136 for name
, origin
, busword
, obj
in regions
:
137 if not with_shadow_base
:
138 origin
&= (~shadow_base
)
139 if isinstance(obj
, Memory
):
140 r
+= "\n/* "+name
+" */\n"
141 r
+= "#define CSR_"+name
.upper()+"_BASE "+hex(origin
)+"\n"
143 r
+= "\n/* "+name
+" */\n"
144 r
+= "#define CSR_"+name
.upper()+"_BASE "+hex(origin
)+"\n"
146 nr
= (csr
.size
+ busword
- 1)//busword
147 r
+= _get_rw_functions_c(name
+ "_" + csr
.name
, origin
, nr
, busword
, isinstance(csr
, CSRStatus
), with_access_functions
)
150 r
+= "\n/* constants */\n"
151 for name
, value
in constants
:
153 r
+= "#define "+name
+"\n"
155 if isinstance(value
, str):
156 value
= "\"" + value
+ "\""
157 ctype
= "const char *"
161 r
+= "#define "+name
+" "+value
+"\n"
162 if with_access_functions
:
163 r
+= "static inline "+ctype
+" "+name
.lower()+"_read(void) {\n"
164 r
+= "\treturn "+value
+";\n}\n"
170 def get_csr_csv(csr_regions
=None, constants
=None, memory_regions
=None):
173 if csr_regions
is not None:
174 for name
, origin
, busword
, obj
in csr_regions
:
175 r
+= "csr_base,{},0x{:08x},,\n".format(name
, origin
)
177 for name
, origin
, busword
, obj
in csr_regions
:
178 if not isinstance(obj
, Memory
):
180 nr
= (csr
.size
+ busword
- 1)//busword
181 r
+= "csr_register,{}_{},0x{:08x},{},{}\n".format(name
, csr
.name
, origin
, nr
, "ro" if isinstance(csr
, CSRStatus
) else "rw")
184 if constants
is not None:
185 for name
, value
in constants
:
186 r
+= "constant,{},{},,\n".format(name
.lower(), value
)
188 if memory_regions
is not None:
189 for name
, origin
, length
in memory_regions
:
190 r
+= "memory_region,{},0x{:08x},{:d},\n".format(name
.lower(), origin
, length
)