26dceffaeaed2dceaff50e1ca25d594d59b2fb6f
[fw/sdcc] / sim / ucsim / xa.src / xa.cc
1 /*
2  * Simulator of microcontrollers (xa.cc)
3  *
4  * Copyright (C) 1999,2002 Drotos Daniel, Talker Bt.
5  *
6  * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
7  * Other contributors include:
8  *   Karl Bongers karl@turbobit.com,
9  *   Johan Knol johan.knol@iduna.nl
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 <stdio.h>
33 #include <stdlib.h>
34 #include <ctype.h>
35 #include "i_string.h"
36
37 // prj
38 #include "pobjcl.h"
39
40 // sim
41 #include "simcl.h"
42
43 // local
44 #include "xacl.h"
45 #include "glob.h"
46 #include "regsxa.h"
47
48 /*
49  * Base type of xa controllers
50  */
51
52 cl_xa::cl_xa(class cl_sim *asim):
53   cl_uc(asim)
54 {
55   type= CPU_XA;
56 }
57
58 int
59 cl_xa::init(void)
60 {
61   cl_uc::init(); /* Memories now exist */
62   ram= mem(MEM_XRAM);
63   rom= mem(MEM_ROM);
64
65   /* set SCR to osc/4, native XA mode, flat 24 */
66   set_scr(0);
67   /* initialize SP to 100H */
68   set_reg2(7, 0x100);
69   /* set PSW from reset vector */
70   set_psw(getcode2(0));
71   /* set PC from reset vector */
72   PC = getcode2(2);
73
74   printf("The XA Simulator is in development, UNSTABLE, DEVELOPERS ONLY!\n");
75
76   return(0);
77 }
78
79 class cl_mem *
80 cl_xa::mk_mem(enum mem_class type, char *class_name)
81 {
82   class cl_mem *m= cl_uc::mk_mem(type, class_name);
83   if (type == MEM_SFR)
84     sfr= m;
85   if (type == MEM_IRAM)
86     iram= m;
87   return(m);
88 }
89
90 char *
91 cl_xa::id_string(void)
92 {
93   return("unspecified XA");
94 }
95
96
97 /*
98  * Making elements of the controller
99  */
100
101 t_addr
102 cl_xa::get_mem_size(enum mem_class type)
103 {
104   switch(type)
105     {
106     case MEM_IRAM: return(0x2000);
107     case MEM_SFR: return(0x2000);
108     case MEM_ROM: return(0x10000);
109     case MEM_XRAM: return(0x10000);
110     default: return(0);
111     }
112  return(cl_uc::get_mem_size(type));
113 }
114
115 void
116 cl_xa::mk_hw_elements(void)
117 {
118   //class cl_base *o;
119   /* t_uc::mk_hw() does nothing */
120 }
121
122
123 /*
124  * Help command interpreter
125  */
126
127 struct dis_entry *
128 cl_xa::dis_tbl(void)
129 {
130   // this should be unused, we need to make main prog code
131   // independent of any array thing.
132   printf("ERROR - Using disass[] table in XA sim code!\n");
133   return(glob_disass_xa);
134 }
135
136 struct name_entry *cl_xa::sfr_tbl(void)
137 {
138   return(sfr_tabXA51);
139 }
140
141 struct name_entry *cl_xa::bit_tbl(void)
142 {
143   return(bit_tabXA51);
144 }
145
146 int
147 cl_xa::inst_length(t_addr addr)
148 {
149   int len = 0;
150
151   get_disasm_info(addr, &len, NULL, NULL, NULL, NULL);
152
153   return len;
154 }
155
156 int
157 cl_xa::inst_branch(t_addr addr)
158 {
159   int b;
160
161   get_disasm_info(addr, NULL, &b, NULL, NULL, NULL);
162
163   return b;
164 }
165
166 int
167 cl_xa::longest_inst(void)
168 {
169   return 6;
170 }
171
172 static char dir_name[64];
173 char *cl_xa::get_dir_name(short addr) {
174   if (!get_name(addr, sfr_tbl(), dir_name)) {
175     sprintf (dir_name, "0x%03x", addr);
176   }
177   return dir_name;
178 }
179
180 static char bit_name[64];
181 char *cl_xa::get_bit_name(short addr) {
182   if (!get_name(addr, bit_tbl(), bit_name)) {
183     sprintf (bit_name, "0x%03x", addr);
184   }
185   return bit_name;
186 }
187
188 /*--------------------------------------------------------------------
189 get_disasm_info - Given an address, return information about the opcode
190   which resides there.
191   addr - address of opcode we want information on.
192   ret_len - return length of opcode.
193   ret_branch - return a character which indicates if we are
194     a branching opcode.  Used by main app to implement "Next"
195     function which steps over functions.
196   immed_offset - return a number which represents the number of bytes
197     offset to where any immediate data is(tail end of opcode).  Used
198     for printing disassembly.
199   operands - return a key indicating the form of the operands,
200     used for printing the disassembly.
201   mnemonic - return a key indicating the mnemonic of the instruction.
202
203   Return value: Return the operand code formed by either the single
204   byte opcode or 2 bytes of the opcode for multi-byte opcodes.
205
206   Note: Any of the return pointer parameters can be set to NULL to
207     indicate the caller does not want the information.
208 |--------------------------------------------------------------------*/
209 int
210 cl_xa::get_disasm_info(t_addr addr,
211                        int *ret_len,
212                        int *ret_branch,
213                        int *immed_offset,
214                        int *parms,
215                        int *mnemonic)
216 {
217   uint code;
218   int len = 0;
219   int immed_n = 0;
220   int i;
221   int start_addr = addr;
222
223   code= get_mem(MEM_ROM, addr++);
224   if (code == 0x00) {
225     i= 0;
226     while (disass_xa[i].mnemonic != NOP)
227       i++;
228   } else {
229     len = 2;
230     code = (code << 8) | get_mem(MEM_ROM, addr++);
231     i= 0;
232     while ((code & disass_xa[i].mask) != disass_xa[i].code &&
233            disass_xa[i].mnemonic != BAD_OPCODE)
234       i++;
235   }
236
237   if (ret_len)
238     *ret_len = disass_xa[i].length;
239   if (ret_branch)
240    *ret_branch = disass_xa[i].branch;
241   if (immed_offset) {
242     if (immed_n > 0)
243          *immed_offset = immed_n;
244     else *immed_offset = (addr - start_addr);
245   }
246   if (parms) {
247     *parms = disass_xa[i].operands;
248   }
249   if (mnemonic) {
250     *mnemonic = disass_xa[i].mnemonic;
251   }
252
253   return code;
254 }
255
256 static char *w_reg_strs[] = {
257  "R0", "R1",
258  "R2", "R3",
259  "R4", "R5",
260  "R6", "R7",
261  "R8", "R9",
262  "R10", "R11",
263  "R12", "R13",
264  "R14", "R15"};
265
266 static char *b_reg_strs[] = {
267  "R0l", "R0h",
268  "R1l", "R1h",
269  "R2l", "R2h",
270  "R3l", "R3h",
271  "R4l", "R4h",
272  "R5l", "R5h",
273  "R6l", "R6h",
274  "R7l", "R7h"};
275
276 /*--------------------------------------------------------------------
277 disass - Disassemble an opcode.
278     addr - address of opcode to disassemble/print.
279     sep - optionally points to string(tab) to use as separator.
280 |--------------------------------------------------------------------*/
281 char *
282 cl_xa::disass(t_addr addr, char *sep)
283 {
284   char work[256], parm_str[40];
285   char *buf, *p, *b;
286   int code;
287   int len = 0;
288   int immed_offset = 0;
289   int operands;
290   int mnemonic;
291   char **reg_strs;
292
293   p= work;
294
295   code = get_disasm_info(addr, &len, NULL, &immed_offset, &operands, &mnemonic);
296
297   if (mnemonic == BAD_OPCODE) {
298     buf= (char*)malloc(30);
299     strcpy(buf, "UNKNOWN/INVALID");
300     return(buf);
301   }
302
303   if (code & 0x0800)
304     reg_strs = w_reg_strs;
305   else
306     reg_strs = b_reg_strs;
307
308   switch(operands) {
309      // the repeating common parameter encoding for ADD, ADDC, SUB, AND...
310     case REG_REG :
311       sprintf(parm_str, "%s,%s",
312               reg_strs[((code >> 4) & 0xf)],
313               reg_strs[(code & 0xf)]);
314     break;
315     case REG_IREG :
316       sprintf(parm_str, "%s,[%s]",
317               reg_strs[((code >> 4) & 0xf)],
318               w_reg_strs[(code & 0xf)]);
319     break;
320     case IREG_REG :
321       sprintf(parm_str, "[%s],%s",
322               w_reg_strs[(code & 0x7)],
323               reg_strs[((code >> 4) & 0xf)] );
324     break;
325     case REG_IREGOFF8 :
326       sprintf(parm_str, "%s,[%s+%02x]",
327               reg_strs[((code >> 4) & 0xf)],
328               w_reg_strs[(code & 0x7)],
329               get_mem(MEM_ROM, addr+immed_offset));
330       ++immed_offset;
331     break;
332     case IREGOFF8_REG :
333       sprintf(parm_str, "[%s+%02x],%s",
334               w_reg_strs[(code & 0x7)],
335               get_mem(MEM_ROM, addr+immed_offset),
336               reg_strs[((code >> 4) & 0xf)] );
337       ++immed_offset;
338     break;
339     case REG_IREGOFF16 :
340       sprintf(parm_str, "%s,[%s+%04x]",
341               reg_strs[((code >> 4) & 0xf)],
342               w_reg_strs[(code & 0x7)],
343               (short)((get_mem(MEM_ROM, addr+immed_offset+1)) |
344                      (get_mem(MEM_ROM, addr+immed_offset)<<8)) );
345       ++immed_offset;
346       ++immed_offset;
347     break;
348     case IREGOFF16_REG :
349       sprintf(parm_str, "[%s+%04x],%s",
350               w_reg_strs[(code & 0x7)],
351               (short)((get_mem(MEM_ROM, addr+immed_offset+1)) |
352                      (get_mem(MEM_ROM, addr+immed_offset)<<8)),
353               reg_strs[((code >> 4) & 0xf)] );
354       ++immed_offset;
355       ++immed_offset;
356     break;
357     case REG_IREGINC :
358       sprintf(parm_str, "%s,[%s+]",
359               reg_strs[((code >> 4) & 0xf)],
360               w_reg_strs[(code & 0xf)]);
361     break;
362     case IREGINC_REG :
363       sprintf(parm_str, "[%s+],%s",
364               w_reg_strs[(code & 0x7)],
365               reg_strs[((code >> 4) & 0xf)] );
366     break;
367     case DIRECT_REG :
368       sprintf(parm_str, "%s,%s",
369               get_dir_name(((code & 0x7) << 8) | 
370                            get_mem(MEM_ROM, addr+immed_offset)),
371               reg_strs[((code >> 4) & 0xf)] );
372       ++immed_offset;
373     break;
374     case REG_DIRECT :
375       sprintf(parm_str, "%s,%s",
376               reg_strs[((code >> 4) & 0xf)], 
377               get_dir_name(((code & 0x7) << 8) | 
378                            get_mem(MEM_ROM, addr+immed_offset)));
379       ++immed_offset;
380     break;
381     case REG_DATA8 :
382       sprintf(parm_str, "%s,#0x%02x",
383               b_reg_strs[((code >> 4) & 0xf)],
384               get_mem(MEM_ROM, addr+immed_offset) );
385       ++immed_offset;
386     break;
387     case REG_DATA16 :
388       sprintf(parm_str, "%s,#0x%04x",
389               reg_strs[((code >> 4) & 0xf)],
390               (short)((get_mem(MEM_ROM, addr+immed_offset+1)) |
391                      (get_mem(MEM_ROM, addr+immed_offset)<<8)) );
392       ++immed_offset;
393       ++immed_offset;
394     break;
395     case IREG_DATA8 :
396       sprintf(parm_str, "[%s], 0x%02x",
397               w_reg_strs[((code >> 4) & 0x7)],
398               get_mem(MEM_ROM, addr+immed_offset) );
399       ++immed_offset;
400     break;
401     case IREG_DATA16 :
402       sprintf(parm_str, "[%s], 0x%04x",
403               w_reg_strs[((code >> 4) & 0x7)],
404               (short)((get_mem(MEM_ROM, addr+immed_offset+1)) |
405                      (get_mem(MEM_ROM, addr+immed_offset)<<8)) );
406       ++immed_offset;
407       ++immed_offset;
408     break;
409     case IREGINC_DATA8 :
410       sprintf(parm_str, "[%s+], 0x%02x",
411               w_reg_strs[((code >> 4) & 0x7)],
412               get_mem(MEM_ROM, addr+immed_offset) );
413       ++immed_offset;
414     break;
415     case IREGINC_DATA16 :
416       sprintf(parm_str, "[%s+], 0x%04x",
417               w_reg_strs[((code >> 4) & 0x7)],
418               (short)((get_mem(MEM_ROM, addr+immed_offset+1)) |
419                      (get_mem(MEM_ROM, addr+immed_offset)<<8)) );
420       ++immed_offset;
421       ++immed_offset;
422     break;
423     case IREGOFF8_DATA8 :
424       sprintf(parm_str, "[%s+%02x], 0x%02x",
425               w_reg_strs[((code >> 4) & 0x7)],
426               get_mem(MEM_ROM, addr+immed_offset),
427               get_mem(MEM_ROM, addr+immed_offset+1) );
428       immed_offset += 2;
429     break;
430     case IREGOFF8_DATA16 :
431       sprintf(parm_str, "[%s+%02x], 0x%04x",
432               w_reg_strs[((code >> 4) & 0x7)],
433               get_mem(MEM_ROM, addr+immed_offset),
434               (short)((get_mem(MEM_ROM, addr+immed_offset+2)) |
435                      (get_mem(MEM_ROM, addr+immed_offset+1)<<8)) );
436       immed_offset += 3;
437     break;
438     case IREGOFF16_DATA8 :
439       sprintf(parm_str, "[%s+%04x], 0x%02x",
440               w_reg_strs[((code >> 4) & 0x7)],
441               (short)((get_mem(MEM_ROM, addr+immed_offset+1)) |
442                      (get_mem(MEM_ROM, addr+immed_offset+0)<<8)),
443               get_mem(MEM_ROM, addr+immed_offset+2) );
444       immed_offset += 3;
445     break;
446     case IREGOFF16_DATA16 :
447       sprintf(parm_str, "[%s+%04x], 0x%04x",
448               w_reg_strs[((code >> 4) & 0x7)],
449               (short)((get_mem(MEM_ROM, addr+immed_offset+1)) |
450                      (get_mem(MEM_ROM, addr+immed_offset+0)<<8)),
451               (short)((get_mem(MEM_ROM, addr+immed_offset+3)) |
452                      (get_mem(MEM_ROM, addr+immed_offset+2)<<8)) );
453       immed_offset += 4;
454     break;
455     case DIRECT_DATA8 :
456       sprintf(parm_str, "%s,#0x%02x",
457               get_dir_name(((code & 0x0070) << 4) | 
458                            get_mem(MEM_ROM, addr+immed_offset)),
459               get_mem(MEM_ROM, addr+immed_offset+1));
460       immed_offset += 3;
461     break;
462     case DIRECT_DATA16 :
463       sprintf(parm_str, "%s,#0x%04x",
464               get_dir_name(((code & 0x0070) << 4) | 
465                            get_mem(MEM_ROM, addr+immed_offset)),
466               get_mem(MEM_ROM, addr+immed_offset+2) +
467               (get_mem(MEM_ROM, addr+immed_offset+1)<<8));
468       immed_offset += 3;
469     break;
470
471 // odd-ball ones
472     case NO_OPERANDS :  // for NOP
473       strcpy(parm_str, "");
474     break;
475     case C_BIT :
476       strcpy(parm_str, "C_BIT");
477     break;
478     case REG_DATA4 :
479       strcpy(parm_str, "REG_DATA4");
480     break;
481     case REG_DATA5 :
482       strcpy(parm_str, "REG_DATA5");
483     break;
484     case IREG_DATA4 :
485       strcpy(parm_str, "IREG_DATA4");
486     break;
487     case IREGINC_DATA4 :
488       strcpy(parm_str, "IREGINC_DATA4");
489     break;
490     case IREGOFF8_DATA4 :
491       strcpy(parm_str, "IREGOFF8_DATA4");
492     break;
493     case IREGOFF16_DATA4 :
494       strcpy(parm_str, "IREGOFF16_DATA4");
495     break;
496     case DIRECT_DATA4 :
497       sprintf(parm_str, "%s,#0x%x",
498               get_dir_name(((code & 0x70)<<4) |
499                            get_mem(MEM_ROM, addr+2)),
500               code&0x0f);
501     break;
502     case DIRECT :
503       sprintf(parm_str, "%s",
504               get_dir_name(((code & 0x007) << 4) + 
505                            get_mem(MEM_ROM, addr+2)));
506     break;
507     case REG :
508       sprintf(parm_str, "%s",
509               reg_strs[((code >> 4) & 0xf)] );
510     break;
511     case IREG :
512       sprintf(parm_str, "[%s]",
513               reg_strs[((code >> 4) & 0xf)] );
514     break;
515     case BIT_ALONE :
516       sprintf(parm_str, "%s",
517               get_bit_name(((code&0x0003)<<8) + get_mem(MEM_ROM, addr+2)));
518     break;
519     case BIT_REL8 :
520       sprintf(parm_str, "%s,0x%04x",
521               get_bit_name((code&0x0003)<<8) + get_mem(MEM_ROM, addr+2),
522               ((signed char)get_mem(MEM_ROM, addr+3)*2+addr+len)&0xfffe);
523     break;
524     case ADDR24 :
525       strcpy(parm_str, "ADDR24");
526     break;
527     case REG_REL8 :
528       sprintf(parm_str, "%s,0x%04x",
529               reg_strs[(code>>4) & 0xf],
530               ((signed char)get_mem(MEM_ROM, addr+2)*2+addr+len)&0xfffe);
531     break;
532     case DIRECT_REL8 :
533       sprintf(parm_str, "%s,0x%04x",
534               get_dir_name(((code&0x07)<<8) +
535                            get_mem(MEM_ROM, addr+2)),
536               ((signed char)get_mem(MEM_ROM, addr+2)*2+addr+len)&0xfffe);
537     break;
538
539     case REL8 :
540       sprintf(parm_str, "0x%04x",
541               ((signed char)get_mem(MEM_ROM, addr+1)*2+addr+len)&0xfffe);
542     break;
543     case REL16 :
544       sprintf(parm_str, "0x%04x",
545               ((signed short)((get_mem(MEM_ROM, addr+1)<<8) + get_mem(MEM_ROM, addr+2))*2+addr+len)&0xfffe);
546     break;
547
548     case RLIST : {
549       /* TODO: the list should be comma reperated
550          and maybe for POP the list should be reversed */
551       unsigned char rlist=code&0xff;
552       parm_str[0]='\0';
553       if (code&0x0800) { // word list
554         if (code&0x4000) { // R8-R15
555           if (rlist&0x80) strcat (parm_str, "R15 ");
556           if (rlist&0x40) strcat (parm_str, "R14");
557           if (rlist&0x20) strcat (parm_str, "R13 ");
558           if (rlist&0x10) strcat (parm_str, "R12 ");
559           if (rlist&0x08) strcat (parm_str, "R11 ");
560           if (rlist&0x04) strcat (parm_str, "R10 ");
561           if (rlist&0x02) strcat (parm_str, "R9 ");
562           if (rlist&0x01) strcat (parm_str, "R8 ");
563         } else { // R7-R0
564           if (rlist&0x80) strcat (parm_str, "R7 ");
565           if (rlist&0x40) strcat (parm_str, "R6 ");
566           if (rlist&0x20) strcat (parm_str, "R5 ");
567           if (rlist&0x10) strcat (parm_str, "R4 ");
568           if (rlist&0x08) strcat (parm_str, "R3 ");
569           if (rlist&0x04) strcat (parm_str, "R2 ");
570           if (rlist&0x02) strcat (parm_str, "R1 ");
571           if (rlist&0x01) strcat (parm_str, "R0 ");
572         }
573       } else { // byte list
574         if (code&0x4000) { //R7h-R4l
575           if (rlist&0x80) strcat (parm_str, "R7h ");
576           if (rlist&0x40) strcat (parm_str, "R7l ");
577           if (rlist&0x20) strcat (parm_str, "R6h ");
578           if (rlist&0x10) strcat (parm_str, "R6l ");
579           if (rlist&0x08) strcat (parm_str, "R5h ");
580           if (rlist&0x04) strcat (parm_str, "R5l ");
581           if (rlist&0x02) strcat (parm_str, "R4h ");
582           if (rlist&0x01) strcat (parm_str, "R4l ");
583         } else { // R3h-R0l
584           if (rlist&0x80) strcat (parm_str, "R3h ");
585           if (rlist&0x40) strcat (parm_str, "R3l ");
586           if (rlist&0x20) strcat (parm_str, "R2h ");
587           if (rlist&0x10) strcat (parm_str, "R2l ");
588           if (rlist&0x08) strcat (parm_str, "R1h ");
589           if (rlist&0x04) strcat (parm_str, "R1l ");
590           if (rlist&0x02) strcat (parm_str, "R0h ");
591           if (rlist&0x01) strcat (parm_str, "R0l ");
592         }
593       }
594     }
595     break;
596
597     case REG_DIRECT_REL8 :
598       sprintf(parm_str, "%s,%s,0x%02x",
599               reg_strs[((code >> 4) & 0xf)],
600               get_dir_name(((code & 0x7) << 8) + 
601                            get_mem(MEM_ROM, addr+immed_offset)),
602               ((signed char) get_mem(MEM_ROM, addr+immed_offset+1) * 2) & 0xfffe );
603     break;
604     case REG_DATA8_REL8 :
605       sprintf(parm_str, "%s,#0x%04x,0x%02x",
606               reg_strs[((code >> 4) & 0xf)],
607               get_mem(MEM_ROM, addr+immed_offset),
608               ((signed char)get_mem(MEM_ROM, addr+immed_offset+1) * 2) & 0xfffe );
609     break;
610     case REG_DATA16_REL8 :
611       sprintf(parm_str, "%s,#0x%02x,0x%02x",
612               w_reg_strs[((code >> 4) & 0x7)*2],
613               get_mem(MEM_ROM, addr+immed_offset+1) +
614                  (get_mem(MEM_ROM, addr+immed_offset+0)<<8),
615               ((signed char)get_mem(MEM_ROM, addr+immed_offset+2) * 2) & 0xfffe );
616     break;
617     case IREG_DATA8_REL8 :
618       sprintf(parm_str, "[%s],#0x%04x,0x%02x",
619               reg_strs[((code >> 4) & 0x7)],
620               get_mem(MEM_ROM, addr+immed_offset),
621               ((signed char)get_mem(MEM_ROM, addr+immed_offset+1) * 2) & 0xfffe );
622     break;
623     case IREG_DATA16_REL8 :
624       sprintf(parm_str, "[%s],#0x%02x,0x%02x",
625               w_reg_strs[((code >> 4) & 0x7)*2],
626               get_mem(MEM_ROM, addr+immed_offset+1) +
627                  (get_mem(MEM_ROM, addr+immed_offset+0)<<8),
628               ((signed char)get_mem(MEM_ROM, addr+immed_offset+2) * 2) & 0xfffe );
629     break;
630
631     case A_APLUSDPTR :
632       strcpy(parm_str, "A, [A+DPTR]");
633     break;
634
635     case A_APLUSPC :
636       strcpy(parm_str, "A, [A+PC]");
637     break;
638
639     default:
640       strcpy(parm_str, "???");
641     break;
642   }
643
644   sprintf(work, "%s %s",
645           op_mnemonic_str[ mnemonic ],
646           parm_str);
647
648   p= strchr(work, ' ');
649   if (!p)
650     {
651       buf= strdup(work);
652       return(buf);
653     }
654   if (sep == NULL)
655     buf= (char *)malloc(6+strlen(p)+1);
656   else
657     buf= (char *)malloc((p-work)+strlen(sep)+strlen(p)+1);
658
659   for (p= work, b= buf; *p != ' '; p++, b++)
660     *b= *p;
661   p++;
662   *b= '\0';
663   if (sep == NULL)
664     {
665       while (strlen(buf) < 6)
666         strcat(buf, " ");
667     }
668   else
669     strcat(buf, sep);
670   strcat(buf, p);
671   return(buf);
672 }
673
674 /*--------------------------------------------------------------------
675  print_regs - Print the registers, flags and other useful information.
676    Used to print a status line while stepping through the code.
677 |--------------------------------------------------------------------*/
678 void
679 cl_xa::print_regs(class cl_console *con)
680 {
681   unsigned char flags;
682
683   flags = get_psw();
684   con->dd_printf("CA---VNZ | ", flags);
685   con->dd_printf("R0:%04x R1:%04x R2:%04x R3:%04x\n",
686                  reg2(0), reg2(1), reg2(2), reg2(3));
687
688   con->dd_printf("%c%c---%c%c%c | ",
689          (flags & BIT_C)?'1':'0',
690          (flags & BIT_AC)?'1':'0',
691          (flags & BIT_V)?'1':'0',
692          (flags & BIT_N)?'1':'0',
693          (flags & BIT_Z)?'1':'0');
694
695   con->dd_printf("R4:%04x R5:%04x R6:%04x SP:%04x ES:%04x  DS:%04x\n",
696                  reg2(4), reg2(5), reg2(6), reg2(7), 0, 0);
697
698   print_disass(PC, con);
699 }
700
701
702 /*--------------------------------------------------------------------
703  exec_inst - Called to implement simulator execution of 1 instruction
704    at the current PC(program counter) address.
705 |--------------------------------------------------------------------*/
706 int cl_xa::exec_inst(void)
707 {
708   t_mem code1;
709   uint code;
710   int i;
711   int operands;
712
713   if (fetch(&code1))
714     return(resBREAKPOINT);
715   tick(1);
716
717 /* the following lookups make for a slow simulation, we will
718   figure out how to make it fast later... */
719
720   /* scan to see if its a 1 byte-opcode */
721   code = (code1 << 8);
722   i= 0;
723   while ( ((code & disass_xa[i].mask) != disass_xa[i].code ||
724           (!disass_xa[i].is1byte)) /* not a one byte op code */
725                     &&
726          disass_xa[i].mnemonic != BAD_OPCODE)
727     i++;
728
729   if (disass_xa[i].mnemonic == BAD_OPCODE) {
730     /* hit the end of the list, must be a 2 or more byte opcode */
731     /* fetch another code byte and search the list again */
732       //if (fetch(&code2))  ?not sure if break allowed in middle of opcode?
733       //  return(resBREAKPOINT);
734     code |= fetch();  /* add 2nd opcode */
735
736     i= 0;
737     while ((code & disass_xa[i].mask) != disass_xa[i].code &&
738            disass_xa[i].mnemonic != BAD_OPCODE)
739       i++;
740     /* we should have found the opcode by now, if not invalid entry at eol */
741   }
742
743   operands = (int)(disass_xa[i].operands);
744   switch (disass_xa[i].mnemonic)
745   {
746     case ADD:
747     return inst_ADD(code, operands);
748     case ADDC:
749     return inst_ADDC(code, operands);
750     case ADDS:
751     return inst_ADDS(code, operands);
752     case AND:
753     return inst_AND(code, operands);
754     case ANL:
755     return inst_ANL(code, operands);
756     case ASL:
757     return inst_ASL(code, operands);
758     case ASR:
759     return inst_ASR(code, operands);
760     case BCC:
761     return inst_BCC(code, operands);
762     case BCS:
763     return inst_BCS(code, operands);
764     case BEQ:
765     return inst_BEQ(code, operands);
766     case BG:
767     return inst_BG(code, operands);
768     case BGE:
769     return inst_BGE(code, operands);
770     case BGT:
771     return inst_BGT(code, operands);
772     case BKPT:
773     return inst_BKPT(code, operands);
774     case BL:
775     return inst_BL(code, operands);
776     case BLE:
777     return inst_BLE(code, operands);
778     case BLT:
779     return inst_BLT(code, operands);
780     case BMI:
781     return inst_BMI(code, operands);
782     case BNE:
783     return inst_BNE(code, operands);
784     case BNV:
785     return inst_BNV(code, operands);
786     case BOV:
787     return inst_BOV(code, operands);
788     case BPL:
789     return inst_BPL(code, operands);
790     case BR:
791     return inst_BR(code, operands);
792     case CALL:
793     return inst_CALL(code, operands);
794     case CJNE:
795     return inst_CJNE(code, operands);
796     case CLR:
797     return inst_CLR(code, operands);
798     case CMP:
799     return inst_CMP(code, operands);
800     case CPL:
801     return inst_CPL(code, operands);
802     case DA:
803     return inst_DA(code, operands);
804     case DIV_w :
805     case DIV_d :
806     case DIVU_b:
807     case DIVU_w:
808     case DIVU_d:
809     return inst_DIV(code, operands);
810     case DJNZ:
811     return inst_DJNZ(code, operands);
812     case FCALL:
813     return inst_FCALL(code, operands);
814     case FJMP:
815     return inst_FJMP(code, operands);
816     case JB:
817     return inst_JB(code, operands);
818     case JBC:
819     return inst_JBC(code, operands);
820     case JMP:
821     return inst_JMP(code, operands);
822     case JNB:
823     return inst_JNB(code, operands);
824     case JNZ:
825     return inst_JNZ(code, operands);
826     case JZ:
827     return inst_JZ(code, operands);
828     case LEA:
829     return inst_LEA(code, operands);
830     case LSR:
831     return inst_LSR(code, operands);
832     case MOV:
833     return inst_MOV(code, operands);
834     case MOVC:
835     return inst_MOVC(code, operands);
836     case MOVS:
837     return inst_MOVS(code, operands);
838     case MOVX:
839     return inst_MOVX(code, operands);
840     case MUL_w:
841     case MULU_b:
842     case MULU_w:
843     return inst_MUL(code, operands);
844     case NEG:
845     return inst_NEG(code, operands);
846     case NOP:
847     return inst_NOP(code, operands);
848     case NORM:
849     return inst_NORM(code, operands);
850     case OR:
851     return inst_OR(code, operands);
852     case ORL:
853     return inst_ORL(code, operands);
854     case POP:
855     case POPU:
856     return inst_POP(code, operands);
857     case PUSH:
858     case PUSHU:
859     return inst_PUSH(code, operands);
860     case RESET:
861     return inst_RESET(code, operands);
862     case RET:
863     return inst_RET(code, operands);
864     case RETI:
865     return inst_RETI(code, operands);
866     case RL:
867     return inst_RL(code, operands);
868     case RLC:
869     return inst_RLC(code, operands);
870     case RR:
871     return inst_RR(code, operands);
872     case RRC:
873     return inst_RRC(code, operands);
874     case SETB:
875     return inst_SETB(code, operands);
876     case SEXT:
877     return inst_SEXT(code, operands);
878     case SUB:
879     return inst_SUB(code, operands);
880     case SUBB:
881     return inst_SUBB(code, operands);
882     case TRAP:
883     return inst_TRAP(code, operands);
884     case XCH:
885     return inst_XCH(code, operands);
886     case XOR:
887     return inst_XOR(code, operands);
888
889     case BAD_OPCODE:
890     default:
891     break;
892   }
893
894   if (PC)
895     PC--;
896   else
897     PC= get_mem_size(MEM_ROM)-1;
898   //tick(-clock_per_cycle());
899   sim->stop(resINV_INST);
900   return(resINV_INST);
901 }
902
903
904 /* End of xa.src/xa.cc */