-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
forgot to add cfunc.c and cfunc.h to repo. bah. things compile and st…
…art to run, but the loading of ROM doesn't work.
- Loading branch information
1 parent
0ab4b85
commit 84ea6fb
Showing
5 changed files
with
309 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
// vim: set tabstop=4 shiftwidth=4 expandtab | ||
// ============================================================================ | ||
// Filename: cfunc.c | ||
// | ||
// Description: Functions to be called by Sail to get values from a yaml file. | ||
// | ||
// Author(s): Bill McSpadden ([email protected]) | ||
// | ||
// Revision: See git log | ||
// ============================================================================ | ||
|
||
#include <sail.h> | ||
#include "cfunc.h" | ||
#include "string.h" | ||
|
||
/* RISC-V Config YAML configuration support */ | ||
static char *rv_config_platform_file = NULL; | ||
static int rv_config_platform_fd = 0; | ||
static struct fy_document *rv_config_fyd_platform = NULL; | ||
|
||
static char *rv_config_isa_file = NULL; | ||
static int rv_config_isa_fd = 0; | ||
static struct fy_document *rv_config_fyd_isa = NULL; | ||
|
||
// rv_cfg_enum2string_a is built (in rv_cfg_init()) so that the | ||
// array index is the enum value. A poor man's associative array. | ||
static rv_cfg_enum2string_t rv_cfg_enum2string_a[RV_CFG_LAST]; | ||
|
||
#define EXPAND(__ENUM2STR__) { __ENUM2STR__, #__ENUM2STR__ } | ||
static rv_cfg_enum2string_t rv_cfg_enum2string_init_a[] = | ||
{ | ||
EXPAND(RV_CFG_ISA), | ||
EXPAND(RV_CFG_PLATFORM), | ||
EXPAND(RV_CFG_DEBUG), | ||
}; | ||
|
||
static rv_cfg_enum2doc_t rv_cfg_enum2doc_a[RV_CFG_LAST]; | ||
|
||
// ============================================================================ | ||
int | ||
rv_cfg_init() | ||
{ | ||
for (int i = 0; i < RV_CFG_LAST; i++) | ||
{ | ||
rv_cfg_enum2string_a[rv_cfg_enum2string_init_a[i].e].e = rv_cfg_enum2string_init_a[i].e; | ||
rv_cfg_enum2string_a[rv_cfg_enum2string_init_a[i].e].s = rv_cfg_enum2string_init_a[i].s; | ||
} | ||
|
||
} | ||
|
||
// ============================================================================ | ||
char * | ||
rv_cfg_get_string(rv_cfg_e e) | ||
{ | ||
return(rv_cfg_enum2string_a[e].s); | ||
} | ||
|
||
// ============================================================================ | ||
rv_cfg_e | ||
rv_cfg_get_enum(char * s) | ||
{ | ||
for (int i = 0; i < (sizeof(rv_cfg_enum2string_a)/sizeof(rv_cfg_enum2string_t)); i++) | ||
{ | ||
if ( strcmp(rv_cfg_enum2string_a[i].s, s) == 0 ) | ||
{ | ||
return(rv_cfg_enum2string_a[i].e); | ||
} | ||
} | ||
fprintf(stderr, "%s, %d: error: internal error. bad lookup of '%s'\n", __FILE__, __LINE__, s); | ||
exit(1); | ||
} | ||
|
||
|
||
// ============================================================================ | ||
int | ||
rv_cfg_build_from_file(rv_cfg_e rv_cfg_type, char * filename ) | ||
{ | ||
struct stat buffer; | ||
struct fy_document *fyd = NULL; | ||
|
||
printf("%s file: %s\n", rv_cfg_get_string(rv_cfg_type), filename); | ||
if ( (stat(filename, &buffer) == -1) ) | ||
{ | ||
fprintf(stderr, "%s, %d: error: file, %s, does not exist.\n", __FILE__, __LINE__, filename); | ||
return(0); | ||
} | ||
|
||
rv_cfg_enum2doc_a[rv_cfg_type].fyd = fy_document_build_from_file(NULL, filename); | ||
if (! rv_cfg_enum2doc_a[rv_cfg_type].fyd) | ||
{ | ||
fprintf(stderr, "unable to build document from %s\n", filename); | ||
return(0); | ||
} | ||
rv_cfg_enum2doc_a[rv_cfg_type].filename = filename; | ||
|
||
return (1); | ||
} | ||
|
||
|
||
// ============================================================================ | ||
unsigned int | ||
cfunc_int_c(char * key_str) | ||
{ | ||
struct fy_document *fyd = NULL; | ||
unsigned int yaml_val_int; | ||
int count; | ||
char *tmp_str; | ||
// TODO: concersion strings: also need hex, octal (perhaps binary???) | ||
|
||
|
||
for (int i = 0; i < RV_CFG_LAST; i++) | ||
{ | ||
char *conversion_str; | ||
if ( (fyd = rv_cfg_enum2doc_a[i].fyd) == NULL) | ||
{ | ||
continue; | ||
} | ||
|
||
// TODO: How to iterate through all of the conversion strings | ||
// effeciently? | ||
conversion_str = " %d"; | ||
tmp_str = malloc(strlen(key_str) + strlen(conversion_str) + 1); | ||
strcpy(tmp_str, key_str); | ||
strcat(tmp_str, conversion_str); | ||
|
||
count = fy_document_scanf(fyd, tmp_str, &yaml_val_int); | ||
if (count == 1) | ||
{ | ||
free(tmp_str); | ||
return(yaml_val_int); | ||
} | ||
free(tmp_str); | ||
|
||
conversion_str = " 0x%x"; | ||
tmp_str = malloc(strlen(key_str) + strlen(conversion_str) + 1); | ||
strcpy(tmp_str, key_str); | ||
strcat(tmp_str, conversion_str); | ||
|
||
count = fy_document_scanf(fyd, tmp_str, &yaml_val_int); | ||
if (count == 1) | ||
{ | ||
free(tmp_str); | ||
return(yaml_val_int); | ||
} | ||
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. | ||
|
||
exit(1); | ||
return(0); // Never taken. | ||
} | ||
|
||
|
||
|
||
|
||
// ============================================================================ | ||
int | ||
cfunc_int(sail_int *zret_int, char * yaml_key_str) | ||
{ | ||
mpz_set_ui(*zret_int, cfunc_int_c(yaml_key_str)); | ||
return(1); | ||
|
||
// struct fy_document *fyd = NULL; | ||
// unsigned int yaml_val_int; | ||
// int count; | ||
// char *tmp_str; | ||
// char *conversion_str = " %d"; | ||
// | ||
// tmp_str = malloc(strlen(yaml_key_str) + strlen(conversion_str)); | ||
// strcpy(tmp_str, yaml_key_str); | ||
// strcat(tmp_str, conversion_str); | ||
// | ||
// fyd = fy_document_build_from_file(NULL, yaml_filename); | ||
// if ( !fyd ) | ||
// { | ||
// fprintf(stderr, "%s, %d: error: failed to build document from yaml file, %s\n", __FILE__, __LINE__, yaml_filename); | ||
// exit(1); | ||
// } | ||
// | ||
// count = fy_document_scanf(fyd, tmp_str, &yaml_val_int); | ||
// if (count == 1) | ||
// { | ||
// mpz_set_ui(*zret_int, yaml_val_int); | ||
// } | ||
// else | ||
// { | ||
// fprintf(stderr, "%s, %d: error: value for key, %s, not found in yaml file, %s\n", __FILE__, __LINE__, yaml_key_str, yaml_filename); | ||
// // TODO: figure out a return mechanism and let caller decide on action. | ||
// exit(1); | ||
// } | ||
// | ||
// // TODO: need to de-allocate memory from fy_document_build_from_file() | ||
// free(fyd); | ||
// free(tmp_str); | ||
// | ||
// return(1); | ||
} | ||
|
||
unit | ||
cfunc_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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// vim: set tabstop=4 shiftwidth=4 expandtab | ||
// ============================================================================ | ||
// Filename: cfunc.h | ||
// | ||
// Description: Functions prototype support for cfunc | ||
// | ||
// Author(s): Bill McSpadden ([email protected]) | ||
// | ||
// Revision: See git log | ||
// ============================================================================ | ||
//#ifndef __CFUNC_H__ | ||
//#define __CFUNC_H__ | ||
// | ||
|
||
#pragma once | ||
|
||
#include "sail.h" | ||
#include <libfyaml.h> | ||
#include <sys/stat.h> | ||
|
||
// ================================================== | ||
// Function prototypes just for the C side of things. | ||
// The items in this section should only be used on | ||
// the C side of the simulator. | ||
|
||
// Keep these in linear order as they are used as indices | ||
// into an array where the index is the enum. | ||
typedef enum | ||
{ | ||
RV_CFG_ISA = 0, | ||
RV_CFG_PLATFORM = 1, | ||
RV_CFG_DEBUG = 2, | ||
RV_CFG_LAST = 3 | ||
} rv_cfg_e; | ||
|
||
typedef struct rv_cfg_enum2string_tt | ||
{ | ||
rv_cfg_e e; | ||
char * s; | ||
} rv_cfg_enum2string_t; | ||
|
||
typedef struct rv_cfg_enum2doc_tt | ||
{ | ||
rv_cfg_e e; | ||
struct fy_document * fyd; | ||
char * filename; | ||
} rv_cfg_enum2doc_t; | ||
|
||
int rv_cfg_init(void); | ||
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 cfunc_int_c(char *); | ||
|
||
|
||
|
||
// ================================================== | ||
// Function prototypes for the Sail interface | ||
|
||
// It doesn't appear that Sail does anything with the | ||
// function's return value. "return values" are done | ||
// by passing a pointer to a return value struct, which | ||
// is the first element in the function's argument list. | ||
// | ||
// TODO: make the return value of type void. | ||
|
||
//INT_RET_TYPE cfunc_int(sail_int *, char *, unit); // TODO: find out why unit is sometimes required, sometimes not INT_RET_TYPE cfunc_int(sail_int *, char *); | ||
|
||
int cfunc_int(sail_int *, char *); | ||
unit cfunc_dump_yaml(char *); | ||
|
||
//#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters