BrianBeuken Posted April 28, 2014 Share Posted April 28, 2014 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 Quote Link to comment Share on other sites More sharing options...
+nanochess Posted April 28, 2014 Share Posted April 28, 2014 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. Quote Link to comment Share on other sites More sharing options...
BrianBeuken Posted April 28, 2014 Author Share Posted April 28, 2014 Aha, that is indeed the problem, I bypassed the colecovision screen, thanks so much for this little gem of wisdom. Quote Link to comment Share on other sites More sharing options...
BrianBeuken Posted April 28, 2014 Author Share Posted April 28, 2014 (edited) 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 April 28, 2014 by BrianBeuken Quote Link to comment Share on other sites More sharing options...
CrazyBoss Posted April 28, 2014 Share Posted April 28, 2014 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. 1 Quote Link to comment Share on other sites More sharing options...
+nanochess Posted April 28, 2014 Share Posted April 28, 2014 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. 1 Quote Link to comment Share on other sites More sharing options...
Kiwi Posted April 28, 2014 Share Posted April 28, 2014 Here's the memory map taken from the last page of cv programming.pdf. From the crtcv.map that generated by SDCC my variable starts at 706F. 1 Quote Link to comment Share on other sites More sharing options...
BrianBeuken Posted April 28, 2014 Author Share Posted April 28, 2014 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 2 Quote Link to comment Share on other sites More sharing options...
Tony Cruise Posted April 29, 2014 Share Posted April 29, 2014 (edited) 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 April 29, 2014 by Electric Adventures 1 Quote Link to comment Share on other sites More sharing options...
BrianBeuken Posted April 29, 2014 Author Share Posted April 29, 2014 (edited) 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 April 29, 2014 by BrianBeuken Quote Link to comment Share on other sites More sharing options...
artrag Posted June 15, 2014 Share Posted June 15, 2014 (edited) 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 June 15, 2014 by artrag 1 Quote Link to comment Share on other sites More sharing options...
BrianBeuken Posted June 18, 2014 Author Share Posted June 18, 2014 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.