Help keep the free Atari 2600 programming episodes and code coming by supporting the channel on Patreon.
In this episode we explore how to add color to our graphics, the pitfalls of poor timing, spanning multiple scanlines, drawing duplicates of our graphics and spacing them apart. Lots of information is packed in here and plenty of code examples in our github.
Released:
November 15, 2021
Original Link:
More Sprites & Color on the ATARI VCS
GitHub Link:
s01e07 - Adding Color to Player Graphics
This episode is part 2 of 'Drawing Sprites on an ATARI VCS?'
Welcome back, everyone! In this episode, we’re going to continue exploring how to draw graphics on the Atari VCS, some difficulties you may encounter, and some really interesting modifiers you can apply to your player, missile, and ball graphics. Stick around because we’ll be taking you on a wild ride as we attempt to get our graphics from our code onto the screen. Let’s get started.
In the last episode, we introduced the player graphics registers, briefly touched on the missile and ball registers, and learned how to position our graphics along the X and Y axes. If you missed that episode, you can scan the QR code on the screen or find the link in the description below. We ended the episode with a quick example of a two-dimensional sprite as we smoothly scrolled it across the screen. Let’s look at another example of a two-dimensional sprite and see if we can add a little colour to our players.
Let’s start by defining what our graphic is so we can better understand the issues we need to address when drawing it. As we know from the previous episode, the player graphics are 8-bit registers, with each bit representing a pixel that’s either on or off. With this, we can use multiple bytes to define our two-dimensional graphic. Here’s a basic cat graphic made with several bytes represented as binary values, and this is what we’d expect to see when it’s drawn to the screen. However, due to the limited number of cycles we have per scanline, we need to be very careful with how we choose to draw our sprites.
At first glance, we could do something basic, like checking which scanline we’re on and whether the graphic should be on it, by maintaining an index of which line of the graphic we’re currently on. We could implement this by first initializing our graphic index to zero and then setting the default value to write to the player graphics register to zero as well. We then load the scanline and subtract the Y position of the player graphic from the current scanline.
If the result of the subtraction is less than 0, then our scanline is already past our graphic, so we would jump down to clear the bits on our player graphic. If it’s more than zero, we’ll subtract the height of the graphic to determine if we need to draw a line. If so, then we’ll load the current index for the graphic and use that to obtain the correct byte of our player graphic. When we have that, we’ll increase the graphic index for the next pass.
After that, all we need to do is wait for a horizontal blank, write our player graphics to the register, move down to the next scanline, and then jump back up to the start of our kernel to work on the next scanline. Now, this doesn’t look like much, but it uses a lot of machine cycles, and we’ll probably want to save some of those to handle other objects we might also want to draw on the screen. In fact, adding up all those instructions brings us to a whopping 39 machine cycles when we’re drawing our cat and 41 maximum cycles when we’re not. That’s more than 50 percent of our available cycles per scanline. In the end, though, if we execute our code, we’re introduced to our brand-new buddy.
Using this method to draw our graphics is very limiting. At best, we could probably squeeze in a second player graphic on the screen, but that wouldn’t leave time for any playfield graphics, colour, player missiles, or the ball. So we’ll need to revise how we approach this.
So this time, we’re going to do a couple of things differently. The first change may seem a little weird, but we’re going to flip our graphic definition. This is due to the calculation we’re going to perform to determine which line of our graphic to draw on each scanline. So we’ll carefully reverse the order of the bytes to start at what will be the bottom of the graphic. We’ll also add a zeroed byte at the beginning to use to clear the graphics register when we’re not drawing.
Second, we’re not going to check which scanline we’re on. Instead, we’re going to create an offset variable that will count upwards towards zero. More specifically, it will count up to 255, which is the maximum value an 8-bit register can hold, and when increased again by one, it rolls over to zero. We get this offset by creating a copy of our graphic’s Y position at the start of the frame and increasing it by one on each scanline. When it rolls over to zero, we display a line from our graphic.
This brings us to our third change: we’re going to replace our index that tells us which line of the graphic to draw with a simple calculation. Now, we’re going to determine which line to display by subtracting our temporary Y position from the total number of lines in our graphic. The way we’ve set this up, on lines where our graphic should be drawn, the result of that calculation will be the index of the line in our graphic to draw. Any other result will be negative, in which case we’ll clear the graphics register and draw nothing. So let’s have a look at how that would work.
We start by loading our Y position and storing it in a new temporary variable. Then, for each scanline, we load the height of our player graphic, set the processor status register’s Carry flag, and use the ISB instruction to increase our temporary Y position by one and then subtract it from the height of the graphic. The result of this calculation will be stored in the A register. We’ll talk more about the ISB instruction in a future episode where we’ll cover illegal opcodes of the 6502 processor.
After ISB is executed, it will set the processor status Carry flag if the instruction resulted in the register rolling over. Then we override the results of our calculation currently in the A register and set it to zero to pull the first byte of our graphic, which we had set to eight empty bits. Next, we’ll transfer our new graphic index from the A register to the Y register.
We do this because the load instruction can only use the Y register to pull a memory offset. So, in this case, we’re asking it to give us the value located at the nth byte starting at the memory address represented by Cat_A. After this, we just wait for the next horizontal sync, push the graphics byte to the Graphics Player 0 register, and finish up the rest of the scanlines. All these changes reduce our maximum cycle count from 41 down to 34, a modest improvement.
We’ve given ourselves a little more breathing room to add complexity to our graphics, so how about we give our little buddy some colour? One of the limitations we have with our player graphics is that there can only be one colour per scanline. There’s just no time to change our colour mid-graphic on a scanline. There is a way to fake this using two overlapping player graphics. This was used in a few games, but we’ll touch on that in a future episode. For now, we’ll apply our colour at the beginning of each scanline by writing a value to the Colour Player 0 register. We’ve already seen this in our previous video, but this time we’re going to create a data table with the colour values.
Just like we did with the cat graphic, our data table will be a list of 11 bytes in reverse order, with the first byte being a placeholder so the graphic and colour data tables are the same length, avoiding any additional calculations. In our kernel, we’re going to make only a few small adjustments. First, after we load the current scanline’s graphic, we’re going to move that value into the X register to save for later. Then we’ll use the same graphic line index we stored previously in the Y register to load the corresponding colour value from our new data table.
Now all we need to do is wait for the horizontal blank and then apply our changes. First, we write to the Graphics Player 0 register, which will be set with our value stored in the X register, and then to the Colour Player 0 register with the value currently in the A register. With this done, we can run our code and see our little orange buddy with a white-tipped tail and toes.
We’ve managed to bring our buddy into the Technicolor world. Now let’s see about giving him a friend to keep him company while using the last few remaining cycles we have. It’s going to be tight, but there are still a few tricks we can use to help us out.
Just like we did with our cat graphic, we’re going to create a data table to define bits in the shape of a rough estimation of a dog. We’re going to reverse the order of bytes in our data table, just like the cat, because we’re going to build upon the base we’ve already created. This time, we’ll create an extra variable to temporarily store Player 0’s colour in memory and load it again when we need it. Additionally, we’ll need to create variables to hold our Player 1 Y position, as well as our temporary copy of the Player 1 Y position.
Our routine to get the next line of the cat graphic is almost the same, except we will store the line colour in our temporary colour variable, and after we load the graphic line, we immediately store it in the X register. We need to reuse the A register later for our dog, so we need to shuffle the registers a bit. We can’t use the Y register for this purpose because the load A instruction can only use the Y register for the memory offset when loading our graphic byte from its data table location in memory.
Now we can work on our routine to draw our dog. Most of this is the same, except we’re not yet implementing our dog in colour, so we’re not loading from a colour table. After the horizontal sync, we’re pushing our Player 0 graphic from X, our Player 1 graphic from the accumulator, and then loading and pushing our Player 1 colour. When we execute our code, we can see our buddy has a new companion. Now that we have our dog on the screen, let’s add a little colour as well.
Just like we did with our cat, we’ll create a data table for our new friend to hold the colour values for his graphics. We’ll also use another variable to temporarily hold the Player 1 colour until we need to use it. Looking at the drawing routine again, we’re now loading our graphic colour and storing it in a temporary variable, loading the graphic into the accumulator, and then loading our cat’s colour from its temporary variable into the Y register. After our horizontal sync, we push our player graphics into their registers, our Player 0 colour from the Y register, and then load and push our Player 1 colour. That’s all it takes to draw two player graphics with colour. Except, it doesn’t work.
When we added the extra code we went over the maximum machine cycles for a single scanline, ending up with a total of 94 cycles in our kernel!
We’ll need to carve out 16 cycles to fix this, but there’s not too much we can do. If we revisit our code, there’s one interesting feature of the TIA that we can try. There are three registers that allow you to delay the drawing of the graphics. These are Vertical Delay Player 0 and 1 and Vertical Delay Ball.
Vertical Delay Player 0 will delay writing the Graphics Player 0 register until the Graphics Player 1 register is written, while Vertical Delay Player 1 works in the opposite manner. Vertical Delay Ball will delay writing the Enable Ball register until Graphics Player 1 is written. Each of these registers can initialize its delay by setting the first bit of the register.
We’ll initialize the delay on our Graphics Player 0 to wait until Graphics Player 1 is written. This will be done outside of our kernel by setting the first bit of Vertical Delay Player 0. Down in our kernel, we can now change our cat drawing routine to immediately store our player graphic directly in Graphics Player 0. This means that we can save some cycles by not having to save the value and then reload and store it again. By tweaking our code and moving some things around, we’ve managed to reduce our kernel to exactly 78 machine cycles. So let’s execute our code and bring our dog into our colourful world. Except, it still doesn’t work.
The reason for this is pretty simple. While our kernel is 78 machine cycles, the last three are used to write to WSYNC, which means that by the time WSYNC actually executes, we’ll already be on the next scanline, resulting in the instability we see here. There is one way we can fix this: by removing the WSYNC.
If we can pad our kernel to be exactly 78 machine cycles, then we don’t even have to wait for the horizontal sync because we’ll already be hitting it. So, by removing WSYNC, all we need to do is use another three cycles, and we can do this by using a store instruction, which takes three cycles. For our purposes, we’ll just duplicate the last write to the Colour Player 1 register. Now, when we execute the code, we can see that we’re finally generating a stable screen, and we have our two friends in full colour.
So we have a stable screen, but again, we’ve run out of machine cycles to use for anything else.
Another approach we can take is to use two or more full scanlines for each graphic line on our screen. We can do this by adding another WSYNC sometime after writing to the player graphics registers or padding our code again to use an additional 78 machine cycles.
However, the consequence is that each line of our graphics is now at least twice the height. Now that we have our two slightly larger friends, let’s make a slight adjustment so they can face each other. The Reflect Player 0 and 1 registers will flip the order of bits when drawing the player graphics. We can turn reflection on by setting bit four of the reflect registers.
As we can see in this example, our cat is now facing our dog. They look happy enough, but let’s see about adding some missiles! Just like the player graphics, the player missiles are positioned horizontally on the screen in exactly the same way. But there are two really big differences.
The first difference is the size of the graphic. While the player graphics are each eight bits, the missile graphics are only a single bit. You can either turn them on or off. This is done by setting the second bit of the Enable Missile 0 or 1 registers. The second difference is how we set the colour of each missile, or rather, how we don’t set it. You see, the missile graphics don’t have their own colour registers. They use the same registers as the player graphics. So if you want to change the colour of Missile 0, you need to change the colour of Player 0, and vice versa.
Here’s an example of our two friends, along with their missiles drawn behind them, reaching all the way from the top to the bottom of the screen. As you can see here, the scanlines that are shared between each player and their missile use the same colour.
We’ve already covered how we can position missiles on the screen just like we do with the player graphics, and we move them around the same way, but there is another method. Each missile has a reset register associated with it. Writing a value to the second bit of the Reset Missile to Player 0 or 1 registers will reposition the missile to the centre of the player’s graphic. So instead of positioning the missiles separately, we can just reset them to be drawn along with the player graphics. In this example, we can see both firing a missile at each other, which moves across the screen for 60 frames and then resets to the player positions.
Along with the two missiles, we also have the ball graphic we can use. The ball graphic works in exactly the same way as our missiles when it comes to placing it horizontally using the Reset Ball register and showing it on the screen using the Enable Ball register. Just like the missile graphics, our ball graphic doesn’t have its own colour register. Unlike the missile graphics, instead of using the player colour, it uses the playfield colour.
So now that we’ve seen how to position and draw our two players, their missiles, and our ball, we can talk about another graphics register we have at our disposal: the Number and Size register. This register is packed tightly with features. Not only can it change the size of your players and missiles, but it can draw duplicates of them on the screen at the same time. This register only affects the player graphics and the missiles.
However, we can also change the size of the ball graphic, but that’s done through the Control Playfield register. We included these three registers together because the attributes used for modifying the missile size are the same as those used to modify the ball size. But let’s first have a look at changing our player graphics size.
Looking at the player width values, we can see that we have three sizes available to use: normal size, where one bit is one colour clock in length; double size, at two colour clocks in length; and quadruple size, at four colour clocks in length. Likewise, the missiles work the same way, with normal, double, and quadruple size, and this time also octuple size. When it comes to the ball graphic, we’re just writing these same values to a different register, but the outcome is the same: normal, double, quadruple, and octuple size.
Along with increasing the size of our graphics, we can also make duplicates of the player and missile. We can have just a single graphic, two, or even three. Not only that, but we can adjust their spacing with two copies close together, medium spacing, or wide spacing. If you’re not content to settle for only two copies, we can push this to three copies close together or with medium spacing. You may have noticed that the binary values for each of the attributes don’t conflict with each other, which enables us to combine attributes. Here’s an example of a quadruple-sized, tripled player with octuple-sized missiles.
This has been a huge episode, and we’ve covered a lot of material. We’ll do a deeper dive on each of these topics in future episodes, but in the meantime, our GitHub has been updated with nine more examples covering everything we’ve talked about in this episode and more. Feel free to pull the examples to try and modify them for yourself. Each example has plenty of comments, and if you have questions about them, feel free to let me know in the comments below.
I want to thank everyone for your continued support. 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 just getting started, and there’s a long way to go! As always, the example code for this episode and all the previous episodes is available on our GitHub, which is linked in the description below.
I’d like to end this episode with a question. What’s the very first game you played on an Atari VCS? Where were you when you played it, and who was with you? Whether it was on an original Atari VCS in 1978 or playing one for the first time at a retro gaming meetup, please share your memories in the comments below.
So, thanks for watching, and I’ll catch you next time!