home *** CD-ROM | disk | FTP | other *** search
-
- Zilog Z80 CPU emulator package for the Motorola M68000 family.
-
- Version 0.99b
-
- (C) Copyright 1993 Richard Carlsson
-
- Author: Richard Carlsson
-
- E-Mail: m90rca @ tdb.uu.se (Uppsala University, Sweden)
- Mail: Solgatan 58, 195 55 Maersta, Sweden.
-
-
- The emulator package is freeware. It may be distributed freely as long
- as nothing is charged for the distribution beyond the actual costs of
- media and transfer. If the package is used in a program, the same rules
- for distribution must apply to that program unless the rights to charge
- a fee have been explicitly obtained from the author.
-
- That should cover just about everything. If you include the emulator in
- a project, I would like to hear about it. Please always inform me if
- you release such a project as public domain or freeware.
-
- I have done my best to make the emulator as flexible as possible. The
- interface should be easy to understand and use, from C as well as from
- assembler. The C stubs and the header file Z80.h should make it easy to
- use the emulator from a shell written in C. (As I have done myself.)
- Please report any bugs, assembler or machine incompatibilities, or mere
- stupidity in my code, and I shall fix it as soon as possible. You are
- of course also welcome to present new ideas, but please read the file
- cache.txt before you attempt to improve the emulation method itself.
-
- The other goal of this project has been emulation speed. There are
- Z80 emulators that may be even more flexible and accurate in emulation
- than this one, but I know of no one that is also this fast.
-
- This program is a tribute to Sir Clive Sinclair.
-
- Credits:
-
- To Peter McGavin, for the basic idea of using threaded code,
- for a lot of idea bouncing and testing, and for giving me new
- inspiration to finish this old project. Also to Linus and Oliver
- for starting this whole business. See the history below.
-
-
- Examples of emulation speed:
-
- Run on processor Corresponds to Hz factor
-
- Full-featured version:
- 68000, 7.16 MHz 0.52 MHz Z80 0.073
- 68030, 40 MHz 3.3 MHz Z80 0.083
-
- Stripped "games" version:
- 68000, 7.16 MHz 0.67 MHz Z80 0.094
- 68030, 40 MHz 3.8 MHz Z80 0.095
-
- The tests were run on an Amiga, with the system up and running,
- and lots of other tasks loaded (most of them in waiting state).
- To find out what the emulator would correspond to on your machine,
- multiply the frequency of your processor with the "Hz factor". For
- instance, a 33 MHz 68030 running the full version gives 33 * 0.083
- -> 2.7 MHz Z80. Of course this will vary with different memory
- configurations etc.
-
-
- Main files of interest:
-
- Z80.a The main emulator source file.
- Z80.i Include file for using the emulator.
- Z80_struct.i Include file defining the control structure.
- Z80_coding.i Include file with definitions needed to code the
- Z80 instruction emulation routines.
- extern_instr.a File for external compilation of implementation-
- specific Z80 instructions like In/Out.
- generic_macs.i Include file defining the implementation-specific
- Z80 instructions (generic versions).
- user.i User definitions.
- Z80.h Header file for using the emulator from C.
- c_stubs.a File defining the assembler stubs for calling
- the emulator functions from C.
- files A more detailed description of all included files.
-
- Use:
-
- Includes/header files
-
- Assembler:
- All constants and cross-references are defined in Z80.i. The
- control structure, offsets and size, are defined in Z80_struct.i.
- The function calls are documented in Z80.i.
-
- C:
- Constants, function prototypes and documentation, structure
- declarations and so on are all defined/declared in Z80.h.
-
- Allocating memory
-
- The emulator needs several areas of memory to execute:
- Size Comment
- * Z80 address space Z80_MEMSIZE
- * Cache memory Z80_CACHESIZE Must be word-aligned
- * Control structure Z80_StructSize Must be word-aligned.
-
- If the memory write access checking feature is used, memory for the
- control flags is also needed:
- * Flag memory Z80_FLAGMEMSIZE
-
- Z80_MEMSIZE and Z80_FLAGMEMSIZE are approx. 64 K each.
- Z80_CACHESIZE is approx. 128 K. The control structure takes about
- 500 bytes.
-
- Setting up before coldstart
-
- Set the fields Z80_Memory and Z80_Cachemem of the control
- structure to point to the corresponding memory areas. If memory
- checking is used, set the Z80_Flagmem field to point to the flag
- memory, or to zero if the feature is not used. Also set the field
- Z80_MemHandler to the address of a user-defined memory exception
- handler routine, or to zero if no such routine exists.
- After that, call Z80_Init to initialise the remaining fields of
- the control structure. The user environment data area is not
- affected.
- Call Z80_SetMemFlag to flag any memory areas before start.
- Documentation on the flags and the User Exception Handler Call can
- be found in Z80.i. Other helper functions could for instance be
- used to initialise the memory before start.
-
- Coldstart
-
- A coldstart does a CPU reset as it starts the emulation. When (and
- if) the emulation exits (see Exit emulation below), you may inspect
- and even change the public fields of the control structure, where
- the Cpu Status entries are the most interesting ones.
-
- Continuing emulation
-
- Call Z80_Continue each time you want the emulation to proceed
- with the information in the Cpu Status entries. Z80_Init sets up
- these entries as after a CPU reset. For no other fields of the
- control structure are changes guaranteed to have any effect when
- calling Z80_Continue directly afterwards (see Reallocating Memory
- Areas below).
-
- Exit emulation
-
- Presently, the only way to exit the emulator is to call
- Z80_EXITreq from an outside task or interrupt routine. I intend
- to make it possible to patch Z80 instructions (particularly
- undefined ones) to take on special meanings, like EXIT. It will
- then be possible for the task running the emulator to return "by
- its own will".
-
- Reallocating memory areas
-
- To inform the emulator that changes to the public fields like
- the memory pointers have taken place, call Z80_NewSettings.
- Calling Z80_Continue will then proceed using the new locations
- (see Continuing emulation above). Do not move a memory area or
- call Z80_NewSettings before the emulation has stopped (inspect
- the Running field of the control structure).
- The control structure may be moved without calling Z80_Realloc,
- since its address is always passed as a parameter.
-
- Compilation:
-
- The main emulator file Z80.a and its associated files are written
- in Devpac 3 (and mostly Devpac 2 compatible) macro assembler
- syntax. The main not generally compatible feature is the local
- labels, which are on the form .<label> (that is, an identifier
- beginning with a dot).
- Compilation of Z80.a can be to either a single stand-alone object
- file containing the whole emulator function package, or to a
- generic object file that needs external labels (see the section on
- In/Out and other implementation-dependent instructions below).
-
- The include files Z80.i and Z80_struct.i as well as the files
- external_instr.a, generic_macs.i, Z80_coding.i and c_stubs.a are in
- a more standardized macro assembler form.
- All user definitions are to be entered in user.i.
- Compiling the C stubs should be straightforward. Check with your
- C compiler manual to see if they are behaving as they should.
-
- I/O instructions and other implementation-dependent behaviour:
-
- All instructions that are involved with external signalling and I/O
- and generally depend on your environment, should be possible to
- redesign fairly easily with only a small amount of knowledge about
- the internal workings of the emulator. The prototypical examples in
- generic_macs.i are probably not exactly what you want, so:
- Copy the file generic_macs.i to a file machine_macs.i, and edit the
- macros in it. Set Z80_ENV_SIZE (see Z80.i) to suit your needs, and
- modify any compilation flags in the file you are compiling. Then
- recompile (to object code) either the file external_instr.i (to
- link with a 'generic object' emulator object file), or the main
- emulator file Z80.a itself (see the compilation flags in Z80.a).
-
- Coding rules:
- Every instruction routine must end with a call of the "next" macro.
- Always use the putsr/getsr macros to access the 68000 flags.
- Always use the getz/putz macros to access the Z80 memory, even if
- you only change a single bit or simply clear a byte.
- On entering the instruction routine, the PPC (Pseudo-PC) points to
- the word corresponding to the byte following the first byte of the
- opcode. Use the getRPC macro to translate the PPC into a Real-PC
- Z80 address.
- The Z80_Envdata structure is there for quick access relative to the
- TableB register. It is provided for use by the implementation-
- dependent instruction routines.
- "Use the source". The generic macros should be easy to modify.
- All macros are defined in Z80_coding.i, and are described there.
-
-
- History:
-
- Somewhere around 1987/88, Linus Eklund gets the bizarre idea of
- turning a multitasking Amiga 500 into a single-tasking "the machine
- is all yours" system with a Basic-like language and a full-screen
- editor with a horrible font (the Sinclair ZX Spectrum 8x8 pixel
- font, "because it was easy to obtain a hex listing of it").
- Some time later, he stepped up the project to actually emulate
- Z80 instructions (so he could run the Spectrum ROM code, which would
- spare him the writing of an own Basic interpreter). He did a
- straight fetch-decode-jump emulation and implemented the more common
- instructions (he ran a statistics program on the 'Hungry Horace'
- program file).
- Around 1989 a friend, Oliver Waldegg, joined in and together they
- got an emulator going that successfully emulated (in multi-coloured
- bitmap on a white background) exactly one game - Hungry Horace.
- In September 1990, I (Richard Carlsson) bought an Amiga to upgrade
- from my old 48K ZX Spectrum, on which I had been hacking for many
- years. I got interested in this emulator thing and began to work on
- it. I made some progress, improving the emulation a bit, and
- implementing all official instructions, but mostly on emulating the
- Spectrum hardware.
- Some time in 1991, I downloaded a ZX Spectrum emulator from the
- internet. It was written by Peter McGavin, whom I contacted. He had
- shown that using a translation cache (my term), or 'threaded code',
- was not only possible, but also faster than the fetch-decode-jump
- method. I got extremely interested in this, which totally ruined my
- studies at the university for that year, but I managed to improve
- his method and extend it to also caching prefixed instructions.
- (Don't mail him to say "did you know that..." - he is also using
- the improved method now.) See the file cache.txt if you are
- interested in the (rather hairy) details.
- Seeing that Peters emulator was more or less doing what I had
- intended mine to do, I changed my goal to be a pure Z80 emulator
- package that could be included in any Z80-based emulation project
- on a 680x0, while retaining maximal speed of emulation.
- Having a somewhat stable version running, I put it on the shelf
- in April 1992 and got back to studying. I did not touch it again
- until July 1993, when I had recovered sufficiently.
-
- Released versions:
- 0.99b October 1993. Developing version number 32.
- All code can be shared.
- Memory write access checking as compile option (does RAM, ROM,
- Access Count and User Exception Handler Call).
- Copes with all forms of modified code.
- Interrupts and other requests have priority levels. Requests of
- lower priority are remembered.
- All "undocumented" Z80 instructions implemented; compile option
- to treat them as undefined.
- BCD information storage for normal arithmetic instructions.
- Push AF can stack CCR form or Z80 F form.
- Compile option to store/retreive BCD information upon Push/Pop AF.
-
- --------
-