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; +}