Some fixes towards 0.2.37-pre5
[fw/sdcc] / sim / ucsim / s51.src / set.cc
1 /*
2  * Simulator of microcontrollers (set.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 "i_string.h"
34
35 #include "stypes.h"
36 #include "simcl.h"
37 #include "memcl.h"
38 #include "uccl.h"
39 #include "globals.h"
40
41 #include "cmdutil.h"
42 #include "dump.h"
43
44 #include "regs51.h" //FIXME
45
46
47 /*
48  * Parsing command and write data into specified buffer
49  */
50
51 static void
52 set_memory(cl_mem *mem, t_addr first,
53            class cl_uc *uc, struct name_entry tabl[], class cl_sim *sim)
54 {
55   t_addr start, addr;
56   char *s, *p;
57   uchar data, *what= 0;
58   struct name_entry *ne= NULL;
59
60   if ((s= strtok(NULL, delimiters)) == NULL)
61     {
62       fprintf(sim->cmd_out(), "Address has not given.\n");
63       return;
64     }
65   if (tabl)
66     ne= get_name_entry(tabl, s, uc);
67   if (ne)
68     addr= ne->addr;
69   else
70     {
71       addr= strtol(s, &p, 0);
72       if (p &&
73           *p)
74         {
75           fprintf(sim->cmd_out(), "Bad address.\n");
76           return;
77         }
78     }
79   start= addr;
80   if (start >= mem->size)
81     {
82       fprintf(sim->cmd_out(),
83               "Address %ld(0x%lx) is bigger than %ld(0x%lx).\n",
84               start, start, mem->size-1, mem->size-1);
85       return;
86     }
87   if (!(start >= first))
88     {
89       fprintf(sim->cmd_out(),
90               "Address %ld(0x%lx) is less than %ld(0x%lx).\n",
91               start, start, first, first);
92       return;
93     }
94   if ((s= strtok(NULL, " \t\v")) == NULL)
95     {
96       fprintf(sim->cmd_out(), "Data has not given.\n");
97       return;
98     }
99   while (s &&
100          addr < mem->size)
101     {
102       if (isdigit(*s))
103         {
104           data= strtol(s, &p, 0);
105           if (p &&
106               *p)
107             {
108               fprintf(sim->cmd_out(), "Bad data %s\n", s);
109               break;
110             }
111           mem->set(addr, data);
112           if (what == uc->MEM(MEM_SFR))
113             {
114               uc->proc_write/*FIXME*/(&what[addr]);
115               if (addr == ACC)
116                 uc->set_p_flag/*FIXME*/();
117             }
118           if (what == uc->MEM(MEM_ROM))
119             uc->del_inst_at/*FIXME*/(addr);
120           addr++;
121         }
122       else
123         {
124           int i, len;
125           p= proc_escape(s, &len);
126           i= 0;
127           while ((i < len) &&
128                  addr < mem->size)
129             {
130               what[addr]= p[i++];
131               if (what == uc->MEM(MEM_SFR))
132                 {
133                   uc->proc_write/*FIXME*/(&what[addr]);
134                   if (addr == ACC)
135                     uc->set_p_flag/*FIXME*/();
136                 }
137               if (what == uc->MEM(MEM_ROM))
138                 uc->del_inst_at/*FIXME*/(addr);
139               addr++;
140             }
141           free(p);
142         }
143       s= strtok(NULL, " \t\v");
144     }
145   dump_memory(mem, &start, addr-1, 16, sim->cmd_out(), sim);
146 }
147
148
149 /*
150  * SET IRAM addr data...
151  */
152
153 bool
154 cmd_set_iram(char *cmd, class cl_uc *uc, class cl_sim *sim)
155 {
156   set_memory(uc->mem(MEM_IRAM), 0, uc, NULL, sim);
157   return(DD_FALSE);
158 }
159
160
161 /*
162  * SET XRAM addr data...
163  */
164
165 bool
166 cmd_set_xram(char *cmd, class cl_uc *uc, class cl_sim *sim)
167 {
168   uc->eram2xram();
169   set_memory(uc->mem(MEM_XRAM), 0, uc, NULL, sim);
170   uc->xram2eram();
171   return(DD_FALSE);
172 }
173
174
175 /*
176  * SET CODE addr data...
177  */
178
179 bool
180 cmd_set_code(char *cmd, class cl_uc *uc, class cl_sim *sim)
181 {
182   set_memory(uc->mem(MEM_ROM), 0, uc, NULL, sim);
183   return(DD_FALSE);
184 }
185
186
187 /*
188  * SET SFR addr data...
189  */
190
191 bool
192 cmd_set_sfr(char *cmd, class cl_uc *uc, class cl_sim *sim)
193 {
194   set_memory(uc->mem(MEM_SFR), SFR_START, uc, uc->sfr_tbl(), sim);
195   return(DD_FALSE);
196 }
197
198
199 /*
200  * SET BIT addr data...
201  */
202
203 bool
204 cmd_set_bit(char *cmd, class cl_uc *uc, class cl_sim *sim)
205 {
206   char *s;
207   uchar *cell, addr;
208   uchar bitaddr, bitmask;
209
210   if ((s= strtok(NULL, delimiters)) == NULL)
211     {
212       fprintf(sim->cmd_out(), "Address has not given.\n");
213       return(DD_FALSE);
214     }
215   if (!interpret_bitname(s, uc, &cell, &addr, &bitaddr, &bitmask, NULL))
216     {
217       fprintf(sim->cmd_out(), "Bad address %s\n", s);
218       return(DD_FALSE);
219     }
220
221   if ((s= strtok(NULL, delimiters)) == NULL)
222     {
223       fprintf(sim->cmd_out(), "Data has not given.\n");
224       return(DD_FALSE);
225     }
226   while (s)
227     {
228       if (!isdigit(*s))
229         {
230           fprintf(sim->cmd_out(), "Bad data %s\n", s);
231           return(DD_FALSE);
232         }
233       if (*s == '0')
234         *cell&= ~bitmask;
235       else
236         *cell|= bitmask;
237       bitmask<<= 1;
238       bitmask&= 0xff;
239       s= strtok(NULL, delimiters);
240     }
241   return(DD_FALSE);
242 }
243
244
245 /*
246  * SET PORT port data
247  */
248
249 bool
250 cmd_set_port(char *cmd, class cl_uc *uc, class cl_sim *sim)
251 {
252   char *s, *p;
253   uchar port, data;
254   
255   if ((s= strtok(NULL, delimiters)) == NULL)
256     {
257       fprintf(sim->cmd_out(), "Port number has not given.\n");
258       return(DD_FALSE);
259     }
260   port= strtol(s, &p, 0);
261   if ((p && *p) ||
262       (port > 3))
263     {
264       fprintf(sim->cmd_out(), "Port number %s is wrong.\n", s);
265       return(DD_FALSE);
266     }
267
268   if ((s= strtok(NULL, delimiters)) == NULL)
269     {
270       fprintf(sim->cmd_out(), "Date has not given.\n");
271       return(DD_FALSE);
272     }
273   data= strtol(s, &p, 0);
274   if (p && *p)
275     {
276       fprintf(sim->cmd_out(), "Data %s is wrong.\n", s);
277       return(DD_FALSE);
278     }
279   uc->port_pins[port]= data;
280   return(DD_FALSE);
281 }
282
283
284 /*
285  * Fill memory commands
286  */
287
288 static void
289 fill_memory(uchar *what, int size, int first, class cl_uc *uc,
290             class cl_sim *sim)
291 {
292   char *s, *p;
293   int start, stop;
294   uchar data;
295
296   if ((s= strtok(NULL, delimiters)) == NULL)
297     return;
298   start= strtol(s, &p, 0);
299   if ((p && *p) ||
300       (start < 0) ||
301       (start < first) ||
302       (start >= size))
303     fprintf(sim->cmd_out(), "Start address %s is wrong.\n", s);
304
305   if ((s= strtok(NULL, delimiters)) == NULL)
306     return;
307   stop= strtol(s, &p, 0);
308   if ((p && *p) ||
309       (stop < start) ||
310       (stop >= size))
311     fprintf(sim->cmd_out(), "Stop address %s is wrong.\n", s);
312
313   if ((s= strtok(NULL, delimiters)) == NULL)
314     return;
315   data= strtol(s, &p, 0);
316   if (p && *p)
317     fprintf(sim->cmd_out(), "Data %s is wrong.\n", s);
318   
319   while (start <= stop)
320     {
321       what[start]= data;
322       if (what == uc->MEM(MEM_SFR))
323         {
324           uc->proc_write/*FIXME*/(&what[start]);
325           if (start == ACC)
326             uc->set_p_flag/*FIXME*/();
327         }
328       if (what == uc->MEM(MEM_ROM))
329         uc->del_inst_at/*FIXME*/(start);
330       start++;
331     }
332 }
333
334
335 /*
336  * FILL IRAM from to data
337  */
338
339 bool
340 cmd_fill_iram(char *cmd, class cl_uc *uc, class cl_sim *sim)
341 {
342   fill_memory(uc->MEM(MEM_IRAM), uc->get_mem_size(MEM_IRAM), 0, uc, sim);
343   return(DD_FALSE);
344 }
345
346
347 /*
348  * FILL XRAM from to data
349  */
350
351 bool
352 cmd_fill_xram(char *cmd, class cl_uc *uc, class cl_sim *sim)
353 {
354   fill_memory(uc->MEM(MEM_XRAM), uc->get_mem_size(MEM_XRAM), 0, uc, sim);
355   uc->xram2eram/*FIXME*/();
356   return(DD_FALSE);
357 }
358
359
360 /*
361  * FILL SFR from to data
362  */
363
364 bool
365 cmd_fill_sfr(char *cmd, class cl_uc *uc, class cl_sim *sim)
366 {
367   fill_memory(uc->MEM(MEM_SFR), uc->get_mem_size(MEM_SFR), SFR_START, uc, sim);
368   return(DD_FALSE);
369 }
370
371
372 /*
373  * FILL CODE from to data
374  */
375
376 bool
377 cmd_fill_code(char *cmd, class cl_uc *uc, class cl_sim *sim)
378 {
379   fill_memory(uc->MEM(MEM_ROM), EROM_SIZE, 0, uc, sim);
380   return(DD_FALSE);
381 }
382
383
384 /* End of s51.src/set.cc */