]> git.sesse.net Git - pistorm/blob - m68kcpu.h
FPU update as latest MAME
[pistorm] / m68kcpu.h
1 /* ======================================================================== */
2 /* ========================= LICENSING & COPYRIGHT ======================== */
3 /* ======================================================================== */
4 /*
5  *                                  MUSASHI
6  *                                Version 4.5
7  *
8  * A portable Motorola M680x0 processor emulation engine.
9  * Copyright Karl Stenerud.  All rights reserved.
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27  * THE SOFTWARE.
28  */
29
30
31
32
33 #ifndef M68KCPU__HEADER
34 #define M68KCPU__HEADER
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 #include "m68k.h"
41
42 #include <limits.h>
43 #include <endian.h>
44
45 #include <setjmp.h>
46
47 /* ======================================================================== */
48 /* ==================== ARCHITECTURE-DEPENDANT DEFINES ==================== */
49 /* ======================================================================== */
50
51 /* Check for > 32bit sizes */
52 #if UINT_MAX > 0xffffffff
53         #define M68K_INT_GT_32_BIT  1
54 #else
55         #define M68K_INT_GT_32_BIT  0
56 #endif
57
58 /* Data types used in this emulation core */
59 #undef sint8
60 #undef sint16
61 #undef sint32
62 #undef sint64
63 #undef uint8
64 #undef uint16
65 #undef uint32
66 #undef uint64
67 #undef sint
68 #undef uint
69
70 typedef signed   char  sint8;           /* ASG: changed from char to signed char */
71 typedef signed   short sint16;
72 typedef signed   int   sint32;          /* AWJ: changed from long to int */
73 typedef unsigned char  uint8;
74 typedef unsigned short uint16;
75 typedef unsigned int   uint32;                  /* AWJ: changed from long to int */
76
77 /* signed and unsigned int must be at least 32 bits wide */
78 typedef signed   int sint;
79 typedef unsigned int uint;
80
81
82 #if M68K_USE_64_BIT
83 typedef signed   long long sint64;
84 typedef unsigned long long uint64;
85 #else
86 typedef sint32 sint64;
87 typedef uint32 uint64;
88 #endif /* M68K_USE_64_BIT */
89
90 /* U64 and S64 are used to wrap long integer constants. */
91 #ifdef __GNUC__
92 #define U64(val) val##ULL
93 #define S64(val) val##LL
94 #else
95 #define U64(val) val
96 #define S64(val) val
97 #endif
98
99 #include "softfloat/milieu.h"
100 #include "softfloat/softfloat.h"
101
102
103 /* Allow for architectures that don't have 8-bit sizes */
104 #if UCHAR_MAX == 0xff
105         #define MAKE_INT_8(A) (sint8)(A)
106 #else
107         #undef  sint8
108         #define sint8  signed   int
109         #undef  uint8
110         #define uint8  unsigned int
111         static inline sint MAKE_INT_8(uint value)
112         {
113                 return (value & 0x80) ? value | ~0xff : value & 0xff;
114         }
115 #endif /* UCHAR_MAX == 0xff */
116
117
118 /* Allow for architectures that don't have 16-bit sizes */
119 #if USHRT_MAX == 0xffff
120         #define MAKE_INT_16(A) (sint16)(A)
121 #else
122         #undef  sint16
123         #define sint16 signed   int
124         #undef  uint16
125         #define uint16 unsigned int
126         static inline sint MAKE_INT_16(uint value)
127         {
128                 return (value & 0x8000) ? value | ~0xffff : value & 0xffff;
129         }
130 #endif /* USHRT_MAX == 0xffff */
131
132
133 /* Allow for architectures that don't have 32-bit sizes */
134 #if UINT_MAX == 0xffffffff
135         #define MAKE_INT_32(A) (sint32)(A)
136 #else
137         #undef  sint32
138         #define sint32  signed   int
139         #undef  uint32
140         #define uint32  unsigned int
141         static inline sint MAKE_INT_32(uint value)
142         {
143                 return (value & 0x80000000) ? value | ~0xffffffff : value & 0xffffffff;
144         }
145 #endif /* UINT_MAX == 0xffffffff */
146
147
148
149
150 /* ======================================================================== */
151 /* ============================ GENERAL DEFINES =========================== */
152 /* ======================================================================== */
153
154 /* Exception Vectors handled by emulation */
155 #define EXCEPTION_RESET                    0
156 #define EXCEPTION_BUS_ERROR                2 /* This one is not emulated! */
157 #define EXCEPTION_ADDRESS_ERROR            3 /* This one is partially emulated (doesn't stack a proper frame yet) */
158 #define EXCEPTION_ILLEGAL_INSTRUCTION      4
159 #define EXCEPTION_ZERO_DIVIDE              5
160 #define EXCEPTION_CHK                      6
161 #define EXCEPTION_TRAPV                    7
162 #define EXCEPTION_PRIVILEGE_VIOLATION      8
163 #define EXCEPTION_TRACE                    9
164 #define EXCEPTION_1010                    10
165 #define EXCEPTION_1111                    11
166 #define EXCEPTION_FORMAT_ERROR            14
167 #define EXCEPTION_UNINITIALIZED_INTERRUPT 15
168 #define EXCEPTION_SPURIOUS_INTERRUPT      24
169 #define EXCEPTION_INTERRUPT_AUTOVECTOR    24
170 #define EXCEPTION_TRAP_BASE               32
171
172 /* Function codes set by CPU during data/address bus activity */
173 #define FUNCTION_CODE_USER_DATA          1
174 #define FUNCTION_CODE_USER_PROGRAM       2
175 #define FUNCTION_CODE_SUPERVISOR_DATA    5
176 #define FUNCTION_CODE_SUPERVISOR_PROGRAM 6
177 #define FUNCTION_CODE_CPU_SPACE          7
178
179 /* CPU types for deciding what to emulate */
180 #define CPU_TYPE_000    (0x00000001)
181 #define CPU_TYPE_008    (0x00000002)
182 #define CPU_TYPE_010    (0x00000004)
183 #define CPU_TYPE_EC020  (0x00000008)
184 #define CPU_TYPE_020    (0x00000010)
185 #define CPU_TYPE_EC030  (0x00000020)
186 #define CPU_TYPE_030    (0x00000040)
187 #define CPU_TYPE_EC040  (0x00000080)
188 #define CPU_TYPE_LC040  (0x00000100)
189 #define CPU_TYPE_040    (0x00000200)
190 #define CPU_TYPE_SCC070 (0x00000400)
191
192 /* Different ways to stop the CPU */
193 #define STOP_LEVEL_STOP 1
194 #define STOP_LEVEL_HALT 2
195
196 /* Used for 68000 address error processing */
197 #define INSTRUCTION_YES 0
198 #define INSTRUCTION_NO  0x08
199 #define MODE_READ       0x10
200 #define MODE_WRITE      0
201
202 #define RUN_MODE_NORMAL          0
203 #define RUN_MODE_BERR_AERR_RESET 1
204
205 #ifndef NULL
206 #define NULL ((void*)0)
207 #endif
208
209 /* ======================================================================== */
210 /* ================================ MACROS ================================ */
211 /* ======================================================================== */
212
213
214 /* ---------------------------- General Macros ---------------------------- */
215
216 /* Bit Isolation Macros */
217 #define BIT_0(A)  ((A) & 0x00000001)
218 #define BIT_1(A)  ((A) & 0x00000002)
219 #define BIT_2(A)  ((A) & 0x00000004)
220 #define BIT_3(A)  ((A) & 0x00000008)
221 #define BIT_4(A)  ((A) & 0x00000010)
222 #define BIT_5(A)  ((A) & 0x00000020)
223 #define BIT_6(A)  ((A) & 0x00000040)
224 #define BIT_7(A)  ((A) & 0x00000080)
225 #define BIT_8(A)  ((A) & 0x00000100)
226 #define BIT_9(A)  ((A) & 0x00000200)
227 #define BIT_A(A)  ((A) & 0x00000400)
228 #define BIT_B(A)  ((A) & 0x00000800)
229 #define BIT_C(A)  ((A) & 0x00001000)
230 #define BIT_D(A)  ((A) & 0x00002000)
231 #define BIT_E(A)  ((A) & 0x00004000)
232 #define BIT_F(A)  ((A) & 0x00008000)
233 #define BIT_10(A) ((A) & 0x00010000)
234 #define BIT_11(A) ((A) & 0x00020000)
235 #define BIT_12(A) ((A) & 0x00040000)
236 #define BIT_13(A) ((A) & 0x00080000)
237 #define BIT_14(A) ((A) & 0x00100000)
238 #define BIT_15(A) ((A) & 0x00200000)
239 #define BIT_16(A) ((A) & 0x00400000)
240 #define BIT_17(A) ((A) & 0x00800000)
241 #define BIT_18(A) ((A) & 0x01000000)
242 #define BIT_19(A) ((A) & 0x02000000)
243 #define BIT_1A(A) ((A) & 0x04000000)
244 #define BIT_1B(A) ((A) & 0x08000000)
245 #define BIT_1C(A) ((A) & 0x10000000)
246 #define BIT_1D(A) ((A) & 0x20000000)
247 #define BIT_1E(A) ((A) & 0x40000000)
248 #define BIT_1F(A) ((A) & 0x80000000)
249
250 /* Get the most significant bit for specific sizes */
251 #define GET_MSB_8(A)  ((A) & 0x80)
252 #define GET_MSB_9(A)  ((A) & 0x100)
253 #define GET_MSB_16(A) ((A) & 0x8000)
254 #define GET_MSB_17(A) ((A) & 0x10000)
255 #define GET_MSB_32(A) ((A) & 0x80000000)
256 #if M68K_USE_64_BIT
257 #define GET_MSB_33(A) ((A) & 0x100000000)
258 #endif /* M68K_USE_64_BIT */
259
260 /* Isolate nibbles */
261 #define LOW_NIBBLE(A)  ((A) & 0x0f)
262 #define HIGH_NIBBLE(A) ((A) & 0xf0)
263
264 /* These are used to isolate 8, 16, and 32 bit sizes */
265 #define MASK_OUT_ABOVE_2(A)  ((A) & 3)
266 #define MASK_OUT_ABOVE_8(A)  ((A) & 0xff)
267 #define MASK_OUT_ABOVE_16(A) ((A) & 0xffff)
268 #define MASK_OUT_BELOW_2(A)  ((A) & ~3)
269 #define MASK_OUT_BELOW_8(A)  ((A) & ~0xff)
270 #define MASK_OUT_BELOW_16(A) ((A) & ~0xffff)
271
272 /* No need to mask if we are 32 bit */
273 #if M68K_INT_GT_32_BIT || M68K_USE_64_BIT
274         #define MASK_OUT_ABOVE_32(A) ((A) & 0xffffffff)
275         #define MASK_OUT_BELOW_32(A) ((A) & ~0xffffffff)
276 #else
277         #define MASK_OUT_ABOVE_32(A) (A)
278         #define MASK_OUT_BELOW_32(A) 0
279 #endif /* M68K_INT_GT_32_BIT || M68K_USE_64_BIT */
280
281 /* Simulate address lines of 68k family */
282 #define ADDRESS_68K(A) ((A)&CPU_ADDRESS_MASK)
283
284
285 /* Shift & Rotate Macros. */
286 #define LSL(A, C) ((A) << (C))
287 #define LSR(A, C) ((A) >> (C))
288
289 /* Some > 32-bit optimizations */
290 #if M68K_INT_GT_32_BIT
291         /* Shift left and right */
292         #define LSR_32(A, C) ((A) >> (C))
293         #define LSL_32(A, C) ((A) << (C))
294 #else
295         /* We have to do this because the morons at ANSI decided that shifts
296          * by >= data size are undefined.
297          */
298         #define LSR_32(A, C) ((C) < 32 ? (A) >> (C) : 0)
299         #define LSL_32(A, C) ((C) < 32 ? (A) << (C) : 0)
300 #endif /* M68K_INT_GT_32_BIT */
301
302 #if M68K_USE_64_BIT
303         #define LSL_32_64(A, C) ((A) << (C))
304         #define LSR_32_64(A, C) ((A) >> (C))
305         #define ROL_33_64(A, C) (LSL_32_64(A, C) | LSR_32_64(A, 33-(C)))
306         #define ROR_33_64(A, C) (LSR_32_64(A, C) | LSL_32_64(A, 33-(C)))
307 #endif /* M68K_USE_64_BIT */
308
309 #define ROL_8(A, C)      MASK_OUT_ABOVE_8(LSL(A, C) | LSR(A, 8-(C)))
310 #define ROL_9(A, C)                      (LSL(A, C) | LSR(A, 9-(C)))
311 #define ROL_16(A, C)    MASK_OUT_ABOVE_16(LSL(A, C) | LSR(A, 16-(C)))
312 #define ROL_17(A, C)                     (LSL(A, C) | LSR(A, 17-(C)))
313 #define ROL_32(A, C)    MASK_OUT_ABOVE_32(LSL_32(A, C) | LSR_32(A, 32-(C)))
314 #define ROL_33(A, C)                     (LSL_32(A, C) | LSR_32(A, 33-(C)))
315
316 #define ROR_8(A, C)      MASK_OUT_ABOVE_8(LSR(A, C) | LSL(A, 8-(C)))
317 #define ROR_9(A, C)                      (LSR(A, C) | LSL(A, 9-(C)))
318 #define ROR_16(A, C)    MASK_OUT_ABOVE_16(LSR(A, C) | LSL(A, 16-(C)))
319 #define ROR_17(A, C)                     (LSR(A, C) | LSL(A, 17-(C)))
320 #define ROR_32(A, C)    MASK_OUT_ABOVE_32(LSR_32(A, C) | LSL_32(A, 32-(C)))
321 #define ROR_33(A, C)                     (LSR_32(A, C) | LSL_32(A, 33-(C)))
322
323
324
325 /* ------------------------------ CPU Access ------------------------------ */
326
327 /* Access the CPU registers */
328 #define CPU_TYPE         m68ki_cpu.cpu_type
329
330 #define REG_DA           m68ki_cpu.dar /* easy access to data and address regs */
331 #define REG_DA_SAVE           m68ki_cpu.dar_save
332 #define REG_D            m68ki_cpu.dar
333 #define REG_A            (m68ki_cpu.dar+8)
334 #define REG_PPC                  m68ki_cpu.ppc
335 #define REG_PC           m68ki_cpu.pc
336 #define REG_SP_BASE      m68ki_cpu.sp
337 #define REG_USP          m68ki_cpu.sp[0]
338 #define REG_ISP          m68ki_cpu.sp[4]
339 #define REG_MSP          m68ki_cpu.sp[6]
340 #define REG_SP           m68ki_cpu.dar[15]
341 #define REG_VBR          m68ki_cpu.vbr
342 #define REG_SFC          m68ki_cpu.sfc
343 #define REG_DFC          m68ki_cpu.dfc
344 #define REG_CACR         m68ki_cpu.cacr
345 #define REG_CAAR         m68ki_cpu.caar
346 #define REG_IR           m68ki_cpu.ir
347
348 #define REG_FP           m68ki_cpu.fpr
349 #define REG_FPCR         m68ki_cpu.fpcr
350 #define REG_FPSR         m68ki_cpu.fpsr
351 #define REG_FPIAR        m68ki_cpu.fpiar
352
353 #define FLAG_T1          m68ki_cpu.t1_flag
354 #define FLAG_T0          m68ki_cpu.t0_flag
355 #define FLAG_S           m68ki_cpu.s_flag
356 #define FLAG_M           m68ki_cpu.m_flag
357 #define FLAG_X           m68ki_cpu.x_flag
358 #define FLAG_N           m68ki_cpu.n_flag
359 #define FLAG_Z           m68ki_cpu.not_z_flag
360 #define FLAG_V           m68ki_cpu.v_flag
361 #define FLAG_C           m68ki_cpu.c_flag
362 #define FLAG_INT_MASK    m68ki_cpu.int_mask
363
364 #define CPU_INT_LEVEL    m68ki_cpu.int_level /* ASG: changed from CPU_INTS_PENDING */
365 #define CPU_STOPPED      m68ki_cpu.stopped
366 #define CPU_PREF_ADDR    m68ki_cpu.pref_addr
367 #define CPU_PREF_DATA    m68ki_cpu.pref_data
368 #define CPU_ADDRESS_MASK m68ki_cpu.address_mask
369 #define CPU_SR_MASK      m68ki_cpu.sr_mask
370 #define CPU_INSTR_MODE   m68ki_cpu.instr_mode
371 #define CPU_RUN_MODE     m68ki_cpu.run_mode
372
373 #define CYC_INSTRUCTION  m68ki_cpu.cyc_instruction
374 #define CYC_EXCEPTION    m68ki_cpu.cyc_exception
375 #define CYC_BCC_NOTAKE_B m68ki_cpu.cyc_bcc_notake_b
376 #define CYC_BCC_NOTAKE_W m68ki_cpu.cyc_bcc_notake_w
377 #define CYC_DBCC_F_NOEXP m68ki_cpu.cyc_dbcc_f_noexp
378 #define CYC_DBCC_F_EXP   m68ki_cpu.cyc_dbcc_f_exp
379 #define CYC_SCC_R_TRUE   m68ki_cpu.cyc_scc_r_true
380 #define CYC_MOVEM_W      m68ki_cpu.cyc_movem_w
381 #define CYC_MOVEM_L      m68ki_cpu.cyc_movem_l
382 #define CYC_SHIFT        m68ki_cpu.cyc_shift
383 #define CYC_RESET        m68ki_cpu.cyc_reset
384 #define HAS_PMMU         m68ki_cpu.has_pmmu
385 #define HAS_FPU          m68ki_cpu.has_fpu
386 #define PMMU_ENABLED     m68ki_cpu.pmmu_enabled
387 #define RESET_CYCLES     m68ki_cpu.reset_cycles
388
389
390 #define CALLBACK_INT_ACK     m68ki_cpu.int_ack_callback
391 #define CALLBACK_BKPT_ACK    m68ki_cpu.bkpt_ack_callback
392 #define CALLBACK_RESET_INSTR m68ki_cpu.reset_instr_callback
393 #define CALLBACK_CMPILD_INSTR m68ki_cpu.cmpild_instr_callback
394 #define CALLBACK_RTE_INSTR    m68ki_cpu.rte_instr_callback
395 #define CALLBACK_TAS_INSTR    m68ki_cpu.tas_instr_callback
396 #define CALLBACK_ILLG_INSTR    m68ki_cpu.illg_instr_callback
397 #define CALLBACK_PC_CHANGED  m68ki_cpu.pc_changed_callback
398 #define CALLBACK_SET_FC      m68ki_cpu.set_fc_callback
399 #define CALLBACK_INSTR_HOOK  m68ki_cpu.instr_hook_callback
400
401
402
403 /* ----------------------------- Configuration ---------------------------- */
404
405 /* These defines are dependant on the configuration defines in m68kconf.h */
406
407 /* Disable certain comparisons if we're not using all CPU types */
408 #if M68K_EMULATE_040
409 #define CPU_TYPE_IS_040_PLUS(A)    ((A) & (CPU_TYPE_040 | CPU_TYPE_EC040))
410         #define CPU_TYPE_IS_040_LESS(A)    1
411 #else
412         #define CPU_TYPE_IS_040_PLUS(A)    0
413         #define CPU_TYPE_IS_040_LESS(A)    1
414 #endif
415
416 #if M68K_EMULATE_030
417 #define CPU_TYPE_IS_030_PLUS(A)    ((A) & (CPU_TYPE_030 | CPU_TYPE_EC030 | CPU_TYPE_040 | CPU_TYPE_EC040))
418 #define CPU_TYPE_IS_030_LESS(A)    1
419 #else
420 #define CPU_TYPE_IS_030_PLUS(A) 0
421 #define CPU_TYPE_IS_030_LESS(A)    1
422 #endif
423
424 #if M68K_EMULATE_020
425 #define CPU_TYPE_IS_020_PLUS(A)    ((A) & (CPU_TYPE_020 | CPU_TYPE_030 | CPU_TYPE_EC030 | CPU_TYPE_040 | CPU_TYPE_EC040))
426         #define CPU_TYPE_IS_020_LESS(A)    1
427 #else
428         #define CPU_TYPE_IS_020_PLUS(A)    0
429         #define CPU_TYPE_IS_020_LESS(A)    1
430 #endif
431
432 #if M68K_EMULATE_EC020
433 #define CPU_TYPE_IS_EC020_PLUS(A)  ((A) & (CPU_TYPE_EC020 | CPU_TYPE_020 | CPU_TYPE_030 | CPU_TYPE_EC030 | CPU_TYPE_040 | CPU_TYPE_EC040))
434         #define CPU_TYPE_IS_EC020_LESS(A)  ((A) & (CPU_TYPE_000 | CPU_TYPE_010 | CPU_TYPE_EC020))
435 #else
436         #define CPU_TYPE_IS_EC020_PLUS(A)  CPU_TYPE_IS_020_PLUS(A)
437         #define CPU_TYPE_IS_EC020_LESS(A)  CPU_TYPE_IS_020_LESS(A)
438 #endif
439
440 #if M68K_EMULATE_010
441         #define CPU_TYPE_IS_010(A)         ((A) == CPU_TYPE_010)
442 #define CPU_TYPE_IS_010_PLUS(A)    ((A) & (CPU_TYPE_010 | CPU_TYPE_EC020 | CPU_TYPE_020 | CPU_TYPE_EC030 | CPU_TYPE_030 | CPU_TYPE_040 | CPU_TYPE_EC040))
443 #define CPU_TYPE_IS_010_LESS(A)    ((A) & (CPU_TYPE_000 | CPU_TYPE_008 | CPU_TYPE_010))
444 #else
445         #define CPU_TYPE_IS_010(A)         0
446         #define CPU_TYPE_IS_010_PLUS(A)    CPU_TYPE_IS_EC020_PLUS(A)
447         #define CPU_TYPE_IS_010_LESS(A)    CPU_TYPE_IS_EC020_LESS(A)
448 #endif
449
450 #if M68K_EMULATE_020 || M68K_EMULATE_EC020
451         #define CPU_TYPE_IS_020_VARIANT(A) ((A) & (CPU_TYPE_EC020 | CPU_TYPE_020))
452 #else
453         #define CPU_TYPE_IS_020_VARIANT(A) 0
454 #endif
455
456 #if M68K_EMULATE_040 || M68K_EMULATE_020 || M68K_EMULATE_EC020 || M68K_EMULATE_010
457         #define CPU_TYPE_IS_000(A)         ((A) == CPU_TYPE_000)
458 #else
459         #define CPU_TYPE_IS_000(A)         1
460 #endif
461
462
463 #if !M68K_SEPARATE_READS
464 #define m68k_read_immediate_16(A) m68ki_read_program_16(A)
465 #define m68k_read_immediate_32(A) m68ki_read_program_32(A)
466
467 #define m68k_read_pcrelative_8(A) m68ki_read_program_8(A)
468 #define m68k_read_pcrelative_16(A) m68ki_read_program_16(A)
469 #define m68k_read_pcrelative_32(A) m68ki_read_program_32(A)
470 #endif /* M68K_SEPARATE_READS */
471
472
473 /* Enable or disable callback functions */
474 #if M68K_EMULATE_INT_ACK
475         #if M68K_EMULATE_INT_ACK == OPT_SPECIFY_HANDLER
476                 #define m68ki_int_ack(A) M68K_INT_ACK_CALLBACK(A)
477         #else
478                 #define m68ki_int_ack(A) CALLBACK_INT_ACK(A)
479         #endif
480 #else
481         /* Default action is to used autovector mode, which is most common */
482         #define m68ki_int_ack(A) M68K_INT_ACK_AUTOVECTOR
483 #endif /* M68K_EMULATE_INT_ACK */
484
485 #if M68K_EMULATE_BKPT_ACK
486         #if M68K_EMULATE_BKPT_ACK == OPT_SPECIFY_HANDLER
487                 #define m68ki_bkpt_ack(A) M68K_BKPT_ACK_CALLBACK(A)
488         #else
489                 #define m68ki_bkpt_ack(A) CALLBACK_BKPT_ACK(A)
490         #endif
491 #else
492         #define m68ki_bkpt_ack(A)
493 #endif /* M68K_EMULATE_BKPT_ACK */
494
495 #if M68K_EMULATE_RESET
496         #if M68K_EMULATE_RESET == OPT_SPECIFY_HANDLER
497                 #define m68ki_output_reset() M68K_RESET_CALLBACK()
498         #else
499                 #define m68ki_output_reset() CALLBACK_RESET_INSTR()
500         #endif
501 #else
502         #define m68ki_output_reset()
503 #endif /* M68K_EMULATE_RESET */
504
505 #if M68K_CMPILD_HAS_CALLBACK
506         #if M68K_CMPILD_HAS_CALLBACK == OPT_SPECIFY_HANDLER
507                 #define m68ki_cmpild_callback(v,r) M68K_CMPILD_CALLBACK(v,r)
508         #else
509                 #define m68ki_cmpild_callback(v,r) CALLBACK_CMPILD_INSTR(v,r)
510         #endif
511 #else
512         #define m68ki_cmpild_callback(v,r)
513 #endif /* M68K_CMPILD_HAS_CALLBACK */
514
515 #if M68K_RTE_HAS_CALLBACK
516         #if M68K_RTE_HAS_CALLBACK == OPT_SPECIFY_HANDLER
517                 #define m68ki_rte_callback() M68K_RTE_CALLBACK()
518         #else
519                 #define m68ki_rte_callback() CALLBACK_RTE_INSTR()
520         #endif
521 #else
522         #define m68ki_rte_callback()
523 #endif /* M68K_RTE_HAS_CALLBACK */
524
525 #if M68K_TAS_HAS_CALLBACK
526         #if M68K_TAS_HAS_CALLBACK == OPT_SPECIFY_HANDLER
527                 #define m68ki_tas_callback() M68K_TAS_CALLBACK()
528         #else
529                 #define m68ki_tas_callback() CALLBACK_TAS_INSTR()
530         #endif
531 #else
532         #define m68ki_tas_callback() 1
533 #endif /* M68K_TAS_HAS_CALLBACK */
534
535 #if M68K_ILLG_HAS_CALLBACK
536         #if M68K_ILLG_HAS_CALLBACK == OPT_SPECIFY_HANDLER
537                 #define m68ki_illg_callback(opcode) M68K_ILLG_CALLBACK(opcode)
538         #else
539                 #define m68ki_illg_callback(opcode) CALLBACK_ILLG_INSTR(opcode)
540         #endif
541 #else
542         #define m68ki_illg_callback(opcode) 0 // Default is 0 = not handled, exception will occur
543 #endif /* M68K_ILLG_HAS_CALLBACK */
544
545 #if M68K_INSTRUCTION_HOOK
546         #if M68K_INSTRUCTION_HOOK == OPT_SPECIFY_HANDLER
547                 #define m68ki_instr_hook(pc) M68K_INSTRUCTION_CALLBACK(pc)
548         #else
549                 #define m68ki_instr_hook(pc) CALLBACK_INSTR_HOOK(pc)
550         #endif
551 #else
552         #define m68ki_instr_hook(pc)
553 #endif /* M68K_INSTRUCTION_HOOK */
554
555 #if M68K_MONITOR_PC
556         #if M68K_MONITOR_PC == OPT_SPECIFY_HANDLER
557                 #define m68ki_pc_changed(A) M68K_SET_PC_CALLBACK(ADDRESS_68K(A))
558         #else
559                 #define m68ki_pc_changed(A) CALLBACK_PC_CHANGED(ADDRESS_68K(A))
560         #endif
561 #else
562         #define m68ki_pc_changed(A)
563 #endif /* M68K_MONITOR_PC */
564
565
566 /* Enable or disable function code emulation */
567 #if M68K_EMULATE_FC
568         #if M68K_EMULATE_FC == OPT_SPECIFY_HANDLER
569                 #define m68ki_set_fc(A) M68K_SET_FC_CALLBACK(A)
570         #else
571                 #define m68ki_set_fc(A) CALLBACK_SET_FC(A)
572         #endif
573         #define m68ki_use_data_space() m68ki_address_space = FUNCTION_CODE_USER_DATA
574         #define m68ki_use_program_space() m68ki_address_space = FUNCTION_CODE_USER_PROGRAM
575         #define m68ki_get_address_space() m68ki_address_space
576 #else
577         #define m68ki_set_fc(A)
578         #define m68ki_use_data_space()
579         #define m68ki_use_program_space()
580         #define m68ki_get_address_space() FUNCTION_CODE_USER_DATA
581 #endif /* M68K_EMULATE_FC */
582
583
584 /* Enable or disable trace emulation */
585 #if M68K_EMULATE_TRACE
586         /* Initiates trace checking before each instruction (t1) */
587         #define m68ki_trace_t1() m68ki_tracing = FLAG_T1
588         /* adds t0 to trace checking if we encounter change of flow */
589         #define m68ki_trace_t0() m68ki_tracing |= FLAG_T0
590         /* Clear all tracing */
591         #define m68ki_clear_trace() m68ki_tracing = 0
592         /* Cause a trace exception if we are tracing */
593         #define m68ki_exception_if_trace() if(m68ki_tracing) m68ki_exception_trace()
594 #else
595         #define m68ki_trace_t1()
596         #define m68ki_trace_t0()
597         #define m68ki_clear_trace()
598         #define m68ki_exception_if_trace()
599 #endif /* M68K_EMULATE_TRACE */
600
601
602
603 /* Address error */
604 #if M68K_EMULATE_ADDRESS_ERROR
605         #include <setjmp.h>
606
607 /* sigjmp() on Mac OS X and *BSD in general saves signal contexts and is super-slow, use sigsetjmp() to tell it not to */
608 #ifdef _BSD_SETJMP_H
609 extern sigjmp_buf m68ki_aerr_trap;
610 #define m68ki_set_address_error_trap(m68k) \
611         if(sigsetjmp(m68ki_aerr_trap, 0) != 0) \
612         { \
613                 m68ki_exception_address_error(m68k); \
614                 if(CPU_STOPPED) \
615                 { \
616                         if (m68ki_remaining_cycles > 0) \
617                                 m68ki_remaining_cycles = 0; \
618                         return m68ki_initial_cycles; \
619                 } \
620         }
621
622 #define m68ki_check_address_error(ADDR, WRITE_MODE, FC) \
623         if((ADDR)&1) \
624         { \
625                 m68ki_aerr_address = ADDR; \
626                 m68ki_aerr_write_mode = WRITE_MODE; \
627                 m68ki_aerr_fc = FC; \
628                 siglongjmp(m68ki_aerr_trap, 1); \
629         }
630 #else
631 extern jmp_buf m68ki_aerr_trap;
632         #define m68ki_set_address_error_trap() \
633                 if(setjmp(m68ki_aerr_trap) != 0) \
634                 { \
635                         m68ki_exception_address_error(); \
636                         if(CPU_STOPPED) \
637                         { \
638                                 SET_CYCLES(0); \
639                                 return m68ki_initial_cycles; \
640                         } \
641                         /* ensure we don't re-enter execution loop after an
642                            address error if there's no more cycles remaining */ \
643                         if(GET_CYCLES() <= 0) \
644                         { \
645                                 /* return how many clocks we used */ \
646                                 return m68ki_initial_cycles - GET_CYCLES(); \
647                         } \
648                 }
649
650         #define m68ki_check_address_error(ADDR, WRITE_MODE, FC) \
651                 if((ADDR)&1) \
652                 { \
653                         m68ki_aerr_address = ADDR; \
654                         m68ki_aerr_write_mode = WRITE_MODE; \
655                         m68ki_aerr_fc = FC; \
656                         longjmp(m68ki_aerr_trap, 1); \
657                 }
658 #endif
659
660         #define m68ki_check_address_error_010_less(ADDR, WRITE_MODE, FC) \
661                 if (CPU_TYPE_IS_010_LESS(CPU_TYPE)) \
662                 { \
663                         m68ki_check_address_error(ADDR, WRITE_MODE, FC) \
664                 }
665 #else
666         #define m68ki_set_address_error_trap()
667         #define m68ki_check_address_error(ADDR, WRITE_MODE, FC)
668         #define m68ki_check_address_error_010_less(ADDR, WRITE_MODE, FC)
669 #endif /* M68K_ADDRESS_ERROR */
670
671 /* Logging */
672 #if M68K_LOG_ENABLE
673         #include <stdio.h>
674         extern FILE* M68K_LOG_FILEHANDLE
675         extern const char *const m68ki_cpu_names[];
676
677         #define M68K_DO_LOG(A) if(M68K_LOG_FILEHANDLE) fprintf A
678         #if M68K_LOG_1010_1111
679                 #define M68K_DO_LOG_EMU(A) if(M68K_LOG_FILEHANDLE) fprintf A
680         #else
681                 #define M68K_DO_LOG_EMU(A)
682         #endif
683 #else
684         #define M68K_DO_LOG(A)
685         #define M68K_DO_LOG_EMU(A)
686 #endif
687
688
689
690 /* -------------------------- EA / Operand Access ------------------------- */
691
692 /*
693  * The general instruction format follows this pattern:
694  * .... XXX. .... .YYY
695  * where XXX is register X and YYY is register Y
696  */
697 /* Data Register Isolation */
698 #define DX (REG_D[(REG_IR >> 9) & 7])
699 #define DY (REG_D[REG_IR & 7])
700 /* Address Register Isolation */
701 #define AX (REG_A[(REG_IR >> 9) & 7])
702 #define AY (REG_A[REG_IR & 7])
703
704
705 /* Effective Address Calculations */
706 #define EA_AY_AI_8()   AY                                    /* address register indirect */
707 #define EA_AY_AI_16()  EA_AY_AI_8()
708 #define EA_AY_AI_32()  EA_AY_AI_8()
709 #define EA_AY_PI_8()   (AY++)                                /* postincrement (size = byte) */
710 #define EA_AY_PI_16()  ((AY+=2)-2)                           /* postincrement (size = word) */
711 #define EA_AY_PI_32()  ((AY+=4)-4)                           /* postincrement (size = long) */
712 #define EA_AY_PD_8()   (--AY)                                /* predecrement (size = byte) */
713 #define EA_AY_PD_16()  (AY-=2)                               /* predecrement (size = word) */
714 #define EA_AY_PD_32()  (AY-=4)                               /* predecrement (size = long) */
715 #define EA_AY_DI_8()   (AY+MAKE_INT_16(m68ki_read_imm_16())) /* displacement */
716 #define EA_AY_DI_16()  EA_AY_DI_8()
717 #define EA_AY_DI_32()  EA_AY_DI_8()
718 #define EA_AY_IX_8()   m68ki_get_ea_ix(AY)                   /* indirect + index */
719 #define EA_AY_IX_16()  EA_AY_IX_8()
720 #define EA_AY_IX_32()  EA_AY_IX_8()
721
722 #define EA_AX_AI_8()   AX
723 #define EA_AX_AI_16()  EA_AX_AI_8()
724 #define EA_AX_AI_32()  EA_AX_AI_8()
725 #define EA_AX_PI_8()   (AX++)
726 #define EA_AX_PI_16()  ((AX+=2)-2)
727 #define EA_AX_PI_32()  ((AX+=4)-4)
728 #define EA_AX_PD_8()   (--AX)
729 #define EA_AX_PD_16()  (AX-=2)
730 #define EA_AX_PD_32()  (AX-=4)
731 #define EA_AX_DI_8()   (AX+MAKE_INT_16(m68ki_read_imm_16()))
732 #define EA_AX_DI_16()  EA_AX_DI_8()
733 #define EA_AX_DI_32()  EA_AX_DI_8()
734 #define EA_AX_IX_8()   m68ki_get_ea_ix(AX)
735 #define EA_AX_IX_16()  EA_AX_IX_8()
736 #define EA_AX_IX_32()  EA_AX_IX_8()
737
738 #define EA_A7_PI_8()   ((REG_A[7]+=2)-2)
739 #define EA_A7_PD_8()   (REG_A[7]-=2)
740
741 #define EA_AW_8()      MAKE_INT_16(m68ki_read_imm_16())      /* absolute word */
742 #define EA_AW_16()     EA_AW_8()
743 #define EA_AW_32()     EA_AW_8()
744 #define EA_AL_8()      m68ki_read_imm_32()                   /* absolute long */
745 #define EA_AL_16()     EA_AL_8()
746 #define EA_AL_32()     EA_AL_8()
747 #define EA_PCDI_8()    m68ki_get_ea_pcdi()                   /* pc indirect + displacement */
748 #define EA_PCDI_16()   EA_PCDI_8()
749 #define EA_PCDI_32()   EA_PCDI_8()
750 #define EA_PCIX_8()    m68ki_get_ea_pcix()                   /* pc indirect + index */
751 #define EA_PCIX_16()   EA_PCIX_8()
752 #define EA_PCIX_32()   EA_PCIX_8()
753
754
755 #define OPER_I_8()     m68ki_read_imm_8()
756 #define OPER_I_16()    m68ki_read_imm_16()
757 #define OPER_I_32()    m68ki_read_imm_32()
758
759
760
761 /* --------------------------- Status Register ---------------------------- */
762
763 /* Flag Calculation Macros */
764 #define CFLAG_8(A) (A)
765 #define CFLAG_16(A) ((A)>>8)
766
767 #if M68K_INT_GT_32_BIT
768         #define CFLAG_ADD_32(S, D, R) ((R)>>24)
769         #define CFLAG_SUB_32(S, D, R) ((R)>>24)
770 #else
771         #define CFLAG_ADD_32(S, D, R) (((S & D) | (~R & (S | D)))>>23)
772         #define CFLAG_SUB_32(S, D, R) (((S & R) | (~D & (S | R)))>>23)
773 #endif /* M68K_INT_GT_32_BIT */
774
775 #define VFLAG_ADD_8(S, D, R) ((S^R) & (D^R))
776 #define VFLAG_ADD_16(S, D, R) (((S^R) & (D^R))>>8)
777 #define VFLAG_ADD_32(S, D, R) (((S^R) & (D^R))>>24)
778
779 #define VFLAG_SUB_8(S, D, R) ((S^D) & (R^D))
780 #define VFLAG_SUB_16(S, D, R) (((S^D) & (R^D))>>8)
781 #define VFLAG_SUB_32(S, D, R) (((S^D) & (R^D))>>24)
782
783 #define NFLAG_8(A) (A)
784 #define NFLAG_16(A) ((A)>>8)
785 #define NFLAG_32(A) ((A)>>24)
786 #define NFLAG_64(A) ((A)>>56)
787
788 #define ZFLAG_8(A) MASK_OUT_ABOVE_8(A)
789 #define ZFLAG_16(A) MASK_OUT_ABOVE_16(A)
790 #define ZFLAG_32(A) MASK_OUT_ABOVE_32(A)
791
792
793 /* Flag values */
794 #define NFLAG_SET   0x80
795 #define NFLAG_CLEAR 0
796 #define CFLAG_SET   0x100
797 #define CFLAG_CLEAR 0
798 #define XFLAG_SET   0x100
799 #define XFLAG_CLEAR 0
800 #define VFLAG_SET   0x80
801 #define VFLAG_CLEAR 0
802 #define ZFLAG_SET   0
803 #define ZFLAG_CLEAR 0xffffffff
804
805 #define SFLAG_SET   4
806 #define SFLAG_CLEAR 0
807 #define MFLAG_SET   2
808 #define MFLAG_CLEAR 0
809
810 /* Turn flag values into 1 or 0 */
811 #define XFLAG_AS_1() ((FLAG_X>>8)&1)
812 #define NFLAG_AS_1() ((FLAG_N>>7)&1)
813 #define VFLAG_AS_1() ((FLAG_V>>7)&1)
814 #define ZFLAG_AS_1() (!FLAG_Z)
815 #define CFLAG_AS_1() ((FLAG_C>>8)&1)
816
817
818 /* Conditions */
819 #define COND_CS() (FLAG_C&0x100)
820 #define COND_CC() (!COND_CS())
821 #define COND_VS() (FLAG_V&0x80)
822 #define COND_VC() (!COND_VS())
823 #define COND_NE() FLAG_Z
824 #define COND_EQ() (!COND_NE())
825 #define COND_MI() (FLAG_N&0x80)
826 #define COND_PL() (!COND_MI())
827 #define COND_LT() ((FLAG_N^FLAG_V)&0x80)
828 #define COND_GE() (!COND_LT())
829 #define COND_HI() (COND_CC() && COND_NE())
830 #define COND_LS() (COND_CS() || COND_EQ())
831 #define COND_GT() (COND_GE() && COND_NE())
832 #define COND_LE() (COND_LT() || COND_EQ())
833
834 /* Reversed conditions */
835 #define COND_NOT_CS() COND_CC()
836 #define COND_NOT_CC() COND_CS()
837 #define COND_NOT_VS() COND_VC()
838 #define COND_NOT_VC() COND_VS()
839 #define COND_NOT_NE() COND_EQ()
840 #define COND_NOT_EQ() COND_NE()
841 #define COND_NOT_MI() COND_PL()
842 #define COND_NOT_PL() COND_MI()
843 #define COND_NOT_LT() COND_GE()
844 #define COND_NOT_GE() COND_LT()
845 #define COND_NOT_HI() COND_LS()
846 #define COND_NOT_LS() COND_HI()
847 #define COND_NOT_GT() COND_LE()
848 #define COND_NOT_LE() COND_GT()
849
850 /* Not real conditions, but here for convenience */
851 #define COND_XS() (FLAG_X&0x100)
852 #define COND_XC() (!COND_XS)
853
854
855 /* Get the condition code register */
856 #define m68ki_get_ccr() ((COND_XS() >> 4) | \
857                                                  (COND_MI() >> 4) | \
858                                                  (COND_EQ() << 2) | \
859                                                  (COND_VS() >> 6) | \
860                                                  (COND_CS() >> 8))
861
862 /* Get the status register */
863 #define m68ki_get_sr() ( FLAG_T1              | \
864                                                  FLAG_T0              | \
865                                                 (FLAG_S        << 11) | \
866                                                 (FLAG_M        << 11) | \
867                                                  FLAG_INT_MASK        | \
868                                                  m68ki_get_ccr())
869
870
871
872 /* ---------------------------- Cycle Counting ---------------------------- */
873
874 #define ADD_CYCLES(A)    m68ki_remaining_cycles += (A)
875 #define USE_CYCLES(A)    m68ki_remaining_cycles -= (A)
876 #define SET_CYCLES(A)    m68ki_remaining_cycles = A
877 #define GET_CYCLES()     m68ki_remaining_cycles
878 #define USE_ALL_CYCLES() m68ki_remaining_cycles %= CYC_INSTRUCTION[REG_IR]
879
880
881
882 /* ----------------------------- Read / Write ----------------------------- */
883
884 /* Read from the current address space */
885 #define m68ki_read_8(A)  m68ki_read_8_fc (A, FLAG_S | m68ki_get_address_space())
886 #define m68ki_read_16(A) m68ki_read_16_fc(A, FLAG_S | m68ki_get_address_space())
887 #define m68ki_read_32(A) m68ki_read_32_fc(A, FLAG_S | m68ki_get_address_space())
888
889 /* Write to the current data space */
890 #define m68ki_write_8(A, V)  m68ki_write_8_fc (A, FLAG_S | FUNCTION_CODE_USER_DATA, V)
891 #define m68ki_write_16(A, V) m68ki_write_16_fc(A, FLAG_S | FUNCTION_CODE_USER_DATA, V)
892 #define m68ki_write_32(A, V) m68ki_write_32_fc(A, FLAG_S | FUNCTION_CODE_USER_DATA, V)
893
894 #if M68K_SIMULATE_PD_WRITES
895 #define m68ki_write_32_pd(A, V) m68ki_write_32_pd_fc(A, FLAG_S | FUNCTION_CODE_USER_DATA, V)
896 #else
897 #define m68ki_write_32_pd(A, V) m68ki_write_32_fc(A, FLAG_S | FUNCTION_CODE_USER_DATA, V)
898 #endif
899
900 /* Map PC-relative reads */
901 #define m68ki_read_pcrel_8(A) m68k_read_pcrelative_8(A)
902 #define m68ki_read_pcrel_16(A) m68k_read_pcrelative_16(A)
903 #define m68ki_read_pcrel_32(A) m68k_read_pcrelative_32(A)
904
905 /* Read from the program space */
906 #define m68ki_read_program_8(A)         m68ki_read_8_fc(A, FLAG_S | FUNCTION_CODE_USER_PROGRAM)
907 #define m68ki_read_program_16(A)        m68ki_read_16_fc(A, FLAG_S | FUNCTION_CODE_USER_PROGRAM)
908 #define m68ki_read_program_32(A)        m68ki_read_32_fc(A, FLAG_S | FUNCTION_CODE_USER_PROGRAM)
909
910 /* Read from the data space */
911 #define m68ki_read_data_8(A)    m68ki_read_8_fc(A, FLAG_S | FUNCTION_CODE_USER_DATA)
912 #define m68ki_read_data_16(A)   m68ki_read_16_fc(A, FLAG_S | FUNCTION_CODE_USER_DATA)
913 #define m68ki_read_data_32(A)   m68ki_read_32_fc(A, FLAG_S | FUNCTION_CODE_USER_DATA)
914
915
916
917 /* ======================================================================== */
918 /* =============================== PROTOTYPES ============================= */
919 /* ======================================================================== */
920
921 typedef union
922 {
923         uint64 i;
924         double f;
925 } fp_reg;
926
927 typedef struct
928 {
929         uint cpu_type;     /* CPU Type: 68000, 68008, 68010, 68EC020, 68020, 68EC030, 68030, 68EC040, or 68040 */
930         uint dar[16];      /* Data and Address Registers */
931         uint dar_save[16];  /* Saved Data and Address Registers (pushed onto the
932                                                    stack when a bus error occurs)*/
933         uint ppc;                  /* Previous program counter */
934         uint pc;           /* Program Counter */
935         uint sp[7];        /* User, Interrupt, and Master Stack Pointers */
936         uint vbr;          /* Vector Base Register (m68010+) */
937         uint sfc;          /* Source Function Code Register (m68010+) */
938         uint dfc;          /* Destination Function Code Register (m68010+) */
939         uint cacr;         /* Cache Control Register (m68020, unemulated) */
940         uint caar;         /* Cache Address Register (m68020, unemulated) */
941         uint ir;           /* Instruction Register */
942         floatx80 fpr[8];     /* FPU Data Register (m68030/040) */
943         uint fpiar;        /* FPU Instruction Address Register (m68040) */
944         uint fpsr;         /* FPU Status Register (m68040) */
945         uint fpcr;         /* FPU Control Register (m68040) */
946         uint t1_flag;      /* Trace 1 */
947         uint t0_flag;      /* Trace 0 */
948         uint s_flag;       /* Supervisor */
949         uint m_flag;       /* Master/Interrupt state */
950         uint x_flag;       /* Extend */
951         uint n_flag;       /* Negative */
952         uint not_z_flag;   /* Zero, inverted for speedups */
953         uint v_flag;       /* Overflow */
954         uint c_flag;       /* Carry */
955         uint int_mask;     /* I0-I2 */
956         uint int_level;    /* State of interrupt pins IPL0-IPL2 -- ASG: changed from ints_pending */
957         uint stopped;      /* Stopped state */
958         uint pref_addr;    /* Last prefetch address */
959         uint pref_data;    /* Data in the prefetch queue */
960         uint address_mask; /* Available address pins */
961         uint sr_mask;      /* Implemented status register bits */
962         uint instr_mode;   /* Stores whether we are in instruction mode or group 0/1 exception mode */
963         uint run_mode;     /* Stores whether we are processing a reset, bus error, address error, or something else */
964         int    has_pmmu;   /* Indicates if a PMMU available (yes on 030, 040, no on EC030) */
965         int    has_fpu;    /* Indicates if a FPU available */
966         int    pmmu_enabled; /* Indicates if the PMMU is enabled */
967         int    fpu_just_reset; /* Indicates the FPU was just reset */
968         uint reset_cycles;
969
970         /* Clocks required for instructions / exceptions */
971         uint cyc_bcc_notake_b;
972         uint cyc_bcc_notake_w;
973         uint cyc_dbcc_f_noexp;
974         uint cyc_dbcc_f_exp;
975         uint cyc_scc_r_true;
976         uint cyc_movem_w;
977         uint cyc_movem_l;
978         uint cyc_shift;
979         uint cyc_reset;
980
981         /* Virtual IRQ lines state */
982         uint virq_state;
983         uint nmi_pending;
984
985         /* PMMU registers */
986         uint mmu_crp_aptr, mmu_crp_limit;
987         uint mmu_srp_aptr, mmu_srp_limit;
988         uint mmu_tc;
989         uint16 mmu_sr;
990
991         const uint8* cyc_instruction;
992         const uint8* cyc_exception;
993
994         /* Callbacks to host */
995         int  (*int_ack_callback)(int int_line);           /* Interrupt Acknowledge */
996         void (*bkpt_ack_callback)(unsigned int data);     /* Breakpoint Acknowledge */
997         void (*reset_instr_callback)(void);               /* Called when a RESET instruction is encountered */
998         void (*cmpild_instr_callback)(unsigned int, int); /* Called when a CMPI.L #v, Dn instruction is encountered */
999         void (*rte_instr_callback)(void);                 /* Called when a RTE instruction is encountered */
1000         int  (*tas_instr_callback)(void);                 /* Called when a TAS instruction is encountered, allows / disallows writeback */
1001         int  (*illg_instr_callback)(int);                 /* Called when an illegal instruction is encountered, allows handling */
1002         void (*pc_changed_callback)(unsigned int new_pc); /* Called when the PC changes by a large amount */
1003         void (*set_fc_callback)(unsigned int new_fc);     /* Called when the CPU function code changes */
1004         void (*instr_hook_callback)(unsigned int pc);     /* Called every instruction cycle prior to execution */
1005
1006 } m68ki_cpu_core;
1007
1008
1009 extern m68ki_cpu_core m68ki_cpu;
1010 extern sint           m68ki_remaining_cycles;
1011 extern uint           m68ki_tracing;
1012 extern const uint8    m68ki_shift_8_table[];
1013 extern const uint16   m68ki_shift_16_table[];
1014 extern const uint     m68ki_shift_32_table[];
1015 extern const uint8    m68ki_exception_cycle_table[][256];
1016 extern uint           m68ki_address_space;
1017 extern const uint8    m68ki_ea_idx_cycle_table[];
1018
1019 extern uint           m68ki_aerr_address;
1020 extern uint           m68ki_aerr_write_mode;
1021 extern uint           m68ki_aerr_fc;
1022
1023 /* Forward declarations to keep some of the macros happy */
1024 static inline uint m68ki_read_16_fc (uint address, uint fc);
1025 static inline uint m68ki_read_32_fc (uint address, uint fc);
1026 static inline uint m68ki_get_ea_ix(uint An);
1027 static inline void m68ki_check_interrupts(void);            /* ASG: check for interrupts */
1028
1029 /* quick disassembly (used for logging) */
1030 char* m68ki_disassemble_quick(unsigned int pc, unsigned int cpu_type);
1031
1032
1033 /* ======================================================================== */
1034 /* =========================== UTILITY FUNCTIONS ========================== */
1035 /* ======================================================================== */
1036
1037
1038 /* ---------------------------- Read Immediate ---------------------------- */
1039
1040 extern unsigned char read_ranges;
1041 extern unsigned int read_addr[8];
1042 extern unsigned int read_upper[8];
1043 extern unsigned char *read_data[8];
1044 extern unsigned char write_ranges;
1045 extern unsigned int write_addr[8];
1046 extern unsigned int write_upper[8];
1047 extern unsigned char *write_data[8];
1048
1049 extern uint pmmu_translate_addr(uint addr_in);
1050
1051 /* Handles all immediate reads, does address error check, function code setting,
1052  * and prefetching if they are enabled in m68kconf.h
1053  */
1054 static inline uint m68ki_read_imm_16(void)
1055 {
1056         m68ki_set_fc(FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */
1057         m68ki_check_address_error(REG_PC, MODE_READ, FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */
1058
1059 #if M68K_SEPARATE_READS
1060 /*#if M68K_EMULATE_PMMU
1061         if (PMMU_ENABLED)
1062             address = pmmu_translate_addr(ADDRESS_68K(CPU_PREF_ADDR));
1063 #endif*/
1064 #endif
1065
1066 #if M68K_EMULATE_PREFETCH
1067 {
1068         uint result;
1069         if(REG_PC != CPU_PREF_ADDR)
1070         {
1071                 CPU_PREF_ADDR = REG_PC;
1072                 CPU_PREF_DATA = m68k_read_immediate_16(ADDRESS_68K(CPU_PREF_ADDR));
1073         }
1074         result = MASK_OUT_ABOVE_16(CPU_PREF_DATA);
1075         REG_PC += 2;
1076         CPU_PREF_ADDR = REG_PC;
1077         CPU_PREF_DATA = m68k_read_immediate_16(ADDRESS_68K(CPU_PREF_ADDR));
1078         return result;
1079 }
1080 #else
1081
1082         uint32_t address = ADDRESS_68K(REG_PC);
1083         REG_PC += 2;
1084
1085         for (int i = 0; i < read_ranges; i++) {
1086                 if(address >= read_addr[i] && address < read_upper[i]) {
1087                         return be16toh(((unsigned short *)(read_data[i] + (address - read_addr[i])))[0]);
1088                 }
1089         }
1090
1091         return m68k_read_immediate_16(ADDRESS_68K(REG_PC-2));
1092 #endif /* M68K_EMULATE_PREFETCH */
1093 }
1094
1095 static inline uint m68ki_read_imm_8(void)
1096 {
1097         /* map read immediate 8 to read immediate 16 */
1098         return MASK_OUT_ABOVE_8(m68ki_read_imm_16());
1099 }
1100
1101 static inline uint m68ki_read_imm_32(void)
1102 {
1103 #if M68K_SEPARATE_READS
1104 /*#if M68K_EMULATE_PMMU
1105         if (PMMU_ENABLED)
1106             address = pmmu_translate_addr(ADDRESS_68K(CPU_PREF_ADDR));
1107 #endif*/
1108 #endif
1109
1110 #if M68K_EMULATE_PREFETCH
1111         uint temp_val;
1112
1113         m68ki_set_fc(FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */
1114         m68ki_check_address_error(REG_PC, MODE_READ, FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */
1115
1116         if(REG_PC != CPU_PREF_ADDR)
1117         {
1118                 CPU_PREF_ADDR = REG_PC;
1119
1120                 CPU_PREF_DATA = m68k_read_immediate_16(ADDRESS_68K(CPU_PREF_ADDR));
1121         }
1122         temp_val = MASK_OUT_ABOVE_16(CPU_PREF_DATA);
1123         REG_PC += 2;
1124         CPU_PREF_ADDR = REG_PC;
1125         CPU_PREF_DATA = m68k_read_immediate_16(ADDRESS_68K(CPU_PREF_ADDR));
1126
1127         temp_val = MASK_OUT_ABOVE_32((temp_val << 16) | MASK_OUT_ABOVE_16(CPU_PREF_DATA));
1128         REG_PC += 2;
1129         CPU_PREF_ADDR = REG_PC;
1130         CPU_PREF_DATA = m68k_read_immediate_16(ADDRESS_68K(CPU_PREF_ADDR));
1131
1132         return temp_val;
1133 #else
1134         m68ki_set_fc(FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */
1135         m68ki_check_address_error(REG_PC, MODE_READ, FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */
1136         uint32_t address = ADDRESS_68K(REG_PC);
1137         REG_PC += 4;
1138         for (int i = 0; i < read_ranges; i++) {
1139                 if(address >= read_addr[i] && address < read_upper[i]) {
1140                         return be32toh(((unsigned int *)(read_data[i] + (address - read_addr[i])))[0]);
1141                 }
1142         }
1143
1144         return m68k_read_immediate_32(ADDRESS_68K(REG_PC-4));
1145 #endif /* M68K_EMULATE_PREFETCH */
1146 }
1147
1148 /* ------------------------- Top level read/write ------------------------- */
1149
1150 /* Handles all memory accesses (except for immediate reads if they are
1151  * configured to use separate functions in m68kconf.h).
1152  * All memory accesses must go through these top level functions.
1153  * These functions will also check for address error and set the function
1154  * code if they are enabled in m68kconf.h.
1155  */
1156
1157 static inline uint m68ki_read_8_fc(uint address, uint fc)
1158 {
1159         (void)fc;
1160         m68ki_set_fc(fc); /* auto-disable (see m68kcpu.h) */
1161
1162 #if M68K_EMULATE_PMMU
1163         if (PMMU_ENABLED)
1164             address = pmmu_translate_addr(address);
1165 #endif
1166
1167         for (int i = 0; i < read_ranges; i++) {
1168                 if(address >= read_addr[i] && address < read_upper[i]) {
1169                         return read_data[i][address - read_addr[i]];
1170                 }
1171         }
1172
1173         return m68k_read_memory_8(ADDRESS_68K(address));
1174 }
1175 static inline uint m68ki_read_16_fc(uint address, uint fc)
1176 {
1177         (void)fc;
1178         m68ki_set_fc(fc); /* auto-disable (see m68kcpu.h) */
1179         m68ki_check_address_error_010_less(address, MODE_READ, fc); /* auto-disable (see m68kcpu.h) */
1180
1181 #if M68K_EMULATE_PMMU
1182         if (PMMU_ENABLED)
1183             address = pmmu_translate_addr(address);
1184 #endif
1185
1186         for (int i = 0; i < read_ranges; i++) {
1187                 if(address >= read_addr[i] && address < read_upper[i]) {
1188                         return be16toh(((unsigned short *)(read_data[i] + (address - read_addr[i])))[0]);
1189                 }
1190         }
1191
1192         return m68k_read_memory_16(ADDRESS_68K(address));
1193 }
1194 static inline uint m68ki_read_32_fc(uint address, uint fc)
1195 {
1196         (void)fc;
1197         m68ki_set_fc(fc); /* auto-disable (see m68kcpu.h) */
1198         m68ki_check_address_error_010_less(address, MODE_READ, fc); /* auto-disable (see m68kcpu.h) */
1199
1200 #if M68K_EMULATE_PMMU
1201         if (PMMU_ENABLED)
1202             address = pmmu_translate_addr(address);
1203 #endif
1204
1205         for (int i = 0; i < read_ranges; i++) {
1206                 if(address >= read_addr[i] && address < read_upper[i]) {
1207                         return be32toh(((unsigned int *)(read_data[i] + (address - read_addr[i])))[0]);
1208                 }
1209         }
1210
1211         return m68k_read_memory_32(ADDRESS_68K(address));
1212 }
1213
1214 static inline void m68ki_write_8_fc(uint address, uint fc, uint value)
1215 {
1216         (void)fc;
1217         m68ki_set_fc(fc); /* auto-disable (see m68kcpu.h) */
1218
1219 #if M68K_EMULATE_PMMU
1220         if (PMMU_ENABLED)
1221             address = pmmu_translate_addr(address);
1222 #endif
1223
1224         for (int i = 0; i < write_ranges; i++) {
1225                 if(address >= write_addr[i] && address < write_upper[i]) {
1226                         write_data[i][address - write_addr[i]] = (unsigned char)value;
1227                         return;
1228                 }
1229         }
1230
1231         m68k_write_memory_8(ADDRESS_68K(address), value);
1232 }
1233 static inline void m68ki_write_16_fc(uint address, uint fc, uint value)
1234 {
1235         (void)fc;
1236         m68ki_set_fc(fc); /* auto-disable (see m68kcpu.h) */
1237         m68ki_check_address_error_010_less(address, MODE_WRITE, fc); /* auto-disable (see m68kcpu.h) */
1238
1239 #if M68K_EMULATE_PMMU
1240         if (PMMU_ENABLED)
1241             address = pmmu_translate_addr(address);
1242 #endif
1243
1244         for (int i = 0; i < write_ranges; i++) {
1245                 if(address >= write_addr[i] && address < write_upper[i]) {
1246                         ((short *)(write_data[i] + (address - write_addr[i])))[0] = htobe16(value);
1247                         return;
1248                 }
1249         }
1250
1251         m68k_write_memory_16(ADDRESS_68K(address), value);
1252 }
1253 static inline void m68ki_write_32_fc(uint address, uint fc, uint value)
1254 {
1255         (void)fc;
1256         m68ki_set_fc(fc); /* auto-disable (see m68kcpu.h) */
1257         m68ki_check_address_error_010_less(address, MODE_WRITE, fc); /* auto-disable (see m68kcpu.h) */
1258
1259 #if M68K_EMULATE_PMMU
1260         if (PMMU_ENABLED)
1261             address = pmmu_translate_addr(address);
1262 #endif
1263
1264         for (int i = 0; i < write_ranges; i++) {
1265                 if(address >= write_addr[i] && address < write_upper[i]) {
1266                         ((int *)(write_data[i] + (address - write_addr[i])))[0] = htobe32(value);
1267                         return;
1268                 }
1269         }
1270
1271         m68k_write_memory_32(ADDRESS_68K(address), value);
1272 }
1273
1274 #if M68K_SIMULATE_PD_WRITES
1275 static inline void m68ki_write_32_pd_fc(uint address, uint fc, uint value)
1276 {
1277         (void)fc;
1278         m68ki_set_fc(fc); /* auto-disable (see m68kcpu.h) */
1279         m68ki_check_address_error_010_less(address, MODE_WRITE, fc); /* auto-disable (see m68kcpu.h) */
1280
1281 #if M68K_EMULATE_PMMU
1282         if (PMMU_ENABLED)
1283             address = pmmu_translate_addr(address);
1284 #endif
1285
1286         m68k_write_memory_32_pd(ADDRESS_68K(address), value);
1287 }
1288 #endif
1289
1290 /* --------------------- Effective Address Calculation -------------------- */
1291
1292 /* The program counter relative addressing modes cause operands to be
1293  * retrieved from program space, not data space.
1294  */
1295 static inline uint m68ki_get_ea_pcdi(void)
1296 {
1297         uint old_pc = REG_PC;
1298         m68ki_use_program_space(); /* auto-disable */
1299         return old_pc + MAKE_INT_16(m68ki_read_imm_16());
1300 }
1301
1302
1303 static inline uint m68ki_get_ea_pcix(void)
1304 {
1305         m68ki_use_program_space(); /* auto-disable */
1306         return m68ki_get_ea_ix(REG_PC);
1307 }
1308
1309 /* Indexed addressing modes are encoded as follows:
1310  *
1311  * Base instruction format:
1312  * F E D C B A 9 8 7 6 | 5 4 3 | 2 1 0
1313  * x x x x x x x x x x | 1 1 0 | BASE REGISTER      (An)
1314  *
1315  * Base instruction format for destination EA in move instructions:
1316  * F E D C | B A 9    | 8 7 6 | 5 4 3 2 1 0
1317  * x x x x | BASE REG | 1 1 0 | X X X X X X       (An)
1318  *
1319  * Brief extension format:
1320  *  F  |  E D C   |  B  |  A 9  | 8 | 7 6 5 4 3 2 1 0
1321  * D/A | REGISTER | W/L | SCALE | 0 |  DISPLACEMENT
1322  *
1323  * Full extension format:
1324  *  F     E D C      B     A 9    8   7    6    5 4       3   2 1 0
1325  * D/A | REGISTER | W/L | SCALE | 1 | BS | IS | BD SIZE | 0 | I/IS
1326  * BASE DISPLACEMENT (0, 16, 32 bit)                (bd)
1327  * OUTER DISPLACEMENT (0, 16, 32 bit)               (od)
1328  *
1329  * D/A:     0 = Dn, 1 = An                          (Xn)
1330  * W/L:     0 = W (sign extend), 1 = L              (.SIZE)
1331  * SCALE:   00=1, 01=2, 10=4, 11=8                  (*SCALE)
1332  * BS:      0=add base reg, 1=suppress base reg     (An suppressed)
1333  * IS:      0=add index, 1=suppress index           (Xn suppressed)
1334  * BD SIZE: 00=reserved, 01=NULL, 10=Word, 11=Long  (size of bd)
1335  *
1336  * IS I/IS Operation
1337  * 0  000  No Memory Indirect
1338  * 0  001  indir prex with null outer
1339  * 0  010  indir prex with word outer
1340  * 0  011  indir prex with long outer
1341  * 0  100  reserved
1342  * 0  101  indir postx with null outer
1343  * 0  110  indir postx with word outer
1344  * 0  111  indir postx with long outer
1345  * 1  000  no memory indirect
1346  * 1  001  mem indir with null outer
1347  * 1  010  mem indir with word outer
1348  * 1  011  mem indir with long outer
1349  * 1  100-111  reserved
1350  */
1351 static inline uint m68ki_get_ea_ix(uint An)
1352 {
1353         /* An = base register */
1354         uint extension = m68ki_read_imm_16();
1355         uint Xn = 0;                        /* Index register */
1356         uint bd = 0;                        /* Base Displacement */
1357         uint od = 0;                        /* Outer Displacement */
1358
1359         if(CPU_TYPE_IS_010_LESS(CPU_TYPE))
1360         {
1361                 /* Calculate index */
1362                 Xn = REG_DA[extension>>12];     /* Xn */
1363                 if(!BIT_B(extension))           /* W/L */
1364                         Xn = MAKE_INT_16(Xn);
1365
1366                 /* Add base register and displacement and return */
1367                 return An + Xn + MAKE_INT_8(extension);
1368         }
1369
1370         /* Brief extension format */
1371         if(!BIT_8(extension))
1372         {
1373                 /* Calculate index */
1374                 Xn = REG_DA[extension>>12];     /* Xn */
1375                 if(!BIT_B(extension))           /* W/L */
1376                         Xn = MAKE_INT_16(Xn);
1377                 /* Add scale if proper CPU type */
1378                 if(CPU_TYPE_IS_EC020_PLUS(CPU_TYPE))
1379                         Xn <<= (extension>>9) & 3;  /* SCALE */
1380
1381                 /* Add base register and displacement and return */
1382                 return An + Xn + MAKE_INT_8(extension);
1383         }
1384
1385         /* Full extension format */
1386
1387         USE_CYCLES(m68ki_ea_idx_cycle_table[extension&0x3f]);
1388
1389         /* Check if base register is present */
1390         if(BIT_7(extension))                /* BS */
1391                 An = 0;                         /* An */
1392
1393         /* Check if index is present */
1394         if(!BIT_6(extension))               /* IS */
1395         {
1396                 Xn = REG_DA[extension>>12];     /* Xn */
1397                 if(!BIT_B(extension))           /* W/L */
1398                         Xn = MAKE_INT_16(Xn);
1399                 Xn <<= (extension>>9) & 3;      /* SCALE */
1400         }
1401
1402         /* Check if base displacement is present */
1403         if(BIT_5(extension))                /* BD SIZE */
1404                 bd = BIT_4(extension) ? m68ki_read_imm_32() : (uint32)MAKE_INT_16(m68ki_read_imm_16());
1405
1406         /* If no indirect action, we are done */
1407         if(!(extension&7))                  /* No Memory Indirect */
1408                 return An + bd + Xn;
1409
1410         /* Check if outer displacement is present */
1411         if(BIT_1(extension))                /* I/IS:  od */
1412                 od = BIT_0(extension) ? m68ki_read_imm_32() : (uint32)MAKE_INT_16(m68ki_read_imm_16());
1413
1414         /* Postindex */
1415         if(BIT_2(extension))                /* I/IS:  0 = preindex, 1 = postindex */
1416                 return m68ki_read_32(An + bd) + Xn + od;
1417
1418         /* Preindex */
1419         return m68ki_read_32(An + bd + Xn) + od;
1420 }
1421
1422
1423 /* Fetch operands */
1424 static inline uint OPER_AY_AI_8(void)  {uint ea = EA_AY_AI_8();  return m68ki_read_8(ea); }
1425 static inline uint OPER_AY_AI_16(void) {uint ea = EA_AY_AI_16(); return m68ki_read_16(ea);}
1426 static inline uint OPER_AY_AI_32(void) {uint ea = EA_AY_AI_32(); return m68ki_read_32(ea);}
1427 static inline uint OPER_AY_PI_8(void)  {uint ea = EA_AY_PI_8();  return m68ki_read_8(ea); }
1428 static inline uint OPER_AY_PI_16(void) {uint ea = EA_AY_PI_16(); return m68ki_read_16(ea);}
1429 static inline uint OPER_AY_PI_32(void) {uint ea = EA_AY_PI_32(); return m68ki_read_32(ea);}
1430 static inline uint OPER_AY_PD_8(void)  {uint ea = EA_AY_PD_8();  return m68ki_read_8(ea); }
1431 static inline uint OPER_AY_PD_16(void) {uint ea = EA_AY_PD_16(); return m68ki_read_16(ea);}
1432 static inline uint OPER_AY_PD_32(void) {uint ea = EA_AY_PD_32(); return m68ki_read_32(ea);}
1433 static inline uint OPER_AY_DI_8(void)  {uint ea = EA_AY_DI_8();  return m68ki_read_8(ea); }
1434 static inline uint OPER_AY_DI_16(void) {uint ea = EA_AY_DI_16(); return m68ki_read_16(ea);}
1435 static inline uint OPER_AY_DI_32(void) {uint ea = EA_AY_DI_32(); return m68ki_read_32(ea);}
1436 static inline uint OPER_AY_IX_8(void)  {uint ea = EA_AY_IX_8();  return m68ki_read_8(ea); }
1437 static inline uint OPER_AY_IX_16(void) {uint ea = EA_AY_IX_16(); return m68ki_read_16(ea);}
1438 static inline uint OPER_AY_IX_32(void) {uint ea = EA_AY_IX_32(); return m68ki_read_32(ea);}
1439
1440 static inline uint OPER_AX_AI_8(void)  {uint ea = EA_AX_AI_8();  return m68ki_read_8(ea); }
1441 static inline uint OPER_AX_AI_16(void) {uint ea = EA_AX_AI_16(); return m68ki_read_16(ea);}
1442 static inline uint OPER_AX_AI_32(void) {uint ea = EA_AX_AI_32(); return m68ki_read_32(ea);}
1443 static inline uint OPER_AX_PI_8(void)  {uint ea = EA_AX_PI_8();  return m68ki_read_8(ea); }
1444 static inline uint OPER_AX_PI_16(void) {uint ea = EA_AX_PI_16(); return m68ki_read_16(ea);}
1445 static inline uint OPER_AX_PI_32(void) {uint ea = EA_AX_PI_32(); return m68ki_read_32(ea);}
1446 static inline uint OPER_AX_PD_8(void)  {uint ea = EA_AX_PD_8();  return m68ki_read_8(ea); }
1447 static inline uint OPER_AX_PD_16(void) {uint ea = EA_AX_PD_16(); return m68ki_read_16(ea);}
1448 static inline uint OPER_AX_PD_32(void) {uint ea = EA_AX_PD_32(); return m68ki_read_32(ea);}
1449 static inline uint OPER_AX_DI_8(void)  {uint ea = EA_AX_DI_8();  return m68ki_read_8(ea); }
1450 static inline uint OPER_AX_DI_16(void) {uint ea = EA_AX_DI_16(); return m68ki_read_16(ea);}
1451 static inline uint OPER_AX_DI_32(void) {uint ea = EA_AX_DI_32(); return m68ki_read_32(ea);}
1452 static inline uint OPER_AX_IX_8(void)  {uint ea = EA_AX_IX_8();  return m68ki_read_8(ea); }
1453 static inline uint OPER_AX_IX_16(void) {uint ea = EA_AX_IX_16(); return m68ki_read_16(ea);}
1454 static inline uint OPER_AX_IX_32(void) {uint ea = EA_AX_IX_32(); return m68ki_read_32(ea);}
1455
1456 static inline uint OPER_A7_PI_8(void)  {uint ea = EA_A7_PI_8();  return m68ki_read_8(ea); }
1457 static inline uint OPER_A7_PD_8(void)  {uint ea = EA_A7_PD_8();  return m68ki_read_8(ea); }
1458
1459 static inline uint OPER_AW_8(void)     {uint ea = EA_AW_8();     return m68ki_read_8(ea); }
1460 static inline uint OPER_AW_16(void)    {uint ea = EA_AW_16();    return m68ki_read_16(ea);}
1461 static inline uint OPER_AW_32(void)    {uint ea = EA_AW_32();    return m68ki_read_32(ea);}
1462 static inline uint OPER_AL_8(void)     {uint ea = EA_AL_8();     return m68ki_read_8(ea); }
1463 static inline uint OPER_AL_16(void)    {uint ea = EA_AL_16();    return m68ki_read_16(ea);}
1464 static inline uint OPER_AL_32(void)    {uint ea = EA_AL_32();    return m68ki_read_32(ea);}
1465 static inline uint OPER_PCDI_8(void)   {uint ea = EA_PCDI_8();   return m68ki_read_pcrel_8(ea); }
1466 static inline uint OPER_PCDI_16(void)  {uint ea = EA_PCDI_16();  return m68ki_read_pcrel_16(ea);}
1467 static inline uint OPER_PCDI_32(void)  {uint ea = EA_PCDI_32();  return m68ki_read_pcrel_32(ea);}
1468 static inline uint OPER_PCIX_8(void)   {uint ea = EA_PCIX_8();   return m68ki_read_pcrel_8(ea); }
1469 static inline uint OPER_PCIX_16(void)  {uint ea = EA_PCIX_16();  return m68ki_read_pcrel_16(ea);}
1470 static inline uint OPER_PCIX_32(void)  {uint ea = EA_PCIX_32();  return m68ki_read_pcrel_32(ea);}
1471
1472
1473
1474 /* ---------------------------- Stack Functions --------------------------- */
1475
1476 /* Push/pull data from the stack */
1477 static inline void m68ki_push_16(uint value)
1478 {
1479         REG_SP = MASK_OUT_ABOVE_32(REG_SP - 2);
1480         m68ki_write_16(REG_SP, value);
1481 }
1482
1483 static inline void m68ki_push_32(uint value)
1484 {
1485         REG_SP = MASK_OUT_ABOVE_32(REG_SP - 4);
1486         m68ki_write_32(REG_SP, value);
1487 }
1488
1489 static inline uint m68ki_pull_16(void)
1490 {
1491         REG_SP = MASK_OUT_ABOVE_32(REG_SP + 2);
1492         return m68ki_read_16(REG_SP-2);
1493 }
1494
1495 static inline uint m68ki_pull_32(void)
1496 {
1497         REG_SP = MASK_OUT_ABOVE_32(REG_SP + 4);
1498         return m68ki_read_32(REG_SP-4);
1499 }
1500
1501
1502 /* Increment/decrement the stack as if doing a push/pull but
1503  * don't do any memory access.
1504  */
1505 static inline void m68ki_fake_push_16(void)
1506 {
1507         REG_SP = MASK_OUT_ABOVE_32(REG_SP - 2);
1508 }
1509
1510 static inline void m68ki_fake_push_32(void)
1511 {
1512         REG_SP = MASK_OUT_ABOVE_32(REG_SP - 4);
1513 }
1514
1515 static inline void m68ki_fake_pull_16(void)
1516 {
1517         REG_SP = MASK_OUT_ABOVE_32(REG_SP + 2);
1518 }
1519
1520 static inline void m68ki_fake_pull_32(void)
1521 {
1522         REG_SP = MASK_OUT_ABOVE_32(REG_SP + 4);
1523 }
1524
1525
1526 /* ----------------------------- Program Flow ----------------------------- */
1527
1528 /* Jump to a new program location or vector.
1529  * These functions will also call the pc_changed callback if it was enabled
1530  * in m68kconf.h.
1531  */
1532 static inline void m68ki_jump(uint new_pc)
1533 {
1534         REG_PC = new_pc;
1535         m68ki_pc_changed(REG_PC);
1536 }
1537
1538 static inline void m68ki_jump_vector(uint vector)
1539 {
1540         REG_PC = (vector<<2) + REG_VBR;
1541         REG_PC = m68ki_read_data_32(REG_PC);
1542         m68ki_pc_changed(REG_PC);
1543 }
1544
1545
1546 /* Branch to a new memory location.
1547  * The 32-bit branch will call pc_changed if it was enabled in m68kconf.h.
1548  * So far I've found no problems with not calling pc_changed for 8 or 16
1549  * bit branches.
1550  */
1551 static inline void m68ki_branch_8(uint offset)
1552 {
1553         REG_PC += MAKE_INT_8(offset);
1554 }
1555
1556 static inline void m68ki_branch_16(uint offset)
1557 {
1558         REG_PC += MAKE_INT_16(offset);
1559 }
1560
1561 static inline void m68ki_branch_32(uint offset)
1562 {
1563         REG_PC += offset;
1564         m68ki_pc_changed(REG_PC);
1565 }
1566
1567 /* ---------------------------- Status Register --------------------------- */
1568
1569 /* Set the S flag and change the active stack pointer.
1570  * Note that value MUST be 4 or 0.
1571  */
1572 static inline void m68ki_set_s_flag(uint value)
1573 {
1574         /* Backup the old stack pointer */
1575         REG_SP_BASE[FLAG_S | ((FLAG_S>>1) & FLAG_M)] = REG_SP;
1576         /* Set the S flag */
1577         FLAG_S = value;
1578         /* Set the new stack pointer */
1579         REG_SP = REG_SP_BASE[FLAG_S | ((FLAG_S>>1) & FLAG_M)];
1580 }
1581
1582 /* Set the S and M flags and change the active stack pointer.
1583  * Note that value MUST be 0, 2, 4, or 6 (bit2 = S, bit1 = M).
1584  */
1585 static inline void m68ki_set_sm_flag(uint value)
1586 {
1587         /* Backup the old stack pointer */
1588         REG_SP_BASE[FLAG_S | ((FLAG_S>>1) & FLAG_M)] = REG_SP;
1589         /* Set the S and M flags */
1590         FLAG_S = value & SFLAG_SET;
1591         FLAG_M = value & MFLAG_SET;
1592         /* Set the new stack pointer */
1593         REG_SP = REG_SP_BASE[FLAG_S | ((FLAG_S>>1) & FLAG_M)];
1594 }
1595
1596 /* Set the S and M flags.  Don't touch the stack pointer. */
1597 static inline void m68ki_set_sm_flag_nosp(uint value)
1598 {
1599         /* Set the S and M flags */
1600         FLAG_S = value & SFLAG_SET;
1601         FLAG_M = value & MFLAG_SET;
1602 }
1603
1604
1605 /* Set the condition code register */
1606 static inline void m68ki_set_ccr(uint value)
1607 {
1608         FLAG_X = BIT_4(value)  << 4;
1609         FLAG_N = BIT_3(value)  << 4;
1610         FLAG_Z = !BIT_2(value);
1611         FLAG_V = BIT_1(value)  << 6;
1612         FLAG_C = BIT_0(value)  << 8;
1613 }
1614
1615 /* Set the status register but don't check for interrupts */
1616 static inline void m68ki_set_sr_noint(uint value)
1617 {
1618         /* Mask out the "unimplemented" bits */
1619         value &= CPU_SR_MASK;
1620
1621         /* Now set the status register */
1622         FLAG_T1 = BIT_F(value);
1623         FLAG_T0 = BIT_E(value);
1624         FLAG_INT_MASK = value & 0x0700;
1625         m68ki_set_ccr(value);
1626         m68ki_set_sm_flag((value >> 11) & 6);
1627 }
1628
1629 /* Set the status register but don't check for interrupts nor
1630  * change the stack pointer
1631  */
1632 static inline void m68ki_set_sr_noint_nosp(uint value)
1633 {
1634         /* Mask out the "unimplemented" bits */
1635         value &= CPU_SR_MASK;
1636
1637         /* Now set the status register */
1638         FLAG_T1 = BIT_F(value);
1639         FLAG_T0 = BIT_E(value);
1640         FLAG_INT_MASK = value & 0x0700;
1641         m68ki_set_ccr(value);
1642         m68ki_set_sm_flag_nosp((value >> 11) & 6);
1643 }
1644
1645 /* Set the status register and check for interrupts */
1646 static inline void m68ki_set_sr(uint value)
1647 {
1648         m68ki_set_sr_noint(value);
1649         m68ki_check_interrupts();
1650 }
1651
1652
1653 /* ------------------------- Exception Processing ------------------------- */
1654
1655 /* Initiate exception processing */
1656 static inline uint m68ki_init_exception(void)
1657 {
1658         /* Save the old status register */
1659         uint sr = m68ki_get_sr();
1660
1661         /* Turn off trace flag, clear pending traces */
1662         FLAG_T1 = FLAG_T0 = 0;
1663         m68ki_clear_trace();
1664         /* Enter supervisor mode */
1665         m68ki_set_s_flag(SFLAG_SET);
1666
1667         return sr;
1668 }
1669
1670 /* 3 word stack frame (68000 only) */
1671 static inline void m68ki_stack_frame_3word(uint pc, uint sr)
1672 {
1673         m68ki_push_32(pc);
1674         m68ki_push_16(sr);
1675 }
1676
1677 /* Format 0 stack frame.
1678  * This is the standard stack frame for 68010+.
1679  */
1680 static inline void m68ki_stack_frame_0000(uint pc, uint sr, uint vector)
1681 {
1682         /* Stack a 3-word frame if we are 68000 */
1683         if(CPU_TYPE == CPU_TYPE_000)
1684         {
1685                 m68ki_stack_frame_3word(pc, sr);
1686                 return;
1687         }
1688         m68ki_push_16(vector<<2);
1689         m68ki_push_32(pc);
1690         m68ki_push_16(sr);
1691 }
1692
1693 /* Format 1 stack frame (68020).
1694  * For 68020, this is the 4 word throwaway frame.
1695  */
1696 static inline void m68ki_stack_frame_0001(uint pc, uint sr, uint vector)
1697 {
1698         m68ki_push_16(0x1000 | (vector<<2));
1699         m68ki_push_32(pc);
1700         m68ki_push_16(sr);
1701 }
1702
1703 /* Format 2 stack frame.
1704  * This is used only by 68020 for trap exceptions.
1705  */
1706 static inline void m68ki_stack_frame_0010(uint sr, uint vector)
1707 {
1708         m68ki_push_32(REG_PPC);
1709         m68ki_push_16(0x2000 | (vector<<2));
1710         m68ki_push_32(REG_PC);
1711         m68ki_push_16(sr);
1712 }
1713
1714
1715 /* Bus error stack frame (68000 only).
1716  */
1717 static inline void m68ki_stack_frame_buserr(uint sr)
1718 {
1719         m68ki_push_32(REG_PC);
1720         m68ki_push_16(sr);
1721         m68ki_push_16(REG_IR);
1722         m68ki_push_32(m68ki_aerr_address);      /* access address */
1723         /* 0 0 0 0 0 0 0 0 0 0 0 R/W I/N FC
1724          * R/W  0 = write, 1 = read
1725          * I/N  0 = instruction, 1 = not
1726          * FC   3-bit function code
1727          */
1728         m68ki_push_16(m68ki_aerr_write_mode | CPU_INSTR_MODE | m68ki_aerr_fc);
1729 }
1730
1731 /* Format 8 stack frame (68010).
1732  * 68010 only.  This is the 29 word bus/address error frame.
1733  */
1734 static inline void m68ki_stack_frame_1000(uint pc, uint sr, uint vector)
1735 {
1736         /* VERSION
1737          * NUMBER
1738          * INTERNAL INFORMATION, 16 WORDS
1739          */
1740         m68ki_fake_push_32();
1741         m68ki_fake_push_32();
1742         m68ki_fake_push_32();
1743         m68ki_fake_push_32();
1744         m68ki_fake_push_32();
1745         m68ki_fake_push_32();
1746         m68ki_fake_push_32();
1747         m68ki_fake_push_32();
1748
1749         /* INSTRUCTION INPUT BUFFER */
1750         m68ki_push_16(0);
1751
1752         /* UNUSED, RESERVED (not written) */
1753         m68ki_fake_push_16();
1754
1755         /* DATA INPUT BUFFER */
1756         m68ki_push_16(0);
1757
1758         /* UNUSED, RESERVED (not written) */
1759         m68ki_fake_push_16();
1760
1761         /* DATA OUTPUT BUFFER */
1762         m68ki_push_16(0);
1763
1764         /* UNUSED, RESERVED (not written) */
1765         m68ki_fake_push_16();
1766
1767         /* FAULT ADDRESS */
1768         m68ki_push_32(0);
1769
1770         /* SPECIAL STATUS WORD */
1771         m68ki_push_16(0);
1772
1773         /* 1000, VECTOR OFFSET */
1774         m68ki_push_16(0x8000 | (vector<<2));
1775
1776         /* PROGRAM COUNTER */
1777         m68ki_push_32(pc);
1778
1779         /* STATUS REGISTER */
1780         m68ki_push_16(sr);
1781 }
1782
1783 /* Format A stack frame (short bus fault).
1784  * This is used only by 68020 for bus fault and address error
1785  * if the error happens at an instruction boundary.
1786  * PC stacked is address of next instruction.
1787  */
1788 static inline void m68ki_stack_frame_1010(uint sr, uint vector, uint pc)
1789 {
1790         /* INTERNAL REGISTER */
1791         m68ki_push_16(0);
1792
1793         /* INTERNAL REGISTER */
1794         m68ki_push_16(0);
1795
1796         /* DATA OUTPUT BUFFER (2 words) */
1797         m68ki_push_32(0);
1798
1799         /* INTERNAL REGISTER */
1800         m68ki_push_16(0);
1801
1802         /* INTERNAL REGISTER */
1803         m68ki_push_16(0);
1804
1805         /* DATA CYCLE FAULT ADDRESS (2 words) */
1806         m68ki_push_32(0);
1807
1808         /* INSTRUCTION PIPE STAGE B */
1809         m68ki_push_16(0);
1810
1811         /* INSTRUCTION PIPE STAGE C */
1812         m68ki_push_16(0);
1813
1814         /* SPECIAL STATUS REGISTER */
1815         m68ki_push_16(0);
1816
1817         /* INTERNAL REGISTER */
1818         m68ki_push_16(0);
1819
1820         /* 1010, VECTOR OFFSET */
1821         m68ki_push_16(0xa000 | (vector<<2));
1822
1823         /* PROGRAM COUNTER */
1824         m68ki_push_32(pc);
1825
1826         /* STATUS REGISTER */
1827         m68ki_push_16(sr);
1828 }
1829
1830 /* Format B stack frame (long bus fault).
1831  * This is used only by 68020 for bus fault and address error
1832  * if the error happens during instruction execution.
1833  * PC stacked is address of instruction in progress.
1834  */
1835 static inline void m68ki_stack_frame_1011(uint sr, uint vector, uint pc)
1836 {
1837         /* INTERNAL REGISTERS (18 words) */
1838         m68ki_push_32(0);
1839         m68ki_push_32(0);
1840         m68ki_push_32(0);
1841         m68ki_push_32(0);
1842         m68ki_push_32(0);
1843         m68ki_push_32(0);
1844         m68ki_push_32(0);
1845         m68ki_push_32(0);
1846         m68ki_push_32(0);
1847
1848         /* VERSION# (4 bits), INTERNAL INFORMATION */
1849         m68ki_push_16(0);
1850
1851         /* INTERNAL REGISTERS (3 words) */
1852         m68ki_push_32(0);
1853         m68ki_push_16(0);
1854
1855         /* DATA INTPUT BUFFER (2 words) */
1856         m68ki_push_32(0);
1857
1858         /* INTERNAL REGISTERS (2 words) */
1859         m68ki_push_32(0);
1860
1861         /* STAGE B ADDRESS (2 words) */
1862         m68ki_push_32(0);
1863
1864         /* INTERNAL REGISTER (4 words) */
1865         m68ki_push_32(0);
1866         m68ki_push_32(0);
1867
1868         /* DATA OUTPUT BUFFER (2 words) */
1869         m68ki_push_32(0);
1870
1871         /* INTERNAL REGISTER */
1872         m68ki_push_16(0);
1873
1874         /* INTERNAL REGISTER */
1875         m68ki_push_16(0);
1876
1877         /* DATA CYCLE FAULT ADDRESS (2 words) */
1878         m68ki_push_32(0);
1879
1880         /* INSTRUCTION PIPE STAGE B */
1881         m68ki_push_16(0);
1882
1883         /* INSTRUCTION PIPE STAGE C */
1884         m68ki_push_16(0);
1885
1886         /* SPECIAL STATUS REGISTER */
1887         m68ki_push_16(0);
1888
1889         /* INTERNAL REGISTER */
1890         m68ki_push_16(0);
1891
1892         /* 1011, VECTOR OFFSET */
1893         m68ki_push_16(0xb000 | (vector<<2));
1894
1895         /* PROGRAM COUNTER */
1896         m68ki_push_32(pc);
1897
1898         /* STATUS REGISTER */
1899         m68ki_push_16(sr);
1900 }
1901
1902
1903 /* Used for Group 2 exceptions.
1904  * These stack a type 2 frame on the 020.
1905  */
1906 static inline void m68ki_exception_trap(uint vector)
1907 {
1908         uint sr = m68ki_init_exception();
1909
1910         if(CPU_TYPE_IS_010_LESS(CPU_TYPE))
1911                 m68ki_stack_frame_0000(REG_PC, sr, vector);
1912         else
1913                 m68ki_stack_frame_0010(sr, vector);
1914
1915         m68ki_jump_vector(vector);
1916
1917         /* Use up some clock cycles and undo the instruction's cycles */
1918         USE_CYCLES(CYC_EXCEPTION[vector] - CYC_INSTRUCTION[REG_IR]);
1919 }
1920
1921 /* Trap#n stacks a 0 frame but behaves like group2 otherwise */
1922 static inline void m68ki_exception_trapN(uint vector)
1923 {
1924         uint sr = m68ki_init_exception();
1925         m68ki_stack_frame_0000(REG_PC, sr, vector);
1926         m68ki_jump_vector(vector);
1927
1928         /* Use up some clock cycles and undo the instruction's cycles */
1929         USE_CYCLES(CYC_EXCEPTION[vector] - CYC_INSTRUCTION[REG_IR]);
1930 }
1931
1932 /* Exception for trace mode */
1933 static inline void m68ki_exception_trace(void)
1934 {
1935         uint sr = m68ki_init_exception();
1936
1937         if(CPU_TYPE_IS_010_LESS(CPU_TYPE))
1938         {
1939                 #if M68K_EMULATE_ADDRESS_ERROR == OPT_ON
1940                 if(CPU_TYPE_IS_000(CPU_TYPE))
1941                 {
1942                         CPU_INSTR_MODE = INSTRUCTION_NO;
1943                 }
1944                 #endif /* M68K_EMULATE_ADDRESS_ERROR */
1945                 m68ki_stack_frame_0000(REG_PC, sr, EXCEPTION_TRACE);
1946         }
1947         else
1948                 m68ki_stack_frame_0010(sr, EXCEPTION_TRACE);
1949
1950         m68ki_jump_vector(EXCEPTION_TRACE);
1951
1952         /* Trace nullifies a STOP instruction */
1953         CPU_STOPPED &= ~STOP_LEVEL_STOP;
1954
1955         /* Use up some clock cycles */
1956         USE_CYCLES(CYC_EXCEPTION[EXCEPTION_TRACE]);
1957 }
1958
1959 /* Exception for privilege violation */
1960 static inline void m68ki_exception_privilege_violation(void)
1961 {
1962         uint sr = m68ki_init_exception();
1963
1964         #if M68K_EMULATE_ADDRESS_ERROR == OPT_ON
1965         if(CPU_TYPE_IS_000(CPU_TYPE))
1966         {
1967                 CPU_INSTR_MODE = INSTRUCTION_NO;
1968         }
1969         #endif /* M68K_EMULATE_ADDRESS_ERROR */
1970
1971         m68ki_stack_frame_0000(REG_PPC, sr, EXCEPTION_PRIVILEGE_VIOLATION);
1972         m68ki_jump_vector(EXCEPTION_PRIVILEGE_VIOLATION);
1973
1974         /* Use up some clock cycles and undo the instruction's cycles */
1975         USE_CYCLES(CYC_EXCEPTION[EXCEPTION_PRIVILEGE_VIOLATION] - CYC_INSTRUCTION[REG_IR]);
1976 }
1977
1978 extern jmp_buf m68ki_bus_error_jmp_buf;
1979
1980 #define m68ki_check_bus_error_trap() setjmp(m68ki_bus_error_jmp_buf)
1981
1982 /* Exception for bus error */
1983 static inline void m68ki_exception_bus_error(void)
1984 {
1985         int i;
1986
1987         /* If we were processing a bus error, address error, or reset,
1988          * this is a catastrophic failure.
1989          * Halt the CPU
1990          */
1991         if(CPU_RUN_MODE == RUN_MODE_BERR_AERR_RESET)
1992         {
1993 m68k_read_memory_8(0x00ffff01);
1994                 CPU_STOPPED = STOP_LEVEL_HALT;
1995                 return;
1996         }
1997         CPU_RUN_MODE = RUN_MODE_BERR_AERR_RESET;
1998
1999         /* Use up some clock cycles and undo the instruction's cycles */
2000         USE_CYCLES(CYC_EXCEPTION[EXCEPTION_BUS_ERROR] - CYC_INSTRUCTION[REG_IR]);
2001
2002         for (i = 15; i >= 0; i--){
2003                 REG_DA[i] = REG_DA_SAVE[i];
2004         }
2005
2006         uint sr = m68ki_init_exception();
2007         m68ki_stack_frame_1000(REG_PPC, sr, EXCEPTION_BUS_ERROR);
2008
2009         m68ki_jump_vector(EXCEPTION_BUS_ERROR);
2010         longjmp(m68ki_bus_error_jmp_buf, 1);
2011 }
2012
2013 extern int cpu_log_enabled;
2014
2015 /* Exception for A-Line instructions */
2016 static inline void m68ki_exception_1010(void)
2017 {
2018         uint sr;
2019 #if M68K_LOG_1010_1111 == OPT_ON
2020         M68K_DO_LOG_EMU((M68K_LOG_FILEHANDLE "%s at %08x: called 1010 instruction %04x (%s)\n",
2021                                          m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PPC), REG_IR,
2022                                          m68ki_disassemble_quick(ADDRESS_68K(REG_PPC))));
2023 #endif
2024
2025         sr = m68ki_init_exception();
2026         m68ki_stack_frame_0000(REG_PPC, sr, EXCEPTION_1010);
2027         m68ki_jump_vector(EXCEPTION_1010);
2028
2029         /* Use up some clock cycles and undo the instruction's cycles */
2030         USE_CYCLES(CYC_EXCEPTION[EXCEPTION_1010] - CYC_INSTRUCTION[REG_IR]);
2031 }
2032
2033 /* Exception for F-Line instructions */
2034 static inline void m68ki_exception_1111(void)
2035 {
2036         uint sr;
2037
2038 #if M68K_LOG_1010_1111 == OPT_ON
2039         M68K_DO_LOG_EMU((M68K_LOG_FILEHANDLE "%s at %08x: called 1111 instruction %04x (%s)\n",
2040                                          m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PPC), REG_IR,
2041                                          m68ki_disassemble_quick(ADDRESS_68K(REG_PPC))));
2042 #endif
2043
2044         sr = m68ki_init_exception();
2045         m68ki_stack_frame_0000(REG_PPC, sr, EXCEPTION_1111);
2046         m68ki_jump_vector(EXCEPTION_1111);
2047
2048         /* Use up some clock cycles and undo the instruction's cycles */
2049         USE_CYCLES(CYC_EXCEPTION[EXCEPTION_1111] - CYC_INSTRUCTION[REG_IR]);
2050 }
2051
2052 #if M68K_ILLG_HAS_CALLBACK == OPT_SPECIFY_HANDLER
2053 extern int m68ki_illg_callback(int);
2054 #endif
2055
2056 /* Exception for illegal instructions */
2057 static inline void m68ki_exception_illegal(void)
2058 {
2059         uint sr;
2060
2061         M68K_DO_LOG((M68K_LOG_FILEHANDLE "%s at %08x: illegal instruction %04x (%s)\n",
2062                                  m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PPC), REG_IR,
2063                                  m68ki_disassemble_quick(ADDRESS_68K(REG_PPC))));
2064         if (m68ki_illg_callback(REG_IR))
2065             return;
2066
2067         sr = m68ki_init_exception();
2068
2069         #if M68K_EMULATE_ADDRESS_ERROR == OPT_ON
2070         if(CPU_TYPE_IS_000(CPU_TYPE))
2071         {
2072                 CPU_INSTR_MODE = INSTRUCTION_NO;
2073         }
2074         #endif /* M68K_EMULATE_ADDRESS_ERROR */
2075
2076         m68ki_stack_frame_0000(REG_PPC, sr, EXCEPTION_ILLEGAL_INSTRUCTION);
2077         m68ki_jump_vector(EXCEPTION_ILLEGAL_INSTRUCTION);
2078
2079         /* Use up some clock cycles and undo the instruction's cycles */
2080         USE_CYCLES(CYC_EXCEPTION[EXCEPTION_ILLEGAL_INSTRUCTION] - CYC_INSTRUCTION[REG_IR]);
2081 }
2082
2083 /* Exception for format errror in RTE */
2084 static inline void m68ki_exception_format_error(void)
2085 {
2086         uint sr = m68ki_init_exception();
2087         m68ki_stack_frame_0000(REG_PC, sr, EXCEPTION_FORMAT_ERROR);
2088         m68ki_jump_vector(EXCEPTION_FORMAT_ERROR);
2089
2090         /* Use up some clock cycles and undo the instruction's cycles */
2091         USE_CYCLES(CYC_EXCEPTION[EXCEPTION_FORMAT_ERROR] - CYC_INSTRUCTION[REG_IR]);
2092 }
2093
2094 /* Exception for address error */
2095 static inline void m68ki_exception_address_error(void)
2096 {
2097         uint sr = m68ki_init_exception();
2098
2099         /* If we were processing a bus error, address error, or reset,
2100          * this is a catastrophic failure.
2101          * Halt the CPU
2102          */
2103         if(CPU_RUN_MODE == RUN_MODE_BERR_AERR_RESET)
2104         {
2105 m68k_read_memory_8(0x00ffff01);
2106                 CPU_STOPPED = STOP_LEVEL_HALT;
2107                 return;
2108         }
2109         CPU_RUN_MODE = RUN_MODE_BERR_AERR_RESET;
2110
2111         /* Note: This is implemented for 68000 only! */
2112         m68ki_stack_frame_buserr(sr);
2113
2114         m68ki_jump_vector(EXCEPTION_ADDRESS_ERROR);
2115
2116         /* Use up some clock cycles. Note that we don't need to undo the
2117         instruction's cycles here as we've longjmp:ed directly from the
2118         instruction handler without passing the part of the excecute loop
2119         that deducts instruction cycles */
2120         USE_CYCLES(CYC_EXCEPTION[EXCEPTION_ADDRESS_ERROR]);
2121 }
2122
2123
2124 /* Service an interrupt request and start exception processing */
2125 static inline void m68ki_exception_interrupt(uint int_level)
2126 {
2127         uint vector;
2128         uint sr;
2129         uint new_pc;
2130
2131         #if M68K_EMULATE_ADDRESS_ERROR == OPT_ON
2132         if(CPU_TYPE_IS_000(CPU_TYPE))
2133         {
2134                 CPU_INSTR_MODE = INSTRUCTION_NO;
2135         }
2136         #endif /* M68K_EMULATE_ADDRESS_ERROR */
2137
2138         /* Turn off the stopped state */
2139         CPU_STOPPED &= ~STOP_LEVEL_STOP;
2140
2141         /* If we are halted, don't do anything */
2142         if(CPU_STOPPED)
2143                 return;
2144
2145         /* Acknowledge the interrupt */
2146         vector = m68ki_int_ack(int_level);
2147
2148         /* Get the interrupt vector */
2149         if(vector == M68K_INT_ACK_AUTOVECTOR)
2150                 /* Use the autovectors.  This is the most commonly used implementation */
2151                 vector = EXCEPTION_INTERRUPT_AUTOVECTOR+int_level;
2152         else if(vector == M68K_INT_ACK_SPURIOUS)
2153                 /* Called if no devices respond to the interrupt acknowledge */
2154                 vector = EXCEPTION_SPURIOUS_INTERRUPT;
2155         else if(vector > 255)
2156         {
2157                 M68K_DO_LOG_EMU((M68K_LOG_FILEHANDLE "%s at %08x: Interrupt acknowledge returned invalid vector $%x\n",
2158                                  m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PC), vector));
2159                 return;
2160         }
2161
2162         /* Start exception processing */
2163         sr = m68ki_init_exception();
2164
2165         /* Set the interrupt mask to the level of the one being serviced */
2166         FLAG_INT_MASK = int_level<<8;
2167
2168         /* Get the new PC */
2169         new_pc = m68ki_read_data_32((vector<<2) + REG_VBR);
2170
2171         /* If vector is uninitialized, call the uninitialized interrupt vector */
2172         if(new_pc == 0)
2173                 new_pc = m68ki_read_data_32((EXCEPTION_UNINITIALIZED_INTERRUPT<<2) + REG_VBR);
2174
2175         /* Generate a stack frame */
2176         m68ki_stack_frame_0000(REG_PC, sr, vector);
2177         if(FLAG_M && CPU_TYPE_IS_EC020_PLUS(CPU_TYPE))
2178         {
2179                 /* Create throwaway frame */
2180                 m68ki_set_sm_flag(FLAG_S);      /* clear M */
2181                 sr |= 0x2000; /* Same as SR in master stack frame except S is forced high */
2182                 m68ki_stack_frame_0001(REG_PC, sr, vector);
2183         }
2184
2185         m68ki_jump(new_pc);
2186
2187         /* Defer cycle counting until later */
2188         USE_CYCLES(CYC_EXCEPTION[vector]);
2189
2190 #if !M68K_EMULATE_INT_ACK
2191         /* Automatically clear IRQ if we are not using an acknowledge scheme */
2192         CPU_INT_LEVEL = 0;
2193 #endif /* M68K_EMULATE_INT_ACK */
2194 }
2195
2196
2197 /* ASG: Check for interrupts */
2198 static inline void m68ki_check_interrupts(void)
2199 {
2200         if(m68ki_cpu.nmi_pending)
2201         {
2202                 m68ki_cpu.nmi_pending = FALSE;
2203                 m68ki_exception_interrupt(7);
2204         }
2205         else if(CPU_INT_LEVEL > FLAG_INT_MASK)
2206                 m68ki_exception_interrupt(CPU_INT_LEVEL>>8);
2207 }
2208
2209
2210
2211 /* ======================================================================== */
2212 /* ============================== END OF FILE ============================= */
2213 /* ======================================================================== */
2214
2215 #ifdef __cplusplus
2216 }
2217 #endif
2218
2219 #endif /* M68KCPU__HEADER */