Home | History | Annotate | Line # | Download | only in libx86emu
      1 /*	$NetBSD: x86emu_util.c,v 1.2 2007/12/04 17:32:22 joerg Exp $	*/
      2 
      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 
     34 #include <x86emu/x86emu.h>
     35 #include <x86emu/x86emu_regs.h>
     36 
     37 #include <sys/endian.h>
     38 #include <sys/null.h>
     39 
     40 /****************************************************************************
     41 PARAMETERS:
     42 addr	- Emulator memory address to read
     43 
     44 RETURNS:
     45 Byte value read from emulator memory.
     46 
     47 REMARKS:
     48 Reads a byte value from the emulator memory.
     49 ****************************************************************************/
     50 static uint8_t
     51 rdb(struct X86EMU *emu, uint32_t addr)
     52 {
     53 	if (addr > emu->mem_size - 1)
     54 		X86EMU_halt_sys(emu);
     55 	return emu->mem_base[addr];
     56 }
     57 /****************************************************************************
     58 PARAMETERS:
     59 addr	- Emulator memory address to read
     60 
     61 RETURNS:
     62 Word value read from emulator memory.
     63 
     64 REMARKS:
     65 Reads a word value from the emulator memory.
     66 ****************************************************************************/
     67 static uint16_t
     68 rdw(struct X86EMU *emu, uint32_t addr)
     69 {
     70 	if (addr > emu->mem_size - 2)
     71 		X86EMU_halt_sys(emu);
     72 	return le16dec(emu->mem_base + addr);
     73 }
     74 /****************************************************************************
     75 PARAMETERS:
     76 addr	- Emulator memory address to read
     77 
     78 RETURNS:
     79 Long value read from emulator memory.
     80 REMARKS:
     81 Reads a long value from the emulator memory.
     82 ****************************************************************************/
     83 static uint32_t
     84 rdl(struct X86EMU *emu, uint32_t addr)
     85 {
     86 	if (addr > emu->mem_size - 4)
     87 		X86EMU_halt_sys(emu);
     88 	return le32dec(emu->mem_base + addr);
     89 }
     90 /****************************************************************************
     91 PARAMETERS:
     92 addr	- Emulator memory address to read
     93 val		- Value to store
     94 
     95 REMARKS:
     96 Writes a byte value to emulator memory.
     97 ****************************************************************************/
     98 static void
     99 wrb(struct X86EMU *emu, uint32_t addr, uint8_t val)
    100 {
    101 	if (addr > emu->mem_size - 1)
    102 		X86EMU_halt_sys(emu);
    103 	emu->mem_base[addr] = val;
    104 }
    105 /****************************************************************************
    106 PARAMETERS:
    107 addr	- Emulator memory address to read
    108 val		- Value to store
    109 
    110 REMARKS:
    111 Writes a word value to emulator memory.
    112 ****************************************************************************/
    113 static void
    114 wrw(struct X86EMU *emu, uint32_t addr, uint16_t val)
    115 {
    116 	if (addr > emu->mem_size - 2)
    117 		X86EMU_halt_sys(emu);
    118 	le16enc(emu->mem_base + addr, val);
    119 }
    120 /****************************************************************************
    121 PARAMETERS:
    122 addr	- Emulator memory address to read
    123 val		- Value to store
    124 
    125 REMARKS:
    126 Writes a long value to emulator memory.
    127 ****************************************************************************/
    128 static void
    129 wrl(struct X86EMU *emu, uint32_t addr, uint32_t val)
    130 {
    131 	if (addr > emu->mem_size - 4)
    132 		X86EMU_halt_sys(emu);
    133 	le32enc(emu->mem_base + addr, val);
    134 }
    135 
    136 /*----------------------------- Setup -------------------------------------*/
    137 
    138 void
    139 X86EMU_init_default(struct X86EMU *emu)
    140 {
    141 	int i;
    142 
    143 	emu->emu_rdb = rdb;
    144 	emu->emu_rdw = rdw;
    145 	emu->emu_rdl = rdl;
    146 	emu->emu_wrb = wrb;
    147 	emu->emu_wrw = wrw;
    148 	emu->emu_wrl = wrl;
    149 
    150 	for (i = 0; i < 256; i++)
    151 		emu->_X86EMU_intrTab[i] = NULL;
    152 }
    153