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