From 5601353fc9cd66dbcca4d06fc7068af853317442 Mon Sep 17 00:00:00 2001 From: Ian C Date: Fri, 26 Nov 2010 15:00:08 +0000 Subject: Copied DS81 files as a baseline --- include/config.h | 55 ++++++++++ include/ds81_debug.h | 43 ++++++++ include/framebuffer.h | 90 ++++++++++++++++ include/gui.h | 32 ++++++ include/keyboard.h | 142 ++++++++++++++++++++++++++ include/monitor.h | 29 ++++++ include/snapshot.h | 37 +++++++ include/stream.h | 35 +++++++ include/tapes.h | 27 +++++ include/textmode.h | 37 +++++++ include/touchwrap.h | 38 +++++++ include/z80.h | 257 ++++++++++++++++++++++++++++++++++++++++++++++ include/z80_config.h | 59 +++++++++++ include/z80_private.h | 276 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/zx81.h | 93 +++++++++++++++++ 15 files changed, 1250 insertions(+) create mode 100644 include/config.h create mode 100644 include/ds81_debug.h create mode 100644 include/framebuffer.h create mode 100644 include/gui.h create mode 100644 include/keyboard.h create mode 100644 include/monitor.h create mode 100644 include/snapshot.h create mode 100644 include/stream.h create mode 100644 include/tapes.h create mode 100644 include/textmode.h create mode 100644 include/touchwrap.h create mode 100644 include/z80.h create mode 100644 include/z80_config.h create mode 100644 include/z80_private.h create mode 100644 include/zx81.h (limited to 'include') diff --git a/include/config.h b/include/config.h new file mode 100644 index 0000000..3b99da6 --- /dev/null +++ b/include/config.h @@ -0,0 +1,55 @@ +/* + ds81 - Nintendo DS ZX81 emulator. + + Copyright (C) 2006 Ian Cowburn + + 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. + + $Id: config.h 65 2008-12-12 00:19:08Z ianc $ +*/ +#ifndef DS81_CONFIG_H +#define DS81_CONFIG_H + +/* Default snapshot dir +*/ +#define DEFAULT_SNAPDIR "/ZX81SNAP/" + +typedef enum +{ + DS81_STICKY_SHIFT, + DS81_AVERAGE_TOUCHSCREEN, + DS81_STATIC_RAM_AT_0x2000, + DS81_ALLOW_TAPE_SAVE, + DS81_LOAD_DEFAULT_SNAPSHOT, + DS81_NUM_CONFIG_ITEMS +} DS81_ConfigItem; + +/* Returns TRUE if config loaded from FAT device +*/ +int LoadConfig(void); + +/* Returns TRUE if config saved to FAT device +*/ +int SaveConfig(void); + +/* Gets a description for a config item. +*/ +const char *ConfigDesc(DS81_ConfigItem item); + +/* Table of configs. Done like this for simple performance reasons. +*/ +extern int DS81_Config[/*DS81_ConfigItem item*/]; + +#endif /* DS81_CONFIG_H */ diff --git a/include/ds81_debug.h b/include/ds81_debug.h new file mode 100644 index 0000000..fa79526 --- /dev/null +++ b/include/ds81_debug.h @@ -0,0 +1,43 @@ +/* + ds81 - Nintendo DS ZX81 emulator. + + Copyright (C) 2006 Ian Cowburn + + 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. + + $Id: ds81_debug.h 61 2008-11-03 17:07:32Z ianc $ +*/ +#ifndef DS81_DEBUG_H +#define DS81_DEBUG_H + +#include "gui.h" +#include "framebuffer.h" + +#define DS81_DEBUG(fmt, args...) \ + do \ + { \ + char tempdebug[512]; \ + sprintf(tempdebug, fmt, ## args); \ + GUI_Alert(FALSE, tempdebug); \ + } while(0) + +#define DS81_DEBUG_STATUS(fmt, args...) \ + do \ + { \ + FB_FillBox(0,184,256,8,COL_DARKGREY); \ + FB_printf(0,184,COL_WHITE,COL_DARKGREY, fmt , ## args); \ + } while(0) + +#endif /* DS81_DEBUG_H */ diff --git a/include/framebuffer.h b/include/framebuffer.h new file mode 100644 index 0000000..695bf44 --- /dev/null +++ b/include/framebuffer.h @@ -0,0 +1,90 @@ +/* + ds81 - Nintendo DS ZX81 emulator. + + Copyright (C) 2006 Ian Cowburn + + 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. + + $Id: framebuffer.h 43 2007-03-12 00:59:51Z ianc $ +*/ +#ifndef DS81_FRAMEBUFFER_H +#define DS81_FRAMEBUFFER_H + +/* Predefined colours. +*/ +typedef enum +{ + COL_TRANSPARENT = -1, + COL_BLACK = 0, + COL_WHITE = 240, + COL_RED = 241, + COL_GREEN = 242, + COL_BLUE = 243, + COL_GUISELECT = 244, + COL_GREY = 245, + COL_LIGHTGREY = 246, + COL_DARKGREY = 247, + COL_YELLOW = 248 +} FB_Colour; + + +/* Initialise 'framebuffer' code. vram is where the 8-bit framebuffer is. + palette is the palette to use/set. +*/ +void FB_Init(uint16 *vram, uint16 *palette); + +/* Gives access to the parameters of the frame buffer. +*/ +uint16 *FB_VRAM(void); +uint16 *FB_PALETTE(void); + +/* Load the internal framebuffer font as a set of ASCII tiles (starting with + space) at tiles. The tiles will use colour COL_WHITE. +*/ +void FB_LoadASCIITiles(uint16 *tiles); + +/* Print the text into the framebuffer. +*/ +void FB_Print(const char *text, int x, int y, + FB_Colour colour, FB_Colour paper); +void FB_Centre(const char *text, int y, + FB_Colour colour, FB_Colour paper); +void FB_printf(int x, int y, FB_Colour colour, FB_Colour paper, + const char *format, ...); + +/* Lines and boxes. +*/ +void FB_HLine(int x1, int x2, int y, FB_Colour colour); +void FB_VLine(int x, int y1, int y2, FB_Colour colour); +void FB_Box(int x, int y, int w, int h, FB_Colour colour); +void FB_FillBox(int x, int y, int w, int h, FB_Colour colour); + +/* Clear to background +*/ +void FB_Clear(void); + +/* Draw the image. The image must be an 8-bit image, but with only the first + 16 palette entries used. Just to complicate matters! + + The image is assumed to be an even number of pixels wide. Also the passed + X co-ord will be forced even. + + offset is used to give an offset into the palette to place colours from the + image. Palette entries 1 - 128 will always be safe to use (these routines + will never use them). +*/ +void FB_Blit(sImage *img, int x, int y, int offset); + +#endif /* DS81_FRAMEBUFFER_H */ diff --git a/include/gui.h b/include/gui.h new file mode 100644 index 0000000..06c68c3 --- /dev/null +++ b/include/gui.h @@ -0,0 +1,32 @@ +/* + ds81 - Nintendo DS ZX81 emulator. + + Copyright (C) 2006 Ian Cowburn + + 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. + + $Id: gui.h 64 2008-12-05 00:37:26Z ianc $ +*/ +#ifndef DS81_GUI_H +#define DS81_GUI_H + +int GUI_Menu(const char *opts[]); +void GUI_Alert(int fatal, const char *text); +void GUI_Config(void); +int GUI_FileSelect(char pwd[], char selected_file[], const char *filter); +int GUI_InputName(const char *prompt, const char *ext, + char name[], int maxlen); + +#endif /* DS81_GUI_H */ diff --git a/include/keyboard.h b/include/keyboard.h new file mode 100644 index 0000000..7b0e8b3 --- /dev/null +++ b/include/keyboard.h @@ -0,0 +1,142 @@ +/* + ds81 - Nintendo ZX81 emulator. + + Copyright (C) 2006 Ian Cowburn + + 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. + + $Id: keyboard.h 61 2008-11-03 17:07:32Z ianc $ +*/ +#ifndef DS81_KEYBOARD_H +#define DS81_KEYBOARD_H + +#include + +/* Note that the first 40 values purposefully are the keyboard matrix keys. + Note also that they are in display order, not matrix order. +*/ +typedef enum +{ + SK_1, + SK_2, + SK_3, + SK_4, + SK_5, + + SK_6, + SK_7, + SK_8, + SK_9, + SK_0, + + SK_Q, + SK_W, + SK_E, + SK_R, + SK_T, + + SK_Y, + SK_U, + SK_I, + SK_O, + SK_P, + + SK_A, + SK_S, + SK_D, + SK_F, + SK_G, + + SK_H, + SK_J, + SK_K, + SK_L, + SK_NEWLINE, + + SK_SHIFT, + SK_Z, + SK_X, + SK_C, + SK_V, + + SK_B, + SK_N, + SK_M, + SK_PERIOD, + SK_SPACE, + + SK_ABOUT, + SK_CONFIG, + SK_PAD_UP, + SK_PAD_DOWN, + SK_PAD_LEFT, + SK_PAD_RIGHT, + SK_PAD_A, + SK_PAD_B, + SK_PAD_X, + SK_PAD_Y, + SK_PAD_R, + SK_PAD_L, + SK_PAD_START, + SK_PAD_SELECT, + + NUM_SOFT_KEYS +} SoftKey; + +typedef struct +{ + SoftKey key; + int pressed; +} SoftKeyEvent; + + +/* Display the soft keyboard. +*/ +void SK_DisplayKeyboard(void); + +/* If dim is TRUE, then the keyboard is displayed with reduced brightness along + with the selection box. This routine simply adjusts the palette, and + assumes that the keyboard is already on display. +*/ +void SK_SetDisplayBrightness(int dim); + +/* Returns TRUE while there are still key events for this cycle +*/ +int SK_GetEvent(SoftKeyEvent *ev); + +/* Returns TRUE while there are still key events for this cycle. Unlike + SK_GetEvent this does not do joypad mappings. +*/ +int SK_GetBareEvent(SoftKeyEvent *ev); + +/* Sets a key to be 'sticky'. +*/ +void SK_SetSticky(SoftKey key, int is_sticky); + +/* Map the joypad to keys. Note that when mapped that both the key and the + joypad code will be generated. +*/ +void SK_DefinePad(SoftKey pad, SoftKey key); + +/* Returns a name for key symbols. +*/ +const char *SK_KeyName(SoftKey pad); + +/* Allows the keyboard to save/restore its state from a stream +*/ +void SK_SaveSnapshot(FILE *fp); +void SK_LoadSnapshot(FILE *fp); + +#endif /* DS81_KEYBOARD_H */ diff --git a/include/monitor.h b/include/monitor.h new file mode 100644 index 0000000..34f34e3 --- /dev/null +++ b/include/monitor.h @@ -0,0 +1,29 @@ +/* + ds81 - Nintendo DS ZX81 emulator. + + Copyright (C) 2007 Ian Cowburn + + 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. + + $Id: monitor.h 44 2007-03-14 01:00:54Z ianc $ +*/ +#ifndef DS81_MONITOR_H +#define DS81_MONITOR_H + +#include "z80.h" + +void MachineCodeMonitor(Z80 *cpu); + +#endif /* DS81_MONITOR_H */ diff --git a/include/snapshot.h b/include/snapshot.h new file mode 100644 index 0000000..38ec989 --- /dev/null +++ b/include/snapshot.h @@ -0,0 +1,37 @@ +/* + ds81 - Nintendo DS ZX81 emulator. + + Copyright (C) 2006 Ian Cowburn + + 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. + + $Id: snapshot.h 65 2008-12-12 00:19:08Z ianc $ +*/ +#ifndef DS81_SNAPSHOT_H +#define DS81_SNAPSHOT_H + +#include "z80.h" + +typedef enum +{ + SNAP_TYPE_FULL, + SNAP_TYPE_KEYBOARD +} SnapshotType; + +void SNAP_Enable(int enable); +void SNAP_Save(Z80 *cpu, SnapshotType type); +void SNAP_Load(Z80 *cpu, const char *optional_name, SnapshotType type); + +#endif /* DS81_SNAPSHOT_H */ diff --git a/include/stream.h b/include/stream.h new file mode 100644 index 0000000..2c3b915 --- /dev/null +++ b/include/stream.h @@ -0,0 +1,35 @@ +/* + ds81 - Nintendo DS ZX81 emulator. + + Copyright (C) 2006 Ian Cowburn + + 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. + + $Id: stream.h 64 2008-12-05 00:37:26Z ianc $ +*/ +#ifndef DS81_STREAM_H +#define DS81_STREAM_H + +#include + +void PUT_Byte(FILE *fp, unsigned char c); +void PUT_Long(FILE *fp, long l); +void PUT_ULong(FILE *fp, unsigned long l); + +unsigned char GET_Byte(FILE *fp); +long GET_Long(FILE *fp); +unsigned long GET_ULong(FILE *fp); + +#endif /* DS81_STREAM_H */ diff --git a/include/tapes.h b/include/tapes.h new file mode 100644 index 0000000..520e26e --- /dev/null +++ b/include/tapes.h @@ -0,0 +1,27 @@ +/* + ds81 - Nintendo DS ZX81 emulator. + + Copyright (C) 2006 Ian Cowburn + + 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. + + $Id: tapes.h 13 2006-10-12 16:38:57Z ianc $ +*/ +#ifndef DS81_TAPES_H +#define DS81_TAPES_H + +void SelectTape(void); + +#endif /* DS81_TAPES_H */ diff --git a/include/textmode.h b/include/textmode.h new file mode 100644 index 0000000..1e069fe --- /dev/null +++ b/include/textmode.h @@ -0,0 +1,37 @@ +/* + ds81 - Nintendo DS ZX81 emulator. + + Copyright (C) 2007 Ian Cowburn + + 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. + + $Id: textmode.h 41 2007-03-01 00:36:54Z ianc $ +*/ +#ifndef DS81_TEXTMODE_H +#define DS81_TEXTMODE_H + +/* Note that the co-ords are into the map -- the user is free to use this and + move the map around, scale it, blend it, do want they want with it... + + The routines assume they can write into this map using the ASCII code + with 32 subtracted for each char. +*/ +void TM_Init(uint16 *vram, int map_width, int map_height, int map_is_rotation); + +void TM_Cls(void); +void TM_Put(int x, int y, const char *str); +void TM_printf(int x, int y, const char *format, ...); + +#endif /* DS81_TEXTMODE_H */ diff --git a/include/touchwrap.h b/include/touchwrap.h new file mode 100644 index 0000000..5a0fe21 --- /dev/null +++ b/include/touchwrap.h @@ -0,0 +1,38 @@ +/* + ds81 - Nintendo DS ZX81 emulator. + + Copyright (C) 2006 Ian Cowburn + + 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. + + $Id: touchwrap.h 35 2007-02-16 01:10:41Z ianc $ +*/ +#ifndef DS81_TOUCHWRAP_H +#define DS81_TOUCHWRAP_H + +/* Don't know whether I have a problem with my DS or the library, but sometimes + the touchscreen value is off (one co-ord generally completely out). + + To alleviate this, and as this is a simple touch screen keyboard, allow + touchs to be averaged if the config says so. And averaged touch just means + that two touchs have to happen within 5 pixels on X and Y before being + allowed. + + If not configured to average, this simply reads the touchscreen position + and returns true. +*/ +int AllowTouch(touchPosition *tp); + +#endif /* DS81_TOUCHWRAP_H */ diff --git a/include/z80.h b/include/z80.h new file mode 100644 index 0000000..a00c8b0 --- /dev/null +++ b/include/z80.h @@ -0,0 +1,257 @@ +/* + + z80 - Z80 emulation + + Copyright (C) 2006 Ian Cowburn + + 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 + + ------------------------------------------------------------------------- + + $Id: z80.h 61 2008-11-03 17:07:32Z ianc $ + +*/ + +#ifndef Z80_H +#define Z80_H "$Id: z80.h 61 2008-11-03 17:07:32Z ianc $" + +#include + +/* Configuration +*/ +#include "z80_config.h" + + +/* ---------------------------------------- TYPES +*/ + +/* Large unsigned type +*/ +typedef unsigned long Z80Val; + + +/* 8-bit type. The emulation will exit with code 2 if this isn't 8 bits. +*/ +typedef unsigned char Z80Byte; + + +/* 8-bit signed type. The emulation will exit with code 2 if this isn't 8 bits. +*/ +typedef signed char Z80Relative; + + +/* 16-bit type. The emulation will exit with code 2 if this isn't 16 bits. +*/ +typedef unsigned short Z80Word; + + +/* A Z80 16-bit register. To access the HI/LO component use the indexes + Z80_HI_WORD and Z80_LO_WORD which will be initialised once Z80Init has been + called. +*/ +typedef union +{ + Z80Word w; + Z80Byte b[2]; +} Z80Reg; + +extern int Z80_HI_WORD; +extern int Z80_LO_WORD; + + +/* The processor +*/ +struct Z80Private; + +typedef struct +{ + Z80Word PC; + + Z80Reg AF; + Z80Reg BC; + Z80Reg DE; + Z80Reg HL; + + Z80Word AF_; + Z80Word BC_; + Z80Word DE_; + Z80Word HL_; + + Z80Reg IX; + Z80Reg IY; + + Z80Word SP; + + Z80Byte IFF1; + Z80Byte IFF2; + Z80Byte IM; + Z80Byte I; + Z80Byte R; + + struct Z80Private *priv; +} Z80; + + +/* Interfaces used to handle memory +*/ +typedef Z80Byte (*Z80ReadMemory)(Z80 *cpu, Z80Word address); +typedef void (*Z80WriteMemory)(Z80 *cpu, Z80Word address, Z80Byte value); + + +/* Interfaces needed to handle ports (IN/OUT commands) +*/ +typedef Z80Byte (*Z80ReadPort)(Z80 *cpu, Z80Word address); +typedef void (*Z80WritePort)(Z80 *cpu, Z80Word address, Z80Byte value); + + +/* Callback. Callback should return TRUE for processing to continue. +*/ +typedef int (*Z80Callback)(Z80 *cpu, Z80Val data); + + +/* Callback reasons + + eZ80_Instruction Called before the initial fetch for an instruction + (called just to once no matter how many bytes the + instruction is made up of). + + eZ80_EDHook Called when an undefined ED opcode is executed. + + eZ80_Halt Called when the HALT instruction is hit and released. + + eZ80_RETI Called when the RETI instruction is executed +*/ +typedef enum +{ + eZ80_Instruction, /* data = no cycles since reset */ + eZ80_EDHook, /* data = byte after ED opcode (only for NOP opcodes) */ + eZ80_Halt, /* data = 1 halt raised, 0 halt cleared by int */ + eZ80_RETI, /* data = ignored */ + eZ80_NO_CALLBACK /* leave at end */ +} Z80CallbackReason; + + +/* Flags in the F register +*/ +typedef enum +{ + eZ80_Carry =0x01, + eZ80_Neg =0x02, + eZ80_PV =0x04, + eZ80_Hidden3 =0x08, + eZ80_HalfCarry =0x10, + eZ80_Hidden5 =0x20, + eZ80_Zero =0x40, + eZ80_Sign =0x80 +} Z80FlagRegister; + + +/* Disassembly label -- only useful if ENABLE_DISASSEMBLER is set. + Labels are stored as an array, where a NULL in the label field marks + the end of the list. +*/ +typedef struct +{ + Z80Word address; + const char *label; +} Z80Label; + + +/* ---------------------------------------- INTERFACES +*/ + + +/* Initialises the processor. +*/ +#ifdef ENABLE_ARRAY_MEMORY +Z80 *Z80Init(Z80ReadPort read_port, + Z80WritePort write_port); +#else +Z80 *Z80Init(Z80ReadMemory read_memory, + Z80WriteMemory write_memory, + Z80ReadPort read_port, + Z80WritePort write_port, + Z80ReadMemory read_for_disassem); +#endif + + +/* Resets the processor. +*/ +void Z80Reset(Z80 *cpu); + + +/* Lodge a callback to be invoked after special events. Returns FALSE + if the callback couldn't be lodged (there is a max of 10 callbacks per + reason). +*/ +int Z80LodgeCallback(Z80 *cpu, + Z80CallbackReason reason, + Z80Callback callback); + + +/* Remove a callback. Does nothing if reason was not lodged with + Z80LodgeCallback() +*/ +void Z80RemoveCallback(Z80 *cpu, + Z80CallbackReason reason, + Z80Callback callback); + + +/* Cause an interrupt before the next opcode. + devbyte is the byte generated by the device (if any). +*/ +void Z80Interrupt(Z80 *cpu, Z80Byte devbyte); + + +/* Cause an NMI +*/ +void Z80NMI(Z80 *cpu); + + +/* Execute a single instruction. Returns FALSE if any callback returned + FALSE. +*/ +int Z80SingleStep(Z80 *cpu); + + +/* Executes until a callback returns FALSE (never returns otherwise) +*/ +void Z80Exec(Z80 *cpu); + + +/* Manipulate the cylce count of the Z80 +*/ +Z80Val Z80Cycles(Z80 *cpu); +void Z80ResetCycles(Z80 *cpu, Z80Val cycles); + + +/* Set address to label mappings for the disassembler +*/ +void Z80SetLabels(Z80Label labels[]); + + +/* Simple disassembly of memory accessed through read_for_disassem, or + Z80_MEMORY as appropriate. addr is updated on exit. +*/ +const char *Z80Disassemble(Z80 *cpu, Z80Word *addr); + +/* Allows the CPU state to be saved/loaded from a stream +*/ +void Z80SaveSnapshot(Z80 *cpu, FILE *fp); +void Z80LoadSnapshot(Z80 *cpu, FILE *fp); + +#endif + +/* END OF FILE */ diff --git a/include/z80_config.h b/include/z80_config.h new file mode 100644 index 0000000..467a789 --- /dev/null +++ b/include/z80_config.h @@ -0,0 +1,59 @@ +/* + + z80 - Z80 emulation + + Copyright (C) 2006 Ian Cowburn + + 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 + + ------------------------------------------------------------------------- + + $Id: z80_config.h 41 2007-03-01 00:36:54Z ianc $ + +*/ + +#ifndef Z80_CONFIG_H +#define Z80_CONFIG_H "$Id: z80_config.h 41 2007-03-01 00:36:54Z ianc $" + + +/* This file defines various compile-time configuration options + for the Z80 emulation +*/ + + +/* Define this to enable the disassembly interface +*/ +#define ENABLE_DISASSEM + + +/* Define this to enable the array-based memory model. In this mode + an externally visible Z80Byte array called Z80_MEMORY must be + defined. The macros RAMBOT and RAMTOP define the writable area of + memory and must be changed accordingly. + + In this mode the signature of Z80Init changes so that the memory functions + are not passed. ALL processor instances share the same memory. +#define ENABLE_ARRAY_MEMORY +*/ + +#ifdef ENABLE_ARRAY_MEMORY +#define RAMBOT 0x0000 +#define RAMTOP 0xffff +#endif + + +#endif + +/* END OF FILE */ diff --git a/include/z80_private.h b/include/z80_private.h new file mode 100644 index 0000000..f444632 --- /dev/null +++ b/include/z80_private.h @@ -0,0 +1,276 @@ +/* + + z80 - Z80 emulation + + Copyright (C) 2006 Ian Cowburn (ianc@noddybox.co.uk) + + 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 + + ------------------------------------------------------------------------- + + $Id: z80_private.h 13 2006-10-12 16:38:57Z ianc $ + + Private macros for Z80 + +*/ + +#ifndef Z80_PRIVATE_H +#define Z80_PRIVATE_H "$Id: z80_private.h 13 2006-10-12 16:38:57Z ianc $" + +#include "z80_config.h" + +#ifndef TRUE +#define TRUE 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif + +#define MAX_PER_CALLBACK 10 + + +/* ---------------------------------------- TYPES +*/ + +struct Z80Private +{ + Z80Val cycle; + + int halt; + + Z80Byte shift; + + int raise; + Z80Byte devbyte; + int nmi; + +#ifndef ENABLE_ARRAY_MEMORY + Z80ReadMemory disread; + + Z80ReadMemory mread; + Z80WriteMemory mwrite; +#endif + + Z80ReadPort pread; + Z80WritePort pwrite; + + Z80Callback callback[eZ80_NO_CALLBACK][MAX_PER_CALLBACK]; + + int last_cb; +}; + +#define PRIV cpu->priv + + +/* ---------------------------------------- ARRAY MEMORY +*/ + +#ifdef ENABLE_ARRAY_MEMORY +extern Z80Byte Z80_MEMORY[]; +#endif + + +/* ---------------------------------------- MACROS +*/ + +/* NOTE: A lot of these macros assume you have a variable called 'cpu' + which is a pointer to Z80 +*/ + + +/* Invoke a callback class +*/ +#define CALLBACK(r,d) do \ + { \ + int f; \ + \ + for(f=0;fcallback[r][f]) \ + PRIV->last_cb &= \ + PRIV->callback[r][f](cpu,d);\ + } while(0) + +/* Flag register +*/ +#define C_Z80 0x01 +#define N_Z80 0x02 +#define P_Z80 0x04 +#define V_Z80 P_Z80 +#define H_Z80 0x10 +#define Z_Z80 0x40 +#define S_Z80 0x80 + +#define B3_Z80 0x08 +#define B5_Z80 0x20 + + +#define SET(v,b) (v)|=b +#define CLR(v,b) (v)&=~(b) + +#define SETFLAG(f) SET(cpu->AF.b[LO],f) +#define CLRFLAG(f) CLR(cpu->AF.b[LO],f) + +#ifdef ENABLE_ARRAY_MEMORY + +#define PEEK(addr) Z80_MEMORY[addr] + +static inline Z80Word PEEKW(Z80Word addr) +{ + return (PEEK(addr) | (Z80Word)PEEK(addr+1)<<8); +} + +#define POKE(addr,val) do \ + { \ + Z80Word ba=addr; \ + if (ba>=RAMBOT && ba<=RAMTOP) \ + Z80_MEMORY[ba]=val; \ + } while(0) + +#define POKEW(addr,val) do \ + { \ + Z80Word wa=addr; \ + Z80Word wv=val; \ + POKE(wa,wv); \ + POKE(wa+1,wv>>8); \ + } while(0) + + +#define FETCH_BYTE (Z80_MEMORY[cpu->PC++]) +#define FETCH_WORD (cpu->PC+=2, \ + Z80_MEMORY[cpu->PC-2]| \ + ((Z80Word)Z80_MEMORY[cpu->PC-1]<<8)) + +#else + +#define PEEK(addr) (PRIV->mread(cpu,addr)) +#define PEEKW(addr) FPEEKW(cpu,addr) + +#define POKE(addr,val) PRIV->mwrite(cpu,addr,val) +#define POKEW(addr,val) FPOKEW(cpu,addr,val) + +#define FETCH_BYTE (PRIV->mread(cpu,cpu->PC++)) +#define FETCH_WORD (cpu->PC+=2,FPEEKW(cpu,cpu->PC-2)) + +#endif + + +#define IS_C (cpu->AF.b[LO]&C_Z80) +#define IS_N (cpu->AF.b[LO]&N_Z80) +#define IS_P (cpu->AF.b[LO]&P_Z80) +#define IS_H (cpu->AF.b[LO]&H_Z80) +#define IS_Z (cpu->AF.b[LO]&Z_Z80) +#define IS_S (cpu->AF.b[LO]&S_Z80) + +#define CARRY IS_C + +#define IS_IX_IY (PRIV->shift==0xdd || PRIV->shift==0xfd) +#define OFFSET(off) off=(IS_IX_IY ? (Z80Relative)FETCH_BYTE:0) + +#define TSTATE(n) PRIV->cycle+=n + +#define ADD_R(v) cpu->R=((cpu->R&0x80)|((cpu->R+(v))&0x7f)) +#define INC_R ADD_R(1) + +#ifdef ENABLE_ARRAY_MEMORY + +#define PUSH(REG) do \ + { \ + Z80Word pv=REG; \ + cpu->SP-=2; \ + POKE(cpu->SP,pv); \ + POKE(cpu->SP+1,pv>>8); \ + } while(0) + +#else + +#define PUSH(REG) do \ + { \ + Z80Word pushv=REG; \ + cpu->SP-=2; \ + PRIV->mwrite(cpu,cpu->SP,pushv); \ + PRIV->mwrite(cpu,cpu->SP+1,pushv>>8);\ + } while(0) +#endif + +#define POP(REG) do \ + { \ + REG=PEEK(cpu->SP) | \ + (Z80Word)PEEK(cpu->SP+1)<<8; \ + cpu->SP+=2; \ + } while(0) + +#define SETHIDDEN(res) cpu->AF.b[LO]=(cpu->AF.b[LO]&~(B3_Z80|B5_Z80))|\ + ((res)&(B3_Z80|B5_Z80)) + +#define CALL do \ + { \ + PUSH(cpu->PC+2); \ + cpu->PC=PEEKW(cpu->PC); \ + } while(0) + +#define NOCALL cpu->PC+=2 +#define JP cpu->PC=PEEKW(cpu->PC) +#define NOJP cpu->PC+=2 +#define JR cpu->PC+=(Z80Relative)PEEK(cpu->PC)+1 +#define NOJR cpu->PC++ + +#define OUT(P,V) do \ + { \ + if (PRIV->pwrite) \ + PRIV->pwrite(cpu,P,V); \ + } while(0) + +#define IN(P) (PRIV->pread?PRIV->pread(cpu,P):0) + + + +/* ---------------------------------------- LABELS +*/ +extern Z80Label *z80_labels; + + +/* ---------------------------------------- GLOBAL GENERAL OPCODES/ROUTINES +*/ +void Z80_Decode(Z80 *cpu, Z80Byte opcode); +void Z80_InitialiseInternals(void); + + +/* ---------------------------------------- DISASSEMBLY +*/ +#ifdef ENABLE_DISASSEM +typedef void (*DIS_OP_CALLBACK)(Z80 *z80, Z80Byte op, Z80Word *pc); + +extern DIS_OP_CALLBACK dis_CB_opcode[]; +extern DIS_OP_CALLBACK dis_DD_opcode[]; +extern DIS_OP_CALLBACK dis_DD_CB_opcode[]; +extern DIS_OP_CALLBACK dis_ED_opcode[]; +extern DIS_OP_CALLBACK dis_FD_opcode[]; +extern DIS_OP_CALLBACK dis_FD_CB_opcode[]; +extern DIS_OP_CALLBACK dis_opcode_z80[]; + +const char *Z80_Dis_Printf(const char *format, ...); + +Z80Byte Z80_Dis_FetchByte(Z80 *cpu, Z80Word *pc); +Z80Word Z80_Dis_FetchWord(Z80 *cpu, Z80Word *pc); + +void Z80_Dis_Set(const char *op, const char *arg); +const char *Z80_Dis_GetOp(void); +const char *Z80_Dis_GetArg(void); +#endif /* ENABLE_DISASSEM */ + +#endif /* Z80_PRIVATE_H */ + +/* END OF FILE */ diff --git a/include/zx81.h b/include/zx81.h new file mode 100644 index 0000000..1f77448 --- /dev/null +++ b/include/zx81.h @@ -0,0 +1,93 @@ +/* + + ds81 - Nintendo DS ZX81 emulator + + Copyright (C) 2006 Ian Cowburn (ianc@noddybox.co.uk) + + 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 + + ------------------------------------------------------------------------- + + Provides the emulation for the ZX81 + +*/ + +#ifndef DS81_ZX81_H +#define DS81_ZX81_H + +#include + +#include "z80.h" +#include "keyboard.h" + + +/* Initialise the ZX81 +*/ +void ZX81Init(uint16 *text_vram, uint16 *bitmap_vram, Z80 *z80); + +/* Handle keypresses +*/ +void ZX81HandleKey(SoftKey k, int is_pressed); + +/* Enable fopen() loading of tape files +*/ +void ZX81EnableFileSystem(int enable); + +/* Set a file to load from tape +*/ +void ZX81SetTape(const Z80Byte *image, int len); + +/* Reset the 81 +*/ +void ZX81Reset(Z80 *z80); + +/* Tell the 81 that config may have changed. +*/ +void ZX81Reconfigure(void); + +/* Displays a string on the ZX81's dislpay. The screen is cleared and the + string displayed with \n characters breaking the line. + + Not all characters can be respresented by the ZX81, and the screen will be + lost on the next emulation update cycle. + + The character '%' toggles inverse video. + + ZX81SuspendDisplay() and ZX81ResumeDisplay() should be called so that the + ZX81 can set up its internals. +*/ +void ZX81DisplayString(const char *p); +void ZX81SuspendDisplay(void); +void ZX81ResumeDisplay(void); + +/* Interfaces for the Z80 +*/ +Z80Byte ZX81ReadMem(Z80 *z80, Z80Word addr); +void ZX81WriteMem(Z80 *z80, Z80Word addr, Z80Byte val); +Z80Byte ZX81ReadPort(Z80 *z80, Z80Word port); +void ZX81WritePort(Z80 *z80, Z80Word port, Z80Byte val); + +#define ZX81ReadDisassem ZX81ReadMem + +/* Interfaces to allows the ZX81 to save/load itself as a snapshot to/from + a stream. +*/ +void ZX81SaveSnapshot(FILE *fp); +void ZX81LoadSnapshot(FILE *fp); + +#endif + + +/* END OF FILE */ -- cgit v1.2.3