Z80 source code with comments

By Tatt2tim

Supporter (1)

Tatt2tim's picture

06-07-2022, 18:35

Hey y'all! I'm starting to get into MSX programming and I've read through the book Modern MSX BASIC game development and played around with the code in there a bit. I'd like to start with Z80 assembler code but it seems like every resource out there is either aimed at absolute beginners or wizards. I was wondering if there was an example of source code for a simple game written in z80 assembly that has extremely well commented code.

Speaking of Modern MSX BASIC Game Development, as I understand it even with a cruncher and X-BASIC z80 is still faster. Is there any kind of guide somewhere for converting code from BASIC to ASM? As I understand it it seems like you just need to be cognizant of memory addresses in assembly whereas BASIC has part of the RAM set aside for programs already and as such you don't have to explicitly map your program to memory. Do I have that more or less correct? If so, then converting from BASIC to ASM would simply be a matter of knowing what memory addresses the BASIC commands are changing and what value it's sending to that address, no?

Login or register to post comments

By djh1697

Paragon (1643)

djh1697's picture

06-07-2022, 22:56

I agree, there are plenty of books about the basic Z80 programming, but very few intermediate books.

The important thing to remember is to avoid using the Z80 in/out to access ports, use should use BIOS routines.

This website is pretty good http://map.grauw.nl/

By Pokun

Resident (47)

Pokun's picture

07-07-2022, 01:55

The VDP is the sole exception, but you need to get the port numbers used by the system by reading system registers VDP_DR and VDP_DW instead of using hard coded I/O port numbers if you are targeting MSX2. If you are targeting MSX2+ or later, the VDP port numbers are standardized though and can be hardcoded to $98~$9B. If targeting MSX1 I suppose $98 and $99 are standard?

I find using BIOS calls for the joystick being very limiting though since GTSTCK has unique values for diagonals instead of simply using the 4 bits from the joystick ports. I've been using hardcoded joystick port numbers to read joysticks directly, but it seems that's not MSX compliant after all. I suppose accessing the joystick ports via WRTPSG and RDPSG might be a better option.

By theNestruo

Champion (351)

theNestruo's picture

07-07-2022, 09:51

Answering to OP question, I found these sources very well commmented when I was learning assembly:

In GitHub you can find great games such as The Sword of Ianna, Santi Ontañón's games or even MetalGear. But I think they may be too big or too complex to be used as "learning examples" (but are great to peek at particular routines!)
You will find other disassemblies of "small" games (such as King's Valley, but comments are in Spanish).
I disassembled an small enough (8KB) game Pyramid Warp and, as it was my first disassembly, commented it exhaustively. And Stevedore's source code is also published; I think it has a good amount of comments but hey! I'm the author so I'm biased :)

Regarding BIOS usage: my advice is to use it. It makes things easier. Performance may not be optimal, but it is good enough for most scenarios (for example: Stevedore uses BIOS extensively!). And if in the future you are coding a game where BIOS performance is "not enough" (e.g.: fast scrolling), you are likely to have gained enough knowledge to do it.

By Timmy

Master (191)

Timmy's picture

09-07-2022, 02:37

You'd be surprised how many steps is needed to convert a simple basic game to asm. Or what "extremely well commented code" would mean for not "absolute beginners".

I did a similar exercise, not for the MSX, by explaining how to convert a small game (less than 20 lines of BASIC) to assembly/C.

Basically it took me 6 (small) chapters to explain the BASIC game, and another 6 chapters to convert it to machine code. Because most "intermediate" people don't even understand how the game worked in basic, therefore you will end up with something similar to a small novel.

And that's excluding many intricate details like how to set up a specific compiler/assembler, or how to find out which bios call you need, or how the screen is laid out, or why choose this solution over the other, and many more.

TLDR, no one wants to write or read a novel sized book just to convert a simple game to machine code. Smile