Hacker News new | past | comments | ask | show | jobs | submit login
The Adventures of Writing a CHIP8 Emulator (benjcal.space)
50 points by lrsjng on June 13, 2023 | hide | past | favorite | 16 comments



I learned programming in 1977, age 12, on an RCA VIP (CDP1802 cpu) with 2K RAM, a hex keypad, composite video out (thru an RF modulator) and cassettes. None of that fancy ASCII stuff, and floppy drives were an unimaginable luxury.

CHIP-8 was a fun, approachable language. "Hacking" was hand-disassembling the CHIP-8 programs in the user manual.

It was easy to understand how computers were simply more complex TTL and CMOS projects like my father and I built each month from Popular Electronics and Radio Electronics. Dad worked at RCA, programmed in IBM 360 Assembler, FORTRAN, and at that time was helping write the ATLAS language (IEEE 416). Together we learned Tiny BASIC, and this launched my computer business for forty years.

Kudos to all the folks keeping CHIP-8 alive as a vital link from Boole and Shannon thru machine code to the "modern" world.


> I decided to use C. Wait a minute, isn't that illegal? This might surprise you but, it is not! You are not forced to use Rust for all new projects henceforth!

That made my day!


And then in part 2, he spends 5 day figuring out that fgetc returns int, not int8_t, and that apparently this design has consequences. So much for "a simple language that fits in my head". Just as a personal anecdote: I still can't get all the "usual arithmetic conversions" to fit inside my head. But that's just me being stupid and/or forgetful, for sure.

The C language looks like it simple but it's not, it's just a wonderful deception. C has quite an amount of invisible sharp edges and footguns and its standard library has even more footguns and sharp edges of its own design. And with the approach modern C compilers take to UB, the "It's the access and control that it gives me!" is simply not true; it's the compiler that has all the control which the programmer unwittingly gave to it: "unwittingly", because UB is invisible and since "no diagnostic is required", none is provided unless the programmer specifies -Wall -Wextra -Wpedantic; but of course, those options for some reason are unpopular between those former frontend programmers that recently discovered that they love C.


I’ve written a chip 8 emulator in both C and Rust and the Rust one is definitely more clear, and with destructured pattern matching you can really cleanly decode the opcodes in Rust: https://github.com/plan-x64/chip8/blob/master/chip8-core/src...

Normally I’d do something like a simple prefix lookup table with function pointers in C for this but chip8 opcodes require parsing the postfix to fully determine the operation (I.e. 8xy[0..e], where x and y are registers) which requires further parsing than a simple lookup table based on prefix.


The C standard library certainly has warts (errno, anyone?) but surely you can't begin to use fgetc() without knowing about EOF… even if you forget that int8_t is not required to exist (people writing int8_t usually mean int_least8_t).


I have previously written about coding a chip8 emu inside the nand2tetris emu: https://abhaynayar.github.io/blog/chip8-in-jack.html


Nice to see someone implementing a chip8 emulator. This is always a great learning project.

However, I do not think that he needs to go so far to explain what a stack does. If you can understand the rest of the text you probably have enough context and knowledge to know that already.


I had a friend over while I was playing with chip8, and I’d need to explain to him how to implement a stack… I probably had him in mind while writing ¯\_(ツ)_/¯


If you want another, slightly more feaureful toy platform to emulate, consider uxn/varvara.


That looks really nice! I was thinking gameboy next but I wasn’t sure I wanted to commit that much time to a toy project!


What stack is this blog using? It looks like plain HTML/CSS/JS. It's very nice.

I'm asking because the choice of stack to use is keeping me away from starting my own blog.


Is 11ty with this theme:

https://github.com/arpitbatra123/eleventy-blog-mnml

Choice of stack also kept me away so I decided to just keep it simple!

11ty is pretty close to html/css, but it has the niceties of being able to reuse html where you want.

I recommend to start without a theme, Just make a html file named layout and add {{ content }} And then an md file and put on the top matter layout: <name-of-your-layout>

It’s really all is needed and it’s good to understand how it works (as opposed to jekyll or hugo that I couldn’t figure out because they have a bunch of undocumented assumptions and requirements…)


This is very nice. Thanks for the recommendation.



Maybe a typo, maybe I didn't get it:

> 6101 is simply telling the CPU "set V1 (1 comes from the 3rd nibble) to the value of 0c"

Shouldn't it set V1 to 0x01?


Typo indeed. I think I was using a diffuser ROM to illustrate something and then I decided to use a more simpler one but forgot to update the text… will fix soon :)




Consider applying for YC's W25 batch! Applications are open till Nov 12.

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

Search: