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