Nodes of Yesod : ZX Spectrum Next : Update #4

A Big Change Of Plans!

Astro Charlie has a close encounter

If you have read my previous updates, you'll know that Nodes of Yesod for the Speccy Next is based on a C codebase. I've discussed details of my progress in that direction in previous posts, tool selection, and various other considerations.

I have to share with you that I've had a rethink and there's a big change of plans!

As I intend to make this version of Nodes freely available, and since it follows that there's no direct benefit to me financially, building the game has to be a fun and enjoyable endeavor. While I've enjoyed certain aspects of getting a C codebase to compile and run on the Next, I've had a few nagging doubts. C was always going to be a 'heavy' way to do things on an 8-bit Z80 (even with a faster clock), and paging code in and out of 8K or 16K banks does not make things less complicated; however, more than that, I need to personally get something out of this, it needs to be a fun process, and I want to write some Z80 code!

What's The Plan?

Let me take you back in time. In 2003 I purchased a full version of IDA Pro from Hex-Rays, version Perhaps I'll cover the specific use case for that purchase in a future post, but suffice to say there were a couple of 8-bit and 16-bit games I wanted to disassemble. And IDA Pro was the best possible disassembler tool available at that time.

IDA stands for 'interactive disassembler', which is what the tool does. A disassembler can be compared by analogy to a tool into which you could feed a cake, whereupon it will output the ingredients for that cake. Eggs, sugar, flour, etc. An 'interactive' disassembler can go much further and give you not only the ingredients but also the recipe for that cake, along with baking instructions. IDA happens to be extraordinarily good at this.

With IDA, you take a binary program (for example a memory dump of a Sinclair Spectrum game), load it into the disassembler, and then interactively recreate the program symbols, functions, branches, data areas, and data types that were present in the original source code (which may have been lost forever, as is the case for Nodes of Yesod). When a memory location is identified as a variable, 'health' for example, you can designate that memory address as a named 'label', and then IDA automatically changes any references to that memory location into a reference to that label. The same with functions, IDA will change any subroutine call into a call to a named label.

I've used this fantastic tool for various projects, but I have never upgraded it because, in addition to being extraordinarily good at what it does, it is extraordinarily expensive (it's about $2,000)!

One thing I have done, in the intervening years, sporadically, is to create a disassembly of Nodes of Yesod, which I'd guess is 90% plus complete. Not 100%, but close. You may be able to see what is coming at this point - I'm going to upgrade IDA Pro to the latest version (the original version is a DOS program) and complete the disassembly, right?


I'm not about to spend $$$ on the current version of IDA Pro (though did flirt with the idea!) but there is an alternative.


Ghidra, interactively disassembling Nodes of Yesod

If one thing is certain, it is that there is no certainty in life. While it was not certain that I could do what I wanted to do, I spent a couple of weeks evaluating Ghidra to continue the disassembly of Nodes ('continue' meaning 'start over' using the disassembly generated in IDA Pro as a guide) and things seemed promising, there appeared to be light at the end of the tunnel. Or else, there was a train headed my way and I was about to be flattened!

Ghidra is an interesting beast, in some ways similar to IDA, but open source. It is a creation of the NSA (yes, that NSA), and it is used to find vulnerabilities in binary code that is out in the wild, and to dissect malware. It's probably used to create malware too if we're honest. It is sophisticated, and I needed a mere fraction of its capabilities. For example, as you interactively disassemble binary code, Ghidra creates a C-like implementation of each function which could, theoretically, be used to reimplement the binary code in C. I did not need anything like that, I merely needed the Z80 disassembly, with the goal of generating a buildable set of source code. Would it be possible?

The Benefits

With this approach, I'd end up, if all went to plan, with a complete source code representation of Nodes. Not the original source code, but something that I could assemble with modern-day tools. From there, I could start to support Next-specific changes needed to make the updates I want. Additionally, it'd allow me to start from a point of a working game, and then add to it. For example, replacing the sprites with hardware sprites while having a playable game. And, I'd be able to debug the Z80 code directly without the additional C abstractions getting in the way.

But there would be other benefits. I'd like to publish the source code for the original, unmodified game. And, with an assembly source code version of the game, it would make it easier to target other 8-bit or 16-bit systems machines that never had a version of Nodes. NES perhaps? I've never developed for the non-Z80 8-bitters so that might be an interesting challenge.  And if this works out, I could potentially do the same with other Speccy games. For example, I have a substantially complete disassembly of Crosswize, again in IDA Pro.

Another benefit would be that by starting with a complete disassembly of the original game, it would be possible to release a new version of Nodes for the Next sooner and incrementally release upgrades. That appeals to me because I'd be able to get software into peoples' hands sooner.

What's Stopping Me?

Creating a complete disassembly of anything is a ton of work, but that is not something I find offputting. As I said, it was not 100% certain I could generate the buildable source code I need from Ghidra. After diving into the disassembly, and doing a few experimental exports, I had yet to prove that the output was viable, meaning I could feed it into a tool like SNasm and get out of SNasm something identical, or at least very similar, to the original Nodes binary.

So Where Are We At?

TLDR, it's done! I have 100% completed the disassembly of Nodes of Yesod, wrote an output formatter for Ghidra, in Python, that generates SNasm compatible source code with no hand modifications, assembled that code with SNasm, and created a binary identical to the one I started disassembling! And, it works! Here is the resultant binary running in CSpect (with commentary by yours truly).

The first time Nodes has built from Z80 source in over 30 years!

What Was Involved?

More than I thought! Ghidra is great at interactively disassembling and patching binary files. But I found generating an actual assembly language source code file tricky. And, there are bugs on the Z80 side that cannot be worked around, completely missing parentheses on some instructions, incorrect disassembly of other instructions, left or right parenthesis missing on other instructions, and so on. And the output it does generate doesn't seem standard to me at all. So I wrote a Python (Jython) script to fix all these deficiencies, and more besides! Here's what my 'Listing' script does, in no particular order:
  • Generates an equates table at the top of the source file
  • Outputs all equate definitions and references in ALL_CAPS to distinguish equates
  • Fixes all the disassembly errors encountered so far, including bit/res/set (hl), jp (hl)/(ix)/(iy), add ix,iy, and more. These fixes should be in the core disassembler but they're fixed at the point of generating the listing, for now.
  • Honors function and end-of-line comments and outputs them into the source
  • Co-opts two of the Ghidra comment modes (pre and post comments) to output pseudo assembler instructions into the code (this could be expanded but is functional for now):

    opt z80
    device zxspectrum48
    org     $5b00

  • Adds a trailing colon to labels 
  • Formats hexadecimal numbers in SNasm format ($0000)
Here's some actual output from the tool (it is the same area of code as the Ghidra screenshot above):
		ld sp,irq_entry
		ld a,$39
		ld i,a
		im 2		; im2
		ld a,JR_OPCODE
		ld (irq_branch),a
		ld a,JP_OPCODE
		ld hl,irq_service
		ld (irq_entry),a
		ld (irq_address),hl
		call initialize_game
		ld a,(time_hours)
		cp $10
		jr c,lab_ram_7dcf
		ld a,$01
		jp game_over
		ld ix,player_scb
		call check_room_switch
		call update_items
		ld ix,gravstick_scb
		ld hl,$00
		ld a,h
		or l
		jr nz,lab_ram_7e30
		ld a,(player_flags)
		and $03
Perhaps it goes without saying that getting the code to output in the above format took hours of poring over Ghidra docs, figuring out their Jython scripting stuff, trying stuff out, falling flat on my face, rinse, repeat. There is no debugger (error logging FTW) and there's not even a text search facility in the script editor. It is pretty gnarly, though the interactive Python console was very helpful, from there you can introspect objects and get interactive help on things. Got there in the end though!

What's Next?

I have a small list of improvements that I want to make to the Listing script, including sorting the equates numerically into groups after renaming them with a <context>_<meaning> convention. For example, SPRITE_XPOS or ENEMY_HEALTHOnce done, I'll automatically sort by the context and then numerical value.

For example:

SPRITE_Y     equ 2
SPRITE_X     equ 1


SPRITE_X     equ 1
SPRITE_Y     equ 2

Pretty obvious, but at the moment the equates appear in the order they were originally defined, which is basically random (and irritates me every time I see it).

Once these quality of life improvements are complete, I will then spend a little more time improving the comments in the disassembly, and adding symbolic names to the various places where the exact function still needs to be determined. Basically, I want to get the automatically generated source to be as good as practically possible before embarking on modifications for the Speccy Next.

I do hope to make this disassembly available on my Github in due course. I will also make my Ghidra listing script available, should any budding reverse engineers or code archeologists wish to review it (it's not pretty and it is not clever but gets the job done).

I may also make an interim version of Nodes of Yesod available at that point, prior to embarking on the various planned improvements for the Spectrum Next version.

Hopefully, I'll have an update within the next couple of weeks once all the groundwork listed above is done.



i. To those wondering why I don't use the original source code, the answer is simple: it, along with all other source code pre-1992, was 'lost'. Perhaps I will cover that in a future post.

ii. Yes, it has taken me longer to get to this point than it took to write the original game! My hope is that this effort will pay off and the approach can be applied to other games in the future.


Unknown said…
Nice - if you're in need of a next style remake of Nodes on the next let me know - Check for what I do.

Steve said…
Thanks for commenting! Will check out your site!
DarkFalzX said…
Any chance of the Nodes of Yesod 30th Anniversary ever coming to PC/Steam? AppleTV is quite an obscure platform for gaming, unfortunately.
Steve said…
Right, AppleTV is obscure. I don't really have any plans to bring it to other platforms. When I finally get the Spectrum Next version done then perhaps this is something I can revisit.
DarkFalzX said…
I meant no offense about my Apple TV comment, though it was seriously the very first time I've ever heard of it being used for gaming.
Steve said…
Yep! No offense, lol! I just mean that is is relatively obscure though there are quite a few games for the system. The AppleTV version came about because it was an iteration of the iOS version. The iOS version was pulled from the App Store (by Apple) when Apple mandated 64-bit only apps. Funnily enough I did complete a 64-bit version that is also fully localized in English, French, German & Spanish, but I think I moved jobs and cities right as that work was completed and I lost momentum & motivation to submit the app to the store. That was about 5 years and probably 100 iterations of XCode ago so it is probable that the code does not even build on current XCode.
Steve said…
The AppleTV version is interesting because it updates the display as fast as possible. It uses floating point for the screen position which is still 256x192 pixels internally, so essentially this ends up with virtual sub-pixel positioning at 60fps.
Pamplemoose said…
This tool may be of interest:
Steve said…
Very interesting! I watched the video and this looks cool. Windows only I guess, will need to fire up the old Windows machine. I really do need to get back to some Speccy development ...
Matt said…
Hi Steve,

Great insights. It's really interesting to m being able to see the process and how you are going about it.

Have you been able to spend anymore time on it?

Steve said…
Hi Matt - I've been stalled for a bit so not much progress to speak of since the most recent update. It is definitely on the to-do list though!
Matt said…
Yeah, life does tend to get in the way. I've got a Next project I started in 2020, that I'm still working on. It's fun exploring what all the new capabilities and mass storage for assets and code can offer though.

Looking forward to yours. Thanks for taking the time to reply. :)

Jace said…
Hi Steve

I'm really interested to know if you have made any further progress?

All the best

Simon said…
I`m after this game too!
Is it still going to be available as a free offer for KS1 backers like it was suggested on the kickstart stretch goals?
Steve said…
Thinking about this more recently, especially after receiving shipping notification for the KS2 machine. Watch this space!
fuzzweed said…
Hi you popped up googling nodes disassembly. Any chance you could release the code for the sample / speech at the beginning?
Steve said…
Hello fuzzweed! No concrete plans for that but I’ll post here if anything concrete comes up. Steve.

Popular posts from this blog

Odin Computer Graphics Part One : 1985

Software Projects : 1984