@@ -40,16 +40,17 @@ class Program: # pylint: disable=too-few-public-methods
40
40
41
41
"""
42
42
43
- def __init__ (self , text_program : str ) -> None :
43
+ def __init__ (self , text_program : str , * , build_debuginfo = False ) -> None :
44
44
"""Converts pioasm text to encoded instruction bytes"""
45
45
# pylint: disable=too-many-branches,too-many-statements,too-many-locals
46
46
assembled = []
47
47
program_name = None
48
48
labels = {}
49
+ linemap = []
49
50
instructions = []
50
51
sideset_count = 0
51
52
sideset_enable = 0
52
- for line in text_program .split ("\n " ):
53
+ for i , line in enumerate ( text_program .split ("\n " ) ):
53
54
line = line .strip ()
54
55
if not line :
55
56
continue
@@ -75,6 +76,7 @@ def __init__(self, text_program: str) -> None:
75
76
elif line :
76
77
# Only add as an instruction if the line isn't empty
77
78
instructions .append (line )
79
+ linemap .append (i )
78
80
79
81
max_delay = 2 ** (5 - sideset_count - sideset_enable ) - 1
80
82
assembled = []
@@ -219,12 +221,59 @@ def __init__(self, text_program: str) -> None:
219
221
# print(bin(assembled[-1]))
220
222
221
223
self .pio_kwargs = {
222
- "sideset_count " : sideset_count ,
224
+ "sideset_pin_count " : sideset_count ,
223
225
"sideset_enable" : sideset_enable ,
224
226
}
225
227
226
228
self .assembled = array .array ("H" , assembled )
227
229
230
+ if build_debuginfo :
231
+ self .debuginfo = (linemap , text_program )
232
+ else :
233
+ self .debuginfo = None
234
+
235
+ def print_c_program (self , name , qualifier = "const" ):
236
+ """Print the program into a C program snippet"""
237
+ if self .debuginfo is None :
238
+ linemap = None
239
+ program_lines = None
240
+ else :
241
+ linemap = self .debuginfo [0 ][:] # Use a copy since we destroy it
242
+ program_lines = self .debuginfo [1 ].split ("\n " )
243
+
244
+ print (
245
+ f"{ qualifier } int { name } _sideset_pin_count = { self .pio_kwargs ['sideset_pin_count' ]} ;"
246
+ )
247
+ print (
248
+ f"{ qualifier } bool { name } _sideset_enable = { self .pio_kwargs ['sideset_enable' ]} ;"
249
+ )
250
+ print (f"{ qualifier } uint16_t { name } [] = " + "{" )
251
+ last_line = 0
252
+ if linemap :
253
+ for inst in self .assembled :
254
+ next_line = linemap [0 ]
255
+ del linemap [0 ]
256
+ while last_line < next_line :
257
+ line = program_lines [last_line ]
258
+ if line :
259
+ print (f" // { line } " )
260
+ last_line += 1
261
+ line = program_lines [last_line ]
262
+ print (f" 0x{ inst :04x} , // { line } " )
263
+ last_line += 1
264
+ while last_line < len (program_lines ):
265
+ line = program_lines [last_line ]
266
+ if line :
267
+ print (f" // { line } " )
268
+ last_line += 1
269
+ else :
270
+ for i in range (0 , len (self .assembled ), 8 ):
271
+ print (
272
+ " " + ", " .join ("0x%04x" % i for i in self .assembled [i : i + 8 ])
273
+ )
274
+ print ("};" )
275
+ print ()
276
+
228
277
229
278
def assemble (program_text : str ) -> array .array :
230
279
"""Converts pioasm text to encoded instruction bytes
0 commit comments