Skip to content

Commit 8b7352b

Browse files
authored
Merge pull request #390 from boriel/bugfix/save_data
Bugfix/save data
2 parents 7a359c2 + 135194f commit 8b7352b

682 files changed

Lines changed: 15549 additions & 17594 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

arch/zx48k/backend/__init__.py

Lines changed: 28 additions & 11 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,23 @@ 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)
22342237

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 ' +
2240+
str(OPTIONS.heap_size.value))
2241+
heap_init.append(OPTIONS.heap_start_label.value + ':')
2242+
heap_init.append('DEFS %s' % str(OPTIONS.heap_size.value))
2243+
2244+
heap_init.append('; Defines USER DATA Length in bytes\n' +
2245+
'ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA')
2246+
heap_init.append('.__LABEL__.ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_LEN')
2247+
heap_init.append('.__LABEL__.ZXBASIC_USER_DATA EQU ZXBASIC_USER_DATA')
22372248

22382249
output.append('%s:' % START_LABEL)
22392250
if OPTIONS.headerless.value:
2251+
output.extend(heap_init)
22402252
return output
22412253

22422254
output.append('di')
@@ -2253,6 +2265,9 @@ def emit_start():
22532265
for x in sorted(INITS):
22542266
output.append('call %s' % x)
22552267

2268+
output.append('jp %s' % MAIN_LABEL)
2269+
output.extend(heap_init)
2270+
22562271
return output
22572272

22582273

@@ -2265,7 +2280,7 @@ def convertToBool():
22652280

22662281
REQUIRES.add('strictbool.asm')
22672282

2268-
result = []
2283+
result = list()
22692284
result.append('pop af')
22702285
result.append('call __NORMALIZE_BOOLEAN')
22712286
result.append('push af')
@@ -2281,14 +2296,16 @@ def emit_end():
22812296
output = []
22822297
output.extend(AT_END)
22832298

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')
2299+
# if REQUIRES.intersection(MEMINITS) or '__MEM_INIT' in INITS:
2300+
# output.append(OPTIONS.heap_start_label.value + ':')
2301+
# output.append('; Defines DATA END\n' + 'ZXBASIC_USER_DATA_END EQU ZXBASIC_MEM_HEAP + ZXBASIC_HEAP_SIZE')
2302+
# else:
2303+
# output.append('; Defines DATA END --> HEAP size is 0\n' + 'ZXBASIC_USER_DATA_END:')
2304+
#
2305+
# output.append('; Defines USER DATA Length in bytes\n' +
2306+
# 'ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA')
2307+
# output.append('.__LABEL__.ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_LEN')
2308+
# output.append('.__LABEL__.ZXBASIC_USER_DATA EQU ZXBASIC_USER_DATA')
22922309

22932310
if OPTIONS.autorun.value:
22942311
output.append('END %s' % START_LABEL)

arch/zx48k/library-asm/load.asm

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include once <alloc.asm>
21
#include once <free.asm>
32

43
#ifndef HIDE_LOAD_MSG
@@ -26,12 +25,14 @@ LOAD_CODE:
2625
LOCAL LOAD_END
2726
LOCAL VR_CONTROL, VR_CONT_1, VR_CONT_2
2827
LOCAL MEM0
28+
LOCAL TMP_SP
2929

3030
MEM0 EQU 5C92h ; Temporary memory buffer
3131
HEAD1 EQU MEM0 + 8 ; Uses CALC Mem for temporary storage
3232
; Must skip first 8 bytes used by
3333
; PRINT routine
3434
TMP_HEADER EQU HEAD1 + 17 ; Temporary HEADER2 pointer storage
35+
TMP_SP EQU TMP_HEADER + 2 ; Temporary SP storage
3536

3637
#ifdef __ENABLE_BREAK__
3738
LD_BYTES EQU 0556h ; ROM Routine LD-BYTES
@@ -59,6 +60,8 @@ __LOAD_CODE: ; INLINE version
5960
ld (ix + 13), e
6061
ld (ix + 14), d ; Store address in bytes
6162

63+
push hl ; String ptr to be freed later
64+
6265
ld a, h
6366
or l
6467
ld b, h
@@ -99,15 +102,15 @@ LOAD_CONT:
99102
ldir ; Copy String block NAME in header
100103

101104
LOAD_CONT2:
102-
ld bc, 17; 2nd Header
103-
call __MEM_ALLOC
105+
pop hl ; String ptr
106+
call MEM_FREE
104107

105-
ld a, h
106-
or l
107-
jr nz, LOAD_CONT3; there's memory
108-
109-
ld a, ERROR_OutOfMemory
110-
jp __ERROR
108+
ld hl, 0
109+
add hl, sp
110+
ld (TMP_SP), hl
111+
ld bc, -18
112+
add hl, sp
113+
ld sp, hl
111114

112115
LOAD_CONT3:
113116
ld (TMP_HEADER), hl
@@ -239,9 +242,10 @@ LOAD_ERROR:
239242
ld (ERR_NR), a
240243

241244
LOAD_END:
245+
ld hl, (TMP_SP)
246+
ld sp, hl ; Recovers stack
242247
pop ix ; Recovers stack frame pointer
243-
ld hl, (TMP_HEADER) ; Recovers tmp_header pointer
244-
jp MEM_FREE ; Returns via FREE_MEM, freeing tmp header
248+
ret
245249

246250
#ifndef __ENABLE_BREAK__
247251
LOCAL LD_BYTES_RET

arch/zx48k/library-asm/save.asm

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
; YYY and ZZZ are 16 bit on top of the stack.
55

66
#include once <error.asm>
7+
#include once <free.asm>
78

89
SAVE_CODE:
910

@@ -14,16 +15,20 @@ SAVE_CODE:
1415
LOCAL ROM_SAVE
1516
LOCAL __ERR_EMPTY
1617
LOCAL SAVE_STOP
18+
LOCAL STR_PTR
19+
LOCAL SAVE_EMPTY_ERROR
1720

1821
#ifdef __ENABLE_BREAK__
1922
ROM_SAVE EQU 0970h
2023
#endif
2124
MEMBOT EQU 23698 ; Use the CALC mem to store header
25+
STR_PTR EQU MEMBOT + 17
2226

2327
pop hl ; Return address
2428
pop bc ; data length in bytes
2529
pop de ; address start
2630
ex (sp), hl ; CALLE => now hl = String
31+
ld (STR_PTR), hl
2732

2833
; This function will call the ROM SAVE CODE Routine
2934
; Parameters in the stack are HL => String with SAVE name
@@ -34,7 +39,7 @@ SAVE_CODE:
3439
__SAVE_CODE: ; INLINE version
3540
ld a, b
3641
or c
37-
ret z ; Return if block length == 0
42+
jr z, SAVE_EMPTY_ERROR ; Return if block length == 0
3843
3944
push ix
4045
ld a, h
@@ -79,6 +84,8 @@ __ERR_EMPTY:
7984
SAVE_CONT:
8085
ld de, MEMBOT + 1
8186
ldir ; Copy String block NAME
87+
ld hl, (STR_PTR)
88+
call MEM_FREE
8289
ld l, (ix + 13)
8390
ld h, (ix + 14) ; Restores start of bytes
8491

@@ -97,8 +104,15 @@ NO_INT:
97104
pop ix
98105
ret
99106

107+
SAVE_EMPTY_ERROR:
108+
ld a, ERROR_InvalidArg
109+
100110
SAVE_STOP:
101111
pop ix
112+
push af
113+
ld hl, (STR_PTR)
114+
call MEM_FREE
115+
pop af
102116
jp __STOP
103117

104118
#ifndef __ENABLE_BREAK__

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: 6 additions & 6 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_:
@@ -2209,10 +2209,10 @@ def p_save_data(p):
22092209
else:
22102210
length = make_number(entry.type_.size, lineno=p.lineno(4))
22112211
else:
2212-
access = SYMBOL_TABLE.access_id('.ZXBASIC_USER_DATA', p.lineno(3))
2212+
access = SYMBOL_TABLE.access_label('.ZXBASIC_USER_DATA', p.lineno(3), 0)
22132213
start = make_unary(p.lineno(3), 'ADDRESS', access, type_=TYPE.uinteger)
22142214

2215-
access = SYMBOL_TABLE.access_id('.ZXBASIC_USER_DATA_LEN', p.lineno(3))
2215+
access = SYMBOL_TABLE.access_label('.ZXBASIC_USER_DATA_LEN', p.lineno(3), 0)
22162216
length = make_unary(p.lineno(3), 'ADDRESS', access, type_=TYPE.uinteger)
22172217

22182218
p[0] = make_sentence(p[1], p[2], start, length)
@@ -2278,10 +2278,10 @@ def p_load_data(p):
22782278
else:
22792279
length = make_number(entry.type_.size, lineno=p.lineno(4))
22802280
else:
2281-
entry = SYMBOL_TABLE.access_id('.ZXBASIC_USER_DATA', p.lineno(3))
2281+
entry = SYMBOL_TABLE.access_label('.ZXBASIC_USER_DATA', p.lineno(3), 0)
22822282
start = make_unary(p.lineno(3), 'ADDRESS', entry, type_=TYPE.uinteger)
22832283

2284-
entry = SYMBOL_TABLE.access_id('.ZXBASIC_USER_DATA_LEN', p.lineno(3))
2284+
entry = SYMBOL_TABLE.access_label('.ZXBASIC_USER_DATA_LEN', p.lineno(3), 0)
22852285
length = make_unary(p.lineno(3), 'ADDRESS', entry, type_=TYPE.uinteger)
22862286

22872287
p[0] = make_sentence(p[1], p[2], start, length)

tests/functional/00.asm

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ __START_PROGRAM:
1010
add hl, sp
1111
ld (__CALL_BACK__), hl
1212
ei
13+
jp __MAIN_PROGRAM__
14+
ZXBASIC_USER_DATA:
15+
; Defines USER DATA Length in bytes
16+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
17+
.__LABEL__.ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_LEN
18+
.__LABEL__.ZXBASIC_USER_DATA EQU ZXBASIC_USER_DATA
19+
_a:
20+
DEFB 00, 00, 00, 00, 00
21+
ZXBASIC_USER_DATA_END:
22+
__MAIN_PROGRAM__:
1323
ld hl, 0
1424
ld b, h
1525
ld c, l
@@ -26,11 +36,4 @@ __END_PROGRAM:
2636
ret
2737
__CALL_BACK__:
2838
DEFW 0
29-
ZXBASIC_USER_DATA:
30-
_a:
31-
DEFB 00, 00, 00, 00, 00
32-
; Defines DATA END --> HEAP size is 0
33-
ZXBASIC_USER_DATA_END:
34-
; Defines USER DATA Length in bytes
35-
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
3639
END

tests/functional/02.asm

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ __START_PROGRAM:
1010
add hl, sp
1111
ld (__CALL_BACK__), hl
1212
ei
13+
jp __MAIN_PROGRAM__
14+
ZXBASIC_USER_DATA:
15+
; Defines USER DATA Length in bytes
16+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
17+
.__LABEL__.ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_LEN
18+
.__LABEL__.ZXBASIC_USER_DATA EQU ZXBASIC_USER_DATA
19+
_a:
20+
DEFB 00, 00
21+
ZXBASIC_USER_DATA_END:
22+
__MAIN_PROGRAM__:
1323
ld hl, 0
1424
ld b, h
1525
ld c, l
@@ -26,11 +36,4 @@ __END_PROGRAM:
2636
ret
2737
__CALL_BACK__:
2838
DEFW 0
29-
ZXBASIC_USER_DATA:
30-
_a:
31-
DEFB 00, 00
32-
; Defines DATA END --> HEAP size is 0
33-
ZXBASIC_USER_DATA_END:
34-
; Defines USER DATA Length in bytes
35-
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
3639
END

tests/functional/03.asm

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ __START_PROGRAM:
1010
add hl, sp
1111
ld (__CALL_BACK__), hl
1212
ei
13+
jp __MAIN_PROGRAM__
14+
ZXBASIC_USER_DATA:
15+
; Defines USER DATA Length in bytes
16+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
17+
.__LABEL__.ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_LEN
18+
.__LABEL__.ZXBASIC_USER_DATA EQU ZXBASIC_USER_DATA
19+
_a:
20+
DEFB 00
21+
ZXBASIC_USER_DATA_END:
22+
__MAIN_PROGRAM__:
1323
ld hl, 0
1424
ld b, h
1525
ld c, l
@@ -26,11 +36,4 @@ __END_PROGRAM:
2636
ret
2737
__CALL_BACK__:
2838
DEFW 0
29-
ZXBASIC_USER_DATA:
30-
_a:
31-
DEFB 00
32-
; Defines DATA END --> HEAP size is 0
33-
ZXBASIC_USER_DATA_END:
34-
; Defines USER DATA Length in bytes
35-
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
3639
END

tests/functional/04.asm

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ __START_PROGRAM:
1010
add hl, sp
1111
ld (__CALL_BACK__), hl
1212
ei
13+
jp __MAIN_PROGRAM__
14+
ZXBASIC_USER_DATA:
15+
; Defines USER DATA Length in bytes
16+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
17+
.__LABEL__.ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_LEN
18+
.__LABEL__.ZXBASIC_USER_DATA EQU ZXBASIC_USER_DATA
19+
_a:
20+
DEFB 00, 00
21+
ZXBASIC_USER_DATA_END:
22+
__MAIN_PROGRAM__:
1323
ld hl, 0
1424
ld b, h
1525
ld c, l
@@ -26,11 +36,4 @@ __END_PROGRAM:
2636
ret
2737
__CALL_BACK__:
2838
DEFW 0
29-
ZXBASIC_USER_DATA:
30-
_a:
31-
DEFB 00, 00
32-
; Defines DATA END --> HEAP size is 0
33-
ZXBASIC_USER_DATA_END:
34-
; Defines USER DATA Length in bytes
35-
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
3639
END

0 commit comments

Comments
 (0)