Skip to content

added imulu_b lmulu_b and smulu_b #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/crt/imulu_b.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
assume adl=1

section .text

public __imul_b
public __imulu_b

if PREFER_OS_CRT

__imul_b := 000150h
__imulu_b := __imul_b

else

__imul_b:
__imulu_b:

; Multiplies UHL by A (unsigned) and returns the 24-bit product uhl.
; I: A=multiplier, UHL=multiplicand, ADL=1
; O: uhl=UHL*A
; CC: 32*r(PC)+12*r(SPL)+9*w(SPL)+13
; CC: 31 bytes | 32F + 12R + 9W + 13
Mul_UHL_A_UHL:
push de
push af ; preserve A

dec sp
push hl
inc sp
; (SP + 3) = preserved E
; (SP + 2) = ? --> UDE
; (SP + 1) = UHL --> D
; (SP + 0) = H --> E
; (SP - 1) = L
pop de ; D = UHL, E = H

ld e, a
mlt de ; DE = UHL * A

ld d, l

ld l, a
mlt hl ; HL = H * A

ld a, h
add a, e
ld h, a

add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl

pop af ; restore A
ld e, a
mlt de ; DE = L * A
add hl, de

pop de
ret

end if
38 changes: 38 additions & 0 deletions src/crt/imulu_b_fast.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
assume adl=1

section .text

public __imulu_b_fast

__imulu_b_fast:

; Multiplies UHL by A (unsigned) and returns the 24-bit product uhl.
; I: A=multiplier, UHL=multiplicand, ADL=1
; O: uhl=UHL*A
; CC: 28*r(PC)+9*r(SPL)+6*w(SPL)+13
; CC: 27 bytes | 28F + 9R + 6W + 13
Mul_UHL_A_UHL_Fast:
dec sp
push hl
inc sp
pop de ; D = UHL, E = H
ld e, a
mlt de ; DE = UHL * A
ld b, l
ld c, a
mlt bc ; BC = L * A
ld l, a
mlt hl ; HL = H * A
ld a, h
add a, e
ld h, a
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, bc
ret
61 changes: 61 additions & 0 deletions src/crt/lmulu_b.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
assume adl=1

section .text

public __lmulu_b

__lmulu_b:

; Multiplies EUHL by A and returns the 32-bit product euhl.
; I: A=multiplier, EUHL=multiplicand, ADL=1
; O: euhl=EUHL*A
; CC: 43*r(PC)+12*r(SPL)+9*w(SPL)+13
; CC: 42 bytes | 43F + 12R + 9W + 13
Mul_EUHL_A_EUHL:
push bc
push de

dec sp
push hl
inc sp
pop bc ; B = UHL, C = H

ld c, a
mlt bc ; BC = A * U

ld d, a ; preserve A
push de ; A * E
ld d, l
ld e, a

ld l, a
mlt hl ; HL = A * H

ld a, h
add a, c
ld h, a ; A = H
adc a, b ; A = H + AU.hi + carry
sub a, h ; A = AU.hi + carry

add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl

mlt de ; DE = A * L
add hl, de ; UHL = AH.hi + AU.lo, AH.lo + AL.hi, AL.lo

pop de
ld b, d ; retrieve A
mlt de ; DE = A * E
adc a, e ; AU.hi + AE.lo + Carry
pop de
ld e, a
ld a, b ; restore A

pop bc
ret
53 changes: 53 additions & 0 deletions src/crt/lmulu_b_fast.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
assume adl=1

section .text

public __lmulu_b_fast

__lmulu_b_fast:

; Multiplies EUHL by A and returns the 32-bit product euhl.
; I: A=multiplier, EUHL=multiplicand, ADL=1
; O: euhl=EUHL*A
; CC: 37*r(PC)+6*r(SPL)+3*w(SPL)+13
; CC: 36 bytes | 37F + 6R + 3W + 13
Mul_EUHL_A_EUHL:
dec sp
push hl
inc sp
pop bc ; B = UHL, C = H

ld c, a
mlt bc ; BC = A * U

ld d, a
push de ; A * E
ld d, l
ld e, a

ld l, a
mlt hl ; HL = A * H

ld a, h
add a, c
ld h, a ; A = H
adc a, b ; A = H + AU.hi + carry
sub a, h ; A = AU.hi + carry

add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl

mlt de ; DE = A * L
add hl, de ; UHL = AH.hi + AU.lo, AH.lo + AL.hi, AL.lo

pop de
mlt de ; DE = A * E
adc a, e ; AU.hi + AE.lo + Carry
ld e, a
ret
2 changes: 0 additions & 2 deletions src/crt/os.src
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ __fsub := 000290h
__ftol := 00027Ch
public __ftoul
__ftoul := __ftol
public __imul_b
__imul_b := 000150h
public __indcall
__indcall := 00015Ch
public __ishl_b
Expand Down
25 changes: 25 additions & 0 deletions src/crt/smulu_b.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
assume adl=1

section .text

public __smulu_b

__smulu_b:

; Multiplies HL by A (unsigned) and returns the 16-bit product hl.
; I: A=multiplier, HL=multiplicand, ADL=1
; O: hl=HL*A
; CC: 15*r(PC)+6*r(SPL)+3*w(SPL)+9
; CC: 14 bytes | 15F + 6R + 3W + 9
Mul_HL_A_HL:
push de
ld e, a
ld d, h
ld h, e
mlt de ; DE = H * A
mlt hl ; HL = A * L
ld d, e
ld e, 0
add hl, de
pop de
ret
26 changes: 26 additions & 0 deletions src/crt/smulu_b_fast.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


assume adl=1

section .text

public __smulu_b_fast

__smulu_b_fast:

; Multiplies HL by A (unsigned) and returns the 16-bit product hl.
; I: A=multiplier, HL=multiplicand, ADL=1
; O: hl=HL*A
; CC: 12*r(PC)+3*r(SPL)+9
; CC: 11 bytes | 12F + 3R + 9
Mul_HL_A_HL_Fast:
; destroys DE and A. You can swap DE with BC to destroy BC instead
ld e, a
ld d, h
ld h, e
mlt de ; DE = H * A
mlt hl ; HL = A * L
ld a, e
add a, h
ld h, a
ret
40 changes: 40 additions & 0 deletions test/standalone/mulu_b/autotest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"transfer_files": [
"bin/DEMO.8xp"
],
"target": {
"name": "DEMO",
"isASM": true
},
"sequence": [
"action|launch",
"delay|1000",
"hashWait|1",
"key|enter",
"delay|300",
"hashWait|2"
],
"hashes": {
"1": {
"description": "All tests passed",
"timeout": 5000,
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"38E2AD5A"
]
},
"2": {
"description": "Exit",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"FFAF89BA",
"101734A5",
"9DA19F44",
"A32840C8",
"349F4775"
]
}
}
}
19 changes: 19 additions & 0 deletions test/standalone/mulu_b/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ----------------------------
# Makefile Options
# ----------------------------

NAME = DEMO
ICON = icon.png
DESCRIPTION = "CE C Toolchain Demo"
COMPRESSED = NO
ARCHIVED = NO

CFLAGS = -Wall -Wextra -Wshadow -Wconversion -Wformat=2 -Wno-sign-conversion -Oz
CXXFLAGS = -Wall -Wextra -Wshadow -Wconversion -Wformat=2 -Wno-sign-conversion -Oz

PREFER_OS_LIBC = NO
PREFER_OS_CRT = NO

# ----------------------------

include $(shell cedev-config --makefile)
Loading
Loading