load mem from file
This commit is contained in:
52
src/main.c
52
src/main.c
@@ -1,9 +1,9 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "tinyriscv.h"
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "tinyriscv.h"
|
||||||
|
|
||||||
typedef enum{
|
typedef enum{
|
||||||
RESET = 0, DEFAULT = 39,
|
RESET = 0, DEFAULT = 39,
|
||||||
BLACK = 30, BRIGHT_BLACK = 90,
|
BLACK = 30, BRIGHT_BLACK = 90,
|
||||||
@@ -16,10 +16,14 @@ typedef enum{
|
|||||||
WHITE = 37, BRIGHT_WHITE = 97
|
WHITE = 37, BRIGHT_WHITE = 97
|
||||||
}Color;
|
}Color;
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
static inline void set_fg_color(const Color color){
|
static inline void set_fg_color(const Color color){
|
||||||
printf("\x1b[%dm", color);
|
printf("\x1b[%dm", color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void help(const char* message){
|
void help(const char* message){
|
||||||
if(message) printf("%s\n\n", message);
|
if(message) printf("%s\n\n", message);
|
||||||
|
|
||||||
@@ -33,7 +37,7 @@ void help(const char* message){
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void regdump(const tinyriscv_hart* hart){
|
void regdump(const tinyriscv_hart* hart){
|
||||||
printf("\n");
|
printf("\n");
|
||||||
@@ -71,6 +75,8 @@ void regdump(const tinyriscv_hart* hart){
|
|||||||
printf(" │\n\n");
|
printf(" │\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void memdump(const tinyriscv_hart* hart, uint32_t addr){
|
void memdump(const tinyriscv_hart* hart, uint32_t addr){
|
||||||
addr -= tinyriscv_MEM_OFFSET;
|
addr -= tinyriscv_MEM_OFFSET;
|
||||||
if(addr + 256 > hart->mem_size) help("memdump: Address out of memory range.");
|
if(addr + 256 > hart->mem_size) help("memdump: Address out of memory range.");
|
||||||
@@ -102,6 +108,31 @@ void memdump(const tinyriscv_hart* hart, uint32_t addr){
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void load_mem_from_file(tinyriscv_hart* hart, const char* file_path){
|
||||||
|
FILE* file = fopen(file_path, "rb");
|
||||||
|
if(!file){
|
||||||
|
perror("ERROR: Can't open file");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(file, 0, SEEK_END);
|
||||||
|
const unsigned int file_size = ftell(file);
|
||||||
|
rewind(file);
|
||||||
|
|
||||||
|
if(file_size > hart->mem_size){
|
||||||
|
printf("ERROR: [file] is bigger than allocated memory.");
|
||||||
|
fclose(file);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fread(hart->mem, 1, file_size, file);
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
int main(int argc, char** argv){
|
int main(int argc, char** argv){
|
||||||
char file_path[256] = "";
|
char file_path[256] = "";
|
||||||
size_t mem_size = 4096;
|
size_t mem_size = 4096;
|
||||||
@@ -136,19 +167,8 @@ int main(int argc, char** argv){
|
|||||||
tinyriscv_hart hart;
|
tinyriscv_hart hart;
|
||||||
hart.mem = calloc(hart.mem_size = mem_size, 1);
|
hart.mem = calloc(hart.mem_size = mem_size, 1);
|
||||||
|
|
||||||
//file handling
|
|
||||||
FILE* file = fopen(file_path, "rb");
|
|
||||||
if(!file){
|
|
||||||
perror("ERROR: Can't open file");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
//load program into memory
|
//load program into memory
|
||||||
fseek(file, 0, SEEK_END);
|
load_mem_from_file(&hart, file_path);
|
||||||
const unsigned int file_size = ftell(file);
|
|
||||||
rewind(file);
|
|
||||||
fread(hart.mem, 1, file_size, file);
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
//program execution
|
//program execution
|
||||||
tinyriscv_init(&hart);
|
tinyriscv_init(&hart);
|
||||||
@@ -160,3 +180,5 @@ int main(int argc, char** argv){
|
|||||||
//cleanup
|
//cleanup
|
||||||
free(hart.mem);
|
free(hart.mem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
Reference in New Issue
Block a user