Today we're going to have a look at playfield graphics, and for the first time learn how to use RAM. The playfield is quite a complex beast, so we may be spending the next few sessions exploring its capabilities.
The '2600 was originally designed to be more or less a sophisticated programmable PONG-style machine, able to display 2-player games - but still pretty much PONG in style. These typically took place on a screen containing not much more than walls, two "players" - usually just straight lines ? and a ball. Despite this, the design of the system was versatile enough that clever programmers have produced a wide variety of games.
The playfield is that part of the display which usually shows "walls" or "backgrounds" (not to be confused with THE background colour). These walls are usually only a single colour (for any given scanline), though games typically change the colour over multiple scanlines to give some very nice effects.
The playfield is also sometimes used to display very large (square, blocky looking) scores and words.
Just like with COLUBK, the TIA has internal memory where it stores exactly 20 bits of playfield data, corresponding to just 20 pixels of playfield. Each one of these pixels can be on (displayed) or off (not displayed).
The horizontal resolution of the playfield is a very-low 40 pixels, divided into two halves - both of which display the same 20 bits held in the TIA internal memory. Each half of the playfield may have its own colour (we'll cover this later), but all pixels either half are the same colour. Each playfield pixel is exactly 4 colour-clocks wide (160 colour clocks / 40 pixels = 4 colour clocks per pixel).
The TIA manages to draw a 40 pixel playfield from only 20 bits of playfield data by duplicating the playfield (the right side of the playfield displays the same data as the left side). It is possible to mirror the right side, and it is also possible to create an "asymmetrical playfield" - where the right and left sides of the playfield are NOT symmetrical. I'll leave you to figure out how to do that for now - we'll cover it in a future session. For now, we're just going to learn how to play with those 20 bits of TIA memory, and see what we can do with them.
Let's get right into it. Here's some sample code which introduces a few new TIA registers, and also (for the first time for us) uses a RAM location to store some temporary information (a variable!). There are three TIA playfield registers (two holding 8 bits of playfield data, and one holding the remaining 4 bits) - PF0, PF1, PF2. Today we're going to focus on just one of these TIA playfield registers, PF1, because it is the simplest to understand.
; '2600 for Newbies ; Session 13 - Playfield processor 6502 include "vcs.h" include "macro.h" ;------------------------------------------------------------------------------ PATTERN = $80 ; storage location (1st byte in RAM) TIMETOCHANGE = 20 ; speed of "animation" - change as desired ;------------------------------------------------------------------------------ SEG ORG $F000 Reset ; Clear RAM and all TIA registers ldx #0 lda #0 Clear sta 0,x inx bne Clear ;------------------------------------------------ ; Once-only initialisation... lda #0 sta PATTERN ; The binary PF 'pattern' lda #$45 sta COLUPF ; set the playfield colour ldy #0 ; "speed" counter ;------------------------------------------------ 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 ;------------------------------------------------ ; Handle a change in the pattern once every 20 frames ; and write the pattern to the PF1 register iny ; increment speed count by one cpy #TIMETOCHANGE ; has it reached our "change point"? bne notyet ; no, so branch past ldy #0 ; reset speed count inc PATTERN ; switch to next pattern notyet lda PATTERN ; use our saved pattern sta PF1 ; as the playfield shape ;------------------------------------------------ ; Do 192 scanlines of colour-changing (our picture) ldx #0 ; this counts our scanline number Picture stx COLUBK ; change background colour (rainbow effect) sta WSYNC ; wait till end of scanline inx cpx #192 bne Picture ;------------------------------------------------ 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
The binary for this code, and a snapshot of the output are included below.
What you will see is our rainbow-coloured background, as before - but over the top of it we see a strange-pattern of vertical stripe(s). And the pattern changes. These vertical stripes are our first introduction to playfield graphics.
Have a good look at what this demo does; although it is only writing to a single playfield register (PF1) which can only hold 8 bits (pixels) of playfield data, you always see the same stripe(s) on the left side of the screen, as on the right. This is a result, as noted earlier, of the TIA displaying its playfield data twice on any scanline - the first 20 bits on the left side, then repeated for the right side.
Let's walk through the code and have a look at some of the new bits...
PATTERN = $80 ; storage location (1st byte in RAM) TIMETOCHANGE = 20 ; speed of "animation" - change as desired
At the beginning of our code we have a couple of equates. Equates are labels with values assigned to them. We have covered this sort of label value assignation when we looked at how DASM resolved symbols when assembling our source code. In this case, we have one symbol (PATTERN) which in the code is used as a storage location
sta PATTERN
... and the other (TIMETOCHANGE) which is used in the code as a number for comparison
cpy #TIMETOCHANGE
Remember how we noted that the assembler simply replaced any symbol it found with the actual value of that symbol. Thus the above two sections of code are exactly identical to writing "sta $80" and "cpy #20". But from our point of view, it's much better to read (and understand) when we use symbols instead of values.
So, at the beginning of our source code (by convention, though you can pretty much define symbols anywhere), we include a section giving values to symbols which are used throughout the code. We have a convenient section we can go back to and "adjust" things later on.
Here's our very first usage of RAM...
lda #0 sta PATTERN ; The binary PF 'pattern'
Remember, DASM replaces that symbol with its value. And we've defined the value already as $80. So that "sta" is actually a "sta $80", and if we have a look at our memory map, we see that our RAM is located at addresses $80 - $FF. So this code will load the accumulator with the value 0 (that's what that crosshatch means - load a value, not a load from memory) and then store the accumulator to memory location $80. We use PATTERN to hold the "shape" of the graphics we want to see. It's just a byte, consisting of 8 bits. But as we have seen, the playfield is 20 bits each being on or off, representing a pixel. By writing to PF1 we are actually modifying just 8 of the TIA playfield bits. We could also write to PF0 and PF2 - but let's get our understanding of the basic playfield operation correct, first.
lda #$45 sta COLUPF ; set the playfield colour
When we modified the colour of the background, we wrote to COLUBK. As we know, the TIA has its own internal 'state', and we can modify its state by writing to its registers. Just like COLUBK, COLUPF is a colour register. It is used by the TIA for the colour of playfield pixels (which are visible - ie: their corresponding bit in the PF0, PF1, PF2 registers is set).
If you want to know what colour $45 is, look it up in the colour charts presented earlier. I just chose a random value, which looks reddish to me
ldy #0 ; "speed" counter
We should be familiar with the X,Y and A registers by now. This is loading the value 0 into the y register. Since Y was previously unused in our kernel, for this example I am using it as a sort of speed throttle. It is incremented by one every frame, and every time it gets to 20 (or more precisely, the value of TIMETOCHANGE) then we change the pattern that is being placed into the PF1 register. We change the speed at which the pattern changes by changing the value of the TIMETOCHANGE equate at the top of the file.
That speed throttle and pattern change is handled in this section...
; Handle a change in the pattern once every 20 frames ; and write the pattern to the PF1 register iny ; increment speed count by one cpy #TIMETOCHANGE ; has it reached our "change point"? bne notyet ; no, so branch past ldy #0 ; reset speed count inc PATTERN ; switch to next pattern notyet lda PATTERN ; use our saved pattern sta PF1 ; as the playfield shape
This is the first time we've seen an instruction like "inc PATTERN" - the others we have already covered. "inc" is an increment - and it simply adds 1 to the contents of any memory (mostly RAM) location. We initialised PATTERN (which lives at $80, remember!) to 0. So after 20 frames, we will find that the value gets incremented to 1. 20 frames after that, it is incremented to 2.
Now let's go back to our binary number system for a few minutes. Here's the binary representation of the numbers 0 to 10
00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001
00001010
Have a real close look at the pattern there, and then run the binary again and look at the pattern of the stripe. I'm telling you, they're identical! That is because, of course, we are writing these values to the PF1 register and where there is a set bit (value of 1) that corresponds directly to a pixel being displayed on the screen.
See how the PF1 write is outside the 192-line picture loop. We only ever write the PF1 once per frame (though we could write it every scanline if we wished). This demonstrates that the TIA has kept the value we write to its register(s) and uses that same value again and again until it is changed by us.
The rest of the code is identical to our earlier tutorials - so to get our playfield graphics working, all we've had to do is write a colour to the playfield colour register (COLUPF), and then write actual pixel data to the playfield register(s) PF0, PF1 and PF2. We've only touched PF1 this time - feel free to have a play and see what happens when you write the others.
You might also like to play with writing values INSIDE the picture (192-line) loop, and see what happens when you play around with the registers 'on-the-fly'. In fact, since the TIA retains and redraws the same thing again and again, to achieve different 'shapes' on the screen, this is exactly what we have to do - write different values to PF0, PF1, PF2 not only every scanline, but also change the shapes in the middle of a scanline!
Today's session is meant to be an introduction to playfield graphics - don't worry too much about the missing information, or understanding exactly what's happening. Try and have a play with the code, do the exercises - and next session we should have a more comprehensive treatment of the whole shebang.
==============================
Exercises
1. Modify the kernel so that instead of showing a rainbow-colour for the background, it is the playfield which has the rainbow effect.
2. What happens when you use PF0 or PF2 instead of PF1? It can get pretty bizarre - we'll explain what's going on in the next session.
3. Can you change the kernel so it only shows *ONE* copy of the playfield you write (that is, on the left side you see the pattern, and on the right side it's blank). Hint: You'll need to modify PF1 mid-scanline.
We'll have a look at these exercises next session. Don't worry if you can't understand or implement them - they're pretty tricky.
==============================
Subjects we will tackle next time include...
* The other playfield registers (PF0, PF2)
* The super-weird TIA pixel -> screen pixel mapping
* Mirrored playfields
* Two colours playfields
* Asymmetrical playfield
See you then, then!















