Subversion Repositories Kolibri OS

Rev

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

  1. ************************************************
  2. *                                              *
  3. *     CZ80 (Z80 CPU emulator) version 0.91     *
  4. *          Compiled with Dev-C++               *
  5. *  Copyright 2004-2005 Stéphane Dallongeville  *
  6. *                                              *
  7. ************************************************
  8.  
  9. CZ80 is a Z80 CPU emulator, priorities were given to :
  10. - code size
  11. - speed
  12. - accuracy
  13. - portablity
  14.  
  15. It supports almost all undocumented opcodes and flags.
  16.  
  17. The emulator can be freely distribued and used for any non commercial
  18. project as long you don't forget to credit me somewhere :)
  19. If you want some support about the CZ80, you can contact me on
  20. the Gens forum (http://gens.consolemul.com then go to the forum).
  21.  
  22.  
  23. You should find the following files in the emulation pack :
  24. - cz80.h          -> header file (prototypes, declarations...)
  25. - cz80.c          -> contains emulation core itself
  26. - cz80.inc        -> contains most used macros
  27. - cz80jmp.inc     -> Jump table definition when Jump Table used
  28. - cz80exec.inc    -> contains the major Cz80_Exec(...) function
  29. - cz80_op.inc     -> contains code for simple Z80 opcodes emulation
  30. - cz80_opCB.inc   -> contains code for CB prefixed Z80 opcodes emulation
  31. - cz80_opED.inc   -> contains code for ED prefixed Z80 opcodes emulation
  32. - cz80_opXY.inc   -> contains code for DD/FD prefixed Z80 opcodes emulation
  33. - cz80_opXYCB.inc -> contains code for DD/FD + CB prefixed Z80 opcodes emulation
  34. - readme.txt      -> the current file you're reading ;)
  35.  
  36.  
  37. * How compile the emulator ?
  38. ****************************
  39.  
  40. The emulator has been written with Dev-CPP 4.9.X.X
  41. You will maybe need to modify the u8, u16, u32, s8, s16, s32 and FASTCALL
  42. definitions (cz80.h) according to your C compiler and the target system.
  43. Then compile the cz80.c file, you should obtain a cz80.o (or cz80.obj) file...
  44. at this moment, you're ready to use the emulator just by linking the file in your project :)
  45.  
  46.  
  47. * How to use the emulator ?
  48. ***************************
  49.  
  50. 1) Include the header file in your source :
  51. ------------------------------------------
  52.  
  53. #include "cz80.h"
  54.  
  55.  
  56. 2) Init the CZ80 core :
  57. -----------------------
  58.  
  59. If you want to use the internal CZ80 context offered :
  60.  
  61.   Cz80_Init(&CZ80);
  62.  
  63. but you can also define your own CZ80 context :
  64.  
  65.   cz80_struc My_Z80;
  66.  
  67.   ....
  68.  
  69.   Cz80_Init(&My_Z80);
  70.  
  71.  
  72. You'll can emulate as many Z80 CPU as you want by defining severals CZ80 contexts.
  73.  
  74. An arbitrary pointer can also be stored in the CZ80 context.
  75. This pointer is passed to each I/O callback:
  76.  
  77.   Cz80_Set_Ctx(&CZ80, &user_context);
  78.  
  79.  
  80. 3) Set up your fetch region (where the Z80 will run code from) :
  81. ----------------------------------------------------------------
  82.  
  83.   Cz80_Set_Fetch(&CZ80, 0x0000, 0x7FFF, (u32) your_ROM);
  84.   Cz80_Set_Fetch(&CZ80, 0xA000, 0xFFFF, (u32) your_RAM);
  85.   ...
  86.  
  87.  
  88. 4) Set up your memory (where the Z80 will read and write data) :
  89. ----------------------------------------------------------------
  90.  
  91.   Cz80_Set_ReadB(&CZ80, your_z80readbyte_function);
  92.   Cz80_Set_WriteB(&CZ80, your_z80readbyte_function);
  93.  
  94. You can improve CZ80 performance by using WORD read/write function.
  95. For that, you need to enable the 'CZ80_USE_WORD_HANDLER' define in cz80.h file.
  96. In this case, you'll need to add that :
  97.  
  98. #if CZ80_USE_WORD_HANDLER
  99.   Cz80_Set_ReadW(&CZ80, your_z80readword_function);
  100.   Cz80_Set_WriteW(&CZ80, your_z80readword_function);
  101. #endif
  102.  
  103. Your read function need to be of CZ80_READ type :
  104. typedef u32  FASTCALL CZ80_READ(u32 adr);
  105.  
  106. Your write function need to be of CZ80_WRITE type :
  107. typedef void FASTCALL CZ80_WRITE(u32 adr, u32 data);
  108.  
  109.  
  110. 5) Set Up your port (where the Z80 will read and write IO data) :
  111. -----------------------------------------------------------------
  112.  
  113.   Cz80_Set_INPort(&CZ80, your_z80readport_function);
  114.   Cz80_Set_OUTPort(&CZ80, your_z80writport_function);
  115.    
  116. Your readPort function need to be of CZ80_READ type :
  117. typedef u32  FASTCALL CZ80_READ(u32 adr);
  118.  
  119. Your writePort function need to be of CZ80_WRITE type :
  120. typedef void FASTCALL CZ80_WRITE(u32 adr, u32 data);
  121.  
  122.  
  123. 6) Set Up your interrupt callback function :
  124. --------------------------------------------
  125.  
  126.   Cz80_Set_IRQ_Callback(&CZ80, your_z80irqcallback_function);
  127.  
  128. Your IRQ callback function need to be of CZ80_INT_CALLBACK type :
  129. typedef s32  FASTCALL CZ80_INT_CALLBACK(s32 param);
  130.  
  131. If you don't understand what i am talking about here, just ignore...
  132. it's not needed in almost case.
  133.  
  134.  
  135. 6) Set Up your RETI callback function :
  136. ---------------------------------------
  137.  
  138.   Cz80_Set_RETI_Callback(&CZ80, your_z80reticallback_function);
  139.  
  140. Your RETI callback function need to be of CZ80_RETI_CALLBACKtype :
  141. typedef void FASTCALL CZ80_RETI_CALLBACK();
  142.  
  143. Again, if you don't understand what i am talking about here, ignore...
  144.  
  145.  
  146. 7) Reset the CZ80 core before fisrt use :
  147. -----------------------------------------
  148.  
  149.   Cz80_Reset(&CZ80);
  150.  
  151.  
  152. 8) Do some cycles :
  153. -------------------
  154.  
  155. Then it's time to really do some work, if you want to execute 1000 cycles, just do :
  156.  
  157.   cycles_done = Cz80_Exec(&CZ80, 1000);
  158.  
  159. Cz80_Exec function return the number of cycles actually done.
  160. Since each instruction take more than 1 cycle, Cz80_Exec will execute a bit more than
  161. you requested, for instance here, it can return 1008 cycles instead of 1000.
  162. In this case, adjust the number of cycle to do like that :
  163.  
  164.   cycles_by_frame = 4800;
  165.   extra_cycles = 0;
  166.   while (true)
  167.   {
  168.      ...
  169.      extra_cycles = CZ80_Exec(&CZ80, cycles_by_frame - extra_cycles) - cycles_by_frame;
  170.      ...
  171.   }
  172.  
  173. If Cz80_Exec returns a negatif value, an error occured.
  174.  
  175.  
  176. 9) Do an interrupt request :
  177. ----------------------------
  178.  
  179.   Cz80_Set_IRQ(&CZ80, 0);
  180.  
  181. or for a NMI :
  182.  
  183.   Cz80_Set_NMI(&CZ80);
  184.  
  185.  
  186. 10) Cancel an interrupt request :
  187. ---------------------------------
  188.  
  189.   Cz80_Clear_IRQ(&CZ80);
  190.  
  191. or for a NMI :
  192.  
  193.   Cz80_Clear_NMI(&CZ80);
  194.  
  195.  
  196.  
  197. * Switchs
  198. *********
  199.  
  200. There are severals switchs in the cz80.h file which permit you to configure
  201. CZ80 depending your needs.
  202.  
  203. - CZ80_FETCH_BITS (default = 4)
  204.  
  205. This defines the number of bits to select fetch region.
  206. This value must be 4 <= X <= 12
  207. Greater value offers permit to have more fetch region.
  208. In almost case, 4 is enough, but if you have fetch region smaller than 0x1000 bytes,
  209. increase this value.
  210.  
  211. - CZ80_LITTLE_ENDIAN
  212.  
  213. Define the endianess of the target platform.
  214. x86 CPU use Little Endian.
  215.  
  216. - CZ80_USE_JUMPTABLE
  217.  
  218. Set it to 1 to use Jump table instead of big case statement.
  219. This can bring some small speed improvemen.
  220. Be careful, some compiler doesn't support (computed label) so it's
  221. saffer to not use it.
  222.  
  223. - CZ80_SIZE_OPT
  224.  
  225. Add some extras optimisation for the code size versus speed.
  226. Minor changes anyway...
  227.  
  228. - CZ80_USE_WORD_HANDLER
  229.  
  230. See the "Set Up Memory" section for more détail.
  231.  
  232. - CZ80_EXACT
  233.  
  234. Enable accurate emulation of extended undocumented opcode and flags.
  235. minor speed decrease when activated.
  236. Even without that flag, CZ80 is already uite accurate, keep it
  237. disable unless you need it or if speed isn't important for you.
  238.  
  239. - CZ80_DEBUG
  240.  
  241. Used by me, keep it disable :p
  242.  
  243.  
  244.  
  245. * History
  246. *********
  247.  
  248. Version 0.90 :
  249. --------------
  250.  
  251. * Initial release for debugging purpose ^^
  252.