Skip to content

Commit

Permalink
can now process strings
Browse files Browse the repository at this point in the history
  • Loading branch information
billmcspadden-riscv committed Aug 5, 2022
1 parent 9c1341c commit b50c4ae
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 56 deletions.
52 changes: 30 additions & 22 deletions c_emulator/riscv_sim.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@
struct tv_spike_t;
#endif

const char *RV64ISA = "RV64IMAC";
const char *RV32ISA = "RV32IMAC";
//const char *RV64ISA = "RV64IMAC";
//const char *RV32ISA = "RV32IMAC";

char *RV64ISA = "";
char *RV32ISA = "";

/* Selected CSRs from riscv-isa-sim/riscv/encoding.h */
#define CSR_STVEC 0x105
Expand Down Expand Up @@ -270,13 +273,15 @@ char *process_args(int argc, char **argv)
fprintf(stderr, "unable to build fast-yaml document from %s\n", optarg);
exit(1);
}
rv_cfg_dump_yaml_c(optarg);
break;

case 'u':
if ( rv_cfg_build_from_file(RV_CFG_ISA, optarg) == 0 ) {
fprintf(stderr, "unable to build fast-yaml document from %s\n", optarg);
exit(1);
}
rv_cfg_dump_yaml_c(optarg);
break;
case 'a':
report_arch();
Expand Down Expand Up @@ -507,28 +512,28 @@ void init_sail_reset_vector(uint64_t entry)
// into the appropriate place in memory?

#define RST_VEC_SIZE 8
uint32_t reset_vec[RST_VEC_SIZE] = {
0x297, // auipc t0,0x0
0x28593 + (RST_VEC_SIZE * 4 << 20), // addi a1, t0, &dtb
0xf1402573, // csrr a0, mhartid
is_32bit_model() ?
0x0182a283u : // lw t0,24(t0)
0x0182b283u, // ld t0,24(t0)
0x28067, // jr t0
0,
(uint32_t) (entry & 0xffffffff),
(uint32_t) (entry >> 32)
};
// uint32_t reset_vec[RST_VEC_SIZE] = {
// 0x297, // auipc t0,0x0
// 0x28593 + (RST_VEC_SIZE * 4 << 20), // addi a1, t0, &dtb
// 0xf1402573, // csrr a0, mhartid
// is_32bit_model() ?
// 0x0182a283u : // lw t0,24(t0)
// 0x0182b283u, // ld t0,24(t0)
// 0x28067, // jr t0
// 0,
// (uint32_t) (entry & 0xffffffff),
// (uint32_t) (entry >> 32)
// };

rv_rom_base = DEFAULT_RSTVEC;
uint64_t addr = rv_rom_base;
for (int i = 0; i < sizeof(reset_vec); i++)
write_mem(addr++, (uint64_t)((char *)reset_vec)[i]);

if (dtb && dtb_len) {
for (size_t i = 0; i < dtb_len; i++)
write_mem(addr++, dtb[i]);
}
// for (int i = 0; i < sizeof(reset_vec); i++)
// write_mem(addr++, (uint64_t)((char *)reset_vec)[i]);
//
// if (dtb && dtb_len) {
// for (size_t i = 0; i < dtb_len; i++)
// write_mem(addr++, dtb[i]);
// }

#ifdef ENABLE_SPIKE
if (dtb && dtb_len) {
Expand Down Expand Up @@ -562,7 +567,9 @@ void init_sail_reset_vector(uint64_t entry)
/* set rom size */
rv_rom_size = rom_end - rv_rom_base;
/* boot at reset vector */
zPC = rv_rom_base;
// zPC = rv_rom_base;
zPC = rv_cfg_int_c("/reset/address");
printf("%s, %d: reset address: 0x%lx\n", __FILE__, __LINE__, zPC);
}

void preinit_sail()
Expand Down Expand Up @@ -1019,6 +1026,7 @@ int main(int argc, char **argv)

char *file = process_args(argc, argv);
init_logs();
rv_cfg_configure_c();

if (gettimeofday(&init_start, NULL) < 0) {
fprintf(stderr, "Cannot gettimeofday: %s\n", strerror(errno));
Expand Down
85 changes: 80 additions & 5 deletions c_emulator/rv_cfg_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ rv_cfg_init()

}

// ============================================================================

extern char * RV32ISA; // TODO: This needs to be in a header file.

int
rv_cfg_configure_c()
{
RV32ISA = rv_cfg_string_c("/hart0/ISA");
printf("%s, %d: RV32ISA: '%s'\n", __FILE__, __LINE__, RV32ISA);
}

// ============================================================================
char *
rv_cfg_get_string(rv_cfg_e e)
Expand Down Expand Up @@ -149,8 +160,66 @@ rv_cfg_int_c(char * key_str)
return(0); // Never taken.
}

// ============================================================================
char *
rv_cfg_string_c(char * key_str)
{
struct fy_document *fyd = NULL;
//char * yaml_val_string;
char yaml_val_string[1024];
int count;
char *tmp_str;
char * ret_str_ptr;


for (int i = 0; i < RV_CFG_LAST; i++)
{
char *conversion_str;
if ( (fyd = rv_cfg_enum2doc_a[i].fyd) == NULL)
{
continue;
}

printf("%s, %d: checking yaml file, %s\n", __FILE__, __LINE__, rv_cfg_enum2doc_a[i].filename);
conversion_str = " %1023s";
tmp_str = malloc(strlen(key_str) + strlen(conversion_str) + 1);
strcpy(tmp_str, key_str);
strcat(tmp_str, conversion_str);
printf("%s, %d: scanf string: '%s'\n", __FILE__, __LINE__, tmp_str);

count = fy_document_scanf(fyd, tmp_str, yaml_val_string);
if (count == 1)
{
free(tmp_str);
printf("%s, %d: value found, '%s', in %s\n", __FILE__, __LINE__, yaml_val_string, rv_cfg_enum2doc_a[i].filename);
ret_str_ptr = malloc(strlen(yaml_val_string)); // TODO: where should this be freed?
strcpy(ret_str_ptr, yaml_val_string);
//fy_emit_document_to_fp(fyd, FYECF_DEFAULT | FYECF_SORT_KEYS, stdout);
return(yaml_val_string);
}
free(tmp_str);
}

// If we've gotten to this point, we've gone through each of the
// rv_cfg files and didn't find the pattern OR the pattern
// didn't match the conversion string. In either case, there is
// something wrong.

fprintf(stderr, "%s, %d: the key, '%s', was not found.\n", __FILE__, __LINE__, key_str);
exit(1);
return(0); // Never taken.
}

// ============================================================================
void
rv_cfg_dump_yaml_c(char *yaml_filename)
{
struct fy_document *fyd = NULL;

fyd = fy_document_build_from_file(NULL, yaml_filename);
fy_emit_document_to_fp(fyd, FYECF_DEFAULT | FYECF_SORT_KEYS, stdout);
free(fyd);
}

// ============================================================================
int
Expand All @@ -159,16 +228,22 @@ rv_cfg_int(sail_int *zret_int, char * yaml_key_str)
printf("%s, %d: entering rv_cfg_int()\n", __FILE__, __LINE__);
mpz_set_ui(*zret_int, rv_cfg_int_c(yaml_key_str));
return(1);
}

// ============================================================================
int
rv_cfg_string(sail_string **s, char * yaml_key_str)
{
printf("%s, %d: entering rv_cfg_string()\n", __FILE__, __LINE__);
*s = rv_cfg_string_c(yaml_key_str);
return(1);
}



unit
rv_cfg_dump_yaml(char *yaml_filename)
{
struct fy_document *fyd = NULL;

fyd = fy_document_build_from_file(NULL, yaml_filename);
fy_emit_document_to_fp(fyd, FYECF_DEFAULT | FYECF_SORT_KEYS, stdout);
free(fyd);
rv_cfg_dump_yaml_c(yaml_filename);
}

6 changes: 6 additions & 0 deletions c_emulator/rv_cfg_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ char * rv_cfg_get_string(rv_cfg_e e);
rv_cfg_e rv_cfg_get_enum(char * s);
int rv_cfg_build_from_file(rv_cfg_e rv_cfg_type, char * filename );
unsigned int rv_cfg_int_c(char *);
char * rv_cfg_string_c(char *);
bool rv_cfg_bool_c(char *);
int rv_cfg_configure_c(void);
void rv_cfg_dump_yaml_c(char *);



Expand All @@ -64,6 +68,8 @@ unsigned int rv_cfg_int_c(char *);
//INT_RET_TYPE rv_cfg_int(sail_int *, char *, unit); // TODO: find out why unit is sometimes required, sometimes not INT_RET_TYPE rv_cfg_int(sail_int *, char *);

int rv_cfg_int(sail_int *, char *);
int rv_cfg_string(sail_string **, char *);
int rv_cfg_bool(sail_string *, char *);
unit rv_cfg_dump_yaml(char *);

//#endif
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ DUMP := $(subst .elf,.dump,${TARGET})
ASM_SRC := $(wildcard *.S *.s)
ASM_OBJS := $(subst .S,.o,${ASM_SRC})
ASM_FLAGS := -DCONFIG_BASE=${CONFIG_BASE} ${MARCH}
OBJDUMPFLAGS = -t -Dz
OBJDUMPFLAGS = -t -Dz -Mnumeric

C_SRC := $(wildcard *.c)
C_OBJS := $(subst .c,.o,${C_SRC})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ SECTIONS

/* text: test code section */
/*. = 0x00000040; */
/*. = 0x00001000; */
. = 0x00001000;
/*
. = 0x80000000;
.text.init : { *(.text.init) }
*/
.rotext.init : { *(.rotext.init) }

/*
making the symbol, reset_vector, a global, put the
reset vector at 0x80000000
*/
. = 0x80000000;
.text.begin . : { *(.text.begin) }

.text : { *(.text) }

/* data segment */
/*. = 0x00080000; */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ nmi:
label: nmi_vector
reset:
# label: reset_vector
address: 0x1000
# address: 0x1000
address: 0x80000000
mtime:
implemented: True
address: 0x20000
27 changes: 22 additions & 5 deletions cookbook/functional_code_examples/platform_configuration/test.S
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,37 @@
// Following power-on reset, we start executing at _start.
// We jump to "reset_vector"
//
.section ".text.init"
// .section ".text.init"
.section ".rotext.init"
.globl _start
_start:
la x5, reset_vector
jr x5
// --------------------------------------------------------
// The following is a re-write of the "rom" code that is
// in riscv_sim.c (around lines 509-521). I want to get
// rid of that bit of code in riscv_sim.c

// auipc t0, 0x0
auipc x5, 0x0
// addi a1, t0, &dtb // What is dtb???
addi x11, x5, (8 * 4) // What is the purpose of this instruction?
// csrr a0, mhartid
csrr x10, mhartid
// LREG t0, 24(t0)
la t0, reset_vector
jr t0



.section ".text"
// --------------------------------------------------------


// .section ".text"
.section ".text.begin"
.global reset_vector // This pushed the symbol, reset_vector, to 0x80000000
// --------------------------------------------------------
// Initialization of the processor, starting with the
// register file.
reset_vector:

li x1, 0
li x2, 0
li x3, 0
Expand Down
19 changes: 0 additions & 19 deletions make.csim.out

This file was deleted.

0 comments on commit b50c4ae

Please sign in to comment.