 |
|
 |
| Parallax Forums > Public Forums > Propeller Chip > ZiCog a Zilog Z80 emulator in 1 Cog | Forum Quick Jump
|
|  heater Registered Member

       Date Joined Feb 2008 Total Posts : 3345 | Posted 2/27/2009 4:50 PM (GMT -7) |   | ZiCog a Z80 emulator for the Propeller.
Run CP/M 2 and 3, Microsoft BASIC etc on a Propeller.
Latest version is zicog_v0.10.zip
--------------------------------------------------------------------------------------------------------------------
The demise of the 4 COG Z80 emulator project was precipitated by the realization that it is possible to create a full up Z80 emulator that uses only one COG. Further that this emulator would be just as fast as the 4 COG effort, would be a lot more elegant and understandable than the old PropAltair 8080 emulator and would be far more friendly to external RAM.
How is this possible?
The classic way of building an emulator is to have a look up table that is used to get from a given instruction opcode to the function that will emulate that instruction. This is fine if you have a lot of space to put those instruction handler functions. In the Prop COGs there is not enough space. We could try to use more COGs. This is slow and wasteful of COGs.
Another emulator approach is to try and decode the various bit fields of the opcode and determine what the instructions data sources and destinations are and what operations should occur. In this way one can fold up the emulator into a twisted mess of code logic that will fit in a COG. This was how the first PropAltair 8080 emulator worked. It is slow.
A new approach: After looking at the emulation process long enough I came to the realization that most of the instructions boil down to three steps: 1) Get an operand from somewhere. The Source. 2) Do some operation with that operand and (mostly) the accumulator (The A register). The Operation. 3) Put the result somewhere. The Destination.
In the 8080 there are about 25 different sources and destinations: Registers A, B, C etc, memory via HL, memory via direct address, immediate data etc etc. There are about 30 different operations, ADD, SUB, ROT, INC, DEC etc etc.
All these sources, destinations and operations can be implemented in a from 2 to 20 lines of PASM each and they will all easily fit in a COG. All we need now is a way to connect different combinations of source, operation, destination together to implement each instruction.
This is where Cluso comes in. Who should, from now on, be referred to as "The Great Cluso".
Clusso suggests that one can put three COG addresses, vectors, in each LONG of a look up table, plus 5 bits of other possibly useful info. For a long time I wondered what such a clever thing could be used for. Until the euro dropped.
The first vector jumps us to a procedure to fetch the source info. The second vector gets us to some code to perform the operation and third vector takes care of posting the result to the right destination. This technique can also be used to take care of conditional/unconditional jumps, calls and returns.
Using this approach it is possible to fit a whole 8080 emulator in one COG with no LMM or other such junk and around 90 longs free!!
Turning to the Z80 we see that it has more than twice as many opcodes as the 8080 BUT looking closely we see there are only a handful more of those Source, Destination and Operation functions. Which can be coded as PASM in the free longs we have. The logic that ties them all together is in the dispatch tables in HUB.
Enough of the long lecture. My first pass at this idea is attached. I have not included the rest of the emulator files as this barely runs as it is but it does show the general outline. There is code in place for 90% of a Z80.
Early tests show that speed is nearly up to that of the 4 COG version, faster than the old PropAltair version and can be pushed a little more with some tweaks here and there.
It is early days yet but this file is intended to show what is coming for those who want to put CP/M on a Prop with external RAM or build some other emulator. For me, the past is not over yet.Post Edited (heater) : 8/19/2009 4:44:49 AM GMT
File Attachment : z80_emu.spin 124KB (application/octet-stream)This file has been downloaded 523 time(s). File Attachment : zicog_v0_10.zip 114KB (application/x-zip-compressed)This file has been downloaded 343 time(s). | | Back to Top | | |
 |  TreeLab Registered Member
        Date Joined Nov 2006 Total Posts : 136 | Posted 2/27/2009 7:34 PM (GMT -7) |   | @Heater (and The Great Cluso) : Bravo, this is very clever. I especially like the heartbeat ... Can you give an estimate of the speed relative to a 4MHz Z80?
Cheers! Paul Rowntree | | Back to Top | | |
    |  kwinn Registered Member
        Date Joined Sep 2008 Total Posts : 1616 | Posted 2/27/2009 8:28 PM (GMT -7) |   | Wow. That is one of the most impressive pieces of code an data structuring I have seen. I think both of you deserve "The great" accolade. I am truly impressed by the structure and elegance of it. As for the name, I like ZiCog. Somehow it seems to be in harmony with the "Propeller" spirit and way of doing things. | | Back to Top | | |
 |  Mike Huselton QuantumMax

       Date Joined Nov 2007 Total Posts : 716 | Posted 2/27/2009 8:28 PM (GMT -7) |   | | | |
   |  Mike Huselton QuantumMax

       Date Joined Nov 2007 Total Posts : 716 | Posted 2/27/2009 10:06 PM (GMT -7) |   | | | |
 |  Dr_Acula Registered Member

       Date Joined Dec 2008 Total Posts : 1640 | Posted 2/27/2009 10:38 PM (GMT -7) |   | What an awesome bit of code! For the rest of us - to quote Wayne's World, "we are not worthy"
I was kind of hoping since we hadn't heard from you for a bit that you were deep in the world of coding. The new code is very elegant. Re the Z80 specific instructions, if you can do just LDIR I reckon it could do the N8VEM. How would the 64k memory interface (and could it use the cluso triblade?) | | Back to Top | | |
         |  Ale Registered Member

       Date Joined May 2007 Total Posts : 1534 | Posted 2/28/2009 9:23 AM (GMT -7) |   | heater: I saw that you picked a name already, great start.
In the implementation of daa I think you could compare data_8 directly against 100 ($A0) without moving it to nibble and shifting it right 4 bits (line 517 of your v 0.0). The mux instructions sure were handy! | | Back to Top | | |
      | 1085 posts in this thread. Viewing Page : | | Forum Information | Currently it is Thursday, July 29, 2010 5:16 PM (GMT -7) There are a total of 462,439 posts in 62,066 threads. In the last 3 days there were 90 new threads and 802 reply posts. View Active Threads
| | Who's Online | This forum has 20143 registered members. Please welcome our newest member, ME01. 57 Guest(s), 9 Registered Member(s) are currently online. Details John Abshier, Rayman, kf4ixm, BradC, Sapieha, Gene Bonin, laser-vector, localroger, Nick McClick |
Forum powered by dotNetBB v2.42EC SP2.02 dotNetBB © 2000-2010 |
|
|