8ff7743f20811a7b17a3e0894beeae5489f1e60e
[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, FILE *f,
50             class cl_sim *sim)
51 {
52   int i;
53
54   while ((*start <= stop) &&
55          (*start < mem->size))
56     {
57       sim->cmd->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           fprintf(f, format/*"%02x "*/, mem->get(*start+i));
66         }
67       while (i < bpl)
68         {
69           fprintf(f, "   ");
70           i++;
71         }
72       for (i= 0; (i < bpl) &&
73              (*start+i < mem->size) &&
74              (*start+i <= stop);
75            i++)
76         fprintf(f, "%c",
77                 isprint(mem->get(*start+i))?(char)mem->get(*start+i):'.');
78       fprintf(f, "\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   data= uc->get_mem(MEM_SFR, P0);
148   sim->cmd->printf("P0    ");
149   print_bin(data, 8, sim->cmd_out());
150   sim->cmd->printf(" 0x%02x %3d %c", data, data, isprint(data)?data:'.');
151
152   data= uc->get_mem(MEM_SFR, P1);
153   sim->cmd->printf("    P1    ");
154   print_bin(data, 8, sim->cmd_out());
155   sim->cmd->printf(" 0x%02x %3d %c\n", data, data, isprint(data)?data:'.');
156
157   data= uc->port_pins[0];
158   sim->cmd->printf("Pin0  ");
159   print_bin(data, 8, sim->cmd_out());
160   sim->cmd->printf(" 0x%02x %3d %c", data, data, isprint(data)?data:'.');
161
162   data= uc->port_pins[1];
163   sim->cmd->printf("    Pin1  ");
164   print_bin(data, 8, sim->cmd_out());
165   sim->cmd->printf(" 0x%02x %3d %c\n", data, data, isprint(data)?data:'.');
166
167   data= uc->port_pins[0] & uc->get_mem(MEM_SFR, P0);
168   sim->cmd->printf("Port0 ");
169   print_bin(data, 8, sim->cmd_out());
170   sim->cmd->printf(" 0x%02x %3d %c", data, data, isprint(data)?data:'.');
171
172   data= uc->port_pins[1] & uc->get_mem(MEM_SFR, P1);
173   sim->cmd->printf("    Port1 ");
174   print_bin(data, 8, sim->cmd_out());
175   sim->cmd->printf(" 0x%02x %3d %c\n", data, data, isprint(data)?data:'.');
176
177   sim->cmd->printf("\n");
178
179   data= uc->get_mem(MEM_SFR, P2);
180   sim->cmd->printf("P2    ");
181   print_bin(data, 8, sim->cmd_out());
182   sim->cmd->printf(" 0x%02x %3d %c", data, data, isprint(data)?data:'.');
183
184   data= uc->get_mem(MEM_SFR, P3);
185   sim->cmd->printf("    P3    ");
186   print_bin(data, 8, sim->cmd_out());
187   sim->cmd->printf(" 0x%02x %3d %c\n", data, data, isprint(data)?data:'.');
188
189   data= uc->port_pins[2];
190   sim->cmd->printf("Pin2  ");
191   print_bin(data, 8, sim->cmd_out());
192   sim->cmd->printf(" 0x%02x %3d %c", data, data, isprint(data)?data:'.');
193
194   data= uc->port_pins[3];
195   sim->cmd->printf("    Pin3  ");
196   print_bin(data, 8, sim->cmd_out());
197   sim->cmd->printf(" 0x%02x %3d %c\n", data, data, isprint(data)?data:'.');
198
199   data= uc->port_pins[2] & uc->get_mem(MEM_SFR, P2);
200   sim->cmd->printf("Port2 ");
201   print_bin(data, 8, sim->cmd_out());
202   sim->cmd->printf(" 0x%02x %3d %c", data, data, isprint(data)?data:'.');
203
204   data= uc->port_pins[3] & uc->get_mem(MEM_SFR, P3);
205   sim->cmd->printf("    Port3 ");
206   print_bin(data, 8, sim->cmd_out());
207   sim->cmd->printf(" 0x%02x %3d %c\n", data, data, isprint(data)?data:'.');
208
209   return(DD_FALSE);
210 }
211
212
213 /*
214  * DUMP SFR [addr...]
215  */
216 //FIXME
217 bool
218 cmd_dump_sfr(char *cmd, class t_uc51 *uc, class cl_sim *sim)
219 {
220   char *s;
221   t_addr start= 0x80;
222   uchar data;
223   struct name_entry *ne;
224
225   if ((s= strtok(NULL, delimiters)) != NULL)
226     while (s != NULL)
227       {
228         if ((ne= get_name_entry(uc->sfr_tbl(), s, uc)) != NULL)
229           {
230             data= uc->get_mem(MEM_SFR, ne->addr);
231             sim->cmd->printf("%6s %02x %3d %c\n", ne->name, data, data,
232                              isprint(data)?data:'.');
233           }
234         else
235           {
236             start= strtol(s, NULL, 0);
237             data = uc->get_mem(MEM_SFR, start);
238             if (start >= SFR_START)
239               sim->cmd->printf("%06x %02x %3d %c\n", start, data, data,
240                                isprint(data)?data:'.');
241           }
242         s= strtok(NULL, delimiters);
243       }
244   else
245     // dump all
246     dump_memory(uc->mem(MEM_SFR), &start, 255, 16, sim->cmd_out(), sim);
247   return(DD_FALSE);
248 }
249
250
251 /*
252  * DUMP BIT addr...
253  */
254 //FIXME
255 bool
256 cmd_dump_bit(char *cmd, class t_uc51 *uc, class cl_sim *sim)
257 {
258   char *s, *sym;
259   uchar *cell, addr;
260   uchar bitaddr, bitmask;
261
262   if ((s= strtok(NULL, delimiters)) == NULL)
263     {
264       sim->cmd->printf("Address has not given.\n");
265       return(DD_FALSE);
266     }
267   while (s)
268     {
269       if (!interpret_bitname(s, uc, &cell, &addr, &bitaddr, &bitmask, &sym))
270         {
271           sim->cmd->printf("Bad address %s\n", s);
272           return(DD_FALSE);
273         }
274       sim->cmd->printf("%06x %6s %c\n", addr, sym, (*cell & bitmask)?'1':'0');
275       free(sym);
276       s= strtok(NULL, delimiters);
277     }
278   return(DD_FALSE);
279 }
280
281
282 /* End of s51.src/dump.cc */