summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c236
1 files changed, 151 insertions, 85 deletions
diff --git a/main.c b/main.c
index 40fc250..9646725 100644
--- a/main.c
+++ b/main.c
@@ -1,123 +1,189 @@
/*
- Nios-sim - one simple NIOSII simulator only for personal interest and fun.
- Copyright (C) 2010 chysun2000@gmail.com
-
- 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.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
+ * nios2sim-ng -- Nios II Simulator (Next Generation)
+ *
+ * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
+ *
+ * Based on Nios-sim, which is:
+ * Copyright (C) 2010 chysun2000@gmail.com
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
-#include "public.h"
+#include <errno.h>
+
+#include "nios2sim-ng.h"
+#include "image.h"
-static void print_help(void)
+#define PROGRAM_NAME "nios2sim-ng"
+#define PROGRAM_VERSION VERSION /* Set in Makefile */
+
+#define DEFAULT_MEM_SIZE (16 * 1024 * 1024)
+
+static void usage(const int status)
{
- printf("\n----------------------------------------------\n");
- printf(" HELP INFORMATION of NIOS2 Simulator\n");
- printf("----------------------------------------------\n");
- printf(" --image|-i image path name\n");
- printf(" --srec|-s image format is SREC\n");
- printf(" --mem_size|-m memory size of board\n");
- printf(" --debug_mode|-d enter debug mode\n");
- printf(" --base_addr|-b base address of memory\n");
- printf(" --sys_map|-p load symbol table\n");
- printf(" --cmdline|-c set kernel command line\n");
- printf(" --initrd|-r set the address of initrd\n");
- printf(" --fs_image|-f set the file system image\n");
- printf(" --version|-v print the version information\n");
- printf("----------------------------------------------\n");
- printf(" For Example:\n");
- printf(" nios-sim -s -i [srec file] -m [memsize] -d [set to debug mode] \n");
- printf(" -b [base addr of mem] -p [symbol file]\n");
- printf(" -c [kernel command line]\n");
- printf("----------------------------------------------\n");
+ fprintf(stdout, "Usage: %s [OPTION...] IMAGE\n"
+ " -c CMDLINE, --cmdline=CMDLINE\n"
+ " specify kernel command line\n"
+ " -m MEMSIZE, --memsize=MEMSIZE\n"
+ " set memory size for simulator (default %zu%s)\n"
+ " -d, --debug enable debug mode\n"
+ " -e, --elf image is in ELF format (default)\n"
+ " -s, --srec image is in SREC format\n"
+ " -h, --help print this help and exit\n"
+ " -V, --version print version information and exit\n"
+ "\n"
+ "Example:\n"
+ " %s -s -m 32M -d -c console=ttyJ0 init=/bin/sh\n"
+ "",
+ PROGRAM_NAME, size_scale(DEFAULT_MEM_SIZE),
+ size_postfix(DEFAULT_MEM_SIZE), PROGRAM_NAME);
+
+ exit(status);
}
+static const struct option long_opts[] = {
+ { "elf", no_argument, NULL, 'e' },
+ { "srec", no_argument, NULL, 's' },
+ { "mem_size", required_argument, NULL, 'm' },
+ { "debug", no_argument, NULL, 'd' },
+ { "base_addr", required_argument, NULL, 'b' },
+ { "sys_map", required_argument, NULL, 'p' },
+ { "cmdline", required_argument, NULL, 'c' },
+ { "initrd", required_argument, NULL, 'r' },
+ { "fs_image", required_argument, NULL, 'f' },
+ { "version", no_argument, NULL, 'V' },
+ { "help", no_argument, NULL, 'h' },
+ { NULL, 0, NULL, 0 }
+};
-static const char * version = "Simulator for NIOSII(None-MMU) Version 0.1\n Copyright@chysun2000@gmail.com\n";
-static void print_version(void)
+static const char *short_opts = "sm:db:p:c:r:f:Vh";
+
+static size_t parse_mem_size(char *opt)
{
- printf("\n---------------------------------------------\n");
- printf("%s", version);
- printf("---------------------------------------------\n");
+ size_t len = strlen(opt);
+ size_t mem_size;
+ size_t mul = 1;
+
+ switch (opt[len - 1]) {
+ case 'k':
+ case 'K':
+ mul = 1024;
+ opt[len - 1] = '\0';
+ break;
+ case 'm':
+ case 'M':
+ mul = 1024 * 1024;
+ opt[len - 1] = '\0';
+ break;
+ }
+
+ errno = 0;
+ mem_size = strtoul(optarg, NULL, 0);
+ if (errno != 0) {
+ err("Invalid memory size: %s\n", opt);
+ exit(EXIT_FAILURE);
+ }
+
+ mem_size *= mul;
+
+ return round_up(mem_size, 4);
}
-int main(int argc, char * argv[])
+int main(int argc, char *argv[])
{
- struct option long_opt [] = {
- {"image",required_argument,NULL,'i'},
- {"srec",no_argument, NULL, 's'},
- {"mem_size",required_argument, NULL, 'm'},
- {"debug_mode",no_argument, NULL, 'd'},
- {"base_addr", required_argument, NULL, 'b'},
- {"sys_map", required_argument, NULL, 'p'},
- {"cmdline", required_argument, NULL, 'c'},
- {"initrd", required_argument, NULL, 'r'},
- {"fs_image",required_argument, NULL, 'f'},
- {"version", no_argument, NULL, 'v'},
- {"help", no_argument, NULL, 'h'},
- {0,0,0,0}
- };
-
- char * short_opt = "i:sm:db:p:c:vhr:f:";
- int32_t c = 0;
-
- while((c = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1){
- switch(c){
- case 'i':
- set_image_pathname(optarg);
+ char *image_path;
+ char *cmdline = "";
+ int image_format = FORMAT_ELF;
+ uint8_t *mem_base;
+ size_t mem_size = DEFAULT_MEM_SIZE;
+ int c;
+
+ while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
+ switch(c) {
+ case 'e':
+ image_format = FORMAT_ELF;
break;
case 's':
- set_image_format(SREC_FMT);
+ image_format = FORMAT_SREC;
break;
case 'm':
- set_image_memsize(optarg);
+ mem_size = parse_mem_size(optarg);
break;
case 'd':
- set_debug_mode(get_nios_cpu());
+// set_debug_mode(get_nios_cpu());
break;
case 'b':
- set_image_base_addr(optarg);
+// sscanf(addr, "0x%X\n", &image_info.base_addr);
break;
case 'p':
- load_symbol_file(optarg);
+// load_symbol_file(optarg);
break;
case 'c':
- set_cmdline(optarg);
+ cmdline = optarg;
break;
case 'r':
- set_initrd(optarg);
+// set_initrd(optarg);
break;
case 'f':
- set_fs_image(optarg);
+// set_fs_image(optarg);
break;
- case 'v':
- print_version();
- return 0;
+ case 'V':
+ info("%s %s\n", PROGRAM_NAME, PROGRAM_VERSION);
+ exit(EXIT_SUCCESS);
case 'h':
- print_help();
- return 0;
- default:
+ usage(EXIT_SUCCESS);
break;
+ default:
+ usage(EXIT_FAILURE);
}
}
-
- alloc_image_mem();
- load_image();
- print_image_info();
+
+ if (optind >= argc) {
+ err("No image file specified\n");
+ exit(EXIT_FAILURE);
+ }
+
+ image_path = argv[optind];
+
+ mem_base = malloc(mem_size);
+ if (unlikely(mem_base == NULL)) {
+ err("Failed to allocate memory\n");
+ exit(EXIT_FAILURE);
+ }
+
+ if (image_load(image_path, image_format, mem_base, mem_size))
+ exit(EXIT_FAILURE);
+
+ info(" Image file: %s\n", image_path);
+ info(" Image format: %s\n", image_format_str(image_format));
+ info(" Memory size: %zu %sbytes\n", size_scale(mem_size), size_postfix(mem_size));
+ info(" Memory base: %p\n", mem_base);
+
+#if 0
+ printf("Base address:0x%08X, Entry address:0x%08X\n",image_info.base_addr,
+ image_info.entry_addr);
+ 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();
- return 0;
+#endif
+
+ exit(EXIT_SUCCESS);
}