OSDN Git Service

[VM][DEVICE][I386] Add bios_call_far_ia32() and bios_int_ia32() because BIOS calling...
[csp-qt/common_source_project-fm7.git] / source / src / vm / z80.h
1 /*
2         Skelton for retropc emulator
3
4         Origin : MAME 0.145
5         Author : Takeda.Toshiya
6         Date   : 2012.02.15-
7
8         [ Z80 ]
9 */
10
11 #ifndef _Z80_H_ 
12 #define _Z80_H_
13
14 #include "device.h"
15
16 //#ifdef HAS_NSC800
17 #define SIG_NSC800_INT  0
18 #define SIG_NSC800_RSTA 1
19 #define SIG_NSC800_RSTB 2
20 #define SIG_NSC800_RSTC 3
21 //#endif
22 //#if defined(USE_SHARED_DLL) || defined(USE_QT)
23 #define Z80_INLINE
24 //#else
25 //#define Z80_INLINE inline
26 //#endif
27 //#ifdef USE_DEBUGGER
28 class DEBUGGER;
29 //#endif
30 class Z80_BASE : public DEVICE
31 {
32 protected:
33         /* ---------------------------------------------------------------------------
34         contexts
35         --------------------------------------------------------------------------- */
36         
37         DEVICE *d_mem, *d_io, *d_pic;
38 //#ifdef Z80_PSEUDO_BIOS
39         DEVICE *d_bios;
40 //#endif
41 //#ifdef SINGLE_MODE_DMA
42         DEVICE *d_dma;
43 //#endif
44 //#ifdef USE_DEBUGGER
45         DEBUGGER *d_debugger;
46         DEVICE *d_mem_stored, *d_io_stored;
47 //#endif
48         outputs_t outputs_busack;
49
50         bool has_nsc800;
51         bool has_memory_wait;
52         bool has_io_wait;
53         bool has_pseudo_bios;
54         bool has_ldair_quirk;
55         bool has_single_mode_dma;
56         bool flags_initialized;
57         
58         
59         bool is_primary;
60         
61         /* ---------------------------------------------------------------------------
62         registers
63         --------------------------------------------------------------------------- */
64         
65         uint64_t total_icount;
66         uint64_t prev_total_icount;
67         int icount;
68         int extra_icount;
69         int busreq_icount;
70         uint16_t prevpc;
71         pair32_t pc, sp, af, bc, de, hl, ix, iy, wz;
72         pair32_t af2, bc2, de2, hl2;
73         uint8_t I, R, R2;
74         uint32_t ea;
75         
76         bool busreq, after_halt;
77         uint8_t im, iff1, iff2, icr;
78         bool after_ei, after_ldair;
79         uint32_t intr_req_bit, intr_pend_bit;
80         
81         Z80_INLINE uint8_t RM8(uint32_t addr);
82         Z80_INLINE void WM8(uint32_t addr, uint8_t val);
83         Z80_INLINE void RM16(uint32_t addr, pair32_t *r);
84         Z80_INLINE void WM16(uint32_t addr, pair32_t *r);
85         Z80_INLINE uint8_t FETCHOP();
86         Z80_INLINE uint8_t FETCH8();
87         Z80_INLINE uint32_t FETCH16();
88         Z80_INLINE uint8_t IN8(uint32_t addr);
89         Z80_INLINE void OUT8(uint32_t addr, uint8_t val);
90         
91         Z80_INLINE uint8_t INC(uint8_t value);
92         Z80_INLINE uint8_t DEC(uint8_t value);
93         
94         Z80_INLINE uint8_t RLC(uint8_t value);
95         Z80_INLINE uint8_t RRC(uint8_t value);
96         Z80_INLINE uint8_t RL(uint8_t value);
97         Z80_INLINE uint8_t RR(uint8_t value);
98         Z80_INLINE uint8_t SLA(uint8_t value);
99         Z80_INLINE uint8_t SRA(uint8_t value);
100         Z80_INLINE uint8_t SLL(uint8_t value);
101         Z80_INLINE uint8_t SRL(uint8_t value);
102         
103         Z80_INLINE uint8_t RES(uint8_t bit, uint8_t value);
104         Z80_INLINE uint8_t SET(uint8_t bit, uint8_t value);
105         
106         void OP_CB(uint8_t code);
107         void OP_XY(uint8_t code);
108         void OP_DD(uint8_t code);
109         void OP_FD(uint8_t code);
110         void OP_ED(uint8_t code);
111         void OP(uint8_t code);
112         virtual void run_one_opecode();
113         virtual void debugger_hook(void);
114         
115         uint8_t SZ[256];                /* zero and sign flags */
116         uint8_t SZ_BIT[256];    /* zero, sign and parity/overflow (=zero) flags for BIT opcode */
117         uint8_t SZP[256];               /* zero, sign and parity flags */
118         uint8_t SZHV_inc[256];  /* zero, sign, half carry and overflow flags INC r8 */
119         uint8_t SZHV_dec[256];  /* zero, sign, half carry and overflow flags DEC r8 */
120
121         uint8_t SZHVC_add[2 * 256 * 256];
122         uint8_t SZHVC_sub[2 * 256 * 256];
123
124         const uint8_t cc_op[0x100] = {
125                 4,10, 7, 6, 4, 4, 7, 4, 4,11, 7, 6, 4, 4, 7, 4,
126                 8,10, 7, 6, 4, 4, 7, 4,12,11, 7, 6, 4, 4, 7, 4,
127                 7,10,16, 6, 4, 4, 7, 4, 7,11,16, 6, 4, 4, 7, 4,
128                 7,10,13, 6,11,11,10, 4, 7,11,13, 6, 4, 4, 7, 4,
129                 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
130                 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
131                 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
132                 7, 7, 7, 7, 7, 7, 4, 7, 4, 4, 4, 4, 4, 4, 7, 4,
133                 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
134                 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
135                 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
136                 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
137                 5,10,10,10,10,11, 7,11, 5,10,10, 0,10,17, 7,11,
138                 5,10,10,11,10,11, 7,11, 5, 4,10,11,10, 0, 7,11,
139                 5,10,10,19,10,11, 7,11, 5, 4,10, 4,10, 0, 7,11,
140                 5,10,10, 4,10,11, 7,11, 5, 6,10, 4,10, 0, 7,11
141         };
142
143          const uint8_t cc_cb[0x100] = {
144                 8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
145                 8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
146                 8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
147                 8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
148                 8, 8, 8, 8, 8, 8,12, 8, 8, 8, 8, 8, 8, 8,12, 8,
149                 8, 8, 8, 8, 8, 8,12, 8, 8, 8, 8, 8, 8, 8,12, 8,
150                 8, 8, 8, 8, 8, 8,12, 8, 8, 8, 8, 8, 8, 8,12, 8,
151                 8, 8, 8, 8, 8, 8,12, 8, 8, 8, 8, 8, 8, 8,12, 8,
152                 8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
153                 8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
154                 8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
155                 8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
156                 8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
157                 8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
158                 8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8,
159                 8, 8, 8, 8, 8, 8,15, 8, 8, 8, 8, 8, 8, 8,15, 8
160         };
161         
162          const uint8_t cc_ed[0x100] = {
163                 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
164                 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
165                 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
166                 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
167                 12,12,15,20, 8,14, 8, 9,12,12,15,20, 8,14, 8, 9,
168                 12,12,15,20, 8,14, 8, 9,12,12,15,20, 8,14, 8, 9,
169                 12,12,15,20, 8,14, 8,18,12,12,15,20, 8,14, 8,18,
170                 12,12,15,20, 8,14, 8, 8,12,12,15,20, 8,14, 8, 8,
171                 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
172                 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
173                 16,16,16,16, 8, 8, 8, 8,16,16,16,16, 8, 8, 8, 8,
174                 16,16,16,16, 8, 8, 8, 8,16,16,16,16, 8, 8, 8, 8,
175                 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
176                 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
177                 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
178                 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
179         };
180         
181          const uint8_t cc_xy[0x100] = {
182                 4, 4, 4, 4, 4, 4, 4, 4, 4,15, 4, 4, 4, 4, 4, 4,
183                 4, 4, 4, 4, 4, 4, 4, 4, 4,15, 4, 4, 4, 4, 4, 4,
184                 4,14,20,10, 9, 9,11, 4, 4,15,20,10, 9, 9,11, 4,
185                 4, 4, 4, 4,23,23,19, 4, 4,15, 4, 4, 4, 4, 4, 4,
186                 4, 4, 4, 4, 9, 9,19, 4, 4, 4, 4, 4, 9, 9,19, 4,
187                 4, 4, 4, 4, 9, 9,19, 4, 4, 4, 4, 4, 9, 9,19, 4,
188                 9, 9, 9, 9, 9, 9,19, 9, 9, 9, 9, 9, 9, 9,19, 9,
189                 19,19,19,19,19,19, 4,19, 4, 4, 4, 4, 9, 9,19, 4,
190                 4, 4, 4, 4, 9, 9,19, 4, 4, 4, 4, 4, 9, 9,19, 4,
191                 4, 4, 4, 4, 9, 9,19, 4, 4, 4, 4, 4, 9, 9,19, 4,
192                 4, 4, 4, 4, 9, 9,19, 4, 4, 4, 4, 4, 9, 9,19, 4,
193                 4, 4, 4, 4, 9, 9,19, 4, 4, 4, 4, 4, 9, 9,19, 4,
194                 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4,
195                 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
196                 4,14, 4,23, 4,15, 4, 4, 4, 8, 4, 4, 4, 4, 4, 4,
197                 4, 4, 4, 4, 4, 4, 4, 4, 4,10, 4, 4, 4, 4, 4, 4
198         };
199
200          const uint8_t cc_xycb[0x100] = {
201                 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
202                 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
203                 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
204                 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
205                 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
206                 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
207                 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
208                 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
209                 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
210                 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
211                 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
212                 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
213                 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
214                 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
215                 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,
216                 23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23
217         };
218         
219         const uint8_t cc_ex[0x100] = {
220                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
221                 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* DJNZ */
222                 5, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, /* JR NZ/JR Z */
223                 5, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, /* JR NC/JR C */
224                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
225                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
226                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
227                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
228                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
229                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
230                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
231                 5, 5, 5, 5, 0, 0, 0, 0, 5, 5, 5, 5, 0, 0, 0, 0, /* LDIR/CPIR/INIR/OTIR LDDR/CPDR/INDR/OTDR */
232                 6, 0, 0, 0, 7, 0, 0, 2, 6, 0, 0, 0, 7, 0, 0, 2,
233                 6, 0, 0, 0, 7, 0, 0, 2, 6, 0, 0, 0, 7, 0, 0, 2,
234                 6, 0, 0, 0, 7, 0, 0, 2, 6, 0, 0, 0, 7, 0, 0, 2,
235                 6, 0, 0, 0, 7, 0, 0, 2, 6, 0, 0, 0, 7, 0, 0, 2
236         };
237
238         /* ---------------------------------------------------------------------------
239         debug
240         --------------------------------------------------------------------------- */
241         // Collect counters
242         uint64_t cycles_tmp_count;
243         uint64_t extra_tmp_count;
244         uint32_t insns_count;
245         int frames_count;
246         int nmi_count;
247         int     irq_count;
248         int     nsc800_int_count;
249         int     nsc800_rsta_count;
250         int     nsc800_rstb_count;
251         int     nsc800_rstc_count;
252
253 public:
254         Z80_BASE(VM_TEMPLATE* parent_vm, EMU* parent_emu) : DEVICE(parent_vm, parent_emu)
255         {
256                 flags_initialized = false;
257                 busreq = false;
258 //#ifdef Z80_PSEUDO_BIOS
259                 d_bios = NULL;
260 //#endif
261 //#ifdef SINGLE_MODE_DMA
262                 d_dma = NULL;
263 //#endif
264                 d_debugger = NULL;
265                 d_mem_stored = NULL;
266                 d_io_stored = NULL;
267                 d_pic = NULL;
268                 has_nsc800 = false;
269                 has_io_wait = false;
270                 has_memory_wait = false;
271                 has_pseudo_bios = false;
272                 has_ldair_quirk = false;
273                 has_single_mode_dma = false;
274                 total_icount = prev_total_icount = 0;
275                 initialize_output_signals(&outputs_busack);
276                 is_primary = false;
277                 set_device_name(_T("Z80 CPU"));
278
279         }
280         ~Z80_BASE() {}
281         
282         // common functions
283         virtual void initialize();
284         virtual void reset();
285         void event_frame();
286         int run(int clock);
287
288         virtual bool process_state(FILEIO* state_fio, bool loading);
289         
290         void write_signal(int id, uint32_t data, uint32_t mask);
291         uint32_t read_signal(int id);
292         void set_intr_line(bool line, bool pending, uint32_t bit)
293         {
294                 uint32_t mask = 1 << bit;
295                 intr_req_bit = line ? (intr_req_bit | mask) : (intr_req_bit & ~mask);
296                 intr_pend_bit = pending ? (intr_pend_bit | mask) : (intr_pend_bit & ~mask);
297                 if(line) irq_count++;
298         }
299         void set_extra_clock(int clock)
300         {
301                 extra_icount += clock;
302         }
303         int get_extra_clock()
304         {
305                 return extra_icount;
306         }
307         uint32_t get_pc()
308         {
309                 return prevpc;
310         }
311         uint32_t get_next_pc()
312         {
313                 return pc.w.l;
314         }
315 //#ifdef USE_DEBUGGER
316         bool is_cpu()
317         {
318                 return true;
319         }
320         bool is_debugger_available()
321         {
322                 return true;
323         }
324         void *get_debugger()
325         {
326                 return d_debugger;
327         }
328         uint32_t get_debug_prog_addr_mask()
329         {
330                 return 0xffff;
331         }
332         uint32_t get_debug_data_addr_mask()
333         {
334                 return 0xffff;
335         }
336         bool write_debug_reg(const _TCHAR *reg, uint32_t data);
337         bool get_debug_regs_info(_TCHAR *buffer, size_t buffer_len);
338         virtual int debug_dasm(uint32_t pc, _TCHAR *buffer, size_t buffer_len);
339 //#endif
340         // unique functions
341         void set_context_mem(DEVICE* device)
342         {
343                 d_mem = device;
344         }
345         void set_context_io(DEVICE* device)
346         {
347                 d_io = device;
348         }
349         DEVICE *get_context_child()
350         {
351                 return d_pic;
352         }
353         void set_context_intr(DEVICE* device)
354         {
355                 d_pic = device;
356         }
357         void set_context_busack(DEVICE* device, int id, uint32_t mask)
358         {
359                 register_output_signal(&outputs_busack, device, id, mask);
360         }
361         void set_pc(uint16_t value)
362         {
363                 pc.w.l = value;
364         }
365         void set_sp(uint16_t value)
366         {
367                 sp.w.l = value;
368         }
369 };
370
371 class Z80 : public Z80_BASE 
372 {
373 protected:
374         void check_interrupt();
375         void run_one_opecode() override;
376         void debugger_hook(void) override;
377 public:
378         Z80(VM_TEMPLATE* parent_vm, EMU* parent_emu);
379         ~Z80();
380         void initialize();
381         void reset();
382         int run(int clock) override;
383
384         int debug_dasm(uint32_t pc, _TCHAR *buffer, size_t buffer_len);
385 #ifdef USE_DEBUGGER
386         void write_debug_data8(uint32_t addr, uint32_t data);
387         uint32_t read_debug_data8(uint32_t addr);
388         void write_debug_io8(uint32_t addr, uint32_t data);
389         uint32_t read_debug_io8(uint32_t addr);
390 #endif
391 #ifdef Z80_PSEUDO_BIOS
392         void set_context_bios(DEVICE* device)
393         {
394                 d_bios = device;
395         }
396 #endif
397 #ifdef SINGLE_MODE_DMA
398         void set_context_dma(DEVICE* device)
399         {
400                 d_dma = device;
401         }
402 #endif
403 #ifdef USE_DEBUGGER
404         void set_context_debugger(DEBUGGER* device)
405         {
406                 d_debugger = device;
407         }
408 #endif
409 };
410
411
412 #endif
413