;* ;* Set GameBoy Colo(u)r palettes ;* ;* v1.0 - Original ;* v1.1 - Fixed interrupt bugs ;* ;* Compiled with RGBASM ;* ; Note: ; Testing of register STAT is done to allow setting ; palettes while the screen is on or off. Without this ; test you can only reliably set the palettes with ; the screen off. There are probably many other methods ; for setting palettes with the screen on that are not ; covered here. ; ; Example for setting background palettes: ; ; ld hl,BGPaletteTable ; call SetAllCGB_BGP ; *** Set a single background palette *** ; Entry: HL = pntr to data for 1 palette ; A = palette number (0-7) SetSingleCGB_BGP: add a,a ; a = pal # * 2 add a,a ; a = pal # * 4 add a,a ; a = pal # * 8 add a,$80 ld [rBCPS],a ld bc,$0869 ; b = 8, c = rBCPD .loop1: di .loop2: ld a,[rSTAT] and 2 jr nz,.loop2 ld a,[hl+] ld [c],a ei dec b jr nz,.loop1 ret ; *** Set all background palettes *** ; Entry: HL = pntr to data for 8 palettes SetAllCGB_BGP: ld a,$80 ld [rBCPS],a ld bc,$4069 ; b = 64, c = rBCPD ,loop1: di .loop2: ld a,[rSTAT] and 2 jr nz,.loop2 ld a,[hl+] ld [c],a ei dec b jr nz,.loop1 ret ; *** Set a single object (sprite) palette *** ; Entry: HL = pntr to data for 1 palette ; A = palette number (0-7) SetSingleCGB_OBJP: add a,a ; a = pal # * 2 add a,a ; a = pal # * 4 add a,a ; a = pal # * 8 add a,$80 ld [rOCPS],a ld bc,$086b ; b = 8, c = rOCPD .loop1: di .loop2: ld a,[rSTAT] and 2 jr nz,.loop2 ld a,[hl+] ld [c],a ei dec b jr nz,.loop1 ret ; *** Set all object (sprite) palettes *** ; Entry: HL = pntr to data for 8 palettes SetAllCGB_OBJP: ld a,$80 ld [rOCPS],a ld bc,$406b ; b = 64, c = rOCPD .loop1: di .loop2: ld a,[rSTAT] and 2 jr nz,.loop2 ld a,[hl+] ld [c],a ei dec b jr nz,.loop1 ret ; This RGBSet Macro uses RGB values from 0 to 255. ; Even though this is a greater color range than the ; GBC supports it allows you to directly read color ; values from your favorite paint program using the ; eye dropper tool and drop them directly into your ; program. RGBSet: MACRO DW ((\3 >> 3) << 10) + ((\2 >> 3) << 5) + (\1 >> 3) ENDM ; ** Example colo(u)r palettes ** BGPaletteTable: RGBSet 255,255,255 ; White - Palette #1 RGBSet 191,191,191 ; Light Grey RGBSet 127,127,127 ; Dark Grey RGBSet 0,0,0 ; Black RGBSet 255,0,0 ; Red - Palette #2 RGBSet 0,255,0 ; Green RGBSet 0,0,255 ; Blue RGBSet 0,0,0 ; Black ; ........ RGBSet 255,255,0 ; Yellow - Palette #8 RGBSet 255,0,255 ; Purple RGBSet 127,127,127 ; Dark Grey RGBSet 0,0,0 ; Black OBJPaletteTable: RGBSet 0,0,0 ; Transparent (not used, value not important) RGBSet 191,191,191 ; Light Grey RGBSet 127,127,127 ; Dark Grey RGBSet 0,0,0 ; Black RGBSet 0,0,0 ; Transparent - Palette #2 RGBSet 0,255,0 ; Green RGBSet 0,0,255 ; Blue RGBSet 0,0,0 ; Black ; ........ RGBSet 0,0,0 ; Transparent - Palette #8 RGBSet 255,0,255 ; Purple RGBSet 127,127,127 ; Dark Grey RGBSet 0,0,0 ; Black ; ** end of file **