1 #ifndef _Z80_H_
2 #define _Z80_H_
3 
4 #define	CPUINFO_PTR_CPU_SPECIFIC	0x18000
5 #define Z80_CLEAR_LINE		0
6 #define Z80_ASSERT_LINE		1
7 #define Z80_INPUT_LINE_NMI	32
8 
9 #include "z80daisy.h"
10 
11 typedef union
12 {
13 #ifdef LSB_FIRST
14 	struct { UINT8 l,h,h2,h3; } b;
15 	struct { UINT16 l,h; } w;
16 #else
17 	struct { UINT8 h3,h2,h,l; } b;
18 	struct { UINT16 h,l; } w;
19 #endif
20 	UINT32 d;
21 } Z80_PAIR;
22 
23 typedef struct
24 {
25 	Z80_PAIR	prvpc,pc,sp,af,bc,de,hl,ix,iy;
26 	Z80_PAIR	af2,bc2,de2,hl2,wz;
27 	UINT8	r,r2,iff1,iff2,halt,im,i;
28 	UINT8	nmi_state;			/* nmi line state */
29 	UINT8	nmi_pending;		/* nmi pending */
30 	UINT8	irq_state;			/* irq line state */
31 	UINT8   vector;             /* vector */
32 	UINT8	after_ei;			/* are we in the EI shadow? */
33 	INT32   cycles_left;
34 	INT32   ICount;
35 	INT32   end_run;
36 	UINT32  EA;
37 	INT32   hold_irq;
38 
39 	const struct z80_irq_daisy_chain *daisy;
40 	int (*irq_callback)(int irqline);
41 
42 	int (*spectrum_tape_cb)();
43 	int spectrum_mode;
44 } Z80_Regs;
45 
46 enum {
47 	Z80_PC=1, Z80_SP,
48 	Z80_A, Z80_B, Z80_C, Z80_D, Z80_E, Z80_H, Z80_L,
49 	Z80_AF, Z80_BC, Z80_DE, Z80_HL,
50 	Z80_IX, Z80_IY,	Z80_AF2, Z80_BC2, Z80_DE2, Z80_HL2,
51 	Z80_R, Z80_I, Z80_IM, Z80_IFF1, Z80_IFF2, Z80_HALT,
52 	Z80_DC0, Z80_DC1, Z80_DC2, Z80_DC3
53 };
54 
55 enum {
56 	Z80_TABLE_op,
57 	Z80_TABLE_cb,
58 	Z80_TABLE_ed,
59 	Z80_TABLE_xy,
60 	Z80_TABLE_xycb,
61 	Z80_TABLE_ex	/* cycles counts for taken jr/jp/call and interrupt latency (rst opcodes) */
62 };
63 
64 enum
65 {
66 	CPUINFO_PTR_Z80_CYCLE_TABLE = CPUINFO_PTR_CPU_SPECIFIC,
67 	CPUINFO_PTR_Z80_CYCLE_TABLE_LAST = CPUINFO_PTR_Z80_CYCLE_TABLE + Z80_TABLE_ex
68 };
69 
70 void Z80Init();
71 void Z80InitContention(int is_on_type, void (*rastercallback)(int));
72 void Z80Contention_set_bank(int bankno);
73 void Z80Reset();
74 void Z80Exit();
75 int  Z80Execute(int cycles);
76 void Z80Burn(int cycles);
77 void Z80SetIrqLine(int irqline, int state);
78 void Z80GetContext (void *dst);
79 void Z80SetContext (void *src);
80 int Z80Scan(int nAction);
81 INT32 z80TotalCycles();
82 void Z80StopExecute();
83 void z80_set_spectrum_tape_callback(int (*tape_cb)());
84 void z80_set_cycle_tables_msx();
85 
86 extern unsigned char Z80Vector;
87 extern void (*z80edfe_callback)(Z80_Regs *Regs);
88 extern int z80_ICount;
89 extern UINT32 EA;
90 
91 typedef unsigned char (__fastcall *Z80ReadIoHandler)(unsigned int a);
92 typedef void (__fastcall *Z80WriteIoHandler)(unsigned int a, unsigned char v);
93 typedef unsigned char (__fastcall *Z80ReadProgHandler)(unsigned int a);
94 typedef void (__fastcall *Z80WriteProgHandler)(unsigned int a, unsigned char v);
95 typedef unsigned char (__fastcall *Z80ReadOpHandler)(unsigned int a);
96 typedef unsigned char (__fastcall *Z80ReadOpArgHandler)(unsigned int a);
97 
98 void Z80SetIOReadHandler(Z80ReadIoHandler handler);
99 void Z80SetIOWriteHandler(Z80WriteIoHandler handler);
100 void Z80SetProgramReadHandler(Z80ReadProgHandler handler);
101 void Z80SetProgramWriteHandler(Z80WriteProgHandler handler);
102 void Z80SetCPUOpReadHandler(Z80ReadOpHandler handler);
103 void Z80SetCPUOpArgReadHandler(Z80ReadOpArgHandler handler);
104 
105 void ActiveZ80SetPC(int pc);
106 int ActiveZ80GetPC();
107 int ActiveZ80GetAF();
108 int ActiveZ80GetAF2();
109 void ActiveZ80SetAF2(int af2);
110 int ActiveZ80GetBC();
111 int ActiveZ80GetDE();
112 int ActiveZ80GetHL();
113 int ActiveZ80GetI();
114 int ActiveZ80GetIX();
115 int ActiveZ80GetIM();
116 int ActiveZ80GetSP();
117 int ActiveZ80GetPrevPC();
118 void ActiveZ80SetCarry(int carry);
119 int ActiveZ80GetCarry();
120 int ActiveZ80GetCarry2();
121 void ActiveZ80EXAF();
122 int ActiveZ80GetPOP();
123 int ActiveZ80GetA();
124 void ActiveZ80SetA(int a);
125 int ActiveZ80GetF();
126 void ActiveZ80SetF(int f);
127 int ActiveZ80GetIFF1();
128 int ActiveZ80GetIFF2();
129 void ActiveZ80SetDE(int de);
130 void ActiveZ80SetHL(int hl);
131 void ActiveZ80SetIX(int ix);
132 void ActiveZ80SetSP(int sp);
133 
134 void ActiveZ80SetIRQHold();
135 int ActiveZ80GetVector();
136 void ActiveZ80SetVector(INT32 vector);
137 
138 #define MAX_CMSE	9	//Maximum contended memory script elements
139 #define MAX_RWINFO	6	//Maximum reads/writes per opcode
140 #define MAX_CM_SCRIPTS 37
141 
142 enum CMSE_TYPES
143 {
144 	CMSE_TYPE_MEMORY,
145 	CMSE_TYPE_IO_PORT,
146 	CMSE_TYPE_IR_REGISTER,
147 	CMSE_TYPE_BC_REGISTER,
148 	CMSE_TYPE_UNCONTENDED
149 };
150 
151 enum ULA_VARIANT_TYPES
152 {
153 	ULA_VARIANT_NONE,
154 	ULA_VARIANT_SINCLAIR,
155 	ULA_VARIANT_AMSTRAD
156 };
157 
158 enum RWINFO_FLAGS
159 {
160 	RWINFO_READ      = 0x01,
161 	RWINFO_WRITE     = 0x02,
162 	RWINFO_IO_PORT   = 0x04,
163 	RWINFO_MEMORY    = 0x08,
164 	RWINFO_PROCESSED = 0x10
165 };
166 
167 typedef struct ContendedMemoryScriptElement
168 {
169 	int	rw_ix;
170 	int	inst_cycles;
171 	int     type;
172 	int	multiplier;
173 	bool	is_optional;
174 }CMSE;
175 
176 typedef struct ContendedMemoryScriptBreakdown
177 {
178 	CMSE elements[MAX_CMSE];
179 	int  number_of_elements;
180 	int  inst_cycles_mandatory;
181 	int  inst_cycles_optional;
182 	int  inst_cycles_total;
183 }CM_SCRIPT_BREAKDOWN;
184 
185 typedef struct ContendedMemoryScriptDescription
186 {
187 	const char*		sinclair;
188 	const char*		amstrad;
189 }CM_SCRIPT_DESCRIPTION;
190 
191 typedef struct ContendedMemoryScript
192 {
193 	int 			id;
194 	const char*		desc;
195 	CM_SCRIPT_BREAKDOWN	breakdown;
196 }CM_SCRIPT;
197 
198 typedef struct MemoryReadWriteInformation
199 {
200 	UINT16   addr;
201 	UINT8    val;
202         UINT16   flags;
203 	const char *dbg;
204 } RWINFO;
205 
206 typedef struct OpcodeHistory
207 {
208 	bool     capturing;
209 	RWINFO   rw[MAX_RWINFO];
210 	int      rw_count;
211 	int      tstate_start;
212 	UINT16 register_ir;
213 	UINT16 register_bc;
214 
215 	int 	 uncontended_cycles_predicted;
216 	int      uncontended_cycles_eaten;
217 	bool     do_optional;
218 
219 	CM_SCRIPT           *script;
220 	CM_SCRIPT_BREAKDOWN *breakdown;
221 	int                 element;
222 }OPCODE_HISTORY;
223 
224 enum CYCLES_TYPE
225 {
226 	CYCLES_ISR,		// Cycles eaten when processing interrupts
227 	CYCLES_EXEC,		// Cycles eaten when the EXEC() macro is called
228 	CYCLES_CONTENDED,	// Contended cycles eaten when processing opcode history (specz80_device only)
229 	CYCLES_UNCONTENDED	// Uncontended cycles eaten when processing opcode history (specz80_device only)
230 };
231 
232 #endif
233 
234