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.1 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 * Lesser 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/>.
21 #include "qemu/osdep.h"
23 #include "exec/exec-all.h"
24 #include "exec/gdbstub.h"
25 #include "exec/helper-proto.h"
26 #include "fpu/softfloat.h"
27 #include "qemu/qemu-print.h"
29 #define SIGNBIT (1u << 31)
31 /* Sort alphabetically, except for "any". */
32 static gint m68k_cpu_list_compare(gconstpointer a, gconstpointer b)
34 ObjectClass *class_a = (ObjectClass *)a;
35 ObjectClass *class_b = (ObjectClass *)b;
36 const char *name_a, *name_b;
38 name_a = object_class_get_name(class_a);
39 name_b = object_class_get_name(class_b);
40 if (strcmp(name_a, "any-" TYPE_M68K_CPU) == 0) {
42 } else if (strcmp(name_b, "any-" TYPE_M68K_CPU) == 0) {
45 return strcasecmp(name_a, name_b);
49 static void m68k_cpu_list_entry(gpointer data, gpointer user_data)
51 ObjectClass *c = data;
55 typename = object_class_get_name(c);
56 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_M68K_CPU));
57 qemu_printf("%s\n", name);
61 void m68k_cpu_list(void)
65 list = object_class_get_list(TYPE_M68K_CPU, false);
66 list = g_slist_sort(list, m68k_cpu_list_compare);
67 g_slist_foreach(list, m68k_cpu_list_entry, NULL);
71 static int cf_fpu_gdb_get_reg(CPUM68KState *env, uint8_t *mem_buf, int n)
75 stfq_p(mem_buf, floatx80_to_float64(env->fregs[n].d, &s));
79 case 8: /* fpcontrol */
80 stl_be_p(mem_buf, env->fpcr);
82 case 9: /* fpstatus */
83 stl_be_p(mem_buf, env->fpsr);
85 case 10: /* fpiar, not implemented */
86 memset(mem_buf, 0, 4);
92 static int cf_fpu_gdb_set_reg(CPUM68KState *env, uint8_t *mem_buf, int n)
96 env->fregs[n].d = float64_to_floatx80(ldfq_p(mem_buf), &s);
100 case 8: /* fpcontrol */
101 cpu_m68k_set_fpcr(env, ldl_p(mem_buf));
103 case 9: /* fpstatus */
104 env->fpsr = ldl_p(mem_buf);
106 case 10: /* fpiar, not implemented */
112 static int m68k_fpu_gdb_get_reg(CPUM68KState *env, uint8_t *mem_buf, int n)
115 stw_be_p(mem_buf, env->fregs[n].l.upper);
116 memset(mem_buf + 2, 0, 2);
117 stq_be_p(mem_buf + 4, env->fregs[n].l.lower);
121 case 8: /* fpcontrol */
122 stl_be_p(mem_buf, env->fpcr);
124 case 9: /* fpstatus */
125 stl_be_p(mem_buf, env->fpsr);
127 case 10: /* fpiar, not implemented */
128 memset(mem_buf, 0, 4);
134 static int m68k_fpu_gdb_set_reg(CPUM68KState *env, uint8_t *mem_buf, int n)
137 env->fregs[n].l.upper = lduw_be_p(mem_buf);
138 env->fregs[n].l.lower = ldq_be_p(mem_buf + 4);
142 case 8: /* fpcontrol */
143 cpu_m68k_set_fpcr(env, ldl_p(mem_buf));
145 case 9: /* fpstatus */
146 env->fpsr = ldl_p(mem_buf);
148 case 10: /* fpiar, not implemented */
154 void m68k_cpu_init_gdb(M68kCPU *cpu)
156 CPUState *cs = CPU(cpu);
157 CPUM68KState *env = &cpu->env;
159 if (m68k_feature(env, M68K_FEATURE_CF_FPU)) {
160 gdb_register_coprocessor(cs, cf_fpu_gdb_get_reg, cf_fpu_gdb_set_reg,
161 11, "cf-fp.xml", 18);
162 } else if (m68k_feature(env, M68K_FEATURE_FPU)) {
163 gdb_register_coprocessor(cs, m68k_fpu_gdb_get_reg,
164 m68k_fpu_gdb_set_reg, 11, "m68k-fp.xml", 18);
166 /* TODO: Add [E]MAC registers. */
169 void HELPER(cf_movec_to)(CPUM68KState *env, uint32_t reg, uint32_t val)
180 /* TODO: Implement Access Control Registers. */
185 /* TODO: Implement control registers. */
187 cpu_abort(env_cpu(env),
188 "Unimplemented control register write 0x%x = 0x%x\n",
193 void HELPER(m68k_movec_to)(CPUM68KState *env, uint32_t reg, uint32_t val)
216 env->mmu.mmusr = val;
225 env->sp[M68K_USP] = val;
228 env->sp[M68K_SSP] = val;
231 env->sp[M68K_ISP] = val;
233 /* MC68040/MC68LC040 */
235 env->mmu.ttr[M68K_ITTR0] = val;
238 env->mmu.ttr[M68K_ITTR1] = val;
241 env->mmu.ttr[M68K_DTTR0] = val;
244 env->mmu.ttr[M68K_DTTR1] = val;
247 cpu_abort(env_cpu(env),
248 "Unimplemented control register write 0x%x = 0x%x\n",
252 uint32_t HELPER(m68k_movec_from)(CPUM68KState *env, uint32_t reg)
269 return env->mmu.mmusr;
273 return env->sp[M68K_USP];
275 return env->sp[M68K_SSP];
277 return env->sp[M68K_ISP];
278 /* MC68040/MC68LC040 */
282 return env->mmu.ttr[M68K_ITTR0];
284 return env->mmu.ttr[M68K_ITTR1];
286 return env->mmu.ttr[M68K_DTTR0];
288 return env->mmu.ttr[M68K_DTTR1];
290 cpu_abort(env_cpu(env), "Unimplemented control register read 0x%x\n",
294 void HELPER(set_macsr)(CPUM68KState *env, uint32_t val)
301 if ((env->macsr ^ val) & (MACSR_FI | MACSR_SU)) {
302 for (i = 0; i < 4; i++) {
303 regval = env->macc[i];
304 exthigh = regval >> 40;
305 if (env->macsr & MACSR_FI) {
310 extlow = regval >> 32;
312 if (env->macsr & MACSR_FI) {
313 regval = (((uint64_t)acc) << 8) | extlow;
314 regval |= ((int64_t)exthigh) << 40;
315 } else if (env->macsr & MACSR_SU) {
316 regval = acc | (((int64_t)extlow) << 32);
317 regval |= ((int64_t)exthigh) << 40;
319 regval = acc | (((uint64_t)extlow) << 32);
320 regval |= ((uint64_t)(uint8_t)exthigh) << 40;
322 env->macc[i] = regval;
328 void m68k_switch_sp(CPUM68KState *env)
332 env->sp[env->current_sp] = env->aregs[7];
333 if (m68k_feature(env, M68K_FEATURE_M68000)) {
334 if (env->sr & SR_S) {
335 if (env->sr & SR_M) {
344 new_sp = (env->sr & SR_S && env->cacr & M68K_CACR_EUSP)
345 ? M68K_SSP : M68K_USP;
347 env->aregs[7] = env->sp[new_sp];
348 env->current_sp = new_sp;
351 #if !defined(CONFIG_USER_ONLY)
352 /* MMU: 68040 only */
354 static void print_address_zone(uint32_t logical, uint32_t physical,
355 uint32_t size, int attr)
357 qemu_printf("%08x - %08x -> %08x - %08x %c ",
358 logical, logical + size - 1,
359 physical, physical + size - 1,
360 attr & 4 ? 'W' : '-');
363 qemu_printf("(%d KiB)\n", size);
367 qemu_printf("(%d MiB)\n", size);
370 qemu_printf("(%d GiB)\n", size);
375 static void dump_address_map(CPUM68KState *env, uint32_t root_pointer)
378 int tic_size, tic_shift;
380 uint32_t tia, tib, tic;
381 uint32_t logical = 0xffffffff, physical = 0xffffffff;
382 uint32_t first_logical = 0xffffffff, first_physical = 0xffffffff;
383 uint32_t last_logical, last_physical;
385 int last_attr = -1, attr = -1;
386 CPUState *cs = env_cpu(env);
389 if (env->mmu.tcr & M68K_TCR_PAGE_8K) {
393 tib_mask = M68K_8K_PAGE_MASK;
398 tib_mask = M68K_4K_PAGE_MASK;
400 for (i = 0; i < M68K_ROOT_POINTER_ENTRIES; i++) {
401 tia = address_space_ldl(cs->as, M68K_POINTER_BASE(root_pointer) + i * 4,
402 MEMTXATTRS_UNSPECIFIED, &txres);
403 if (txres != MEMTX_OK || !M68K_UDT_VALID(tia)) {
406 for (j = 0; j < M68K_ROOT_POINTER_ENTRIES; j++) {
407 tib = address_space_ldl(cs->as, M68K_POINTER_BASE(tia) + j * 4,
408 MEMTXATTRS_UNSPECIFIED, &txres);
409 if (txres != MEMTX_OK || !M68K_UDT_VALID(tib)) {
412 for (k = 0; k < tic_size; k++) {
413 tic = address_space_ldl(cs->as, (tib & tib_mask) + k * 4,
414 MEMTXATTRS_UNSPECIFIED, &txres);
415 if (txres != MEMTX_OK || !M68K_PDT_VALID(tic)) {
418 if (M68K_PDT_INDIRECT(tic)) {
419 tic = address_space_ldl(cs->as, M68K_INDIRECT_POINTER(tic),
420 MEMTXATTRS_UNSPECIFIED, &txres);
421 if (txres != MEMTX_OK) {
426 last_logical = logical;
427 logical = (i << M68K_TTS_ROOT_SHIFT) |
428 (j << M68K_TTS_POINTER_SHIFT) |
431 last_physical = physical;
432 physical = tic & ~((1 << tic_shift) - 1);
435 attr = tic & ((1 << tic_shift) - 1);
437 if ((logical != (last_logical + (1 << tic_shift))) ||
438 (physical != (last_physical + (1 << tic_shift))) ||
439 (attr & 4) != (last_attr & 4)) {
441 if (first_logical != 0xffffffff) {
442 size = last_logical + (1 << tic_shift) -
444 print_address_zone(first_logical,
445 first_physical, size, last_attr);
447 first_logical = logical;
448 first_physical = physical;
453 if (first_logical != logical || (attr & 4) != (last_attr & 4)) {
454 size = logical + (1 << tic_shift) - first_logical;
455 print_address_zone(first_logical, first_physical, size, last_attr);
459 #define DUMP_CACHEFLAGS(a) \
460 switch (a & M68K_DESC_CACHEMODE) { \
461 case M68K_DESC_CM_WRTHRU: /* cachable, write-through */ \
464 case M68K_DESC_CM_COPYBK: /* cachable, copyback */ \
467 case M68K_DESC_CM_SERIAL: /* noncachable, serialized */ \
470 case M68K_DESC_CM_NCACHE: /* noncachable */ \
475 static void dump_ttr(uint32_t ttr)
477 if ((ttr & M68K_TTR_ENABLED) == 0) {
478 qemu_printf("disabled\n");
481 qemu_printf("Base: 0x%08x Mask: 0x%08x Control: ",
482 ttr & M68K_TTR_ADDR_BASE,
483 (ttr & M68K_TTR_ADDR_MASK) << M68K_TTR_ADDR_MASK_SHIFT);
484 switch (ttr & M68K_TTR_SFIELD) {
485 case M68K_TTR_SFIELD_USER:
488 case M68K_TTR_SFIELD_SUPER:
495 DUMP_CACHEFLAGS(ttr);
496 if (ttr & M68K_DESC_WRITEPROT) {
501 qemu_printf(" U: %d\n", (ttr & M68K_DESC_USERATTR) >>
502 M68K_DESC_USERATTR_SHIFT);
505 void dump_mmu(CPUM68KState *env)
507 if ((env->mmu.tcr & M68K_TCR_ENABLED) == 0) {
508 qemu_printf("Translation disabled\n");
511 qemu_printf("Page Size: ");
512 if (env->mmu.tcr & M68K_TCR_PAGE_8K) {
513 qemu_printf("8kB\n");
515 qemu_printf("4kB\n");
518 qemu_printf("MMUSR: ");
519 if (env->mmu.mmusr & M68K_MMU_B_040) {
520 qemu_printf("BUS ERROR\n");
522 qemu_printf("Phy=%08x Flags: ", env->mmu.mmusr & 0xfffff000);
523 /* flags found on the page descriptor */
524 if (env->mmu.mmusr & M68K_MMU_G_040) {
525 qemu_printf("G"); /* Global */
529 if (env->mmu.mmusr & M68K_MMU_S_040) {
530 qemu_printf("S"); /* Supervisor */
534 if (env->mmu.mmusr & M68K_MMU_M_040) {
535 qemu_printf("M"); /* Modified */
539 if (env->mmu.mmusr & M68K_MMU_WP_040) {
540 qemu_printf("W"); /* Write protect */
544 if (env->mmu.mmusr & M68K_MMU_T_040) {
545 qemu_printf("T"); /* Transparent */
549 if (env->mmu.mmusr & M68K_MMU_R_040) {
550 qemu_printf("R"); /* Resident */
554 qemu_printf(" Cache: ");
555 DUMP_CACHEFLAGS(env->mmu.mmusr);
556 qemu_printf(" U: %d\n", (env->mmu.mmusr >> 8) & 3);
560 qemu_printf("ITTR0: ");
561 dump_ttr(env->mmu.ttr[M68K_ITTR0]);
562 qemu_printf("ITTR1: ");
563 dump_ttr(env->mmu.ttr[M68K_ITTR1]);
564 qemu_printf("DTTR0: ");
565 dump_ttr(env->mmu.ttr[M68K_DTTR0]);
566 qemu_printf("DTTR1: ");
567 dump_ttr(env->mmu.ttr[M68K_DTTR1]);
569 qemu_printf("SRP: 0x%08x\n", env->mmu.srp);
570 dump_address_map(env, env->mmu.srp);
572 qemu_printf("URP: 0x%08x\n", env->mmu.urp);
573 dump_address_map(env, env->mmu.urp);
576 static int check_TTR(uint32_t ttr, int *prot, target_ulong addr,
581 /* check if transparent translation is enabled */
582 if ((ttr & M68K_TTR_ENABLED) == 0) {
586 /* check mode access */
587 switch (ttr & M68K_TTR_SFIELD) {
588 case M68K_TTR_SFIELD_USER:
589 /* match only if user */
590 if ((access_type & ACCESS_SUPER) != 0) {
594 case M68K_TTR_SFIELD_SUPER:
595 /* match only if supervisor */
596 if ((access_type & ACCESS_SUPER) == 0) {
601 /* all other values disable mode matching (FC2) */
605 /* check address matching */
607 base = ttr & M68K_TTR_ADDR_BASE;
608 mask = (ttr & M68K_TTR_ADDR_MASK) ^ M68K_TTR_ADDR_MASK;
609 mask <<= M68K_TTR_ADDR_MASK_SHIFT;
611 if ((addr & mask) != (base & mask)) {
615 *prot = PAGE_READ | PAGE_EXEC;
616 if ((ttr & M68K_DESC_WRITEPROT) == 0) {
623 static int get_physical_address(CPUM68KState *env, hwaddr *physical,
624 int *prot, target_ulong address,
625 int access_type, target_ulong *page_size)
627 CPUState *cs = env_cpu(env);
630 target_ulong page_mask;
631 bool debug = access_type & ACCESS_DEBUG;
636 /* Transparent Translation (physical = logical) */
637 for (i = 0; i < M68K_MAX_TTR; i++) {
638 if (check_TTR(env->mmu.TTR(access_type, i),
639 prot, address, access_type)) {
640 if (access_type & ACCESS_PTEST) {
641 /* Transparent Translation Register bit */
642 env->mmu.mmusr = M68K_MMU_T_040 | M68K_MMU_R_040;
644 *physical = address & TARGET_PAGE_MASK;
645 *page_size = TARGET_PAGE_SIZE;
650 /* Page Table Root Pointer */
651 *prot = PAGE_READ | PAGE_WRITE;
652 if (access_type & ACCESS_CODE) {
655 if (access_type & ACCESS_SUPER) {
662 entry = M68K_POINTER_BASE(next) | M68K_ROOT_INDEX(address);
664 next = address_space_ldl(cs->as, entry, MEMTXATTRS_UNSPECIFIED, &txres);
665 if (txres != MEMTX_OK) {
668 if (!M68K_UDT_VALID(next)) {
671 if (!(next & M68K_DESC_USED) && !debug) {
672 address_space_stl(cs->as, entry, next | M68K_DESC_USED,
673 MEMTXATTRS_UNSPECIFIED, &txres);
674 if (txres != MEMTX_OK) {
678 if (next & M68K_DESC_WRITEPROT) {
679 if (access_type & ACCESS_PTEST) {
680 env->mmu.mmusr |= M68K_MMU_WP_040;
682 *prot &= ~PAGE_WRITE;
683 if (access_type & ACCESS_STORE) {
689 entry = M68K_POINTER_BASE(next) | M68K_POINTER_INDEX(address);
691 next = address_space_ldl(cs->as, entry, MEMTXATTRS_UNSPECIFIED, &txres);
692 if (txres != MEMTX_OK) {
695 if (!M68K_UDT_VALID(next)) {
698 if (!(next & M68K_DESC_USED) && !debug) {
699 address_space_stl(cs->as, entry, next | M68K_DESC_USED,
700 MEMTXATTRS_UNSPECIFIED, &txres);
701 if (txres != MEMTX_OK) {
705 if (next & M68K_DESC_WRITEPROT) {
706 if (access_type & ACCESS_PTEST) {
707 env->mmu.mmusr |= M68K_MMU_WP_040;
709 *prot &= ~PAGE_WRITE;
710 if (access_type & ACCESS_STORE) {
716 if (env->mmu.tcr & M68K_TCR_PAGE_8K) {
717 entry = M68K_8K_PAGE_BASE(next) | M68K_8K_PAGE_INDEX(address);
719 entry = M68K_4K_PAGE_BASE(next) | M68K_4K_PAGE_INDEX(address);
722 next = address_space_ldl(cs->as, entry, MEMTXATTRS_UNSPECIFIED, &txres);
723 if (txres != MEMTX_OK) {
727 if (!M68K_PDT_VALID(next)) {
730 if (M68K_PDT_INDIRECT(next)) {
731 next = address_space_ldl(cs->as, M68K_INDIRECT_POINTER(next),
732 MEMTXATTRS_UNSPECIFIED, &txres);
733 if (txres != MEMTX_OK) {
737 if (access_type & ACCESS_STORE) {
738 if (next & M68K_DESC_WRITEPROT) {
739 if (!(next & M68K_DESC_USED) && !debug) {
740 address_space_stl(cs->as, entry, next | M68K_DESC_USED,
741 MEMTXATTRS_UNSPECIFIED, &txres);
742 if (txres != MEMTX_OK) {
746 } else if ((next & (M68K_DESC_MODIFIED | M68K_DESC_USED)) !=
747 (M68K_DESC_MODIFIED | M68K_DESC_USED) && !debug) {
748 address_space_stl(cs->as, entry,
749 next | (M68K_DESC_MODIFIED | M68K_DESC_USED),
750 MEMTXATTRS_UNSPECIFIED, &txres);
751 if (txres != MEMTX_OK) {
756 if (!(next & M68K_DESC_USED) && !debug) {
757 address_space_stl(cs->as, entry, next | M68K_DESC_USED,
758 MEMTXATTRS_UNSPECIFIED, &txres);
759 if (txres != MEMTX_OK) {
765 if (env->mmu.tcr & M68K_TCR_PAGE_8K) {
770 *page_size = 1 << page_bits;
771 page_mask = ~(*page_size - 1);
772 *physical = next & page_mask;
774 if (access_type & ACCESS_PTEST) {
775 env->mmu.mmusr |= next & M68K_MMU_SR_MASK_040;
776 env->mmu.mmusr |= *physical & 0xfffff000;
777 env->mmu.mmusr |= M68K_MMU_R_040;
780 if (next & M68K_DESC_WRITEPROT) {
781 *prot &= ~PAGE_WRITE;
782 if (access_type & ACCESS_STORE) {
786 if (next & M68K_DESC_SUPERONLY) {
787 if ((access_type & ACCESS_SUPER) == 0) {
796 * A page table load/store failed. TODO: we should really raise a
797 * suitable guest fault here if this is not a debug access.
798 * For now just return that the translation failed.
803 hwaddr m68k_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
805 M68kCPU *cpu = M68K_CPU(cs);
806 CPUM68KState *env = &cpu->env;
810 target_ulong page_size;
812 if ((env->mmu.tcr & M68K_TCR_ENABLED) == 0) {
817 access_type = ACCESS_DATA | ACCESS_DEBUG;
818 if (env->sr & SR_S) {
819 access_type |= ACCESS_SUPER;
821 if (get_physical_address(env, &phys_addr, &prot,
822 addr, access_type, &page_size) != 0) {
829 * Notify CPU of a pending interrupt. Prioritization and vectoring should
830 * be handled by the interrupt controller. Real hardware only requests
831 * the vector when the interrupt is acknowledged by the CPU. For
832 * simplicity we calculate it when the interrupt is signalled.
834 void m68k_set_irq_level(M68kCPU *cpu, int level, uint8_t vector)
836 CPUState *cs = CPU(cpu);
837 CPUM68KState *env = &cpu->env;
839 env->pending_level = level;
840 env->pending_vector = vector;
842 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
844 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
850 bool m68k_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
851 MMUAccessType qemu_access_type, int mmu_idx,
852 bool probe, uintptr_t retaddr)
854 M68kCPU *cpu = M68K_CPU(cs);
855 CPUM68KState *env = &cpu->env;
857 #ifndef CONFIG_USER_ONLY
862 target_ulong page_size;
864 if ((env->mmu.tcr & M68K_TCR_ENABLED) == 0) {
866 tlb_set_page(cs, address & TARGET_PAGE_MASK,
867 address & TARGET_PAGE_MASK,
868 PAGE_READ | PAGE_WRITE | PAGE_EXEC,
869 mmu_idx, TARGET_PAGE_SIZE);
873 if (qemu_access_type == MMU_INST_FETCH) {
874 access_type = ACCESS_CODE;
876 access_type = ACCESS_DATA;
877 if (qemu_access_type == MMU_DATA_STORE) {
878 access_type |= ACCESS_STORE;
881 if (mmu_idx != MMU_USER_IDX) {
882 access_type |= ACCESS_SUPER;
885 ret = get_physical_address(&cpu->env, &physical, &prot,
886 address, access_type, &page_size);
887 if (likely(ret == 0)) {
888 address &= TARGET_PAGE_MASK;
889 physical += address & (page_size - 1);
890 tlb_set_page(cs, address, physical,
891 prot, mmu_idx, TARGET_PAGE_SIZE);
900 env->mmu.ssw = M68K_ATC_040;
903 env->mmu.ssw |= M68K_BA_SIZE_BYTE;
906 env->mmu.ssw |= M68K_BA_SIZE_WORD;
909 env->mmu.ssw |= M68K_BA_SIZE_LONG;
912 if (access_type & ACCESS_SUPER) {
913 env->mmu.ssw |= M68K_TM_040_SUPER;
915 if (access_type & ACCESS_CODE) {
916 env->mmu.ssw |= M68K_TM_040_CODE;
918 env->mmu.ssw |= M68K_TM_040_DATA;
920 if (!(access_type & ACCESS_STORE)) {
921 env->mmu.ssw |= M68K_RW_040;
925 cs->exception_index = EXCP_ACCESS;
926 env->mmu.ar = address;
927 cpu_loop_exit_restore(cs, retaddr);
930 uint32_t HELPER(bitrev)(uint32_t x)
932 x = ((x >> 1) & 0x55555555u) | ((x << 1) & 0xaaaaaaaau);
933 x = ((x >> 2) & 0x33333333u) | ((x << 2) & 0xccccccccu);
934 x = ((x >> 4) & 0x0f0f0f0fu) | ((x << 4) & 0xf0f0f0f0u);
938 uint32_t HELPER(ff1)(uint32_t x)
946 uint32_t HELPER(sats)(uint32_t val, uint32_t v)
948 /* The result has the opposite sign to the original value. */
949 if ((int32_t)v < 0) {
950 val = (((int32_t)val) >> 31) ^ SIGNBIT;
955 void cpu_m68k_set_sr(CPUM68KState *env, uint32_t sr)
957 env->sr = sr & 0xffe0;
958 cpu_m68k_set_ccr(env, sr);
962 void HELPER(set_sr)(CPUM68KState *env, uint32_t val)
964 cpu_m68k_set_sr(env, val);
968 /* FIXME: The MAC unit implementation is a bit of a mess. Some helpers
969 take values, others take register numbers and manipulate the contents
971 void HELPER(mac_move)(CPUM68KState *env, uint32_t dest, uint32_t src)
974 env->macc[dest] = env->macc[src];
975 mask = MACSR_PAV0 << dest;
976 if (env->macsr & (MACSR_PAV0 << src))
982 uint64_t HELPER(macmuls)(CPUM68KState *env, uint32_t op1, uint32_t op2)
987 product = (uint64_t)op1 * op2;
988 res = (product << 24) >> 24;
989 if (res != product) {
990 env->macsr |= MACSR_V;
991 if (env->macsr & MACSR_OMC) {
992 /* Make sure the accumulate operation overflows. */
1002 uint64_t HELPER(macmulu)(CPUM68KState *env, uint32_t op1, uint32_t op2)
1006 product = (uint64_t)op1 * op2;
1007 if (product & (0xffffffull << 40)) {
1008 env->macsr |= MACSR_V;
1009 if (env->macsr & MACSR_OMC) {
1010 /* Make sure the accumulate operation overflows. */
1011 product = 1ll << 50;
1013 product &= ((1ull << 40) - 1);
1019 uint64_t HELPER(macmulf)(CPUM68KState *env, uint32_t op1, uint32_t op2)
1024 product = (uint64_t)op1 * op2;
1025 if (env->macsr & MACSR_RT) {
1026 remainder = product & 0xffffff;
1028 if (remainder > 0x800000)
1030 else if (remainder == 0x800000)
1031 product += (product & 1);
1038 void HELPER(macsats)(CPUM68KState *env, uint32_t acc)
1042 tmp = env->macc[acc];
1043 result = ((tmp << 16) >> 16);
1044 if (result != tmp) {
1045 env->macsr |= MACSR_V;
1047 if (env->macsr & MACSR_V) {
1048 env->macsr |= MACSR_PAV0 << acc;
1049 if (env->macsr & MACSR_OMC) {
1050 /* The result is saturated to 32 bits, despite overflow occurring
1051 at 48 bits. Seems weird, but that's what the hardware docs
1053 result = (result >> 63) ^ 0x7fffffff;
1056 env->macc[acc] = result;
1059 void HELPER(macsatu)(CPUM68KState *env, uint32_t acc)
1063 val = env->macc[acc];
1064 if (val & (0xffffull << 48)) {
1065 env->macsr |= MACSR_V;
1067 if (env->macsr & MACSR_V) {
1068 env->macsr |= MACSR_PAV0 << acc;
1069 if (env->macsr & MACSR_OMC) {
1070 if (val > (1ull << 53))
1073 val = (1ull << 48) - 1;
1075 val &= ((1ull << 48) - 1);
1078 env->macc[acc] = val;
1081 void HELPER(macsatf)(CPUM68KState *env, uint32_t acc)
1086 sum = env->macc[acc];
1087 result = (sum << 16) >> 16;
1088 if (result != sum) {
1089 env->macsr |= MACSR_V;
1091 if (env->macsr & MACSR_V) {
1092 env->macsr |= MACSR_PAV0 << acc;
1093 if (env->macsr & MACSR_OMC) {
1094 result = (result >> 63) ^ 0x7fffffffffffll;
1097 env->macc[acc] = result;
1100 void HELPER(mac_set_flags)(CPUM68KState *env, uint32_t acc)
1103 val = env->macc[acc];
1105 env->macsr |= MACSR_Z;
1106 } else if (val & (1ull << 47)) {
1107 env->macsr |= MACSR_N;
1109 if (env->macsr & (MACSR_PAV0 << acc)) {
1110 env->macsr |= MACSR_V;
1112 if (env->macsr & MACSR_FI) {
1113 val = ((int64_t)val) >> 40;
1114 if (val != 0 && val != -1)
1115 env->macsr |= MACSR_EV;
1116 } else if (env->macsr & MACSR_SU) {
1117 val = ((int64_t)val) >> 32;
1118 if (val != 0 && val != -1)
1119 env->macsr |= MACSR_EV;
1121 if ((val >> 32) != 0)
1122 env->macsr |= MACSR_EV;
1126 #define EXTSIGN(val, index) ( \
1127 (index == 0) ? (int8_t)(val) : ((index == 1) ? (int16_t)(val) : (val)) \
1130 #define COMPUTE_CCR(op, x, n, z, v, c) { \
1133 /* Everything in place. */ \
1140 src1 = EXTSIGN(res - src2, op - CC_OP_ADDB); \
1143 v = (res ^ src1) & ~(src1 ^ src2); \
1150 src1 = EXTSIGN(res + src2, op - CC_OP_SUBB); \
1153 v = (res ^ src1) & (src1 ^ src2); \
1160 res = EXTSIGN(src1 - src2, op - CC_OP_CMPB); \
1164 v = (res ^ src1) & (src1 ^ src2); \
1171 cpu_abort(env_cpu(env), "Bad CC_OP %d", op); \
1175 uint32_t cpu_m68k_get_ccr(CPUM68KState *env)
1177 uint32_t x, c, n, z, v;
1178 uint32_t res, src1, src2;
1186 COMPUTE_CCR(env->cc_op, x, n, z, v, c);
1192 return x * CCF_X + n * CCF_N + z * CCF_Z + v * CCF_V + c * CCF_C;
1195 uint32_t HELPER(get_ccr)(CPUM68KState *env)
1197 return cpu_m68k_get_ccr(env);
1200 void cpu_m68k_set_ccr(CPUM68KState *env, uint32_t ccr)
1202 env->cc_x = (ccr & CCF_X ? 1 : 0);
1203 env->cc_n = (ccr & CCF_N ? -1 : 0);
1204 env->cc_z = (ccr & CCF_Z ? 0 : 1);
1205 env->cc_v = (ccr & CCF_V ? -1 : 0);
1206 env->cc_c = (ccr & CCF_C ? 1 : 0);
1207 env->cc_op = CC_OP_FLAGS;
1210 void HELPER(set_ccr)(CPUM68KState *env, uint32_t ccr)
1212 cpu_m68k_set_ccr(env, ccr);
1215 void HELPER(flush_flags)(CPUM68KState *env, uint32_t cc_op)
1217 uint32_t res, src1, src2;
1219 COMPUTE_CCR(cc_op, env->cc_x, env->cc_n, env->cc_z, env->cc_v, env->cc_c);
1220 env->cc_op = CC_OP_FLAGS;
1223 uint32_t HELPER(get_macf)(CPUM68KState *env, uint64_t val)
1228 if (env->macsr & MACSR_SU) {
1229 /* 16-bit rounding. */
1230 rem = val & 0xffffff;
1231 val = (val >> 24) & 0xffffu;
1234 else if (rem == 0x800000)
1236 } else if (env->macsr & MACSR_RT) {
1237 /* 32-bit rounding. */
1242 else if (rem == 0x80)
1248 if (env->macsr & MACSR_OMC) {
1250 if (env->macsr & MACSR_SU) {
1251 if (val != (uint16_t) val) {
1252 result = ((val >> 63) ^ 0x7fff) & 0xffff;
1254 result = val & 0xffff;
1257 if (val != (uint32_t)val) {
1258 result = ((uint32_t)(val >> 63) & 0x7fffffff);
1260 result = (uint32_t)val;
1264 /* No saturation. */
1265 if (env->macsr & MACSR_SU) {
1266 result = val & 0xffff;
1268 result = (uint32_t)val;
1274 uint32_t HELPER(get_macs)(uint64_t val)
1276 if (val == (int32_t)val) {
1277 return (int32_t)val;
1279 return (val >> 61) ^ ~SIGNBIT;
1283 uint32_t HELPER(get_macu)(uint64_t val)
1285 if ((val >> 32) == 0) {
1286 return (uint32_t)val;
1292 uint32_t HELPER(get_mac_extf)(CPUM68KState *env, uint32_t acc)
1295 val = env->macc[acc] & 0x00ff;
1296 val |= (env->macc[acc] >> 32) & 0xff00;
1297 val |= (env->macc[acc + 1] << 16) & 0x00ff0000;
1298 val |= (env->macc[acc + 1] >> 16) & 0xff000000;
1302 uint32_t HELPER(get_mac_exti)(CPUM68KState *env, uint32_t acc)
1305 val = (env->macc[acc] >> 32) & 0xffff;
1306 val |= (env->macc[acc + 1] >> 16) & 0xffff0000;
1310 void HELPER(set_mac_extf)(CPUM68KState *env, uint32_t val, uint32_t acc)
1314 res = env->macc[acc] & 0xffffffff00ull;
1315 tmp = (int16_t)(val & 0xff00);
1316 res |= ((int64_t)tmp) << 32;
1318 env->macc[acc] = res;
1319 res = env->macc[acc + 1] & 0xffffffff00ull;
1320 tmp = (val & 0xff000000);
1321 res |= ((int64_t)tmp) << 16;
1322 res |= (val >> 16) & 0xff;
1323 env->macc[acc + 1] = res;
1326 void HELPER(set_mac_exts)(CPUM68KState *env, uint32_t val, uint32_t acc)
1330 res = (uint32_t)env->macc[acc];
1332 res |= ((int64_t)tmp) << 32;
1333 env->macc[acc] = res;
1334 res = (uint32_t)env->macc[acc + 1];
1335 tmp = val & 0xffff0000;
1336 res |= (int64_t)tmp << 16;
1337 env->macc[acc + 1] = res;
1340 void HELPER(set_mac_extu)(CPUM68KState *env, uint32_t val, uint32_t acc)
1343 res = (uint32_t)env->macc[acc];
1344 res |= ((uint64_t)(val & 0xffff)) << 32;
1345 env->macc[acc] = res;
1346 res = (uint32_t)env->macc[acc + 1];
1347 res |= (uint64_t)(val & 0xffff0000) << 16;
1348 env->macc[acc + 1] = res;
1351 #if defined(CONFIG_SOFTMMU)
1352 void HELPER(ptest)(CPUM68KState *env, uint32_t addr, uint32_t is_read)
1358 target_ulong page_size;
1360 access_type = ACCESS_PTEST;
1362 access_type |= ACCESS_SUPER;
1364 if ((env->dfc & 3) == 2) {
1365 access_type |= ACCESS_CODE;
1368 access_type |= ACCESS_STORE;
1373 ret = get_physical_address(env, &physical, &prot, addr,
1374 access_type, &page_size);
1376 addr &= TARGET_PAGE_MASK;
1377 physical += addr & (page_size - 1);
1378 tlb_set_page(env_cpu(env), addr, physical,
1379 prot, access_type & ACCESS_SUPER ?
1380 MMU_KERNEL_IDX : MMU_USER_IDX, page_size);
1384 void HELPER(pflush)(CPUM68KState *env, uint32_t addr, uint32_t opmode)
1386 CPUState *cs = env_cpu(env);
1389 case 0: /* Flush page entry if not global */
1390 case 1: /* Flush page entry */
1391 tlb_flush_page(cs, addr);
1393 case 2: /* Flush all except global entries */
1396 case 3: /* Flush all entries */
1402 void HELPER(reset)(CPUM68KState *env)
1404 /* FIXME: reset all except CPU */