Help keep the free Atari 2600 programming episodes and code coming by supporting the channel on Patreon.
In this episode we explore how the Atari VCS uses graphics registers to draw what we now call sprites. We'll examine Graphics Player Zero (GRP0), and the Horizontal Motion registers in the TIA to place our graphics anywhere on the screen.
Released:
August 30, 2021
Original Link:
Sprites on the ATARI VCS?!
GitHub Link:
s01e06 - Player Graphics and Horizontal Positioning
Welcome back, everyone.
In our last episode, we delved deeper into the playfield and examined how it was used in a few classic games. In this episode, we’re going to take a first look at drawing sprites and moving them around the screen. Trust me, if you’re unfamiliar with the process, you’re going to want to stick around. But first, a little history.
Atari founder Nolan Bushnell came up with the original concept of a sprite when developing the first arcade video game, Computer Space, in 1971. The rockets used in the game were bitmaps that moved around the screen independently of the background. The sprites were hardwired and controlled individually with a dedicated transistor, which allowed the background to be processed separately and the sprites to be composited with the background by the hardware.
Danny Hillis, an American inventor, entrepreneur, and scientist, coined the term “sprites” while working at Texas Instruments in the late 1970s. The label “sprites” was derived from the bitmaps floating around on top of the background without affecting anything below, much like a ghost, which can also be referred to as a “sprite.” The first commercial use of the term “sprites” was with the Commodore 64, which was released in August 1982.
Many game systems that followed would offer dedicated hardware sprites, right up to the Nintendo DS in 2004, which allowed up to 128 on the screen at the same time at a maximum size of 64 by 64 pixels. The Atari VCS, with its limited resources, only offered five dedicated hardware sprites. That’s a huge difference from the game systems that followed in its footsteps, but a big improvement over what was on the market at the time. Let’s have a closer look at what we have to work with.
The Television Interface Adapter, or TIA, on the Atari VCS provides us with the following hardwired graphics registers for our sprites. The largest of them are Graphics Player 0 and Graphics Player 1, referred to as GRP0 and GRP1, respectively. Each of these registers is one byte, or eight bits, wide, with each bit representing a single pixel that is either visible or hidden. The other three graphics objects are even more limited, coming in at only a single bit per register. We have the Graphics Enable Missile 0 and 1 and Graphics Enable Ball registers, or ENAM0, ENAM1, and ENABL, respectively.
Now, the height of these sprites doesn’t work like you may think. On most machines, a sprite is a two-dimensional representation of a game object, like a player or a car, with both a width and a height. A static sprite that’s eight pixels wide by eight pixels tall requires 64 bits. But on the Atari VCS, that’s many times more bits than we have to work with. Here, each player is only eight bits in total and represents a sprite that is eight bits wide by one bit tall: a single scanline.
Each bit of a graphics register takes a single TIA colour clock to render, giving the player object graphics a higher resolution than we saw in the playfield graphics, at one quarter of the width.
When it comes to moving the sprite around the screen, things get a little more interesting. On most other game systems or frameworks, we can place a sprite anywhere along the X and Y coordinates on the screen. We simply give it a position, and when it comes time to render the screen, the sprite manager blits the sprite right where you told it to, usually on a screen buffer. When the screen is rendered, it simply pushes that buffer to the screen all at once. This is technically impossible on the Atari VCS because (A) it doesn’t have a sprite manager, (B) it doesn’t have a screen buffer, and finally, (C) there is no X-Y coordinate system. There’s only the colour clock and the scanline.
Drawing the sprite at a specific scanline is fairly easy. Generally, we’re already counting the scanlines as we go, so we just wait until we get to where we want it and then tell the TIA to draw it. This gives us our Y position, but placing it horizontally along the X position is another matter.
You see, we need to tell the TIA when to draw the player graphics as it’s drawing the current scanline. As soon as we tell it to draw, it does so immediately and then continues to draw it at that position on every scanline forever, until you tell it to stop. It all comes down to timing. As we learned in a previous episode, the visible screen is 160 colour clocks wide horizontally, or rather 76 machine cycles. If we want to draw our player graphics in a specific location on the screen, we need to wait a certain number of machine cycles.
So what exactly is a machine cycle? The steps a processor executes for each instruction you give it are fetch, decode, execute, and store. The processor will first fetch the instruction from memory, decode the instruction into commands, execute the command, and then possibly store the results back into memory.
If we follow this piece of our code, it simply counts down from 10. When the BNE, or Branch Not Equal, instruction is executed, it checks whether the Zero flag has been set in the processor status register and branches accordingly. Using this as an example, we can get a feel for how many machine cycles are used per instruction. Here, we can see that the LDX instruction takes two machine cycles to assign the immediate value 10 to the X register. DEX takes two machine cycles to execute. BNE takes two machine cycles if a branch is not taken and three machine cycles if it is taken.
So let’s try calculating how many machine cycles this loop uses. The first iteration will use two to load the value 10 into X, two to decrement X, and then three to jump back up to our loop point, using seven machine cycles. The second through ninth iterations will use two to decrement X and then three to jump back to the loop point, using five machine cycles each loop for a total of 40 machine cycles. The last iteration will use two to decrement X and then two on the BNE instruction because the Zero flag on the processor status register has been set, so it will continue execution with the next instruction that immediately follows it. This iteration only uses four machine cycles.
Altogether, this simple loop uses 51 machine cycles. That’s 67% of the time it takes to draw the visible screen. That’s pretty tight, but luckily for us, we can stand on the shoulders of giants and learn the tips and tricks from over 40 years of game development for the Atari VCS.
So now that we know the theory behind positioning player graphics horizontally on the screen, let’s have a look at how this can be implemented. Today, we’ll only focus on placing Graphics Player 0 because it’s a pretty big topic all by itself. We’ll follow up with the other objects in a future episode, along with some of the modifiers available to change their appearance.
As we’ve already mentioned, the Graphics Player 0 register is eight bits, which represent the pixels used to draw a single scanline of our player. The other registers used to draw our player are as follows.
Reflect Player Zero is turned on by setting the D3 bit. When on, the bits in our player graphic will be reversed, meaning it will use the least significant bit first. Setting D3 back to zero will restore the bit ordering of the player to use the most significant bit first.
Reset Player Zero sets the horizontal position of the graphics to the current position of the beam on that scanline. This is a strobe register, so writing any value to this address will set the position.
Horizontal Motion Player Zero allows you to fine-tune the position of the graphic by up to eight pixels to the right or seven pixels to the left.
Horizontal Motion Clear will clear all horizontal motion registers, including the player and missile for both players and the ball as well. This is also a strobe register, so writing any value will clear the motion registers.
Horizontal Move will apply the fine-tuning set in the Horizontal Motion registers. This is a strobe register as well, so writing any value will apply the fine positioning. This should only be used directly after a WSYNC to ensure the operation starts at the beginning and finishes before the end of horizontal blanking.
With these 6 registers you will be able to place your player graphics anywhere on the screen… in theory… but in practice… it’s a little more complicated.
Let’s start with the Graphics Player Zero register, or GRP0 for short. As we know, it’s an 8-bit register representing eight colour clocks, or eight pixels on the screen. It’s fairly easy to work with because each bit is a single pixel, so if we load the register with an 8-bit binary value, then we’re assigning which bits will be drawn and which will be empty spaces. The colour used for these bits can be assigned to the Colour Player Zero register, or COLUP0 for short. In this case, we’ll use white.
Now we’ll just strobe the Reset Player Zero register, or RESP0 for short. A strobe register is a trigger that is activated when any value is written to it, so in this case, we’ll store whatever value happens to be in the accumulator register. This will trigger the TIA to start writing our player graphic on the screen wherever the beam currently is on the scanline. More precisely, it will be drawn after the 6507 interprets our instruction and the TIA prepares to draw. This takes about five colour clocks in all. If we execute this code, we can see a couple of things here.
First is the position of the graphic. Since we didn’t take into account any timing at all, it was placed on the screen where the beam happened to be at that time. If we want a bit more control, we’d place a WSYNC right before we call Reset Player 0. There we go, now it’s positioned at the left side of the screen. It’s a few colour clocks away from the edge, and there’s a reason for that, but we’ll cover that in a moment.
The second thing we need to observe is that the player graphic is being displayed on all scan lines at exactly the same place. This is because the TIA will continue drawing the player graphic in the exact same place until you tell it to stop. It maintains this horizontal position because the TIA stores this timing in an internal register.
Let’s introduce a little bit of timing to our code and get it positioned right where we want it.
As we saw moments ago, strobing Reset Player 0 right after a WSYNC sets the position of the graphic close to the left side of the screen. In fact, if we strobe the register at any point during a horizontal blank period, which is the first 68 clock counts of a scanline, then this is exactly where the player graphic will be drawn.
However, if we introduce a little delay after the WSYNC to bring us to a point after the horizontal blank, then we can position it farther from the left side. We can do this in our code by using the SLEEP macro. All this does is tell the assembler to insert a number of NOP instructions into the binary file. A NOP instruction is just that, No Operation; it just wastes machine cycles, more specifically two machine cycles. Just provide the SLEEP macro with the number of machine cycles you want to waste, and the function will insert the appropriate number of NOP instructions.
It’s important to note, however, that the SLEEP function is not practical for your game code, nor can you change the number of machine cycles it uses while your code is running. This is only used to pad your code with a specific number of machine cycles and can’t be changed later. So let’s change the number we’re passing to the SLEEP function and see what happens.
Anything that is 19 or below will take place during horizontal blank, so we need to set it after that. Let’s try 20. We can see that it’s moved over to the right by three colour clocks, or three pixels. By increasing this number even more, we can move it all the way over to the right.
So the question you may be asking yourself right now is, if each instruction moves by three colour clocks, is that the smallest move you can make? It seems a little choppy to me.
And the answer to that is no. The smallest move you can make is actually 15 colour clocks.
You see, you can’t write a game using the SLEEP function to move your player graphic because it can’t ever change. Your graphics will always be in the same place, and that’s just not a fun game.
In order to place the player graphic where we want it on the screen, we’ll need to create a routine that uses a variable number of machine cycles. What we need is a loop.
This is as basic as it gets. First, we set the position where we want to draw the player graphic in X, and then we decrement by one. If the decrement results in zero, then the Zero flag on the processor status register will be set, and Branch Not Equal will exit the loop. If it’s not zero, then execution will branch back to the loop label.
It’s such a simple loop, taking only five machine cycles: two to decrement the X register and three to take the branch back up to the start of the loop. Since there are three colour clocks per machine cycle, this leaves us with a granularity of 15 colour clocks. Now we can move the player graphic around the screen.
Still, that would be a pretty boring game if we had to jump around like that. Luckily, the TIA can help fill in the gaps, quite literally.
The architects of the TIA provided the ability to overcome the limitations of a 15-colour-clock granularity with the Horizontal Move Player 0 register, or HMP0 for short. It allows the TIA to adjust Graphics Player 0 to the left by seven colour clocks, to the right by eight colour clocks, or not to adjust the position at all.
This is an 8-bit register, but it only uses the four high bits to specify the adjustment you want to make. Looking at this chart, we can see that setting all four bits to 0 will disable any adjustment, but setting them all to 1 will adjust the player graphic one colour clock to the right of the coarse position we set when strobing Reset Player 0.
The value stored in Horizontal Move player 0 will remain until you change it, However, the adjustment will not be applied unless you strobe another register called Horizontal Move.
Horizontal Move should only be strobed during horizontal blanking so you should do this immediately after a WSYNC. Strobing Horizontal Move will also move all of the other graphics that have had their only horizontal adjustment register set. Like the other player, missiles and ball graphics.
Taking a look at the third example for this episode, we are first loading the X register with all four high bits set, which will move our graphic by one colour clock to the right. Then, down in our main loop, we strobe the Horizontal Move register right after a WSYNC. By doing this, we’re strobing Horizontal Move on every frame, moving the graphic by one colour clock each time.
What we observe here is that the TIA remembers where the player graphic was last positioned and then applies the adjustment to that position, moving the graphic across the screen in a smooth scroll one pixel at a time. The best part about this is that we only set the Player 0 graphics position once during initialization and then used the player graphics Horizontal Move register to move it around the screen, taking only three machine cycles per frame.
Now we have the capability to position a player graphic anywhere horizontally on the screen… but playing with a long line on the screen still isn’t very interesting after a while, no matter how smoothly it scrolls… but, with a little trickery…. we can turn what we have now into something a little more interesting.
We’ll examine the code for this in the next episode, but it’s available right now in our Git repository. Check it out! In this episode, we’ve only focused on the Graphics Player 0 object, but everything we’ve covered so far works about the same for Graphics Player 1, both player missiles, and the ball graphics. Next episode, we’ll also cover the other objects, size modifiers, creating bitmaps for our objects, and sprucing them up with a little colour.
If I’ve piqued your interest in developing for the Atari VCS, please let me know by liking and sharing this video, and if you haven’t done so already, please consider subscribing to the channel. We’re slowly growing together, and I’m really looking forward to what we can become. As always, the example code for this episode and all the previous episodes is available in our Git repository, which is linked in the description below.
Thanks for watching, and I’ll catch you next time!