Results 1 to 2 of 2

Thread: Atari 2600 Programming Tutorial 2 - Your First Atari Program (Background Demo)

  1. #1

    Thread Starter
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Atari 2600 Programming Tutorial 2 - Your First Atari Program (Background Demo)

    Before you write your first Atari 2600 program, you will first need some tools.

    1) You could use a notepad or wordpad but my recommendation is Notepad++. And it can be set to any language you choose which will highlight the correct syntax. Plus you'll need the line numbers for debugging purposes. Every programmer should have Notepad++.

    2) The latest version of DASM. You will need this to compile the code you wrote to binary format. It is a versatile macro assembler with support for several 8-bit microprocessors including 6502, 6507, 6803, HD6303, 68HC11, 68705, and F8. In our examples, we will be using it for 6502 obviously.

    3) macro.h header file.

    4) vcs.h header file.

    5) Have all of this saved into one folder. Now you are gonna create a batch file to compile your code through DASM.

    Code:
    @echo off
    dasm background.asm -lkernel.txt -f3 -v5 -obackground.bin
    Save it as Compile.bat in the same folder. So anytime you save your code you have in your asm file, you run this batch file so it compiles it to binary.

    6) The last tool you will need is an Atari 2600 emulator. And a really good one is Stella. Generally its best for debugging when running in windowed mode so its faster to jump from code window, to compiling, and running it from the emulator over and over. By default its running full screen, even the menu.

    So now you have everything you need. Now it's time to learn some important information about how the Atari 2600 and television in general works. There were 3 different kinds of televisions in the past depending where you live. That is NTSC (60Hz), PAL (50Hz), and SECAM (Hz).
    As you probably already know, the NTSC TVs are in the US. Whereas PAL is European based sets. SECAM is rare and not really worth talking about. Now the number of scanlines an NTSC TV unit can have are 525 scanlines. PAL TV units can have 625 scanlines. However, the NTSC TV frames display 262 scanlines while PAL TV frames display 312 scanlines. In this tutorial we will be focusing on NTSC. But once you learn how an Atari displays onto television sets, you can make the proper adjustments on programming as PAL.

    Television sets that had color during the time used 3 electron beams that fired from left to right one scanline at a time from top to bottom to display a frame of whatever was being broadcasted. When it reached the bottom. The electron beam is turned off, and a certain amount of time is needed for it to reach the top again to draw the next frame one scanline at a time. This is important to know. As we have to think this way when programming an Atari game.

    The Atari 2600 has a special chip inside called the TIA (Television Interface Adaptor.) The TIA is responsible for creating the signal for a single scanline for the TV. Each scanline consists of at least 228 color clocks. But of the 228 color clocks, 160 are actually displayed on screen. The 68 color clocks of empty space before the scanline actually appears is called a Horizontal Blank or HBLANK. The CPU on the other hand is 3 times less the speed of the TIA. So during that time of 228 clock cycles from the TIA that ticked, 76 machine cycles from the 6502 CPU have passed, which is 1.19 MHz. So 228 / 3 is 76! This is important because there probably will be times you'll need to count the cycles your instructions use. If they exceeded 76 for that scanline, you'll end up with bad results.

    From top to bottom there are 262 scanlines total, but only 192 are displayed on screen. Before it is displayed, there are 3 lines of VSYNC (Vertical Sync) and 37 lines of VBLANK (Vertical Blank). After that, is when you display 192 lines of whatever is displayed that frame. Then once it reaches the end, there are 30 scanlines of overscan that also doesn't get displayed. This is how we are going to code every Atari program. And it's important to remember these numbers. Here is a visual representation of what it looks like:

    Name:  post-214-1053698855.jpg
Views: 3183
Size:  42.3 KB


    Another thing you must be aware of when programming an Atari game is that it is only limited to 4k bytes of programming data. Every line of code takes up a byte. So there may be times where you have to figure out how to reduce the number of instructions in order to save bytes! Watch your bytes!

    Now before we give you a good setup program to render the background a single color, there are a variety of colors to choose from on this table from this website:
    http://www.randomterrain.com/atari-2...-davie-11.html

    When you move your mouse, it'll give you the value of the color of your liking. In our case, we are going to use a sky blue background #$9A. Thankfully DASM allows you to use constants so we will make a constant value by saying BLUE = $9A.

    DASMs a picky little bastard though when it comes to indenting. When using commands such as processor, include, SEG and ORG, you have to indent it or it wont execute. I was baffled when I ran my first program only to do something weird which looked like a blue door closing and it played some weird noise and crashed when the door closed. I call it the closing door of death LOL!
    So yes just like C++ you can use commands such as include (not #include) to include header libraries or other asm files incase you wanna make your program bigger. As long as the include is indented.

    ORG means it will originate in a certain place in memory. In our case, we will be starting at $F000 in our program. Which means we will have $0FFF worth of code to put in, which is 4095 bytes. The Interrupt vectors such as NMI, IRQ, and RESET will originate at $FFFA.

    The vcs.h file we are including has all the addresses well need as constants to make life so much easier, such as VBLANK, VSYNC, WSYNC, COLUBK (which is the background color address), and so much more will be used. In our example we will be using these 4 for now. WSYNC simply just halts cpu executions until the next scanline whenever you use STA WSYNC.

    Now on to the main event! Here is the source code to a simple sky blue background:

    Code:
    	processor 6502
    
    	include "vcs.h"
    	include "macro.h"
    
    BLUE         = $9A
    	
    ;------------------------------------------------------------------------------
    	SEG
    	ORG $F000
    	
    Reset
    ; Clear RAM and all TIA registers
    	ldx #0 
    	lda #0 
    Clear           
    	sta 0,x 
    	inx 
    	bne Clear
    ;------------------------------------------------
    ; Once-only initialization. . .
    	lda #BLUE
    	sta COLUBK             ; set the background color
    ;------------------------------------------------
    
    StartOfFrame
    ; Start of new frame
    ; Start of vertical blank processing
    	lda #0
    	sta VBLANK
    	lda #2
    	sta VSYNC
    	sta WSYNC
    	sta WSYNC
    	sta WSYNC               ; 3 scanlines of VSYNC signal
    	lda #0
    	sta VSYNC
    ;------------------------------------------------
    ; 37 scanlines of vertical blank. . .
    	ldx #0
    VerticalBlank   
    	sta WSYNC
    	inx
    	cpx #37
    	bne VerticalBlank
    ;------------------------------------------------
    ;192 lines of drawfield
        ldx #0
    DrawField:
    
            ;This will draw a blank background with the color you chose for
            ;COLUBK
    
    	sta WSYNC
            inx
    	cpx #192
    	bne DrawField
    ;------------------------------------------------
    ; end of screen - enter blanking
        lda #%01000010
        sta VBLANK          
    ;------------------------------------------------
    ; 30 scanlines of overscan. . .
    	ldx #0
    Overscan        
    	sta WSYNC
    	inx
    	cpx #30
    	bne Overscan
    	jmp StartOfFrame
    ;------------------------------------------------
    
    	ORG $FFFA
    	
    InterruptVectors
    	.word Reset          ; NMI
    	.word Reset          ; RESET
    	.word Reset          ; IRQ
    
    END
    Save the file as background.asm in the same directory as your Compile.bat and DASM.exe, run the batch file Compile.bat that contained this code:

    Code:
    @echo off
    dasm background.asm -lkernel.txt -f3 -v5 -obackground.bin
    It should've compiled a file called background.bin

    Now open Stella if is isn't open already (recommended window mode to go back and forth easier), and run the file background.bin. You should see this if it worked:



    So far so good! Now you can play around with the background colors to whatever color you want. Like I said before you can choose whatever colors are to your liking HERE. Everything else you don't change if you want it to work correctly. So whatever goes on in your drawfield area will be the main section you will be doing all of your Atari programming, with the exception of creating graphics.

    Exercise: Have the display show 192 different scaneline colors at once!

    Answer to Tutorial 1 exercise:
    Code:
    ldx #31      ;Pixel X location at #31
    ldy #31      ;Pixel Y location at #31
    stx $05
    sty $06
    
    main:
     ldx $05
     ldy $06
    
     txa          ;Transfer X to A
     sta $0 ;xpos ;Store x pos in memory location $0
     
     tya          ;Transfer Y to A
     sta $1 ;ypos ;Store y pos in memory location $1
    
     lda $1     ;load y position
     and #$1f   ;keep the value within the range 0-31
     asl        ;shifting left becomes 62
     tay        ;Transfer the A to Y
     lda map,y  ;Load 62 (of 63) from table; store in A
     sta $2     ;Memory location $2 now equals #$e0
     iny        ;62 + 1 = 63
     lda map,y  ;Load 63 (of 63) from table; store in A
     sta $3     ;Memory location $3 now equals #$05
    
     lda $0     ;Load x position
     and #$1f   ;Keep the value within the range 0-31
     tay        ;Transfer the A to Y
    
     LDA #$01   ;Set Pixel Color
     sta ($2),y ;Draw Pixel
     jmp main
    
    map:
     dcb $00,$02, $20,$02, $40,$02, $60,$02
     dcb $80,$02, $a0,$02, $c0,$02, $e0,$02
    
     dcb $00,$03, $20,$03, $40,$03, $60,$03
     dcb $80,$03, $a0,$03, $c0,$03, $e0,$03
    
     dcb $00,$04, $20,$04, $40,$04, $60,$04
     dcb $80,$04, $a0,$04, $c0,$04, $e0,$04
    
     dcb $00,$05, $20,$05, $40,$05, $60,$05
     dcb $80,$05, $a0,$05, $c0,$05, $e0,$05
    Next tutorial I'm gonna show you how to draw the playfield! Have fun
    Attached Files Attached Files

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Atari 2600 Programming Tutorial 2 - Your First Atari Program (Background Demo)

    Quote Originally Posted by Jacob Roman View Post
    ...
    Television sets that had color during the time used 3 electron beams that fired from left to right one scanline at a time from top to bottom to display a frame of whatever was being broadcasted. When it reached the bottom. The electron beam is turned off, and a certain amount of time is needed for it to reach the top again to draw the next frame one scanline at a time. This is important to know. As we have to think this way when programming an Atari game....
    Of course the left to right scan is "stage left", i.e. the left side of the monitor from the perspective of the viewer. From the electron guns perspective, the beam is scanning right to left.

    Not that it matters for programming purposes, but the electron beam is also turned off at the end of each scanline, thus the need for the horizontal blank time to allow the voltages on the yoke to be driven so that the beam is ready to start at the left side again for the next scanline.
    The voltages on the yoke to drive the beams is fairly high, in the neighborhood of 20,000 volts or more. The high voltage is necessary to be able to steer the electron beams so quickly. In addition, to pull the beam back to the starting point even quicker than than the scan time requires even more voltage, so a flyback transformer is used (so named because the beam needs to fly back to the other side) to give a quick short term boost in voltage to pull the beam(s) back to the other side quickly.
    I think it is the winding in the flyback transformer that can usually loosen a bit over time and start moving slightly in response to the large magnetic fields being generated and produce the annoying buzz that emanates from some older TVs. It might also be winding in the yoke as well can do this, but I'm not sure.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width