4 * Copyright (c) 2007 CodeSourcery
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
21 #include "exec/helper-proto.h"
22 #include "exec/exec-all.h"
23 #include "exec/cpu_ldst.h"
24 #include "exec/semihost.h"
26 #if defined(CONFIG_USER_ONLY)
28 void m68k_cpu_do_interrupt(CPUState
*cs
)
30 cs
->exception_index
= -1;
33 static inline void do_interrupt_m68k_hardirq(CPUM68KState
*env
)
39 /* Try to fill the TLB and return an exception if error. If retaddr is
40 NULL, it means that the function was called in C code (i.e. not
41 from generated code or from helper.c) */
42 void tlb_fill(CPUState
*cs
, target_ulong addr
, int size
,
43 MMUAccessType access_type
, int mmu_idx
, uintptr_t retaddr
)
47 ret
= m68k_cpu_handle_mmu_fault(cs
, addr
, size
, access_type
, mmu_idx
);
49 /* now we have a real cpu fault */
50 cpu_loop_exit_restore(cs
, retaddr
);
54 static void cf_rte(CPUM68KState
*env
)
60 fmt
= cpu_ldl_kernel(env
, sp
);
61 env
->pc
= cpu_ldl_kernel(env
, sp
+ 4);
62 sp
|= (fmt
>> 28) & 3;
63 env
->aregs
[7] = sp
+ 8;
65 cpu_m68k_set_sr(env
, fmt
);
68 static void m68k_rte(CPUM68KState
*env
)
76 sr
= cpu_lduw_kernel(env
, sp
);
78 env
->pc
= cpu_ldl_kernel(env
, sp
);
80 if (m68k_feature(env
, M68K_FEATURE_QUAD_MULDIV
)) {
81 /* all except 68000 */
82 fmt
= cpu_lduw_kernel(env
, sp
);
89 cpu_m68k_set_sr(env
, sr
);
104 cpu_m68k_set_sr(env
, sr
);
107 static const char *m68k_exception_name(int index
)
111 return "Access Fault";
113 return "Address Error";
115 return "Illegal Instruction";
117 return "Divide by Zero";
121 return "FTRAPcc, TRAPcc, TRAPV";
123 return "Privilege Violation";
130 case EXCP_DEBEGBP
: /* 68020/030 only */
131 return "Copro Protocol Violation";
133 return "Format Error";
134 case EXCP_UNINITIALIZED
:
135 return "Unitialized Interruot";
137 return "Spurious Interrupt";
138 case EXCP_INT_LEVEL_1
:
139 return "Level 1 Interrupt";
140 case EXCP_INT_LEVEL_1
+ 1:
141 return "Level 2 Interrupt";
142 case EXCP_INT_LEVEL_1
+ 2:
143 return "Level 3 Interrupt";
144 case EXCP_INT_LEVEL_1
+ 3:
145 return "Level 4 Interrupt";
146 case EXCP_INT_LEVEL_1
+ 4:
147 return "Level 5 Interrupt";
148 case EXCP_INT_LEVEL_1
+ 5:
149 return "Level 6 Interrupt";
150 case EXCP_INT_LEVEL_1
+ 6:
151 return "Level 7 Interrupt";
172 case EXCP_TRAP0
+ 10:
174 case EXCP_TRAP0
+ 11:
176 case EXCP_TRAP0
+ 12:
178 case EXCP_TRAP0
+ 13:
180 case EXCP_TRAP0
+ 14:
182 case EXCP_TRAP0
+ 15:
185 return "FP Branch/Set on unordered condition";
187 return "FP Inexact Result";
189 return "FP Divide by Zero";
191 return "FP Underflow";
193 return "FP Operand Error";
195 return "FP Overflow";
197 return "FP Signaling NAN";
199 return "FP Unimplemented Data Type";
200 case EXCP_MMU_CONF
: /* 68030/68851 only */
201 return "MMU Configuration Error";
202 case EXCP_MMU_ILLEGAL
: /* 68851 only */
203 return "MMU Illegal Operation";
204 case EXCP_MMU_ACCESS
: /* 68851 only */
205 return "MMU Access Level Violation";
207 return "User Defined Vector";
212 static void cf_interrupt_all(CPUM68KState
*env
, int is_hw
)
214 CPUState
*cs
= CPU(m68k_env_get_cpu(env
));
225 switch (cs
->exception_index
) {
227 /* Return from an exception. */
231 if (semihosting_enabled()
232 && (env
->sr
& SR_S
) != 0
233 && (env
->pc
& 3) == 0
234 && cpu_lduw_code(env
, env
->pc
- 4) == 0x4e71
235 && cpu_ldl_code(env
, env
->pc
) == 0x4e7bf000) {
237 do_m68k_semihosting(env
, env
->dregs
[0]);
241 cs
->exception_index
= EXCP_HLT
;
245 if (cs
->exception_index
>= EXCP_TRAP0
246 && cs
->exception_index
<= EXCP_TRAP15
) {
247 /* Move the PC after the trap instruction. */
252 vector
= cs
->exception_index
<< 2;
254 sr
= env
->sr
| cpu_m68k_get_ccr(env
);
255 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
257 qemu_log("INT %6d: %s(%#x) pc=%08x sp=%08x sr=%04x\n",
258 ++count
, m68k_exception_name(cs
->exception_index
),
259 vector
, env
->pc
, env
->aregs
[7], sr
);
268 env
->sr
= (env
->sr
& ~SR_I
) | (env
->pending_level
<< SR_I_SHIFT
);
273 fmt
|= (sp
& 3) << 28;
275 /* ??? This could cause MMU faults. */
278 cpu_stl_kernel(env
, sp
, retaddr
);
280 cpu_stl_kernel(env
, sp
, fmt
);
282 /* Jump to vector. */
283 env
->pc
= cpu_ldl_kernel(env
, env
->vbr
+ vector
);
286 static inline void do_stack_frame(CPUM68KState
*env
, uint32_t *sp
,
287 uint16_t format
, uint16_t sr
,
288 uint32_t addr
, uint32_t retaddr
)
290 if (m68k_feature(env
, M68K_FEATURE_QUAD_MULDIV
)) {
291 /* all except 68000 */
292 CPUState
*cs
= CPU(m68k_env_get_cpu(env
));
296 cpu_stl_kernel(env
, *sp
, env
->pc
);
298 cpu_stl_kernel(env
, *sp
, addr
);
303 cpu_stl_kernel(env
, *sp
, addr
);
307 cpu_stw_kernel(env
, *sp
, (format
<< 12) + (cs
->exception_index
<< 2));
310 cpu_stl_kernel(env
, *sp
, retaddr
);
312 cpu_stw_kernel(env
, *sp
, sr
);
315 static void m68k_interrupt_all(CPUM68KState
*env
, int is_hw
)
317 CPUState
*cs
= CPU(m68k_env_get_cpu(env
));
326 switch (cs
->exception_index
) {
328 /* Return from an exception. */
331 case EXCP_TRAP0
... EXCP_TRAP15
:
332 /* Move the PC after the trap instruction. */
338 vector
= cs
->exception_index
<< 2;
340 sr
= env
->sr
| cpu_m68k_get_ccr(env
);
341 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
343 qemu_log("INT %6d: %s(%#x) pc=%08x sp=%08x sr=%04x\n",
344 ++count
, m68k_exception_name(cs
->exception_index
),
345 vector
, env
->pc
, env
->aregs
[7], sr
);
349 * MC68040UM/AD, chapter 9.3.10
352 /* "the processor first make an internal copy" */
354 /* "set the mode to supervisor" */
356 /* "suppress tracing" */
358 /* "sets the processor interrupt mask" */
360 sr
|= (env
->sr
& ~SR_I
) | (env
->pending_level
<< SR_I_SHIFT
);
362 cpu_m68k_set_sr(env
, sr
);
366 if (cs
->exception_index
== EXCP_ACCESS
) {
367 if (env
->mmu
.fault
) {
368 cpu_abort(cs
, "DOUBLE MMU FAULT\n");
370 env
->mmu
.fault
= true;
372 cpu_stl_kernel(env
, sp
, 0); /* push data 3 */
374 cpu_stl_kernel(env
, sp
, 0); /* push data 2 */
376 cpu_stl_kernel(env
, sp
, 0); /* push data 1 */
378 cpu_stl_kernel(env
, sp
, 0); /* write back 1 / push data 0 */
380 cpu_stl_kernel(env
, sp
, 0); /* write back 1 address */
382 cpu_stl_kernel(env
, sp
, 0); /* write back 2 data */
384 cpu_stl_kernel(env
, sp
, 0); /* write back 2 address */
386 cpu_stl_kernel(env
, sp
, 0); /* write back 3 data */
388 cpu_stl_kernel(env
, sp
, env
->mmu
.ar
); /* write back 3 address */
390 cpu_stl_kernel(env
, sp
, env
->mmu
.ar
); /* fault address */
392 cpu_stw_kernel(env
, sp
, 0); /* write back 1 status */
394 cpu_stw_kernel(env
, sp
, 0); /* write back 2 status */
396 cpu_stw_kernel(env
, sp
, 0); /* write back 3 status */
398 cpu_stw_kernel(env
, sp
, env
->mmu
.ssw
); /* special status word */
400 cpu_stl_kernel(env
, sp
, env
->mmu
.ar
); /* effective address */
401 do_stack_frame(env
, &sp
, 7, oldsr
, 0, retaddr
);
402 env
->mmu
.fault
= false;
403 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
405 "ssw: %08x ea: %08x sfc: %d dfc: %d\n",
406 env
->mmu
.ssw
, env
->mmu
.ar
, env
->sfc
, env
->dfc
);
408 } else if (cs
->exception_index
== EXCP_ADDRESS
) {
409 do_stack_frame(env
, &sp
, 2, oldsr
, 0, retaddr
);
410 } else if (cs
->exception_index
== EXCP_ILLEGAL
||
411 cs
->exception_index
== EXCP_DIV0
||
412 cs
->exception_index
== EXCP_CHK
||
413 cs
->exception_index
== EXCP_TRAPCC
||
414 cs
->exception_index
== EXCP_TRACE
) {
415 /* FIXME: addr is not only env->pc */
416 do_stack_frame(env
, &sp
, 2, oldsr
, env
->pc
, retaddr
);
417 } else if (is_hw
&& oldsr
& SR_M
&&
418 cs
->exception_index
>= EXCP_SPURIOUS
&&
419 cs
->exception_index
<= EXCP_INT_LEVEL_7
) {
420 do_stack_frame(env
, &sp
, 0, oldsr
, 0, retaddr
);
423 cpu_m68k_set_sr(env
, sr
&= ~SR_M
);
424 sp
= env
->aregs
[7] & ~1;
425 do_stack_frame(env
, &sp
, 1, oldsr
, 0, retaddr
);
427 do_stack_frame(env
, &sp
, 0, oldsr
, 0, retaddr
);
431 /* Jump to vector. */
432 env
->pc
= cpu_ldl_kernel(env
, env
->vbr
+ vector
);
435 static void do_interrupt_all(CPUM68KState
*env
, int is_hw
)
437 if (m68k_feature(env
, M68K_FEATURE_M68000
)) {
438 m68k_interrupt_all(env
, is_hw
);
441 cf_interrupt_all(env
, is_hw
);
444 void m68k_cpu_do_interrupt(CPUState
*cs
)
446 M68kCPU
*cpu
= M68K_CPU(cs
);
447 CPUM68KState
*env
= &cpu
->env
;
449 do_interrupt_all(env
, 0);
452 static inline void do_interrupt_m68k_hardirq(CPUM68KState
*env
)
454 do_interrupt_all(env
, 1);
457 void m68k_cpu_unassigned_access(CPUState
*cs
, hwaddr addr
, bool is_write
,
458 bool is_exec
, int is_asi
, unsigned size
)
460 M68kCPU
*cpu
= M68K_CPU(cs
);
461 CPUM68KState
*env
= &cpu
->env
;
462 #ifdef DEBUG_UNASSIGNED
463 qemu_log_mask(CPU_LOG_INT
, "Unassigned " TARGET_FMT_plx
" wr=%d exe=%d\n",
464 addr
, is_write
, is_exec
);
467 /* when called from gdb, env is NULL */
471 if (m68k_feature(env
, M68K_FEATURE_M68040
)) {
473 env
->mmu
.ssw
|= M68K_ATC_040
;
474 /* FIXME: manage MMU table access error */
475 env
->mmu
.ssw
&= ~M68K_TM_040
;
476 if (env
->sr
& SR_S
) { /* SUPERVISOR */
477 env
->mmu
.ssw
|= M68K_TM_040_SUPER
;
479 if (is_exec
) { /* instruction or data */
480 env
->mmu
.ssw
|= M68K_TM_040_CODE
;
482 env
->mmu
.ssw
|= M68K_TM_040_DATA
;
484 env
->mmu
.ssw
&= ~M68K_BA_SIZE_MASK
;
487 env
->mmu
.ssw
|= M68K_BA_SIZE_BYTE
;
490 env
->mmu
.ssw
|= M68K_BA_SIZE_WORD
;
493 env
->mmu
.ssw
|= M68K_BA_SIZE_LONG
;
498 env
->mmu
.ssw
|= M68K_RW_040
;
503 cs
->exception_index
= EXCP_ACCESS
;
509 bool m68k_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
511 M68kCPU
*cpu
= M68K_CPU(cs
);
512 CPUM68KState
*env
= &cpu
->env
;
514 if (interrupt_request
& CPU_INTERRUPT_HARD
515 && ((env
->sr
& SR_I
) >> SR_I_SHIFT
) < env
->pending_level
) {
516 /* Real hardware gets the interrupt vector via an IACK cycle
517 at this point. Current emulated hardware doesn't rely on
518 this, so we provide/save the vector when the interrupt is
520 cs
->exception_index
= env
->pending_vector
;
521 do_interrupt_m68k_hardirq(env
);
527 static void raise_exception_ra(CPUM68KState
*env
, int tt
, uintptr_t raddr
)
529 CPUState
*cs
= CPU(m68k_env_get_cpu(env
));
531 cs
->exception_index
= tt
;
532 cpu_loop_exit_restore(cs
, raddr
);
535 static void raise_exception(CPUM68KState
*env
, int tt
)
537 raise_exception_ra(env
, tt
, 0);
540 void HELPER(raise_exception
)(CPUM68KState
*env
, uint32_t tt
)
542 raise_exception(env
, tt
);
545 void HELPER(divuw
)(CPUM68KState
*env
, int destr
, uint32_t den
)
547 uint32_t num
= env
->dregs
[destr
];
551 raise_exception_ra(env
, EXCP_DIV0
, GETPC());
556 env
->cc_c
= 0; /* always cleared, even if overflow */
559 /* real 68040 keeps N and unset Z on overflow,
560 * whereas documentation says "undefined"
565 env
->dregs
[destr
] = deposit32(quot
, 16, 16, rem
);
566 env
->cc_z
= (int16_t)quot
;
567 env
->cc_n
= (int16_t)quot
;
571 void HELPER(divsw
)(CPUM68KState
*env
, int destr
, int32_t den
)
573 int32_t num
= env
->dregs
[destr
];
577 raise_exception_ra(env
, EXCP_DIV0
, GETPC());
582 env
->cc_c
= 0; /* always cleared, even if overflow */
583 if (quot
!= (int16_t)quot
) {
585 /* nothing else is modified */
586 /* real 68040 keeps N and unset Z on overflow,
587 * whereas documentation says "undefined"
592 env
->dregs
[destr
] = deposit32(quot
, 16, 16, rem
);
593 env
->cc_z
= (int16_t)quot
;
594 env
->cc_n
= (int16_t)quot
;
598 void HELPER(divul
)(CPUM68KState
*env
, int numr
, int regr
, uint32_t den
)
600 uint32_t num
= env
->dregs
[numr
];
604 raise_exception_ra(env
, EXCP_DIV0
, GETPC());
614 if (m68k_feature(env
, M68K_FEATURE_CF_ISA_A
)) {
616 env
->dregs
[numr
] = quot
;
618 env
->dregs
[regr
] = rem
;
621 env
->dregs
[regr
] = rem
;
622 env
->dregs
[numr
] = quot
;
626 void HELPER(divsl
)(CPUM68KState
*env
, int numr
, int regr
, int32_t den
)
628 int32_t num
= env
->dregs
[numr
];
632 raise_exception_ra(env
, EXCP_DIV0
, GETPC());
642 if (m68k_feature(env
, M68K_FEATURE_CF_ISA_A
)) {
644 env
->dregs
[numr
] = quot
;
646 env
->dregs
[regr
] = rem
;
649 env
->dregs
[regr
] = rem
;
650 env
->dregs
[numr
] = quot
;
654 void HELPER(divull
)(CPUM68KState
*env
, int numr
, int regr
, uint32_t den
)
656 uint64_t num
= deposit64(env
->dregs
[numr
], 32, 32, env
->dregs
[regr
]);
661 raise_exception_ra(env
, EXCP_DIV0
, GETPC());
666 env
->cc_c
= 0; /* always cleared, even if overflow */
667 if (quot
> 0xffffffffULL
) {
669 /* real 68040 keeps N and unset Z on overflow,
670 * whereas documentation says "undefined"
680 * If Dq and Dr are the same, the quotient is returned.
681 * therefore we set Dq last.
684 env
->dregs
[regr
] = rem
;
685 env
->dregs
[numr
] = quot
;
688 void HELPER(divsll
)(CPUM68KState
*env
, int numr
, int regr
, int32_t den
)
690 int64_t num
= deposit64(env
->dregs
[numr
], 32, 32, env
->dregs
[regr
]);
695 raise_exception_ra(env
, EXCP_DIV0
, GETPC());
700 env
->cc_c
= 0; /* always cleared, even if overflow */
701 if (quot
!= (int32_t)quot
) {
703 /* real 68040 keeps N and unset Z on overflow,
704 * whereas documentation says "undefined"
714 * If Dq and Dr are the same, the quotient is returned.
715 * therefore we set Dq last.
718 env
->dregs
[regr
] = rem
;
719 env
->dregs
[numr
] = quot
;
722 /* We're executing in a serial context -- no need to be atomic. */
723 void HELPER(cas2w
)(CPUM68KState
*env
, uint32_t regs
, uint32_t a1
, uint32_t a2
)
725 uint32_t Dc1
= extract32(regs
, 9, 3);
726 uint32_t Dc2
= extract32(regs
, 6, 3);
727 uint32_t Du1
= extract32(regs
, 3, 3);
728 uint32_t Du2
= extract32(regs
, 0, 3);
729 int16_t c1
= env
->dregs
[Dc1
];
730 int16_t c2
= env
->dregs
[Dc2
];
731 int16_t u1
= env
->dregs
[Du1
];
732 int16_t u2
= env
->dregs
[Du2
];
734 uintptr_t ra
= GETPC();
736 l1
= cpu_lduw_data_ra(env
, a1
, ra
);
737 l2
= cpu_lduw_data_ra(env
, a2
, ra
);
738 if (l1
== c1
&& l2
== c2
) {
739 cpu_stw_data_ra(env
, a1
, u1
, ra
);
740 cpu_stw_data_ra(env
, a2
, u2
, ra
);
750 env
->cc_op
= CC_OP_CMPW
;
751 env
->dregs
[Dc1
] = deposit32(env
->dregs
[Dc1
], 0, 16, l1
);
752 env
->dregs
[Dc2
] = deposit32(env
->dregs
[Dc2
], 0, 16, l2
);
755 static void do_cas2l(CPUM68KState
*env
, uint32_t regs
, uint32_t a1
, uint32_t a2
,
758 uint32_t Dc1
= extract32(regs
, 9, 3);
759 uint32_t Dc2
= extract32(regs
, 6, 3);
760 uint32_t Du1
= extract32(regs
, 3, 3);
761 uint32_t Du2
= extract32(regs
, 0, 3);
762 uint32_t c1
= env
->dregs
[Dc1
];
763 uint32_t c2
= env
->dregs
[Dc2
];
764 uint32_t u1
= env
->dregs
[Du1
];
765 uint32_t u2
= env
->dregs
[Du2
];
767 uintptr_t ra
= GETPC();
768 #if defined(CONFIG_ATOMIC64) && !defined(CONFIG_USER_ONLY)
769 int mmu_idx
= cpu_mmu_index(env
, 0);
774 /* We're executing in a parallel context -- must be atomic. */
775 #ifdef CONFIG_ATOMIC64
777 if ((a1
& 7) == 0 && a2
== a1
+ 4) {
778 c
= deposit64(c2
, 32, 32, c1
);
779 u
= deposit64(u2
, 32, 32, u1
);
780 #ifdef CONFIG_USER_ONLY
781 l
= helper_atomic_cmpxchgq_be(env
, a1
, c
, u
);
783 oi
= make_memop_idx(MO_BEQ
, mmu_idx
);
784 l
= helper_atomic_cmpxchgq_be_mmu(env
, a1
, c
, u
, oi
, ra
);
788 } else if ((a2
& 7) == 0 && a1
== a2
+ 4) {
789 c
= deposit64(c1
, 32, 32, c2
);
790 u
= deposit64(u1
, 32, 32, u2
);
791 #ifdef CONFIG_USER_ONLY
792 l
= helper_atomic_cmpxchgq_be(env
, a2
, c
, u
);
794 oi
= make_memop_idx(MO_BEQ
, mmu_idx
);
795 l
= helper_atomic_cmpxchgq_be_mmu(env
, a2
, c
, u
, oi
, ra
);
802 /* Tell the main loop we need to serialize this insn. */
803 cpu_loop_exit_atomic(ENV_GET_CPU(env
), ra
);
806 /* We're executing in a serial context -- no need to be atomic. */
807 l1
= cpu_ldl_data_ra(env
, a1
, ra
);
808 l2
= cpu_ldl_data_ra(env
, a2
, ra
);
809 if (l1
== c1
&& l2
== c2
) {
810 cpu_stl_data_ra(env
, a1
, u1
, ra
);
811 cpu_stl_data_ra(env
, a2
, u2
, ra
);
822 env
->cc_op
= CC_OP_CMPL
;
823 env
->dregs
[Dc1
] = l1
;
824 env
->dregs
[Dc2
] = l2
;
827 void HELPER(cas2l
)(CPUM68KState
*env
, uint32_t regs
, uint32_t a1
, uint32_t a2
)
829 do_cas2l(env
, regs
, a1
, a2
, false);
832 void HELPER(cas2l_parallel
)(CPUM68KState
*env
, uint32_t regs
, uint32_t a1
,
835 do_cas2l(env
, regs
, a1
, a2
, true);
845 static struct bf_data
bf_prep(uint32_t addr
, int32_t ofs
, uint32_t len
)
849 /* Bound length; map 0 to 32. */
850 len
= ((len
- 1) & 31) + 1;
852 /* Note that ofs is signed. */
860 /* Compute the number of bytes required (minus one) to
861 satisfy the bitfield. */
862 blen
= (bofs
+ len
- 1) / 8;
864 /* Canonicalize the bit offset for data loaded into a 64-bit big-endian
865 word. For the cases where BLEN is not a power of 2, adjust ADDR so
866 that we can use the next power of two sized load without crossing a
867 page boundary, unless the field itself crosses the boundary. */
886 bofs
+= 8 * (addr
& 3);
891 g_assert_not_reached();
894 return (struct bf_data
){
902 static uint64_t bf_load(CPUM68KState
*env
, uint32_t addr
, int blen
,
907 return cpu_ldub_data_ra(env
, addr
, ra
);
909 return cpu_lduw_data_ra(env
, addr
, ra
);
912 return cpu_ldl_data_ra(env
, addr
, ra
);
914 return cpu_ldq_data_ra(env
, addr
, ra
);
916 g_assert_not_reached();
920 static void bf_store(CPUM68KState
*env
, uint32_t addr
, int blen
,
921 uint64_t data
, uintptr_t ra
)
925 cpu_stb_data_ra(env
, addr
, data
, ra
);
928 cpu_stw_data_ra(env
, addr
, data
, ra
);
932 cpu_stl_data_ra(env
, addr
, data
, ra
);
935 cpu_stq_data_ra(env
, addr
, data
, ra
);
938 g_assert_not_reached();
942 uint32_t HELPER(bfexts_mem
)(CPUM68KState
*env
, uint32_t addr
,
943 int32_t ofs
, uint32_t len
)
945 uintptr_t ra
= GETPC();
946 struct bf_data d
= bf_prep(addr
, ofs
, len
);
947 uint64_t data
= bf_load(env
, d
.addr
, d
.blen
, ra
);
949 return (int64_t)(data
<< d
.bofs
) >> (64 - d
.len
);
952 uint64_t HELPER(bfextu_mem
)(CPUM68KState
*env
, uint32_t addr
,
953 int32_t ofs
, uint32_t len
)
955 uintptr_t ra
= GETPC();
956 struct bf_data d
= bf_prep(addr
, ofs
, len
);
957 uint64_t data
= bf_load(env
, d
.addr
, d
.blen
, ra
);
959 /* Put CC_N at the top of the high word; put the zero-extended value
960 at the bottom of the low word. */
963 data
|= data
<< (64 - d
.len
);
968 uint32_t HELPER(bfins_mem
)(CPUM68KState
*env
, uint32_t addr
, uint32_t val
,
969 int32_t ofs
, uint32_t len
)
971 uintptr_t ra
= GETPC();
972 struct bf_data d
= bf_prep(addr
, ofs
, len
);
973 uint64_t data
= bf_load(env
, d
.addr
, d
.blen
, ra
);
974 uint64_t mask
= -1ull << (64 - d
.len
) >> d
.bofs
;
976 data
= (data
& ~mask
) | (((uint64_t)val
<< (64 - d
.len
)) >> d
.bofs
);
978 bf_store(env
, d
.addr
, d
.blen
, data
, ra
);
980 /* The field at the top of the word is also CC_N for CC_OP_LOGIC. */
981 return val
<< (32 - d
.len
);
984 uint32_t HELPER(bfchg_mem
)(CPUM68KState
*env
, uint32_t addr
,
985 int32_t ofs
, uint32_t len
)
987 uintptr_t ra
= GETPC();
988 struct bf_data d
= bf_prep(addr
, ofs
, len
);
989 uint64_t data
= bf_load(env
, d
.addr
, d
.blen
, ra
);
990 uint64_t mask
= -1ull << (64 - d
.len
) >> d
.bofs
;
992 bf_store(env
, d
.addr
, d
.blen
, data
^ mask
, ra
);
994 return ((data
& mask
) << d
.bofs
) >> 32;
997 uint32_t HELPER(bfclr_mem
)(CPUM68KState
*env
, uint32_t addr
,
998 int32_t ofs
, uint32_t len
)
1000 uintptr_t ra
= GETPC();
1001 struct bf_data d
= bf_prep(addr
, ofs
, len
);
1002 uint64_t data
= bf_load(env
, d
.addr
, d
.blen
, ra
);
1003 uint64_t mask
= -1ull << (64 - d
.len
) >> d
.bofs
;
1005 bf_store(env
, d
.addr
, d
.blen
, data
& ~mask
, ra
);
1007 return ((data
& mask
) << d
.bofs
) >> 32;
1010 uint32_t HELPER(bfset_mem
)(CPUM68KState
*env
, uint32_t addr
,
1011 int32_t ofs
, uint32_t len
)
1013 uintptr_t ra
= GETPC();
1014 struct bf_data d
= bf_prep(addr
, ofs
, len
);
1015 uint64_t data
= bf_load(env
, d
.addr
, d
.blen
, ra
);
1016 uint64_t mask
= -1ull << (64 - d
.len
) >> d
.bofs
;
1018 bf_store(env
, d
.addr
, d
.blen
, data
| mask
, ra
);
1020 return ((data
& mask
) << d
.bofs
) >> 32;
1023 uint32_t HELPER(bfffo_reg
)(uint32_t n
, uint32_t ofs
, uint32_t len
)
1025 return (n
? clz32(n
) : len
) + ofs
;
1028 uint64_t HELPER(bfffo_mem
)(CPUM68KState
*env
, uint32_t addr
,
1029 int32_t ofs
, uint32_t len
)
1031 uintptr_t ra
= GETPC();
1032 struct bf_data d
= bf_prep(addr
, ofs
, len
);
1033 uint64_t data
= bf_load(env
, d
.addr
, d
.blen
, ra
);
1034 uint64_t mask
= -1ull << (64 - d
.len
) >> d
.bofs
;
1035 uint64_t n
= (data
& mask
) << d
.bofs
;
1036 uint32_t ffo
= helper_bfffo_reg(n
>> 32, ofs
, d
.len
);
1038 /* Return FFO in the low word and N in the high word.
1039 Note that because of MASK and the shift, the low word
1044 void HELPER(chk
)(CPUM68KState
*env
, int32_t val
, int32_t ub
)
1047 * X: Not affected, C,V,Z: Undefined,
1048 * N: Set if val < 0; cleared if val > ub, undefined otherwise
1049 * We implement here values found from a real MC68040:
1050 * X,V,Z: Not affected
1051 * N: Set if val < 0; cleared if val >= 0
1052 * C: if 0 <= ub: set if val < 0 or val > ub, cleared otherwise
1053 * if 0 > ub: set if val > ub and val < 0, cleared otherwise
1056 env
->cc_c
= 0 <= ub
? val
< 0 || val
> ub
: val
> ub
&& val
< 0;
1058 if (val
< 0 || val
> ub
) {
1059 CPUState
*cs
= CPU(m68k_env_get_cpu(env
));
1061 /* Recover PC and CC_OP for the beginning of the insn. */
1062 cpu_restore_state(cs
, GETPC(), true);
1064 /* flags have been modified by gen_flush_flags() */
1065 env
->cc_op
= CC_OP_FLAGS
;
1066 /* Adjust PC to end of the insn. */
1069 cs
->exception_index
= EXCP_CHK
;
1074 void HELPER(chk2
)(CPUM68KState
*env
, int32_t val
, int32_t lb
, int32_t ub
)
1077 * X: Not affected, N,V: Undefined,
1078 * Z: Set if val is equal to lb or ub
1079 * C: Set if val < lb or val > ub, cleared otherwise
1080 * We implement here values found from a real MC68040:
1081 * X,N,V: Not affected
1082 * Z: Set if val is equal to lb or ub
1083 * C: if lb <= ub: set if val < lb or val > ub, cleared otherwise
1084 * if lb > ub: set if val > ub and val < lb, cleared otherwise
1086 env
->cc_z
= val
!= lb
&& val
!= ub
;
1087 env
->cc_c
= lb
<= ub
? val
< lb
|| val
> ub
: val
> ub
&& val
< lb
;
1090 CPUState
*cs
= CPU(m68k_env_get_cpu(env
));
1092 /* Recover PC and CC_OP for the beginning of the insn. */
1093 cpu_restore_state(cs
, GETPC(), true);
1095 /* flags have been modified by gen_flush_flags() */
1096 env
->cc_op
= CC_OP_FLAGS
;
1097 /* Adjust PC to end of the insn. */
1100 cs
->exception_index
= EXCP_CHK
;