diff --git a/am/src/platform/nemu/trm.c b/am/src/platform/nemu/trm.c index f1802aa..d4a4370 100644 --- a/am/src/platform/nemu/trm.c +++ b/am/src/platform/nemu/trm.c @@ -5,10 +5,7 @@ extern char _heap_start; int main(const char *args); Area heap = RANGE(&_heap_start, PMEM_END); -#ifndef MAINARGS -#define MAINARGS "" -#endif -static const char mainargs[] = MAINARGS; +static const char mainargs[MAINARGS_MAX_LEN] = MAINARGS_PLACEHOLDER; // defined in CFLAGS void putch(char ch) { outb(SERIAL_PORT, ch); diff --git a/am/src/riscv/npc/trm.c b/am/src/riscv/npc/trm.c index 0efe6e7..f805217 100644 --- a/am/src/riscv/npc/trm.c +++ b/am/src/riscv/npc/trm.c @@ -9,10 +9,7 @@ extern char _pmem_start; #define PMEM_END ((uintptr_t)&_pmem_start + PMEM_SIZE) Area heap = RANGE(&_heap_start, PMEM_END); -#ifndef MAINARGS -#define MAINARGS "" -#endif -static const char mainargs[] = MAINARGS; +static const char mainargs[MAINARGS_MAX_LEN] = MAINARGS_PLACEHOLDER; // defined in CFLAGS void putch(char ch) { } diff --git a/scripts/platform/nemu.mk b/scripts/platform/nemu.mk index 5536816..6f1a061 100644 --- a/scripts/platform/nemu.mk +++ b/scripts/platform/nemu.mk @@ -8,22 +8,28 @@ AM_SRCS := platform/nemu/trm.c \ platform/nemu/mpe.c CFLAGS += -fdata-sections -ffunction-sections +CFLAGS += -I$(AM_HOME)/am/src/platform/nemu/include LDFLAGS += -T $(AM_HOME)/scripts/linker.ld \ --defsym=_pmem_start=0x80000000 --defsym=_entry_offset=0x0 LDFLAGS += --gc-sections -e _start NEMUFLAGS += -l $(shell dirname $(IMAGE).elf)/nemu-log.txt -CFLAGS += -DMAINARGS=\"$(mainargs)\" -CFLAGS += -I$(AM_HOME)/am/src/platform/nemu/include -.PHONY: $(AM_HOME)/am/src/platform/nemu/trm.c +MAINARGS_MAX_LEN = 64 +MAINARGS_PLACEHOLDER = The insert-arg rule in Makefile will insert mainargs here. +CFLAGS += -DMAINARGS_MAX_LEN=$(MAINARGS_MAX_LEN) -DMAINARGS_PLACEHOLDER=\""$(MAINARGS_PLACEHOLDER)"\" + +insert-arg: image + @python $(AM_HOME)/tools/insert-arg.py $(IMAGE).bin $(MAINARGS_MAX_LEN) "$(MAINARGS_PLACEHOLDER)" "$(mainargs)" image: image-dep @$(OBJDUMP) -d $(IMAGE).elf > $(IMAGE).txt @echo + OBJCOPY "->" $(IMAGE_REL).bin @$(OBJCOPY) -S --set-section-flags .bss=alloc,contents -O binary $(IMAGE).elf $(IMAGE).bin -run: image +run: insert-arg $(MAKE) -C $(NEMU_HOME) ISA=$(ISA) run ARGS="$(NEMUFLAGS)" IMG=$(IMAGE).bin -gdb: image +gdb: insert-arg $(MAKE) -C $(NEMU_HOME) ISA=$(ISA) gdb ARGS="$(NEMUFLAGS)" IMG=$(IMAGE).bin + +.PHONY: insert-arg diff --git a/scripts/platform/npc.mk b/scripts/platform/npc.mk index 6cbc7f8..ea8a34d 100644 --- a/scripts/platform/npc.mk +++ b/scripts/platform/npc.mk @@ -12,10 +12,20 @@ CFLAGS += -fdata-sections -ffunction-sections LDFLAGS += -T $(AM_HOME)/scripts/linker.ld \ --defsym=_pmem_start=0x80000000 --defsym=_entry_offset=0x0 LDFLAGS += --gc-sections -e _start -CFLAGS += -DMAINARGS=\"$(mainargs)\" -.PHONY: $(AM_HOME)/am/src/riscv/npc/trm.c + +MAINARGS_MAX_LEN = 64 +MAINARGS_PLACEHOLDER = The insert-arg rule in Makefile will insert mainargs here. +CFLAGS += -DMAINARGS_MAX_LEN=$(MAINARGS_MAX_LEN) -DMAINARGS_PLACEHOLDER=\""$(MAINARGS_PLACEHOLDER)"\" + +insert-arg: image + @python $(AM_HOME)/tools/insert-arg.py $(IMAGE).bin $(MAINARGS_MAX_LEN) "$(MAINARGS_PLACEHOLDER)" "$(mainargs)" image: image-dep @$(OBJDUMP) -d $(IMAGE).elf > $(IMAGE).txt @echo + OBJCOPY "->" $(IMAGE_REL).bin @$(OBJCOPY) -S --set-section-flags .bss=alloc,contents -O binary $(IMAGE).elf $(IMAGE).bin + +run: insert-arg + echo "TODO: add command here to run simulation" + +.PHONY: insert-arg diff --git a/tools/insert-arg.py b/tools/insert-arg.py new file mode 100644 index 0000000..6f39d76 --- /dev/null +++ b/tools/insert-arg.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +from sys import argv + +bin = argv[1] +max_len = int(argv[2]) +placeholder = argv[3] +mainargs = argv[4] + +if len(mainargs) >= max_len: + print("Error: mainargs should not be longer than {0} bytes\n".format( max_len)) + exit(1) +print("mainargs={0}".format(mainargs)) + +fp = open(bin, 'r+b') +data = fp.read() +idx = data.find(str.encode(placeholder)) +if idx == -1: + print("Error: placeholder not found!\n") + exit(1) +fp.seek(idx) +mainargs_pad = str.encode(mainargs)+ ((max_len - len(mainargs)) * str.encode("\0")) +if len(mainargs_pad) != max_len: + print("Error: len(mainargs_pad) != max_len\n") + exit(1) +fp.write(mainargs_pad) +fp.close()