f333990d6cf5bb0329cfc392ae09794a11ecf2a6
[fw/sdcc] / sim / ucsim / hc08.src / hc08.cc
1 /*
2  * Simulator of microcontrollers (hc08.cc)
3  *
4  * some hc08 code base from Karl Bongers karl@turbobit.com
5  * 
6  * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
7  * 
8  * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
9  *
10  */
11
12 /* This file is part of microcontroller simulator: ucsim.
13
14 UCSIM is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 UCSIM is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with UCSIM; see the file COPYING.  If not, write to the Free
26 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 02111-1307, USA. */
28 /*@1@*/
29
30 #include "ddconfig.h"
31
32 #include <stdarg.h> /* for va_list */
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include "i_string.h"
37
38 // prj
39 #include "pobjcl.h"
40
41 // sim
42 #include "simcl.h"
43
44 // local
45 #include "hc08cl.h"
46 #include "glob.h"
47 #include "regshc08.h"
48 #include "hc08mac.h"
49
50 #define uint32 t_addr
51 #define uint8 unsigned char
52 #define int8 char
53
54 /*******************************************************************/
55
56
57 /*
58  * Base type of HC08 controllers
59  */
60
61 cl_hc08::cl_hc08(class cl_sim *asim):
62   cl_uc(asim)
63 {
64   type= CPU_HC08;
65 }
66
67 int
68 cl_hc08::init(void)
69 {
70   cl_uc::init(); /* Memories now exist */
71
72   xtal = 8000000;
73
74   rom= address_space(MEM_ROM_ID);
75 //  ram= mem(MEM_XRAM);
76   ram= rom;
77
78   // zero out ram(this is assumed in regression tests)
79   for (int i=0x80; i<0x8000; i++) {
80     ram->set((t_addr) i, 0);
81   }
82
83   return(0);
84 }
85
86
87 void
88 cl_hc08::reset(void)
89 {
90   cl_uc::reset();
91
92   regs.SP = 0xff;
93   regs.A = 0;
94   regs.X = 0;
95   regs.H = 0;
96   regs.P = 0x60;
97   regs.VECTOR = 1;
98
99 }
100
101
102 char *
103 cl_hc08::id_string(void)
104 {
105   return("unspecified HC08");
106 }
107
108
109 /*
110  * Making elements of the controller
111  */
112 /*
113 t_addr
114 cl_hc08::get_mem_size(enum mem_class type)
115 {
116   switch(type)
117     {
118     case MEM_ROM: return(0x10000);
119     case MEM_XRAM: return(0x10000);
120     default: return(0);
121     }
122  return(cl_uc::get_mem_size(type));
123 }
124 */
125 void
126 cl_hc08::mk_hw_elements(void)
127 {
128   //class cl_base *o;
129   /* t_uc::mk_hw() does nothing */
130 }
131
132 void
133 cl_hc08::make_memories(void)
134 {
135   class cl_address_space *as;
136
137   as= new cl_address_space("rom", 0, 0x10000, 8);
138   as->init();
139   address_spaces->add(as);
140
141   class cl_address_decoder *ad;
142   class cl_memory_chip *chip;
143
144   chip= new cl_memory_chip("rom_chip", 0x10000, 8);
145   chip->init();
146   memchips->add(chip);
147   ad= new cl_address_decoder(as= address_space("rom"), chip, 0, 0xffff, 0);
148   ad->init();
149   as->decoders->add(ad);
150   ad->activate(0);
151 }
152
153
154 /*
155  * Help command interpreter
156  */
157
158 struct dis_entry *
159 cl_hc08::dis_tbl(void)
160 {
161   return(disass_hc08);
162 }
163
164 /*struct name_entry *
165 cl_hc08::sfr_tbl(void)
166 {
167   return(0);
168 }*/
169
170 /*struct name_entry *
171 cl_hc08::bit_tbl(void)
172 {
173   //FIXME
174   return(0);
175 }*/
176
177 int
178 cl_hc08::inst_length(t_addr addr)
179 {
180   int len = 0;
181   char *s;
182
183   s = get_disasm_info(addr, &len, NULL, NULL);
184
185   return len;
186 }
187
188 int
189 cl_hc08::inst_branch(t_addr addr)
190 {
191   int b;
192   char *s;
193
194   s = get_disasm_info(addr, NULL, &b, NULL);
195
196   return b;
197 }
198
199 int
200 cl_hc08::longest_inst(void)
201 {
202   return 4;
203 }
204
205
206 char *
207 cl_hc08::get_disasm_info(t_addr addr,
208                         int *ret_len,
209                         int *ret_branch,
210                         int *immed_offset)
211 {
212   char *b = NULL;
213   uint code;
214   int len = 0;
215   int immed_n = 0;
216   int i;
217   int start_addr = addr;
218   struct dis_entry *dis_e;
219
220   code= get_mem(MEM_ROM_ID, addr++);
221   dis_e = NULL;
222
223   switch(code) {
224     case 0x9e:  /* ESC code to sp relative op-codes */
225       code= get_mem(MEM_ROM_ID, addr++);
226       i= 0;
227       while ((code & disass_hc08_9e[i].mask) != disass_hc08_9e[i].code &&
228         disass_hc08_9e[i].mnemonic)
229         i++;
230       dis_e = &disass_hc08_9e[i];
231       b= disass_hc08_9e[i].mnemonic;
232       if (b != NULL)
233         len += (disass_hc08_9e[i].length + 1);
234     break;
235
236     default:
237       i= 0;
238       while ((code & disass_hc08[i].mask) != disass_hc08[i].code &&
239              disass_hc08[i].mnemonic)
240         i++;
241       dis_e = &disass_hc08[i];
242       b= disass_hc08[i].mnemonic;
243       if (b != NULL)
244         len += (disass_hc08[i].length);
245     break;
246   }
247
248   if (ret_branch) {
249     *ret_branch = dis_e->branch;
250   }
251
252   if (immed_offset) {
253     if (immed_n > 0)
254          *immed_offset = immed_n;
255     else *immed_offset = (addr - start_addr);
256   }
257
258   if (len == 0)
259     len = 1;
260
261   if (ret_len)
262     *ret_len = len;
263
264   return b;
265 }
266
267 char *
268 cl_hc08::disass(t_addr addr, char *sep)
269 {
270   char work[256], temp[20];
271   char *buf, *p, *b, *t;
272   int len = 0;
273   int immed_offset = 0;
274
275   p= work;
276
277   b = get_disasm_info(addr, &len, NULL, &immed_offset);
278   
279   if (b == NULL) {
280     buf= (char*)malloc(30);
281     strcpy(buf, "UNKNOWN/INVALID");
282     return(buf);
283   }
284
285   while (*b)
286     {
287       if (*b == '%')
288         {
289           b++;
290           switch (*(b++))
291             {
292             case 's': // s    signed byte immediate
293               sprintf(temp, "#%d", (char)get_mem(MEM_ROM_ID, addr+immed_offset));
294               ++immed_offset;
295               break;
296             case 'w': // w    word immediate operand
297               sprintf(temp, "#0x%04x",
298                  (uint)((get_mem(MEM_ROM_ID, addr+immed_offset)<<8) |
299                         (get_mem(MEM_ROM_ID, addr+immed_offset+1))) );
300               ++immed_offset;
301               ++immed_offset;
302               break;
303             case 'b': // b    byte immediate operand
304               sprintf(temp, "#0x%02x", (uint)get_mem(MEM_ROM_ID, addr+immed_offset));
305               ++immed_offset;
306               break;
307             case 'x': // x    extended addressing
308               sprintf(temp, "0x%04x",
309                  (uint)((get_mem(MEM_ROM_ID, addr+immed_offset)<<8) |
310                         (get_mem(MEM_ROM_ID, addr+immed_offset+1))) );
311               ++immed_offset;
312               ++immed_offset;
313               break;
314             case 'd': // d    direct addressing
315               sprintf(temp, "*0x%02x", (uint)get_mem(MEM_ROM_ID, addr+immed_offset));
316               ++immed_offset;
317               break;
318             case '2': // 2    word index offset
319               sprintf(temp, "0x%04x",
320                  (uint)((get_mem(MEM_ROM_ID, addr+immed_offset)<<8) |
321                         (get_mem(MEM_ROM_ID, addr+immed_offset+1))) );
322               ++immed_offset;
323               ++immed_offset;
324               break;
325             case '1': // b    byte index offset
326               sprintf(temp, "0x%02x", (uint)get_mem(MEM_ROM_ID, addr+immed_offset));
327               ++immed_offset;
328               break;
329             case 'p': // b    byte index offset
330               sprintf(temp, "0x%04x",
331                  addr+immed_offset+1
332                  +(char)get_mem(MEM_ROM_ID, addr+immed_offset));
333               ++immed_offset;
334               break;
335             default:
336               strcpy(temp, "?");
337               break;
338             }
339           t= temp;
340           while (*t)
341             *(p++)= *(t++);
342         }
343       else
344         *(p++)= *(b++);
345     }
346   *p= '\0';
347
348   p= strchr(work, ' ');
349   if (!p)
350     {
351       buf= strdup(work);
352       return(buf);
353     }
354   if (sep == NULL)
355     buf= (char *)malloc(6+strlen(p)+1);
356   else
357     buf= (char *)malloc((p-work)+strlen(sep)+strlen(p)+1);
358   for (p= work, b= buf; *p != ' '; p++, b++)
359     *b= *p;
360   p++;
361   *b= '\0';
362   if (sep == NULL)
363     {
364       while (strlen(buf) < 6)
365         strcat(buf, " ");
366     }
367   else
368     strcat(buf, sep);
369   strcat(buf, p);
370   return(buf);
371 }
372
373
374 void
375 cl_hc08::print_regs(class cl_console *con)
376 {
377   con->dd_printf("V--HINZC  Flags= 0x%02x %3d %c  ",
378                  regs.P, regs.P, isprint(regs.P)?regs.P:'.');
379   con->dd_printf("A= 0x%02x %3d %c\n",
380                  regs.A, regs.A, isprint(regs.A)?regs.A:'.');
381   con->dd_printf("%c--%c%c%c%c%c  ",
382                  (regs.P&BIT_V)?'1':'0',
383                  (regs.P&BIT_H)?'1':'0',
384                  (regs.P&BIT_I)?'1':'0',
385                  (regs.P&BIT_N)?'1':'0',
386                  (regs.P&BIT_Z)?'1':'0',
387                  (regs.P&BIT_C)?'1':'0');
388   con->dd_printf("    H= 0x%02x %3d %c  ",
389                  regs.H, regs.H, isprint(regs.H)?regs.H:'.');
390   con->dd_printf("X= 0x%02x %3d %c\n",
391                  regs.X, regs.X, isprint(regs.X)?regs.X:'.');
392   con->dd_printf("SP= 0x%04x [SP+1]= %02x %3d %c\n",
393                  regs.SP, ram->get(regs.SP+1), ram->get(regs.SP+1),
394                  isprint(ram->get(regs.SP+1))?ram->get(regs.SP+1):'.');
395   
396   print_disass(PC, con);
397 }
398
399 /*
400  * Execution
401  */
402
403 int
404 cl_hc08::exec_inst(void)
405 {
406   t_mem code;
407
408   if (regs.VECTOR) {
409     PC = get2(0xfffe);
410     regs.VECTOR = 0;
411     return(resGO);
412   }
413
414   if (fetch(&code))
415     return(resBREAKPOINT);
416   tick(1);
417   switch ((code >> 4) & 0xf) {
418     case 0x0: return(inst_bittestsetclear(code, false));
419     case 0x1: return(inst_bitsetclear(code, false));
420     case 0x2: return(inst_condbranch(code, false));
421     case 0x3:
422     case 0x4:
423     case 0x5:
424     case 0x6:
425     case 0x7:
426       switch (code & 0xf) {
427         case 0x0: return(inst_neg(code, false));
428         case 0x1: return(inst_cbeq(code, false));
429         case 0x2:
430           switch (code) {
431             case 0x42: return(inst_mul(code, false));
432             case 0x52: return(inst_div(code, false));
433             case 0x62: return(inst_nsa(code, false));
434             case 0x72: return(inst_daa(code, false));
435             default: return(resHALT);
436           }
437         case 0x3: return(inst_com(code, false));
438         case 0x4: return(inst_lsr(code, false));
439         case 0x5:
440           switch (code) {
441             case 0x35: return(inst_sthx(code, false));
442             case 0x45:
443             case 0x55: return(inst_ldhx(code, false));
444             case 0x65:
445             case 0x75: return(inst_cphx(code, false));
446             default: return(resHALT);
447           }
448         case 0x6: return(inst_ror(code, false));
449         case 0x7: return(inst_asr(code, false));
450         case 0x8: return(inst_lsl(code, false));
451         case 0x9: return(inst_rol(code, false));
452         case 0xa: return(inst_dec(code, false));
453         case 0xb: return(inst_dbnz(code, false));
454         case 0xc: return(inst_inc(code, false));
455         case 0xd: return(inst_tst(code, false));
456         case 0xe:
457           switch (code) {
458             case 0x4e:
459             case 0x5e:
460             case 0x6e:
461             case 0x7e: return(inst_mov(code, false));
462             default: return(resHALT);
463           }
464         case 0xf: return(inst_clr(code, false));
465         default: return(resHALT);
466       }
467     case 0x8:
468       switch (code & 0xf) {
469         case 0x0: return(inst_rti(code, false));
470         case 0x1: return(inst_rts(code, false));
471         case 0x3: return(inst_swi(code, false));
472         case 0x4:
473         case 0x5: return(inst_transfer(code, false));
474         case 0x6:
475         case 0x7:
476         case 0x8:
477         case 0x9:
478         case 0xa:
479         case 0xb: return(inst_pushpull(code, false));
480         case 0xc: return(inst_clrh(code, false));
481         case 0xe: return(inst_stop(code, false));
482         case 0xf: return(inst_wait(code, false));
483         default: return(resHALT);
484       }
485     case 0x9:
486       switch (code & 0xf) {
487         case 0x0:
488         case 0x1:
489         case 0x2:
490         case 0x3: return(inst_condbranch(code, false));
491         case 0x4:
492         case 0x5:
493         case 0x7:
494         case 0xf: return(inst_transfer(code, false));
495         case 0x8:
496         case 0x9:
497         case 0xa:
498         case 0xb: return(inst_setclearflags(code, false));
499         case 0xc: return(inst_rsp(code, false));
500         case 0xd: return(inst_nop(code, false));
501         case 0xe:
502           code = fetch();
503           switch ((code >> 4) & 0xf) {
504             case 0x6:
505               switch (code & 0xf) {
506                 case 0x0: return(inst_neg(code, true));
507                 case 0x1: return(inst_cbeq(code, true));
508                 case 0x3: return(inst_com(code, true));
509                 case 0x4: return(inst_lsr(code, true));
510                 case 0x6: return(inst_ror(code, true));
511                 case 0x7: return(inst_asr(code, true));
512                 case 0x8: return(inst_lsl(code, true));
513                 case 0x9: return(inst_rol(code, true));
514                 case 0xa: return(inst_dec(code, true));
515                 case 0xb: return(inst_dbnz(code, true));
516                 case 0xc: return(inst_inc(code, true));
517                 case 0xd: return(inst_tst(code, true));
518                 case 0xf: return(inst_clr(code, true));
519                 default: return(resHALT);
520               }
521             case 0xd:
522             case 0xe:
523               switch (code & 0xf) {
524                 case 0x0: return(inst_sub(code, true));
525                 case 0x1: return(inst_cmp(code, true));
526                 case 0x2: return(inst_sbc(code, true));
527                 case 0x3: return(inst_cpx(code, true));
528                 case 0x4: return(inst_and(code, true));
529                 case 0x5: return(inst_bit(code, true));
530                 case 0x6: return(inst_lda(code, true));
531                 case 0x7: return(inst_sta(code, true));
532                 case 0x8: return(inst_eor(code, true));
533                 case 0x9: return(inst_adc(code, true));
534                 case 0xa: return(inst_ora(code, true));
535                 case 0xb: return(inst_add(code, true));
536                 case 0xc: return(resHALT);
537                 case 0xd: putchar(regs.A); fflush(stdout); return(resGO);
538                 case 0xe: return(inst_ldx(code, true));
539                 case 0xf: return(inst_stx(code, true));
540                 default: return(resHALT);
541               }
542             default: return(resHALT);
543           }
544               
545       }
546     case 0xa:
547     case 0xb:
548     case 0xc:
549     case 0xd:
550     case 0xe:
551     case 0xf:
552       switch (code & 0xf) {
553         case 0x0: return(inst_sub(code, false));
554         case 0x1: return(inst_cmp(code, false));
555         case 0x2: return(inst_sbc(code, false));
556         case 0x3: return(inst_cpx(code, false));
557         case 0x4: return(inst_and(code, false));
558         case 0x5: return(inst_bit(code, false));
559         case 0x6: return(inst_lda(code, false));
560         case 0x7:
561           if (code==0xa7)
562             return(inst_ais(code, false));
563           else
564             return(inst_sta(code, false));
565         case 0x8: return(inst_eor(code, false));
566         case 0x9: return(inst_adc(code, false));
567         case 0xa: return(inst_ora(code, false));
568         case 0xb: return(inst_add(code, false));
569         case 0xc:
570           if (code==0xac)
571             return(resHALT);
572           else
573             return(inst_jmp(code, false));
574         case 0xd:
575           if (code==0xad)
576             return(inst_bsr(code, false));
577           else
578             return(inst_jsr(code, false));
579         case 0xe: return(inst_ldx(code, false));
580         case 0xf:
581           if (code==0xaf)
582             return(inst_aix(code, false));
583           else
584             return(inst_stx(code, false));
585         default: return(resHALT);
586       }
587     default: return(resHALT);
588   }
589
590   /*if (PC)
591     PC--;
592   else
593   PC= get_mem_size(MEM_ROM_ID)-1;*/
594   PC= rom->inc_address(PC, -1);
595
596   sim->stop(resINV_INST);
597   return(resINV_INST);
598 }
599
600
601 /* End of hc08.src/hc08.cc */