# Initial Exercise: ALU

Let's start with some initial exercises!&#x20;

1. Complete these two HDLBits exercises, if you have not already

{% embed url="<https://hdlbits.01xz.net/wiki/Always_case>" %}

{% embed url="<https://hdlbits.01xz.net/wiki/Module_add>" %}

1. Read these ChipVerify pages (it will be helpful for the design!)

{% embed url="<https://www.chipverify.com/verilog/verilog-case-statement>" %}

{% embed url="<https://www.chipverify.com/verilog/verilog-4to1-mux>" %}

## ALU

The ALU, or the arithmetic logic unit, is one of the core components of any processor design. It has 3 simple inputs:

* `a`: operand 1, 32 bits wide
* `b`: operand 2. 32 bits wide,&#x20;
* `ALUSel`: selector signal, 4 bits wide

There's also one output:

* `out` output, 32 bits wide

At a high level, what the ALU does is based on the `ALUSel` signal, it will choose the proper operation to perform on the operands `a` and `b`. Our design follows the RV32 specification and should support the same operations, listed below.

Some helpful links:

RISC-V Green Card

{% embed url="<https://inst.eecs.berkeley.edu/~cs61c/su22/pdfs/resources/reference-card.pdf>" %}

Signed vs unsigned representations

{% embed url="<https://www.electronics-lab.com/article/signed-binary-numbers/>" %}

Here's a quick table what some of the `ALUSel` do. For the sake of this assignment, just do the ones that are listed here. The `-` incidates operations that we will not support at the moment.&#x20;

| ALUSel | Operation | Explanation                    |
| ------ | --------- | ------------------------------ |
| 0      | ADD       | Adds the operands              |
| 1      | SLL       | logical left shifts `a` by `b` |
| 2      | SLT       | Signed less than comparison    |
| 3      | SLTU      | Unsigned less than             |
| 4      | XOR       | XORs the operands              |
| 5      | SRL       | logical right shift            |
| 6      | OR        | logical OR                     |
| 7      | AND       | logical AND                    |
| 8      | -         | -                              |
| 9      | -         | -                              |
| 10     | -         | -                              |
| 11     | -         | -                              |
| 12     | SUB       | subtract the operands          |
| 13     | SRA       | arithmetic right shift         |
| 14     |           |                                |
| 15     | BSEL      | set the result to `b`          |

{% hint style="info" %}
Your task: Please write a Verilog module that accomplishes the above operations. These actions should be performed irrespective of clock.&#x20;
{% endhint %}

You may choose to write this in either EDAPlayground or the editor in HDLBits, though the EDA Playground editor is much more robust. We also expect you to be able to test this through writing a testbench.&#x20;

## Testing the ALU

When testing Verilog, you typically write something called a *testbench*. Testbenches simulate the Verilog code that you wrote and checks if certain waveforms are what you expect at each clock tick. Here are some links to read about this:

{% embed url="<https://www.chipverify.com/verilog/verilog-testbench-simulation>" %}

{% embed url="<https://www.chipverify.com/verilog/verilog-testbench>" %}

Your next task is to create a testbench for the ALU you just made. Make sure it tests all the possible cases in the module!
