ucsim-0.2.37-pre3 into cvs
[fw/sdcc] / sim / ucsim / cmd.src / cmdutil.cc
1 /*
2  * Simulator of microcontrollers (cmdutil.cc)
3  *
4  * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
5  * 
6  * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
7  *
8  */
9
10 /* This file is part of microcontroller simulator: ucsim.
11
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.
16
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.
21
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
25 02111-1307, USA. */
26 /*@1@*/
27
28 #include "ddconfig.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_SOCKET_H
35 # include <sys/socket.h>
36 # include <netinet/in.h>
37 # include <arpa/inet.h>
38 #endif
39 #include "i_string.h"
40
41 #include "stypes.h"
42 #include "globals.h"
43 #include "uccl.h"
44
45
46 /*
47  * Making a socket which can be used to listen for a specified port
48  */
49
50 #ifdef SOCKET_AVAIL
51 int
52 make_server_socket(unsigned short int port)
53 {
54   int sock, i;
55   struct sockaddr_in name;
56      
57   /* Create the socket. */
58   sock= socket(PF_INET, SOCK_STREAM, 0);
59   if (sock < 0)
60     {
61       perror("socket");
62       return(0);
63     }
64      
65   /* Give the socket a name. */
66   i= 1;
67   if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&i, sizeof(i)) < 0)
68     {
69       perror("setsockopt");
70     }
71   name.sin_family     = AF_INET;
72   name.sin_port       = htons(port);
73   name.sin_addr.s_addr= htonl(INADDR_ANY);
74   if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0)
75     {
76       perror("bind");
77       return(0);
78     }
79
80   return(sock);
81 }
82 #endif
83
84
85 /*
86  * Printing out an integer in binary format
87  */
88
89 void
90 print_bin(long data, int bits, FILE *f)
91 {
92   long mask= 1;
93
94   mask= mask << ((bits >= 1)?(bits-1):0);
95   while (bits--)
96     {
97       fprintf(f, "%c", (data&mask)?'1':'0');
98       mask>>= 1;
99     }
100 }
101
102
103 /*
104  * Searching for a name in the specified table
105  */
106
107 struct name_entry *
108 get_name_entry(struct name_entry tabl[], char *name, class cl_uc *uc)
109 {
110   int i= 0;
111   char *p;
112
113   for (p= name; *p; *p= toupper(*p), p++);
114   while (tabl[i].name &&
115          (!(tabl[i].cpu_type & uc->type) ||
116          (strcmp(tabl[i].name, name) != 0)))
117     i++;
118   if (tabl[i].name != NULL)
119     return(&tabl[i]);
120   else
121     return(NULL);
122 }
123
124
125 /*
126  * Interpreting a bitname
127  */
128
129 bool
130 interpret_bitname(char *name, class cl_uc *uc,
131                   uchar **cell, uchar *celladdr,
132                   uchar *bitaddr, uchar *bitmask,
133                   char **symname)
134 {
135   char *dot, *p;
136   char *sym, bitnumstr[2];
137   struct name_entry *ne;
138   int bitnum, i;
139   
140   if ((dot= strchr(name, '.')) != NULL)
141     {
142       *dot++= '\0';
143       if ((ne= get_name_entry(uc->sfr_tbl(), name, uc)) == NULL)
144         {
145           *celladdr= strtol(name, &p, 0);
146           if (p && *p)
147             {
148               dot--;
149               *dot= '.';
150               return(FALSE);
151             }
152         }
153       else
154         *celladdr= ne->addr;
155       if ((*celladdr < 0x20) ||
156           ((*celladdr > 0x2f) && (*celladdr < 0x80)) ||
157           ((*celladdr > 0x7f) && (*celladdr & 0x07)))
158         return(FALSE);
159       bitnum= strtol(dot, &p, 0);
160       if ((p && *p) ||
161           (bitnum < 0) ||
162           (bitnum > 7))
163         return(FALSE);
164       if (*celladdr > 0x7f)
165         *bitaddr= *celladdr + bitnum;
166       else
167         *bitaddr= (*celladdr - 0x20)*8 + bitnum;
168       dot--;
169       *dot= '.';
170     }
171   else
172     {
173       if ((ne= get_name_entry(uc->bit_tbl(), name, uc)) == NULL)
174         {
175           *bitaddr= strtol(name, &p, 0);
176           if ((p && *p) ||
177               (*bitaddr > 0xff))
178             return(FALSE);
179         }
180       else
181         *bitaddr= ne->addr;
182       if (*bitaddr > 0x7f)
183         *celladdr= *bitaddr & 0xf8;
184       else
185         *celladdr= (*bitaddr >> 3) + 0x20;
186     }
187   // *bitaddr, *celladdr now OK
188   *cell= uc->get_bit/*FIXME*/(*bitaddr);
189   *bitmask= BIT_MASK(*bitaddr);
190   // making symbolic name
191   if (!symname)
192     return(TRUE);
193   i= 0;
194   while (uc->bit_tbl()[i].name &&
195          (uc->bit_tbl()[i].addr != *bitaddr))
196     i++;
197   if (uc->bit_tbl()[i].name)
198     {
199       sym= strdup(uc->bit_tbl()[i].name);
200       *symname= sym;
201       return(TRUE);
202     }
203   i= 0;
204   while (uc->sfr_tbl()[i].name &&
205          (uc->sfr_tbl()[i].addr != *celladdr))
206     i++;
207   if (uc->sfr_tbl()[i].name)
208     sym= strdup(uc->sfr_tbl()[i].name);
209   else
210     {
211       sym= (char *)malloc(3);
212       sprintf(sym, "%02x", *celladdr);
213     }
214   sym= (char *)realloc(sym, strlen(sym)+2);
215   strcat(sym, ".");
216   sprintf(bitnumstr, "%1d", *bitaddr & 0x07);
217   strcat(sym, bitnumstr);
218   *symname= sym;
219   return(TRUE);
220 }
221
222
223 /*
224  * Processing escape sequencies in a string
225  */
226
227 char *
228 proc_escape(char *string, int *len)
229 {
230   char  spec_chars[]= "fnrtvab";
231   char  spec[]= "\f\n\r\t\v\a\b";
232   char  *s, *str, *p;
233
234   s  = string;
235   str= (char *)malloc(strlen(string)+1);
236   p  = str;
237   while (*s)
238     {
239       char *spec_c;
240
241       if (*s == '\\' &&
242           *(s+1))
243         {
244           s++;
245           if (*s == '0')
246             {
247               if (!isdigit(*(s+1)))
248                 {
249                   *p++= '\0';
250                   s++;
251                 }
252               else
253                 {
254                   char *octal, *chk, data;
255                   int i, j;
256                   i= strspn(s, "01234567");
257                   octal= (char *)malloc(i+1);
258                   j= 0;
259                   while (*s &&
260                          (j < i))
261                     octal[j++]= *s++;
262                   octal[j]= '\0';
263                   data= strtol(octal, &chk, 8);
264                   if (!chk || !(*chk))
265                     *p++= data;
266                 }
267             }
268           else
269             if ((spec_c= strchr(spec_chars, *s)) != NULL)
270               {
271                 *p++= spec[spec_c-spec_chars];
272                 s++;
273               }
274             else
275               *p++= *s++;
276         }
277       else
278         *p++= *s++;
279     }
280   *p= '\0';
281   *len= p-str;
282   return(str);
283 }
284
285
286 /* End of cmdutil.cc */