fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
at master 139 lines 3.5 kB view raw
1/***************************************************************************** 2 * pce * 3 *****************************************************************************/ 4 5/***************************************************************************** 6 * File name: src/lib/endian.h * 7 * Created: 2022-02-12 by Hampa Hug <hampa@hampa.ch> * 8 * Copyright: (C) 2022 Hampa Hug <hampa@hampa.ch> * 9 *****************************************************************************/ 10 11/***************************************************************************** 12 * This program is free software. You can redistribute it and / or modify it * 13 * under the terms of the GNU General Public License version 2 as published * 14 * by the Free Software Foundation. * 15 * * 16 * This program is distributed in the hope that it will be useful, but * 17 * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 * Public License for more details. * 20 *****************************************************************************/ 21 22 23#ifndef PCE_LIB_ENDIAN_H 24#define PCE_LIB_ENDIAN_H 1 25 26 27#include <stdint.h> 28 29 30static inline 31void set_uint8 (void *buf, unsigned idx, uint8_t val) 32{ 33 uint8_t *p = (uint8_t *) buf + idx; 34 35 p[0] = val & 0xff; 36} 37 38static inline 39void set_uint16_be (void *buf, unsigned idx, uint16_t val) 40{ 41 uint8_t *p = (uint8_t *) buf + idx; 42 43 p[0] = (val >> 8) & 0xff; 44 p[1] = val & 0xff; 45} 46 47static inline 48void set_uint16_le (void *buf, unsigned idx, uint16_t val) 49{ 50 uint8_t *p = (uint8_t *) buf + idx; 51 52 p[0] = val & 0xff; 53 p[1] = (val >> 8) & 0xff; 54} 55 56static inline 57void set_uint32_be (void *buf, unsigned idx, uint32_t val) 58{ 59 uint8_t *p = (uint8_t *) buf + idx; 60 61 p[0] = (val >> 24) & 0xff; 62 p[1] = (val >> 16) & 0xff; 63 p[2] = (val >> 8) & 0xff; 64 p[3] = val & 0xff; 65} 66 67static inline 68void set_uint32_le (void *buf, unsigned idx, uint32_t val) 69{ 70 uint8_t *p = (uint8_t *) buf + idx; 71 72 p[0] = val & 0xff; 73 p[1] = (val >> 8) & 0xff; 74 p[2] = (val >> 16) & 0xff; 75 p[3] = (val >> 24) & 0xff; 76} 77 78static inline 79uint8_t get_uint8 (const void *buf, unsigned idx) 80{ 81 const uint8_t *p = (const uint8_t *) buf + idx; 82 83 return (*p & 0xff); 84} 85 86static inline 87uint16_t get_uint16_be (const void *buf, unsigned idx) 88{ 89 uint16_t val; 90 const uint8_t *p = (const uint8_t *) buf + idx; 91 92 val = p[0] & 0xff; 93 val = (val << 8) | (p[1] & 0xff); 94 95 return (val); 96} 97 98static inline 99uint16_t get_uint16_le (const void *buf, unsigned idx) 100{ 101 uint16_t val; 102 const uint8_t *p = (const uint8_t *) buf + idx; 103 104 val = p[1] & 0xff; 105 val = (val << 8) | (p[0] & 0xff); 106 107 return (val); 108} 109 110static inline 111uint32_t get_uint32_be (const void *buf, unsigned idx) 112{ 113 uint32_t val; 114 const uint8_t *p = (const uint8_t *) buf + idx; 115 116 val = p[0] & 0xff; 117 val = (val << 8) | (p[1] & 0xff); 118 val = (val << 8) | (p[2] & 0xff); 119 val = (val << 8) | (p[3] & 0xff); 120 121 return (val); 122} 123 124static inline 125uint32_t get_uint32_le (const void *buf, unsigned idx) 126{ 127 uint32_t val; 128 const uint8_t *p = (const uint8_t *) buf + idx; 129 130 val = p[3] & 0xff; 131 val = (val << 8) | (p[2] & 0xff); 132 val = (val << 8) | (p[1] & 0xff); 133 val = (val << 8) | (p[0] & 0xff); 134 135 return (val); 136} 137 138 139#endif