Screen

The screen in Astro-8 is a 108x108 BGR555 display. This means that each pixel is represented by 15 bits, with 5 bits for each color channel (blue, green, and red).

Since the bit width of each color channel is 5 bits, the maximum value of each color channel is 31. This means that the maximum color value is 31, 31, 31 (white), and the minimum color value is 0, 0, 0 (black).

Every pixel is 1 uint16 in size, so the total size of the screen is 108 * 108 * 1 = 11664 uint16s. The default position of the screen is at address 53870 at bank 1 in memory, and ends at address 65534.

Changing the color of a pixel

There are multiple ways to change the color of a pixel. In this section, we will go over 2 ways to change the color of a pixel: using a function that converts RGB values to BGR555 values, and using a struct.

Using a function

In today standards, we're used to RGB values ranging from 0 to 255. To convert an RGB value to a BGR555 value, we can create a function that divides each color channel by 8 and shifts them to the correct position:

inline int get_color(int r, int g, int b) => (r / 8 << 10) + (g / 8 << 5) + (b / 8);

Then you can create a pointer to the screen and change the color of a pixel. The following example will set the first pixel to red:

var screen = create_pointer(53870, 1)

screen[0] = get_color(255, 0, 0)

inline int get_color(int r, int g, int b) => (r / 8 << 10) + (g / 8 << 5) + (b / 8);

Because we're using constant values, the compiler will optimize the function call to a constant value of 31744, which is the BGR555 value of red. However, if we're not using constant values, the CPU will have to calculate the BGR555 value every time we call the function, which is slower than using using the color range of 0 to 31 directly.

Using a struct

Another way to change the color of a pixel is to use a struct. First, you need to create a struct with 3 fields, one for each color channel:

struct Pixel {
    int b : 5
    int g : 5
    int r : 5
}

Then you can create a pointer to the screen:

var screen = create_pointer<Pixel>(53870, 1)

To change the color of a pixel, initialize the struct with the desired color values. The following example will set the first pixel to red:

var screen = create_pointer<Pixel>(53870, 1)

screen[0] = { b: 0, g: 0, r: 31 }

struct Pixel {
    int b : 5
    int g : 5
    int r : 5
}