Skip to content

Commit dffab26

Browse files
committed
refact: Use runtime /arith from zx48k if they are identical
1 parent 80c78e7 commit dffab26

Some content is hidden

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

70 files changed

+696
-840
lines changed
Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,8 @@
1-
2-
; Ripped from: http://www.andreadrian.de/oldcpu/z80_number_cruncher.html#moztocid784223
3-
; Used with permission.
4-
; Multiplies 32x32 bit integer (DEHL x D'E'H'L')
5-
; 64bit result is returned in H'L'H L B'C'A C
6-
7-
8-
push namespace core
9-
10-
__MUL32_64START:
11-
push hl
12-
exx
13-
ld b, h
14-
ld c, l ; BC = Low Part (A)
15-
pop hl ; HL = Load Part (B)
16-
ex de, hl ; DE = Low Part (B), HL = HightPart(A) (must be in B'C')
17-
push hl
18-
19-
exx
20-
pop bc ; B'C' = HightPart(A)
21-
exx ; A = B'C'BC , B = D'E'DE
22-
23-
; multiply routine 32 * 32bit = 64bit
24-
; h'l'hlb'c'ac = b'c'bc * d'e'de
25-
; needs register a, changes flags
26-
;
27-
; this routine was with tiny differences in the
28-
; sinclair zx81 rom for the mantissa multiply
29-
30-
__LMUL:
31-
xor a ; reset carry flag
32-
ld h, a ; result bits 32..47 = 0
33-
ld l, a
34-
exx
35-
ld h, a ; result bits 48..63 = 0
36-
ld l, a
37-
exx
38-
ld a,b ; mpr is b'c'ac
39-
ld b,33 ; initialize loop counter
40-
jp __LMULSTART
41-
42-
__LMULLOOP:
43-
jr nc,__LMULNOADD ; JP is 2 cycles faster than JR. Since it's inside a LOOP
44-
; it can save up to 33 * 2 = 66 cycles
45-
; But JR if 3 cycles faster if JUMP not taken!
46-
add hl,de ; result += mpd
47-
exx
48-
adc hl,de
49-
exx
50-
51-
__LMULNOADD:
52-
exx
53-
rr h ; right shift upper
54-
rr l ; 32bit of result
55-
exx
56-
rr h
57-
rr l
58-
59-
__LMULSTART:
60-
exx
61-
rr b ; right shift mpr/
62-
rr c ; lower 32bit of result
63-
exx
64-
rra ; equivalent to rr a
65-
rr c
66-
djnz __LMULLOOP
67-
68-
ret ; result in h'l'hlb'c'ac
69-
70-
pop namespace
1+
' ----------------------------------------------------------------
2+
' This file is released under the MIT License
3+
'
4+
' Copyleft (k) 2008
5+
' by Jose Rodriguez-Rosa (a.k.a. Boriel) <https://www.boriel.com>
6+
' ----------------------------------------------------------------
7+
8+
#include once [arch:zx48k] <arith/_mul32.asm>
Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
1-
#include once <stackf.asm>
2-
3-
; -------------------------------------------------------------
4-
; Floating point library using the FP ROM Calculator (ZX 48K)
5-
; All of them uses A EDCB registers as 1st paramter.
6-
; For binary operators, the 2n operator must be pushed into the
7-
; stack, in the order AF DE BC (F not used).
8-
;
9-
; Uses CALLEE convention
10-
; -------------------------------------------------------------
11-
12-
push namespace core
13-
14-
__ADDF: ; Addition
15-
call __FPSTACK_PUSH2
16-
17-
; ------------- ROM ADD
18-
rst 28h
19-
defb 0fh ; ADD
20-
defb 38h; ; END CALC
21-
22-
jp __FPSTACK_POP
23-
24-
pop namespace
1+
' ----------------------------------------------------------------
2+
' This file is released under the MIT License
3+
'
4+
' Copyleft (k) 2008
5+
' by Jose Rodriguez-Rosa (a.k.a. Boriel) <https://www.boriel.com>
6+
' ----------------------------------------------------------------
7+
8+
#include once [arch:zx48k] <arith/addf.asm>
Lines changed: 8 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,8 @@
1-
; 16 bit division and modulo functions
2-
; for both signed and unsigned values
3-
4-
#include once <neg16.asm>
5-
6-
push namespace core
7-
8-
__DIVU16: ; 16 bit unsigned division
9-
; HL = Dividend, Stack Top = Divisor
10-
11-
; -- OBSOLETE ; Now uses FASTCALL convention
12-
; ex de, hl
13-
; pop hl ; Return address
14-
; ex (sp), hl ; CALLEE Convention
15-
16-
__DIVU16_FAST:
17-
ld a, h
18-
ld c, l
19-
ld hl, 0
20-
ld b, 16
21-
22-
__DIV16LOOP:
23-
sll c
24-
rla
25-
adc hl,hl
26-
sbc hl,de
27-
jr nc, __DIV16NOADD
28-
add hl,de
29-
dec c
30-
31-
__DIV16NOADD:
32-
djnz __DIV16LOOP
33-
34-
ex de, hl
35-
ld h, a
36-
ld l, c
37-
38-
ret ; HL = quotient, DE = Mudulus
39-
40-
41-
42-
__MODU16: ; 16 bit modulus
43-
; HL = Dividend, Stack Top = Divisor
44-
45-
;ex de, hl
46-
;pop hl
47-
;ex (sp), hl ; CALLEE Convention
48-
49-
call __DIVU16_FAST
50-
ex de, hl ; hl = reminder (modulus)
51-
; de = quotient
52-
53-
ret
54-
55-
56-
__DIVI16: ; 16 bit signed division
57-
; --- The following is OBSOLETE ---
58-
; ex de, hl
59-
; pop hl
60-
; ex (sp), hl ; CALLEE Convention
61-
62-
__DIVI16_FAST:
63-
ld a, d
64-
xor h
65-
ex af, af' ; BIT 7 of a contains result
66-
67-
bit 7, d ; DE is negative?
68-
jr z, __DIVI16A
69-
70-
ld a, e ; DE = -DE
71-
cpl
72-
ld e, a
73-
ld a, d
74-
cpl
75-
ld d, a
76-
inc de
77-
78-
__DIVI16A:
79-
bit 7, h ; HL is negative?
80-
call nz, __NEGHL
81-
82-
__DIVI16B:
83-
call __DIVU16_FAST
84-
ex af, af'
85-
86-
or a
87-
ret p ; return if positive
88-
jp __NEGHL
89-
90-
91-
__MODI16: ; 16 bit modulus
92-
; HL = Dividend, Stack Top = Divisor
93-
94-
;ex de, hl
95-
;pop hl
96-
;ex (sp), hl ; CALLEE Convention
97-
98-
call __DIVI16_FAST
99-
ex de, hl ; hl = reminder (modulus)
100-
; de = quotient
101-
102-
ret
103-
104-
pop namespace
1+
' ----------------------------------------------------------------
2+
' This file is released under the MIT License
3+
'
4+
' Copyleft (k) 2008
5+
' by Jose Rodriguez-Rosa (a.k.a. Boriel) <https://www.boriel.com>
6+
' ----------------------------------------------------------------
7+
8+
#include once [arch:zx48k] <arith/div16.asm>
Lines changed: 8 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,8 @@
1-
#include once <neg32.asm>
2-
3-
; ---------------------------------------------------------
4-
push namespace core
5-
6-
__DIVU32: ; 32 bit unsigned division
7-
; DEHL = Dividend, Stack Top = Divisor
8-
; OPERANDS P = Dividend, Q = Divisor => OPERATION => P / Q
9-
;
10-
; Changes A, BC DE HL B'C' D'E' H'L'
11-
; ---------------------------------------------------------
12-
exx
13-
pop hl ; return address
14-
pop de ; low part
15-
ex (sp), hl ; CALLEE Convention ; H'L'D'E' => Dividend
16-
17-
__DIVU32START: ; Performs D'E'H'L' / HLDE
18-
; Now switch to DIVIDEND = B'C'BC / DIVISOR = D'E'DE (A / B)
19-
push de ; push Lowpart(Q)
20-
ex de, hl ; DE = HL
21-
ld hl, 0
22-
exx
23-
ld b, h
24-
ld c, l
25-
pop hl
26-
push de
27-
ex de, hl
28-
ld hl, 0 ; H'L'HL = 0
29-
exx
30-
pop bc ; Pop HightPart(B) => B = B'C'BC
31-
exx
32-
33-
ld a, 32 ; Loop count
34-
35-
__DIV32LOOP:
36-
sll c ; B'C'BC << 1 ; Output most left bit to carry
37-
rl b
38-
exx
39-
rl c
40-
rl b
41-
exx
42-
43-
adc hl, hl
44-
exx
45-
adc hl, hl
46-
exx
47-
48-
sbc hl,de
49-
exx
50-
sbc hl,de
51-
exx
52-
jp nc, __DIV32NOADD ; use JP inside a loop for being faster
53-
54-
add hl, de
55-
exx
56-
adc hl, de
57-
exx
58-
dec bc
59-
60-
__DIV32NOADD:
61-
dec a
62-
jp nz, __DIV32LOOP ; use JP inside a loop for being faster
63-
; At this point, quotient is stored in B'C'BC and the reminder in H'L'HL
64-
65-
push hl
66-
exx
67-
pop de
68-
ex de, hl ; D'E'H'L' = 32 bits modulus
69-
push bc
70-
exx
71-
pop de ; DE = B'C'
72-
ld h, b
73-
ld l, c ; DEHL = quotient D'E'H'L' = Modulus
74-
75-
ret ; DEHL = quotient, D'E'H'L' = Modulus
76-
77-
78-
79-
__MODU32: ; 32 bit modulus for 32bit unsigned division
80-
; DEHL = Dividend, Stack Top = Divisor (DE, HL)
81-
82-
exx
83-
pop hl ; return address
84-
pop de ; low part
85-
ex (sp), hl ; CALLEE Convention ; H'L'D'E' => Dividend
86-
87-
call __DIVU32START ; At return, modulus is at D'E'H'L'
88-
89-
__MODU32START:
90-
91-
exx
92-
push de
93-
push hl
94-
95-
exx
96-
pop hl
97-
pop de
98-
99-
ret
100-
101-
102-
__DIVI32: ; 32 bit signed division
103-
; DEHL = Dividend, Stack Top = Divisor
104-
; A = Dividend, B = Divisor => A / B
105-
exx
106-
pop hl ; return address
107-
pop de ; low part
108-
ex (sp), hl ; CALLEE Convention ; H'L'D'E' => Dividend
109-
110-
__DIVI32START:
111-
exx
112-
ld a, d ; Save sign
113-
ex af, af'
114-
bit 7, d ; Negative?
115-
call nz, __NEG32 ; Negates DEHL
116-
117-
exx ; Now works with H'L'D'E'
118-
ex af, af'
119-
xor h
120-
ex af, af' ; Stores sign of the result for later
121-
122-
bit 7, h ; Negative?
123-
ex de, hl ; HLDE = DEHL
124-
call nz, __NEG32
125-
ex de, hl
126-
127-
call __DIVU32START
128-
ex af, af' ; Recovers sign
129-
and 128 ; positive?
130-
ret z
131-
132-
jp __NEG32 ; Negates DEHL and returns from there
133-
134-
135-
__MODI32: ; 32bits signed division modulus
136-
exx
137-
pop hl ; return address
138-
pop de ; low part
139-
ex (sp), hl ; CALLEE Convention ; H'L'D'E' => Dividend
140-
141-
call __DIVI32START
142-
jp __MODU32START
143-
144-
pop namespace
1+
' ----------------------------------------------------------------
2+
' This file is released under the MIT License
3+
'
4+
' Copyleft (k) 2008
5+
' by Jose Rodriguez-Rosa (a.k.a. Boriel) <https://www.boriel.com>
6+
' ----------------------------------------------------------------
7+
8+
#include once [arch:zx48k] <arith/div32.asm>

0 commit comments

Comments
 (0)