
A 6502 emulator in Common Lisp [pdf] - lispm
http://redlinernotes.com/docs/cl-6502.pdf
======
e3pi
To me, this post is in the finest spirit of HN's reason for being. It is
delightful to to see the endless colliding of important primitive and historic
objects written well and become accessible to many. My mind reels with all
these fantastic permutations of what is possible and made accessible, and I
must constantly refresh and resort the "I want to know this one too" queue.

~~~
redline6561
I'm flattered. :)

~~~
hsmyers
As a long time fellow traveler in the land of Literate Programming, I must ask
how you created the document---by hand or with tools and if tools, which ones?

~~~
redline6561
That magic actually comes courtesy of
[SnabbSwitch]([https://github.com/SnabbCo/snabbswitch/wiki](https://github.com/SnabbCo/snabbswitch/wiki)),
latest project of the inimitable Luke Gorrie. Luke is also publishing
SnabbSwitch semi-regularly as a [readable
program]([http://blog.lukego.com/blog/2012/10/24/readable-
programs/](http://blog.lukego.com/blog/2012/10/24/readable-programs/)).

Basically, you write markdown comments in the code and there is a Makefile
that uses awk and sed to grab them out, cat some things together and pass it
to an awesome LaTeX stylesheet written by Pete Kazmier. See:
[https://github.com/redline6561/cl-6502/tree/master/src/doc](https://github.com/redline6561/cl-6502/tree/master/src/doc)
and the Makefile in the parent directory.

------
pcwalton
Shameless plug: My Rust 6502 emulator used macros for opcode decoding as well.
CPU emulation is an ideal use case for Lispy macros: the addressing mode and
opcode combinations have a lot of closely related code that's amenable to
macro factoring, and having the interpreter, JIT (if present), and
disassembler share code is a nice application as well.

~~~
redline6561
Hi, Patrick! Sprocketnes has been helpful reading as I've worked on my own
emulator. Ian Piumarta's lib6502 uses plain C macros. It's even almost as
short as cl-6502...though I don't think it's quite as readable. ;)

Keep up the great work on Rust, been waiting on the gc/scheduler stuff to
shake out a little bit. Looking forward to 0.8!

------
dschiptsov
"All programs have been written, and that program is Lisp. With perfection
already attained, implementation is left to the ignorant who do not already
know the truth of this."

and, of course,

"Any sufficiently complicated C or Fortran program contains an ad-hoc,
informally-specified, bug-ridden, slow implementation of half of Common Lisp".

Robert Morris's addendum: "Including Common Lisp."

... except if you use a well-written Lisp interpreter library :) - Scheme

~~~
lesslaw
You spelled Forth wrong :>

------
swah
This is interesting because its exactly the kind of code that I thought would
be easier in C than Lisp, due to easy bit manipulation, memory access, etc.

~~~
redline6561
Me too! Common Lisp actually was intended for system programming historically,
back from when Lisp Machines had a shot at ruling the world. Consequently,
there is quite nice support for low-level programming.

------
mark_l_watson
That is nicely written! Even though I had the serial number 71 Apple II, and
dipped into 6502 assembler way back, I am not much interested at all in the
6502 any more, but the author's literate programming/writing style is very
nice - good read for those of us who still are into Common Lisp.

------
ghostDancer
The github repo :
[https://github.com/redline6561/cl-6502](https://github.com/redline6561/cl-6502)

~~~
mjn
Looks like there's a work-in-progress NES emulator based on cl-6502 in there
as well:
[https://github.com/redline6561/famiclom](https://github.com/redline6561/famiclom)

~~~
redline6561
I've been stalled out on that for a month or two. The PPU has been quite
tricky. I'll probably take inspiration from
[nesemu1]([http://bisqwit.iki.fi/jutut/kuvat/programming_examples/nesem...](http://bisqwit.iki.fi/jutut/kuvat/programming_examples/nesemu1/))
for the next rewrite.

Related (with screenshot!): [http://blog.redlinernotes.com/posts/So-Close-and-
Yet-So-Far....](http://blog.redlinernotes.com/posts/So-Close-and-Yet-So-
Far.html)

If I can wind up with a NES emulator that is "fast enough" and 2500 lines of
code instead of 75,000 (Fceux) or 150,000 (Nestopia) then I'll be happy.

------
rasur
This is really quite enjoyable to read (although I've yet to finish it).
Thanks for posting it.

------
peterbotond
My old vic-20 had a 6502, and c-64 had a 6510. 6510 was backwards compatible
with 6502. the most obvious diffrence was the 6510 had more instructions. has
any of you had a c-64 with a 6502?

------
marcosscriven
The is very interesting indeed.

Whenever I see old ICs like the 6502 I have this fantasy of going back 30
years and making my own computer from scratch.

