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 if (ERROR_INVALID_HANDLE == GetLastError())
170 int optlen = sizeof(sockbuf);
172 if (SOCKET_ERROR != getsockopt((SOCKET)handle, SOL_SOCKET, SO_TYPE, sockbuf, &optlen) ||
173 WSAENOTSOCK != WSAGetLastError())
181 input_avail(HANDLE handle, e_handle_type type)
183 if (CH_UNDEF == type)
184 type = get_handle_type(handle);
190 struct timeval tv = {0, 0};
192 assert(INVALID_HANDLE_VALUE != handle);
196 FD_SET((SOCKET)handle, &s);
198 int ret = select(0, &s, NULL, NULL, &tv);
199 if (SOCKET_ERROR == ret)
200 fprintf(stderr, "Can't select: %d\n", WSAGetLastError());
202 return ret != SOCKET_ERROR && ret != 0;
210 PINPUT_RECORD pIRBuf;
215 * Peek all pending console events
217 if (INVALID_HANDLE_VALUE == handle ||
218 !GetNumberOfConsoleInputEvents(handle, &NumPending) ||
220 NULL == (pIRBuf = (PINPUT_RECORD)_alloca(NumPending * sizeof(INPUT_RECORD))))
223 if (PeekConsoleInput(handle, pIRBuf, NumPending, &NumPeeked) &&
225 NumPeeked <= NumPending)
229 * Scan all of the peeked events to determine if any is a key event
230 * which should be recognized.
232 for ( ; NumPeeked > 0 ; NumPeeked--, pIRBuf++ )
234 if (KEY_EVENT == pIRBuf->EventType &&
235 pIRBuf->Event.KeyEvent.bKeyDown &&
236 pIRBuf->Event.KeyEvent.uChar.AsciiChar)
249 bool res = ClearCommError(handle, &err, &comStat);
252 return res ? comStat.cbInQue > 0 : false;
262 input_avail(UCSOCKET_T fd)
270 struct timeval tv = {0, 0};
272 int i = select(fd + 1, &s, NULL, NULL, &tv);
281 * Searching for a name in the specified table
285 get_name_entry(struct name_entry tabl[], char *name, class cl_uc *uc)
294 for (p= name; *p; *p= toupper(*p), p++);
295 while (tabl[i].name &&
296 (!(tabl[i].cpu_type & uc->type) ||
297 (strcmp(tabl[i].name, name) != 0)))
299 //printf("tabl[%d].name=%s <-> %s\n",i,tabl[i].name,name);
302 if (tabl[i].name != NULL)
310 * Interpreting a bitname
314 interpret_bitname(char *name, class cl_uc *uc,
315 uchar **cell, uchar *celladdr,
316 uchar *bitaddr, uchar *bitmask,
320 char *sym, bitnumstr[2];
321 struct name_entry *ne;
324 if ((dot= strchr(name, '.')) != NULL)
327 if ((ne= get_name_entry(uc->sfr_tbl(), name, uc)) == NULL)
329 *celladdr= strtol(name, &p, 0);
339 if ((*celladdr < 0x20) ||
340 ((*celladdr > 0x2f) && (*celladdr < 0x80)) ||
341 ((*celladdr > 0x7f) && (*celladdr & 0x07)))
343 bitnum= strtol(dot, &p, 0);
348 if (*celladdr > 0x7f)
349 *bitaddr= *celladdr + bitnum;
351 *bitaddr= (*celladdr - 0x20)*8 + bitnum;
357 if ((ne= get_name_entry(uc->bit_tbl(), name, uc)) == NULL)
359 *bitaddr= strtol(name, &p, 0);
367 *celladdr= *bitaddr & 0xf8;
369 *celladdr= (*bitaddr >> 3) + 0x20;
371 // *bitaddr, *celladdr now OK
372 *cell= uc->get_bit//FIXME
374 *bitmask= BIT_MASK(*bitaddr);
375 // making symbolic name
379 while (uc->bit_tbl()[i].name &&
380 (uc->bit_tbl()[i].addr != *bitaddr))
382 if (uc->bit_tbl()[i].name)
384 sym= strdup(uc->bit_tbl()[i].name);
389 while (uc->sfr_tbl()[i].name &&
390 (uc->sfr_tbl()[i].addr != *celladdr))
392 if (uc->sfr_tbl()[i].name)
393 sym= strdup(uc->sfr_tbl()[i].name);
396 sym= (char *)malloc(3);
397 sprintf(sym, "%02x", *celladdr);
399 sym= (char *)realloc(sym, strlen(sym)+2);
401 sprintf(bitnumstr, "%1d", *bitaddr & 0x07);
402 strcat(sym, bitnumstr);
409 * Processing escape sequencies in a string
413 proc_escape(char *string, int *len)
415 char spec_chars[]= "fnrtvab\"";
416 char spec[]= "\f\n\r\t\v\a\b\"";
420 str= (char *)malloc(strlen(string)+1);
432 if (!isdigit(*(s+1)))
439 char *octal, *chk, data;
441 i= strspn(s, "01234567");
442 octal= (char *)malloc(i+1);
448 data= strtol(octal, &chk, 8);
454 if ((spec_c= strchr(spec_chars, *s)) != NULL)
456 *p++= spec[spec_c-spec_chars];
471 /* End of cmd.src/cmdutil.cc */