Computing With The Command Line

Here’s something we thought we would never see: computing with just pipes, /dev/zero, and /dev/null.

As a thought experiment, [Linus] imagined a null byte represented an electron. /dev/zero would have an infinite supply of electrons and /dev/null would make a wonderful positive power supply. With a very short program (named mosfet.c), [Linus] can use Linux pipes to control the flow of electrons between /zero and /null. [Linus] used mosfet.c with a very short shell script to create a NAND gate. From there all bets were off. He ended up creating a D flip-flop4-bit adder and a counter.

From a bit of cursory research, Linux has a maximum pipe capacity of 1,048,576 bytes and the maximum number of PIDs is 4,194,304 (correct us if we’re wrong). [Linus]  can theoretically build some of the classic CPUs of the 70s and 80s with his pipe logic. An Intel 486 is just out of reach, though. If you give someone a NAND or a NOR they’ll eventually build a computer; we thought we’d never see this, though.

40 thoughts on “Computing With The Command Line

  1. So he created a really cumbersome form of vhdl using the OS for the logic. It is unfortunate that he had to resort to writing c code for the mofset. If you are going to concede that, you may as well write a program for a nand, or a latch, or a full adder, or a cpu.

    Sounds like linux needs to increase its pipe capacity to make x86 shell scripts possible.

  2. I love this kind of thing, to recreate a CPU from basic gates is the only way to really grok what is going on inside.

    it’s not very useful as a real tool, but very good for teaching what is going on at a gate level.

    1. Relay logic was the thing in the olden days. I used to have a contact cleaner – sort of like a nail-file but with diamond dust as the abrasive. You cannot imagine how many individual contacts a large system could have. Vacuum-sealed relays were more reliable but still failed. Plug-in relays were a real luxury.

    2. Relay logic isn’t particularily difficult.

      A single SPDT relay can be configured to work as a NOR gate with arbitrary number of inputs, and since NOR is a universal gate, that’s all you need.

      There are better ways to produce the simple gates though. AND is just one relay, OR is just two wires put together and isolated by diodes, NOT is just one relay… etc. etc.

      Once you know how to make the gates, it’s a simple matter of coming up with a logic table of inputs and outputs, and figuring out what gates you need to perform the funtion.

      I recently designed a curtain pulling machine that takes two SPDT relays to form a state machine that opens and closes a curtain. Two more relays were needed to control the direction of the motor. The only problem with the setup is, that it consumes lots of power when idle because at least one relay is always pulling, so I had to replace it with a 4001B logic IC.

    3. That’s because the set of logical connectives {AND, OR, NOT} is functionally complete, ie, you can build any other type of logic gate from them. In actual fact, both the sets {NOT, OR} and {NOT, AND} are functionally complete, so really he only needed two gates :P

      Though, it would have made the circuitry a lot bigger…

      1. Considering that relays are usually switching type and not simply connecting type, you have the NOT function built in anyways. The basic relay has one input A and two outputs B and -B making it either a buffer or a NOT gate.

        Thus you can make a OR/NOR gate out of a single relay simply by wiring the outputs of other gates to its input. You need to add a diode in line if any of the outputs goes to more than one input, because otherwise the other outputs would trigger them, but other than that it’s all you need.

        Making a NAND gate requires at least two relays, so NOR logic is more efficient with relays. It also has the added advantage that the single relay OR can have an arbitrary number of inputs, since all you’re doing is soldering wires to a single terminal.

        If you don’t have any diodes, for example if you live in the 19th century, then NAND logic makes more sense.

  3. The accomplishment is not the idea.
    The accomplishment is not the basic simplicity.
    The accomplishment IS the simple basic idea made and executed…

    Disparagers please understand what virtual computing is… and isn’t

    Understand, just a few years ago what Graphene was and wasn’t.

    Exercises in thought processes and accomplishment of ‘what if’ thinking says more than all that was said by what I call psuedo science against what was then called cold fusion

      1. wasn’t there going to be some new near-zero-tolerance policy on asshole comments?

        typos aren’t the problem, responses like his are the problem.

        I can always tell when I’m on hackaday. the comments are vile hateful outpourings of bile.

      2. @jeremiah

        Hackaday removed the “Report” links a while ago it seems. Even reporting such posts (or worse ones) by email gets no response or action.

        I liked the fact some of the really talented engineers, designers, and hackers came back for a bit to post about their projects. They originally left due to exactly the kind of hateful and petty commenting that seems to be back to stay now.

        Is there perhaps another similar website, with moderators, that these contributors moved to before and will likely be moving back to again?
        I’ll miss know what what they are up to :{

      3. @jeremiah:

        In the interest of objectivity, I allowed Right… to stay up. I’m big enough to allow my failings to be public.

        As far as other asshole comments, I’ve noticed a bit of an increase in the last few days. I’ve taken the worst down.

        IF YOU’D LIKE TO HELP (me and the other guys can’t be on here 24/7 anyway): Click that ‘Report comment’ link if it’s horribly offensive. It’ll go down automagically after enough people click that. Also, If you’ve got any issues with any of my posts, feel free to email me. it’s brianbenchoff (symbol derived from et) hackaday (full stop) com.

      4. Thank you sir. I do not mean to be an “asshole commenter”. I do not feel that asking for spelling checks qualifies me as such and based on your responses neither do you.

        It is a very real problem when non-hacker-types stumble across these often rather interesting articles and find such typos. It makes all hackers in general look bad (uneducated) in the eyes of the non-hacker public.

        Furthermore it makes the article hard to read when you have to stop and think what word the author was trying to use instead of their mistaken word. “Though” and “thought” in this case are just a letter different but sound nothing alike which means I have to stop reading and go back to decipher what was meant.

        As a poster on a predominant hacker news site that even my tech-illiterate-mother-in-law has stumbled across, you most certainly represent all of us in the eyes of the public.

        I’d like to say to the posters decrying me as an asshole that they are the problem. I am simply asking to be represented better.

        @ jeremiah, Typos may in fact not be a major problem, however responses like yours are are absolutely a problem. Your comment was a “vile hateful outpouring of bile” for publicly asking to silence and censor a person that is only asking, quite kindly I might add, to be better represented in the media as a literate citizen of earth.

      5. The problem isn’t spell check (usually), it is grammar check which, as far as I know, doesn’t really work that well.

        ‘Though’ is a real word and so is ‘thought’, he just missed the ‘t’ (which I do all the time) and it really takes a proofread, or us to find.

        I would say read it backwards and then post but even then you can miss grammar mistakes because it is correct, just not the right word.

        Everyone is human, give him a break. Let’s try to clean up some of these comments too. Sometimes they are way harsh and if someone commented on a project I posted I would never return to look at the comments either.

        Come on guys, let’s step it up, stop being trolls and be respectable human beings.

  4. Yo dawg, I heard you like computers…

    To make this post more than just a meme reference, is it not possible to do the logic of mosfet.c in a shell script? I haven’t checked out the source, and even if I had I don’t know enough about shell scripting to see if it is.

  5. This is a bit challenging to wrap your head around, but this is brilliant. Obviously is has no practical use but for educational purposes, but hey, you do NOT need special software or hardware to learn digital circuits!

  6. We need to go deeper. We need to use this to simulate a computer that could run Linux that does the same thing. Computer within a computer within a computer.

    Or, you know . . . Install VirtualBox on a VM.

      1. People still use Virtual PC? Yeah I was always thinking VirtualBox. I usually use the OSE version of VirtualBox, and I’m pretty sure I’ve double-nested virtual environments like this before with it. Just to cure boredom.

  7. Yup, this is just the type of thing I like to see. Someone who has the intellect to think abstractly and then use the tools at hand to prove their concept.
    I’m a big fan of this bare metal computing. If you can understand computing at the CPU level where every clock cycle matters it makes you a much more proficient developer when you’re using higher level languages.

  8. in my opingion the reason that this is so interesting of an idea is that he can simulate things without ANY of the following:

    HARDWARE GATE IC’s
    or
    COMPILIERS
    or
    EXPENSIVE SIMULATION SOFTWARE

    those sim. softwares cost 100’s (or even 1000’s) of dollars… throw in a colorful gui and you have something that could be adopted by non-money-wasting people the world over!

    1. You should check out ghdl and gtkwave. This can be used to simulate quite advanced logic designs for free. Imho it’s almost as good as modelsim (I like the interface of ghdl/gtkwave more but modelsim has a few not often used, but handy, features that gtkwave does not have)

Leave a Reply to notmyfault2000Cancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.