Atari 2600 VCS Programming

  • View
    3.436

  • Download
    26

Embed Size (px)

DESCRIPTION

Introduces the core architecture of the Atari 2600 graphics chip (TIA), its 6502 CPU and showcases a "Hello, World!" application that can be executed on an emulator or on the real console. Includes reference links for those who want to learn more. Originally presented in Brazil at events such as Dev in Sampa and Campus Party.

Text of Atari 2600 VCS Programming

  • 1. game programATARI 2600PROGRAMMINGUse with Keyboard ControllersPROGRAM CONTENTS 2011-2013 CHESTER, INC.

2. WhatAn overview of the Atari 2600architecture, covering everythingneeded to create a Hello,World!program that can run on emulatorsor even on a real Atarihttp://slideshare.net/chesterbr 3. Why Pure nostalgia Homebrew games Demoscene Appreciate masterworks such asEnduro, Pitfall! or River Raid Feel better about todays toolsand hardware limitations :-)http://slideshare.net/chesterbr 4. @chesterbrhttp://chester.meWho 5. Atari 2600(Video Computer System) 6. Over 600 titles...imagem: mitchelaneous.com 7. but why were they so...Atari-ish? 8. Lets look inside and find out!(Atari 2600 Jr. printed circuit board) 9. CPU: 6507 10. CPU: 65076502 11. Video:TIA 12. Everything else: RIOT (6532) 13. Look ma, no O.S.!Atari programs talk directly to thehardware there is no middle man!Its 6502 CPU only understandsmemory reads and writes, so all theother chips are hard-wired to act asmemory (even those who arent) 14. Memory Map(very, very, very simplified*)0000-002C TIA (write)0030-003D TIA (read)0080-00FF RIOT (RAM)0280-0297 RIOT (I/O,Timer)F000-FFFF Cartridge (ROM)*http://nocash.emubase.de/2k6specs.htm 15. Memory MapF000-FFFF Cartridge (ROM)(this is not your biggest problem...)4 KBytes! 16. Memory Map0080-00FF RIOT (RAM)(and still not your biggest problem)128 BYTES!!!!!(1/8 of a KB) 17. VRAM(frame buffer)Typical video chips translatebit patterns stored onVideo RAM(VRAM) into pixels and colors 18. VRAMVRAMVRAM3844447C4444EE00 19. VRAMScreen resolution and color depthare subject toVRAM size limitsMemory was expensive on the70s/80s, leading to trade-offsHow muchVRAM does an Atari have? 20. Memory Map0000-002C TIA (write)0030-003D TIA (read)0080-00FF RIOT (RAM)0280-0297 RIOT (I/O,Timer)F000-FFFF Cartridge (ROM) 21. Memory Map????-???? VRAM 22. Memory Map0 bytes !!!!????-???? VRAMright, now yougot a problem... 23. Racing the BeamSince we cant just write pixels tosomeVRAM frame buffer, our codewill need to work a bit closer tothe TV hardware, with a little helpfrom a very unique chip... 24. TIA(Television Interface Adaptor) 25. How a TV set worksImage: cc-by wikipedia user Grm_wnr 26. Scanlinespublic domain illustration by Ian Harvey60 framesper second(NTSCstandard) 27. TIA is scanline-orientedAs the beam draws each scanline, thegame program must set TIA registersto configure the objects drawn on itMost of this objects have only onecolor, making multiple colors on thesame scanline theoretically impossible... 28. ...which explains:vs. 29. constraints creativityvs. 30. Screen Objects Playfield (PF) Players (P0, P1) Missiles/Ball (M0, M1, BL)Scanlines will be rendered based onhow we configure TIAs screen objects: 31. Playfield (PF)20-bit pattern with a foreground anda background color, renderedover the left side of the scanlineThe right side will either repeat orrefect the same pattern (the latergenerating a symmetrical image) 32. PLAYFIELD 33. PLAYFIELD 34. PLAYFIELD 35. PLAYFIELD 36. Playfield configurationPF0 = 0000 orderPF1 = 00000000 order PF2 = 00000000 orderREFLECT = 0resulting scanline 37. Playfield configurationPF0 = 0001 orderPF1 = 00000000 order PF2 = 00000000 orderREFLECT = 0resulting scanline_ _ 38. Playfield configurationPF0 = 0011 orderPF1 = 00000000 order PF2 = 00000000 orderREFLECT = 0resulting scanline__ __ 39. Playfield configurationPF0 = 0111 orderPF1 = 00000000 order PF2 = 00000000 orderREFLECT = 0resulting scanline___ ___ 40. Playfield configurationPF0 = 1111 orderPF1 = 11110000 order PF2 = 00000000 orderREFLECT = 0resulting scanline________ _______ 41. Playfield configurationPF0 = 1111 orderPF1 = 11111110 order PF2 = 00010101 orderREFLECT = 0resulting scanline___________ _ _ _ ___________ _ _ _ 42. Playfield configurationPF0 = 1111 orderPF1 = 11111110 order PF2 = 00010101 orderREFLECT = 1resulting scanline___________ _ _ _ _ _ _ ___________ 43. ___________ _ _ _ _ _ _ ___________ 44. Players (P0, P1)Each one is an independent 8 bit pattern(GRP0/GRP1) with a foreground color(COLUP0 / COLUP1) that can bepositioned at any column of the scanlinee.g.: 10100001 45. PLAYERS 46. PLAYERS 47. PlayersEach player can be horizontallystretched, multiplied or inverted bysetting NUSIZn / REFPn (n=0/1)(number/size and reflect player) 48. NUSIZ0 (or NUSIZ1)000001010011100101110111NUSIZn 49. With REFP0 (or REFP1) on000001010011100101110111NUSIZn 50. NUSIZn 51. NUSIZn 52. NUSIZn 53. NUSIZn 54. NUSIZn 55. Missiles/Ball (M0/M1/BL)Can be positioned just like players, butno bit pattern, just a pixel (although itcan be horizontally stretched 2/4/8x)M0/M1 use P0/P1colors, whileBL uses the PF foreground color 56. MISSILES 57. BALL 58. BALL 59. BALLMISSILE 60. BALLMISSILE 61. Master PlanFor each scanline, configure theoptions for each object before thebeam reaches its intended positionThe time slot is very short, forcingprogrammers to pick and choose whatto change, reusing as much as they can 62. How short?6502 1,19Mhz (1.194.720 cycles/sec)NTSC: 60 frames per second1.194.720/60 19.912 cycles per frame 63. How short?CPU: 19.912 cyles per frameNTSC: 262 scanlines per frame19.912 / 262 = 76 cycles per scanline 64. How short?CPU: 19.912 cyles per frameNTSC: 262 scanlines per frame19.912 / 262 = 76 cycles per scanlineand what can we do with 76 cycles?(by the way:WTF is a cycle?) 65. Assembly 6502 66. 6502 67. 6502 (Atari-wise)Reads a program from the cartridge(ROM) composed of operations thatmanipulate and transfer bytes betweencartridge, RIOT (RAM, I/O, timers) andTIA, keeping state on internal registers 68. OperationsEach operation that composes a 6502program in memory is identified by a1-byte opcode and can be followedby up to 2 bytes of parametersAn instruction can take up to 6cycles to be executed 69. RegistersA = Accumulator (8 bits)X,Y= Indexes (8 bits)S = Stack Pointer (8 bits)P = Status (fags, 8 bits)PC = Program Counter (16 bits) 70. Example programadd 2 to a value stored at amemory position; store the resultinto another memory position 71. Implementation Read the byte stored on memoryposition 0200 into register A Add 2 to register As value Write register As value intomemory position 0201 72. 6502 Machine CodeAD Opcode (Memory A)00 Last part of 020002 First part of 020069 Opcode (value + A A)02 Value to add8D Opcode (A Memory)01 Last part of 020102 First part of 0201 73. 6502 Assembly LanguageAssociates the 151 opcodeswith 56 mnnemonic instructionsand a notation for theirparameters (access mode) 74. 6502 Machine CodeAD Opcode (Memory A)00 Last part of 020002 First part of 020069 Opcode (value + A A)02 Value to add8D Opcode (A Memory)01 Last part of 020102 First part of 0201 75. Assembly 6502AD LDA $0200000269 ADC #02028D STA $02010102 76. AssemblerProgram that reads a text filewritten in Assembly language andassembles a binary file with thecorresponding machine codefoo.asmLDA $0200ADC #02STA $0201...foo.binAD000269028D0102...ASSEMBLER 77. Macro AssemblerORG $0100 ; Start @ memory 0100...SomeLabel:LDX #$10 ; No idea where thisDEX ; will be in memory,BNE SomeLabel ; and dont need to!... 78. DASM 6502 Macro Assembler Includes Atari headers Multiplataform Free and open-source (GPLv2)http://dasm-dillon.sourceforge.net/ 79. Notation (for today)#... = decimal value#$... = hex value$... = hex address$... , X = hex address + Xhttp://www.obelisk.demon.co.uk/6502/addressing.html 80. 6502 Instruction Set= most relevant for Atari 2600 programming 81. Data TransferLDA, LDX, LDY = LoadSTA, STX, STY = StoreTAX,TAY,TXA,TYA,TSX,TXS = TransferLDA #$10 0x10ASTY $0200 Ym(0x0200)TXA XA 82. ArithmeticADC, SBC = +,- (w/ carry)INC, INX, INY = ++DEC, DEX, DEY = --ADC $0100 m(0x100)+AAINC $0200 m(0x200)+1m(0x200)DEX X-1X 83. Bit OperationsAND, ORA, EOR = &, |, ^ (A)ASL, LSR = Arithmetic shiftROL, ROR = Rotating shiftAND #$11 A&0x11ALSR A>>1A (A/2A)ROR A>>1 (bit 7=carry) 84. Comparing / BranchingCMP, CPX, CPY = compare A/X/Y (-)BCS, BCC = if carry set / clearBEQ, BNE = if equal / not equalBVS, BVC = if overfow set / clearBMI, BPL = if minus / plusCPY $1234 if y=m(0x1234),BEQ $0200 0x0200PC 85. Stack and SubroutinesJSR, RTS = call/return subroutinePHA, PLA = push / pull APHP, PLP = push / pull status (P)JSR $1234 PC(+3)stack,0x1234PCRTS stackPC 86. Everything else...NOP = No OperationJMP = Direct Jump (GOTO)SEC, CLC = Set/Clear CarrySEV, CLV = Set/Clear oVerfowSEI, CLI = Set/Clear Interrupt-offSED, CLD = Set/Clear DecimalRTI = Return from InterruptBRK = Break 87. Neo:I know kung fu.Morpheus:Show me. 1999 Warner Bros 88. Hello,World! 89. Hello,World!Horizontal writing is hard(too many pixels per scanline) 90. Hello,World!Vertical writingis the way (less pixels/scanline)We can use a playeror the playfield 91. Hello,World!Our display kernel will configure eachpair of visible scanlines with a bytefrom a hello world bitmap (storedon the card, just after the code)Lets find which are visible, and howwe need to deal with TV timings 92. Source: Stella Programmers Guide, SteveWright, 1979GAMELOGIC(3+37+30).76=5320cyclesKERNEL 93. Program structureVertical SyncVertical BlankOverscanPlayfield 94. mainloop(infinite)KernelX: count 0 to 191(192 scanlines)Program structureVertical SyncVertical BlankOverscan11 chars *8 bytes *2 scanlinesper byte =176scanlines 95. Program structureVertical SyncVertical BlankKernelOverscan 96. Lets begin!PROCESSOR 6502INCLUDE "vcs.h"ORG $F000 ; Cart begins hereVertical SyncVertical BlankKernelOverscan 97. Frame (main loop) startStartFrame:lda #%00000010 ; Signal VSYNC start bysta VSYNC ; setting bit 1REPEAT 3 ; lasts 3 scanlinessta WSYNC ; (WSYNC = wait untilREPEND ; scanlin