From dfe7562f76185b141795f1db1bad905a5f4b1acc Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Wed, 6 Sep 2023 08:09:00 +0200 Subject: [PATCH 1/5] Add ntohs, htons, ntohl, htons. --- doc/funcref.sgml | 87 +++++++++++++++++++++++++++++++++++++ include/arpa/inet.h | 10 +++++ libsrc/common/ntohl.s | 34 +++++++++++++++ libsrc/common/ntohs.s | 18 ++++++++ test/val/lib_common_htonl.c | 34 +++++++++++++++ test/val/lib_common_htons.c | 34 +++++++++++++++ 6 files changed, 217 insertions(+) create mode 100644 include/arpa/inet.h create mode 100644 libsrc/common/ntohl.s create mode 100644 libsrc/common/ntohs.s create mode 100644 test/val/lib_common_htonl.c create mode 100644 test/val/lib_common_htons.c diff --git a/doc/funcref.sgml b/doc/funcref.sgml index 2a6d77adc..ae25851e8 100644 --- a/doc/funcref.sgml +++ b/doc/funcref.sgml @@ -435,6 +435,16 @@ see also testcode/lib/em-test.c and samples/multidemo.c. (incomplete) +

+ + + + + + + + +

. @@ -4388,6 +4398,45 @@ to undefined behaviour. +htonl

+ + + +/ + +The function is only available as fastcall function, so it may only +be used in presence of a prototype. + + + + + + +htons

+ + + +/ + +The function is only available as fastcall function, so it may only +be used in presence of a prototype. + + + + + isalnum

@@ -5757,6 +5806,44 @@ memory allocated for the driver. +ntohl

+ + + +/ + +The function is only available as fastcall function, so it may only +be used in presence of a prototype. + + + + + +ntohs

+ + + +/ + +The function is only available as fastcall function, so it may only +be used in presence of a prototype. + + + + + offsetof

diff --git a/include/arpa/inet.h b/include/arpa/inet.h new file mode 100644 index 000000000..ddcea2446 --- /dev/null +++ b/include/arpa/inet.h @@ -0,0 +1,10 @@ +#ifndef _ARPA_INET_H +#define _ARPA_INET_H + +int __fastcall__ ntohs (int val); +int __fastcall__ htons (int val); + +long __fastcall__ ntohl (long val); +long __fastcall__ htonl (long val); + +#endif diff --git a/libsrc/common/ntohl.s b/libsrc/common/ntohl.s new file mode 100644 index 000000000..6bf959b95 --- /dev/null +++ b/libsrc/common/ntohl.s @@ -0,0 +1,34 @@ +; +; Colin Leroy-Mira , 2023-09-06 +; +; int __fastcall__ ntohl (long val); +; + +.export _ntohl, _htonl +.import popa +.importzp tmp1, tmp2, sreg + +_htonl := _ntohl +.code + +_ntohl: + ; The parts of our 32 bit word + ; are in sreg+1, sreg, X, A. + + + ; Save A and X + stx tmp1 + sta tmp2 + + ; Invert high word + lda sreg+1 + ldx sreg + + ; Invert low word + ldy tmp1 + sty sreg + + ldy tmp2 + sty sreg+1 + + rts diff --git a/libsrc/common/ntohs.s b/libsrc/common/ntohs.s new file mode 100644 index 000000000..e1914770e --- /dev/null +++ b/libsrc/common/ntohs.s @@ -0,0 +1,18 @@ +; +; Colin Leroy-Mira , 2023-09-06 +; +; int __fastcall__ ntohs (int val); +; + +.export _ntohs, _htons +.importzp tmp1 + +_htons := _ntohs + +.code + +_ntohs: + sta tmp1 + txa + ldx tmp1 + rts diff --git a/test/val/lib_common_htonl.c b/test/val/lib_common_htonl.c new file mode 100644 index 000000000..53a210a84 --- /dev/null +++ b/test/val/lib_common_htonl.c @@ -0,0 +1,34 @@ +/* + !!DESCRIPTION!! A small test for htons. + !!ORIGIN!! + !!LICENCE!! + !!AUTHOR!! Colin Leroy-Mira +*/ + +#include +#include +#include + +static unsigned int Failures = 0; + +static void CheckHtonl (long input, long expected) +{ + long result = htonl(input); + if (result != expected) { + printf ("htonl error:\n" + " result = %ld for %ld, should be %ld\n", result, input, expected); + ++Failures; + } +} + +int main (void) +{ + CheckHtonl(0x00000000, 0x00000000); + CheckHtonl(0x12345678, 0x78563412); + CheckHtonl(0xAABBCCDD, 0xDDCCBBAA); + CheckHtonl(0xFFFFFFFF, 0xFFFFFFFF); + + printf ("Failures: %u\n", Failures); + + return Failures; +} diff --git a/test/val/lib_common_htons.c b/test/val/lib_common_htons.c new file mode 100644 index 000000000..42bbb3d6b --- /dev/null +++ b/test/val/lib_common_htons.c @@ -0,0 +1,34 @@ +/* + !!DESCRIPTION!! A small test for htons. + !!ORIGIN!! + !!LICENCE!! + !!AUTHOR!! Colin Leroy-Mira +*/ + +#include +#include +#include + +static unsigned int Failures = 0; + +static void CheckHtons (int input, int expected) +{ + int result = htons(input); + if (result != expected) { + printf ("htons error:\n" + " result = %d for %d, should be %d\n", result, input, expected); + ++Failures; + } +} + +int main (void) +{ + CheckHtons(0x0000, 0x0000); + CheckHtons(0x1234, 0x3412); + CheckHtons(0xA0F2, 0xF2A0); + CheckHtons(0xFFFF, 0xFFFF); + + printf ("Failures: %u\n", Failures); + + return Failures; +} From a90aa575105b5a1f33c99e7feab8d1921aee99d4 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Thu, 7 Sep 2023 16:48:56 +0200 Subject: [PATCH 2/5] Address Oliver's comments --- doc/funcref.sgml | 8 ++++---- include/arpa/inet.h | 30 ++++++++++++++++++++++++++++++ libsrc/common/ntohl.s | 2 -- libsrc/common/ntohs.s | 2 -- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/doc/funcref.sgml b/doc/funcref.sgml index ae25851e8..5fa1720e4 100644 --- a/doc/funcref.sgml +++ b/doc/funcref.sgml @@ -4403,7 +4403,7 @@ to undefined behaviour. / +/ / +/ / +/ / +/ */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + #ifndef _ARPA_INET_H #define _ARPA_INET_H diff --git a/libsrc/common/ntohl.s b/libsrc/common/ntohl.s index 6bf959b95..77adba253 100644 --- a/libsrc/common/ntohl.s +++ b/libsrc/common/ntohl.s @@ -9,13 +9,11 @@ .importzp tmp1, tmp2, sreg _htonl := _ntohl -.code _ntohl: ; The parts of our 32 bit word ; are in sreg+1, sreg, X, A. - ; Save A and X stx tmp1 sta tmp2 diff --git a/libsrc/common/ntohs.s b/libsrc/common/ntohs.s index e1914770e..042ddb005 100644 --- a/libsrc/common/ntohs.s +++ b/libsrc/common/ntohs.s @@ -9,8 +9,6 @@ _htons := _ntohs -.code - _ntohs: sta tmp1 txa From cbc5603d631cff1f1ba8aaa78909c56bbd1387f9 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Thu, 7 Sep 2023 21:00:30 +0200 Subject: [PATCH 3/5] Inline ntohs/htons as a macro if -i is passed --- include/arpa/inet.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/arpa/inet.h b/include/arpa/inet.h index e1e970e07..2fb47388d 100644 --- a/include/arpa/inet.h +++ b/include/arpa/inet.h @@ -31,8 +31,22 @@ #ifndef _ARPA_INET_H #define _ARPA_INET_H +#ifndef __OPT_i__ int __fastcall__ ntohs (int val); int __fastcall__ htons (int val); +#else + +#define ntohs(x) \ + ( \ + __AX__=(x), \ + asm("sta tmp1"), \ + asm("txa"), \ + asm("ldx tmp1"), \ + __AX__ \ + ) +#define htons(x) ntohs(x) + +#endif long __fastcall__ ntohl (long val); long __fastcall__ htonl (long val); From 9669710cc3bd047b8a3a4a8d0e5490c31d4fd5bf Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Thu, 7 Sep 2023 21:36:39 +0200 Subject: [PATCH 4/5] Fix usage of __OPT_i__ --- include/arpa/inet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/arpa/inet.h b/include/arpa/inet.h index 2fb47388d..5f4b994e8 100644 --- a/include/arpa/inet.h +++ b/include/arpa/inet.h @@ -31,7 +31,7 @@ #ifndef _ARPA_INET_H #define _ARPA_INET_H -#ifndef __OPT_i__ +#if (__OPT_i__ < 200) int __fastcall__ ntohs (int val); int __fastcall__ htons (int val); #else From 9e5620f127c636afe0e2c3901ca5cbfdf39fa96e Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Thu, 7 Sep 2023 22:37:30 +0200 Subject: [PATCH 5/5] Fix coding-style on header --- include/arpa/inet.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/arpa/inet.h b/include/arpa/inet.h index 5f4b994e8..cd353a2bb 100644 --- a/include/arpa/inet.h +++ b/include/arpa/inet.h @@ -28,9 +28,19 @@ /* */ /*****************************************************************************/ + + #ifndef _ARPA_INET_H #define _ARPA_INET_H + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + #if (__OPT_i__ < 200) int __fastcall__ ntohs (int val); int __fastcall__ htons (int val); @@ -51,4 +61,7 @@ int __fastcall__ htons (int val); long __fastcall__ ntohl (long val); long __fastcall__ htonl (long val); + + +/* End of arpa/inet.h */ #endif