Author Topic: Z80 cpu core emulation  (Read 5693 times)

0 Members and 1 Guest are viewing this topic.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Z80 cpu core emulation
« on: May 19, 2007 »
right ive been wanting to write an emulator for the speccy for a while but not got round to it.so im going to make a start i thought a good place to start would be to make a cpu core emulator up first then do memory managment graphics audio etc.

but i need good reliable reasources and ive been googling for a few hours now and not got very far so just wondered if someone knew any good ones.

i want to write this from scratch without looking at the various opensources there are around the net.
« Last Edit: May 20, 2007 by ninogenio »
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Z80 cpu core emulation
« Reply #1 on: May 20, 2007 »
You need to pay a visit to WOS http://www.worldofspectrum.org/.

It's a tricky project building one of these!

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Z80 cpu core emulation
« Reply #2 on: May 20, 2007 »
cheers jim thats a perfect link for this here also is a few links ive picked up.

http://fms.komkon.org/EMUL8/HOWTO.html

so when the cpu core is written can it be used in other emulators ie to make a gameboy seing as it uses a z80 also without modifying the core or is there changes in the core from aplication to aplication?
Challenge Trophies Won:

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: Z80 cpu core emulation
« Reply #3 on: May 20, 2007 »
Oh man good luck.

HCF forever.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Z80 cpu core emulation
« Reply #4 on: May 20, 2007 »
cheers taj today has been mostly spent getting my memory back into z80 assembler its been so looong!

the last time i was near a spectrum was about 15 years ago lol.
its bringing back fond memories when im messing around with the basic commands too ie border and paper.
i remeber when i first made the screen flash diffrent colors my freinds looking at me as if i was something from another planet :D
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Z80 cpu core emulation
« Reply #5 on: May 20, 2007 »
I'd wanted to do that for the 6502, only ever got as far as a couple of instructions and some screenmodes. Goodluck with it, afaik once you have the hardware emulated the rom should take care of the basic instructions.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Z80 cpu core emulation
« Reply #6 on: May 20, 2007 »
yeah the rom files do take care of a lot although i still think to emulate the cpu core every instruction has to be dealt with most people yous a huge set of switch statments.

another question would be how is the video on the spectrum takin care of.

does it have seperate vram and deos it have tile modes sprite modes etc i know it has a double buffer mode and shadow ram but thats as far as i got there.

i thought the documentation would have been better but some areas are as clear as mud.
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Z80 cpu core emulation
« Reply #7 on: May 20, 2007 »
Yep, I'd guess a huge set of switch statements is the way to go and was the route i was trying. For it to work it has to take care of all the machine intructions but the basic instructions should be dealt with as the machine code instructions in the rom.
I remember something about the graphics on the speccy being more character related than bitmapped but how it was stored and displayed I have no idea, sorry.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Z80 cpu core emulation
« Reply #8 on: May 20, 2007 »
cheers fryer,

this might take a while as im going to have to just keep diging around looking for bits and bobs of data.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Z80 cpu core emulation
« Reply #9 on: May 21, 2007 »
If you write an instruction level emulation of the Z80 you'll be able to use it to emulate other computers than the Spectrum, but be aware they are all subtly different.

Basically you need to write a short piece of code for each instruction.  You will need to work out the best way to represent the Z80 register set, that eg. modifying B also modifies BC - think C unions.
Each instruction is read and it has an effect on these registers and/or memory.
The starting values for the registers can be found on WOS.

Then you will need to emulate memory.  The 48Kb Spectrum has 16Kb ROM followed by 48Kb RAM.  You should emulate this in 16Kb banks since each bank behaves differently and later spectrums are able to swap in different ROMs and RAMs into the same memory addresses.  It's vitally important for instance that even though some programs (including the Spectrum ROM) write to ROM addresses that the memory doesn't change.

Then you will need to emulate the screen.  It's a weird layout.  Starting immediately after the ROM at offset 16384, There are 256x192bits of pixels, 32 bytes per scanline.  But the scanlines aren't sequential, they go 0,8,16,24,32,40,48,56, 1,9,17,25,33,41,49,57 etc, and there are 3 banks of 64 lines.  These bits control the black and white dots on the screen.

Then there is the colour overlay immediately after the pixels at offset 22528.  It's 32x24 (768bytes).  Each byte represents a colour, 3 bits for INK, 3 bits for PAPER, 1 bit for BRIGHT, 1 bit for FLASH.  The format is described in the Spectrum manual.

In a very basic emulator you can convert the screen in one go, roughly every 48000cycles (the number of cycles in a frame - you can find the exact figure on WOS, it's different for different versions of the speccy).  For more advanced emulation you need to convert a scanline at a time, or better still, you need to emulate the ULA (the Spectrum's display chip) and have it read the memory directly.

And there's one of the biggest problems with Spectrum emulation.  The ULA shares memory cycles with the CPU, and depending which byte is being accessed at which cycle of the CPU, the CPU has to stall and wait for the ULA to finish.  This effect happens only in the bottom 16Kb of memory and to make an accurate emulation you need to simulate it.  It's called 'contention'.

Next you need to make sure you have the cycle counts for each instruction correct, and you need to make sure you have the R register accurately emulated else you'll never be able to load Speedlock games.

There's also sound, and various other bits you can emulate - you need to look at the IN/OUT instructions and handle the data they send to the ports, eg, port 254 controls the beeper/tape and the border colour, port 31 is Kempston joystick, etc.

You need to emulate the NMI interrupt handler too, every now and then you need to stop the CPU where it is, push the PC and jump to the interrupt handler.

Snapshot loading is pretty easy, and the formats are described on WOS, you have SNA and Z80.  Then there are tape files, like TAP and TZX which require you to emulate the tape recorder.

Lots of things to do, have fun!

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Z80 cpu core emulation
« Reply #10 on: May 21, 2007 »
thank you jim!

this will keep me busy for a while.

k+
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Z80 cpu core emulation
« Reply #11 on: May 26, 2007 »
You should find this site has plenty of invalueable information

http://www.6502.org/

This document in particular will tell you everything from the instructions binary encodings to the number of clock cycles that each instruction takes to complete

http://archive.6502.org/datasheets/synertek_programming_manual.pdf

Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Z80 cpu core emulation
« Reply #12 on: May 26, 2007 »
thanks rain!!

very handy. like i said this one might take a while as my asm knowledge is no where near where it needs to be so im brushing up on asm before i get started.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Z80 cpu core emulation
« Reply #13 on: May 27, 2007 »
->rain_storm - that's for a 6502, which was in the Commodore 64 and the BBC Model B and the Gameboy.

It's still useful - the techniques for emulating all these machines are the same, get the CPU emulation working, get the ROM executing, and then add on the screen and other devices by coding the machine-specific bits, including sprite engines and what-not.

Jim
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Z80 cpu core emulation
« Reply #14 on: May 27, 2007 »
Sorry I thought the spectrum used a modified version of the 6502 CPU which was so popular throughout the 8-bit years so I immediately thought of the programmers manual I saw on the net. Thought it would be useful here

Challenge Trophies Won: