2 * ARMV7M System emulation.
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
12 /* Bitbanded IO. Each word corresponds to a single bit. */
14 /* Get the byte address of the real memory for a bitband acess. */
15 static inline uint32_t bitband_addr(uint32_t addr
)
19 res
= addr
& 0xe0000000;
20 res
|= (addr
& 0x1ffffff) >> 5;
25 static uint32_t bitband_readb(void *opaque
, target_phys_addr_t offset
)
28 cpu_physical_memory_read(bitband_addr(offset
), &v
, 1);
29 return (v
& (1 << ((offset
>> 2) & 7))) != 0;
32 static void bitband_writeb(void *opaque
, target_phys_addr_t offset
,
38 addr
= bitband_addr(offset
);
39 mask
= (1 << ((offset
>> 2) & 7));
40 cpu_physical_memory_read(addr
, &v
, 1);
45 cpu_physical_memory_write(addr
, &v
, 1);
48 static uint32_t bitband_readw(void *opaque
, target_phys_addr_t offset
)
53 addr
= bitband_addr(offset
) & ~1;
54 mask
= (1 << ((offset
>> 2) & 15));
56 cpu_physical_memory_read(addr
, (uint8_t *)&v
, 2);
57 return (v
& mask
) != 0;
60 static void bitband_writew(void *opaque
, target_phys_addr_t offset
,
66 addr
= bitband_addr(offset
) & ~1;
67 mask
= (1 << ((offset
>> 2) & 15));
69 cpu_physical_memory_read(addr
, (uint8_t *)&v
, 2);
74 cpu_physical_memory_write(addr
, (uint8_t *)&v
, 2);
77 static uint32_t bitband_readl(void *opaque
, target_phys_addr_t offset
)
82 addr
= bitband_addr(offset
) & ~3;
83 mask
= (1 << ((offset
>> 2) & 31));
85 cpu_physical_memory_read(addr
, (uint8_t *)&v
, 4);
86 return (v
& mask
) != 0;
89 static void bitband_writel(void *opaque
, target_phys_addr_t offset
,
95 addr
= bitband_addr(offset
) & ~3;
96 mask
= (1 << ((offset
>> 2) & 31));
98 cpu_physical_memory_read(addr
, (uint8_t *)&v
, 4);
103 cpu_physical_memory_write(addr
, (uint8_t *)&v
, 4);
106 static CPUReadMemoryFunc
*bitband_readfn
[] = {
112 static CPUWriteMemoryFunc
*bitband_writefn
[] = {
118 static void armv7m_bitband_init(void)
122 iomemtype
= cpu_register_io_memory(0, bitband_readfn
, bitband_writefn
,
124 cpu_register_physical_memory(0x22000000, 0x02000000, iomemtype
);
125 cpu_register_physical_memory(0x42000000, 0x02000000, iomemtype
);
129 /* Init CPU and memory for a v7-M based board.
130 flash_size and sram_size are in kb.
131 Returns the NVIC array. */
133 qemu_irq
*armv7m_init(int flash_size
, int sram_size
,
134 const char *kernel_filename
, const char *cpu_model
)
147 cpu_model
= "cortex-m3";
148 env
= cpu_init(cpu_model
);
150 fprintf(stderr
, "Unable to find CPU definition\n");
155 /* > 32Mb SRAM gets complicated because it overlaps the bitband area.
156 We don't have proper commandline options, so allocate half of memory
157 as SRAM, up to a maximum of 32Mb, and the rest as code. */
158 if (ram_size
> (512 + 32) * 1024 * 1024)
159 ram_size
= (512 + 32) * 1024 * 1024;
160 sram_size
= (ram_size
/ 2) & TARGET_PAGE_MASK
;
161 if (sram_size
> 32 * 1024 * 1024)
162 sram_size
= 32 * 1024 * 1024;
163 code_size
= ram_size
- sram_size
;
166 /* Flash programming is done via the SCU, so pretend it is ROM. */
167 cpu_register_physical_memory(0, flash_size
, IO_MEM_ROM
);
168 cpu_register_physical_memory(0x20000000, sram_size
,
169 flash_size
+ IO_MEM_RAM
);
170 armv7m_bitband_init();
172 pic
= armv7m_nvic_init(env
);
174 image_size
= load_elf(kernel_filename
, 0, &entry
, &lowaddr
, NULL
);
175 if (image_size
< 0) {
176 image_size
= load_image(kernel_filename
, phys_ram_base
);
179 if (image_size
< 0) {
180 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
185 /* If the image was loaded at address zero then assume it is a
186 regular ROM image and perform the normal CPU reset sequence.
187 Otherwise jump directly to the entry point. */
189 env
->regs
[13] = tswap32(*(uint32_t *)phys_ram_base
);
190 pc
= tswap32(*(uint32_t *)(phys_ram_base
+ 4));
195 env
->regs
[15] = pc
& ~1;
197 /* Hack to map an additional page of ram at the top of the address
198 space. This stops qemu complaining about executing code outside RAM
199 when returning from an exception. */
200 cpu_register_physical_memory(0xfffff000, 0x1000, IO_MEM_RAM
+ ram_size
);