don't click here

Zilog Z80 instruction set compared to 68K instructions

Discussion in 'Engineering & Reverse Engineering' started by Ravenfreak, Nov 16, 2010.

Thread Status:
Not open for further replies.
  1. Ravenfreak

    Ravenfreak

    2 Edgy 4 U Tech Member
    3,103
    197
    43
    O'Fallon Mo
    Sonic 1 Game Gear Disassembly
    Some things need to be edited, and added, in the future I will edit this post. Some instructions are similar, others obviously aren't. xP A small description of the Z80 (for you peeps that don't know what the heck this is all about. xP) This guide has been written with the SEGA Genesis/Mega Drive, GG and MS in mind. (Yes the classic Sega consoles.) What the heck is the Z80 used for you ask, well it's used as the main processor for the GG (Game Gear) and MS (Master System), but is used as a co-processor for the Genesis/MD. Before going on, keep this handy:
    Code (Text):
    1. Binary    Decimal Hexadecimal
    2.           0000    0           0
    3.          0001    1           1
    4.          0010    2           2
    5.          0011    3           3
    6.          0100    4           4
    7.          0101    5           5
    8.          0110    6           6
    9.          0111    7           7
    10.          1000    8           8
    11.          1001    9           9
    12.          1010    10           A
    13.          1011    11           B
    14.          1100    12           C
    15.          1101    13           D
    16.          1110    14           E
    17.          1111    15           F
    This list will save a lot of time. xP Before going on, I'll explain the registers. In Z80 asm, the registers are as follows:
    A,B,C,D,E,F,H,L and A',B',C',D',E',F',H',L'.
    A and F are special ones, A is the accumulator, and F is used for flags. B,C,D,E,H,and L are used to store temporary data. A' B' C' D' E' F' H' and L' are known as shadow registers that can only be swapped with other registers. Now onto the instruction list.
    ADC ADD WITH CARRY- if you have 3 or more binary offsets to add, this would be the
    instruction you'd use since the third offset would be the carry. These usually start with 0,
    unless the result of addition is too large to fit into one column then it'd start with 1.
    For the most part, I think this is only exclusive to Z80 asm. Example:
    00000110
    +00000011
    --------
    00001001 Result
    0 00001100 Carry
    As you can see, the carry above starts with zero, meaning it fits. Note: addition and subtraction are used most in Z80 asm.
    ADD ADD- simple add either offsets or registers together. This is the same in 68k asm.
    Example: Lets say register L's value is D9h while H's is 1Fh and the instruction is add l,h the value would be F8h very simple.(Note: in Z80 asm offsets/addresses end with the letter h. In 68k $ is used and is at the beginning of an offset/address. But I'm sure most of you guys know this already. xP)
    AND LOGICAL AND
    BIT BIT TEST- tests a bit, simple. the 68k asm equilvalent is BTST.
    CALL CALL SUBROUTINE- this calls a subroutine (obviously) in 68kasm, the equilvalent is
    BSR (Branch to SubRoutine)
    CCF COMPLEMENT CARRY FLAG- this inverts the value of the carry flag. As follows, only
    a few flags are affected by this: H: the previous carry will be copied here
    N: resets C: is set only if the carry value was zero, otherwise this gets reseted
    Note: the flags in Z80 asm are as follows:
    C: this is the carry flag. This is only set if the sum is greater than 255 or if the
    subtraction of two or more values is less than zero.
    S: the sign flag. This is set if an operation on a register leaves a negative result (I.e. bit 7 is set).
    P/V: parity/overflow flag. This is only set if an overflow occurs (that is, if an operation
    results in a value larger that can be represented. Ex: value -128 will cause an overflow.)
    A parity bit is a bit that is added to ensure that the number of bits with the value one in a set of bits is even or odd.
    With that there's either PO for Parity Odd or PE for Parity Even. As you can tell, these are used for error control in Z80 asm.
    Z: Zero flag set only if something obviously equals zero. Simple.
    N: Subtraction flag, this is only set if the last operation was a subtraction.
    CP COMPARE- compare the value of offsets, and registers. The 68k equilvalent is CMP.
    CPD COMPARE AND DECREMENT- first compare data, then decrease the value of said data.
    CPDR COMPARE DECREMENT AND REPEAT- same as CPD, only this is repeated until some condition is met.
    CPI COMPARE AND INCREMENT- first compare data, then increase the value
    CPIR COMPARE INCREMENT AND REPEAT- repeat until condition is met
    CPL COMPLEMENT ACCUMULATOR
    DAA DECIMAL ADJUST ACCUMULATOR- This instruction conditionally adjusts the accumulator for BCD addition
    and subtraction operations.
    DEC DECREMENT- decrease the value of an offset, or register. Simple.
    DI DISABLE INTERRUPTS
    DJNZ DEC JUMP NON-ZERO- This instruction is similar to the conditional jump instructions except that a register value is used to determine branching. The B register is decremented, and if a non zero value remains, the value of the displacement e is added to the Program Counter (PC). The next instruction is fetched from the location designated by the new contents of the PC. The jump is measured from the address of the instruction Op Code and has a range of -126 to +129 bytes. The assembler automatically adjusts for the twice incremented PC. If the result of decrementing leaves B with a zero value, the next instruction executed is taken from the location following this instruction.
    if B doesn't equal 0:
    M Cycles T States 4 MHz E.T.
    3 13 (5,3, 5) 3.25
    If B = 0:
    M Cycles T States 4 MHz E.T.
    2 8 (5, 3) 2.00
    EI ENABLE INTERRUPTS
    EX EXCHANGE REGISTER PAIR
    EXX EXCHANGE ALTERNATE REGISTERS- this is only used to swap data registers with shadow or alternate registers. For example: EXX BC will swap BC with B'C'.
    HALT HALT, WAIT FOR INTERRUPT OR RESET- this is pretty much self explanatory, it halts the cpu until either an interrupt is called or if the data has been reseted. 68K equivalent is STOP.
    IM INTERRUPT MODE 0 1 2
    IN INPUT FROM PORT
    INC INCREMENT
    IND INPUT, DEC HL, DEC B
    INDR INPUT, DEC HL, DEC B, REPEAT IF B>0
    INI INPUT, INC HL, DEC B
    INIR INPUT, INC HL, DEC B, REPEAT IF B>0
    JP JUMP- jump to offset or address.The 68k equilvalent is JMP.
    The conditions that can be used are:
    Z, jump if the zero flag is set.
    NZ, jump if the zero flag is reset.
    C, jump if the carry flag is set.
    NC, jump if the carry flag is reset.
    PO, jump if parity odd or there's no overflow.
    PE, jump if parity even or there's an overflow.
    P, jump if positive.
    M, jump if negative.
    JR JUMP RELATIVE
    LD LOAD DATA TO/FROM REGISTERS/MEMORY
    LDD LOAD DECREMENT
    LDDR LOAD DECREMENT AND REPEAT
    LDI LOAD AND INCREMENT
    LDIR LOAD INCREMENT AND REPEAT
    NEG NEGATE ACCUMULATOR 2'S COMPLEMENT- same as 68k.
    NOP NO OPERATION- same as 68k used as a placeholder in some cases
    OR A logical OR operation is performed between the byte specified by the s operand and the byte contained in the Accumulator; the result is stored in the Accumulator. The instruction is the same as 68k.
    OTDR OUTPUT, DEC HL, DEC B, REPEAT IF B>0
    OTIR OUTPUT, INC HL, DEC B, REPEAT IF B>0
    OUT OUTPUT TO PORT
    OUTD OUTPUT, DEC HL, DEC B
    OUTI OUTPUT, INC HL, DEC B
    POP POP FROM STACK- same as 68k, pop or take data from the stack pointer.
    PUSH PUSH INTO STACK- same as 68k, push or store data on the stack pointer.
    RES RESET BIT
    RET RETURN FROM SUBROUTINE- 68k equilvalent is rts, used to restart a subroutine.
    RETI RETURN FROM INTERRUPT
    RETN RETURN FROM NON MASKABEL INTERRUPT
    RL ROTATE LEFT register
    RLA ROTATE LEFT ACUMULATOR
    RLC ROTATE LEFT THROUGH CARRY register
    RLCA ROTATE LEFT THROUGH CARRY ACCUMULATUR
    RLD ROTATE LEFT DIGIT
    RR ROTATE RIGHT register
    RRA ROTATE RIGHT ACCUMULATOR
    RRC ROTATE RIGHT CIRCULAR register
    RRCA ROTATE RIGHT CIRCULAR ACCUMULATOR
    RRD ROTATE RIGHT DIGIT
    RST RESTART
    SBC SUBTRACT WITH CARRY
    SCF SET CARRY FLAG- simple, sets the carry flag. (See above.)
    SET SET BIT- sets bit, usually used to set a flag. 68k equilvalent is BSET
    SLA SHIFT LEFT ARITHMETIC register
    SRA SHIFT RIGHT ARITHMETIC register
    SRL SHIFT RIGHT LOGICAL register
    SUB SUBTRACTION- Simple subtract either offsets, or registers. Same as 68k.
    XOR EXCLUSIVE OR- A logical exclusive-OR opperation is performed between the byte specified by the s operand and the byte contained in the Accumulator, the result is stored in the Accumulator.
     
Thread Status:
Not open for further replies.