In this week’s lab, you’ll be making a very simple “game” where you have to “catch an LED” (that is, press a button at a specific time to win). There is a long tradition of making tiny, simple games out of digital logic components, and we’re continuing that tradition!

The goal of this lab is to get some experience with the clock and using a register, including getting a feel for “when” the register’s value is updated, how to prevent the register from changing, and using the output of the register to make something happen over time.

This is a finite state machine, and it will have some of the “intelligence” of a program, but without the ability to actually read and execute instructions. All the logic will instead be hardwired.


1. The clock

There will be a register in this lab, and registers need clocks. First, build this thing shown to the right, which makes it a little easier to interact with sequential circuits and will be used in all the labs and the project in the future. It is composed of:

The CLK tunnel will be used to connect the clock signal to every register in your circuit. You will do that by copying and pasting the CLK tunnel wherever you need it. This circuit will only have one register, but in the future you will have more.

This funny arrangement gives us flexibility for how to tick the clock. There are three ways:


Components (subcircuits)

A component or subcircuit is kind of like a function: you have inputs (“parameters”) and outputs (“return values”). You must use components in your second project, as they will be very helpful in organizing the many parts of your CPU. For this lab, the component will make your main circuit much cleaner, and will also make your lab easy to automatically grade.

Components in Logisim have some neat features:


2. Creating the game component

  1. Do Project > Add Circuit… from the menu bar in Logisim.
  2. When it asks for a name, call it THE GAME. All capitals. please do this to make autograding possible.
  3. When you hit OK, the main circuit disappears, and you are now editing the new circuit. You can tell because on the left side, you’ll see this: To switch which one to edit, just double click their names there.

Make sure you are editing THE GAME, and:

  1. Create 2 1-bit inputs and 2 1-bit outputs and arrange them like this.
  2. Now switch to editing the main circuit. On the left side, single-click THE GAME, then place it on the main circuit. You should see this:
  3. Place your mouse cursor over one of the blue dots on the sides of the component you just placed, and let it sit there for a second or two. A message should pop up like this:
    • If it doesn’t show you that message: are you sure you’re using the newest version of Logisim? If you’re using 2211_1019, you need to update.

See, each input and output has a name, set by its Label property. Inputs and outputs are how we get values into and out of components, and it’s very important that you name your inputs and outputs properly so that you can tell which is which.

To fix this:

  1. Switch back to editing THE GAME.
  2. Change the Label properties of each input and output to match this image.
    • It’s very important that the top input be CLK and the bottom be Stop; and that the top output be Win and the bottom output be Lose. This is for autograding purposes.
  3. Once you’ve done that, go back to main and hover over the dots on the sides again. You should see the input and output names that you just set!

Cool, now we just have one more set of outputs to add:

  1. Switch back to editing THE GAME.
  2. Create a new output at the top of your circuit.
    • While it’s selected, use the down arrow on your keyboard to make it face down (or change its “Facing” property to “South”)
    • Set its “Label” to 0
    • Set its “Label Location” to “North”
    • You should now have this:
  3. Now duplicate that 8 times and place/rename them to match this image:
  4. If you now go back to main, you’ll see this:
    • W I D E component

Notice that the direction that the inputs/outputs are facing determines on which side they will appear when you place the component! The 0 through 8 outputs are on top because they are facing South inside THE GAME.

It’s very important that your component look exactly like this for autograding purposes. Please make sure it matches the image above!


3. The “Light chaser” outputs

Time to build a classic bit of electronic fun, a “chaser” or “sequencer.” These have been used in automated signs, displays, and holiday lights for decades.

Inside THE GAME, you’re going to build this (instructions after the image):

Here’s what you’ll use:

So what the heck does a decoder do? Play around with the input and see. This one takes a 4-bit number as its input, and turns on exactly one output, based on that number. If you input 0000, output 0 turns on. If you input 0001, output 1 turns on. And so forth. (Decoders will come back up in the project!)

What about the rest of the decoder’s outputs? We’re just not using them for this lab. We’ll never put a value > 8 into it, so it won’t matter.

Once you’re satisfied everything is working, delete the input, because it was just for testing and we are about to replace it with something much cooler.


4. Making a register count up

You are going to use a register to keep track of a number. That number will increase by 1 every tick of the clock, and that number will be fed into the decoder.

Here’s how you’ll make it:

  1. Go to the materials page, download the simple_counter.circ example, and look at how it works.
    • (You have to use Logisim’s File > Open menu item to open it.)
    • It’s a register whose Q output feeds into an adder, whose other input is a constant 1.
    • The output of the adder feeds back into the same register.
    • The clock is connected to the register’s clock input (the triangle).
    • When you tick the clock, the value in the register is incremented by 1.
  2. Don’t copy and paste that into your circuit. Get some practice making your own version with these components:
    • a Memory > Register, with its “Data Bits” set to 4
    • an Arithmetic > Adder, with its “Data Bits” set to 4
    • a Wiring > Constant, with its “Data Bits” set to 4 and its value to 0x1
      • tip: click on the constant in the circuit, and then just type a number on your keyboard.
  3. Then:
    • Go to the main circuit
    • Copy the CLK tunnel that you created earlier
    • Go back to THE GAME
    • Paste that tunnel and connect it to the register

When all is said and done, you should have something like this:

Now, to tick the clock, hit Ctrl+T/⌘T.

Nothing happens.

Uh.

What?

Tunnels can’t cross circuits!

Tunnels are like local variables. Every circuit gets its own set of tunnels. The CLK tunnel on the main circuit is a different tunnel from the CLK tunnel in THE GAME.

You cannot use tunnels to pass values into and out of components. The only way to do that is with inputs and outputs. That’s why we made the CLK input earlier!

So now, duplicate the CLK tunnel and attach it to the CLK input you made earlier, like this:

Now, to test it, use the hand tool to poke the CLK input to toggle it between 0 and 1 over and over. Every full clock cycle, the value in the register should increment by 1. It will go 0, 1, 2, 3, 4, 5 …. c, d, e, f, then 0 again. If it does, great! Your register is working! If not, get help!


Resetting

You can also do this with Simulate > Reset Simulation, but resetting is so common that you should just get used to using the keyboard to do it.

Try poking the CLK input a few times so that the register contains some non-zero value. Now, hit Ctrl+R/⌘R. This resets everything in the circuit. Resetting:

Resetting sometimes also fixes minor weird problems with Logisim. If you ever get wires behaving strangely, or values not moving through wires correctly, try resetting. It might work. (lol.)

Get used to resetting! It’s a very useful thing when testing your circuits. Think of it like running a program from the beginning, instead of starting somewhere in the middle. Resetting also puts the circuit into the same state as when it is first opened in Logisim, and is therefore how the graders will see your circuit.


5. Connecting the register to the decoder

Make a tunnel connected to the Q output of the register, called count or something.

Don’t use the output of the adder. Use the Q output of the register.

Now, you’re going to make some logic that comes up with the value that goes into the decoder’s select input. It’s going to work like this:

if(count < 9)
    decoderSelect = count;
else
    decoderSelect = 0 - count;

To make this you’ll need:

Go for it. The output of this circuit will go to the decoder’s select input.

What does all of this logic do? It makes the decoder see the sequence 0, 1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1. That will make the decoder’s outputs turn on from left to right, then right to left.

To test this,


6. Hooking it up on the main circuit

I bet your finger is tired of clicking. Let’s make it automatic.

Now:

Finally, you can make it go automatically like so:

And watch it go! This is pretty much exactly how some christmas lights, decorative lights, “neon” signs etc. work.

You can Simulate > Ticks Enabled again to make it stop.

But this isn’t a game yet. Games have inputs and outputs and rules!


7. Making it into a game

Here is how the game will work:

I know, it’s not exactly the MIPS game project…

First, let’s set up the user interface for the game. The LED chaser you just made is part of that interface, but we need a few more things - an Input/Output > Button and two more LEDs, with some tunnels attached to them. See the below image for what I mean and follow my example.

7.1 Stopping the register from counting

The button you just made is the “stop” button. When you press it, two things should happen:

Let’s do the “stop counting” part first.

Go back into THE GAME and look at the register. Right now you are using the D input on the left, the clock input on the bottom, and the Q output on the right. But you are not using the en input on the left. Remember that this is the write enable for the register. Because we have nothing connected, it is always enabled and will change on every clock tick.

What we want is the following: if stop is 0, enable the register (input a 1 for its write enable). If stop is 1, disable the register (input a 0 for its write enable). The logic for this is a single gate, the simplest gate. Please get this.

To test it:

7.2 Deciding if you won or lost

The goal of the game is to hit the stop button when the middle LED (LED 4) is lit up. That’s why I suggested you change its “On color” property!

Back in THE GAME:

Now you can play the game.

If the win and/or lose LEDs light up even when you are not pressing stop, then your logic for the win and lose outputs in THE GAME are wrong.

Try changing the Simulate > Tick Frequency to change the difficulty of the game. (I feel like 32Hz is about the fastest you can go and still have better-than-luck chance of getting it lol… at 64Hz the screen can’t keep up)


Submitting

Once you’re sure your circuit works, you can submit.

To submit:

  1. On Canvas, go to “Assignments” and click this lab.
  2. Click “Start Assignment.”
  3. Under “File Upload,” click the “Browse” button and choose your .circ file.
  4. Click “Submit Assignment.”

If you need to resubmit, that’s fine, just click “New Attempt” on the assignment page and upload it again.