diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/config.h | 52 | ||||
-rw-r--r-- | include/framebuffer.h | 120 | ||||
-rw-r--r-- | include/gui.h | 32 | ||||
-rw-r--r-- | include/keyboard.h | 135 | ||||
-rw-r--r-- | include/snapshot.h | 36 | ||||
-rw-r--r-- | include/spec.h | 82 | ||||
-rw-r--r-- | include/stream.h | 34 | ||||
-rw-r--r-- | include/z80.h | 256 | ||||
-rw-r--r-- | include/z80_config.h | 58 | ||||
-rw-r--r-- | include/z80_private.h | 275 |
10 files changed, 1080 insertions, 0 deletions
diff --git a/include/config.h b/include/config.h new file mode 100644 index 0000000..a6734f7 --- /dev/null +++ b/include/config.h @@ -0,0 +1,52 @@ +/* + 3dsspec - Nintendo 3DS Spectrum emulator. + + Copyright (C) 2021 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 3 + 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, see <http://www.gnu.org/licenses/>. + + $Id: config.h 65 2008-12-12 00:19:08Z ianc $ +*/ +#ifndef DSSPEC_CONFIG_H +#define DSSPEC_CONFIG_H + +/* Default snapshot dir +*/ +#define DEFAULT_SNAPDIR "/SPECTRUM/" + +typedef enum +{ + DSSPEC_STICKY_SHIFT, + DSSPEC_ALLOW_TAPE_SAVE, + DSSPEC_LOAD_DEFAULT_SNAPSHOT, + DSSPEC_NUM_CONFIG_ITEMS +} DSSPEC_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(DSSPEC_ConfigItem item); + +/* Table of configs. Done like this for simple performance reasons. +*/ +extern int DSSPEC_Config[/*DSSPEC_ConfigItem item*/]; + +#endif /* DSSPEC_CONFIG_H */ diff --git a/include/framebuffer.h b/include/framebuffer.h new file mode 100644 index 0000000..fe48385 --- /dev/null +++ b/include/framebuffer.h @@ -0,0 +1,120 @@ +/* + 3dsspec - Nintendo 3DS Spectrum emulator. + + Copyright (C) 2021 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 3 + 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, see <http://www.gnu.org/licenses/>. + + $Id: framebuffer.h 43 2007-03-12 00:59:51Z ianc $ +*/ +#ifndef DSSPEC_FRAMEBUFFER_H +#define DSSPEC_FRAMEBUFFER_H + +#include <3ds.h> + +/* Predefined colours. +*/ +typedef enum +{ + COL_TRANSPARENT = -1, + COL_BLACK = 0, + COL_WHITE = 1, + COL_RED = 2, + COL_GREEN = 3, + COL_BLUE = 4, + COL_GUISELECT = 5, + COL_GREY = 6, + COL_LIGHTGREY = 7, + COL_DARKGREY = 8, + COL_YELLOW = 9 +} FB_Colour; + +/* Predefined images +*/ +typedef enum +{ + IMG_KEYBOARD, + IMG_SPLASH +} FB_Image; + +/* A framebuffer or image +*/ +typedef struct +{ + u16 *buffer; + u16 width; + u16 height; +} Framebuffer; + +/* Macro to get the address of a framebuffer pixel +*/ +#define FB_ADDR(fb,x,y) (fb)->buffer[(x) * (fb)->height + (y)] + +/* Macro to plot a framebuffer pixel safely clippped +*/ +#define FB_PLOT_CLIPPED(fb,x,y,c) \ + do \ + { \ + if ((x) >= 0 && (x) < (fb)->width &&\ + (y) >= 0 && (y) < (fb)->height) \ + { \ + FB_ADDR(fb, x, y) = c; \ + } \ + } while(0) + +/* Initialise framebuffer code. +*/ +void FB_Init(void); + +/* Start a frame and setup the framebuffers. This can be called multiple times + per frame. Either pointer can be NULL if you're not interested in it. +*/ +void FB_StartFrame(Framebuffer *upper, Framebuffer *lower); + +/* Convenience function to flush the framebuffers, wait for a vsync and scan + HID input +*/ +void FB_EndFrame(void); + +/* Get the encoded value for a colour +*/ +u16 FB_GetColour(FB_Colour col); + +/* Print the text into the framebuffer. The text is inverted if the character + '\001' is found. +*/ +void FB_Print(Framebuffer *fb, const char *text, int x, int y, + FB_Colour colour, FB_Colour paper); +void FB_Centre(Framebuffer *fb, const char *text, int y, + FB_Colour colour, FB_Colour paper); +void FB_printf(Framebuffer *fb, int x, int y, + FB_Colour colour, FB_Colour paper, const char *format, ...); + +/* Lines and boxes. +*/ +void FB_HLine(Framebuffer *fb, u16 x1, u16 x2, u16 y, FB_Colour colour); +void FB_VLine(Framebuffer *fb, u16 x, u16 y1, u16 y2, FB_Colour colour); +void FB_Box(Framebuffer *fb, u16 x, u16 y, u16 w, u16 h, FB_Colour colour); +void FB_FillBox(Framebuffer *fb, u16 x, u16 y, u16 w, u16 h, + FB_Colour colour); + +/* Clear to colour +*/ +void FB_Clear(Framebuffer *fb, FB_Colour col); + +/* Draw the image. +*/ +void FB_Blit(Framebuffer *fb, FB_Image img, u16 x, u16 y); + +#endif /* DSSPEC_FRAMEBUFFER_H */ diff --git a/include/gui.h b/include/gui.h new file mode 100644 index 0000000..ed64ffe --- /dev/null +++ b/include/gui.h @@ -0,0 +1,32 @@ +/* + 3dsspec - Nintendo 3DS Spectrum emulator. + + Copyright (C) 2021 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 3 + 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, see <http://www.gnu.org/licenses/>. + + $Id: gui.h 64 2008-12-05 00:37:26Z ianc $ +*/ +#ifndef DSSPEC_GUI_H +#define DSSPEC_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 /* DSSPEC_GUI_H */ diff --git a/include/keyboard.h b/include/keyboard.h new file mode 100644 index 0000000..00fdcb6 --- /dev/null +++ b/include/keyboard.h @@ -0,0 +1,135 @@ +/* + 3dsspec - Nintendo 3DS Spectrum emulator. + + Copyright (C) 2021 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 3 + 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, see <http://www.gnu.org/licenses/>. + + $Id: keyboard.h 61 2008-11-03 17:07:32Z ianc $ +*/ +#ifndef DSSPEC_KEYBOARD_H +#define DSSPEC_KEYBOARD_H + +#include <stdio.h> + +/* 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); + +/* 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 /* DSSPEC_KEYBOARD_H */ diff --git a/include/snapshot.h b/include/snapshot.h new file mode 100644 index 0000000..9dc72d5 --- /dev/null +++ b/include/snapshot.h @@ -0,0 +1,36 @@ +/* + 3dsspec - Nintendo 3DS Spectrum emulator. + + Copyright (C) 2021 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 3 + 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, see <http://www.gnu.org/licenses/> + + $Id: snapshot.h 65 2008-12-12 00:19:08Z ianc $ +*/ +#ifndef DSSPEC_SNAPSHOT_H +#define DSSPEC_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 /* DSSPEC_SNAPSHOT_H */ diff --git a/include/spec.h b/include/spec.h new file mode 100644 index 0000000..b6b73eb --- /dev/null +++ b/include/spec.h @@ -0,0 +1,82 @@ +/* + + 3dsspec - Nintendo 3DS Spectrum emulator + + Copyright (C) 2021 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 3 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, see <http://www.gnu.org/licenses/> + + ------------------------------------------------------------------------- + + Provides the emulation for the Spectrum + +*/ + +#ifndef DSSPEC_SPEC_H +#define DSSPEC_SPEC_H + +#include <stdio.h> + +#include "framebuffer.h" +#include "z80.h" +#include "keyboard.h" + + +/* Initialise the SPEC +*/ +void SPECInit(Z80 *z80); + +/* Render the display +*/ +void SPECRenderDisplay(Framebuffer *fb, Z80 *z80); + +/* Handle keypresses +*/ +void SPECHandleKey(SoftKey k, int is_pressed); + +/* Enable fopen() loading of tape files +*/ +void SPECEnableFileSystem(int enable); + +/* Set a file to load from tape +*/ +void SPECSetTape(const Z80Byte *image, int len); + +/* Reset the spectrum +*/ +void SPECReset(Z80 *z80); + +/* Tell the spectrum that config may have changed. +*/ +void SPECReconfigure(void); + +/* Interfaces for the Z80 +*/ +Z80Byte SPECReadMem(Z80 *z80, Z80Word addr); +void SPECWriteMem(Z80 *z80, Z80Word addr, Z80Byte val); +Z80Byte SPECReadPort(Z80 *z80, Z80Word port); +void SPECWritePort(Z80 *z80, Z80Word port, Z80Byte val); + +#define SPECReadDisassem SPECReadMem + +/* Interfaces to allows the SPEC to save/load itself as a snapshot to/from + a stream. +*/ +void SPECSaveSnapshot(FILE *fp); +void SPECLoadSnapshot(FILE *fp); + +#endif + + +/* END OF FILE */ diff --git a/include/stream.h b/include/stream.h new file mode 100644 index 0000000..7c69369 --- /dev/null +++ b/include/stream.h @@ -0,0 +1,34 @@ +/* + 3dsspec - Nintendo 3DS Spectrum emulator. + + Copyright (C) 2021 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 3 + 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, see <http://www.gnu.org/licenses/> + + $Id: stream.h 64 2008-12-05 00:37:26Z ianc $ +*/ +#ifndef DSSPEC_STREAM_H +#define DSSPEC_STREAM_H + +#include <stdio.h> + +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 /* DSSPEC_STREAM_H */ diff --git a/include/z80.h b/include/z80.h new file mode 100644 index 0000000..ef24fd0 --- /dev/null +++ b/include/z80.h @@ -0,0 +1,256 @@ +/* + + z80 - Z80 emulation + + Copyright (C) 2021 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 3 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, see <http://www.gnu.org/licenses/> + + ------------------------------------------------------------------------- + + $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 <stdio.h> + +/* 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..8d9ee79 --- /dev/null +++ b/include/z80_config.h @@ -0,0 +1,58 @@ +/* + + z80 - Z80 emulation + + Copyright (C) 2021 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 3 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, see <http://www.gnu.org/licenses/> + + ------------------------------------------------------------------------- + + $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..10cf97e --- /dev/null +++ b/include/z80_private.h @@ -0,0 +1,275 @@ +/* + + z80 - Z80 emulation + + Copyright (C) 2021 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 3 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, see <http://www.gnu.org/licenses/> + + ------------------------------------------------------------------------- + + $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;f<MAX_PER_CALLBACK;f++) \ + if (PRIV->callback[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 */ |