command line arguments
This commit is contained in:
57
src/main.c
57
src/main.c
@@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "tinyriscv.h"
|
||||
|
||||
void regdump(const trv_cpu* cpu){
|
||||
@@ -17,48 +18,60 @@ void help(void){
|
||||
printf(" --help Display this information.\n");
|
||||
printf(" --bin <file> Load <file> into memory.\n");
|
||||
printf(" --mem_size <size> Set memory to have <size> bytes.\n");
|
||||
printf(" --regdump Dump registers at execution halt.\n");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv){
|
||||
|
||||
char file_path[256] = "";
|
||||
size_t mem_size = 4096;
|
||||
unsigned int do_regdump = 0;
|
||||
|
||||
for(unsigned int i = 0; i < argc; i++){
|
||||
if(!strcmp(argv[i], "--help")) help();
|
||||
|
||||
else if(!strcmp(argv[i], "--mem_size")){
|
||||
if(!argv[i + 1]){
|
||||
printf("")
|
||||
}
|
||||
} mem_size = atoi(argv[i + 1]);
|
||||
}
|
||||
|
||||
//command line arguments
|
||||
if(argc < 2){
|
||||
//printf("USAGE: tinyricv <file_path>\n");
|
||||
exit(0);
|
||||
printf("Not enough arguments.\n");
|
||||
help();
|
||||
}
|
||||
|
||||
FILE* file = fopen(argv[1], "rb");
|
||||
if(!file){
|
||||
perror("ERROR: Can't open file: ");
|
||||
exit(-1);
|
||||
for(unsigned int i = 1; i < argc; i++){
|
||||
if(!strcmp(argv[i], "--help")) help();
|
||||
else if(!strcmp(argv[i], "--bin")){
|
||||
assert("Invalid file path." && argv[i + 1]);
|
||||
strncpy(file_path, argv[i++ + 1], 256);
|
||||
}
|
||||
else if(!strcmp(argv[i], "--mem_size")){
|
||||
assert("Invalid [mem_size] value." && argv[i + 1]);
|
||||
mem_size = atoi(argv[i++ + 1]);
|
||||
}
|
||||
else if(!strcmp(argv[i], "--regdump")) do_regdump = 1;
|
||||
else{
|
||||
printf("Invalid argument: %s\n", argv[i]);
|
||||
help();
|
||||
}
|
||||
}
|
||||
|
||||
trv_cpu cpu;
|
||||
cpu.mem = malloc(4096);
|
||||
cpu.mem = malloc(mem_size);
|
||||
|
||||
//file handling
|
||||
FILE* file = fopen(file_path, "rb");
|
||||
if(!file){
|
||||
perror("ERROR: Can't open file");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
//load program into memory
|
||||
fseek(file, 0, SEEK_END);
|
||||
unsigned int file_size = ftell(file);
|
||||
const unsigned int file_size = ftell(file);
|
||||
rewind(file);
|
||||
fread(cpu.mem, 1, file_size, file);
|
||||
fclose(file);
|
||||
|
||||
//program execution
|
||||
trv_init(&cpu);
|
||||
|
||||
while(cpu.pc < file_size + trv_MEM_OFFSET) trv_step(&cpu);
|
||||
|
||||
regdump(&cpu);
|
||||
//cleanup
|
||||
if(do_regdump) regdump(&cpu);
|
||||
free(cpu.mem);
|
||||
}
|
||||
Reference in New Issue
Block a user