nesdev.com
http://forums.nesdev.com/

PPU TEXT WRITE
http://forums.nesdev.com/viewtopic.php?f=2&t=16074
Page 1 of 1

Author:  FlameCyclone [ Mon Jun 12, 2017 3:19 am ]
Post subject:  PPU TEXT WRITE

Code:
;[FC][PPU文本写入]
;FlameCyclone 2017.6.11

SEL_NUM = $40

 .ORG $A300

PPU_ADDR_TL = $70    ;PPU地址设置低位
PPU_ADDR_TH = $71    ;PPU地址设置高位
PPU_DATA_TL = $72    ;PPU数据源地址低位
PPU_DATA_TH = $73    ;PPU数据源地址低位
BUTTON_ONCE = $13    ;单次按键
BUTTON_UP   = $08    ;按键上键值
BUTTON_DOWN = $04    ;按键下键值
BUTTON_LEFT = $02    ;按键左键值
BUTTON_RIGHT = $01    ;按键右键值

;********************************************************
 PHA
 TXA
 PHA
 TYA
 PHA
 
 LDA #$00
 STA SEL_NUM
 LDA $2002
 LDA #$00
 STA $2001
 LDA #$00
 STA $8000
 LDA #$CC
 STA $8001
 
 LDX #$00
RAM_CLEARN:
 LDA #$F8
 STA $0200,X
 INX
 LDA #$00
 STA $0200,X
 INX
 LDA #$00
 STA $0200,X
 INX
 LDA #$F8
 STA $0200,X
 INX
 BNE RAM_CLEARN
 LDA #$02
 STA $4014
 
 JSR SPARITE_WRITE
 JSR BACKGROUND_PALETTES
 JSR SPARITE_LAYOUT
 JSR SCROLL_SET
 JSR PPU_LAYOUT
 JSR SCROLL_SET
 JSR PPU_GAME_NAME
 JSR SCROLL_SET
 LDA #$1E
 STA $2001
CYC:
 JSR SPARITE_MOVE
 JSR PAGE_SWITCH
 JSR SPARITE_SET
 JSR SCROLL_SET
 JSR FOUCTION_SWITCH
 JSR SWITHC_STATUS
 JSR BUT
 LDA $13
 CMP #$10
 BNE CYC
 
 PLA
 TAY
 PLA
 TAX
 PLA
 
 RTS

;********************************************************
PPU_ADDR_PROGRAM:    ;PPU地址处理程序
 LDA PPU_ADDR_TH
 STA $2006     ;设置PPU地址高位
 LDA PPU_ADDR_TL
 STA $2006     ;设置PPU地址低位
 RTS
 
PPU_ADDR_DATA:    ;PPU地址设置递增
 INC PPU_DATA_TL     ;PPU数据源低位递增
 BNE PPU_ADDR_DATA_RTS
 INC PPU_DATA_TH    ;PPU数据源高位递增
PPU_ADDR_DATA_RTS:
 RTS
 
;********************************************************
PPU_GAME_NAME:   ;PPU数据写入
 LDA #<GAME_NAME_DATA
 STA PPU_DATA_TL     ;设置PPU数据源低位
 LDA #>GAME_NAME_DATA
 STA PPU_DATA_TH     ;设置PPU数据源高位
 JSR PPU_WRITE
 RTS

;********************************************************
PPU_WRITE:    ;PPU写入
 LDX #$00
 LDY #$00
PPU_ADDR_SET:    ;PPU地址重设
 JSR PPU_ADDR_PROGRAM
PPU_ADDR_CMP:    ;重设地址判断
 LDA (PPU_DATA_TL),Y
 CMP #$FD         ;遇到0xFD则重设地址,地址为0xFD后的2字节
 BNE PPU_LINE_CMP
 JSR PPU_ADDR_DATA
 LDA (PPU_DATA_TL),Y
 STA PPU_ADDR_TH     ;设置PPU地址高位
 JSR PPU_ADDR_DATA
 LDA (PPU_DATA_TL),Y
 STA PPU_ADDR_TL     ;设置PPU地址低位
 JSR PPU_ADDR_DATA
 JMP PPU_ADDR_SET
PPU_LINE_CMP:    ;转行判断
 LDA (PPU_DATA_TL),Y
 CMP #$FE         ;遇到0xFE则转行,行开头偏移为0xFE后的1字节
 BNE PPU_DATA_WRITE
 JSR PPU_ADDR_DATA
LINE_OFFSET_0:    ;是否刚好在下一行0偏移处
 LDA PPU_ADDR_TL
 AND #$1F
 BEQ LINE_OFFSET
NEXT_LINE:    ;转到下一行
 LDA PPU_ADDR_TL
 AND #$E0
 CLC
 ADC #$20
 STA PPU_ADDR_TL     ;将PPU地址转到下一行
 BCC LINE_OFFSET
 INC PPU_ADDR_TH
LINE_OFFSET:    ;行开始偏移
 LDA PPU_ADDR_TL
 AND #$1F
 BNE PPU_LINE_OVER
 LDA (PPU_DATA_TL),Y    ;读取行开始偏移值
 CLC
 ADC PPU_ADDR_TL
 STA PPU_ADDR_TL     ;将PPU地址低位加上行开头偏移值
 BCC PPU_LINE_OVER
 INC PPU_ADDR_TH
PPU_LINE_OVER:    ;转行结束
 JSR PPU_ADDR_DATA
 JMP PPU_ADDR_SET
PPU_DATA_WRITE:    ;写入数据到PPU
 LDA (PPU_DATA_TL),Y    ;将PPU数据源写入PPU
 STA $2007
 INC PPU_ADDR_TL
 BNE PPU_H_NOP
PPU_H_ADD:    ;PPU地址高位递增
 INC PPU_ADDR_TH
PPU_H_NOP:
 JSR PPU_ADDR_DATA
PPU_OVER_CMP    ;结束判断
 LDA (PPU_DATA_TL),Y
 CMP #$FF         ;遇到0xFF则结束PPU写入
 BEQ PPU_WRITE_RTS
 JMP PPU_ADDR_CMP
PPU_WRITE_RTS:    ;返回
 RTS

GAME_NAME_DATA:
 .DB $FD,$20,$88,"FOUNCTION SWITCH"
 .DB $FD,$23,$42,"MADE BY FLAMECYCLONE IN 2017"
 .DB $FD,$20,$E6
 .DB "01.UNLIMITED JUMP",$FE,$26
 .DB "02.INVINCIBLE",$FE,$26
 .DB "03.TIME LOCK",$FE,$26
 .DB "04.BLOOD LOCK",$FE,$26
 .DB "05.SPEED MODE",$FE,$26
 .DB "06.KILL BY ONE",$FE,$26
 .DB "07.NINJATSU VALUE",$FE,$26
 .DB "08.STRONG SWORD",$FF

;********************************************************
SCROLL_SET:   ;滚动重设
 LDA SEL_NUM
 CMP MULTCARTTYPE
 BEQ LA
 BCC LA
 LDA MULTCARTTYPE
 STA SEL_NUM
LA:
 LDA $2002
 BPL LA
 LDA #$20
 STA $2006
 LDA #$00
 STA $2006
 LDA #$00
 STA $2005
 STA $2005
 RTS
 
;********************************************************
BACKGROUND_PALETTES:    ;背景调色板设置
 LDA #$3F
 STA $2006
 LDA #$00
 STA $2006
 LDX #$00
BACKGROUND_PALETTES_WRITE:
 LDA BPD,X
 STA $2007
 INX
 CPX #$0F
 BCC BACKGROUND_PALETTES_WRITE
 RTS

;********************************************************
SPARITE_LAYOUT:    ;精灵调色板设置
 LDA #$3F
 STA $2006
 LDA #$10
 STA $2006
 LDX #$00
SPD_WRITE:   
 LDA SPD,X
 STA $2007
 INX
 CPX #$0F
 BCC SPD_WRITE
 RTS
 
;********************************************************
PPU_LAYOUT:    ;调色板布局配置
 LDA #$23
 STA $2006
 LDA #$C0
 STA $2006
 LDX #$00
PPU_PL_WRITE:
 LDA PPU_LAYOUT_DATA,X
 STA $2007
 INX
 CPX #$40
 BNE PPU_PL_WRITE
 RTS
 
;********************************************************
SPARITE_SET:   ;精灵块设置
 LDA SEL_NUM
 CMP NUMBER_PAGE
 BCC SET_V
 LDA SEL_NUM
 SEC
 SBC NUMBER_PAGE
SET_V:
 ASL
 ASL
 ASL
 ASL
 CLC
 ADC SPARITE_DATA
 STA $0200
SPARITE_GPS:
 LDA #$02
 STA $4014
SPARITE_RTS:
 RTS
 
;********************************************************
SPARITE_WRITE:
 LDX #$00
 LDA #$00
 STA SEL_NUM
SPARITE_WRITE_DATA:
 LDA SPARITE_DATA,X
 STA $0200,X
 INX
 CPX #$04
 BCC SPARITE_WRITE_DATA
 RTS

;********************************************************
SPARITE_MOVE:    ;光标移动
SPA_UP:
 LDA BUTTON_ONCE
 CMP #BUTTON_UP
 BNE SPA_DN
 LDA SEL_NUM
 CMP #$00
 BNE UP1
 LDA MULTCARTTYPE
 STA SEL_NUM
 INC SEL_NUM
UP1:
 DEC SEL_NUM
SPA_DN:
 LDA BUTTON_ONCE
 CMP #BUTTON_DOWN
 BNE SPA_INC_RTS
 LDA SEL_NUM
 CMP MULTCARTTYPE
 BNE DN1
 LDA #$FF
 STA SEL_NUM
DN1:
 INC SEL_NUM
SPA_INC_RTS:
 RTS


;********************************************************
PAGE_SWITCH:
SPA_LT:
 LDA BUTTON_ONCE
 CMP #BUTTON_LEFT
 BNE SPA_RG
 LDA SEL_NUM
 CMP NUMBER_PAGE
 BCC LT_ADD
 LDA SEL_NUM
 SEC
 SBC NUMBER_PAGE
 STA SEL_NUM
 RTS
LT_ADD:
 LDA #$00
 STA SEL_NUM
 RTS
 
SPA_RG:
 LDA BUTTON_ONCE
 CMP #BUTTON_RIGHT
 BNE PAGE_RTS
 LDA SEL_NUM
 CMP NUMBER_PAGE
 BCS RG_ADD
 LDA SEL_NUM
 CLC
 ADC NUMBER_PAGE
 STA SEL_NUM
 RTS
RG_ADD:
 LDA MULTCARTTYPE
 STA SEL_NUM
 RTS
PAGE_RTS:
 RTS

PPU_LAYOUT_DATA:    ;属性表数据
 .BYTE $AA,$AA,$AA,$AA,$AA,$AA,$AA,$AA,$5A,$5A,$5A,$5A,$5A,$5A,$5A,$5A
 .BYTE $55,$55,$55,$55,$55,$55,$55,$55,$55,$55,$55,$55,$55,$55,$55,$55
 .BYTE $55,$55,$55,$55,$55,$55,$55,$55,$05,$05,$05,$05,$05,$05,$05,$05
 .BYTE $00,$00,$C0,$F0,$F0,$30,$00,$00,$00,$00,$CC,$FF,$FF,$33,$00,$00
 
BPD:    ;背景调色板数据
 .BYTE $0F,$24,$24,$24,$0F,$20,$20,$20,$0F,$27,$27,$27,$0F,$16,$16,$16
 
SPD:    ;精灵调色板数据
 .BYTE $0F,$2C,$2C,$2C,$0F,$27,$27,$27,$0F,$17,$17,$17,$0F,$07,$07,$07
 
SPARITE_DATA:    ;光标数据
 .BYTE $37,$1F,$00,$20

BUT:
 JSR $C67E
 LDA $12
 STA $2B
 JSR $C67E
 LDA $12
 CMP $2B
 BEQ LLL
 LDA $14
 STA $12
LLL:
 TAY
 EOR $14
 AND $12
 STA $13
 STY $14
 LDA #$80
 STA $15
 LDA $12
 STA $2B
 RTS
 
 
 
MULTCARTTYPE:    ;节目数量(0=1个节目,1=2个节目,0F=16个节目)
 .BYTE $07

NUMBER_PAGE:
 .BYTE $08


FOUCTION_SWITCH:
 LDX SEL_NUM
 LDA BUTTON_ONCE
 CMP #$40
 BNE OFF_BUT
 LDA $07FF
 ORA ON_DATA,X
 STA $07FF
OFF_BUT:
 LDA BUTTON_ONCE
 CMP #$80
 BNE SWITCH_RTS
 LDA $07FF
 AND OFF_DATA,X
 STA $07FF
SWITCH_RTS:
 RTS


ON_DATA:
 .DB $08,$04,$02,$01,$80,$40,$20,$10

OFF_DATA:
 .DB $F7,$FB,$FD,$FE,$7F,$BF,$DF,$EF

 
SWITHC_STATUS:
 LDY #$00
STATUS:
 TYA
 ASL
 ASL
 ASL
 ASL
 STA $41
 TYA
 ASL
 ASL
 ASL
 ASL
 TAX
 LDA $07FF
 AND ON_ORA,Y
 BEQ STATUS_OFF
STATUS_ON:
 JSR ON_ERITE
 JMP STATUS_INX
STATUS_OFF:
 JSR OFF_WRITE
STATUS_INX:
 INY
 CPY #$08
 BCC STATUS
STATUS_RTS:
 RTS
 
ON_ERITE:
 LDA #$37
 CLC
 ADC $41
 STA $0210,X
 STA $0214,X
 STA $0218,X
 LDA #$D0
 STA $0213,X
 LDA #$D8
 STA $0217,X
 LDA #$E0
 STA $021B,X
 LDA #$20
 STA $0211,X
 LDA #$4F
 STA $0215,X
 LDA #$4E
 STA $0219,X
 LDA #$00
 STA $0212,X
 STA $0216,X
 STA $021A,X
 RTS
 
OFF_WRITE:
 LDA #$37
 CLC
 ADC $41
 STA $0210,X
 STA $0214,X
 STA $0218,X
 LDA #$D0
 STA $0213,X
 LDA #$D8
 STA $0217,X
 LDA #$E0
 STA $021B,X
 LDA #$4F
 STA $0211,X
 LDA #$46
 STA $0215,X
 LDA #$46
 STA $0219,X
 LDA #$00
 STA $0212,X
 STA $0216,X
 STA $021A,X
 RTS

ON_ORA:
 .DB $08,$04,$02,$01,$80,$40,$20,$10
 
 

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/