Single color fading in assembly

By BlueCrystal

Expert (113)

BlueCrystal's picture

19-08-2012, 22:02

I am trying to fade color 5 from white to black, but unfortunately my code apparently only fades the blue and green parts. The red part does note fade and is directly set to 0, atleast so it appears to me.

Where is my thinking flaw?

DB #FE
DW START,END,START

ORG #C000

START:
;ld a,5
;call #5F
call setpalet

Main:
ld bc,(palet.col5)
dec c ; green value decreased with 1

ld a,b
and 112 ;and 0111 0000, remove lower 4 bits
rra
rra
rra
rra
dec a
rla
rla
rla
rla
ld d,a ; red data decreased with 1 and stored in D

ld a,b
and 7 ; and 0000 0111 Keep first 3 bits
dec a
add a,d ; blue value decreased with 1 and stored in A
ld b,a

ld (palet.col5),bc ; send data back
ld a,c
or a
jp nz,.go
ld bc,#7707
ld (palet.col5),bc
.go:
call setpalet

SPC_Check:
halt
halt
halt
halt
halt
xor a
CALL #D8 ; Spacebar pressed?
jp z,Main

call #C0 ;beep
End_Rout:
ret ; Back to basic

setpalet:
di
xor a
out (#99),a
ld a,16+128 ; register 16 + set bit 7
out (#99),a
ei

ld bc,#209a ; 32 bytes naar #9a (b=#20, c=#9a)
ld hl,palet
otir

ret

; red_blue,green
palet:
.col0: db 0,0
.col1: db 0,0
.col2: db 0,0
.col3: db 0,0
.col4: db 0,0
.col5: dw #7707
.col6: db 0,0
.col7: dw #7707
.col8: db 0000_0111b,0
.col9: db 0,0
.col10: db 0000_0101b,0
.col11: db 0,0
.col12: db 0,0
.col13: db 0,0
.col14: db 0,0
.col15: db 0,5

END

Login or register to post comments

By Pat

Expert (71)

Pat's picture

19-08-2012, 22:38

Why are you using rla and rra for red? They both shift with carry.
To subtract 1 from the high nibble in the byte you might just substract 16?

By NYYRIKKI

Enlighted (6091)

NYYRIKKI's picture

19-08-2012, 23:06

You output green first while you should output red & blue first to VDP.

Quote:

Why are you using rla and rra for red? They both shift with carry.
To subtract 1 from the high nibble in the byte you might just substract 16?

The carry does not matter, but to do same effect I would say this should be enough:
LD HL,(palet.col5)
LD DE,#FEEF
ADD HL,DE
LD (palet.col5),HL

By NYYRIKKI

Enlighted (6091)

NYYRIKKI's picture

20-08-2012, 01:24

Problem with this "fade" is that it is not really fade, but a color rotation... When you fade, you usually want to fade in or fade out of some defined palette.

I wrote a simple example of real fade:

10 DEFINT A-Z
20 W=4 'Interrupts to wait I=Brightness
30 FOR I= 0 TO-7 STEP -1:A=USR(W*256+(IAND255)):NEXT I ' Fade from normal to black
40 FOR I=-7 TO 7 STEP  1:A=USR(W*256+(IAND255)):NEXT I ' Fade from black to white (trough normal)
50 FOR I= 7 TO 0 STEP -1:A=USR(W*256+(IAND255)):NEXT I ' Fade from white to normal

--------------------------------------------------------

JIFFY	EQU #FC9E
USR0	EQU #F39A
USR	EQU #F7F8

	DB #FE
	DW BEGIN,END,START
	ORG #C000

BEGIN:
PALETTE:  ; (This is MSX standard palette)
	DB #00,#00,#00,#00,#11,#06,#33,#07
	DB #17,#01,#27,#03,#51,#01,#27,#06
	DB #71,#01,#73,#03,#61,#06,#64,#06
	DB #11,#04,#65,#02,#55,#05,#77,#07
START:

	LD HL,DOFADE
	LD (USR0),HL
	RET

DOFADE:
	LD DE,(USR)
	EI
	LD HL,JIFFY
	LD A,(HL)
	ADD A,D
.WAIT
	CP (HL)
	JR NZ,.WAIT

FADE:
	; IN E=FADE (-7 ... +7)

	DI
	XOR A
	OUT (#99),A
	LD A,#90
	OUT (#99),A
	LD HL,PALETTE
	LD B,16
.LOOP
	LD A,(HL)
	CALL .CLIP	; Blue
	LD C,A
	LD A,(HL)
	RRCA
	RRCA
	RRCA
	RRCA
	CALL .CLIP	; Red
	RRCA
	RRCA
	RRCA
	RRCA
	OR C
	OUT (#9A),A
	INC HL
	LD A,(HL)
	CALL .CLIP	; Green
	OUT (#9A),A
	INC HL
	DJNZ .LOOP
	EI
	RET

.CLIP
	AND 7
	ADD A,E
	LD D,A
	AND 7
	CP D
	RET Z
	XOR A
	RLC D
	RET C
	LD A,7
	RET
END:

By BlueCrystal

Expert (113)

BlueCrystal's picture

20-08-2012, 20:42

Thanks, I got it working now Smile