
Sweet 16: The 6502 Dream Machine (1977) - jacquesm
http://amigan.1emu.net/kolsen/programming/sweet16.html
======
reidrac
I've been working for the last two months in a personal project to build a
8-bit microcomputer based on AVR MCUs. Because these MUCs use a Harvard
architecture I implemented a 6502 VM to run programs from external SRAM.

But I digress; the relevant bits for this story are that I'm using CC65, a
compiler for the 6502, so I can write programs in C for my toy computer.

It's impressive the amount of stuff CC65 does in its runtime just to implement
C. Sure the 6502 is a beautiful processor (I loved implementing the VM!), but
it is also very limited.

Just an example: C65 implements its own stack in regular memory space because
the actual stack of the 6502 is limited to 256 bytes!

Every time you pass a pointer as a parameter in a function call the compiler
loads the 16 bit address in A and X and pushes it into its own stack, where
the the destination will access to it as needed.

Look at the pushax implementation here:
[https://github.com/cc65/cc65/blob/master/libsrc/runtime/push...](https://github.com/cc65/cc65/blob/master/libsrc/runtime/pushax.s)

All that just to "push value in a/x onto the stack". Amazing :)

EDIT: typo

~~~
6502nerdface
The cc65 project also has a very excellent 6502 assembler, ca65, which I find
quite nice when writing assembly by hand. Together with ld65 it implements a
full unix-style module/library-archive stack for 6502 object code. The
assembler's macro system is quite well developed, as well, to the point that I
prefer using it over cpp.

~~~
JoachimS
Do you prefer it over KickAssembler?
[http://www.theweb.dk/KickAssembler/Main.php](http://www.theweb.dk/KickAssembler/Main.php)

------
xioxox
Some more detail here:
[http://www.6502.org/source/interpreters/sweet16.htm](http://www.6502.org/source/interpreters/sweet16.htm)

------
kps
The Internet Archive has the November 1977 issue of _Byte_ , containing this
article on page 150, here: [https://archive.org/details/byte-
magazine-1977-11](https://archive.org/details/byte-magazine-1977-11)

------
jacquesm
I've posted this because I really like the elegant way in which he expanded
the 6502 instruction set. Something similar could be done with just about any
other processor.

~~~
zellyn
If you like the "call a subroutine, subsequent data is interpreted as custom
instructions" on the 6502, check out Super-Mon / NakedOS by Martin Haye. Very
nicely done, incredibly minimal programming environment / OS for the Apple II:
[https://bitbucket.org/martin.haye/super-
mon/wiki/Home](https://bitbucket.org/martin.haye/super-mon/wiki/Home)

------
DonHopkins
John Draper once asked a friend of mine if he wanted to go into the back of a
van and execute an 1802 instruction. Can you guess which one?

[http://en.wikipedia.org/wiki/RCA_1802#Addressing_modes](http://en.wikipedia.org/wiki/RCA_1802#Addressing_modes)

Because of the 16-bit address bus, and the 8-bit data bus, the sixteen general
purpose registers are 16 bits wide, but the accumulator (the so-called data
register, or D-register) is only 8 bits wide. The accumulator, therefore,
tends to be a bottleneck. Transferring the contents of one register to another
involves four instructions (one Get and one Put on the HI byte of the
register, and a similar pair for the LO byte: GHI R1; PHI R2; GLO R1; PLO R2).
Similarly, loading a new constant into a register (such as a new address for a
subroutine jump, or the address of a data variable) also involves four
instructions (two load immediate, LDI, instructions, one for each half of the
constant, each one followed by a Put instruction to the register, PHI and
PLO).

------
fit2rule
Still being ported to old systems and used in, for example, the Oric-1/Atmos
machines:

[http://forums.oric.org/t208-pilot-focal-brainfuck-what-
else#...](http://forums.oric.org/t208-pilot-focal-brainfuck-what-else#2818)

(Old computers never die, their users do!)

------
jhallenworld
There is a 16-bit version of the 6502: the 65816. It would have been cool if
the 65816 was an implementation of the Sweet16.

~~~
michielvoo
It supports both 16-bit and 8-bit mode. I find it quite annoying to have to
keep track of which mode a register is in.

