3. Mode 4 - screen memory usage

So, as I said above, we have two bits per pixel (or 8 pixels per memory word) in mode 4. How does this work? Mode 4 allows 4 colours, in binary the numbers from 0 to 3 can be represented by two bits. Colours are also represented by 'digits' in that if you add two colours together you get a different colour

The word in memory looks like this :

Table 8.1. Mode 4 Screen Memory Word Format

Green byte bits (even address)Red byte bits (odd address = green address + 1)
G7 G6 G5 G4 G3 G2 G1 G0R7 R6 R5 R4 R3 R2 R1 R0

In the above table, G7 refers to bit 7 of the green byte. The green byte is alwas even and lower in memory than the red byte which is always odd.

The colour codes for the allowed mode 4 colours are as follows :

Table 8.2. Mode 4 Colour Codes

ColourGR (Binary)Value (Decimal)
Black000
Red011
Green102
White113

So white is represented by both colours mixed together, black by the lack of both colours and red and green by themselves.

If in memory we have the green byte and the red byte in each word set up as follows, we can add the corresponding bit in each byte to represent the colour for a single pixel as follows :

Green byte = 0000 1111Red byte = 0101 0101

Which gives us the following :

BitValue (GR in Binary)Colour
700Black
601Red
500Black
401Red
310Green
211White
110Red
011White

And that is how it works in mode 4. Ok so we know the screen address (or do we? Think about it) and we know how to poke values into the correct location so we can now write directly to the screen can't we? More later, keep those brain cells ticking over for now. There is something I have not yet mentioned.