;*****************************************************************
; 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