Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.12 KB | None | 0 0
  1. local ffi = require("ffi")
  2.  
  3. ffi.cdef[[
  4.  
  5. /* =============================================================================
  6.  *  libz80 - Z80 emulation library
  7.  * =============================================================================
  8.  *
  9.  * (C) Gabriel Gambetta (gabriel.gambetta@gmail.com) 2000 - 2012
  10.  *
  11.  * Version 2.1.0
  12.  *
  13.  * -----------------------------------------------------------------------------
  14.  *
  15.  *  This program is free software; you can redistribute it and/or modify
  16.  *  it under the terms of the GNU General Public License as published by
  17.  *  the Free Software Foundation; either version 2 of the License, or
  18.  *  (at your option) any later version.
  19.  *
  20.  *  This program is distributed in the hope that it will be useful,
  21.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  *  GNU General Public License for more details.
  24.  *
  25.  *  You should have received a copy of the GNU General Public License
  26.  *  along with this program; if not, write to the Free Software
  27.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  28.  */
  29.  
  30. /* modified from libz80.h for minetest by J. Aldo G. de F. Junior 2014 */
  31.  
  32. typedef unsigned short ushort;
  33. typedef unsigned char byte;
  34.  
  35. typedef byte (*Z80DataIn)   (int param, ushort address);
  36. typedef void (*Z80DataOut)  (int param, ushort address, byte data);
  37.  
  38. typedef union
  39. {
  40.     struct
  41.     {
  42.         ushort AF, BC, DE, HL, IX, IY, SP;
  43.     } wr;
  44.    
  45.     struct
  46.     {
  47.         byte F, A, C, B, E, D, L, H, IXl, IXh, IYl, IYh;
  48.     } br;
  49. } Z80Regs;
  50.  
  51. typedef enum
  52. {
  53.     F_C  =   1,
  54.     F_N  =   2,
  55.     F_PV =   4,
  56.     F_3  =   8,
  57.     F_H  =  16,
  58.     F_5  =  32,
  59.     F_Z  =  64,
  60.     F_S  = 128  
  61. } Z80Flags;
  62.  
  63. typedef struct
  64. {
  65.     Z80Regs     R1;
  66.     Z80Regs     R2;
  67.     ushort      PC;
  68.     byte        R;
  69.     byte        I;
  70.     byte        IFF1;
  71.     byte        IFF2;
  72.     byte        IM;    
  73.     Z80DataIn   memRead;
  74.     Z80DataOut  memWrite;
  75.     int         memParam;
  76.     Z80DataIn   ioRead;
  77.     Z80DataOut  ioWrite;
  78.     int         ioParam;
  79.     byte        halted;
  80.     unsigned    tstates;
  81.     byte        nmi_req;
  82.     byte        int_req;
  83.     byte        defer_int;
  84.     byte        int_vector;
  85.     byte        exec_int_vector;
  86. } Z80Context;
  87.  
  88. /* Low level library routines */
  89.  
  90. void Z80Execute (Z80Context* ctx);
  91. unsigned Z80ExecuteTStates(Z80Context* ctx, unsigned tstates);
  92. void Z80Debug (Z80Context* ctx, char* dump, char* decode);
  93. void Z80RESET (Z80Context* ctx);
  94. void Z80INT (Z80Context* ctx, byte value);
  95. void Z80NMI (Z80Context* ctx);
  96.  
  97. /* End of low level routines */
  98.  
  99. typedef enum
  100. {
  101.     IO_MemRead = 1,
  102.     IO_MemWrite = 2,
  103.     IO_BusRead = 4,
  104.     IO_BusWrite = 8
  105. } IOKind;
  106.  
  107. typedef struct
  108. {
  109.     IOKind      ikind;
  110.     ushort      iaddress;
  111.     byte        idata;
  112. } IOContext;
  113.  
  114. typedef struct
  115. {
  116.     byte        MemSpace[65536];
  117.     byte        IOSpace[65536];
  118.     IOContext   IOInfo;
  119.     Z80Context  Context;
  120. } Z80Machine;
  121.  
  122. Z80Machine machine;
  123.  
  124. byte IOZ80DataIn(int param, ushort address)
  125. {
  126.     machine.IOInfo.ikind = IO_BusRead;
  127.     machine.IOInfo.iaddress = address;
  128.     machine.IOInfo.idata = machine.IOSpace[address];
  129.     return machine.IOInfo.idata;
  130. };
  131.  
  132. void IOZ80DataOut(int param, ushort address, byte data)
  133. {
  134.     machine.IOInfo.ikind = IO_BusWrite;
  135.     machine.IOInfo.iaddress = address;
  136.     machine.IOInfo.idata = data;
  137.     machine.IOSpace[address] = data;
  138. };
  139.  
  140. byte MemZ80DataIn(int param, ushort address)
  141. {
  142.     machine.IOInfo.ikind = IO_MemRead;
  143.     machine.IOInfo.iaddress = address;
  144.     machine.IOInfo.idata = 0;
  145.     return machine.MemSpace[address];  
  146. };
  147.  
  148. void MemZ80DataOut(int param, ushort address, byte data)
  149. {
  150.     machine.IOInfo.ikind = IO_MemWrite;
  151.     machine.IOInfo.iaddress = address;
  152.     machine.IOInfo.idata = data;
  153.     machine.MemSpace[address] = data;
  154. };
  155.  
  156. /* Use those functions to run the emulator */
  157.  
  158. void Z80MachineStart()
  159. {
  160.     machine.Z80Context.memRead = &MemZ80DataIn;
  161.     machine.Z80Context.memWrite = &MemZ80DataOut;
  162.     machine.Z80Context.ioRead = &IOZ80DataIn;
  163.     machine.Z80Context.ioWrite = &MemZ80DataOut;
  164.     Z80RESET(&machine.Z80Context);
  165. };
  166.  
  167. void Z80MachineCycle()
  168. {
  169.     Z80Execute(&machine.Z80Context);
  170. };
  171.  
  172. void Z80GenerateInterrupt(byte value)
  173. {
  174.     Z80Int(&machine.Z80Context, value);
  175. };
  176.  
  177. void Z80GenerateNMI()
  178. {
  179.     Z80NMI(&machine.Z80Context)
  180. };
  181.  
  182. ]]
  183.  
  184. local libz80 = ffi.load('z80');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement