Multiplatform Series
In this series we'll take a look at a variety of simple blocks of code, which will provide functions that may help you in your code. These do not relate to a particular platform, so should work on any 6809 based machine
The Random
number generator shown today is far from the 'Best'... but it does
work! It's used in these tutorials by all the games as it's been tested without any problems, and has been ported to many different systems and CPUs, and given the same random seed, it gives the same results... meaning it can be used for generating random levels in your game, and the level will be the same on every system. |
![]() |
Let's make... Suck Shoot!
Lets make a little vectrex game! We're going to write a random number generator, and use it to position a 'bat' on screen. We'll use a range checker to detect when our 'crosshair' cursor goes over the bat. In a later episode we'll add score and lives, and make the bat threaten us! |
![]() |
Random number generation
To allow our random number generator to create good varied results, we use a pair of 16 byte lookup tables | ![]() |
Our random number generator produces a 16 bit result (z_hl in the
zero page) from a 16 bit random seed (z_bc in the zero page). The 'High byte' of the 16 bit pair is created by bitshifting and combining the two. |
![]() |
We need the 'Low byte' to be very different, or the 16 bit values
won't be very random! We use the lookup tables to get some new data, and combine these to produce the second value |
![]() |
We have two routines we can use to get values "DoRandomWord" will get a 16 bit value zHL from seed zBC, this can be used to produce 'psuedorandom' data for times we want repeatable results. "DoRandom" is an easy function to return an 8 bit byte in the Accumulator... it automatically updates its seed every time. |
![]() |
![]() |
To be usable, a random
number generator needs to produce every possible random number
(0-255 or 0-65535)... otherwise you could have serious problems with
your program (if you're waiting for a result that never happens). The more 'random' the data the better... that is, if you plotted the values on a graph there should be no patterns present. |
Range checking
We need to do 'Collision detection'. We specify a two targets and a 'range' ... if target 1 is within 'Range' of target 2, we return A=1... if it's out of range we return A=0... This means we can do a BNE or BEQ on return. |
![]() |
The range check is simple... we test X and Y axis We test the X axis first. we subtract the 'RANGE' from each axis of the object, and check if we went below Zero (if so that direction is over the limit)... we then compare to the position... if it's lower we're out of range... if not we need to test more! Next we add 'RANGE' twice... once to move back to the center and once to move to the right... we then compare to the position... If it's higher we're out of range... if not we need to test the Y axis! We repeat the same tests for the Y axis. |
![]() |
BCD and DAA
We're going to show scores on screen in our game - They'll use 8
digits. We'll use 'Packed' format - meaning 1 digit per nibble... the format we'll use will be 'Little Endian' so the low value BCD bytes will appear first. |
![]() |
Here's $00000005 in BCD bytes |
![]() |
BCD only uses a 0-9 value in each nibble... but of course each
nibble can really support 0-F Suppose we repeatedly add 1 to the value 8... we'll need to correct the value once the value goes over 9 values like $0A will need to be converted to the correct BCD value of $10 - and that's what DAA does! |
$08 +1 $09 +1 $0A -> $10 |
Here is a BCD add routine... it works on two BCD values Y
and X of length B Effectively this performs Y=Y+X This routine works from the least significant byte to the most, adding each pair of bytes with the carry. |
![]() |
We can use this function to add two BCD values together... we'll
use this to increase our score. |
![]() |
Here is the result of adding $12345678 and $00000005 | ![]() |
Showing what you've got... and comparing!
Want to show off your Packed BCD? We can do this by showing each byte as a pair of digits to the screen... we'll need to start from the most significant byte, we do this with a LEAX command. Next we step through each byte, showing it to screen. |
![]() |
We can use this to show our results to the screen. |
![]() |
We can Compare by doing
repeated CMP commands - we just need to start from the most
significant byte. The first byte that is higher or lower will tell us which BCD value is the greater... if a byte is the same we need to move to the next. If we get to the end of the sequence, then both were the same |
![]() |
Here we compare two BCD Values | ![]() |
6809 DAA and Z80 DAA
are not the same! Z80 DAA is "Decimal Adjust Accumulator" and works for Addition and Subtraction 6809 DAA is "Decimal Adjust after Addition"... it only works for Addition, and don't go looking for a DAS command, there isn't one!... we'll have to trick our way to that sweet BCD subtraction! |
![]() |
Negativity!
We don't have a subtract command but we can add a 10's compliment
value Tens compliment of 1 = $99999999 - Val + 1 = $99999998 + 1 = $99999999 |
![]() ![]() |
Here is the results. |
![]() |