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.

Comments

  1. Gragg says:

    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. baobrien says:

    That’s pretty awesome.

  3. Derek says:

    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.

  4. BeagleBreath says:

    This is totally awesome. this reminds me of an engineer i worked for who would design complex control system logic using only DPDT relays. he could do it all with AND OR NOT

    • someoldguy says:

      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.

    • Dax says:

      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.

    • Adam says:

      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…

      • Dax says:

        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.

  5. the old rang says:

    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

  6. Right... says:

    >>As a though experiment,

    It’s thought, with a “t” at the end. Mr. Benchoff, please represent the hacker community better.

    • gotcha.

      • jeremiah says:

        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.

      • Dissy says:

        @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 :{

      • @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.

      • Right... says:

        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.

      • Rob R. says:

        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.

  7. notmyfault2000 says:

    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.

  8. apburner says:

    So is the next step water pipes. The transistors could be pilot controlled solenoids. Just another thought experiment.

  9. hawkeyeaz1 says:

    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!

  10. Jon says:

    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.

  11. Andrew says:

    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.

  12. NewCommentor1283,ihatetorx says:

    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!

    • Plo says:

      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)

  13. Panikos says:

    This is really interesting with zero cost of entry money and time-wise. I am giving this a try just for the joy of it later on today.

    Brian, awesome post.

  14. Alex Rossie says:

    Great stuff reminds me of birken http://www.ioccc.org/years.html

  15. gz says:

    Just loving this! I wonder if someone will run with it and build out an older cpu.

  16. Not Torvalds! says:

    The post attributes the hack to Linus without a surname, and I read Linus Torvalds, but that is incorrect. Another Linus — Linus Akesson — was responsible.

  17. Kevin Keith says:

    This is a perfect demonstration of dataflow programming, of which Unix pipes are the most visible example

  18. Paul Potter says:

    Genius. Just had the counter running.

  19. RikusW says:

    There is actually water and compressed air based logic, have a look at en.wikipedia.org/wiki/Fluidics

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 92,354 other followers