x86emu_util.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* $OpenBSD: x86emu_util.c,v 1.6 2015/03/14 03:38:50 jsg Exp $ */
  2. /* $NetBSD: x86emu_util.c,v 1.2 2007/12/04 17:32:22 joerg Exp $ */
  3. /*
  4. *
  5. * Realmode X86 Emulator Library
  6. *
  7. * Copyright (C) 1996-1999 SciTech Software, Inc.
  8. * Copyright (C) David Mosberger-Tang
  9. * Copyright (C) 1999 Egbert Eich
  10. * Copyright (C) 2007 Joerg Sonnenberger
  11. *
  12. * ========================================================================
  13. *
  14. * Permission to use, copy, modify, distribute, and sell this software and
  15. * its documentation for any purpose is hereby granted without fee,
  16. * provided that the above copyright notice appear in all copies and that
  17. * both that copyright notice and this permission notice appear in
  18. * supporting documentation, and that the name of the authors not be used
  19. * in advertising or publicity pertaining to distribution of the software
  20. * without specific, written prior permission. The authors makes no
  21. * representations about the suitability of this software for any purpose.
  22. * It is provided "as is" without express or implied warranty.
  23. *
  24. * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  25. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  26. * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  27. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  28. * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  29. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  30. * PERFORMANCE OF THIS SOFTWARE.
  31. *
  32. */
  33. #include <sys/param.h>
  34. #include <sys/endian.h>
  35. #include <dev/x86emu/x86emu.h>
  36. /*
  37. * PARAMETERS:
  38. * addr - Emulator memory address to read
  39. *
  40. * RETURNS:
  41. * Byte value read from emulator memory.
  42. *
  43. * REMARKS:
  44. * Reads a byte value from the emulator memory.
  45. */
  46. static uint8_t
  47. rdb(struct x86emu *emu, uint32_t addr)
  48. {
  49. if (addr > emu->mem_size - 1)
  50. x86emu_halt_sys(emu);
  51. return emu->mem_base[addr];
  52. }
  53. /*
  54. * PARAMETERS:
  55. * addr - Emulator memory address to read
  56. *
  57. * RETURNS:
  58. * Word value read from emulator memory.
  59. *
  60. * REMARKS:
  61. * Reads a word value from the emulator memory.
  62. */
  63. static uint16_t
  64. rdw(struct x86emu *emu, uint32_t addr)
  65. {
  66. if (addr > emu->mem_size - 2)
  67. x86emu_halt_sys(emu);
  68. #ifdef __STRICT_ALIGNMENT
  69. if (addr & 1) {
  70. u_int8_t *a = emu->mem_base + addr;
  71. u_int16_t r;
  72. r = ((*(a + 0) << 0) & 0x00ff) |
  73. ((*(a + 1) << 8) & 0xff00);
  74. return r;
  75. } else
  76. return letoh32(*(u_int32_t *)(emu->mem_base + addr));
  77. #else
  78. return letoh16(*(u_int16_t *)(emu->mem_base + addr));
  79. #endif
  80. }
  81. /*
  82. * PARAMETERS:
  83. * addr - Emulator memory address to read
  84. *
  85. * RETURNS:
  86. * Long value read from emulator memory.
  87. * REMARKS:
  88. * Reads a long value from the emulator memory.
  89. */
  90. static uint32_t
  91. rdl(struct x86emu *emu, uint32_t addr)
  92. {
  93. if (addr > emu->mem_size - 4)
  94. x86emu_halt_sys(emu);
  95. #ifdef __STRICT_ALIGNMENT
  96. if (addr & 3) {
  97. u_int8_t *a = emu->mem_base + addr;
  98. u_int32_t r;
  99. r = ((*(a + 0) << 0) & 0x000000ff) |
  100. ((*(a + 1) << 8) & 0x0000ff00) |
  101. ((*(a + 2) << 16) & 0x00ff0000) |
  102. ((*(a + 3) << 24) & 0xff000000);
  103. return r;
  104. } else
  105. return letoh32(*(u_int32_t *)(emu->mem_base + addr));
  106. #else
  107. return letoh32(*(u_int32_t *)(emu->mem_base + addr));
  108. #endif
  109. }
  110. /*
  111. * PARAMETERS:
  112. * addr - Emulator memory address to read
  113. * val - Value to store
  114. *
  115. * REMARKS:
  116. * Writes a byte value to emulator memory.
  117. */
  118. static void
  119. wrb(struct x86emu *emu, uint32_t addr, uint8_t val)
  120. {
  121. if (addr > emu->mem_size - 1)
  122. x86emu_halt_sys(emu);
  123. emu->mem_base[addr] = val;
  124. }
  125. /*
  126. * PARAMETERS:
  127. * addr - Emulator memory address to read
  128. * val - Value to store
  129. *
  130. * REMARKS:
  131. * Writes a word value to emulator memory.
  132. */
  133. static void
  134. wrw(struct x86emu *emu, uint32_t addr, uint16_t val)
  135. {
  136. if (addr > emu->mem_size - 2)
  137. x86emu_halt_sys(emu);
  138. #ifdef __STRICT_ALIGNMENT
  139. if (addr & 1) {
  140. u_int8_t *a = emu->mem_base + addr;
  141. *((a + 0)) = (val >> 0) & 0xff;
  142. *((a + 1)) = (val >> 8) & 0xff;
  143. } else
  144. *((u_int16_t *)(emu->mem_base + addr)) = htole16(val);
  145. #else
  146. *((u_int16_t *)(emu->mem_base + addr)) = htole16(val);
  147. #endif
  148. }
  149. /*
  150. * PARAMETERS:
  151. * addr - Emulator memory address to read
  152. * val - Value to store
  153. *
  154. * REMARKS:
  155. * Writes a long value to emulator memory.
  156. */
  157. static void
  158. wrl(struct x86emu *emu, uint32_t addr, uint32_t val)
  159. {
  160. if (addr > emu->mem_size - 4)
  161. x86emu_halt_sys(emu);
  162. #ifdef __STRICT_ALIGNMENT
  163. if (addr & 3) {
  164. u_int8_t *a = emu->mem_base + addr;
  165. *((a + 0) = (val >> 0) & 0xff;
  166. *((a + 1) = (val >> 8) & 0xff;
  167. *((a + 2) = (val >> 16) & 0xff;
  168. *((a + 3) = (val >> 24) & 0xff;
  169. } else
  170. *((u_int32_t *)(emu->mem_base + addr)) = htole32(val);
  171. #else
  172. *((u_int32_t *)(emu->mem_base + addr)) = htole32(val);
  173. #endif
  174. }
  175. /* Setup */
  176. void
  177. x86emu_init_default(struct x86emu *emu)
  178. {
  179. int i;
  180. emu->emu_rdb = rdb;
  181. emu->emu_rdw = rdw;
  182. emu->emu_rdl = rdl;
  183. emu->emu_wrb = wrb;
  184. emu->emu_wrw = wrw;
  185. emu->emu_wrl = wrl;
  186. for (i = 0; i < 256; i++)
  187. emu->_x86emu_intrTab[i] = NULL;
  188. }