The Draw instruction allows the addition of bits to a bitmapped display.
The original Chip8 machines used byte oriented display where one bit could be on or off to indicate a lit pixel.
Emulators can either display their screens as an array of pixels or as a sequence of memory bytes.
The Draw instruction would then either read and Xor in bits from an image made of bytes or just directly store the pixels as elements in an internal screen array.
To emulate the original bitmapped machines, the bit per pixel method must be emulated.
DXYN - The Draw instruction.
X is the variable containing the x position.
Y is the variable containing the y position.
N is the number of bytes to be displayed.
The X Y co-ordinates start from 0, 0 and extend to the right and the bottom of the screen.
A graphic image created by the bytes can move off the right edge of the screen in partial steps so that only half of the graphic might be shown at the very right edge.
The same occurs for an image going below the bottom of the screen.
Generally an object being show at -1 ($FF) or less for x or y will not be shown unless an Image Wrapping option is turned on for an emulator.
Chip8 will not wrap the image drawn generally.
The bytes required are stored at a location pointed to by the Image Pointer.
Draw Instruction Modes
- The Chip8 Draw instruction can display from 1 to 16 ($1 to $F) graphics bytes of data.
- The SuperChip Draw instruction can display from 1 to 16 ($1 to $F) graphics bytes of data as normal.
- The SuperChip Draw instruction can also display a two byte image with a 16 pixel height when the Draw instruction specifies a zero for the n value.
- The MegaChip Draw instruction can display a multicolor sprite with a program definable width and height.
Drawing into a bitmapped memory
To emulate a chip8 machine using bitmapped memory, the memory for the x and y needs to be calculated.
The Y co-ordinate needs to be multiplied by the number of bytes across in the chip8 screen.
The X co-ordinate needs to be divided down so it is the number of bytes across.
The lower 3 bits of the x co-ordinate need to be used to calculate how many bits each image byte needs to be shifted right.
Each image byte needs to be shifted by the low bits of X.
Each shifted byte becomes a two byte word.
The right edge needs to be checked for so the image can slide off the edge of the screen smoothly.
So in the middle of the screen, the two bytes are written but at the edge only the first byte is written.