From 4f7a05e3b7eb90b3dad802cc13c64c7267065434 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Thu, 12 Dec 2019 15:53:14 -0500 Subject: [PATCH] core: remove cpHLDE It wasn't used much, so I replaced its use in the kernel with direct code and moved the routine in apps/ed, the only other place where it was used. --- apps/ed/glue.asm | 1 + apps/ed/util.asm | 8 ++++++ kernel/core.asm | 9 ------- kernel/fs.asm | 18 ++++++------- kernel/mmap.asm | 4 ++- tools/tests/unit/test_core.asm | 25 ++---------------- tools/tests/unit/test_ed_util.asm | 42 +++++++++++++++++++++++++++++++ 7 files changed, 65 insertions(+), 42 deletions(-) create mode 100644 apps/ed/util.asm create mode 100644 tools/tests/unit/test_ed_util.asm diff --git a/apps/ed/glue.asm b/apps/ed/glue.asm index 03760f0..942a384 100644 --- a/apps/ed/glue.asm +++ b/apps/ed/glue.asm @@ -33,6 +33,7 @@ .inc "core.asm" .inc "lib/util.asm" .inc "lib/parse.asm" +.inc "ed/util.asm" .equ IO_RAMSTART USER_RAMSTART .inc "ed/io.asm" .equ BUF_RAMSTART IO_RAMEND diff --git a/apps/ed/util.asm b/apps/ed/util.asm new file mode 100644 index 0000000..b23b658 --- /dev/null +++ b/apps/ed/util.asm @@ -0,0 +1,8 @@ +; Compare HL with DE and sets Z and C in the same way as a regular cp X where +; HL is A and DE is X. +cpHLDE: + push hl + or a ;reset carry flag + sbc hl, de ;There is no 'sub hl, de', so we must use sbc + pop hl + ret diff --git a/kernel/core.asm b/kernel/core.asm index cd39c71..4305358 100644 --- a/kernel/core.asm +++ b/kernel/core.asm @@ -57,15 +57,6 @@ intoIX: pop ix ret -; Compare HL with DE and sets Z and C in the same way as a regular cp X where -; HL is A and DE is X. -cpHLDE: - push hl - or a ;reset carry flag - sbc hl, de ;There is no 'sub hl, de', so we must use sbc - pop hl - ret - ; Write the contents of HL in (DE) ; de and hl are preserved, so no pushing/popping necessary writeHLinDE: diff --git a/kernel/fs.asm b/kernel/fs.asm index 24beac0..b574d79 100644 --- a/kernel/fs.asm +++ b/kernel/fs.asm @@ -413,17 +413,17 @@ fsPlaceH: ; Sets Z according to whether HL is within bounds for file handle at (IX), that ; is, if it is smaller than file size. fsWithinBounds: - push de - ; file size - ld e, (ix+4) - ld d, (ix+5) - call cpHLDE - pop de - jr nc, .outOfBounds ; HL >= DE + ld a, h + cp (ix+5) + jr c, .within ; H < (IX+5) + jp nz, unsetZ ; H > (IX+5) + ; H == (IX+5) + ld a, l + cp (ix+4) + jp nc, unsetZ ; L >= (IX+4) +.within: cp a ; ensure Z ret -.outOfBounds: - jp unsetZ ; returns ; Set size of file handle (IX) to value in HL. ; This writes directly in handle's metadata. diff --git a/kernel/mmap.asm b/kernel/mmap.asm index ce332c9..82da26f 100644 --- a/kernel/mmap.asm +++ b/kernel/mmap.asm @@ -13,8 +13,10 @@ _mmapAddr: push de ld de, MMAP_LEN - call cpHLDE + or a ; reset carry flag + sbc hl, de jr nc, .outOfBounds ; HL >= DE + add hl, de ; old HL value ld de, MMAP_START add hl, de cp a ; ensure Z diff --git a/tools/tests/unit/test_core.asm b/tools/tests/unit/test_core.asm index c3929ea..a640ed3 100644 --- a/tools/tests/unit/test_core.asm +++ b/tools/tests/unit/test_core.asm @@ -5,8 +5,6 @@ jp test -.inc "core.asm" - dummyLabel: testNum: .db 1 @@ -48,30 +46,11 @@ test: ; test that "@" is updated by a .org directive ld hl, AFTER_ORG ld de, 0x1234 - call cpHLDE + or a ; clear carry + sbc hl, de jp nz, fail call nexttest - ; *** cpHLDE *** - ld hl, 0x42 - ld de, 0x42 - call cpHLDE - jp nz, fail - jp c, fail - call nexttest - - ld de, 0x4242 - call cpHLDE - jp z, fail - jp nc, fail - call nexttest - - ld hl, 0x4243 - call cpHLDE - jp z, fail - jp c, fail - call nexttest - ; success xor a halt diff --git a/tools/tests/unit/test_ed_util.asm b/tools/tests/unit/test_ed_util.asm new file mode 100644 index 0000000..f7af5af --- /dev/null +++ b/tools/tests/unit/test_ed_util.asm @@ -0,0 +1,42 @@ +jp test + +.inc "ed/util.asm" + +test: + ld sp, 0xffff + + ; *** cpHLDE *** + ld hl, 0x42 + ld de, 0x42 + call cpHLDE + jp nz, fail + jp c, fail + call nexttest + + ld de, 0x4242 + call cpHLDE + jp z, fail + jp nc, fail + call nexttest + + ld hl, 0x4243 + call cpHLDE + jp z, fail + jp c, fail + call nexttest + + ; success + xor a + halt + +testNum: .db 1 + +nexttest: + ld a, (testNum) + inc a + ld (testNum), a + ret + +fail: + ld a, (testNum) + halt