Classic Computer Magazine Archive COMPUTE! ISSUE 146 / NOVEMBER 1992 / PAGE G25

MOB Master. (computer program for movable object blocks)
by Hong Pham

Sprites (or movable object blocks) are large user-defined graphics that can be placed anywhere on your monitor's screen. The 128 has a powerful sprite controller that is built into its BASIC operating system to make sprite programming fairly easy. The 64, which has the same sprite capabilities as the 128, lacks the 128's sprite controller system. Programming sprites on a 64 usually requires many lines of code filled with awkward POKEs--but now there's MOB Master.

MOB Master gives the 64 many of the same features and sprite commands that are found on a 128. It also has extras, such as sprite animation and boundary-handling commands.

While this article explains how to use MOB Master's commands, it doesn't provide a complete tutorial for creating and using sprites. Programmers who already use sprites should have no trouble using MOB Master. Beginners can find more detailed descriptions of sprites and video banking in such reference books as Commodore 64 Programmer's Reference Guide or Mapping the Commodore 64.

Getting Started

MOB Master is written entirely in machine language. Use MLX, our machine language entry program, to type it in. If you don't have a copy of MLX, see "Typing Aids" elsewhere in this section. When MLX prompts, respond with the following values.

Starting address: 7D00 Ending address: 86EF

When you've finished typing in MOB Master, be sure to save it before exiting MLX.

To activate MOB Master, load it with the ,8,1 extension and then type SYS 32000. At this point you'll see a title screen that lets you know MOB Master has been activated. You may now begin writing your own sprite program. Instead of using cumbersome POKEs to control your sprites, however, you'll have a whole new library of commands at your disposal.

Ten Sprite Commands

MOB Master adds ten new BASIC commands for easier sprite definition, positioning, movement, animation, and other miscellaneous functions. The first three commands are similar to the 128's sprite commands of the same name.

Here's an important programming note to remember: When using a MOB Master command within a BASIC program, you must precede that command with a slash (/). In immediate mode, however, you don't need to use the slash.

SPRITE #, on/off, fgnd, priority, x-exp, y-exp, mode

The SPRITE command defines most of the characteristics of a sprite. Select the sprite number (#) with a value ranging from 0 to 7.

Use a 1 in the on/off parameter to turn on your sprite; use a 0 to turn it off.

The sprite foreground (fgnd) color is defined with a value between 0 and 16.

To make the sprite appear in front of objects on the screen, set its priority parameter to 0. To make it appear behind the objects on the screen, set the parameter to 1.

The sprite can expand to twice its original size horizontally (x-exp) or vertically (y-exp) by setting the next two parameters to 1. Set these parameters to 0 to turn off sprite expansion.

Turn on multicolor mode with a 1 or turn it off with a 0.

MOVSPR #, x, y

MOVSPR either positions or moves the sprite. The first example plots the sprite anywhere on the screen, with x being any pixel number between 0 and 319 and y any number between 0 and 199. Unlike normal sprite programming, MOB Master lets you place sprites beyond the 255th pixel without additional programming.

MOVSPR #, direction # speed

This variation moves the sprite in a specific direction and speed. The direction value can range from 0 to 255. This value can be converted to degrees by multiplying it by 45/32. To move the sprite up, use a value of 0. To move it to the right, use a value of 64. To move down, use 128. To move left, use 192. Intermediate values will move the sprite at different angles across the screen.

The value for speed can range from 0 to 255, with 0 being fastest and 254 being slowest. A value of 255 means that the sprite is stationary.

The format for this command is similar to that used for positioning a sprite, but instead of using a comma to separate the values, use the # sign. For example, MOVSPR 0, 64 # 100 would move sprite 0 to the right at a fairly slow speed.

SPRCOLOR color 1, color 2

In multicolor mode, the two multicolor colors are shared among all eight sprites. The first parameters defines multicolor 0, and the second parameter defines multicolor 1.

ANIMATE #, speed, mode, start frame, end frame

ANIMATE defines a sprite image or animates the sprite by successively changing its image pointers. The animation speed can range from 0 to 255, with 0 being fastest and 254 being slowest. A value of 255 means that the sprite has no animation. The mode parameter tells MOB Master how the sprite will be animated. A value of 0 means that the sprite will always be animated, and a 1 means that the sprite will be animated just once. Any other value will stop the sprite from being animated.

Sprite data resides in blocks of 64 bytes each. These blocks are numbered from 0-255. To calculate the location of a block of sprite data in memory, multiply the block number by 64. The result gives you the location where the first byte of a sprite definition should be poked. If you define several sprites whose shapes differ slightly and then switch rapidly among these blocks with the ANIMATE command, the sprite will appear to move in an animated fashion.

The start frame parameter indicates the first sprite image or block for animation. The end frame parameter indicates the ending block number for the animated sequence. Any sprite frames that are in between these will be automatically called.

BOUNDARY #, mode, top, bottom, left, right

Each sprite has its own individual screen boundaries. Once the sprite reaches a boundary that you set with a pixel number, the mode parameter indicates the action that the sprite will take. A 0 means that the sprite will wrap around and be placed on the opposite boundary. A 1 indicates that the sprite will bounce off the boundary. A 2 indicates that the sprite will stop at the boundary. Any other value indicates that the sprite will be turned off when it reaches a boundary, discontinuing motion.

For convenience, MOB Master allows only the horizontal boundary to be accurate to within two pixels. The actual boundary occurs on every even pixel. MOB Master will automatically divide the value that you have supplied with the boundary parameter by 2.

BOUNCE #, mode

BOUNCE bounces a sprite in a certain way, even if it's not at its boundary. Mode indicates how the sprite will bounce. A 0 argument means that the sprite will bounce vertically; a 1 indicates that the sprite will bounce laterally. Any other value will reverse the sprite's direction.

SPLIT mode

MOB Master supports two different raster-interrupt routines for flicker-free sprites. It accomplishes this task by updating its shadow registers when the raster scan is at a certain position on the screen. To select one of the two raster-interrupt routines, set mode to either 0 or 1. The only difference is that the latter routine allows you to display sprites on the top and bottom borders. If no argument follows SPLIT, it will turn off the raster-interrupt routine.

Before attempting an input or output operation, especially with a disk drive, it's best that you turn off the raster-interrupt routine. If you don't turn off the routine, the computer may freeze until you hit the Run/Stop and Restore keys simultaneously.

IRQ enable/disable

When you move multiple sprites as if they were one sprite, one sprite may move ahead of the others, creating a gap. This is because MOB Master updates the sprites 60 times a second, and BASIC may be too slow to move all the sprites before MOB Master updates them. One sprite may be updated before BASIC updates the others.

To temporarily stop MOB Master from updating the sprites, use IRQ 0. Any other value will allow MOB Master to continue updating the sprites. Be careful not to hold the system for too long, or the computer may hang up.


ZAP clears all the sprite registers.


KILL disables MOB Master and restores the previous interrupt and BASIC vectors.

Additional Notes

For all MOB Master statements, with the exception of IRQ, you can substitute an unknown parameter with an asterisk (*). You can also use the asterisk if you don't want to make any changes to the current parameter. You don't have to supply all of the parameters of the command, but you must denote the sprite number. You cannot substitute an asterisk for the sprite number.

Collision Detection

Sprite-to-sprite or sprite-to-background collisions can be monitored by using the USR command. To return the status of the last sprite-to-sprite collision, type in PRINT USR(0). Likewise, to return the status of the last sprite-to-background status, type PRINT USR(1).

Shadow Registers

MOB Master updates its shadow registers to the VIC-ll during a raster interrupt, or once every 1/60 of a second, to avoid sprite flickers. An advantage of this setup is that the sprites continue to move while your program does something else. You shouldn't make a direct POKE to the VIC-ll registers to define a sprite, because once a raster interrupt occurs, MOB Master overwrites the VIC-ll register with the contents of the shadow register. Therefore, poke to the shadow register instead. Below is the memory map of the shadow register and its VIC-ll equivalent. VIC-ll Equivalent Description Location Shadow Register (Base + offset) $D000 Base + 1312 Sprite 0 x position

(53248) $D001 Base + 1320 Sprite 0 y position

(53249) $D010 Base + 1328 Most significant bits of sprites 0-7 horizontal positions $D015 Base + 1329 Sprite enable (53269) register $D017 Base + 1330 Sprite Y-Expand (53271) register $D01D Base + 1331 Sprite X-Expand (53277) register $D01B Base + 1332 Sprite-to-foreground (53275) priority register $D01C Base + 1333 Sprite multicolor (53276) mode register $D025 Base + 1334 Sprite multicolor (53285) register 0 $D026 Base + 1335 Sprite multicolor (53286) register 1 $D027 Base + 1336 Sprite 0 color (53287) register $07F8 Base + 1344 Sprite shape (2040) data pointers. The actual location of this register depends on the location of the video matrix. The default base is $7D00 (32000).

MOB Master and Machine Language

MOB Master's sprite-handling ability is not restricted to BASIC programs. Machine language programmers will find MOB Master useful, as well. In fact, MOB Master and machine language are a great combination because you can do much more with machine language than you can with BASIC.

To make access to MOB Master's subroutines easier, MOB Master has a jump table. For all of MOB Master's subroutines, enter it with a JSR instruction, and use the X register to denote the sprite number. The following is the memory layout of the jump table. Location Description

(Base + offset) Base Enable MOB Master's BASIC interface. Base + 3 Enable raster-interrupt routine 1. Base + 6 Enable raster-interrupt routine 2. Base + 9 Disable raster-interrupt routine. Base + 12 Zap all sprite registers. Base + 15 Turn sprite on or off; C flag set = sprite is on. Base + 18 Position sprite at x, y. AC = LSB of x position; C flag = MSB of x position; YR = y position. Base + 21 Set sprite color; put sprite color in AC. Base + 24 Set sprite multicolor mode characteristics. C flag set = multicolor mode on. AC = multicolor 0; YR = multicolor 1. Base + 27 Set sprite to background priority; C flag set = background has priority. Base + 30 Set Y-expand; C flag set = expand sprite vertically. Base + 33 Set X-expand; C flag set = expand sprite horizontally. Base + 36 Set sprite speed; AC = sprite speed. Base + 39 Set boundary action mode (similar to BASIC BOUNDARY statement). Base + 42 Set sprite direction; AC = sprite direction. Base + 45 Set animation speed and mode. AC = animation speed; YR = mode. Base + 48 Set animation start and end image pointers. AC = start image location; YR = end image location. Base + 51 Set top and bottom borders. AC = top border; YR = bottom border. Base + 54 Set left and right borders. AC = left border; YR = right border. Note: Divide border value by 2. Base + 57 Bounce sprite vertically. Base + 60 Bounce sprite laterally. Base + 63 Reverse sprite direction.

Note: C flag = Carry flag, AC = Accumulator, XR = X register, YR = Y register If you're using MOB Master exclusively in machine language, you may delete the BASIC interface module starting at location $82CC (33484) or (base) + 1484 to $86EA 34538 or (base) + 2538.