1. Goals for this week:

  1. ALU condition codes (flags) in Lab 3.

  2. Explore real computer hardware.

2. Starting Point Code

Start by creating a week04 in your cs31/WeeklyLabs subdirectory and copying over some files:

$ cd ~/cs31/WeeklyLabs
$ mkdir week04
$ cd week04
$ pwd
/home/you/cs31/WeeklyLabs/week04
$ cp ~sukrit/public/cs31/week04/* ./
$ ls
Makefile conditions.c testing.txt

3. Tips for Lab 3

3.1. ALU Flags

On the ALU lab, you need to set 5 flags (the condition codes). This will be the most complicated circuitry in your ALU.

Recall the flags (or condition codes) are used encode state about the result of an ALU operation. Each flag is a 1-bit value. For the ALU you are building in Logisim, the flags are defined as the following:

  • EF: Is set (1), if the two operands are equal. Otherwise, it is clear (0).

  • ZF: Is set, if the computed result is zero.

  • SF: Is set, if the computed result’s most significant bit is set (i.e., it’s negative if interpreted as signed).

  • CF: Is set, if the computed result caused an overflow, when interpreted as an unsigned operation.

  • OF: Is set, if the computed result caused an overflow, when interpreted as a signed operation.

3.1.1. Tips for where to start

We recommend starting with just the circuit to compute the ZF. Consider what you need to test to set this flag’s value (these are the inputs to your ZF flag circuit, its output is a single bit); does your ZF flag setting circuit need:

  • operand values, or bits from operand values?

  • output value, or bits from output value?

  • instruction bits?

  • other?

Once you have built and tested your ZF flag circuit, then consider the next flag.

Although not required, we recommend creating a circuit for each flag and adding it to your ALU (this will make your ALU circuit easier to read and possibly easier to test). A flag circuit has some number of inputs of possibly different bit widths, and a 1-bit output.

We recommend writing and testing circuits for ZF, SF, and EF first, and then do the CF and OF flags.

3.1.2. Code to test for conditions

You can also get an idea of how to set these by writing some code in C and reasoning about what the values of the flags should be after those operations, considering how the flags should be set.

Look at the conditions.c program:

$ vim conditions.c

This program includes some example arithmetic and bit-wise operations, operations that your ALU will implement. We will use it to reason about the ALU flags part of Lab 3.

Compile conditions.c and run the program and look at some of the example operations and think about what the flag settings should be for these examples.

$ make
$ ./conditions

ADD
---
unsigned 2147483647 + 4 is  2147483651 (0x80000003)
  signed 2147483647 + 4 is -2147483645 (0x80000003)

SUB
---
unsigned 3 - 5 is  4294967294 (0xfffffffe)
  signed 3 - 5 is          -2 (0xfffffffe)
...

The first couple examples are ADD and SUB operations. Think about the value of each of the 5 flags for each of these example operations. In other words, which flags should be set (1) and which should be clear (0) as the result of adding 2147483647 + 4? And which should be set and which should be clear as the result of 3 - 5?

Next, consider the OR and AND examples, and how the flags should be set for these examples.

When implementing the flags in your ALU, you can return to this program and try different examples to help you think about how the flags should be set.

3.2. Testing your ALU

Take a look at testing.txt and for the first two cases, manually calculate what the result and flags should be. Then come up with some additional test cases for different operations and flags.

Once you’ve built your ALU, you can come back to this file and test with it.

3.3. Logisim Tips

  • You can often change the orientation of circuits in the circuit menu and sometimes some pin locations for easier wiring.

  • If connections seem to not be working properly check if a wire is shorting pins by being connected along an edge of a component. Move the component and look underneath it — if all looks good you can hit Undo and put the component back. If things look bad, try deleting the component, fix the wiring and then re-place the component.

  • Wires can occasionally pass underneath components and create invisible connections. As above, move the component to check for this if you experience problems.

4. Computer Teardown

Log out. Form a group of 4 people (including yourself). Your group is assigned a single computer that you can take apart.

The goal is to find as many parts of a computer as you can. We have tools available to remove parts from your computer, and here are a few links that may be helpful:

Try to identify some of the following:

  • the processor

  • RAM memory card(s)

  • the backplane

  • the chipset, PCI and Memory buses

  • expansion slots

  • the hard drive (what’s inside?)

  • the network interface card (NIC)

  • the graphics card

  • where do different ports go?

  • …​ then see what else you can find

Before leaving lab, please clean-up all spare parts, screws, etc. that you have removed by putting them in the boxes. You do not need to put the cases in the boxes, just all loose parts. Cases can be left on the desks. CPU thermal glue is toxic, so just to be safe I recommend washing your hands after lab today.

5. Handy References