What is keeping you from dabbling in writing Hardware ?
Tags: discuss, rust, beginners, computerscience
Published: 2022-02-19 14:32:45

Let's face it, if you are writing any program, in any language : you need hardware to run it.

If you write C / C++ / Rust code it will compile to binary which will execute inside you processor (eventually) and if you use python / JS it will run on top of their interpreters, which themselves run on the hardware. One way or other you will have to use hardware to execute your code. I hope we can agree on this :wink: :wink:

Hardware such as CPU does some really amazing things , latest CPUs have pipelined processing, so that they can run multiple instructions at the same time, and also logic to detect if the next instructions can run at the same time or not. They have several 128 bit-and-more registers which can process floating numbers and strings crazy fast, parallely and with various options : Do you know there is an instruction in x86 instruction set ( one used by common Intel and AMD CPUs) that can check if a substring is present or not in given string?

![a complex but processor board](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3e7jbvblvv4uzvjaqyh.jpg)

I agree that at the beginning it can be hard, even outright scary to get into such low level of systems, where everything is in binary and you have to take into consideration the things that you didn't know existed. But as with any other programming / coding / computer related stuff, I think it can get more manageable, if not easy, if one works with it more. 

Think about the first website or app or any project that you made, and how complex or technical it seemed at first, and how hard it seems after having done it several times after. The more you play with it, the more you try and make mistakes, I feel the better you get at it.

So, what is stopping you from getting into something so cool as hardware ?

In my case :
- To buy and work with actual Hardware, like Raspberry Pi, involves some cost, and even then I'm still working on an API abstraction of some libraries (usually)
- For designing actual hardware at the low level, current options usually involve VHDL or Verilog like softwares, and it can get pretty complex pretty soon, even to implement something very simple. Don't get me wrong, those s/w are pretty amazing, they can **_Optimize_** the circuits you wrote,and can be used to actually synthesize stuff that can be etched onto FPGAs , Which is pretty cool! But for a beginner who want to write few chips, the overhead can be tedious.

So, mainly because of the point 2, I am currently working on a library in Rust which will make it easier to write s/w emulated h/w in Rust. The complete logic of your chip will be in Rust, you can access files and stuff if you want, and do anything that Rust can do. The way I'm thinking, it will be as simple as writing a struct/class, annotating which pins are input, output and a function to operate what the chip will actually do based on the values of those pins.

Now this won't help you write Intel Pentium in 100 lines of code. Even emulators for Intel 8086, which was released in 1987, take about 2000-5000 lines of code (I would know, I have [written one](https://dev.to/yjdoc2/we-made-8086-emulator-in-rust-and-integrated-it-with-react-using-wasm-742)).

That said, this will allow you to ignore how to connect pins of various chips or how to write interface to connect chips and allow you to focus on the logic of the chip, like how 4-bit binary adders add two numbers or how ring counters actually count, or  how a CPU actually reads the data from RAM and so on. This also aims to make them sharable so you can take RAM someone else wrote and interface it with you CPU,  just like you would use different modules or packages for a project.

Even though I am writing it in Rust, the logic behind it does not depend on Rust, so one can easily do the same in Python or JS, and maybe even use Rust components with them.

I will share it once the things are done, but in the meantime I would like to know what is keeping **_you_** form dabbling in writing s/w emulated h/w. Maybe I can add features which will solve your issues too. Let me know in the comments!

---

PS : if you want to know when I publish the library, follow me :wink: you can unfollow after I publish it , I won't mind :smile:


Thank you!

Note : all images are taken from google images search, and are not mine.