Re: [stella] Cosmic Ark stars

Subject: Re: [stella] Cosmic Ark stars
From: Eckhard_Stolberg@xxxxxxxxxxxxxxxxxxxxx (Eckhard Stolberg)
Date: Sat, 10 May 1997 18:17:52 +0200
>How did they do those nice blinking background stars in "Cosmic Ark"?
>They're not correctly emulated by PC Atari, and it's even more interesting
>that they look very different when I run Cosmic Ark on my Atari 2600 hacked
>for composite video output: instead of looking like stars in the sky, they
>seem to follow some strange, changing patterns...
>For greater fun, please answer without looking at the code :)

OK, I cheated. I looked at the code, but it still took some experimentation
to figure it out.

Actually the answer is right in the programming manual. It says:

'WARNING: These motion registers should not be modified
during the 24 computer cycles immediately following an 
HMOVE command. Unpredictable motion values may result.'

And who would have predicted an effect like that. :-)

Cosmic Ark uses missile0 for this and I am not sure if it would work with
other moving object too. The trick is to do a HMOVE command, then wait 18
cycles and finally write a different value into HMM0 again. 

If you use the right values for both writes to HMM0, then the TIA gets
confused, moves the missile about 15 pixels away and keeps doing so in
every line until you do another HMOVE command, even if you change the 
motion registers before. My little demo program uses $07 and $06 for this.
Several other values, that I tried, had no effect.

The rest of the effect is just changing the initial position of missile0
every couple of frames to get a moving starpattern effect and changing the
colour in every line to make the stars sparkle.

This little program displays a similar starpattern as cosmic ark does.
It should work on PAL as well as on NTSC. I hope it has enough comments to
understand how it works.

Ciao, Eckhard Stolberg
; TIA (Stella) write-only registers
Vsync		equ	$00
Vblank		equ	$01
Wsync		equ	$02
Colup0		equ	$06
Colubk		equ	$09
Ctrlpf		equ	$0A
Resm0		equ	$12
Enam0		equ	$1D
Hmm0		equ	$22
Hmove		equ	$2A
Hmclr		equ	$2B
; RAM definitions
; Note: The system RAM maps in at 0080-00FF and also at 0180-01FF. It is
; used for variables and the system stack. The programmer must make sure
; the stack never grows so deep as to overwrite the variables.
RamStart	equ	$0080
RamEnd		equ	$00FF
StackBottom	equ	$00FF
StackTop	equ	$0080
; 6532 (RIOT) registers
Swcha		equ	$0280
Swacnt		equ	$0281
Swchb		equ	$0282
Swbcnt		equ	$0283
Intim		equ	$0284
Tim64t		equ	$0296
; ROM definitions
RomStart        equ     $F000
RomEnd          equ     $FFFF
IntVectors      equ     $FFFA
; Ram definitions
                ORG     $80
tablePTR        DS      1
colour          DS      1
counter         DS      1
colourcounter   DS      1
; Data Area
                ORG     $F800
table:          DB      4,5,6,7,8,9,10,11,12,11,10,9,8,7,6,5
; Program initialisation

		SEI				; Disable interrupts.:
		CLD				; Clear "decimal" mode.

		LDX	#$FF
		TXS				; Clear the stack

		LDX	#$28		; Clear the TIA registers ($04-$2C)
		LDA	#$00
		STA	$04,X
                BPL     TIAClear        ; loop exits with X=$FF
		LDX	#$FF
		STA	$00,X		; Clear the RAM ($FF-$80)
                BMI     RAMClear        ; loop exits with X=$7F
		LDX	#$FF
		TXS				; Reset the stack
		STA	Swbcnt		; console I/O always set to INPUT
		STA	Swacnt		; set controller I/O to INPUT

StarsInit:      LDA     #$03
                STA     colour
; Main program loop
                LDA     #$02
		STA	Wsync		; Wait for horizontal sync
		STA	Vblank		; Turn on Vblank
                STA	Vsync		; Turn on Vsync
		STA	Wsync		; Leave Vsync on for 3 lines
		STA	Wsync
		STA	Wsync
                LDA     #$00
		STA	Vsync		; Turn Vsync off

                LDA     #43             ; Vblank for 37 lines
		STA	Tim64t		; 43*64intvls=2752=8256colclks=36.2lines

                LDA     #$00            ;black
                STA     ColuBK
                LDA     #$0e            ;white
                STA     ColuP0
                LDA     #$02            ;turn missile0 on
                STA     ENAM0
                LDX     tablePTR        ;reposition missile0 every couple of
                                        ;frames for starpattern movement
                LDY     counter
                CPY     #10
                BNE     @1
                LDY     #0
                CPX     #16
                BNE     @1
                LDX     #$00
@1:             STX     tablePTR       
                STY     counter
                LDA     table,X
                STA     Wsync
@2:             DEY
                BPL     @2
                STA     ResM0
                LDA     #$70            ;this value is important for the effect
                STA     HMM0

                STA     Wsync
                STA     HMOVE
                JSR     Trick           ;waste 18 cycles and load move value
                STA     HMM0            ;this is the tricky part

                LDX     colourcounter   ;change colours for spakling stars
                STX     colourcounter
                CPX     #3
                BNE     VblankLoop
                LDA     colour
                ADC     #$10
                STA     colour

		LDA	Intim
                BNE     VblankLoop      ; wait for vblank timer
		STA	Wsync		; finish waiting for the current line
                STA     Vblank          ; turn off Vblank

Screen:         LDA     colour
                LDY     #192
@1:             STA     Wsync           ;change the colour every line for
                                        ;sparkling stars - otherwise do what
                                        ;you want here
                ADC     #$03
                EOR     #$A0
                STA     ColuP0
                BNE     @1
                LDA     #$02
                STA     Vblank          ;turn on Vblank
                LDX     #30
@END:           STA     Wsync
                BNE     @END
                JMP     Start

Trick:          NOP                     ;the tricky subroutine
                LDA     #$60
; Set up the 6502 interrupt vector table
		ORG	IntVectors
NMI             dw      Cart_Init
Reset           dw      Cart_Init
IRQ             dw      Cart_Init

Archives available at
E-mail UNSUBSCRIBE in the body to stella-request@xxxxxxxxxxx to be removed.

Current Thread