sys.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /****************************************************************************
  2. *
  3. * Realmode X86 Emulator Library
  4. *
  5. * Copyright (C) 1991-2004 SciTech Software, Inc.
  6. * Copyright (C) David Mosberger-Tang
  7. * Copyright (C) 1999 Egbert Eich
  8. *
  9. * ========================================================================
  10. *
  11. * Permission to use, copy, modify, distribute, and sell this software and
  12. * its documentation for any purpose is hereby granted without fee,
  13. * provided that the above copyright notice appear in all copies and that
  14. * both that copyright notice and this permission notice appear in
  15. * supporting documentation, and that the name of the authors not be used
  16. * in advertising or publicity pertaining to distribution of the software
  17. * without specific, written prior permission. The authors makes no
  18. * representations about the suitability of this software for any purpose.
  19. * It is provided "as is" without express or implied warranty.
  20. *
  21. * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  22. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  23. * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  24. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  25. * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  26. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  27. * PERFORMANCE OF THIS SOFTWARE.
  28. *
  29. * ========================================================================
  30. *
  31. * Language: ANSI C
  32. * Environment: Any
  33. * Developer: Kendall Bennett
  34. *
  35. * Description: This file includes subroutines which are related to
  36. * programmed I/O and memory access. Included in this module
  37. * are default functions that do nothing. For real uses these
  38. * functions will have to be overridden by the user library.
  39. *
  40. ****************************************************************************/
  41. #include <common.h>
  42. #include "x86emu/x86emui.h"
  43. /*------------------------- Global Variables ------------------------------*/
  44. X86EMU_sysEnv _X86EMU_env; /* Global emulator machine state */
  45. X86EMU_intrFuncs _X86EMU_intrTab[256];
  46. int debug_intr;
  47. /*----------------------------- Implementation ----------------------------*/
  48. /****************************************************************************
  49. PARAMETERS:
  50. addr - Emulator memory address to read
  51. RETURNS:
  52. Byte value read from emulator memory.
  53. REMARKS:
  54. Reads a byte value from the emulator memory.
  55. ****************************************************************************/
  56. u8 X86API rdb(u32 addr)
  57. {
  58. return 0;
  59. }
  60. /****************************************************************************
  61. PARAMETERS:
  62. addr - Emulator memory address to read
  63. RETURNS:
  64. Word value read from emulator memory.
  65. REMARKS:
  66. Reads a word value from the emulator memory.
  67. ****************************************************************************/
  68. u16 X86API rdw(u32 addr)
  69. {
  70. return 0;
  71. }
  72. /****************************************************************************
  73. PARAMETERS:
  74. addr - Emulator memory address to read
  75. RETURNS:
  76. Long value read from emulator memory.
  77. REMARKS:
  78. Reads a long value from the emulator memory.
  79. ****************************************************************************/
  80. u32 X86API rdl(u32 addr)
  81. {
  82. return 0;
  83. }
  84. /****************************************************************************
  85. PARAMETERS:
  86. addr - Emulator memory address to read
  87. val - Value to store
  88. REMARKS:
  89. Writes a byte value to emulator memory.
  90. ****************************************************************************/
  91. void X86API wrb(u32 addr, u8 val)
  92. {
  93. }
  94. /****************************************************************************
  95. PARAMETERS:
  96. addr - Emulator memory address to read
  97. val - Value to store
  98. REMARKS:
  99. Writes a word value to emulator memory.
  100. ****************************************************************************/
  101. void X86API wrw(u32 addr, u16 val)
  102. {
  103. }
  104. /****************************************************************************
  105. PARAMETERS:
  106. addr - Emulator memory address to read
  107. val - Value to store
  108. REMARKS:
  109. Writes a long value to emulator memory.
  110. ****************************************************************************/
  111. void X86API wrl(u32 addr, u32 val)
  112. {
  113. }
  114. /****************************************************************************
  115. PARAMETERS:
  116. addr - PIO address to read
  117. RETURN:
  118. 0
  119. REMARKS:
  120. Default PIO byte read function. Doesn't perform real inb.
  121. ****************************************************************************/
  122. static u8 X86API p_inb(X86EMU_pioAddr addr)
  123. {
  124. DB(if (DEBUG_IO_TRACE())
  125. printk("inb %#04x \n", addr);)
  126. return 0;
  127. }
  128. /****************************************************************************
  129. PARAMETERS:
  130. addr - PIO address to read
  131. RETURN:
  132. 0
  133. REMARKS:
  134. Default PIO word read function. Doesn't perform real inw.
  135. ****************************************************************************/
  136. static u16 X86API p_inw(X86EMU_pioAddr addr)
  137. {
  138. DB(if (DEBUG_IO_TRACE())
  139. printk("inw %#04x \n", addr);)
  140. return 0;
  141. }
  142. /****************************************************************************
  143. PARAMETERS:
  144. addr - PIO address to read
  145. RETURN:
  146. 0
  147. REMARKS:
  148. Default PIO long read function. Doesn't perform real inl.
  149. ****************************************************************************/
  150. static u32 X86API p_inl(X86EMU_pioAddr addr)
  151. {
  152. DB(if (DEBUG_IO_TRACE())
  153. printk("inl %#04x \n", addr);)
  154. return 0;
  155. }
  156. /****************************************************************************
  157. PARAMETERS:
  158. addr - PIO address to write
  159. val - Value to store
  160. REMARKS:
  161. Default PIO byte write function. Doesn't perform real outb.
  162. ****************************************************************************/
  163. static void X86API p_outb(X86EMU_pioAddr addr, u8 val)
  164. {
  165. DB(if (DEBUG_IO_TRACE())
  166. printk("outb %#02x -> %#04x \n", val, addr);)
  167. return;
  168. }
  169. /****************************************************************************
  170. PARAMETERS:
  171. addr - PIO address to write
  172. val - Value to store
  173. REMARKS:
  174. Default PIO word write function. Doesn't perform real outw.
  175. ****************************************************************************/
  176. static void X86API p_outw(X86EMU_pioAddr addr, u16 val)
  177. {
  178. DB(if (DEBUG_IO_TRACE())
  179. printk("outw %#04x -> %#04x \n", val, addr);)
  180. return;
  181. }
  182. /****************************************************************************
  183. PARAMETERS:
  184. addr - PIO address to write
  185. val - Value to store
  186. REMARKS:
  187. Default PIO ;ong write function. Doesn't perform real outl.
  188. ****************************************************************************/
  189. static void X86API p_outl(X86EMU_pioAddr addr, u32 val)
  190. {
  191. DB(if (DEBUG_IO_TRACE())
  192. printk("outl %#08x -> %#04x \n", val, addr);)
  193. return;
  194. }
  195. /*------------------------- Global Variables ------------------------------*/
  196. u8(X86APIP sys_rdb) (u32 addr) = rdb;
  197. u16(X86APIP sys_rdw) (u32 addr) = rdw;
  198. u32(X86APIP sys_rdl) (u32 addr) = rdl;
  199. void (X86APIP sys_wrb) (u32 addr, u8 val) = wrb;
  200. void (X86APIP sys_wrw) (u32 addr, u16 val) = wrw;
  201. void (X86APIP sys_wrl) (u32 addr, u32 val) = wrl;
  202. u8(X86APIP sys_inb) (X86EMU_pioAddr addr) = p_inb;
  203. u16(X86APIP sys_inw) (X86EMU_pioAddr addr) = p_inw;
  204. u32(X86APIP sys_inl) (X86EMU_pioAddr addr) = p_inl;
  205. void (X86APIP sys_outb) (X86EMU_pioAddr addr, u8 val) = p_outb;
  206. void (X86APIP sys_outw) (X86EMU_pioAddr addr, u16 val) = p_outw;
  207. void (X86APIP sys_outl) (X86EMU_pioAddr addr, u32 val) = p_outl;
  208. /*----------------------------- Setup -------------------------------------*/
  209. /****************************************************************************
  210. PARAMETERS:
  211. funcs - New memory function pointers to make active
  212. REMARKS:
  213. This function is used to set the pointers to functions which access
  214. memory space, allowing the user application to override these functions
  215. and hook them out as necessary for their application.
  216. ****************************************************************************/
  217. void X86EMU_setupMemFuncs(X86EMU_memFuncs * funcs)
  218. {
  219. sys_rdb = funcs->rdb;
  220. sys_rdw = funcs->rdw;
  221. sys_rdl = funcs->rdl;
  222. sys_wrb = funcs->wrb;
  223. sys_wrw = funcs->wrw;
  224. sys_wrl = funcs->wrl;
  225. }
  226. /****************************************************************************
  227. PARAMETERS:
  228. funcs - New programmed I/O function pointers to make active
  229. REMARKS:
  230. This function is used to set the pointers to functions which access
  231. I/O space, allowing the user application to override these functions
  232. and hook them out as necessary for their application.
  233. ****************************************************************************/
  234. void X86EMU_setupPioFuncs(X86EMU_pioFuncs * funcs)
  235. {
  236. sys_inb = funcs->inb;
  237. sys_inw = funcs->inw;
  238. sys_inl = funcs->inl;
  239. sys_outb = funcs->outb;
  240. sys_outw = funcs->outw;
  241. sys_outl = funcs->outl;
  242. }
  243. void X86EMU_setupIntrFunc(int intnum, X86EMU_intrFuncs func)
  244. {
  245. _X86EMU_intrTab[intnum] = func;
  246. }
  247. /****************************************************************************
  248. PARAMETERS:
  249. funcs - New interrupt vector table to make active
  250. REMARKS:
  251. This function is used to set the pointers to functions which handle
  252. interrupt processing in the emulator, allowing the user application to
  253. hook interrupts as necessary for their application. Any interrupts that
  254. are not hooked by the user application, and reflected and handled internally
  255. in the emulator via the interrupt vector table. This allows the application
  256. to get control when the code being emulated executes specific software
  257. interrupts.
  258. ****************************************************************************/
  259. void X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])
  260. {
  261. int i;
  262. for (i = 0; i < 256; i++)
  263. _X86EMU_intrTab[i] = NULL;
  264. if (funcs) {
  265. for (i = 0; i < 256; i++)
  266. _X86EMU_intrTab[i] = funcs[i];
  267. }
  268. }
  269. /****************************************************************************
  270. PARAMETERS:
  271. int - New software interrupt to prepare for
  272. REMARKS:
  273. This function is used to set up the emulator state to exceute a software
  274. interrupt. This can be used by the user application code to allow an
  275. interrupt to be hooked, examined and then reflected back to the emulator
  276. so that the code in the emulator will continue processing the software
  277. interrupt as per normal. This essentially allows system code to actively
  278. hook and handle certain software interrupts as necessary.
  279. ****************************************************************************/
  280. void X86EMU_prepareForInt(int num)
  281. {
  282. push_word((u16) M.x86.R_FLG);
  283. CLEAR_FLAG(F_IF);
  284. CLEAR_FLAG(F_TF);
  285. push_word(M.x86.R_CS);
  286. M.x86.R_CS = mem_access_word(num * 4 + 2);
  287. push_word(M.x86.R_IP);
  288. M.x86.R_IP = mem_access_word(num * 4);
  289. M.x86.intr = 0;
  290. }