Jump to content
IGNORED

system Random routine RAND_GEN equ $1FFD


Recommended Posts

Hi guys

can any one advise me on the use of the RAND_GEN routine, it does not seem to produce a result, returning 0 each time, I assume that is because it needs a random seed set somewhere but I can't locate where that seeding is done.

 

in the meantime I'm using the R register but would very much like ot use the system routine RAND_GEN equ $1FFD

 

 

thanks

 

Brian

Link to comment
Share on other sites

The $73C8 location (RAND_NUM) two bytes should be initialized to $33 $00

 

LD HL,$0033

LD ($73C8),HL

 

This is done by default if you use the Colecovision standard screen, but it's not done if you bypass the Colecovision screen.

 

Once this is done you can get the random value from HL or A.

Link to comment
Share on other sites

The $73C8 location (RAND_NUM) two bytes should be initialized to $33 $00

 

LD HL,$0033

LD ($73C8),HL

 

This is done by default if you use the Colecovision standard screen, but it's not done if you bypass the Colecovision screen.

 

Once this is done you can get the random value from HL or A.

I note that the system is using the upper part of its 1K to do system stuff, ie, $73C8 I thought that low part of RAM was being used so I have my ram starting at $7100.

 

I think I may have that wrong now, can you give me some info on the system RAM usage?

 

Brian

Edited by BrianBeuken
Link to comment
Share on other sites

As I know that random routine is not very good, I know programmers who didn use it. Another way is to use:

LD A,R

 

but its not very random too.

 

Some programmers avoid using the BIOS at all, since the CV ram is limited to 1024 bytes, including the stack, so you dont want to use a bios call which also lock up your memory.

 

If you dont use any of the bios calls, you can put your SP to $7FFF

 

If using bios then I think it steals some of the addresses, I dont remember, how much but the init sp, is around $7FB0 ? i think.

  • Like 1
Link to comment
Share on other sites

I note that the system is using the upper part of its 1K to do system stuff, ie, $73C8 I thought that low part of RAM was being used so I have my ram starting at $7100.

 

I think I may have that wrong now, can you give me some info on the system RAM usage?

 

Brian

 

This document contains system RAM usage: http://www.theadamresource.com/manuals/technical/ColecoVision%20Coding%20Guide.pdf

 

Note that if you bypass the BIOS you can use RAM as you like.

  • Like 1
Link to comment
Share on other sites

Thanks Guys this is very helpful

 

I'm currently not really using the BIOS beyond getting control pads and that Random number, R is of course usable but limited and I do occasionally need multiple random values in one routine making R less useful, I may just use my own random systems, but I do want to try and use the sound player for my music, since I don't have a decent music engine...I'll have to see, but the map is very helpful help me avoid crashing into the systems RAM.

 

I appreciate the help.

 

Brian

  • Like 2
Link to comment
Share on other sites

Try this random routine, you need to seed it with something, one of the best things is to have a time counter in your NMI routine and make that the seed when the user starts the game i.e. the user becomes the random seed value.


;
;   Seed Random numbers
;   Seed in HL
SEED_RANDOM:
    LD (SEED),HL
    RR H
    RL L
    LD (SEED+2),HL
    RET
;
;   Generate a random number, based on the initial Seed
;   value.
;
RND:
    PUSH HL
    PUSH BC
    PUSH DE
    LD DE,(SEED+2)
    LD HL,(SEED)
    LD B,5
RLP1:
    RR H
    RL L
    RR D
    RL E
    DJNZ RLP1
    LD B,3
RLP2:
    PUSH DE
    LD DE,(SEED)
    OR A
    SBC HL,DE
    EX DE,HL
    POP HL
    DJNZ RLP2
    LD (SEED),HL
    LD (SEED+2),DE
    LD A,E
    ;OR H
    POP DE
    POP BC
    POP HL
    RET
ORG 07000h
SEED:       DS 4z

 

 

Edited by Electric Adventures
  • Like 1
Link to comment
Share on other sites

Thanks, I'll use that, I was aware of the timer and user input, it provides an unpredictable seed creating a better sequence.

 

All help much appreciated, I've been away from Z80 for a long time an missing my old library routines so little snippets like this are a big help.

 

 

 

Brian

Edited by BrianBeuken
Link to comment
Share on other sites

  • 1 month later...

if you need 7 bit random numbers this code gives fairly uniform and uncorrelated random values



; Fast random number generator using the same method
; as the CDMA used in cellular telephones
;--------------------------------------------------------------------
; init random seed 

rand8_init:
	LD      HL,(JIFFY); on msx this is a time counter updated at any vblank interrupt, coleco has an equivalent
	SET		0,L
	LD      (randSeed),HL
	RET


; -------------------------------------------------------------------
; rand8
; -------------------------------------------------------------------
;
; choose a random number in the set [0,127] with uniform distribution
; return: A = random value

rand8:
	ld      hl,(randSeed)
	add     hl,hl
	sbc     a,a
	and     083h
	xor     l
	ld      l,a
	ld      (randSeed),hl
	ret
Edited by artrag
  • Like 1
Link to comment
Share on other sites

 

if you need 7 bit random numbers this code gives fairly uniform and uncorrelated random values



; Fast random number generator using the same method
; as the CDMA used in cellular telephones
;--------------------------------------------------------------------
; init random seed 

rand8_init:
	LD      HL,(JIFFY); on msx this is a time counter updated at any vblank interrupt, coleco has an equivalent
	SET		0,L
	LD      (randSeed),HL
	RET


; -------------------------------------------------------------------
; rand8
; -------------------------------------------------------------------
;
; choose a random number in the set [0,127] with uniform distribution
; return: A = random value

rand8:
	ld      hl,(randSeed)
	add     hl,hl
	sbc     a,a
	and     083h
	xor     l
	ld      l,a
	ld      (randSeed),hl
	ret

neat and fast thanks...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...