Where software meets hardware..

Oliver Fromme olli at lurza.secnetix.de
Thu Jun 21 16:33:43 UTC 2007


Mark Jayson Alvarez wrote:
 > Let's say I have a very simple washing machine program.
 > Now it has a timer which the duration of the spinning can be set.
 > If I press the 3-minute button, wires beneath will get shorted.
 > Electric current will flow into pin number 5 of the parallel cable
 > connected to the parallel port of my PC. Now the CPU has a pin
 > connected to this port. If it receives let's say 5V, it will stop
 > what it's doing and 
 > 
 > > fetches the
 > > address of an "interrupt handler routine" from memory,
 > > and jumps to that address (i.e. starts executing
 > > instructions from that address).  That handler is
 > > usually installed in memory by the operating system.
 > > The code checks which device caused the interrupt,
 > > and then executes the appropriate routine in the
 > > corresponding device driver.
 > 
 > And when exactly did the Operating system installed this interrupt
 > handler??

When it boots.  The processor supports an interrupt
handler address table.  That's simply a list of memory
addresses which is itself stored in memory.  Each
kind of interrupt (they're numbered) has an entry
in that table that points to the appropriate interrupt
handler which has been installed by the OS upon boot.

For example, let's say interrupt line 7 is connected
to the parallel port.  So when the processer receives
a signal on that line, it looks up the address that
is stored in entry #7 in the interrupt table.  Then
it will execute commands at that memory address, and
afterwards it will resume whatever was interupted.

 > And suppose this handler runs the driver and the 
 > appropriate routine inside it, how did the driver able to convert the
 > electric
 > current into a machine understandable data and was able to pass it
 > to a program and the program receive the data as 3 minutes?

It depends.  If you have one interrupt per button,
then there's a one-to-one relation ship between
buttons and interrupt numbers.  So if you press
that 3-minutes-button, let's say it's connected to
interrupt pin #7, so the processor will run the
handler that has been registered for interrupt #7.
That handler is specific to that interrupt and to
that button, so it "knows" that the 3-minutes-button
has been pressed when it is called (because that's
why it was installed for the interrupt in the first
place).  There is no need for the driver to "convert
the electric current".  The handler is called as a
reaction to the interrupt signal, and that reaction
in itself contains the information about the press
of the button.

However -- normally you don't have one interrupt
per button, but rather one interrupt per device.
Having one interrupt per key (on a keyboard) would
be very inefficient.  Instead, there is one interrupt
for the whole keyboard (or for all the buttons on a
device).  So, any button press will cause the same
handler to be executed.  The device driver routine
knows that a button has been pressed, but it still
has to find out which one.  How does it do that?

Well, in simple cases (like embedded systems in a
washing machine), the electrical lines from the
buttons are connected to I/O pins on the processor,
or on separate I/O chip which is connected to the
actual processor.  Basically this is similar to an
interrupt line, in that it causes a pin to go from
0 V to 5 V (or whatever voltage levels are used).
But the difference is that it does not cause an
interrupt to occur.  The processor simply ignores
those I/O pins during normal operation.  However,
the processor supports machine codes that can read
the current state of the I/O pins.  If the processor
executes such a code (i.e. a certain byte sequence),
it copies the current state of the I/O pins into a
register, where it can be dissected and examined
with other machine instructions.  That state is
usually encoded in a binary format, where each bit
corresponds to one I/O pin.  A single byte has 8 bit,
so it can contain the information of 8 such I/O pins.
If a pin is 0 V, the corresponding bit is 0,
otherwise it is 1.

 > Driver is just a software right?

Right.

 > I'm sure if I can find out how electric current have been actually
 > converted into 1's and 0's I will not have trouble understanding
 > how it can be converted the other way around.

Actually nothing needs to be converted.  "0" and "1"
are just interpretations of different voltage levels.

 > It has something to do with registers right? What are this
 > registers looks like? A microchip that can get written using
 > electric current?

Processor register are simply small pieces of memory
inside the processor.  They are required for the
processor to perform calculations and other things,
because they cannot be performed directly in RAM.
In order to do anything with data stored in RAM, the
processor has to load values into registers, and
when it's done, the result have to be stored back
into RAM.

For example, in order to add two numbers that are
stored in memory, the processor loads both of them
into two of its registers.  Once they're there, they
are added by the ALU (== arithmetic-logial unit,
part of the processor), and the result is again
stored in a register.  Then the contents of that
register are written back to main memory.

 > What are these 1's and 0's look like anyway? How are they written in the
 > memory? A chemical reaction when electric current flows into the ram?

No, it's all electrophysical, not chemical.  Well, a
"1" usually looks like 0 V, and a "1" looks like 5V
(or 3 V or whatever).  Inside electronic components
such as processors, RAM, graphics and network cards
etc., bits are almost always represented as voltage
levels.

 > Data that is written in the RAM differs the way they are written in a
 > hard drive or a CD right? But the truth is they are all 1's and 0's?

Yes.  All media (RAM, flash, disks, tapes, CD, DVD
and even punch cards) have in common that they store
data as "0" or "1" in one form or another.  The
important property is that the media is capable of
having two distinct states, so one of the states is
assigned the "0" value and the other the "1" value.

For example, CDs have tiny "pits" on the surface.
A laser beam measures the width of those pits (small
ones and large ones), and a DSP converts that into a
sequence of "0" and "1".

On hard disks the same information is stored using
magnetism.  RAM (DRAM == dynamic RAM) uses tiny
capacitors to hold a very small electric charge that
represents the bit value.

If you want to know more details about how a processor
access data in memory, how address bus and data bus
works, how a processor is built up from transistor
functions, I strongly recommend that you buy a good
beginners book of processor design.

I remember at school we've built simple electronic
components ourselves:  a flipflop (that's a simple
1-bit memory) from two transistors, logical gates
(i.e. "and", "or", "not" circuits), bit counters,
adders and similar things.  At that time that was
very enlightening for me.  I suggest you try
something like that, too.  You can buy electronic
construction and experimentation kits at toy shops.
Don't be afraid that they're intended for children,
I know quite some adults who play with things like
that once in a while, including myself.  :-)

Best regards
   Oliver

-- 
Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:  http://www.secnetix.de/bsd

"C++ is the only current language making COBOL look good."
        -- Bertrand Meyer


More information about the freebsd-chat mailing list