From 229407408827ed49ce2e5cb2ce6d01189832b354 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Fri, 19 Nov 2010 14:06:29 +0100 Subject: Hook up simulator --- main.c | 51 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index a08fa44..bc0136c 100644 --- a/main.c +++ b/main.c @@ -30,10 +30,10 @@ #include "nios2sim-ng.h" #include "image.h" - -/* XXX: TMP */ -#include "instruction.h" #include "nios2.h" +#include "memory.h" +#include "device.h" +#include "simulator.h" #define PROGRAM_NAME "nios2sim-ng" #define PROGRAM_VERSION VERSION /* Set in Makefile */ @@ -45,6 +45,8 @@ bool verbose = false; static void usage_and_exit(const int status) { fprintf(stdout, "Usage: %s [OPTION...] IMAGE\n" + " -b BASE, --baseaddr=BASE\n" + " specify image base address\n" " -c CMDLINE, --cmdline=CMDLINE\n" " specify kernel command line\n" " -m MEMSIZE, --memsize=MEMSIZE\n" @@ -66,6 +68,7 @@ static void usage_and_exit(const int status) exit(status); } static const struct option long_opts[] = { + { "baseaddr", required_argument, NULL, 'b' }, { "cmdline", required_argument, NULL, 'c' }, { "memsize", required_argument, NULL, 'm' }, { "mmu", no_argument, NULL, 'M' }, @@ -116,21 +119,27 @@ int main(int argc, char *argv[]) char *image_path; char *cmdline = ""; int image_format = FORMAT_ELF; - uint8_t *mem_base; - size_t mem_size = DEFAULT_MEM_SIZE; struct nios2 cpu; + struct memory mem; int c; + mem.size = DEFAULT_MEM_SIZE; + mem.image_base = IMAGE_BASE_UNINITIALIZED; + while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch(c) { + case 'b': + mem.image_base = strtoul(optarg, NULL, 0); + break; case 'c': cmdline = optarg; break; case 'm': - mem_size = parse_mem_size(optarg); + mem.size = parse_mem_size(optarg); break; case 'M': cpu.has_mmu = true; + break; case 'e': image_format = FORMAT_ELF; break; @@ -162,21 +171,34 @@ int main(int argc, char *argv[]) image_path = argv[optind]; - mem_base = malloc(mem_size); - if (unlikely(mem_base == NULL)) { + if (mem.image_base != IMAGE_BASE_UNINITIALIZED + && mem.image_base >= mem.size) { + err("Image base address points beyond memory\n"); + exit(EXIT_FAILURE); + } + + mem.base = zalloc(mem.size); + if (unlikely(mem.base == NULL)) { err("Failed to allocate memory\n"); exit(EXIT_FAILURE); } - memset(mem_base, 0x00, mem_size); + /* Load the image to memory */ + if (image_load(image_path, image_format, &mem)) + exit(EXIT_FAILURE); - if (image_load(image_path, image_format, mem_base, mem_size)) + /* Initialize devices */ + if (device_init_all() < 0) exit(EXIT_FAILURE); + /* XXX: tmp */ + mem.image_base = 0x00500000; + vinfo(" Image file: %s\n", image_path); vinfo(" Image format: %s\n", image_format_str(image_format)); - vinfo(" Memory size: %zu %sbytes\n", size_scale(mem_size), size_postfix(mem_size)); - vinfo(" Memory base: %p\n", mem_base); + vinfo(" Memory size: %zu %sbytes\n", size_scale(mem.size), size_postfix(mem.size)); + vinfo(" Memory base: %p\n", mem.base); + vinfo(" Image base: 0x%08x\n", mem.image_base); vinfo(" Kernel command line: %s\n", cmdline); #if 0 @@ -185,9 +207,10 @@ int main(int argc, char *argv[]) printf("Set command line:%s\n", cmd_line); printf("Initrd: 0x%08x size:0x%08x\n",initrd_start, initrd_size); printf("rootfs: %s \n",fs_image); - - simulating(); #endif + cpu.mem = &mem; + simulator_run(&cpu); + exit(EXIT_SUCCESS); } -- cgit v1.2.3-54-g00ecf