1+ import itertools
2+ from enum import Enum
13from pathlib import Path
24from typing import Annotated
35
270272 "tile-logic-display" ,
271273]
272274
275+
276+ class VariableFormat (Enum ):
277+ minimize = "minimize"
278+ decimal = "decimal"
279+ hex = "hex"
280+
281+ def iter_variables (self ):
282+ match self :
283+ case VariableFormat .minimize :
284+ for c1 in VARIABLE_NAME_CHARS :
285+ for c2 in VARIABLE_NAME_CHARS :
286+ yield c1 + c2
287+
288+ case VariableFormat .decimal :
289+ for i in itertools .count ():
290+ yield f"_{ i } "
291+
292+ case VariableFormat .hex :
293+ # only prepend an underscore if we need to
294+ # otherwise we hit the save size limit with 4096 vars
295+ for i in itertools .count ():
296+ variable = f"{ i :x} "
297+ if variable [0 ].isdigit ():
298+ yield f"_{ variable } "
299+ else :
300+ yield variable
301+
302+ def get_variable (self , index : int ):
303+ for i , variable in enumerate (self .iter_variables ()):
304+ if i == index :
305+ return variable
306+ raise AssertionError
307+
308+
273309app = Typer ()
274310
275311
@@ -278,6 +314,10 @@ def lookup(
278314 address_str : str ,
279315 ram_size : Annotated [int , Option (min = 1 , max = 726 * 6 )] = 4096 ,
280316 ram_width : int = 128 ,
317+ variable_format : Annotated [
318+ VariableFormat ,
319+ Option ("-f" , "--format" ),
320+ ] = VariableFormat .hex ,
281321):
282322 address = int (address_str , base = 0 )
283323
@@ -288,7 +328,7 @@ def lookup(
288328 ram_y = ram_index // ram_width + 1
289329
290330 variable_index = word_address % ram_size
291- variable = get_variable (variable_index )
331+ variable = variable_format . get_variable (variable_index )
292332
293333 print (
294334 f"Address { hex (address )} is at processor { ram_index } ({ ram_x } , { ram_y } ) in variable { variable } ."
@@ -299,9 +339,13 @@ def lookup(
299339def build (
300340 ram_size : Annotated [int , Option (min = 1 , max = 726 * 6 )] = 4096 ,
301341 lookup_width : Annotated [int , Option (min = 1 )] = 4 ,
342+ variable_format : Annotated [
343+ VariableFormat ,
344+ Option ("-f" , "--format" ),
345+ ] = VariableFormat .hex ,
302346 out : Path = Path ("schematics" ),
303347):
304- lookup_procs , ram_proc = generate_code (ram_size )
348+ lookup_procs , ram_proc = generate_code (ram_size , variable_format )
305349
306350 out .mkdir (parents = True , exist_ok = True )
307351
@@ -338,17 +382,7 @@ def build(
338382 )
339383
340384
341- def get_variable (index : int ):
342- i = 0
343- for c1 in VARIABLE_NAME_CHARS :
344- for c2 in VARIABLE_NAME_CHARS :
345- if i == index :
346- return c1 + c2
347- i += 1
348- raise AssertionError
349-
350-
351- def generate_code (ram_size : int ):
385+ def generate_code (ram_size : int , variable_format : VariableFormat ):
352386 done = False
353387
354388 lookup_procs = list [str ]()
@@ -357,40 +391,36 @@ def generate_code(ram_size: int):
357391 ram_proc = list [str ]()
358392 ram_proc_line = list [str ]()
359393
360- for c1 in VARIABLE_NAME_CHARS :
361- for c2 in VARIABLE_NAME_CHARS :
362- variable = c1 + c2
363-
364- ram_proc_line .append (variable )
365- remaining_space = ram_size - len (ram_proc ) * 6
366- if remaining_space == len (ram_proc_line ):
367- done = True
368- if remaining_space > 0 :
369- ram_proc .append (
370- "draw triangle "
371- + " " .join (
372- ram_proc_line
373- + ram_proc_line [- 1 :] * (6 - len (ram_proc_line ))
374- )
394+ for variable in variable_format .iter_variables ():
395+ ram_proc_line .append (variable )
396+ remaining_space = ram_size - len (ram_proc ) * 6
397+ if remaining_space == len (ram_proc_line ):
398+ done = True
399+ if remaining_space > 0 :
400+ ram_proc .append (
401+ "draw triangle "
402+ + " " .join (
403+ ram_proc_line + ram_proc_line [- 1 :] * (6 - len (ram_proc_line ))
375404 )
376- elif len (ram_proc_line ) == 6 :
377- ram_proc .append ("draw triangle " + " " .join (ram_proc_line ))
378- ram_proc_line .clear ()
379-
380- block_id = BLOCK_IDS [len (lookup_proc )]
381- lookup_proc .append (f'set { block_id } "{ variable } "' )
382- if len (lookup_proc ) == len (BLOCK_IDS ) or done :
383- lines = [
384- 'set _type "lookup"' ,
385- f"set _index { len (lookup_procs )} " ,
386- * lookup_proc ,
387- "stop" ,
388- ]
389- lookup_procs .append ("\n " .join (lines ))
390- lookup_proc .clear ()
391-
392- if done :
393- return lookup_procs , "\n " .join (["stop" ] + ram_proc )
405+ )
406+ elif len (ram_proc_line ) == 6 :
407+ ram_proc .append ("draw triangle " + " " .join (ram_proc_line ))
408+ ram_proc_line .clear ()
409+
410+ block_id = BLOCK_IDS [len (lookup_proc )]
411+ lookup_proc .append (f'set { block_id } "{ variable } "' )
412+ if len (lookup_proc ) == len (BLOCK_IDS ) or done :
413+ lines = [
414+ 'set _type "lookup"' ,
415+ f"set _index { len (lookup_procs )} " ,
416+ * lookup_proc ,
417+ "stop" ,
418+ ]
419+ lookup_procs .append ("\n " .join (lines ))
420+ lookup_proc .clear ()
421+
422+ if done :
423+ return lookup_procs , "\n " .join (["stop" ] + ram_proc )
394424
395425 raise ValueError ("Ran out of variable names!" )
396426
0 commit comments