Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
A 6502 emulator in Common Lisp [pdf] (redlinernotes.com)
139 points by _19qg on July 6, 2013 | hide | past | favorite | 19 comments


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.


I'm flattered. :)


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?


That magic actually comes courtesy of [SnabbSwitch](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/).

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 and the Makefile in the parent directory.


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.


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!


LLVM invented the tablegen language to do this in C++.


"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


You spelled Forth wrong :>


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.


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.


In the early 80's, when C was yet to become widely used, there were quite a few languages that offered support for doing systems programming.

Lisp was one of them.


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.



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


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...) for the next rewrite.

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

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.


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


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?


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.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: