Hi,
I made a routine to write the 256 colors in 16 palettes , (1FF Bytes , data is in $8200 to $83ff)
e2 20............SEP #20
9C 2121.........STZ $2121
AD 0082.........LDA $8200
8D 2221.........STA $2122
c2 20............REP #20
A9 0000.........LDA #0000
*
1A................INC
AA................TAX
e2 20.............SEP #20
BD 0082..........LDA $8200,X
8D 2221..........STA $2122
c2 20.............REP #20
8A.................TXA
C9 FF01..........CMP #1FF
D0 ee.............BNE *
Can we make it simpler (using less cycles) ?
Question about a routine and optimization
Moderator: Moderators
Forum rules
- For making cartridges of your Super NES games, see Reproduction.
Re: Question about a routine and optimization
INX exists
and
CPX also
use those instead, so you don't have to transfer back and forth between A and X
and instead of LDA #0000
you would use LDX #0000
and
CPX also
use those instead, so you don't have to transfer back and forth between A and X
and instead of LDA #0000
you would use LDX #0000
nesdoug.com -- blog/tutorial on programming for the NES
Re: Question about a routine and optimization
Also, the whole thing could be replaced with a DMA routine. Which I recommend.
nesdoug.com -- blog/tutorial on programming for the NES
Re: Question about a routine and optimization
Thanks Dougeff
With DMA is it that ?
LDA#02 STA$4300.....(writing by 2 octets)
LDA#22 STA$4301.....(choice $2122)
STZ$2121...............(start at colors 00)
LDA#00 STA$4304......(choice bank 00)
LDX#8200 STX$4302...($data)
LDX#0200 STX$4305...(number of bytes)
LDA#01 STA$420B......(launch transfert)
With DMA is it that ?
LDA#02 STA$4300.....(writing by 2 octets)
LDA#22 STA$4301.....(choice $2122)
STZ$2121...............(start at colors 00)
LDA#00 STA$4304......(choice bank 00)
LDX#8200 STX$4302...($data)
LDX#0200 STX$4305...(number of bytes)
LDA#01 STA$420B......(launch transfert)
Re: Question about a routine and optimization
my code is
DMA_Palette:
;copies the buffer to the CGRAM
php
A8
XY16
stz $2121 ;Palette Address
ldx #$2200 ;1 reg 1 write, to PAL_DATA 2122
stx $4300 ; and 4301
ldx #.loword(PAL_BUFFER)
stx $4302 ; and 4303
lda #^PAL_BUFFER ;bank #
sta $4304
ldx #$200 ;512 bytes
stx $4305 ; and 4306
lda #1
sta $420B ; DMA_ENABLE start dma, channel 0
plp
rts
where A8 is the same as sep #$20
and XY16 is the same as rep #$10
where it says
ldx #.loword(PAL_BUFFER)
you should use your own address of your palette data
ldx #$8200
and I'm not sure which bank that palette data is, but is that for the write to $4304
This writes zero to 4300, DMA mode (1 register, 1 write)
I believe it would also work if you wrote 2 to 4300 (1 register, 2 writes), like in your last post
make sure you have a dollar sign in front of the 200. You don't want 200 decimal, you want $200 hexadecimal (for size of transfer)
edit. maybe you are writing this in a hex editor, and not with an assembler. IDK. I was going with the assumption you were using an assembler, which is where the $ is important
DMA_Palette:
;copies the buffer to the CGRAM
php
A8
XY16
stz $2121 ;Palette Address
ldx #$2200 ;1 reg 1 write, to PAL_DATA 2122
stx $4300 ; and 4301
ldx #.loword(PAL_BUFFER)
stx $4302 ; and 4303
lda #^PAL_BUFFER ;bank #
sta $4304
ldx #$200 ;512 bytes
stx $4305 ; and 4306
lda #1
sta $420B ; DMA_ENABLE start dma, channel 0
plp
rts
where A8 is the same as sep #$20
and XY16 is the same as rep #$10
where it says
ldx #.loword(PAL_BUFFER)
you should use your own address of your palette data
ldx #$8200
and I'm not sure which bank that palette data is, but is that for the write to $4304
This writes zero to 4300, DMA mode (1 register, 1 write)
I believe it would also work if you wrote 2 to 4300 (1 register, 2 writes), like in your last post
make sure you have a dollar sign in front of the 200. You don't want 200 decimal, you want $200 hexadecimal (for size of transfer)
edit. maybe you are writing this in a hex editor, and not with an assembler. IDK. I was going with the assumption you were using an assembler, which is where the $ is important
nesdoug.com -- blog/tutorial on programming for the NES
Re: Question about a routine and optimization
Thanks,
Yes, I don't use assembler's software
dougeff wrote: ↑Sat Jun 03, 2023 1:04 pm
make sure you have a dollar sign in front of the 200. You don't want 200 decimal, you want $200 hexadecimal (for size of transfer)
edit. maybe you are writing this in a hex editor, and not with an assembler. IDK. I was going with the assumption you were using an assembler, which is where the $ is important
Yes, I don't use assembler's software
Re: Question about a routine and optimization
I think you should write color values to Ram buffer, then write a function that automatically transfer these values to CGram during NMI.
Rep #$30
Ldx #$01FE
loop:
Lda source, x
Sta destine, x
Dex #2
Bpl loop
Rep #$30
Ldx #$01FE
loop:
Lda source, x
Sta destine, x
Dex #2
Bpl loop
-
- Posts: 1565
- Joined: Tue Feb 07, 2017 2:03 am
Re: Question about a routine and optimization
There is no Immediate mode DEX it only decrements 1, so you need to
Code: Select all
dex
dex
Re: Question about a routine and optimization
Dex #2 means Dex and Dex. You can do this with xkas.