2 * Simulator of microcontrollers (cmd.src/cmdutil.cc)
4 * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
6 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
10 /* This file is part of microcontroller simulator: ucsim.
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
33 #include <sys/types.h>
35 # include <winsock2.h>
37 #elif defined HAVE_SYS_SOCKET_H
38 # include <sys/socket.h>
39 # include <netinet/in.h>
40 # include <arpa/inet.h>
50 * Making a socket which can be used to listen for a specified port
55 make_server_socket(unsigned short int port)
58 struct sockaddr_in name;
60 /* Create the socket. */
61 sock= socket(PF_INET, SOCK_STREAM, 0);
68 /* Give the socket a name. */
70 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&i, sizeof(i)) < 0)
74 name.sin_family = AF_INET;
75 name.sin_port = htons(port);
76 name.sin_addr.s_addr= htonl(INADDR_ANY);
77 if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0)
89 * Printing out an integer in binary format
93 print_bin(long data, int bits, class cl_console *con)
97 mask= mask << ((bits >= 1)?(bits-1):0);
100 con->printf("%c", (data&mask)?'1':'0');
107 * Searching for a name in the specified table
111 get_name_entry(struct name_entry tabl[], char *name, class cl_uc *uc)
120 for (p= name; *p; *p= toupper(*p), p++);
121 while (tabl[i].name &&
122 (!(tabl[i].cpu_type & uc->type) ||
123 (strcmp(tabl[i].name, name) != 0)))
125 //printf("tabl[%d].name=%s <-> %s\n",i,tabl[i].name,name);
128 if (tabl[i].name != NULL)
136 * Interpreting a bitname
140 interpret_bitname(char *name, class cl_uc *uc,
141 uchar **cell, uchar *celladdr,
142 uchar *bitaddr, uchar *bitmask,
146 char *sym, bitnumstr[2];
147 struct name_entry *ne;
150 if ((dot= strchr(name, '.')) != NULL)
153 if ((ne= get_name_entry(uc->sfr_tbl(), name, uc)) == NULL)
155 *celladdr= strtol(name, &p, 0);
165 if ((*celladdr < 0x20) ||
166 ((*celladdr > 0x2f) && (*celladdr < 0x80)) ||
167 ((*celladdr > 0x7f) && (*celladdr & 0x07)))
169 bitnum= strtol(dot, &p, 0);
174 if (*celladdr > 0x7f)
175 *bitaddr= *celladdr + bitnum;
177 *bitaddr= (*celladdr - 0x20)*8 + bitnum;
183 if ((ne= get_name_entry(uc->bit_tbl(), name, uc)) == NULL)
185 *bitaddr= strtol(name, &p, 0);
193 *celladdr= *bitaddr & 0xf8;
195 *celladdr= (*bitaddr >> 3) + 0x20;
197 // *bitaddr, *celladdr now OK
198 *cell= uc->get_bit//FIXME
200 *bitmask= BIT_MASK(*bitaddr);
201 // making symbolic name
205 while (uc->bit_tbl()[i].name &&
206 (uc->bit_tbl()[i].addr != *bitaddr))
208 if (uc->bit_tbl()[i].name)
210 sym= strdup(uc->bit_tbl()[i].name);
215 while (uc->sfr_tbl()[i].name &&
216 (uc->sfr_tbl()[i].addr != *celladdr))
218 if (uc->sfr_tbl()[i].name)
219 sym= strdup(uc->sfr_tbl()[i].name);
222 sym= (char *)malloc(3);
223 sprintf(sym, "%02x", *celladdr);
225 sym= (char *)realloc(sym, strlen(sym)+2);
227 sprintf(bitnumstr, "%1d", *bitaddr & 0x07);
228 strcat(sym, bitnumstr);
235 * Processing escape sequencies in a string
239 proc_escape(char *string, int *len)
241 char spec_chars[]= "fnrtvab\"";
242 char spec[]= "\f\n\r\t\v\a\b\"";
246 str= (char *)malloc(strlen(string)+1);
258 if (!isdigit(*(s+1)))
265 char *octal, *chk, data;
267 i= strspn(s, "01234567");
268 octal= (char *)malloc(i+1);
274 data= strtol(octal, &chk, 8);
280 if ((spec_c= strchr(spec_chars, *s)) != NULL)
282 *p++= spec[spec_c-spec_chars];
298 extern "C" int vasprintf(char **strp, const char *format, va_list ap);
299 extern "C" int vsnprintf(char *str, size_t size,const char *format,va_list ap);
302 cmd_vfprintf(FILE *f, char *format, va_list ap)
307 #ifdef HAVE_VASPRINTF
309 vasprintf(&msg, format, ap);
310 ret= fprintf(f, "%s", msg);
313 # ifdef HAVE_VSNPRINTF
315 vsnprintf(msg, 80*25, format, ap);
316 ret= fprintf(f, "%s", msg);
320 vsprintf(msg, format, ap); /* Dangerous */
321 ret= fprintf(f, "%s", msg);
325 /*strcpy(msg, "Unimplemented printf has called.\n");*/
327 /*strcpy(msg, "printf can not be implemented, upgrade your libc.\n");*/
337 cmd_fprintf(FILE *f, char *format, ...)
340 va_start(ap, format);
341 int ret= cmd_vfprintf(f, format, ap);
347 /* End of cmd.src/cmdutil.cc */