This laboratory exercise provides practice with the CPU architecture, especially
How an instruction is processed along the CPU data path cycle
How values from memory are incorporated into CPU instructions
Part I: CPU Datapath
Exercise 1
Open
this page
in a new window by right-clicking on the link and
choosing "Open Link in New Window" and then come back to this
lab. This will bring up the Knob & Switch Datapath
Simulator. You should resize your window(s) so that you can see
both the simulator and the lab at the same time.
This simple CPU contains four registers, but no control unit. That
is because you will be the control unit! You direct
which registers are operated on, and which operations are done.
Take a look at the top box that says "Register Bank." At the right
are two command knobs, "A Bus Address" and "B Bus Address." These
specify which registers should be read as inputs to the ALU. You
can turn each of these knobs one notch by clicking on them. Try
that now.
Set the A bus address to R2 and the B bus address to R1 by
clicking each appropriately. When a cycle is executed, the CPU
will send the values from R2 and R1 along the bus.
The register values are the textboxes with a green
background. Currently, the default values of zero are in all the
registers. Set the value of R1 to 42 by clicking on the zero and
typing in a new value.
Set the value of R2 to be 31.
Now we are almost ready to begin a CPU cycle. First, be sure that
the "Animation Speed" is set on "Medium" and "Number Base" is set
to "+-10." (Both of these are the default, so if you haven't
changed them, you should be in good shape.)
Based on the settings of the bus addresses, what values do you
expect to show along the A and B bus? (The text boxes immediately
below the two knobs you set.)
Here is what should happen when you click on "Execute" (don't do
it yet!) You should see the register values flash and then the
arrows connecting the register bank to the "A Bus" and "B Bus"
flash. When these flash, click "Halt," which should have appeared
in place of "Execute."
Ok, now click "Execute." Are the values along the A and B buses
what you expected? (You may click "Halt" once these values appear.)
Exercise 2
Now you have seen how to send data to the ALU. Next we will tell
the ALU what to do with the data. Notice that in the bottom right
of the simulator is yet another knob entitled "ALU Operation." As
you might guess, this knob instructs the ALU what operation to
perform on the data it has received. Click the knob so that it
points to the instruction "A-B."
There is another box to the left of the ALU knob. This is a
diagnostic that reports information on the result of performing
the operation you just selected on the input you provided to the
ALU. Thus, "zero" has a check next to it because the ALU
previously hadn't done anything and its default result was in fact
zero. You should see this reflected in the textbox labeled
"C:". What do you think will happen when the to the boxes when the
result is less than zero?
The current settings settings for your simulator should be for it
to run "A-B", where A is R2 and B is R1). With the values of R1=42
and R2=31 that you input earlier, what do you expect the result
"C" to be?
Run the simulator to start a cycle with these settings. You
should halt the simulator when the result flashes in "C." Is the
result what you expected? What happened to the diagnostic boxes?
(You may ignore the overflow boxes for now).
Exercise 3
Of course, we want the ALU to do something with the result
it has just calculated. This result gets sent along the C bus back
into the register bank, where there is one last knob for you to
set. The "C Bus Address" knob in the top-left of the simulator
indicates where the CPU which register it should store the result
of the calculation into. Click the knob to store the result in R3.
Now, run a simulation of a full cycle to subtract R1 from R2 and
store the result in R3. You could think of this in terms of your
prior programming experience as the statement:
R3 = R2-R1
Exercise 4
What would happen if you placed the number 15 in R0 and then set
all the knobs so that
A bus is R0
B bus is R0
ALU operation is A-B, and
C bus is R0
Use the simulator to verify your prediction.
Exercise 5
What settings (e.g., knob positions) would cause the value stored in R3 to be doubled?
Set R3 to 12 and use the simulator to double that value, using the
settings you described above.
Exercise 6
Suppose we want to add the contents of R0, R1, and R2, placing the
result in R3. How many cycles will be required? Note that the ALU
can only add two things at a time, so you will have to break the
problem down into smaller steps.
Write down the settings for each of the cycles to accomplish this task.
Place the values 1, 3, and 5 in R0, R1, and R2, respectively. Use
the simulator with the settings you described to add these,
placing the resulting sum in R3.
Close the Datapath window.
Part II: CPU and Memory
Exercise 1
In your simulation thus far, we have only been using data from a
limited number of CPU registers. Actual computers have much more
storage in their main memory. How does the data get from memory
into the registers? We need another communication channel (bus).
Right-click
on this link to open another simulator in a new
window. You should see many familiar pieces from Part I, but
there are also some new pieces.
Two notable components here are the memory and bus switches. The
memory values on the left of the simulator represent RAM. The
button next to each memory location indicates which location will
be read (R) from or written to (W) in one cycle of the machine.
Notice that the bus connections from the ALU to the register bank
now have switches on them. Click these connections to toggle (open
and close) the switches.
Open the circuit from the ALU into the C bus, so that there is no
longer a connection.
There are also two new connections along the C bus. Remember
this is the pathway into the register bank. With memory a part
of our machine, we may want to put memory values into registers,
rather than ALU results. The connection pointing up from the "Main
Memory Bus" into the "C bus" allows us to control this.
Click to close the connection from the "Main Memory bus" to the "C
bus." The connection from the C bus to the memory bus should be
open.
Put the value 42 into memory location 0, and select it as the RW
location.
Set the connection switch along the "C Bus" into the
register bank so that it is closed. Select the "C Bus address" to be R0.
Based on these settings, what do you think will happen when you
click "Execute"?
Verify your prediction.
You should have found that the value 42 was copied from memory
address 0 and stored in R0. You may have also noted that the ALU
was busy working on values that were subsequently ignored. It
turns out this is easier than adding circuitry to disable the ALU
during a memory read.
Exercise 2
How can we write data to memory? By closing the connection from
the C bus to the main memory bus.
Open the switch from memory to the C bus so that nothing is read
from memory, then close the switch from the C bus to the memory so
that a result can be written to memory.
Set R0=8 and R1=9, and set the A Bus, B Bus, and ALU operation to
calculate R0+R1.
Connect the ALU to the C bus by closing the switch that connects them.
Now everything should be set to add our registers and send the
output from the C bus along to memory. Set memory location 1 as
the destination for the result using the RW button.
Verify this operation by clicking "Execute."
If you have any questions at this point, be sure to ask the
instructor or mentor.
Exercise 3
What settings (bus addresses, ALU operation, switches, and memory
RW) would result in R0-R3 being stored in memory location 4?
Set R0=23 and R3=16. Using your settings above, verify the desired
outcome.
Exercise 4
Change the value of memory location 1 to 28.
What settings would result in the contents of memory location 1
being copied to R2?
Using these settings, verify the desired outcome. Be careful to
set all the switches correctly!
For those with Extra Time
Exercise 5
Suppose we wish to add the contents of memory locations 3 and 4
and store the result in memory location 5.
How many cycles would this take?
What are the settings for each cycle?
Set memory location 3 to 13 and memory location 4 to 29 and verify
that your settings achieve storing 13+29=42 into memory location 5.
Created: Jerod Weinman, March 16, 2009
Portions adapted from Dave Reed, "A Balanced Introduction to Computer Science," Exercises 14.1-14.6.