Noticed that the GBAX competition is on again. Got until July, so I might try to get Defender DS finished in time for that.
I’ve been temporarily distracted by 6502 assembly, though. I went on a hiking/camping trip to Brecon, and ended up spending one of the more rainy days wandering around Hay-on-Wye’s bookshops. Last time I went (I must have been about 14) I remember nearly buying a book on m68k assembly - in the end I didn’t bother because it suffered from the usual problem these books have:
The first chapter explains how to switch a computer on, whilst the second launches you into the most efficient way to swap two 32-bit integers using just one 16-bit register and a single memory address. So what happened to the chapter in between explaining all of this jargon?
This time I thought that, in all of those shops, I must be able to find something interesting.
As it turned out, there weren’t any interesting computery books. There were a few books on BBC-B programming, but who’d want to program one of those? I did manage to find a copy of iWoz, Steve Wozniak’s autobiography, for the bargain price of £2.95 (£10 off!). As I remembered wanting to buy it when I first heard about it I figured that I couldn’t turn it down.
The book turned out to be fascinating and entirely inspirational, so I heartily recommend that anyone interested in either the history of home computing or practical jokes gets hold of it. Woz goes into the design of the first two Apple computers in a reasonable amount of detail - it’s understandable for the kind of people who’d go out and buy a book about the history of Apple (the nutters). Part of that involves a discussion of the 6502 CPU, and that got me interested in it.
The 6502, compared with something like the 68k or even the Z80, is an incredibly simple chip. It’s completely 8-bit (none of this 16/32-bit hybridisation like the 68k, or 16-bit registers like the Z80). It’s got a total of three registers, can address 64K of RAM, and has roughly 50 documented instructions. It looks like a really good starting point for getting the hang of assembly language, or more specifically, how to translate high-level concepts like linked lists, structs, pointers, etc, into assembly. As a side effect, learning 6502 asm will put me in the position to code for the C64, Atari VCS, NES, SNES, BBC-B, Apple 1, and (I think) the Apple 2, Dragon and Tandy CoCo.
So, which one to concentrate on? The C64 looks to be simple enough. The VCS is weird - you have to slot your code into the space between horizontal scans of the TV’s electron gun, then process the HSYNC, then start again on the next scanline. Yikes. It’s also fantastically limited - it only gives you half a dozen movable objects to play with (that are explicitly designed to be a player graphic, a ball graphic or a missile graphic - spot the VCS’ heritage as a home Pong machine!), and backgrounds are even stranger. The VCS will draw the left side of the background, and then for the right side either draw the left side again, or flip the left side horizontally and draw it as a mirror image.
This raises the obvious question - how did they manage to achieve so many different games with such limited hardware? It also explains why the asteroid count doesn’t increase in VCS Asteroids…
The NES undoubtedly has daft graphics hardware, and the mapper business is off-putting. The SNES uses an 8/16-bit hybrid CPU based on the 6502, so that’d probably mean learning a modified version of 6502 asm. I’m not particularly interested in coding for the others.
So, C64 asm it is. As a wee bairn I had a Speccy, so this is a bit sacrilegious, but I’m sure the techniques I learn will translate to Z80 assembly.
The first such trick I’ve learned is how to find the integer root of a number. I remember the Newton-Raphson method from A-Level maths, but that’s an incredibly inefficient way of doing things, particularly if you’re not interested in the fractional part of the root. I’ve always wondered how you’d go about finding a square root more efficiently, and now I know.
Basically, take the number you want to find the root of, and keep subtracting sequential odd numbers from it until the next subtraction will result in 0 or a negative value. The number of iterations required is the integer square root. So, to find the square root of 25:
#___Value___- 1_____25____1 2_____24____3 3_____21____5 4_____16____7 5_____9_____9 6_____0_____11
The first column is the iteration number (excuse the formatting; WordPress doesn’t want me to show you this for some reason). The next column is the current value, and the last column is the odd number we’re subtracting. Start with a value of 25 and subtract the first odd number (1) and we get 24, which we use in the second iteration. Eventually we reach iteration 6 which gives us a value of 0. Since we don’t want to reach 0 or a negative number, this means that the square root is the last “good” iteration, number 5.
5 is the square root of 25.