P. 1
Study of the techniques for emulation programming by Victor Moya del Barrio

Study of the techniques for emulation programming by Victor Moya del Barrio

4.4

|Views: 12,453|Likes:
Published by Moe B. Us
In the time I started to learn about how an emulator is programmed I found that there was a limited amount of documentation. Documentation about how an emulator should be programmed or about the characteristics of the machines to be emulated. Thinking about how to contribute to the emulation I found that the second part would mean the construction of a database with all the information available of a large amount of computers and systems. This wasn’t very suited neither with a university project for
with my own preferences, and there are a few sites in Internet which more or less serve to that purpose. The first part meant to study and learn the various techniques and knowledges related to emulator programming. Since I like a lot learning and also writing I thought that could be my way to contribute. So I thought it would be nice to have a large, ordered and precise documentation about how to program an emulator. That is the purpose of this document and my work in the last months
In the time I started to learn about how an emulator is programmed I found that there was a limited amount of documentation. Documentation about how an emulator should be programmed or about the characteristics of the machines to be emulated. Thinking about how to contribute to the emulation I found that the second part would mean the construction of a database with all the information available of a large amount of computers and systems. This wasn’t very suited neither with a university project for
with my own preferences, and there are a few sites in Internet which more or less serve to that purpose. The first part meant to study and learn the various techniques and knowledges related to emulator programming. Since I like a lot learning and also writing I thought that could be my way to contribute. So I thought it would be nice to have a large, ordered and precise documentation about how to program an emulator. That is the purpose of this document and my work in the last months

More info:

Published by: Moe B. Us on Jun 04, 2007
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

01/04/2016

pdf

text

original

The basic memory map is a list of regions which must be accessed in a different manner. For each
region exists a pointer to a memory buffer where the read or writes will be directed or a pointer to a
function which will be called with an access to this address is found. There uses to be more than one list
of those memory regions. Since it can be very different the behaviour in read and write access many
times it will be implemented a separate list for read and writes. The data size of the access can be also
important in some cases so sometimes we will found different list for the different data sizes. If a
separated address space for IO exists it will have its own region lists for reading and writing and the
different data sizes. Some CPU emulators include a separated region list for fetching (read code).
However most of the CPU emulators try to do not read opcodes through the memory regions because it is
very expensive, a direct read to the main memory buffer uses to be enough for fetching code. In some
cases this could be different and special mechanism will be implemented.

Figure 46. Genesis memory map (memory mapped IO).

ROM
CARTRIDGE

SEGA
RESERVED

SYSTEM I/O

SEGA
RESERVED

VDP

WORK RAM

0x000000

0x200000

0xA00000

0xB00000

0xC00000

0xE00000

SEGA
RESERVED

CONTROL

I / O

Z80

0xA00000

0xA10000

0xA11000

ACCESS
PROHIBITED

WORK RAM

0xE00000

0xFF0000

0xFFFFF

92

This is the structure of a memory region entry in C:

{0xa000, 0xc000, NULL, videoMemoryHand}

The first two fields are the limits of the region. When a CPU emulator is going to perform a memory
access of any kind it will test the address in the memory region list for this kind of address. If a range in
the list matches the address then the action associated with this region is performed. If a match does not
exists it can be interpreted as it was an access to the normal main buffer (faster access) or that it is an
access to an undefined address and a default value must be returned. The next fields in the entry have the
information about which action must be performed for this memory region. In this case the fields are a
pointer to a memory buffer and a pointer to a function. In the example the memory buffer address field is
NULL or undefined so this field is ignored. The next field contains a pointer to a function which will be
called for implementing the access.

The basic implementation of the memory emulation using region lists starts with a main buffer for all the
emulated memory. This buffer will be accessed whenever and address and access type does not match an
entry in the region list. This kind of access is the fastest (a memory access is directly emulated as a
memory access in the target CPU). Then the basic algorithm is to put a loop in each memory access
instruction which checks the access address for the region list sequentially. There are other
implementations (hash tables) which are faster but if the number of regions is small enough they are not
useful. After the loop there is a code which decides what kind of access must be performed, either an
access to the main buffer, or to the associated buffer or to call the function handler.

struct z80PortRead ReadPorts[] =
{

{0xbe, 0xbf, readVDP},
{0x7e, 0x7f, readVHCount},
{0xc0, 0xc1, readJoy},
{0xdc, 0xdd, readJoy},
{0x00, 0x00, readStartNationGG},
{0x01, 0x05, readIOGG},
{-1, -1, NULL}

};

struct z80PortWrite WritePorts[] =
{

{0xbe, 0xbf, writeVDP},
{0x7e, 0x7f, writePSG},
{0x3e, 0x3f, writeNationBIOS},
{0xde, 0xdf, unknownWrite},
{0x01, 0x05, writeIOGG},
{-1, -1, NULL}

};

struct MemoryReadByte ReadMemory[] =

{

{-1, -1, NULL}

};

struct MemoryWriteByte WriteMemory[] =

{

{0x0000, 0xbfff, romWriteProtect},
{0xc000, 0xfffb, ramWrite},
{0xfffc, 0xffff, writePageRegistersBS},
{-1, -1, NULL}

};

Figure 47. Master System memory and IO region list handlers.

93

The actions which have to be performed because of an access in some cases can be complex. This is the
reason that, rather than implementing different types of general access in the main access emulation code,
a function which acts as a memory handler is implemented for each region. The memory handlers have a
standard interface. They receive the address, the data (if it is a write access), and the entry of region list.
If it is a handler for a read access they return the read data. Those functions can perform any task. The
most common is just access a special memory buffer for this region, or perform mirroring (the same
physical memory is accessed through two regions, so any write to any of the regions must be performed
in the two regions). If it is a mapped region of device memory or a device register the function handler
will be implemented in the device side using the standard interface (function definition). Device register
handlers use to trigger actions in the device emulation or to set internal variables for later use. Some
access will be directly emulated as access to the hardware in the real machine.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->