VERA PALLETTE UPDATE USING VPOKE.

All aspects of programming on the Commander X16.
User avatar
ahenry3068
Posts: 1132
Joined: Tue Apr 04, 2023 9:57 pm

VERA PALLETTE UPDATE USING VPOKE.

Post by ahenry3068 »

I'm doing this in service of learning the VERA layout. I test my
understanding by seeing if my code works as expected. In this
case it did almost on the first try... Yahoo... Still a lot more
to learn.

Hope this might be useful to someone else struggling to understand the layout.
Its in BASIC so should be easily readable.

Code: Select all

REM DEMONSTRATES USING VPOKE TO CHANGE THE VERA PALLETTE COLORS
REM R%,G%,B%  HOLD R,G,B PALLETTE VALUES (0-15)
REM PC POINTS TO THE PALLETTE ENTRY (0-255)
REM
REM FOLLOWING CODE DRAWS A RECTANGLE IN COLOR $FF
REM THEN PALLETTE CHANGES THROUGH RED, GREEN AND BLUE.
REM THE PALLETTE CHANGING CODE IS A GOSUB AT LINE 20300

10 SCREEN $80
20 RECT 0,0,319,239,$10
30 RECT 50,50,90,90, $FF

35 PC = $FF: REM PC IS PALLETTE ENTRY TO CHANGE

REM RED PALLETTE
40 FOR I = 0 TO 15
50     R%=I:G%=0:B%=0
60     GOSUB 20300
70     SLEEP 5
80 NEXT I

REM GREEN PALLETTE
90 FOR I = 0 TO 15
95      R%=0:G%=I:B%=0
100     GOSUB 20300
105     SLEEP 5
110 NEXT I

REM BLUE PALLETTE
120 FOR I = 0 TO 15
130     R%=0:G%=0:B%=I
140     GOSUB 20300
145    SLEEP 5
150 NEXT I

REM GRAYSCALE
155 FOR I = 0 TO 15
160     R%=I:G%=I:B%=I
165     GOSUB 20300
170    SLEEP 5
175 NEXT I

190 INPUT X$
200 SCREEN 0:END

20300 IF R%>=0 AND R%<=15 AND G%>=0 AND G%<=15 AND B%>=0 AND B%<=15 THEN 20330
20310 PRINT "PALLETTE INVALID":END
20330 B1 = (G%*16) + B%  : REM FIRST BYTE HAS UPPER AND LOWER NIBBLE....   G & B
                             REM IF I WAS USING C IT WOULD BE ........ B1 =  (G << 4) | B)
20500 VPOKE 1,$FA00+(PC*2),B1 
20510 VPOKE 1,$FA00+((PC*2)+1),R%
20520 RETURN
User avatar
ahenry3068
Posts: 1132
Joined: Tue Apr 04, 2023 9:57 pm

Re: VERA PALLETTE UPDATE USING VPOKE.

Post by ahenry3068 »

To anyone else coming from DOS and the VGA 256 color screen. This is kinda opposite of the Pallette
order there. On IBM PC VGA the pallette entry is 24 bit .... R. G. B. 8 bit, 8 bit, 8 bit.
on the VERA its only 12 bit. Using a 16 bit pallette entry. Written in HEX its GB0R. Only the lower 4 bits of
the second byte is significant.
User avatar
ahenry3068
Posts: 1132
Joined: Tue Apr 04, 2023 9:57 pm

Re: VERA PALLETTE UPDATE USING VPOKE.

Post by ahenry3068 »

Ok.. Now I have code that doesn't work......... It appears I'm not getting the correct values
from VPEEK for the default Pallette. The code is meant to swap two Pallette entrys.
the one I've created from VPOKE works correctly. I'm not getting the correct Values from
the default Pallette.

YOU HAVE TO RUN THE CODE TO SEE WHAT I'M SAYING. AT THE END THE
TWO RECTANGLES SHOULD JUST SWAP COLORS.

Code: Select all

REM DEMONSTRATES USING VPOKE TO CHANGE THE VERA PALLETTE COLORS
REM R%,G%,B%  HOLD R,G,B PALLETTE VALUES (0-15)
REM PC POINTS TO THE PALLETTE ENTRY (0-255)
REM
REM FOLLOWING CODE DRAWS A RECTANGLE IN COLOR $FF
REM THEN PALLETTE CHANGES THROUGH RED, GREEN,BLUE AND GRAYSCALE
REM THE PALLETTE CHANGING CODE IS A GOSUB AT LINE 20300

10 SCREEN $80
20 RECT 0,0,319,239,$10
30 RECT 50,50,90,90, $FF
31 RECT 100,100,160,160, $3B


35 PC = $FF: REM PC IS PALLETTE ENTRY TO CHANGE

REM RED PALLETTE
40 FOR I = 0 TO 15
50     R%=I:G%=0:B%=0
60     GOSUB 20300
70     SLEEP 5
80 NEXT I

REM GREEN PALLETTE
90 FOR I = 0 TO 15
95      R%=0:G%=I:B%=0
100     GOSUB 20300
105     SLEEP 5
110 NEXT I

REM BLUE PALLETTE
120 FOR I = 0 TO 15
130     R%=0:G%=0:B%=I
140     GOSUB 20300
145    SLEEP 5
150 NEXT I

REM GRAYSCALE
155 FOR I = 0 TO 15
160     R%=I:G%=I:B%=I
165     GOSUB 20300
170    SLEEP 5
175 NEXT I
180 INPUT X$
190 P1 = $FF:P2=$3B
200 GOSUB 20750






290 INPUT X$
300 SCREEN 0:END

20300 IF R%>=0 AND R%<=15 AND G%>=0 AND G%<=15 AND B%>=0 AND B%<=15 THEN 20330
20310 PRINT "PALLETTE INVALID":END
20330 B1 = (G%*16) + B%
20500 VPOKE 1,$FA00+(PC*2),B1
20510 VPOKE 1,$FA00+((PC*2)+1),R%
20520 RETURN


REM SWAP 2 PALLETTE ENTRYS 
20750 A1 = $FA00+(P1*2)
20760 A2 = $FA00+(P2*2)
20770 B1 = VPEEK (1,A1)
20780 B2 = VPEEK (1,A1+1)
20790 B3 = VPEEK (1,A2)
20800 B4 = VPEEK (1,A2+1)
20810 VPOKE 1, A1, B3
20820 VPOKE 1, A1+1, B4
20830 VPOKE 1, A2, B1
20840 VPOKE 1, A2+1, B2
20850 RETURN 

User avatar
Daedalus
Posts: 229
Joined: Fri Nov 11, 2022 3:03 am

Re: VERA PALLETTE UPDATE USING VPOKE.

Post by Daedalus »

This will twist your noodle. The palette entries are not there by default unless you write them.

I know right? WUT? The default palette is not in the data you read, it's just random data at that point, the actual default palette is in VERA... You can write new values in... and they will stay there AND you can subsequently read them, but the initial data is random.

It's just a twist of how VERA stores and displays data. The short version is you need to write the palette values, even if you write the default value, before you can read the correct value.

Of course, it's been a while since I've messed with the palette... perhaps I've gone senile.
User avatar
Daedalus
Posts: 229
Joined: Fri Nov 11, 2022 3:03 am

Re: VERA PALLETTE UPDATE USING VPOKE.

Post by Daedalus »

Ah yes, here's the note in the docs that explain this:

Important note: Video RAM locations 1F9C0-1FFFF contain registers for the PSG/Palette/Sprite attributes. Reading anywhere in VRAM will always read back the 128kB VRAM itself (not the contents of the (write-only) PSG/Palette/Sprite attribute registers). Writing to a location in the register area will write to the registers in addition to writing the value also to VRAM. Since the VRAM contains random values at startup the values read back in the register area will not correspond to the actual values in the write-only registers until they are written to once. Because of this it is highly recommended to initialize the area from 1F9C0-1FFFF at startup.

The TL;DR of that is these values are read only in VERA, the mirror in the 128K RAM isn't initialized.
User avatar
ahenry3068
Posts: 1132
Joined: Tue Apr 04, 2023 9:57 pm

Re: VERA PALLETTE UPDATE USING VPOKE.

Post by ahenry3068 »

That explains it well.......... Thank you, I was kinda suspecting something like that.

I'm going to go ahead and say if they have a default Pallette it should be there when you
read the Pallette memory locations !!!!!
User avatar
ahenry3068
Posts: 1132
Joined: Tue Apr 04, 2023 9:57 pm

Re: VERA PALLETTE UPDATE USING VPOKE.

Post by ahenry3068 »

It should only be a dozen or so bytes in the Startup Code.
User avatar
ahenry3068
Posts: 1132
Joined: Tue Apr 04, 2023 9:57 pm

Re: VERA PALLETTE UPDATE USING VPOKE.

Post by ahenry3068 »

Is there anyway to read the Default Pallette Data ? Besides POKING it first ?
User avatar
ahenry3068
Posts: 1132
Joined: Tue Apr 04, 2023 9:57 pm

Re: VERA PALLETTE UPDATE USING VPOKE.

Post by ahenry3068 »

I guess I could load the default pallette and just POKE the whole thing before
any other code runs... !! McGyver R US
User avatar
ahenry3068
Posts: 1132
Joined: Tue Apr 04, 2023 9:57 pm

Re: VERA PALLETTE UPDATE USING VPOKE.

Post by ahenry3068 »

OK The $3B Pallette entry is Maximum Red.

Modified the code to just confirm that via POKE.

After that code works as expected. I guess if I pursue this in BASIC
I'll write a GOSUB that fills in all the Default Pallette Values
Not sure if thats necessary though.... Mostly I'll be working with
much less than 255 values

Code: Select all

REM DEMONSTRATES USING VPOKE TO CHANGE THE VERA PALLETTE COLORS
REM R%,G%,B%  HOLD R,G,B PALLETTE VALUES (0-15)
REM PC POINTS TO THE PALLETTE ENTRY (0-255)
REM
REM FOLLOWING CODE DRAWS A RECTANGLE IN COLOR $FF
REM THEN PALLETTE CHANGES THROUGH RED, GREEN,BLUE AND GRAYSCALE
REM THE PALLETTE CHANGING CODE IS A GOSUB AT LINE 20300

10 SCREEN $80
20 RECT 0,0,319,239,$10
30 RECT 50,50,90,90, $FF
31 RECT 100,100,160,160, $3B
32 PC=$3B
33 R%=15:G%=0:B%=0:GOSUB 20300



35 PC = $FF: REM PC IS PALLETTE ENTRY TO CHANGE

REM RED PALLETTE
40 FOR I = 0 TO 15
50     R%=I:G%=0:B%=0
60     GOSUB 20300
70     SLEEP 5
80 NEXT I

REM GREEN PALLETTE
90 FOR I = 0 TO 15
95      R%=0:G%=I:B%=0
100     GOSUB 20300
105     SLEEP 5
110 NEXT I

REM BLUE PALLETTE
120 FOR I = 0 TO 15
130     R%=0:G%=0:B%=I
140     GOSUB 20300
145    SLEEP 5
150 NEXT I

REM GRAYSCALE
155 FOR I = 0 TO 15
160     R%=I:G%=I:B%=I
165     GOSUB 20300
170    SLEEP 5
175 NEXT I
180 INPUT X$
190 P1 = $FF:P2=$3B
200 GOSUB 20750






290 INPUT X$
300 SCREEN 0:END

20300 IF R%>=0 AND R%<=15 AND G%>=0 AND G%<=15 AND B%>=0 AND B%<=15 THEN 20330
20310 PRINT "PALLETTE INVALID":END
20330 B1 = (G%*16) + B%
20500 VPOKE 1,$FA00+(PC*2),B1
20510 VPOKE 1,$FA00+((PC*2)+1),R%
20520 RETURN

20750 A1 = $FA00+(P1*2)
20760 A2 = $FA00+(P2*2)
20770 B1 = VPEEK (1,A1)
20780 B2 = VPEEK (1,A1+1)
20790 B3 = VPEEK (1,A2)
20800 B4 = VPEEK (1,A2+1)
20810 VPOKE 1, A1, B3
20820 VPOKE 1, A1+1, B4
20830 VPOKE 1, A2, B1
20840 VPOKE 1, A2+1, B2
20850 RETURN 











Post Reply