version 0.5.2
[fw/sdcc] / sim / ucsim / avr.src / avr.cc
1 /*
2  * Simulator of microcontrollers (avr.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 // prj
36 #include "pobjcl.h"
37
38 // sim
39 #include "simcl.h"
40 #include "memcl.h"
41
42 // local
43 #include "portcl.h"
44 #include "avrcl.h"
45 #include "glob.h"
46 #include "regsavr.h"
47
48
49 /*
50  * Base type of AVR microcontrollers
51  */
52
53 cl_avr::cl_avr(class cl_sim *asim):
54   cl_uc(asim)
55 {
56   type= CPU_AVR;
57   sleep_executed= 0;
58 }
59
60 int
61 cl_avr::init(void)
62 {
63   cl_uc::init(); /* Memories now exist */
64   ram= address_space(MEM_IRAM_ID);
65   rom= address_space(MEM_ROM_ID);
66   return(0);
67 }
68
69 char *
70 cl_avr::id_string(void)
71 {
72   return("unspecified AVR");
73 }
74
75
76 /*
77  * Making elements of the controller
78  */
79 /*
80 t_addr
81 cl_avr::get_mem_size(enum mem_class type)
82 {
83   switch(type)
84     {
85     case MEM_ROM: return(8*1024);
86     case MEM_IRAM: return(0x10000);
87     default: return(0);
88     }
89   //return(0);
90   //return(cl_uc::get_mem_size(type));
91 }
92 */
93 /*
94 int
95 cl_avr::get_mem_width(enum mem_class type)
96 {
97   if (type == MEM_ROM)
98     return(16);
99   return(cl_uc::get_mem_width(type));
100 }
101 */
102
103 void
104 cl_avr::mk_hw_elements(void)
105 {
106   class cl_base *o;
107   /* t_uc::mk_hw() does nothing */
108   hws->add(o= new cl_port(this));
109   o->init();
110 }
111
112
113 void
114 cl_avr::make_memories(void)
115 {
116   class cl_address_space *as;
117
118   rom= as= new cl_address_space(MEM_ROM_ID, 0, 0x10000, 16);
119   as->init();
120   address_spaces->add(as);
121   ram= as= new cl_address_space(MEM_IRAM_ID, 0, 0x10000, 8);
122   as->init();
123   address_spaces->add(as);
124
125   class cl_address_decoder *ad;
126   class cl_memory_chip *chip;
127
128   chip= new cl_memory_chip("rom_chip", 0x10000, 16);
129   chip->init();
130   memchips->add(chip);
131   ad= new cl_address_decoder(as= rom/*address_space(MEM_ROM_ID)*/,
132                              chip, 0, 0xffff, 0);
133   ad->init();
134   as->decoders->add(ad);
135   ad->activate(0);
136
137   chip= new cl_memory_chip("iram_chip", 0x80, 8);
138   chip->init();
139   memchips->add(chip);
140   ad= new cl_address_decoder(as= ram/*address_space(MEM_IRAM_ID)*/,
141                              chip, 0, 0x7f, 0);
142   ad->init();
143   as->decoders->add(ad);
144   ad->activate(0);
145 }
146
147
148 /*
149  * Help command interpreter
150  */
151
152 struct dis_entry *
153 cl_avr::dis_tbl(void)
154 {
155   return(disass_avr);
156 }
157
158 struct name_entry *
159 cl_avr::sfr_tbl(void)
160 {
161   return(sfr_tabl);
162 }
163
164 struct name_entry *
165 cl_avr::bit_tbl(void)
166 {
167   //FIXME
168   return(0);
169 }
170
171 char *
172 cl_avr::disass(t_addr addr, char *sep)
173 {
174   char work[256], temp[20];
175   char *buf, *p, *b, *t;
176   uint code, data= 0;
177   int i;
178
179   p= work;
180   
181   code= get_mem(MEM_ROM_ID, addr);
182   i= 0;
183   while ((code & dis_tbl()[i].mask) != dis_tbl()[i].code &&
184          dis_tbl()[i].mnemonic)
185     i++;
186   if (dis_tbl()[i].mnemonic == NULL)
187     {
188       buf= (char*)malloc(30);
189       strcpy(buf, "UNKNOWN/INVALID");
190       return(buf);
191     }
192   b= dis_tbl()[i].mnemonic;
193
194   while (*b)
195     {
196       if (*b == '%')
197         {
198           b++;
199           switch (*(b++))
200             {
201             case 'd': // Rd   .... ...d dddd ....  0<=d<=31
202               if (!get_name(data= (code&0x01f0)>>4, sfr_tbl(), temp))
203                 sprintf(temp, "r%d", data);
204               break;
205             case 'D': // Rd   .... .... dddd ....  16<=d<=31
206               if (!get_name(data= 16+((code&0xf0)>>4), sfr_tbl(), temp))
207                 sprintf(temp, "r%d", data);
208               break;
209             case 'K': // K    .... KKKK .... KKKK  0<=K<=255
210               sprintf(temp, "%d", ((code&0xf00)>>4)|(code&0xf));
211               break;
212             case 'r': // Rr   .... ..r. .... rrrr  0<=r<=31
213               if (!get_name(data= ((code&0x0200)>>5)|(code&0x000f),
214                             sfr_tbl(), temp))
215                 sprintf(temp, "r%d", data);
216               break;
217             case '2': // Rdl  .... .... ..dd ....  dl= {24,26,28,30}
218               if (!get_name(data= 24+(2*((code&0x0030)>>4)),
219                             sfr_tbl(), temp))
220                 sprintf(temp, "r%d", data);
221               break;
222             case '6': // K    .... .... KK.. KKKK  0<=K<=63
223               sprintf(temp, "%d", ((code&0xc0)>>2)|(code&0xf));
224               break;
225             case 's': // s    .... .... .sss ....  0<=s<=7
226               sprintf(temp, "%d", (code&0x70)>>4);
227               break;
228             case 'b': // b    .... .... .... .bbb  0<=b<=7
229               sprintf(temp, "%d", code&0x7);
230               break;
231             case 'k': // k    .... ..kk kkkk k...  -64<=k<=+63
232               {
233                 int k= (code&0x3f8)>>3;
234                 if (code&0x200)
235                   k|= -128;
236                 sprintf(temp, "0x%06x", k+1+(signed int)addr);
237                 break;
238               }
239             case 'A': // k    .... ...k kkkk ...k  0<=k<=64K
240                       //      kkkk kkkk kkkk kkkk  0<=k<=4M
241               sprintf(temp, "0x%06x",
242                       (((code&0x1f0)>>3)|(code&1))*0x10000+
243                       (uint)get_mem(MEM_ROM_ID, addr+1));
244               break;
245             case 'P': // P    .... .... pppp p...  0<=P<=31
246               data= (code&0xf8)>>3;
247               if (!get_name(data+0x20, sfr_tbl(), temp))
248                 sprintf(temp, "%d", data);
249               break;
250             case 'p': // P    .... .PP. .... PPPP  0<=P<=63
251               data= ((code&0x600)>>5)|(code&0xf);
252               if (!get_name(data+0x20, sfr_tbl(), temp))
253                 sprintf(temp, "%d", data);
254               break;
255             case 'q': // q    ..q. qq.. .... .qqq  0<=q<=63
256               sprintf(temp, "%d",
257                       ((code&0x2000)>>8)|((code&0xc00)>>7)|(code&7));
258               break;
259             case 'R': // k    SRAM address on second word 0<=k<=65535
260               sprintf(temp, "0x%06x", (uint)get_mem(MEM_ROM_ID, addr+1));
261               break;
262             case 'a': // k    .... kkkk kkkk kkkk  -2k<=k<=2k
263               {
264                 int k= code&0xfff;
265                 if (code&0x800)
266                   k|= -4096;
267                 sprintf(temp, "0x%06"_A_"x",
268                         rom->validate_address(k+1+(signed int)addr));
269                 break;
270               }
271             default:
272               strcpy(temp, "?");
273               break;
274             }
275           t= temp;
276           while (*t)
277             *(p++)= *(t++);
278         }
279       else
280         *(p++)= *(b++);
281     }
282   *p= '\0';
283
284   p= strchr(work, ' ');
285   if (!p)
286     {
287       buf= strdup(work);
288       return(buf);
289     }
290   if (sep == NULL)
291     buf= (char *)malloc(6+strlen(p)+1);
292   else
293     buf= (char *)malloc((p-work)+strlen(sep)+strlen(p)+1);
294   for (p= work, b= buf; *p != ' '; p++, b++)
295     *b= *p;
296   p++;
297   *b= '\0';
298   if (sep == NULL)
299     {
300       while (strlen(buf) < 6)
301         strcat(buf, " ");
302     }
303   else
304     strcat(buf, sep);
305   strcat(buf, p);
306   return(buf);
307 }
308
309
310 void
311 cl_avr::print_regs(class cl_console *con)
312 {
313   uchar data, sreg= ram->get(SREG);
314   uint x, y, z;
315
316   ram->dump(0, 31, 16, con);
317
318   con->dd_printf("ITHSVNZC  SREG= 0x%02x %3d %c\n",
319                  sreg, sreg, isprint(sreg)?sreg:'.');
320   con->dd_printf("%c%c%c%c%c%c%c%c  ",
321                  (sreg&BIT_I)?'1':'0',
322                  (sreg&BIT_T)?'1':'0',
323                  (sreg&BIT_H)?'1':'0',
324                  (sreg&BIT_S)?'1':'0',
325                  (sreg&BIT_V)?'1':'0',
326                  (sreg&BIT_N)?'1':'0',
327                  (sreg&BIT_Z)?'1':'0',
328                  (sreg&BIT_C)?'1':'0');
329   con->dd_printf("SP  = 0x%06x\n", ram->get(SPH)*256+ram->get(SPL));
330
331   x= ram->get(XH)*256 + ram->get(XL);
332   data= ram->get(x);
333   con->dd_printf("X= 0x%04x [X]= 0x%02x %3d %c  ", x,
334                  data, data, isprint(data)?data:'.');
335   y= ram->get(YH)*256 + ram->get(YL);
336   data= ram->get(y);
337   con->dd_printf("Y= 0x%04x [Y]= 0x%02x %3d %c  ", y,
338                  data, data, isprint(data)?data:'.');
339   z= ram->get(ZH)*256 + ram->get(ZL);
340   data= ram->get(z);
341   con->dd_printf("Z= 0x%04x [Z]= 0x%02x %3d %c\n", z,
342                  data, data, isprint(data)?data:'.');
343
344   print_disass(PC, con);
345 }
346
347
348 /*
349  * Execution
350  */
351
352 int
353 cl_avr::exec_inst(void)
354 {
355   t_mem code;
356
357   instPC= PC;
358   if (fetch(&code))
359     return(resBREAKPOINT);
360   tick(1);
361   switch (code)
362     {
363     case 0x9419:
364       return(eijmp(code));
365     case 0x9519:
366       return(eicall(code));
367     case 0x9508: case 0x9528: case 0x9548: case 0x9568:
368       return(ret(code));
369     case 0x9518: case 0x9538: case 0x9558: case 0x9578:
370       return(reti(code));
371     case 0x95c8:
372       return(lpm(code));
373     case 0x95d8:
374       return(elpm(code)); // in some devices equal to lpm
375     case 0x95e8:
376       return(spm(code));
377     case 0x95f8:
378       return(espm(code));
379     case 0x9408:
380       return(sec(code));
381     case 0x9488:
382       return(clc(code));
383     case 0x9428:
384       return(sen(code));
385     case 0x94a8:
386       return(cln(code));
387     case 0x9418:
388       return(sez(code));
389     case 0x9498:
390       return(clz(code));
391     case 0x9478:
392      return(sei(code));
393     case 0x94f8:
394       return(cli(code));
395     case 0x9448:
396       return(ses(code));
397     case 0x94c8:
398       return(cls(code));
399     case 0x9438:
400       return(sev(code));
401     case 0x94b8:
402       return(clv(code));
403     case 0x9468:
404       return(set(code));
405     case 0x94e8:
406       return(clt(code));
407     case 0x9458:
408       return(seh(code));
409     case 0x94d8:
410       return(clh(code));
411     case 0x0000:
412       return(nop(code));
413     case 0x9588: case 0x9598:
414       return(sleep(code));
415     case 0x95a8: case 0x95b8:
416       return(wdr(code));
417     }
418   switch (code & 0xf000)
419     {
420     case 0x3000: return(cpi_Rd_K(code));
421     case 0x4000: return(sbci_Rd_K(code));
422     case 0x5000: return(subi_Rd_K(code));
423     case 0x6000: return(ori_Rd_K(code));
424     case 0x7000: return(andi_Rd_K(code));
425     case 0xc000: return(rjmp_k(code));
426     case 0xd000: return(rcall_k(code));
427     case 0xe000: return(ldi_Rd_K(code));
428     }
429   switch (code & 0xf000)
430     {
431     case 0x0000:
432       {
433         // 0x0...
434         switch (code & 0xfc00)
435           {
436           case 0x0000:
437             {
438               switch (code & 0xff00)
439                 {
440                 case 0x0100: return(movw_Rd_Rr(code));
441                 case 0x0200: return(muls_Rd_Rr(code));
442                 case 0x0300:
443                   {
444                     switch (code & 0xff88)
445                       {
446                       case 0x0300: return(mulsu_Rd_Rr(code));
447                       case 0x0308: return(fmul_Rd_Rr(code));
448                       case 0x0380: return(fmuls_Rd_Rr(code));
449                       case 0x0388: return(fmulsu_Rd_Rr(code));
450                       }
451                     break;
452                   }
453                   break;
454                 }
455               break;
456             }
457           case 0x0400: return(cpc_Rd_Rr(code));
458           case 0x0800: return(sbc_Rd_Rr(code));
459           case 0x0c00: return(add_Rd_Rr(code));
460           }
461         break;
462       }
463     case 0x1000:
464       {
465         // 0x1...
466         switch (code & 0xfc00)
467           {
468           case 0x1000: return(cpse_Rd_Rr(code));
469           case 0x1400: return(cp_Rd_Rr(code));
470           case 0x1800: return(sub_Rd_Rr(code));
471           case 0x1c00: return(adc_Rd_Rr(code));
472           }
473         break;
474       }
475     case 0x2000:
476       {
477         // 0x2...
478         switch (code & 0xfc00)
479           {
480           case 0x2000: return(and_Rd_Rr(code));
481           case 0x2400: return(eor_Rd_Rr(code));
482           case 0x2800: return(or_Rd_Rr(code));
483           case 0x2c00: return(mov_Rd_Rr(code));
484         }
485         break;
486       }
487     case 0x8000:
488       {
489         // 0x8...
490         switch (code &0xf208)
491           {
492           case 0x8000: return(ldd_Rd_Z_q(code));
493           case 0x8008: return(ldd_Rd_Y_q(code));
494           case 0x8200: return(std_Z_q_Rr(code));
495           case 0x8208: return(std_Y_q_Rr(code));
496           }
497         break;
498       }
499     case 0x9000:
500       {
501         // 0x9...
502         if ((code & 0xff0f) == 0x9509)
503           return(icall(code));
504         if ((code & 0xff0f) == 0x9409)
505           return(ijmp(code));
506         if ((code & 0xff00) == 0x9600)
507           return(adiw_Rdl_K(code));
508         if ((code & 0xff00) == 0x9700)
509           return(sbiw_Rdl_K(code));
510         switch (code & 0xfc00)
511           {
512           case 0x9000:
513             {
514               switch (code & 0xfe0f)
515                 {
516                 case 0x9000: return(lds_Rd_k(code));
517                 case 0x9001: return(ld_Rd_ZS(code));
518                 case 0x9002: return(ld_Rd_SZ(code));
519                 case 0x9004: return(lpm_Rd_Z(code));
520                 case 0x9005: return(lpm_Rd_ZS(code));
521                 case 0x9006: return(elpm_Rd_Z(code));
522                 case 0x9007: return(elpm_Rd_ZS(code));
523                 case 0x9009: return(ld_Rd_YS(code));
524                 case 0x900a: return(ld_Rd_SY(code));
525                 case 0x900c: return(ld_Rd_X(code));
526                 case 0x900d: return(ld_Rd_XS(code));
527                 case 0x900e: return(ld_Rd_SX(code));
528                 case 0x900f: return(pop_Rd(code));
529                 case 0x9200: return(sts_k_Rr(code));
530                 case 0x9201: return(st_ZS_Rr(code));
531                 case 0x9202: return(st_SZ_Rr(code));
532                 case 0x9209: return(st_YS_Rr(code));
533                 case 0x920a: return(st_SY_Rr(code));
534                 case 0x920c: return(st_X_Rr(code));
535                 case 0x920d: return(st_XS_Rr(code));
536                 case 0x920e: return(st_SX_Rr(code));
537                 case 0x920f: return(push_Rr(code));
538                 }
539               break;
540             }
541           case 0x9400:
542             {
543               switch (code & 0xfe0f)
544                 {
545                 case 0x9400: return(com_Rd(code));
546                 case 0x9401: return(neg_Rd(code));
547                 case 0x9402: return(swap_Rd(code));
548                 case 0x9403: return(inc_Rd(code));
549                 case 0x9405: return(asr_Rd(code));
550                 case 0x9406: return(lsr_Rd(code));
551                 case 0x9407: return(ror_Rd(code));
552                 case 0x940a: return(dec_Rd(code));
553                 case 0x940c: case 0x940d: return(jmp_k(code));
554                 case 0x940e: case 0x940f: return(call_k(code));
555                 }
556               break;
557             }
558           case 0x9800:
559             {
560               switch (code & 0xff00)
561                 {
562                 case 0x9800: return(cbi_A_b(code));
563                 case 0x9900: return(sbic_P_b(code));
564                 case 0x9a00: return(sbi_A_b(code));
565                 case 0x9b00: return(sbis_P_b(code));
566                 }
567               break;
568             }
569           case 0x9c00: return(mul_Rd_Rr(code));
570           }
571         break;
572       }
573     case 0xa000:
574       {
575         // 0xa...
576         switch (code &0xf208)
577           {
578           case 0xa000: return(ldd_Rd_Z_q(code));
579           case 0xa008: return(ldd_Rd_Y_q(code));
580           case 0xa200: return(std_Z_q_Rr(code));
581           case 0xa208: return(std_Y_q_Rr(code));
582           }
583         break;
584       }
585     case 0xb000:
586       {
587         // 0xb...
588         switch (code & 0xf800)
589           {
590           case 0xb000: return(in_Rd_A(code));
591           case 0xb800: return(out_A_Rr(code));
592           }
593         break;
594       }
595     case 0xe000:
596       {
597         // 0xe...
598         switch (code & 0xff0f)
599           {
600           case 0xef0f: return(ser_Rd(code));
601           }
602         break;
603       }
604     case 0xf000:
605       {
606         // 0xf...
607         switch (code & 0xfc00)
608           {
609           case 0xf000: return(brbs_s_k(code));
610           case 0xf400: return(brbc_s_k(code));
611           case 0xf800: case 0xfc00:
612             {
613               switch (code & 0xfe08)
614                 {
615                 case 0xf800: return(bld_Rd_b(code));
616                 case 0xfa00: return(bst_Rd_b(code));
617                 case 0xfc00: case 0xfc08: return(sbrc_Rr_b(code));
618                 case 0xfe00: case 0xfe08: return(sbrs_Rr_b(code));
619                 }
620               break;
621             }
622           }
623         break;
624       }
625     }
626   /*if (PC)
627     PC--;
628   else
629   PC= get_mem_size(MEM_ROM_ID)-1;*/
630   class cl_error_unknown_code *e= new cl_error_unknown_code(this);
631   error(e);
632   return(resGO);
633   PC= rom->inc_address(PC, -1);
634   //tick(-clock_per_cycle());
635   sim->stop(resINV_INST);
636   return(resINV_INST);
637 }
638
639
640 /*
641  */
642
643 int
644 cl_avr::push_data(t_mem data)
645 {
646   t_addr sp;
647   t_mem spl, sph;
648   
649   spl= ram->read(SPL);
650   sph= ram->read(SPH);
651   sp= 0xffff & (256*sph + spl);
652   data= ram->write(sp, data);
653   sp= 0xffff & (sp-1);
654   spl= sp & 0xff;
655   sph= (sp>>8) & 0xff;
656   ram->write(SPL, spl);
657   ram->write(SPH, sph);
658   return(resGO);
659 }
660
661 int
662 cl_avr::push_addr(t_addr addr)
663 {
664   t_addr sp;
665   t_mem spl, sph, al, ah;
666   
667   spl= ram->read(SPL);
668   sph= ram->read(SPH);
669   sp= 0xffff & (256*sph + spl);
670   al= addr & 0xff;
671   ah= (addr>>8) & 0xff;
672   ram->write(sp, ah);
673   sp= 0xffff & (sp-1);
674   ram->write(sp, al);
675   sp= 0xffff & (sp-1);
676   spl= sp & 0xff;
677   sph= (sp>>8) & 0xff;
678   ram->write(SPL, spl);
679   ram->write(SPH, sph);
680   return(resGO);
681 }
682
683 int
684 cl_avr::pop_data(t_mem *data)
685 {
686   t_addr sp;
687   t_mem spl, sph;
688
689   spl= ram->read(SPL);
690   sph= ram->read(SPH);
691   sp= 256*sph + spl;
692   sp= 0xffff & (sp+1);
693   *data= ram->read(sp);
694   spl= sp & 0xff;
695   sph= (sp>>8) & 0xff;
696   ram->write(SPL, spl);
697   ram->write(SPH, sph);
698
699   return(resGO);
700 }
701
702 int
703 cl_avr::pop_addr(t_addr *addr)
704 {
705   t_addr sp;
706   t_mem spl, sph, al, ah;
707
708   spl= ram->read(SPL);
709   sph= ram->read(SPH);
710   sp= 256*sph + spl;
711   sp= 0xffff & (sp+1);
712   al= ram->read(sp);
713   sp= 0xffff & (sp+1);
714   ah= ram->read(sp);
715   *addr= ah*256 + al;
716   spl= sp & 0xff;
717   sph= (sp>>8) & 0xff;
718   ram->write(SPL, spl);
719   ram->write(SPH, sph);
720   
721   return(resGO);
722 }
723
724
725 /*
726  * Set Z, N, V, S bits of SREG after logic instructions and some others
727  */
728
729 void
730 cl_avr::set_zn0s(t_mem data)
731 {
732   t_mem sreg= ram->get(SREG) & ~BIT_V;
733   data= data&0xff;
734   if (!data)
735     sreg|= BIT_Z;
736   else
737     sreg&= ~BIT_Z;
738   if (data & 0x80)
739     sreg|= (BIT_N|BIT_S);
740   else
741     sreg&= ~(BIT_N|BIT_S);
742   ram->set(SREG, sreg);
743 }
744
745
746 /* End of avr.src/avr.cc */