Skip to content

Commit 8ad0d64

Browse files
committed
added imulu_b lmulu_b and smulu_b
1 parent a9687a1 commit 8ad0d64

File tree

11 files changed

+716
-2
lines changed

11 files changed

+716
-2
lines changed

src/crt/imulu_b.src

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public __imul_b
6+
public __imulu_b
7+
8+
if PREFER_OS_CRT
9+
10+
__imul_b := 000150h
11+
__imulu_b := __imul_b
12+
13+
else
14+
15+
__imul_b:
16+
__imulu_b:
17+
18+
; Multiplies UHL by A (unsigned) and returns the 24-bit product uhl.
19+
; I: A=multiplier, UHL=multiplicand, ADL=1
20+
; O: uhl=UHL*A
21+
; CC: 32*r(PC)+12*r(SPL)+9*w(SPL)+13
22+
; CC: 31 bytes | 32F + 12R + 9W + 13
23+
Mul_UHL_A_UHL:
24+
push de
25+
push af ; preserve A
26+
27+
dec sp
28+
push hl
29+
inc sp
30+
; (SP + 3) = preserved E
31+
; (SP + 2) = ? --> UDE
32+
; (SP + 1) = UHL --> D
33+
; (SP + 0) = H --> E
34+
; (SP - 1) = L
35+
pop de ; D = UHL, E = H
36+
37+
ld e, a
38+
mlt de ; DE = UHL * A
39+
40+
ld d, l
41+
42+
ld l, a
43+
mlt hl ; HL = H * A
44+
45+
ld a, h
46+
add a, e
47+
ld h, a
48+
49+
add hl, hl
50+
add hl, hl
51+
add hl, hl
52+
add hl, hl
53+
add hl, hl
54+
add hl, hl
55+
add hl, hl
56+
add hl, hl
57+
58+
pop af ; restore A
59+
ld e, a
60+
mlt de ; DE = L * A
61+
add hl, de
62+
63+
pop de
64+
ret
65+
66+
end if

src/crt/imulu_b_fast.src

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public __imulu_b_fast
6+
7+
__imulu_b_fast:
8+
9+
; Multiplies UHL by A (unsigned) and returns the 24-bit product uhl.
10+
; I: A=multiplier, UHL=multiplicand, ADL=1
11+
; O: uhl=UHL*A
12+
; CC: 28*r(PC)+9*r(SPL)+6*w(SPL)+13
13+
; CC: 27 bytes | 28F + 9R + 6W + 13
14+
Mul_UHL_A_UHL_Fast:
15+
dec sp
16+
push hl
17+
inc sp
18+
pop de ; D = UHL, E = H
19+
ld e, a
20+
mlt de ; DE = UHL * A
21+
ld b, l
22+
ld c, a
23+
mlt bc ; BC = L * A
24+
ld l, a
25+
mlt hl ; HL = H * A
26+
ld a, h
27+
add a, e
28+
ld h, a
29+
add hl, hl
30+
add hl, hl
31+
add hl, hl
32+
add hl, hl
33+
add hl, hl
34+
add hl, hl
35+
add hl, hl
36+
add hl, hl
37+
add hl, bc
38+
ret

src/crt/lmulu_b.src

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public __lmulu_b
6+
7+
__lmulu_b:
8+
9+
; Multiplies EUHL by A and returns the 32-bit product euhl.
10+
; I: A=multiplier, EUHL=multiplicand, ADL=1
11+
; O: euhl=EUHL*A
12+
; CC: 43*r(PC)+12*r(SPL)+9*w(SPL)+13
13+
; CC: 42 bytes | 43F + 12R + 9W + 13
14+
Mul_EUHL_A_EUHL:
15+
push bc
16+
push de
17+
18+
dec sp
19+
push hl
20+
inc sp
21+
pop bc ; B = UHL, C = H
22+
23+
ld c, a
24+
mlt bc ; BC = A * U
25+
26+
ld d, a ; preserve A
27+
push de ; A * E
28+
ld d, l
29+
ld e, a
30+
31+
ld l, a
32+
mlt hl ; HL = A * H
33+
34+
ld a, h
35+
add a, c
36+
ld h, a ; A = H
37+
adc a, b ; A = H + AU.hi + carry
38+
sub a, h ; A = AU.hi + carry
39+
40+
add hl, hl
41+
add hl, hl
42+
add hl, hl
43+
add hl, hl
44+
add hl, hl
45+
add hl, hl
46+
add hl, hl
47+
add hl, hl
48+
49+
mlt de ; DE = A * L
50+
add hl, de ; UHL = AH.hi + AU.lo, AH.lo + AL.hi, AL.lo
51+
52+
pop de
53+
ld b, d ; retrieve A
54+
mlt de ; DE = A * E
55+
adc a, e ; AU.hi + AE.lo + Carry
56+
pop de
57+
ld e, a
58+
ld a, b ; restore A
59+
60+
pop bc
61+
ret

src/crt/lmulu_b_fast.src

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public __lmulu_b_fast
6+
7+
__lmulu_b_fast:
8+
9+
; Multiplies EUHL by A and returns the 32-bit product euhl.
10+
; I: A=multiplier, EUHL=multiplicand, ADL=1
11+
; O: euhl=EUHL*A
12+
; CC: 37*r(PC)+6*r(SPL)+3*w(SPL)+13
13+
; CC: 36 bytes | 37F + 6R + 3W + 13
14+
Mul_EUHL_A_EUHL:
15+
dec sp
16+
push hl
17+
inc sp
18+
pop bc ; B = UHL, C = H
19+
20+
ld c, a
21+
mlt bc ; BC = A * U
22+
23+
ld d, a
24+
push de ; A * E
25+
ld d, l
26+
ld e, a
27+
28+
ld l, a
29+
mlt hl ; HL = A * H
30+
31+
ld a, h
32+
add a, c
33+
ld h, a ; A = H
34+
adc a, b ; A = H + AU.hi + carry
35+
sub a, h ; A = AU.hi + carry
36+
37+
add hl, hl
38+
add hl, hl
39+
add hl, hl
40+
add hl, hl
41+
add hl, hl
42+
add hl, hl
43+
add hl, hl
44+
add hl, hl
45+
46+
mlt de ; DE = A * L
47+
add hl, de ; UHL = AH.hi + AU.lo, AH.lo + AL.hi, AL.lo
48+
49+
pop de
50+
mlt de ; DE = A * E
51+
adc a, e ; AU.hi + AE.lo + Carry
52+
ld e, a
53+
ret

src/crt/os.src

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ __fsub := 000290h
1616
__ftol := 00027Ch
1717
public __ftoul
1818
__ftoul := __ftol
19-
public __imul_b
20-
__imul_b := 000150h
2119
public __indcall
2220
__indcall := 00015Ch
2321
public __ishl_b

src/crt/smulu_b.src

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public __smulu_b
6+
7+
__smulu_b:
8+
9+
; Multiplies HL by A (unsigned) and returns the 16-bit product hl.
10+
; I: A=multiplier, HL=multiplicand, ADL=1
11+
; O: hl=HL*A
12+
; CC: 15*r(PC)+6*r(SPL)+3*w(SPL)+9
13+
; CC: 14 bytes | 15F + 6R + 3W + 9
14+
Mul_HL_A_HL:
15+
push de
16+
ld e, a
17+
ld d, h
18+
ld h, e
19+
mlt de ; DE = H * A
20+
mlt hl ; HL = A * L
21+
ld d, e
22+
ld e, 0
23+
add hl, de
24+
pop de
25+
ret

src/crt/smulu_b_fast.src

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
3+
assume adl=1
4+
5+
section .text
6+
7+
public __smulu_b_fast
8+
9+
__smulu_b_fast:
10+
11+
; Multiplies HL by A (unsigned) and returns the 16-bit product hl.
12+
; I: A=multiplier, HL=multiplicand, ADL=1
13+
; O: hl=HL*A
14+
; CC: 12*r(PC)+3*r(SPL)+9
15+
; CC: 11 bytes | 12F + 3R + 9
16+
Mul_HL_A_HL_Fast:
17+
; destroys DE and A. You can swap DE with BC to destroy BC instead
18+
ld e, a
19+
ld d, h
20+
ld h, e
21+
mlt de ; DE = H * A
22+
mlt hl ; HL = A * L
23+
ld a, e
24+
add a, h
25+
ld h, a
26+
ret

test/standalone/mulu_b/autotest.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"transfer_files": [
3+
"bin/DEMO.8xp"
4+
],
5+
"target": {
6+
"name": "DEMO",
7+
"isASM": true
8+
},
9+
"sequence": [
10+
"action|launch",
11+
"delay|1000",
12+
"hashWait|1",
13+
"key|enter",
14+
"delay|300",
15+
"hashWait|2"
16+
],
17+
"hashes": {
18+
"1": {
19+
"description": "All tests passed",
20+
"timeout": 5000,
21+
"start": "vram_start",
22+
"size": "vram_16_size",
23+
"expected_CRCs": [
24+
"38E2AD5A"
25+
]
26+
},
27+
"2": {
28+
"description": "Exit",
29+
"start": "vram_start",
30+
"size": "vram_16_size",
31+
"expected_CRCs": [
32+
"FFAF89BA",
33+
"101734A5",
34+
"9DA19F44",
35+
"A32840C8",
36+
"349F4775"
37+
]
38+
}
39+
}
40+
}

test/standalone/mulu_b/makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ----------------------------
2+
# Makefile Options
3+
# ----------------------------
4+
5+
NAME = DEMO
6+
ICON = icon.png
7+
DESCRIPTION = "CE C Toolchain Demo"
8+
COMPRESSED = NO
9+
ARCHIVED = NO
10+
11+
CFLAGS = -Wall -Wextra -Wshadow -Wconversion -Wformat=2 -Wno-sign-conversion -Oz
12+
CXXFLAGS = -Wall -Wextra -Wshadow -Wconversion -Wformat=2 -Wno-sign-conversion -Oz
13+
14+
PREFER_OS_LIBC = NO
15+
PREFER_OS_CRT = NO
16+
17+
# ----------------------------
18+
19+
include $(shell cedev-config --makefile)

0 commit comments

Comments
 (0)