sys.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 overriden by the user library.
  39. *
  40. ****************************************************************************/
  41. #include "x86emu/x86emui.h"
  42. /*------------------------- Global Variables ------------------------------*/
  43. X86EMU_sysEnv _X86EMU_env; /* Global emulator machine state */
  44. X86EMU_intrFuncs _X86EMU_intrTab[256];
  45. int debug_intr;
  46. /*----------------------------- Implementation ----------------------------*/
  47. /****************************************************************************
  48. PARAMETERS:
  49. addr - Emulator memory address to read
  50. RETURNS:
  51. Byte value read from emulator memory.
  52. REMARKS:
  53. Reads a byte value from the emulator memory.
  54. ****************************************************************************/
  55. u8 X86API rdb(u32 addr)
  56. {
  57. return 0;
  58. }
  59. /****************************************************************************
  60. PARAMETERS:
  61. addr - Emulator memory address to read
  62. RETURNS:
  63. Word value read from emulator memory.
  64. REMARKS:
  65. Reads a word value from the emulator memory.
  66. ****************************************************************************/
  67. u16 X86API rdw(u32 addr)
  68. {
  69. return 0;
  70. }
  71. /****************************************************************************
  72. PARAMETERS:
  73. addr - Emulator memory address to read
  74. RETURNS:
  75. Long value read from emulator memory.
  76. REMARKS:
  77. Reads a long value from the emulator memory.
  78. ****************************************************************************/
  79. u32 X86API rdl(u32 addr)
  80. {
  81. return 0;
  82. }
  83. /****************************************************************************
  84. PARAMETERS:
  85. addr - Emulator memory address to read
  86. val - Value to store
  87. REMARKS:
  88. Writes a byte value to emulator memory.
  89. ****************************************************************************/
  90. void X86API wrb(u32 addr, u8 val)
  91. {
  92. }
  93. /****************************************************************************
  94. PARAMETERS:
  95. addr - Emulator memory address to read
  96. val - Value to store
  97. REMARKS:
  98. Writes a word value to emulator memory.
  99. ****************************************************************************/
  100. void X86API wrw(u32 addr, u16 val)
  101. {
  102. }
  103. /****************************************************************************
  104. PARAMETERS:
  105. addr - Emulator memory address to read
  106. val - Value to store
  107. REMARKS:
  108. Writes a long value to emulator memory.
  109. ****************************************************************************/
  110. void X86API wrl(u32 addr, u32 val)
  111. {
  112. }
  113. /****************************************************************************
  114. PARAMETERS:
  115. addr - PIO address to read
  116. RETURN:
  117. 0
  118. REMARKS:
  119. Default PIO byte read function. Doesn't perform real inb.
  120. ****************************************************************************/
  121. static u8 X86API p_inb(X86EMU_pioAddr addr)
  122. {
  123. DB(if (DEBUG_IO_TRACE())
  124. printk("inb %#04x \n", addr);)
  125. return 0;
  126. }
  127. /****************************************************************************
  128. PARAMETERS:
  129. addr - PIO address to read
  130. RETURN:
  131. 0
  132. REMARKS:
  133. Default PIO word read function. Doesn't perform real inw.
  134. ****************************************************************************/
  135. static u16 X86API p_inw(X86EMU_pioAddr addr)
  136. {
  137. DB(if (DEBUG_IO_TRACE())
  138. printk("inw %#04x \n", addr);)
  139. return 0;
  140. }
  141. /****************************************************************************
  142. PARAMETERS:
  143. addr - PIO address to read
  144. RETURN:
  145. 0
  146. REMARKS:
  147. Default PIO long read function. Doesn't perform real inl.
  148. ****************************************************************************/
  149. static u32 X86API p_inl(X86EMU_pioAddr addr)
  150. {
  151. DB(if (DEBUG_IO_TRACE())
  152. printk("inl %#04x \n", addr);)
  153. return 0;
  154. }
  155. /****************************************************************************
  156. PARAMETERS:
  157. addr - PIO address to write
  158. val - Value to store
  159. REMARKS:
  160. Default PIO byte write function. Doesn't perform real outb.
  161. ****************************************************************************/
  162. static void X86API p_outb(X86EMU_pioAddr addr, u8 val)
  163. {
  164. DB(if (DEBUG_IO_TRACE())
  165. printk("outb %#02x -> %#04x \n", val, addr);)
  166. return;
  167. }
  168. /****************************************************************************
  169. PARAMETERS:
  170. addr - PIO address to write
  171. val - Value to store
  172. REMARKS:
  173. Default PIO word write function. Doesn't perform real outw.
  174. ****************************************************************************/
  175. static void X86API p_outw(X86EMU_pioAddr addr, u16 val)
  176. {
  177. DB(if (DEBUG_IO_TRACE())
  178. printk("outw %#04x -> %#04x \n", val, addr);)
  179. return;
  180. }
  181. /****************************************************************************
  182. PARAMETERS:
  183. addr - PIO address to write
  184. val - Value to store
  185. REMARKS:
  186. Default PIO ;ong write function. Doesn't perform real outl.
  187. ****************************************************************************/
  188. static void X86API p_outl(X86EMU_pioAddr addr, u32 val)
  189. {
  190. DB(if (DEBUG_IO_TRACE())
  191. printk("outl %#08x -> %#04x \n", val, addr);)
  192. return;
  193. }
  194. /*------------------------- Global Variables ------------------------------*/
  195. u8(X86APIP sys_rdb) (u32 addr) = rdb;
  196. u16(X86APIP sys_rdw) (u32 addr) = rdw;
  197. u32(X86APIP sys_rdl) (u32 addr) = rdl;
  198. void (X86APIP sys_wrb) (u32 addr, u8 val) = wrb;
  199. void (X86APIP sys_wrw) (u32 addr, u16 val) = wrw;
  200. void (X86APIP sys_wrl) (u32 addr, u32 val) = wrl;
  201. u8(X86APIP sys_inb) (X86EMU_pioAddr addr) = p_inb;
  202. u16(X86APIP sys_inw) (X86EMU_pioAddr addr) = p_inw;
  203. u32(X86APIP sys_inl) (X86EMU_pioAddr addr) = p_inl;
  204. void (X86APIP sys_outb) (X86EMU_pioAddr addr, u8 val) = p_outb;
  205. void (X86APIP sys_outw) (X86EMU_pioAddr addr, u16 val) = p_outw;
  206. void (X86APIP sys_outl) (X86EMU_pioAddr addr, u32 val) = p_outl;
  207. /*----------------------------- Setup -------------------------------------*/
  208. /****************************************************************************
  209. PARAMETERS:
  210. funcs - New memory function pointers to make active
  211. REMARKS:
  212. This function is used to set the pointers to functions which access
  213. memory space, allowing the user application to override these functions
  214. and hook them out as necessary for their application.
  215. ****************************************************************************/
  216. void X86EMU_setupMemFuncs(X86EMU_memFuncs * funcs)
  217. {
  218. sys_rdb = funcs->rdb;
  219. sys_rdw = funcs->rdw;
  220. sys_rdl = funcs->rdl;
  221. sys_wrb = funcs->wrb;
  222. sys_wrw = funcs->wrw;
  223. sys_wrl = funcs->wrl;
  224. }
  225. /****************************************************************************
  226. PARAMETERS:
  227. funcs - New programmed I/O function pointers to make active
  228. REMARKS:
  229. This function is used to set the pointers to functions which access
  230. I/O space, allowing the user application to override these functions
  231. and hook them out as necessary for their application.
  232. ****************************************************************************/
  233. void X86EMU_setupPioFuncs(X86EMU_pioFuncs * funcs)
  234. {
  235. sys_inb = funcs->inb;
  236. sys_inw = funcs->inw;
  237. sys_inl = funcs->inl;
  238. sys_outb = funcs->outb;
  239. sys_outw = funcs->outw;
  240. sys_outl = funcs->outl;
  241. }
  242. /****************************************************************************
  243. PARAMETERS:
  244. funcs - New interrupt vector table to make active
  245. REMARKS:
  246. This function is used to set the pointers to functions which handle
  247. interrupt processing in the emulator, allowing the user application to
  248. hook interrupts as necessary for their application. Any interrupts that
  249. are not hooked by the user application, and reflected and handled internally
  250. in the emulator via the interrupt vector table. This allows the application
  251. to get control when the code being emulated executes specific software
  252. interrupts.
  253. ****************************************************************************/
  254. void X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])
  255. {
  256. int i;
  257. for (i = 0; i < 256; i++)
  258. _X86EMU_intrTab[i] = NULL;
  259. if (funcs) {
  260. for (i = 0; i < 256; i++)
  261. _X86EMU_intrTab[i] = funcs[i];
  262. }
  263. }
  264. /****************************************************************************
  265. PARAMETERS:
  266. int - New software interrupt to prepare for
  267. REMARKS:
  268. This function is used to set up the emulator state to exceute a software
  269. interrupt. This can be used by the user application code to allow an
  270. interrupt to be hooked, examined and then reflected back to the emulator
  271. so that the code in the emulator will continue processing the software
  272. interrupt as per normal. This essentially allows system code to actively
  273. hook and handle certain software interrupts as necessary.
  274. ****************************************************************************/
  275. void X86EMU_prepareForInt(int num)
  276. {
  277. push_word((u16) M.x86.R_FLG);
  278. CLEAR_FLAG(F_IF);
  279. CLEAR_FLAG(F_TF);
  280. push_word(M.x86.R_CS);
  281. M.x86.R_CS = mem_access_word(num * 4 + 2);
  282. push_word(M.x86.R_IP);
  283. M.x86.R_IP = mem_access_word(num * 4);
  284. M.x86.intr = 0;
  285. }