# Tutorial — Background grid

    See also: 2636 PVI: Background grid
Programming the background grid is easy enough once it is understood, but its' design is somewhat convoluted and explaining it and a comprehending it is not easy. Refer back to the link above for the details, then follow along with this tutorial and play with the settings to test you understanding. Visualising what can and cannot be designed with it can be tricky though, so a worksheet is included that can be printed and doodled on.
It is really just an array of dots and vertical lines one pixel wide. The dot is two lines tall and immediately underneath it the vertical line is 18 lines tall. This pair of elements is repeated 16 times across the screen, and 10 times down, and all 320 individual parts can individually be turned on or off.
They can also be extended horizontally to 1, 2 4 or 8 pixels wide. By extending just the dots, a grid can be constructed. It is easy to see how something like a maze could be constructed from this by turning some of the parts off:
This pattern is generated by Tutorial code - background grid. Go ahead and assemble it with WinArcadia. Notice how there are lines sticking out the right side and the bottom. This is a consequence of the structure of the grid. To remove the lines on the right hand side, use the memory editor to change $1F81 from $FF to $FE, and repeat at every fourth byte. To remove the lines at the bottom, change $1FA6 and $1FA7 from $FF to $00. You should now have a nice 9 x 15 array of cells. There is no fix however for the small gaps down the right hand edge.
Now change $1F82 and $1F86 both to $9F, and $1F84 to $5F. You can probably see now how this could easily become a maze or some other similar structure.
So far this has been reasonably straightforward, but the horizontal extensions add significant complexity. The most important things to recall are that extensions apply to whole rows, and that 8x extensions can be applied to each individual row but x2 and x4 extensions are applied to groups of four rows. As a reminder, here is the table showing extension assignments:
Extension registers :
| Register | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| | Extend group x1,2,4 | Extend group x1,2,4 | Extend row x8 | Extend row x8 | Extend row x8 | Extend row x8 | Extend row x8 | Extend row x8 |
| $1FA8 | Rows 1-4 | Rows 1-4 | 4B | 4A | 3 | 2B | 2A | 1 |
| $1FA9 | Rows 5-8 | Rows 5-8 | 8B | 8A | 7 | 6B | 6A | 5 |
| $1FAA | Rows 9-12 | Rows 9-12 | 12B | 12A | 11 | 10B | 10A | 9 |
| $1FAB | Rows 13-16 | Rows 13-16 | 16B | 16A | 15 | 14B | 14A | 13 |
| $1FAC | Rows 17-20 | Rows 17-20 | 20B | 20A | 19 | 18B | 18A | 17 |
Group extension :
| Extension | 7 | 6 |
| x1 | 0 | 0 |
| x2 | 0 | 1 |
| x1 | 1 | 0 |
| x4 | 1 | 1 |
Currently all extension registers are set to $09, which only extends the odd numbered rows (the 2 line bars) to 8 pixels. Change extension register $1FA8 to $0B. This adds a x8 extension to all the active vertical bars in the top half of row 2.
Now change $1FA9 to $C9. This extends all the active bars in the second group to 4 pixels. Likewise, all active bars in the bottom group can extended to 2 pixels by setting register $1FAC to $49. You should now see this in the emulator screen:

## Quiz

There was a bug in the program when it was first run and the top part of the grid is corrupted. Can you tell which lines of code had to be added to the tutorial code to fix this?

## Grid design

This worksheet should help with the design and programming of background grids.
Remember these constraints:
* Extensions apply to all cells in a row.
* Extension to eight pixels may be applied individually to any of the 20 rows.
* Extensions to two or four pixels apply to all 6 rows in a group.
Don't forget that the 18-line vertical bars can be extended in two part, top and bottom.