Page 1 of 2

PEEKing memory locations with z88dk (for ZX80 randomiser)

Posted: Mon Nov 27, 2017 9:45 pm
by Shaun_B
Hi,

I've found this program for the Sinclair ZX80 Programming Course:

Code: Select all

    10 FOR I=1 to 20* RND(100)
    20 NEXT I
    30 POKE 16414,0
    40 POKE 16415,0
    50 PRINT "HIT RETURN"
    60 INPUT C$
    70 LET A=PEEK(16414)
    80 LET B=PEEK(16415)
    90 PRINT "YOUR REACTION TIME WAS";(B**256+A-4) *20;	"MS";
Does anyone know how to PEEK memory with z88dk?, as I need a better randomiser than what I have. I'm asking here as www.z88dk.org seems to be down at the moment.

Thanks,

Shaun

Re: PEEKing memory locations with z88dk (for ZX80 randomiser)

Posted: Mon Nov 27, 2017 10:45 pm
by Shaun_B
It's okay, I think that I've guessed a solution, as follows:

Code: Select all


int main();
void ramdomiser();

unsigned char random;
unsigned char stringBuffer;

/**
 * Random number generator based on
 * how long the user interacts with the
 * program - prints random numbers
 * between 0 and 15
 * DONKEYSOFT MMXVII
 */
int main()
{
    unsigned char i, r;
    for(i = 0; i < 20; i++)
    {
        randomiser();
        r = srand(random) % 16;
        printf("%d", r);
        gets(stringBuffer);
    }
}

/**
 * Sets the unsigned char random to
 * the current low byte value of $401e
 * If this routine causes issues with
 * your program then push hl and a
 * to the stack and restore them, or
 * exchange for the shadow registers
 */
void randomiser()
{
    __asm
        ld hl, ($401e)
        ld a, l
        ld (_random), a
    __endasm;
}
When writing to 'globals' in your assembly listing, you need to put an underscore before the variable name (_random is where we are storing the value passed to a, so each time randomiser() is called, it will update the value in random).

Enjoy!

Shaun

Re: PEEKing memory locations with z88dk (for ZX80 randomiser)

Posted: Tue Nov 28, 2017 9:01 am
by siggi
Hi Shaun
use somthing like that:

Code: Select all

char *ptr;
char ch;
ptr = (char *) 0x4711; /* address to be peeked */
c  = *ptr;
Regards
Siggi

Re: PEEKing memory locations with z88dk (for ZX80 randomiser)

Posted: Tue Nov 28, 2017 9:25 am
by siggi
Or to PEEK/POKE system variables (or other fixed memory locations) just create a variable there and use it like usual:

Code: Select all


int sysvar @ 0x401e;

if (sysvar ==0x4711)
 sysvar = 0;
 
Siggi

Re: PEEKing memory locations with z88dk (for ZX80 randomiser)

Posted: Tue Nov 28, 2017 10:30 am
by Shaun_B
Thanks for the tips siggi, the @ thing looks like something very powerful.

Regards,

Shaun

Re: PEEKing memory locations with z88dk (for ZX80 randomiser)

Posted: Tue Nov 28, 2017 12:26 pm
by PokeMon
Isn't it usual /classic defined in C this way ? 8-)

Code: Select all

int *sysvar=0x401e;
if (*sysvar==0x4711) *sysvar=0;

Re: PEEKing memory locations with z88dk (for ZX80 randomiser)

Posted: Tue Nov 28, 2017 8:12 pm
by Shaun_B
You guys are too clever for me - cheers!

Shaun

Re: PEEKing memory locations with z88dk (for ZX80 randomiser)

Posted: Wed Nov 29, 2017 3:25 pm
by dr beep
Shaun_B wrote: Tue Nov 28, 2017 8:12 pm You guys are too clever for me - cheers!

Shaun
Is this a standard answer, Shaun?
I got the same in a different code-effort.

Re: PEEKing memory locations with z88dk (for ZX80 randomiser)

Posted: Thu Nov 30, 2017 7:35 am
by Shaun_B
Hi dr beep,

Not a standard response but a default position; I retrained into the world of computer science from health and social care around 8 years ago and my aim is always to be a better developer in six months time than I am today. But because I didn't keep up with computer science for around 20 years I feel like a big dumb dinosaur sometimes.

Regards,

Shaun.

Re: PEEKing memory locations with z88dk (for ZX80 randomiser)

Posted: Thu Nov 30, 2017 1:14 pm
by PokeMon
My comment was because the declaration below is in my eyes not C standard but some z88dk special syntax.

Code: Select all

int sysvar @ 0x401e;
So if you do further C developments with other kits and targets you might benefit from getting familiar with pointers (*) and casting declarations in C. ;)