summaryrefslogtreecommitdiff
path: root/cpu/cpu-byteorder.c
diff options
context:
space:
mode:
Diffstat (limited to 'cpu/cpu-byteorder.c')
-rw-r--r--cpu/cpu-byteorder.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/cpu/cpu-byteorder.c b/cpu/cpu-byteorder.c
new file mode 100644
index 0000000..ebc612a
--- /dev/null
+++ b/cpu/cpu-byteorder.c
@@ -0,0 +1,116 @@
+/***************************************************************
+ __CPU_BYTEORDER.C
+
+ Byte-order accessors for canonical PROGRAM fields and
+ architectural data-object representation.
+
+ PART OF : CPU - library .
+
+ Copyright (C) 2026 by Andrew V.Kosteltsev.
+ All Rights Reserved.
+ ***************************************************************/
+
+#include <string.h>
+#include <libmcpu.h>
+
+__mpu_uint16_t
+__cpu_data_get_u16( const __mpu_UNIT_t *p )
+{
+ __mpu_uint16_t v = 0;
+ if( p ) memcpy( &v, p, sizeof(v) );
+ return( v );
+}
+
+__mpu_uint32_t
+__cpu_data_get_u32( const __mpu_UNIT_t *p )
+{
+ __mpu_uint32_t v = 0;
+ if( p ) memcpy( &v, p, sizeof(v) );
+ return( v );
+}
+
+__mpu_uint64_t
+__cpu_data_get_u64( const __mpu_UNIT_t *p )
+{
+ __mpu_uint64_t v = 0;
+ if( p ) memcpy( &v, p, sizeof(v) );
+ return( v );
+}
+
+void
+__cpu_data_put_u16( __mpu_UNIT_t *p, __mpu_uint16_t v )
+{
+ if( p ) memcpy( p, &v, sizeof(v) );
+}
+
+void
+__cpu_data_put_u32( __mpu_UNIT_t *p, __mpu_uint32_t v )
+{
+ if( p ) memcpy( p, &v, sizeof(v) );
+}
+
+void
+__cpu_data_put_u64( __mpu_UNIT_t *p, __mpu_uint64_t v )
+{
+ if( p ) memcpy( p, &v, sizeof(v) );
+}
+
+__mpu_uint16_t
+__cpu_code_get_u16( const __mpu_UNIT_t *p )
+{
+ if( !p ) return( 0 );
+ return (__mpu_uint16_t)(((__mpu_uint16_t)p[0] << 8) |
+ (__mpu_uint16_t)p[1]);
+}
+
+__mpu_uint32_t
+__cpu_code_get_u32( const __mpu_UNIT_t *p )
+{
+ __mpu_uint32_t v;
+ if( !p ) return( 0 );
+ v = ((__mpu_uint32_t)p[0] << 24);
+ v |= ((__mpu_uint32_t)p[1] << 16);
+ v |= ((__mpu_uint32_t)p[2] << 8);
+ v |= (__mpu_uint32_t)p[3];
+ return( v );
+}
+
+__mpu_uint64_t
+__cpu_code_get_u64( const __mpu_UNIT_t *p )
+{
+ __mpu_uint64_t v = 0;
+ int k;
+ if( !p ) return( 0 );
+ for( k = 0; k < 8; ++k ) v = ( v << 8 ) | p[k];
+ return( v );
+}
+
+void
+__cpu_code_put_u16( __mpu_UNIT_t *p, __mpu_uint16_t v )
+{
+ if( !p ) return;
+ p[0] = (__mpu_UNIT_t)(v >> 8);
+ p[1] = (__mpu_UNIT_t)v;
+}
+
+void
+__cpu_code_put_u32( __mpu_UNIT_t *p, __mpu_uint32_t v )
+{
+ if( !p ) return;
+ p[0] = (__mpu_UNIT_t)(v >> 24);
+ p[1] = (__mpu_UNIT_t)(v >> 16);
+ p[2] = (__mpu_UNIT_t)(v >> 8);
+ p[3] = (__mpu_UNIT_t)v;
+}
+
+void
+__cpu_code_put_u64( __mpu_UNIT_t *p, __mpu_uint64_t v )
+{
+ int k;
+ if( !p ) return;
+ for( k = 7; k >= 0; --k )
+ {
+ p[k] = (__mpu_UNIT_t)( v & __mpu_UINT64_C( 0xff ) );
+ v >>= 8;
+ }
+}