diff options
author | athulappadan | 2017-03-28 11:10:20 +0530 |
---|---|---|
committer | athulappadan | 2017-03-28 11:10:20 +0530 |
commit | 71abd63a65e0e5d4c028ba0772a037ab1bdb6257 (patch) | |
tree | d9402baee85a84b018ebd75d23fda7ebc88f7a27 /src | |
parent | b397923dddfc9484a669cb69a3585d947d930e73 (diff) | |
download | nghdl-71abd63a65e0e5d4c028ba0772a037ab1bdb6257.tar.gz nghdl-71abd63a65e0e5d4c028ba0772a037ab1bdb6257.tar.bz2 nghdl-71abd63a65e0e5d4c028ba0772a037ab1bdb6257.zip |
mixed-mode simulation modifications
Diffstat (limited to 'src')
-rw-r--r-- | src/ghdlserver/ghdlserver.c | 984 | ||||
-rw-r--r-- | src/ghdlserver/ghdlserver.h | 36 | ||||
-rw-r--r-- | src/outitf.c | 1354 |
3 files changed, 1869 insertions, 505 deletions
diff --git a/src/ghdlserver/ghdlserver.c b/src/ghdlserver/ghdlserver.c index 603f925..09514e1 100644 --- a/src/ghdlserver/ghdlserver.c +++ b/src/ghdlserver/ghdlserver.c @@ -1,3 +1,28 @@ +/************************************************************************* + * <ghdlserver.c> FOSSEE, IIT-Mumbai + * 24.Mar.2017 - Raj Mohan - Added signal handler for SIGUSR1, to handle an + * orphan test bench process. + * The test bench will now create a PID file in + * /tmp directory with the name + * NGHDL_<ngspice pid>_<test bench>_<instance_id> + * This file contains the PID of the test bench . + * On exit, the test bench removes this file. + * The SIGUSR1 signal serves the same purpose as the + * "End" signal. + * - Added syslog interface for logging. + * - Enabled SO_REUSEADDR socket option. + * - Added the following functions: + * o create_pid_file() + * o get_ngspice_pid() + * 22.Feb.2017 - Raj Mohan - Implemented a kludge to fix a problem in the + * test bench VHDL code. + * - Changed sleep() to nanosleep(). + * 10.Feb.2017 - Raj Mohan - Log messages with timestamp/code clean up. + * Added the following functions: + * o curtim() + * o print_hash_table() + ************************************************************************* + */ #include <string.h> #include "ghdlserver.h" #include "uthash.h" @@ -6,615 +31,626 @@ #include <stdlib.h> #include <assert.h> #include <signal.h> +#include <unistd.h> #include <sys/types.h> -#include <sys/socket.h> +#include <sys/socket.h> +#include <sys/time.h> #include <netinet/in.h> #include <netdb.h> #include <limits.h> #include <time.h> - +#include <errno.h> +#include <dirent.h> +#include <syslog.h> + #define _XOPEN_SOURCE 500 #define MAX_NUMBER_PORT 100 -//#define LOG_FILE "vhpi.log" -//FILE *log_file = NULL; -//FILE *log_sock_id = NULL; -FILE *log_server = NULL; -FILE *log_server1 = NULL; -#define z32__ "00000000000000000000000000000000" - -char* Out_Port_Array[MAX_NUMBER_PORT]; -int out_port_num=0; - -int vhpi_cycle_count =0; -//server socket -int server_socket_id=-1; + +#define NGSPICE "ngspice" // 17.Mar.2017 - RM + +extern char* __progname; // 26.Feb.2017 May not be portable to non-GNU systems. + +void Vhpi_Exit(int sig); + +static FILE* pid_file; +static char pid_filename[80]; + +static char* Out_Port_Array[MAX_NUMBER_PORT]; +static int out_port_num = 0; +static int server_socket_id = -1; +static int sendto_sock; // 22.Feb.2017 - RM - Kludge +static int prev_sendto_sock; // 22.Feb.2017 - RM - Kludge +static int pid_file_created; // 10.Mar.2017 - RM + struct my_struct { char val[1024]; - char key[1024]; //Key - UT_hash_handle hh; /* makes this structure hashable */ + char key[1024]; + UT_hash_handle hh; //Makes this structure hashable. }; +static struct my_struct *s, *users, *tmp = NULL; -struct my_struct *s, *users ,*tmp = NULL; - -void parse_buffer(int sock_id,char* receive_buffer) +/* 17.Mar.2017 - RM - Get the process id of ngspice program.*/ +static int get_ngspice_pid(void) { - /*Taking time information for log*/ - - //time_t systime; - //systime = time(NULL); - log_server=fopen("server.log","a"); - //fprintf(log_server,"Buffer came at %s \n",ctime(&systime)); - - fprintf(log_server,"Server-The recieved buffer is : %s \n",receive_buffer); - fprintf(log_server,"Server-The socket id is : %d \n",sock_id); - - /*Parsing buffer to store in hash table */ - char *rest; - char *token; - char *ptr1=receive_buffer; - - char *var; - char *value; + DIR* dirp; + FILE* fp = NULL; + struct dirent* dir_entry; + char path[1024], rd_buff[1024]; + pid_t pid = -1; - - fprintf(stderr,"Server-The recieved buffer is : %s \n",receive_buffer); - fprintf(stderr,"Server-The socket id is : %d \n",sock_id); - while(token = strtok_r(ptr1,",",&rest)) + if ((dirp = opendir("/proc/")) == NULL) { - ptr1 = rest; // rest contains the left over part..assign it to ptr...and start tokenizing again. - - //Processing token again; - - while(var=strtok_r(token,":",&value)) - { + perror("opendir /proc failed"); + exit(-1); + } - s = (struct my_struct*)malloc(sizeof(struct my_struct)); - printf("Server-Variable is %s \n",var); - printf("Server-Value is %s \n",value); - - strncpy(s->key, var,10); - strncpy(s->val,value,10); - HASH_ADD_STR( users, key, s ); - break; - } + while ((dir_entry = readdir(dirp)) != NULL) + { + char* nptr; + int valid_num = 0; + + int tmp = strtol(dir_entry->d_name, &nptr, 10); + if ((errno == ERANGE) && (tmp == LONG_MAX || tmp == LONG_MIN)) + { + perror("strtol"); // Number out of range. + return(-1); + } + if (dir_entry->d_name == nptr) + { + continue; // No digits found. + } + if (tmp) + { + sprintf(path, "/proc/%s/comm", dir_entry->d_name); + if ((fp = fopen(path, "r")) != NULL) + { + fscanf(fp, "%s", rd_buff); + if (strcmp(rd_buff, NGSPICE) == 0) + { + pid = (pid_t)tmp; + break; + } + } + } } - - s = (struct my_struct*)malloc(sizeof(struct my_struct)); - strncpy(s->key,"sock_id",10); - snprintf(s->val,10,"%d",sock_id); - HASH_ADD_STR(users,key,s); - fflush(log_server); - fclose(log_server); -} + + if (fp) fclose(fp); + return(pid); +} -void Vhpi_Set_Port_Value(char *port_name,char *port_value,int port_width) +/* 23.Mar.2017 - RM - Pass the sock_port argument. We need this if a netlist + * uses more than one instance of the same test bench, so that we can uniquely + * identify the PID files. + */ +/* 10.Mar.2017 - RM - Create PID file for the test bench in /tmp. */ +static void create_pid_file(int sock_port) { - //char *lb; // you need to know maximum size of lb. - //int I; - //snprintf(lb,sizeof(char),"%s", port_value); - - printf("Server-Vhpi_Set_Port_value \n"); - printf("Server-The port name is %s \n",port_name); - - - //printf("Port valu is %s",port_value); - - s = (struct my_struct*)malloc(sizeof(struct my_struct)); - strncpy(s->key, port_name,10); - strncpy(s->val,port_value,10); - HASH_ADD_STR( users, key, s ); - - - printf("Server-The out port value is %s \n ",port_value); - - log_server=fopen("server.log","a"); - fprintf(log_server,"Set Port Details \n"); - fprintf(log_server,"Port Name - %s And Port Value - %s \n",port_name,port_value); - fflush(log_server); - fclose(log_server); - + pid_t my_pid = getpid(); + pid_t ngspice_pid = get_ngspice_pid(); + if (ngspice_pid == -1) + { + fprintf(stderr, "create_pid_file() Failed to get ngspice PID"); + syslog(LOG_ERR, "create_pid_file() Failed to get ngspice PID"); + exit(1); + } + sprintf(pid_filename, "/tmp/NGHDL_%d_%s_%d", ngspice_pid, __progname, + sock_port); + pid_file = fopen(pid_filename, "w"); + if (pid_file) + { + pid_file_created = 1; + fprintf(pid_file,"%d\n", my_pid); + fclose(pid_file); + } else { + perror("fopen() - PID file"); + syslog(LOG_ERR, "create_pid_file(): Unable to open PID file in /tmp"); + exit(1); + } + return; } -void Vhpi_Get_Port_Value(char* port_name,char* port_value,int port_width) +#ifdef DEBUG +static char* curtim(void) +{ + static char ct[50]; + struct timeval tv; + struct tm* ptm; + long milliseconds; + char time_string[40]; + + gettimeofday (&tv, NULL); + ptm = localtime (&tv.tv_sec); + strftime (time_string, sizeof (time_string), "%Y-%m-%d %H:%M:%S", ptm); + milliseconds = tv.tv_usec / 1000; + sprintf (ct, "%s.%03ld", time_string, milliseconds); + return(ct); +} +#endif +#ifdef DEBUG +static void print_hash_table(void) { - - printf("Server-Vhpi_Get_Port_Value \n"); - //int I; - //snprintf(port_value,1024,"1"); - - log_server=fopen("server.log","a"); - fprintf(log_server,"Get Port Details \n"); - - HASH_FIND_STR(users,port_name,s); - if(s) - { - printf("Server-The key is %s and value is %s \n",s->key,s->val); + struct my_struct *sptr; - snprintf(port_value,sizeof(port_value),"%s",s->val); - fprintf(log_server,"Port Name - %s And Port Value - %s \n",port_name,port_value); - HASH_DEL(users, s); - free(s); - } - else - { - printf("Server-Port %s Not found \n",port_name); - fprintf(log_server,"Port : %s not found \n",port_name); - } - - fflush(log_server); - fclose(log_server); -} -/* -int Copy_Value(char* dest, char* src, int width) -{ - int ret_val = 0; - char src_buf[4096]; - int src_width = 0; - - // skip spaces - while(*src == ' ') - src++; - while(1) - { - if(src[src_width] == '1' || src[src_width] == '0') - { - src_buf[src_width] = src[src_width]; - src_width++; - } - else - break; - } - src_buf[src_width] = 0; // null-terminate - ret_val = src_width - width; - dest[width] = 0; - int i; - for(i = 1; i <= width; i++) - { - if(i <= src_width) - dest[width-i] = src[src_width-i]; - else - dest[width-i] = '0'; // pad with 0. - } - - return(ret_val); + for(sptr=users; sptr != NULL; sptr=sptr->hh.next) + { + syslog(LOG_INFO, "Hash table:val:%s: key: %s", sptr->val, sptr->key); + } } +#endif + +static void parse_buffer(int sock_id, char* receive_buffer) +{ + static int rcvnum; + + syslog(LOG_INFO,"RCVD RCVN:%d from CLT:%d buffer : %s", + rcvnum++, sock_id,receive_buffer); + + /*Parsing buffer to store in hash table */ + char *rest; + char *token; + char *ptr1=receive_buffer; + char *var; + char *value; -*/ -/* -int extract_payload(char* receive_buffer,char* payload, int max_n) -{ - int hash_pos = 0; - int ret_val = 0; - while(receive_buffer[hash_pos] != '#') + // Processing tokens. + while(token = strtok_r(ptr1, ",", &rest)) { - printf("Buffer is %c \n",receive_buffer[hash_pos]); - if(receive_buffer[hash_pos] == 0) // end of string + ptr1 = rest; + while(var=strtok_r(token, ":", &value)) { - hash_pos = -1; - break; + s = (struct my_struct*)malloc(sizeof(struct my_struct)); + strncpy(s->key, var, 10); + strncpy(s->val, value, 10); + HASH_ADD_STR(users, key, s ); + break; } - hash_pos++; } - - if(hash_pos >= 0) - { - receive_buffer[hash_pos] = 0; - ret_val = max_n - (hash_pos+1); - bcopy(receive_buffer+(hash_pos+1),payload,ret_val); - } - - return(ret_val); -} -*/ - + + s = (struct my_struct*)malloc(sizeof(struct my_struct)); + strncpy(s->key, "sock_id", 10); + snprintf(s->val,10, "%d", sock_id); + HASH_ADD_STR(users, key, s); +} // -//Create Server to listen for message +//Create Server and listen for client connections. // - -int create_server(int port_number,int max_connections) +static int create_server(int port_number,int max_connections) { - - - int sockfd; + int sockfd, reuse = 1; struct sockaddr_in serv_addr; + sockfd = socket(AF_INET, SOCK_STREAM, 0); - fprintf(stderr, "Server- Info: opening socket for server on port %d with socket id %d \n",port_number,sockfd); + if (sockfd < 0) - fprintf(stderr, "Server- Error: in opening socket on port %d\n", port_number); + { + fprintf(stderr, "%s- Error: in opening socket on port %d\n", + __progname, port_number); + exit(1); + } + +/* 20.Mar.2017 - RM - SO_REUSEADDR option. To take care of TIME_WAIT state.*/ + int ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)); + if (ret < 0) + { + syslog(LOG_ERR, "create_server:setsockopt() failed...."); + } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(port_number); - + if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { - fprintf(stderr,"Server- Error: could not bind socket to port %d\n",port_number); - close(sockfd); - sockfd= -1; + fprintf(stderr,"%s- Error: could not bind socket to port %d\n", + __progname, port_number); + syslog(LOG_ERR, "Error: could not bind socket to port %d", port_number); + + close(sockfd); + + exit(1); } - else - fprintf(stderr,"Server- Info: finished binding socket to port %d\n",port_number); - // start listening on the server. - listen(sockfd,max_connections); - return sockfd; - -} + // Start listening on the server. + listen(sockfd, max_connections); -// -// ask the server to wait for a client connection -// and accept one connection if possible -// -// uses select to make this non-blocking. -// -// + return sockfd; +} -int connect_to_client(int server_fd) +// The server to wait (non-blocking) for a client connection. +static int connect_to_client(int server_fd) { - int ret_val = 1; - int newsockfd = -1; - socklen_t clilen; - struct sockaddr_in cli_addr; - fd_set c_set; - struct timeval time_limit; - time_limit.tv_sec = 0; - time_limit.tv_usec = 1000; - - clilen = sizeof(cli_addr); - FD_ZERO(&c_set); - FD_SET(server_fd,&c_set); - select(server_fd+1, &c_set,NULL,NULL,&time_limit); - if(FD_ISSET(server_fd,&c_set)) - { - newsockfd = accept(server_fd,(struct sockaddr *) &cli_addr,&clilen); - if (newsockfd >= 0) - { - fprintf(stderr,"Server- Info: new client connection %d \n",newsockfd); - } - else - { - fprintf(stderr,"Server- Info: failed in accept()\n"); - } - } - //else - //{ - // fprintf(stderr,"Server- failed to connect to client \n"); - //} + int ret_val = 1; + int newsockfd = -1; + socklen_t clilen; + struct sockaddr_in cli_addr; + fd_set c_set; + struct timeval time_limit; + + time_limit.tv_sec = 0; + time_limit.tv_usec = 1000; - return(newsockfd); -} + clilen = sizeof(cli_addr); + FD_ZERO(&c_set); + FD_SET(server_fd, &c_set); -// -//use select to check if we can write to -// the socket.. -// + select(server_fd + 1, &c_set, NULL, NULL, &time_limit); + + ret_val = FD_ISSET(server_fd, &c_set); + + if(ret_val) + { + newsockfd = accept(server_fd, (struct sockaddr *) &cli_addr, &clilen); + if (newsockfd >= 0) + { + syslog(LOG_INFO, "SRV:%d New Client Connection CLT:%d", + server_fd, newsockfd); + } + else + { + syslog(LOG_ERR,"Error: failed in accept(), socket=%d", server_fd); + exit(1); + } + } + return(newsockfd); +} -int can_read_from_socket(int socket_id) +// +// Check if we can read from the socket.. +// +static int can_read_from_socket(int socket_id) { - struct timeval time_limit; - time_limit.tv_sec = 0; - time_limit.tv_usec = 1000; - - fd_set c_set; - FD_ZERO(&c_set); - FD_SET(socket_id,&c_set); + struct timeval time_limit; + time_limit.tv_sec = 0; + time_limit.tv_usec = 1000; - int npending = select(socket_id + 1, &c_set, NULL,NULL,&time_limit); + fd_set c_set; + FD_ZERO(&c_set); + FD_SET(socket_id, &c_set); - return(FD_ISSET(socket_id,&c_set)); + int npending = select(socket_id + 1, &c_set, NULL, NULL, &time_limit); + if (npending == -1) + { + npending = errno; + syslog(LOG_ERR, "can_read_from_socket:select() ERRNO=%d",npending); + return(-100); + } + return(FD_ISSET(socket_id, &c_set)); } - // -// use select to check if we can write to -// the socket.. +// Check if we can write to the socket.. // - -int can_write_to_socket(int socket_id) +static int can_write_to_socket(int socket_id) { - struct timeval time_limit; - time_limit.tv_sec = 0; - time_limit.tv_usec = 1000; - - fd_set c_set; - FD_ZERO(&c_set); - FD_SET(socket_id,&c_set); - int npending = select(socket_id + 1, NULL, &c_set,NULL,&time_limit); + struct timeval time_limit; + time_limit.tv_sec = 0; + time_limit.tv_usec = 1000; - return(FD_ISSET(socket_id,&c_set)); -} + fd_set c_set; + FD_ZERO(&c_set); + FD_SET(socket_id, &c_set); + int npending = select(socket_id + 1, NULL, &c_set, NULL, &time_limit); + if (npending == -1) + { + npending = errno; -//receive string from socket and put it inside buffer. + syslog(LOG_ERR, "can_write_to_socket() select() ERRNO=%d",npending); -int receive_string(int sock_id, char* buffer) + return (-100); + } else if (npending == 0) { // select() timed out... + return(0); + } + return(FD_ISSET(socket_id,&c_set)); +} + +//Receive string from socket and put it inside buffer. +static int receive_string(int sock_id, char* buffer) { - int nbytes = 0; + int nbytes = 0; + int ret; - while(1) - { - if(can_read_from_socket(sock_id)) - break; - else - usleep(1000); + while(1) + { + ret = can_read_from_socket(sock_id); + if (ret == 0) + { // select() had timed out. Retry... + usleep(1000); + continue; + } else + if (ret == -100) + { + return(-1); + } + break; } - nbytes = recv(sock_id,buffer,MAX_BUF_SIZE,0); - return(nbytes); + nbytes = recv(sock_id, buffer, MAX_BUF_SIZE, 0); + if (nbytes < 0) + { + perror("READ FAILURE"); + exit(1); + } + return(nbytes); } -// -// will establish a connection, send the -// packet and block till a response is obtained. -// socket will be closed after the response -// is obtained.. -// the buffer is used for the sent as well -// as the received data. -// - - -void set_non_blocking(int sock_id) +static void set_non_blocking(int sock_id) { - int x; - x=fcntl(sock_id,F_GETFL,0); - fcntl(sock_id,F_SETFL,x | O_NONBLOCK); - fprintf(stderr,"Server- Setting server to non blocking state"); + int x; + x = fcntl(sock_id, F_GETFL, 0); + fcntl(sock_id, F_SETFL, x | O_NONBLOCK); + syslog(LOG_INFO, "Setting server to non blocking state."); } +static void Data_Send(int sockid) +{ + static int trnum; + char* out; + + int i; + char colon = ':'; + char semicolon = ';'; + int wrt_retries = 0; + int ret; + + s = NULL; + int found = 0; + + out = calloc(1, 1024); + + for (i=0;i<out_port_num;i++) + { + HASH_FIND_STR(users,Out_Port_Array[i],s); + if (strcmp(Out_Port_Array[i], s->key) == 0) + { + found=1; + break; + } + } + + if(found) + { + strncat(out, s->key, strlen(s->key)); + strncat(out, &colon, 1); + strncat(out, s->val, strlen(s->val)); + strncat(out, &semicolon, 1); + + while(1) + { + if (wrt_retries > 2) // 22.Feb.2017 - Kludge + { + free(out); + return; + } + ret = can_write_to_socket(sockid); + if (ret > 0) break; + if( ret == -100) + { + syslog(LOG_ERR,"Send aborted to CLT:%d buffer:%s ret=%d", + sockid, out,ret); + free(out); + return; + } + else // select() timed out. Retry.... + { + usleep(1000); + wrt_retries++; + } + } + } + else + { + syslog(LOG_ERR,"The %s's value not found in the table.", + Out_Port_Array[i]); + free(out); + return; + } + + if ((send(sockid, out, strlen(out), 0)) == -1) + { + syslog(LOG_ERR,"Failure sending to CLT:%d buffer:%s", sockid, out); + exit(1); + } + + syslog(LOG_INFO,"SNT:TRNUM:%d to CLT:%d buffer: %s", trnum++, sockid, out); + free(out); +} void Vhpi_Initialize(int sock_port) { DEFAULT_SERVER_PORT = sock_port; - /*Taking time info for log*/ - time_t systime; - systime = time(NULL); - - log_server=fopen("server.log","a"); - //log_file=fopen("vhpi.log","a"); - signal(SIGINT,Vhpi_Close); - signal(SIGTERM,Vhpi_Close); + signal(SIGINT,Vhpi_Exit); + signal(SIGTERM,Vhpi_Exit); + + signal(SIGUSR1, Vhpi_Exit); //10.Mar.2017 - RM int try_limit = 100; while(try_limit > 0) { - server_socket_id = create_server(DEFAULT_SERVER_PORT,DEFAULT_MAX_CONNECTIONS); - - if(server_socket_id > 0) + server_socket_id = create_server(DEFAULT_SERVER_PORT,DEFAULT_MAX_CONNECTIONS); + if(server_socket_id > 0) { - fprintf(stderr,"Server- Info:Success: Started the server on port %d\n",DEFAULT_SERVER_PORT); - fprintf(log_server,"Server -Started the server at port %d \n",DEFAULT_SERVER_PORT); + syslog(LOG_INFO,"Started the server on port %d SRV:%d", + DEFAULT_SERVER_PORT, server_socket_id); set_non_blocking(server_socket_id); break; - } else - fprintf(stderr,"Server- Info:Could not start server on port %d,will try again\n",DEFAULT_SERVER_PORT); - - usleep(1000); - try_limit--; + { + syslog(LOG_ERR,"Could not start server on port %d,will try again", + DEFAULT_SERVER_PORT); + usleep(1000); + try_limit--; - if(try_limit==0) - { - fprintf(stderr,"Server- Error:Tried to start server on port %d, failed..giving up \n",DEFAULT_SERVER_PORT); - exit(1); + if(try_limit==0) + { + syslog(LOG_ERR, + "Error:Tried to start server on port %d, failed..giving up.", + DEFAULT_SERVER_PORT); + exit(1); } - + + } } - - //fprintf(log_server,"Setup completed on server side at %s ",ctime(&systime)); - fflush(log_server); - fclose(log_server); - // //Reading Output Port name and storing in Out_Port_Array; // - char *line = NULL; - size_t len = 0; - ssize_t read; - char *token; - FILE *fp; - fp=fopen("connection_info.txt","r"); - - while ((read = getline(&line, &len, fp)) != -1) - { - if (strstr(line,"OUT") != NULL || strstr(line,"out") != NULL ) - { - strtok_r(line, " ",&token); - Out_Port_Array[out_port_num] = line; - out_port_num++; - } - line = (char *)malloc(sizeof(char)); - } + char* line = NULL; + size_t len = 0; + ssize_t read; + char *token; + FILE *fp; + struct timespec ts; + + fp=fopen("connection_info.txt","r"); + if (! fp) + { + syslog(LOG_ERR,"Vhpi_Initialize: Failed to open connection_info.txt. Exiting..."); + exit(1); + } + + line = (char*) malloc(80); + while ((read = getline(&line, &len, fp)) != -1) + { + if (strstr(line,"OUT") != NULL || strstr(line,"out") != NULL ) + { + strtok_r(line, " ",&token); + Out_Port_Array[out_port_num] = line; + out_port_num++; + } + line = (char*) malloc(80); + } + fclose(fp); + + free(line); + ts.tv_sec = 2; + ts.tv_nsec = 0; + nanosleep(&ts, NULL); - fprintf(stderr,"\n Server- Vhpi_Initialize finished \n"); - sleep(2); - fclose(fp); +// 10.Mar.2017 - RM - Create PID file for the test bench. + create_pid_file(sock_port); +} +void Vhpi_Set_Port_Value(char *port_name,char *port_value,int port_width) +{ + + s = (struct my_struct*)malloc(sizeof(struct my_struct)); + strncpy(s->key, port_name,10); + strncpy(s->val,port_value,10); + HASH_ADD_STR( users, key, s ); + +} + +void Vhpi_Get_Port_Value(char* port_name,char* port_value,int port_width) +{ + + HASH_FIND_STR(users,port_name,s); + if(s) + { + snprintf(port_value,sizeof(port_value),"%s",s->val); + + HASH_DEL(users, s); + free(s); + s=NULL; + } } void Vhpi_Listen() { - char payload[4096]; - int payload_length; - vhpi_cycle_count++; - //#ifdef DEBUG - //fprintf(log_file,"Server- Info: listening in cycle %d\n", vhpi_cycle_count); - //fflush(log_file); - //#endif int new_sock; + while(1) { - if((new_sock = connect_to_client(server_socket_id)) > 0) + new_sock=connect_to_client(server_socket_id); + if(new_sock > 0) { char receive_buffer[MAX_BUF_SIZE]; - fprintf(stderr,"Server- Info : waiting for client message \n"); - - //if the client has connected "just now" - // it must send something! - //#ifdef DEBUG - //fprintf(log_file,"Server- Info: waiting for message from client %d\n", new_sock); - //fflush(log_file); - //#endif - int n = receive_string(new_sock,receive_buffer); - - - if(n > 0) + int n = receive_string(new_sock, receive_buffer); + if(n > 0) { - - //payload_length = extract_payload(receive_buffer,payload,n); - //#ifdef DEBUG - //fprintf(log_file,"Info: received message from client %d: %s (payload-length=%d)\n",new_sock, receive_buffer,payload_length); - //fprintf(log_file,"Server- Info: received message from client %d : %s \n",new_sock,receive_buffer); - //fflush(log_file); - //#endif - - - if(strcmp(receive_buffer,"END")==0) + sendto_sock = new_sock; // 22.Feb.2017 - RM - Kludge + syslog(LOG_INFO, + "Vhpi_Listen:New socket connection CLT:%d",new_sock); + if(strcmp(receive_buffer, "END")==0) { - - log_server=fopen("server.log","a"); - fprintf(log_server,"Accept Server closing request \n"); - printf("Accept server closing request \n"); - fflush(log_server); - fclose(log_server); - Vhpi_Close(); - exit(0); - sleep(1); - //close(server_socket_id); + syslog(LOG_INFO, + "RCVD:CLOSE REQUEST from CLT:%d", new_sock); + Vhpi_Exit(0); } - - else + else { parse_buffer(new_sock,receive_buffer); - } - - //parse_buffer(new_sock,receive_buffer); break; } - } else { break; } } - //#ifdef DEBUG - //fprintf(log_file,"Server- Info: finished listening in cycle %d\n", vhpi_cycle_count); - //fflush(log_file); - //#endif } -// go down the list of finished jobs and send -// out the resulting port values.. + void Vhpi_Send() { int sockid; char* out; - //#ifdef DEBUG - //fprintf(log_file,"Server- Info: sending in cycle %d\n", vhpi_cycle_count); - //fflush(log_file); - //#endif - - log_server=fopen("server.log","a"); - - fprintf(stderr,"Server- Sending data to client \n"); - fprintf(log_server,"Sending data from server to client \n"); - HASH_FIND_STR(users,"sock_id",s); - if(s) - { - printf("Server- The key is %s and value is %s \n",s->key,s->val); - sockid=atoi(s->val); - //strncpy(sockid,10,atoi(s->val)); - //HASH_DEL(users, s); - //free(s); - } - else - { - printf("Server- The socket id not found in table \n"); - fprintf(log_server,"The socket id is not present in table \n"); - } - - //snprintf(sockid,sizeof(sockid),"%d",s->val); - - Data_Send(sockid); - fflush(log_server); - fclose(log_server); +// Traverse the list of finished jobs and send out the resulting port values.. +// 22.Feb.2017 - RM - Kludge +// log_server=fopen("server.log","a"); +// fprintf(log_server, "%s Vhpi_Send() called\n", curtim()); + +// fprintf(log_server,"Vhpi_Send()-------------------\n"); +// print_hash_table(); +// fprintf(log_server,"----------------------------------------\n"); +// HASH_FIND_STR(users,"sock_id",s); +// if(s) +// { +// sockid=atoi(s->val); +// } +// else +// { +// fprintf(log_server,"%s Socket id not in table - key=%s val=%s\n", +// curtim(), +// users->key, users->val); +// } +// Data_Send(sockid); + + if (prev_sendto_sock != sendto_sock) + { + Data_Send(sendto_sock); + prev_sendto_sock = sendto_sock; + } +// 22.Feb.2017 End kludge } -void Data_Send(int sockid) -{ - char* out; - out = (char *) malloc(sizeof(char)); - *out = '\0'; - char send_data_buf[BUFSIZ]; - int i; - char colon = ':'; - char semicolon = ';'; - for (i=0;i<out_port_num;i++) - { - HASH_FIND_STR(users,Out_Port_Array[i],s); - if(s) - { - printf("Server-Sending data has key:%s and value:%s \n",s->key,s->val); - fprintf(log_server,"Sending data has key:%s and value:%s \n",s->key,s->val); - - strncat(out, s->key, strlen(s->key)); - strncat(out, &colon, 1); - strncat(out, s->val, strlen(s->val)); - strncat(out, &semicolon, 1); - - - //HASH_DEL(users, s); - //free(s); - while(1) - { - if(can_write_to_socket(sockid)) - break; - usleep(1000); - } - - } - else - { - printf("Server- The output port's %s value Not found \n",Out_Port_Array[i]); - fprintf(log_server,"The %s's value not found in the table \n",Out_Port_Array[i]); - } - } - - strcpy(send_data_buf, out); - - if ((send(sockid, send_data_buf, sizeof(send_data_buf), 0)) == -1) - { - perror("Server- Failure Sending Message\n"); - exit(1); - } - fprintf(log_server,"Val of output buffer %s\n",send_data_buf); -} - void Vhpi_Close() { - fprintf(stderr,"Server- Info: closing VHPI link\n"); - //fclose(log_file); - close(server_socket_id); - + close(server_socket_id); + syslog(LOG_INFO, "*** Closed VHPI link. ***"); } -static void Vhpi_Exit(int sig) +void Vhpi_Exit(int sig) { - fprintf(stderr, "Server- *** Break! ***\n"); - fprintf(stderr,"Server- Info: Stopping the simulation \n"); - Vhpi_Close(); - exit(0); + Vhpi_Close(); + +// 10.Mar.2017 - RM + if (pid_file_created) + remove(pid_filename); + + syslog(LOG_INFO, "*** Exiting ***"); + + exit(0); } diff --git a/src/ghdlserver/ghdlserver.h b/src/ghdlserver/ghdlserver.h index ad82385..9dc8afc 100644 --- a/src/ghdlserver/ghdlserver.h +++ b/src/ghdlserver/ghdlserver.h @@ -1,3 +1,4 @@ +/* 18.Mar.2017 - RM - Cleaned up.*/ #include <stdlib.h> #include <stdint.h> @@ -8,7 +9,6 @@ #include <netinet/in.h> #include <netdb.h> - // Should be enough.. #define MAX_BUF_SIZE 4096 @@ -18,42 +18,16 @@ //threads talking to the TB? #define DEFAULT_MAX_CONNECTIONS 65535 - - - int DEFAULT_SERVER_PORT; -//Payload Handling -int extract_payload(char* receive_buffer,char* payload, int max_n); -//void print_payload(FILE* log_file,char* send_buffer, int wlength, int nwords); -//void pack_value(char* payload,int wlength,int offset, char* port_value); -//void unpack_value(char* payload,int wlength,int offset, char* port_value); - - -//Create Server to listen for message -int create_server(int port_number, int max_connections); - - -int connect_to_client(int server_fd); - -int can_read_from_socket(int socket_id); - -int can_write_to_socket(int socket_id); - -int receive_string(int n, char* buffer); - -void send_packet_and_wait_for_response(char* buffer, int send_length, char* server_host_name, int server_port_number); - -void set_non_blocking(int sock_id); -void Data_Send(int sockid); - -//Vhpi Function -void Vhpi_Initialize(int sock_port); +//Vhpi Functions. +void Vhpi_Initialize(int sock_port); void Vhpi_Close(); +void Vhpi_Exit(); void Vhpi_Listen(); void Vhpi_Send(); void Vhpi_Set_Port_Value(char* reg_name, char* reg_value, int port_width); void Vhpi_Get_Port_Value(char* reg_name, char* reg_value, int port_width); -void Vhpi_Log(char* message_string); + diff --git a/src/outitf.c b/src/outitf.c new file mode 100644 index 0000000..ce3e910 --- /dev/null +++ b/src/outitf.c @@ -0,0 +1,1354 @@ +/********** +Copyright 1990 Regents of the University of California. All rights reserved. +Author: 1988 Wayne A. Christopher, U. C. Berkeley CAD Group +Modified: 2000 AlansFixes +**********/ +/************************************************************************** + * 10.Mar.2017 - RM - Added a dirty fix to handle orphan FOSSEE test bench + * processes. The following static functions were added in the process: + * o nghdl_orphan_tb() + * o nghdl_tb_SIGUSR1() + ************************************************************************** + */ +/* + * This module replaces the old "writedata" routines in nutmeg. + * Unlike the writedata routines, the OUT routines are only called by + * the simulator routines, and only call routines in nutmeg. The rest + * of nutmeg doesn't deal with OUT at all. + */ + +#include "ngspice/ngspice.h" +#include "ngspice/cpdefs.h" +#include "ngspice/ftedefs.h" +#include "ngspice/dvec.h" +#include "ngspice/plot.h" +#include "ngspice/sim.h" +#include "ngspice/inpdefs.h" /* for INPtables */ +#include "ngspice/ifsim.h" +#include "ngspice/jobdefs.h" +#include "ngspice/iferrmsg.h" +#include "circuits.h" +#include "outitf.h" +#include "variable.h" +#include <fcntl.h> +#include "ngspice/cktdefs.h" +#include "ngspice/inpdefs.h" +#include "breakp2.h" +#include "runcoms.h" +#include "plotting/graf.h" +#include "../misc/misc_time.h" + +/* 10.Mar.2917 - RM - Added the following #include */ +#include <dirent.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +extern char *spice_analysis_get_name(int index); +extern char *spice_analysis_get_description(int index); + + +static int beginPlot(JOB *analysisPtr, CKTcircuit *circuitPtr, char *cktName, char *analName, + char *refName, int refType, int numNames, char **dataNames, int dataType, + bool windowed, runDesc **runp); +static int addDataDesc(runDesc *run, char *name, int type, int ind); +static int addSpecialDesc(runDesc *run, char *name, char *devname, char *param, int depind); +static void fileInit(runDesc *run); +static void fileInit_pass2(runDesc *run); +static void fileStartPoint(FILE *fp, bool bin, int num); +static void fileAddRealValue(FILE *fp, bool bin, double value); +static void fileAddComplexValue(FILE *fp, bool bin, IFcomplex value); +static void fileEndPoint(FILE *fp, bool bin); +static void fileEnd(runDesc *run); +static void plotInit(runDesc *run); +static void plotAddRealValue(dataDesc *desc, double value); +static void plotAddComplexValue(dataDesc *desc, IFcomplex value); +static void plotEnd(runDesc *run); +static bool parseSpecial(char *name, char *dev, char *param, char *ind); +static bool name_eq(char *n1, char *n2); +static bool getSpecial(dataDesc *desc, runDesc *run, IFvalue *val); +static void freeRun(runDesc *run); + +/*Output data to spice module*/ +#ifdef TCL_MODULE +#include "ngspice/tclspice.h" +#elif defined SHARED_MODULE +extern int sh_ExecutePerLoop(void); +extern void sh_vecinit(runDesc *run); +#endif + +/*Suppressing progress info in -o option */ +#ifndef HAS_WINGUI +extern bool orflag; +#endif + + +#define DOUBLE_PRECISION 15 + + +static clock_t lastclock, currclock; +static double *rowbuf; +static size_t column, rowbuflen; + +static bool shouldstop = FALSE; /* Tell simulator to stop next time it asks. */ + +/* 10.Mar.2017 - RM - Added nghdl_tb_SIGUSR1().*/ +static void nghdl_tb_SIGUSR1(char* pid_file) +{ + int ret; + char line[80]; + char* nptr; + + FILE* fp = fopen(pid_file, "r"); + + if (fp) + { + if (fscanf(fp, "%s", line) != EOF) + { + fclose(fp); + pid_t pid = (pid_t) strtol(line, &nptr, 10); + // PID is converted to a decimal value. + if ((errno != ERANGE) && (errno!= EINVAL)) + { + if (pid) + { + // Check if a process with this pid really exists. + ret = kill(pid, 0); + if (ret == 0) + { + kill(pid, SIGUSR1); + } + } + } + } + } +} + +/* 10.Mar.2017 - RM - Added nghdl_orphan_tb().*/ +static void nghdl_orphan_tb(void) +{ + struct dirent* dirp; + DIR* dirfd; + char* dir = "/tmp"; + char filename_tmp[1024]; + char pid_file_prefix[256]; + + sprintf(pid_file_prefix, "NGHDL_%d", getpid()); + + if ((dirfd = opendir(dir)) == NULL) + { + fprintf(stderr, "nghdl_orphan_tb(): Cannot open /tmp\n"); + return; + } + +/* Loop through /tmp directories looking for "NGHDL_<my pid>*" files.*/ + while ((dirp = readdir(dirfd)) != NULL) + { + struct stat stbuf; + sprintf(filename_tmp, "/tmp/%s", dirp->d_name); + if (strstr(filename_tmp, pid_file_prefix)) + { + if (stat(filename_tmp, &stbuf) == -1) + { + fprintf(stderr, + "nghdl_orphan_tb: stat() failed; ERRNO=%d on file:%s\n", + errno, filename_tmp); + continue; + } + + if ((stbuf.st_mode & S_IFMT) == S_IFDIR) + { + continue; + } + else + { + nghdl_tb_SIGUSR1(filename_tmp); + } + } + } +} +/* End 10.Mar.2017 - RM */ + +/* The two "begin plot" routines share all their internals... */ + +int +OUTpBeginPlot(CKTcircuit *circuitPtr, JOB *analysisPtr, + IFuid analName, + IFuid refName, int refType, + int numNames, IFuid *dataNames, int dataType, runDesc **plotPtr) +{ + char *name; + + if (ft_curckt->ci_ckt == circuitPtr) + name = ft_curckt->ci_name; + else + name = "circuit name"; + + return (beginPlot(analysisPtr, circuitPtr, name, + analName, refName, refType, numNames, + dataNames, dataType, FALSE, + plotPtr)); +} + + +int +OUTwBeginPlot(CKTcircuit *circuitPtr, JOB *analysisPtr, + IFuid analName, + IFuid refName, int refType, + int numNames, IFuid *dataNames, int dataType, runDesc **plotPtr) +{ + + return (beginPlot(analysisPtr, circuitPtr, "circuit name", + analName, refName, refType, numNames, + dataNames, dataType, TRUE, + plotPtr)); +} + + +static int +beginPlot(JOB *analysisPtr, CKTcircuit *circuitPtr, char *cktName, char *analName, char *refName, int refType, int numNames, char **dataNames, int dataType, bool windowed, runDesc **runp) +{ + runDesc *run; + struct save_info *saves; + bool *savesused = NULL; + int numsaves; + int i, j, depind = 0; + char namebuf[BSIZE_SP], parambuf[BSIZE_SP], depbuf[BSIZE_SP]; + char *ch, tmpname[BSIZE_SP]; + bool saveall = TRUE; + bool savealli = FALSE; + char *an_name; + /*to resume a run saj + *All it does is reassign the file pointer and return (requires *runp to be NULL if this is not needed) + */ + if (dataType == 666 && numNames == 666) { + run = *runp; + run->writeOut = ft_getOutReq(&run->fp, &run->runPlot, &run->binary, + run->type, run->name); + + } else { + /*end saj*/ + + /* Check to see if we want to print informational data. */ + if (cp_getvar("printinfo", CP_BOOL, NULL)) + fprintf(cp_err, "(debug printing enabled)\n"); + + *runp = run = alloc(struct runDesc); + + /* First fill in some general information. */ + run->analysis = analysisPtr; + run->circuit = circuitPtr; + run->name = copy(cktName); + run->type = copy(analName); + run->windowed = windowed; + run->numData = 0; + + an_name = spice_analysis_get_name(analysisPtr->JOBtype); + ft_curckt->ci_last_an = an_name; + + /* Now let's see which of these things we need. First toss in the + * reference vector. Then toss in anything that getSaves() tells + * us to save that we can find in the name list. Finally unpack + * the remaining saves into parameters. + */ + numsaves = ft_getSaves(&saves); + if (numsaves) { + savesused = TMALLOC(bool, numsaves); + saveall = FALSE; + for (i = 0; i < numsaves; i++) { + if (saves[i].analysis && !cieq(saves[i].analysis, an_name)) { + /* ignore this one this time around */ + savesused[i] = TRUE; + continue; + } + + /* Check for ".save all" and new synonym ".save allv" */ + + if (cieq(saves[i].name, "all") || cieq(saves[i].name, "allv")) { + saveall = TRUE; + savesused[i] = TRUE; + saves[i].used = 1; + continue; + } + + /* And now for the new ".save alli" option */ + + if (cieq(saves[i].name, "alli")) { + savealli = TRUE; + savesused[i] = TRUE; + saves[i].used = 1; + continue; + } + } + } + + /* Pass 0. */ + if (refName) { + addDataDesc(run, refName, refType, -1); + for (i = 0; i < numsaves; i++) + if (!savesused[i] && name_eq(saves[i].name, refName)) { + savesused[i] = TRUE; + saves[i].used = 1; + } + } else { + run->refIndex = -1; + } + + + /* Pass 1. */ + if (numsaves && !saveall) { + for (i = 0; i < numsaves; i++) + if (!savesused[i]) + for (j = 0; j < numNames; j++) + if (name_eq(saves[i].name, dataNames[j])) { + addDataDesc(run, dataNames[j], dataType, j); + savesused[i] = TRUE; + saves[i].used = 1; + break; + } + } else { + for (i = 0; i < numNames; i++) + if (!refName || !name_eq(dataNames[i], refName)) + /* Save the node as long as it's an internal device node */ + if (!strstr(dataNames[i], "#internal") && + !strstr(dataNames[i], "#source") && + !strstr(dataNames[i], "#drain") && + !strstr(dataNames[i], "#collector") && + !strstr(dataNames[i], "#emitter") && + !strstr(dataNames[i], "#base")) + { + addDataDesc(run, dataNames[i], dataType, i); + } + } + + /* Pass 1 and a bit. + This is a new pass which searches for all the internal device + nodes, and saves the terminal currents instead */ + + if (savealli) { + depind = 0; + for (i = 0; i < numNames; i++) { + if (strstr(dataNames[i], "#internal") || + strstr(dataNames[i], "#source") || + strstr(dataNames[i], "#drain") || + strstr(dataNames[i], "#collector") || + strstr(dataNames[i], "#emitter") || + strstr(dataNames[i], "#base")) + { + tmpname[0] = '@'; + tmpname[1] = '\0'; + strncat(tmpname, dataNames[i], BSIZE_SP-1); + ch = strchr(tmpname, '#'); + + if (strstr(ch, "#collector")) { + strcpy(ch, "[ic]"); + } else if (strstr(ch, "#base")) { + strcpy(ch, "[ib]"); + } else if (strstr(ch, "#emitter")) { + strcpy(ch, "[ie]"); + if (parseSpecial(tmpname, namebuf, parambuf, depbuf)) + addSpecialDesc(run, tmpname, namebuf, parambuf, depind); + strcpy(ch, "[is]"); + } else if (strstr(ch, "#drain")) { + strcpy(ch, "[id]"); + if (parseSpecial(tmpname, namebuf, parambuf, depbuf)) + addSpecialDesc(run, tmpname, namebuf, parambuf, depind); + strcpy(ch, "[ig]"); + } else if (strstr(ch, "#source")) { + strcpy(ch, "[is]"); + if (parseSpecial(tmpname, namebuf, parambuf, depbuf)) + addSpecialDesc(run, tmpname, namebuf, parambuf, depind); + strcpy(ch, "[ib]"); + } else if (strstr(ch, "#internal") && (tmpname[1] == 'd')) { + strcpy(ch, "[id]"); + } else { + fprintf(cp_err, + "Debug: could output current for %s\n", tmpname); + continue; + } + if (parseSpecial(tmpname, namebuf, parambuf, depbuf)) { + if (*depbuf) { + fprintf(stderr, + "Warning : unexpected dependent variable on %s\n", tmpname); + } else { + addSpecialDesc(run, tmpname, namebuf, parambuf, depind); + } + } + } + } + } + + + /* Pass 2. */ + for (i = 0; i < numsaves; i++) { + + if (savesused[i]) + continue; + + if (!parseSpecial(saves[i].name, namebuf, parambuf, depbuf)) { + if (saves[i].analysis) + fprintf(cp_err, "Warning: can't parse '%s': ignored\n", + saves[i].name); + continue; + } + + /* Now, if there's a dep variable, do we already have it? */ + if (*depbuf) { + for (j = 0; j < run->numData; j++) + if (name_eq(depbuf, run->data[j].name)) + break; + if (j == run->numData) { + /* Better add it. */ + for (j = 0; j < numNames; j++) + if (name_eq(depbuf, dataNames[j])) + break; + if (j == numNames) { + fprintf(cp_err, + "Warning: can't find '%s': value '%s' ignored\n", + depbuf, saves[i].name); + continue; + } + addDataDesc(run, dataNames[j], dataType, j); + savesused[i] = TRUE; + saves[i].used = 1; + depind = j; + } else { + depind = run->data[j].outIndex; + } + } + + addSpecialDesc(run, saves[i].name, namebuf, parambuf, depind); + } + + if (numsaves) { + for (i = 0; i < numsaves; i++) { + tfree(saves[i].analysis); + tfree(saves[i].name); + } + tfree(saves); + tfree(savesused); + } + + if (numNames && + ((run->numData == 1 && run->refIndex != -1) || + (run->numData == 0 && run->refIndex == -1))) + { + fprintf(cp_err, "Error: no data saved for %s; analysis not run\n", + spice_analysis_get_description(analysisPtr->JOBtype)); + return E_NOTFOUND; + } + + /* Now that we have our own data structures built up, let's see what + * nutmeg wants us to do. + */ + run->writeOut = ft_getOutReq(&run->fp, &run->runPlot, &run->binary, + run->type, run->name); + + if (run->writeOut) { + fileInit(run); + } else { + plotInit(run); + if (refName) + run->runPlot->pl_ndims = 1; + } + } + + /*Start BLT, initilises the blt vectors saj*/ +#ifdef TCL_MODULE + blt_init(run); +#elif defined SHARED_MODULE + sh_vecinit(run); +#endif + + return (OK); +} + + +static int +addDataDesc(runDesc *run, char *name, int type, int ind) +{ + dataDesc *data; + + if (!run->numData) + run->data = TMALLOC(dataDesc, 1); + else + run->data = TREALLOC(dataDesc, run->data, run->numData + 1); + + data = &run->data[run->numData]; + /* so freeRun will get nice NULL pointers for the fields we don't set */ + bzero(data, sizeof(dataDesc)); + + data->name = copy(name); + data->type = type; + data->gtype = GRID_LIN; + data->regular = TRUE; + data->outIndex = ind; + + /* It's the reference vector. */ + if (ind == -1) + run->refIndex = run->numData; + + run->numData++; + + return (OK); +} + + +static int +addSpecialDesc(runDesc *run, char *name, char *devname, char *param, int depind) +{ + dataDesc *data; + char *unique; /* unique char * from back-end */ + + if (!run->numData) + run->data = TMALLOC(dataDesc, 1); + else + run->data = TREALLOC(dataDesc, run->data, run->numData + 1); + + data = &run->data[run->numData]; + /* so freeRun will get nice NULL pointers for the fields we don't set */ + bzero(data, sizeof(dataDesc)); + + data->name = copy(name); + + unique = copy(devname); + + /* MW. My "special" routine here */ + INPinsertNofree(&unique, ft_curckt->ci_symtab); + data->specName = unique; + + data->specParamName = copy(param); + + data->specIndex = depind; + data->specType = -1; + data->specFast = NULL; + data->regular = FALSE; + + run->numData++; + + return (OK); +} + + +int +OUTpData(runDesc *plotPtr, IFvalue *refValue, IFvalue *valuePtr) +{ + runDesc *run = plotPtr; // FIXME + int i; + + run->pointCount++; + +#ifdef TCL_MODULE + steps_completed = run->pointCount; +#endif + + if (run->writeOut) { + + if (run->pointCount == 1) + fileInit_pass2(run); + + fileStartPoint(run->fp, run->binary, run->pointCount); + + if (run->refIndex != -1) { + if (run->isComplex) { + fileAddComplexValue(run->fp, run->binary, refValue->cValue); + + /* While we're looking at the reference value, print it to the screen + every quarter of a second, to give some feedback without using + too much CPU time */ +#ifndef HAS_WINGUI + if (!orflag) { + currclock = clock(); + if ((currclock-lastclock) > (0.25*CLOCKS_PER_SEC)) { + fprintf(stderr, " Reference value : % 12.5e\r", + refValue->cValue.real); + lastclock = currclock; + } + } +#endif + } else { + + /* And the same for a non-complex value */ + + fileAddRealValue(run->fp, run->binary, refValue->rValue); +#ifndef HAS_WINGUI + if (!orflag) { + currclock = clock(); + if ((currclock-lastclock) > (0.25*CLOCKS_PER_SEC)) { + fprintf(stderr, " Reference value : % 12.5e\r", + refValue->rValue); + lastclock = currclock; + } + } +#endif + } + } + + for (i = 0; i < run->numData; i++) { + /* we've already printed reference vec first */ + if (run->data[i].outIndex == -1) + continue; + +#ifdef TCL_MODULE + blt_add(i, refValue ? refValue->rValue : NAN); +#endif + + if (run->data[i].regular) { + if (run->data[i].type == IF_REAL) + fileAddRealValue(run->fp, run->binary, + valuePtr->v.vec.rVec [run->data[i].outIndex]); + else if (run->data[i].type == IF_COMPLEX) + fileAddComplexValue(run->fp, run->binary, + valuePtr->v.vec.cVec [run->data[i].outIndex]); + else + fprintf(stderr, "OUTpData: unsupported data type\n"); + } else { + IFvalue val; + /* should pre-check instance */ + if (!getSpecial(&run->data[i], run, &val)) { + + /* If this is the first data point, print a warning for any unrecognized + variables, since this has not already been checked */ + + if (run->pointCount == 1) + fprintf(stderr, "Warning: unrecognized variable - %s\n", + run->data[i].name); + + if (run->isComplex) { + val.cValue.real = 0; + val.cValue.imag = 0; + fileAddComplexValue(run->fp, run->binary, val.cValue); + } else { + val.rValue = 0; + fileAddRealValue(run->fp, run->binary, val.rValue); + } + + continue; + } + + if (run->data[i].type == IF_REAL) + fileAddRealValue(run->fp, run->binary, val.rValue); + else if (run->data[i].type == IF_COMPLEX) + fileAddComplexValue(run->fp, run->binary, val.cValue); + else + fprintf(stderr, "OUTpData: unsupported data type\n"); + } + +#ifdef TCL_MODULE + blt_add(i, valuePtr->v.vec.rVec [run->data[i].outIndex]); +#endif + + } + + + fileEndPoint(run->fp, run->binary); + + /* Check that the write to disk completed successfully, otherwise abort */ + + if (ferror(run->fp)) { + fprintf(stderr, "Warning: rawfile write error !!\n"); + shouldstop = TRUE; + } + + } else { + + /* This is interactive mode. Update the screen with the reference + variable just the same */ + +#ifndef HAS_WINGUI + if (!orflag) { + currclock = clock(); + if ((currclock-lastclock) > (0.25*CLOCKS_PER_SEC)) { + if (run->isComplex) { + fprintf(stderr, " Reference value : % 12.5e\r", + refValue ? refValue->cValue.real : NAN); + } else { + fprintf(stderr, " Reference value : % 12.5e\r", + refValue ? refValue->rValue : NAN); + } + lastclock = currclock; + } + } +#endif + + for (i = 0; i < run->numData; i++) { + +#ifdef TCL_MODULE + /*Locks the blt vector to stop access*/ + blt_lockvec(i); +#endif + + if (run->data[i].outIndex == -1) { + if (run->data[i].type == IF_REAL) + plotAddRealValue(&run->data[i], refValue->rValue); + else if (run->data[i].type == IF_COMPLEX) + plotAddComplexValue(&run->data[i], refValue->cValue); + } else if (run->data[i].regular) { + if (run->data[i].type == IF_REAL) + plotAddRealValue(&run->data[i], + valuePtr->v.vec.rVec[run->data[i].outIndex]); + else if (run->data[i].type == IF_COMPLEX) + plotAddComplexValue(&run->data[i], + valuePtr->v.vec.cVec[run->data[i].outIndex]); + } else { + IFvalue val; + /* should pre-check instance */ + if (!getSpecial(&run->data[i], run, &val)) + continue; + if (run->data[i].type == IF_REAL) + plotAddRealValue(&run->data[i], val.rValue); + else if (run->data[i].type == IF_COMPLEX) + plotAddComplexValue(&run->data[i], val.cValue); + else + fprintf(stderr, "OUTpData: unsupported data type\n"); + } + +#ifdef TCL_MODULE + /*relinks and unlocks vector*/ + blt_relink(i, (run->data[i]).vec); +#endif + + } + + gr_iplot(run->runPlot); + } + + if (ft_bpcheck(run->runPlot, run->pointCount) == FALSE) + shouldstop = TRUE; + +#ifdef TCL_MODULE + Tcl_ExecutePerLoop(); +#elif defined SHARED_MODULE + sh_ExecutePerLoop(); +#endif + + return (OK); +} + + +/* ARGSUSED */ /* until some code gets written */ +int +OUTwReference(void *plotPtr, IFvalue *valuePtr, void **refPtr) +{ + NG_IGNORE(refPtr); + NG_IGNORE(valuePtr); + NG_IGNORE(plotPtr); + + return (OK); +} + + +/* ARGSUSED */ /* until some code gets written */ +int +OUTwData(runDesc *plotPtr, int dataIndex, IFvalue *valuePtr, void *refPtr) +{ + NG_IGNORE(refPtr); + NG_IGNORE(valuePtr); + NG_IGNORE(dataIndex); + NG_IGNORE(plotPtr); + + return (OK); +} + + +/* ARGSUSED */ /* until some code gets written */ +int +OUTwEnd(runDesc *plotPtr) +{ + NG_IGNORE(plotPtr); + + return (OK); +} + + +int +OUTendPlot(runDesc *plotPtr) +{ + runDesc *run = plotPtr; // FIXME + + if (run->writeOut) { + fileEnd(run); + } else { + gr_end_iplot(); + plotEnd(run); + } + + freeRun(run); + + return (OK); +} + + +/* ARGSUSED */ /* until some code gets written */ +int +OUTbeginDomain(runDesc *plotPtr, IFuid refName, int refType, IFvalue *outerRefValue) +{ + NG_IGNORE(outerRefValue); + NG_IGNORE(refType); + NG_IGNORE(refName); + NG_IGNORE(plotPtr); + + return (OK); +} + + +/* ARGSUSED */ /* until some code gets written */ +int +OUTendDomain(runDesc *plotPtr) +{ + NG_IGNORE(plotPtr); + + return (OK); +} + + +/* ARGSUSED */ /* until some code gets written */ +int +OUTattributes(runDesc *plotPtr, IFuid varName, int param, IFvalue *value) +{ + runDesc *run = plotPtr; // FIXME + GRIDTYPE type; + + struct dvec *d; + + NG_IGNORE(value); + + if (param == OUT_SCALE_LIN) + type = GRID_LIN; + else if (param == OUT_SCALE_LOG) + type = GRID_XLOG; + else + return E_UNSUPP; + + if (run->writeOut) { + if (varName) { + int i; + for (i = 0; i < run->numData; i++) + if (!strcmp(varName, run->data[i].name)) + run->data[i].gtype = type; + } else { + run->data[run->refIndex].gtype = type; + } + } else { + if (varName) { + for (d = run->runPlot->pl_dvecs; d; d = d->v_next) + if (!strcmp(varName, d->v_name)) + d->v_gridtype = type; + } else if (param == PLOT_COMB) { + for (d = run->runPlot->pl_dvecs; d; d = d->v_next) + d->v_plottype = PLOT_COMB; + } else { + run->runPlot->pl_scale->v_gridtype = type; + } + } + + return (OK); +} + + +/* The file writing routines. */ + +static void +fileInit(runDesc *run) +{ + char buf[513]; + int i; + size_t n; + + lastclock = clock(); + + /* This is a hack. */ + run->isComplex = FALSE; + for (i = 0; i < run->numData; i++) + if (run->data[i].type == IF_COMPLEX) + run->isComplex = TRUE; + + n = 0; + sprintf(buf, "Title: %s\n", run->name); + n += strlen(buf); + fputs(buf, run->fp); + sprintf(buf, "Date: %s\n", datestring()); + n += strlen(buf); + fputs(buf, run->fp); + sprintf(buf, "Plotname: %s\n", run->type); + n += strlen(buf); + fputs(buf, run->fp); + sprintf(buf, "Flags: %s\n", run->isComplex ? "complex" : "real"); + n += strlen(buf); + fputs(buf, run->fp); + sprintf(buf, "No. Variables: %d\n", run->numData); + n += strlen(buf); + fputs(buf, run->fp); + sprintf(buf, "No. Points: "); + n += strlen(buf); + fputs(buf, run->fp); + + fflush(run->fp); /* Gotta do this for LATTICE. */ + if (run->fp == stdout || (run->pointPos = ftell(run->fp)) <= 0) + run->pointPos = (long) n; + fprintf(run->fp, "0 \n"); /* Save 8 spaces here. */ + + /*fprintf(run->fp, "Command: version %s\n", ft_sim->version);*/ + fprintf(run->fp, "Variables:\n"); + + printf("No. of Data Columns : %d \n", run->numData); +} + + +static void +fileInit_pass2(runDesc *run) +{ + int i, type; + + for (i = 0; i < run->numData; i++) { + + char *name = run->data[i].name; + + if (substring("#branch", name)) + type = SV_CURRENT; + else if (cieq(name, "time")) + type = SV_TIME; + else if (cieq(name, "frequency")) + type = SV_FREQUENCY; + else if (cieq(name, "temp-sweep")) + type = SV_TEMP; + else if (cieq(name, "res-sweep")) + type = SV_RES; + else if ((*name == '@') && (substring("[g", name))) + type = SV_ADMITTANCE; + else if ((*name == '@') && (substring("[c", name))) + type = SV_CAPACITANCE; + else if ((*name == '@') && (substring("[i", name))) + type = SV_CURRENT; + else if ((*name == '@') && (substring("[q", name))) + type = SV_CHARGE; + else + type = SV_VOLTAGE; + + if (type == SV_CURRENT) { + char *branch = strstr(name, "#branch"); + if (branch) + *branch = '\0'; + fprintf(run->fp, "\t%d\ti(%s)\t%s", i, name, ft_typenames(type)); + if (branch) + *branch = '#'; + } else if (type == SV_VOLTAGE) { + fprintf(run->fp, "\t%d\tv(%s)\t%s", i, name, ft_typenames(type)); + } else { + fprintf(run->fp, "\t%d\t%s\t%s", i, name, ft_typenames(type)); + } + + if (run->data[i].gtype == GRID_XLOG) + fprintf(run->fp, "\tgrid=3"); + + fprintf(run->fp, "\n"); + } + + fprintf(run->fp, "%s:\n", run->binary ? "Binary" : "Values"); + fflush(run->fp); + + /* Allocate Row buffer */ + + if (run->binary) { + rowbuflen = (size_t) (run->numData); + if (run->isComplex) + rowbuflen *= 2; + rowbuf = TMALLOC(double, rowbuflen); + } else { + // fIXME rowbuflen = 0; + rowbuf = NULL; + } +} + + +static void +fileStartPoint(FILE *fp, bool bin, int num) +{ + if (!bin) + fprintf(fp, "%d\t", num - 1); + + /* reset buffer pointer to zero */ + + column = 0; +} + + +static void +fileAddRealValue(FILE *fp, bool bin, double value) +{ + if (bin) + rowbuf[column++] = value; + else + fprintf(fp, "\t%.*e\n", DOUBLE_PRECISION, value); +} + + +static void +fileAddComplexValue(FILE *fp, bool bin, IFcomplex value) +{ + if (bin) { + rowbuf[column++] = value.real; + rowbuf[column++] = value.imag; + } else { + fprintf(fp, "\t%.*e,%.*e\n", DOUBLE_PRECISION, value.real, + DOUBLE_PRECISION, value.imag); + } +} + + +/* ARGSUSED */ /* until some code gets written */ +static void +fileEndPoint(FILE *fp, bool bin) +{ + /* write row buffer to file */ + /* otherwise the data has already been written */ + + if (bin) + fwrite(rowbuf, sizeof(double), rowbuflen, fp); +} + + +/* Here's the hack... Run back and fill in the number of points. */ + +static void +fileEnd(runDesc *run) +{ +/* 10.Mar.2017 - RM - Check if any orphan test benches are running. If any are + * found, force them to exit. + */ + nghdl_orphan_tb(); + +/* End 10.Mar.2017 */ + + if (run->fp != stdout) { + long place = ftell(run->fp); + fseek(run->fp, run->pointPos, SEEK_SET); + fprintf(run->fp, "%d", run->pointCount); + fprintf(stdout, "\nNo. of Data Rows : %d\n", run->pointCount); + fseek(run->fp, place, SEEK_SET); + } else { + /* Yet another hack-around */ + fprintf(stderr, "@@@ %ld %d\n", run->pointPos, run->pointCount); + } + + fflush(run->fp); + + if (run->binary) { + /* deallocate row buffer */ + tfree(rowbuf); + } +} + + +/* The plot maintenance routines. */ + +static void +plotInit(runDesc *run) +{ + struct plot *pl = plot_alloc(run->type); + char buf[100]; + struct dvec *v; + dataDesc *dd; + int i; + + pl->pl_title = copy(run->name); + pl->pl_name = copy(run->type); + pl->pl_date = copy(datestring()); + pl->pl_ndims = 0; + plot_new(pl); + plot_setcur(pl->pl_typename); + run->runPlot = pl; + + /* This is a hack. */ + /* if any of them complex, make them all complex */ + run->isComplex = FALSE; + for (i = 0; i < run->numData; i++) + if (run->data[i].type == IF_COMPLEX) + run->isComplex = TRUE; + + for (i = 0; i < run->numData; i++) { + dd = &run->data[i]; + v = alloc(struct dvec); + if (isdigit(*dd->name)) { + (void) sprintf(buf, "V(%s)", dd->name); + v->v_name = copy(buf); + } else { + v->v_name = copy(dd->name); + } + if (substring("#branch", v->v_name)) + v->v_type = SV_CURRENT; + else if (cieq(v->v_name, "time")) + v->v_type = SV_TIME; + else if (cieq(v->v_name, "frequency")) + v->v_type = SV_FREQUENCY; + else if (cieq(v->v_name, "onoise_spectrum")) + v->v_type = SV_OUTPUT_N_DENS; + else if (cieq(v->v_name, "onoise_integrated")) + v->v_type = SV_OUTPUT_NOISE; + else if (cieq(v->v_name, "inoise_spectrum")) + v->v_type = SV_INPUT_N_DENS; + else if (cieq(v->v_name, "inoise_integrated")) + v->v_type = SV_INPUT_NOISE; + else if (cieq(v->v_name, "temp-sweep")) + v->v_type = SV_TEMP; + else if (cieq(v->v_name, "res-sweep")) + v->v_type = SV_RES; + else if ((*(v->v_name) == '@') && (substring("[g", v->v_name))) + v->v_type = SV_ADMITTANCE; + else if ((*(v->v_name) == '@') && (substring("[c", v->v_name))) + v->v_type = SV_CAPACITANCE; + else if ((*(v->v_name) == '@') && (substring("[i", v->v_name))) + v->v_type = SV_CURRENT; + else if ((*(v->v_name) == '@') && (substring("[q", v->v_name))) + v->v_type = SV_CHARGE; + else + v->v_type = SV_VOLTAGE; + v->v_length = 0; + v->v_scale = NULL; + if (!run->isComplex) { + v->v_flags = VF_REAL; + v->v_realdata = NULL; + } else { + v->v_flags = VF_COMPLEX; + v->v_compdata = NULL; + } + + v->v_flags |= VF_PERMANENT; + + vec_new(v); + dd->vec = v; + } +} + + +static void +plotAddRealValue(dataDesc *desc, double value) +{ + struct dvec *v = desc->vec; + + if (isreal(v)) { + v->v_realdata = TREALLOC(double, v->v_realdata, v->v_length + 1); + v->v_realdata[v->v_length] = value; + } else { + /* a real parading as a VF_COMPLEX */ + v->v_compdata = TREALLOC(ngcomplex_t, v->v_compdata, v->v_length + 1); + v->v_compdata[v->v_length].cx_real = value; + v->v_compdata[v->v_length].cx_imag = 0.0; + } + + v->v_length++; + v->v_dims[0] = v->v_length; /* va, must be updated */ +} + + +static void +plotAddComplexValue(dataDesc *desc, IFcomplex value) +{ + struct dvec *v = desc->vec; + + v->v_compdata = TREALLOC(ngcomplex_t, v->v_compdata, v->v_length + 1); + v->v_compdata[v->v_length].cx_real = value.real; + v->v_compdata[v->v_length].cx_imag = value.imag; + + v->v_length++; + v->v_dims[0] = v->v_length; /* va, must be updated */ +} + + +/* ARGSUSED */ /* until some code gets written */ +static void +plotEnd(runDesc *run) +{ +/* 10.Mar.2017 - RM */ + nghdl_orphan_tb(); +/* End 10.Mar.2017 */ + + fprintf(stderr, "\n"); + fprintf(stdout, "\nNo. of Data Rows : %d\n", run->pointCount); +} + +/* ParseSpecial takes something of the form "@name[param,index]" and rips + * out name, param, andstrchr. + */ + +static bool +parseSpecial(char *name, char *dev, char *param, char *ind) +{ + char *s; + + *dev = *param = *ind = '\0'; + + if (*name != '@') + return FALSE; + name++; + + s = dev; + while (*name && (*name != '[')) + *s++ = *name++; + *s = '\0'; + + if (!*name) + return TRUE; + name++; + + s = param; + while (*name && (*name != ',') && (*name != ']')) + *s++ = *name++; + *s = '\0'; + + if (*name == ']') + return (!name[1] ? TRUE : FALSE); + else if (!*name) + return FALSE; + name++; + + s = ind; + while (*name && (*name != ']')) + *s++ = *name++; + *s = '\0'; + + if (*name && !name[1]) + return TRUE; + else + return FALSE; +} + + +/* This routine must match two names with or without a V() around them. */ + +static bool +name_eq(char *n1, char *n2) +{ + char buf1[BSIZE_SP], buf2[BSIZE_SP], *s; + + if ((s = strchr(n1, '(')) != NULL) { + strcpy(buf1, s); + if ((s = strchr(buf1, ')')) == NULL) + return FALSE; + *s = '\0'; + n1 = buf1; + } + + if ((s = strchr(n2, '(')) != NULL) { + strcpy(buf2, s); + if ((s = strchr(buf2, ')')) == NULL) + return FALSE; + *s = '\0'; + n2 = buf2; + } + + return (strcmp(n1, n2) ? FALSE : TRUE); +} + + +static bool +getSpecial(dataDesc *desc, runDesc *run, IFvalue *val) +{ + IFvalue selector; + struct variable *vv; + + selector.iValue = desc->specIndex; + if (INPaName(desc->specParamName, val, run->circuit, &desc->specType, + desc->specName, &desc->specFast, ft_sim, &desc->type, + &selector) == OK) { + desc->type &= (IF_REAL | IF_COMPLEX); /* mask out other bits */ + return TRUE; + } + + if ((vv = if_getstat(run->circuit, &desc->name[1])) != NULL) { + /* skip @ sign */ + desc->type = IF_REAL; + if (vv->va_type == CP_REAL) + val->rValue = vv->va_real; + else if (vv->va_type == CP_NUM) + val->rValue = vv->va_num; + else if (vv->va_type == CP_BOOL) + val->rValue = (vv->va_bool ? 1.0 : 0.0); + else + return FALSE; /* not a real */ + tfree(vv); + return TRUE; + } + + return FALSE; +} + + +static void +freeRun(runDesc *run) +{ + int i; + + for (i = 0; i < run->numData; i++) { + tfree(run->data[i].name); + tfree(run->data[i].specParamName); + } + + tfree(run->data); + tfree(run->type); + tfree(run->name); + + tfree(run); +} + + +int +OUTstopnow(void) +{ + if (ft_intrpt || shouldstop) { + ft_intrpt = shouldstop = FALSE; + return (1); + } + + return (0); +} + + +/* Print out error messages. */ + +static struct mesg { + char *string; + long flag; +} msgs[] = { + { "Warning", ERR_WARNING } , + { "Fatal error", ERR_FATAL } , + { "Panic", ERR_PANIC } , + { "Note", ERR_INFO } , + { NULL, 0 } +}; + + +void +OUTerror(int flags, char *format, IFuid *names) +{ + struct mesg *m; + char buf[BSIZE_SP], *s, *bptr; + int nindex = 0; + + if ((flags == ERR_INFO) && cp_getvar("printinfo", CP_BOOL, NULL)) + return; + + for (m = msgs; m->flag; m++) + if (flags & m->flag) + fprintf(cp_err, "%s: ", m->string); + + for (s = format, bptr = buf; *s; s++) { + if (*s == '%' && (s == format || s[-1] != '%') && s[1] == 's') { + if (names[nindex]) + strcpy(bptr, names[nindex]); + else + strcpy(bptr, "(null)"); + bptr += strlen(bptr); + s++; + nindex++; + } else { + *bptr++ = *s; + } + } + + *bptr = '\0'; + fprintf(cp_err, "%s\n", buf); + fflush(cp_err); +} |