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