Jump to content
IGNORED

Z80 -> 6502 recompiler used with Pentagram port


Recommended Posts

thanks to you all!

i now tried the version @Wrathchild provided

for now i tried to test with simple stuff like this:

org $8000
start:
ld bc,$0000
ld de,$1000
ld hl,$2000
ldir
halt
nop
loop01:
jr loop01

and when i entered “./z80compiler 01.asm”, i got

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is  > this->size() (which is 6)
Aborted

which which kind of code and and command line arguments should i try?

 

also considering the manual work needed, and considering that this so promisingly amazing tool, i might try to use it combined with... Boriel’s ZX-Basic Compiler!!! ;D \o/ - sounds crazy, but seeing that i could do this without further struggle - http://www.boriel.com/wiki/en/index.php/ZX_BASIC:Released_Programs#OTHER_HARDWARE- i think it would be not impossible one day soon, at all! :)

  • Like 1
Link to comment
Share on other sites

Seems to be a by-product of the code expecting labels to be of the 'generated' type and so taking up 8 characters (e.g. "jmp_150:")

 

I've made adjustments to try and cater for this but would appreciate fresh eyes to understand how labels are handled.

At the moment I'm not sure labels and instructions on the same line would work, but this could be possible to fix with just left trimming the instruction after it is cut from the label.

 

Also, its a good idea to format the input z80 source such that instructions follow a single tab character at the start of the line.

z80compiler_20180702_2327.zip

Link to comment
Share on other sites

  • 2 weeks later...

@mariusz (and @Wrathchild as well ), thanks again a lot for all code, information and motivation!

 

btw, when trying to compile the conversion results, we get errors like “:Label 'z80_c' not defined” - is after $80 the best place for defining z80 registers? and how? (i’m using xa65 assembler for now)

 

the ldir routine you posted here in this thread, but lddr, exx (i guess it’s not that difficult), ex, add (16bit), sbc (16bit), dec (16bit), di, ei, are missing - where can we find them?

 

and i’m seeing that the z80 asm code needs tabs in all line codes, and after decades, now it’s the very first time i’m using tabs on asm code! :D - how could the converter accept spaces (like just 1) instead of tabs? i were expecting that all code compilable on Pasmo (for example), that we don’t need to use tabs, could be also easily convertible with this tool

 

another question: z80compiler is a very mistake-inducting name, wouldn’t be better naming it to z80to6502asmconverter or something like that?

 

and what about hosting the code somewhere like github? there people might want to fork it for extending for other architectures, like 6809, 8080, tms9900, etc. - which licence would it use? (gpl3?), and what do you think?

 

thanks again!

Edited by nitrofurano
Link to comment
Share on other sites

thanks!

i didn’t indent the code example above for pure laziness

i prefer indenting with spaces instead of tabs - when we use tabs for indenting, the code starts looking like this:

BtjZedW.jpg

Which is how it looks indented with spaces, just not as much. There is a setting in most modern editors called "TAB Size", which can be set to whatever you want.

Link to comment
Share on other sites

Which is how it looks indented with spaces, just not as much. There is a setting in most modern editors called "TAB Size", which can be set to whatever you want.

anyway, using tabs is indeed very confusing for me (even on Geany, that shows a grey arrow where the tab character is)

Link to comment
Share on other sites

I think if you used 8 spaces instead then it will be ok, but I don't see a problem with 'preparing' any sources with a regex substitution before passing into the program.

Similarly you could also head/tail that same file with any stock includes for the extra defines/sources needed to assemble to a standalone binary.

Link to comment
Share on other sites

Which is how it looks indented with spaces, just not as much. There is a setting in most modern editors called "TAB Size", which can be set to whatever you want.

 

Variable tab size is great for leading indentation but makes aligning comments (after instructions) a nightmare. Unless everyone uses the same tab width when viewing the same source file, things look a mess.

  • Like 3
Link to comment
Share on other sites

@mariusz (and @Wrathchild as well ), thanks again a lot for all code, information and motivation!

 

btw, when trying to compile the conversion results, we get errors like “:Label 'z80_c' not defined” - is after $80 the best place for defining z80 registers? and how? (i’m using xa65 assembler for now)

 

the ldir routine you posted here in this thread, but lddr, exx (i guess it’s not that difficult), ex, add (16bit), sbc (16bit), dec (16bit), di, ei, are missing - where can we find them?

 

and i’m seeing that the z80 asm code needs tabs in all line codes, and after decades, now it’s the very first time i’m using tabs on asm code! :D - how could the converter accept spaces (like just 1) instead of tabs? i were expecting that all code compilable on Pasmo (for example), that we don’t need to use tabs, could be also easily convertible with this tool

 

another question: z80compiler is a very mistake-inducting name, wouldn’t be better naming it to z80to6502asmconverter or something like that?

 

and what about hosting the code somewhere like github? there people might want to fork it for extending for other architectures, like 6809, 8080, tms9900, etc. - which licence would it use? (gpl3?), and what do you think?

 

thanks again!

 

You need z80.asm (attached) which provides definitions for z80 registers location as well as routines used to implement complex instructions (note that only instructions required by my games are implemented). Other file needed is spectrum_atari.asm implements hardware features of spectrum (input and output ports and screen emulation).

 

z80compiler is strict about syntax, but since I was always feeding output from my disassembler, this was not a problem. It shouldn't be too difficult to improve the parser to simply ignore whitespaces though.

 

The name of the tool was choose as an abbreviation to z80 to 6502 source-to-source compiler ;) (https://en.wikipedia.org/wiki/Source-to-source_compiler). Compiler sounds much better than just simple "converter", so I'd better stick with my name ;)

 

I released the tool as people were asking how I did Pentagram conversion, I haven't planned any other use for it (apart from my own personal use), so it is not sophisticated nor user friendly. I may consider setting up github project for it.

 

Mariusz.

z80.asm

spectrum_atari.asm

  • Like 1
Link to comment
Share on other sites

 

Variable tab size is great for leading indentation but makes aligning comments (after instructions) a nightmare. Unless everyone uses the same tab width when viewing the same source file, things look a mess.

There is this little feature in Notepad++ Edit->Blank Operations->TAB to Space. On the same menu it also has Space to TAB (all) and Space to TAB (leading).

Very handy.

 

FWIW, I use a TAB size of 4. You can clearly see the code nesting but it doesn't stretch the width out too far.

  • Like 1
Link to comment
Share on other sites

Yeah: I've used those facilities in Notepad++ before: very handy indeed. I suppose tab width is a non-issue once you decide on a value for your own projects and stick to it, but it all goes to Hell when the next guy opens the file and uses a different tab width. Eventually I started trying to avoid using multiple consecutive tabs anywhere but at the start of a line. Comments don't line up, but with syntax highlighting, etc, it's not a big problem.

Link to comment
Share on other sites

I think i need to create a bash script that puts a tab beginning in all lines that doesn’t have “:” character... :D (using cat and grep somehow...) - is that what you were mentioning, @Wrathchild ?

 

The tab size i have on Pluma (based on Gedit) and Geany is 2 characters, even that way using tabs is quite annoying for me...

 

@mariuszw thanks for the includes! (btw, i’m actually learning 6502 assembly from your tool, finally after decades trying! thanks a lot for everything indeed!)

 

@JamesD where can we have Notepad++ compiled for GNU/Linux? i didn’t want to run it on Wine... - and i’d love trying these features there then! :) - anyway, on most text editors i use (like Pluma and Gedit) i can convert a bunch of spaces into tabs and back, but even that way is quite “painful” for me

Edited by nitrofurano
Link to comment
Share on other sites

@JamesD where can we have Notepad++ compiled for GNU/Linux? i didn’t want to run it on Wine... - and i’d love trying these features there then! :) - anyway, on most text editors i use (like Pluma and Gedit) i can convert a bunch of spaces into tabs and back, but even that way is quite “painful” for me

Sadly, there isn't a version for Linux at this time. But I found this:

https://itsfoss.com/notepad-alternatives-for-linux/

 

 

Notepadqq might be the best choice since it is intended to be a clone of Notepad++

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Hi, I'm a newbie here (alhough I've been lurking for a while) and I found this topic very interesting especially with all the fairly recent conversions such as SkoolDaze and I wondered if people were also aware of a toolset called Skoolkit, https://skoolkit.ca/skoolkit/.

 

The idea is that Skoolkit can take a ZX spectrum dump file and work it into a readable format using a file known as a skool file. The skool file can be edited as the code is better understood to create a better disassembly in HTML ( as a few are visible on their website).

 

Apart from the fact that there are a few disassemblies there of some games we don't have for Atari 8, I was wondering as a theory if  the skoolkit toolset (written in Python which is on their github link) could be worked on or added to, to provide an extra command line option to produce to 6502 ASM option given a z80 dump + a skool file (i.e. it would translate the z80 to a suitable 6502 assembly).  Obviously, I've not tried it myself and there would still be the usual issues of self modifying code needing rewriting/ necessary relocation of code and hw specific translation (i.e. keyboard/ joystick), but I was wondering if it might make translation of new ZX spectrum games slightly easier (at least to a newbie like myself).

 

I'm not sure whether this approach would be any better than Marius' existing tool apart from there are a few skool files already and perhaps you can work on disassembly of a game (as a group ?) before you try and convert it to 6502.  Unfortunately, I don't have much provision to work on C stuff as I have only a work laptop and it's limited what I can install, although I do have Python.

 

Let me know if anyone thinks this is an idea worth pursuing.

Edited by TracMan
forgot to mention github
  • Like 1
Link to comment
Share on other sites

12 hours ago, TracMan said:

Hi, I'm a newbie here (alhough I've been lurking for a while) and I found this topic very interesting especially with all the fairly recent conversions such as SkoolDaze and I wondered if people were also aware of a toolset called Skoolkit, https://skoolkit.ca/skoolkit/.

 

The idea is that Skoolkit can take a ZX spectrum dump file and work it into a readable format using a file known as a skool file. The skool file can be edited as the code is better understood to create a better disassembly in HTML ( as a few are visible on their website).

 

Apart from the fact that there are a few disassemblies there of some games we don't have for Atari 8, I was wondering as a theory if  the skoolkit toolset (written in Python which is on their github link) could be worked on or added to, to provide an extra command line option to produce to 6502 ASM option given a z80 dump + a skool file (i.e. it would translate the z80 to a suitable 6502 assembly).  Obviously, I've not tried it myself and there would still be the usual issues of self modifying code needing rewriting/ necessary relocation of code and hw specific translation (i.e. keyboard/ joystick), but I was wondering if it might make translation of new ZX spectrum games slightly easier (at least to a newbie like myself).

 

I'm not sure whether this approach would be any better than Marius' existing tool apart from there are a few skool files already and perhaps you can work on disassembly of a game (as a group ?) before you try and convert it to 6502.  Unfortunately, I don't have much provision to work on C stuff as I have only a work laptop and it's limited what I can install, although I do have Python.

 

Let me know if anyone thinks this is an idea worth pursuing.

Hi,

 

Well, I am aware of Skoolkit :)

 

My tools accept disassembly listing in one file, but I think skoolkit can be used to produce one file with complete disassembly, instead of creating separate html files for each routine.

 

Published recompiler is quite picky about format of disassembly and actually accepts only the format produced by my own disassembly tool. With the time, I updated recompiler to accept different formats of disassembly. Chances are I skoolkit produced disassembly listings can work now. If they don't, I can update recompiler.

 

Note that produced code produced by recompiler usually doesn't even compile and requires some work to compile, and even more work to make game work properly - although recompiler provides assistance for this process, by marking places in generated files which require manual intervention with warning. Once warnings are fixed, the converted game should work, but at the very slow speed, as produced 6502 code is no way optimized, neither for speed nor for size. Code size should usually be optimized, as recompiler produces 2x bigger code, and some games simply do not fit straight in RAM after recompilation (this was the case with Jack The Nipper). For performance optimization it is usually enough to optimize manually sprite drawing routines, as these are responsible for 80-90% of game execution time.

 

In other words, recompiler is not intended to perform whole process on conversion of Z80 Spectrum game to 6502 Atari game, rather it is intended to assist in whole process, taking care of most tedious and error prone phase, i.e. rewriting Z80 assembly into 6502 assembly line by line, as it was the case before my tools were prepared :). For verifying correctness of 6502 code I use a tool which runs original Z80 code and translated 6502 code side by side, and differences can be spotted quite fast. The size and speed optimization is done manually.

 

HW routines do not need to be rewritten, I have prepared routines which deal with hardware and provide their output in format expected by Spectrum code - like routines for reading keyboard and joystick. Also, the screen is handled in Spectrum native layout, with Antic display list taking care of proper display.

 

I am not sure what is your goal. If you plan to make tools suite to convert skoolkit disassemblies into Atari games, they I am afraid, it will be not possible to achieve. If you want to simply provide a tool so that skoolkit disassembly can be fed into recompiler, then this is perfectly doable. On the other hand, if you want to convert specific Spectrum game to Atari, then I would rather not focus on skoolkit tools, but rather on that specific game itself.

 

Mariusz

 

  • Like 1
Link to comment
Share on other sites

I agree with pretty much with what Mariusz said. While I was looking to take Maziacs apart to port it to the Atari ST (which I did) I did look around for z80 disassemblers. What I was looking for was one that would be interactive and have a GUI. I did stumble upon skoolit and after 5 minutes of reading about it I dismissed it - it fails miserably on both my criteria :). Actually the way you control the disassembly and recognise parts of the binary as code or data is one of the most un-intuitive things I've seen.

 

So, to cut a long story short I would recommend that people try Ghidra out. For anyone that gets triggered with the tool being released by the NSA (tinfoil hats on etc etc) there is radare2 and one of its GUIs, cutter. Lastly, for people who want to spend a few bucks there's Binary Ninja, and for those who money is not an issue, IDA Pro.

 

With so many superior choices for disassembly tools, the only reason one would ever use skoolkit is if someone actually disassembled a few z80 games with it (I haven't checked but I doubt that there are people lining up to use this :)). And even then you just get the source code and pipe it into Mariusz' tool and work from there.

Edited by ggn
  • Like 1
Link to comment
Share on other sites

I don't think the freeware version of IDA has support for z80 though, but I could be wrong--I've been using the paid version for years and haven't kept up.  Well, I know that the free version doesn't support 64-bit processors of any kind at least.

 

The biggest problem with an auto-translation, though, is that it probably won't be very speedy.  One must definitely code for the particular processor's mentality, because trying to do one processor's job in another processor's mentality will definitely waste the other processor's potential.  For instance, 6502 to z80:  z80's strength is in having a large number of general-purpose registers to reduce the need for memory access.  6502's strength is in faster memory access.  If you just converted code that loads, adds, saves the accumulator as is to z80, the z80 code would crawl, and the many other registers would feel left out.

Link to comment
Share on other sites

23 minutes ago, ChildOfCv said:

The biggest problem with an auto-translation, though, is that it probably won't be very speedy.  One must definitely code for the particular processor's mentality, because trying to do one processor's job in another processor's mentality will definitely waste the other processor's potential.  For instance, 6502 to z80:  z80's strength is in having a large number of general-purpose registers to reduce the need for memory access.  6502's strength is in faster memory access.  If you just converted code that loads, adds, saves the accumulator as is to z80, the z80 code would crawl, and the many other registers would feel left out.

 

Apparently speed is not that big problem with auto-translation. Just remember that 80% of execution time is spent within 20% of code, which implies there is really no point to optimize for speed the other 80% of code, which accounts for 20% of execution time.

That was the reason I haven't attempted to write performance optimizer, as I thought I'd spend less time optimizing appropriate parts of generated code manually compared to time required for designing, writing and testing performance optimizer for 6502 code.

 

On the other hand problem with code size (z80 vs 6502) is more important. Auto-translation generates code 6502 2x bigger than original z80 code. Spectrum has 48K of RAM, and typical game has around 12KB of code. After translation I need 60KB of RAM, and another 1KB for support code, so almost all RAM is used, and there is no space for any Atari specific code, like music or colour handling. With simple optimization I get 1,5x code size rate, with very aggressive manual optimization I get 1,2x code size rate. I think this "simple optimization" phase could be implemented with a tool (with reasonable effort), but this still yet to be done.

Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

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...