Close

Random post

A project log for Merlin

68000 Retro Super Computer

matthew-pearceMatthew Pearce 07/06/2021 at 11:420 Comments

I tried to use the existing RND function in ehBasic. It left a bit to be desired, so I thought I'd use the Gameduino Library random function. It's a good example on how to merge the basic code, through assembler and out to a C function.

I have a C wrapper around the C++ Gameduino object. This simplifies the call from the basic asm code.

So the C code:

uint32_t gd_rand(uint32_t *seed) {
    return GD.random(*seed);
}


The assembler code is fairly simple, put the seed received as a parameter from the interpreter onto the stack using 'pea' a 68000 opcode. Call the C routine as a standard asm sub-routine, when it returns remove the parameter from the stack and the return value is in the 'd0' register. That value is then passed back to the interpreter.

 

	MOVE.l	d0,PRNlword(a3)		* save back to seed word

	pea PRNlword(a3)
	jsr gd_rand
	addq #4,sp

	MOVE.l	d0,FAC1_m(a3)		* copy to FAC1 mantissa


During compilation the toolchain will compile each separately and then the linker will create the complete program.

This function is then available to basic, the program below produces a random starfield in multiple colours in an area of 512x512.

Discussions