Skip to content

Instantly share code, notes, and snippets.

@rylev
Created March 21, 2017 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rylev/4ea24c7a3863346cb5a452683086d467 to your computer and use it in GitHub Desktop.
Save rylev/4ea24c7a3863346cb5a452683086d467 to your computer and use it in GitHub Desktop.
Rainbows on Atari 2600
PROCESSOR 6502; set processor
INCLUDE "vcs.h" ; include helpful stuff
INCLUDE "macro.h" ; include helpful stuff
Seg Code ; set code segment
ORG $F000 ; code should start at 0xF000
BGColor equ $81 ; Reserve space at 0x81
Start
CLEAN_START ; make sure system is in a good state
NextFrame
;; Vertical Sync!
lda #2 ; set A to 2 a.k.a 0b10
STA VBLANK ; set VBlank to 0b10. This turns on Vblank
STA VSYNC ; set VSYNC to 0b10. This turns on Vsync
STA WSYNC ; 'strobe' Wsync which waits for the next line to be start
STA WSYNC ; we do this three times for the three lines of vsync signal (part of the NTSC standard)
STA WSYNC ; one last time!
LDA #0 ; load 0 into A
STA VSYNC ; turn off Vsync. We're done with it until the next frame
;; Vertical Blank!
LDX #37 ; Load 37 into X. 37 is the number of VBlank lines before we can draw
LVBlank
STA WSYNC ; Strobe Wsync. This halts the CPU until the start of the next scan line
DEX ; decrement X
BNE LVBlank ; do LVBlank again if X isn't 0
LDA #0 ; Set A to 0
STA VBLANK ; Write 0 to Vblank. This turns it off. We're ready to draw!
;; Drawing!
LDX #192 ; Set X to 192. We got 192 lines to draw!
LDY BGColor ; Y is set to whatever is in BGColor
LVScan
STY COLUBK ; The background color in Y is written to the background color register
STA WSYNC ; Wait for the start of the next line
INY ; increment Y (i.e. the background color)
DEX ; decrement the number of lines we have left
BNE LVScan ; start over drawing lines if we still have any left
;; Overscan!!
LDA #2 ; Set A to 0b10 again
STA VBLANK ; Turn on VBlank
LDX #30 ; We got 30 lines of over scan
LVOver
STA WSYNC ;; wait for next scan line
DEX ; decrement our counter of over scan lines
BNE LVOver ; do it again if we have any more over scan lines to go
DEC BGColor ; change the starting background color
jmp NextFrame ; let's do another frame!
org #$FFFC ; this is how we end a binary!
.word Start ; reset vector
.word Start ; interrupt vector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment