cpu.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * QEMU AVR CPU
  3. *
  4. * Copyright (c) 2019-2020 Michael Rolnik
  5. *
  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.
  10. *
  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.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see
  18. * <http://www.gnu.org/licenses/lgpl-2.1.html>
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "qemu/qemu-print.h"
  23. #include "exec/exec-all.h"
  24. #include "cpu.h"
  25. #include "disas/dis-asm.h"
  26. static void avr_cpu_set_pc(CPUState *cs, vaddr value)
  27. {
  28. AVRCPU *cpu = AVR_CPU(cs);
  29. cpu->env.pc_w = value / 2; /* internally PC points to words */
  30. }
  31. static bool avr_cpu_has_work(CPUState *cs)
  32. {
  33. AVRCPU *cpu = AVR_CPU(cs);
  34. CPUAVRState *env = &cpu->env;
  35. return (cs->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_RESET))
  36. && cpu_interrupts_enabled(env);
  37. }
  38. static void avr_cpu_synchronize_from_tb(CPUState *cs, TranslationBlock *tb)
  39. {
  40. AVRCPU *cpu = AVR_CPU(cs);
  41. CPUAVRState *env = &cpu->env;
  42. env->pc_w = tb->pc / 2; /* internally PC points to words */
  43. }
  44. static void avr_cpu_reset(DeviceState *ds)
  45. {
  46. CPUState *cs = CPU(ds);
  47. AVRCPU *cpu = AVR_CPU(cs);
  48. AVRCPUClass *mcc = AVR_CPU_GET_CLASS(cpu);
  49. CPUAVRState *env = &cpu->env;
  50. mcc->parent_reset(ds);
  51. env->pc_w = 0;
  52. env->sregI = 1;
  53. env->sregC = 0;
  54. env->sregZ = 0;
  55. env->sregN = 0;
  56. env->sregV = 0;
  57. env->sregS = 0;
  58. env->sregH = 0;
  59. env->sregT = 0;
  60. env->rampD = 0;
  61. env->rampX = 0;
  62. env->rampY = 0;
  63. env->rampZ = 0;
  64. env->eind = 0;
  65. env->sp = 0;
  66. env->skip = 0;
  67. memset(env->r, 0, sizeof(env->r));
  68. tlb_flush(cs);
  69. }
  70. static void avr_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
  71. {
  72. info->mach = bfd_arch_avr;
  73. info->print_insn = NULL;
  74. }
  75. static void avr_cpu_realizefn(DeviceState *dev, Error **errp)
  76. {
  77. CPUState *cs = CPU(dev);
  78. AVRCPUClass *mcc = AVR_CPU_GET_CLASS(dev);
  79. Error *local_err = NULL;
  80. cpu_exec_realizefn(cs, &local_err);
  81. if (local_err != NULL) {
  82. error_propagate(errp, local_err);
  83. return;
  84. }
  85. qemu_init_vcpu(cs);
  86. cpu_reset(cs);
  87. mcc->parent_realize(dev, errp);
  88. }
  89. static void avr_cpu_set_int(void *opaque, int irq, int level)
  90. {
  91. AVRCPU *cpu = opaque;
  92. CPUAVRState *env = &cpu->env;
  93. CPUState *cs = CPU(cpu);
  94. uint64_t mask = (1ull << irq);
  95. if (level) {
  96. env->intsrc |= mask;
  97. cpu_interrupt(cs, CPU_INTERRUPT_HARD);
  98. } else {
  99. env->intsrc &= ~mask;
  100. if (env->intsrc == 0) {
  101. cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
  102. }
  103. }
  104. }
  105. static void avr_cpu_initfn(Object *obj)
  106. {
  107. AVRCPU *cpu = AVR_CPU(obj);
  108. cpu_set_cpustate_pointers(cpu);
  109. /* Set the number of interrupts supported by the CPU. */
  110. qdev_init_gpio_in(DEVICE(cpu), avr_cpu_set_int,
  111. sizeof(cpu->env.intsrc) * 8);
  112. }
  113. static ObjectClass *avr_cpu_class_by_name(const char *cpu_model)
  114. {
  115. ObjectClass *oc;
  116. oc = object_class_by_name(cpu_model);
  117. if (object_class_dynamic_cast(oc, TYPE_AVR_CPU) == NULL ||
  118. object_class_is_abstract(oc)) {
  119. oc = NULL;
  120. }
  121. return oc;
  122. }
  123. static void avr_cpu_dump_state(CPUState *cs, FILE *f, int flags)
  124. {
  125. AVRCPU *cpu = AVR_CPU(cs);
  126. CPUAVRState *env = &cpu->env;
  127. int i;
  128. qemu_fprintf(f, "\n");
  129. qemu_fprintf(f, "PC: %06x\n", env->pc_w);
  130. qemu_fprintf(f, "SP: %04x\n", env->sp);
  131. qemu_fprintf(f, "rampD: %02x\n", env->rampD >> 16);
  132. qemu_fprintf(f, "rampX: %02x\n", env->rampX >> 16);
  133. qemu_fprintf(f, "rampY: %02x\n", env->rampY >> 16);
  134. qemu_fprintf(f, "rampZ: %02x\n", env->rampZ >> 16);
  135. qemu_fprintf(f, "EIND: %02x\n", env->eind >> 16);
  136. qemu_fprintf(f, "X: %02x%02x\n", env->r[27], env->r[26]);
  137. qemu_fprintf(f, "Y: %02x%02x\n", env->r[29], env->r[28]);
  138. qemu_fprintf(f, "Z: %02x%02x\n", env->r[31], env->r[30]);
  139. qemu_fprintf(f, "SREG: [ %c %c %c %c %c %c %c %c ]\n",
  140. env->sregI ? 'I' : '-',
  141. env->sregT ? 'T' : '-',
  142. env->sregH ? 'H' : '-',
  143. env->sregS ? 'S' : '-',
  144. env->sregV ? 'V' : '-',
  145. env->sregN ? '-' : 'N', /* Zf has negative logic */
  146. env->sregZ ? 'Z' : '-',
  147. env->sregC ? 'I' : '-');
  148. qemu_fprintf(f, "SKIP: %02x\n", env->skip);
  149. qemu_fprintf(f, "\n");
  150. for (i = 0; i < ARRAY_SIZE(env->r); i++) {
  151. qemu_fprintf(f, "R[%02d]: %02x ", i, env->r[i]);
  152. if ((i % 8) == 7) {
  153. qemu_fprintf(f, "\n");
  154. }
  155. }
  156. qemu_fprintf(f, "\n");
  157. }
  158. static void avr_cpu_class_init(ObjectClass *oc, void *data)
  159. {
  160. DeviceClass *dc = DEVICE_CLASS(oc);
  161. CPUClass *cc = CPU_CLASS(oc);
  162. AVRCPUClass *mcc = AVR_CPU_CLASS(oc);
  163. mcc->parent_realize = dc->realize;
  164. dc->realize = avr_cpu_realizefn;
  165. device_class_set_parent_reset(dc, avr_cpu_reset, &mcc->parent_reset);
  166. cc->class_by_name = avr_cpu_class_by_name;
  167. cc->has_work = avr_cpu_has_work;
  168. cc->do_interrupt = avr_cpu_do_interrupt;
  169. cc->cpu_exec_interrupt = avr_cpu_exec_interrupt;
  170. cc->dump_state = avr_cpu_dump_state;
  171. cc->set_pc = avr_cpu_set_pc;
  172. cc->memory_rw_debug = avr_cpu_memory_rw_debug;
  173. cc->get_phys_page_debug = avr_cpu_get_phys_page_debug;
  174. cc->tlb_fill = avr_cpu_tlb_fill;
  175. cc->vmsd = &vms_avr_cpu;
  176. cc->disas_set_info = avr_cpu_disas_set_info;
  177. cc->tcg_initialize = avr_cpu_tcg_init;
  178. cc->synchronize_from_tb = avr_cpu_synchronize_from_tb;
  179. cc->gdb_read_register = avr_cpu_gdb_read_register;
  180. cc->gdb_write_register = avr_cpu_gdb_write_register;
  181. cc->gdb_num_core_regs = 35;
  182. cc->gdb_core_xml_file = "avr-cpu.xml";
  183. }