
https://github.com/z88dk/z88dk/blob/mas ... /hangman.c
I remember someone saying in the basic thread that he wanted to port to zx81, but never did.
So I just spent a part of a weekend to do something...
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;
}