The code used for this guide was created for the ca65 cross-compiler. I used Visual Studio Code as my text editor with git version control. For use on r36 of the X16 emulator.
Finally I managed to get sprites working on the X16. A guide will follow soon. One of the issues I had was figuring out where I could put my sprites in VERA’s VRAM.
|$00000 – $1FFFF||Video RAM|
|$F0000 – $F001F||Display composer registers|
|$F1000 – $F11FF||Palette|
|$F2000 – $F200F||Layer 0 registers|
|$F3000 – $F300F||Layer 1 registers|
|$F4000 – $F400F||Sprite registers|
|$F5000 – $F53FF||Sprite attributes|
|$F6000 – $F6xxx||Audio|
|$F7000 – $F7001||SPI|
|$F8000 – $F8003||UART|
Video RAM is used by VERA to hold the image that is displayed. There are a number of different modes and they use different parts of Video RAM. By default the X16 uses 80×60 characters. 80×60 text character has 4800 characters so 4800 bytes. Each character will need 1 byte for colour data, so another 4800 bytes. A total of 9600 bytes. Converting 9600 decimal to hex is $2580. However I found that wasn’t the case.
When using 40×30 character mode should only use 1200 for character data and 1200 for colour data. So 2400 bytes, so from $960.
So I tried using 320×200 256 colour mode. I put the sprite up at $10000 things looked interesting as well.
I clearly didn’t understand how VERA was working. I found an interesting video CX 16 VERA VRAM Allocation Guide Mk2 by Oziphanto. However it didn’t help me with understanding what I was doing wrong. So I watched Hardware Sprites on the Commander X16 by ChibiAkumas. Which while interesting also didn’t explain to me which parts of VRAM I could use.
So decided to work it out with trial and error. I found for 40×30 mode that I can use from $1e00-$1ffff for sprite data. In 80×60 mode I can use from $3c00-$1ffff for sprite data. In 320×200 mode I could use from $0000 to $ffff. I could not use from $10000 to $1ffff.
After I posted the initial version of this document SlithyMatt on the forums pointed out to me how VERA actually does 80×60 and 40×30 modes. 80×60 is achieved by switching to 640×480 and using tile maps for each character. To get 80×60 VERA uses 128 x 64 tiles. 40×30 is done by switching to 320×240 and using 64×32 tiles.
With this new understanding, how much VRAM does 80×60 use? 128x64x2 gives us 16384 bytes, 16k, $4000. I’m sure now I had been over writing tile data with my sprite and if I had scrolled I would have seen it. When we view the X16 screen we are only seeing a portion of the total. The image below shows what is happening with the tile map and the viewable area.
How much VRAM does the 40×30 screen mode use? Well it turns out it uses the same amount. The image below shows what is happening with the tile map and the viewable area. The same tile map is being used. You can place your sprite data at $4000 and you should be ok.
In the Programmers reference guide in the Video programming there is a note that the kernal loads the PETSCII character sets in to VRAM at $0f800. So there a small amount of space we should not overwrite if we want to use text.
In theory I should be able to use space after the display composer registers, f0020 to f0fff. I should also be able to use the space after each of the Layer registers. f2010 to f2fff and f3010 to f3fff. Each of those pockets should hold 3k each.
The end result is that for each game you will have to work out which space works for you based on which graphic mode are in and which features you need to allocate VRAM to.
As I was about to post this article I saw that Emmanuel had posted a link to this youtube video Explore Video Memory from the Commander X16 in ASMFun. This appeared to answer my questions. I had tried a much earlier version of ASMFun and I have to say the latest version with the VERA VRAM allocation is super useful on Windows 10.
If other people have ideas on how VRAM is being used or if I am wrong about something please comment so that I can fix up this guide.
Thanks to SlithyMatt from the forum.