Hacker News new | past | comments | ask | show | jobs | submit login
NES Emulator in Common Lisp (2016) (github.com/samanthadoran)
137 points by oumua_don17 on Aug 26, 2023 | hide | past | favorite | 33 comments



An interesting related and older project is this post showing a bit of using Lisp to write code for the NES: https://ahefner.livejournal.com/20528.html Making an assembler in Lisp is not a daunting project.


Another issue with several .asd files, no readme and not sure how to start. I am interested as this is simpler than the one here. At least the programmer has a test ROM sort of. Just not sure how to start.

As might be noticed, I have spent a few days here to try to sort out a way to start a simple (and possibly) interesting simple lisp project. NES as said in that web page is great. Just no good starting point (or may I say too complete as a starting point) for lisp project.


I think if you've never written any sort of lower level code before it's somewhat more daunting. I did spend a little time trying to get this emulator to run but ran into too many issues and gave up -- it's either a problem with some optimizations no longer being valid and raising type errors by default now (e.g. specifying a type as string does not, like other languages, implicitly mean it's either string or null¹) or something about the sdl2 bindings has changed (it's not the most stable of library binds unfortunately and has some misleading docs/examples out there, and the with- macros are not good and best avoided).

The assembler project I linked on the other hand works, though as you say having multiple asdf files is annoying, and the dollhouse.lisp demo makes an unfortunate choice of setting its *path* to #.*compile-file-pathname* Here's what I did to run it without changing that line (which would be better), translate to your flavor of editor+slime for convenience:

    $ git clone https://github.com/ahefner/asm6502
    $ cd asm6502/
    $ sbcl
    * (asdf-here)
    ; my own function I have in ~/.sbclrc, it is just:
    ;(defun asdf-here ()
    ;  (push (uiop:getcwd) asdf:*central-registry*))
    * (asdf:load-system "ichr")
    T
    * (defpackage :dollhouse-demo)
    #<PACKAGE "DOLLHOUSE-DEMO">
    * (defvar dollhouse-demo::*path* "hacks/")
    DOLLHOUSE-DEMO::*PATH*
    * (load "hacks/dollhouse.lisp")
    ; ...output
    ;
    ;(:NUM-UNIQUE 458) 
    ;Empty space begins at F600
    ;Created "/tmp/dollhouse.nes"
    T
    * ^D
    $ fceux /tmp/dollhouse.nes 

¹Example of SBCL raising a type error:

    (let ((x 3))
      (declare (type integer x))
      x)
    ; -> 3
    
    (let ((x nil))
      (declare (type integer x))
      x)
    ; in: LET ((X NIL))
    ;     (LET ((X NIL))
    ;       (DECLARE (TYPE INTEGER X))
    ;       X)
    ; 
    ; caught WARNING:
    ;   Constant NIL conflicts with its asserted type INTEGER.
    ;   See also:
    ;     The SBCL Manual, Node "Handling of Types"
    ; 
    ; compilation unit finished
    ;   caught 1 WARNING condition
    
    debugger invoked on a SIMPLE-TYPE-ERROR in thread
    #<THREAD tid=9761 "main thread" RUNNING {1001750003}>:
      Value of NIL in
      (LET ((X NIL))
      (DECLARE (TYPE INTEGER X))
      X)
    
      is
        NIL,
      not a
        INTEGER.


Very cool code. I bookmarked the author’s repo list to peruse later.

I don’t get to use Common Lisp as often now because almost everything dealing with LLMs, deep learning, etc. is now Python (soon to be Mojo, for me?). Still it simply makes me happy to see projects like this.


This project is a good example to show people that lisp is nothing to be scared of. I find this project's code to be very readable.


That's funny considering one of the comments in the README:

> TODO: Write more idiomatic Lisp

Perhaps it's readable because it's not idiomatic :D


But what if I just tried to write C in Lisp? What could go wrong?

Author here: please don't look at this as a way to organize a lisp project.


pretty sure one could say that about many languages


Sadly, I've nothing of too much interest in there just now. Though, there is a not really working psx emulator in there in Lisp.


Has anyone managed to run a game?

I tried it but had some issues... first of all, on Mac, I had to run `brew install sdl2` as that's a native dependency required...

Now, I seem to need a ROM:

> Where path to rom is a string

I tried to use this one: https://www.emulatorgames.net/download/?rom=super-mario-bros

But just got lots of nonsense error messages no matter what I did.


I tried more in-depth. MacOS hang there and not even sdl examples can run. I switch to windows wsl2 sample run and a windows also pop up. But as you exprience a lot of message which seems strange. Anyway at least I guess it is NOT the main-thread issue (e.g. one said bad op code.)


Further testing and basically two kind of err code for the 6 rom I tested, example here:

;(nes:setup-and-emulate "1943.nes") #| The value NIL is not of type 6502-CPU::INSTRUCTION when binding 6502-CPU::INST |#

;(nes:setup-and-emulate "DKJU.nes") #| invalid number of arguments: 6 |#

I test both Rom using fcuex64.exe and both worked. Hence, I guess it is not usable. Sadly as the source code is very readable, unlike well python one.


I'm not sure what changed up break everything. When I can get back to my computer in a week or so, I'll take a look.


I've not tried to run this repo in "a long time" :tm: even back then, it had some fun and exciting sharp edges that I never got around to fixing. Apologies.


I'm impressed. I wonder what made the author created that.


According to the author, it was their second Common Lisp project for learning purposes https://old.reddit.com/r/programming/comments/5jh7fy/basic_n...


Writing a NES emulator is a fun programming challenge. Start here: https://www.nesdev.org/wiki/NES_reference_guide

and maybe read this first to understand where to begin with emulating an old CPU such as the 6502

http://www.emulator101.com/


The 6502 bit is fun, but the NES PPU not so much! I never got past that part, maybe one day ...


Yes the PPU is more complex than the CPU, but the audio emulation was more difficult than the PPU for me. Noticed the audio is a TODO for this emulator.


Just for fun and learning, after writing a CHIP-8 emulator, what would be a good first emulator for a real device to tackle?

NES or Gameboy are my two main ideas, but I'm curious if there are other suggestions.


Any 8 bit machines will have similar complexity and difficulty. Personally I have only done the NES as my first and only emulator. I used C++.


It's fun, give it a try


Sadly it does not work


I believe he meant try making your own NES emulator


I wish I knew.


sdl2 for the graphic. Interesting.


Last commit to the repo seems to be from 7 years ago. Previous post from 2016: https://news.ycombinator.com/item?id=13230382


I don't mind stale repos when it comes to emulation, because nothing changes (except the host/build ecosystems) so there is less to keep updated. In the case of the NES, the last game released was in 1995.


NES is a little unusual in this respect because the vast majority of cartridges have "mapper" hardware that must be emulated, and there has been a more-or-less continuous trickle of unlicensed cartridges over the years, some of which have unique mappers. If memory serves you can get to something like 95% compatibility with the few dozen most common mappers, but then there's a pile of oddball mappers that are each used by 1 or 2 games (many of which are just pirate hacks of a licensed game).


Yeah a stale repo isn't bad as long as there aren't outstanding issues to be worked. It is still a bad sign in evolving tooling environments like java c++ or rust, but lisp is pretty stable at this point too...


I'm not really replying to this post per se, but the NES emulator is pretty cool. Just wondering if anyone who has posted here can help me. I posted my very first hacker news submission, but it only shows up on the "new" tab when I'm logged in (with an orange asterisk next to it). When I log out it's nowhere to be found. When I log back in, I see it. The FAQ says that "All Ask HNs appear on newest and asknew". Just a shot in the dark, but if anyone has any ideas what might be going on, it's appreciated. And sorry for spamming this submission.


email the mods at hn@ycombinator.com like the FAQ mentions and they'll sort you out.

New accounts sometimes get caught up in various filters.


Thanks! I did that and all is good now




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

Search: