from Hacker News

NandGame – Build a Computer from Scratch

by dalemyers on 12/2/20, 10:21 PM with 136 comments

  • by carls on 12/2/20, 10:31 PM

    I've done the Coursera course, Nand2Tetris, that inspired this website [1] and found it deeply intellectually satisfying and engaging.

    If anyone is interested in learning about the logical primitives that build up to a computer, and how they're implemented using logic gates, I would deeply recommend the course!

    -----------

    [1] https://www.nand2tetris.org/

  • by Sniffnoy on 12/2/20, 11:15 PM

    Building circuits is neat, but I think this has some problems as a game:

    1. It seems to only let you save one solution for each level, even though there are two optimization goals. It should let you save one for fewest components and one for fewest NAND gates.

    2. It's not really clear how NAND gate counts work in later levels where you're using potentially non-optimal components you've built? Like does it count each one as the number of gates that you used, or the number of gates that is optimal, or what? Someone else told me it was the latter, but it seems like XOR counts as 6 even though there's apparently a way to do it with fewer?

    3. Why do some levels give me the message "This is optimal!" while others say "This is the simplest solution!"? What's going on here? What do these messages mean specifically? It's not clear.

    All this adds up making attempts at optimization a bit frustrating and as such I just don't really want to play it very far. If you're not worried about optimization it seems fine, but if you are the interface really kind of works against you.

  • by kayson on 12/3/20, 3:04 AM

    Not that this will ever matter for someone who isn't a circuit designer, but the "optimal" solution for the latch is actually really bad.

    If you use a "select" block (which should really be called "mux") and tie the output to the input, you create a race condition. If the data input is 1, and you change set from 1 to 0, the output can actually glitch to 0 for a very short period of time, which is a big no-no. (If you draw the equivalent circuit out with just nand gates and assume they all have some delay between input and output, you'll see why).

    A safer solution would be to cross couple two nand gates into an SR latch [0], use another two nand gates to control the Set and Reset signals, and an inverter to create NOT(data).

    Of course in reality, a latch is usually custom built out of transistors rather than logic gates, but there are some cases where I've used logic-gate-latches to make things easier.

    </pedanticism>

    [0] http://electronics-course.com/sr-nand-latch

  • by IIAOPSW on 12/2/20, 10:43 PM

    There was this ancient Flash game called "Engineer of the People" that was all about designing IC circuits (NPN and PNP junctions) in the Soviet Union for some secretive reason. I never beat the game and got to the reveal. IIRC it was by Zachtronics, the same guy that did Shenzhen I/O and infiniminer (the inspiration for Minecraft).
  • by userbinator on 12/3/20, 4:45 AM

    I know digital logic, yet was perplexed by the odd definition of D flip-flop this game uses with both "store" and "clock" inputs, which matches none of the standard variants of DFFs: https://en.wikipedia.org/wiki/Flip-flop_(electronics)#D_flip...

    Edit: this continues on with the counter level, where you can use the register but it does not behave like the typical set of edge-triggered flip-flops (or transparent latches) that one would normally use. My best attempt at coming up with an explanation is that it's like two-phase edge-triggered logic.

  • by ElHacker on 12/2/20, 11:51 PM

    I got excited when I saw the title, then checked the URL and it is a software simulation / game of building a computer. I'm not denying this can be interesting, but I've been thinking for a while to get my hands dirty with hardware as a side project. (I'm a software eng by profession). I haven't really done a lot of research on this yet, but does anyone have any recommended guides/books/tutorials on how to get started designing and building my own computer? (extra points it it's a mobile/embedded computer)
  • by rkagerer on 12/3/20, 10:16 PM

    Neat! My only complaint is I feel like it would be more intuitive to be able to drag connections from output to input. I understand why it's the other way around (each input can only have one source, while outputs can go to multiple chips) but it still feels backwards - like reading right to left.
  • by iKlsR on 12/3/20, 1:34 AM

    If you like this or want to get into it a bit more, this recent video is lovely https://www.youtube.com/watch?v=QZwneRb-zqA
  • by bstrong on 12/2/20, 11:24 PM

    Very cool! Reminds me of all the fun I had with Rocky's Boots on Apple II:

    https://en.wikipedia.org/wiki/Rocky%27s_Boots

  • by linkdd on 12/2/20, 10:37 PM

    This looks a bit like MHRD [1] but with a GUI :)

    ----

    [1] https://store.steampowered.com/app/576030/MHRD/

  • by evangow on 12/2/20, 10:34 PM

    Awesome! I've always thought it would be interesting to port Nand2Tetris to the web to make it more easily accessible. Glad someone finally ported the virtual computer building part!
  • by herghost on 12/3/20, 1:14 PM

    I was able to get as far as building the half-adder, but then didn't have the means of figuring out the full adder as that seemed massively more complex.
  • by mkl95 on 12/3/20, 10:30 AM

    I have fond memories from reading this book and doing the exercises. I never got past building RAM memory though :-)

    It's worth mentioning that the book is getting a extensively revised (sic) 2nd edition in July 2021.

    * https://www.amazon.com/Elements-Computing-Systems-second-Pri...

  • by Koshkin on 12/3/20, 1:40 AM

    I find using an FPGA for this an ideal middle point. Building a functional computer from discrete components is fun but the result may not be worth the effort, to be honest. At the other extreme, modeling it in software may leave a weird feeling of not having achieved anything. With an FPGA, on the other hand, you can create something of a usable computer, even one you could run Linux on.
  • by noncoml on 12/3/20, 7:33 AM

    There are 10 kind of people. Those who like to drag from the input back to the output and those who prefer to drag from the output to the input.
  • by radicalriddler on 12/3/20, 12:47 AM

    I did logic gates as part one of my first uni courses in I.T. The assignment was actually so fun, I hated when I finished it.
  • by yellowapple on 12/3/20, 6:36 AM

    So I'm a bit confused with how this is measuring the number of NANDs; for example, with the first adder problem, I solved it with exactly two gates, neither of which were NAND, and yet while it's recognized as the lowest number of gates, it insists that it can be done with fewer NANDs (than zero, apparently). Is it counting the NANDs that may or may not be used to implement those two gates? And if so, is that based on how I solved the previous problems? Or is it based on the lowest possible number of NANDs that would be required to solve those problems / produce those gates?

    If it is indeed counting the NANDs used to make the gates I actually used, it'd be useful to have a clear indication of exactly how many NANDs each gate uses so I can chase after a game of NAND Golf.

    In any case, this is fun :)

  • by ducktective on 12/2/20, 10:47 PM

    For anyone interested, also check out this computer designed in Powder Toy game! https://www.youtube.com/watch?v=h4eHgnLFk9k
  • by AQXt on 12/3/20, 10:45 AM

    That's the coolest thing I have seen today. Thanks! :)

    Suggestion: replace the blue button text ("I have completed the level") with something simpler, like "Run", "Check", "Test" or "Go".

  • by simonbarker87 on 12/2/20, 10:38 PM

    Some of my favourite courses of undergrad were Digital Electronics 1 and 2. How you can build everything up from nand gates is so satisfying, I’ve forgotten much of it now but this is bookmarked to come back to for posterity
  • by h_anna_h on 12/3/20, 9:47 PM

    The test in the opcodes level is incomplete as for X-Y it accepts ~(X&Y)
  • by ColinWright on 12/3/20, 12:03 PM

    It probably sounds petty, but I found it disturbing that inputs are at the bottom ... I was constantly having to readjust my thinking, and have become sufficiently frustrated to give up.
  • by dusted on 12/3/20, 3:02 PM

    This is fun! But, the first few told me "this is the optimal solution", then I hit the xor gate, and I can get either "this uses the fewest nand gates but it can be done with fewer components" or "this uses the fewest components, but it can be done with fewer nand gates".. Does this imply that there is no optimal solution, or just that I've not yet found it?
  • by gdcohen on 12/2/20, 11:08 PM

    This is really cool. You've done a tremendous job in making this very usable (I love your yellow sticky note instructions).
  • by vishnuharidas on 12/3/20, 1:34 PM

    Reminds me of Ben Eater's Youtube videos where he builds everything using basic gates on a breadboard. He made a video-card using ICs on a breadboard that fascinated me: https://www.youtube.com/watch?v=l7rce6IQDWs
  • by jzer0cool on 12/3/20, 6:56 AM

    What might be some logical next steps to do, learn, or make (preferred) after learning some basic circuit logic?
  • by Hitton on 12/3/20, 10:43 AM

    I love this game. And was the UI improved since the last time it was posted on HN or am I misremembering it?
  • by pteraspidomorph on 12/3/20, 11:59 AM

    This isn't working very well for me on firefox. The components don't seem to want to be dragged.
  • by rzach on 12/2/20, 11:37 PM

    Love this - I actually started implementing these circuits in C [0] a while back but never got past combined memory.

    [0]: https://news.ycombinator.com/item?id=23672470

  • by BitwiseFool on 12/2/20, 11:03 PM

    This was so much fun! It brings me back to my first class in digial logic.
  • by mathattack on 12/3/20, 2:01 PM

    I used this with several elementary school kids who enjoyed it very much.
  • by wolfgke on 12/3/20, 10:20 PM

    A quite similar game in Steam:

    MHRD

    https://store.steampowered.com/app/576030/MHRD/

  • by newman8r on 12/3/20, 1:09 AM

    this is fun. When I got to the multi-bit-adder I think the half adder was removed from the toolbox though and replaced with the full adder - would be nice to keep it there.
  • by zug_zug on 12/3/20, 12:04 AM

    There's a very similar game(text-based) called MHRD
  • by tunesmith on 12/3/20, 3:15 AM

    Nicely done! If the author is reading, care to share any details on what stack or libraries were used?
  • by mikewarot on 12/3/20, 1:42 AM

    That was a fun diversion, thanks!
  • by signaru on 12/3/20, 4:54 AM

    it would be cool (ambitious) if at some point there is also a programming language/compiler down the line. perhaps it can would also be able to run tetris or even another nandgame.
  • by berkserbet on 12/2/20, 10:35 PM

    Really cool, curious to see how hard it gets. Reminds me of college.
  • by hnick on 12/2/20, 11:22 PM

    Thank you for showing me how much I have forgotten in 15 years :)
  • by DNied on 12/3/20, 3:59 PM

    I wish Pure Data had a GUI like that. Hats off to the author.
  • by mtoner23 on 12/3/20, 9:41 PM

    I'm a simple man. i see NandGame i upvote
  • by blackrock on 12/2/20, 10:35 PM

    I wonder when we can start 3D printing our own resistors and transistors.
  • by LeonB on 12/3/20, 12:17 AM

    Should start from physics building up and up until you have subatomic particles, rising through many layers of abstraction until you are doping P and N type semiconductors .... starting with such a high level concept as “nand gates” is far too easy. /sarcasm.parody