📄 hw_init.c
字号:
/* This file is part of the program psim. Copyright 1994, 1997, 2003, 2004 Andrew Cagney 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. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#ifndef _HW_INIT_C_#define _HW_INIT_C_#include "device_table.h"#include "bfd.h"#include "psim.h"/* DMA a file into memory */static intdma_file(device *me, const char *file_name, unsigned_word addr){ int count; int inc; FILE *image; char buf[1024]; /* get it open */ image = fopen(file_name, "r"); if (image == NULL) return -1; /* read it in slowly */ count = 0; while (1) { inc = fread(buf, 1, sizeof(buf), image); if (inc <= 0) break; if (device_dma_write_buffer(device_parent(me), buf, 0 /*address-space*/, addr+count, inc /*nr-bytes*/, 1 /*violate ro*/) != inc) { fclose(image); return -1; } count += inc; } /* close down again */ fclose(image); return count;}/* DEVICE file - load a file into memory DESCRIPTION Loads the entire contents of <file-name> into memory at starting at <<real-address>>. Assumes that memory exists for the load. PROPERTIES file-name = <string> Name of the file to be loaded into memory real-address = <integer> Real address at which the file is to be loaded */static voidhw_file_init_data_callback(device *me){ int count; const char *file_name = device_find_string_property(me, "file-name"); unsigned_word addr = device_find_integer_property(me, "real-address"); /* load the file */ count = dma_file(me, file_name, addr); if (count < 0) device_error(me, "Problem loading file %s\n", file_name);}static device_callbacks const hw_file_callbacks = { { NULL, hw_file_init_data_callback, }, { NULL, }, /* address */ { NULL, }, /* IO */ { NULL, }, /* DMA */ { NULL, }, /* interrupt */ { NULL, }, /* unit */};/* DEVICE data - initialize a memory location with specified data DESCRIPTION The pseudo device <<data>> provides a mechanism specifying the initialization of a small section of memory. Normally, the data would be written using a dma operation. However, for some addresses this will not result in the desired result. For instance, to initialize an address in an eeprom, instead of a simple dma of the data, a sequence of writes (and then real delays) that program the eeprom would be required. For dma write initialization, the data device will write the specified <<data>> to <<real-address>> using a normal dma. For instance write initialization, the specified <<instance>> is opened. Then a seek to the <<real-address>> is performed followed by a write of the data. Integer properties are stored using the target's endian mode. PROPERTIES data = <any-valid-property> (required) Data to be loaded into memory. The property type determines how it is loaded. real-address = <integer> (required) Start address at which the data is to be stored. instance = <string> (optional) Instance specification of the device that is to be opened so that the specified data can be written to it. EXAMPLES The examples below illustrate the two alternative mechanisms that can be used to store the value 0x12345678 at address 0xfff00c00, which is normally part of the 512k system eeprom. If the eeprom is being modeled by ram (<<memory>> device) then the standard dma initialization can be used. By convention: the data devices are uniquely identified by argumenting them with the destinations real address; and all data devices are put under the node <</openprom/init>>. | /openprom/memory@0xfff00000/reg 0xfff00000 0x80000 | /openprom/init/data@0x1000/data 0x12345678 | /openprom/init/data@0x1000/real-address 0x1000 If instead a real eeprom was being used the instance write method would instead need to be used (storing just a single byte in an eeprom requires a complex sequence of accesses). The <<real-address>> is specified as <<0x0c00>> which is the offset into the eeprom. For brevity, most of the eeprom properties have been omited. | /iobus/eeprom@0xfff00000/reg 0xfff00000 0x80000 | /openprom/init/data@0xfff00c00/real-address 0x0c00 | /openprom/init/data@0xfff00c00/data 0x12345667 | /openprom/init/data@0xfff00c00/instance /iobus/eeprom@0xfff00000/reg BUGS At present, only <<integer>> properties can be specified for an initial data value. */static voidhw_data_init_data_callback(device *me){ unsigned_word addr = device_find_integer_property(me, "real-address"); const device_property *data = device_find_property(me, "data"); const char *instance_spec = (device_find_property(me, "instance") != NULL ? device_find_string_property(me, "instance") : NULL); device_instance *instance = NULL; if (data == NULL) device_error(me, "missing property <data>\n"); if (instance_spec != NULL) instance = tree_instance(me, instance_spec); switch (data->type) { case integer_property: { unsigned_cell buf = device_find_integer_property(me, "data"); H2T(buf); if (instance == NULL) { if (device_dma_write_buffer(device_parent(me), &buf, 0 /*address-space*/, addr, sizeof(buf), /*nr-bytes*/ 1 /*violate ro*/) != sizeof(buf)) device_error(me, "Problem storing integer 0x%x at 0x%lx\n", (unsigned)buf, (unsigned long)addr); } else { if (device_instance_seek(instance, 0, addr) < 0 || device_instance_write(instance, &buf, sizeof(buf)) != sizeof(buf)) device_error(me, "Problem storing integer 0x%x at 0x%lx of instance %s\n", (unsigned)buf, (unsigned long)addr, instance_spec); } } break; default: device_error(me, "Write of this data is not yet implemented\n"); break; } if (instance != NULL) device_instance_delete(instance);}static device_callbacks const hw_data_callbacks = { { NULL, hw_data_init_data_callback, }, { NULL, }, /* address */ { NULL, }, /* IO */ { NULL, }, /* DMA */ { NULL, }, /* interrupt */ { NULL, }, /* unit */};/* DEVICE load-binary - load binary segments into memory DESCRIPTION Each loadable segment of the specified binary is loaded into memory at its required address. It is assumed that the memory at those addresses already exists. This device is normally used to load an executable into memory as part of real mode simulation. PROPERTIES file-name = <string> Name of the binary to be loaded. claim = <anything> (optional) If this property is present, the real memory that is to be used by the image being loaded will be claimed from the memory node (specified by the ihandle <</chosen/memory>>). BUGS When loading the binary the bfd virtual-address is used. It should be using the bfd load-address. *//* DEVICE map-binary - map the binary into the users address space DESCRIPTION Similar to load-binary except that memory for each segment is created before the corresponding data for the segment is loaded. This device is normally used to load an executable into a user mode simulation. PROPERTIES file-name = <string> Name of the binary to be loaded. */static voidupdate_for_binary_section(bfd *abfd, asection *the_section, PTR obj){ unsigned_word section_vma; unsigned_word section_size; access_type access; device *me = (device*)obj; /* skip the section if no memory to allocate */ if (! (bfd_get_section_flags(abfd, the_section) & SEC_ALLOC)) return; /* check/ignore any sections of size zero */ section_size = bfd_get_section_size (the_section); if (section_size == 0) return; /* find where it is to go */ section_vma = bfd_get_section_vma(abfd, the_section); DTRACE(binary, ("name=%-7s, vma=0x%.8lx, size=%6ld, flags=%3lx(%s%s%s%s%s )\n", bfd_get_section_name(abfd, the_section), (long)section_vma, (long)section_size, (long)bfd_get_section_flags(abfd, the_section), bfd_get_section_flags(abfd, the_section) & SEC_LOAD ? " LOAD" : "", bfd_get_section_flags(abfd, the_section) & SEC_CODE ? " CODE" : "", bfd_get_section_flags(abfd, the_section) & SEC_DATA ? " DATA" : "", bfd_get_section_flags(abfd, the_section) & SEC_ALLOC ? " ALLOC" : "", bfd_get_section_flags(abfd, the_section) & SEC_READONLY ? " READONLY" : "" )); /* If there is an .interp section, it means it needs a shared library interpreter. */ if (strcmp(".interp", bfd_get_section_name(abfd, the_section)) == 0) error("Shared libraries are not yet supported.\n"); /* determine the devices access */ access = access_read; if (bfd_get_section_flags(abfd, the_section) & SEC_CODE) access |= access_exec; if (!(bfd_get_section_flags(abfd, the_section) & SEC_READONLY)) access |= access_write;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -