summaryrefslogtreecommitdiff
path: root/source/z80.c
diff options
context:
space:
mode:
Diffstat (limited to 'source/z80.c')
-rw-r--r--source/z80.c191
1 files changed, 78 insertions, 113 deletions
diff --git a/source/z80.c b/source/z80.c
index 60dbca4..36c5793 100644
--- a/source/z80.c
+++ b/source/z80.c
@@ -55,40 +55,43 @@ static void Z80_CheckInterrupt(Z80 *cpu)
{
/* Check interrupts
*/
- if (cpu->raise)
+ if (PRIV->raise)
{
- if (cpu->nmi)
+ if (PRIV->nmi)
{
- if (cpu->halt)
+ if (PRIV->halt)
{
- cpu->halt=FALSE;
+ PRIV->halt=FALSE;
CALLBACK(eZ80_Halt,0);
cpu->PC++;
}
TSTATE(2);
cpu->IFF1=0;
- cpu->nmi=FALSE;
+ PRIV->nmi=FALSE;
PUSH(cpu->PC);
cpu->PC=0x66;
}
else if (cpu->IFF1)
{
- if (cpu->halt)
+ if (PRIV->halt)
{
- cpu->halt=FALSE;
+ PRIV->halt=FALSE;
CALLBACK(eZ80_Halt,0);
cpu->PC++;
}
TSTATE(2);
+ cpu->IFF1=0;
+ cpu->IFF2=0;
+
switch(cpu->IM)
{
default:
case 0:
INC_R;
- Z80_Decode(cpu,cpu->devbyte);
+ Z80_Decode(cpu,PRIV->devbyte);
return;
break;
@@ -99,12 +102,12 @@ static void Z80_CheckInterrupt(Z80 *cpu)
case 2:
PUSH(cpu->PC);
- cpu->PC=(Z80Word)cpu->I*256+cpu->devbyte;
+ cpu->PC=(Z80Word)cpu->I*256+PRIV->devbyte;
break;
}
}
- cpu->raise=FALSE;
+ PRIV->raise=FALSE;
}
}
@@ -112,11 +115,16 @@ static void Z80_CheckInterrupt(Z80 *cpu)
/* ---------------------------------------- INTERFACES
*/
-Z80 *Z80Init(Z80ReadMemory read_memory,
- Z80WriteMemory write_memory,
- Z80ReadPort read_port,
- Z80WritePort write_port,
- Z80ReadMemory read_disassem)
+#ifdef ENABLE_ARRAY_MEMORY
+Z80 *Z80Init(Z80ReadPort read_port,
+ Z80WritePort write_port)
+#else
+Z80 *Z80Init(Z80ReadMemory read_memory,
+ Z80WriteMemory write_memory,
+ Z80ReadPort read_port,
+ Z80WritePort write_port,
+ Z80ReadMemory read_for_disassem)
+#endif
{
Z80 *cpu;
int f;
@@ -124,24 +132,38 @@ Z80 *Z80Init(Z80ReadMemory read_memory,
InitTables();
+#ifndef ENABLE_ARRAY_MEMORY
if (!read_memory || !write_memory)
return NULL;
+#endif
cpu=malloc(sizeof *cpu);
if (cpu)
{
- cpu->mread=read_memory;
- cpu->mwrite=write_memory;
- cpu->pread=read_port;
- cpu->pwrite=write_port;
- cpu->disread=read_disassem;
+ cpu->priv=malloc(sizeof *cpu->priv);
+
+ if (cpu->priv)
+ {
+#ifndef ENABLE_ARRAY_MEMORY
+ PRIV->mread=read_memory;
+ PRIV->mwrite=write_memory;
+ PRIV->disread=read_for_disassem;
+#endif
+ PRIV->pread=read_port;
+ PRIV->pwrite=write_port;
- for(f=0;f<eZ80_NO_CALLBACK;f++)
- for(r=0;r<MAX_PER_CALLBACK;r++)
- cpu->callback[f][r]=NULL;
+ for(f=0;f<eZ80_NO_CALLBACK;f++)
+ for(r=0;r<MAX_PER_CALLBACK;r++)
+ PRIV->callback[f][r]=NULL;
- Z80Reset(cpu);
+ Z80Reset(cpu);
+ }
+ else
+ {
+ free(cpu);
+ cpu=NULL;
+ }
}
return cpu;
@@ -150,7 +172,7 @@ Z80 *Z80Init(Z80ReadMemory read_memory,
void Z80Reset(Z80 *cpu)
{
- cpu->cycle=0;
+ PRIV->cycle=0;
cpu->PC=0;
cpu->AF.w=0xffff;
@@ -171,28 +193,22 @@ void Z80Reset(Z80 *cpu)
cpu->IM=0;
cpu->I=0;
cpu->R=0;
- cpu->halt=0;
-
- cpu->raise=FALSE;
- cpu->nmi=FALSE;
-}
-
+ PRIV->halt=0;
-void Z80SetPC(Z80 *cpu,Z80Word PC)
-{
- cpu->PC=PC;
+ PRIV->raise=FALSE;
+ PRIV->nmi=FALSE;
}
-Z80Word Z80GetPC(Z80 *cpu)
+Z80Val Z80Cycles(Z80 *cpu)
{
- return cpu->PC;
+ return PRIV->cycle;
}
void Z80ResetCycles(Z80 *cpu, Z80Val cycles)
{
- cpu->cycle=cycles;
+ PRIV->cycle=cycles;
}
@@ -202,9 +218,9 @@ int Z80LodgeCallback(Z80 *cpu, Z80CallbackReason reason, Z80Callback callback)
for(f=0;f<MAX_PER_CALLBACK;f++)
{
- if (!cpu->callback[reason][f])
+ if (!PRIV->callback[reason][f])
{
- cpu->callback[reason][f]=callback;
+ PRIV->callback[reason][f]=callback;
return TRUE;
}
}
@@ -218,23 +234,27 @@ void Z80RemoveCallback(Z80 *cpu, Z80CallbackReason reason, Z80Callback callback)
int f;
for(f=0;f<MAX_PER_CALLBACK;f++)
- if (cpu->callback[reason][f]==callback)
- cpu->callback[reason][f]=NULL;
+ {
+ if (PRIV->callback[reason][f]==callback)
+ {
+ PRIV->callback[reason][f]=NULL;
+ }
+ }
}
void Z80Interrupt(Z80 *cpu, Z80Byte devbyte)
{
- cpu->raise=TRUE;
- cpu->devbyte=devbyte;
- cpu->nmi=FALSE;
+ PRIV->raise=TRUE;
+ PRIV->devbyte=devbyte;
+ PRIV->nmi=FALSE;
}
void Z80NMI(Z80 *cpu)
{
- cpu->raise=TRUE;
- cpu->nmi=TRUE;
+ PRIV->raise=TRUE;
+ PRIV->nmi=TRUE;
}
@@ -242,12 +262,12 @@ int Z80SingleStep(Z80 *cpu)
{
Z80Byte opcode;
- cpu->last_cb=TRUE;
- cpu->shift=0;
+ PRIV->last_cb=TRUE;
+ PRIV->shift=0;
Z80_CheckInterrupt(cpu);
- CALLBACK(eZ80_Instruction,cpu->cycle);
+ CALLBACK(eZ80_Instruction,PRIV->cycle);
INC_R;
@@ -255,7 +275,7 @@ int Z80SingleStep(Z80 *cpu)
Z80_Decode(cpu,opcode);
- return cpu->last_cb;
+ return PRIV->last_cb;
}
@@ -265,68 +285,6 @@ void Z80Exec(Z80 *cpu)
}
-void Z80GetState(Z80 *cpu, Z80State *state)
-{
- state->cycle= cpu->cycle;
-
- state->AF = cpu->AF.w;
- state->BC = cpu->BC.w;
- state->DE = cpu->DE.w;
- state->HL = cpu->HL.w;
-
- state->AF_ = cpu->AF_;
- state->BC_ = cpu->BC_;
- state->DE_ = cpu->DE_;
- state->HL_ = cpu->HL_;
-
- state->IX = cpu->IX.w;
- state->IY = cpu->IY.w;
-
- state->SP = cpu->SP;
- state->PC = cpu->PC;
-
- state->IFF1 = cpu->IFF1;
- state->IFF2 = cpu->IFF2;
- state->IM = cpu->IM;
- state->I = cpu->I;
- state->R = cpu->R;
-}
-
-
-void Z80SetState(Z80 *cpu, const Z80State *state)
-{
- cpu->cycle = state->cycle;
-
- cpu->AF.w = state->AF;
- cpu->BC.w = state->BC;
- cpu->DE.w = state->DE;
- cpu->HL.w = state->HL;
-
- cpu->AF_ = state->AF_;
- cpu->BC_ = state->BC_;
- cpu->DE_ = state->DE_;
- cpu->HL_ = state->HL_;
-
- cpu->IX.w = state->IX;
- cpu->IY.w = state->IY;
-
- cpu->SP = state->SP;
- cpu->PC = state->PC;
-
- cpu->IFF1 = state->IFF1;
- cpu->IFF2 = state->IFF2;
- cpu->IM = state->IM;
- cpu->I = state->I;
- cpu->R = state->R;
-}
-
-
-Z80Val Z80Cycles(Z80 *cpu)
-{
- return cpu->cycle;
-}
-
-
void Z80SetLabels(Z80Label labels[])
{
z80_labels=labels;
@@ -336,6 +294,7 @@ void Z80SetLabels(Z80Label labels[])
const char *Z80Disassemble(Z80 *cpu, Z80Word *pc)
{
#ifdef ENABLE_DISASSEM
+ Z80Byte Z80_Dis_FetchByte(Z80 *cpu, Z80Word *pc);
static char s[80];
Z80Word opc,npc;
Z80Byte op;
@@ -350,7 +309,13 @@ const char *Z80Disassemble(Z80 *cpu, Z80Word *pc)
strcat(s,Z80_Dis_Printf("%-40s ;",Z80_Dis_GetArg()));
for(f=0;f<5 && opc!=npc;f++)
- strcat(s,Z80_Dis_Printf(" %.2x",(int)cpu->disread(cpu,opc++)));
+ {
+#ifdef ENABLE_ARRAY_MEMORY
+ strcat(s,Z80_Dis_Printf(" %.2x",(int)Z80_MEMORY[opc++]));
+#else
+ strcat(s,Z80_Dis_Printf(" %.2x",(int)PRIV->disread(cpu,opc++)));
+#endif
+ }
if (opc!=npc)
for(f=1;f<3;f++)