The Lush Engine








Introduction
Background
Math
Objects
Palettes
Introduction
colpal.asm
Tiles
Utilities

Colour Palette Setter

The following file is a routine to allow for easy setting of the gameboy colour palettes. You can download the file as well if you don't want to cut and paste the text.

colpal.asm

To download the file using a PC, right-click the link and select the option "Save Target As" then save the file as you would any other file from windows.

Explanation

The colour palettes on the Gameboy Colour take up two bytes, with 5 bits being used for each colour. This gives us a range of 0-31 for each colour. This can sometimes be hard to work out in your head, especially if you want to make a lot of changes whilst you are trying to find a colour you like.

This is why this routine was written. It accepts three seperate values for the Red, Green, and Blue colours, and mixes them together into the two bytes. It then places the data into the correct place for the palette you want to use.

The start of the routine sets the write specification data which is based on the palette and colour numbers.

The next part is the easiest, and simply places the red data into the first of the data bytes.

Green is slightly more complicated. What we have to do is place the right hand two bits of the data with the red data, and place the left hand three bits in the other data byte.

We do this by shifting the green data left five times, which means that the left hand three bits of data have been shoved off the left hand edge of the byte, and are therefore now ignored. A simple merging of this data with the red data finishes that byte of data.

Now we need to get the green data again, and shove it right three times, therefore losing the right three bits of data. The result is the start of the second data byte.

That was fairly painless, so now we move onto the blue data. We get the data, shove it left two places to stop it writing over the green data and then mix it with the green data.

That was all that we needed to do to set the actual colour data. Now we just need to write it to the correct place.

We check whether we are setting a background or object palette, and jump to the appropriate piece of code.

We write the write specification data that we set at the start of the routine. We then write the first byte of the colour data, increase the write specification data by one to show that we are going to write the second byte of colour data, and then actually write this data, and then the final byte of colour data.

Source

;*****************************************************************
; Procedure Name  : colour_palette
;
; Author          : Marc Gale (Xalthorn or Eldara)
;
; Description     : Set up a colour gameboy palette.  This routine
;                   can be made a little faster by taking the bit
;                   masking commands out (the AND instructions)
;                   and therefore trust the data sent to the 
;                   routine.
;
; On Entry        : B = Background or Object palette (0 or 1)
;                   C = Palette number (0-7)
;                   D = Colour number (0-3)
;                   E = Red value (0-31)
;                   H = Green value (0-31)
;                   L = Blue value (0-31)
;
; During          : All registers are used
;
; On Exit         : Nothing intentional
;*****************************************************************
colour_palette
        ld      a,c             ; get the palette number
        and     7               ; mask out all but the last 3 bits
        sla     a               ; shift the data left to place it 
        sla     a               ; in the correct position for the 
        sla     a               ; write specification byte
        ld      c,a             ; store the value in c
        ld      a,d             ; get the colour number
        and     3               ; mask out all but the last two bits
        sla     a               ; shift the data left one bit
        or      c               ; join the two values together
        ld      c,a             ; stick the write specification in c

        ld      a,e             ; get the red value
        and     31              ; mask out all but the last five bits
        ld      d,a             ; store the red value in d

        ld      a,h             ; get the green value
        and     31              ; mask out all but the last five bits
        ld      h,a             ; stick it back, we'll need it again 
        sla     a               ; shift the data left to place it in
        sla     a               ; the correct position
        sla     a
        sla     a
        sla     a
        or      d               ; join this data with the red data
        ld      d,a             ; store the low data byte in d
        ld      a,h             ; get the green value again
        srl     a               ; shift the data right this time
        srl     a
        srl     a
        ld      e,a             ; stick the data in e

        ld      a,l             ; get the blue value
        and     31              ; mask out all but the last five bits
        sla     a               ; shift the data left
        sla     a
        or      e               ; put the rest of the green with blue
        ld      e,a             ; store the high byte in e
; now, are we setting a background or object palette
        ld      a,b
        and     a               ; quicker way of doing cp 0
        jr      nz,setup_object_palette
setup_background_palette
        ld      hl,$ff68        ; point to the BG Write Spec. register
        ld      a,c             ; get the data for the palette selector
        ld      [hli],a         ; put the data in the register
        ld      a,d             ; get the LOW byte of the palette data
        ld      [hl],a          ; put it in the data register
        ld      hl,$ff68        ; point to the BG Write Spec. register
        ld      a,c             ; get the data for the palette selector
        inc     a               ; alter it to point to the HIGH byte
        ld      [hli],a         ; put the data in the register
        ld      a,e             ; get the HIGH byte of the palette data
        ld      [hl],a          ; put it in the data register
        ret                     ; we're done here, return
setup_object_palette
        ld      hl,$ff6a        ; point to the OBJ Write Spec. register
        ld      a,c             ; get the data for the palette selector
        ld      [hli],a         ; put the data in the register
        ld      a,d             ; get the LOW byte of the palette data
        ld      [hl],a          ; put it in the data register
        ld      hl,$ff6a        ; point to the OBJ Write Spec. register
        ld      a,c             ; get the data for the palette selector
        inc     a               ; alter it to point to the HIGH byte
        ld      [hli],a         ; put the data in the register
        ld      a,e             ; get the HIGH byte of the palette data
        ld      [hl],a          ; put it in the data register
        ret                     ; we're done here, return

Buttons created using the interactive button maker at Coolarchive