uk.co.javagear
Class Z80

java.lang.Object
  extended by uk.co.javagear.Z80

public final class Z80
extends java.lang.Object

Zilog Z80 CPU Emulation.
For generic Z80 use the following need to be implemented correctly:

Version:
18th January 2003
Author:
Copyright (C) 2002-2003 Chris White
See Also:
"JavaGear Final Project Report"

Field Summary
private  Accumulator a
          Accumulator.
private  Registers bc
          Loop counter.
private  int[] daa
          Pre-calculated result for DAA instruction.
private  Registers de
          General purpose.
private  boolean EI_inst
          Previous instruction was EI.
private  Flag f
          Flag register.
private  boolean halt
          Halt instruction.
private  Registers hl
          General purpose.
private  int i
          Interrupt page address register.
private  boolean iff1
          Interrupt flip-flop 1.
private  boolean iff2
          Interrupt flip-flop 2.
private  int im
          Interrupt Mode (0,1,2).
private  InterruptLine irq
          Pointer to Z80 interrupt line.
private  Registers ix
          Index page address register.
private  Registers iy
          Index page address register.
private  Memory mem
          Pointer to system memory.
private  short[] opcbstates
           
private  short[] opddstates
           
private  short[] opedstates
           
private  short[] opindexcbstates
           
private  short[] opstates
           
private  int pc
          Program counter.
private  Ports port
          Pointer to Z80 ports.
private  Refresh r
          Memory refresh register.
private  int sp
          Stack pointer.
private  int tstates
          T states.
 
Constructor Summary
Z80(Memory m, Ports p, InterruptLine i)
          Z80 Constructor.
 
Method Summary
private  void bit(int value, int b)
          CB BIT - Test Bit.
private  void call(boolean condition)
          Call.
private  void ccf()
          CCF - Complement Carry Flag.
private  void consoledebug()
          Output contents of Z80 registers to console for debugging purposes.
private  int d()
          Get Offset Value from memory for index operations.
private  void daa()
          DAA - Decimal Adjust Accumulator adds 6 to left and/or right nibble.
 boolean debug(int cycles)
          Run Z80 for a single instruction (debugging purposes).
private  void dechl(int offset)
          DEC (HL) - Decrement memory location.
private  void doCB(int opcode)
          Execute CB prefixed opcode.
 void doED(int opcode)
          Execute ED Prefixed Opcode.
private  void doIndexCB(Registers index)
          Execute DDCB/FDCB Prefixed Opcode.
private  void doIndexOp(int opcode, Registers index)
          Execute DD/FD Prefixed Index Opcode.
private  void doOp(int opcode)
          Execute opcode.
 void exall()
          Exchange registers with shadow registers.
private  void generateDAATable()
          Pre-calculate DAA Table.
 int getA()
          Get value of accumulator.
 int getB()
          Get value of B register.
 boolean getBit3()
          Get state of bit 3 flag.
 boolean getBit5()
          Get state of bit 5 flag.
 int getC()
          Get value of C register.
 boolean getCarry()
          Get state of the carry flag.
 int getD()
          Get value of D register.
 int getE()
          Get value of E register.
 int getF()
          Get value of flag register.
 int getH()
          Get value of H register.
 boolean getHc()
          Get state of halfcarry flag.
 int getIX()
          Get value of IX register.
 int getIY()
          Get value of IY register.
 int getL()
          Get value of L register.
 java.lang.String getMnu()
          Return mnemonic of next opcode for debugging purposes.
 boolean getNegative()
          Get State of Negative Flag.
 java.lang.String getOp()
          Return next opcode for debugging purposes.
 boolean getParity()
          Get state of parity flag.
 int getPC()
          Get value of the program counter.
 int getR()
          Get value of the Refresh register.
 boolean getSign()
          Get state of sign flag.
 int getSP()
          Get value of the stack pointer.
 boolean getZero()
          Get state of zero flag.
private  void inchl(int offset)
          INC (HL) - increment memory location.
 void interrupt()
          Normal interrupt routine.
private  void jp(boolean condition)
          Jump.
private  void jr(boolean condition)
          Jump relative.
 void nmi()
          Generate Non Maskable Interrupt (NMI).
private  int pop()
          Pop value off stack.
private  void push(int value)
          Push value onto stack.
private  int res(int value, int b)
          CB RES - Reset Bit.
 void reset()
          Reset Z80.
private  void ret(boolean condition)
          Return.
private  int rl(int value)
          CB RL - Rotate Left.
private  int rlc(int value)
          CB RLC - Rotate Left Carry.
private  int rr(int value)
          CB RR - Rotate Right.
private  int rrc(int value)
          CB RRC - Rotate Right Carry.
 void run(int cycles)
          Run Z80.
 void setA(int reg)
          Set accumulator.
 void setBC(int reg)
          Set BC register pair.
private  int setBit(int value, int b)
          CB SET - Set Bit On.
 void setDE(int reg)
          Set DE register pair.
 void setF(int reg)
          Set flag register.
 void setHL(int reg)
          Set HL register pair.
 void setI(int reg)
          Set interrupt register.
 void setIFF2(boolean flag)
          Set interrupt flip-flop 2.
 void setIM(int mode)
          Set interrupt mode.
 void setIX(int reg)
          Set IX register pair.
 void setIY(int reg)
          Set IY register pair.
 void setR(int reg)
          Set refresh register.
 void setSP(int value)
          Set stack pointer.
private  int sla(int value)
          CB SLA - Shift Left Arithmetic.
private  int sll(int value)
          CB SLL - Logical Left Shift.
private  int sra(int value)
          CB SRA - Shift Right Arithmetic.
private  int srl(int value)
          CB SRL - Logical Shift Right.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pc

private int pc
Program counter.


sp

private int sp
Stack pointer.


im

private int im
Interrupt Mode (0,1,2).


iff1

private boolean iff1
Interrupt flip-flop 1.


iff2

private boolean iff2
Interrupt flip-flop 2.


halt

private boolean halt
Halt instruction.


EI_inst

private boolean EI_inst
Previous instruction was EI.


irq

private InterruptLine irq
Pointer to Z80 interrupt line.


mem

private Memory mem
Pointer to system memory.


port

private Ports port
Pointer to Z80 ports.


f

private Flag f
Flag register.


a

private Accumulator a
Accumulator.


bc

private Registers bc
Loop counter.


de

private Registers de
General purpose.


hl

private Registers hl
General purpose.


ix

private Registers ix
Index page address register.


iy

private Registers iy
Index page address register.


r

private Refresh r
Memory refresh register.


i

private int i
Interrupt page address register.


daa

private int[] daa
Pre-calculated result for DAA instruction.


tstates

private int tstates
T states.


opstates

private final short[] opstates

opcbstates

private final short[] opcbstates

opddstates

private final short[] opddstates

opindexcbstates

private final short[] opindexcbstates

opedstates

private final short[] opedstates
Constructor Detail

Z80

public Z80(Memory m,
           Ports p,
           InterruptLine i)
Z80 Constructor.

Parameters:
m - pointer to system memory.
p - pointer to Z80's ports.
i - pointer to Z80's interrupt line.
Method Detail

reset

public void reset()
Reset Z80. Note that some of these values aren't what a real Z80 would reset to. They are the values that the SMS BIOS (to the best of my knowledge) sets the registers to. For example, the Index Registers should reset to 0xFFFF but doing so breaks 'Prince of Persia', so they are set to 0x0000. The stack pointer is also reset to 0xDFF0 as opposed to 0x0000.


setA

public void setA(int reg)
Set accumulator.

Parameters:
reg - value to set register with

setF

public void setF(int reg)
Set flag register.

Parameters:
reg - value to set register with.

setI

public void setI(int reg)
Set interrupt register.

Parameters:
reg - value to set register with.

setR

public void setR(int reg)
Set refresh register.

Parameters:
reg - value to set register with.

setBC

public void setBC(int reg)
Set BC register pair.

Parameters:
reg - value to set register with.

setDE

public void setDE(int reg)
Set DE register pair.

Parameters:
reg - value to set register with.

setHL

public void setHL(int reg)
Set HL register pair.

Parameters:
reg - value to set register with.

setIX

public void setIX(int reg)
Set IX register pair.

Parameters:
reg - value to set register with.

setIY

public void setIY(int reg)
Set IY register pair.

Parameters:
reg - value to set register with.

setSP

public void setSP(int value)
Set stack pointer.

Parameters:
value - value to set stack pointer with

setIM

public void setIM(int mode)
Set interrupt mode.

Parameters:
mode - interrupt mode 0 - 2.

setIFF2

public void setIFF2(boolean flag)
Set interrupt flip-flop 2.

Parameters:
flag - true is On, false is Off.

exall

public void exall()
Exchange registers with shadow registers.


getA

public int getA()
Get value of accumulator.

Returns:
value stored in accumulator.

getF

public int getF()
Get value of flag register.

Returns:
value stored in flag register.

getB

public int getB()
Get value of B register.

Returns:
value stored in B register.

getC

public int getC()
Get value of C register.

Returns:
value stored in C register.

getD

public int getD()
Get value of D register.

Returns:
value stored in D register.

getE

public int getE()
Get value of E register.

Returns:
value stored in E register.

getH

public int getH()
Get value of H register.

Returns:
value stored in H register.

getL

public int getL()
Get value of L register.

Returns:
value stored in L register.

getR

public int getR()
Get value of the Refresh register.

Returns:
value stored in the Refresh register.

getIX

public int getIX()
Get value of IX register.

Returns:
value stored in IX register.

getIY

public int getIY()
Get value of IY register.

Returns:
value stored in IY register.

getPC

public int getPC()
Get value of the program counter.

Returns:
value stored in the program counter.

getSP

public int getSP()
Get value of the stack pointer.

Returns:
value stored in the stack pointer.

getCarry

public boolean getCarry()
Get state of the carry flag.

Returns:
true if set.

getNegative

public boolean getNegative()
Get State of Negative Flag.

Returns:
true if set.

getParity

public boolean getParity()
Get state of parity flag.

Returns:
true if set.

getBit3

public boolean getBit3()
Get state of bit 3 flag.

Returns:
true if set.

getHc

public boolean getHc()
Get state of halfcarry flag.

Returns:
true if set.

getBit5

public boolean getBit5()
Get state of bit 5 flag.

Returns:
true if set.

getZero

public boolean getZero()
Get state of zero flag.

Returns:
true if set.

getSign

public boolean getSign()
Get state of sign flag.

Returns:
true if set.

getOp

public java.lang.String getOp()
Return next opcode for debugging purposes.

Returns:
String containing opcode bytes.

getMnu

public java.lang.String getMnu()
Return mnemonic of next opcode for debugging purposes.

Returns:
String containing opcode bytes.

debug

public boolean debug(int cycles)
Run Z80 for a single instruction (debugging purposes).

Parameters:
cycles - machine cycles to run for in total.
Returns:
true if cycles elapsed.

consoledebug

private void consoledebug()
Output contents of Z80 registers to console for debugging purposes.


run

public void run(int cycles)
Run Z80.

Parameters:
cycles - machine cycles to run for in total.

nmi

public void nmi()
Generate Non Maskable Interrupt (NMI).


interrupt

public void interrupt()
Normal interrupt routine.


doOp

private void doOp(int opcode)
Execute opcode.

Parameters:
opcode - opcode hex value

jp

private void jp(boolean condition)
Jump.

Parameters:
condition - if true jump will be taken.

jr

private void jr(boolean condition)
Jump relative.

Parameters:
condition - if true jump will be taken.

call

private void call(boolean condition)
Call.

Parameters:
condition - if true call will be taken.

ret

private void ret(boolean condition)
Return.

Parameters:
condition - if true return will be taken.

push

private void push(int value)
Push value onto stack.

Parameters:
value - value to push

pop

private int pop()
Pop value off stack.

Returns:
value from stack.

inchl

private void inchl(int offset)
INC (HL) - increment memory location.

Parameters:
offset - memory offset to increment.

dechl

private void dechl(int offset)
DEC (HL) - Decrement memory location.

Parameters:
offset - memory offset to increment.

ccf

private void ccf()
CCF - Complement Carry Flag.


daa

private void daa()
DAA - Decimal Adjust Accumulator adds 6 to left and/or right nibble. Pre-Calculated Result For Speed.


doCB

private void doCB(int opcode)
Execute CB prefixed opcode.

Parameters:
opcode - opcode hex value

rlc

private int rlc(int value)
CB RLC - Rotate Left Carry.

Parameters:
value - value to adjust.
Returns:
adjusted value.

rrc

private int rrc(int value)
CB RRC - Rotate Right Carry.

Parameters:
value - value to adjust.
Returns:
adjusted value.

rl

private int rl(int value)
CB RL - Rotate Left.

Parameters:
value - value to adjust.
Returns:
adjusted value.

rr

private int rr(int value)
CB RR - Rotate Right.

Parameters:
value - value to adjust.
Returns:
adjusted value.

sla

private int sla(int value)
CB SLA - Shift Left Arithmetic.

Parameters:
value - value to adjust.
Returns:
adjusted value.

sra

private int sra(int value)
CB SRA - Shift Right Arithmetic.

Parameters:
value - value to adjust.
Returns:
adjusted value.

sll

private int sll(int value)
CB SLL - Logical Left Shift.

Parameters:
value - value to adjust.
Returns:
adjusted value.

srl

private int srl(int value)
CB SRL - Logical Shift Right.

Parameters:
value - value to adjust.
Returns:
adjusted value.

setBit

private int setBit(int value,
                   int b)
CB SET - Set Bit On.

Parameters:
value - value to adjust.
b - bit to turn on.
Returns:
adjusted value.

res

private int res(int value,
                int b)
CB RES - Reset Bit.

Parameters:
value - value to adjust.
b - bit to turn off.
Returns:
adjusted value.

bit

private void bit(int value,
                 int b)
CB BIT - Test Bit.

Parameters:
value - value to test.
b - bit of value to test.

doIndexOp

private void doIndexOp(int opcode,
                       Registers index)
Execute DD/FD Prefixed Index Opcode.

Parameters:
opcode - opcode hex value.
index - index register to use.

d

private int d()
Get Offset Value from memory for index operations.

Returns:
offset.

doIndexCB

private void doIndexCB(Registers index)
Execute DDCB/FDCB Prefixed Opcode.

Parameters:
index - Index Register To Use.

doED

public void doED(int opcode)
Execute ED Prefixed Opcode.

Parameters:
opcode - Opcode hex value.

generateDAATable

private void generateDAATable()
Pre-calculate DAA Table.



Copyright © 2007 JavaGear. All Rights Reserved.