WIP - Walkabout (z88dk)

If you like transforming your statements into code, this is the place for you

Moderator: Programming Moderators

User avatar
bob_fossil
Posts: 30
Joined: Tue May 30, 2017 5:26 pm

WIP - Walkabout (z88dk)

Post by bob_fossil » Sat Jun 03, 2017 11:23 am

wip001-1.png
wip001-1.png (9.93 KiB) Viewed 3362 times
I downloaded the z88dk last week and was looking for a simple project to start off with. I have done z80 assembly in the past but liked the idea of developing the bulk in C and then swapping any bits out with assembly equivalents where necessary.

Over on the English Amiga Board, an old PC game POD had been converted over. It looked like a good candidate for porting across as the gameplay wasn't too complicated or graphically intensive - and the levels fitted nicely on the Spectrum's screen. I got the level data from this and then used some hand cranked Python to format it into C structures. I then spent the next couple of days writing code to display a level, then move a character around, implementing the game logic and getting it together as a complete game.

Gameplay wise, you need to move around and remove all the tiles from the screen to proceed to the next level. Blue tiles remove after you move off them, green tiles change into blue ones when you move off them and cyan tiles turn into green ones when you move off them. Magenta tiles are one time teleporters which take you back to the first red tile in the level. Red tiles are safe zones and won't disappear however many times you move over them. You lose a life if you step into empty space. There are currently twenty levels. You get a level code when you complete a level.

Additional credits:

z88dk for the compiler
Honx for the levels which I converted over.
Jonathan Cauldwell for his sound effect code which I adapted.
listofrandomwords.com - for the level codes.
Attachments
walkabout_wip004.zip
(5.81 KiB) Downloaded 75 times
Last edited by bob_fossil on Mon Jul 10, 2017 8:51 pm, edited 4 times in total.

JoeZX
Posts: 574
Joined: Mon May 29, 2017 9:11 pm
Location: Slovakia

Re: WIP - Walkabout (z88dk)

Post by JoeZX » Sat Jun 03, 2017 11:40 am

thanks for your effort, downloaded, played, fast, responsive, hard .. /heh, double ponytail/.

User avatar
bob_fossil
Posts: 30
Joined: Tue May 30, 2017 5:26 pm

Re: WIP - Walkabout (z88dk)

Post by bob_fossil » Sun Jun 04, 2017 2:30 pm

JoeZX wrote:
Sat Jun 03, 2017 11:40 am
thanks for your effort, downloaded, played, fast, responsive, hard .. /heh, double ponytail/.
Thanks for the nice feedback. I think the second level needs moving further down as it's one of the hardest of the 20 levels.

Jake
Posts: 4
Joined: Tue May 30, 2017 5:25 am

Re: WIP - Walkabout (z88dk)

Post by Jake » Sun Jun 04, 2017 6:25 pm

Hi Bob, liked this - could you share the source code ?

User avatar
bob_fossil
Posts: 30
Joined: Tue May 30, 2017 5:26 pm

Re: WIP - Walkabout (z88dk)

Post by bob_fossil » Mon Jun 05, 2017 5:48 pm

Jake wrote:
Sun Jun 04, 2017 6:25 pm
Hi Bob, liked this - could you share the source code ?
Thanks for the positive feedback. I've put the code up on my github . Apologies if I've missed anything - this is the first time I've released code on there. I also developed this on Linux, so there may be some issues with compiling under Windows.

I've also updated the initial attachment to include the latest set of changes - a slightly fancier menu and re-definable key controls.

Please be aware that the code wasn't written to be a tutorial or guide to using z88dk. It's just doing a job. :)

Jake
Posts: 4
Joined: Tue May 30, 2017 5:25 am

Re: WIP - Walkabout (z88dk)

Post by Jake » Mon Jun 05, 2017 7:22 pm

Thanks Bob - a great resource for those of us feeling their way with z88dk and getting back into z80 / c after 30 odd years!

JoeZX
Posts: 574
Joined: Mon May 29, 2017 9:11 pm
Location: Slovakia

Re: WIP - Walkabout (z88dk)

Post by JoeZX » Mon Jun 05, 2017 8:05 pm

Just one lil thing .. after entering a bad password /mismatch by accident/ player is than moved to the game, not to menu .. so w/out a QUIT key you have to killl yourself 3 times to get back to enter a new code.

Alcoholics Anonymous
Posts: 513
Joined: Mon May 29, 2017 7:00 pm

Re: WIP - Walkabout (z88dk)

Post by Alcoholics Anonymous » Mon Jun 05, 2017 8:56 pm

Nice work Bob!

You can eliminate sjasm from the build by replacing 'OUTPUT "assets.bin"' with "SECTION ASSETS" in "assets.asm" and then add "assets.asm" to the zcc compile line.

Code: Select all

SECTION ASSETS
ORG 25000

; 25000 - 25767 : Font
...
Section "ASSETS" will be unknown to the memory map and because it has its own ORG, the linker will output it as a separate binary file "game_ASSETS.bin". If you did want the asm to be incorporated into the main executable, you'd get rid of the ORG and use a section known to the memory map like "rodata_user".

It's best practice to have your asm functions in their own asm files rather than inlined in C functions so that you maintain control over section assignment and can cleanly separate the portable C portion from the asm (regarding sound.c, etc). Just assign to sections like "code_user", "bss_user", "rodata_user", "data_user", "smc_user" as appropriate.

The "#asm / #endasm" delimiters you are using to inline asm are now deprecated in favour of "__asm / __endasm;" (note the semi-colon). This is because zsdcc will not accept the former but both compilers will accept the latter. A "__naked" attribute should also be added to the end of a C wrapper containing pure asm for zsdcc compatibility so that zsdcc does not generate prologue or epilogue code for the function.

Code: Select all

void sound_effect(int pitch, char bend) __naked
{
	__asm
	
	ld hl,2
	add hl,sp		// skip over return address on stack

	....

	pop bc
	djnz loop           ; repeat.

	ret
	__endasm;
}
I put the terminating "ret" back because with "__naked" zsdcc will not generate it.

Moving this to a separate asm file will get rid of all the extra furniture and keep it cleaner:

"sound.asm"

Code: Select all

SECTION code_user
PUBLIC _sound_effect

_sound_effect:

	ld hl,2
	add hl,sp		// skip over return address on stack

	....

	pop bc
	djnz loop           ; repeat.

	ret
When your functions do not use C wrappers you can also use other function call linkages. Fastcall, for example, will pass a single parameter in DEHL (a subset of depending on size). Callee will have your function getting parameters from the stack and clearing the stack at the same time (this reduces code size and increases speed at the caller).

Lastly, zsdcc pushes parameters in right-to-left order whereas sccz80 pushes in left-to-right order. This will affect how you gather parameters from the stack. You can remain compatible with both compilers by using IF.ELSE.ENDIF and testing for "__SDCC" defined to supply appropriate code for each compiler.


I thought I'd mention these things because the code you've written is a bit old style :)
Z88DK's Next target will be added to the new c library (this compile is using the classic c library).

User avatar
bob_fossil
Posts: 30
Joined: Tue May 30, 2017 5:26 pm

Re: WIP - Walkabout (z88dk)

Post by bob_fossil » Mon Jun 05, 2017 9:33 pm

Alcoholics Anonymous wrote:
Mon Jun 05, 2017 8:56 pm
Nice work Bob!

You can eliminate sjasm from the build by replacing 'OUTPUT "assets.bin"' with "SECTION ASSETS" in "assets.asm" and then add "assets.asm" to the zcc compile line.

Code: Select all

SECTION ASSETS
ORG 25000

; 25000 - 25767 : Font
...
Section "ASSETS" will be unknown to the memory map and because it has its own ORG, the linker will output it as a separate binary file "game_ASSETS.bin". If you did want the asm to be incorporated into the main executable, you'd get rid of the ORG and use a section known to the memory map like "rodata_user".
Thank you for taking the time to go through my code. I can simplify the build and remove the need for sjasm. As for the other points with inline assembly usage, well - in my defence, the wiki entries for the stack frame and inline assembler don't mention that #asm and #endasm are deprecated, that the stack handling is compiler dependant or that you shouldn't really be using it in the first place. :)

User avatar
bob_fossil
Posts: 30
Joined: Tue May 30, 2017 5:26 pm

Re: WIP - Walkabout (z88dk)

Post by bob_fossil » Mon Jun 05, 2017 9:50 pm

JoeZX wrote:
Mon Jun 05, 2017 8:05 pm
Just one lil thing .. after entering a bad password /mismatch by accident/ player is than moved to the game, not to menu .. so w/out a QUIT key you have to killl yourself 3 times to get back to enter a new code.
You can press delete to remove a character if you've made a mistake whilst typing. The idea was that if you entered a blank or wrong level code that you start at the first level. In the main menu you'd previously selected the play game option so going into the game regardless made sense to me. I will look at adding in a quit key.

Post Reply