AWS Logo
Menu
Back to the future: Writing 6502 assembler with Amazon Q Developer

Back to the future: Writing 6502 assembler with Amazon Q Developer

In this short post I have some fun with Amazon Q Developer and get it to write code that runs on my virtual Commodore 64

Ricardo Sueiras
Amazon Employee
Published Nov 4, 2024
Have you heard about the AWS Game Builder Challenge? This is a great competition where you need to create a game with AWS services of your choosing: all skill levels welcome!
In a recent post I talked about how I was able to quickly create a sideways scrolling arcade game with the help of Amazon Q Developer. I was talking to a few folk online who had got in touch to know more, and the subject turned to whether Amazon Q Developer could write a different kind of game, one that would run on the first computer I ever owned, the Commodore 64. Well there was only one way to find out, so I spent around an hour experimenting and this post is what I learned. Buckle up for some retro computing fun!

Writing my first C64 Basic program

After creating a new project workspace and starting VSCode, I ask Amazon Q Developer in the chat interface the following prompt:
Amazon Q Chat > Write a simple programming in Commodore 64 basic that displays a sprite moving across the screen smoothly. Make sure the screen is reset first and set to a random colour
Initial prompt in Amazon Q chat interface
I grab the code and start VICE, which is my preferred C64 emulator. I select the standard C64 from the available emulated machines, and then paste the code in. Oh dear, this does not look good.
entering upper case code
I ask Amazon Q to suggest a fix, and it provides a bunch of ideas, but the one that immediately seems to make sense to me is this line:
Replace uppercase letters with lowercase (the C64 automatically capitalizes)
I ask Amazon Q to change the code to lowercase, and try again. Success this time, and now I have the code that Amazon Q generated in my virtual C64. Here is the code it produced.
It runs.....well almost. I see a square sprite move smoothly across the screen and when it gets to the end it crashes with the following error:
Illegal quantity error in 200
I turn to Amazon Q to help me fix this issue, asking it to resolve this error. It quickly provides me with updated code and an explanation of why I got the error.
Here's what this does: (x>255) is a boolean expression that evaluates to -1 when true and 0 when false in Commodore 64 BASIC. The minus sign (-) in front of (x>255) negates this, giving us 1 when x > 255 and 0 when x <= 255. This result (either 0 or 1) is then used as the value for the POKE statement. This correction ensures that the high bit of the sprite's X coordinate is set correctly when the sprite moves beyond the 255th pixel, allowing it to move across the entire width of the screen.
I run it again and success.

Making it more interesting

I want to make this a little more interesting so I ask Amazon Q to change the code:
Amazon Q Chat > can you update the code so that there are four sprites in different colours this time. can you make it so that they bounce around the screen
It provides me some updated code
Which runs first time. I know have four sprites bouncing around the screen.

Changing the Sprites

The next step
Amazon Q Chat > can you tell me about any online C64 sprite editing tools that I can use to help me create my own sprites?
It provides some great resources, including a website called spritemate, which I head over to and quickly put together a more interesting looking sprite.
using spritemate to create sprites
I can grab the data directly from the tool, and then update my code.
One of the neat features of these emulated computers is the ability to change the CPU speed. The above videos are all running at 800% of the native C64 6502 speed. When running at baseline, the sprites move much more slowly. This is to be expected - back in the day, you used Basic for simple programs where performance was not necessarily critical. If you needed that, you had to learn assembler.

Writing 6502 Assembler

I have no clue about how to approach this in VSCode on my Mac. After looking at some Redit groups, it is clear that the way forward is to use the Kick Assembler tool together with a VSCode plugin. Once I have those installed and configured I am ready.
I select all the code and Send to Prompt, then ask Amazon Q:
Amazon Q Chat > convert this commodore basic program to run 6502 assembler that will run on the Vice c64 emulator. I want to use the Kick Assembler tool.
I am super impressed that it quickly provides something I remember from my teenage years
I compile the code and then automatically run it via the VSCode plugin integration, and success, we have our sprite bouncing across the screen.
It is not perfect, as the updated code has lost a few things: the background colour and three sprites! However, this is now running at 100% speed, so you can see how dramatically better the performance is.
I ask Amazon Q to update the assembler to add additional sprites.
Amazon Q, Send to Prompt > Update the C64 assembler so that there are three sprites rather than one.
I have to have some back and forth, as Amazon Q gives me just snippets of the updated code. I follow up with the prompt:
Amazon Q Chat interface > provide complete code update
Which provides me with the updated code, which compiles and runs.
As you cn see, running in assembler is much faster than when running this code in Commodore basic.

Wrapping up and next Steps

In this post I showed you how you can use Amazon Q Developer to help you write code, even if that code runs on a machine that is over 40 years old. Amazon Q Developer is simple an awesome companion that I am finding is able to help me across any task I throw at it - including creating Commodore 64 basic and assembler applications.
You might have some legacy code, perhaps some assembler or old code that you have been looking to modernise, update, or even figure out how to refactor. I hope this post will give you with ideas of the potential that tools like Amazon Q Developer provide.
You can try Amazon Q Developer for free today, by signing up for a Builder ID, and then using that to login in after installing the plugin in VSCode or Intelij.
Check out my other posts on community.aws, as I have been sharing a lot of tips and use cases of how I am using Amazon Q Developer. You can also keep up to date with all the new features and improvements of Amazon Q Developer by checking out the changelog.

Call to action

If you're ready to build your own game, AWS is running a hackathon through the month of November. From a simple tic tac toe game built with HTML/CSS/JS to a complex Unity game -- all skill levels and tech stacks are welcome. Join now on DevPost: https://awsdevchallenge.devpost.com! and I hope this post will inspire some of you to join too and maybe submit some retro games!
 

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

3 Comments