Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ffi = require("ffi")
- ffi.cdef[[
- /* =============================================================================
- * libz80 - Z80 emulation library
- * =============================================================================
- *
- * (C) Gabriel Gambetta (gabriel.gambetta@gmail.com) 2000 - 2012
- *
- * Version 2.1.0
- *
- * -----------------------------------------------------------------------------
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- /* modified from libz80.h for minetest by J. Aldo G. de F. Junior 2014 */
- typedef unsigned short ushort;
- typedef unsigned char byte;
- typedef byte (*Z80DataIn) (int param, ushort address);
- typedef void (*Z80DataOut) (int param, ushort address, byte data);
- typedef union
- {
- struct
- {
- ushort AF, BC, DE, HL, IX, IY, SP;
- } wr;
- struct
- {
- byte F, A, C, B, E, D, L, H, IXl, IXh, IYl, IYh;
- } br;
- } Z80Regs;
- typedef enum
- {
- F_C = 1,
- F_N = 2,
- F_PV = 4,
- F_3 = 8,
- F_H = 16,
- F_5 = 32,
- F_Z = 64,
- F_S = 128
- } Z80Flags;
- typedef struct
- {
- Z80Regs R1;
- Z80Regs R2;
- ushort PC;
- byte R;
- byte I;
- byte IFF1;
- byte IFF2;
- byte IM;
- Z80DataIn memRead;
- Z80DataOut memWrite;
- int memParam;
- Z80DataIn ioRead;
- Z80DataOut ioWrite;
- int ioParam;
- byte halted;
- unsigned tstates;
- byte nmi_req;
- byte int_req;
- byte defer_int;
- byte int_vector;
- byte exec_int_vector;
- } Z80Context;
- /* Low level library routines */
- void Z80Execute (Z80Context* ctx);
- unsigned Z80ExecuteTStates(Z80Context* ctx, unsigned tstates);
- void Z80Debug (Z80Context* ctx, char* dump, char* decode);
- void Z80RESET (Z80Context* ctx);
- void Z80INT (Z80Context* ctx, byte value);
- void Z80NMI (Z80Context* ctx);
- /* End of low level routines */
- typedef enum
- {
- IO_MemRead = 1,
- IO_MemWrite = 2,
- IO_BusRead = 4,
- IO_BusWrite = 8
- } IOKind;
- typedef struct
- {
- IOKind ikind;
- ushort iaddress;
- byte idata;
- } IOContext;
- typedef struct
- {
- byte MemSpace[65536];
- byte IOSpace[65536];
- IOContext IOInfo;
- Z80Context Context;
- } Z80Machine;
- Z80Machine machine;
- byte IOZ80DataIn(int param, ushort address)
- {
- machine.IOInfo.ikind = IO_BusRead;
- machine.IOInfo.iaddress = address;
- machine.IOInfo.idata = machine.IOSpace[address];
- return machine.IOInfo.idata;
- };
- void IOZ80DataOut(int param, ushort address, byte data)
- {
- machine.IOInfo.ikind = IO_BusWrite;
- machine.IOInfo.iaddress = address;
- machine.IOInfo.idata = data;
- machine.IOSpace[address] = data;
- };
- byte MemZ80DataIn(int param, ushort address)
- {
- machine.IOInfo.ikind = IO_MemRead;
- machine.IOInfo.iaddress = address;
- machine.IOInfo.idata = 0;
- return machine.MemSpace[address];
- };
- void MemZ80DataOut(int param, ushort address, byte data)
- {
- machine.IOInfo.ikind = IO_MemWrite;
- machine.IOInfo.iaddress = address;
- machine.IOInfo.idata = data;
- machine.MemSpace[address] = data;
- };
- /* Use those functions to run the emulator */
- void Z80MachineStart()
- {
- machine.Z80Context.memRead = &MemZ80DataIn;
- machine.Z80Context.memWrite = &MemZ80DataOut;
- machine.Z80Context.ioRead = &IOZ80DataIn;
- machine.Z80Context.ioWrite = &MemZ80DataOut;
- Z80RESET(&machine.Z80Context);
- };
- void Z80MachineCycle()
- {
- Z80Execute(&machine.Z80Context);
- };
- void Z80GenerateInterrupt(byte value)
- {
- Z80Int(&machine.Z80Context, value);
- };
- void Z80GenerateNMI()
- {
- Z80NMI(&machine.Z80Context)
- };
- ]]
- local libz80 = ffi.load('z80');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement