Statistics
| Branch: | Revision:

root / hw / mips_r4k.c @ f68c781c

History | View | Annotate | Download (7 kB)

1 6af0bf9c bellard
#include "vl.h"
2 6af0bf9c bellard
3 6af0bf9c bellard
#define BIOS_FILENAME "mips_bios.bin"
4 6af0bf9c bellard
//#define BIOS_FILENAME "system.bin"
5 6af0bf9c bellard
#define KERNEL_LOAD_ADDR 0x80010000
6 6af0bf9c bellard
#define INITRD_LOAD_ADDR 0x80800000
7 6af0bf9c bellard
8 6af0bf9c bellard
extern FILE *logfile;
9 6af0bf9c bellard
10 73133662 bellard
static void pic_irq_request(void *opaque, int level)
11 6af0bf9c bellard
{
12 73133662 bellard
    if (level) {
13 73133662 bellard
        cpu_single_env->CP0_Cause |= 0x00000400;
14 73133662 bellard
        cpu_interrupt(cpu_single_env, CPU_INTERRUPT_HARD);
15 6af0bf9c bellard
    } else {
16 73133662 bellard
        cpu_single_env->CP0_Cause &= ~0x00000400;
17 73133662 bellard
        cpu_reset_interrupt(cpu_single_env, CPU_INTERRUPT_HARD);
18 6af0bf9c bellard
    }
19 6af0bf9c bellard
}
20 6af0bf9c bellard
21 6af0bf9c bellard
void cpu_mips_irqctrl_init (void)
22 6af0bf9c bellard
{
23 6af0bf9c bellard
}
24 6af0bf9c bellard
25 6af0bf9c bellard
uint32_t cpu_mips_get_random (CPUState *env)
26 6af0bf9c bellard
{
27 899abcf5 bellard
    uint32_t now = qemu_get_clock(vm_clock);
28 6af0bf9c bellard
29 899abcf5 bellard
    return now % (MIPS_TLB_NB - env->CP0_Wired) + env->CP0_Wired;
30 6af0bf9c bellard
}
31 6af0bf9c bellard
32 899abcf5 bellard
/* MIPS R4K timer */
33 6af0bf9c bellard
uint32_t cpu_mips_get_count (CPUState *env)
34 6af0bf9c bellard
{
35 6af0bf9c bellard
    return env->CP0_Count +
36 6af0bf9c bellard
        (uint32_t)muldiv64(qemu_get_clock(vm_clock),
37 6af0bf9c bellard
                           100 * 1000 * 1000, ticks_per_sec);
38 6af0bf9c bellard
}
39 6af0bf9c bellard
40 6af0bf9c bellard
static void cpu_mips_update_count (CPUState *env, uint32_t count,
41 6af0bf9c bellard
                                   uint32_t compare)
42 6af0bf9c bellard
{
43 6af0bf9c bellard
    uint64_t now, next;
44 6af0bf9c bellard
    uint32_t tmp;
45 6af0bf9c bellard
    
46 6af0bf9c bellard
    tmp = count;
47 6af0bf9c bellard
    if (count == compare)
48 6af0bf9c bellard
        tmp++;
49 6af0bf9c bellard
    now = qemu_get_clock(vm_clock);
50 6af0bf9c bellard
    next = now + muldiv64(compare - tmp, ticks_per_sec, 100 * 1000 * 1000);
51 6af0bf9c bellard
    if (next == now)
52 6af0bf9c bellard
        next++;
53 6af0bf9c bellard
#if 1
54 6af0bf9c bellard
    if (logfile) {
55 6af0bf9c bellard
        fprintf(logfile, "%s: 0x%08llx %08x %08x => 0x%08llx\n",
56 6af0bf9c bellard
                __func__, now, count, compare, next - now);
57 6af0bf9c bellard
    }
58 6af0bf9c bellard
#endif
59 6af0bf9c bellard
    /* Store new count and compare registers */
60 6af0bf9c bellard
    env->CP0_Compare = compare;
61 6af0bf9c bellard
    env->CP0_Count =
62 6af0bf9c bellard
        count - (uint32_t)muldiv64(now, 100 * 1000 * 1000, ticks_per_sec);
63 6af0bf9c bellard
    /* Adjust timer */
64 6af0bf9c bellard
    qemu_mod_timer(env->timer, next);
65 6af0bf9c bellard
}
66 6af0bf9c bellard
67 6af0bf9c bellard
void cpu_mips_store_count (CPUState *env, uint32_t value)
68 6af0bf9c bellard
{
69 6af0bf9c bellard
    cpu_mips_update_count(env, value, env->CP0_Compare);
70 6af0bf9c bellard
}
71 6af0bf9c bellard
72 6af0bf9c bellard
void cpu_mips_store_compare (CPUState *env, uint32_t value)
73 6af0bf9c bellard
{
74 6af0bf9c bellard
    cpu_mips_update_count(env, cpu_mips_get_count(env), value);
75 6af0bf9c bellard
    pic_set_irq(5, 0);
76 6af0bf9c bellard
}
77 6af0bf9c bellard
78 6af0bf9c bellard
static void mips_timer_cb (void *opaque)
79 6af0bf9c bellard
{
80 6af0bf9c bellard
    CPUState *env;
81 6af0bf9c bellard
82 6af0bf9c bellard
    env = opaque;
83 6af0bf9c bellard
#if 1
84 6af0bf9c bellard
    if (logfile) {
85 6af0bf9c bellard
        fprintf(logfile, "%s\n", __func__);
86 6af0bf9c bellard
    }
87 6af0bf9c bellard
#endif
88 6af0bf9c bellard
    cpu_mips_update_count(env, cpu_mips_get_count(env), env->CP0_Compare);
89 6af0bf9c bellard
    pic_set_irq(5, 1);
90 6af0bf9c bellard
}
91 6af0bf9c bellard
92 6af0bf9c bellard
void cpu_mips_clock_init (CPUState *env)
93 6af0bf9c bellard
{
94 6af0bf9c bellard
    env->timer = qemu_new_timer(vm_clock, &mips_timer_cb, env);
95 6af0bf9c bellard
    env->CP0_Compare = 0;
96 6af0bf9c bellard
    cpu_mips_update_count(env, 1, 0);
97 6af0bf9c bellard
}
98 6af0bf9c bellard
99 6af0bf9c bellard
static void io_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
100 6af0bf9c bellard
{
101 6af0bf9c bellard
    if (logfile)
102 6af0bf9c bellard
        fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, value);
103 6af0bf9c bellard
    cpu_outb(NULL, addr & 0xffff, value);
104 6af0bf9c bellard
}
105 6af0bf9c bellard
106 6af0bf9c bellard
static uint32_t io_readb (void *opaque, target_phys_addr_t addr)
107 6af0bf9c bellard
{
108 6af0bf9c bellard
    uint32_t ret = cpu_inb(NULL, addr & 0xffff);
109 6af0bf9c bellard
    if (logfile)
110 6af0bf9c bellard
        fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, ret);
111 6af0bf9c bellard
    return ret;
112 6af0bf9c bellard
}
113 6af0bf9c bellard
114 6af0bf9c bellard
static void io_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
115 6af0bf9c bellard
{
116 6af0bf9c bellard
    if (logfile)
117 6af0bf9c bellard
        fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, value);
118 6af0bf9c bellard
#ifdef TARGET_WORDS_BIGENDIAN
119 6af0bf9c bellard
    value = bswap16(value);
120 6af0bf9c bellard
#endif
121 6af0bf9c bellard
    cpu_outw(NULL, addr & 0xffff, value);
122 6af0bf9c bellard
}
123 6af0bf9c bellard
124 6af0bf9c bellard
static uint32_t io_readw (void *opaque, target_phys_addr_t addr)
125 6af0bf9c bellard
{
126 6af0bf9c bellard
    uint32_t ret = cpu_inw(NULL, addr & 0xffff);
127 6af0bf9c bellard
#ifdef TARGET_WORDS_BIGENDIAN
128 6af0bf9c bellard
    ret = bswap16(ret);
129 6af0bf9c bellard
#endif
130 6af0bf9c bellard
    if (logfile)
131 6af0bf9c bellard
        fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, ret);
132 6af0bf9c bellard
    return ret;
133 6af0bf9c bellard
}
134 6af0bf9c bellard
135 6af0bf9c bellard
static void io_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
136 6af0bf9c bellard
{
137 6af0bf9c bellard
    if (logfile)
138 6af0bf9c bellard
        fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, value);
139 6af0bf9c bellard
#ifdef TARGET_WORDS_BIGENDIAN
140 6af0bf9c bellard
    value = bswap32(value);
141 6af0bf9c bellard
#endif
142 6af0bf9c bellard
    cpu_outl(NULL, addr & 0xffff, value);
143 6af0bf9c bellard
}
144 6af0bf9c bellard
145 6af0bf9c bellard
static uint32_t io_readl (void *opaque, target_phys_addr_t addr)
146 6af0bf9c bellard
{
147 6af0bf9c bellard
    uint32_t ret = cpu_inl(NULL, addr & 0xffff);
148 6af0bf9c bellard
149 6af0bf9c bellard
#ifdef TARGET_WORDS_BIGENDIAN
150 6af0bf9c bellard
    ret = bswap32(ret);
151 6af0bf9c bellard
#endif
152 6af0bf9c bellard
    if (logfile)
153 6af0bf9c bellard
        fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, ret);
154 6af0bf9c bellard
    return ret;
155 6af0bf9c bellard
}
156 6af0bf9c bellard
157 6af0bf9c bellard
CPUWriteMemoryFunc *io_write[] = {
158 6af0bf9c bellard
    &io_writeb,
159 6af0bf9c bellard
    &io_writew,
160 6af0bf9c bellard
    &io_writel,
161 6af0bf9c bellard
};
162 6af0bf9c bellard
163 6af0bf9c bellard
CPUReadMemoryFunc *io_read[] = {
164 6af0bf9c bellard
    &io_readb,
165 6af0bf9c bellard
    &io_readw,
166 6af0bf9c bellard
    &io_readl,
167 6af0bf9c bellard
};
168 6af0bf9c bellard
169 6af0bf9c bellard
void mips_r4k_init (int ram_size, int vga_ram_size, int boot_device,
170 6af0bf9c bellard
                    DisplayState *ds, const char **fd_filename, int snapshot,
171 6af0bf9c bellard
                    const char *kernel_filename, const char *kernel_cmdline,
172 6af0bf9c bellard
                    const char *initrd_filename)
173 6af0bf9c bellard
{
174 6af0bf9c bellard
    char buf[1024];
175 6af0bf9c bellard
    target_ulong kernel_base, kernel_size, initrd_base, initrd_size;
176 6af0bf9c bellard
    unsigned long bios_offset;
177 6af0bf9c bellard
    int io_memory;
178 6af0bf9c bellard
    int linux_boot;
179 6af0bf9c bellard
    int ret;
180 6af0bf9c bellard
181 6af0bf9c bellard
    printf("%s: start\n", __func__);
182 6af0bf9c bellard
    linux_boot = (kernel_filename != NULL);
183 6af0bf9c bellard
    /* allocate RAM */
184 6af0bf9c bellard
    cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
185 6af0bf9c bellard
    bios_offset = ram_size + vga_ram_size;
186 6af0bf9c bellard
    snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
187 6af0bf9c bellard
    printf("%s: load BIOS '%s' size %d\n", __func__, buf, BIOS_SIZE);
188 6af0bf9c bellard
    ret = load_image(buf, phys_ram_base + bios_offset);
189 6af0bf9c bellard
    if (ret != BIOS_SIZE) {
190 6af0bf9c bellard
        fprintf(stderr, "qemu: could not load MIPS bios '%s'\n", buf);
191 6af0bf9c bellard
        exit(1);
192 6af0bf9c bellard
    }
193 6af0bf9c bellard
    cpu_register_physical_memory((uint32_t)(0x1fc00000),
194 6af0bf9c bellard
                                 BIOS_SIZE, bios_offset | IO_MEM_ROM);
195 6af0bf9c bellard
#if 0
196 6af0bf9c bellard
    memcpy(phys_ram_base + 0x10000, phys_ram_base + bios_offset, BIOS_SIZE);
197 6af0bf9c bellard
    cpu_single_env->PC = 0x80010004;
198 6af0bf9c bellard
#else
199 6af0bf9c bellard
    cpu_single_env->PC = 0xBFC00004;
200 6af0bf9c bellard
#endif
201 6af0bf9c bellard
    if (linux_boot) {
202 6af0bf9c bellard
        kernel_base = KERNEL_LOAD_ADDR;
203 6af0bf9c bellard
        /* now we can load the kernel */
204 de12d636 bellard
        kernel_size = load_image(kernel_filename,
205 de12d636 bellard
                                phys_ram_base + (kernel_base - 0x80000000));
206 de12d636 bellard
        if (kernel_size == (target_ulong) -1) {
207 6af0bf9c bellard
            fprintf(stderr, "qemu: could not load kernel '%s'\n", 
208 6af0bf9c bellard
                    kernel_filename);
209 6af0bf9c bellard
            exit(1);
210 6af0bf9c bellard
        }
211 6af0bf9c bellard
        /* load initrd */
212 6af0bf9c bellard
        if (initrd_filename) {
213 6af0bf9c bellard
            initrd_base = INITRD_LOAD_ADDR;
214 6af0bf9c bellard
            initrd_size = load_image(initrd_filename,
215 6af0bf9c bellard
                                     phys_ram_base + initrd_base);
216 de12d636 bellard
            if (initrd_size == (target_ulong) -1) {
217 6af0bf9c bellard
                fprintf(stderr, "qemu: could not load initial ram disk '%s'\n", 
218 6af0bf9c bellard
                        initrd_filename);
219 6af0bf9c bellard
                exit(1);
220 6af0bf9c bellard
            }
221 6af0bf9c bellard
        } else {
222 6af0bf9c bellard
            initrd_base = 0;
223 6af0bf9c bellard
            initrd_size = 0;
224 6af0bf9c bellard
        }
225 6af0bf9c bellard
        cpu_single_env->PC = KERNEL_LOAD_ADDR;
226 6af0bf9c bellard
    } else {
227 6af0bf9c bellard
        kernel_base = 0;
228 6af0bf9c bellard
        kernel_size = 0;
229 6af0bf9c bellard
        initrd_base = 0;
230 6af0bf9c bellard
        initrd_size = 0;
231 6af0bf9c bellard
    }
232 6af0bf9c bellard
233 6af0bf9c bellard
    /* Init internal devices */
234 6af0bf9c bellard
    cpu_mips_clock_init(cpu_single_env);
235 6af0bf9c bellard
    cpu_mips_irqctrl_init();
236 6af0bf9c bellard
237 0699b548 bellard
    /* Register 64 KB of ISA IO space at 0x14000000 */
238 6af0bf9c bellard
    io_memory = cpu_register_io_memory(0, io_read, io_write, NULL);
239 0699b548 bellard
    cpu_register_physical_memory(0x14000000, 0x00010000, io_memory);
240 0699b548 bellard
    isa_mem_base = 0x10000000;
241 0699b548 bellard
242 73133662 bellard
    isa_pic = pic_init(pic_irq_request, cpu_single_env);
243 6af0bf9c bellard
    serial_init(0x3f8, 4, serial_hds[0]);
244 0699b548 bellard
    vga_initialize(NULL, ds, phys_ram_base + ram_size, ram_size, 
245 0699b548 bellard
                   vga_ram_size);
246 9827e95c bellard
247 9827e95c bellard
    isa_ne2000_init(0x300, 9, &nd_table[0]);
248 6af0bf9c bellard
}
249 6af0bf9c bellard
250 6af0bf9c bellard
QEMUMachine mips_machine = {
251 6af0bf9c bellard
    "mips",
252 6af0bf9c bellard
    "mips r4k platform",
253 6af0bf9c bellard
    mips_r4k_init,
254 6af0bf9c bellard
};