Skip to content

Commit 5a33655

Browse files
authored
Merge pull request #649 from boriel/bugfix/array_long_assignation
Bugfix/array long assignation
2 parents 8fff061 + 9dd189b commit 5a33655

5 files changed

Lines changed: 313 additions & 4 deletions

File tree

src/arch/z80/backend/_array.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from typing import List
1414

15+
from src.arch.z80.backend._32bit import _32bit_oper
1516
from src.arch.z80.backend._f16 import _f16_oper
1617
from src.arch.z80.backend._float import _float_oper, _fpush
1718
from src.arch.z80.backend.common import REQUIRES, Quad, is_int, runtime_call
@@ -253,18 +254,17 @@ def _astore32(ins: Quad) -> List[str]:
253254
if indirect:
254255
output.append("push hl")
255256
output.append("ld hl, %i" % (value & 0xFFFF))
256-
output.append(runtime_call(RuntimeLabel.ILOAD32)) # TODO: Check if this is ever used
257+
output.append(runtime_call(RuntimeLabel.ILOAD32))
257258
output.append("ld b, h")
258259
output.append("ld c, l") # BC = Lower 16 bits
259260
output.append("pop hl")
260261
else:
261262
output.append("ld de, %i" % (value >> 16))
262263
output.append("ld bc, %i" % (value & 0xFFFF))
263264
except ValueError:
264-
output.append("pop bc")
265-
output.append("pop de")
265+
output.extend(_32bit_oper(value, preserveHL=True))
266266

267-
output.append(runtime_call(RuntimeLabel.STORE32)) # TODO: Check if this is ever used
267+
output.append(runtime_call(RuntimeLabel.STORE32))
268268

269269
return output
270270

src/zxbc/zxbparser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ def check_bound(boundlist, remaining):
797797
return True # It's OK :-)
798798

799799
error(lineno, "Unexpected extra vector dimensions. It should be %i" % len(remaining))
800+
return False
800801

801802
if not isinstance(remaining, list):
802803
error(lineno, "Mismatched vector size. Missing %i extra dimension(s)" % len(boundlist))
Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
org 32768
2+
.core.__START_PROGRAM:
3+
di
4+
push ix
5+
push iy
6+
exx
7+
push hl
8+
exx
9+
ld hl, 0
10+
add hl, sp
11+
ld (.core.__CALL_BACK__), hl
12+
ei
13+
jp .core.__MAIN_PROGRAM__
14+
.core.__CALL_BACK__:
15+
DEFW 0
16+
.core.ZXBASIC_USER_DATA:
17+
; Defines USER DATA Length in bytes
18+
.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA
19+
.core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN
20+
.core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA
21+
_score:
22+
DEFB 70h
23+
DEFB 11h
24+
DEFB 01h
25+
DEFB 00h
26+
_r:
27+
DEFW .LABEL.__LABEL0
28+
_r.__DATA__.__PTR__:
29+
DEFW _r.__DATA__
30+
_r.__DATA__:
31+
DEFB 40h
32+
DEFB 9Ch
33+
DEFB 00h
34+
DEFB 00h
35+
DEFB 0B8h
36+
DEFB 88h
37+
DEFB 00h
38+
DEFB 00h
39+
DEFB 30h
40+
DEFB 75h
41+
DEFB 00h
42+
DEFB 00h
43+
DEFB 0A8h
44+
DEFB 61h
45+
DEFB 00h
46+
DEFB 00h
47+
DEFB 20h
48+
DEFB 4Eh
49+
DEFB 00h
50+
DEFB 00h
51+
DEFB 98h
52+
DEFB 3Ah
53+
DEFB 00h
54+
DEFB 00h
55+
DEFB 10h
56+
DEFB 27h
57+
DEFB 00h
58+
DEFB 00h
59+
.LABEL.__LABEL0:
60+
DEFW 0000h
61+
DEFB 04h
62+
.core.ZXBASIC_USER_DATA_END:
63+
.core.__MAIN_PROGRAM__:
64+
ld de, 1
65+
ld hl, 14464
66+
push de
67+
push hl
68+
call _main
69+
ld hl, (_r.__DATA__ + 0)
70+
ld de, (_r.__DATA__ + 0 + 2)
71+
ld a, l
72+
ld (0), a
73+
ld hl, 0
74+
ld b, h
75+
ld c, l
76+
.core.__END_PROGRAM:
77+
di
78+
ld hl, (.core.__CALL_BACK__)
79+
ld sp, hl
80+
exx
81+
pop hl
82+
exx
83+
pop iy
84+
pop ix
85+
ei
86+
ret
87+
_main:
88+
push ix
89+
ld ix, 0
90+
add ix, sp
91+
ld hl, 0
92+
push hl
93+
ld l, (ix-2)
94+
ld h, (ix-1)
95+
push hl
96+
ld hl, _r
97+
call .core.__ARRAY
98+
ld de, 1
99+
ld bc, 4464
100+
call .core.__STORE32
101+
ld l, (ix-2)
102+
ld h, (ix-1)
103+
push hl
104+
ld hl, _r
105+
call .core.__ARRAY
106+
ld bc, (_score)
107+
ld de, (_score + 2)
108+
call .core.__STORE32
109+
ld l, (ix+4)
110+
ld h, (ix+5)
111+
ld e, (ix+6)
112+
ld d, (ix+7)
113+
push de
114+
push hl
115+
ld l, (ix-2)
116+
ld h, (ix-1)
117+
inc hl
118+
push hl
119+
ld hl, _r
120+
call .core.__ARRAY
121+
pop bc
122+
pop de
123+
call .core.__STORE32
124+
_main__leave:
125+
ld sp, ix
126+
pop ix
127+
exx
128+
pop hl
129+
pop bc
130+
ex (sp), hl
131+
exx
132+
ret
133+
;; --- end of user code ---
134+
#line 1 "/zxbasic/src/arch/zx48k/library-asm/array.asm"
135+
; vim: ts=4:et:sw=4:
136+
; Copyleft (K) by Jose M. Rodriguez de la Rosa
137+
; (a.k.a. Boriel)
138+
; http://www.boriel.com
139+
; -------------------------------------------------------------------
140+
; Simple array Index routine
141+
; Number of total indexes dimensions - 1 at beginning of memory
142+
; HL = Start of array memory (First two bytes contains N-1 dimensions)
143+
; Dimension values on the stack, (top of the stack, highest dimension)
144+
; E.g. A(2, 4) -> PUSH <4>; PUSH <2>
145+
; For any array of N dimension A(aN-1, ..., a1, a0)
146+
; and dimensions D[bN-1, ..., b1, b0], the offset is calculated as
147+
; O = [a0 + b0 * (a1 + b1 * (a2 + ... bN-2(aN-1)))]
148+
; What I will do here is to calculate the following sequence:
149+
; ((aN-1 * bN-2) + aN-2) * bN-3 + ...
150+
#line 1 "/zxbasic/src/arch/zx48k/library-asm/mul16.asm"
151+
push namespace core
152+
__MUL16: ; Mutiplies HL with the last value stored into de stack
153+
; Works for both signed and unsigned
154+
PROC
155+
LOCAL __MUL16LOOP
156+
LOCAL __MUL16NOADD
157+
ex de, hl
158+
pop hl ; Return address
159+
ex (sp), hl ; CALLEE caller convention
160+
__MUL16_FAST:
161+
ld b, 16
162+
ld a, h
163+
ld c, l
164+
ld hl, 0
165+
__MUL16LOOP:
166+
add hl, hl ; hl << 1
167+
sla c
168+
rla ; a,c << 1
169+
jp nc, __MUL16NOADD
170+
add hl, de
171+
__MUL16NOADD:
172+
djnz __MUL16LOOP
173+
ret ; Result in hl (16 lower bits)
174+
ENDP
175+
pop namespace
176+
#line 20 "/zxbasic/src/arch/zx48k/library-asm/array.asm"
177+
#line 24 "/zxbasic/src/arch/zx48k/library-asm/array.asm"
178+
push namespace core
179+
__ARRAY_PTR: ;; computes an array offset from a pointer
180+
ld c, (hl)
181+
inc hl
182+
ld h, (hl)
183+
ld l, c
184+
__ARRAY:
185+
PROC
186+
LOCAL LOOP
187+
LOCAL ARRAY_END
188+
LOCAL RET_ADDRESS ; Stores return address
189+
LOCAL TMP_ARR_PTR ; Stores pointer temporarily
190+
ld e, (hl)
191+
inc hl
192+
ld d, (hl)
193+
inc hl
194+
ld (TMP_ARR_PTR), hl
195+
ex de, hl
196+
ex (sp), hl ; Return address in HL, array address in the stack
197+
ld (RET_ADDRESS + 1), hl ; Stores it for later
198+
exx
199+
pop hl ; Will use H'L' as the pointer
200+
ld c, (hl) ; Loads Number of dimensions from (hl)
201+
inc hl
202+
ld b, (hl)
203+
inc hl ; Ready
204+
exx
205+
ld hl, 0 ; HL = Offset "accumulator"
206+
LOOP:
207+
#line 64 "/zxbasic/src/arch/zx48k/library-asm/array.asm"
208+
pop bc ; Get next index (Ai) from the stack
209+
#line 74 "/zxbasic/src/arch/zx48k/library-asm/array.asm"
210+
add hl, bc ; Adds current index
211+
exx ; Checks if B'C' = 0
212+
ld a, b ; Which means we must exit (last element is not multiplied by anything)
213+
or c
214+
jr z, ARRAY_END ; if B'Ci == 0 we are done
215+
ld e, (hl) ; Loads next dimension into D'E'
216+
inc hl
217+
ld d, (hl)
218+
inc hl
219+
push de
220+
dec bc ; Decrements loop counter
221+
exx
222+
pop de ; DE = Max bound Number (i-th dimension)
223+
call __FNMUL
224+
jp LOOP
225+
ARRAY_END:
226+
ld a, (hl)
227+
exx
228+
#line 103 "/zxbasic/src/arch/zx48k/library-asm/array.asm"
229+
LOCAL ARRAY_SIZE_LOOP
230+
ex de, hl
231+
ld hl, 0
232+
ld b, a
233+
ARRAY_SIZE_LOOP:
234+
add hl, de
235+
djnz ARRAY_SIZE_LOOP
236+
#line 113 "/zxbasic/src/arch/zx48k/library-asm/array.asm"
237+
ex de, hl
238+
ld hl, (TMP_ARR_PTR)
239+
ld a, (hl)
240+
inc hl
241+
ld h, (hl)
242+
ld l, a
243+
add hl, de ; Adds element start
244+
RET_ADDRESS:
245+
jp 0
246+
;; Performs a faster multiply for little 16bit numbs
247+
LOCAL __FNMUL, __FNMUL2
248+
__FNMUL:
249+
xor a
250+
or h
251+
jp nz, __MUL16_FAST
252+
or l
253+
ret z
254+
cp 33
255+
jp nc, __MUL16_FAST
256+
ld b, l
257+
ld l, h ; HL = 0
258+
__FNMUL2:
259+
add hl, de
260+
djnz __FNMUL2
261+
ret
262+
TMP_ARR_PTR:
263+
DW 0 ; temporary storage for pointer to tables
264+
ENDP
265+
pop namespace
266+
#line 72 "zx48k/astore32.bas"
267+
#line 1 "/zxbasic/src/arch/zx48k/library-asm/store32.asm"
268+
push namespace core
269+
__PISTORE32:
270+
push hl
271+
push ix
272+
pop hl
273+
add hl, bc
274+
pop bc
275+
__ISTORE32: ; Load address at hl, and stores E,D,B,C integer at that address
276+
ld a, (hl)
277+
inc hl
278+
ld h, (hl)
279+
ld l, a
280+
__STORE32: ; Stores the given integer in DEBC at address HL
281+
ld (hl), c
282+
inc hl
283+
ld (hl), b
284+
inc hl
285+
ld (hl), e
286+
inc hl
287+
ld (hl), d
288+
ret
289+
pop namespace
290+
#line 73 "zx48k/astore32.bas"
291+
END
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
dim r(0 to 6) as ulong => {40000, 35000, 30000, 25000, 20000, 15000, 10000}
2+
3+
dim score as ulong = 70000
4+
5+
sub main(scr as ulong)
6+
dim i as Uinteger = 0
7+
r(i) = 70000
8+
r(i) = score
9+
r(i + 1) = scr
10+
end sub
11+
12+
main(80000)
13+
14+
poke 0, r(0)
15+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dim r(0 to r) as ulong => {40000, 35000, 30000, 25000, 20000, 15000, 10000}
2+

0 commit comments

Comments
 (0)