Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. ___________________________________________________________________________
  3.  
  4.   DrZ80 (c) Copyright 2004 Reesy.   Free for non-commercial use
  5.  
  6.   Reesy's e-mail: drsms_reesy(atsymbol)yahoo.co.uk
  7.   Replace (atsymbol) with @
  8.  
  9. ___________________________________________________________________________
  10.  
  11.  
  12. What is it?
  13. -----------
  14.  
  15. DrZ80 is an emulator for the Z80 microprocessor, written in ARM 32-bit assembly.
  16. It is aimed at chips such as ARM7 and ARM9 cores, StrongARM and XScale, to interpret Z80
  17. code as fast as possible.
  18.  
  19. Flags are mapped onto ARM flags whenever possible, which speeds up the processing of an opcode.
  20.  
  21. ARM Register Usage
  22. ------------------
  23.  
  24. See source code for up to date of register usage, however a summary is here:
  25.  
  26.   r0-3: Temporary registers
  27.   r3  : Pointer to Opcode Jump table
  28.   r4  : T-States remaining
  29.   r5  : Pointer to Cpu Context
  30.   r6  : Current PC + Memory Base (i.e. pointer to next opcode)
  31.   r7  : Z80 A Register in top 8 bits (i.e. 0xAA000000)
  32.   r8  : Z80 F Register (Flags) (NZCV) in lowest four bits
  33.   r9  : Z80 BC Register pair in top 16 bits (i.e. 0xBBCC0000)
  34.   r10 : Z80 DE Register pair in top 16 bits (i.e. 0xDDEE0000)
  35.   r11 : Z80 HL Register pair in top 16 bits (i.e. 0xHHLL0000)
  36.   r12 : Z80 Stack + Memory Base (i.e. pointer to current stack in host system memory)
  37.  
  38. ( note: r3,r12 are always preserved when calling external memory functions )
  39.  
  40. How to Compile
  41. --------------
  42.  
  43. The core is targeted for the GNU compiler, so to compile just add the "DrZ80.o" object
  44. to your makefile and that should be it.
  45.  
  46. If you want to compile it seperately, use:  as -o DrZ80.o DrZ80.s
  47.  
  48. ( note: If you want to use DrZ80 with a different compiler you will need to run
  49.         some sort of parser through the source to make the syntax of the source
  50.         compatible with your target compiler )
  51.  
  52.  
  53. Adding to your project
  54. ----------------------
  55.  
  56. To add DrZ80 to your project, add DrZ80.o, and include DrZ80.h
  57. There is one structure: 'struct DrZ80', and  three functions: DrZ80Run,DrZ80RaiseInt
  58. and DrZ80_irq_callback.
  59.  
  60. Don't worry if this seem very minimal - its all you need to run as many Z80s as you want.
  61. It works with both C and C++.
  62.  
  63. ( Note: DrZ80_irq_callback is just a pointer to an irq call back function that needs
  64.         to be written by you )
  65.  
  66. Declaring a Memory handlers
  67. ---------------------------
  68.  
  69. Before you can reset or execute Z80 opcodes you must first set up a set of memory handlers.
  70. There are 8 functions you have to set up per CPU, like this:
  71.  
  72.   unsigned int z80_rebaseSP(unsigned short new_sp);
  73.   unsigned int z80_rebasePC(unsigned short new_pc);
  74.   unsigned char z80_read8(unsigned short a);
  75.   unsigned short z80_read16(unsigned short a);
  76.   void z80_write8(unsigned char d,unsigned short a);
  77.   void z80_write16(unsigned short d,unsigned short a);
  78.   unsigned char z80_in(unsigned char p);
  79.   void z80_out(unsigned char p,unsigned char d);
  80.  
  81. You can think of these functions representing the Z80's memory bus.
  82. The Read and Write functions are called whenever the Z80 reads or writes memory.
  83. The In and Out functions are called whenever the Z80 uses the I/O ports.
  84. The z80_rebasePC and z80_rebaseSP functions are to do with creating direct memory
  85. pointers in the host machines memory, I will explain more about this later.
  86.  
  87. Declaring a CPU Context
  88. -----------------------
  89.  
  90. To declare a CPU simple declare a struct Cyclone in your code. For example to declare
  91. two Z80s:
  92.  
  93.   struct DrZ80 MyCpu;
  94.   struct DrZ80 MyCpu2;
  95.  
  96. It's probably a good idea to initialise the memory to zero:
  97.  
  98.   memset(&MyCpu, 0,sizeof(MyCpu));
  99.   memset(&MyCpu2,0,sizeof(MyCpu2));
  100.  
  101. Next point to your memory handlers:
  102.  
  103.   MyCpu.z80_rebasePC=z80_rebasePC;
  104.   MyCpu.z80_rebaseSP=z80_rebaseSP;
  105.   MyCpu.z80_read8   =z80_read8;
  106.   MyCpu.z80_read16  =z80_read16;
  107.   MyCpu.z80_write8  =z80_write8;
  108.   MyCpu.z80_write16 =z80_write16;
  109.   MyCpu.z80_in      =z80_in;
  110.   MyCpu.z80_out     =z80_out;
  111.  
  112. Now you are nearly ready to reset the Z80, except you need one more function: checkpc().
  113.  
  114. The z80_rebasePC() function
  115. ---------------------------
  116.  
  117. When DrZ80 reads opcodes, it doesn't use a memory handler every time, this would be
  118. far too slow, instead it uses a direct pointer to ARM memory.
  119. For example if your Rom image was at 0x3000000 and the program counter was $206,
  120. Cyclone's program counter would be 0x3000206.
  121.  
  122. The difference between an ARM address and a Z80 address is also stored in a variable called
  123. 'pc_membase'. In the above example it's 0x3000000. To retrieve the real PC, DrZ80 just
  124. subtracts 'pc_membase'.
  125.  
  126. Everytime the Z80 PC is modified, i.e. by a jump,branch intructions or by an interupt, DrZ80
  127. calls the z80_rebasePC function. If the PC is in a different bank, for example Ram instead
  128. of Rom, change 'pc_membase', recalculate the new PC and return it.
  129.  
  130. The z80_rebaseSP() function
  131. ---------------------------
  132.  
  133. When DrZ80 pushs/pops to the Z80 stack pointer, it doesn't use a memory handler every time.  In
  134. order to gain more speed a direct pointer to ARM memory is used.  For example if your Ram was at
  135. 0x3000000 and the z80 stack pointer counter was 0xD000, DrZ80's stack pointer would be 0x300D000.
  136.  
  137. The difference between an ARM address and a Z80 address is also stored in a variable called
  138. 'sp_membase'. In the above example it's 0x3000000. To retrieve the real SP, DrZ80 just
  139. subtracts 'sp_membase'.
  140.  
  141. Everytime the Z80 SP is modified ( i.e. set with a new value LD SP,NN etc ) DrZ80
  142. calls the z80_rebaseSP function. If the SP is in a different bank, for example Rom instead
  143. of Ram, change 'sp_membase', recalculate the new SP and return it.
  144.  
  145.