Skip to content

Commit 20b5860

Browse files
committed
fix: implement ZX Next MUL dependencies
ZX Next MUL instruction dependencies were not implemented in the optimizer.
1 parent fbebabf commit 20b5860

10 files changed

Lines changed: 127 additions & 6 deletions

File tree

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import pathlib
3+
34
from setuptools import setup
45

56
packages = ["src"]

src/arch/z80/optimizer/cpustate.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,17 @@ def execute(self, asm_code):
884884
self.set(o[0], None)
885885
return
886886

887+
if i == "mul":
888+
val_d = self.getv(o[0])
889+
val_e = self.getv(o[1])
890+
if val_d is not None and val_e is not None:
891+
val = val_d * val_e
892+
self.set("de", val)
893+
self.Z = int(val == 0)
894+
self.C = int(val < 0)
895+
self.S = int(val < 0)
896+
return
897+
887898
# Unknown. Resets ALL
888899
self.reset()
889900

src/arch/z80/optimizer/memcell.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ def destroys(self) -> Set[str]:
170170
res.add("f")
171171
elif i in ("set", "res"):
172172
res.update(helpers.single_registers(o[1]))
173+
elif i == "mul":
174+
res.update("d", "e")
173175

174176
return res
175177

@@ -310,6 +312,9 @@ def requires(self) -> Set[str]:
310312
elif i == "im":
311313
result.add("i")
312314

315+
elif i == "mul":
316+
result.update("d", "e")
317+
313318
return result
314319

315320
def affects(self, reglist: Union[List[str], str]) -> bool:
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
_x:
22+
DEFB 00, 00
23+
_y:
24+
DEFB 00
25+
.core.ZXBASIC_USER_DATA_END:
26+
.core.__MAIN_PROGRAM__:
27+
ld a, 1
28+
ld (_y), a
29+
ld d, 16
30+
ld e, a
31+
mul d, e
32+
ld a, e
33+
push af
34+
ld hl, (_x)
35+
call .core.__MUL16_FAST
36+
push hl
37+
call _printTest
38+
ld bc, 0
39+
.core.__END_PROGRAM:
40+
di
41+
ld hl, (.core.__CALL_BACK__)
42+
ld sp, hl
43+
exx
44+
pop hl
45+
pop iy
46+
pop ix
47+
exx
48+
ei
49+
ret
50+
_printTest:
51+
push ix
52+
ld ix, 0
53+
add ix, sp
54+
ld a, (ix+7)
55+
ld (0), a
56+
_printTest__leave:
57+
ld sp, ix
58+
pop ix
59+
exx
60+
pop hl
61+
pop bc
62+
ex (sp), hl
63+
exx
64+
ret
65+
;; --- end of user code ---
66+
#line 1 "/zxbasic/src/arch/zxnext/library-asm/mul16.asm"
67+
push namespace core
68+
__MUL16: ; Mutiplies HL with the last value stored into de stack
69+
; Works for both signed and unsigned
70+
PROC
71+
ex de, hl
72+
pop hl ; Return address
73+
ex (sp), hl ; CALLEE caller convention
74+
__MUL16_FAST:
75+
ld a,d ; a = xh
76+
ld d,h ; d = yh
77+
ld h,a ; h = xh
78+
ld c,e ; c = xl
79+
ld b,l ; b = yl
80+
mul d,e ; yh * yl
81+
ex de,hl
82+
mul d,e ; xh * yl
83+
add hl,de ; add cross products
84+
ld e,c
85+
ld d,b
86+
mul d,e ; yl * xl
87+
ld a,l ; cross products lsb
88+
add a,d ; add to msb final
89+
ld h,a
90+
ld l,e ; hl = final
91+
ret ; Result in hl (16 lower bits)
92+
ENDP
93+
pop namespace
94+
#line 41 "zxnext/opt4_mul8.bas"
95+
END
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
dim x as uinteger
2+
dim y as ubyte
3+
4+
y = 1
5+
6+
printTest(x*16,y*16)
7+
8+
sub printTest(xpos as uinteger, ypos as ubyte)
9+
poke 0, ypos
10+
end sub

tools/normalize_bas.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env python3
22

3-
import sys
43
import re
5-
4+
import sys
65

76
INDENT = 4 * " "
87
RE_END_ASM = re.compile("^END[ \t]+ASM$")

zxb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# -*- coding: utf-8 -*-
33
# vim: ts=4:sw=4:et:
44

5-
import sys
65
import os
6+
import sys
77

88
sys.path.append(os.path.dirname(__file__))
99

zxbasm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
# This is the Parser for the ZXBASM (ZXBasic Assembler)
1212
# ----------------------------------------------------------------------
1313

14-
import sys
1514
import os
15+
import sys
1616

1717
sys.path.append(os.path.dirname(__file__))
1818

zxbc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# -*- coding: utf-8 -*-
33
# vim: ts=4:sw=4:et:
44

5-
import sys
65
import os
6+
import sys
77

88
sys.path.append(os.path.dirname(__file__))
99

zxbpp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
# This is the Parser for the ZXBpp (ZXBasic Preprocessor)
1212
# ----------------------------------------------------------------------
1313

14-
import sys
1514
import os
15+
import sys
1616

1717
sys.path.append(os.path.dirname(__file__))
1818

0 commit comments

Comments
 (0)