Kjetil's Information Center: A Blog About My Projects

lazyboNES Emulator

Here is a special NES emulator made for the specific purpose of playing Super Mario Bros with text-based graphics, that is, using curses.

The NES graphics is based around a background of 32 by 30 tiles, each 8x8 pixels. This emulator maps each tile to a character in the terminal and you need to resize it to at least 32x30 for everything to fit. In addition to the text based interface, an SDL2 based graphical output can also be run in parallel. Sound and joystick input will always be provided by SDL2, even when running only in text mode.

Although it is technically possible to play with the keyboard directly into the terminal, it is highly recommended to play with a joystick/gamepad instead. Using the keyboard into the terminal means that all input is fed into a non-blocking "standard in" stream, which makes key combinations impossible. E.g. when walking and then jumping, the walk key will get "unpressed" causing Mario to stop.

The emulator is very specific to Super Mario Bros since the characters are directly mapped to its pattern tables. Other games, even if they start, will probably look really bad. Internally NTSC timings are used, so maybe only the USA version will work.

Several shortcuts have been made with regards to the emulation. The PPU (graphics) emulation only supports horizontal scrolling and no vertical scrolling. PPU background/sprite priority is also not implemented, so sprites are always drawn on top of the background. Rendering on the edges is also not hidden away. APU (sound) emulation supports pulse/triangle/noise channels but not the DMC channel, and due to lack of filters and such the sound quality is far from perfect.

The joystick/gamepad is detected on startup if connected, but the button mapping is hardcoded, so edit the source and re-build to change it. Save and load of the state is supported but only in one slot and only in memory, so it's not saved when quitting the emulator.

6502 CPU emulation is a modified version from my Commodore 64 emulator so it contains BCD mode which is not actually present on the Ricoh 2A03 used in the NES. Ctrl+C in the terminal will break into a debugger where various data can be dumped, including a CPU trace if compiled.

Here is picture of both the curses and SDL2 graphic outputs running in parallel:

lazyboNES screenshot

The first version of the source code is available here, but it has also been uploaded to GitHub for possible future developments.

Topic: Open Source, by Kjetil @ 25/03-2022, Article Link