1 /*
2  * Simulator of microcontrollers (z80.cc)
3  *
4  * some z80 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 "z80cl.h"
46 #include "glob.h"
47 #include "regsz80.h"
48 
49 #define uint32 t_addr
50 #define uint8 unsigned char
51 
52 /*******************************************************************/
53 
54 
55 /*
56  * Base type of Z80 controllers
57  */
58 
cl_z80(struct cpu_entry * Itype,class cl_sim * asim)59 cl_z80::cl_z80(struct cpu_entry *Itype, class cl_sim *asim):
60   cl_uc(asim)
61 {
62   type= Itype;
63 }
64 
65 int
init(void)66 cl_z80::init(void)
67 {
68 
69   cl_uc::init(); /* Memories now exist */
70 
71   //rom= address_space(MEM_ROM_ID);
72 //  ram= mem(MEM_XRAM);
73   //ram= rom;
74 
75   // zero out ram(this is assumed in regression tests)
76   for (int i=0x8000; i<0x10000; i++) {
77     ram->set((t_addr) i, 0);
78   }
79 
80   return(0);
81 }
82 
83 char *
id_string(void)84 cl_z80::id_string(void)
85 {
86   return((char*)"unspecified Z80");
87 }
88 
89 
90 /*
91  * Making elements of the controller
92  */
93 /*
94 t_addr
95 cl_z80::get_mem_size(enum mem_class type)
96 {
97   switch(type)
98     {
99     case MEM_ROM: return(0x10000);
100     case MEM_XRAM: return(0x10000);
101     default: return(0);
102     }
103  return(cl_uc::get_mem_size(type));
104 }
105 */
106 
107 void
mk_hw_elements(void)108 cl_z80::mk_hw_elements(void)
109 {
110   //class cl_base *o;
111   cl_uc::mk_hw_elements();
112 }
113 
114 void
make_memories(void)115 cl_z80::make_memories(void)
116 {
117   class cl_address_space *as;
118 
119   rom= ram= as= new cl_address_space("rom", 0, 0x10000, 8);
120   as->init();
121   address_spaces->add(as);
122 
123   class cl_address_decoder *ad;
124   class cl_memory_chip *chip;
125 
126   chip= new cl_memory_chip("rom_chip", 0x10000, 8);
127   chip->init();
128   memchips->add(chip);
129   ad= new cl_address_decoder(as= address_space("rom"), chip, 0, 0xffff, 0);
130   ad->init();
131   as->decoders->add(ad);
132   ad->activate(0);
133 
134   inputs= new cl_address_space("inputs", 0, 0x10000, 8);
135   inputs->init();
136   chip= new cl_memory_chip("in_chip", 0x10000, 8);
137   chip->init();
138   memchips->add(chip);
139   ad= new cl_address_decoder(inputs, chip, 0, 0xffff, 0);
140   ad->init();
141   inputs->decoders->add(ad);
142   address_spaces->add(inputs);
143   outputs= new cl_address_space("outputs", 0, 0x10000, 8);
144   outputs->init();
145   chip= new cl_memory_chip("out_chip", 0x10000, 8);
146   chip->init();
147   memchips->add(chip);
148   ad= new cl_address_decoder(outputs, chip, 0, 0xffff, 0);
149   ad->init();
150   outputs->decoders->add(ad);
151   address_spaces->add(outputs);
152 
153   regs8= new cl_address_space("regs8", 0, 16, 8);
154   regs8->init();
155   regs8->get_cell(0)->decode((t_mem*)&regs.raf.A);
156   regs8->get_cell(1)->decode((t_mem*)&regs.raf.F);
157   regs8->get_cell(2)->decode((t_mem*)&regs.bc.h);
158   regs8->get_cell(3)->decode((t_mem*)&regs.bc.l);
159   regs8->get_cell(4)->decode((t_mem*)&regs.de.h);
160   regs8->get_cell(5)->decode((t_mem*)&regs.de.l);
161   regs8->get_cell(6)->decode((t_mem*)&regs.hl.h);
162   regs8->get_cell(7)->decode((t_mem*)&regs.hl.l);
163 
164   regs8->get_cell(8)->decode((t_mem*)&regs.ralt_af.aA);
165   regs8->get_cell(9)->decode((t_mem*)&regs.ralt_af.aF);
166   regs8->get_cell(10)->decode((t_mem*)&regs.a_bc.h);
167   regs8->get_cell(11)->decode((t_mem*)&regs.a_bc.l);
168   regs8->get_cell(12)->decode((t_mem*)&regs.a_de.h);
169   regs8->get_cell(13)->decode((t_mem*)&regs.a_de.l);
170   regs8->get_cell(14)->decode((t_mem*)&regs.a_hl.h);
171   regs8->get_cell(15)->decode((t_mem*)&regs.a_hl.l);
172 
173   regs16= new cl_address_space("regs16", 0, 11, 16);
174   regs16->init();
175 
176   regs16->get_cell(0)->decode((t_mem*)&regs.AF);
177   regs16->get_cell(1)->decode((t_mem*)&regs.BC);
178   regs16->get_cell(2)->decode((t_mem*)&regs.DE);
179   regs16->get_cell(3)->decode((t_mem*)&regs.HL);
180   regs16->get_cell(4)->decode((t_mem*)&regs.IX);
181   regs16->get_cell(5)->decode((t_mem*)&regs.IY);
182   regs16->get_cell(6)->decode((t_mem*)&regs.SP);
183   regs16->get_cell(7)->decode((t_mem*)&regs.aAF);
184   regs16->get_cell(8)->decode((t_mem*)&regs.aBC);
185   regs16->get_cell(9)->decode((t_mem*)&regs.aDE);
186   regs16->get_cell(10)->decode((t_mem*)&regs.aHL);
187 
188   address_spaces->add(regs8);
189   address_spaces->add(regs16);
190 
191   class cl_var *v;
192   vars->add(v= new cl_var(cchars("A"), regs8, 0, ""));
193   v->init();
194   vars->add(v= new cl_var(cchars("F"), regs8, 1, ""));
195   v->init();
196   vars->add(v= new cl_var(cchars("B"), regs8, 2, ""));
197   v->init();
198   vars->add(v= new cl_var(cchars("C"), regs8, 3, ""));
199   v->init();
200   vars->add(v= new cl_var(cchars("D"), regs8, 4, ""));
201   v->init();
202   vars->add(v= new cl_var(cchars("E"), regs8, 5, ""));
203   v->init();
204   vars->add(v= new cl_var(cchars("H"), regs8, 6, ""));
205   v->init();
206   vars->add(v= new cl_var(cchars("L"), regs8, 7, ""));
207   v->init();
208 
209   vars->add(v= new cl_var(cchars("ALT_A"), regs8, 8, ""));
210   v->init();
211   vars->add(v= new cl_var(cchars("ALT_F"), regs8, 9, ""));
212   v->init();
213   vars->add(v= new cl_var(cchars("ALT_B"), regs8, 10, ""));
214   v->init();
215   vars->add(v= new cl_var(cchars("ALT_C"), regs8, 11, ""));
216   v->init();
217   vars->add(v= new cl_var(cchars("ALT_D"), regs8, 12, ""));
218   v->init();
219   vars->add(v= new cl_var(cchars("ALT_E"), regs8, 13, ""));
220   v->init();
221   vars->add(v= new cl_var(cchars("ALT_H"), regs8, 14, ""));
222   v->init();
223   vars->add(v= new cl_var(cchars("ALT_L"), regs8, 15, ""));
224   v->init();
225 
226   vars->add(v= new cl_var(cchars("AF"), regs16, 0, ""));
227   v->init();
228   vars->add(v= new cl_var(cchars("BC"), regs16, 1, ""));
229   v->init();
230   vars->add(v= new cl_var(cchars("DE"), regs16, 2, ""));
231   v->init();
232   vars->add(v= new cl_var(cchars("HL"), regs16, 3, ""));
233   v->init();
234   vars->add(v= new cl_var(cchars("IX"), regs16, 4, ""));
235   v->init();
236   vars->add(v= new cl_var(cchars("IY"), regs16, 5, ""));
237   v->init();
238   vars->add(v= new cl_var(cchars("SP"), regs16, 6, ""));
239   v->init();
240   vars->add(v= new cl_var(cchars("ALT_AF"), regs16, 7, ""));
241   v->init();
242   vars->add(v= new cl_var(cchars("ALT_BC"), regs16, 8, ""));
243   v->init();
244   vars->add(v= new cl_var(cchars("ALT_DE"), regs16, 9, ""));
245   v->init();
246   vars->add(v= new cl_var(cchars("ALT_HL"), regs16, 10, ""));
247   v->init();
248 }
249 
250 
251 /*
252  * Help command interpreter
253  */
254 
255 struct dis_entry *
dis_tbl(void)256 cl_z80::dis_tbl(void)
257 {
258   return(disass_z80);
259 }
260 
261 /*struct name_entry *
262 cl_z80::sfr_tbl(void)
263 {
264   return(0);
265 }*/
266 
267 /*struct name_entry *
268 cl_z80::bit_tbl(void)
269 {
270   //FIXME
271   return(0);
272 }*/
273 
274 int
inst_length(t_addr addr)275 cl_z80::inst_length(t_addr addr)
276 {
277   int len = 0;
278 
279   get_disasm_info(addr, &len, NULL, NULL, NULL);
280 
281   return len;
282 }
283 
284 int
inst_branch(t_addr addr)285 cl_z80::inst_branch(t_addr addr)
286 {
287   int b;
288 
289   get_disasm_info(addr, NULL, &b, NULL, NULL);
290 
291   return b;
292 }
293 
294 bool
is_call(t_addr addr)295 cl_z80::is_call(t_addr addr)
296 {
297   struct dis_entry *e;
298 
299   get_disasm_info(addr, NULL, NULL, NULL, &e);
300 
301   return e?(e->is_call):false;
302 }
303 
304 int
longest_inst(void)305 cl_z80::longest_inst(void)
306 {
307   return 4;
308 }
309 
310 
311 const char *
get_disasm_info(t_addr addr,int * ret_len,int * ret_branch,int * immed_offset,struct dis_entry ** dentry)312 cl_z80::get_disasm_info(t_addr addr,
313                         int *ret_len,
314                         int *ret_branch,
315                         int *immed_offset,
316 			struct dis_entry **dentry)
317 {
318   const char *b = NULL;
319   uint code;
320   int len = 0;
321   int immed_n = 0;
322   int i;
323   int start_addr = addr;
324   struct dis_entry *dis_e;
325 
326   code= rom->get(addr++);
327   dis_e = NULL;
328 
329   switch(code) {
330     case 0xcb:  /* ESC code to lots of op-codes, all 2-byte */
331       code= rom->get(addr++);
332       i= 0;
333       while ((code & disass_z80_cb[i].mask) != disass_z80_cb[i].code &&
334         disass_z80_cb[i].mnemonic)
335         i++;
336       dis_e = &disass_z80_cb[i];
337       b= disass_z80_cb[i].mnemonic;
338       if (b != NULL)
339         len += (disass_z80_cb[i].length + 1);
340     break;
341 
342     case 0xed: /* ESC code to about 80 opcodes of various lengths */
343       code= rom->get(addr++);
344       i= 0;
345       while ((code & disass_z80_ed[i].mask) != disass_z80_ed[i].code &&
346         disass_z80_ed[i].mnemonic)
347         i++;
348       dis_e = &disass_z80_ed[i];
349       b= disass_z80_ed[i].mnemonic;
350       if (b != NULL)
351         len += (disass_z80_ed[i].length + 1);
352     break;
353 
354     case 0xdd: /* ESC codes,about 284, vary lengths, IX centric */
355       code= rom->get(addr++);
356       if (code == 0xcb) {
357         immed_n = 2;
358         addr++;  // pass up immed data
359         code= rom->get(addr++);
360         i= 0;
361         while ((code & disass_z80_ddcb[i].mask) != disass_z80_ddcb[i].code &&
362           disass_z80_ddcb[i].mnemonic)
363           i++;
364         dis_e = &disass_z80_ddcb[i];
365         b= disass_z80_ddcb[i].mnemonic;
366         if (b != NULL)
367           len += (disass_z80_ddcb[i].length + 2);
368       } else {
369         i= 0;
370         while ((code & disass_z80_dd[i].mask) != disass_z80_dd[i].code &&
371           disass_z80_dd[i].mnemonic)
372           i++;
373         dis_e = &disass_z80_dd[i];
374         b= disass_z80_dd[i].mnemonic;
375         if (b != NULL)
376           len += (disass_z80_dd[i].length + 1);
377       }
378     break;
379 
380     case 0xfd: /* ESC codes,sme as dd but IY centric */
381       code= rom->get(addr++);
382       if (code == 0xcb) {
383         immed_n = 2;
384         addr++;  // pass up immed data
385         code= rom->get(addr++);
386         i= 0;
387         while ((code & disass_z80_fdcb[i].mask) != disass_z80_fdcb[i].code &&
388           disass_z80_fdcb[i].mnemonic)
389           i++;
390         dis_e = &disass_z80_fdcb[i];
391         b= disass_z80_fdcb[i].mnemonic;
392         if (b != NULL)
393           len += (disass_z80_fdcb[i].length + 2);
394       } else {
395         i= 0;
396         while ((code & disass_z80_fd[i].mask) != disass_z80_fd[i].code &&
397           disass_z80_fd[i].mnemonic)
398           i++;
399         dis_e = &disass_z80_fd[i];
400         b= disass_z80_fd[i].mnemonic;
401         if (b != NULL)
402           len += (disass_z80_fd[i].length + 1);
403       }
404     break;
405 
406     default:
407       i= 0;
408       while ((code & disass_z80[i].mask) != disass_z80[i].code &&
409              disass_z80[i].mnemonic)
410         i++;
411       dis_e = &disass_z80[i];
412       b= disass_z80[i].mnemonic;
413       if (b != NULL)
414         len += (disass_z80[i].length);
415     break;
416   }
417 
418 
419   if (ret_branch) {
420     *ret_branch = dis_e->branch;
421   }
422 
423   if (immed_offset) {
424     if (immed_n > 0)
425          *immed_offset = immed_n;
426     else *immed_offset = (addr - start_addr);
427   }
428 
429   if (len == 0)
430     len = 1;
431 
432   if (ret_len)
433     *ret_len = len;
434 
435   if (dentry)
436     *dentry= dis_e;
437 
438   return b;
439 }
440 
441 char *
disass(t_addr addr,const char * sep)442 cl_z80::disass(t_addr addr, const char *sep)
443 {
444   char work[256], temp[20];
445   const char *b;
446   char *buf, *p, *t;
447   int len = 0;
448   int immed_offset = 0;
449 
450   p= work;
451 
452   b = get_disasm_info(addr, &len, NULL, &immed_offset, NULL);
453 
454   if (b == NULL) {
455     buf= (char*)malloc(30);
456     strcpy(buf, "UNKNOWN/INVALID");
457     return(buf);
458   }
459 
460   while (*b)
461     {
462       if (*b == '%')
463         {
464           b++;
465           switch (*(b++))
466             {
467             case 'd': // d    jump relative target, signed? byte immediate operand
468               sprintf(temp, "#%d", (signed char)(rom->get(addr+immed_offset)));
469               ++immed_offset;
470               break;
471             case 'w': // w    word immediate operand
472               sprintf(temp, "#0x%04x",
473                  (uint)((rom->get(addr+immed_offset)) |
474                         (rom->get(addr+immed_offset+1)<<8)) );
475               ++immed_offset;
476               ++immed_offset;
477               break;
478             case 'b': // b    byte immediate operand
479               sprintf(temp, "#0x%02x", (uint)rom->get(addr+immed_offset));
480               ++immed_offset;
481               break;
482             default:
483               strcpy(temp, "?");
484               break;
485             }
486           t= temp;
487           while (*t)
488             *(p++)= *(t++);
489         }
490       else
491         *(p++)= *(b++);
492     }
493   *p= '\0';
494 
495   p= strchr(work, ' ');
496   if (!p)
497     {
498       buf= strdup(work);
499       return(buf);
500     }
501   if (sep == NULL)
502     buf= (char *)malloc(6+strlen(p)+1);
503   else
504     buf= (char *)malloc((p-work)+strlen(sep)+strlen(p)+1);
505   for (p= work, t= buf; *p != ' '; p++, t++)
506     *t= *p;
507   p++;
508   *t= '\0';
509   if (sep == NULL)
510     {
511       while (strlen(buf) < 6)
512         strcat(buf, " ");
513     }
514   else
515     strcat(buf, sep);
516   strcat(buf, p);
517   return(buf);
518 }
519 
520 
521 void
print_regs(class cl_console_base * con)522 cl_z80::print_regs(class cl_console_base *con)
523 {
524   con->dd_printf("SZ-A-PNC  Flags= 0x%02x %3d %c  ",
525                  regs.raf.F, regs.raf.F, isprint(regs.raf.F)?regs.raf.F:'.');
526   con->dd_printf("A= 0x%02x %3d %c\n",
527                  regs.raf.A, regs.raf.A, isprint(regs.raf.A)?regs.raf.A:'.');
528   con->dd_printf("%c%c-%c-%c%c%c\n",
529                  (regs.raf.F&BIT_S)?'1':'0',
530                  (regs.raf.F&BIT_Z)?'1':'0',
531                  (regs.raf.F&BIT_A)?'1':'0',
532                  (regs.raf.F&BIT_P)?'1':'0',
533                  (regs.raf.F&BIT_N)?'1':'0',
534                  (regs.raf.F&BIT_C)?'1':'0');
535   con->dd_printf("BC= 0x%04x [BC]= %02x %3d %c  ",
536                  regs.BC, ram->get(regs.BC), ram->get(regs.BC),
537                  isprint(ram->get(regs.BC))?ram->get(regs.BC):'.');
538   con->dd_printf("DE= 0x%04x [DE]= %02x %3d %c  ",
539                  regs.DE, ram->get(regs.DE), ram->get(regs.DE),
540                  isprint(ram->get(regs.DE))?ram->get(regs.DE):'.');
541   con->dd_printf("HL= 0x%04x [HL]= %02x %3d %c\n",
542                  regs.HL, ram->get(regs.HL), ram->get(regs.HL),
543                  isprint(ram->get(regs.HL))?ram->get(regs.HL):'.');
544   con->dd_printf("IX= 0x%04x [IX]= %02x %3d %c  ",
545                  regs.IX, ram->get(regs.IX), ram->get(regs.IX),
546                  isprint(ram->get(regs.IX))?ram->get(regs.IX):'.');
547   con->dd_printf("IY= 0x%04x [IY]= %02x %3d %c  ",
548                  regs.IY, ram->get(regs.IY), ram->get(regs.IY),
549                  isprint(ram->get(regs.IY))?ram->get(regs.IY):'.');
550   con->dd_printf("SP= 0x%04x [SP]= %02x %3d %c\n",
551                  regs.SP, ram->get(regs.SP), ram->get(regs.SP),
552                  isprint(ram->get(regs.SP))?ram->get(regs.SP):'.');
553 
554   print_disass(PC, con);
555 }
556 
557 /*
558  * Execution
559  */
560 
561 int
exec_inst(void)562 cl_z80::exec_inst(void)
563 {
564   t_mem code;
565 
566   instPC= PC;
567 
568   if (fetch(&code))
569     return(resBREAKPOINT);
570   tick(1);
571   switch (code)
572     {
573     case 0x00: return(inst_nop(code));
574     case 0x01: case 0x02: case 0x06: return(inst_ld(code));
575     case 0x03: case 0x04: return(inst_inc(code));
576     case 0x05: return(inst_dec(code));
577     case 0x07: return(inst_rlca(code));
578 
579     case 0x08: return(inst_ex(code));
580     case 0x09: return(inst_add(code));
581     case 0x0a: case 0x0e: return(inst_ld(code));
582     case 0x0b: case 0x0d: return(inst_dec(code));
583     case 0x0c: return(inst_inc(code));
584     case 0x0f: return(inst_rrca(code));
585 
586 
587     case 0x10: return(inst_djnz(code));
588     case 0x11: case 0x12: case 0x16: return(inst_ld(code));
589     case 0x13: case 0x14: return(inst_inc(code));
590     case 0x15: return(inst_dec(code));
591     case 0x17: return(inst_rla(code));
592 
593     case 0x18: return(inst_jr(code));
594     case 0x19: return(inst_add(code));
595     case 0x1a: case 0x1e: return(inst_ld(code));
596     case 0x1b: case 0x1d: return(inst_dec(code));
597     case 0x1c: return(inst_inc(code));
598     case 0x1f: return(inst_rra(code));
599 
600 
601     case 0x20: return(inst_jr(code));
602     case 0x21: case 0x22: case 0x26: return(inst_ld(code));
603     case 0x23: case 0x24: return(inst_inc(code));
604     case 0x25: return(inst_dec(code));
605     case 0x27: return(inst_daa(code));
606 
607     case 0x28: return(inst_jr(code));
608     case 0x29: return(inst_add(code));
609     case 0x2a: case 0x2e: return(inst_ld(code));
610     case 0x2b: case 0x2d: return(inst_dec(code));
611     case 0x2c: return(inst_inc(code));
612     case 0x2f: return(inst_cpl(code));
613 
614 
615     case 0x30: return(inst_jr(code));
616     case 0x31: case 0x32: case 0x36: return(inst_ld(code));
617     case 0x33: case 0x34: return(inst_inc(code));
618     case 0x35: return(inst_dec(code));
619     case 0x37: return(inst_scf(code));
620 
621     case 0x38: return(inst_jr(code));
622     case 0x39: return(inst_add(code));
623     case 0x3a: case 0x3e: return(inst_ld(code));
624     case 0x3b: case 0x3d: return(inst_dec(code));
625     case 0x3c: return(inst_inc(code));
626     case 0x3f: return(inst_ccf(code));
627 
628     case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
629     case 0x48: case 0x49: case 0x4a: case 0x4b: case 0x4c: case 0x4d: case 0x4e: case 0x4f:
630       return(inst_ld(code));
631 
632     case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
633     case 0x58: case 0x59: case 0x5a: case 0x5b: case 0x5c: case 0x5d: case 0x5e: case 0x5f:
634       return(inst_ld(code));
635 
636     case 0x60: case 0x61: case 0x62: case 0x63: case 0x64: case 0x65: case 0x66: case 0x67:
637     case 0x68: case 0x69: case 0x6a: case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f:
638       return(inst_ld(code));
639 
640     case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x77:
641     case 0x78: case 0x79: case 0x7a: case 0x7b: case 0x7c: case 0x7d: case 0x7e: case 0x7f:
642       return(inst_ld(code));
643     case 0x76:
644       return(inst_halt(code));
645 
646     case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
647       return(inst_add(code));
648     case 0x88: case 0x89: case 0x8a: case 0x8b: case 0x8c: case 0x8d: case 0x8e: case 0x8f:
649       return(inst_adc(code));
650 
651     case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
652       return(inst_sub(code));
653     case 0x98: case 0x99: case 0x9a: case 0x9b: case 0x9c: case 0x9d: case 0x9e: case 0x9f:
654       return(inst_sbc(code));
655 
656     case 0xa0: case 0xa1: case 0xa2: case 0xa3: case 0xa4: case 0xa5: case 0xa6: case 0xa7:
657       return(inst_and(code));
658     case 0xa8: case 0xa9: case 0xaa: case 0xab: case 0xac: case 0xad: case 0xae: case 0xaf:
659       return(inst_xor(code));
660 
661     case 0xb0: case 0xb1: case 0xb2: case 0xb3: case 0xb4: case 0xb5: case 0xb6: case 0xb7:
662       return(inst_or(code));
663     case 0xb8: case 0xb9: case 0xba: case 0xbb: case 0xbc: case 0xbd: case 0xbe: case 0xbf:
664       return(inst_cp(code));
665 
666     case 0xc0: return(inst_ret(code));
667     case 0xc1: return(inst_pop(code));
668     case 0xc2: case 0xc3: return(inst_jp(code));
669     case 0xc4: return(inst_call(code));
670     case 0xc5: return(inst_push(code));
671     case 0xc6: return(inst_add(code));
672     case 0xc7: return(inst_rst(code));
673 
674     case 0xc8: case 0xc9: return(inst_ret(code));
675     case 0xca: return(inst_jp(code));
676 
677       /* CB escapes out to 2 byte opcodes(CB include), opcodes
678          to do register bit manipulations */
679     case 0xcb: return(inst_cb());
680     case 0xcc: case 0xcd: return(inst_call(code));
681     case 0xce: return(inst_adc(code));
682     case 0xcf: return(inst_rst(code));
683 
684 
685     case 0xd0: return(inst_ret(code));
686     case 0xd1: return(inst_pop(code));
687     case 0xd2: return(inst_jp(code));
688     case 0xd3: return(inst_out(code));
689     case 0xd4: return(inst_call(code));
690     case 0xd5: return(inst_push(code));
691     case 0xd6: return(inst_sub(code));
692     case 0xd7: return(inst_rst(code));
693 
694     case 0xd8: return(inst_ret(code));
695     case 0xd9: return(inst_exx(code));
696     case 0xda: return(inst_jp(code));
697     case 0xdb: return(inst_in(code));
698     case 0xdc: return(inst_call(code));
699       /* DD escapes out to 2 to 4 byte opcodes(DD included)
700         with a variety of uses.  It can precede the CB escape
701         sequence to extend CB codes with IX+immed_byte */
702     case 0xdd: return(inst_dd(0xdd));
703     case 0xde: return(inst_sbc(code));
704     case 0xdf: return(inst_rst(code));
705 
706 
707     case 0xe0: return(inst_ret(code));
708     case 0xe1: return(inst_pop(code));
709     case 0xe2: return(inst_jp(code));
710     case 0xe3: return(inst_ex(code));
711     case 0xe4: return(inst_call(code));
712     case 0xe5: return(inst_push(code));
713     case 0xe6: return(inst_and(code));
714     case 0xe7: return(inst_rst(code));
715 
716     case 0xe8: return(inst_ret(code));
717     case 0xe9: return(inst_jp(code));
718     case 0xea: return(inst_jp(code));
719     case 0xeb: return(inst_ex(code));
720     case 0xec: return(inst_call(code));
721       /* ED escapes out to misc IN, OUT and other oddball opcodes */
722     case 0xed: return(inst_ed(0xed));
723     case 0xee: return(inst_xor(code));
724     case 0xef: return(inst_rst(code));
725 
726 
727     case 0xf0: return(inst_ret(code));
728     case 0xf1: return(inst_pop(code));
729     case 0xf2: return(inst_jp(code));
730     case 0xf3: return(inst_di(code));
731     case 0xf4: return(inst_call(code));
732     case 0xf5: return(inst_push(code));
733     case 0xf6: return(inst_or(code));
734     case 0xf7: return(inst_rst(code));
735 
736     case 0xf8: return(inst_ret(code));
737     case 0xf9: return(inst_ld(code));
738     case 0xfa: return(inst_jp(code));
739     case 0xfb: return(inst_ei(code));
740     case 0xfc: return(inst_call(code));
741       /* DD escapes out to 2 to 4 byte opcodes(DD included)
742         with a variety of uses.  It can precede the CB escape
743         sequence to extend CB codes with IX+immed_byte */
744     case 0xfd: return(inst_fd(0xfd));
745     case 0xfe: return(inst_cp(code));
746     case 0xff: return(inst_rst(code));
747     }
748 
749   /*if (PC)
750     PC--;
751   else
752   PC= get_mem_size(MEM_ROM_ID)-1;*/
753   PC= rom->inc_address(PC, -1);
754 
755   sim->stop(resINV_INST);
756   return(resINV_INST);
757 }
758 
store1(u16_t addr,t_mem val)759 void cl_z80::store1( u16_t addr, t_mem val ) {
760   ram->write(addr, val);
761 }
762 
store2(u16_t addr,u16_t val)763 void cl_z80::store2( u16_t addr, u16_t val ) {
764   ram->write(addr,   val & 0xff);
765   ram->write(addr+1, (val >> 8) & 0xff);
766 }
767 
get1(u16_t addr)768 u8_t  cl_z80::get1( u16_t addr ) {
769   return ram->read(addr);
770 }
771 
get2(u16_t addr)772 u16_t  cl_z80::get2( u16_t addr ) {
773   u16_t  l, h;
774 
775   l = ram->read(addr  );
776   h = ram->read(addr+1);
777 
778   return (h << 8) | l;
779 }
780 
fetch1(void)781 t_mem       cl_z80::fetch1( void ) {
782   return fetch( );
783 }
784 
fetch2(void)785 u16_t  cl_z80::fetch2( void ) {
786   u16_t  c1, c2;
787 
788   c1 = fetch( );
789   c2 = fetch( );
790   return (c2 << 8) | c1;
791 }
792 
peek1(void)793 t_mem       cl_z80::peek1 ( void ) {
794   return rom->read(PC);
795 }
796 
in_byte(u16_t ioaddr)797 u8_t  cl_z80:: in_byte( u16_t ioaddr )
798 {
799   return inputs->read(ioaddr);
800 }
801 
out_byte(u16_t ioaddr,u8_t io_val)802 void        cl_z80::out_byte( u16_t ioaddr, u8_t io_val )
803 {
804   outputs->write(ioaddr, io_val);
805   return;
806 }
807 
reg_g_read(t_mem g)808 u8_t  cl_z80::reg_g_read ( t_mem g )
809 {
810   switch( g )
811     {
812     case 0:  return regs.bc.h;
813     case 1:  return regs.bc.l;
814     case 2:  return regs.de.h;
815     case 3:  return regs.de.l;
816     case 4:  return regs.hl.h;
817     case 5:  return regs.hl.l;
818     case 6:  return get1( regs.HL );
819     case 7:  return regs.raf.A;
820     default:
821       return 0xffU;
822     }
823 }
824 
reg_g_store(t_mem g,u8_t new_val)825 void        cl_z80::reg_g_store( t_mem g, u8_t new_val )
826 {
827   switch( g )
828     {
829     case 0:  regs.bc.h = new_val;  break;  /* write to b */
830     case 1:  regs.bc.l = new_val;  break;  /* write to c */
831     case 2:  regs.de.h = new_val;  break;  /* write to d */
832     case 3:  regs.de.l = new_val;  break;  /* write to e */
833     case 4:  regs.hl.h = new_val;  break;  /* write to h */
834     case 5:  regs.hl.l = new_val;  break;  /* write to l */
835     case 6:  /* write to (hl) */
836       store1( regs.HL, new_val );
837       break;
838 
839     case 7:  regs.raf.A    = new_val;  break;  /* write to a */
840     }
841 }
842 
843 /* End of z80.src/z80.cc */
844