Thread: The inner-workings of a uP

  1. #1
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72

    The inner-workings of a uP

    Hey guys,

    I have a question that has been on my mind for a while now: How does code physically run on a microprocessor or other programmable IC? What I mean is how does the object code physically control the transistors? How are they turned 'on' and 'off'?

    I know about assembly code and computer architectures, but I'm missing the step explaining how the lowest level binary code controls the physical devices on a chip. How does code compiled down to '1's and '0's actually turn a transistor on or off?

    Thanks.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Each instruction opcode is a bit pattern. This bit pattern flows into the instruction decoder unit, causing it to change states and drive the rest of the circuitry in the CPU. A modern CPU has over a billion transistors on it, so it's not very easy to understand it at the level of single transistors. Ultimately, that's all that's happening, though.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Quote Originally Posted by brewbuck View Post
    Each instruction opcode is a bit pattern. This bit pattern flows into the instruction decoder unit, causing it to change states and drive the rest of the circuitry in the CPU. A modern CPU has over a billion transistors on it, so it's not very easy to understand it at the level of single transistors. Ultimately, that's all that's happening, though.
    Well, I know about instruction decoders and how each op-code is decoded. I was using the example of one transistor - but my question can be extrapolated to millions.

    How does the instruction decoder physically receive the stimulus to change states? There is a certain point where the binary is converted to positive voltage or, in most systems, 0 volts. I.E. 'on' or 'off' states. How does this happen?

    This is a level of detail most literature and people gloss over when describing how code runs on an architecture.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The heart of a digital circuit is the clock, which synchronizes all the logical units. The instruction decoder waits until its clock comes in sync with the memory bus clock, then asserts the physical address of the next instruction onto the address lines. The memory controller responds to this, and causes the cells of RAM which correspond to that address to dump their contents onto the data lines. The instruction decoder is physically in contact with these lines, and reads the data from the bus.

    (I'm totally ignoring cache.)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Quote Originally Posted by brewbuck View Post
    The heart of a digital circuit is the clock, which synchronizes all the logical units. The instruction decoder waits until its clock comes in sync with the memory bus clock, then asserts the physical address of the next instruction onto the address lines. The memory controller responds to this, and causes the cells of RAM which correspond to that address to dump their contents onto the data lines. The instruction decoder is physically in contact with these lines, and reads the data from the bus.

    (I'm totally ignoring cache.)
    Again, I think you're missing what I'm asking. I understand how digital systems work on the level you are using.

    What I'm asking: how does compiled code translate to voltage inputs to the instruction decoder, etc?

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Most modern processors/motherboards don't use 0-5 volts anymore. Apparently it's more efficient to fluctuate somewhere between 1-3 volts. The values of instruction code are stored somewhere, and when indicated, they are read or written to. When pulling from memory the value is asserted on the line as its appropriate voltage level. RAM is constantly powered in order to keep the appropriate values in place. When querying from memory, the values are asserted (works similar to a multiplexer/demux I believe).

    I'm thinking your question lies in how the hardware knows to write what voltage when it reads instruction code. The hardware doesn't see instruction code, it sees voltages. The processor is what determines what to do with those voltages. We see it on the screen as "jmp ebx" etc.

    edit:

    Keep in mind that the computer is going out of its way to show us a representation on the computer. The fact that we chose certain voltages to show up as characters, and that we put those characters on keys at a keyboard is a little irrelevant. Signals sent from keyboard to processor are then sent/saved elsewhere in the system.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    A uP uses a wide variety of circuits such as Latches, Gates, and control logic. The registers are usually implimented as tristate buffers with access to multiple internal buses, aka ports, connecting the different sections of the uP.

    for example, in a simple uP with registers A and B and given teh command

    MOV A , B

    it would decode to signals to the B registers buffer to place PORT1 in WRITE mode (i.e. the value in B is placed on PORT1), a signal to register A to place PORT1 in READ mode, i.e. the value on PORT1 is connected to the inputs of A. The negative goign edge of the clock would then cause the value on PORT1 to be latched into register A's buffer. The mode that a particular port is in is entirely relative to an individual register, i.e. a port can be in write generally only for one register or circuit, but can be in read for 1 or more circuits.
    Other circuitry handles other parts of the fetch/decode/execute cycle. Generally speakign these crcuits act independantly of one another. The instruction fetch circuit for example would use Register IP to determine the next byte to fetch.

  8. #8
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    How does the processor 'see' the voltage code? How is the value asserted on the line based upon the 1's and 0's of a program? I know 1's and 0's are an abstraction, but I'm wondering how low level code is physically translated to a processor.

    I want to know on the lowest level - not using a 'system' view of architecture blocks.

    How does the processor take a 1, for a simple example, and assert 3 volts on a line? What on the processor is able to recognize directions from the low level code? There is a step in-between that is missing.

    We see "jmp ebx" on our screen. The jump instruction's op-code may be 0110. So how does 0110 actually map to voltage? In our small example, you'd need 4 transistors - the first would be turned off, the middle two turned on, and the last turned off. That would pass the signals necessary for 0110. That's understood completely. But HOW does a transistor know when to turn on or off? (Again, I'm simplifying to one transistor. We could be talking millions here).

    Quote Originally Posted by abachler View Post
    A uP uses a wide variety of circuits such as Latches, Gates, and control logic. The registers are usually implimented as tristate buffers with access to multiple internal buses, aka ports, connecting the different sections of the uP.

    for example, in a simple uP with registers A and B and given teh command

    MOV A , B

    it would decode to signals to the B registers buffer to place PORT1 in WRITE mode (i.e. the value in B is placed on PORT1), a signal to register A to place PORT1 in READ mode, i.e. the value on PORT1 is connected to the inputs of A. The negative goign edge of the clock would then cause the value on PORT1 to be latched into register A's buffer. The mode that a particular port is in is entirely relative to an individual register, i.e. a port can be in write generally only for one register or circuit, but can be in read for 1 or more circuits.
    Other circuitry handles other parts of the fetch/decode/execute cycle. Generally speakign these crcuits act independantly of one another. The instruction fetch circuit for example would use Register IP to determine the next byte to fetch.
    Again, you are describing WHAT happens not HOW it happens. I understand exactly what you are saying - but we are still talking on too high a level. See my above example.

    I'm looking to know how the circuits receive their voltage inputs from the low level code.
    Last edited by Cell; 02-22-2009 at 10:47 PM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The hard disk read head translates the magnetic state of the disk into high or low voltage, which is sent out over the bus. This is how compiled code 0110 is mapped onto voltage. There's your low-level answer. If you store that in RAM, you use the voltage to change gate states, so that when you send pulses through those gates, you get the voltage back that you sent in.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Transistors are controlled by an input, so if you want to output 0110 somewhere, it needs to have a 0110 input (unless you use the other kind of transistors, and the output is the inverse of the input, in which case you'd use 1001 as the input).

    There is probably some website that describes the physics involved in transistors, but the basic principle is that they are electronic on-off switches [when used in microprocessors].

    By using a combination of several transistors, we can make gates that do AND, OR, XOR, NOT, ADD, SUBTRACT and more complex sets of combinatorial logic. It obviously gets fairly complex fairly quickly - a simple MEMORY cell [in an SRAM] is a 6-transistor combination just to hold ONE bit in a stable state.

    A microprocessor knows what to do by being given instructions from memory. It starts at a defined place (e.g 0xFFFFFFF0 for x86-32) - this is in a ROM of some sort, which has a fixed content for any given location in the ROM [1]. From there it is given instructions how to start the memory controller, read data from the hard-disk and many other things it needs to do before you can run your application code.

    [1] Early ROM's where "mask programmed", so the ROM itself would have transistors designed to give off a particular bit-pattern for address X. Modern ROM's are usually FLASH-memory, which is a tiny capacitor (like a battery, almost) that holds a charge or no charge, corresponding to a 0 or 1 value to be output. There are sensitive transistors connected to the capacitor that feeds the value out based on the read-commands sent by the memory controller. Because the transistors are VERY sensitive, they do not need many electrons from the capacitor, so the small capacitor will hold it's charge value for a long time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    I had wondered the same thing myself from the first time I ever saw the guts of a computer. When you look closely enough at how the processor works on a physical level (ignoring the high level discussions and concepts of why some abstract concept is occurring), IMHO even the simple electronics of a calculator are amazing.

    Salem had a contest a while ago on the contest board for an implementation of WireWorld (*cough* that he, er, never did anything with *cough* ). In learning how to implement the rules of WireWorld, it inspired me to learn a bit more about actual processors, and I learned quite a bit about how they physically work (lots of Google and Wikipedia). While the rules of WireWorld are not quite how electricity actually flows through real circuits, I think it is a fairly good representation that demonstrates what you are asking for. If you get more accurate than what WireWorld does then you are diving into the world of electronics and how electrons flow and whatnot.

    If you examine this WireWorld computer, you can see how digital signals are interacting and changing how other digital signals are interpreted. There are no 1's or 0's, rather a stream of "electrons" or no "electrons" that are expected to be received in certain ways by various parts of the computer. In a computer, these high and low voltages affect transistors according to how the creator of the circuit designed them. For simple circuits, the logic circuit might be just a single "and" type arrangement where the output is a 1 (the high voltage value) if and only if both inputs are 1's. For more complex circuits, the logic circuit might be a cascade of logic gates where a series of 4 bits will result in 7 possible output signals. It all depends on the design and function of the circuit.

    Now, when you power your computer on, electricity begins to flow through the components. The first thing that happens (after the microsecond it takes for electricity to flow from the power supply through the wires) is BIOS gets the electricity and begins sending those "1's and 0's" (or high voltage and low voltage) signals appropriately to the various components to do the POST and then get boot instructions from the boot device. This is hard coded into the BIOS, though you can change some of the settings (like what the boot device is) which will change those instructions appropriately. At the most basic level though, BIOS is basically doing the same thing that the WW computer I referenced is doing, but orders of magnitude bigger and more complex.

    So BIOS handles getting the boot instructions from the boot device to place into RAM at some starting point, and then control is passed on to the processor which begins processing the instructions at that starting point (loading the OS and so on). When the processor reads an instruction (whether it is the first or 4 billionth instruction), it has already emitted an appropriate voltage "message" to RAM to have the data at the appropriate address (where the Instruction pointer points to) sent back to the processor, all timed appropriately by the system clock. This instruction is nothing more than a series of high and low voltages that are the same as what is in RAM. As this messages is received by the processor, the pulses flow through the logic of the processor in the same way as the WW computer I referenced (but using real electronics and transistors arranged in logic gates and such). The actual instruction for JMP (whatever that combination of high and low voltages is) flows through the logic gates in such a way that when the voltage pulses of the argument (where to jump) is received, they are directed to the place in the processor that will change the Instruction Pointer. The actual instruction for MOV flows through the same logic gates in such a way that when the arguments are received, appropriate signals are emitted to the RAM bus and so on and so forth.

    When you load a program, the instructions used basically retrieve the 1's and 0's from the HD (which translates the magnetic data to the appropriate voltages) and stores them in RAM, and then the processor retrieves and processes them just like any other instruction, whether it is a program you just compiled or whatever.

    [edit]Wow, 3 responses in the time it took me to write this up. I are slow. [/edit]
    Last edited by jEssYcAt; 02-23-2009 at 05:17 AM.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  13. #13
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Guys, first I want to thank you for participating in this thread. I think we're getting to the answer I'm looking for by clarifying what I am actually asking.

    So, basically -

    1) The compiled code is placed on the HDD. This is where the code is translated to signals usable by the uP electronics. The lines of each program are stored (as voltages) on different places on the platter and are accessed based upon that address in which they are stored on the disk.

    2) The program, when it's 'called', is loaded into RAM. As a very, very simple model the RAM has 2 buses - the address bus and the data bus. The address bus lines are the inputs into the RAM and the data bus lines are outputs (again, a very simple abstraction for our example).

    3) The program starts at a certain address in the RAM. To begin executing the program the starting address of the program is 'placed' as a pattern of voltages on the address bus (that is, one voltage per single line of the bus, of course) corresponding to the starting address. Each subsequent address (containing the program lines of code) is selected using the address bus (as the program executes) it spills its contents (voltages) onto the data lines which are fed (after some buffering, etc.) to the uP data input pins.

    Now, this is where some of the the uP outputs are fed back to the RAM. This is for instructions like Jump or Branches. That way, you do not have to execute the lines of code sequentially all the time - it is possible to jump around in the RAM.

    4) Finally, which I knew already, the data inputs into the uP are routed to the different architecture blocks. So the first X bits will be routed to the instruction decoder to figure out what class of instruction is being executed, etc.


    I knew steps 2-4. Step 1 is where I am unsure. So thanks to you guys I am now able to narrow my question down a bit more so I can look around on my own. We can also discuss it here.

    So I guess my question is how does step 1 happen? How does the compiled program, when it is stored on the magnetic hard disk, turn into voltage levels?

    (I should mention I have a physics background. That is how I know about transistors and how they operate. I also have a good background in computer architecture and digital systems. However, it seems all my courses glossed over how the HDDs work. The step in which the code is translated and stored on the HDDs magnetic platters is never discussed.)

    Again, thank you all for discussing this with me.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Now, this is where some of the the uP outputs are fed back to the RAM. This is for instructions like Jump or Branches. That way, you do not have to execute the lines of code sequentially all the time - it is possible to jump around in the RAM.
    You write back to the RAM for write instructions only. Jumps and branches only change the instruction pointer, which is a uP register, not a place in RAM. This affects the next address that is asserted on the address lines of the RAM, but it doesn't cause a write.

    However, it seems all my courses glossed over how the HDDs work. The step in which the code is translated and stored on the HDDs magnetic platters is never discussed.
    Well, the code could come from anywhere, really. Network, magnetic platter HDD, solid state HDD, optical medium (CD-ROM, DVD, ...), or it could be stored in a RAMDISK (reserved memory in the RAM which is treated by the OS like a mass storage device). The possibilities are endless. A computer deals in bit patterns, and the transition between different physical representations of those patterns (high/low voltage, magnetised/not, reflecting/not, even nuclear spin if you want to get fancy) is straight-forward, really.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Quote Originally Posted by CornedBee View Post
    You write back to the RAM for write instructions only. Jumps and branches only change the instruction pointer, which is a uP register, not a place in RAM. This affects the next address that is asserted on the address lines of the RAM, but it doesn't cause a write.
    I didn't mean you write back. You just have a feedback system - you're feeding back the new address. Inside the uP you'll have Branch Unit or something similar that will calculate the new address. That address is feedback to the address bus. (Though I do know this is a slight simplification - it's not really the question).

    Quote Originally Posted by CornedBee View Post
    Well, the code could come from anywhere, really. Network, magnetic platter HDD, solid state HDD, optical medium (CD-ROM, DVD, ...), or it could be stored in a RAMDISK (reserved memory in the RAM which is treated by the OS like a mass storage device). The possibilities are endless. A computer deals in bit patterns, and the transition between different physical representations of those patterns (high/low voltage, magnetised/not, reflecting/not, even nuclear spin if you want to get fancy) is straight-forward, really.
    Sure, but we were using the HDD as an example. I guess you're right though - each medium has a way of translating the compiled code to signals.

    But it's not straightforward. Simply saying the bit patterns are physical state representations of two-state devices is straightforward but it's hand waving to say as much. That's jumping from point A to point C. As for nuclear spin, well that would yield more than 2 states.

    How does our abstract view of a bit pattern translate to magnetized/not or high/low voltage? It's a SIMPLE thing to connect when you're going from point A to C, but not from point A to B.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-03-2009, 08:02 AM
  2. Inner Workings of Vector Graphics?
    By thetinman in forum Tech Board
    Replies: 2
    Last Post: 12-18-2008, 05:44 PM
  3. Internal Workings Of The = Operator?
    By Geolingo in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2003, 11:07 PM
  4. inner workings of win API functions
    By SAMSAM in forum Windows Programming
    Replies: 1
    Last Post: 02-23-2003, 06:17 PM