[Coco] Pascal random() for OS-9 and DEFT
Robert Gault
robert.gault at worldnet.att.net
Sun Jan 24 08:12:08 EST 2010
Willard Goosey wrote:
> I've finally finished one of my little projects!
Here is a routine I've used in an OS-9 C program which could easily be
converted to straight asm use.
#include <time.h>
direct char rand[5]; /* used by random number routine */
struct registers {
char rg_cc,rg_a,rg_b,rg_dp;
unsigned rg_x,rg_y,rg_u;
};
struct registers reg;
main()
{
int rnd(),*reg_d;
reg_d=®.rg_a; /* reg.rg_a & reg.rg_b now addressed by reg_d */
/* get random seed for rnd from system time */
getime(&buffer); /* see time.h */
temp=&buffer;
for(i=0;i<5;i++)
rand[i]=*(temp+i+1);
/* example of rnd use
x/=(rnd(8)+2); */
/* Stack checking can be removed here for speed increase */
int rnd(val) /* standard rnd rountine replaced with faster asm; no floats */
unsigned val;
{
#asm
ldb 5,s
incb
andcc #$fe
lda <rand+4
bita #1
beq r1
lda #8
eora <rand
sta <rand
lda #128
eora <rand
sta <rand
orcc #1
r1 ror <rand
ror <rand+1
ror <rand+2
ror <rand+3
ror <rand+4
pshs b
lda <rand+1
mul
ldb ,s
sta ,s
lda <rand
mul
addb ,s+
adca #0
clrb
stb 4,s
sta 5,s
#endasm
return val; /* whole number returned; no fractional part */
}
More information about the Coco
mailing list