1 /* Emulation of the Z80 CPU with hooks into the other parts of aylet.
2  * Copyright (C) 1994 Ian Collier. aylet changes (C) 2001-2002 Russell Marks.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #include <stdio.h>
20 #include <signal.h>
21 #include <unistd.h>
22 #include "main.h"
23 #include "z80.h"
24 
25 #define parity(a) (partable[a])
26 
27 unsigned char partable[256]={
28       4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
29       0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
30       0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
31       4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
32       0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
33       4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
34       4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
35       0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
36       0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
37       4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
38       4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
39       0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
40       4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
41       0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
42       0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
43       4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4
44    };
45 
46 
z80loop(unsigned char * data,unsigned char * stacketc)47 void z80loop(unsigned char *data,unsigned char *stacketc)
48 {
49 unsigned char a, f, b, c, d, e, h, l;
50 unsigned char r, a1, f1, b1, c1, d1, e1, h1, l1, i, iff1, iff2, im;
51 unsigned short pc;
52 unsigned short ix, iy, sp;
53 unsigned int radjust;
54 unsigned int ixoriy, new_ixoriy;
55 unsigned int intsample;
56 unsigned char op;
57 int interrupted=0;
58 
59 a=f=b=c=d=e=h=l=a1=f1=b1=c1=d1=e1=h1=l1=i=r=iff1=iff2=im=0;
60 ixoriy=new_ixoriy=0;
61 ix=iy=sp=pc=0;
62 tstates=0;
63 radjust=0;
64 
65 a=a1=b=b1=d=d1=h=h1=data[8];
66 f=f1=c=c1=e=e1=l=l1=data[9];
67 ix=iy=hl;
68 
69 sp=stacketc[0]*256+stacketc[1];
70 
71 while(1)
72   {
73   if(tstates>=tsmax)
74     {
75     do
76       {
77       if(!do_interrupt())
78         {
79         /* they want to exit */
80         return;
81         }
82       }
83     while(playing && paused);	/* when it's paused, we'd better wait here */
84 
85     tstates-=tsmax;
86     interrupted=1;
87     }
88 
89   ixoriy=new_ixoriy;
90   new_ixoriy=0;
91   intsample=1;
92   op=fetch(pc);
93   pc++;
94   radjust++;
95   switch(op)
96     {
97 #include "z80ops.c"
98     }
99 
100   if(interrupted && intsample && iff1)
101     {
102     interrupted=0;
103     if(fetch(pc)==0x76)pc++;
104     iff1=iff2=0;
105     tstates+=5; /* accompanied by an input from the data bus */
106     switch(im)
107       {
108       case 0: /* IM 0 */
109       case 1: /* undocumented */
110       case 2: /* IM 1 */
111         /* there is little to distinguish between these cases */
112         tstates+=7; /* perhaps */
113         push2(pc);
114         pc=0x38;
115         break;
116       case 3: /* IM 2 */
117         tstates+=13; /* perhaps */
118         {
119         int addr=fetch2((i<<8)|0xff);
120         push2(pc);
121         pc=addr;
122         }
123       }
124     }
125   }
126 }
127