Learn Multi platform 6809 Assembly Programming... 8 bit resurrection!

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


Lesson M1 - Random Numbers and Ranges
Let's write some simple routines we may need in a game!

We'll write a psuedo random number generator, which can create repeatable 16 bit random numbers.
We'll also create a 'Range checker' which can be used as a collision detection routine for unsigned 8 bit numbers

VTX_SuckShoot.asm
V1_Random.asm


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.



Lesson M2 - Binary Coded Decimal
We're going to need to show some scores to the screen...
To give a nice looking score, we'll show 8 digits... and we'll need to add score, and keep a highscore.

Using Hexadecimal numbers is great for calculating, but is hard to show... the solution is Binary Coded Decimal!... each of the two digits in a HEX number only go from 0-9... so a byte goes from $00-$99... Lets learn more!

VTX_SuckShoot.asm
v1_BCD.asm



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.



Lesson M3 - The GenericAnimator code - Part 1
GenericAnimator is based on the Animation Scripts from ChibiAkumas episode 2.

They allow objects to follow simple movement sequences 'automatically' without extra program code or intervention.

V1_GenericAnimator.asm



GenericAnimator Introduction

GenericAnimator performs all the movement animations for the characters in the ChibiLife demo. Movement and sprite animation is all done by these GenericAnimator scripts

The Collision detections are handled by special code, but the GenericAnimator checks these collision flags are detected, and characters 'Turn round' automatically


A pointer to the object to be animated is passed to the animator code, and changes will be made to things like Xpos, Ypos and sprite number etc to 'automatically' animate the object.
Pointers for the animation scripts are held in a list.

The first entry is 'animator 1' as animator 0 is 'no animation'
Animation scripts start with a 1 byte 'Tick Mask'. This is ANDed with the current game tick - if the result is zero, the next step of the animation will occur.

Each line of an animation script is 4 bytes , the first byte is the 'command' the other three are parameters for that command.

Note that while a tick normally processes 1 line, it's possible to process extra lines per tick with the 'aEXT' flag.


Code - Init

At the start of the script processor we will be passed:

An animator number in z_b

An animator Frame/Tick in z_c (the line number we will process next in the script)

We back these up, then check the animator number - 0 Means no animation in which case we return.

We use GetAnimatorMemPos to calculate the address of this script, then load in the first byte of the animation script

This first byte is the 'Tick Mask' - it's ANDed with the current game tick, if the result is zero, the animation script processes a line.

BUT we can override this, and use a custom tick, according to the value in R3/R6



If the tick should occur this time, we run objectanimator_ProcessTick Which will process a line of the script

objectanimator_executetick take's the current script line number and multiplying it by four (as each line is 4 bytes)

We load in the command byte number, the bottom 4 bits are the command (16 commands total MAX) (The top bits are for conditionals and multiline support)

we use this to load a pointer in the animator_vectorarray which is the address of the command processor - we call it now


Code - Extended Commands

The first command is a special one!

EXT allows for extended commands. 'Extended commands' use the first parameter byte as a 'command number' - This allows more commands (an extra 255!), but allows only 0-2 parameters (rather than 0-3)

We load the command number and use it as an offset to the animator_vectorarrayext list



LoopToStart resets the frame/tick number back to 0


Halt stops the animation - by setting the animator number to 0
SetAnim sets the Animator and Frame/line (0=first frame).
vmexec Executes a call within the 'ChibiVM' virtual machine.

The address to call is in 16 bit little endian.
Call runs a call natively on the 6809 cpu.


As the destination address needs to be 32 bit, we load a 1 byte number from the command parameter, and use it as an offset in a call list
SwapIXIY allows a pointer to an alternate object to be used (IX and IY were registers on the Z80)

This should only be used as part of a multiline sequence! (using aEXT)



Lesson M4 - The GenericAnimator code - Part 2
GenericAnimator is based on the Animation Scripts from ChibiAkumas episode 2.

They allow objects to follow simple movement sequences 'automatically' without extra program code or intervention.

V1_GenericAnimator.asm


Commands
condjmp will jump to an alternative frame/tick based on a condition

condprocess sets the Carry flag according to a condition. objectanimator_condprocess will perform the comparison - The condition is selected by the top 3 bits of the command byte.

Parameter 1 is the offset in the object data to be tested/
Parameter 2 is the 'comparison value'

Parameter 3 is the new tick/frame for the animation script

The top 3 bits of the command byte define the condition.
condbra will branch to an alternative frame/tick based on a condition, This is a relaitve offset to the tick number, so allows a number of ticks to be 'skipped' and may be more useful than jump for skipping a few commands.


Parameter 1 is the offset in the object data to be tested/
Parameter 2 is the 'comparison value'

Parameter 3 is the new offset

The top 3 bits of the command byte define the condition.
load16 will load a 16 bit value into an offset of the objects variables. The data is in LITTLE ENDIAN format.

This is useful for changing a 16 bit sprite pointer.
load8 will load an 8 bit value into an offset of the objects variables.

This is useful for changing a Xpos or Ypos
load8dual Sets two separate 8 bit values.

The first byte is a pair of offsets (in the object data) - one is defined by the low nibble, the other by the high byte of the nibble.

This means only the first 16 bytes of the object data can be changed in this way.
add8dual Adds two separate 8 bit values.

The first byte is a pair of offsets (in the object data) - one is defined by the low nibble, the other by the high byte of the nibble.

This means only the first 16 bytes of the object data can be changed in this way.
Add16 will add a 16 bit number in LITTLE ENDIAN format
addmasked performs an add and mask operation

The first parameter is added to the destination
The second parameter is ANDed with the result.

By setting the AND mask to a value of #3 it would be possible to cycle between values 0,1,2,3 (maybe for toggling sprite frames)

By setting the AND mask to #255, this acts as as simple ADD
loadadd8dual Loads one 8 bit value to one object value, and adds another 8 bit value to another object value

The first byte is a pair of offsets (in the object data) - one is defined by the low nibble, the other by the high byte of the nibble.
condld8 will load a new 8 bit value to the object value if the condition is true.

Parameter 1 is the offset in the object data to be tested/changed
Parameter 2 is the 'comparison value'

Parameter 3 is the new value to set if the condition is true.


The top 3 bits of the command byte define the condition.
condadd8 will conditionally add an 8 bit value to the object value if the condition is true.

Parameter 1 is the offset in the object data to be tested/changed
Parameter 2 is the 'comparison value'

Parameter 3 is the new value to set if the condition is true.


The top 3 bits of the command byte define the condition.
condanim will conditionally switch to a new animator script (starts from frame 0)


Parameter 1 is the offset in the object data to be tested/changed
Parameter 2 is the 'comparison value'

Parameter 3 is the new animator to set if the condition is true.


The top 3 bits of the command byte define the condition.

Helper routines

objectanimator_update is executed after the command, Bit 5 of the original command number is tested, this is the 'EXTra line'  flag, which allows multiple lines to be processed in a single tick.
offsetdestnibble routines will set R5 to the object offsets according to two 4 bit offsets.

The top nibble can optionally switch to R8/IY as the source object, based on bit 7 of the command byte (used for conditions on some commands)
offsetdestfromhl will set R5 to an offset defined by a single byte.

There are two versions, one for conditions (which cannot swap IY) , and one for non-conditions (which can swap IY)


Condition processing

The conditions take a byte from the object data, and compare it to an immediate value parameter.
One of 8 possible conditions can be tested, decided by the top 3 bits of the command byte.
If the condition is true an action will be performed.

Here we load the parameters, and branch to the condition processing routines.
Condition type 7 is a special case, it's ANDed with the world tick. and is true if the result is zero.

This allows events to occur only rarely, for example anding with a value %00000010 would make something happen for two ticks, every two ticks.
We perform the required comparison and set the Carry flag according to the result.


Note we clear R4 once we're done - it contained the command before, but the top bit is used by some 'non conditional' commands for  IX-IY flip

 


View Options
Default Dark
Simple (Hide this menu)
Print Mode (white background)

Top Menu
***Main Menu***
Youtube channel
Patreon
Introduction to Assembly (Basics for absolute beginners)
AkuSprite Editor
ChibiTracker
Dec/Bin/Hex/Oct/Ascii Table

Alt Tech
Archive.org
Bitchute
Odysee
Rumble
DailyMotion
Please note: I wlll upload more content to these alt platforms based on the views they bring in

Z80 Content
***Z80 Tutorial List***
Learn Z80 Assembly (2021)
Learn Z80 Assembly (old)
Hello World
Simple Samples
Advanced Series
Multiplatform Series
Platform Specific Series
ChibiAkumas Series
Grime Z80
Z80 Downloads
Z80 Cheatsheet
Sources.7z
DevTools kit
Z80 Platforms
Amstrad CPC
Elan Enterprise
Gameboy & Gameboy Color
Master System & GameGear
MSX & MSX2
Sam Coupe
TI-83
ZX Spectrum
Spectrum NEXT
Camputers Lynx

6502 Content
***6502 Tutorial List***
Learn 6502 Assembly
Advanced Series
Platform Specific Series
Hello World Series
Simple Samples
Grime 6502
6502 Downloads
6502 Cheatsheet
Sources.7z
DevTools kit
6502 Platforms
Apple IIe
Atari 800 and 5200
Atari Lynx
BBC Micro
Commodore 64
Commodore PET
Commander x16
Super Nintendo (SNES)
Nintendo NES / Famicom
PC Engine (Turbografx-16)
Vic 20

68000 Content
***68000 Tutorial List***
Learn 68000 Assembly
Hello World Series
Platform Specific Series
Simple Samples
Grime 68000
68000 Downloads
68000 Cheatsheet
Sources.7z
DevTools kit
68000 Platforms
Amiga 500
Atari ST
Neo Geo
Sega Genesis / Mega Drive
Sinclair QL
X68000 (Sharp x68k)

8086 Content
Learn 8086 Assembly
Platform Specific Series
Hello World Series
Simple Samples
8086 Downloads
8086 Cheatsheet
Sources.7z
DevTools kit
8086 Platforms
Wonderswan
MsDos

ARM Content
Learn ARM Assembly
Learn ARM Thumb Assembly
Platform Specific Series
Hello World
Simple Samples
ARM Downloads
ARM Cheatsheet
Sources.7z
DevTools kit
ARM Platforms
Gameboy Advance
Nintendo DS
Risc Os

Risc-V Content
Learn Risc-V Assembly
Risc-V Downloads
Risc-V Cheatsheet
Sources.7z
DevTools kit

MIPS Content
Learn Risc-V Assembly
Platform Specific Series
Hello World
Simple Samples
MIPS Downloads
MIPS Cheatsheet
Sources.7z
DevTools kit
MIPS Platforms
Playstation
N64

PDP-11 Content
Learn PDP-11 Assembly
Platform Specific Series
Simple Samples
PDP-11 Downloads
PDP-11 Cheatsheet
Sources.7z
DevTools kit
PDP-11 Platforms
PDP-11
UKNC

TMS9900 Content
Learn TMS9900 Assembly
Platform Specific Series
Hello World
TMS9900 Downloads
TMS9900 Cheatsheet
Sources.7z
DevTools kit
TMS9900 Platforms
Ti 99

6809 Content
Learn 6809 Assembly
Learn 6309 Assembly
Platform Specific Series
Hello World Series
Simple Samples
6809 Downloads
6809/6309 Cheatsheet
Sources.7z
DevTools kit
6809 Platforms
Dragon 32/Tandy Coco
Fujitsu FM7
TRS-80 Coco 3
Vectrex

65816 Content
Learn 65816 Assembly
Hello World
Simple Samples
65816 Downloads
65816 Cheatsheet
Sources.7z
DevTools kit
65816 Platforms
SNES

eZ80 Content
Learn eZ80 Assembly
Platform Specific Series
eZ80 Downloads
eZ80 Cheatsheet
Sources.7z
DevTools kit
eZ80 Platforms
Ti84 PCE

IBM370 Content
Learn IBM370 Assembly
Simple Samples
IBM370 Downloads
IBM370 Cheatsheet
Sources.7z
DevTools kit

Super-H Content
Learn SH2 Assembly
Hello World Series
Simple Samples
SH2 Downloads
SH2 Cheatsheet
Sources.7z
DevTools kit
SH2 Platforms
32x
Saturn

PowerPC Content
Learn PowerPC Assembly
Hello World Series
Simple Samples
PowerPC Downloads
PowerPC Cheatsheet
Sources.7z
DevTools kit
PowerPC Platforms
Gamecube

Work in Progress
ChibiAndroids

Misc bits
Ruby programming









Buy my Assembly programming book
on Amazon in Print or Kindle!


Buy my Assembly programming book





Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!







































































































































Buy my Assembly programming book
on Amazon in Print or Kindle!


Buy my Assembly programming book





Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!







































































































































Buy my Assembly programming book
on Amazon in Print or Kindle!


Buy my Assembly programming book





Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!