2 * Simulator of microcontrollers (cmd.src/cmdutil.cc)
4 * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
5 * Copyright (C) 2006, Borut Razem - borut.razem@siol.net
7 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
11 /* This file is part of microcontroller simulator: ucsim.
13 UCSIM is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 UCSIM is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with UCSIM; see the file COPYING. If not, write to the Free
25 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
36 #include <sys/types.h>
38 # include HEADER_SOCKET
39 # if defined HAVE_SYS_SOCKET_H
40 # include <netinet/in.h>
41 # include <arpa/inet.h>
60 * Making a socket which can be used to listen for a specified port
68 static bool is_initialized = false;
75 int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
78 printf("WSAStartup failed: %d\n", iResult);
85 make_server_socket(unsigned short int port)
89 struct sockaddr_in name;
91 /* Create the socket. */
92 SOCKET sock = WSASocket(PF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
93 if (INVALID_SOCKET == sock)
95 fprintf(stderr, "socket: %d\n", WSAGetLastError());
96 return INVALID_SOCKET;
99 name.sin_family = AF_INET;
100 name.sin_port = htons(port);
101 name.sin_addr.s_addr= htonl(INADDR_ANY);
102 if (SOCKET_ERROR == bind(sock, (struct sockaddr *)&name, sizeof(name)))
104 fprintf(stderr, "bind: %d\n", WSAGetLastError());
105 return INVALID_SOCKET;
112 make_server_socket(unsigned short int port)
115 struct sockaddr_in name;
117 /* Create the socket. */
118 sock= socket(PF_INET, SOCK_STREAM, 0);
125 /* Give the socket a name. */
127 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&i, sizeof(i)) < 0)
129 perror("setsockopt");
131 name.sin_family = AF_INET;
132 name.sin_port = htons(port);
133 name.sin_addr.s_addr= htonl(INADDR_ANY);
134 if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0)
147 get_handle_type(HANDLE handle)
149 DWORD file_type = GetFileType(handle);
157 if (!ClearCommError(handle, &err, NULL))
159 switch (GetLastError())
161 case ERROR_INVALID_HANDLE:
164 case ERROR_INVALID_FUNCTION:
166 * In case of NUL device return type CH_FILE.
167 * Is this the correct way to test it?
183 int optlen = sizeof(sockbuf);
185 if (SOCKET_ERROR != getsockopt((SOCKET)handle, SOL_SOCKET, SO_TYPE, sockbuf, &optlen) ||
186 WSAENOTSOCK != WSAGetLastError())
194 input_avail(HANDLE handle, e_handle_type type)
196 if (CH_UNDEF == type)
197 type = get_handle_type(handle);
203 struct timeval tv = {0, 0};
205 assert(INVALID_HANDLE_VALUE != handle);
209 FD_SET((SOCKET)handle, &s);
211 int ret = select(0, &s, NULL, NULL, &tv);
212 if (SOCKET_ERROR == ret)
213 fprintf(stderr, "Can't select: %d\n", WSAGetLastError());
215 return ret != SOCKET_ERROR && ret != 0;
223 PINPUT_RECORD pIRBuf;
228 * Peek all pending console events
230 if (INVALID_HANDLE_VALUE == handle ||
231 !GetNumberOfConsoleInputEvents(handle, &NumPending) ||
233 NULL == (pIRBuf = (PINPUT_RECORD)_alloca(NumPending * sizeof(INPUT_RECORD))))
236 if (PeekConsoleInput(handle, pIRBuf, NumPending, &NumPeeked) &&
238 NumPeeked <= NumPending)
241 * Scan all of the peeked events to determine if any is a key event
242 * which should be recognized.
244 for ( ; NumPeeked > 0 ; NumPeeked--, pIRBuf++ )
246 if (KEY_EVENT == pIRBuf->EventType &&
247 pIRBuf->Event.KeyEvent.bKeyDown &&
248 pIRBuf->Event.KeyEvent.uChar.AsciiChar)
261 bool res = ClearCommError(handle, &err, &comStat);
264 return res ? comStat.cbInQue > 0 : false;
274 input_avail(UCSOCKET_T fd)
282 struct timeval tv = {0, 0};
284 int i = select(fd + 1, &s, NULL, NULL, &tv);
293 * Searching for a name in the specified table
297 get_name_entry(struct name_entry tabl[], char *name, class cl_uc *uc)
306 for (p= name; *p; *p= toupper(*p), p++);
307 while (tabl[i].name &&
308 (!(tabl[i].cpu_type & uc->type) ||
309 (strcmp(tabl[i].name, name) != 0)))
311 //printf("tabl[%d].name=%s <-> %s\n",i,tabl[i].name,name);
314 if (tabl[i].name != NULL)
322 * Interpreting a bitname
326 interpret_bitname(char *name, class cl_uc *uc,
327 uchar **cell, uchar *celladdr,
328 uchar *bitaddr, uchar *bitmask,
332 char *sym, bitnumstr[2];
333 struct name_entry *ne;
336 if ((dot= strchr(name, '.')) != NULL)
339 if ((ne= get_name_entry(uc->sfr_tbl(), name, uc)) == NULL)
341 *celladdr= strtol(name, &p, 0);
351 if ((*celladdr < 0x20) ||
352 ((*celladdr > 0x2f) && (*celladdr < 0x80)) ||
353 ((*celladdr > 0x7f) && (*celladdr & 0x07)))
355 bitnum= strtol(dot, &p, 0);
360 if (*celladdr > 0x7f)
361 *bitaddr= *celladdr + bitnum;
363 *bitaddr= (*celladdr - 0x20)*8 + bitnum;
369 if ((ne= get_name_entry(uc->bit_tbl(), name, uc)) == NULL)
371 *bitaddr= strtol(name, &p, 0);
379 *celladdr= *bitaddr & 0xf8;
381 *celladdr= (*bitaddr >> 3) + 0x20;
383 // *bitaddr, *celladdr now OK
384 *cell= uc->get_bit//FIXME
386 *bitmask= BIT_MASK(*bitaddr);
387 // making symbolic name
391 while (uc->bit_tbl()[i].name &&
392 (uc->bit_tbl()[i].addr != *bitaddr))
394 if (uc->bit_tbl()[i].name)
396 sym= strdup(uc->bit_tbl()[i].name);
401 while (uc->sfr_tbl()[i].name &&
402 (uc->sfr_tbl()[i].addr != *celladdr))
404 if (uc->sfr_tbl()[i].name)
405 sym= strdup(uc->sfr_tbl()[i].name);
408 sym= (char *)malloc(3);
409 sprintf(sym, "%02x", *celladdr);
411 sym= (char *)realloc(sym, strlen(sym)+2);
413 sprintf(bitnumstr, "%1d", *bitaddr & 0x07);
414 strcat(sym, bitnumstr);
421 * Processing escape sequencies in a string
425 proc_escape(char *string, int *len)
427 char spec_chars[]= "fnrtvab\"";
428 char spec[]= "\f\n\r\t\v\a\b\"";
432 str= (char *)malloc(strlen(string)+1);
444 if (!isdigit(*(s+1)))
451 char *octal, *chk, data;
453 i= strspn(s, "01234567");
454 octal= (char *)malloc(i+1);
460 data= strtol(octal, &chk, 8);
466 if ((spec_c= strchr(spec_chars, *s)) != NULL)
468 *p++= spec[spec_c-spec_chars];
483 /* End of cmd.src/cmdutil.cc */