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.1 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"
22 #include "exec/helper-proto.h"
23 #include "exec/exec-all.h"
24 #include "exec/cpu_ldst.h"
25 #include "semihosting/semihost.h"
27 #if !defined(CONFIG_USER_ONLY)
29 static void cf_rte(CPUM68KState
*env
)
35 fmt
= cpu_ldl_mmuidx_ra(env
, sp
, MMU_KERNEL_IDX
, 0);
36 env
->pc
= cpu_ldl_mmuidx_ra(env
, sp
+ 4, MMU_KERNEL_IDX
, 0);
37 sp
|= (fmt
>> 28) & 3;
38 env
->aregs
[7] = sp
+ 8;
40 cpu_m68k_set_sr(env
, fmt
);
43 static void m68k_rte(CPUM68KState
*env
)
51 sr
= cpu_lduw_mmuidx_ra(env
, sp
, MMU_KERNEL_IDX
, 0);
53 env
->pc
= cpu_ldl_mmuidx_ra(env
, sp
, MMU_KERNEL_IDX
, 0);
55 if (m68k_feature(env
, M68K_FEATURE_EXCEPTION_FORMAT_VEC
)) {
56 /* all except 68000 */
57 fmt
= cpu_lduw_mmuidx_ra(env
, sp
, MMU_KERNEL_IDX
, 0);
64 cpu_m68k_set_sr(env
, sr
);
79 cpu_m68k_set_sr(env
, sr
);
82 static const char *m68k_exception_name(int index
)
86 return "Access Fault";
88 return "Address Error";
90 return "Illegal Instruction";
92 return "Divide by Zero";
96 return "FTRAPcc, TRAPcc, TRAPV";
98 return "Privilege Violation";
105 case EXCP_DEBEGBP
: /* 68020/030 only */
106 return "Copro Protocol Violation";
108 return "Format Error";
109 case EXCP_UNINITIALIZED
:
110 return "Uninitialized Interrupt";
112 return "Spurious Interrupt";
113 case EXCP_INT_LEVEL_1
:
114 return "Level 1 Interrupt";
115 case EXCP_INT_LEVEL_1
+ 1:
116 return "Level 2 Interrupt";
117 case EXCP_INT_LEVEL_1
+ 2:
118 return "Level 3 Interrupt";
119 case EXCP_INT_LEVEL_1
+ 3:
120 return "Level 4 Interrupt";
121 case EXCP_INT_LEVEL_1
+ 4:
122 return "Level 5 Interrupt";
123 case EXCP_INT_LEVEL_1
+ 5:
124 return "Level 6 Interrupt";
125 case EXCP_INT_LEVEL_1
+ 6:
126 return "Level 7 Interrupt";
147 case EXCP_TRAP0
+ 10:
149 case EXCP_TRAP0
+ 11:
151 case EXCP_TRAP0
+ 12:
153 case EXCP_TRAP0
+ 13:
155 case EXCP_TRAP0
+ 14:
157 case EXCP_TRAP0
+ 15:
160 return "FP Branch/Set on unordered condition";
162 return "FP Inexact Result";
164 return "FP Divide by Zero";
166 return "FP Underflow";
168 return "FP Operand Error";
170 return "FP Overflow";
172 return "FP Signaling NAN";
174 return "FP Unimplemented Data Type";
175 case EXCP_MMU_CONF
: /* 68030/68851 only */
176 return "MMU Configuration Error";
177 case EXCP_MMU_ILLEGAL
: /* 68851 only */
178 return "MMU Illegal Operation";
179 case EXCP_MMU_ACCESS
: /* 68851 only */
180 return "MMU Access Level Violation";
182 return "User Defined Vector";
187 static void cf_interrupt_all(CPUM68KState
*env
, int is_hw
)
189 CPUState
*cs
= env_cpu(env
);
200 switch (cs
->exception_index
) {
202 /* Return from an exception. */
205 case EXCP_SEMIHOSTING
:
206 do_m68k_semihosting(env
, env
->dregs
[0]);
211 vector
= cs
->exception_index
<< 2;
213 sr
= env
->sr
| cpu_m68k_get_ccr(env
);
214 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
216 qemu_log("INT %6d: %s(%#x) pc=%08x sp=%08x sr=%04x\n",
217 ++count
, m68k_exception_name(cs
->exception_index
),
218 vector
, env
->pc
, env
->aregs
[7], sr
);
227 env
->sr
= (env
->sr
& ~SR_I
) | (env
->pending_level
<< SR_I_SHIFT
);
232 fmt
|= (sp
& 3) << 28;
234 /* ??? This could cause MMU faults. */
237 cpu_stl_mmuidx_ra(env
, sp
, retaddr
, MMU_KERNEL_IDX
, 0);
239 cpu_stl_mmuidx_ra(env
, sp
, fmt
, MMU_KERNEL_IDX
, 0);
241 /* Jump to vector. */
242 env
->pc
= cpu_ldl_mmuidx_ra(env
, env
->vbr
+ vector
, MMU_KERNEL_IDX
, 0);
245 static inline void do_stack_frame(CPUM68KState
*env
, uint32_t *sp
,
246 uint16_t format
, uint16_t sr
,
247 uint32_t addr
, uint32_t retaddr
)
249 if (m68k_feature(env
, M68K_FEATURE_EXCEPTION_FORMAT_VEC
)) {
250 /* all except 68000 */
251 CPUState
*cs
= env_cpu(env
);
255 cpu_stl_mmuidx_ra(env
, *sp
, env
->pc
, MMU_KERNEL_IDX
, 0);
257 cpu_stl_mmuidx_ra(env
, *sp
, addr
, MMU_KERNEL_IDX
, 0);
262 cpu_stl_mmuidx_ra(env
, *sp
, addr
, MMU_KERNEL_IDX
, 0);
266 cpu_stw_mmuidx_ra(env
, *sp
, (format
<< 12) + (cs
->exception_index
<< 2),
270 cpu_stl_mmuidx_ra(env
, *sp
, retaddr
, MMU_KERNEL_IDX
, 0);
272 cpu_stw_mmuidx_ra(env
, *sp
, sr
, MMU_KERNEL_IDX
, 0);
275 static void m68k_interrupt_all(CPUM68KState
*env
, int is_hw
)
277 CPUState
*cs
= env_cpu(env
);
283 switch (cs
->exception_index
) {
285 /* Return from an exception. */
291 vector
= cs
->exception_index
<< 2;
293 sr
= env
->sr
| cpu_m68k_get_ccr(env
);
294 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
296 qemu_log("INT %6d: %s(%#x) pc=%08x sp=%08x sr=%04x\n",
297 ++count
, m68k_exception_name(cs
->exception_index
),
298 vector
, env
->pc
, env
->aregs
[7], sr
);
302 * MC68040UM/AD, chapter 9.3.10
305 /* "the processor first make an internal copy" */
307 /* "set the mode to supervisor" */
309 /* "suppress tracing" */
311 /* "sets the processor interrupt mask" */
313 sr
|= (env
->sr
& ~SR_I
) | (env
->pending_level
<< SR_I_SHIFT
);
315 cpu_m68k_set_sr(env
, sr
);
318 if (!m68k_feature(env
, M68K_FEATURE_UNALIGNED_DATA
)) {
322 switch (cs
->exception_index
) {
324 if (env
->mmu
.fault
) {
325 cpu_abort(cs
, "DOUBLE MMU FAULT\n");
327 env
->mmu
.fault
= true;
330 cpu_stl_mmuidx_ra(env
, sp
, 0, MMU_KERNEL_IDX
, 0);
333 cpu_stl_mmuidx_ra(env
, sp
, 0, MMU_KERNEL_IDX
, 0);
336 cpu_stl_mmuidx_ra(env
, sp
, 0, MMU_KERNEL_IDX
, 0);
337 /* write back 1 / push data 0 */
339 cpu_stl_mmuidx_ra(env
, sp
, 0, MMU_KERNEL_IDX
, 0);
340 /* write back 1 address */
342 cpu_stl_mmuidx_ra(env
, sp
, 0, MMU_KERNEL_IDX
, 0);
343 /* write back 2 data */
345 cpu_stl_mmuidx_ra(env
, sp
, 0, MMU_KERNEL_IDX
, 0);
346 /* write back 2 address */
348 cpu_stl_mmuidx_ra(env
, sp
, 0, MMU_KERNEL_IDX
, 0);
349 /* write back 3 data */
351 cpu_stl_mmuidx_ra(env
, sp
, 0, MMU_KERNEL_IDX
, 0);
352 /* write back 3 address */
354 cpu_stl_mmuidx_ra(env
, sp
, env
->mmu
.ar
, MMU_KERNEL_IDX
, 0);
357 cpu_stl_mmuidx_ra(env
, sp
, env
->mmu
.ar
, MMU_KERNEL_IDX
, 0);
358 /* write back 1 status */
360 cpu_stw_mmuidx_ra(env
, sp
, 0, MMU_KERNEL_IDX
, 0);
361 /* write back 2 status */
363 cpu_stw_mmuidx_ra(env
, sp
, 0, MMU_KERNEL_IDX
, 0);
364 /* write back 3 status */
366 cpu_stw_mmuidx_ra(env
, sp
, 0, MMU_KERNEL_IDX
, 0);
367 /* special status word */
369 cpu_stw_mmuidx_ra(env
, sp
, env
->mmu
.ssw
, MMU_KERNEL_IDX
, 0);
370 /* effective address */
372 cpu_stl_mmuidx_ra(env
, sp
, env
->mmu
.ar
, MMU_KERNEL_IDX
, 0);
374 do_stack_frame(env
, &sp
, 7, oldsr
, 0, env
->pc
);
375 env
->mmu
.fault
= false;
376 if (qemu_loglevel_mask(CPU_LOG_INT
)) {
378 "ssw: %08x ea: %08x sfc: %d dfc: %d\n",
379 env
->mmu
.ssw
, env
->mmu
.ar
, env
->sfc
, env
->dfc
);
384 do_stack_frame(env
, &sp
, 0, oldsr
, 0, env
->pc
);
388 do_stack_frame(env
, &sp
, 2, oldsr
, 0, env
->pc
);
395 do_stack_frame(env
, &sp
, 2, oldsr
, env
->mmu
.ar
, env
->pc
);
398 case EXCP_SPURIOUS
... EXCP_INT_LEVEL_7
:
399 if (is_hw
&& (oldsr
& SR_M
)) {
400 do_stack_frame(env
, &sp
, 0, oldsr
, 0, env
->pc
);
403 cpu_m68k_set_sr(env
, sr
& ~SR_M
);
405 if (!m68k_feature(env
, M68K_FEATURE_UNALIGNED_DATA
)) {
408 do_stack_frame(env
, &sp
, 1, oldsr
, 0, env
->pc
);
414 do_stack_frame(env
, &sp
, 0, oldsr
, 0, env
->pc
);
419 /* Jump to vector. */
420 env
->pc
= cpu_ldl_mmuidx_ra(env
, env
->vbr
+ vector
, MMU_KERNEL_IDX
, 0);
423 static void do_interrupt_all(CPUM68KState
*env
, int is_hw
)
425 if (m68k_feature(env
, M68K_FEATURE_M68K
)) {
426 m68k_interrupt_all(env
, is_hw
);
429 cf_interrupt_all(env
, is_hw
);
432 void m68k_cpu_do_interrupt(CPUState
*cs
)
434 do_interrupt_all(cpu_env(cs
), 0);
437 static inline void do_interrupt_m68k_hardirq(CPUM68KState
*env
)
439 do_interrupt_all(env
, 1);
442 void m68k_cpu_transaction_failed(CPUState
*cs
, hwaddr physaddr
, vaddr addr
,
443 unsigned size
, MMUAccessType access_type
,
444 int mmu_idx
, MemTxAttrs attrs
,
445 MemTxResult response
, uintptr_t retaddr
)
447 CPUM68KState
*env
= cpu_env(cs
);
449 cpu_restore_state(cs
, retaddr
);
451 if (m68k_feature(env
, M68K_FEATURE_M68040
)) {
455 * According to the MC68040 users manual the ATC bit of the SSW is
456 * used to distinguish between ATC faults and physical bus errors.
457 * In the case of a bus error e.g. during nubus read from an empty
458 * slot this bit should not be set
460 if (response
!= MEMTX_DECODE_ERROR
) {
461 env
->mmu
.ssw
|= M68K_ATC_040
;
464 /* FIXME: manage MMU table access error */
465 env
->mmu
.ssw
&= ~M68K_TM_040
;
466 if (env
->sr
& SR_S
) { /* SUPERVISOR */
467 env
->mmu
.ssw
|= M68K_TM_040_SUPER
;
469 if (access_type
== MMU_INST_FETCH
) { /* instruction or data */
470 env
->mmu
.ssw
|= M68K_TM_040_CODE
;
472 env
->mmu
.ssw
|= M68K_TM_040_DATA
;
474 env
->mmu
.ssw
&= ~M68K_BA_SIZE_MASK
;
477 env
->mmu
.ssw
|= M68K_BA_SIZE_BYTE
;
480 env
->mmu
.ssw
|= M68K_BA_SIZE_WORD
;
483 env
->mmu
.ssw
|= M68K_BA_SIZE_LONG
;
487 if (access_type
!= MMU_DATA_STORE
) {
488 env
->mmu
.ssw
|= M68K_RW_040
;
493 cs
->exception_index
= EXCP_ACCESS
;
498 bool m68k_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
500 CPUM68KState
*env
= cpu_env(cs
);
502 if (interrupt_request
& CPU_INTERRUPT_HARD
503 && ((env
->sr
& SR_I
) >> SR_I_SHIFT
) < env
->pending_level
) {
505 * Real hardware gets the interrupt vector via an IACK cycle
506 * at this point. Current emulated hardware doesn't rely on
507 * this, so we provide/save the vector when the interrupt is
510 cs
->exception_index
= env
->pending_vector
;
511 do_interrupt_m68k_hardirq(env
);
517 #endif /* !CONFIG_USER_ONLY */
519 G_NORETURN
static void
520 raise_exception_ra(CPUM68KState
*env
, int tt
, uintptr_t raddr
)
522 CPUState
*cs
= env_cpu(env
);
524 cs
->exception_index
= tt
;
525 cpu_loop_exit_restore(cs
, raddr
);
528 G_NORETURN
static void raise_exception(CPUM68KState
*env
, int tt
)
530 raise_exception_ra(env
, tt
, 0);
533 void HELPER(raise_exception
)(CPUM68KState
*env
, uint32_t tt
)
535 raise_exception(env
, tt
);
538 G_NORETURN
static void
539 raise_exception_format2(CPUM68KState
*env
, int tt
, int ilen
, uintptr_t raddr
)
541 CPUState
*cs
= env_cpu(env
);
543 cs
->exception_index
= tt
;
545 /* Recover PC and CC_OP for the beginning of the insn. */
546 cpu_restore_state(cs
, raddr
);
548 /* Flags are current in env->cc_*, or are undefined. */
549 env
->cc_op
= CC_OP_FLAGS
;
552 * Remember original pc in mmu.ar, for the Format 2 stack frame.
553 * Adjust PC to end of the insn.
555 env
->mmu
.ar
= env
->pc
;
561 void HELPER(divuw
)(CPUM68KState
*env
, int destr
, uint32_t den
, int ilen
)
563 uint32_t num
= env
->dregs
[destr
];
566 env
->cc_c
= 0; /* always cleared, even if div0 */
569 raise_exception_format2(env
, EXCP_DIV0
, ilen
, GETPC());
577 * real 68040 keeps N and unset Z on overflow,
578 * whereas documentation says "undefined"
583 env
->dregs
[destr
] = deposit32(quot
, 16, 16, rem
);
584 env
->cc_z
= (int16_t)quot
;
585 env
->cc_n
= (int16_t)quot
;
589 void HELPER(divsw
)(CPUM68KState
*env
, int destr
, int32_t den
, int ilen
)
591 int32_t num
= env
->dregs
[destr
];
594 env
->cc_c
= 0; /* always cleared, even if overflow/div0 */
597 raise_exception_format2(env
, EXCP_DIV0
, ilen
, GETPC());
602 if (quot
!= (int16_t)quot
) {
604 /* nothing else is modified */
606 * real 68040 keeps N and unset Z on overflow,
607 * whereas documentation says "undefined"
612 env
->dregs
[destr
] = deposit32(quot
, 16, 16, rem
);
613 env
->cc_z
= (int16_t)quot
;
614 env
->cc_n
= (int16_t)quot
;
618 void HELPER(divul
)(CPUM68KState
*env
, int numr
, int regr
,
619 uint32_t den
, int ilen
)
621 uint32_t num
= env
->dregs
[numr
];
624 env
->cc_c
= 0; /* always cleared, even if div0 */
627 raise_exception_format2(env
, EXCP_DIV0
, ilen
, GETPC());
636 if (m68k_feature(env
, M68K_FEATURE_CF_ISA_A
)) {
638 env
->dregs
[numr
] = quot
;
640 env
->dregs
[regr
] = rem
;
643 env
->dregs
[regr
] = rem
;
644 env
->dregs
[numr
] = quot
;
648 void HELPER(divsl
)(CPUM68KState
*env
, int numr
, int regr
,
649 int32_t den
, int ilen
)
651 int32_t num
= env
->dregs
[numr
];
654 env
->cc_c
= 0; /* always cleared, even if overflow/div0 */
657 raise_exception_format2(env
, EXCP_DIV0
, ilen
, GETPC());
666 if (m68k_feature(env
, M68K_FEATURE_CF_ISA_A
)) {
668 env
->dregs
[numr
] = quot
;
670 env
->dregs
[regr
] = rem
;
673 env
->dregs
[regr
] = rem
;
674 env
->dregs
[numr
] = quot
;
678 void HELPER(divull
)(CPUM68KState
*env
, int numr
, int regr
,
679 uint32_t den
, int ilen
)
681 uint64_t num
= deposit64(env
->dregs
[numr
], 32, 32, env
->dregs
[regr
]);
685 env
->cc_c
= 0; /* always cleared, even if overflow/div0 */
688 raise_exception_format2(env
, EXCP_DIV0
, ilen
, GETPC());
693 if (quot
> 0xffffffffULL
) {
696 * real 68040 keeps N and unset Z on overflow,
697 * whereas documentation says "undefined"
707 * If Dq and Dr are the same, the quotient is returned.
708 * therefore we set Dq last.
711 env
->dregs
[regr
] = rem
;
712 env
->dregs
[numr
] = quot
;
715 void HELPER(divsll
)(CPUM68KState
*env
, int numr
, int regr
,
716 int32_t den
, int ilen
)
718 int64_t num
= deposit64(env
->dregs
[numr
], 32, 32, env
->dregs
[regr
]);
722 env
->cc_c
= 0; /* always cleared, even if overflow/div0 */
725 raise_exception_format2(env
, EXCP_DIV0
, ilen
, GETPC());
730 if (quot
!= (int32_t)quot
) {
733 * real 68040 keeps N and unset Z on overflow,
734 * whereas documentation says "undefined"
744 * If Dq and Dr are the same, the quotient is returned.
745 * therefore we set Dq last.
748 env
->dregs
[regr
] = rem
;
749 env
->dregs
[numr
] = quot
;
752 /* We're executing in a serial context -- no need to be atomic. */
753 void HELPER(cas2w
)(CPUM68KState
*env
, uint32_t regs
, uint32_t a1
, uint32_t a2
)
755 uint32_t Dc1
= extract32(regs
, 9, 3);
756 uint32_t Dc2
= extract32(regs
, 6, 3);
757 uint32_t Du1
= extract32(regs
, 3, 3);
758 uint32_t Du2
= extract32(regs
, 0, 3);
759 int16_t c1
= env
->dregs
[Dc1
];
760 int16_t c2
= env
->dregs
[Dc2
];
761 int16_t u1
= env
->dregs
[Du1
];
762 int16_t u2
= env
->dregs
[Du2
];
764 uintptr_t ra
= GETPC();
766 l1
= cpu_lduw_data_ra(env
, a1
, ra
);
767 l2
= cpu_lduw_data_ra(env
, a2
, ra
);
768 if (l1
== c1
&& l2
== c2
) {
769 cpu_stw_data_ra(env
, a1
, u1
, ra
);
770 cpu_stw_data_ra(env
, a2
, u2
, ra
);
780 env
->cc_op
= CC_OP_CMPW
;
781 env
->dregs
[Dc1
] = deposit32(env
->dregs
[Dc1
], 0, 16, l1
);
782 env
->dregs
[Dc2
] = deposit32(env
->dregs
[Dc2
], 0, 16, l2
);
785 static void do_cas2l(CPUM68KState
*env
, uint32_t regs
, uint32_t a1
, uint32_t a2
,
788 uint32_t Dc1
= extract32(regs
, 9, 3);
789 uint32_t Dc2
= extract32(regs
, 6, 3);
790 uint32_t Du1
= extract32(regs
, 3, 3);
791 uint32_t Du2
= extract32(regs
, 0, 3);
792 uint32_t c1
= env
->dregs
[Dc1
];
793 uint32_t c2
= env
->dregs
[Dc2
];
794 uint32_t u1
= env
->dregs
[Du1
];
795 uint32_t u2
= env
->dregs
[Du2
];
797 uintptr_t ra
= GETPC();
798 #if defined(CONFIG_ATOMIC64)
799 int mmu_idx
= cpu_mmu_index(env_cpu(env
), 0);
800 MemOpIdx oi
= make_memop_idx(MO_BEUQ
, mmu_idx
);
804 /* We're executing in a parallel context -- must be atomic. */
805 #ifdef CONFIG_ATOMIC64
807 if ((a1
& 7) == 0 && a2
== a1
+ 4) {
808 c
= deposit64(c2
, 32, 32, c1
);
809 u
= deposit64(u2
, 32, 32, u1
);
810 l
= cpu_atomic_cmpxchgq_be_mmu(env
, a1
, c
, u
, oi
, ra
);
813 } else if ((a2
& 7) == 0 && a1
== a2
+ 4) {
814 c
= deposit64(c1
, 32, 32, c2
);
815 u
= deposit64(u1
, 32, 32, u2
);
816 l
= cpu_atomic_cmpxchgq_be_mmu(env
, a2
, c
, u
, oi
, ra
);
822 /* Tell the main loop we need to serialize this insn. */
823 cpu_loop_exit_atomic(env_cpu(env
), ra
);
826 /* We're executing in a serial context -- no need to be atomic. */
827 l1
= cpu_ldl_data_ra(env
, a1
, ra
);
828 l2
= cpu_ldl_data_ra(env
, a2
, ra
);
829 if (l1
== c1
&& l2
== c2
) {
830 cpu_stl_data_ra(env
, a1
, u1
, ra
);
831 cpu_stl_data_ra(env
, a2
, u2
, ra
);
842 env
->cc_op
= CC_OP_CMPL
;
843 env
->dregs
[Dc1
] = l1
;
844 env
->dregs
[Dc2
] = l2
;
847 void HELPER(cas2l
)(CPUM68KState
*env
, uint32_t regs
, uint32_t a1
, uint32_t a2
)
849 do_cas2l(env
, regs
, a1
, a2
, false);
852 void HELPER(cas2l_parallel
)(CPUM68KState
*env
, uint32_t regs
, uint32_t a1
,
855 do_cas2l(env
, regs
, a1
, a2
, true);
865 static struct bf_data
bf_prep(uint32_t addr
, int32_t ofs
, uint32_t len
)
869 /* Bound length; map 0 to 32. */
870 len
= ((len
- 1) & 31) + 1;
872 /* Note that ofs is signed. */
881 * Compute the number of bytes required (minus one) to
882 * satisfy the bitfield.
884 blen
= (bofs
+ len
- 1) / 8;
887 * Canonicalize the bit offset for data loaded into a 64-bit big-endian
888 * word. For the cases where BLEN is not a power of 2, adjust ADDR so
889 * that we can use the next power of two sized load without crossing a
890 * page boundary, unless the field itself crosses the boundary.
910 bofs
+= 8 * (addr
& 3);
915 g_assert_not_reached();
918 return (struct bf_data
){
926 static uint64_t bf_load(CPUM68KState
*env
, uint32_t addr
, int blen
,
931 return cpu_ldub_data_ra(env
, addr
, ra
);
933 return cpu_lduw_data_ra(env
, addr
, ra
);
936 return cpu_ldl_data_ra(env
, addr
, ra
);
938 return cpu_ldq_data_ra(env
, addr
, ra
);
940 g_assert_not_reached();
944 static void bf_store(CPUM68KState
*env
, uint32_t addr
, int blen
,
945 uint64_t data
, uintptr_t ra
)
949 cpu_stb_data_ra(env
, addr
, data
, ra
);
952 cpu_stw_data_ra(env
, addr
, data
, ra
);
956 cpu_stl_data_ra(env
, addr
, data
, ra
);
959 cpu_stq_data_ra(env
, addr
, data
, ra
);
962 g_assert_not_reached();
966 uint32_t HELPER(bfexts_mem
)(CPUM68KState
*env
, uint32_t addr
,
967 int32_t ofs
, uint32_t len
)
969 uintptr_t ra
= GETPC();
970 struct bf_data d
= bf_prep(addr
, ofs
, len
);
971 uint64_t data
= bf_load(env
, d
.addr
, d
.blen
, ra
);
973 return (int64_t)(data
<< d
.bofs
) >> (64 - d
.len
);
976 uint64_t HELPER(bfextu_mem
)(CPUM68KState
*env
, uint32_t addr
,
977 int32_t ofs
, uint32_t len
)
979 uintptr_t ra
= GETPC();
980 struct bf_data d
= bf_prep(addr
, ofs
, len
);
981 uint64_t data
= bf_load(env
, d
.addr
, d
.blen
, ra
);
984 * Put CC_N at the top of the high word; put the zero-extended value
985 * at the bottom of the low word.
989 data
|= data
<< (64 - d
.len
);
994 uint32_t HELPER(bfins_mem
)(CPUM68KState
*env
, uint32_t addr
, uint32_t val
,
995 int32_t ofs
, uint32_t len
)
997 uintptr_t ra
= GETPC();
998 struct bf_data d
= bf_prep(addr
, ofs
, len
);
999 uint64_t data
= bf_load(env
, d
.addr
, d
.blen
, ra
);
1000 uint64_t mask
= -1ull << (64 - d
.len
) >> d
.bofs
;
1002 data
= (data
& ~mask
) | (((uint64_t)val
<< (64 - d
.len
)) >> d
.bofs
);
1004 bf_store(env
, d
.addr
, d
.blen
, data
, ra
);
1006 /* The field at the top of the word is also CC_N for CC_OP_LOGIC. */
1007 return val
<< (32 - d
.len
);
1010 uint32_t HELPER(bfchg_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(bfclr_mem
)(CPUM68KState
*env
, uint32_t addr
,
1024 int32_t ofs
, uint32_t len
)
1026 uintptr_t ra
= GETPC();
1027 struct bf_data d
= bf_prep(addr
, ofs
, len
);
1028 uint64_t data
= bf_load(env
, d
.addr
, d
.blen
, ra
);
1029 uint64_t mask
= -1ull << (64 - d
.len
) >> d
.bofs
;
1031 bf_store(env
, d
.addr
, d
.blen
, data
& ~mask
, ra
);
1033 return ((data
& mask
) << d
.bofs
) >> 32;
1036 uint32_t HELPER(bfset_mem
)(CPUM68KState
*env
, uint32_t addr
,
1037 int32_t ofs
, uint32_t len
)
1039 uintptr_t ra
= GETPC();
1040 struct bf_data d
= bf_prep(addr
, ofs
, len
);
1041 uint64_t data
= bf_load(env
, d
.addr
, d
.blen
, ra
);
1042 uint64_t mask
= -1ull << (64 - d
.len
) >> d
.bofs
;
1044 bf_store(env
, d
.addr
, d
.blen
, data
| mask
, ra
);
1046 return ((data
& mask
) << d
.bofs
) >> 32;
1049 uint32_t HELPER(bfffo_reg
)(uint32_t n
, uint32_t ofs
, uint32_t len
)
1051 return (n
? clz32(n
) : len
) + ofs
;
1054 uint64_t HELPER(bfffo_mem
)(CPUM68KState
*env
, uint32_t addr
,
1055 int32_t ofs
, uint32_t len
)
1057 uintptr_t ra
= GETPC();
1058 struct bf_data d
= bf_prep(addr
, ofs
, len
);
1059 uint64_t data
= bf_load(env
, d
.addr
, d
.blen
, ra
);
1060 uint64_t mask
= -1ull << (64 - d
.len
) >> d
.bofs
;
1061 uint64_t n
= (data
& mask
) << d
.bofs
;
1062 uint32_t ffo
= helper_bfffo_reg(n
>> 32, ofs
, d
.len
);
1065 * Return FFO in the low word and N in the high word.
1066 * Note that because of MASK and the shift, the low word
1072 void HELPER(chk
)(CPUM68KState
*env
, int32_t val
, int32_t ub
)
1076 * X: Not affected, C,V,Z: Undefined,
1077 * N: Set if val < 0; cleared if val > ub, undefined otherwise
1078 * We implement here values found from a real MC68040:
1079 * X,V,Z: Not affected
1080 * N: Set if val < 0; cleared if val >= 0
1081 * C: if 0 <= ub: set if val < 0 or val > ub, cleared otherwise
1082 * if 0 > ub: set if val > ub and val < 0, cleared otherwise
1085 env
->cc_c
= 0 <= ub
? val
< 0 || val
> ub
: val
> ub
&& val
< 0;
1087 if (val
< 0 || val
> ub
) {
1088 raise_exception_format2(env
, EXCP_CHK
, 2, GETPC());
1092 void HELPER(chk2
)(CPUM68KState
*env
, int32_t val
, int32_t lb
, int32_t ub
)
1096 * X: Not affected, N,V: Undefined,
1097 * Z: Set if val is equal to lb or ub
1098 * C: Set if val < lb or val > ub, cleared otherwise
1099 * We implement here values found from a real MC68040:
1100 * X,N,V: Not affected
1101 * Z: Set if val is equal to lb or ub
1102 * C: if lb <= ub: set if val < lb or val > ub, cleared otherwise
1103 * if lb > ub: set if val > ub and val < lb, cleared otherwise
1105 env
->cc_z
= val
!= lb
&& val
!= ub
;
1106 env
->cc_c
= lb
<= ub
? val
< lb
|| val
> ub
: val
> ub
&& val
< lb
;
1109 raise_exception_format2(env
, EXCP_CHK
, 4, GETPC());