Knob & Switch Computer

Overview

In this lab, you will explore a complete CPU architecture. Specifically, you will look at how an instruction is processed along the CPU datapath, how values from memory are incorporated into CPU instructions, and the fetch-execute cycle.

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.

Part 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.

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.

  1. 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.
  2. 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?
  3. 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.
  4. Find the ALU. What operation is the ALU currently configured to perform?
  5. Find the C bus and the C bus address. Where do you think the result will be stored?
  6. Click the Execute button and watch the data flow through the datapath. Is the result what you expected?
  7. Set R1=42 and R2=17 again then “program” the simulator to compute R3=R1-R2 and execute your simulation.

Part 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.)

  1. 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.)

  2. Set Mem[1]=42. That is, put the value 42 into memory location 1.

  3. Select Mem[1] as the RW location.

  4. 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?

  5. Click the Execute button and watch the data flow through the datapath. Is the result what you expected? You should notice two things:

    If you did not see both those things, set Mem[1]=43 and click Execute again.

  6. How can we write data to memory? By closing the connection from the C bus to the main memory bus, as follows:

    1. Open (disconnect) the switch from the memory to the C bus so that nothing is read from memory, then close (connect) the switch from the C bus to the memory so that a result can be written to memory.
    2. Connect the ALU to the C bus by closing the switch that connects them.
    3. Open the switch that connects the C bus to the register bank.
    4. Set R0=8 and R1=9.
    5. 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.

  7. 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?

  8. 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?

Part 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.

  1. 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.
  2. 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.
  3. 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.
  4. Click Execute to run your program. Notice that the simulator changes the datapath settings according to what is stored in the microprogram memory.
  5. 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.
  6. Test your program. Before you click Execute, you may want to set the Animation Speed to Fast.

Part D: Fetch-Execute Cycle

In this section, we add the ability to store machine language instructions in memory.

  1. Open the Complete K&S Computer Simulator.

  2. Study the control unit. Find the instruction register (IR), the program counter (PC), and the micro-instruction register.

  3. In Mem[0], type this assembly instruction: add r1 r2 r3. (That is, you literally type those ASCII assembly characters, not binary machine code or micro-instruction fields.)

  4. Let’s compare machine and asssembly versions of this instruction:

    1. 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.
    2. Change the view back to Inst so you can see the instruction in (human) assembly language.
  5. Enter the HALT instruction in Mem[1].

  6. Put some more interesting (i.e., non-zero) numbers in the registers, set the animation speed to Fast, and execute the program.

  7. Open the reference card for the K&S ISA and find the instructions for accessing memory.

    1. 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.
    2. 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.
  8. Referring to the K&S ISA reference, is the K&S ISA branching more like MIPS or ARM?

  9. Thought question: What do you think the following program will do?

    0: SUB    R0 R0 R0  ; Mem[0]
    1: LOAD   R1 5      ; Mem[1]
    2: ADD    R0 R0 R1  ; Mem[2]
    3: BRANCH 2         ; Mem[3]
    4: HALT             ; Mem[4]
    5: 1                ; Mem[5]

    The last line is not a typo: Mem[5] should contain the value 1.

  10. Make it real:

  1. Enter this program in main memory (starting at memory location 0).
  2. Reset the PC then Execute the program.
  3. As you execute the program, keep an eye on the PC.

Part E: For those with extra time

Extra 1: Multiply by 4

Write a sequence of assembly instructions to multiply the value at memory address 11 by 4.

For example, if the contents of memory address 11 was the number 7, your instructions should store the number 28 there. While the ALU has no multiplication setting on its knob, multiplication can be accomplished via repeated addition: \(7\times4 = 7 + 7 + 7 + 7\).

Type your program into the simulator and execute it to verify its correctness.

Extra 2: Branching

The K&S ISA supports the instruction BZERO MMMMM, which means, if the current ALU result is zero, set the program counter to the memory address MMMMM. Use this instruction to write a K&S program that uses a loop to multiply R0 and R1, placing the result in R2. Your program may change the value of R0 or R1 if necessary.

Notes:


Copyright © 2018–2026 Charlie Curtsinger and Jerod Weinman

CC-BY-NC-SA This work is licensed under a Creative Commons Attribution NonCommercial ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.