;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Atari 5200 bios disassembly
; done by Jindroush <jindroush@seznam.cz> in May 2002
;
; rev1	2002/05/08	Commented logo creation
;
; See http://jindroush.atari.org for more goodies
;
; This file should be assembled by XA assembler
; When _2PORT symbol defined, 2port binary is created, otherwise 4port one
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

VECTOR_TABLE_RAM = $0200
LOGO_DLIST_RAM1 = $2000
LOGO_DLIST_RAM2 = $2055
LOGO_DLIST_RAMI = $2007

;keypad equates
KEY52_0 = $00
KEY52_1 = $01
KEY52_2 = $02
KEY52_3 = $03
KEY52_4 = $04
KEY52_5 = $05
KEY52_6 = $06
KEY52_7 = $07
KEY52_8 = $08
KEY52_9 = $09
KEY52_ASTERISK = $0A
KEY52_HASH = $0B
KEY52_START = $0C
KEY52_PAUSE = $0D
KEY52_RESET = $0E
KEY52_NONE = $FF

CART_YEAR_LO =	$BFFD
CART_YEAR_HI =  $BFFC
CART_RUN_VECT=	$BFFE
CART_NAME =	$BFE8
CART_ISPAL = 	$BFE7

ANTIC_BASE =	$D400
POKEY_BASE =	$E800
GTIA_BASE =	$C000

;gtia
M0PF	=	$C000
HPOSP0	=	$C000
M1PF	=	$C001
HPOSP1	=	$C001
M2PF	=	$C002
HPOSP2	=	$C002
M3PF	=	$C003
HPOSP3	=	$C003
P0PF	=	$C004
HPOSM0	=	$C004
P1PF	=	$C005
HPOSM1	=	$C005
P2PF	=	$C006
HPOSM2	=	$C006
P3PF	=	$C007
HPOSM3	=	$C007
M0PL	=	$C008
SIZEP0	=	$C008
M1PL	=	$C009
SIZEP1	=	$C009
M2PL	=	$C00A
SIZEP2	=	$C00A
M3PL	=	$C00B
SIZEP3	=	$C00B
P0PL	=	$C00C
SIZEM	=	$C00C
P1PL	=	$C00D
GRAFP0	=	$C00D
P2PL	=	$C00E
GRAFP1	=	$C00E
P3PL	=	$C00F
GRAFP2	=	$C00F
TRIG0	=	$C010
GRAFP3	=	$C010
TRIG1	=	$C011
GRAFM	=	$C011
TRIG2	=	$C012
COLPM0	=	$C012
TRIG3	=	$C013
COLPM1	=	$C013
PAL	=	$C014
COLPM2	=	$C014
COLPM3	=	$C015
COLPF0	=	$C016
COLPF1	=	$C017
COLPF2	=	$C018
COLPF3	=	$C019
COLBK	=	$C01A
PRIOR	=	$C01B
VDELAY	=	$C01C
GRACTL	=	$C01D
HITCLR	=	$C01E
CONSOL	=	$C01F

;pokey
POT0	=	$E800
AUDF1	=	$E800
POT1	=	$E801
AUDC1	=	$E801
POT2	=	$E802
AUDF2	=	$E802
POT3	=	$E803
AUDC2	=	$E803
POT4	=	$E804
AUDF3	=	$E804
POT5	=	$E805
AUDC3	=	$E805
POT6	=	$E806
AUDF4	=	$E806
POT7	=	$E807
AUDC4	=	$E807
ALLPOT	=	$E808
AUDCTL	=	$E808
KBCODE	=	$E809
STIMER	=	$E809
RANDOM	=	$E80A
SKREST	=	$E80A
POTGO	=	$E80B
SERIN	=	$E80D
SEROUT	=	$E80D
IRQST	=	$E80E
IRQEN	=	$E80E
SKSTAT	=	$E80F
SKCTL	=	$E80F

;antic
DMACTL	=	$D400
CHACTL	=	$D401
DLISTL	=	$D402
DLISTH	=	$D403
HSCROL	=	$D404
VSCROL	=	$D405
PMBASE	=	$D407
CHBASE	=	$D409
WSYNC	=	$D40A
VCOUNT	=	$D40B
PENH	=	$D40C
PENL	=	$D40D
NMIEN	=	$D40E
NMIST	=	$D40F
NMIRES	=	$D40F

;shadow regs
POKMSK	=	$00
RTCLOKH	=	$01
RTCLOKL = 	$02
CRITIC  =	$03
ATRACT  =	$04
SDLSTL	=	$05
SDLSTH	=	$06
SDMCTL	=	$07
PCOLR0	=	$08
PCOLR1	=	$09
PCOLR2	=	$0A
PCOLR3	=	$0B
COLOR0	=	$0C
COLOR1	=	$0D
COLOR2	=	$0E
COLOR3	=	$0F
COLOR4	=	$10
PADDL0	=	$11
PADDL1	=	$12
PADDL2	=	$13
PADDL3	=	$14
PADDL4	=	$15
PADDL5	=	$16
PADDL6	=	$17
PADDL7	=	$18

;vectors
VIMIRQ	=	$200 ;immediate IRQ vector
VVBLKI	=	$202 ;immediate VBI
VVBLKD	=	$204 ;deferred VBI
VDSLST  =	$206 ;DLI vector
VKEYBD  =	$208 ;Keyboard IRQ vector
VKPD    =	$20A ;Keypad routine continuation vector
BRKKY   =	$20C ;BREAK key IRQ vector
VBREAK  =	$20E ;BRK instruction IRQ vector
VSERIN  =	$210 ;Serial Input Data Ready IRQ vector
VSEROR  =	$212 ;Serial Output Data Needed IRQ vector
VSEROC  =	$214 ;Serial Output Finished IRQ vector
VTIMR1  =	$216 ;POKEY Timer 1 IRQ vector
VTIMR2  =	$218 ;POKEY Timer 2 IRQ vector
VTIMR4  =	$21A ;POKEY Timer 4 IRQ vector

*=	$F800

CHARSET:
	.BYT $00, $00, $00, $00, $00, $00, $00, $00
	.BYT $00, $18, $18, $18, $18, $00, $18, $00
	.BYT $00, $66, $66, $66, $00, $00, $00, $00
	.BYT $00, $66, $FF, $66, $66, $FF, $66, $00
	.BYT $18, $3E, $60, $3C, $06, $7C, $18, $00
	.BYT $00, $66, $6C, $18, $30, $66, $46, $00
	.BYT $1C, $36, $1C, $38, $6F, $66, $3B, $00
	.BYT $00, $18, $18, $18, $00, $00, $00, $00
	.BYT $00, $0E, $1C, $18, $18, $1C, $0E, $00
	.BYT $00, $70, $38, $18, $18, $38, $70, $00
	.BYT $00, $66, $3C, $FF, $3C, $66, $00, $00
	.BYT $00, $18, $18, $7E, $18, $18, $00, $00
	.BYT $00, $00, $00, $00, $00, $18, $18, $30
	.BYT $00, $00, $00, $7E, $00, $00, $00, $00
	.BYT $00, $00, $00, $00, $00, $18, $18, $00
	.BYT $00, $06, $0C, $18, $30, $60, $40, $00
	.BYT $00, $3C, $66, $6E, $76, $66, $3C, $00
	.BYT $00, $18, $38, $18, $18, $18, $7E, $00
	.BYT $00, $3C, $66, $0C, $18, $30, $7E, $00
	.BYT $00, $7E, $0C, $18, $0C, $66, $3C, $00
	.BYT $00, $0C, $1C, $3C, $6C, $7E, $0C, $00
	.BYT $00, $7E, $60, $7C, $06, $66, $3C, $00
	.BYT $00, $3C, $60, $7C, $66, $66, $3C, $00
	.BYT $00, $7E, $06, $0C, $18, $30, $30, $00
	.BYT $00, $3C, $66, $3C, $66, $66, $3C, $00
	.BYT $00, $3C, $66, $3E, $06, $0C, $38, $00
	.BYT $00, $00, $18, $18, $00, $18, $18, $00
	.BYT $00, $00, $18, $18, $00, $18, $18, $30
	.BYT $06, $0C, $18, $30, $18, $0C, $06, $00
	.BYT $00, $00, $7E, $00, $00, $7E, $00, $00
	.BYT $60, $30, $18, $0C, $18, $30, $60, $00
	.BYT $00, $3C, $66, $0C, $18, $00, $18, $00
	.BYT $00, $3C, $66, $6E, $6E, $60, $3E, $00
	.BYT $00, $18, $3C, $66, $66, $7E, $66, $00
	.BYT $00, $7C, $66, $7C, $66, $66, $7C, $00
	.BYT $00, $3C, $66, $60, $60, $66, $3C, $00
	.BYT $00, $78, $6C, $66, $66, $6C, $78, $00
	.BYT $00, $7E, $60, $7C, $60, $60, $7E, $00
	.BYT $00, $7E, $60, $7C, $60, $60, $60, $00
	.BYT $00, $3E, $60, $60, $6E, $66, $3E, $00
	.BYT $00, $66, $66, $7E, $66, $66, $66, $00
	.BYT $00, $7E, $18, $18, $18, $18, $7E, $00
	.BYT $00, $06, $06, $06, $06, $66, $3C, $00
	.BYT $00, $66, $6C, $78, $78, $6C, $66, $00
	.BYT $00, $60, $60, $60, $60, $60, $7E, $00
	.BYT $00, $63, $77, $7F, $6B, $63, $63, $00
	.BYT $00, $66, $76, $7E, $7E, $6E, $66, $00
	.BYT $00, $3C, $66, $66, $66, $66, $3C, $00
	.BYT $00, $7C, $66, $66, $7C, $60, $60, $00
	.BYT $00, $3C, $66, $66, $66, $6C, $36, $00
	.BYT $00, $7C, $66, $66, $7C, $6C, $66, $00
	.BYT $00, $3C, $60, $3C, $06, $06, $3C, $00
	.BYT $00, $7E, $18, $18, $18, $18, $18, $00
	.BYT $00, $66, $66, $66, $66, $66, $7E, $00
	.BYT $00, $66, $66, $66, $66, $3C, $18, $00
	.BYT $00, $63, $63, $6B, $7F, $77, $63, $00
	.BYT $00, $66, $66, $3C, $3C, $66, $66, $00
	.BYT $00, $66, $66, $3C, $18, $18, $18, $00
	.BYT $00, $7E, $0C, $18, $30, $60, $7E, $00
	.BYT $00, $1E, $18, $18, $18, $18, $1E, $00
	.BYT $00, $40, $60, $30, $18, $0C, $06, $00
	.BYT $00, $78, $18, $18, $18, $18, $78, $00
	.BYT $00, $08, $1C, $36, $63, $00, $00, $00
	.BYT $00, $00, $00, $00, $00, $00, $FF, $00
	.BYT $00, $36, $7F, $7F, $3E, $1C, $08, $00
	.BYT $18, $18, $18, $1F, $1F, $18, $18, $18
	.BYT $03, $03, $03, $03, $03, $03, $03, $03
	.BYT $18, $18, $18, $F8, $F8, $00, $00, $00
	.BYT $18, $18, $18, $F8, $F8, $18, $18, $18
	.BYT $00, $00, $00, $F8, $F8, $18, $18, $18
	.BYT $03, $07, $0E, $1C, $38, $70, $E0, $C0
	.BYT $C0, $E0, $70, $38, $1C, $0E, $07, $03
	.BYT $01, $03, $07, $0F, $1F, $3F, $7F, $FF
	.BYT $00, $00, $00, $00, $0F, $0F, $0F, $0F
	.BYT $80, $C0, $E0, $F0, $F8, $FC, $FE, $FF
	.BYT $0F, $0F, $0F, $0F, $00, $00, $00, $00
	.BYT $F0, $F0, $F0, $F0, $00, $00, $00, $00
	.BYT $FF, $FF, $00, $00, $00, $00, $00, $00
	.BYT $00, $00, $00, $00, $00, $00, $FF, $FF
	.BYT $00, $00, $00, $00, $F0, $F0, $F0, $F0
	.BYT $00, $1C, $1C, $77, $77, $08, $1C, $00
	.BYT $00, $00, $00, $1F, $1F, $18, $18, $18
	.BYT $00, $00, $00, $FF, $FF, $00, $00, $00
	.BYT $18, $18, $18, $FF, $FF, $18, $18, $18
	.BYT $00, $00, $3C, $7E, $7E, $7E, $3C, $00
	.BYT $00, $00, $00, $00, $FF, $FF, $FF, $FF
	.BYT $C0, $C0, $C0, $C0, $C0, $C0, $C0, $C0
	.BYT $00, $00, $00, $FF, $FF, $18, $18, $18
	.BYT $18, $18, $18, $FF, $FF, $00, $00, $00
	.BYT $F0, $F0, $F0, $F0, $F0, $F0, $F0, $F0
	.BYT $18, $18, $18, $1F, $1F, $00, $00, $00
	.BYT $78, $60, $78, $60, $7E, $18, $1E, $00
	.BYT $00, $18, $3C, $7E, $18, $18, $18, $00
	.BYT $00, $18, $18, $18, $7E, $3C, $18, $00
	.BYT $00, $18, $30, $7E, $30, $18, $00, $00
	.BYT $00, $18, $0C, $7E, $0C, $18, $00, $00
	.BYT $00, $18, $3C, $7E, $7E, $3C, $18, $00
	.BYT $00, $00, $3C, $06, $3E, $66, $3E, $00
	.BYT $00, $60, $60, $7C, $66, $66, $7C, $00
	.BYT $00, $00, $3C, $60, $60, $60, $3C, $00
	.BYT $00, $06, $06, $3E, $66, $66, $3E, $00
	.BYT $00, $00, $3C, $66, $7E, $60, $3C, $00
	.BYT $00, $0E, $18, $3E, $18, $18, $18, $00
	.BYT $00, $00, $3E, $66, $66, $3E, $06, $7C
	.BYT $00, $60, $60, $7C, $66, $66, $66, $00
	.BYT $00, $18, $00, $38, $18, $18, $3C, $00
	.BYT $00, $06, $00, $06, $06, $06, $06, $3C
	.BYT $00, $60, $60, $6C, $78, $6C, $66, $00
	.BYT $00, $38, $18, $18, $18, $18, $3C, $00
	.BYT $00, $00, $66, $7F, $7F, $6B, $63, $00
	.BYT $00, $00, $7C, $66, $66, $66, $66, $00
	.BYT $00, $00, $3C, $66, $66, $66, $3C, $00
	.BYT $00, $00, $7C, $66, $66, $7C, $60, $60
	.BYT $00, $00, $3E, $66, $66, $3E, $06, $06
	.BYT $00, $00, $7C, $66, $60, $60, $60, $00
	.BYT $00, $00, $3E, $60, $3C, $06, $7C, $00
	.BYT $00, $18, $7E, $18, $18, $18, $0E, $00
	.BYT $00, $00, $66, $66, $66, $66, $3E, $00
	.BYT $00, $00, $66, $66, $66, $3C, $18, $00
	.BYT $00, $00, $63, $6B, $7F, $3E, $36, $00
	.BYT $00, $00, $66, $3C, $18, $3C, $66, $00
	.BYT $00, $00, $66, $66, $66, $3E, $0C, $78
	.BYT $00, $00, $7E, $0C, $18, $30, $7E, $00
	.BYT $00, $18, $3C, $7E, $7E, $18, $3C, $00
	.BYT $18, $18, $18, $18, $18, $18, $18, $18
	.BYT $00, $7E, $78, $7C, $6E, $66, $06, $00
	.BYT $08, $18, $38, $78, $38, $18, $08, $00
	.BYT $10, $18, $1C, $1E, $1C, $18, $10, $00

MAIN_IRQ:	JMP	(VIMIRQ)	;main irq vector

IRQ_IMM:	PHA
		LDA	#$20
		BIT	IRQST
		BNE	IRQ_T_2
		LDA	#$DF
		STA	IRQEN
		LDA	POKMSK
		STA	IRQEN
		JMP	(VSERIN)

IRQ_T_2:	BPL	IRQ_T_8
		BVC	IRQ_T_9
		LDA	#$10
		AND	IRQST
		BNE	IRQ_T_3
		LDA	#$EF
		STA	IRQEN
		LDA	POKMSK
		STA	IRQEN
		JMP	(VSEROR)

IRQ_T_3:	LDA	#$08
		AND	POKMSK
		BEQ	IRQ_T_4
		AND	IRQST
		BNE	IRQ_T_4
		LDA	#$F7
		STA	IRQEN
		LDA	POKMSK
		STA	IRQEN
		JMP	(VSEROC)

IRQ_T_4:	LDA	IRQST
		ROR
		BCS	IRQ_T_5
		LDA	#$FE
		STA	IRQEN
		LDA	POKMSK
		STA	IRQEN
		JMP	(VTIMR1)

IRQ_T_5:	ROR
		BCS	IRQ_T_6
		LDA	#$FD
		STA	IRQEN
		LDA	POKMSK
		STA	IRQEN
		JMP	(VTIMR2)

IRQ_T_6:	ROR
		BCS	IRQ_T_7
		LDA	#$FB
		STA	IRQEN
		LDA	POKMSK
		STA	IRQEN
		JMP	(VTIMR4)

IRQ_T_7:	TXA
		PHA
		TSX
		LDA	$0103,X		;check flags on stack if B is on
		AND	#$10
		BEQ	IRQ_OUT		;not
		JMP	(VBREAK)	;go to BRK vector

IRQ_T_8:	LDA	#$7F
		STA	IRQEN
		LDA	POKMSK
		STA	IRQEN
		JMP	(BRKKY)		;go to BREAK key vector (upper btn)

IRQ_T_9:	LDA	#$BF
		STA	IRQEN
		LDA	POKMSK
		STA	IRQEN
		JMP	(VKEYBD)	;go to keyboard vector

MAIN_NMI:	BIT	NMIST  		;check two upper bits of NMIST
		STA	NMIRES 		;clear NMI flag
		BMI	GO_DLI		;if bit 7
		BVC	NMI_EXIT	;if not bit 6
GO_VBI:		JMP	(VVBLKI)	;do VBLANK
GO_DLI:		JMP	(VDSLST)	;do DLI

DEF_INTR_OUT:	PLA
		TAY
IRQ_OUT:	PLA
		TAX
		PLA
NMI_EXIT:	RTI

VBL_IMM:	PHA
		TXA
		PHA
		TYA
		PHA
		INC	RTCLOKL		;increment clock-lo
		BNE	SHCLK
		INC	ATRACT		;increment atract timer
		INC	RTCLOKH		;increment clock-hi
SHCLK:		LDA	CRITIC		;if CRITIC set
		BNE	DEF_INTR_OUT	;go out
		LDA	SDLSTH		;copy DLIST vec from shadow
		STA	DLISTH
		LDA	SDLSTL
		STA	DLISTL
		LDA	SDMCTL		;copy DMACTL from shadow
		STA	DMACTL
		LDY	ATRACT		;check atract mode
		BPL	NO_ATTR		;no atract
		LDY	#$80
		STY	ATRACT
NO_ATTR:	LDX	#$08
VBI_SHCOL:	LDA	PCOLR0,X	;read color shadows
		CPY	#$80
		BCC	NO_ATTR2	;no atract
		EOR	RTCLOKH		;xor with clock-hi if atract
		AND	#$F6
NO_ATTR2:	STA	COLPM0,X	;store to hw regs
		DEX
		BPL	VBI_SHCOL	;loop for all of them
		LDX	#$07
SHPOTS:		LDA	POT0,X		;shadow pot registers
		STA	PADDL0,X
		DEX
		BPL	SHPOTS		;loop
		STA	POTGO		;clear pots
		JMP	(VVBLKD)	;end vblank

KBD_IRQ:	TXA
		PHA
		TYA
		PHA
		LDA	KBCODE		;read hw reg
		LSR
		AND	#$0F
		TAX
		LDA	KBTBL,X		;convert to key value
		JMP	(VKPD)		;continue in keypad irq

KBTBL:		.BYT	KEY52_NONE
		.BYT	KEY52_HASH
		.BYT	KEY52_0
		.BYT	KEY52_ASTERISK
		.BYT	KEY52_RESET
		.BYT	KEY52_9
		.BYT	KEY52_8
		.BYT	KEY52_7
		.BYT	KEY52_PAUSE
		.BYT	KEY52_6
		.BYT	KEY52_5
		.BYT	KEY52_4
		.BYT	KEY52_START
		.BYT	KEY52_3
		.BYT	KEY52_2
		.BYT	KEY52_1

MAIN_RESET:	SEI
		CLD
		LDX	#$FF
		TXS
		LDA	CART_YEAR_LO	;check if year is $ff
		CMP	#$FF
		BNE	NO_DIAG
		JMP	(CART_RUN_VECT)	;diagnostic cart, run it (no logo)

NO_DIAG:

#ifdef _2PORT
		;contained only in 2PORT osrom
		;means
		;if you have NTSC machine, no checks are done, all
		;carts run
		;if you have PAL machine, cartridge must have in CART_ISPAL
		;values $15 or $30 or bit 2 set
		;otherwise system will hang (before the logo is displayed)

		LDA	PAL		;is PAL gtia?
		AND	#$0E
		BNE	CLN_LOOP	;NTSC
		LDA	CART_ISPAL	;check ispal in headers
		CMP	#$15		;magic value
		BEQ	CLN_LOOP	;let it go
		CMP	#$30		;magic value
		BEQ	CLN_LOOP	;let it go
		AND	#$02		;if bit 2 is not set on cart
DYN_HALT:	BEQ	DYN_HALT	;halt the machine
#endif

CLN_LOOP:	INX			;clear all registers
		TXA
CLEAN_IC:	STA	POKEY_BASE,X
		STA	GTIA_BASE,X
		STA	ANTIC_BASE,X
		STA	$00,X
		INX
		BNE	CLEAN_IC

		LDA	#>CHARSET	;set character table
		STA	CHBASE

		LDX	#$0B		;copy vector values
CPY_VECTORS:	LDA	VECTOR_TABLE_ROM,X
		STA	VECTOR_TABLE_RAM,X
		DEX
		BPL	CPY_VECTORS

DRAW_LOGO		    		
		LDA	#$3C		;clear graphics memory
		STA	$12
		LDA	#$00
		STA	$11
		LDX	#$0C
		TAY
DLG_CGM:	STA	($11),Y
		DEY
		BNE	DLG_CGM
		DEC	$12
		DEX
		BPL	DLG_CGM

					
		LDA	#$0D		;create display list
		LDX	#$4D
DLIST_STOREI:	STA	LOGO_DLIST_RAMI,X
		DEX
		BPL	DLIST_STOREI

		LDX	#$06
DLIST_CPY1:	LDA	LOGO_DLIST_ROM1,X
		STA	LOGO_DLIST_RAM1,X
		DEX
		BPL	DLIST_CPY1
		LDX	#$04
DLIST_CPY2:	LDA	LOGO_DLIST_ROM2,X
		STA	LOGO_DLIST_RAM2,X
		DEX
		BPL	DLIST_CPY2

					;set dlist in registers
		LDA	#<LOGO_DLIST_RAM1
		STA	SDLSTL
		LDA	#>LOGO_DLIST_RAM1
		STA	SDLSTH
		LDA	#$22
		STA	SDMCTL 		;normal playfield


		;this creates table of starts of $37 lines
CR_LGO_TBL:	LDA	#$30
		TAY
		LDA	#$28		;start at $3028
		LDX	#$36		;make $37 values
CR_DATA:	STA	$1100,X		;lo byte of address
		PHA
		TYA
		STA	$1000,X		;hi byte of address
		PLA
		DEX
		BMI	LGO_DRAW
		CLC			;move to next line
		ADC	#$28
		BCC	CR_DATA		;don't increment hi
		INY			;increment hi
		BCS	CR_DATA

		;draws atari logo
		;size is $37 lines
		;$20 columns
		;logo is drawn column by column (bit by bit)
		;it's written symmetrically, one left & one right pixel at
		;once

LGO_DRAW	LDX	#$13		;left byte
		STX	$17
		INX
		STX	$18		;right byte
		LDA	#$20
		STA	$13		;number of columns

DLG_NEXTCOL2:	LDA	#$01		;bits 1-0 (left)
		STA	$15
		LDA	#$40		;bits 7-6 (right)
		STA	$16

DLG_NEXTCOL:	DEC	$13		;dec column number
		BMI	AT_DRAW
		LDX	$13
		LDA	LGO_COLEND,X	;get end line of column
		STA	$14
		LDA	LGO_COLSTRT,X	;get start line of column
		TAX
DLG_DRCOL:	CPX	$14		;compare if end
		BEQ	DLG_COLEND
		LDA	$1100,X		;get display address
		STA	$11
		LDA	$1000,X
		STA	$12

		LDY	$17		;left pixel position
		LDA	$15		;left pixel value
		ORA	($11),Y
		STA	($11),Y		;or it to the position
		LDY	$18		;right pixel position
		LDA	$16		;right pixel value
		ORA	($11),Y
		STA	($11),Y
		INX
		BNE	DLG_DRCOL
DLG_COLEND:	ASL	$15		;shift left pixel value
		ASL	$15		;twice -> 2 bits of color value
		BCS	DLG_MOVECOL	;move to next column
		LSR	$16		;shift right pixel value
		LSR	$16
		BCC	DLG_NEXTCOL	;draw next column
DLG_MOVECOL:	DEC	$17		;dec left pixel position
		INC	$18		;inc right pixel position
		BCS	DLG_NEXTCOL2

		;this draws atari name
		;draws by one line - data are stored in LGO_ATNAME
		;and each line/block is ended by zero
		;data are interpreted like this:
		;upper nibble + 1 -> number of black pixels
		;lower nibble + 1 -> number of white pixels

AT_DRAW:	LDA	#$11		;start from $3911
		STA	$11
		LDA	#$39
		STA	$12

		LDA	#$13		;number of data blocks = lines
		STA	$13		;i.e. $14 = 20

		LDA	#$00		;data pointer start value
		STA	$15

DLG_NEXTBLK:	LDA	#$01		;initial byte value
		LDY	#$0A		;initial column value
DLG_LINECONT:	STA	$18		;store last byte value

		LDX	$15		;get pointer
		INC	$15		;increment it
		LDA	LGO_ATNAME,X	;read data
		BEQ	DLG_ENDBLK	;is 0? -> end of this block
		TAX
		AND	#$0F
		STA	$16		;store lower nibble (white)
		TXA
		LSR
		LSR
		LSR
		LSR
		TAX			;upper nibble into X (black)
		LDA	$18		;get last byte value
DLG_BLK:	ASL			;shift left twice
		ASL
		BCC	DLG_BLKNOST	;not yet whole byte
		STA	($11),Y		;store byte
		INY			;move to next
		LDA	#$01		;restore byte value
DLG_BLKNOST:	DEX			;decrement
		BPL	DLG_BLK		;draw next black pix

		LDX	$16		;get number of white bits
DLG_WHI:	SEC			;shift left twice and
		ROL			;create white pixel
		ASL
		BCC	DLG_WHINOST	;not yet whole byte
		STA	($11),Y		;store byte
		INY			;move to next
		LDA	#$01		;restore byte value
DLG_WHINOST:	DEX			;decrement
		BPL    	DLG_WHI		;draw next white pix
		BMI	DLG_LINECONT	;continue this line

DLG_ENDBLK:	LDA	$18		;get last byte value
DLG_EFBIT:	ASL			;create whole byte
		ASL
		BCC	DLG_EFBIT
		STA	($11),Y		;store last byte

		LDA	$11		;move to next line
		CLC
		ADC	#$28
		STA	$11
		BCC	LINE_HI_NOINC
		INC	$12
LINE_HI_NOINC:	DEC	$13		;compare if last block/line
		BPL	DLG_NEXTBLK	;go to next block

		LDX	#$13		;copy 'copyright atari'
TXT_CP1:	LDA	TXT_COPY_ROM,X
		STA	$3C94,X
		LDA	CART_NAME,X	;copy cart name
		STA	$3C80,X
		DEX
		BPL	TXT_CP1
		LDA	CART_YEAR_HI	;copy cart date
		STA	$3CA0
		LDA	CART_YEAR_LO
		STA	$3CA1

		LDA	#$0F		;set color
		STA	COLOR1
		LDA	#$C0		;enable nmi
		STA	NMIEN
		LDA	#$02
		STA	SKCTL		;???
LOGO_WAIT:	CPX	RTCLOKL		;wait a bit
		BNE	LOGO_WAIT
		JMP	(CART_RUN_VECT)	;run cart

VECTOR_TABLE_ROM:
		.WORD	IRQ_IMM		;200 VIMIRQ
		.WORD	VBL_IMM		;202 VVBLKI
		.WORD	DEF_INTR_OUT	;204 VVBLKD
		.WORD	DEF_DLI		;206 VDSLST
		.WORD	KBD_IRQ		;208 VKEYBD
		.WORD	DEF_INTR_OUT	;20A VKPD

DEF_DLI:	PHA			;dli for creating the rainbow
		TXA			;effect in logo
		PHA
		TYA
		PHA
		LDX	PCOLR0
		LDY	#$72
DD_LOOP		CPX	#$10
		BCS	DD_NOT_YET
		LDX	#$FE
DD_NOT_YET	STX	WSYNC
		STX	COLPF0
		DEX
		DEX
		DEY
		BNE	DD_LOOP
		INC	PCOLR0
		LDY	#$10
		CPY	PCOLR0
		BCC	DD_GO_OUT
		STY	PCOLR0
DD_GO_OUT:	JMP	DEF_INTR_OUT

LOGO_DLIST_ROM1:.BYT	$70
		.BYT	$70
		.BYT	$70
		.BYT	$4d
		.WORD	$3000
		.BYT	$8d

LOGO_DLIST_ROM2:.BYT	$07
		.BYT	$07
		.BYT	$41
		.WORD	LOGO_DLIST_RAM1

TXT_COPY_ROM:	.ASC	"copyright", 0

		.BYT	"1" + $20
		.BYT	"9" + $20
		.BYT	"8" + $20
		.BYT	"1" + $20

		.ASC	0, "atari"

LGO_COLEND:
		.BYT	$08
		.BYT	$08
		.BYT	$08
		.BYT	$09
		.BYT	$09
		.BYT	$09
		.BYT	$0A
		.BYT	$0A
		.BYT	$0B
		.BYT	$0B
		.BYT	$0c
		.BYT	$0d
		.BYT	$0e
		.BYT	$0F
		.BYT	$10
		.BYT	$11
		.BYT	$12
		.BYT	$14
		.BYT	$16
		.BYT	$19
		.BYT	$1C
		.BYT	$1F
		.BYT	$36
		.BYT	$36
		.BYT	$36
		.BYT	$36
		.BYT	$00
		.BYT	$00
		.BYT	$36
		.BYT	$36
		.BYT	$36
		.BYT	$36

LGO_COLSTRT:
		.BYT	$00
		.BYT	$00
		.BYT	$00
		.BYT	$00
		.BYT	$00
		.BYT	$00
		.BYT	$01
		.BYT	$01
		.BYT	$01
		.BYT	$02
		.BYT	$02
		.BYT	$03
		.BYT	$03
		.BYT	$04
		.BYT	$05
		.BYT	$06
		.BYT	$07
		.BYT	$08
		.BYT	$09
		.BYT	$0A
		.BYT	$0B
		.BYT	$0c
		.BYT	$0e
		.BYT	$10
		.BYT	$13
		.BYT	$16
		.BYT	$00
		.BYT	$00
		.BYT	$00
		.BYT	$00
		.BYT	$00
		.BYT	$00

LGO_ATNAME:
		.BYT	$92
		.BYT	$4E
		.BYT	$42
		.BYT	$97
		.BYT	$52
		.BYT	$00

		.BYT	$84
		.BYT	$3E
		.BYT	$34
		.BYT	$79
		.BYT	$42
		.BYT	$00

		.BYT	$76
		.BYT	$2E
		.BYT	$26
		.BYT	$5B
		.BYT	$32
		.BYT	$00

		.BYT	$76
		.BYT	$82
		.BYT	$86
		.BYT	$53
		.BYT	$43
		.BYT	$22
		.BYT	$00

		.BYT	$63
		.BYT	$03
		.BYT	$72
		.BYT	$73
		.BYT	$03
		.BYT	$42
		.BYT	$63
		.BYT	$12
		.BYT	$00

		.BYT	$62
		.BYT	$22
		.BYT	$72
		.BYT	$72
		.BYT	$22
		.BYT	$42
		.BYT	$72
		.BYT	$12
		.BYT	$00

		.BYT	$62
		.BYT	$22
		.BYT	$72
		.BYT	$72
		.BYT	$22
		.BYT	$42
		.BYT	$72
		.BYT	$12
		.BYT	$00

		.BYT	$52
		.BYT	$42
		.BYT	$62
		.BYT	$62
		.BYT	$42
		.BYT	$32
		.BYT	$62
		.BYT	$22
		.BYT	$00

		.BYT	$52
		.BYT	$42
		.BYT	$62
		.BYT	$62
		.BYT	$42
		.BYT	$32
		.BYT	$52
		.BYT	$32
		.BYT	$00

		.BYT	$52
		.BYT	$42
		.BYT	$62
		.BYT	$62
		.BYT	$42
		.BYT	$32
		.BYT	$06
		.BYT	$42
		.BYT	$00

		.BYT	$43
		.BYT	$43
		.BYT	$52
		.BYT	$53
		.BYT	$43
		.BYT	$22
		.BYT	$05
		.BYT	$52
		.BYT	$00

		.BYT	$42
		.BYT	$62
		.BYT	$52
		.BYT	$52
		.BYT	$62
		.BYT	$22
		.BYT	$05
		.BYT	$52
		.BYT	$00

		.BYT	$42
		.BYT	$62
		.BYT	$52
		.BYT	$52
		.BYT	$62
		.BYT	$22
		.BYT	$42
		.BYT	$42
		.BYT	$00

		.BYT	$3E
		.BYT	$42
		.BYT	$4E
		.BYT	$12
		.BYT	$42
		.BYT	$42
		.BYT	$00

		.BYT	$3E
		.BYT	$42
		.BYT	$4E
		.BYT	$12
		.BYT	$52
		.BYT	$32
		.BYT	$00

		.BYT	$3E
		.BYT	$42
		.BYT	$4E
		.BYT	$12
		.BYT	$52
		.BYT	$32
		.BYT	$00

		.BYT	$23
		.BYT	$83
		.BYT	$32
		.BYT	$33
		.BYT	$83
		.BYT	$02
		.BYT	$62
		.BYT	$22
		.BYT	$00

		.BYT	$22
		.BYT	$A2
		.BYT	$32
		.BYT	$32
		.BYT	$A2
		.BYT	$02
		.BYT	$62
		.BYT	$22
		.BYT	$00

		.BYT	$22
		.BYT	$A2
		.BYT	$32
		.BYT	$32
		.BYT	$A2
		.BYT	$02
		.BYT	$72
		.BYT	$12
		.BYT	$00

		.BYT	$22
		.BYT	$A2
		.BYT	$32
		.BYT	$32
		.BYT	$A2
		.BYT	$02
		.BYT	$72
		.BYT	$12
		.BYT	$00

#ifdef _2PORT
		.DSB 25, 0
#else
		.DSB 38, 0
		.ASC	"RJZ 1982", 0	;programmer's sig
					;only in 4port
#endif

		.WORD MAIN_NMI	;nmi
		.WORD MAIN_RESET;reset
		.WORD MAIN_IRQ	;irq
