From 4433e2449af06d668ff12c273c616394b9893b73 Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Wed, 28 Jun 2017 18:01:04 +0200 Subject: [PATCH] litex/build/sim: cleanup modules --- .../build/sim/core/modules/clocker/clocker.c | 33 ++--- .../sim/core/modules/ethernet/ethernet.c | 140 ++++++------------ .../modules/serial2console/serial2console.c | 1 + .../sim/core/modules/serial2tcp/serial2tcp.c | 105 +++++-------- 4 files changed, 99 insertions(+), 180 deletions(-) diff --git a/litex/build/sim/core/modules/clocker/clocker.c b/litex/build/sim/core/modules/clocker/clocker.c index 3543534aa..13d047892 100644 --- a/litex/build/sim/core/modules/clocker/clocker.c +++ b/litex/build/sim/core/modules/clocker/clocker.c @@ -14,14 +14,13 @@ static int litex_sim_module_pads_get( struct pad_s *pads, char *name, void **sig void *sig=NULL; int i; - if(!pads || !name || !signal) - { + if(!pads || !name || !signal) { ret=RC_INVARG; goto out; } + i = 0; - while(pads[i].name) - { + while(pads[i].name) { if(!strcmp(pads[i].name, name)) { sig=(void*)pads[i].signal; @@ -37,7 +36,7 @@ out: static int clocker_start() { - printf("Loaded !\n"); + printf("[clocker] loaded\n"); return RC_OK; } @@ -47,43 +46,41 @@ static int clocker_new(void **sess, char *args) struct session_s *s=NULL; - if(!sess) - { + if(!sess) { ret = RC_INVARG; goto out; } s=(struct session_s*)malloc(sizeof(struct session_s)); - if(!s) - { + if(!s) { ret=RC_NOENMEM; goto out; } memset(s, 0, sizeof(struct session_s)); + out: *sess=(void*)s; - return ret; - + return ret; } static int clocker_add_pads(void *sess, struct pad_list_s *plist) { - int ret=RC_OK; - struct session_s *s=(struct session_s*)sess; + int ret = RC_OK; + struct session_s *s = (struct session_s*)sess; struct pad_s *pads; - if(!sess || !plist) - { + + if(!sess || !plist) { ret = RC_INVARG; goto out; } 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); } *s->sys_clk=0; + out: 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 ret=RC_OK; + int ret = RC_OK; ret = register_module(&ext_mod); return ret; } diff --git a/litex/build/sim/core/modules/ethernet/ethernet.c b/litex/build/sim/core/modules/ethernet/ethernet.c index 00882fa67..7125c5548 100644 --- a/litex/build/sim/core/modules/ethernet/ethernet.c +++ b/litex/build/sim/core/modules/ethernet/ethernet.c @@ -37,58 +37,55 @@ struct session_s { 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; - json_object *jsobj=NULL; - json_object *obj=NULL; - char *value=NULL; + int ret = RC_OK; + json_object *jsobj = NULL; + json_object *obj = NULL; + char *value = NULL; int r; jsobj = json_tokener_parse(args); - if(NULL==jsobj) - { + if(NULL == jsobj) { fprintf(stderr, "Error parsing json arg: %s \n", args); - ret=RC_JSERROR; + ret = RC_JSERROR; 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); - ret=RC_JSERROR; + ret = RC_JSERROR; goto out; } + obj=NULL; r = json_object_object_get_ex(jsobj, arg, &obj); - if(!r) - { + if(!r) { fprintf(stderr, "Could not find object: \"%s\" (%s)\n", arg, args); - ret=RC_JSERROR; + ret = RC_JSERROR; goto out; } - value=strdup(json_object_get_string(obj)); + value = strdup(json_object_get_string(obj)); out: *val = value; 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; - void *sig=NULL; + void *sig = NULL; int i; - if(!pads || !name || !signal) - { + if(!pads || !name || !signal) { ret=RC_INVARG; goto out; } + i = 0; - while(pads[i].name) - { - if(!strcmp(pads[i].name, name)) - { + while(pads[i].name) { + if(!strcmp(pads[i].name, name)) { sig=(void*)pads[i].signal; break; } @@ -102,34 +99,27 @@ out: static int ethernet_start(void *b) { - base=(struct event_base *)b; - printf("Loaded eth %p!\n", base); + base = (struct event_base *) b; + printf("[ethernet] loaded (%p)\n", base); return RC_OK; } 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 *tep; - - - if (event & EV_TIMEOUT) { - //printf("timeout\n"); - } else if (event & EV_READ) { + if (event & EV_READ) { ep = malloc(sizeof(struct eth_packet_s)); memset(ep, 0, sizeof(struct eth_packet_s)); ep->len = tapcfg_read(s->tapcfg, ep->data, 2000); if(ep->len < 60) - { ep->len = 60; - } if(!s->ethpack) - { s->ethpack = ep; - } else { + else { for(tep=s->ethpack; tep->next; tep=tep->next); 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) { - int ret=RC_OK; - char *c_tap=NULL; - char *c_tap_ip=NULL; - struct session_s *s=NULL; - struct timeval tv={10, 0}; - if(!sess) - { + int ret = RC_OK; + char *c_tap = NULL; + char *c_tap_ip = NULL; + struct session_s *s = NULL; + struct timeval tv = {10, 0}; + if(!sess) { ret = RC_INVARG; goto out; } s=(struct session_s*)malloc(sizeof(struct session_s)); - if(!s) - { + if(!s) { ret=RC_NOENMEM; goto out; } @@ -162,16 +150,12 @@ static int ethernet_new(void **sess, char *args) ret = litex_sim_module_get_args(args, "interface", &c_tap); { if(RC_OK != ret) - { goto out; - } } ret = litex_sim_module_get_args(args, "ip", &c_tap_ip); { if(RC_OK != ret) - { goto out; - } } 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); free(c_tap); 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); event_add(s->ev, &tv); @@ -196,16 +178,14 @@ out: static int ethernet_add_pads(void *sess, struct pad_list_s *plist) { int ret=RC_OK; - struct session_s *s=(struct session_s*)sess; + struct session_s *s = (struct session_s*)sess; struct pad_s *pads; - if(!sess || !plist) - { + if(!sess || !plist) { ret = RC_INVARG; goto out; } 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_valid", (void**)&s->rx_valid); 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); } - if(!strcmp(plist->name, "sys_clk")) - { + if(!strcmp(plist->name, "sys_clk")) litex_sim_module_pads_get(pads, "sys_clk", (void**)&s->sys_clk); - } out: return ret; @@ -226,48 +204,33 @@ out: static int ethernet_tick(void *sess) { char c; - struct session_s *s=(struct session_s*)sess; + struct session_s *s = (struct session_s*)sess; struct eth_packet_s *pep; if(*s->sys_clk == 0) - { return RC_OK; - } - *s->tx_ready = 1; - if(*s->tx_valid == 1) - { + if(*s->tx_valid == 1) { c = *s->tx; - //printf("%02x ", (unsigned char)c); s->databuf[s->datalen++]=c; } else { - if(s->datalen) - { - //printf("send fini\n"); + if(s->datalen) { tapcfg_write(s->tapcfg, s->databuf, s->datalen); s->datalen=0; } } *s->rx_valid=0; - if(s->inlen) - { + if(s->inlen) { *s->rx_valid=1; *s->rx = s->inbuf[s->insent++]; - //printf("%02x ", (unsigned char)*s->rx); - //printf("%d", *s->rx_ready); - if(s->insent == s->inlen) - { - //printf("recv fini\n"); + if(s->insent == s->inlen) { s->insent =0; s->inlen = 0; } - } - else - { - if(s->ethpack) - { + } else { + if(s->ethpack) { memcpy(s->inbuf, s->ethpack->data, s->ethpack->len); s->inlen = s->ethpack->len; pep=s->ethpack->next; @@ -275,21 +238,6 @@ static int ethernet_tick(void *sess) 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; } @@ -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 ret=RC_OK; + int ret = RC_OK; ret = register_module(&ext_mod); return ret; } diff --git a/litex/build/sim/core/modules/serial2console/serial2console.c b/litex/build/sim/core/modules/serial2console/serial2console.c index 8d49ff5df..20cb12d2e 100644 --- a/litex/build/sim/core/modules/serial2console/serial2console.c +++ b/litex/build/sim/core/modules/serial2console/serial2console.c @@ -63,6 +63,7 @@ static int serial2console_start(void *b) { base = (struct event_base *)b; set_conio_terminal_mode(); + printf("[serial2console] loaded (%p)\n", base); return RC_OK; } diff --git a/litex/build/sim/core/modules/serial2tcp/serial2tcp.c b/litex/build/sim/core/modules/serial2tcp/serial2tcp.c index 8e8eaf9e5..347c92eb8 100644 --- a/litex/build/sim/core/modules/serial2tcp/serial2tcp.c +++ b/litex/build/sim/core/modules/serial2tcp/serial2tcp.c @@ -29,29 +29,26 @@ struct event_base *base; int litex_sim_module_get_args( char *args, char *arg, char **val) { - int ret=RC_OK; - json_object *jsobj=NULL; - json_object *obj=NULL; - char *value=NULL; + int ret = RC_OK; + json_object *jsobj = NULL; + json_object *obj = NULL; + char *value = NULL; int r; jsobj = json_tokener_parse(args); - if(NULL==jsobj) - { + if(NULL==jsobj) { fprintf(stderr, "Error parsing json arg: %s \n", args); ret=RC_JSERROR; 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); ret=RC_JSERROR; goto out; } obj=NULL; r = json_object_object_get_ex(jsobj, arg, &obj); - if(!r) - { + if(!r) { fprintf(stderr, "Could not find object: \"%s\" (%s)\n", arg, args); ret=RC_JSERROR; goto out; @@ -66,40 +63,38 @@ out: static int litex_sim_module_pads_get( struct pad_s *pads, char *name, void **signal) { int ret; - void *sig=NULL; + void *sig = NULL; int i; - if(!pads || !name || !signal) - { - ret=RC_INVARG; + if(!pads || !name || !signal) { + ret = RC_INVARG; goto out; } + i = 0; - while(pads[i].name) - { - if(!strcmp(pads[i].name, name)) - { - sig=(void*)pads[i].signal; + while(pads[i].name) { + if(!strcmp(pads[i].name, name)) { + sig = (void*)pads[i].signal; break; } i++; } out: - *signal=sig; + *signal = sig; return ret; } static int serial2tcp_start(void *b) { - base =(struct event_base *)b; - printf("Loaded %p!\n", base); + base = (struct event_base *)b; + printf("[serial2tcp] loaded (%p)\n", base); return RC_OK; } 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]; 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) { - //printf("hit\n"); if (event & EV_READ) - { read_handler(fd, event, arg); - } } 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}; s->fd = fd; @@ -143,41 +135,34 @@ accept_error_cb(struct evconnlistener *listener, void *ctx) static int serial2tcp_new(void **sess, char *args) { - int ret=RC_OK; - struct session_s *s=NULL; - char *cport=NULL; + int ret = RC_OK; + struct session_s *s = NULL; + char *cport = NULL; int port; struct evconnlistener *listener; struct sockaddr_in sin; - if(!sess) - { + if(!sess) { ret = RC_INVARG; goto out; } ret = litex_sim_module_get_args(args, "port", &cport); - { - if(RC_OK != ret) - { - goto out; - } - } + if(RC_OK != ret) + goto out; printf("Found port %s\n", cport); sscanf(cport, "%d", &port); free(cport); - if(!port) - { - ret=RC_ERROR; + if(!port) { + ret = RC_ERROR; fprintf(stderr, "Invalid port selected!\n"); goto out; } s=(struct session_s*)malloc(sizeof(struct session_s)); - if(!s) - { - ret=RC_NOENMEM; + if(!s) { + ret = RC_NOENMEM; goto out; } 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_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)); - if (!listener) - { + if (!listener) { ret=RC_ERROR; eprintf("Can't bind port %d\n!\n", port); goto out; @@ -198,7 +182,6 @@ static int serial2tcp_new(void **sess, char *args) out: *sess=(void*)s; return ret; - } 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; struct session_s *s=(struct session_s*)sess; struct pad_s *pads; - if(!sess || !plist) - { + if(!sess || !plist) { ret = RC_INVARG; goto out; } 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_valid", (void**)&s->rx_valid); 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); } - if(!strcmp(plist->name, "sys_clk")) - { + if(!strcmp(plist->name, "sys_clk")) litex_sim_module_pads_get(pads, "sys_clk", (void**)&s->sys_clk); - } - out: return ret; @@ -235,20 +213,16 @@ out: static int serial2tcp_tick(void *sess) { 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) - { return RC_OK; - } *s->tx_ready = 1; - if(s->fd && *s->tx_valid) - { + if(s->fd && *s->tx_valid) { c = *s->tx; - if(-1 ==write(s->fd, &c, 1)) - { + if(-1 ==write(s->fd, &c, 1)) { eprintf("Error writing on socket\n"); ret = RC_ERROR; goto out; @@ -256,8 +230,7 @@ static int serial2tcp_tick(void *sess) } *s->rx_valid=0; - if(s->datalen) - { + if(s->datalen) { *s->rx=s->databuf[s->data_start]; s->data_start = (s->data_start + 1) % 2048; 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 ret=RC_OK; + int ret = RC_OK; ret = register_module(&ext_mod); return ret; }