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,23 +46,21 @@ 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)
@ -71,19 +68,19 @@ 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;
} }

View File

@ -46,22 +46,21 @@ int litex_sim_module_get_args( char *args, char *arg, char **val)
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;
@ -79,16 +78,14 @@ 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;
break; break;
} }
@ -103,7 +100,7 @@ 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;
} }
@ -113,23 +110,16 @@ void event_handler(int fd, short event, void *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;
} }
@ -145,15 +135,13 @@ static int ethernet_new(void **sess, char *args)
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,17 +150,13 @@ 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();
tapcfg_start(s->tapcfg, c_tap, 0); tapcfg_start(s->tapcfg, c_tap, 0);
@ -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);
@ -198,14 +180,12 @@ 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);
@ -215,9 +195,7 @@ static int ethernet_add_pads(void *sess, struct pad_list_s *plist)
} }
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;
@ -230,44 +208,29 @@ static int ethernet_tick(void *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;
} }

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

@ -36,22 +36,19 @@ int litex_sim_module_get_args( char *args, char *arg, char **val)
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;
@ -69,16 +66,14 @@ 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;
break; break;
} }
@ -93,7 +88,7 @@ out:
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;
} }
@ -115,12 +110,9 @@ 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)
{ {
@ -150,33 +142,26 @@ static int serial2tcp_new(void **sess, char *args)
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;
} }
@ -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);
@ -223,11 +204,8 @@ static int serial2tcp_add_pads(void *sess, struct pad_list_s *plist)
} }
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;
@ -239,16 +217,12 @@ static int serial2tcp_tick(void *sess)
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--;