/* * AVR emulator * * Copyright (C) 2007 Michael Buesch * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include "memory.h" #include "util.h" #include "cpu.h" #include "hardware.h" #include #include #include /* The memory (SRAM and MMIO). */ struct avr_memory memory; int register_io_write_handler(io_write_handler_t handler, enum avr_io_port virt) { uint16_t phys; if (virt >= NR_VIRT_IO_PORTS) goto unavailable; phys = io_virt_to_phys(virt); if (phys == IO_PORT_UNAVAILABLE) goto unavailable; phys -= IO_MEM_OFFSET; if (memory.io_write_handlers[phys]) goto busy; memory.io_write_handlers[phys] = handler; return 0; unavailable: fprintf(stderr, "IO-write: Tried to register handler for unavailable IO port\n"); return -EEXIST; busy: fprintf(stderr, "IO-write: Tried to register an IO handler twice\n"); return -EALREADY; } int register_io_read_handler(io_read_handler_t handler, enum avr_io_port virt) { uint16_t phys; if (virt >= NR_VIRT_IO_PORTS) goto unavailable; phys = io_virt_to_phys(virt); if (phys == IO_PORT_UNAVAILABLE) goto unavailable; phys -= IO_MEM_OFFSET; if (memory.io_read_handlers[phys]) goto busy; memory.io_read_handlers[phys] = handler; return 0; unavailable: fprintf(stderr, "IO-read: Tried to register handler for unavailable IO port\n"); return -EEXIST; busy: fprintf(stderr, "IO-read: Tried to register an IO handler twice\n"); return -EALREADY; } void memory_cleanup(void) { //TODO } int memory_initialize(void) { int err; size_t nr_io; memory.ram = active_setup.memory_data; memory.size = active_setup.memory_size; memory.io = memory.ram + IO_MEM_OFFSET; memory.sram_offset = active_setup.sram_offset; err = pthread_spin_init(&memory.io_lock, 0); if (err) { fprintf(stderr, "Failed to init the I/O spinlock\n"); return -ENOMEM; } memory.sp_offset = active_setup.sp_offset; nr_io = active_setup.sram_offset - IO_MEM_OFFSET; memory.iospace_size = nr_io; memory.io_write_handlers = malloc(sizeof(io_write_handler_t) * nr_io); if (!memory.io_write_handlers) { fprintf(stderr, "Failed to alloc IO write handlers\n"); return -ENOMEM; } memset(memory.io_write_handlers, 0, sizeof(io_write_handler_t) * nr_io); memory.io_read_handlers = malloc(sizeof(io_read_handler_t) * nr_io); if (!memory.io_read_handlers) { fprintf(stderr, "Failed to alloc IO read handlers\n"); return -ENOMEM; } memset(memory.io_read_handlers, 0, sizeof(io_read_handler_t) * nr_io); active_setup.io_setup(&memory); return 0; }