Skip to content

Commit 5c792fe

Browse files
committed
Moves HEAP and vars to the beginning
This makes load / save of program longer because now includes the heap, but it also allows SAVE "" DATA and LOAD "" DATA to work properly on different programs providing both the saver and the loader have the same org and the same variables declared in the same order and with the same type.
1 parent 146e4b0 commit 5c792fe

3 files changed

Lines changed: 33 additions & 19 deletions

File tree

arch/zx48k/backend/__init__.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@
112112
START_LABEL = '__START_PROGRAM'
113113
END_LABEL = '__END_PROGRAM'
114114
CALL_BACK = '__CALL_BACK__'
115-
115+
MAIN_LABEL = '__MAIN_PROGRAM__'
116+
DATA_LABEL = 'ZXBASIC_USER_DATA'
117+
DATA_END_LABEL = 'ZXBASIC_USER_DATA_END'
116118

117119
# Whether to use the FunctionExit scheme
118120
FLAG_use_function_exit = False
@@ -2230,13 +2232,22 @@ def __str__(self):
22302232
# -------------------------
22312233
def emit_start():
22322234
output = list()
2235+
heap_init = ['%s:' % DATA_LABEL]
22332236
output.append('org %s' % OPTIONS.org.value)
2234-
2237+
22352238
if REQUIRES.intersection(MEMINITS) or '__MEM_INIT' in INITS:
2236-
output.append('; Defines HEAP SIZE\n' + OPTIONS.heap_size_label.value + ' EQU ' + str(OPTIONS.heap_size.value))
2239+
heap_init.append('; Defines HEAP SIZE\n' + OPTIONS.heap_size_label.value + ' EQU ' + str(OPTIONS.heap_size.value))
2240+
heap_init.append(OPTIONS.heap_start_label.value + ':')
2241+
heap_init.append('DEFS %s' % str(OPTIONS.heap_size.value))
2242+
2243+
heap_init.append('; Defines USER DATA Length in bytes\n' +
2244+
'ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA')
2245+
heap_init.append('.__LABEL__.ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_LEN')
2246+
heap_init.append('.__LABEL__.ZXBASIC_USER_DATA EQU ZXBASIC_USER_DATA')
22372247

22382248
output.append('%s:' % START_LABEL)
22392249
if OPTIONS.headerless.value:
2250+
output.extend(heap_init)
22402251
return output
22412252

22422253
output.append('di')
@@ -2253,6 +2264,9 @@ def emit_start():
22532264
for x in sorted(INITS):
22542265
output.append('call %s' % x)
22552266

2267+
output.append('jp %s' % MAIN_LABEL)
2268+
output.extend(heap_init)
2269+
22562270
return output
22572271

22582272

@@ -2265,7 +2279,7 @@ def convertToBool():
22652279

22662280
REQUIRES.add('strictbool.asm')
22672281

2268-
result = []
2282+
result = list()
22692283
result.append('pop af')
22702284
result.append('call __NORMALIZE_BOOLEAN')
22712285
result.append('push af')
@@ -2281,16 +2295,16 @@ def emit_end():
22812295
output = []
22822296
output.extend(AT_END)
22832297

2284-
if REQUIRES.intersection(MEMINITS) or '__MEM_INIT' in INITS:
2285-
output.append(OPTIONS.heap_start_label.value + ':')
2286-
output.append('; Defines DATA END\n' + 'ZXBASIC_USER_DATA_END EQU ZXBASIC_MEM_HEAP + ZXBASIC_HEAP_SIZE')
2287-
else:
2288-
output.append('; Defines DATA END --> HEAP size is 0\n' + 'ZXBASIC_USER_DATA_END:')
2289-
2290-
output.append('; Defines USER DATA Length in bytes\n' +
2291-
'ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA')
2292-
output.append('.__LABEL__.ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_LEN')
2293-
output.append('.__LABEL__.ZXBASIC_USER_DATA EQU ZXBASIC_USER_DATA')
2298+
# if REQUIRES.intersection(MEMINITS) or '__MEM_INIT' in INITS:
2299+
# output.append(OPTIONS.heap_start_label.value + ':')
2300+
# output.append('; Defines DATA END\n' + 'ZXBASIC_USER_DATA_END EQU ZXBASIC_MEM_HEAP + ZXBASIC_HEAP_SIZE')
2301+
# else:
2302+
# output.append('; Defines DATA END --> HEAP size is 0\n' + 'ZXBASIC_USER_DATA_END:')
2303+
#
2304+
# output.append('; Defines USER DATA Length in bytes\n' +
2305+
# 'ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA')
2306+
# output.append('.__LABEL__.ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_LEN')
2307+
# output.append('.__LABEL__.ZXBASIC_USER_DATA EQU ZXBASIC_USER_DATA')
22942308

22952309
if OPTIONS.autorun.value:
22962310
output.append('END %s' % START_LABEL)

libzxbc/zxb.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,9 @@ def main(args=None, emitter=None):
348348
return 1 # Exit with errors
349349

350350
tmp = [x for x in backend.emit(backend.MEMORY, optimize=False) if x.strip()[0] != '#']
351-
asm_output += tmp
352-
asm_output = backend.emit_start() + asm_output
353-
asm_output += backend.emit_end()
351+
asm_output = backend.emit_start() + tmp \
352+
+ ['%s:' % backend.DATA_END_LABEL, '%s:' % backend.MAIN_LABEL] \
353+
+ asm_output + backend.emit_end()
354354

355355
if options.asm: # Only output assembler file
356356
with open_file(OPTIONS.outputFileName.value, 'wt', 'utf-8') as output_file:

libzxbc/zxbparser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ def p_start(p):
499499
"""
500500
global ast, data_ast
501501

502-
user_data = make_label('.ZXBASIC_USER_DATA', 0)
502+
make_label('.ZXBASIC_USER_DATA', 0)
503503
make_label('.ZXBASIC_USER_DATA_LEN', 0)
504504

505505
if PRINT_IS_USED:
@@ -532,7 +532,7 @@ def p_start(p):
532532
if not api.check.check_pending_calls():
533533
return
534534

535-
data_ast = make_sentence('BLOCK', user_data)
535+
data_ast = make_sentence('BLOCK')
536536

537537
# Appends variable declarations at the end.
538538
for var in SYMBOL_TABLE.vars_:

0 commit comments

Comments
 (0)