b00a87e0dbc053ded33b911c2a2f905506e9c5dd
[fw/sdcc] / sim / ucsim / s51.src / uc51.cc
1 /*
2  * Simulator of microcontrollers (uc51.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 <termios.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <sys/types.h>
38 #include <sys/time.h>
39 #if FD_HEADER_OK
40 # include HEADER_FD
41 #endif
42 #include "i_string.h"
43
44 // sim
45 #include "optioncl.h"
46
47 // local
48 #include "uc51cl.h"
49 #include "glob.h"
50 #include "regs51.h"
51 #include "timer0cl.h"
52 #include "timer1cl.h"
53 #include "serialcl.h"
54 #include "portcl.h"
55 #include "interruptcl.h"
56 #include "types51.h"
57
58
59 /*
60  * Making a new micro-controller and reset it
61  */
62
63 t_uc51::t_uc51(int Itype, int Itech, class cl_sim *asim):
64   cl_uc(asim)
65 {
66   //int i;
67   /*
68   struct termios tattr;
69   */  
70   type= Itype;
71   technology= Itech;
72
73   debug= asim->app->args->get_iarg('V', 0);
74   stop_at_it= DD_FALSE;
75   options->add(new cl_bool_opt(&debug, "verbose", "Verbose flag."));
76   options->add(new cl_bool_opt(&stop_at_it, "stopit",
77                                "Stop if interrupt accepted."));
78   options->add(new cl_cons_debug_opt(asim->app, "debug",
79                                      "Debug messages appears on this console."));
80
81   /*
82   serial_in = (FILE*)asim->app->args->get_parg(0, "Ser_in");
83   serial_out= (FILE*)asim->app->args->get_parg(0, "Ser_out");
84   if (serial_in)
85     {
86       // making `serial' unbuffered
87       if (setvbuf(serial_in, NULL, _IONBF, 0))
88         perror("Unbuffer serial input channel");
89       // setting O_NONBLOCK
90       if ((i= fcntl(fileno(serial_in), F_GETFL, 0)) < 0)
91         perror("Get flags of serial input");
92       i|= O_NONBLOCK;
93       if (fcntl(fileno(serial_in), F_SETFL, i) < 0)
94         perror("Set flags of serial input");
95       // switching terminal to noncanonical mode
96       if (isatty(fileno(serial_in)))
97         {
98           tcgetattr(fileno(serial_in), &saved_attributes_in);
99           tcgetattr(fileno(serial_in), &tattr);
100           tattr.c_lflag&= ~(ICANON|ECHO);
101           tattr.c_cc[VMIN] = 1;
102           tattr.c_cc[VTIME]= 0;
103           tcsetattr(fileno(serial_in), TCSAFLUSH, &tattr);
104         }
105       else
106         fprintf(stderr, "Warning: serial input interface connected to a "
107                 "non-terminal file.\n");
108     }
109   if (serial_out)
110     {
111       // making `serial' unbuffered
112       if (setvbuf(serial_out, NULL, _IONBF, 0))
113         perror("Unbuffer serial output channel");
114       // setting O_NONBLOCK
115       if ((i= fcntl(fileno(serial_out), F_GETFL, 0)) < 0)
116         perror("Get flags of serial output");
117       i|= O_NONBLOCK;
118       if (fcntl(fileno(serial_out), F_SETFL, i) < 0)
119         perror("Set flags of serial output");
120       // switching terminal to noncanonical mode
121       if (isatty(fileno(serial_out)))
122         {
123           tcgetattr(fileno(serial_out), &saved_attributes_out);
124           tcgetattr(fileno(serial_out), &tattr);
125           tattr.c_lflag&= ~(ICANON|ECHO);
126           tattr.c_cc[VMIN] = 1;
127           tattr.c_cc[VTIME]= 0;
128           tcsetattr(fileno(serial_out), TCSAFLUSH, &tattr);
129         }
130       else
131         fprintf(stderr, "Warning: serial output interface connected to a "
132                 "non-terminal file.\n");
133     }
134   */
135
136   /*for (i= 0; i < 4; i++)
137     port_pins[i]= 0xff;*/
138   /*it_sources->add(new cl_it_src(bmEX0, TCON, bmIE0, 0x0003, true,
139     "external #0"));*/
140   /*it_sources->add(new cl_it_src(bmET0, TCON, bmTF0, 0x000b, true,
141     "timer #0"));*/
142   /*it_sources->add(new cl_it_src(bmEX1, TCON, bmIE1, 0x0013, true,
143     "external #1"));*/
144   /*it_sources->add(new cl_it_src(bmET1, TCON, bmTF1, 0x001b, true,
145     "timer #1"));*/
146   /*it_sources->add(new cl_it_src(bmES , SCON, bmTI , 0x0023, false,
147                                 "serial transmit"));
148   it_sources->add(new cl_it_src(bmES , SCON, bmRI , 0x0023, false,
149   "serial receive"));*/
150 }
151
152
153 /*
154  * Initializing. Virtual calls go here
155  * This method must be called first after object creation.
156  */
157
158 int
159 t_uc51::init(void)
160 {
161   cl_uc::init();
162   reset();
163   return(0);
164 }
165
166 static char id_string_51[100];
167
168 char *
169 t_uc51::id_string(void)
170 {
171   int i;
172
173   for (i= 0; cpus_51[i].type_str != NULL && cpus_51[i].type != type; i++) ;
174   sprintf(id_string_51, "%s %s",
175           cpus_51[i].type_str?cpus_51[i].type_str:"51",
176           (technology==CPU_HMOS)?"HMOS":"CMOS");
177   return(id_string_51);
178 }
179
180 void
181 t_uc51::mk_hw_elements(void)
182 {
183   class cl_hw *h;
184
185   acc= sfr->get_cell(ACC);
186   psw= sfr->get_cell(PSW);
187
188   hws->add(h= new cl_timer0(this, 0, "timer0"));
189   h->init();
190   hws->add(h= new cl_timer1(this, 1, "timer1"));
191   h->init();
192   hws->add(h= new cl_serial(this));
193   h->init();
194   hws->add(h= new cl_port(this, 0));
195   h->init();
196   hws->add(h= new cl_port(this, 1));
197   h->init();
198   hws->add(h= new cl_port(this, 2));
199   h->init();
200   hws->add(h= new cl_port(this, 3));
201   h->init();
202   hws->add(interrupt= new cl_interrupt(this));
203   interrupt->init();
204   hws->add(h= new cl_uc51_dummy_hw(this));
205   h->init();
206   /*
207   acc= sfr->get_cell(ACC);
208   psw= sfr->get_cell(PSW);
209   */
210 }
211
212 class cl_mem *
213 t_uc51::mk_mem(enum mem_class type, char *class_name)
214 {
215   class cl_mem *m= cl_uc::mk_mem(type, class_name);
216   if (type == MEM_SFR)
217     sfr= m;
218   if (type == MEM_IRAM)
219     iram= m;
220   return(m);
221 }
222
223
224 /*
225  * Destroying the micro-controller object
226  */
227
228 t_uc51::~t_uc51(void)
229 {
230   /*
231   if (serial_out)
232     {
233       if (isatty(fileno(serial_out)))
234         tcsetattr(fileno(serial_out), TCSANOW, &saved_attributes_out);
235       fclose(serial_out);
236     }
237   if (serial_in)
238     {
239       if (isatty(fileno(serial_in)))
240         tcsetattr(fileno(serial_in), TCSANOW, &saved_attributes_in);
241       fclose(serial_in);
242     }
243   */
244 }
245
246
247 /*
248  * Writing data to EROM
249  */
250
251 void
252 t_uc51::write_rom(t_addr addr, ulong data)
253 {
254   if (addr < EROM_SIZE)
255     set_mem(MEM_ROM, addr, data);
256 }
257
258
259 /*
260  * Disassembling an instruction
261  */
262
263 struct dis_entry *
264 t_uc51::dis_tbl(void)
265 {
266   return(disass_51);
267 }
268
269 struct name_entry *
270 t_uc51::sfr_tbl(void)
271 {
272   return(sfr_tab51);
273 }
274
275 struct name_entry *
276 t_uc51::bit_tbl(void)
277 {
278   return(bit_tab51);
279 }
280
281 char *
282 t_uc51::disass(t_addr addr, char *sep)
283 {
284   char work[256], temp[20], c[2];
285   char *buf, *p, *b, *t;
286   t_mem code= get_mem(MEM_ROM, addr);
287
288   p= work;
289   b= dis_tbl()[code].mnemonic;
290   while (*b)
291     {
292       if (*b == '%')
293         {
294           b++;
295           switch (*(b++))
296             {
297             case 'A': // absolute address
298               sprintf(temp, "%04"_A_"x",
299                       t_addr((addr&0xf800)|
300                              (((code>>5)&0x07)*256 +
301                               get_mem(MEM_ROM, addr+1))));
302               break;
303             case 'l': // long address
304               sprintf(temp, "%04"_A_"x",
305                       t_addr(get_mem(MEM_ROM, addr+1)*256 +
306                              get_mem(MEM_ROM, addr+2)));
307               break;
308             case 'a': // addr8 (direct address) at 2nd byte
309               if (!get_name(get_mem(MEM_ROM, addr+1), sfr_tbl(), temp))
310                 sprintf(temp, "%02"_M_"x", get_mem(MEM_ROM, addr+1));
311               break;
312             case '8': // addr8 (direct address) at 3rd byte
313               if (!get_name(get_mem(MEM_ROM, addr+2), sfr_tbl(), temp))
314                 sprintf(temp, "%02"_M_"x", get_mem(MEM_ROM, addr+2));
315               //sprintf(temp, "%02"_M_"x", get_mem(MEM_ROM, addr+2));
316               break;
317             case 'b': // bitaddr at 2nd byte
318               {
319                 t_addr ba= get_mem(MEM_ROM, addr+1);
320                 if (get_name(ba, bit_tbl(), temp))
321                   break;
322                 if (get_name((ba<128)?((ba/8)+32):(ba&0xf8), sfr_tbl(), temp))
323                   {
324                     strcat(temp, ".");
325                     sprintf(c, "%1"_M_"d", ba & 0x07);
326                     strcat(temp, c);
327                     break;
328                   }
329                 sprintf(temp, "%02x.%"_M_"d", (ba<128)?((ba/8)+32):(ba&0xf8),
330                         ba & 0x07);
331                 break;
332               }
333             case 'r': // rel8 address at 2nd byte
334               sprintf(temp, "%04"_A_"x",
335                       t_addr(addr+2+(signed char)(get_mem(MEM_ROM, addr+1))));
336               break;
337             case 'R': // rel8 address at 3rd byte
338               sprintf(temp, "%04"_A_"x",
339                       t_addr(addr+3+(signed char)(get_mem(MEM_ROM, addr+2))));
340               break;
341             case 'd': // data8 at 2nd byte
342               sprintf(temp, "%02"_M_"x", get_mem(MEM_ROM, addr+1));
343               break;
344             case 'D': // data8 at 3rd byte
345               sprintf(temp, "%02"_M_"x", get_mem(MEM_ROM, addr+2));
346               break;
347             case '6': // data16 at 2nd(H)-3rd(L) byte
348               sprintf(temp, "%04"_A_"x",
349                       t_addr(get_mem(MEM_ROM, addr+1)*256 +
350                              get_mem(MEM_ROM, addr+2)));
351               break;
352             default:
353               strcpy(temp, "?");
354               break;
355             }
356           t= temp;
357           while (*t)
358             *(p++)= *(t++);
359         }
360       else
361         *(p++)= *(b++);
362     }
363   *p= '\0';
364
365   p= strchr(work, ' ');
366   if (!p)
367     {
368       buf= strdup(work);
369       return(buf);
370     }
371   if (sep == NULL)
372     buf= (char *)malloc(6+strlen(p)+1);
373   else
374     buf= (char *)malloc((p-work)+strlen(sep)+strlen(p)+1);
375   for (p= work, b= buf; *p != ' '; p++, b++)
376     *b= *p;
377   p++;
378   *b= '\0';
379   if (sep == NULL)
380     {
381       while (strlen(buf) < 6)
382         strcat(buf, " ");
383     }
384   else
385     strcat(buf, sep);
386   strcat(buf, p);
387   return(buf);
388 }
389
390
391 void
392 t_uc51::print_regs(class cl_console *con)
393 {
394   t_addr start;
395   uchar data;
396
397   start= psw->get() & 0x18;
398   //dump_memory(iram, &start, start+7, 8, /*sim->cmd_out()*/con, sim);
399   iram->dump(start, start+7, 8, con);
400   start= psw->get() & 0x18;
401   data= iram->get(iram->get(start));
402   con->dd_printf("%06x %02x %c",
403               iram->get(start), data, isprint(data)?data:'.');
404
405   con->dd_printf("  ACC= 0x%02x %3d %c  B= 0x%02x", sfr->get(ACC), sfr->get(ACC),
406               isprint(sfr->get(ACC))?(sfr->get(ACC)):'.', sfr->get(B)); 
407   eram2xram();
408   data= get_mem(MEM_XRAM, sfr->get(DPH)*256+sfr->get(DPL));
409   con->dd_printf("   DPTR= 0x%02x%02x @DPTR= 0x%02x %3d %c\n", sfr->get(DPH),
410               sfr->get(DPL), data, data, isprint(data)?data:'.');
411
412   data= iram->get(iram->get(start+1));
413   con->dd_printf("%06x %02x %c", iram->get(start+1), data,
414               isprint(data)?data:'.');
415   data= psw->get();
416   con->dd_printf("  PSW= 0x%02x CY=%c AC=%c OV=%c P=%c\n", data,
417               (data&bmCY)?'1':'0', (data&bmAC)?'1':'0',
418               (data&bmOV)?'1':'0', (data&bmP)?'1':'0');
419
420   print_disass(PC, con);
421 }
422
423
424 /*
425  * Converting bit address into real memory
426  */
427
428 class cl_mem *
429 t_uc51::bit2mem(t_addr bitaddr, t_addr *memaddr, t_mem *bitmask)
430 {
431   class cl_mem *m;
432   t_addr ma;
433
434   bitaddr&= 0xff;
435   if (bitaddr < 128)
436     {
437       m= iram;
438       ma= bitaddr/8 + 0x20;
439     }
440   else
441     {
442       m= sfr;
443       ma= bitaddr & 0xf8;
444     }
445   if (memaddr)
446     *memaddr= ma;
447   if (bitmask)
448     *bitmask= 1 << (bitaddr & 0x7);
449   return(m);
450 }
451
452
453 /*
454  * Resetting the micro-controller
455  */
456
457 void
458 t_uc51::reset(void)
459 {
460   cl_uc::reset();
461
462   clear_sfr();
463
464   result= resGO;
465
466   //was_reti= DD_FALSE;
467 }
468
469
470 /*
471  * Setting up SFR area to reset value
472  */
473
474 void
475 t_uc51::clear_sfr(void)
476 {
477   int i;
478   
479   for (i= 0; i < SFR_SIZE; i++)
480     sfr->set(i, 0);
481   sfr->/*set*/write(P0, 0xff);
482   sfr->/*set*/write(P1, 0xff);
483   sfr->/*set*/write(P2, 0xff);
484   sfr->/*set*/write(P3, 0xff);
485   sfr->/*set*/write(SP, 7);
486   prev_p1= /*port_pins[1] &*/ sfr->/*get*/read(P1);
487   prev_p3= /*port_pins[3] &*/ sfr->/*get*/read(P3);
488 }
489
490
491 /*
492  * Analyzing code and settig up instruction map
493  */
494
495 void
496 t_uc51::analyze(t_addr addr)
497 {
498   uint code;
499   struct dis_entry *tabl;
500
501   code= get_mem(MEM_ROM, addr);
502   tabl= &(dis_tbl()[code]);
503   while (!inst_at(addr) &&
504          code != 0xa5 /* break point */)
505     {
506       set_inst_at(addr);
507       switch (tabl->branch)
508         {
509         case 'a': // acall
510           analyze((addr & 0xf800)|
511                   ((get_mem(MEM_ROM, addr+1)&0x07)*256+
512                    get_mem(MEM_ROM, addr+2)));
513           analyze(addr+tabl->length);
514           break;
515         case 'A': // ajmp
516           addr= (addr & 0xf800)|
517             ((get_mem(MEM_ROM, addr+1) & 0x07)*256 + get_mem(MEM_ROM, addr+2));
518           break;
519         case 'l': // lcall
520           analyze(get_mem(MEM_ROM, addr+1)*256 + get_mem(MEM_ROM, addr+2));
521           analyze(addr+tabl->length);
522           break;
523         case 'L': // ljmp
524           addr= get_mem(MEM_ROM, addr+1)*256 + get_mem(MEM_ROM, addr+2);
525           break;
526         case 'r': // reljmp (2nd byte)
527           analyze((addr + (signed char)(get_mem(MEM_ROM, addr+1))) &
528                   (EROM_SIZE - 1));
529           analyze(addr+tabl->length);
530           break;
531         case 'R': // reljmp (3rd byte)
532           analyze((addr+
533                    (signed char)(get_mem(MEM_ROM, addr+2)))&(EROM_SIZE-1));
534           analyze(addr+tabl->length);
535           break;
536         case 's': // sjmp
537           {
538             signed char target;
539             target= get_mem(MEM_ROM, addr+1);
540             addr+= 2;
541             addr= (addr+target)&(EROM_SIZE-1);
542             break;
543           }
544         case '_':
545           return;
546         default:
547           addr= (addr+tabl->length) & (EROM_SIZE - 1);
548           break;
549         }
550       code= get_mem(MEM_ROM, addr);
551       tabl= &(dis_tbl()[code]);
552     }
553 }
554
555
556 /*
557  * Inform hardware elements that `cycles' machine cycles have elapsed
558  */
559
560 /*int
561 t_uc51::tick_hw(int cycles)
562 {
563   cl_uc::tick_hw(cycles);
564   //do_hardware(cycles);
565   return(0);
566 }*/
567
568 /*int
569 t_uc51::tick(int cycles)
570 {
571   cl_uc::tick(cycles);
572   //do_hardware(cycles);
573   return(0);
574 }*/
575
576
577 /*
578  * Correcting direct address
579  *
580  * This function returns address of addressed element which can be an IRAM
581  * or an SFR.
582  */
583
584 class cl_cell *
585 t_uc51::get_direct(t_mem addr)
586 {
587   if (addr < SFR_START)
588     return(iram->get_cell(addr));
589   else
590     return(sfr->get_cell(addr));
591 }
592
593
594 /*
595  * Calculating address of specified register cell in IRAM
596  */
597
598 class cl_cell *
599 t_uc51::get_reg(uchar regnum)
600 {
601   t_addr a= (psw->get() & (bmRS0|bmRS1)) | (regnum & 0x07);
602   return(iram->get_cell(a));
603 }
604
605
606 /*
607  * Fetching one instruction and executing it
608  */
609
610 int
611 t_uc51::exec_inst(void)
612 {
613   t_mem code;
614   int res;
615
616   //pr_inst();
617   instPC= PC;
618   if (fetch(&code))
619     return(resBREAKPOINT);
620   //tick_hw(1);
621   tick(1);
622   switch (code)
623     {
624     case 0x00: res= inst_nop(code); break;
625     case 0x01: case 0x21: case 0x41: case 0x61:
626     case 0x81: case 0xa1: case 0xc1: case 0xe1:res=inst_ajmp_addr(code);break;
627     case 0x02: res= inst_ljmp(code); break;
628     case 0x03: res= inst_rr(code); break;
629     case 0x04: res= inst_inc_a(code); break;
630     case 0x05: res= inst_inc_addr(code); break;
631     case 0x06: case 0x07: res= inst_inc_Sri(code); break;
632     case 0x08: case 0x09: case 0x0a: case 0x0b:
633     case 0x0c: case 0x0d: case 0x0e: case 0x0f: res= inst_inc_rn(code); break;
634     case 0x10: res= inst_jbc_bit_addr(code); break;
635     case 0x11: case 0x31: case 0x51: case 0x71:
636     case 0x91: case 0xb1: case 0xd1: case 0xf1:res=inst_acall_addr(code);break;
637     case 0x12: res= inst_lcall(code, 0); break;
638     case 0x13: res= inst_rrc(code); break;
639     case 0x14: res= inst_dec_a(code); break;
640     case 0x15: res= inst_dec_addr(code); break;
641     case 0x16: case 0x17: res= inst_dec_Sri(code); break;
642     case 0x18: case 0x19: case 0x1a: case 0x1b:
643     case 0x1c: case 0x1d: case 0x1e: case 0x1f: res= inst_dec_rn(code); break;
644     case 0x20: res= inst_jb_bit_addr(code); break;
645     case 0x22: res= inst_ret(code); break;
646     case 0x23: res= inst_rl(code); break;
647     case 0x24: res= inst_add_a_Sdata(code); break;
648     case 0x25: res= inst_add_a_addr(code); break;
649     case 0x26: case 0x27: res= inst_add_a_Sri(code); break;
650     case 0x28: case 0x29: case 0x2a: case 0x2b:
651     case 0x2c: case 0x2d: case 0x2e: case 0x2f:res= inst_add_a_rn(code);break;
652     case 0x30: res= inst_jnb_bit_addr(code); break;
653     case 0x32: res= inst_reti(code); break;
654     case 0x33: res= inst_rlc(code); break;
655     case 0x34: res= inst_addc_a_Sdata(code); break;
656     case 0x35: res= inst_addc_a_addr(code); break;
657     case 0x36: case 0x37: res= inst_addc_a_Sri(code); break;
658     case 0x38: case 0x39: case 0x3a: case 0x3b:
659     case 0x3c: case 0x3d: case 0x3e: case 0x3f:res= inst_addc_a_rn(code);break;
660     case 0x40: res= inst_jc_addr(code); break;
661     case 0x42: res= inst_orl_addr_a(code); break;
662     case 0x43: res= inst_orl_addr_Sdata(code); break;
663     case 0x44: res= inst_orl_a_Sdata(code); break;
664     case 0x45: res= inst_orl_a_addr(code); break;
665     case 0x46: case 0x47: res= inst_orl_a_Sri(code); break;
666     case 0x48: case 0x49: case 0x4a: case 0x4b:
667     case 0x4c: case 0x4d: case 0x4e: case 0x4f: res= inst_orl_a_rn(code);break;
668     case 0x50: res= inst_jnc_addr(code); break;
669     case 0x52: res= inst_anl_addr_a(code); break;
670     case 0x53: res= inst_anl_addr_Sdata(code); break;
671     case 0x54: res= inst_anl_a_Sdata(code); break;
672     case 0x55: res= inst_anl_a_addr(code); break;
673     case 0x56: case 0x57: res= inst_anl_a_Sri(code); break;
674     case 0x58: case 0x59: case 0x5a: case 0x5b:
675     case 0x5c: case 0x5d: case 0x5e: case 0x5f: res= inst_anl_a_rn(code);break;
676     case 0x60: res= inst_jz_addr(code); break;
677     case 0x62: res= inst_xrl_addr_a(code); break;
678     case 0x63: res= inst_xrl_addr_Sdata(code); break;
679     case 0x64: res= inst_xrl_a_Sdata(code); break;
680     case 0x65: res= inst_xrl_a_addr(code); break;
681     case 0x66: case 0x67: res= inst_xrl_a_Sri(code); break;
682     case 0x68: case 0x69: case 0x6a: case 0x6b:
683     case 0x6c: case 0x6d: case 0x6e: case 0x6f: res= inst_xrl_a_rn(code);break;
684     case 0x70: res= inst_jnz_addr(code); break;
685     case 0x72: res= inst_orl_c_bit(code); break;
686     case 0x73: res= inst_jmp_Sa_dptr(code); break;
687     case 0x74: res= inst_mov_a_Sdata(code); break;
688     case 0x75: res= inst_mov_addr_Sdata(code); break;
689     case 0x76: case 0x77: res= inst_mov_Sri_Sdata(code); break;
690     case 0x78: case 0x79: case 0x7a: case 0x7b: case 0x7c:
691     case 0x7d: case 0x7e: case 0x7f: res=inst_mov_rn_Sdata(code); break;
692     case 0x80: res= inst_sjmp(code); break;
693     case 0x82: res= inst_anl_c_bit(code); break;
694     case 0x83: res= inst_movc_a_Sa_pc(code); break;
695     case 0x84: res= inst_div_ab(code); break;
696     case 0x85: res= inst_mov_addr_addr(code); break;
697     case 0x86: case 0x87: res= inst_mov_addr_Sri(code); break;
698     case 0x88: case 0x89: case 0x8a: case 0x8b:
699     case 0x8c: case 0x8d: case 0x8e: case 0x8f:res=inst_mov_addr_rn(code);break;
700     case 0x90: res= inst_mov_dptr_Sdata(code); break;
701     case 0x92: res= inst_mov_bit_c(code); break;
702     case 0x93: res= inst_movc_a_Sa_dptr(code); break;
703     case 0x94: res= inst_subb_a_Sdata(code); break;
704     case 0x95: res= inst_subb_a_addr(code); break;
705     case 0x96: case 0x97: res= inst_subb_a_Sri(code); break;
706     case 0x98: case 0x99: case 0x9a: case 0x9b:
707     case 0x9c: case 0x9d: case 0x9e: case 0x9f:res= inst_subb_a_rn(code);break;
708     case 0xa2: res= inst_mov_c_bit(code); break;
709     case 0xa3: res= inst_inc_dptr(code); break;
710     case 0xa4: res= inst_mul_ab(code); break;
711     case 0xa5: res= inst_unknown(code); break;
712     case 0xa6: case 0xa7: res= inst_mov_Sri_addr(code); break;
713     case 0xa8: case 0xa9: case 0xaa: case 0xab:
714     case 0xac: case 0xad: case 0xae: case 0xaf:res=inst_mov_rn_addr(code);break;
715     case 0xb0: res= inst_anl_c_Sbit(code); break;
716     case 0xb2: res= inst_cpl_bit(code); break;
717     case 0xb3: res= inst_cpl_c(code); break;
718     case 0xb4: res= inst_cjne_a_Sdata_addr(code); break;
719     case 0xb5: res= inst_cjne_a_addr_addr(code); break;
720     case 0xb6: case 0xb7: res= inst_cjne_Sri_Sdata_addr(code); break;
721     case 0xb8: case 0xb9: case 0xba: case 0xbb: case 0xbc:
722     case 0xbd: case 0xbe: case 0xbf: res=inst_cjne_rn_Sdata_addr(code); break;
723     case 0xc0: res= inst_push(code); break;
724     case 0xc2: res= inst_clr_bit(code); break;
725     case 0xc3: res= inst_clr_c(code); break;
726     case 0xc4: res= inst_swap(code); break;
727     case 0xc5: res= inst_xch_a_addr(code); break;
728     case 0xc6: case 0xc7: res= inst_xch_a_Sri(code); break;
729     case 0xc8: case 0xc9: case 0xca: case 0xcb:
730     case 0xcc: case 0xcd: case 0xce: case 0xcf: res= inst_xch_a_rn(code);break;
731     case 0xd0: res= inst_pop(code); break;
732     case 0xd2: res= inst_setb_bit(code); break;
733     case 0xd3: res= inst_setb_c(code); break;
734     case 0xd4: res= inst_da_a(code); break;
735     case 0xd5: res= inst_djnz_addr_addr(code); break;
736     case 0xd6: case 0xd7: res= inst_xchd_a_Sri(code); break;
737     case 0xd8: case 0xd9: case 0xda: case 0xdb: case 0xdc:
738     case 0xdd: case 0xde: case 0xdf: res=inst_djnz_rn_addr(code); break;
739     case 0xe0: res= inst_movx_a_Sdptr(code); break;
740     case 0xe2: case 0xe3: res= inst_movx_a_Sri(code); break;
741     case 0xe4: res= inst_clr_a(code); break;
742     case 0xe5: res= inst_mov_a_addr(code); break;
743     case 0xe6: case 0xe7: res= inst_mov_a_Sri(code); break;
744     case 0xe8: case 0xe9: case 0xea: case 0xeb:
745     case 0xec: case 0xed: case 0xee: case 0xef: res= inst_mov_a_rn(code);break;
746     case 0xf0: res= inst_movx_Sdptr_a(code); break;
747     case 0xf2: case 0xf3: res= inst_movx_Sri_a(code); break;
748     case 0xf4: res= inst_cpl_a(code); break;
749     case 0xf5: res= inst_mov_addr_a(code); break;
750     case 0xf6: case 0xf7: res= inst_mov_Sri_a(code); break;
751     case 0xf8: case 0xf9: case 0xfa: case 0xfb:
752     case 0xfc: case 0xfd: case 0xfe: case 0xff: res= inst_mov_rn_a(code);break;
753     default:
754       res= inst_unknown(code);
755       break;
756     }
757   //post_inst();
758   return(res);
759 }
760
761
762 /*
763  * Simulating execution of next instruction
764  *
765  * This is an endless loop if requested number of steps is negative.
766  * In this case execution is stopped if an instruction results other
767  * status than GO. Execution can be stopped if `cmd_in' is not NULL
768  * and there is input available on that file. It is usefull if the
769  * command console is on a terminal. If input is available then a
770  * complete line is read and dropped out because input is buffered
771  * (inp_avail will be TRUE if ENTER is pressed) and it can confuse
772  * command interepter.
773  */
774
775 int
776 t_uc51::do_inst(int step)
777 {
778   result= resGO;
779   while ((result == resGO) &&
780          (state != stPD) &&
781          (step != 0))
782     {
783       if (step > 0)
784         step--;
785       if (state == stGO)
786         {
787           interrupt->was_reti= DD_FALSE;
788           pre_inst();
789           result= exec_inst();
790           post_inst();
791           /*if (result == resGO)
792             result= check_events();*/
793         }
794       else
795         {
796           // tick hw in idle state
797           post_inst();
798           tick(1);
799         }
800       if (result == resGO)
801         {
802           int res;
803           if ((res= do_interrupt()) != resGO)
804             result= res;
805           else
806             result= idle_pd();
807         }
808       if ((step < 0) &&
809           ((ticks->ticks % 100000) < 50))
810         {
811           if (sim->app->get_commander()->input_avail_on_frozen())
812             {
813               result= resUSER;
814             }
815           else
816             if (sim->app->get_commander()->input_avail())
817               break;
818         }
819       if (((result == resINTERRUPT) &&
820            stop_at_it) ||
821           result >= resSTOP)
822         {
823           sim->stop(result);
824           break;
825         }
826     }
827   if (state == stPD)
828     {
829       //FIXME: tick outsiders eg. watchdog
830       if (sim->app->get_commander()->input_avail_on_frozen())
831         {
832           //fprintf(stderr,"uc: inp avail in PD mode, user stop\n");
833           result= resUSER;
834           sim->stop(result); 
835         }
836     }
837   return(result);
838 }
839
840 /*void
841 t_uc51::post_inst(void)
842 {*/
843   //uint tcon= sfr->get(TCON);
844   //uint p3= sfr->read(P3);
845
846   //cl_uc::post_inst();
847   //set_p_flag();
848
849   // Setting up external interrupt request bits (IEx)
850   /*if ((tcon & bmIT0))
851     {
852       // IE0 edge triggered
853       if (p3_int0_edge)
854         {
855           // falling edge on INT0
856           sim->app->get_commander()->
857             debug("%g sec (%d clks): Falling edge detected on INT0 (P3.2)\n",
858                           get_rtime(), ticks->ticks);
859           sfr->set_bit1(TCON, bmIE0);
860           p3_int0_edge= 0;
861         }
862     }
863   else
864     {
865       // IE0 level triggered
866       if (p3 & bm_INT0)
867         sfr->set_bit0(TCON, bmIE0);
868       else
869         sfr->set_bit1(TCON, bmIE0);
870     }
871   if ((tcon & bmIT1))
872     {
873       // IE1 edge triggered
874       if (p3_int1_edge)
875         {
876           // falling edge on INT1
877           sfr->set_bit1(TCON, bmIE1);
878           p3_int1_edge= 0;
879         }
880     }
881   else
882     {
883       // IE1 level triggered
884       if (p3 & bm_INT1)
885         sfr->set_bit0(TCON, bmIE1);
886       else
887         sfr->set_bit1(TCON, bmIE1);
888         }*/
889   //prev_p3= p3 & port_pins[3];
890   //prev_p1= p3 & port_pins[1];
891 //}
892
893
894 /*
895  * Abstract method to handle WDT
896  */
897
898 /*int
899 t_uc51::do_wdt(int cycles)
900 {
901   return(resGO);
902 }*/
903
904
905 /*
906  * Checking for interrupt requests and accept one if needed
907  */
908
909 int
910 t_uc51::do_interrupt(void)
911 {
912   int i, ie= 0;
913
914   if (interrupt->was_reti)
915     {
916       interrupt->was_reti= DD_FALSE;
917       return(resGO);
918     }
919   if (!((ie= sfr->get(IE)) & bmEA))
920     return(resGO);
921   class it_level *il= (class it_level *)(it_levels->top()), *IL= 0;
922   for (i= 0; i < it_sources->count; i++)
923     {
924       class cl_it_src *is= (class cl_it_src *)(it_sources->at(i));
925       if (is->is_active() &&
926           (ie & is->ie_mask) &&
927           (sfr->get(is->src_reg) & is->src_mask))
928         {
929           int pr= it_priority(is->ie_mask);
930           if (il->level >= 0 &&
931               pr <= il->level)
932             continue;
933           if (state == stIDLE)
934             {
935               state= stGO;
936               sfr->set_bit0(PCON, bmIDL);
937               interrupt->was_reti= DD_TRUE;
938               return(resGO);
939             }
940           if (is->clr_bit)
941             sfr->set_bit0(is->src_reg, is->src_mask);
942           sim->app->get_commander()->
943             debug("%g sec (%d clks): Accepting interrupt `%s' PC= 0x%06x\n",
944                           get_rtime(), ticks->ticks, is->name, PC);
945           IL= new it_level(pr, is->addr, PC, is);
946           return(accept_it(IL));
947         }
948     }
949   return(resGO);
950 }
951
952 int
953 t_uc51::it_priority(uchar ie_mask)
954 {
955   if (sfr->get(IP) & ie_mask)
956     return(1);
957   return(0);
958 }
959
960
961 /*
962  * Accept an interrupt
963  */
964
965 int
966 t_uc51::accept_it(class it_level *il)
967 {
968   state= stGO;
969   sfr->set_bit0(PCON, bmIDL);
970   it_levels->push(il);
971   tick(1);
972   int res= inst_lcall(0, il->addr);
973   if (res != resGO)
974     return(res);
975   else
976     return(resINTERRUPT);
977 }
978
979
980 /*
981  * Checking if Idle or PowerDown mode should be activated
982  */
983
984 int
985 t_uc51::idle_pd(void)
986 {
987   uint pcon= sfr->get(PCON);
988
989   if (technology != CPU_CMOS)
990     return(resGO);
991   if (pcon & bmIDL)
992     {
993       if (state != stIDLE)
994         sim->app->get_commander()->
995           debug("%g sec (%d clks): CPU in Idle mode\n",
996                 get_rtime(), ticks->ticks);
997       state= stIDLE;
998       //was_reti= 1;
999     }
1000   if (pcon & bmPD)
1001     {
1002       if (state != stPD)
1003         sim->app->get_commander()->
1004           debug("%g sec (%d clks): CPU in PowerDown mode\n",
1005                         get_rtime(), ticks->ticks);
1006       state= stPD;
1007     }
1008   return(resGO);
1009 }
1010
1011
1012 /*
1013  * Checking if EVENT break happened
1014  */
1015
1016 /*int
1017 t_uc51::check_events(void)
1018 {
1019   int i;
1020   class cl_ev_brk *eb;
1021
1022   if (!ebrk->count)
1023     return(resGO);
1024   for (i= 0; i < ebrk->count; i++)
1025     {
1026       eb= (class cl_ev_brk *)(ebrk->at(i));
1027       if (eb->match(&event_at))
1028         return(resBREAKPOINT);
1029     }
1030   return(resGO);
1031 }*/
1032
1033
1034 /*
1035  */
1036
1037 void
1038 t_uc51::mem_cell_changed(class cl_mem *mem, t_addr addr)
1039 {
1040   if (mem == sfr)
1041     switch (addr)
1042       {
1043       case ACC: acc= mem->get_cell(ACC); break;
1044       case PSW: psw= mem->get_cell(PSW); break;
1045       }
1046   cl_uc::mem_cell_changed(mem, addr);
1047 }
1048
1049
1050 /*
1051  * Simulating an unknown instruction
1052  *
1053  * Normally this function is called for unimplemented instructions, because
1054  * every instruction must be known!
1055  */
1056
1057 int
1058 t_uc51::inst_unknown(uchar code)
1059 {
1060   PC--;
1061   if (1)//debug)// && sim->cmd_out())
1062     sim->app->get_commander()->
1063       debug("Unknown instruction %02x at %06x\n", code, PC);
1064   return(resHALT);
1065 }
1066
1067
1068 /*
1069  * 0x00 1 12 NOP
1070  */
1071
1072 int
1073 t_uc51::inst_nop(uchar code)
1074 {
1075   return(resGO);
1076 }
1077
1078
1079 /*
1080  * 0xe4 1 12 CLR A
1081  */
1082
1083 int
1084 t_uc51::inst_clr_a(uchar code)
1085 {
1086   acc->write(0);
1087   return(resGO);
1088 }
1089
1090
1091 /*
1092  * 0xc4 1 1 SWAP A
1093  */
1094
1095 int
1096 t_uc51::inst_swap(uchar code)
1097 {
1098   uchar temp;
1099
1100   temp= (acc->read() >> 4) & 0x0f;
1101   sfr->write(ACC, (acc->get() << 4) | temp);
1102   return(resGO);
1103 }
1104
1105
1106 /*
1107  */
1108
1109 cl_uc51_dummy_hw::cl_uc51_dummy_hw(class cl_uc *auc):
1110   cl_hw(auc, HW_DUMMY, 0, "_51_dummy")
1111 {
1112   //uc51= (class t_uc51 *)uc;
1113 }
1114
1115 int
1116 cl_uc51_dummy_hw::init(void)
1117 {
1118   class cl_mem *sfr= uc->mem(MEM_SFR);
1119   if (!sfr)
1120     {
1121       fprintf(stderr, "No SFR to register %s[%d] into\n", id_string, id);
1122     }
1123   //acc= sfr->register_hw(ACC, this, 0);
1124   //sp = sfr->register_hw(SP , this, 0);
1125   use_cell(sfr, PSW, &cell_psw, wtd_restore);
1126   register_cell(sfr, ACC, &cell_acc, wtd_restore_write);
1127   register_cell(sfr, SP , &cell_sp , wtd_restore);
1128   return(0);
1129 }
1130
1131 void
1132 cl_uc51_dummy_hw::write(class cl_cell *cell, t_mem *val)
1133 {
1134   if (cell == cell_acc)
1135     {
1136       bool p;
1137       int i;
1138       uchar uc;
1139
1140       p = DD_FALSE;
1141       uc= *val;
1142       for (i= 0; i < 8; i++)
1143         {
1144           if (uc & 1)
1145             p= !p;
1146           uc>>= 1;
1147         }
1148       if (p)
1149         cell_psw->set_bit1(bmP);
1150       else
1151         cell_psw->set_bit0(bmP);
1152     }
1153   else if (cell == cell_sp)
1154     {
1155       if (*val > uc->sp_max)
1156         uc->sp_max= *val;
1157       uc->sp_avg= (uc->sp_avg+(*val))/2;
1158     }
1159 }
1160
1161 /*void
1162 cl_uc51_dummy_hw::happen(class cl_hw *where, enum hw_event he, void *params)
1163 {
1164   struct ev_port_changed *ep= (struct ev_port_changed *)params;
1165
1166   if (where->cathegory == HW_PORT &&
1167       he == EV_PORT_CHANGED &&
1168       ep->id == 3)
1169     {
1170       t_mem p3o= ep->pins & ep->prev_value;
1171       t_mem p3n= ep->new_pins & ep->new_value;
1172       if ((p3o & bm_INT0) &&
1173           !(p3n & bm_INT0))
1174         uc51->p3_int0_edge++;
1175       if ((p3o & bm_INT1) &&
1176           !(p3n & bm_INT1))
1177         uc51->p3_int1_edge++;
1178     }
1179 }*/
1180
1181
1182 /* End of s51.src/uc51.cc */