From e80d648a2d5441d8462c99932a427c7f6196c248 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Thu, 16 Nov 2017 18:55:10 +0000 Subject: [PATCH] cc: z80 helpers First drafts --- Applications/SmallC/Z80/lib/_isalnum.s | 25 ++++++++++++++ Applications/SmallC/Z80/lib/_isalpha.s | 20 +++++++++++ Applications/SmallC/Z80/lib/_isascii.s | 16 +++++++++ Applications/SmallC/Z80/lib/_isblank.s | 17 ++++++++++ Applications/SmallC/Z80/lib/_iscntrl.s | 17 ++++++++++ Applications/SmallC/Z80/lib/_isdigit.s | 16 +++++++++ Applications/SmallC/Z80/lib/_isgraph.s | 16 +++++++++ Applications/SmallC/Z80/lib/_islower.s | 17 ++++++++++ Applications/SmallC/Z80/lib/_isprint.s | 16 +++++++++ Applications/SmallC/Z80/lib/_ispunct.s | 1 + Applications/SmallC/Z80/lib/_isspace.s | 20 +++++++++++ Applications/SmallC/Z80/lib/_isupper.s | 17 ++++++++++ Applications/SmallC/Z80/lib/_isxdigit.s | 28 ++++++++++++++++ Applications/SmallC/Z80/lib/_memcpy.s | 19 +++++++++++ Applications/SmallC/Z80/lib/_memset.s | 26 +++++++++++++++ Applications/SmallC/Z80/lib/_strcat.s | 25 ++++++++++++++ Applications/SmallC/Z80/lib/_strchr.s | 20 +++++++++++ Applications/SmallC/Z80/lib/_strcmp.s | 28 ++++++++++++++++ Applications/SmallC/Z80/lib/_strcpy.s | 20 +++++++++++ Applications/SmallC/Z80/lib/_strcspn.s | 32 ++++++++++++++++++ Applications/SmallC/Z80/lib/_strlen.s | 17 ++++++++++ Applications/SmallC/Z80/lib/_strncat.s | 44 +++++++++++++++++++++++++ Applications/SmallC/Z80/lib/_strncpy.s | 38 +++++++++++++++++++++ Applications/SmallC/Z80/lib/_strrchr.s | 27 +++++++++++++++ Applications/SmallC/Z80/lib/_strspn.s | 32 ++++++++++++++++++ 25 files changed, 554 insertions(+) create mode 100644 Applications/SmallC/Z80/lib/_isalnum.s create mode 100644 Applications/SmallC/Z80/lib/_isalpha.s create mode 100644 Applications/SmallC/Z80/lib/_isascii.s create mode 100644 Applications/SmallC/Z80/lib/_isblank.s create mode 100644 Applications/SmallC/Z80/lib/_iscntrl.s create mode 100644 Applications/SmallC/Z80/lib/_isdigit.s create mode 100644 Applications/SmallC/Z80/lib/_isgraph.s create mode 100644 Applications/SmallC/Z80/lib/_islower.s create mode 100644 Applications/SmallC/Z80/lib/_isprint.s create mode 100644 Applications/SmallC/Z80/lib/_ispunct.s create mode 100644 Applications/SmallC/Z80/lib/_isspace.s create mode 100644 Applications/SmallC/Z80/lib/_isupper.s create mode 100644 Applications/SmallC/Z80/lib/_isxdigit.s create mode 100644 Applications/SmallC/Z80/lib/_memcpy.s create mode 100644 Applications/SmallC/Z80/lib/_memset.s create mode 100644 Applications/SmallC/Z80/lib/_strcat.s create mode 100644 Applications/SmallC/Z80/lib/_strchr.s create mode 100644 Applications/SmallC/Z80/lib/_strcmp.s create mode 100644 Applications/SmallC/Z80/lib/_strcpy.s create mode 100644 Applications/SmallC/Z80/lib/_strcspn.s create mode 100644 Applications/SmallC/Z80/lib/_strlen.s create mode 100644 Applications/SmallC/Z80/lib/_strncat.s create mode 100644 Applications/SmallC/Z80/lib/_strncpy.s create mode 100644 Applications/SmallC/Z80/lib/_strrchr.s create mode 100644 Applications/SmallC/Z80/lib/_strspn.s diff --git a/Applications/SmallC/Z80/lib/_isalnum.s b/Applications/SmallC/Z80/lib/_isalnum.s new file mode 100644 index 00000000..ccc70ff5 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_isalnum.s @@ -0,0 +1,25 @@ + .code + .export _isalnum + +_isalnum: pop bc + pop hl + push hl + push bc + ld a,l + cp '9' + jr nc, isalnum1 + cp '0' + jr c, ret0 +ret1: ld hl,1 + ret +isalnum1: + cp 'a' + jr c, isalnum2 + sub 32 +isalnum2: + cp 'A' + jr c, ret0 + cp 'Z'+1 + jr c, ret1 +ret0: ld hl,0 + ret diff --git a/Applications/SmallC/Z80/lib/_isalpha.s b/Applications/SmallC/Z80/lib/_isalpha.s new file mode 100644 index 00000000..afe0dd83 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_isalpha.s @@ -0,0 +1,20 @@ + .code + .export _isalpha +_isalpha: pop bc + pop hl + push hl + push bc + ld a,l + cp 'a' + jr c, isalpha1 + sub 32 +isalpha1: + cp 'A' + jr c, ret0 + cp 'Z'+1 + jr c, ret1 +ret0: ld hl,0 + ret + ; No need to handle -1 specially +ret1: ld hl,1 + ret diff --git a/Applications/SmallC/Z80/lib/_isascii.s b/Applications/SmallC/Z80/lib/_isascii.s new file mode 100644 index 00000000..8f1142b7 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_isascii.s @@ -0,0 +1,16 @@ + .code + .export _isascii +_isascii: pop bc + pop hl + push hl + push bc + ld a,l + cp 32 + jr c, ret0 + rlca + jr c, ret0 + ; No need to handle -1 specially +ret1: ld hl,1 + ret +ret0: ld hl,0 + ret diff --git a/Applications/SmallC/Z80/lib/_isblank.s b/Applications/SmallC/Z80/lib/_isblank.s new file mode 100644 index 00000000..134d9566 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_isblank.s @@ -0,0 +1,17 @@ + .code + .export _isblank +_isblank: pop bc + pop hl + push hl + push bc + ld a,l + ; Space + cp 32 + jr z, ret1 + cp 9 + jr z, ret1 +ret0: ld hl,0 + ret + ; No need to handle -1 specially +ret1: ld hl,1 + ret diff --git a/Applications/SmallC/Z80/lib/_iscntrl.s b/Applications/SmallC/Z80/lib/_iscntrl.s new file mode 100644 index 00000000..00c93c55 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_iscntrl.s @@ -0,0 +1,17 @@ + .code + .export _iscntrl +_iscntrl: pop bc + pop hl + push hl + push bc + ld a,l + ; Space + cp 32 + jr c, ret1 + cp 127 + jr z, ret1 +ret0: ld hl,0 + ret + ; No need to handle -1 specially +ret1: ld hl,1 + ret diff --git a/Applications/SmallC/Z80/lib/_isdigit.s b/Applications/SmallC/Z80/lib/_isdigit.s new file mode 100644 index 00000000..283418ef --- /dev/null +++ b/Applications/SmallC/Z80/lib/_isdigit.s @@ -0,0 +1,16 @@ + .code + .export _isdigit +_isdigit: pop bc + pop hl + push hl + push bc + ld a,l + cp '0' + jr c, ret0 + cp '9'+1 + jr c, ret1 +ret0: ld hl,0 + ret + ; No need to handle -1 specially +ret1: ld hl,1 + ret diff --git a/Applications/SmallC/Z80/lib/_isgraph.s b/Applications/SmallC/Z80/lib/_isgraph.s new file mode 100644 index 00000000..e1dedbd4 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_isgraph.s @@ -0,0 +1,16 @@ + .code + .export _isgraph +_isgraph: pop bc + pop hl + push hl + push bc + ld a,l + cp 33 + jr c, ret0 + cp 127 + jr nc, ret1 +ret0: ld hl,0 + ret + ; No need to handle -1 specially +ret1: ld hl,1 + ret diff --git a/Applications/SmallC/Z80/lib/_islower.s b/Applications/SmallC/Z80/lib/_islower.s new file mode 100644 index 00000000..8ff8dc94 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_islower.s @@ -0,0 +1,17 @@ + .code + .export _islower +_islower: pop bc + pop hl + push hl + push bc + ld a,l + ; Space + cp 'a' + jr c, ret0 + cp 'z'+1 + jr c, ret1 +ret0: ld hl,0 + ret + ; No need to handle -1 specially +ret1: ld hl,1 + ret diff --git a/Applications/SmallC/Z80/lib/_isprint.s b/Applications/SmallC/Z80/lib/_isprint.s new file mode 100644 index 00000000..c5a78f48 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_isprint.s @@ -0,0 +1,16 @@ + .code + .export _isprint +_isprint: pop bc + pop hl + push hl + push bc + ld a,l + cp 32 + jr c, ret0 + cp 128 + jr c, ret1 +ret0: ld hl,0 + ret + ; No need to handle -1 specially +ret1: ld hl,1 + ret diff --git a/Applications/SmallC/Z80/lib/_ispunct.s b/Applications/SmallC/Z80/lib/_ispunct.s new file mode 100644 index 00000000..969bf850 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_ispunct.s @@ -0,0 +1 @@ +ispunct.s \ No newline at end of file diff --git a/Applications/SmallC/Z80/lib/_isspace.s b/Applications/SmallC/Z80/lib/_isspace.s new file mode 100644 index 00000000..ac0c6ac5 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_isspace.s @@ -0,0 +1,20 @@ + .code + .export _isspace +_isspace: pop bc + pop hl + push hl + push bc + ld a,l + ; Space + cp 32 + jr z, ret1 + ; 9-13 + cp 9 + jr c, ret0 + cp 13 + jr nc, ret0 + ; No need to handle -1 specially +ret1: ld hl,1 + ret +ret0: ld hl,0 + ret diff --git a/Applications/SmallC/Z80/lib/_isupper.s b/Applications/SmallC/Z80/lib/_isupper.s new file mode 100644 index 00000000..810e0b38 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_isupper.s @@ -0,0 +1,17 @@ + .code + .export _isupper +_isupper: pop bc + pop hl + push hl + push bc + ld a,l + ; Space + cp 'A' + jr c, ret0 + cp 'Z'+1 + jr c, ret1 +ret0: ld hl,0 + ret + ; No need to handle -1 specially +ret1: ld hl,1 + ret diff --git a/Applications/SmallC/Z80/lib/_isxdigit.s b/Applications/SmallC/Z80/lib/_isxdigit.s new file mode 100644 index 00000000..b3a6878e --- /dev/null +++ b/Applications/SmallC/Z80/lib/_isxdigit.s @@ -0,0 +1,28 @@ + .code + .global _isxdigit +_isxdigit: pop bc + pop hl + push hl + push bc + ld a,l + ; Space + cp '0' + jr c, ret0 + cp 'A' + jr nc,ascii1 + cp 'a' + jr nc,ascii2 + cp '9'+1 + jr nc, ret0 + jr ret1 +ascii1: add 32 +ascii2: + cp 'a' + jr c, ret0 + cp 'z'+1 + jr c, ret1 +ret0: ld hl,0 + ret + ; No need to handle -1 specially +ret1: ld hl,1 + ret diff --git a/Applications/SmallC/Z80/lib/_memcpy.s b/Applications/SmallC/Z80/lib/_memcpy.s new file mode 100644 index 00000000..e9781266 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_memcpy.s @@ -0,0 +1,19 @@ + .code + .export _memcpy + +_memcpy: push ix + ld ix,#0 + add ix,sp + ld e,4(ix) + ld d,5(ix) + ld l,6(ix) + ld h,7(ix) + ld c,8(ix) + ld b,9(ix) + ld a,b + or c + jr z, _memcpy_none + ldir +_memcpy_none: ld l,4(ix) + ld h,5(ix) + ret diff --git a/Applications/SmallC/Z80/lib/_memset.s b/Applications/SmallC/Z80/lib/_memset.s new file mode 100644 index 00000000..b1f4ac72 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_memset.s @@ -0,0 +1,26 @@ + .code + .export _memset + +_memset: push ix + ld ix,0 + add ix,sp + ld h,4(ix) + ld l,5(ix) + ld e,6(ix) + ld c,8(ix) + ld b,9(ix) + ld a,b + or c + jr z, _memset_none + ld (hl),e + dec bc + ld a,b + or c + jr z,_memset_none + ld e,l + ld d,h + inc de + ldir +_memset_none: ld l,4(ix) + ld h,5(ix) + ret diff --git a/Applications/SmallC/Z80/lib/_strcat.s b/Applications/SmallC/Z80/lib/_strcat.s new file mode 100644 index 00000000..7764f389 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_strcat.s @@ -0,0 +1,25 @@ + .code + .export _strcat + +_strcat: pop bc ;ret + pop de ;dest + pop hl ;src + push hl + push de + push bc + push de ; for return code +_strcat_1: ld a,(de) + or a + jr z,_strcat_2 + inc de + jr _strcat_1 +_strcat_2: + ld a,(hl) + ld (de),a + or a + jr z _strcat_3 + inc hl + inc de + jr _strcat_2 +strcat_3: pop de + ret diff --git a/Applications/SmallC/Z80/lib/_strchr.s b/Applications/SmallC/Z80/lib/_strchr.s new file mode 100644 index 00000000..de206f14 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_strchr.s @@ -0,0 +1,20 @@ + .code + .export _strchr + +_strchr: + pop de ; char + pop hl ; ptr + pop bc + push bc + push hl + push de +_strchr_1: ld a,(hl) ; "The terminting nul is considered part + cp e ; of the string" + ret z + or a + inc hl + jr nz,_strchr_1 + ld hl,0 + ret + + \ No newline at end of file diff --git a/Applications/SmallC/Z80/lib/_strcmp.s b/Applications/SmallC/Z80/lib/_strcmp.s new file mode 100644 index 00000000..4a666f46 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_strcmp.s @@ -0,0 +1,28 @@ + .code + .export _strcmp + +_strcmp: + pop bc + pop de + pop hl + push hl + push de + +_strcmp_1: + ld a,(de) + cp (hl) + jr nz, strcmp_2 + or a + jr z, strcmp_4 + inc hl + inc de + jr _strcmp_1 +_strcmp_2: jr c,_strcmp_3 + ld hl,1 + ret +_strcmp_3: + ld hl,0xFFFF + ret +_strcmp_4: + ld hl,0 + ret diff --git a/Applications/SmallC/Z80/lib/_strcpy.s b/Applications/SmallC/Z80/lib/_strcpy.s new file mode 100644 index 00000000..e1ce6114 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_strcpy.s @@ -0,0 +1,20 @@ + .code + .export _strcpy + +_strcpy: pop bc ;ret + pop de ;dest + pop hl ;src + push hl + push de + push bc + push de ; for return code +_strcpy_1: ld a,(hl) + ld (de),a + or a + jr z _strcpy_2 + inc hl + inc de + jr _strcpy_1 +_strcpy_2: + pop hl + ret diff --git a/Applications/SmallC/Z80/lib/_strcspn.s b/Applications/SmallC/Z80/lib/_strcspn.s new file mode 100644 index 00000000..0b77ff08 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_strcspn.s @@ -0,0 +1,32 @@ + .code + .export _strcspn + +_strcspn: + pop bc + pop hl ; string + pop de ; match + push de + push hl + push bc + +_strcspn_1: + ld a,(hl) + or a + jr z,_strcspn_4 + ld c,a + + push de +_strcspn_2: + ld a,(de) + or a + jr z, _strcspn_3 + cp c + ret z ; Matched HL ponts to right spot + inc de + jr _strcspn_2 +_strcpsn_3: pop de + inc hl + jr _strcspn_1 +_strcspn_4: ld h,a ; A always 0 here + ld l,a + ret diff --git a/Applications/SmallC/Z80/lib/_strlen.s b/Applications/SmallC/Z80/lib/_strlen.s new file mode 100644 index 00000000..a45546af --- /dev/null +++ b/Applications/SmallC/Z80/lib/_strlen.s @@ -0,0 +1,17 @@ + .code + .export _strlen + +_strlen: + pop hl + pop de + push de + push hl + xor a + ld b,a + ld c,a + ld e,l + ld d,h + cpir + dec hl + sbc hl,de + ret diff --git a/Applications/SmallC/Z80/lib/_strncat.s b/Applications/SmallC/Z80/lib/_strncat.s new file mode 100644 index 00000000..bf649ebf --- /dev/null +++ b/Applications/SmallC/Z80/lib/_strncat.s @@ -0,0 +1,44 @@ + .code + .export _strncat + +_strncat: + push ix + ld ix,#0 + add ix,sp + ld l,4(ix) + ld h,5(ix) + xor a + ld b,a + ld c,a + ; Move past end of original string + cpir + dec hl + ld e,6(ix) + ld d,7(ix) + ld c,8(ix) + ld b,9(ix) + ; Copy bytes until limit or \0 +_strncat_1: + ld a,b + or c + jr z, _strncat_2 + ld a,(de) + ld (hl),a + or a + jr z, _strncat_3 + inc de + inc hl + dec bc + jr _strncat_1 +_strncat_2: ld l,4(ix) + ld h,5(ix) + pop ix + ret + ; Copy \0 until limit +_strncat_3: inc hl + dec bc + ld a,b + or c + jr z,_strncat_2 + ld (hl),0 + jr _strncat_3 diff --git a/Applications/SmallC/Z80/lib/_strncpy.s b/Applications/SmallC/Z80/lib/_strncpy.s new file mode 100644 index 00000000..317afa97 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_strncpy.s @@ -0,0 +1,38 @@ + .code + .export _strncpy + +_strncpy: + push ix + ld ix,#0 + add ix,sp + ld l,4(ix) + ld h,5(ix) + ld e,6(ix) + ld d,7(ix) + ld c,8(ix) + ld b,9(ix) + ; Copy bytes until limit or \0 +_strncpy_1: + ld a,b + or c + jr z, _strncpy_2 + ld a,(de) + ld (hl),a + or a + jr z, _strncpy_3 + inc de + inc hl + dec bc + jr _strncpy_1 +_strncpy_2: ld l,4(ix) + ld h,5(ix) + pop ix + ret + ; Copy \0 until limit +_strncpy_3: inc hl + dec bc + ld a,b + or c + jr z,_strncpy_2 + ld (hl),0 + jr _strncpy_3 diff --git a/Applications/SmallC/Z80/lib/_strrchr.s b/Applications/SmallC/Z80/lib/_strrchr.s new file mode 100644 index 00000000..b970d5c2 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_strrchr.s @@ -0,0 +1,27 @@ + .code + .export _strrchr + +_strrchr: + pop de ; char + pop hl ; ptr + pop bc + push bc + push hl + push de + ld bc, 0 +_strrchr_1: ld a,(hl) + or a + jr z, _strrchr_2 + cp e + jr z, _strrchr_3 + inc hl + jr _strrchr_1 +_strrchr_2: + ld h,b + ld l,c + ret +_strrchr_3: + ld b,h + ld c,l + jr _strrchr_1 + \ No newline at end of file diff --git a/Applications/SmallC/Z80/lib/_strspn.s b/Applications/SmallC/Z80/lib/_strspn.s new file mode 100644 index 00000000..0b77ff08 --- /dev/null +++ b/Applications/SmallC/Z80/lib/_strspn.s @@ -0,0 +1,32 @@ + .code + .export _strcspn + +_strcspn: + pop bc + pop hl ; string + pop de ; match + push de + push hl + push bc + +_strcspn_1: + ld a,(hl) + or a + jr z,_strcspn_4 + ld c,a + + push de +_strcspn_2: + ld a,(de) + or a + jr z, _strcspn_3 + cp c + ret z ; Matched HL ponts to right spot + inc de + jr _strcspn_2 +_strcpsn_3: pop de + inc hl + jr _strcspn_1 +_strcspn_4: ld h,a ; A always 0 here + ld l,a + ret -- 2.34.1