3 #define DEBUG_IRQ_COUNT
5 #define BIOS_FILENAME "mips_bios.bin"
6 //#define BIOS_FILENAME "system.bin"
7 #define KERNEL_LOAD_ADDR 0x80010000
8 #define INITRD_LOAD_ADDR 0x80800000
10 /* MIPS R4K IRQ controler */
11 #if defined(DEBUG_IRQ_COUNT)
12 static uint64_t irq_count[16];
17 void mips_set_irq (int n_IRQ, int level)
21 if (n_IRQ < 0 || n_IRQ >= 8)
23 mask = 0x100 << n_IRQ;
27 fprintf(logfile, "%s n %d l %d mask %08x %08x\n",
28 __func__, n_IRQ, level, mask, cpu_single_env->CP0_Status);
31 cpu_single_env->CP0_Cause |= mask;
32 if ((cpu_single_env->CP0_Status & 0x00000001) &&
33 (cpu_single_env->CP0_Status & mask)) {
34 #if defined(DEBUG_IRQ_COUNT)
39 fprintf(logfile, "%s raise IRQ\n", __func__);
41 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_HARD);
44 cpu_single_env->CP0_Cause &= ~mask;
48 void pic_set_irq (int n_IRQ, int level)
50 mips_set_irq(n_IRQ + 2, level);
55 term_printf("IRQ asserted: %02x mask: %02x\n",
56 (cpu_single_env->CP0_Cause >> 8) & 0xFF,
57 (cpu_single_env->CP0_Status >> 8) & 0xFF);
62 #if !defined(DEBUG_IRQ_COUNT)
63 term_printf("irq statistic code not compiled.\n");
68 term_printf("IRQ statistics:\n");
69 for (i = 0; i < 8; i++) {
72 term_printf("%2d: %lld\n", i, count);
77 void cpu_mips_irqctrl_init (void)
82 uint32_t cpu_mips_get_random (CPUState *env)
84 uint64_t now = qemu_get_clock(vm_clock);
86 return (uint32_t)now & 0x0000000F;
89 uint32_t cpu_mips_get_count (CPUState *env)
91 return env->CP0_Count +
92 (uint32_t)muldiv64(qemu_get_clock(vm_clock),
93 100 * 1000 * 1000, ticks_per_sec);
96 static void cpu_mips_update_count (CPUState *env, uint32_t count,
103 if (count == compare)
105 now = qemu_get_clock(vm_clock);
106 next = now + muldiv64(compare - tmp, ticks_per_sec, 100 * 1000 * 1000);
111 fprintf(logfile, "%s: 0x%08llx %08x %08x => 0x%08llx\n",
112 __func__, now, count, compare, next - now);
115 /* Store new count and compare registers */
116 env->CP0_Compare = compare;
118 count - (uint32_t)muldiv64(now, 100 * 1000 * 1000, ticks_per_sec);
120 qemu_mod_timer(env->timer, next);
123 void cpu_mips_store_count (CPUState *env, uint32_t value)
125 cpu_mips_update_count(env, value, env->CP0_Compare);
128 void cpu_mips_store_compare (CPUState *env, uint32_t value)
130 cpu_mips_update_count(env, cpu_mips_get_count(env), value);
134 static void mips_timer_cb (void *opaque)
141 fprintf(logfile, "%s\n", __func__);
144 cpu_mips_update_count(env, cpu_mips_get_count(env), env->CP0_Compare);
148 void cpu_mips_clock_init (CPUState *env)
150 env->timer = qemu_new_timer(vm_clock, &mips_timer_cb, env);
151 env->CP0_Compare = 0;
152 cpu_mips_update_count(env, 1, 0);
155 static void io_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
158 fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, value);
159 cpu_outb(NULL, addr & 0xffff, value);
162 static uint32_t io_readb (void *opaque, target_phys_addr_t addr)
164 uint32_t ret = cpu_inb(NULL, addr & 0xffff);
166 fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, ret);
170 static void io_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
173 fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, value);
174 #ifdef TARGET_WORDS_BIGENDIAN
175 value = bswap16(value);
177 cpu_outw(NULL, addr & 0xffff, value);
180 static uint32_t io_readw (void *opaque, target_phys_addr_t addr)
182 uint32_t ret = cpu_inw(NULL, addr & 0xffff);
183 #ifdef TARGET_WORDS_BIGENDIAN
187 fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, ret);
191 static void io_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
194 fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, value);
195 #ifdef TARGET_WORDS_BIGENDIAN
196 value = bswap32(value);
198 cpu_outl(NULL, addr & 0xffff, value);
201 static uint32_t io_readl (void *opaque, target_phys_addr_t addr)
203 uint32_t ret = cpu_inl(NULL, addr & 0xffff);
205 #ifdef TARGET_WORDS_BIGENDIAN
209 fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, ret);
213 CPUWriteMemoryFunc *io_write[] = {
219 CPUReadMemoryFunc *io_read[] = {
225 void mips_r4k_init (int ram_size, int vga_ram_size, int boot_device,
226 DisplayState *ds, const char **fd_filename, int snapshot,
227 const char *kernel_filename, const char *kernel_cmdline,
228 const char *initrd_filename)
231 target_ulong kernel_base, kernel_size, initrd_base, initrd_size;
232 unsigned long bios_offset;
237 printf("%s: start\n", __func__);
238 linux_boot = (kernel_filename != NULL);
240 cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
241 bios_offset = ram_size + vga_ram_size;
242 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
243 printf("%s: load BIOS '%s' size %d\n", __func__, buf, BIOS_SIZE);
244 ret = load_image(buf, phys_ram_base + bios_offset);
245 if (ret != BIOS_SIZE) {
246 fprintf(stderr, "qemu: could not load MIPS bios '%s'\n", buf);
249 cpu_register_physical_memory((uint32_t)(0x1fc00000),
250 BIOS_SIZE, bios_offset | IO_MEM_ROM);
252 memcpy(phys_ram_base + 0x10000, phys_ram_base + bios_offset, BIOS_SIZE);
253 cpu_single_env->PC = 0x80010004;
255 cpu_single_env->PC = 0xBFC00004;
258 kernel_base = KERNEL_LOAD_ADDR;
259 /* now we can load the kernel */
260 kernel_size = load_image(kernel_filename, phys_ram_base + kernel_base);
261 if (kernel_size < 0) {
262 fprintf(stderr, "qemu: could not load kernel '%s'\n",
267 if (initrd_filename) {
268 initrd_base = INITRD_LOAD_ADDR;
269 initrd_size = load_image(initrd_filename,
270 phys_ram_base + initrd_base);
271 if (initrd_size < 0) {
272 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
280 cpu_single_env->PC = KERNEL_LOAD_ADDR;
287 /* XXX: should not be ! */
288 printf("%s: init VGA\n", __func__);
289 vga_initialize(NULL, ds, phys_ram_base + ram_size, ram_size,
293 /* Init internal devices */
294 cpu_mips_clock_init(cpu_single_env);
295 cpu_mips_irqctrl_init();
297 isa_mem_base = 0x78000000;
298 /* Register 64 KB of ISA IO space at random address */
299 io_memory = cpu_register_io_memory(0, io_read, io_write, NULL);
300 cpu_register_physical_memory(0x70000000, 0x00010000, io_memory);
301 serial_init(0x3f8, 4, serial_hds[0]);
302 printf("%s: done\n", __func__);
305 QEMUMachine mips_machine = {