From 0814090782aee298a33d18d974fa38fd3c6cb7a1 Mon Sep 17 00:00:00 2001 From: unknown <71151164+ZERICO2005@users.noreply.github.com> Date: Sat, 13 Sep 2025 19:15:41 -0600 Subject: [PATCH 1/2] added saturated add/sub routines (except 8 bit) --- src/crt/i48adds_sat.src | 25 +++++++++++++++++++++++++ src/crt/i48addu_sat.src | 22 ++++++++++++++++++++++ src/crt/i48subs_sat.src | 26 ++++++++++++++++++++++++++ src/crt/i48subu_sat.src | 24 ++++++++++++++++++++++++ src/crt/iadds_sat.src | 18 ++++++++++++++++++ src/crt/iaddu_sat.src | 14 ++++++++++++++ src/crt/isubs_sat.src | 18 ++++++++++++++++++ src/crt/isubu_sat.src | 16 ++++++++++++++++ src/crt/ladds_sat.src | 25 +++++++++++++++++++++++++ src/crt/laddu_sat.src | 21 +++++++++++++++++++++ src/crt/lladds_sat.src | 39 +++++++++++++++++++++++++++++++++++++++ src/crt/lladdu_sat.src | 35 +++++++++++++++++++++++++++++++++++ src/crt/llsubs_sat.src | 39 +++++++++++++++++++++++++++++++++++++++ src/crt/llsubu_sat.src | 35 +++++++++++++++++++++++++++++++++++ src/crt/lsubs_sat.src | 27 +++++++++++++++++++++++++++ src/crt/lsubu_sat.src | 24 ++++++++++++++++++++++++ src/crt/sadds_sat.src | 18 ++++++++++++++++++ src/crt/saddu_sat.src | 14 ++++++++++++++ src/crt/ssubs_sat.src | 18 ++++++++++++++++++ src/crt/ssubu_sat.src | 16 ++++++++++++++++ 20 files changed, 474 insertions(+) create mode 100644 src/crt/i48adds_sat.src create mode 100644 src/crt/i48addu_sat.src create mode 100644 src/crt/i48subs_sat.src create mode 100644 src/crt/i48subu_sat.src create mode 100644 src/crt/iadds_sat.src create mode 100644 src/crt/iaddu_sat.src create mode 100644 src/crt/isubs_sat.src create mode 100644 src/crt/isubu_sat.src create mode 100644 src/crt/ladds_sat.src create mode 100644 src/crt/laddu_sat.src create mode 100644 src/crt/lladds_sat.src create mode 100644 src/crt/lladdu_sat.src create mode 100644 src/crt/llsubs_sat.src create mode 100644 src/crt/llsubu_sat.src create mode 100644 src/crt/lsubs_sat.src create mode 100644 src/crt/lsubu_sat.src create mode 100644 src/crt/sadds_sat.src create mode 100644 src/crt/saddu_sat.src create mode 100644 src/crt/ssubs_sat.src create mode 100644 src/crt/ssubu_sat.src diff --git a/src/crt/i48adds_sat.src b/src/crt/i48adds_sat.src new file mode 100644 index 000000000..1b95f78bf --- /dev/null +++ b/src/crt/i48adds_sat.src @@ -0,0 +1,25 @@ + .assume adl=1 + + .section .text + + .global __i48adds_sat + .type __i48adds_sat, @function + +__i48adds_sat: + ; signed saturated addition + ; UDE:UHL = UDE:UHL + UIY:UBC + add hl, bc + ex de, hl + push bc + lea bc, iy + 0 + adc hl, bc + pop bc + ex de, hl + ret po + ld hl, $FFFFFF + ld de, $7FFFFF + ret m ; positive overflow + ; negative underflow + inc hl + inc de + ret diff --git a/src/crt/i48addu_sat.src b/src/crt/i48addu_sat.src new file mode 100644 index 000000000..41bfc159b --- /dev/null +++ b/src/crt/i48addu_sat.src @@ -0,0 +1,22 @@ + .assume adl=1 + + .section .text + + .global __i48addu_sat + .type __i48addu_sat, @function + +__i48addu_sat: + ; unsigned saturated addition + ; UDE:UHL = UDE:UHL + UIY:UBC + add hl, bc + ex de, hl + push bc + lea bc, iy + 0 + adc hl, bc + pop bc + ex de, hl + ret nc + sbc hl, hl + ex de, hl + sbc hl, hl + ret diff --git a/src/crt/i48subs_sat.src b/src/crt/i48subs_sat.src new file mode 100644 index 000000000..f24d241d1 --- /dev/null +++ b/src/crt/i48subs_sat.src @@ -0,0 +1,26 @@ + .assume adl=1 + + .section .text + + .global __i48subs_sat + .type __i48subs_sat, @function + +__i48subs_sat: + ; signed saturated subtraction + ; UDE:UHL = UDE:UHL - UIY:UBC + or a, a + sbc hl, bc + ex de, hl + push bc + lea bc, iy + 0 + sbc hl, bc + pop bc + ex de, hl + ret po + ld hl, $000000 + ld de, $800000 + ret p ; negative underflow + ; positive overflow + dec hl + dec de + ret diff --git a/src/crt/i48subu_sat.src b/src/crt/i48subu_sat.src new file mode 100644 index 000000000..69f0fed76 --- /dev/null +++ b/src/crt/i48subu_sat.src @@ -0,0 +1,24 @@ + .assume adl=1 + + .section .text + + .global __i48subu_sat + .type __i48subu_sat, @function + +__i48subu_sat: + ; unsigned saturated subtraction + ; UDE:UHL = UDE:UHL - UIY:UBC + or a, a + sbc hl, bc + ex de, hl + push bc + lea bc, iy + 0 + sbc hl, bc + pop bc + ex de, hl + ret nc + or a, a + sbc hl, hl + ex de, hl + sbc hl, hl + ret diff --git a/src/crt/iadds_sat.src b/src/crt/iadds_sat.src new file mode 100644 index 000000000..67afd0b00 --- /dev/null +++ b/src/crt/iadds_sat.src @@ -0,0 +1,18 @@ + .assume adl=1 + + .section .text + + .global __iadds_sat + .type __iadds_sat, @function + +__iadds_sat: + ; signed saturated addition + ; UHL = UHL + UBC + or a, a + adc hl, bc + ret po + ld hl, $7FFFFF + ret m ; positive overflow + ; negative underflow + inc hl + ret diff --git a/src/crt/iaddu_sat.src b/src/crt/iaddu_sat.src new file mode 100644 index 000000000..1bd4067a4 --- /dev/null +++ b/src/crt/iaddu_sat.src @@ -0,0 +1,14 @@ + .assume adl=1 + + .section .text + + .global __iaddu_sat + .type __iaddu_sat, @function + +__iaddu_sat: + ; unsigned saturated addition + ; UHL = UHL + UBC + add hl, bc + ret nc + sbc hl, hl + ret diff --git a/src/crt/isubs_sat.src b/src/crt/isubs_sat.src new file mode 100644 index 000000000..5ca369df8 --- /dev/null +++ b/src/crt/isubs_sat.src @@ -0,0 +1,18 @@ + .assume adl=1 + + .section .text + + .global __isubs_sat + .type __isubs_sat, @function + +__isubs_sat: + ; signed saturated subtraction + ; HL = HL - BC + or a, a + sbc hl, bc + ret po + ld hl, $800000 + ret p ; negative underflow + ; positive overflow + inc hl + ret diff --git a/src/crt/isubu_sat.src b/src/crt/isubu_sat.src new file mode 100644 index 000000000..e5d300fae --- /dev/null +++ b/src/crt/isubu_sat.src @@ -0,0 +1,16 @@ + .assume adl=1 + + .section .text + + .global __isubu_sat + .type __isubu_sat, @function + +__isubu_sat: + ; unsigned saturated subtraction + ; UHL = UHL - UBC + or a, a + sbc hl, bc + ret nc + or a, a + sbc hl, hl + ret diff --git a/src/crt/ladds_sat.src b/src/crt/ladds_sat.src new file mode 100644 index 000000000..180995f1c --- /dev/null +++ b/src/crt/ladds_sat.src @@ -0,0 +1,25 @@ + .assume adl=1 + + .section .text + + .global __ladds_sat + .type __ladds_sat, @function + +__ladds_sat: + ; signed saturated addition + ; E:UHL = E:UHL + A:UBC + push bc + add hl, bc + ld c, a + adc a, e + ld e, a + ld a, c + pop bc + ret po + ld e, $7F + ld hl, $FFFFFF + ret m ; positive overflow + ; negative underflow + inc hl + inc e + ret diff --git a/src/crt/laddu_sat.src b/src/crt/laddu_sat.src new file mode 100644 index 000000000..208b0a5ef --- /dev/null +++ b/src/crt/laddu_sat.src @@ -0,0 +1,21 @@ + .assume adl=1 + + .section .text + + .global __laddu_sat + .type __laddu_sat, @function + +__laddu_sat: + ; unsigned saturated addition + ; E:UHL = E:UHL + A:UBC + push bc + add hl, bc + ld c, a + adc a, e + ld e, a + ld a, c + pop bc + ret nc + sbc hl, hl + ld e, l + ret diff --git a/src/crt/lladds_sat.src b/src/crt/lladds_sat.src new file mode 100644 index 000000000..47b1c92db --- /dev/null +++ b/src/crt/lladds_sat.src @@ -0,0 +1,39 @@ + .assume adl=1 + + .section .text + + .global __lladds_sat + .type __lladds_sat, @function + +__lladds_sat: + ; signed saturated addition + ; BC:UDE:UHL = BC:UDE:UHL + (SP) + push iy + ld iy, 0 + add iy, sp + push bc + ld bc, (iy + 6) + add hl, bc + ex de, hl + ld bc, (iy + 9) + adc hl, bc + ex de, hl + ex (sp), hl + ld bc, (iy + 12) + adc.s hl, bc + ld c, l + ld b, h + pop hl + pop iy + ret po + ld hl, $FFFFFF + push hl + pop de + ld c, l + ld b, $7F + ret m ; positive overflow + ; negative underflow + inc hl + inc de + inc bc + ret diff --git a/src/crt/lladdu_sat.src b/src/crt/lladdu_sat.src new file mode 100644 index 000000000..513c40975 --- /dev/null +++ b/src/crt/lladdu_sat.src @@ -0,0 +1,35 @@ + .assume adl=1 + + .section .text + + .global __lladdu_sat + .type __lladdu_sat, @function + +__lladdu_sat: + ; unsigned saturated addition + ; BC:UDE:UHL = BC:UDE:UHL + (SP) + push iy + ld iy, 0 + add iy, sp + push bc + ld bc, (iy + 6) + add hl, bc + ex de, hl + ld bc, (iy + 9) + adc hl, bc + ex de, hl + pop bc + jr nc, .nc48 + inc bc +.nc48: + ld iy, (iy + 12) + add.s iy, bc + lea bc, iy + 0 + pop iy + ret nc + sbc hl, hl + ex de, hl + sbc hl, hl + ld b, l + ld c, l + ret diff --git a/src/crt/llsubs_sat.src b/src/crt/llsubs_sat.src new file mode 100644 index 000000000..b006c2980 --- /dev/null +++ b/src/crt/llsubs_sat.src @@ -0,0 +1,39 @@ + .assume adl=1 + + .section .text + + .global __llsubs_sat + .type __llsubs_sat, @function + +__llsubs_sat: + ; signed saturated subtraction + ; BC:UDE:UHL = BC:UDE:UHL - (SP) + push iy + ld iy, 0 + add iy, sp + push bc + ld bc, (iy + 6) + sbc hl, bc + ex de, hl + ld bc, (iy + 9) + sbc hl, bc + ex de, hl + ex (sp), hl + ld bc, (iy + 12) + sbc.s hl, bc + ld c, l + ld b, h + pop hl + pop iy + ret po + ld hl, $000000 + push hl + pop de + ld c, l + ld b, $80 + ret m ; negative underflow + ; positive overflow + dec hl + dec de + dec bc + ret diff --git a/src/crt/llsubu_sat.src b/src/crt/llsubu_sat.src new file mode 100644 index 000000000..1a3451313 --- /dev/null +++ b/src/crt/llsubu_sat.src @@ -0,0 +1,35 @@ + .assume adl=1 + + .section .text + + .global __llsubu_sat + .type __llsubu_sat, @function + +__llsubu_sat: + ; unsigned saturated subtraction + ; BC:UDE:UHL = BC:UDE:UHL - (SP) + push iy + ld iy, 0 + add iy, sp + push bc + ld bc, (iy + 6) + sbc hl, bc + ex de, hl + ld bc, (iy + 9) + sbc hl, bc + ex de, hl + ex (sp), hl + ld bc, (iy + 12) + sbc.s hl, bc + ld c, l + ld b, h + pop hl + pop iy + ret nc + or a, a + sbc hl, hl + ex de, hl + sbc hl, hl + ld b, l + ld c, l + ret diff --git a/src/crt/lsubs_sat.src b/src/crt/lsubs_sat.src new file mode 100644 index 000000000..7313b2bec --- /dev/null +++ b/src/crt/lsubs_sat.src @@ -0,0 +1,27 @@ + .assume adl=1 + + .section .text + + .global __lsubs_sat + .type __lsubs_sat, @function + +__lsubs_sat: + ; signed saturated subtraction + ; E:UHL = E:UHL - A:UBC + push bc + or a, a + sbc hl, bc + ld c, a + ld a, e + sbc a, c + ld e, a + ld a, c + pop bc + ret po + ld hl, $000000 + ld e, $80 + ret p ; negative underflow + ; positive overflow + dec hl + dec e + ret diff --git a/src/crt/lsubu_sat.src b/src/crt/lsubu_sat.src new file mode 100644 index 000000000..723daec81 --- /dev/null +++ b/src/crt/lsubu_sat.src @@ -0,0 +1,24 @@ + .assume adl=1 + + .section .text + + .global __lsubu_sat + .type __lsubu_sat, @function + +__lsubu_sat: + ; unsigned saturated subtraction + ; E:UHL = E:UHL - A:UBC + push bc + or a, a + sbc hl, bc + ld c, a + ld a, e + sbc a, c + ld e, a + ld a, c + pop bc + ret nc + or a, a + sbc hl, hl + ld e, l + ret diff --git a/src/crt/sadds_sat.src b/src/crt/sadds_sat.src new file mode 100644 index 000000000..feac5c9e6 --- /dev/null +++ b/src/crt/sadds_sat.src @@ -0,0 +1,18 @@ + .assume adl=1 + + .section .text + + .global __sadds_sat + .type __sadds_sat, @function + +__sadds_sat: + ; signed saturated addition + ; HL = HL + BC + or a, a + adc.s hl, bc + ret po + ld.sis hl, $7FFF + ret m ; positive overflow + ; negative underflow + inc hl + ret diff --git a/src/crt/saddu_sat.src b/src/crt/saddu_sat.src new file mode 100644 index 000000000..560a4d2fa --- /dev/null +++ b/src/crt/saddu_sat.src @@ -0,0 +1,14 @@ + .assume adl=1 + + .section .text + + .global __saddu_sat + .type __saddu_sat, @function + +__saddu_sat: + ; unsigned saturated addition + ; HL = HL + BC + add.s hl, bc + ret nc + sbc hl, hl + ret diff --git a/src/crt/ssubs_sat.src b/src/crt/ssubs_sat.src new file mode 100644 index 000000000..fcec393a1 --- /dev/null +++ b/src/crt/ssubs_sat.src @@ -0,0 +1,18 @@ + .assume adl=1 + + .section .text + + .global __ssubs_sat + .type __ssubs_sat, @function + +__ssubs_sat: + ; signed saturated subtraction + ; HL = HL - BC + or a, a + sbc.s hl, bc + ret po + ld.sis hl, $8000 + ret p ; negative underflow + ; positive overflow + dec hl + ret diff --git a/src/crt/ssubu_sat.src b/src/crt/ssubu_sat.src new file mode 100644 index 000000000..ec53a91b2 --- /dev/null +++ b/src/crt/ssubu_sat.src @@ -0,0 +1,16 @@ + .assume adl=1 + + .section .text + + .global __ssubu_sat + .type __ssubu_sat, @function + +__ssubu_sat: + ; unsigned saturated subtraction + ; HL = HL - BC + or a, a + sbc.s hl, bc + ret nc + or a, a + sbc hl, hl + ret From 54db337d19f549a07ff62427789ea198cbc2188a Mon Sep 17 00:00:00 2001 From: unknown <71151164+ZERICO2005@users.noreply.github.com> Date: Wed, 17 Sep 2025 11:34:26 -0600 Subject: [PATCH 2/2] added add/sub overflow routines --- src/crt/i48add.src | 15 +++++++++++++++ src/crt/i48adds_sat.src | 23 ++++++++++------------- src/crt/i48addu_sat.src | 10 +++------- src/crt/i48sub.src | 15 +++++++++++++++ src/crt/i48subs_sat.src | 18 +++++++----------- src/crt/i48subu_sat.src | 11 +++-------- src/crt/ladd_overflow.src | 24 ++++++++++++++++++++++++ src/crt/ladds_sat.src | 22 +++++++++------------- src/crt/laddu_sat.src | 10 +++------- src/crt/lladd_overflow.src | 34 ++++++++++++++++++++++++++++++++++ src/crt/lladds_sat.src | 37 +++++++++++-------------------------- src/crt/lladdu_sat.src | 21 +++------------------ src/crt/llsub_overflow.src | 34 ++++++++++++++++++++++++++++++++++ src/crt/llsubs_sat.src | 33 +++++++++------------------------ src/crt/llsubu_sat.src | 20 +++----------------- src/crt/lsub_overflow.src | 26 ++++++++++++++++++++++++++ src/crt/lsubs_sat.src | 18 ++++++------------ src/crt/lsubu_sat.src | 12 +++--------- 18 files changed, 218 insertions(+), 165 deletions(-) create mode 100644 src/crt/ladd_overflow.src create mode 100644 src/crt/lladd_overflow.src create mode 100644 src/crt/llsub_overflow.src create mode 100644 src/crt/lsub_overflow.src diff --git a/src/crt/i48add.src b/src/crt/i48add.src index 2381e7a9c..47535ae5c 100644 --- a/src/crt/i48add.src +++ b/src/crt/i48add.src @@ -6,9 +6,24 @@ .assume adl=1 .section .text + .global __i48add .type __i48add, @function + .global __i48addu_overflow + .type __i48addu_overflow, @function + + .global __i48subs_overflow + .type __i48subs_overflow, @function + +__i48addu_overflow: + ; returned flags: + ; NC : no unsigned overflow + ; C : unsigned overflow occured +__i48adds_overflow: + ; returned flags: + ; PO : no signed overflow + ; PE : signed overflow occured __i48add: ; CC: 11 bytes | 12F + 6R + 3W + 1 add hl, bc diff --git a/src/crt/i48adds_sat.src b/src/crt/i48adds_sat.src index 1b95f78bf..fed740f74 100644 --- a/src/crt/i48adds_sat.src +++ b/src/crt/i48adds_sat.src @@ -8,18 +8,15 @@ __i48adds_sat: ; signed saturated addition ; UDE:UHL = UDE:UHL + UIY:UBC - add hl, bc - ex de, hl - push bc - lea bc, iy + 0 - adc hl, bc - pop bc - ex de, hl + call __i48adds_overflow ret po - ld hl, $FFFFFF - ld de, $7FFFFF - ret m ; positive overflow - ; negative underflow - inc hl - inc de + ex de, hl + add hl, hl + sbc hl, hl + ld de, $800000 + ret nc ; negative underflow + ; positive overflow + dec de ret + + .extern __i48adds_overflow diff --git a/src/crt/i48addu_sat.src b/src/crt/i48addu_sat.src index 41bfc159b..d0a3bb9db 100644 --- a/src/crt/i48addu_sat.src +++ b/src/crt/i48addu_sat.src @@ -8,15 +8,11 @@ __i48addu_sat: ; unsigned saturated addition ; UDE:UHL = UDE:UHL + UIY:UBC - add hl, bc - ex de, hl - push bc - lea bc, iy + 0 - adc hl, bc - pop bc - ex de, hl + call __i48addu_overflow ret nc sbc hl, hl ex de, hl sbc hl, hl ret + + .extern __i48addu_overflow diff --git a/src/crt/i48sub.src b/src/crt/i48sub.src index 9c77968d5..8704dc7e6 100644 --- a/src/crt/i48sub.src +++ b/src/crt/i48sub.src @@ -6,9 +6,24 @@ .assume adl=1 .section .text + .global __i48sub .type __i48sub, @function + .global __i48subu_overflow + .type __i48subu_overflow, @function + + .global __i48subs_overflow + .type __i48subs_overflow, @function + +__i48subu_overflow: + ; returned flags: + ; NC : no unsigned overflow + ; C : unsigned overflow occured +__i48subs_overflow: + ; returned flags: + ; PO : no signed overflow + ; PE : signed overflow occured __i48sub: ; CC: 13 bytes | 14F + 6R + 3W + 1 or a, a diff --git a/src/crt/i48subs_sat.src b/src/crt/i48subs_sat.src index f24d241d1..629a29c8a 100644 --- a/src/crt/i48subs_sat.src +++ b/src/crt/i48subs_sat.src @@ -8,19 +8,15 @@ __i48subs_sat: ; signed saturated subtraction ; UDE:UHL = UDE:UHL - UIY:UBC - or a, a - sbc hl, bc - ex de, hl - push bc - lea bc, iy + 0 - sbc hl, bc - pop bc - ex de, hl + call __i48subs_overflow ret po - ld hl, $000000 + ex de, hl + add hl, hl + sbc hl, hl ld de, $800000 - ret p ; negative underflow + ret nc ; negative underflow ; positive overflow - dec hl dec de ret + + .extern __i48subs_overflow diff --git a/src/crt/i48subu_sat.src b/src/crt/i48subu_sat.src index 69f0fed76..2d514a69e 100644 --- a/src/crt/i48subu_sat.src +++ b/src/crt/i48subu_sat.src @@ -8,17 +8,12 @@ __i48subu_sat: ; unsigned saturated subtraction ; UDE:UHL = UDE:UHL - UIY:UBC - or a, a - sbc hl, bc - ex de, hl - push bc - lea bc, iy + 0 - sbc hl, bc - pop bc - ex de, hl + call __i48subu_overflow ret nc or a, a sbc hl, hl ex de, hl sbc hl, hl ret + + .extern __i48subu_overflow diff --git a/src/crt/ladd_overflow.src b/src/crt/ladd_overflow.src new file mode 100644 index 000000000..3cc998bd8 --- /dev/null +++ b/src/crt/ladd_overflow.src @@ -0,0 +1,24 @@ + .assume adl=1 + + .section .text + + .global __laddu_overflow + .type __laddu_overflow, @function + + .global __ladds_overflow + .type __ladds_overflow, @function + +__laddu_overflow: + ; NC : no unsigned overflow + ; C : unsigned overflow occured +__ladds_overflow: + ; PO : no signed overflow + ; PE : signed overflow occured + push bc + add hl, bc + ld c, a + adc a, e + ld e, a + ld a, c + pop bc + ret diff --git a/src/crt/ladds_sat.src b/src/crt/ladds_sat.src index 180995f1c..9626a4134 100644 --- a/src/crt/ladds_sat.src +++ b/src/crt/ladds_sat.src @@ -8,18 +8,14 @@ __ladds_sat: ; signed saturated addition ; E:UHL = E:UHL + A:UBC - push bc - add hl, bc - ld c, a - adc a, e - ld e, a - ld a, c - pop bc + call __ladds_overflow ret po - ld e, $7F - ld hl, $FFFFFF - ret m ; positive overflow - ; negative underflow - inc hl - inc e + sla e + sbc hl, hl + ld e, $80 + ret nc ; negative underflow + ; positive overflow + dec e ret + + .extern __ladds_overflow diff --git a/src/crt/laddu_sat.src b/src/crt/laddu_sat.src index 208b0a5ef..19f9d376b 100644 --- a/src/crt/laddu_sat.src +++ b/src/crt/laddu_sat.src @@ -8,14 +8,10 @@ __laddu_sat: ; unsigned saturated addition ; E:UHL = E:UHL + A:UBC - push bc - add hl, bc - ld c, a - adc a, e - ld e, a - ld a, c - pop bc + call __laddu_overflow ret nc sbc hl, hl ld e, l ret + + .extern __laddu_overflow diff --git a/src/crt/lladd_overflow.src b/src/crt/lladd_overflow.src new file mode 100644 index 000000000..91dd1c992 --- /dev/null +++ b/src/crt/lladd_overflow.src @@ -0,0 +1,34 @@ + .assume adl=1 + + .section .text + + .global __lladdu_overflow + .type __lladdu_overflow, @function + + .global __lladds_overflow + .type __lladds_overflow, @function + +__lladdu_overflow: + ; NC : no unsigned overflow + ; C : unsigned overflow occured +__lladds_overflow: + ; PO : no signed overflow + ; PE : signed overflow + push iy + ld iy, 0 + add iy, sp + push bc + ld bc, (iy + 6) + add hl, bc + ex de, hl + ld bc, (iy + 9) + adc hl, bc + ex de, hl + ex (sp), hl + ld bc, (iy + 12) + adc.s hl, bc + ld c, l + ld b, h + pop hl + pop iy + ret diff --git a/src/crt/lladds_sat.src b/src/crt/lladds_sat.src index 47b1c92db..eb78f69dd 100644 --- a/src/crt/lladds_sat.src +++ b/src/crt/lladds_sat.src @@ -8,32 +8,17 @@ __lladds_sat: ; signed saturated addition ; BC:UDE:UHL = BC:UDE:UHL + (SP) - push iy - ld iy, 0 - add iy, sp - push bc - ld bc, (iy + 6) - add hl, bc - ex de, hl - ld bc, (iy + 9) - adc hl, bc - ex de, hl - ex (sp), hl - ld bc, (iy + 12) - adc.s hl, bc - ld c, l - ld b, h - pop hl - pop iy + call __lladds_overflow ret po - ld hl, $FFFFFF - push hl - pop de + sla b + sbc hl, hl + ex de, hl + sbc hl, hl ld c, l - ld b, $7F - ret m ; positive overflow - ; negative underflow - inc hl - inc de - inc bc + ld b, $80 + ret nc ; negative underflow + ; positive overflow + dec b ret + + .extern __lladds_overflow diff --git a/src/crt/lladdu_sat.src b/src/crt/lladdu_sat.src index 513c40975..89ee5c065 100644 --- a/src/crt/lladdu_sat.src +++ b/src/crt/lladdu_sat.src @@ -8,24 +8,7 @@ __lladdu_sat: ; unsigned saturated addition ; BC:UDE:UHL = BC:UDE:UHL + (SP) - push iy - ld iy, 0 - add iy, sp - push bc - ld bc, (iy + 6) - add hl, bc - ex de, hl - ld bc, (iy + 9) - adc hl, bc - ex de, hl - pop bc - jr nc, .nc48 - inc bc -.nc48: - ld iy, (iy + 12) - add.s iy, bc - lea bc, iy + 0 - pop iy + call __lladdu_overflow ret nc sbc hl, hl ex de, hl @@ -33,3 +16,5 @@ __lladdu_sat: ld b, l ld c, l ret + + .extern __lladdu_overflow diff --git a/src/crt/llsub_overflow.src b/src/crt/llsub_overflow.src new file mode 100644 index 000000000..b36ac8a34 --- /dev/null +++ b/src/crt/llsub_overflow.src @@ -0,0 +1,34 @@ + .assume adl=1 + + .section .text + + .global __lladdu_overflow + .type __lladdu_overflow, @function + + .global __lladds_overflow + .type __lladds_overflow, @function + +__lladdu_overflow: + ; NC : no unsigned overflow + ; C : unsigned overflow occured +__lladds_overflow: + ; PO : no signed overflow + ; PE : signed overflow occured + push iy + ld iy, 0 + add iy, sp + push bc + ld bc, (iy + 6) + sbc hl, bc + ex de, hl + ld bc, (iy + 9) + sbc hl, bc + ex de, hl + ex (sp), hl + ld bc, (iy + 12) + sbc.s hl, bc + ld c, l + ld b, h + pop hl + pop iy + ret diff --git a/src/crt/llsubs_sat.src b/src/crt/llsubs_sat.src index b006c2980..33f4340a8 100644 --- a/src/crt/llsubs_sat.src +++ b/src/crt/llsubs_sat.src @@ -8,32 +8,17 @@ __llsubs_sat: ; signed saturated subtraction ; BC:UDE:UHL = BC:UDE:UHL - (SP) - push iy - ld iy, 0 - add iy, sp - push bc - ld bc, (iy + 6) - sbc hl, bc - ex de, hl - ld bc, (iy + 9) - sbc hl, bc - ex de, hl - ex (sp), hl - ld bc, (iy + 12) - sbc.s hl, bc - ld c, l - ld b, h - pop hl - pop iy + call __llsubs_overflow ret po - ld hl, $000000 - push hl - pop de + sla b + sbc hl, hl + ex de, hl + sbc hl, hl ld c, l ld b, $80 - ret m ; negative underflow + ret nc ; negative underflow ; positive overflow - dec hl - dec de - dec bc + dec b ret + + .extern __llsubs_overflow diff --git a/src/crt/llsubu_sat.src b/src/crt/llsubu_sat.src index 1a3451313..9f042af2b 100644 --- a/src/crt/llsubu_sat.src +++ b/src/crt/llsubu_sat.src @@ -8,23 +8,7 @@ __llsubu_sat: ; unsigned saturated subtraction ; BC:UDE:UHL = BC:UDE:UHL - (SP) - push iy - ld iy, 0 - add iy, sp - push bc - ld bc, (iy + 6) - sbc hl, bc - ex de, hl - ld bc, (iy + 9) - sbc hl, bc - ex de, hl - ex (sp), hl - ld bc, (iy + 12) - sbc.s hl, bc - ld c, l - ld b, h - pop hl - pop iy + call __llsubu_overflow ret nc or a, a sbc hl, hl @@ -33,3 +17,5 @@ __llsubu_sat: ld b, l ld c, l ret + + .extern __llsubu_overflow diff --git a/src/crt/lsub_overflow.src b/src/crt/lsub_overflow.src new file mode 100644 index 000000000..6697db688 --- /dev/null +++ b/src/crt/lsub_overflow.src @@ -0,0 +1,26 @@ + .assume adl=1 + + .section .text + + .global __lsubu_overflow + .type __lsubu_overflow, @function + + .global __lsubs_overflow + .type __lsubs_overflow, @function + +__lsubu_overflow: + ; NC : no unsigned overflow + ; C : unsigned overflow occured +__lsubs_overflow: + ; PO : no signed overflow + ; PE : signed overflow occured + push bc + or a, a + sbc hl, bc + ld c, a + ld a, e + sbc a, c + ld e, a + ld a, c + pop bc + ret diff --git a/src/crt/lsubs_sat.src b/src/crt/lsubs_sat.src index 7313b2bec..6a218d66d 100644 --- a/src/crt/lsubs_sat.src +++ b/src/crt/lsubs_sat.src @@ -8,20 +8,14 @@ __lsubs_sat: ; signed saturated subtraction ; E:UHL = E:UHL - A:UBC - push bc - or a, a - sbc hl, bc - ld c, a - ld a, e - sbc a, c - ld e, a - ld a, c - pop bc + call __lsubs_overflow ret po - ld hl, $000000 + sla e + sbc hl, hl ld e, $80 - ret p ; negative underflow + ret nc ; negative underflow ; positive overflow - dec hl dec e ret + + .extern __lsubs_overflow diff --git a/src/crt/lsubu_sat.src b/src/crt/lsubu_sat.src index 723daec81..29635a93e 100644 --- a/src/crt/lsubu_sat.src +++ b/src/crt/lsubu_sat.src @@ -8,17 +8,11 @@ __lsubu_sat: ; unsigned saturated subtraction ; E:UHL = E:UHL - A:UBC - push bc - or a, a - sbc hl, bc - ld c, a - ld a, e - sbc a, c - ld e, a - ld a, c - pop bc + call __lsubu_overflow ret nc or a, a sbc hl, hl ld e, l ret + + .extern __lsubu_overflow