SMOK SLOOP

CSE 378 Project 1

Winter 2001

Due: Monday 2/19/01

Purpose

The purpose of this stage of the project is to get familiar with the SMOK toolkit. We'll give you some exercises that will illustrate the capabilities of the toolkit.

Getting Started

You'll want to start by visiting the SMOK/Sloop homepage. Read the documentation, download the toolkit, and install it on your (or the lab's) PC. Do the tutorial/example on the webpage.

Exercise 0

Suppose we wanted to build a simple machine to multiply two numbers. Our machine might use three registers, called A, B, and Product, respectively. We could use the following algorithm:

while (A != 0) {
  Product = Product + B;
  A = A - 1;
}
You can download an example of a machine that implements this algorithm from here. Download this model, load it into the simulator, and experiment with it. What is the cost of this machine? How many cycles are required to multiply two relatively large numbers (say 0x1234 by 0xABCD)?

Exercise 1

Build a better multiplier machine. Your machine should contain three registers, called A, B, and Product, but will use a more savy algorithm for multiplying numbers. It's called the "Russian Peasant's Algorithm":


while (A != 0) {
  if (A % 2 == 1)              // If A is odd
    Product = Product + B;
  B = B << 1;                  // Multiply B by two
  A = A >> 1;                  // Divide A by two
}
Checking for oddness is easy in a binary representation. Just look at the low order bit. Doubling and halving numbers is easily accomplished by shifting left or right, respectively. What is the cost of this new machine? How many cycles are required to multiply two numbers?

Exercise 2: Return of the HW3 Machine

Remember the HW3 machine? Well, it's back. In case you've blocked it out of your memory, here's a review:

The machine you'll simulate (called the HW3 machine) is a 32-bit machine with an extremely simple architecture. There are only eight instructions, no general purpose registers, and no explicit addressing of memory. Instead, data memory is organized as a stack of 32-bit integers, and all instructions refer to the top of the stack for operands. (There is also a separate memory to hold instructions, addressable only through the HW3 PC.) The HW3 CPU has an internal register, the SP, to keep track of the top of the stack. The user's program can affect the SP only indirectly, by adding to or removing from the stack.

The table below lists the set of instructions available (as well as some other info to be explained shortly). A user of the HW3 machine would write a program using those instructions. (Please note: the opcodes have changed from the original HW3 definition. Once you get started, you'll see why we did this.)

Opcode Operation Example Meaning
0000Stop0x00000000Stop the program
0001Show0x10000000Show the stack top
0010Clear0x20000000Clear the stack
1010Mult0xA0000000Multiply
1011Div0xB0000000Divide
1000Add0x80000000Add
1001Sub0x90000000Subtract
1111Push0xF0000003Push 3 onto the stack

Stop indicates the end of a program. Clear indicates that the stack should be cleared, that is, set to empty. Show requests that the integer at the top of the stack be printed. Push adds a new integer to the top of the stack. Add, Sub, Mult, and Div replace the top two integers on the stack with the single integer result of the operation specified. For example, if the stack contains (from bottom to top) {1 2 3}, a Sub instruction would result in {1 -1}. Notice that only the Push instruction specifies an operand.

HW3 instructions are encoded as 32-bit values. The high order four bits (bits 28 through 31) specify the opcode, as shown in the table. In all instructions other than Push, the low order 28 bits are ignored. In the Push instruction, the low order 28 bits contain a signed integer.

Your machine will implement a fetch-increment-execute loop: it fetches the instruction specified by the PC, increments the PC to point to the next instruction, and then performs the operation given by the instruction just fetched.

Although this machine seems to be too simple to do anything useful, we could turn it into a fully general purpose machine by adding just a few instructions (such as Load/Store/Branch).

Implementing the HW3 Machine in SMOK

Designing the datapath and control is totally up to you, but here are some hints:

Running and Testing

In order to test your design, you'll need some test programs. Generating these is easy. Create a text file with this format:


// Comment line
<starting address in decimal>
<word1 in hex>
<word2 in hex>
...
<wordN in hex>
For example:

// My first program
0
F0000002
F0000003
F0000004
F0000005
A0000000
80000000
90000000
00000000
Pushes four operands onto the stack and then does a MULT, ADD, SUB, and finally a STOP. Double clicking the memory component will allow you to load a file like the above into memory.

Implementation

Here are some more hints to help you along the way:

Error Checking

It is possible for the HW3 machine to encounter error conditions. You should deal with stack overflow (pushing more data onto the stack than it can hold) and underflow (performing more operations than there are elements on the stack to serve as operands for). Your machine should halt and place a value into a status register indicating what happened: 0 means normal termination, 1 means stack underflow, 2 means stack overflow.

Evaluating Your Design

The SMOK system provides you with some important metrics that you should keep in mind. The first is the cycle time of your machine and the second is the "cost", which is a measure of how many transistors your components would consume. The person(s) who get the lowest cycle time and cost will receive a special prize!

Turn-In

Please turn in the following:

This is Too Easy

Check back to the old HW3 extra credit and implement some of those extensions: loading, storing, branching and/or jumping.


dugan@cs.washington.edu