Porting from BASIC

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
stefano
Well known member
Posts: 1772
Joined: Mon Jul 16, 2007 7:39 pm

Porting from BASIC

Post by stefano »

There are few differences to deal with, like the inverted Y coordinate when drawing graphics, but it is definitely possible ;)

https://github.com/z88dk/z88dk/blob/mas ... /hangman.c
hangman.png
You do not have the required permissions to view the files attached to this post.
stefano
Well known member
Posts: 1772
Joined: Mon Jul 16, 2007 7:39 pm

Re: Porting from BASIC

Post by stefano »

zx81 port, the one in the picture has an internal mod for memory expansion and wrx
Image
Timmy
Well known member
Posts: 262
Joined: Sat Mar 10, 2012 4:18 pm

Re: Porting from BASIC

Post by Timmy »

I've ported a relatively simple game from BASIC to C a while ago. The exercise is covered in these two threads over at WoS:

BASIC version: https://worldofspectrum.org/forums/disc ... e-tutorial

C version: https://worldofspectrum.org/forums/disc ... rial-z88dk

(No graphical commands, just simple character usage.)
stefano
Well known member
Posts: 1772
Joined: Mon Jul 16, 2007 7:39 pm

Re: Porting from BASIC

Post by stefano »

interesting, you chose a C/asm mixture, surely a fun project.
I'm wondering if, in example, it could be ported to the zx81 in text mode., the scroll functions should be available.
Timmy
Well known member
Posts: 262
Joined: Sat Mar 10, 2012 4:18 pm

Re: Porting from BASIC

Post by Timmy »

stefano wrote: Wed Jun 29, 2022 8:34 pm I'm wondering if, in example, it could be ported to the zx81 in text mode., the scroll functions should be available.
I remember someone saying in the basic thread that he wanted to port to zx81, but never did.

I personally know too little about the zx81 that I have no idea how to port it. I don't even know what emulator to use (at the moment of writing).

What do you mean by "scroll functions" by the way? Does the zx81 have specific scroll functions in z88dk?
stefano
Well known member
Posts: 1772
Joined: Mon Jul 16, 2007 7:39 pm

Re: Porting from BASIC

Post by stefano »

Yes, it does, look at zx81.h.
By the way I remembered something better than the actual ones, horizontal scrolling is not available (while I think there's already something for the Spectrum and possibly the TS2068). The scroll down must be somewhat delicate, because I had trouble calling it from BASIC.
rollchr() is an impressive effect but does not have to do with scrolling.
Timmy
Well known member
Posts: 262
Joined: Sat Mar 10, 2012 4:18 pm

Re: Porting from BASIC

Post by Timmy »

stefano wrote: Wed Jun 29, 2022 8:34 pm I'm wondering if, in example, it could be ported to the zx81 in text mode., the scroll functions should be available.
So I just spent a part of a weekend to do something...

(Now this could be better and smaller and use conio and other libraries, but this is quicker and it's enough for now.)

Code: Select all

#include <input.h>
#include <zx81.h>

int __FASTCALL__ scroll_left()
// works on all models, untested.
{
	#asm
	ld	hl,(16396)	; D_FILE
	inc	hl			
	
	ld b, 21		; scrolling 20 lines for now
	
.loop1
	ld a, (hl)
	cp 0x76
	jp z, empty_line
	
	ld d, h
	ld e, l		; de -> previous char
	inc hl		; hl -> current char
	
.first
	ld a, (hl)
	cp 0x76
	jr nz, copying
	
	ld a, 0
	ld (de), a		; fill last char with space
	jr empty_line
	
.copying
	ld (de), a
	inc de
	inc hl
	
	jr first
	
.empty_line

	inc hl
	djnz loop1
	
	#endasm
}

// combine 2 chars into 1 int
int combine(uchar y, uchar x)
{
	int p;
	p = (y<<8) + x;
	return p;
}


int __FASTCALL__ init_screen(uchar i)
{
	// fill the screen with spaces, so that i don't have to create new lines
	// does the same as filltxt, but i might decide to change resolution later
	#asm
	ld a, l
	ld hl,(16396)	; D_FILE
	inc	hl	
	
	ld b, 23		
	
.loop_init
	push bc
	ld b, 32
	
.loop_row1
	ld (hl), a
	inc hl
	djnz loop_row1
	
	ld (hl), 0x76
	inc hl
	pop bc

	djnz loop_init
	#endasm
}


int __FASTCALL__ zx81_saddr(int yx)
// only works for 4k+ models, and only after using filltxt
{
	#asm
	ld b, h
	ld c, l
	
	ld hl,(16396)	; D_FILE
	inc hl
	ld de, 33		; line size (might change later), this only works for 4k+ models

	ld a, b
	and a
	jr z, no_rows1
	
.loop_rows1
	add hl, de
	djnz loop_rows1
	
.no_rows1
	ld b, 0
	add hl, bc
	
	#endasm
}

void print_string(int address, uchar *string_address)
{
	uchar *s;
	int a;
	
	a = address;
	s = string_address;
	
	while (s[0]!=0x99)
	{
		bpoke(a, s[0]);
		s++;
		a++;
	}
}

void __CALLEE__ i16toa(uint number, char* dest)
{
	#asm

		pop bc
		pop de				; de = dest
		pop hl				; hl = number
		push bc

		ld bc,-10000
		call num1
		ld bc,-1000
		call num1
		ld bc,-100
		call num1
		ld c,-10
		call num1
		ld c,b

.num1	ld	a, 0x1c - 1	; '0'-1
.num2	inc	a

		add hl,bc
		jr c, num2
		sbc hl,bc

		ld (de),a
		inc de
		ret
	#endasm

}

int main()
{
	char y;
	int addr, bottom;
	char game_over;
	int score;
	char score_str[16] = { 0x38, 0x28, 0x34, 0x37, 0x2a, 0, 0xe, 0, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0, 0x99 }; // score string but now in zx81 chars, obviously can't end with 0 now.
	
	init_screen(0);
	
	y = 10;
	game_over = 0;
	score = 0;
	bottom = zx81_saddr(combine(21,0));
	
	while (!game_over)
	{
		scroll_left();
		
		i16toa(score, &(score_str[8]) );
		print_string(bottom, score_str);
		
		bpoke (zx81_saddr(combine(rand()%21,30)), 0x17); // '*'
		
		y = y + (in_Inkey()=='6') - (in_Inkey()=='7');
		y = y + (y<0)-(y>20);
		
		addr = zx81_saddr(combine(y,3));
		
		bpoke (addr, 0x12); // '>'	
		
		game_over = bpeek(addr+1) == 0x17;
		
		score = score + 10;
	}
	
	return score;
}
---
Post Reply