Lab: Knob & Switch Computer
CSC 211 - Computer Organization & Architecture - Weinman
- Summary:
- You will explore a complete CPU architecture, especially
how
- an instruction is processed along the CPU data path cycle,
- values from memory are incorporated into CPU instructions, and
- the fetch-execute cycle operates.
Background
The Knob
& Switch Computer uses metaphor and an incremental approach to
demonstrate the interactions between CPU datapath, CPU control, and
main memory. We use it in this lab to introduce the main concepts
of computer organization before getting into the details of MIPS instructions
and digital logic.
Exercises
A. The Datapath
Open the K&S
Datapath Simulator. If the link doesn't open, then visit the Knob
& Switch Computer and click the appropriate link.
- If necessary, resize your windows so that you can see both the simulator
and these lab exercises at the same time.
- If you use the NoScript browser add-on, be sure to enable JavaScript
on this page by clicking the Options
button in the lower-right corner of the window and selecting Temporarily
allow all this page.
This simple CPU contains four registers, but no control unit (or memory).
That is because you will be the control unit! You direct which registers
are operated on, and which operations are performed.
-
Take a look at the top box that says
Register Bank. The register values are the text boxes with
a green background. Currently, the default value of zero is in all
the registers.
Set the value of R1 to 42 by clicking on the zero
and typing in a new value. Then set the value of R2 to be
17.
- 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.
What digital logic components do these knobs correspond to?
- Set the A bus address to R1 and the B bus address to R2
by clicking each appropriately.
Note: A bus is essentially a collection
of wires used to transport data from one component to another. A better
animation would show the four bits flowing along four wires.
- Find the ALU. What operation is the ALU currently configured to perform?
- Find the C bus and the C bus address. Where do you think the result
will be stored?
- Click the Execute button and watch the data flow through
the datapath. Is the result what you expected?
- As in A.1, set R1=42 and R2=17
again then "program" the simulator to compute R3=R1-R2
and execute your simulation.
B. Memory
In this section, we add memory to the datapath and switches to direct
traffic to and from it.
Open K&S
Datapath Simulator with Main Memory. There are two new components:
main memory and bus switches.
The main 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. (If
you don't see the main memory, try making the browser window larger.)
- 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.
What digital components might these switches represent? (Not literal
switches or individual transistors.)
- Set Mem[1]=42. That is, put the value 42 into
memory location 1.
- Select Mem[1] as the RW location.
- Find the main memory bus. Close the switch from the main memory bus
to the C bus. Then open the switch from the ALU to the C bus.
What do you think will happen when run an execute cycle along the
datapath?
- Click the Execute button and watch the
data flow through the datapath. Is the result what you expected?
You should notice two things:
- reading from memory takes more time than reading from a register,
and
- the ALU computed a result even though that result was ignored.
If you did not see both those things, set Mem[1]=43 and
click Execute again.
- How can we write data to memory? By closing the connection from the
C bus to the main memory bus, as follows:
- Open 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.
- Connect the ALU to the C bus by closing the switch that connects them.
- Open the switch that connects the C bus to the register bank.
- Set R0=8 and R1=9.
- Set the A Bus, B Bus, and ALU operation to calculate R0+R1.
Now everything should be set to add our registers and send the output
from the C bus along to memory. Where in memory will the result be
stored? Verify this by clicking Execute.
- You may have noticed that the K&S architecture differs in a very
important way from the MIPS architecture. What does K&S allow that
MIPS does not?
-
Thought question: Suppose we wish to subtract
Mem[6] from Mem[7] and store the result in Mem[8]. That
is,
-
Mem[8]=Mem[7]-Mem[6]
How many cycles would this take?
C: Control
In this section, we add a virtual control unit to set the knobs and
switches according to a stored program.
Open the K&S
Simulator with Microprogramming. This simulation adds a Microprogram
Memory, which contains binary numbers representing the positions
of the knobs and switches.
- In the Microprogram Memory, click the first green arrow to store the
knob and switch settings in the first microprogram location. You should
see the xs disappear and binary numbers appear. Verify that
they correspond to the default settings of the knobs and switches,
as well as the memory RW address.
- Set Mem[7]=42, then configure the datapath and memory
to load this value into R0:
-
R0=Mem[7]
Click the first green arrow and notice how the binary values change.
- Set up the datapath to double the value in R0, storing the
result in Mem[8]. Click the second green arrow to add
this as the second instruction.
- Click Execute to run your program. Notice that the simulator
changes the datapath settings according to what is stored in the microprogram
memory.
- Make real the thought question from B.8:
Write a microprogram to subtract Mem[6] from Mem[7]
and store the result in Mem[8]. You can type directly
in the microprogram memory, if you prefer.
- Test your program. Before you click Execute, you may want
to set the Animation Speed to Fast.
D. Fetch-Execute Cycle
In this section, we add the ability to store machine language instructions
in memory.
- Open the Complete
K&S Computer Simulator.
- Study the control unit. Find the instruction register (IR), the program
counter (PC), and the micro-instruction register.
- In Mem[0], type the instruction
-
add r1 r2 r3
- Let's compare machine and asssembly versions of this instruction:
- On the pull-down menu next to the instruction you just entered, change
the view from Auto (or Inst) to 2, so that
you can see the corresponding (binary) machine language instruction
stored in memory.
- Change the view back to Inst so you can see the instruction
in (human) assembly language.
- Enter the HALT instruction in Mem[1].
- Put some more interesting (i.e., non-zero) numbers in the registers,
set the animation speed to Fast, and
execute the program.
- Observe the instruction flowing into the IR, the PC being updated,
and the instruction executing in the datapath.
- Notice that the PC update happens in parallel with interpreting the
instruction.
- Open the reference
card for the K&S ISA and find the instructions for accessing memory.
- Write a program in memory locations 0-4 to subtract Mem[6]
from Mem[7] and store the result in Mem[8].
Be sure to end your program with a HALT instruction.
- Test your program. Before you click Execute, click Reset
to reset the PC (to zero). You may also want to set the Animation
Speed to Fast or Fastest.
- Referring to the K&S ISA reference, is the K&S ISA branching more
like MIPS or ARM?
- Thought question: What do you think the following program will do?
-
0: SUB R0 R0 R0
1: LOAD R1 5
2: ADD R0 R0 R1
3: BRANCH 2
4: HALT
5: 1
The last line is not a typo: Mem[5] should contain the
value 1.
- Make it real:
- Enter this program in main memory (starting at memory location 0).
- Reset the PC then Execute the program.
- As you execute the program, keep an eye on the PC.
Reflection
What concepts from today's reading (Section 4.1) can you identify
in the Knob & Switch Computer?
Acknowledgments: The Knob & Switch Computer was developed
by Grant Braught at Dickinson College and Dave Reed at Creighton University.
Parts A and B were originated by Jerod Weinman, with portions adapted
from Dave Reed, A Balanced Introduction to Computer
Science, Exercises 14.1-14.6, while Parts C and D were
originated by Janet
Davis, with edits in this derivative work by Jerod Weinman.
Copyright © 2015 Jerod
Weinman.
This work is licensed under a Creative
Commons Attribution-Noncommercial-Share Alike 4.0 International License.