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