d7b3289ca7485df0a950c3a05cf71d20a3bd9d8d
[fw/sdcc] / sim / ucsim / s51.src / go.cc
1 /*
2  * Simulator of microcontrollers (go.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 "globals.h"
39 #include "dump.h"
40
41
42 /*
43  * GO [start [stop]]
44  */
45 //FIXME
46 bool
47 cmd_go(char *cmd, class t_uc51 *uc, class cl_sim *sim)
48 {
49   char *start_str, *stop_str;
50   t_addr start= uc->PC;
51   long stop= -1;
52   class cl_brk *b;
53   bool brk_at_stop= DD_FALSE;
54
55   if (sim->state & SIM_GO)
56     {
57       fprintf(sim->cmd_out(),
58               "Execution is already running.\n");
59       return(0);
60     }
61   if ((start_str= strtok(NULL, delimiters)) != NULL)
62     {
63       start= strtol(start_str, NULL, 0);
64       if ((stop_str= strtok(NULL, delimiters)) != NULL)
65         stop= strtol(stop_str, NULL, 0);
66     }
67   if (uc->PC != start)
68     {
69       if (!uc->inst_at(start) &&
70           uc->debug)
71         fprintf(sim->cmd_out(),
72                 "Warning: maybe not instruction at 0x%06lx\n", start);
73       uc->PC= start;
74     }
75   if (stop >= 0)
76     {
77       if (start == (t_addr)stop)
78         {
79           fprintf(sim->cmd_out(), "Addresses must be different.\n");
80           return(DD_FALSE);
81         }
82       if ((b= uc->fbrk_at(stop)))
83         {
84           brk_at_stop= DD_TRUE;
85           b->cnt= 1;
86         }
87       else
88         {
89           b= new cl_fetch_brk(uc->fbrk->make_new_nr(), stop, brkDYNAMIC, 1);
90           uc->fbrk->add_bp(b);
91         }
92     }
93   if (uc->fbrk_at(start))
94     uc->do_inst(1);
95   //fprintf(sim->cmd_out(), "Simulation started, PC=0x%06x\n", uc->PC);
96   sim->cmd->printf("Simulation started, PC=0x%06x\n", uc->PC);
97   sim->start(sim->cmd->actual_console);
98   //uc->do_inst(-1);
99   /*if ((stop >= 0) &&
100       !brk_at_stop)
101       uc->fbrk->del_bp(stop);*/
102   /*fprintf(sim->cmd_out(), "%d\n", uc->result);
103     uc->print_disass(uc->PC);*/
104   return(DD_FALSE);
105 }
106
107
108 bool
109 cmd_stop(char *cmd, class t_uc51 *uc, class cl_sim *sim)
110 {
111   sim->stop(resUSER);
112   uc->print_disass(uc->PC, sim->cmd->actual_console);
113   return(DD_FALSE);
114 }
115
116
117 static void
118 dump_step(class t_uc51 *uc, class cl_sim *sim)
119 {
120   //cmd_dump_regs(NULL, uc, sim);
121   uc->print_regs(sim->cmd->actual_console);
122 }
123
124
125 /*
126  * STEP [step]
127  */
128 //FIXME
129 bool
130 cmd_step(char *cmd, class t_uc51 *uc, class cl_sim *sim)
131 {
132   int step= 1;
133   char *s;
134
135   if ((s= strtok(NULL, delimiters)) != NULL)
136     step= strtol(s, NULL, 0);
137   while (step--)
138     {
139       uc->do_inst(1);
140       dump_step(uc, sim);
141     }
142   return(DD_FALSE);
143 }
144
145
146 /*
147  * NEXT [step]
148  */
149 //FIXME
150 bool
151 cmd_next(char *cmd, class t_uc51 *uc, class cl_sim *sim)
152 {
153   int step= 1;
154   char *s;
155   class cl_brk *b;
156   uint next;
157
158   if ((s= strtok(NULL, delimiters)) != NULL)
159     step= strtol(s, NULL, 0);
160   while (step--)
161     {
162       if ((uc->dis_tbl()[uc->get_mem(MEM_ROM, uc->PC)].branch == 'a') ||
163           (uc->dis_tbl()[uc->get_mem(MEM_ROM, uc->PC)].branch == 'l'))
164         {
165           next= uc->PC+2;
166           if (uc->dis_tbl()[uc->get_mem(MEM_ROM, uc->PC)].branch == 'l')
167             next++;
168           if (!uc->fbrk_at(next))
169             {
170               b= new cl_fetch_brk(uc->fbrk->make_new_nr(),
171                                   next, brkDYNAMIC, 1);
172               uc->fbrk->add(b);
173             }
174           uc->do_inst(-1);
175         }
176       else
177         uc->do_inst(1);
178       dump_step(uc, sim);
179     }
180   return(DD_FALSE);
181 }
182
183
184 /*
185  * PC [address]
186  */
187 //FIXME
188 bool
189 cmd_pc(char *cmd, class t_uc51 *uc, class cl_sim *sim)
190 {
191   char *s, *p= NULL;
192   long pc;
193
194   if ((s= strtok(NULL, delimiters)) == NULL)
195     {
196       uc->print_disass(uc->PC, sim->cmd->actual_console);
197       return(DD_FALSE);
198     }
199   pc= strtol(s, &p, 0);
200   if (p &&
201       ((p == s) ||
202        *p))
203     fprintf(sim->cmd_out(), "Wrong parameter, PC unchanged.\n");
204   else
205     {
206       if (pc >= EROM_SIZE)
207         pc= 0;
208       if (!uc->inst_at(pc) &&
209           uc->debug)
210         fprintf(sim->cmd_out(),
211                 "Warning: maybe not instruction at %06lx\n", pc);
212       uc->PC= pc;
213       uc->print_disass(uc->PC, sim->cmd->actual_console);
214     }
215   return(DD_FALSE);
216 }
217
218
219 /* End of s51.src/go.cc */