litex/build/sim: cleanup modules

This commit is contained in:
Florent Kermarrec 2017-06-28 18:01:04 +02:00
parent c3710ec139
commit 4433e2449a
4 changed files with 99 additions and 180 deletions

View File

@ -14,14 +14,13 @@ static int litex_sim_module_pads_get( struct pad_s *pads, char *name, void **sig
void *sig=NULL; void *sig=NULL;
int i; int i;
if(!pads || !name || !signal) if(!pads || !name || !signal) {
{
ret=RC_INVARG; ret=RC_INVARG;
goto out; goto out;
} }
i = 0; i = 0;
while(pads[i].name) while(pads[i].name) {
{
if(!strcmp(pads[i].name, name)) if(!strcmp(pads[i].name, name))
{ {
sig=(void*)pads[i].signal; sig=(void*)pads[i].signal;
@ -37,7 +36,7 @@ out:
static int clocker_start() static int clocker_start()
{ {
printf("Loaded !\n"); printf("[clocker] loaded\n");
return RC_OK; return RC_OK;
} }
@ -47,43 +46,41 @@ static int clocker_new(void **sess, char *args)
struct session_s *s=NULL; struct session_s *s=NULL;
if(!sess) if(!sess) {
{
ret = RC_INVARG; ret = RC_INVARG;
goto out; goto out;
} }
s=(struct session_s*)malloc(sizeof(struct session_s)); s=(struct session_s*)malloc(sizeof(struct session_s));
if(!s) if(!s) {
{
ret=RC_NOENMEM; ret=RC_NOENMEM;
goto out; goto out;
} }
memset(s, 0, sizeof(struct session_s)); memset(s, 0, sizeof(struct session_s));
out: out:
*sess=(void*)s; *sess=(void*)s;
return ret; return ret;
} }
static int clocker_add_pads(void *sess, struct pad_list_s *plist) static int clocker_add_pads(void *sess, struct pad_list_s *plist)
{ {
int ret=RC_OK; int ret = RC_OK;
struct session_s *s=(struct session_s*)sess; struct session_s *s = (struct session_s*)sess;
struct pad_s *pads; struct pad_s *pads;
if(!sess || !plist)
{ if(!sess || !plist) {
ret = RC_INVARG; ret = RC_INVARG;
goto out; goto out;
} }
pads = plist->pads; pads = plist->pads;
if(!strcmp(plist->name, "sys_clk")) if(!strcmp(plist->name, "sys_clk")) {
{
litex_sim_module_pads_get(pads, "sys_clk", (void**)&s->sys_clk); litex_sim_module_pads_get(pads, "sys_clk", (void**)&s->sys_clk);
} }
*s->sys_clk=0; *s->sys_clk=0;
out: out:
return ret; return ret;
} }
@ -106,7 +103,7 @@ static struct ext_module_s ext_mod = {
int litex_sim_ext_module_init(int (*register_module)(struct ext_module_s *)) int litex_sim_ext_module_init(int (*register_module)(struct ext_module_s *))
{ {
int ret=RC_OK; int ret = RC_OK;
ret = register_module(&ext_mod); ret = register_module(&ext_mod);
return ret; return ret;
} }

View File

@ -37,58 +37,55 @@ struct session_s {
static struct event_base *base=NULL; static struct event_base *base=NULL;
int litex_sim_module_get_args( char *args, char *arg, char **val) int litex_sim_module_get_args(char *args, char *arg, char **val)
{ {
int ret=RC_OK; int ret = RC_OK;
json_object *jsobj=NULL; json_object *jsobj = NULL;
json_object *obj=NULL; json_object *obj = NULL;
char *value=NULL; char *value = NULL;
int r; int r;
jsobj = json_tokener_parse(args); jsobj = json_tokener_parse(args);
if(NULL==jsobj) if(NULL == jsobj) {
{
fprintf(stderr, "Error parsing json arg: %s \n", args); fprintf(stderr, "Error parsing json arg: %s \n", args);
ret=RC_JSERROR; ret = RC_JSERROR;
goto out; goto out;
} }
if(!json_object_is_type(jsobj, json_type_object))
{ if(!json_object_is_type(jsobj, json_type_object)) {
fprintf(stderr, "Arg must be type object! : %s \n", args); fprintf(stderr, "Arg must be type object! : %s \n", args);
ret=RC_JSERROR; ret = RC_JSERROR;
goto out; goto out;
} }
obj=NULL; obj=NULL;
r = json_object_object_get_ex(jsobj, arg, &obj); r = json_object_object_get_ex(jsobj, arg, &obj);
if(!r) if(!r) {
{
fprintf(stderr, "Could not find object: \"%s\" (%s)\n", arg, args); fprintf(stderr, "Could not find object: \"%s\" (%s)\n", arg, args);
ret=RC_JSERROR; ret = RC_JSERROR;
goto out; goto out;
} }
value=strdup(json_object_get_string(obj)); value = strdup(json_object_get_string(obj));
out: out:
*val = value; *val = value;
return ret; return ret;
} }
static int litex_sim_module_pads_get( struct pad_s *pads, char *name, void **signal) static int litex_sim_module_pads_get(struct pad_s *pads, char *name, void **signal)
{ {
int ret; int ret;
void *sig=NULL; void *sig = NULL;
int i; int i;
if(!pads || !name || !signal) if(!pads || !name || !signal) {
{
ret=RC_INVARG; ret=RC_INVARG;
goto out; goto out;
} }
i = 0; i = 0;
while(pads[i].name) while(pads[i].name) {
{ if(!strcmp(pads[i].name, name)) {
if(!strcmp(pads[i].name, name))
{
sig=(void*)pads[i].signal; sig=(void*)pads[i].signal;
break; break;
} }
@ -102,34 +99,27 @@ out:
static int ethernet_start(void *b) static int ethernet_start(void *b)
{ {
base=(struct event_base *)b; base = (struct event_base *) b;
printf("Loaded eth %p!\n", base); printf("[ethernet] loaded (%p)\n", base);
return RC_OK; return RC_OK;
} }
void event_handler(int fd, short event, void *arg) void event_handler(int fd, short event, void *arg)
{ {
struct session_s *s=(struct session_s*)arg; struct session_s *s = (struct session_s*)arg;
struct eth_packet_s *ep; struct eth_packet_s *ep;
struct eth_packet_s *tep; struct eth_packet_s *tep;
if (event & EV_READ) {
if (event & EV_TIMEOUT) {
//printf("timeout\n");
} else if (event & EV_READ) {
ep = malloc(sizeof(struct eth_packet_s)); ep = malloc(sizeof(struct eth_packet_s));
memset(ep, 0, sizeof(struct eth_packet_s)); memset(ep, 0, sizeof(struct eth_packet_s));
ep->len = tapcfg_read(s->tapcfg, ep->data, 2000); ep->len = tapcfg_read(s->tapcfg, ep->data, 2000);
if(ep->len < 60) if(ep->len < 60)
{
ep->len = 60; ep->len = 60;
}
if(!s->ethpack) if(!s->ethpack)
{
s->ethpack = ep; s->ethpack = ep;
} else { else {
for(tep=s->ethpack; tep->next; tep=tep->next); for(tep=s->ethpack; tep->next; tep=tep->next);
tep->next = ep; tep->next = ep;
} }
@ -140,20 +130,18 @@ static const char macadr[6] = {0xaa, 0xb6, 0x24, 0x69, 0x77, 0x21};
static int ethernet_new(void **sess, char *args) static int ethernet_new(void **sess, char *args)
{ {
int ret=RC_OK; int ret = RC_OK;
char *c_tap=NULL; char *c_tap = NULL;
char *c_tap_ip=NULL; char *c_tap_ip = NULL;
struct session_s *s=NULL; struct session_s *s = NULL;
struct timeval tv={10, 0}; struct timeval tv = {10, 0};
if(!sess) if(!sess) {
{
ret = RC_INVARG; ret = RC_INVARG;
goto out; goto out;
} }
s=(struct session_s*)malloc(sizeof(struct session_s)); s=(struct session_s*)malloc(sizeof(struct session_s));
if(!s) if(!s) {
{
ret=RC_NOENMEM; ret=RC_NOENMEM;
goto out; goto out;
} }
@ -162,16 +150,12 @@ static int ethernet_new(void **sess, char *args)
ret = litex_sim_module_get_args(args, "interface", &c_tap); ret = litex_sim_module_get_args(args, "interface", &c_tap);
{ {
if(RC_OK != ret) if(RC_OK != ret)
{
goto out; goto out;
}
} }
ret = litex_sim_module_get_args(args, "ip", &c_tap_ip); ret = litex_sim_module_get_args(args, "ip", &c_tap_ip);
{ {
if(RC_OK != ret) if(RC_OK != ret)
{
goto out; goto out;
}
} }
s->tapcfg = tapcfg_init(); s->tapcfg = tapcfg_init();
@ -182,8 +166,6 @@ static int ethernet_new(void **sess, char *args)
tapcfg_iface_set_status(s->tapcfg, TAPCFG_STATUS_ALL_UP); tapcfg_iface_set_status(s->tapcfg, TAPCFG_STATUS_ALL_UP);
free(c_tap); free(c_tap);
free(c_tap_ip); free(c_tap_ip);
printf("FT:%d\n", s->fd);
printf("ETHERNET MODULE NEW CALLED\n");
s->ev = event_new(base, s->fd, EV_READ | EV_PERSIST, event_handler, s); s->ev = event_new(base, s->fd, EV_READ | EV_PERSIST, event_handler, s);
event_add(s->ev, &tv); event_add(s->ev, &tv);
@ -196,16 +178,14 @@ out:
static int ethernet_add_pads(void *sess, struct pad_list_s *plist) static int ethernet_add_pads(void *sess, struct pad_list_s *plist)
{ {
int ret=RC_OK; int ret=RC_OK;
struct session_s *s=(struct session_s*)sess; struct session_s *s = (struct session_s*)sess;
struct pad_s *pads; struct pad_s *pads;
if(!sess || !plist) if(!sess || !plist) {
{
ret = RC_INVARG; ret = RC_INVARG;
goto out; goto out;
} }
pads = plist->pads; pads = plist->pads;
if(!strcmp(plist->name, "eth")) if(!strcmp(plist->name, "eth")) {
{
litex_sim_module_pads_get(pads, "sink_data", (void**)&s->rx); litex_sim_module_pads_get(pads, "sink_data", (void**)&s->rx);
litex_sim_module_pads_get(pads, "sink_valid", (void**)&s->rx_valid); litex_sim_module_pads_get(pads, "sink_valid", (void**)&s->rx_valid);
litex_sim_module_pads_get(pads, "sink_ready", (void**)&s->rx_ready); litex_sim_module_pads_get(pads, "sink_ready", (void**)&s->rx_ready);
@ -214,10 +194,8 @@ static int ethernet_add_pads(void *sess, struct pad_list_s *plist)
litex_sim_module_pads_get(pads, "source_ready", (void**)&s->tx_ready); litex_sim_module_pads_get(pads, "source_ready", (void**)&s->tx_ready);
} }
if(!strcmp(plist->name, "sys_clk")) if(!strcmp(plist->name, "sys_clk"))
{
litex_sim_module_pads_get(pads, "sys_clk", (void**)&s->sys_clk); litex_sim_module_pads_get(pads, "sys_clk", (void**)&s->sys_clk);
}
out: out:
return ret; return ret;
@ -226,48 +204,33 @@ out:
static int ethernet_tick(void *sess) static int ethernet_tick(void *sess)
{ {
char c; char c;
struct session_s *s=(struct session_s*)sess; struct session_s *s = (struct session_s*)sess;
struct eth_packet_s *pep; struct eth_packet_s *pep;
if(*s->sys_clk == 0) if(*s->sys_clk == 0)
{
return RC_OK; return RC_OK;
}
*s->tx_ready = 1; *s->tx_ready = 1;
if(*s->tx_valid == 1) if(*s->tx_valid == 1) {
{
c = *s->tx; c = *s->tx;
//printf("%02x ", (unsigned char)c);
s->databuf[s->datalen++]=c; s->databuf[s->datalen++]=c;
} else { } else {
if(s->datalen) if(s->datalen) {
{
//printf("send fini\n");
tapcfg_write(s->tapcfg, s->databuf, s->datalen); tapcfg_write(s->tapcfg, s->databuf, s->datalen);
s->datalen=0; s->datalen=0;
} }
} }
*s->rx_valid=0; *s->rx_valid=0;
if(s->inlen) if(s->inlen) {
{
*s->rx_valid=1; *s->rx_valid=1;
*s->rx = s->inbuf[s->insent++]; *s->rx = s->inbuf[s->insent++];
//printf("%02x ", (unsigned char)*s->rx); if(s->insent == s->inlen) {
//printf("%d", *s->rx_ready);
if(s->insent == s->inlen)
{
//printf("recv fini\n");
s->insent =0; s->insent =0;
s->inlen = 0; s->inlen = 0;
} }
} } else {
else if(s->ethpack) {
{
if(s->ethpack)
{
memcpy(s->inbuf, s->ethpack->data, s->ethpack->len); memcpy(s->inbuf, s->ethpack->data, s->ethpack->len);
s->inlen = s->ethpack->len; s->inlen = s->ethpack->len;
pep=s->ethpack->next; pep=s->ethpack->next;
@ -275,21 +238,6 @@ static int ethernet_tick(void *sess)
s->ethpack=pep; s->ethpack=pep;
} }
} }
/*
else
{
if(tapcfg_wait_readable(s->tapcfg, 0))
{
memset(s->inbuf, 0, 2000);
s->inlen = tapcfg_read(s->tapcfg, s->inbuf, 2000);
if(s->inlen < 60)
{
s->inlen = 60;
}
}
}
*/
return RC_OK; return RC_OK;
} }
@ -304,7 +252,7 @@ static struct ext_module_s ext_mod = {
int litex_sim_ext_module_init(int (*register_module)(struct ext_module_s *)) int litex_sim_ext_module_init(int (*register_module)(struct ext_module_s *))
{ {
int ret=RC_OK; int ret = RC_OK;
ret = register_module(&ext_mod); ret = register_module(&ext_mod);
return ret; return ret;
} }

View File

@ -63,6 +63,7 @@ static int serial2console_start(void *b)
{ {
base = (struct event_base *)b; base = (struct event_base *)b;
set_conio_terminal_mode(); set_conio_terminal_mode();
printf("[serial2console] loaded (%p)\n", base);
return RC_OK; return RC_OK;
} }

View File

@ -29,29 +29,26 @@ struct event_base *base;
int litex_sim_module_get_args( char *args, char *arg, char **val) int litex_sim_module_get_args( char *args, char *arg, char **val)
{ {
int ret=RC_OK; int ret = RC_OK;
json_object *jsobj=NULL; json_object *jsobj = NULL;
json_object *obj=NULL; json_object *obj = NULL;
char *value=NULL; char *value = NULL;
int r; int r;
jsobj = json_tokener_parse(args); jsobj = json_tokener_parse(args);
if(NULL==jsobj) if(NULL==jsobj) {
{
fprintf(stderr, "Error parsing json arg: %s \n", args); fprintf(stderr, "Error parsing json arg: %s \n", args);
ret=RC_JSERROR; ret=RC_JSERROR;
goto out; goto out;
} }
if(!json_object_is_type(jsobj, json_type_object)) if(!json_object_is_type(jsobj, json_type_object)) {
{
fprintf(stderr, "Arg must be type object! : %s \n", args); fprintf(stderr, "Arg must be type object! : %s \n", args);
ret=RC_JSERROR; ret=RC_JSERROR;
goto out; goto out;
} }
obj=NULL; obj=NULL;
r = json_object_object_get_ex(jsobj, arg, &obj); r = json_object_object_get_ex(jsobj, arg, &obj);
if(!r) if(!r) {
{
fprintf(stderr, "Could not find object: \"%s\" (%s)\n", arg, args); fprintf(stderr, "Could not find object: \"%s\" (%s)\n", arg, args);
ret=RC_JSERROR; ret=RC_JSERROR;
goto out; goto out;
@ -66,40 +63,38 @@ out:
static int litex_sim_module_pads_get( struct pad_s *pads, char *name, void **signal) static int litex_sim_module_pads_get( struct pad_s *pads, char *name, void **signal)
{ {
int ret; int ret;
void *sig=NULL; void *sig = NULL;
int i; int i;
if(!pads || !name || !signal) if(!pads || !name || !signal) {
{ ret = RC_INVARG;
ret=RC_INVARG;
goto out; goto out;
} }
i = 0; i = 0;
while(pads[i].name) while(pads[i].name) {
{ if(!strcmp(pads[i].name, name)) {
if(!strcmp(pads[i].name, name)) sig = (void*)pads[i].signal;
{
sig=(void*)pads[i].signal;
break; break;
} }
i++; i++;
} }
out: out:
*signal=sig; *signal = sig;
return ret; return ret;
} }
static int serial2tcp_start(void *b) static int serial2tcp_start(void *b)
{ {
base =(struct event_base *)b; base = (struct event_base *)b;
printf("Loaded %p!\n", base); printf("[serial2tcp] loaded (%p)\n", base);
return RC_OK; return RC_OK;
} }
void read_handler(int fd, short event, void *arg) void read_handler(int fd, short event, void *arg)
{ {
struct session_s *s= (struct session_s*)arg; struct session_s *s = (struct session_s*)arg;
char buffer[1024]; char buffer[1024];
ssize_t read_len; ssize_t read_len;
@ -115,16 +110,13 @@ void read_handler(int fd, short event, void *arg)
static void event_handler(int fd, short event, void *arg) static void event_handler(int fd, short event, void *arg)
{ {
//printf("hit\n");
if (event & EV_READ) if (event & EV_READ)
{
read_handler(fd, event, arg); read_handler(fd, event, arg);
}
} }
static void accept_conn_cb(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *address, int socklen, void *ctx) static void accept_conn_cb(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *address, int socklen, void *ctx)
{ {
struct session_s *s= (struct session_s*)ctx; struct session_s *s = (struct session_s*)ctx;
struct timeval tv = {1, 0}; struct timeval tv = {1, 0};
s->fd = fd; s->fd = fd;
@ -143,41 +135,34 @@ accept_error_cb(struct evconnlistener *listener, void *ctx)
static int serial2tcp_new(void **sess, char *args) static int serial2tcp_new(void **sess, char *args)
{ {
int ret=RC_OK; int ret = RC_OK;
struct session_s *s=NULL; struct session_s *s = NULL;
char *cport=NULL; char *cport = NULL;
int port; int port;
struct evconnlistener *listener; struct evconnlistener *listener;
struct sockaddr_in sin; struct sockaddr_in sin;
if(!sess) if(!sess) {
{
ret = RC_INVARG; ret = RC_INVARG;
goto out; goto out;
} }
ret = litex_sim_module_get_args(args, "port", &cport); ret = litex_sim_module_get_args(args, "port", &cport);
{ if(RC_OK != ret)
if(RC_OK != ret) goto out;
{
goto out;
}
}
printf("Found port %s\n", cport); printf("Found port %s\n", cport);
sscanf(cport, "%d", &port); sscanf(cport, "%d", &port);
free(cport); free(cport);
if(!port) if(!port) {
{ ret = RC_ERROR;
ret=RC_ERROR;
fprintf(stderr, "Invalid port selected!\n"); fprintf(stderr, "Invalid port selected!\n");
goto out; goto out;
} }
s=(struct session_s*)malloc(sizeof(struct session_s)); s=(struct session_s*)malloc(sizeof(struct session_s));
if(!s) if(!s) {
{ ret = RC_NOENMEM;
ret=RC_NOENMEM;
goto out; goto out;
} }
memset(s, 0, sizeof(struct session_s)); memset(s, 0, sizeof(struct session_s));
@ -187,8 +172,7 @@ static int serial2tcp_new(void **sess, char *args)
sin.sin_addr.s_addr = htonl(0); sin.sin_addr.s_addr = htonl(0);
sin.sin_port = htons(port); sin.sin_port = htons(port);
listener = evconnlistener_new_bind(base, accept_conn_cb, s, LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1, (struct sockaddr*)&sin, sizeof(sin)); listener = evconnlistener_new_bind(base, accept_conn_cb, s, LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1, (struct sockaddr*)&sin, sizeof(sin));
if (!listener) if (!listener) {
{
ret=RC_ERROR; ret=RC_ERROR;
eprintf("Can't bind port %d\n!\n", port); eprintf("Can't bind port %d\n!\n", port);
goto out; goto out;
@ -198,7 +182,6 @@ static int serial2tcp_new(void **sess, char *args)
out: out:
*sess=(void*)s; *sess=(void*)s;
return ret; return ret;
} }
static int serial2tcp_add_pads(void *sess, struct pad_list_s *plist) static int serial2tcp_add_pads(void *sess, struct pad_list_s *plist)
@ -206,14 +189,12 @@ static int serial2tcp_add_pads(void *sess, struct pad_list_s *plist)
int ret=RC_OK; int ret=RC_OK;
struct session_s *s=(struct session_s*)sess; struct session_s *s=(struct session_s*)sess;
struct pad_s *pads; struct pad_s *pads;
if(!sess || !plist) if(!sess || !plist) {
{
ret = RC_INVARG; ret = RC_INVARG;
goto out; goto out;
} }
pads = plist->pads; pads = plist->pads;
if(!strcmp(plist->name, "serial")) if(!strcmp(plist->name, "serial")) {
{
litex_sim_module_pads_get(pads, "sink_data", (void**)&s->rx); litex_sim_module_pads_get(pads, "sink_data", (void**)&s->rx);
litex_sim_module_pads_get(pads, "sink_valid", (void**)&s->rx_valid); litex_sim_module_pads_get(pads, "sink_valid", (void**)&s->rx_valid);
litex_sim_module_pads_get(pads, "sink_ready", (void**)&s->rx_ready); litex_sim_module_pads_get(pads, "sink_ready", (void**)&s->rx_ready);
@ -222,12 +203,9 @@ static int serial2tcp_add_pads(void *sess, struct pad_list_s *plist)
litex_sim_module_pads_get(pads, "source_ready", (void**)&s->tx_ready); litex_sim_module_pads_get(pads, "source_ready", (void**)&s->tx_ready);
} }
if(!strcmp(plist->name, "sys_clk")) if(!strcmp(plist->name, "sys_clk"))
{
litex_sim_module_pads_get(pads, "sys_clk", (void**)&s->sys_clk); litex_sim_module_pads_get(pads, "sys_clk", (void**)&s->sys_clk);
}
out: out:
return ret; return ret;
@ -235,20 +213,16 @@ out:
static int serial2tcp_tick(void *sess) static int serial2tcp_tick(void *sess)
{ {
char c; char c;
int ret=RC_OK; int ret = RC_OK;
struct session_s *s=(struct session_s*)sess; struct session_s *s = (struct session_s*)sess;
if(*s->sys_clk == 0) if(*s->sys_clk == 0)
{
return RC_OK; return RC_OK;
}
*s->tx_ready = 1; *s->tx_ready = 1;
if(s->fd && *s->tx_valid) if(s->fd && *s->tx_valid) {
{
c = *s->tx; c = *s->tx;
if(-1 ==write(s->fd, &c, 1)) if(-1 ==write(s->fd, &c, 1)) {
{
eprintf("Error writing on socket\n"); eprintf("Error writing on socket\n");
ret = RC_ERROR; ret = RC_ERROR;
goto out; goto out;
@ -256,8 +230,7 @@ static int serial2tcp_tick(void *sess)
} }
*s->rx_valid=0; *s->rx_valid=0;
if(s->datalen) if(s->datalen) {
{
*s->rx=s->databuf[s->data_start]; *s->rx=s->databuf[s->data_start];
s->data_start = (s->data_start + 1) % 2048; s->data_start = (s->data_start + 1) % 2048;
s->datalen--; s->datalen--;
@ -279,7 +252,7 @@ static struct ext_module_s ext_mod = {
int litex_sim_ext_module_init(int (*register_module)(struct ext_module_s *)) int litex_sim_ext_module_init(int (*register_module)(struct ext_module_s *))
{ {
int ret=RC_OK; int ret = RC_OK;
ret = register_module(&ext_mod); ret = register_module(&ext_mod);
return ret; return ret;
} }