endian.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* If we're being compiled as a .c file, rather than being included in
  2. d10v_sim.h, then ENDIAN_INLINE won't be defined yet. */
  3. /* This must come before any other includes. */
  4. #include "defs.h"
  5. #ifndef ENDIAN_INLINE
  6. #define NO_ENDIAN_INLINE
  7. #include "sim-main.h"
  8. #define ENDIAN_INLINE
  9. #endif
  10. ENDIAN_INLINE uint16_t
  11. get_word (uint8_t *x)
  12. {
  13. return ((uint16_t)x[0]<<8) + x[1];
  14. }
  15. ENDIAN_INLINE uint32_t
  16. get_longword (uint8_t *x)
  17. {
  18. return ((uint32_t)x[0]<<24) + ((uint32_t)x[1]<<16) + ((uint32_t)x[2]<<8) + ((uint32_t)x[3]);
  19. }
  20. ENDIAN_INLINE int64_t
  21. get_longlong (uint8_t *x)
  22. {
  23. uint32_t top = get_longword (x);
  24. uint32_t bottom = get_longword (x+4);
  25. return (((int64_t)top)<<32) | (int64_t)bottom;
  26. }
  27. ENDIAN_INLINE void
  28. write_word (uint8_t *addr, uint16_t data)
  29. {
  30. addr[0] = (data >> 8) & 0xff;
  31. addr[1] = data & 0xff;
  32. }
  33. ENDIAN_INLINE void
  34. write_longword (uint8_t *addr, uint32_t data)
  35. {
  36. addr[0] = (data >> 24) & 0xff;
  37. addr[1] = (data >> 16) & 0xff;
  38. addr[2] = (data >> 8) & 0xff;
  39. addr[3] = data & 0xff;
  40. }
  41. ENDIAN_INLINE void
  42. write_longlong (uint8_t *addr, int64_t data)
  43. {
  44. write_longword (addr, (uint32_t)(data >> 32));
  45. write_longword (addr+4, (uint32_t)data);
  46. }