Thu, 17 Apr 2014 01:50:41 -0600
ignore out-of-range addresses on low-level format commands (s4diag formats once sector past the end of each track)
1 #include <stdio.h>
2 /* ======================================================================== */
3 /* ========================= LICENSING & COPYRIGHT ======================== */
4 /* ======================================================================== */
5 /*
6 * MUSASHI
7 * Version 3.3
8 *
9 * A portable Motorola M680x0 processor emulation engine.
10 * Copyright 1998-2001 Karl Stenerud. All rights reserved.
11 *
12 * This code may be freely used for non-commercial purposes as long as this
13 * copyright notice remains unaltered in the source code and any binary files
14 * containing this code in compiled form.
15 *
16 * All other lisencing terms must be negotiated with the author
17 * (Karl Stenerud).
18 *
19 * The latest version of this code can be obtained at:
20 * http://kstenerud.cjb.net
21 */
26 #ifndef M68KCPU__HEADER
27 #define M68KCPU__HEADER
29 #include "m68k.h"
30 #include <limits.h>
31 #include <string.h>
33 #include <setjmp.h>
35 /* ======================================================================== */
36 /* ==================== ARCHITECTURE-DEPENDANT DEFINES ==================== */
37 /* ======================================================================== */
39 /* Check for > 32bit sizes */
40 #if UINT_MAX > 0xffffffff
41 #define M68K_INT_GT_32_BIT 1
42 #endif
44 /* Data types used in this emulation core */
45 #undef sint8
46 #undef sint16
47 #undef sint32
48 #undef sint64
49 #undef uint8
50 #undef uint16
51 #undef uint32
52 #undef uint64
53 #undef sint
54 #undef uint
56 #define sint8 signed char /* ASG: changed from char to signed char */
57 #define sint16 signed short
58 #define sint32 signed long
59 #define uint8 unsigned char
60 #define uint16 unsigned short
61 #define uint32 unsigned long
63 /* signed and unsigned int must be at least 32 bits wide */
64 #define sint signed int
65 #define uint unsigned int
68 #if M68K_USE_64_BIT
69 #define sint64 signed long long
70 #define uint64 unsigned long long
71 #else
72 #define sint64 sint32
73 #define uint64 uint32
74 #endif /* M68K_USE_64_BIT */
78 /* Allow for architectures that don't have 8-bit sizes */
79 #if UCHAR_MAX == 0xff
80 #define MAKE_INT_8(A) (sint8)(A)
81 #else
82 #undef sint8
83 #define sint8 signed int
84 #undef uint8
85 #define uint8 unsigned int
86 INLINE sint MAKE_INT_8(uint value)
87 {
88 return (value & 0x80) ? value | ~0xff : value & 0xff;
89 }
90 #endif /* UCHAR_MAX == 0xff */
93 /* Allow for architectures that don't have 16-bit sizes */
94 #if USHRT_MAX == 0xffff
95 #define MAKE_INT_16(A) (sint16)(A)
96 #else
97 #undef sint16
98 #define sint16 signed int
99 #undef uint16
100 #define uint16 unsigned int
101 INLINE sint MAKE_INT_16(uint value)
102 {
103 return (value & 0x8000) ? value | ~0xffff : value & 0xffff;
104 }
105 #endif /* USHRT_MAX == 0xffff */
108 /* Allow for architectures that don't have 32-bit sizes */
109 #if ULONG_MAX == 0xffffffff
110 #define MAKE_INT_32(A) (sint32)(A)
111 #else
112 #undef sint32
113 #define sint32 signed int
114 #undef uint32
115 #define uint32 unsigned int
116 INLINE sint MAKE_INT_32(uint value)
117 {
118 return (value & 0x80000000) ? value | ~0xffffffff : value & 0xffffffff;
119 }
120 #endif /* ULONG_MAX == 0xffffffff */
125 /* ======================================================================== */
126 /* ============================ GENERAL DEFINES =========================== */
127 /* ======================================================================== */
129 /* Exception Vectors handled by emulation */
130 #define EXCEPTION_BUS_ERROR 2 /* This one is not emulated! */
131 #define EXCEPTION_ADDRESS_ERROR 3 /* This one is partially emulated (doesn't stack a proper frame yet) */
132 #define EXCEPTION_ILLEGAL_INSTRUCTION 4
133 #define EXCEPTION_ZERO_DIVIDE 5
134 #define EXCEPTION_CHK 6
135 #define EXCEPTION_TRAPV 7
136 #define EXCEPTION_PRIVILEGE_VIOLATION 8
137 #define EXCEPTION_TRACE 9
138 #define EXCEPTION_1010 10
139 #define EXCEPTION_1111 11
140 #define EXCEPTION_FORMAT_ERROR 14
141 #define EXCEPTION_UNINITIALIZED_INTERRUPT 15
142 #define EXCEPTION_SPURIOUS_INTERRUPT 24
143 #define EXCEPTION_INTERRUPT_AUTOVECTOR 24
144 #define EXCEPTION_TRAP_BASE 32
146 /* Function codes set by CPU during data/address bus activity */
147 #define FUNCTION_CODE_USER_DATA 1
148 #define FUNCTION_CODE_USER_PROGRAM 2
149 #define FUNCTION_CODE_SUPERVISOR_DATA 5
150 #define FUNCTION_CODE_SUPERVISOR_PROGRAM 6
151 #define FUNCTION_CODE_CPU_SPACE 7
153 /* CPU types for deciding what to emulate */
154 #define CPU_TYPE_000 1
155 #define CPU_TYPE_010 2
156 #define CPU_TYPE_EC020 4
157 #define CPU_TYPE_020 8
159 /* Different ways to stop the CPU */
160 #define STOP_LEVEL_STOP 1
161 #define STOP_LEVEL_HALT 2
163 #ifndef NULL
164 #define NULL ((void*)0)
165 #endif
167 /* ======================================================================== */
168 /* ================================ MACROS ================================ */
169 /* ======================================================================== */
172 /* ---------------------------- General Macros ---------------------------- */
174 /* Bit Isolation Macros */
175 #define BIT_0(A) ((A) & 0x00000001)
176 #define BIT_1(A) ((A) & 0x00000002)
177 #define BIT_2(A) ((A) & 0x00000004)
178 #define BIT_3(A) ((A) & 0x00000008)
179 #define BIT_4(A) ((A) & 0x00000010)
180 #define BIT_5(A) ((A) & 0x00000020)
181 #define BIT_6(A) ((A) & 0x00000040)
182 #define BIT_7(A) ((A) & 0x00000080)
183 #define BIT_8(A) ((A) & 0x00000100)
184 #define BIT_9(A) ((A) & 0x00000200)
185 #define BIT_A(A) ((A) & 0x00000400)
186 #define BIT_B(A) ((A) & 0x00000800)
187 #define BIT_C(A) ((A) & 0x00001000)
188 #define BIT_D(A) ((A) & 0x00002000)
189 #define BIT_E(A) ((A) & 0x00004000)
190 #define BIT_F(A) ((A) & 0x00008000)
191 #define BIT_10(A) ((A) & 0x00010000)
192 #define BIT_11(A) ((A) & 0x00020000)
193 #define BIT_12(A) ((A) & 0x00040000)
194 #define BIT_13(A) ((A) & 0x00080000)
195 #define BIT_14(A) ((A) & 0x00100000)
196 #define BIT_15(A) ((A) & 0x00200000)
197 #define BIT_16(A) ((A) & 0x00400000)
198 #define BIT_17(A) ((A) & 0x00800000)
199 #define BIT_18(A) ((A) & 0x01000000)
200 #define BIT_19(A) ((A) & 0x02000000)
201 #define BIT_1A(A) ((A) & 0x04000000)
202 #define BIT_1B(A) ((A) & 0x08000000)
203 #define BIT_1C(A) ((A) & 0x10000000)
204 #define BIT_1D(A) ((A) & 0x20000000)
205 #define BIT_1E(A) ((A) & 0x40000000)
206 #define BIT_1F(A) ((A) & 0x80000000)
208 /* Get the most significant bit for specific sizes */
209 #define GET_MSB_8(A) ((A) & 0x80)
210 #define GET_MSB_9(A) ((A) & 0x100)
211 #define GET_MSB_16(A) ((A) & 0x8000)
212 #define GET_MSB_17(A) ((A) & 0x10000)
213 #define GET_MSB_32(A) ((A) & 0x80000000)
214 #if M68K_USE_64_BIT
215 #define GET_MSB_33(A) ((A) & 0x100000000)
216 #endif /* M68K_USE_64_BIT */
218 /* Isolate nibbles */
219 #define LOW_NIBBLE(A) ((A) & 0x0f)
220 #define HIGH_NIBBLE(A) ((A) & 0xf0)
222 /* These are used to isolate 8, 16, and 32 bit sizes */
223 #define MASK_OUT_ABOVE_2(A) ((A) & 3)
224 #define MASK_OUT_ABOVE_8(A) ((A) & 0xff)
225 #define MASK_OUT_ABOVE_16(A) ((A) & 0xffff)
226 #define MASK_OUT_BELOW_2(A) ((A) & ~3)
227 #define MASK_OUT_BELOW_8(A) ((A) & ~0xff)
228 #define MASK_OUT_BELOW_16(A) ((A) & ~0xffff)
230 /* No need to mask if we are 32 bit */
231 #if M68K_INT_GT_32BIT || M68K_USE_64_BIT
232 #define MASK_OUT_ABOVE_32(A) ((A) & 0xffffffff)
233 #define MASK_OUT_BELOW_32(A) ((A) & ~0xffffffff)
234 #else
235 #define MASK_OUT_ABOVE_32(A) (A)
236 #define MASK_OUT_BELOW_32(A) 0
237 #endif /* M68K_INT_GT_32BIT || M68K_USE_64_BIT */
239 /* Simulate address lines of 68k family */
240 #define ADDRESS_68K(A) ((A)&CPU_ADDRESS_MASK)
243 /* Shift & Rotate Macros. */
244 #define LSL(A, C) ((A) << (C))
245 #define LSR(A, C) ((A) >> (C))
247 /* Some > 32-bit optimizations */
248 #if M68K_INT_GT_32BIT
249 /* Shift left and right */
250 #define LSR_32(A, C) ((A) >> (C))
251 #define LSL_32(A, C) ((A) << (C))
252 #else
253 /* We have to do this because the morons at ANSI decided that shifts
254 * by >= data size are undefined.
255 */
256 #define LSR_32(A, C) ((C) < 32 ? (A) >> (C) : 0)
257 #define LSL_32(A, C) ((C) < 32 ? (A) << (C) : 0)
258 #endif /* M68K_INT_GT_32BIT */
260 #if M68K_USE_64_BIT
261 #define LSL_32_64(A, C) ((A) << (C))
262 #define LSR_32_64(A, C) ((A) >> (C))
263 #define ROL_33_64(A, C) (LSL_32_64(A, C) | LSR_32_64(A, 33-(C)))
264 #define ROR_33_64(A, C) (LSR_32_64(A, C) | LSL_32_64(A, 33-(C)))
265 #endif /* M68K_USE_64_BIT */
267 #define ROL_8(A, C) MASK_OUT_ABOVE_8(LSL(A, C) | LSR(A, 8-(C)))
268 #define ROL_9(A, C) (LSL(A, C) | LSR(A, 9-(C)))
269 #define ROL_16(A, C) MASK_OUT_ABOVE_16(LSL(A, C) | LSR(A, 16-(C)))
270 #define ROL_17(A, C) (LSL(A, C) | LSR(A, 17-(C)))
271 #define ROL_32(A, C) MASK_OUT_ABOVE_32(LSL_32(A, C) | LSR_32(A, 32-(C)))
272 #define ROL_33(A, C) (LSL_32(A, C) | LSR_32(A, 33-(C)))
274 #define ROR_8(A, C) MASK_OUT_ABOVE_8(LSR(A, C) | LSL(A, 8-(C)))
275 #define ROR_9(A, C) (LSR(A, C) | LSL(A, 9-(C)))
276 #define ROR_16(A, C) MASK_OUT_ABOVE_16(LSR(A, C) | LSL(A, 16-(C)))
277 #define ROR_17(A, C) (LSR(A, C) | LSL(A, 17-(C)))
278 #define ROR_32(A, C) MASK_OUT_ABOVE_32(LSR_32(A, C) | LSL_32(A, 32-(C)))
279 #define ROR_33(A, C) (LSR_32(A, C) | LSL_32(A, 33-(C)))
283 /* ------------------------------ CPU Access ------------------------------ */
285 /* Access the CPU registers */
286 #define CPU_TYPE m68ki_cpu.cpu_type
288 #define REG_DA m68ki_cpu.dar /* easy access to data and address regs */
289 #define REG_DA_SAVE m68ki_cpu.dar_save
290 #define REG_D m68ki_cpu.dar
291 #define REG_A (m68ki_cpu.dar+8)
292 #define REG_PPC m68ki_cpu.ppc
293 #define REG_PC m68ki_cpu.pc
294 #define REG_SP_BASE m68ki_cpu.sp
295 #define REG_USP m68ki_cpu.sp[0]
296 #define REG_ISP m68ki_cpu.sp[4]
297 #define REG_MSP m68ki_cpu.sp[6]
298 #define REG_SP m68ki_cpu.dar[15]
299 #define REG_VBR m68ki_cpu.vbr
300 #define REG_SFC m68ki_cpu.sfc
301 #define REG_DFC m68ki_cpu.dfc
302 #define REG_CACR m68ki_cpu.cacr
303 #define REG_CAAR m68ki_cpu.caar
304 #define REG_IR m68ki_cpu.ir
306 #define FLAG_T1 m68ki_cpu.t1_flag
307 #define FLAG_T0 m68ki_cpu.t0_flag
308 #define FLAG_S m68ki_cpu.s_flag
309 #define FLAG_M m68ki_cpu.m_flag
310 #define FLAG_X m68ki_cpu.x_flag
311 #define FLAG_N m68ki_cpu.n_flag
312 #define FLAG_Z m68ki_cpu.not_z_flag
313 #define FLAG_V m68ki_cpu.v_flag
314 #define FLAG_C m68ki_cpu.c_flag
315 #define FLAG_INT_MASK m68ki_cpu.int_mask
317 #define CPU_INT_LEVEL m68ki_cpu.int_level /* ASG: changed from CPU_INTS_PENDING */
318 #define CPU_INT_CYCLES m68ki_cpu.int_cycles /* ASG */
319 #define CPU_STOPPED m68ki_cpu.stopped
320 #define CPU_PREF_ADDR m68ki_cpu.pref_addr
321 #define CPU_PREF_DATA m68ki_cpu.pref_data
322 #define CPU_ADDRESS_MASK m68ki_cpu.address_mask
323 #define CPU_SR_MASK m68ki_cpu.sr_mask
325 #define BUS_ERROR_OCCURRED m68ki_cpu.bus_error_occurred
327 #define CYC_INSTRUCTION m68ki_cpu.cyc_instruction
328 #define CYC_EXCEPTION m68ki_cpu.cyc_exception
329 #define CYC_BCC_NOTAKE_B m68ki_cpu.cyc_bcc_notake_b
330 #define CYC_BCC_NOTAKE_W m68ki_cpu.cyc_bcc_notake_w
331 #define CYC_DBCC_F_NOEXP m68ki_cpu.cyc_dbcc_f_noexp
332 #define CYC_DBCC_F_EXP m68ki_cpu.cyc_dbcc_f_exp
333 #define CYC_SCC_R_FALSE m68ki_cpu.cyc_scc_r_false
334 #define CYC_MOVEM_W m68ki_cpu.cyc_movem_w
335 #define CYC_MOVEM_L m68ki_cpu.cyc_movem_l
336 #define CYC_SHIFT m68ki_cpu.cyc_shift
337 #define CYC_RESET m68ki_cpu.cyc_reset
340 #define CALLBACK_INT_ACK m68ki_cpu.int_ack_callback
341 #define CALLBACK_BKPT_ACK m68ki_cpu.bkpt_ack_callback
342 #define CALLBACK_RESET_INSTR m68ki_cpu.reset_instr_callback
343 #define CALLBACK_PC_CHANGED m68ki_cpu.pc_changed_callback
344 #define CALLBACK_SET_FC m68ki_cpu.set_fc_callback
345 #define CALLBACK_INSTR_HOOK m68ki_cpu.instr_hook_callback
349 /* ----------------------------- Configuration ---------------------------- */
351 /* These defines are dependant on the configuration defines in m68kconf.h */
353 /* Disable certain comparisons if we're not using all CPU types */
354 #if M68K_EMULATE_020
355 #define CPU_TYPE_IS_020_PLUS(A) ((A) & CPU_TYPE_020)
356 #define CPU_TYPE_IS_020_LESS(A) 1
357 #else
358 #define CPU_TYPE_IS_020_PLUS(A) 0
359 #define CPU_TYPE_IS_020_LESS(A) 1
360 #endif
362 #if M68K_EMULATE_EC020
363 #define CPU_TYPE_IS_EC020_PLUS(A) ((A) & (CPU_TYPE_EC020 | CPU_TYPE_020))
364 #define CPU_TYPE_IS_EC020_LESS(A) ((A) & (CPU_TYPE_000 | CPU_TYPE_010 | CPU_TYPE_EC020))
365 #else
366 #define CPU_TYPE_IS_EC020_PLUS(A) CPU_TYPE_IS_020_PLUS(A)
367 #define CPU_TYPE_IS_EC020_LESS(A) CPU_TYPE_IS_020_LESS(A)
368 #endif
370 #if M68K_EMULATE_010
371 #define CPU_TYPE_IS_010(A) ((A) == CPU_TYPE_010)
372 #define CPU_TYPE_IS_010_PLUS(A) ((A) & (CPU_TYPE_010 | CPU_TYPE_EC020 | CPU_TYPE_020))
373 #define CPU_TYPE_IS_010_LESS(A) ((A) & (CPU_TYPE_000 | CPU_TYPE_010))
374 #else
375 #define CPU_TYPE_IS_010(A) 0
376 #define CPU_TYPE_IS_010_PLUS(A) CPU_TYPE_IS_EC020_PLUS(A)
377 #define CPU_TYPE_IS_010_LESS(A) CPU_TYPE_IS_EC020_LESS(A)
378 #endif
380 #if M68K_EMULATE_020 || M68K_EMULATE_EC020
381 #define CPU_TYPE_IS_020_VARIANT(A) ((A) & (CPU_TYPE_EC020 | CPU_TYPE_020))
382 #else
383 #define CPU_TYPE_IS_020_VARIANT(A) 0
384 #endif
386 #if M68K_EMULATE_020 || M68K_EMULATE_EC020 || M68K_EMULATE_010
387 #define CPU_TYPE_IS_000(A) ((A) == CPU_TYPE_000)
388 #else
389 #define CPU_TYPE_IS_000(A) 1
390 #endif
393 #if !M68K_SEPARATE_READS
394 #define m68k_read_immediate_16(A) m68ki_read_program_16(A)
395 #define m68k_read_immediate_32(A) m68ki_read_program_32(A)
397 #define m68k_read_pcrelative_8(A) m68ki_read_program_8(A)
398 #define m68k_read_pcrelative_16(A) m68ki_read_program_16(A)
399 #define m68k_read_pcrelative_32(A) m68ki_read_program_32(A)
400 #endif /* M68K_SEPARATE_READS */
403 /* Enable or disable callback functions */
404 #if M68K_EMULATE_INT_ACK
405 #if M68K_EMULATE_INT_ACK == OPT_SPECIFY_HANDLER
406 #define m68ki_int_ack(A) M68K_INT_ACK_CALLBACK(A)
407 #else
408 #define m68ki_int_ack(A) CALLBACK_INT_ACK(A)
409 #endif
410 #else
411 /* Default action is to used autovector mode, which is most common */
412 #define m68ki_int_ack(A) M68K_INT_ACK_AUTOVECTOR
413 #endif /* M68K_EMULATE_INT_ACK */
415 #if M68K_EMULATE_BKPT_ACK
416 #if M68K_EMULATE_BKPT_ACK == OPT_SPECIFY_HANDLER
417 #define m68ki_bkpt_ack(A) M68K_BKPT_ACK_CALLBACK(A)
418 #else
419 #define m68ki_bkpt_ack(A) CALLBACK_BKPT_ACK(A)
420 #endif
421 #else
422 #define m68ki_bkpt_ack(A)
423 #endif /* M68K_EMULATE_BKPT_ACK */
425 #if M68K_EMULATE_RESET
426 #if M68K_EMULATE_RESET == OPT_SPECIFY_HANDLER
427 #define m68ki_output_reset() M68K_RESET_CALLBACK()
428 #else
429 #define m68ki_output_reset() CALLBACK_RESET_INSTR()
430 #endif
431 #else
432 #define m68ki_output_reset()
433 #endif /* M68K_EMULATE_RESET */
435 #if M68K_INSTRUCTION_HOOK
436 #if M68K_INSTRUCTION_HOOK == OPT_SPECIFY_HANDLER
437 #define m68ki_instr_hook() M68K_INSTRUCTION_CALLBACK()
438 #else
439 #define m68ki_instr_hook() CALLBACK_INSTR_HOOK()
440 #endif
441 #else
442 #define m68ki_instr_hook()
443 #endif /* M68K_INSTRUCTION_HOOK */
445 #if M68K_MONITOR_PC
446 #if M68K_MONITOR_PC == OPT_SPECIFY_HANDLER
447 #define m68ki_pc_changed(A) M68K_SET_PC_CALLBACK(ADDRESS_68K(A))
448 #else
449 #define m68ki_pc_changed(A) CALLBACK_PC_CHANGED(ADDRESS_68K(A))
450 #endif
451 #else
452 #define m68ki_pc_changed(A)
453 #endif /* M68K_MONITOR_PC */
456 /* Enable or disable function code emulation */
457 #if M68K_EMULATE_FC
458 #if M68K_EMULATE_FC == OPT_SPECIFY_HANDLER
459 #define m68ki_set_fc(A) M68K_SET_FC_CALLBACK(A)
460 #else
461 #define m68ki_set_fc(A) CALLBACK_SET_FC(A)
462 #endif
463 #define m68ki_use_data_space() m68ki_address_space = FUNCTION_CODE_USER_DATA
464 #define m68ki_use_program_space() m68ki_address_space = FUNCTION_CODE_USER_PROGRAM
465 #define m68ki_get_address_space() m68ki_address_space
466 #else
467 #define m68ki_set_fc(A)
468 #define m68ki_use_data_space()
469 #define m68ki_use_program_space()
470 #define m68ki_get_address_space() FUNCTION_CODE_USER_DATA
471 #endif /* M68K_EMULATE_FC */
474 /* Enable or disable trace emulation */
475 #if M68K_EMULATE_TRACE
476 /* Initiates trace checking before each instruction (t1) */
477 #define m68ki_trace_t1() m68ki_tracing = FLAG_T1
478 /* adds t0 to trace checking if we encounter change of flow */
479 #define m68ki_trace_t0() m68ki_tracing |= FLAG_T0
480 /* Clear all tracing */
481 #define m68ki_clear_trace() m68ki_tracing = 0
482 /* Cause a trace exception if we are tracing */
483 #define m68ki_exception_if_trace() if(m68ki_tracing) m68ki_exception_trace()
484 #else
485 #define m68ki_trace_t1()
486 #define m68ki_trace_t0()
487 #define m68ki_clear_trace()
488 #define m68ki_exception_if_trace()
489 #endif /* M68K_EMULATE_TRACE */
493 /* Address error */
494 #if M68K_EMULATE_ADDRESS_ERROR
495 extern jmp_buf m68ki_address_error_trap;
496 #define m68ki_set_address_error_trap() if(setjmp(m68ki_address_error_trap)) m68ki_exception_address_error();
497 #define m68ki_check_address_error(A) if((A)&1) longjmp(m68ki_address_error_jump, 1);
498 #else
499 #define m68ki_set_address_error_trap()
500 #define m68ki_check_address_error(A)
501 #endif /* M68K_ADDRESS_ERROR */
503 /* Logging */
504 #if M68K_LOG_ENABLE
505 #include <stdio.h>
506 extern FILE* M68K_LOG_FILEHANDLE
507 extern char* m68ki_cpu_names[];
509 #define M68K_DO_LOG(A) if(M68K_LOG_FILEHANDLE) fprintf A
510 #if M68K_LOG_1010_1111
511 #define M68K_DO_LOG_EMU(A) if(M68K_LOG_FILEHANDLE) fprintf A
512 #else
513 #define M68K_DO_LOG_EMU(A)
514 #endif
515 #else
516 #define M68K_DO_LOG(A)
517 #define M68K_DO_LOG_EMU(A)
518 #endif
522 /* -------------------------- EA / Operand Access ------------------------- */
524 /*
525 * The general instruction format follows this pattern:
526 * .... XXX. .... .YYY
527 * where XXX is register X and YYY is register Y
528 */
529 /* Data Register Isolation */
530 #define DX (REG_D[(REG_IR >> 9) & 7])
531 #define DY (REG_D[REG_IR & 7])
532 /* Address Register Isolation */
533 #define AX (REG_A[(REG_IR >> 9) & 7])
534 #define AY (REG_A[REG_IR & 7])
537 /* Effective Address Calculations */
538 #define EA_AY_AI_8() AY /* address register indirect */
539 #define EA_AY_AI_16() EA_AY_AI_8()
540 #define EA_AY_AI_32() EA_AY_AI_8()
541 #define EA_AY_PI_8() (AY++) /* postincrement (size = byte) */
542 #define EA_AY_PI_16() ((AY+=2)-2) /* postincrement (size = word) */
543 #define EA_AY_PI_32() ((AY+=4)-4) /* postincrement (size = long) */
544 #define EA_AY_PD_8() (--AY) /* predecrement (size = byte) */
545 #define EA_AY_PD_16() (AY-=2) /* predecrement (size = word) */
546 #define EA_AY_PD_32() (AY-=4) /* predecrement (size = long) */
547 #define EA_AY_DI_8() (AY+MAKE_INT_16(m68ki_read_imm_16())) /* displacement */
548 #define EA_AY_DI_16() EA_AY_DI_8()
549 #define EA_AY_DI_32() EA_AY_DI_8()
550 #define EA_AY_IX_8() m68ki_get_ea_ix(AY) /* indirect + index */
551 #define EA_AY_IX_16() EA_AY_IX_8()
552 #define EA_AY_IX_32() EA_AY_IX_8()
554 #define EA_AX_AI_8() AX
555 #define EA_AX_AI_16() EA_AX_AI_8()
556 #define EA_AX_AI_32() EA_AX_AI_8()
557 #define EA_AX_PI_8() (AX++)
558 #define EA_AX_PI_16() ((AX+=2)-2)
559 #define EA_AX_PI_32() ((AX+=4)-4)
560 #define EA_AX_PD_8() (--AX)
561 #define EA_AX_PD_16() (AX-=2)
562 #define EA_AX_PD_32() (AX-=4)
563 #define EA_AX_DI_8() (AX+MAKE_INT_16(m68ki_read_imm_16()))
564 #define EA_AX_DI_16() EA_AX_DI_8()
565 #define EA_AX_DI_32() EA_AX_DI_8()
566 #define EA_AX_IX_8() m68ki_get_ea_ix(AX)
567 #define EA_AX_IX_16() EA_AX_IX_8()
568 #define EA_AX_IX_32() EA_AX_IX_8()
570 #define EA_A7_PI_8() ((REG_A[7]+=2)-2)
571 #define EA_A7_PD_8() (REG_A[7]-=2)
573 #define EA_AW_8() MAKE_INT_16(m68ki_read_imm_16()) /* absolute word */
574 #define EA_AW_16() EA_AW_8()
575 #define EA_AW_32() EA_AW_8()
576 #define EA_AL_8() m68ki_read_imm_32() /* absolute long */
577 #define EA_AL_16() EA_AL_8()
578 #define EA_AL_32() EA_AL_8()
579 #define EA_PCDI_8() m68ki_get_ea_pcdi() /* pc indirect + displacement */
580 #define EA_PCDI_16() EA_PCDI_8()
581 #define EA_PCDI_32() EA_PCDI_8()
582 #define EA_PCIX_8() m68ki_get_ea_pcix() /* pc indirect + index */
583 #define EA_PCIX_16() EA_PCIX_8()
584 #define EA_PCIX_32() EA_PCIX_8()
587 #define OPER_I_8() m68ki_read_imm_8()
588 #define OPER_I_16() m68ki_read_imm_16()
589 #define OPER_I_32() m68ki_read_imm_32()
593 /* --------------------------- Status Register ---------------------------- */
595 /* Flag Calculation Macros */
596 #define CFLAG_8(A) (A)
597 #define CFLAG_16(A) ((A)>>8)
599 #if M68K_INT_GT_32_BIT
600 #define CFLAG_ADD_32(S, D, R) ((R)>>24)
601 #define CFLAG_SUB_32(S, D, R) ((R)>>24)
602 #else
603 #define CFLAG_ADD_32(S, D, R) (((S & D) | (~R & (S | D)))>>23)
604 #define CFLAG_SUB_32(S, D, R) (((S & R) | (~D & (S | R)))>>23)
605 #endif /* M68K_INT_GT_32_BIT */
607 #define VFLAG_ADD_8(S, D, R) ((S^R) & (D^R))
608 #define VFLAG_ADD_16(S, D, R) (((S^R) & (D^R))>>8)
609 #define VFLAG_ADD_32(S, D, R) (((S^R) & (D^R))>>24)
611 #define VFLAG_SUB_8(S, D, R) ((S^D) & (R^D))
612 #define VFLAG_SUB_16(S, D, R) (((S^D) & (R^D))>>8)
613 #define VFLAG_SUB_32(S, D, R) (((S^D) & (R^D))>>24)
615 #define NFLAG_8(A) (A)
616 #define NFLAG_16(A) ((A)>>8)
617 #define NFLAG_32(A) ((A)>>24)
618 #define NFLAG_64(A) ((A)>>56)
620 #define ZFLAG_8(A) MASK_OUT_ABOVE_8(A)
621 #define ZFLAG_16(A) MASK_OUT_ABOVE_16(A)
622 #define ZFLAG_32(A) MASK_OUT_ABOVE_32(A)
625 /* Flag values */
626 #define NFLAG_SET 0x80
627 #define NFLAG_CLEAR 0
628 #define CFLAG_SET 0x100
629 #define CFLAG_CLEAR 0
630 #define XFLAG_SET 0x100
631 #define XFLAG_CLEAR 0
632 #define VFLAG_SET 0x80
633 #define VFLAG_CLEAR 0
634 #define ZFLAG_SET 0
635 #define ZFLAG_CLEAR 0xffffffff
637 #define SFLAG_SET 4
638 #define SFLAG_CLEAR 0
639 #define MFLAG_SET 2
640 #define MFLAG_CLEAR 0
642 /* Turn flag values into 1 or 0 */
643 #define XFLAG_AS_1() ((FLAG_X>>8)&1)
644 #define NFLAG_AS_1() ((FLAG_N>>7)&1)
645 #define VFLAG_AS_1() ((FLAG_V>>7)&1)
646 #define ZFLAG_AS_1() (!FLAG_Z)
647 #define CFLAG_AS_1() ((FLAG_C>>8)&1)
650 /* Conditions */
651 #define COND_CS() (FLAG_C&0x100)
652 #define COND_CC() (!COND_CS())
653 #define COND_VS() (FLAG_V&0x80)
654 #define COND_VC() (!COND_VS())
655 #define COND_NE() FLAG_Z
656 #define COND_EQ() (!COND_NE())
657 #define COND_MI() (FLAG_N&0x80)
658 #define COND_PL() (!COND_MI())
659 #define COND_LT() ((FLAG_N^FLAG_V)&0x80)
660 #define COND_GE() (!COND_LT())
661 #define COND_HI() (COND_CC() && COND_NE())
662 #define COND_LS() (COND_CS() || COND_EQ())
663 #define COND_GT() (COND_GE() && COND_NE())
664 #define COND_LE() (COND_LT() || COND_EQ())
666 /* Reversed conditions */
667 #define COND_NOT_CS() COND_CC()
668 #define COND_NOT_CC() COND_CS()
669 #define COND_NOT_VS() COND_VC()
670 #define COND_NOT_VC() COND_VS()
671 #define COND_NOT_NE() COND_EQ()
672 #define COND_NOT_EQ() COND_NE()
673 #define COND_NOT_MI() COND_PL()
674 #define COND_NOT_PL() COND_MI()
675 #define COND_NOT_LT() COND_GE()
676 #define COND_NOT_GE() COND_LT()
677 #define COND_NOT_HI() COND_LS()
678 #define COND_NOT_LS() COND_HI()
679 #define COND_NOT_GT() COND_LE()
680 #define COND_NOT_LE() COND_GT()
682 /* Not real conditions, but here for convenience */
683 #define COND_XS() (FLAG_X&0x100)
684 #define COND_XC() (!COND_XS)
687 /* Get the condition code register */
688 #define m68ki_get_ccr() ((COND_XS() >> 4) | \
689 (COND_MI() >> 4) | \
690 (COND_EQ() << 2) | \
691 (COND_VS() >> 6) | \
692 (COND_CS() >> 8))
694 /* Get the status register */
695 #define m68ki_get_sr() ( FLAG_T1 | \
696 FLAG_T0 | \
697 (FLAG_S << 11) | \
698 (FLAG_M << 11) | \
699 FLAG_INT_MASK | \
700 m68ki_get_ccr())
704 /* ---------------------------- Cycle Counting ---------------------------- */
706 #define ADD_CYCLES(A) m68ki_remaining_cycles += (A)
707 #define USE_CYCLES(A) m68ki_remaining_cycles -= (A)
708 #define SET_CYCLES(A) m68ki_remaining_cycles = A
709 #define GET_CYCLES() m68ki_remaining_cycles
710 #define USE_ALL_CYCLES() m68ki_remaining_cycles = 0
714 /* ----------------------------- Read / Write ----------------------------- */
716 /* Read from the current address space */
717 #define m68ki_read_8(A) m68ki_read_8_fc (A, FLAG_S | m68ki_get_address_space())
718 #define m68ki_read_16(A) m68ki_read_16_fc(A, FLAG_S | m68ki_get_address_space())
719 #define m68ki_read_32(A) m68ki_read_32_fc(A, FLAG_S | m68ki_get_address_space())
721 /* Write to the current data space */
722 #define m68ki_write_8(A, V) m68ki_write_8_fc (A, FLAG_S | FUNCTION_CODE_USER_DATA, V)
723 #define m68ki_write_16(A, V) m68ki_write_16_fc(A, FLAG_S | FUNCTION_CODE_USER_DATA, V)
724 #define m68ki_write_32(A, V) m68ki_write_32_fc(A, FLAG_S | FUNCTION_CODE_USER_DATA, V)
726 /* map read immediate 8 to read immediate 16 */
727 #define m68ki_read_imm_8() MASK_OUT_ABOVE_8(m68ki_read_imm_16())
729 /* Map PC-relative reads */
730 #define m68ki_read_pcrel_8(A) m68k_read_pcrelative_8(A)
731 #define m68ki_read_pcrel_16(A) m68k_read_pcrelative_16(A)
732 #define m68ki_read_pcrel_32(A) m68k_read_pcrelative_32(A)
734 /* Read from the program space */
735 #define m68ki_read_program_8(A) m68ki_read_8_fc(A, FLAG_S | FUNCTION_CODE_USER_PROGRAM)
736 #define m68ki_read_program_16(A) m68ki_read_16_fc(A, FLAG_S | FUNCTION_CODE_USER_PROGRAM)
737 #define m68ki_read_program_32(A) m68ki_read_32_fc(A, FLAG_S | FUNCTION_CODE_USER_PROGRAM)
739 /* Read from the data space */
740 #define m68ki_read_data_8(A) m68ki_read_8_fc(A, FLAG_S | FUNCTION_CODE_USER_DATA)
741 #define m68ki_read_data_16(A) m68ki_read_16_fc(A, FLAG_S | FUNCTION_CODE_USER_DATA)
742 #define m68ki_read_data_32(A) m68ki_read_32_fc(A, FLAG_S | FUNCTION_CODE_USER_DATA)
746 /* ======================================================================== */
747 /* =============================== PROTOTYPES ============================= */
748 /* ======================================================================== */
750 typedef struct
751 {
752 uint cpu_type; /* CPU Type: 68000, 68010, 68EC020, or 68020 */
753 uint dar[16]; /* Data and Address Registers */
754 uint dar_save[16]; /* Saved Data and Address Registers (pushed onto the
755 stack when a bus error occurs)*/
756 uint ppc; /* Previous program counter */
757 uint pc; /* Program Counter */
758 uint sp[7]; /* User, Interrupt, and Master Stack Pointers */
759 uint vbr; /* Vector Base Register (m68010+) */
760 uint sfc; /* Source Function Code Register (m68010+) */
761 uint dfc; /* Destination Function Code Register (m68010+) */
762 uint cacr; /* Cache Control Register (m68020, unemulated) */
763 uint caar; /* Cache Address Register (m68020, unemulated) */
764 uint ir; /* Instruction Register */
765 uint t1_flag; /* Trace 1 */
766 uint t0_flag; /* Trace 0 */
767 uint s_flag; /* Supervisor */
768 uint m_flag; /* Master/Interrupt state */
769 uint x_flag; /* Extend */
770 uint n_flag; /* Negative */
771 uint not_z_flag; /* Zero, inverted for speedups */
772 uint v_flag; /* Overflow */
773 uint c_flag; /* Carry */
774 uint int_mask; /* I0-I2 */
775 uint int_level; /* State of interrupt pins IPL0-IPL2 -- ASG: changed from ints_pending */
776 uint int_cycles; /* ASG: extra cycles from generated interrupts */
777 uint stopped; /* Stopped state */
778 uint pref_addr; /* Last prefetch address */
779 uint pref_data; /* Data in the prefetch queue */
780 uint address_mask; /* Available address pins */
781 uint sr_mask; /* Implemented status register bits */
783 uint bus_error_occurred;
785 /* Clocks required for instructions / exceptions */
786 uint cyc_bcc_notake_b;
787 uint cyc_bcc_notake_w;
788 uint cyc_dbcc_f_noexp;
789 uint cyc_dbcc_f_exp;
790 uint cyc_scc_r_false;
791 uint cyc_movem_w;
792 uint cyc_movem_l;
793 uint cyc_shift;
794 uint cyc_reset;
795 uint8* cyc_instruction;
796 uint8* cyc_exception;
798 /* Callbacks to host */
799 int (*int_ack_callback)(int int_line); /* Interrupt Acknowledge */
800 void (*bkpt_ack_callback)(unsigned int data); /* Breakpoint Acknowledge */
801 void (*reset_instr_callback)(void); /* Called when a RESET instruction is encountered */
802 void (*pc_changed_callback)(unsigned int new_pc); /* Called when the PC changes by a large amount */
803 void (*set_fc_callback)(unsigned int new_fc); /* Called when the CPU function code changes */
804 void (*instr_hook_callback)(void); /* Called every instruction cycle prior to execution */
806 } m68ki_cpu_core;
809 extern m68ki_cpu_core m68ki_cpu;
810 extern sint m68ki_remaining_cycles;
811 extern uint m68ki_tracing;
812 extern uint8 m68ki_shift_8_table[];
813 extern uint16 m68ki_shift_16_table[];
814 extern uint m68ki_shift_32_table[];
815 extern uint8 m68ki_exception_cycle_table[][256];
816 extern uint m68ki_address_space;
817 extern uint8 m68ki_ea_idx_cycle_table[];
820 /* Read data immediately after the program counter */
821 INLINE uint m68ki_read_imm_16(void);
822 INLINE uint m68ki_read_imm_32(void);
824 /* Read data with specific function code */
825 INLINE uint m68ki_read_8_fc (uint address, uint fc);
826 INLINE uint m68ki_read_16_fc (uint address, uint fc);
827 INLINE uint m68ki_read_32_fc (uint address, uint fc);
829 /* Write data with specific function code */
830 INLINE void m68ki_write_8_fc (uint address, uint fc, uint value);
831 INLINE void m68ki_write_16_fc(uint address, uint fc, uint value);
832 INLINE void m68ki_write_32_fc(uint address, uint fc, uint value);
834 /* Indexed and PC-relative ea fetching */
835 INLINE uint m68ki_get_ea_pcdi(void);
836 INLINE uint m68ki_get_ea_pcix(void);
837 INLINE uint m68ki_get_ea_ix(uint An);
839 /* Operand fetching */
840 INLINE uint OPER_AY_AI_8(void);
841 INLINE uint OPER_AY_AI_16(void);
842 INLINE uint OPER_AY_AI_32(void);
843 INLINE uint OPER_AY_PI_8(void);
844 INLINE uint OPER_AY_PI_16(void);
845 INLINE uint OPER_AY_PI_32(void);
846 INLINE uint OPER_AY_PD_8(void);
847 INLINE uint OPER_AY_PD_16(void);
848 INLINE uint OPER_AY_PD_32(void);
849 INLINE uint OPER_AY_DI_8(void);
850 INLINE uint OPER_AY_DI_16(void);
851 INLINE uint OPER_AY_DI_32(void);
852 INLINE uint OPER_AY_IX_8(void);
853 INLINE uint OPER_AY_IX_16(void);
854 INLINE uint OPER_AY_IX_32(void);
856 INLINE uint OPER_AX_AI_8(void);
857 INLINE uint OPER_AX_AI_16(void);
858 INLINE uint OPER_AX_AI_32(void);
859 INLINE uint OPER_AX_PI_8(void);
860 INLINE uint OPER_AX_PI_16(void);
861 INLINE uint OPER_AX_PI_32(void);
862 INLINE uint OPER_AX_PD_8(void);
863 INLINE uint OPER_AX_PD_16(void);
864 INLINE uint OPER_AX_PD_32(void);
865 INLINE uint OPER_AX_DI_8(void);
866 INLINE uint OPER_AX_DI_16(void);
867 INLINE uint OPER_AX_DI_32(void);
868 INLINE uint OPER_AX_IX_8(void);
869 INLINE uint OPER_AX_IX_16(void);
870 INLINE uint OPER_AX_IX_32(void);
872 INLINE uint OPER_A7_PI_8(void);
873 INLINE uint OPER_A7_PD_8(void);
875 INLINE uint OPER_AW_8(void);
876 INLINE uint OPER_AW_16(void);
877 INLINE uint OPER_AW_32(void);
878 INLINE uint OPER_AL_8(void);
879 INLINE uint OPER_AL_16(void);
880 INLINE uint OPER_AL_32(void);
881 INLINE uint OPER_PCDI_8(void);
882 INLINE uint OPER_PCDI_16(void);
883 INLINE uint OPER_PCDI_32(void);
884 INLINE uint OPER_PCIX_8(void);
885 INLINE uint OPER_PCIX_16(void);
886 INLINE uint OPER_PCIX_32(void);
888 /* Stack operations */
889 INLINE void m68ki_push_16(uint value);
890 INLINE void m68ki_push_32(uint value);
891 INLINE uint m68ki_pull_16(void);
892 INLINE uint m68ki_pull_32(void);
894 /* Program flow operations */
895 INLINE void m68ki_jump(uint new_pc);
896 INLINE void m68ki_jump_vector(uint vector);
897 INLINE void m68ki_branch_8(uint offset);
898 INLINE void m68ki_branch_16(uint offset);
899 INLINE void m68ki_branch_32(uint offset);
901 /* Status register operations. */
902 INLINE void m68ki_set_s_flag(uint value); /* Only bit 2 of value should be set (i.e. 4 or 0) */
903 INLINE void m68ki_set_sm_flag(uint value); /* only bits 1 and 2 of value should be set */
904 INLINE void m68ki_set_ccr(uint value); /* set the condition code register */
905 INLINE void m68ki_set_sr(uint value); /* set the status register */
906 INLINE void m68ki_set_sr_noint(uint value); /* set the status register */
908 /* Exception processing */
909 INLINE uint m68ki_init_exception(void); /* Initial exception processing */
911 INLINE void m68ki_stack_frame_3word(uint pc, uint sr); /* Stack various frame types */
912 INLINE void m68ki_stack_frame_buserr(uint pc, uint sr, uint address, uint write, uint instruction, uint fc);
914 INLINE void m68ki_stack_frame_0000(uint pc, uint sr, uint vector);
915 INLINE void m68ki_stack_frame_0001(uint pc, uint sr, uint vector);
916 INLINE void m68ki_stack_frame_0010(uint sr, uint vector);
917 INLINE void m68ki_stack_frame_1000(uint pc, uint sr, uint vector);
918 INLINE void m68ki_stack_frame_1010(uint sr, uint vector, uint pc);
919 INLINE void m68ki_stack_frame_1011(uint sr, uint vector, uint pc);
921 INLINE void m68ki_exception_trap(uint vector);
922 INLINE void m68ki_exception_trapN(uint vector);
923 INLINE void m68ki_exception_trace(void);
924 INLINE void m68ki_exception_privilege_violation(void);
925 INLINE void m68ki_exception_bus_error(void);
926 INLINE void m68ki_exception_1010(void);
927 INLINE void m68ki_exception_1111(void);
928 INLINE void m68ki_exception_illegal(void);
929 INLINE void m68ki_exception_format_error(void);
930 INLINE void m68ki_exception_address_error(void);
931 INLINE void m68ki_exception_interrupt(uint int_level);
932 INLINE void m68ki_check_interrupts(void); /* ASG: check for interrupts */
934 /* quick disassembly (used for logging) */
935 char* m68ki_disassemble_quick(unsigned int pc, unsigned int cpu_type);
938 /* ======================================================================== */
939 /* =========================== UTILITY FUNCTIONS ========================== */
940 /* ======================================================================== */
943 /* ---------------------------- Read Immediate ---------------------------- */
945 /* Handles all immediate reads, does address error check, function code setting,
946 * and prefetching if they are enabled in m68kconf.h
947 */
948 INLINE uint m68ki_read_imm_16(void)
949 {
950 m68ki_set_fc(FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */
951 m68ki_check_address_error(REG_PC); /* auto-disable (see m68kcpu.h) */
952 #if M68K_EMULATE_PREFETCH
953 if(MASK_OUT_BELOW_2(REG_PC) != CPU_PREF_ADDR)
954 {
955 CPU_PREF_ADDR = MASK_OUT_BELOW_2(REG_PC);
956 CPU_PREF_DATA = m68k_read_immediate_32(ADDRESS_68K(CPU_PREF_ADDR));
957 }
958 REG_PC += 2;
959 return MASK_OUT_ABOVE_16(CPU_PREF_DATA >> ((2-((REG_PC-2)&2))<<3));
960 #else
961 REG_PC += 2;
962 return m68k_read_immediate_16(ADDRESS_68K(REG_PC-2));
963 #endif /* M68K_EMULATE_PREFETCH */
964 }
965 INLINE uint m68ki_read_imm_32(void)
966 {
967 #if M68K_EMULATE_PREFETCH
968 uint temp_val;
970 m68ki_set_fc(FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */
971 m68ki_check_address_error(REG_PC); /* auto-disable (see m68kcpu.h) */
972 if(MASK_OUT_BELOW_2(REG_PC) != CPU_PREF_ADDR)
973 {
974 CPU_PREF_ADDR = MASK_OUT_BELOW_2(REG_PC);
975 CPU_PREF_DATA = m68k_read_immediate_32(ADDRESS_68K(CPU_PREF_ADDR));
976 }
977 temp_val = CPU_PREF_DATA;
978 REG_PC += 2;
979 if(MASK_OUT_BELOW_2(REG_PC) != CPU_PREF_ADDR)
980 {
981 CPU_PREF_ADDR = MASK_OUT_BELOW_2(REG_PC);
982 CPU_PREF_DATA = m68k_read_immediate_32(ADDRESS_68K(CPU_PREF_ADDR));
983 temp_val = MASK_OUT_ABOVE_32((temp_val << 16) | (CPU_PREF_DATA >> 16));
984 }
985 REG_PC += 2;
987 return temp_val;
988 #else
989 m68ki_set_fc(FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */
990 m68ki_check_address_error(REG_PC); /* auto-disable (see m68kcpu.h) */
991 REG_PC += 4;
992 return m68k_read_immediate_32(ADDRESS_68K(REG_PC-4));
993 #endif /* M68K_EMULATE_PREFETCH */
994 }
998 /* ------------------------- Top level read/write ------------------------- */
1000 /* Handles all memory accesses (except for immediate reads if they are
1001 * configured to use separate functions in m68kconf.h).
1002 * All memory accesses must go through these top level functions.
1003 * These functions will also check for address error and set the function
1004 * code if they are enabled in m68kconf.h.
1005 */
1006 INLINE uint m68ki_read_8_fc(uint address, uint fc)
1007 {
1008 m68ki_set_fc(fc); /* auto-disable (see m68kcpu.h) */
1009 return m68k_read_memory_8(ADDRESS_68K(address));
1010 }
1011 INLINE uint m68ki_read_16_fc(uint address, uint fc)
1012 {
1013 m68ki_set_fc(fc); /* auto-disable (see m68kcpu.h) */
1014 m68ki_check_address_error(address); /* auto-disable (see m68kcpu.h) */
1015 return m68k_read_memory_16(ADDRESS_68K(address));
1016 }
1017 INLINE uint m68ki_read_32_fc(uint address, uint fc)
1018 {
1019 m68ki_set_fc(fc); /* auto-disable (see m68kcpu.h) */
1020 m68ki_check_address_error(address); /* auto-disable (see m68kcpu.h) */
1021 return m68k_read_memory_32(ADDRESS_68K(address));
1022 }
1024 INLINE void m68ki_write_8_fc(uint address, uint fc, uint value)
1025 {
1026 m68ki_set_fc(fc); /* auto-disable (see m68kcpu.h) */
1027 m68k_write_memory_8(ADDRESS_68K(address), value);
1028 }
1029 INLINE void m68ki_write_16_fc(uint address, uint fc, uint value)
1030 {
1031 m68ki_set_fc(fc); /* auto-disable (see m68kcpu.h) */
1032 m68ki_check_address_error(address); /* auto-disable (see m68kcpu.h) */
1033 m68k_write_memory_16(ADDRESS_68K(address), value);
1034 }
1035 INLINE void m68ki_write_32_fc(uint address, uint fc, uint value)
1036 {
1037 m68ki_set_fc(fc); /* auto-disable (see m68kcpu.h) */
1038 m68ki_check_address_error(address); /* auto-disable (see m68kcpu.h) */
1039 m68k_write_memory_32(ADDRESS_68K(address), value);
1040 }
1044 /* --------------------- Effective Address Calculation -------------------- */
1046 /* The program counter relative addressing modes cause operands to be
1047 * retrieved from program space, not data space.
1048 */
1049 INLINE uint m68ki_get_ea_pcdi(void)
1050 {
1051 uint old_pc = REG_PC;
1052 m68ki_use_program_space(); /* auto-disable */
1053 return old_pc + MAKE_INT_16(m68ki_read_imm_16());
1054 }
1057 INLINE uint m68ki_get_ea_pcix(void)
1058 {
1059 m68ki_use_program_space(); /* auto-disable */
1060 return m68ki_get_ea_ix(REG_PC);
1061 }
1063 /* Indexed addressing modes are encoded as follows:
1064 *
1065 * Base instruction format:
1066 * F E D C B A 9 8 7 6 | 5 4 3 | 2 1 0
1067 * x x x x x x x x x x | 1 1 0 | BASE REGISTER (An)
1068 *
1069 * Base instruction format for destination EA in move instructions:
1070 * F E D C | B A 9 | 8 7 6 | 5 4 3 2 1 0
1071 * x x x x | BASE REG | 1 1 0 | X X X X X X (An)
1072 *
1073 * Brief extension format:
1074 * F | E D C | B | A 9 | 8 | 7 6 5 4 3 2 1 0
1075 * D/A | REGISTER | W/L | SCALE | 0 | DISPLACEMENT
1076 *
1077 * Full extension format:
1078 * F E D C B A 9 8 7 6 5 4 3 2 1 0
1079 * D/A | REGISTER | W/L | SCALE | 1 | BS | IS | BD SIZE | 0 | I/IS
1080 * BASE DISPLACEMENT (0, 16, 32 bit) (bd)
1081 * OUTER DISPLACEMENT (0, 16, 32 bit) (od)
1082 *
1083 * D/A: 0 = Dn, 1 = An (Xn)
1084 * W/L: 0 = W (sign extend), 1 = L (.SIZE)
1085 * SCALE: 00=1, 01=2, 10=4, 11=8 (*SCALE)
1086 * BS: 0=add base reg, 1=suppress base reg (An suppressed)
1087 * IS: 0=add index, 1=suppress index (Xn suppressed)
1088 * BD SIZE: 00=reserved, 01=NULL, 10=Word, 11=Long (size of bd)
1089 *
1090 * IS I/IS Operation
1091 * 0 000 No Memory Indirect
1092 * 0 001 indir prex with null outer
1093 * 0 010 indir prex with word outer
1094 * 0 011 indir prex with long outer
1095 * 0 100 reserved
1096 * 0 101 indir postx with null outer
1097 * 0 110 indir postx with word outer
1098 * 0 111 indir postx with long outer
1099 * 1 000 no memory indirect
1100 * 1 001 mem indir with null outer
1101 * 1 010 mem indir with word outer
1102 * 1 011 mem indir with long outer
1103 * 1 100-111 reserved
1104 */
1105 INLINE uint m68ki_get_ea_ix(uint An)
1106 {
1107 /* An = base register */
1108 uint extension = m68ki_read_imm_16();
1109 uint Xn = 0; /* Index register */
1110 uint bd = 0; /* Base Displacement */
1111 uint od = 0; /* Outer Displacement */
1113 if(CPU_TYPE_IS_010_LESS(CPU_TYPE))
1114 {
1115 /* Calculate index */
1116 Xn = REG_DA[extension>>12]; /* Xn */
1117 if(!BIT_B(extension)) /* W/L */
1118 Xn = MAKE_INT_16(Xn);
1120 /* Add base register and displacement and return */
1121 return An + Xn + MAKE_INT_8(extension);
1122 }
1124 /* Brief extension format */
1125 if(!BIT_8(extension))
1126 {
1127 /* Calculate index */
1128 Xn = REG_DA[extension>>12]; /* Xn */
1129 if(!BIT_B(extension)) /* W/L */
1130 Xn = MAKE_INT_16(Xn);
1131 /* Add scale if proper CPU type */
1132 if(CPU_TYPE_IS_EC020_PLUS(CPU_TYPE))
1133 Xn <<= (extension>>9) & 3; /* SCALE */
1135 /* Add base register and displacement and return */
1136 return An + Xn + MAKE_INT_8(extension);
1137 }
1139 /* Full extension format */
1141 USE_CYCLES(m68ki_ea_idx_cycle_table[extension&0x3f]);
1143 /* Check if base register is present */
1144 if(BIT_7(extension)) /* BS */
1145 An = 0; /* An */
1147 /* Check if index is present */
1148 if(!BIT_6(extension)) /* IS */
1149 {
1150 Xn = REG_DA[extension>>12]; /* Xn */
1151 if(!BIT_B(extension)) /* W/L */
1152 Xn = MAKE_INT_16(Xn);
1153 Xn <<= (extension>>9) & 3; /* SCALE */
1154 }
1156 /* Check if base displacement is present */
1157 if(BIT_5(extension)) /* BD SIZE */
1158 bd = BIT_4(extension) ? m68ki_read_imm_32() : MAKE_INT_16(m68ki_read_imm_16());
1160 /* If no indirect action, we are done */
1161 if(!(extension&7)) /* No Memory Indirect */
1162 return An + bd + Xn;
1164 /* Check if outer displacement is present */
1165 if(BIT_1(extension)) /* I/IS: od */
1166 od = BIT_0(extension) ? m68ki_read_imm_32() : MAKE_INT_16(m68ki_read_imm_16());
1168 /* Postindex */
1169 if(BIT_2(extension)) /* I/IS: 0 = preindex, 1 = postindex */
1170 return m68ki_read_32(An + bd) + Xn + od;
1172 /* Preindex */
1173 return m68ki_read_32(An + bd + Xn) + od;
1174 }
1177 /* Fetch operands */
1178 INLINE uint OPER_AY_AI_8(void) {uint ea = EA_AY_AI_8(); return m68ki_read_8(ea); }
1179 INLINE uint OPER_AY_AI_16(void) {uint ea = EA_AY_AI_16(); return m68ki_read_16(ea);}
1180 INLINE uint OPER_AY_AI_32(void) {uint ea = EA_AY_AI_32(); return m68ki_read_32(ea);}
1181 INLINE uint OPER_AY_PI_8(void) {uint ea = EA_AY_PI_8(); return m68ki_read_8(ea); }
1182 INLINE uint OPER_AY_PI_16(void) {uint ea = EA_AY_PI_16(); return m68ki_read_16(ea);}
1183 INLINE uint OPER_AY_PI_32(void) {uint ea = EA_AY_PI_32(); return m68ki_read_32(ea);}
1184 INLINE uint OPER_AY_PD_8(void) {uint ea = EA_AY_PD_8(); return m68ki_read_8(ea); }
1185 INLINE uint OPER_AY_PD_16(void) {uint ea = EA_AY_PD_16(); return m68ki_read_16(ea);}
1186 INLINE uint OPER_AY_PD_32(void) {uint ea = EA_AY_PD_32(); return m68ki_read_32(ea);}
1187 INLINE uint OPER_AY_DI_8(void) {uint ea = EA_AY_DI_8(); return m68ki_read_8(ea); }
1188 INLINE uint OPER_AY_DI_16(void) {uint ea = EA_AY_DI_16(); return m68ki_read_16(ea);}
1189 INLINE uint OPER_AY_DI_32(void) {uint ea = EA_AY_DI_32(); return m68ki_read_32(ea);}
1190 INLINE uint OPER_AY_IX_8(void) {uint ea = EA_AY_IX_8(); return m68ki_read_8(ea); }
1191 INLINE uint OPER_AY_IX_16(void) {uint ea = EA_AY_IX_16(); return m68ki_read_16(ea);}
1192 INLINE uint OPER_AY_IX_32(void) {uint ea = EA_AY_IX_32(); return m68ki_read_32(ea);}
1194 INLINE uint OPER_AX_AI_8(void) {uint ea = EA_AX_AI_8(); return m68ki_read_8(ea); }
1195 INLINE uint OPER_AX_AI_16(void) {uint ea = EA_AX_AI_16(); return m68ki_read_16(ea);}
1196 INLINE uint OPER_AX_AI_32(void) {uint ea = EA_AX_AI_32(); return m68ki_read_32(ea);}
1197 INLINE uint OPER_AX_PI_8(void) {uint ea = EA_AX_PI_8(); return m68ki_read_8(ea); }
1198 INLINE uint OPER_AX_PI_16(void) {uint ea = EA_AX_PI_16(); return m68ki_read_16(ea);}
1199 INLINE uint OPER_AX_PI_32(void) {uint ea = EA_AX_PI_32(); return m68ki_read_32(ea);}
1200 INLINE uint OPER_AX_PD_8(void) {uint ea = EA_AX_PD_8(); return m68ki_read_8(ea); }
1201 INLINE uint OPER_AX_PD_16(void) {uint ea = EA_AX_PD_16(); return m68ki_read_16(ea);}
1202 INLINE uint OPER_AX_PD_32(void) {uint ea = EA_AX_PD_32(); return m68ki_read_32(ea);}
1203 INLINE uint OPER_AX_DI_8(void) {uint ea = EA_AX_DI_8(); return m68ki_read_8(ea); }
1204 INLINE uint OPER_AX_DI_16(void) {uint ea = EA_AX_DI_16(); return m68ki_read_16(ea);}
1205 INLINE uint OPER_AX_DI_32(void) {uint ea = EA_AX_DI_32(); return m68ki_read_32(ea);}
1206 INLINE uint OPER_AX_IX_8(void) {uint ea = EA_AX_IX_8(); return m68ki_read_8(ea); }
1207 INLINE uint OPER_AX_IX_16(void) {uint ea = EA_AX_IX_16(); return m68ki_read_16(ea);}
1208 INLINE uint OPER_AX_IX_32(void) {uint ea = EA_AX_IX_32(); return m68ki_read_32(ea);}
1210 INLINE uint OPER_A7_PI_8(void) {uint ea = EA_A7_PI_8(); return m68ki_read_8(ea); }
1211 INLINE uint OPER_A7_PD_8(void) {uint ea = EA_A7_PD_8(); return m68ki_read_8(ea); }
1213 INLINE uint OPER_AW_8(void) {uint ea = EA_AW_8(); return m68ki_read_8(ea); }
1214 INLINE uint OPER_AW_16(void) {uint ea = EA_AW_16(); return m68ki_read_16(ea);}
1215 INLINE uint OPER_AW_32(void) {uint ea = EA_AW_32(); return m68ki_read_32(ea);}
1216 INLINE uint OPER_AL_8(void) {uint ea = EA_AL_8(); return m68ki_read_8(ea); }
1217 INLINE uint OPER_AL_16(void) {uint ea = EA_AL_16(); return m68ki_read_16(ea);}
1218 INLINE uint OPER_AL_32(void) {uint ea = EA_AL_32(); return m68ki_read_32(ea);}
1219 INLINE uint OPER_PCDI_8(void) {uint ea = EA_PCDI_8(); return m68ki_read_pcrel_8(ea); }
1220 INLINE uint OPER_PCDI_16(void) {uint ea = EA_PCDI_16(); return m68ki_read_pcrel_16(ea);}
1221 INLINE uint OPER_PCDI_32(void) {uint ea = EA_PCDI_32(); return m68ki_read_pcrel_32(ea);}
1222 INLINE uint OPER_PCIX_8(void) {uint ea = EA_PCIX_8(); return m68ki_read_pcrel_8(ea); }
1223 INLINE uint OPER_PCIX_16(void) {uint ea = EA_PCIX_16(); return m68ki_read_pcrel_16(ea);}
1224 INLINE uint OPER_PCIX_32(void) {uint ea = EA_PCIX_32(); return m68ki_read_pcrel_32(ea);}
1228 /* ---------------------------- Stack Functions --------------------------- */
1230 /* Push/pull data from the stack */
1231 INLINE void m68ki_push_16(uint value)
1232 {
1233 REG_SP = MASK_OUT_ABOVE_32(REG_SP - 2);
1234 m68ki_write_16(REG_SP, value);
1235 }
1237 INLINE void m68ki_push_32(uint value)
1238 {
1239 REG_SP = MASK_OUT_ABOVE_32(REG_SP - 4);
1240 m68ki_write_32(REG_SP, value);
1241 }
1243 INLINE uint m68ki_pull_16(void)
1244 {
1245 REG_SP = MASK_OUT_ABOVE_32(REG_SP + 2);
1246 return m68ki_read_16(REG_SP-2);
1247 }
1249 INLINE uint m68ki_pull_32(void)
1250 {
1251 REG_SP = MASK_OUT_ABOVE_32(REG_SP + 4);
1252 return m68ki_read_32(REG_SP-4);
1253 }
1256 /* Increment/decrement the stack as if doing a push/pull but
1257 * don't do any memory access.
1258 */
1259 INLINE void m68ki_fake_push_16(void)
1260 {
1261 REG_SP = MASK_OUT_ABOVE_32(REG_SP - 2);
1262 }
1264 INLINE void m68ki_fake_push_32(void)
1265 {
1266 REG_SP = MASK_OUT_ABOVE_32(REG_SP - 4);
1267 }
1269 INLINE void m68ki_fake_pull_16(void)
1270 {
1271 REG_SP = MASK_OUT_ABOVE_32(REG_SP + 2);
1272 }
1274 INLINE void m68ki_fake_pull_32(void)
1275 {
1276 REG_SP = MASK_OUT_ABOVE_32(REG_SP + 4);
1277 }
1280 /* ----------------------------- Program Flow ----------------------------- */
1282 /* Jump to a new program location or vector.
1283 * These functions will also call the pc_changed callback if it was enabled
1284 * in m68kconf.h.
1285 */
1286 INLINE void m68ki_jump(uint new_pc)
1287 {
1288 REG_PC = new_pc;
1289 m68ki_pc_changed(REG_PC);
1290 }
1292 INLINE void m68ki_jump_vector(uint vector)
1293 {
1294 REG_PC = (vector<<2) + REG_VBR;
1295 REG_PC = m68ki_read_data_32(REG_PC);
1296 m68ki_pc_changed(REG_PC);
1297 }
1300 /* Branch to a new memory location.
1301 * The 32-bit branch will call pc_changed if it was enabled in m68kconf.h.
1302 * So far I've found no problems with not calling pc_changed for 8 or 16
1303 * bit branches.
1304 */
1305 INLINE void m68ki_branch_8(uint offset)
1306 {
1307 REG_PC += MAKE_INT_8(offset);
1308 }
1310 INLINE void m68ki_branch_16(uint offset)
1311 {
1312 REG_PC += MAKE_INT_16(offset);
1313 }
1315 INLINE void m68ki_branch_32(uint offset)
1316 {
1317 REG_PC += offset;
1318 m68ki_pc_changed(REG_PC);
1319 }
1323 /* ---------------------------- Status Register --------------------------- */
1325 /* Set the S flag and change the active stack pointer.
1326 * Note that value MUST be 4 or 0.
1327 */
1328 INLINE void m68ki_set_s_flag(uint value)
1329 {
1330 /* Backup the old stack pointer */
1331 REG_SP_BASE[FLAG_S | ((FLAG_S>>1) & FLAG_M)] = REG_SP;
1332 /* Set the S flag */
1333 FLAG_S = value;
1334 /* Set the new stack pointer */
1335 REG_SP = REG_SP_BASE[FLAG_S | ((FLAG_S>>1) & FLAG_M)];
1336 }
1338 /* Set the S and M flags and change the active stack pointer.
1339 * Note that value MUST be 0, 2, 4, or 6 (bit2 = S, bit1 = M).
1340 */
1341 INLINE void m68ki_set_sm_flag(uint value)
1342 {
1343 /* Backup the old stack pointer */
1344 REG_SP_BASE[FLAG_S | ((FLAG_S>>1) & FLAG_M)] = REG_SP;
1345 /* Set the S and M flags */
1346 FLAG_S = value & SFLAG_SET;
1347 FLAG_M = value & MFLAG_SET;
1348 /* Set the new stack pointer */
1349 REG_SP = REG_SP_BASE[FLAG_S | ((FLAG_S>>1) & FLAG_M)];
1350 }
1353 /* Set the condition code register */
1354 INLINE void m68ki_set_ccr(uint value)
1355 {
1356 FLAG_X = BIT_4(value) << 4;
1357 FLAG_N = BIT_3(value) << 4;
1358 FLAG_Z = !BIT_2(value);
1359 FLAG_V = BIT_1(value) << 6;
1360 FLAG_C = BIT_0(value) << 8;
1361 }
1363 /* Set the status register but don't check for interrupts */
1364 INLINE void m68ki_set_sr_noint(uint value)
1365 {
1366 /* Mask out the "unimplemented" bits */
1367 value &= CPU_SR_MASK;
1369 /* Now set the status register */
1370 FLAG_T1 = BIT_F(value);
1371 FLAG_T0 = BIT_E(value);
1372 FLAG_INT_MASK = value & 0x0700;
1373 m68ki_set_ccr(value);
1374 m68ki_set_sm_flag((value >> 11) & 6);
1375 }
1377 /* Set the status register and check for interrupts */
1378 INLINE void m68ki_set_sr(uint value)
1379 {
1380 m68ki_set_sr_noint(value);
1381 m68ki_check_interrupts();
1382 }
1385 /* ------------------------- Exception Processing ------------------------- */
1387 /* Initiate exception processing */
1388 INLINE uint m68ki_init_exception(void)
1389 {
1390 /* Save the old status register */
1391 uint sr = m68ki_get_sr();
1393 /* Turn off trace flag, clear pending traces */
1394 FLAG_T1 = FLAG_T0 = 0;
1395 m68ki_clear_trace();
1396 /* Enter supervisor mode */
1397 m68ki_set_s_flag(SFLAG_SET);
1399 return sr;
1400 }
1402 /* 3 word stack frame (68000 only) */
1403 INLINE void m68ki_stack_frame_3word(uint pc, uint sr)
1404 {
1405 m68ki_push_32(pc);
1406 m68ki_push_16(sr);
1407 }
1409 /* Format 0 stack frame.
1410 * This is the standard stack frame for 68010+.
1411 */
1412 INLINE void m68ki_stack_frame_0000(uint pc, uint sr, uint vector)
1413 {
1414 /* Stack a 3-word frame if we are 68000 */
1415 if(CPU_TYPE == CPU_TYPE_000)
1416 {
1417 m68ki_stack_frame_3word(pc, sr);
1418 return;
1419 }
1420 m68ki_push_16(vector<<2);
1421 m68ki_push_32(pc);
1422 m68ki_push_16(sr);
1423 }
1425 /* Format 1 stack frame (68020).
1426 * For 68020, this is the 4 word throwaway frame.
1427 */
1428 INLINE void m68ki_stack_frame_0001(uint pc, uint sr, uint vector)
1429 {
1430 m68ki_push_16(0x1000 | (vector<<2));
1431 m68ki_push_32(pc);
1432 m68ki_push_16(sr);
1433 }
1435 /* Format 2 stack frame.
1436 * This is used only by 68020 for trap exceptions.
1437 */
1438 INLINE void m68ki_stack_frame_0010(uint sr, uint vector)
1439 {
1440 m68ki_push_32(REG_PPC);
1441 m68ki_push_16(0x2000 | (vector<<2));
1442 m68ki_push_32(REG_PC);
1443 m68ki_push_16(sr);
1444 }
1447 /* Bus error stack frame (68000 only).
1448 */
1449 INLINE void m68ki_stack_frame_buserr(uint pc, uint sr, uint address, uint write, uint instruction, uint fc)
1450 {
1451 m68ki_push_32(pc);
1452 m68ki_push_16(sr);
1453 m68ki_push_16(REG_IR);
1454 m68ki_push_32(address); /* access address */
1455 /* 0 0 0 0 0 0 0 0 0 0 0 R/W I/N FC
1456 * R/W 0 = write, 1 = read
1457 * I/N 0 = instruction, 1 = not
1458 * FC 3-bit function code
1459 */
1460 m68ki_push_16(((!write)<<4) | ((!instruction)<<3) | fc);
1461 }
1463 /* Format 8 stack frame (68010).
1464 * 68010 only. This is the 29 word bus/address error frame.
1465 */
1466 void m68ki_stack_frame_1000(uint pc, uint sr, uint vector)
1467 {
1468 /* VERSION
1469 * NUMBER
1470 * INTERNAL INFORMATION, 16 WORDS
1471 */
1472 m68ki_fake_push_32();
1473 m68ki_fake_push_32();
1474 m68ki_fake_push_32();
1475 m68ki_fake_push_32();
1476 m68ki_fake_push_32();
1477 m68ki_fake_push_32();
1478 m68ki_fake_push_32();
1479 m68ki_fake_push_32();
1481 /* INSTRUCTION INPUT BUFFER */
1482 m68ki_push_16(0);
1484 /* UNUSED, RESERVED (not written) */
1485 m68ki_fake_push_16();
1487 /* DATA INPUT BUFFER */
1488 m68ki_push_16(0);
1490 /* UNUSED, RESERVED (not written) */
1491 m68ki_fake_push_16();
1493 /* DATA OUTPUT BUFFER */
1494 m68ki_push_16(0);
1496 /* UNUSED, RESERVED (not written) */
1497 m68ki_fake_push_16();
1499 /* FAULT ADDRESS */
1500 m68ki_push_32(0);
1502 /* SPECIAL STATUS WORD */
1503 m68ki_push_16(0);
1505 /* 1000, VECTOR OFFSET */
1506 m68ki_push_16(0x8000 | (vector<<2));
1508 /* PROGRAM COUNTER */
1509 m68ki_push_32(pc);
1511 /* STATUS REGISTER */
1512 m68ki_push_16(sr);
1513 }
1515 /* Format A stack frame (short bus fault).
1516 * This is used only by 68020 for bus fault and address error
1517 * if the error happens at an instruction boundary.
1518 * PC stacked is address of next instruction.
1519 */
1520 void m68ki_stack_frame_1010(uint sr, uint vector, uint pc)
1521 {
1522 /* INTERNAL REGISTER */
1523 m68ki_push_16(0);
1525 /* INTERNAL REGISTER */
1526 m68ki_push_16(0);
1528 /* DATA OUTPUT BUFFER (2 words) */
1529 m68ki_push_32(0);
1531 /* INTERNAL REGISTER */
1532 m68ki_push_16(0);
1534 /* INTERNAL REGISTER */
1535 m68ki_push_16(0);
1537 /* DATA CYCLE FAULT ADDRESS (2 words) */
1538 m68ki_push_32(0);
1540 /* INSTRUCTION PIPE STAGE B */
1541 m68ki_push_16(0);
1543 /* INSTRUCTION PIPE STAGE C */
1544 m68ki_push_16(0);
1546 /* SPECIAL STATUS REGISTER */
1547 m68ki_push_16(0);
1549 /* INTERNAL REGISTER */
1550 m68ki_push_16(0);
1552 /* 1010, VECTOR OFFSET */
1553 m68ki_push_16(0xa000 | (vector<<2));
1555 /* PROGRAM COUNTER */
1556 m68ki_push_32(pc);
1558 /* STATUS REGISTER */
1559 m68ki_push_16(sr);
1560 }
1562 /* Format B stack frame (long bus fault).
1563 * This is used only by 68020 for bus fault and address error
1564 * if the error happens during instruction execution.
1565 * PC stacked is address of instruction in progress.
1566 */
1567 void m68ki_stack_frame_1011(uint sr, uint vector, uint pc)
1568 {
1569 /* INTERNAL REGISTERS (18 words) */
1570 m68ki_push_32(0);
1571 m68ki_push_32(0);
1572 m68ki_push_32(0);
1573 m68ki_push_32(0);
1574 m68ki_push_32(0);
1575 m68ki_push_32(0);
1576 m68ki_push_32(0);
1577 m68ki_push_32(0);
1578 m68ki_push_32(0);
1580 /* VERSION# (4 bits), INTERNAL INFORMATION */
1581 m68ki_push_16(0);
1583 /* INTERNAL REGISTERS (3 words) */
1584 m68ki_push_32(0);
1585 m68ki_push_16(0);
1587 /* DATA INTPUT BUFFER (2 words) */
1588 m68ki_push_32(0);
1590 /* INTERNAL REGISTERS (2 words) */
1591 m68ki_push_32(0);
1593 /* STAGE B ADDRESS (2 words) */
1594 m68ki_push_32(0);
1596 /* INTERNAL REGISTER (4 words) */
1597 m68ki_push_32(0);
1598 m68ki_push_32(0);
1600 /* DATA OUTPUT BUFFER (2 words) */
1601 m68ki_push_32(0);
1603 /* INTERNAL REGISTER */
1604 m68ki_push_16(0);
1606 /* INTERNAL REGISTER */
1607 m68ki_push_16(0);
1609 /* DATA CYCLE FAULT ADDRESS (2 words) */
1610 m68ki_push_32(0);
1612 /* INSTRUCTION PIPE STAGE B */
1613 m68ki_push_16(0);
1615 /* INSTRUCTION PIPE STAGE C */
1616 m68ki_push_16(0);
1618 /* SPECIAL STATUS REGISTER */
1619 m68ki_push_16(0);
1621 /* INTERNAL REGISTER */
1622 m68ki_push_16(0);
1624 /* 1011, VECTOR OFFSET */
1625 m68ki_push_16(0xb000 | (vector<<2));
1627 /* PROGRAM COUNTER */
1628 m68ki_push_32(pc);
1630 /* STATUS REGISTER */
1631 m68ki_push_16(sr);
1632 }
1635 /* Used for Group 2 exceptions.
1636 * These stack a type 2 frame on the 020.
1637 */
1638 INLINE void m68ki_exception_trap(uint vector)
1639 {
1640 uint sr = m68ki_init_exception();
1642 if(CPU_TYPE_IS_010_LESS(CPU_TYPE))
1643 m68ki_stack_frame_0000(REG_PC, sr, vector);
1644 else
1645 m68ki_stack_frame_0010(sr, vector);
1647 m68ki_jump_vector(vector);
1649 /* Use up some clock cycles */
1650 USE_CYCLES(CYC_EXCEPTION[vector]);
1651 }
1653 /* Trap#n stacks a 0 frame but behaves like group2 otherwise */
1654 INLINE void m68ki_exception_trapN(uint vector)
1655 {
1656 uint sr = m68ki_init_exception();
1657 m68ki_stack_frame_0000(REG_PC, sr, vector);
1658 m68ki_jump_vector(vector);
1660 /* Use up some clock cycles */
1661 USE_CYCLES(CYC_EXCEPTION[vector]);
1662 }
1664 /* Exception for trace mode */
1665 INLINE void m68ki_exception_trace(void)
1666 {
1667 uint sr = m68ki_init_exception();
1669 if(CPU_TYPE_IS_010_LESS(CPU_TYPE))
1670 m68ki_stack_frame_0000(REG_PC, sr, EXCEPTION_TRACE);
1671 else
1672 m68ki_stack_frame_0010(sr, EXCEPTION_TRACE);
1674 m68ki_jump_vector(EXCEPTION_TRACE);
1676 /* Trace nullifies a STOP instruction */
1677 CPU_STOPPED &= ~STOP_LEVEL_STOP;
1679 /* Use up some clock cycles */
1680 USE_CYCLES(CYC_EXCEPTION[EXCEPTION_TRACE]);
1681 }
1683 /* Exception for privilege violation */
1684 INLINE void m68ki_exception_privilege_violation(void)
1685 {
1686 uint sr = m68ki_init_exception();
1687 m68ki_stack_frame_0000(REG_PC, sr, EXCEPTION_PRIVILEGE_VIOLATION);
1688 m68ki_jump_vector(EXCEPTION_PRIVILEGE_VIOLATION);
1690 /* Use up some clock cycles and undo the instruction's cycles */
1691 USE_CYCLES(CYC_EXCEPTION[EXCEPTION_PRIVILEGE_VIOLATION] - CYC_INSTRUCTION[REG_IR]);
1692 }
1694 extern jmp_buf m68ki_bus_error_jmp_buf;
1695 extern jmp_buf m68ki_bus_error_return_jmp_buf;
1697 #define m68ki_check_bus_error_trap() setjmp(m68ki_bus_error_jmp_buf)
1699 /* Exception for bus error */
1700 INLINE void m68ki_exception_bus_error(void)
1701 {
1702 int i;
1703 BUS_ERROR_OCCURRED = 1;
1704 /* Use up some clock cycles and undo the instruction's cycles */
1705 USE_CYCLES(CYC_EXCEPTION[EXCEPTION_BUS_ERROR] - CYC_INSTRUCTION[REG_IR]);
1707 for (i = 15; i >= 0; i--){
1708 REG_DA[i] = REG_DA_SAVE[i];
1709 }
1711 uint sr = m68ki_init_exception();
1712 m68ki_stack_frame_1000(REG_PPC, sr, EXCEPTION_BUS_ERROR);
1714 m68ki_jump_vector(EXCEPTION_BUS_ERROR);
1715 longjmp(m68ki_bus_error_jmp_buf, 1);
1716 }
1718 extern int cpu_log_enabled;
1720 /* Exception for A-Line instructions */
1721 INLINE void m68ki_exception_1010(void)
1722 {
1723 uint sr;
1724 #if M68K_LOG_1010_1111 == OPT_ON
1725 M68K_DO_LOG_EMU((M68K_LOG_FILEHANDLE "%s at %08x: called 1010 instruction %04x (%s)\n",
1726 m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PPC), REG_IR,
1727 m68ki_disassemble_quick(ADDRESS_68K(REG_PPC))));
1728 #endif
1730 sr = m68ki_init_exception();
1731 m68ki_stack_frame_0000(REG_PC-2, sr, EXCEPTION_1010);
1732 m68ki_jump_vector(EXCEPTION_1010);
1734 /* Use up some clock cycles and undo the instruction's cycles */
1735 USE_CYCLES(CYC_EXCEPTION[EXCEPTION_1010] - CYC_INSTRUCTION[REG_IR]);
1736 }
1738 /* Exception for F-Line instructions */
1739 INLINE void m68ki_exception_1111(void)
1740 {
1741 uint sr;
1743 #if M68K_LOG_1010_1111 == OPT_ON
1744 M68K_DO_LOG_EMU((M68K_LOG_FILEHANDLE "%s at %08x: called 1111 instruction %04x (%s)\n",
1745 m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PPC), REG_IR,
1746 m68ki_disassemble_quick(ADDRESS_68K(REG_PPC))));
1747 #endif
1749 sr = m68ki_init_exception();
1750 m68ki_stack_frame_0000(REG_PC-2, sr, EXCEPTION_1111);
1751 m68ki_jump_vector(EXCEPTION_1111);
1753 /* Use up some clock cycles and undo the instruction's cycles */
1754 USE_CYCLES(CYC_EXCEPTION[EXCEPTION_1111] - CYC_INSTRUCTION[REG_IR]);
1755 }
1757 /* Exception for illegal instructions */
1758 INLINE void m68ki_exception_illegal(void)
1759 {
1760 uint sr;
1762 M68K_DO_LOG((M68K_LOG_FILEHANDLE "%s at %08x: illegal instruction %04x (%s)\n",
1763 m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PPC), REG_IR,
1764 m68ki_disassemble_quick(ADDRESS_68K(REG_PPC))));
1766 sr = m68ki_init_exception();
1767 m68ki_stack_frame_0000(REG_PC, sr, EXCEPTION_ILLEGAL_INSTRUCTION);
1768 m68ki_jump_vector(EXCEPTION_ILLEGAL_INSTRUCTION);
1770 /* Use up some clock cycles and undo the instruction's cycles */
1771 USE_CYCLES(CYC_EXCEPTION[EXCEPTION_ILLEGAL_INSTRUCTION] - CYC_INSTRUCTION[REG_IR]);
1772 }
1774 /* Exception for format errror in RTE */
1775 INLINE void m68ki_exception_format_error(void)
1776 {
1777 uint sr = m68ki_init_exception();
1778 m68ki_stack_frame_0000(REG_PC, sr, EXCEPTION_FORMAT_ERROR);
1779 m68ki_jump_vector(EXCEPTION_FORMAT_ERROR);
1781 /* Use up some clock cycles and undo the instruction's cycles */
1782 USE_CYCLES(CYC_EXCEPTION[EXCEPTION_FORMAT_ERROR] - CYC_INSTRUCTION[REG_IR]);
1783 }
1785 /* Exception for address error */
1786 INLINE void m68ki_exception_address_error(void)
1787 {
1788 /* Not emulated yet */
1789 }
1792 /* Service an interrupt request and start exception processing */
1793 void m68ki_exception_interrupt(uint int_level)
1794 {
1795 uint vector;
1796 uint sr;
1797 uint new_pc;
1799 /* Turn off the stopped state */
1800 CPU_STOPPED &= ~STOP_LEVEL_STOP;
1802 /* If we are halted, don't do anything */
1803 if(CPU_STOPPED)
1804 return;
1806 /* Acknowledge the interrupt */
1807 vector = m68ki_int_ack(int_level);
1809 /* Get the interrupt vector */
1810 if(vector == M68K_INT_ACK_AUTOVECTOR)
1811 /* Use the autovectors. This is the most commonly used implementation */
1812 vector = EXCEPTION_INTERRUPT_AUTOVECTOR+int_level;
1813 else if(vector == M68K_INT_ACK_SPURIOUS)
1814 /* Called if no devices respond to the interrupt acknowledge */
1815 vector = EXCEPTION_SPURIOUS_INTERRUPT;
1816 else if(vector > 255)
1817 {
1818 M68K_DO_LOG_EMU((M68K_LOG_FILEHANDLE "%s at %08x: Interrupt acknowledge returned invalid vector $%x\n",
1819 m68ki_cpu_names[CPU_TYPE], ADDRESS_68K(REG_PC), vector));
1820 return;
1821 }
1823 /* Start exception processing */
1824 sr = m68ki_init_exception();
1826 /* Set the interrupt mask to the level of the one being serviced */
1827 FLAG_INT_MASK = int_level<<8;
1829 /* Get the new PC */
1830 new_pc = m68ki_read_data_32((vector<<2) + REG_VBR);
1832 /* If vector is uninitialized, call the uninitialized interrupt vector */
1833 if(new_pc == 0)
1834 new_pc = m68ki_read_data_32((EXCEPTION_UNINITIALIZED_INTERRUPT<<2) + REG_VBR);
1836 /* Generate a stack frame */
1837 m68ki_stack_frame_0000(REG_PC, sr, vector);
1838 if(FLAG_M && CPU_TYPE_IS_EC020_PLUS(CPU_TYPE))
1839 {
1840 /* Create throwaway frame */
1841 m68ki_set_sm_flag(FLAG_S); /* clear M */
1842 sr |= 0x2000; /* Same as SR in master stack frame except S is forced high */
1843 m68ki_stack_frame_0001(REG_PC, sr, vector);
1844 }
1846 m68ki_jump(new_pc);
1848 /* Defer cycle counting until later */
1849 CPU_INT_CYCLES += CYC_EXCEPTION[vector];
1851 #if !M68K_EMULATE_INT_ACK
1852 /* Automatically clear IRQ if we are not using an acknowledge scheme */
1853 CPU_INT_LEVEL = 0;
1854 #endif /* M68K_EMULATE_INT_ACK */
1855 }
1858 /* ASG: Check for interrupts */
1859 INLINE void m68ki_check_interrupts(void)
1860 {
1861 if(CPU_INT_LEVEL > FLAG_INT_MASK)
1862 m68ki_exception_interrupt(CPU_INT_LEVEL>>8);
1863 }
1867 /* ======================================================================== */
1868 /* ============================== END OF FILE ============================= */
1869 /* ======================================================================== */
1871 #endif /* M68KCPU__HEADER */