Z80 emulation

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
User avatar
JMD
Insane DCEmu
Insane DCEmu
Posts: 234
Joined: Wed Dec 11, 2002 6:45 am
Location: A Melody From a Past Life Keeps Pulling Me Back
Has liked: 0
Been liked: 0
Contact:

Post by JMD » Mon Jul 05, 2004 5:02 am

Just a question about second CPU emulation.

The Z80 CPU share memory with the rest of emulation (system + 68K + VDP + sound) but did it share more?
In others words, how the second CPU communicate with the others parts of the console and what should be changed in an existing z80 CPU core or in the emulator itself to change the Z80 core of Genesis Plus ?

Thanks ?.

Nb : Read your discussion about Genesis plus is really fun and instructive :)
Image
There is the theory of Möbius. A twist in the fabric of space where time becomes a loop
User avatar
Stef.D
DCEmu Respected
DCEmu Respected
Posts: 114
Joined: Wed Oct 15, 2003 1:46 am
Has liked: 0
Been liked: 0
Contact:

Post by Stef.D » Mon Jul 05, 2004 5:11 am

Z80 access 68000 memory by bank of 32 KB...
Generally Z80 access the ROM area, i know some game try to do 68000 RAM access, but that does weird stuff on the real hardware :-/
Z80 also has it own access to VDP, PSG and YM2612 chip...
Actually a game can almost work on Z80 side alone... it would be pity since there is only 8 KB of memory, but it's possible ;)

edit : Z80 can't access IO port, so we need 68000 at least for that ;)
Warmtoe
DC Developer
DC Developer
Posts: 453
Joined: Thu May 16, 2002 8:29 am
Location: ice88's house
Has liked: 0
Been liked: 0
Contact:

Post by Warmtoe » Mon Jul 05, 2004 5:27 am

I was considering the Z80 core problem - It seems to me that a jump table which points to functions is very wasteful - none of the z80 functions returns a value - so surely, GOTOs are the order of the day for speed...

Stef.D - your x86 z80 core - couldn't it be SH4'd?
User avatar
Stef.D
DCEmu Respected
DCEmu Respected
Posts: 114
Joined: Wed Oct 15, 2003 1:46 am
Has liked: 0
Been liked: 0
Contact:

Post by Stef.D » Mon Jul 05, 2004 5:38 am

Warmtoe wrote:I was considering the Z80 core problem - It seems to me that a jump table which points to functions is very wasteful - none of the z80 functions returns a value - so surely, GOTOs are the order of the day for speed...

Stef.D - your x86 z80 core - couldn't it be SH4'd?
Almost Z80 C core already use a jump table, in fact not directly a jump table but a switch statement, which is optimised in a jump table by compiler... but from what i saw in different sources, we can do a bit better than actuals C cores on read, write and fetch stuff... i need to do some tests to be sure it worth a new core.

An ASM core is written for a target architecture, you can't convert it to another CPU easily, you really need to do a complete rewrite, and i don't want to do an ASM core for SH-4 right now... if C can be fast enough, i want to stay on C :)
User avatar
Coagulus
DCEmu Freak
DCEmu Freak
Posts: 62
Joined: Sat Jan 03, 2004 11:06 am
Location: UK
Has liked: 0
Been liked: 0
Contact:

Post by Coagulus » Mon Jul 05, 2004 8:56 am

This is probably a really silly thing to think - but thinking off-the-wall, there were a number of very fast Spectrum emulators on the Amiga/ST which had their z80 core written in 68000 assembly.

Is there any way of using the extra speed of the emulated 68000 to run the 68K ASM z80 emulation?

Would that work at all? If there are sources for them of course!

Actually, the more I type it the sillier it sounds - but it was just a thought!
COAGULUS
:;;::;;;;:::;
Sanchez
DCEmu Ex-Admin
DCEmu Ex-Admin
Posts: 1098
Joined: Wed Oct 17, 2001 7:44 pm
Has liked: 0
Been liked: 0

Post by Sanchez » Mon Jul 05, 2004 9:22 am

Has anyone tried working on Sayten's Z80 SH4 core? It's unfinished from what I remember but might be a good start...
"This is worse than when the Raccoon got in the copier!"
User avatar
Stef.D
DCEmu Respected
DCEmu Respected
Posts: 114
Joined: Wed Oct 15, 2003 1:46 am
Has liked: 0
Been liked: 0
Contact:

Post by Stef.D » Mon Jul 05, 2004 9:45 am

Coagulus wrote:This is probably a really silly thing to think - but thinking off-the-wall, there were a number of very fast Spectrum emulators on the Amiga/ST which had their z80 core written in 68000 assembly.

Is there any way of using the extra speed of the emulated 68000 to run the 68K ASM z80 emulation?

Would that work at all? If there are sources for them of course!

Actually, the more I type it the sillier it sounds - but it was just a thought!
C68K isn't that fast, it's still a simple interpreter, and emulate a Z80 using C68K would be a total waste really ;)
doragasu
DCEmu Cool Poster
DCEmu Cool Poster
Posts: 1048
Joined: Thu May 16, 2002 5:01 pm
Location: Madrid, Spain
Has liked: 0
Been liked: 0

Post by doragasu » Mon Jul 05, 2004 11:07 am

What the hell are "computed labels"?
User avatar
Christuserloeser
Moderator
Moderator
Posts: 5941
Joined: Thu Aug 28, 2003 12:16 am
Location: DCEvolution.net
Has liked: 6 times
Been liked: 0
Contact:

Post by Christuserloeser » Mon Jul 05, 2004 11:51 am

Sanchez wrote:Has anyone tried working on Sayten's Z80 SH4 core? It's unfinished from what I remember but might be a good start...
:o never heard of Sayten. Who is it? Seems that back in 2001 he has been interviewed once over at Boob UK but it isn't available anymore :?


What's with the Z80 emulator by Juergen Buchmueller (SMEG, SMSplusDC)? Ii it slower than the other C based ones that were mentioned in this thread?

I've found this information some time ago and I am sure that it's not very helpful at all, but there are some (strange) notes about SH2 optimizations (the problem with the 'endianess' again):

http://smsemu.emuunlim.com/misc.htm
excyber.txt - ExCyber thoughts on optimizing Z80 emu core for Saturn's SH2s

Chris
Last edited by Christuserloeser on Mon Jul 05, 2004 8:27 pm, edited 1 time in total.
Insane homebrew collector.
User avatar
Stef.D
DCEmu Respected
DCEmu Respected
Posts: 114
Joined: Wed Oct 15, 2003 1:46 am
Has liked: 0
Been liked: 0
Contact:

Post by Stef.D » Mon Jul 05, 2004 6:33 pm

doragasu wrote:What the hell are "computed labels"?

Code: Select all

goto Jumptable[opcode];
That's "computed label", at least it's the name used in some compiler, we can also find it named "dynamic label" ...

normally C can only do :

Code: Select all

void my_func(void)
{
   int i;

   i = 100;

label_1:
   if (--i) then goto label_1;
}

but GCC does support :

Code: Select all

goto var;
where var is a **void.
so you can do that :

Code: Select all

void my_func(void)
{
   void **var;
   int i;

   var = &&label_1;
   i = 100;

label_1:
   if (--i) then goto var;
}
doragasu
DCEmu Cool Poster
DCEmu Cool Poster
Posts: 1048
Joined: Thu May 16, 2002 5:01 pm
Location: Madrid, Spain
Has liked: 0
Been liked: 0

Post by doragasu » Tue Jul 06, 2004 2:30 am

Thanks Stef. I never used "gotos" for coding (well, excepting when coding in Basic with my old Amstrad CPC6128). That's interesting...
User avatar
Stef.D
DCEmu Respected
DCEmu Respected
Posts: 114
Joined: Wed Oct 15, 2003 1:46 am
Has liked: 0
Been liked: 0
Contact:

Post by Stef.D » Tue Jul 06, 2004 3:32 am

Using "goto" in C looks really weird as it appears useless with commons for / while / switch ... statements. Futhermore, that break code stream and by the same way, code optimisation...

But in some rare cases (and in our case) it can be really usefull and bring some nice speed increasement over traditionals ways :)
Warmtoe
DC Developer
DC Developer
Posts: 453
Joined: Thu May 16, 2002 8:29 am
Location: ice88's house
Has liked: 0
Been liked: 0
Contact:

Post by Warmtoe » Tue Jul 06, 2004 5:25 am

Stef.D wrote:Using "goto" in C looks really weird as it appears useless with commons for / while / switch ... statements. Futhermore, that break code stream and by the same way, code optimisation...

But in some rare cases (and in our case) it can be really usefull and bring some nice speed increasement over traditionals ways :)
Stef - I was thinking about taking a pop at changing the z80 emulation to do this - are you already working on it? I might have a go anyway - but am not sure how well I'll do at it - it's all new to me - so it doesn't need to stop you doing it!!!!
User avatar
Stef.D
DCEmu Respected
DCEmu Respected
Posts: 114
Joined: Wed Oct 15, 2003 1:46 am
Has liked: 0
Been liked: 0
Contact:

Post by Stef.D » Tue Jul 06, 2004 7:32 am

Warmtoe wrote: Stef - I was thinking about taking a pop at changing the z80 emulation to do this - are you already working on it? I might have a go anyway - but am not sure how well I'll do at it - it's all new to me - so it doesn't need to stop you doing it!!!!
Actually Z80 emulation doesn't really need it since anyway, 256 ways switch statement are optimised in a jump table by C compiler, i mean that won't change anyhing about speed, but there are others way to optimize it. When i'll have some free time, i will do some tests with the fastest Z80 C core available...
Rev. Layle
Insane DCEmu
Insane DCEmu
Posts: 190
Joined: Sun Jun 27, 2004 8:35 pm
Location: stillwater, ok
Has liked: 0
Been liked: 0
Contact:

Post by Rev. Layle » Tue Jul 06, 2004 8:56 am

also people don't see a lot of "goto"s, as it is considered sloppy programming when creating structured and maintainable programming. however, when speed is *required*, sometimes gotos are a must. if gotos are kept to a specific scope, they are not bad at all.
doragasu
DCEmu Cool Poster
DCEmu Cool Poster
Posts: 1048
Joined: Thu May 16, 2002 5:01 pm
Location: Madrid, Spain
Has liked: 0
Been liked: 0

Post by doragasu » Tue Jul 06, 2004 10:34 am

I have never used them in C, I read somewhere some time ago they are used sometimes to handle errors and such stuff that most time is not excuted.
DcSteve
Modder Of Rage
Modder Of Rage
Posts: 805
Joined: Mon Mar 18, 2002 12:41 pm
Location: Midwest
Has liked: 0
Been liked: 0
Contact:

Post by DcSteve » Tue Jul 06, 2004 10:40 am

at genesis plus's current state- how does the sound work.. Is it more stable and less loopy than preview 3? IM surprised that the fullspeed genesis emulation with sound is not only possible- but already proven. Now, just the sound needs to be fixed up a bit.
Check out the beats of rage community at http://borrevolution.vg-network.com/
Heliophobe
Smeg Creator
Smeg Creator
Posts: 246
Joined: Thu Mar 14, 2002 2:40 pm
Has liked: 0
Been liked: 0
Contact:

Post by Heliophobe » Tue Jul 06, 2004 2:38 pm

Christuserloeser wrote:
Sanchez wrote:Has anyone tried working on Sayten's Z80 SH4 core? It's unfinished from what I remember but might be a good start...
:o never heard of Sayten. Who is it? Seems that back in 2001 he has been interviewed once over at Boob UK but it isn't available anymore :?

[/quote]

Sayten was an early developer back in the days when the holy grail of DC Emulation -- full screen, full speed with sound -- hadn't yet been reached. He was working on an SMS emulator while I was working on Smeg. We had some conversations here about how to do a fast Z80 emulator in SH4 assembly. My suggestions were about keeping the object code and data size down to avoid overloading the cache at the expensive of executing more opcodes per emulated instructions, which ran contrary to the conventional wisdom which says that you're better off writing (or generating via macro) a dedicated,short instruction handler for every single opcode.

For instance, there is a block of ld r,r instructions that transfer the data from one register to the next (there are 7 8bit registers). The way it's coded in the opcode is the top two bits indicate that it's a ld r,r type instructions, the next three bits indicate which is the source register, and the last three indicate the destination register (or the other way around, I don't recall). Which the conventional fast CPU core might want to write a seperate routine for each permutation (i.e. for ld b,c you'd have a routine that is hardcoded to load the c register and put it into b) - but in the code-conserving approach you could have a routine that examines the opcode and determines which register to load and which to save into. You don't need to check the top two bits to ensure it's a load instruction because a jump table is still used to branch to the appropriate opcode handler - and some instructions in that block with different types of instructions (the (hl) instructions and the HALT command are in there too).

You can also have the opcode handler add in the elapsed cycles instead of using a table to look it up, which saves some table accesses.

So Sayten took the notes from those conversations and started on a z80 emulator. I did too at one time but only got as far as emulation the simple load instructions --- I lost interested when I got to the arithmetic instructions with all the flag handling. Bleargh.

Anyhow, I don't know if it's advantegous or not when you're working on a z80 core for a Genesis emulator, since I imagine the code and data caches are going to get cycled out a lot anyhow.


I don't know how J?rgen's emulator fares speedwise, it was fast enough for me (so long as the Big Flag Arrays are disabled) so I used it. I think there was some concern about the MAME license being too unflexible but J?rgen also offers the core with a seperate less restrictive license so it can be used in GPL or closed source products.
Sanchez
DCEmu Ex-Admin
DCEmu Ex-Admin
Posts: 1098
Joined: Wed Oct 17, 2001 7:44 pm
Has liked: 0
Been liked: 0

Post by Sanchez » Tue Jul 06, 2004 3:34 pm

He's still very much around on the #consolevision IRC though I don't think he does much coding for DC anymore...
"This is worse than when the Raccoon got in the copier!"
DcSteve
Modder Of Rage
Modder Of Rage
Posts: 805
Joined: Mon Mar 18, 2002 12:41 pm
Location: Midwest
Has liked: 0
Been liked: 0
Contact:

Post by DcSteve » Tue Jul 06, 2004 7:41 pm

Sayten himself gave me the link to his unfinished z80 sh4 core. It has a basic exec loop, and has ~ 1/2-2/3 of the opcodes implemented. He didn't implement opcodes as he needed them. He took the opcode list for the z80 and started at the top and worked his way down. While he's tested what he's written and it seems to work ok, he hasnt actually used the core in anything real. Its not pretty or fast. Just a basic interpreted cpu emu. He figured max speed on a dc was about 20MHz. Here is his work.

http://www.wolfnet.org/~jkf/dc/src/dc_z80.tar.gz
Check out the beats of rage community at http://borrevolution.vg-network.com/
Post Reply