4 * Copyright (c) 2006-2007 CodeSourcery
5 * Written by Paul Brook
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "exec/gdbstub.h"
26 #define SIGNBIT (1u << 31)
28 /* Sort alphabetically, except for "any". */
29 static gint
m68k_cpu_list_compare(gconstpointer a
, gconstpointer b
)
31 ObjectClass
*class_a
= (ObjectClass
*)a
;
32 ObjectClass
*class_b
= (ObjectClass
*)b
;
33 const char *name_a
, *name_b
;
35 name_a
= object_class_get_name(class_a
);
36 name_b
= object_class_get_name(class_b
);
37 if (strcmp(name_a
, "any-" TYPE_M68K_CPU
) == 0) {
39 } else if (strcmp(name_b
, "any-" TYPE_M68K_CPU
) == 0) {
42 return strcasecmp(name_a
, name_b
);
46 static void m68k_cpu_list_entry(gpointer data
, gpointer user_data
)
48 ObjectClass
*c
= data
;
49 CPUListState
*s
= user_data
;
53 typename
= object_class_get_name(c
);
54 name
= g_strndup(typename
, strlen(typename
) - strlen("-" TYPE_M68K_CPU
));
55 (*s
->cpu_fprintf
)(s
->file
, "%s\n",
60 void m68k_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
64 .cpu_fprintf
= cpu_fprintf
,
68 list
= object_class_get_list(TYPE_M68K_CPU
, false);
69 list
= g_slist_sort(list
, m68k_cpu_list_compare
);
70 g_slist_foreach(list
, m68k_cpu_list_entry
, &s
);
74 static int fpu_gdb_get_reg(CPUM68KState
*env
, uint8_t *mem_buf
, int n
)
77 stfq_p(mem_buf
, env
->fregs
[n
]);
81 /* FP control registers (not implemented) */
82 memset(mem_buf
, 0, 4);
88 static int fpu_gdb_set_reg(CPUM68KState
*env
, uint8_t *mem_buf
, int n
)
91 env
->fregs
[n
] = ldfq_p(mem_buf
);
95 /* FP control registers (not implemented) */
101 M68kCPU
*cpu_m68k_init(const char *cpu_model
)
107 oc
= cpu_class_by_name(TYPE_M68K_CPU
, cpu_model
);
111 cpu
= M68K_CPU(object_new(object_class_get_name(oc
)));
113 env
->cpu_model_str
= cpu_model
;
115 register_m68k_insns(env
);
117 object_property_set_bool(OBJECT(cpu
), true, "realized", NULL
);
122 void m68k_cpu_init_gdb(M68kCPU
*cpu
)
124 CPUState
*cs
= CPU(cpu
);
125 CPUM68KState
*env
= &cpu
->env
;
127 if (m68k_feature(env
, M68K_FEATURE_CF_FPU
)) {
128 gdb_register_coprocessor(cs
, fpu_gdb_get_reg
, fpu_gdb_set_reg
,
129 11, "cf-fp.xml", 18);
131 /* TODO: Add [E]MAC registers. */
134 void cpu_m68k_flush_flags(CPUM68KState
*env
, int cc_op
)
141 #define HIGHBIT 0x80000000u
143 #define SET_NZ(x) do { \
146 else if ((int32_t)(x) < 0) \
150 #define SET_FLAGS_SUB(type, utype) do { \
151 SET_NZ((type)dest); \
153 if ((utype) tmp < (utype) src) \
155 if ((1u << (sizeof(type) * 8 - 1)) & (tmp ^ dest) & (tmp ^ src)) \
174 if (HIGHBIT
& (src
^ dest
) & ~(tmp
^ src
))
178 SET_FLAGS_SUB(int32_t, uint32_t);
181 SET_FLAGS_SUB(int8_t, uint8_t);
184 SET_FLAGS_SUB(int16_t, uint16_t);
190 tmp
= dest
- src
- 1;
191 if (HIGHBIT
& (src
^ dest
) & ~(tmp
^ src
))
196 tmp
= dest
+ src
+ 1;
199 if (HIGHBIT
& (tmp
^ dest
) & (tmp
^ src
))
208 cpu_abort(env
, "Bad CC_OP %d", cc_op
);
210 env
->cc_op
= CC_OP_FLAGS
;
211 env
->cc_dest
= flags
;
214 void HELPER(movec
)(CPUM68KState
*env
, uint32_t reg
, uint32_t val
)
217 case 0x02: /* CACR */
221 case 0x04: case 0x05: case 0x06: case 0x07: /* ACR[0-3] */
222 /* TODO: Implement Access Control Registers. */
224 case 0x801: /* VBR */
227 /* TODO: Implement control registers. */
229 cpu_abort(env
, "Unimplemented control register write 0x%x = 0x%x\n",
234 void HELPER(set_macsr
)(CPUM68KState
*env
, uint32_t val
)
241 if ((env
->macsr
^ val
) & (MACSR_FI
| MACSR_SU
)) {
242 for (i
= 0; i
< 4; i
++) {
243 regval
= env
->macc
[i
];
244 exthigh
= regval
>> 40;
245 if (env
->macsr
& MACSR_FI
) {
250 extlow
= regval
>> 32;
252 if (env
->macsr
& MACSR_FI
) {
253 regval
= (((uint64_t)acc
) << 8) | extlow
;
254 regval
|= ((int64_t)exthigh
) << 40;
255 } else if (env
->macsr
& MACSR_SU
) {
256 regval
= acc
| (((int64_t)extlow
) << 32);
257 regval
|= ((int64_t)exthigh
) << 40;
259 regval
= acc
| (((uint64_t)extlow
) << 32);
260 regval
|= ((uint64_t)(uint8_t)exthigh
) << 40;
262 env
->macc
[i
] = regval
;
268 void m68k_switch_sp(CPUM68KState
*env
)
272 env
->sp
[env
->current_sp
] = env
->aregs
[7];
273 new_sp
= (env
->sr
& SR_S
&& env
->cacr
& M68K_CACR_EUSP
)
274 ? M68K_SSP
: M68K_USP
;
275 env
->aregs
[7] = env
->sp
[new_sp
];
276 env
->current_sp
= new_sp
;
279 #if defined(CONFIG_USER_ONLY)
281 int cpu_m68k_handle_mmu_fault (CPUM68KState
*env
, target_ulong address
, int rw
,
284 env
->exception_index
= EXCP_ACCESS
;
285 env
->mmu
.ar
= address
;
293 /* TODO: This will need fixing once the MMU is implemented. */
294 hwaddr
m68k_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
299 int cpu_m68k_handle_mmu_fault (CPUM68KState
*env
, target_ulong address
, int rw
,
304 address
&= TARGET_PAGE_MASK
;
305 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
306 tlb_set_page(env
, address
, address
, prot
, mmu_idx
, TARGET_PAGE_SIZE
);
310 /* Notify CPU of a pending interrupt. Prioritization and vectoring should
311 be handled by the interrupt controller. Real hardware only requests
312 the vector when the interrupt is acknowledged by the CPU. For
313 simplicitly we calculate it when the interrupt is signalled. */
314 void m68k_set_irq_level(M68kCPU
*cpu
, int level
, uint8_t vector
)
316 CPUState
*cs
= CPU(cpu
);
317 CPUM68KState
*env
= &cpu
->env
;
319 env
->pending_level
= level
;
320 env
->pending_vector
= vector
;
322 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
324 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
330 uint32_t HELPER(bitrev
)(uint32_t x
)
332 x
= ((x
>> 1) & 0x55555555u
) | ((x
<< 1) & 0xaaaaaaaau
);
333 x
= ((x
>> 2) & 0x33333333u
) | ((x
<< 2) & 0xccccccccu
);
334 x
= ((x
>> 4) & 0x0f0f0f0fu
) | ((x
<< 4) & 0xf0f0f0f0u
);
338 uint32_t HELPER(ff1
)(uint32_t x
)
346 uint32_t HELPER(sats
)(uint32_t val
, uint32_t ccr
)
348 /* The result has the opposite sign to the original value. */
350 val
= (((int32_t)val
) >> 31) ^ SIGNBIT
;
354 uint32_t HELPER(subx_cc
)(CPUM68KState
*env
, uint32_t op1
, uint32_t op2
)
359 old_flags
= env
->cc_dest
;
361 env
->cc_x
= (op1
<= op2
);
362 env
->cc_op
= CC_OP_SUBX
;
363 res
= op1
- (op2
+ 1);
365 env
->cc_x
= (op1
< op2
);
366 env
->cc_op
= CC_OP_SUB
;
371 cpu_m68k_flush_flags(env
, env
->cc_op
);
373 env
->cc_dest
&= (old_flags
| ~CCF_Z
);
377 uint32_t HELPER(addx_cc
)(CPUM68KState
*env
, uint32_t op1
, uint32_t op2
)
382 old_flags
= env
->cc_dest
;
385 env
->cc_x
= (res
<= op2
);
386 env
->cc_op
= CC_OP_ADDX
;
389 env
->cc_x
= (res
< op2
);
390 env
->cc_op
= CC_OP_ADD
;
394 cpu_m68k_flush_flags(env
, env
->cc_op
);
396 env
->cc_dest
&= (old_flags
| ~CCF_Z
);
400 uint32_t HELPER(xflag_lt
)(uint32_t a
, uint32_t b
)
405 void HELPER(set_sr
)(CPUM68KState
*env
, uint32_t val
)
407 env
->sr
= val
& 0xffff;
411 uint32_t HELPER(shl_cc
)(CPUM68KState
*env
, uint32_t val
, uint32_t shift
)
419 cf
= env
->cc_src
& CCF_C
;
420 } else if (shift
< 32) {
421 result
= val
<< shift
;
422 cf
= (val
>> (32 - shift
)) & 1;
423 } else if (shift
== 32) {
426 } else /* shift > 32 */ {
431 env
->cc_x
= (cf
!= 0);
432 env
->cc_dest
= result
;
436 uint32_t HELPER(shr_cc
)(CPUM68KState
*env
, uint32_t val
, uint32_t shift
)
444 cf
= env
->cc_src
& CCF_C
;
445 } else if (shift
< 32) {
446 result
= val
>> shift
;
447 cf
= (val
>> (shift
- 1)) & 1;
448 } else if (shift
== 32) {
451 } else /* shift > 32 */ {
456 env
->cc_x
= (cf
!= 0);
457 env
->cc_dest
= result
;
461 uint32_t HELPER(sar_cc
)(CPUM68KState
*env
, uint32_t val
, uint32_t shift
)
469 cf
= (env
->cc_src
& CCF_C
) != 0;
470 } else if (shift
< 32) {
471 result
= (int32_t)val
>> shift
;
472 cf
= (val
>> (shift
- 1)) & 1;
473 } else /* shift >= 32 */ {
474 result
= (int32_t)val
>> 31;
479 env
->cc_dest
= result
;
484 uint32_t HELPER(f64_to_i32
)(CPUM68KState
*env
, float64 val
)
486 return float64_to_int32(val
, &env
->fp_status
);
489 float32
HELPER(f64_to_f32
)(CPUM68KState
*env
, float64 val
)
491 return float64_to_float32(val
, &env
->fp_status
);
494 float64
HELPER(i32_to_f64
)(CPUM68KState
*env
, uint32_t val
)
496 return int32_to_float64(val
, &env
->fp_status
);
499 float64
HELPER(f32_to_f64
)(CPUM68KState
*env
, float32 val
)
501 return float32_to_float64(val
, &env
->fp_status
);
504 float64
HELPER(iround_f64
)(CPUM68KState
*env
, float64 val
)
506 return float64_round_to_int(val
, &env
->fp_status
);
509 float64
HELPER(itrunc_f64
)(CPUM68KState
*env
, float64 val
)
511 return float64_trunc_to_int(val
, &env
->fp_status
);
514 float64
HELPER(sqrt_f64
)(CPUM68KState
*env
, float64 val
)
516 return float64_sqrt(val
, &env
->fp_status
);
519 float64
HELPER(abs_f64
)(float64 val
)
521 return float64_abs(val
);
524 float64
HELPER(chs_f64
)(float64 val
)
526 return float64_chs(val
);
529 float64
HELPER(add_f64
)(CPUM68KState
*env
, float64 a
, float64 b
)
531 return float64_add(a
, b
, &env
->fp_status
);
534 float64
HELPER(sub_f64
)(CPUM68KState
*env
, float64 a
, float64 b
)
536 return float64_sub(a
, b
, &env
->fp_status
);
539 float64
HELPER(mul_f64
)(CPUM68KState
*env
, float64 a
, float64 b
)
541 return float64_mul(a
, b
, &env
->fp_status
);
544 float64
HELPER(div_f64
)(CPUM68KState
*env
, float64 a
, float64 b
)
546 return float64_div(a
, b
, &env
->fp_status
);
549 float64
HELPER(sub_cmp_f64
)(CPUM68KState
*env
, float64 a
, float64 b
)
551 /* ??? This may incorrectly raise exceptions. */
552 /* ??? Should flush denormals to zero. */
554 res
= float64_sub(a
, b
, &env
->fp_status
);
555 if (float64_is_quiet_nan(res
)) {
556 /* +/-inf compares equal against itself, but sub returns nan. */
557 if (!float64_is_quiet_nan(a
)
558 && !float64_is_quiet_nan(b
)) {
560 if (float64_lt_quiet(a
, res
, &env
->fp_status
))
561 res
= float64_chs(res
);
567 uint32_t HELPER(compare_f64
)(CPUM68KState
*env
, float64 val
)
569 return float64_compare_quiet(val
, float64_zero
, &env
->fp_status
);
573 /* FIXME: The MAC unit implementation is a bit of a mess. Some helpers
574 take values, others take register numbers and manipulate the contents
576 void HELPER(mac_move
)(CPUM68KState
*env
, uint32_t dest
, uint32_t src
)
579 env
->macc
[dest
] = env
->macc
[src
];
580 mask
= MACSR_PAV0
<< dest
;
581 if (env
->macsr
& (MACSR_PAV0
<< src
))
587 uint64_t HELPER(macmuls
)(CPUM68KState
*env
, uint32_t op1
, uint32_t op2
)
592 product
= (uint64_t)op1
* op2
;
593 res
= (product
<< 24) >> 24;
594 if (res
!= product
) {
595 env
->macsr
|= MACSR_V
;
596 if (env
->macsr
& MACSR_OMC
) {
597 /* Make sure the accumulate operation overflows. */
607 uint64_t HELPER(macmulu
)(CPUM68KState
*env
, uint32_t op1
, uint32_t op2
)
611 product
= (uint64_t)op1
* op2
;
612 if (product
& (0xffffffull
<< 40)) {
613 env
->macsr
|= MACSR_V
;
614 if (env
->macsr
& MACSR_OMC
) {
615 /* Make sure the accumulate operation overflows. */
618 product
&= ((1ull << 40) - 1);
624 uint64_t HELPER(macmulf
)(CPUM68KState
*env
, uint32_t op1
, uint32_t op2
)
629 product
= (uint64_t)op1
* op2
;
630 if (env
->macsr
& MACSR_RT
) {
631 remainder
= product
& 0xffffff;
633 if (remainder
> 0x800000)
635 else if (remainder
== 0x800000)
636 product
+= (product
& 1);
643 void HELPER(macsats
)(CPUM68KState
*env
, uint32_t acc
)
647 tmp
= env
->macc
[acc
];
648 result
= ((tmp
<< 16) >> 16);
650 env
->macsr
|= MACSR_V
;
652 if (env
->macsr
& MACSR_V
) {
653 env
->macsr
|= MACSR_PAV0
<< acc
;
654 if (env
->macsr
& MACSR_OMC
) {
655 /* The result is saturated to 32 bits, despite overflow occurring
656 at 48 bits. Seems weird, but that's what the hardware docs
658 result
= (result
>> 63) ^ 0x7fffffff;
661 env
->macc
[acc
] = result
;
664 void HELPER(macsatu
)(CPUM68KState
*env
, uint32_t acc
)
668 val
= env
->macc
[acc
];
669 if (val
& (0xffffull
<< 48)) {
670 env
->macsr
|= MACSR_V
;
672 if (env
->macsr
& MACSR_V
) {
673 env
->macsr
|= MACSR_PAV0
<< acc
;
674 if (env
->macsr
& MACSR_OMC
) {
675 if (val
> (1ull << 53))
678 val
= (1ull << 48) - 1;
680 val
&= ((1ull << 48) - 1);
683 env
->macc
[acc
] = val
;
686 void HELPER(macsatf
)(CPUM68KState
*env
, uint32_t acc
)
691 sum
= env
->macc
[acc
];
692 result
= (sum
<< 16) >> 16;
694 env
->macsr
|= MACSR_V
;
696 if (env
->macsr
& MACSR_V
) {
697 env
->macsr
|= MACSR_PAV0
<< acc
;
698 if (env
->macsr
& MACSR_OMC
) {
699 result
= (result
>> 63) ^ 0x7fffffffffffll
;
702 env
->macc
[acc
] = result
;
705 void HELPER(mac_set_flags
)(CPUM68KState
*env
, uint32_t acc
)
708 val
= env
->macc
[acc
];
710 env
->macsr
|= MACSR_Z
;
711 } else if (val
& (1ull << 47)) {
712 env
->macsr
|= MACSR_N
;
714 if (env
->macsr
& (MACSR_PAV0
<< acc
)) {
715 env
->macsr
|= MACSR_V
;
717 if (env
->macsr
& MACSR_FI
) {
718 val
= ((int64_t)val
) >> 40;
719 if (val
!= 0 && val
!= -1)
720 env
->macsr
|= MACSR_EV
;
721 } else if (env
->macsr
& MACSR_SU
) {
722 val
= ((int64_t)val
) >> 32;
723 if (val
!= 0 && val
!= -1)
724 env
->macsr
|= MACSR_EV
;
726 if ((val
>> 32) != 0)
727 env
->macsr
|= MACSR_EV
;
731 void HELPER(flush_flags
)(CPUM68KState
*env
, uint32_t cc_op
)
733 cpu_m68k_flush_flags(env
, cc_op
);
736 uint32_t HELPER(get_macf
)(CPUM68KState
*env
, uint64_t val
)
741 if (env
->macsr
& MACSR_SU
) {
742 /* 16-bit rounding. */
743 rem
= val
& 0xffffff;
744 val
= (val
>> 24) & 0xffffu
;
747 else if (rem
== 0x800000)
749 } else if (env
->macsr
& MACSR_RT
) {
750 /* 32-bit rounding. */
755 else if (rem
== 0x80)
761 if (env
->macsr
& MACSR_OMC
) {
763 if (env
->macsr
& MACSR_SU
) {
764 if (val
!= (uint16_t) val
) {
765 result
= ((val
>> 63) ^ 0x7fff) & 0xffff;
767 result
= val
& 0xffff;
770 if (val
!= (uint32_t)val
) {
771 result
= ((uint32_t)(val
>> 63) & 0x7fffffff);
773 result
= (uint32_t)val
;
778 if (env
->macsr
& MACSR_SU
) {
779 result
= val
& 0xffff;
781 result
= (uint32_t)val
;
787 uint32_t HELPER(get_macs
)(uint64_t val
)
789 if (val
== (int32_t)val
) {
792 return (val
>> 61) ^ ~SIGNBIT
;
796 uint32_t HELPER(get_macu
)(uint64_t val
)
798 if ((val
>> 32) == 0) {
799 return (uint32_t)val
;
805 uint32_t HELPER(get_mac_extf
)(CPUM68KState
*env
, uint32_t acc
)
808 val
= env
->macc
[acc
] & 0x00ff;
809 val
= (env
->macc
[acc
] >> 32) & 0xff00;
810 val
|= (env
->macc
[acc
+ 1] << 16) & 0x00ff0000;
811 val
|= (env
->macc
[acc
+ 1] >> 16) & 0xff000000;
815 uint32_t HELPER(get_mac_exti
)(CPUM68KState
*env
, uint32_t acc
)
818 val
= (env
->macc
[acc
] >> 32) & 0xffff;
819 val
|= (env
->macc
[acc
+ 1] >> 16) & 0xffff0000;
823 void HELPER(set_mac_extf
)(CPUM68KState
*env
, uint32_t val
, uint32_t acc
)
827 res
= env
->macc
[acc
] & 0xffffffff00ull
;
828 tmp
= (int16_t)(val
& 0xff00);
829 res
|= ((int64_t)tmp
) << 32;
831 env
->macc
[acc
] = res
;
832 res
= env
->macc
[acc
+ 1] & 0xffffffff00ull
;
833 tmp
= (val
& 0xff000000);
834 res
|= ((int64_t)tmp
) << 16;
835 res
|= (val
>> 16) & 0xff;
836 env
->macc
[acc
+ 1] = res
;
839 void HELPER(set_mac_exts
)(CPUM68KState
*env
, uint32_t val
, uint32_t acc
)
843 res
= (uint32_t)env
->macc
[acc
];
845 res
|= ((int64_t)tmp
) << 32;
846 env
->macc
[acc
] = res
;
847 res
= (uint32_t)env
->macc
[acc
+ 1];
848 tmp
= val
& 0xffff0000;
849 res
|= (int64_t)tmp
<< 16;
850 env
->macc
[acc
+ 1] = res
;
853 void HELPER(set_mac_extu
)(CPUM68KState
*env
, uint32_t val
, uint32_t acc
)
856 res
= (uint32_t)env
->macc
[acc
];
857 res
|= ((uint64_t)(val
& 0xffff)) << 32;
858 env
->macc
[acc
] = res
;
859 res
= (uint32_t)env
->macc
[acc
+ 1];
860 res
|= (uint64_t)(val
& 0xffff0000) << 16;
861 env
->macc
[acc
+ 1] = res
;