

Fun with Lisp: Programming the NES (2012) - felipebueno
http://ahefner.livejournal.com/20528.html

======
PuercoPop
If one likes the article, as do I, there are couple of cool lisp projects
related to the 6502 chip.

Brit Butler has written a 6502 emulator[0] and is now writting a NES emulator
in lisp[1]

Henry baker wrote a compiler for a simple lisp named 'comfy' for the 6502 chip
which intended to replace it's assembly in emacs lisp[2] which has been ported
to common lisp[3]

[0]:
[https://github.com/redline6561/famiclom](https://github.com/redline6561/famiclom)

[1]:
[https://github.com/redline6561/cl-6502](https://github.com/redline6561/cl-6502)

[2]:
[http://home.pipeline.com/~hbaker1/sigplannotices/sigcol03.pd...](http://home.pipeline.com/~hbaker1/sigplannotices/sigcol03.pdf)

[3]: [https://github.com/jaoswald/cl-
comfy-6502](https://github.com/jaoswald/cl-comfy-6502)

------
yason
The thing that is fascinating——and only possible in Lisp——is that by extending
the DSL that he started as the assembly language he could, eventually,
actually program his 6502 in... Lisp.

A typical assembler reads the program in assembly language syntax and
translates the lines into binary instructions. And you could write a Lisp
compiler that reads in Lisp and produces maybe C which is then compiled into
assembly language which is finally fed to the assembler. But he started by
writing the assembler in Lisp and instead of parsing and translating assembly
language he wrote his assembly program by calling Lisp functions such as (inc)
(txa) (lsr) which just emitted the appropriate instruction codes.

However, at this point there was no barrier left between his host-side Lisp
and target-side machine code. He could write constructs in Lisp that, by
running the Lisp program, expand to the corresponding machine code. And given
some well-chosen Lisp constructs he could elevate the code into a much higher
level but still output 6502 machine code. He could write Lisp macros to
produce Lisp code that produces 6502 code.

And because everything was still tightly integrated in a single running Lisp
program, he could keep doing the low-level stuff as well because he was still
in control of what machine code instructions would be written out.

This IMHO is a much better way to write assembly language than the usual
syntax that assemblers parse. But it's not possible unless code is data is
code.

~~~
segmondy
This is not only possible in Lisp, this is also possible in Prolog. In Prolog,
Data is code. LISP can be viewed as a specialization of Prolog, where the data
structure just lists. Prolog has more complex data structure compounds, where
a list is nothing more but one type of said compound structure. One can write
Prolog code to emit 6502 code or any code as well, and I suspect with as much
ease as he did.

Here is a sample of code generation in Prolog for register machines,
[https://github.com/segmond/PrologThingz/blob/master/clause_a...](https://github.com/segmond/PrologThingz/blob/master/clause_and_effect/compilation/cg_flatten.pl)

An example for accumulator based machines which the 6502
[https://github.com/segmond/PrologThingz/blob/master/clause_a...](https://github.com/segmond/PrologThingz/blob/master/clause_and_effect/compilation/cg_single_accumulator.pl)

The examples I posted is from an awesome Prolog book I read called "Clause and
Effect"

------
Scaevolus
High-level assemblers can greatly reduce the tedium of programming for older
systems. Just a small amount of structure -- loops, conditionals, symbolic
definitions -- improves assembly immensely.

Here's one dev's experiences making a GTA-like game for the NES, using his own
high level assembler:
[https://www.youtube.com/watch?v=Hvx4xXhZMrU](https://www.youtube.com/watch?v=Hvx4xXhZMrU)

Here's one for CHIP8:
[http://johnearnest.github.io/Octo/](http://johnearnest.github.io/Octo/)

------
saganus
Wow... programming in Lisp is already not that mainstream... and on top of it,
for the NES?

Impressive.

I so want to start learning to program in NES now. Not really in Lisp as I
would like to learning curve to be gentler, but this article definitely
sparked my interest into learning one of the devices I used most during my
childhood.

Great work!

~~~
kazinator
Hefner is not really programming the machine in Lisp. The developer appears to
have created a 6502 assembler as a "domain specific language" in Lisp, and
then is programming the machine in that notation. This could have tremendous
advantages over using some clunky conventional assembler; it can likely be
made smarter, with less work. You could probably use his tool without knowing
Lisp, if it were suitably packaged or documented. (But not without knowing
6502 machine language, in all likelihood!)

Knowing Lisp would help you easily understand and extend the assembler, so you
could do things like create yourself some badly needed program organization
abstractions and then use them. Otherwise you could just ask the author, or
people in a mailing list. "Hey I would like a feature whereby if I write (foo
bar) in the code, such and such an effect takes place in the generation of the
6502 image."

~~~
saganus
That's what I thought as well. I guess I was not that off course. But I would
still need to refresh the very basic Lisp I used in college.

In any case, learning 6502 asm seems like a must like you say. I'm gonna have
to try to learn a bit.

------
boomlinde
Nice! I've been using python to augment my 6502 assembly. I haven't programmed
the NES, but I've read that acommon way to wait for a particular raster line
on it without using fancy mapper hardware is to use sprite-to-background
collision interrupts. Collisions aren't triggered until they happen "on
screen" with respect to the monitor beam position (same on C64 or Atari 2600),
so you can effectively use it to get a line- or even a pixel interrupt.

------
etiam
Beautiful.

I've actually been walking around for a few days just now pondering if one
could do the corresponding thing for contemporary graphics processors. Seems
like communicating with them in Lisp could be a great thing. (That idea was
inspired by another HN post
[https://news.ycombinator.com/item?id=7408807](https://news.ycombinator.com/item?id=7408807))
Do you know of any attempts at that?

~~~
emiljbs
Check out

[https://www.youtube.com/watch?v=I0kWZP9L9Kc](https://www.youtube.com/watch?v=I0kWZP9L9Kc)

[https://github.com/cbaggers/varjo](https://github.com/cbaggers/varjo)

------
rtpg
For those interested in NES programming in a similar style (DSL that is
basically an ASM assembler), there's a Python version of this concept (titled
PyNES)

I find it to be very readable:

[https://github.com/gutomaia/pyNES/blob/0.1.x/pynes/examples/...](https://github.com/gutomaia/pyNES/blob/0.1.x/pynes/examples/mario.py)
example file, I like it

------
codezero
My favorite thing about this post is how they're generating music with Lisp :)

------
mrottenkolber
That sure looks like fun!

