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
35 #include <sys/types.h>
37 # include HEADER_SOCKET
38 # if defined HAVE_SYS_SOCKET_H
39 # include <netinet/in.h>
40 # include <arpa/inet.h>
56 * Making a socket which can be used to listen for a specified port
64 static bool is_initialized = false;
71 int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
74 printf("WSAStartup failed: %d\n", iResult);
81 make_server_socket(unsigned short int port)
85 struct sockaddr_in name;
87 /* Create the socket. */
88 SOCKET sock = WSASocket(PF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
89 if (INVALID_SOCKET == sock)
91 fprintf(stderr, "socket: %d\n", WSAGetLastError());
92 return INVALID_SOCKET;
95 name.sin_family = AF_INET;
96 name.sin_port = htons(port);
97 name.sin_addr.s_addr= htonl(INADDR_ANY);
98 if (SOCKET_ERROR == bind(sock, (struct sockaddr *)&name, sizeof(name)))
100 fprintf(stderr, "bind: %d\n", WSAGetLastError());
101 return INVALID_SOCKET;
108 make_server_socket(unsigned short int port)
111 struct sockaddr_in name;
113 /* Create the socket. */
114 sock= socket(PF_INET, SOCK_STREAM, 0);
121 /* Give the socket a name. */
123 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&i, sizeof(i)) < 0)
125 perror("setsockopt");
127 name.sin_family = AF_INET;
128 name.sin_port = htons(port);
129 name.sin_addr.s_addr= htonl(INADDR_ANY);
130 if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0)
143 get_handle_type(HANDLE handle)
145 DWORD file_type = GetFileType(handle);
153 if (!ClearCommError(handle, &err, NULL))
155 if (ERROR_INVALID_HANDLE == GetLastError())
166 int optlen = sizeof(sockbuf);
168 if (SOCKET_ERROR != getsockopt((SOCKET)handle, SOL_SOCKET, SO_TYPE, sockbuf, &optlen) ||
169 WSAENOTSOCK != WSAGetLastError())
177 input_avail(HANDLE handle, e_handle_type type)
179 if (CH_UNDEF == type)
180 type = get_handle_type(handle);
186 struct timeval tv = {0, 0};
188 assert(INVALID_HANDLE_VALUE != handle);
192 FD_SET((SOCKET)handle, &s);
194 int ret = select(0, &s, NULL, NULL, &tv);
195 if (SOCKET_ERROR == ret)
196 fprintf(stderr, "Can't select: %d\n", WSAGetLastError());
198 return ret != SOCKET_ERROR && ret != 0;
206 PINPUT_RECORD pIRBuf;
211 * Peek all pending console events
213 if (INVALID_HANDLE_VALUE == handle ||
214 !GetNumberOfConsoleInputEvents(handle, &NumPending) ||
216 NULL == (pIRBuf = (PINPUT_RECORD)_alloca(NumPending * sizeof(INPUT_RECORD))))
219 if (PeekConsoleInput(handle, pIRBuf, NumPending, &NumPeeked) &&
221 NumPeeked <= NumPending)
225 * Scan all of the peeked events to determine if any is a key event
226 * which should be recognized.
228 for ( ; NumPeeked > 0 ; NumPeeked--, pIRBuf++ )
230 if (KEY_EVENT == pIRBuf->EventType &&
231 pIRBuf->Event.KeyEvent.bKeyDown &&
232 pIRBuf->Event.KeyEvent.uChar.AsciiChar)
245 bool res = ClearCommError(handle, &err, &comStat);
248 return res ? comStat.cbInQue > 0 : false;
258 input_avail(UCSOCKET_T fd)
266 struct timeval tv = {0, 0};
268 int i = select(fd + 1, &s, NULL, NULL, &tv);
277 * Searching for a name in the specified table
281 get_name_entry(struct name_entry tabl[], char *name, class cl_uc *uc)
290 for (p= name; *p; *p= toupper(*p), p++);
291 while (tabl[i].name &&
292 (!(tabl[i].cpu_type & uc->type) ||
293 (strcmp(tabl[i].name, name) != 0)))
295 //printf("tabl[%d].name=%s <-> %s\n",i,tabl[i].name,name);
298 if (tabl[i].name != NULL)
306 * Interpreting a bitname
310 interpret_bitname(char *name, class cl_uc *uc,
311 uchar **cell, uchar *celladdr,
312 uchar *bitaddr, uchar *bitmask,
316 char *sym, bitnumstr[2];
317 struct name_entry *ne;
320 if ((dot= strchr(name, '.')) != NULL)
323 if ((ne= get_name_entry(uc->sfr_tbl(), name, uc)) == NULL)
325 *celladdr= strtol(name, &p, 0);
335 if ((*celladdr < 0x20) ||
336 ((*celladdr > 0x2f) && (*celladdr < 0x80)) ||
337 ((*celladdr > 0x7f) && (*celladdr & 0x07)))
339 bitnum= strtol(dot, &p, 0);
344 if (*celladdr > 0x7f)
345 *bitaddr= *celladdr + bitnum;
347 *bitaddr= (*celladdr - 0x20)*8 + bitnum;
353 if ((ne= get_name_entry(uc->bit_tbl(), name, uc)) == NULL)
355 *bitaddr= strtol(name, &p, 0);
363 *celladdr= *bitaddr & 0xf8;
365 *celladdr= (*bitaddr >> 3) + 0x20;
367 // *bitaddr, *celladdr now OK
368 *cell= uc->get_bit//FIXME
370 *bitmask= BIT_MASK(*bitaddr);
371 // making symbolic name
375 while (uc->bit_tbl()[i].name &&
376 (uc->bit_tbl()[i].addr != *bitaddr))
378 if (uc->bit_tbl()[i].name)
380 sym= strdup(uc->bit_tbl()[i].name);
385 while (uc->sfr_tbl()[i].name &&
386 (uc->sfr_tbl()[i].addr != *celladdr))
388 if (uc->sfr_tbl()[i].name)
389 sym= strdup(uc->sfr_tbl()[i].name);
392 sym= (char *)malloc(3);
393 sprintf(sym, "%02x", *celladdr);
395 sym= (char *)realloc(sym, strlen(sym)+2);
397 sprintf(bitnumstr, "%1d", *bitaddr & 0x07);
398 strcat(sym, bitnumstr);
405 * Processing escape sequencies in a string
409 proc_escape(char *string, int *len)
411 char spec_chars[]= "fnrtvab\"";
412 char spec[]= "\f\n\r\t\v\a\b\"";
416 str= (char *)malloc(strlen(string)+1);
428 if (!isdigit(*(s+1)))
435 char *octal, *chk, data;
437 i= strspn(s, "01234567");
438 octal= (char *)malloc(i+1);
444 data= strtol(octal, &chk, 8);
450 if ((spec_c= strchr(spec_chars, *s)) != NULL)
452 *p++= spec[spec_c-spec_chars];
467 /* End of cmd.src/cmdutil.cc */