Jump to content
mariuszw

Z80 -> 6502 recompiler used with Pentagram port

Recommended Posts

Hi,

 

Some people were asking for recompiler I created for preparing Pentagram, so I decided to release it.

 

z80compiler.zip is a compiler. It takes Pentagram disassembly as input and creates 6502 listing ready to be compiled by MADS. Recompiler is very simple, it just takes every single Z80 instruction and just creates equivalent 6502 code from it. Recompiler performs basic code analysis and issues warning, where it thinks code needs to be reviewed and eventually changed. Following checks are done:

- usage of A register. Since 6502 uses A to emulate A of Z80 but also uses A to emulate many other instructions, a situation where Z80 instruction reads A register but previous Z80 instruction doesn't write the instruction is detected, as previous 6502 instruction may pollute A

- usage of ZERO flag: on Z80, ZERO flag is affected by very few instructions, while no 6502 almost all instructions affect ZERO flag. Warning is issued while Z80 instructions read ZERO flag but previous Z80 instruction does not write it

- usage of CARRY flag: check similar to ZERO flag is done. Also, since for CARRY for subtraction and comparison are reverted between Z80 and 6502, but the same for addition, recompiler inverts CARRY checks (because CMP/SBC are more common than ADD) and issues a warning when not CMP/SBC affects CARRY

 

Recompiler does not detect self-modifying code (2 instances in Pentagram). Recompiler also issues a warning for SP (Stack Pointer) manipulation, but cannot compile code which makes tricks with the stack directly (in Pentagram, sprite drawing codes reads directly using stack pointer).

 

Recompiler is not mean to be generic, so not all instructions are compiled. Basically it is prepared to work with Pentagram source only, while it should be expanded quite easily to support other games. It needs Z80 disassembly with code, data and pointers clearly recognized and identified in order to work.

 

The other tool is z80run.zip. It takes binary image of Z80 code and 6502 code, and executes them, logging all accesses (reads and writes) to memory, apart from:

- opcode fetching

- reading/writing data on zero page on 6502 (ZP registers are used to hold Z80 registers)

- stack reading/writing on both Z80 and 6502

Then, all accesses are compared to find errors. This turned out to be very effective tool to debug 6502 code.

 

z80run uses 6502 and Z80 emulators Copyright © Marat Fayzullin.

 

Both projects are written in C++ and compiled in Microsoft Visual Studio 2012.

 

Mariusz.

z80run.zip

z80compiler.zip

  • Like 19

Share this post


Link to post
Share on other sites

Yep, interesting. On my wish list is to get Galaga going for VBXE.

Candidates would be the arcade original which would need this type of work, or the NES one which is already 6502 but would need rework for the different hardware in use.

  • Like 3

Share this post


Link to post
Share on other sites

Yep, interesting. On my wish list is to get Galaga going for VBXE.

Candidates would be the arcade original which would need this type of work, or the NES one which is already 6502 but would need rework for the different hardware in use.

Shit - you would be my hero if that happened. Z80 code and original graphics courtesy of VBXE.

  • Like 1

Share this post


Link to post
Share on other sites

The thing is I don't think there's anything for Galaga arcade other than the Rom dumps and some driver documentation for Mame.

It'd be going in blind to just make an attempt based on that.

Share this post


Link to post
Share on other sites

(offtopic)

 

I have not checked the site in the last years, could be more updates.. (he was trying to implement the Galaga code in C).

http://pushpopmov.blogspot.cl/2012/08/galaga-z80-disassembly-uploaded.html

 

General info:

http://galaga.info/

 

Some of the interesting links seems to be dead, like this one.. maybe in the wayback machine.

http://www.computerarcheology.com/wiki/wiki/Arcade/Galaga

 

oh.. but this works, nice (code with comments.. also check the info about the sound of Time Pilot :) )

http://www.computerarcheology.com/Arcade/Galaga/

 

more code info:

http://donhodges.com/galaga_stage_256_fix.htm

 

I did have a backup of some source..

 

z80.zip

  • Like 1

Share this post


Link to post
Share on other sites

Cheers, some nice info in there about Galaga. I can remember reading some time back, probably elsewhere about the corrupted shot data structure which allows the wait/no enemy shooting trick to work (and actually played a game in it's fullness up to the Stage 0/256 bug/crash).

  • Like 1

Share this post


Link to post
Share on other sites

A ZX Spectrum adventure game (The Hobbit) would be easier to port for the recompiler?

Share this post


Link to post
Share on other sites

A ZX Spectrum adventure game (The Hobbit) would be easier to port for the recompiler?

 

Hobbit exists for C64 and Plus4, so no need for recompiling Z80 version. I see a problem with preparing hires multicolor graphics, however.

 

Mariusz.

  • Like 1

Share this post


Link to post
Share on other sites

For what it's worth, VBXE can be helpful there. I used blits to totally remap the graphics in Quadrillion. I initially rewrote the game to use Atari's linear mapping but some nasty bugs crept in, so I just resorted to leaving it as is and doing about 8K worth of memory moves per frame.

Not that it's helpful for standard hardware.

Though in narrow DMA you can emulate a C64 type bitmapping using multiple character sets.

  • Like 1

Share this post


Link to post
Share on other sites

how ldir looks like from there? i’m really curious! :)

 

Nothing fancy ;)

 

 

ldir

ldy #$00

ldx z80_b

beq ldir_last_page

ldir_loop

lda (z80_hl),y

sta (z80_de),y

iny

bne ldir_loop

inc z80_h

inc z80_d

dex

bne ldir_loop

ldir_last_page

lda z80_c

beq ldir_end

ldir_last_page_loop

lda (z80_hl),y

sta (z80_de),y

iny

cpy z80_c

bne ldir_last_page_loop

ldir_end

stx z80_c

stx z80_b

tya

clc

adc z80_l

sta z80_l

bcc *+4

inc z80_h

tya

clc

adc z80_e

sta z80_e

bcc *+4

inc z80_d

rts

 

  • Like 1

Share this post


Link to post
Share on other sites

Yes, it was ok in VS 2017 Community edition to build the z80compiler. If anyone interested, I was looking at gb-disasm and able to take something such as the GameBoy R-Type rom and produce z80 code from that which can be compiled with some additions to the z80compiler to support extra instructions in the GB's processor.

Share this post


Link to post
Share on other sites

i’m curious about trying it! (perhaps this tool will help me a lot doing for 6502 what i have made for z80, using Boriel’s ZX-Basic Compiler!)

if anyone tried to compile it on GCC (GNU/Linux), please share makefiles! icon_smile.gif

Edited by nitrofurano

Share this post


Link to post
Share on other sites

Yes, it was ok in VS 2017 Community edition to build the z80compiler. If anyone interested, I was looking at gb-disasm and able to take something such as the GameBoy R-Type rom and produce z80 code from that which can be compiled with some additions to the z80compiler to support extra instructions in the GB's processor.

see that GameBoy CPU is more based on 8080, not Z80! ;) (the removed opcodes makes it retroincompatible with Z80, but not 8080)

Share this post


Link to post
Share on other sites

i tried this now:

[email protected]:~/Downloads/z80to6502/z80compiler$ gcc -o z80compiler z80compiler.cpp 
In file included from stdafx.h:8:0,
                 from z80compiler.cpp:4:
targetver.h:8:10: fatal error: SDKDDKVer.h: No such file or directory
 #include <SDKDDKVer.h>
          ^~~~~~~~~~~~~
compilation terminated.
[email protected]:~/Downloads/z80to6502/z80compiler$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 17.10
Release:	17.10
Codename:	artful
[email protected]:~/Downloads/z80to6502/z80compiler$ 

i don’t know what might be missing there

Share this post


Link to post
Share on other sites

SDKDDKVer is from the Windows SDK libraries which you're not going to have from gcc.

stdafx.h also indicates this is from a Visual Studio generated project and hence that's what should be used to build it.

 

It shouldn't take much to port this code though, so take out the inclusion of stdafx.h and seem what breaks and patch from there.

Share this post


Link to post
Share on other sites

[email protected]:~/Downloads/z80to6502/z80compiler/z80compiler_gccattempt02$ gcc -o z80compiler z80compiler.cpp
In file included from z80compiler.cpp:5:0:
z80compiler.h:3:10: fatal error: resource.h: No such file or directory
#include "resource.h"
^~~~~~~~~~~~
compilation terminated.
[email protected]:~/Downloads/z80to6502/z80compiler/z80compiler_gccattempt02$

Share this post


Link to post
Share on other sites

I doubt it is possible to compile this code with GCC as it isn't standard C++ and uses the MFC library for no good reason. Also it exhibits undefined behavior so it's not worthy of being compiled anyway, it will likely crash.

 

But it should be possible with some effort to translate it into standard C++. Replace CString with std::string (or maybe std::wstring), CStringArray with std::vector, qsort with std::sort, and so on...

Edited by antrykot

Share this post


Link to post
Share on other sites

i guess that, from what i have seen around (projects at github and alike), with some code “cleanup”, it might be possible to have exactly (or almost exactly) the same code compilable, as from gcc, as from vs - but i’m saying this without having enough experience on this, my gcc experience isn’t better than compiling “hello world”, and on vs (which i never had installed at home, and perhaps never will, for lots of reasons ) is totally null...

 

btw, perhaps is there some kind of code compatibility layer as library, that would help to smooth such situation?

Edited by nitrofurano

Share this post


Link to post
Share on other sites

But it should be possible with some effort to translate it into standard C++. Replace CString with std::string (or maybe std::wstring), CStringArray with std::vector, qsort with std::sort, and so on...

i’m afraid that, for me, humbly saying, it seems far less difficult to convert the whole code to Python or Lua, than doing this! :D (anyway, thanks for the motivation!!!! :) )

Share this post


Link to post
Share on other sites

Hi @nitrofurano, I'm glad you are interested in my work.

 

My tool can be compiled with Microsoft Visual Studio only, as it uses MFC. It could be ported to plain C++ as antrykot suggested, but it certainly requires some work, and knowledge of both MFC and stc C++.

 

Please keep in mind however, that with my tool you first need full Z80 program listing. Then, for translated program, additional work must be done to make the program work correctly (as compiler is not meant to produce working output). Then, the code is very suboptimal, so it requires lots of manual work and good 6502 experience to get good performance out of compiled program.

 

I'd suggest using one of high level languages to port your work from Spectrum to Atari. You will find more details here: http://atariage.com/forums/topic/275385-what-modern-high-level-languages-are-available-for-the-8-bit-atari/. You may consider FastBasic, or Mad Pascal.

 

Mariusz.

  • Like 1

Share this post


Link to post
Share on other sites

I've managed to strip out the MFC dependencies from the c++ source such that only the z80compiler.cpp is required, note that this is some extension WIP to tackle the output of gb-disasm.
I can build (g++ -std=c++11 z80compiler.cpp -o z80compiler) and run this against some z80 source both under cygwin64 on Win10 and an ubuntu 16.04 LTS VirtualBox VM.

z80compiler_nonMFC.zip

Edited by Wrathchild
  • Like 4

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...