Task: Interact with Atari’s 6502 Processor in BASIC XL
Needed: A BASIC XL cartridge or Altirra emulator
Time: 10-15 minutes
The 6502 processor developed by Chuck Peddle and his team at MOS Technology was the first inexpensive CPU available in the 1970s. This CPU helped usher in the era of home computers and was featured in the Atari 8-bit line among others. This year (2020) is the 45th anniversary of the release of the 6502. As such, I started thinking about a software demo that would allow users to interact directly with the 6502 as a way to learn about this revolutionary chip. Unfortunately, it is not easy to interact directly with the chip’s components from BASIC. So, I wrote a program I call ‘The Interactive 6502’ that features a point-and-click interface controlled from a joystick that allows users to call assembly language commands which themselves directly interact with the 6502, with results passed back to BASIC and displayed on the screen for the user.
The Interactive 6502 (I6502) is programmed entirely in BASIC with calls to small assembly language programs stored in strings. I have a previous post that covers how to do this. Each assembly language program executes an assembly op code. I have only included those directly involved with manipulating the registers of the 6502 for simple arithmetic operations. For example, op codes such as INX for increasing the X register and LDA for loading the accumulater (A) with a value from a particular memory location are provided. Although not complete, the set of assembly op codes provided should give the user a sense for how the 6502 is programmed using machine language. Further, the user will gain the satisfaction of interacting directly with the 6502 processor from a user-friendly interface.
If you have never done assembly language programming before, this is a great way to get familiar with several components of the language and the logic. One simple exercise to try is to add 2 + 2. There are several ways to this. One approach is to click INC twice. This increases the value in memory location 203 (from page 0) from 0 to 2. You can then click on LDA to load the contents of that memory location into the accumulator (A) by transferring it across the data bus. You can then click on the ADC to add the contents of memory with that of the accumulator. The result in A should now be 4. Arithmetic done! Can you find several other ways to do this?
Open Altirra and load BASIC XL. You can do this from Altirra using the File->Attach Cartridge menu item. Note that this program ONLY works with BASIC XL since I used the FAST command to speed up the line numbering and GOTOs.
Boot the ATR file in Altirra or on your Atari from your PC using SIO2PC or a flash device. You must have a BASIC XL cartridge or ROM loaded if booting from your Atari. You can also copy and paste the code from the text file. However, the cut and paste approach is slow and will take several minutes to complete.
This was a fun project to code. A few notes. First, I have limited the range of values in memory and the registers from 0 to 9. Keep this in mind if you try to add or subtract numbers that will go negative or generate numbers larger than 9. Second, I did not include the ability to provide your own number to load into one of the registers. In assembly, you can load the accumulator with a decimal value that you provide. I thought about various ways to do this, but in the end decided to keep it simple and point-and-click. Third, you can reset the values by pressing START. Finally, if you examine the source code you will see how I interact with the assembly language routines and thus the registers from BASIC. What I do is set up page 0 memory locations for the memory (203), X (204), Y (205), and A (206). This is where BASIC gets the values to display on the screen. When I want to call an op code such as INX, I must first load X with the value in memory location 204. With that value in the X register I can then call INX. I must then load that result back into memory location 204 so BASIC can see it. I do not know of a way for BASIC to look directly at the X register.
Have fun! I hope you find this useful and interesting.