Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ask HN: How does code get translated to voltage inside a CPU transistor?
12 points by hackaflocka on Nov 14, 2013 | hide | past | favorite | 21 comments
In "Zen and the Art of Motorcycle Maintenance" there's a section about how there are these 2 buildings on a corporate campus, housing hardware and software personnel respectively. The hardware people make flip-flop circuits, and the software people work with the software equivalent of the same. But neither group properly understands at which point exactly the software|hardware boundary lies.

Text in my text editor gets converted to 1's and 0's which then get converted to electric signals inside transistors in my iPad's CPU.

What's the mechanism by which 1's and 0's turn into different voltages? Can someone point me to a resource (preferably a video) that explains this?

(How is mere text able to control what happens inside an inanimate machine?)

(In other words, what happens at the software|hardware boundary?)



I'm happy to say that I'm uniquely qualified to help you.

I have a BS in Computer Engineering and a MS in Electrical Engineering, with a specialty in VLSI and microelectronics.

While in school, I had to design a microprocessor, one transistor at a time. Great experience, by the way.

I will summarize this process from a Top-Down approach, in case you're a CS major. (Note that engineering is Bottom-Up!)

- C/C++ gets converted into Assembly Language.

- Assembly Language gets converted into bytes. (One or more bytes encode each machine instruction and data)

- Each machine instruction takes multiple clock cycles to execute.

- On each clock cycle, a "microcode" (a.k.a. Register Transfer Language) moves data around. (The order in which data are moved from one register to another is controlled by a Finite State Machine.)

- Each finite state machine is composed of sequential circuits and combinational circuits: multiplexers, decoders, etc.

- Each sequential circuit is composed of some type of memory element : flip-flops, latches, etc. (Sequential circuits can be static, quasi-static, or dynamic. In all cases, they are composed of combinational logic with feed-back.)

- Combinational logic is composed of CMOS (MOSFET) transistors. In the past, we used BJTs... and they're actually making a comeback.

- Transistors are arranged in such a way as to perform logic equations: Inverter, NAND, NOR, etc. (Look up a CMOS Inverter to know what I'm talking about.)

Pretty easy, right!?!

I want to encourage you to look at the following references, in order.

(1) CMOS Digital Integrated Circuits by Kang and Leblebici --the best digital VLSI book ever (2) Digital Logic Circuit Analysis & Design by Nelson et al. --tough book on digital logic, but pretty good (3) Computer System Architecture by Morris Mano --works its way up from logic design (K-maps, etc.) to state machines to assembly language

And now, I'm going to through a curve ball at you: Any algorithm you can implement in software, you can also implement in hardware.

How is this possible? Hint: Think of Finite State Machines. And then look into ASIC and FPGA design of digital filters and digital control systems.


The boundary where pieces of code manage to control voltage. Where text crosses into electro magnetic flux. How does that happen?


    "The boundary where pieces of code manage to control voltage."
I just explained how that works.If you're talking about external ports, then just think of them as bidirectional registers connected to the outside world. This is literally all there is to it!!!

   "Where the text crosses into electro magnetic flux"
If you ignore crosstalk, which is caused by electromagnetic induction between the internal connections, then we can ASSUME that there is NO magnetic flux in DIGITAL microelectronics. However, if you're dealing with ANALOG or MIXED-SIGNAl VLSI, then you will obviously have inductors (and resistors and capacitors) to manage the fequency components at various stages of your circuits.

It's impossible to explain all this in 300 words. The three books I described ealier are all you need to become an expert in Digitial microelectronics. The Mano book deals strictly with discrete math, so if you don't have a strong background in Integral Calculus and Differential Equations, you can still learn a lot!

Here's some advise: If you're ever given the choice between Computer Science and Computer Engineering, pick Engineering. It's multiple orders-of-magnitude harder, but the rewards will be amazing.

It's like choosing to be a Pilot instead of a Flight Attendant. Trust me on that ;-))


That's a very unusual way to phrase the question, there are many steps between code and voltage in a CPU.

In brief, Code is stored in memory as a series of 1's and 0's.

Now the memory is stored as different voltage levels in transistors - 5 volts for a 1 and 0 Volts for a 0 for argument sake (but the voltage to logic value is arbitary).

Now the CPU executes the code by walking along different pieces of memory and executing different instructions depending on the value of those pieces of memory.

There's a lot more. For a start Google 'Introductory Digital Electronics' or 'Introductory Computer Architecture' and this might get you started.


OK... how do the 1's and 0's in code become different voltage levels? That's all I'm interested in. That's the software|hardware boundary I'm referring to (and artificial one perhaps, but the boundary of interest to me).


Well, where does the code reside? If it is in RAM, then those 0's and 1's are different voltage levels; if it's coming in through a cable (ethernet or USB or keyboard), then 0's and 1's are already different voltage levels; if they're on a hard drive, then they're becoming different voltage levels as soon as the head reads them from the magnetic platter.

I'd draw the boundary this way - hardware talks about how physical components will affect behavior of voltage levels, software talks about how one voltage levels will affect behavior of other voltage levels.


They don't become voltage levels, they are represented by voltage levels. There's nothing that converts a 1 to a particular voltage level. In TTL logic for example (an old type of transistor based logic circuits) it has a voltage swing of 0 to 5 Volts and a 1 is usually defined as 5 volts.

There's a think called a J-K flip flop that is worth having a look at - it's the simplest memory element conceptually and it takes a number of transistors to actually implement (6 I think from memory in TTL - I may be wrong - it was a long time ago).

In a real cpu D-Ram stores the bits (1's and 0's) and it works by storing a charge http://en.wikipedia.org/wiki/Dynamic_random-access_memory

The software / hardware boundary you speak of is a matter of definition and different people would define it differently. For a hardware developer, they would probably regard microcode as software, but an application developer would regard it as part of the hardware.

hth


Presumably, you're already familiar with how the text in your text editor gets converted to a binary representation. The code that gets stored in the iPad's flash memory is already represented in terms of voltages (See http://computer.howstuffworks.com/flash-memory1.htm). To run the app, iOS shall load the code in the flash memory to RAM, where again it's represented as voltages (See http://www.howstuffworks.com/ram.htm). The same is the case for the CPU executing the code.

Point of note is that transistors are used in all three cases (flash memory, RAM and the CPU), but the voltage levels for 1's can be different in each case. The voltage level is part of the design of the flash/RAM/CPU. (For example, when designing a CPU, choosing a voltage level is a tradeoff between performance (higher voltage implies faster clock speeds) and power consumption (higher voltage drains the battery more)).

If you're asking when the 0's and 1's became voltage levels, the answer is that they were created as voltage levels in the first place. At the physical level, the 1's and 0's are always voltage levels. When you compiled your code, the code generated by the compiler was represented as voltage levels in the CPU, then was written to RAM, which again was in voltage levels, and then written to the SSD of your MacBook Air, which again is in voltage levels.


   "At the physical level, the 1's and 0's are always voltage levels."
Let me throw you a curve ball: When we're dealing with DIGITAL logic, we're dealing with DISCRETE (and finite) values of SOMETHING - and it doesn't have to always be voltages.

Just to wet your appetite, take a look at "Current-mode circuits". These are circuits whose logic values are represented by discrete current levels, not voltage levels. And by the way, this has Nothing to do with Ohm's Law.

Another example can be taken from digital communications. We have discrete finite levels for: amplitude, frequency, and phase. And I'm probably missing another component that can also be quantized, but it's been many years since I took that class.

Yes, I remember now: Duty Cycle, as used in Pulse Position Modulation and Pulse Width Modulation. These types of modulation are commonly discretized and used in digital communications. Also very common in analog systems.


You mean they don't always have to be "voltages". Agreed. I only wanted to emphasize that at the physical level, they are not discrete. In this case, they happen to be represented as voltage drops.


Agreed.

I wish more people would take the time to look at the classic CMOS inverter: Looking at the VIn vs. VOut graph, it is overwhelmingly obvious that the word "digital" refers only to a higher level of abstraction for circuits that, in reality, are analog. (And more to the point, these SAME circuits are used as amplifiers in a typical analog design.)

Good luck explaining this to a CS major.


That's funny. I am really a CS guy. Just happened to work for a semiconductor company for a few years after college.


As a little kid I once asked my father "how does the eye work". He brought me a picture in an encyclopedia showing a disection of an eye ball with all the parts being given their medical, technical names. But my thirst of knowledge was great so my father told me about Newton and light rays, the lenses and how they focus the image on the retina's surface.

My thirst was not yet quenched, but my father didn't know what else to say, and I grew unpatient. Later in life I learned that the image projecten on the retina is then turned into electric impulses that go to the brain, are converted into a state of your psyche and perceived as an image coming from your senses.

For me it's a bit miraculous and divine how it happens. Back to your question: I don't really think that there is an answer to it, because even if you take a single letter on your screen, all the voltage shifts that happen inside of the machine are very complex, multiple hardware elements take part in it, there is the graphics processor and the screen, the different memory parts and the operating system that manages everything. In such a complicated structure it is very hard to say what actually happens that you can see a group of pixels on your screen that represents a letter "A". And even if you take a very simple microcontroller and a simple display there is quite a lot of operations going on inside, the data being stored, pushed through the arithmetic and logic unit, through the registers and so on. So there is no simple answer as the letter "a" is not a simple high voltage state in one single transistor, and even if you look at a really single byte or an electric impulse it probably does nothing at all.


Hardware has functionality in and of itself, for example a flop-flop is a circuit that can store a voltage (ie. a one for an arbitrary voltage or a zero for a different arbitrary voltage), an adder is a circuit which adds to sets of values etc.

In a computer there are a bunch of these different low level circuitry components which when combined in different ways can be used to produce any functionality. There is a certain amount of the circuitry which is unusable by the programmer which is reserved to know where all of these components are and how to connect one component to another. You can think of this as a program that is built into the hardware which knows how to install and run other programs.

Basically when you write code, there is a program or multiple programs which transforms the words you write into a string of binary numbers which tells the control circuitry how to arrange the free components to get the desired behavior.



Any boundary noted is is artificial and people doing software must know about the hardware - especially in the 1950s. Every subject in the book is written by somebody who has no clue what he is talking about directed at other people who are equally clueless and perhaps gullible. Most readers grab onto the vague feeling of understanding and ride it like a bearded man on a narwhal. This was the era when hipsters were coined.


This is similar to Matttheatheist's answer but I found this to be much more clear:

https://www.quora.com/Computer-Science/How-do-computers-work...


My answer is too long to fit this comment box, so here it is: http://paste.lisp.org/display/139933


perhaps try this book, i think it will answer some of your questions:

http://www.amazon.com/Code-Language-Computer-Hardware-Softwa...


You're asking the question the wrong way around. Text doesn't get converted to 1's and 0's or turned into voltages. The 1's and 0's and voltages are the first principles. Code is emergent behavior.

When you strike a key on your keyboard, what happens? A few circuit lines on the keyboard switch matrix that are normally open become closed circuits. These circuits are inputs to a keyboard decoder chip such as a 74C922. It interprets voltage levels on these lines as 1's or 0's and produces what we call a scan code. This scan code is output serially to the PC over the keyboard connection by sending voltage levels that switch between 1 and 0 over time.

The PC contains a chip (a collection of transistors wired together) that we call a keyboard controller. It sequentially switches a number of transistors to read the incoming scan code bit by bit and store each bit in a flip-flop. We call this collection of flip-flops a register. The keyboard controller also drives voltages representing 1's and 0's onto some circuit lines that are wired into the CPU. We call this an interrupt. More transistors within the CPU will detect that interrupt signal and copy a pattern of 1's and 0's into a particular collection of flip-flops, which we call the program counter register. We now have the concept of an interrupt handler.

The CPU's transistors now drive the 1's and 0's in the program counter register (flip-flops) onto a group of circuit lines. We call that collection the memory address bus, and the pattern an address. The memory is itself a giant collection of flip-flops, a few (8 or 32 or whatever) of which are wired to respond to this pattern on the address bus and drive their contents onto another group of circuit lines that we call the data bus. These contents we call an opcode, and the pattern of this opcode we might call a port-read instruction. Inside the CPU, a particular group of transistors activates to execute this opcode, with the result of copying the contents of a particular port (the flip-flops in the keyboard controller) into a register within the CPU (more flip-flops.) Then the next opcode activates another group of transistors to copy the register flip-flops onto the memory bus, where the 1's and 0's latch into some more memory flip-flops. (There's more to interrupt handling than this of course, but that's the bare essentials.)

Do that many times and you might eventually have a sequence of memory flip-flops with a pattern of 1's and 0's representing a string such as PRINT "HELLO WORLD". But it's not that this text was converted into 1's and 0's. The 1's and 0's are the first principles. It's flip-flops and electric signals all the way down.

"Text" or "code" or "software" are merely abstractions we use to describe emergent behavior arising from groups of transistors shuffling around these 1's and 0's.


IMHO this is the most accessible and sensible ("makes most sense to me") answer so far. Thanks.




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

Search: