113183231b63de28572ad16837e5b5df5eb0b5ed
[fw/sdcc] / sim / ucsim / s51.src / dump.cc
1 /*
2  * Simulator of microcontrollers (dump.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 "simcl.h"
36
37 #include "uc51cl.h" //FIXME
38 #include "regs51.h" //FIXME
39 #include "globals.h"
40 #include "cmdutil.h"
41
42
43 /*
44  * Dump memory
45  */
46
47 void
48 dump_memory(cl_mem *mem,
49             t_addr *start, t_addr stop, int bpl, class cl_console *con,
50             class cl_sim *sim)
51 {
52   int i;
53
54   while ((*start <= stop) &&
55          (*start < mem->size))
56     {
57       con->printf("%06x ", *start);
58       for (i= 0; (i < bpl) &&
59              (*start+i < mem->size) &&
60              (*start+i <= stop);
61            i++)
62         {
63           char format[10];
64           sprintf(format, "%%0%dx ", mem->width/4);
65           con->printf(format/*"%02x "*/, mem->get(*start+i));
66         }
67       while (i < bpl)
68         {
69           con->printf("   ");
70           i++;
71         }
72       for (i= 0; (i < bpl) &&
73              (*start+i < mem->size) &&
74              (*start+i <= stop);
75            i++)
76         con->printf("%c",
77                     isprint(mem->get(*start+i))?(char)mem->get(*start+i):'.');
78       con->printf("\n");
79       (*start)+= bpl;
80     }
81 }
82
83
84 /*
85  * DISASSEMBLE [start [offset [lines]]]
86  */
87 //FIXME
88 static int disass_last_stop= 0;
89
90 bool
91 cmd_disass(char *cmd, class t_uc51 *uc, class cl_sim *sim)
92 {
93   char *s;
94   int  start, offset= -1, dir;
95   int  lines= 20;
96   uint realstart;
97
98   if (!uc->there_is_inst())
99     return(DD_FALSE);
100   if ((s= strtok(NULL, delimiters)) != NULL)
101     start= strtol(s, NULL, 0);
102   else
103     start= disass_last_stop;
104   if ((s= strtok(NULL, delimiters)) != NULL)
105     offset= strtol(s, NULL, 0);
106   if ((s= strtok(NULL, delimiters)) != NULL)
107     lines= strtol(s, NULL, 0);
108
109   realstart= start;
110   while (!uc->inst_at(realstart))
111     realstart= (realstart+1) & (EROM_SIZE-1);
112   if (offset)
113     {
114       dir= (offset < 0)?-1:+1;
115       while (offset)
116         {
117           realstart= (realstart+dir) & (EROM_SIZE-1);
118           while (!uc->inst_at(realstart))
119             realstart= (realstart+dir) & (EROM_SIZE-1);
120           offset+= -dir;
121         }
122     }
123   
124   while (lines)
125     {
126       uc->print_disass(realstart, sim->cmd->actual_console);
127       realstart= (realstart+1) & (EROM_SIZE-1);
128       while (!uc->inst_at(realstart))
129         realstart= (realstart+1) & (EROM_SIZE-1);
130       lines--;
131     }
132
133   disass_last_stop= realstart;
134   return(DD_FALSE);
135 }
136
137
138 /*
139  * DUMP PORTS
140  */
141 //FIXME
142 bool
143 cmd_dump_port(char *cmd, class t_uc51 *uc, class cl_sim *sim)
144 {
145   uchar data;
146
147   if (sim->cmd->actual_console == 0)
148     return(DD_FALSE);
149   data= uc->get_mem(MEM_SFR, P0);
150   sim->cmd->printf("P0    ");
151   sim->cmd->actual_console->print_bin(data, 8);
152   sim->cmd->printf(" 0x%02x %3d %c", data, data, isprint(data)?data:'.');
153
154   data= uc->get_mem(MEM_SFR, P1);
155   sim->cmd->printf("    P1    ");
156   sim->cmd->actual_console->print_bin(data, 8);
157   sim->cmd->printf(" 0x%02x %3d %c\n", data, data, isprint(data)?data:'.');
158
159   data= uc->port_pins[0];
160   sim->cmd->printf("Pin0  ");
161   sim->cmd->actual_console->print_bin(data, 8);
162   sim->cmd->printf(" 0x%02x %3d %c", data, data, isprint(data)?data:'.');
163
164   data= uc->port_pins[1];
165   sim->cmd->printf("    Pin1  ");
166   sim->cmd->actual_console->print_bin(data, 8);
167   sim->cmd->printf(" 0x%02x %3d %c\n", data, data, isprint(data)?data:'.');
168
169   data= uc->port_pins[0] & uc->get_mem(MEM_SFR, P0);
170   sim->cmd->printf("Port0 ");
171   sim->cmd->actual_console->print_bin(data, 8);
172   sim->cmd->printf(" 0x%02x %3d %c", data, data, isprint(data)?data:'.');
173
174   data= uc->port_pins[1] & uc->get_mem(MEM_SFR, P1);
175   sim->cmd->printf("    Port1 ");
176   sim->cmd->actual_console->print_bin(data, 8);
177   sim->cmd->printf(" 0x%02x %3d %c\n", data, data, isprint(data)?data:'.');
178
179   sim->cmd->printf("\n");
180
181   data= uc->get_mem(MEM_SFR, P2);
182   sim->cmd->printf("P2    ");
183   sim->cmd->actual_console->print_bin(data, 8);
184   sim->cmd->printf(" 0x%02x %3d %c", data, data, isprint(data)?data:'.');
185
186   data= uc->get_mem(MEM_SFR, P3);
187   sim->cmd->printf("    P3    ");
188   sim->cmd->actual_console->print_bin(data, 8);
189   sim->cmd->printf(" 0x%02x %3d %c\n", data, data, isprint(data)?data:'.');
190
191   data= uc->port_pins[2];
192   sim->cmd->printf("Pin2  ");
193   sim->cmd->actual_console->print_bin(data, 8);
194   sim->cmd->printf(" 0x%02x %3d %c", data, data, isprint(data)?data:'.');
195
196   data= uc->port_pins[3];
197   sim->cmd->printf("    Pin3  ");
198   sim->cmd->actual_console->print_bin(data, 8);
199   sim->cmd->printf(" 0x%02x %3d %c\n", data, data, isprint(data)?data:'.');
200
201   data= uc->port_pins[2] & uc->get_mem(MEM_SFR, P2);
202   sim->cmd->printf("Port2 ");
203   sim->cmd->actual_console->print_bin(data, 8);
204   sim->cmd->printf(" 0x%02x %3d %c", data, data, isprint(data)?data:'.');
205
206   data= uc->port_pins[3] & uc->get_mem(MEM_SFR, P3);
207   sim->cmd->printf("    Port3 ");
208   sim->cmd->actual_console->print_bin(data, 8);
209   sim->cmd->printf(" 0x%02x %3d %c\n", data, data, isprint(data)?data:'.');
210
211   return(DD_FALSE);
212 }
213
214
215 /*
216  * DUMP SFR [addr...]
217  */
218 //FIXME
219 bool
220 cmd_dump_sfr(char *cmd, class t_uc51 *uc, class cl_sim *sim)
221 {
222   char *s;
223   t_addr start= 0x80;
224   uchar data;
225   struct name_entry *ne;
226
227   if ((s= strtok(NULL, delimiters)) != NULL)
228     while (s != NULL)
229       {
230         if ((ne= get_name_entry(uc->sfr_tbl(), s, uc)) != NULL)
231           {
232             data= uc->get_mem(MEM_SFR, ne->addr);
233             sim->cmd->printf("%6s %02x %3d %c\n", ne->name, data, data,
234                              isprint(data)?data:'.');
235           }
236         else
237           {
238             start= strtol(s, NULL, 0);
239             data = uc->get_mem(MEM_SFR, start);
240             if (start >= SFR_START)
241               sim->cmd->printf("%06x %02x %3d %c\n", start, data, data,
242                                isprint(data)?data:'.');
243           }
244         s= strtok(NULL, delimiters);
245       }
246   else
247     // dump all
248     dump_memory(uc->mem(MEM_SFR), &start, 255, 16, sim->cmd->actual_console,
249                 sim);
250   return(DD_FALSE);
251 }
252
253
254 /*
255  * DUMP BIT addr...
256  */
257 //FIXME
258 bool
259 cmd_dump_bit(char *cmd, class t_uc51 *uc, class cl_sim *sim)
260 {
261   char *s, *sym;
262   uchar *cell, addr;
263   uchar bitaddr, bitmask;
264
265   if ((s= strtok(NULL, delimiters)) == NULL)
266     {
267       sim->cmd->printf("Address has not given.\n");
268       return(DD_FALSE);
269     }
270   while (s)
271     {
272       if (!interpret_bitname(s, uc, &cell, &addr, &bitaddr, &bitmask, &sym))
273         {
274           sim->cmd->printf("Bad address %s\n", s);
275           return(DD_FALSE);
276         }
277       sim->cmd->printf("%06x %6s %c\n", addr, sym, (*cell & bitmask)?'1':'0');
278       free(sym);
279       s= strtok(NULL, delimiters);
280     }
281   return(DD_FALSE);
282 }
283
284
285 /* End of s51.src/dump.cc */