Forums

Sega Master System / Mark III / Game Gear
SG-1000 / SC-3000 / SF-7000 / OMV
Home - Forums - Games - Scans - Maps - Cheats - Credits
Music - Videos - Development - Hacks - Translations - Homebrew

View topic - Interpreting Z80 Instructions

Reply to topic
Author Message
Chris
  • Guest
Reply with quote
Interpreting Z80 Instructions
Post Posted: Tue Nov 02, 1999 4:50 am
Well, now that everything is comming along nicely, being that I can load the rom into memory, read
it from memory, and exchange values from memory to variables, I can finally work on the emulation
process itself. I understand the emulation process:

fetch
convert
execute

But, before I dig deeper, how exactly does the Z80 store it's program instructions? I know that the
registers are low-endian but what does that mean for storing the instructions in binary? Say for
instance my emu finds the instruction LD BC, (WORD). Now, the machine code instruction in
Z80 for LD BC is simply 1 or 0x01 along with 2 bytes. My question is, do the bytes follow after
the 01 or before it? Because I wouldn't want to continue on with 30 other instructions and their
all decoded wrong because I'm reading values before and not after and mabye vise versa.
For example:

(string of Z80 instructions in hex from ROM)

7F 07 ED 02 01 FC 32

Say my emulator catches the 01 instruction (5th byte from the left), do I send FC and 32 to register
BC or do I send ED and 02?


Chris :o)
 
Eric
  • Guest
Reply with quote
Post Posted: Tue Nov 02, 1999 6:02 am
Quote
> Well, now that everything is comming along nicely, being that I can load the rom into memory, read
> it from memory, and exchange values from memory to variables, I can finally work on the emulation
> process itself. I understand the emulation process:

> fetch
> convert
> execute

> But, before I dig deeper, how exactly does the Z80 store it's program instructions? I know that the
> registers are low-endian but what does that mean for storing the instructions in binary? Say for
> instance my emu finds the instruction LD BC, (WORD). Now, the machine code instruction in
> Z80 for LD BC is simply 1 or 0x01 along with 2 bytes. My question is, do the bytes follow after
> the 01 or before it? Because I wouldn't want to continue on with 30 other instructions and their
> all decoded wrong because I'm reading values before and not after and mabye vise versa.
> For example:

> (string of Z80 instructions in hex from ROM)

> 7F 07 ED 02 01 FC 32

> Say my emulator catches the 01 instruction (5th byte from the left), do I send FC and 32 to register
> BC or do I send ED and 02?

For the Z80 the first byte of the instruction is always an opcode byte and not a data byte. So, upon finding the 0x01, your emulator should load 0x32FC into BC. Notice that the data stored in the instruction is "Little Endian" too. The low byte of the data is stored in the instruction first, followed by the high byte of the data.

Some Z80 instructions have two-byte opcodes. These are usually referred to as the CB, DD, ED, FD "table" of instructions. Again, the first two bytes are opcodes with data following, in "Little Endian" representation.

There are also "DDCB" and "FDCB" tables of instructions. These are the only four-byte Z80 instructions. Note, though, that for these instructions, the opcode bytes are the first, second, and FOURTH byte. The third byte is a data byte.

You can find the exact binary representation for each Z80 instruction on-line. In fact, the technical information section of S8-Dev has the Z80 documents you need to correctly implement the decoder of your Z80 emulator.

Good luck.

Eric Quinn
 
  • Joined: 24 Jun 1999
  • Posts: 1732
  • Location: Paris, France
Reply with quote
Quick suggestion
Post Posted: Tue Nov 02, 1999 8:19 am
Why don't you compile a few instructions then test if they are correcly interpreted by your emulator?
  View user's profile Send private message Visit poster's website
Chris
  • Guest
Reply with quote
Post Posted: Tue Nov 02, 1999 4:14 pm
I can't even use the Z80 info zip archive at this site. It says 3 bytes are missing or something. But anyway,
I understand how it's read now.

Chris :o)
 
Reply to topic



Back to the top of this page

Back to SMS Power!