By Andrew Davie (adapted by Duane Alan Hahn, a.k.a. Random Terrain)
As an Amazon Associate I earn from qualifying purchases. (I get commissions for purchases made through certain links on this page.)
Page Table of Contents
We've had a bit of time to think about the playfield, and hopefully have a go at some of the exercises. Admittedly I threw you in the deep end with the last session—so we'll go back a step and walk through exactly what all this playfield oddity is about. We'll also tackle some of the exercises to show that there's more than one way to skin a fish.
Last session we learned that the playfield registers PF0 and PF2 are reversed. Specifically, the order of pixels in the playfield registers (one bit per pixel, remember!) is backward, compared to the order for the first playfield register we encountered—PF1. This backward ordering is rather confusing, but that's just the way it is. Have a close look at the diagram presented in the last session and try and understand exactly the "playfield register/bit" to "pixel position on the scanline" correspondence.
There's one new playfield-related capability of the '2600 which I'd like to introduce now—playfield mirroring. I've already introduced this to you when I stated that the right hand side of the playfield was a copy of the left hand side (that is, the left 20 pixels come from the 20 playfield bits held in the TIA registers PF0, PF1 and PF2—and the right 20 bits are a copy of the same bits). That copy can be displayed 'normally'—or 'mirrored'. When mirrored, the bits are literally a mirrored copy of the left side of the playfield.
We're already familiar with two 'types' of TIA register. There's the strobe-type, where a write of any value to the register causes something to happen, independent of the value written (an example is WSYNC, which halts the 6502 until the TIA starts the next scanline). A second type is the normal register to which we write a byte, and the TIA uses that byte for some internal purpose (examples of these are the playfield registers PF0, PF1 and PF2). PF0 was a special-case of this type, where—though we wrote a byte—only four of the bits were actually used by the TIA. The remaining bits were discarded/ignored (have a look at the PF0 register in the diagram in the last session—the X for each bit position in bits D0-D3 indicates those bits are not used).
The third type of register (they're not really 'types'—but I want you to understand the difference between the way we're writing data to the TIA) is where we are interested in just the state of a single BIT in a register. Time to introduce a new TIA register, called CTRLPF. It's located at address 10 ($A)
CTRLPF This address is used to write into the playfield control register (a logic 1 causes action as described below) D0 = REF (reflect playfield) D1 = SCORE (left half of playfield gets color of player 0, right half gets color of player 1) D2 = PFP (playfield gets priority over players so they can move behind the playfield) D4 & D5 = BALL SIZE D5 D4 Width 0 0 1 clock 0 1 2 clocks 1 0 4 clocks 1 1 8 clocks
Wow! This register has a lot of different stuff associated with it! Most of it is related to playfield display (bits D0, D1, D2) but bits D4 and D5 control the 'BALL SIZE'—we'll worry about those bits later :)
Bit D0 controls the reflection (mirroring) of our playfield. If this bit is 0, then we have a 'normal' non-mirrored playfield, and that's what we've been seeing so far in our demos. If we set this bit to 1, then the '2600 will display a reflected playfield (that is, the right-side of the playfield is a mirror-image of the left-side, instead of a copy). Note that only a single bit is used to control this feature—if we wrote a byte with this bit set (ie: %00000001) to CTRLPF we would also be setting those other bits to 0—and we should be very sure this is what we want. In fact, it's often NOT what we want, so when we are writing to registers such as this (which contain many bits controlling different parts of the TIA hardware/display), we should be very careful to keep all the bits exactly as we need them. Sometimes this is done with a 'shadow' register—a RAM copy of our current register state, and by first setting or clearing the appropriate bit in the shadow register, and THEN writing the shadow register to the TIA register. This is necessary because many/most of the TIA registers are only writable—that is, you cannot successfully read their contents and expect to get the value last written.
Let's have a quick look at those other bits in this register, related to playfield. . .
D1 = SCORE. This is interesting. Setting this bit causes the playfield to have two colors instead of one. The left side of the playfield will be displayed using the color of sprite 0 (register COLUP0), and the right side of the playfield will be displayed using the color of sprite 1 (register COLUP1). We won't play with this for now—but keep in mind that it is possible. Remember, this machine was designed for PONG-style games, so this sort of effect makes sense in that context.
D2 = PFP. Playfield priority. You may have the playfield appear in front of, or behind, sprites. If you set this bit, then the playfield will be displayed in front, and all sprites will appear to go behind the playfield pixels. If this bit is not set, then all sprites appear to go in front of the playfield pixels.
That's a very quick rundown of this register. We know now that it controls the playfield mirroring (=reflection), the playfield color control for left/right halves, the playfield priority (if sprites go in front of or behind the playfield), and finally it does something with the 'BALL SIZE' which we're not worrying about yet.
I've indicated that it's useful to have a 'shadow' copy of the register in RAM, so that we can easily keep track of the state of this sort of register. In practice, this is rarely done—as we generally just set the reflection on or off, the score coloring on or off, the priority on or off, and the ball size as appropriate. . . and then forget it. But if, for example, you were doing a game where you were changing the priority on the fly (so your sprites went behind SOME bits background, but not other bits) then you'd need to know what those other values should be.
In any case, the point of this is to introduce you slowly to more TIA capabilities, and at the same time build your proficiency with 6502 programming. Here's how we set and clear bits with 6502.
CTRLPF_shadow = $82 ; a RAM location for our shadow register lda #%00000000 sta CTRLPF_shadow ; init our shadow register as required ; lots of code here lda CTRLPF_shadow sta CTRLPF ; copy shadow register to TIA register
The above code snippet shows the general form of shadow register usage. The shadow register is initialised—and at some point later in the code, we copy it to the TIA register. Now for the fun bit—setting and clearing individual bits in the shadow register. . .
; how to set a single bit in a byte lda CTRLPF_shadow ; load the shadow register from RAM ora #%00000001 ; SET bit 0 (D0 = 1) sta CTRLPF_shadow ; save new register value back to RAM ; how to clear a single bit in a byte lda CTRLPF_shadow and #%11111110 ; keep all bits BUT the one we want to clear sta CTRLPF_shadow
OK, that's not too difficult to understand. The two new 6502 instructions we have just used are 'ORA', which does a logical-OR (that is, combines the accumulator with the immediate value bit-by-bit using a OR operation)—and the 'AND', which does a logical-AND (again, combines the accumulator with the immediate value bit-by-bit using an AND operation). Now this is getting into pretty basic binary math—and you should read up on this stuff if you don't already know.
A bit is like a simple light switch. It can be on or off.
AND = OFF (0 = OFF 1 = UNTOUCHED)
Use 0 to make sure the light switch is off. Use 1 to leave it as it is.
OR = ON (1 = ON 0 = UNTOUCHED)
Use 1 to make sure the light switch is on. Use 0 to leave it as it is.
XOR = FLIP (1 = FLIP 0 = UNTOUCHED)
Use 1 to reverse the position of the light switch. Use 0 to leave it as it is.
Here are some truth tables for you. . .
OR operation BIT | 0 1 -----+------------ 0 | 0 1 | 1 | 1 1 AND operation BIT | 0 1 -----+------------ 0 | 0 0 | 1 | 0 1
Basically the above two tables give you the result FOR A SINGLE BIT POSITION, where you either OR or AND together two bits. For example, if I 'OR' together 1 and 0, the resultant value (bit) is 1. Likewise, if I 'AND' together a 1 and 0, I get a 0. This logical operation is performed on each bit of the accumulator, with the corresponding bit of the immediate value as part of the instruction. So 'ora #%00000001' will actually leave the accumulator with the lowest bit SET. No matter what. Likewise, 'and #%11111110' will leave the accumulator with the lowest bit CLEAR. No matter what. And in the other bits, their value will remain unchanged. You should try some values and check this out, because understanding this binary logical operation on bits is pretty fundamental to '2600 programming.
In the initialization section of your current kernel, add the following lines. . .
lda #%00000001 sta CTRLPF
That's our playfield reflection in operation—if you're running any sort of playfield code, you will see that the right-side is now a mirror-image of the left-side. Now have a think about the exercise I offered in session 14 . . .
It should be apparent, now, that in this sort of situation we really only need to worry about the left side of the playfield! If we let the '2600 reflect the right side, we will get a symmetrical copy of the left, and we'll have our box if only we do the left-side borders. This is a huge advantage to the programmer, because we suddenly don't have to write new PF0, PF1, PF2 values each scanline. Remember (and I'll drum this into you until the very last session!) we only have 76 cycles per scanline—the less we have to do on any line, the better. At the very least, rewriting PF0, PF1 and PF2 twice per scanline would cost 30 cycles IF you were being clever. That's almost half the available time JUST to draw background—and there's still colors, sprites, balls and missiles to worry about! However, if you just use a reflected playfield, then we are only looking at single writes to PF0, PF1, PF2, cutting our playfield update to only 15 cycles per line (eg: lda #value / sta PF0 / lda #value2 / sta PF1 / lda #value3 / sta PF2).
Just an aside, here—some people have been posting code IN UPPERCASE. It is quite acceptable to use upper or lowercase for the mnemonics of your 6502 code. I prefer lowercase, as I find it easier to read and LESS LIKE SHOUTING! But its totally up to you—you will typically (but not always) find my code is lowercase, and you may feel free to adopt a style that suits you. I make my constants UPPERCASE, my variables typically a mixture, and my mnemonics lower-case. Your mileage may vary.
So, let's get down to it—here's a solution for exercise 5, of session 14. . .
; '2600 for Newbies ; Session 15 - Playfield Continued ; This kernel draws a simple box around the screen border ; Introduces playfield reflection processor 6502 include "vcs.h" include "macro.h" ;------------------------------------------------------------------------------ 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 #$45 sta COLUPF ; set the playfield color lda #%00000001 sta CTRLPF ; reflect playfield ;------------------------------------------------ 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 ;------------------------------------------------ ; Do 192 scanlines of color-changing (our picture) ldx #0 ; this counts our scanline number lda #%11111111 sta PF0 sta PF1 sta PF2 ; We won't bother rewriting PF0-PF2 every scanline of the top 8 lines - they never change! Top8Lines sta WSYNC inx cpx #8 ; are we at line 8? bne Top8Lines ; No, so do another ; Now we want 176 lines of "wall" ; Note: 176 (middle) + 8 (top) + 8 (bottom) = 192 lines lda #%00010000 ; PF0 is mirrored <--- direction, low 4 bits ignored sta PF0 lda #0 sta PF1 sta PF2 ; again, we don't bother writing PF0-PF2 every scanline - they never change! MiddleLines sta WSYNC inx cpx #184 bne MiddleLines ; Finally, our bottom 8 scanlines - the same as the top 8 ; AGAIN, we aren't going to bother writing PF0-PF2 mid scanline! lda #%11111111 sta PF0 sta PF1 sta PF2 Bottom8Lines sta WSYNC inx cpx #192 bne Bottom8Lines ;------------------------------------------------ lda #%01000010 sta VBLANK ; end of screen - enter blanking ; 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
This kernel is interesting in that it achieves the box effect by writing the playfield registers BEFORE the scanline loops to do the appropriate section. It uses the knowledge that the TIA has an internal state and will keep displaying whatever it already has in the playfield registers. So, in fact, the actual cost (in cycles) of drawing the 'box' playfield on each scanline is 0 cycles—ie; it's free. We just had that short initial load before each section (taking a few cycles out of the very first scanline of each section). This is how you need to think about '2600 programming—how to remove cycles from your scanlines—and do the absolute minimal necessary.
Here's a screenshot:
Here's the .bin file to use with an emulator:
That will do for today's session. We've had an introduction to controlling individual TIA register bits, and seen how to achieve a reflected playfield at next to no cost. We've had a brief introduction to the CTRLPF register, and seen how it has a myriad (well, more than 3) uses. Although some of the previous sessions have asked you to think about tricky subjects like horizontal scrolling, and asymmetrical playfields—now is not the time to actually discuss these tricky areas. So until next time (when we'll develop our playfield skills a bit more). . . ciao!
Other Assembly Language Tutorials
Session 15: Playfield Continued
This book was written in English, not computerese. It's written for Atari users, not for professional programmers (though they might find it useful).
This book only assumes a working knowledge of BASIC. It was designed to speak directly to the amateur programmer, the part-time computerist. It should help you make the transition from BASIC to machine language with relative ease.
The 6502 Instruction Set broken down into 6 groups.
Nice, simple instruction set in little boxes (not made out of ticky-tacky).
This book shows how to put together a large machine language program. All of the fundamentals were covered in Machine Language for Beginners. What remains is to put the rules to use by constructing a working program, to take the theory into the field and show how machine language is done.
An easy-to-read page from The Second Book Of Machine Language.
A useful page from Assembly Language Programming for the Atari Computers.
Continually strives to remain the largest and most complete source for 6502-related information in the world.
By John Pickens. Updated by Bruce Clark.
Below are direct links to the most important pages.
Goes over each of the internal registers and their use.
Gives a summary of whole instruction set.
Describes each of the 6502 memory addressing modes.
Describes the complete instruction set in detail.
Cycle counting is an important aspect of Atari 2600 programming. It makes possible the positioning of sprites, the drawing of six-digit scores, non-mirrored playfield graphics and many other cool TIA tricks that keep every game from looking like Combat.
Atari 2600 programming is different from any other kind of programming in many ways. Just one of these ways is the flow of the program.
The "bankswitching bible." Also check out the Atari 2600 Fun Facts and Information Guide and this post about bankswitching by SeaGtGruff at AtariAge.
Atari 2600 programming specs (HTML version).
Links to useful information, tools, source code, and documentation.
Atari 2600 programming site based on Garon's "The Dig," which is now dead.
Includes interactive color charts, an NTSC/PAL color conversion tool, and Atari 2600 color compatibility tools that can help you quickly find colors that go great together.
Adapted information and charts related to Atari 2600 music and sound.
A guide and a check list for finished carts.
A multi-platform Atari 2600 VCS emulator. It has a built-in debugger to help you with your works in progress or you can use it to study classic games.
A very good emulator that can also be embedded on your own web site so people can play the games you make online. It's much better than JStella.
If assembly language seems a little too hard, don't worry. You can always try to make Atari 2600 games the faster, easier way with batari Basic.
Did you know that Trump's rushed experimental rona jab has less than one percent overall benefit? It also has many possible horrible side effects. Some brainwashed rona jab cultists claim that there are no victims of the jab, but person after person will post what the jab did to them or a family member on web sites such as Facebook and Twitter and they'll be lucky if they don't get banned soon after. Posting the truth is “misinformation” don't you know. Awakened sheep might turn into lions, so powerful people will do just about anything to keep the sheep from waking up.
Check out these videos:
Take a look at my page called The H Word and Beyond. You might also want to look at my page called Zinc and Quercetin. My sister and I have been taking those two supplements since summer of 2020 in the hopes that they would scare away the flu and other viruses (or at least make them less severe).
Some people appear to have a mental illness because they have a vitamin B deficiency. For example, the wife of a guy I used to chat with online had severe mood swings which seemed to be caused by food allergies or intolerances. She would became irrational, obnoxious, throw tantrums, and generally act like she had a mental illness. The horrid behavior stopped after she started taking a vitamin B complex. I've been taking #ad Jarrow B-Right for many years. It makes me much easier to live with.
Unfermented soy is bad! “When she stopped eating soy, the mental problems went away.” Fermented soy doesn't bother me, but the various versions of unfermented soy (soy flour, soybean oil, and so on) that are used in all kinds of products these days causes a negative mental health reaction in me that a vitamin B complex can't tame. The sinister encroachment of soy has made the careful reading of ingredients a necessity.
If you are overweight, have type II diabetes, or are worried about the condition of your heart, check out the videos by Ken D Berry, William Davis, and Ivor Cummins. It seems that most people should avoid wheat, not just those who have a wheat allergy or celiac disease. Check out these books: #ad Undoctored, #ad Wheat Belly, and #ad Eat Rich, Live Long.
Negative ions are good for us. You might want to avoid positive ion generators and ozone generators. Whenever I need a new air cleaner (with negative ion generator), I buy it from surroundair.com. A plain old air cleaner is better than nothing, but one that produces negative ions makes the air in a room fresher and easier for me to breathe. It also helps to brighten my mood.
Never litter. Toss it in the trash or take it home. Do not throw it on the ground. Also remember that good people clean up after themselves at home, out in public, at a campsite and so on. Leave it better than you found it.
Seems like more people than ever finally care about water, land, and air pollution, but the climate change cash grab scam is designed to put more of your money into the bank accounts of greedy politicians. Those power-hungry schemers try to trick us with bad data and lies about overpopulation while pretending to be caring do-gooders. Trying to eliminate pollution is a good thing, but the carbon footprint of the average law-abiding human right now is actually making the planet greener instead of killing it.
Watch these two YouTube videos for more information:
View this page and any external web sites at your own risk. I am not responsible for any possible spiritual, emotional, physical, financial or any other damage to you, your friends, family, ancestors, or descendants in the past, present, or future, living or dead, in this dimension or any other.
Use any example programs at your own risk. I am not responsible if they blow up your computer or melt your Atari 2600. Use assembly language at your own risk. I am not responsible if assembly language makes you cry or gives you brain damage.