2008年12月24日水曜日

Memory Map の作り方

CPUReadMemoryFun *foo_readfn[3]  と CPUWriteMemoryFunc *foo_writefn[3]  をそれぞれ定義する。配列の中身は、それぞれ、メモリを byte/word/double word で読んだときに呼び出されるコールバック。
iomemtype = cpu_register_io_memory(io_index, foo_readfn,foo_writefn, s);
cpu_register_physical_memory(開始アドレス, サイズ, iomemtype);
io_index が 0 の時は新しい io 領域が作られる。
s は、CPU の io の状態を表現する任意の構造体。
サイズはターゲットのページサイズの倍数。
iomemtype (phys_offset) は、phys_offset & ~TARGET_PAGE_MASK) != 0 となる ( io memory page)。

qemu-0.9.1/exec.c

/* mem_read and mem_write are arrays of functions containing the
   function to access byte (index 0), word (index 1) and dword (index
   2). Functions can be omitted with a NULL function pointer. The
   registered functions may be modified dynamically later.
   If io_index is non zero, the corresponding io zone is
   modified. If it is zero, a new io zone is allocated. The return
   value can be used with cpu_register_physical_memory(). (-1) is
   returned if error. */
int cpu_register_io_memory(int io_index,
                           CPUReadMemoryFunc **mem_read,
                           CPUWriteMemoryFunc **mem_write,
                           void *opaque)
{
    int i, subwidth = 0;

    if (io_index <= 0) {
        if (io_mem_nb >= IO_MEM_NB_ENTRIES)
            return -1;
        io_index = io_mem_nb++;
    } else {
        if (io_index >= IO_MEM_NB_ENTRIES)
            return -1;
    }

    for(i = 0;i < 3; i++) {
        if (!mem_read[i] || !mem_write[i])
            subwidth = IO_MEM_SUBWIDTH;
        io_mem_read[io_index][i] = mem_read[i];
        io_mem_write[io_index][i] = mem_write[i];
    }
    io_mem_opaque[io_index] = opaque;
    return (io_index << IO_MEM_SHIFT) | subwidth;
}

qemu-0.9.1/cpu-all.h
typedef void CPUWriteMemoryFunc(void *opaque,
                                target_phys_addr_t addr,
                                uint32_t value);

typedef uint32_t CPUReadMemoryFunc(void *opaque,
                                   target_phys_addr_t addr);

int cpu_register_io_memory(int io_index,
                           CPUReadMemoryFunc **mem_read,
                           CPUWriteMemoryFunc **mem_write,
                           void *opaque);

0 件のコメント:

コメントを投稿