# Pages Yixuan Qiu’s Home CS232 Yixuan's CS232 Project 6

### 1. Summary

In this project, I made use of RAM to implement a stack-based four function calculator. It uses a stack to hold the operands of an operation. The user first enters and pushes the operands, then selects an operation to execute that makes use of the top two operands on the stack.

Testing my stacker: State Machine Diagram: At state "000," if Button 2 is presses, then the current value from the switches will be written to the MBR. Then Button 3 will push the MBR onto the stack. Button 4 will execute the operation specified by the two operation switches, using the MBR and the first value on the stack, putting the result back in the MBR.

Testing using the board:

4 + 5 = 9

I first set the value switches to the binary value for 4, and then clicked the Capture button (Button 2) to move 4 into the MBR. Next, I clicked the Enter button to push 4 (Button 3) onto the stack. Secondly, I set the value switches to the binary value for 5 and then clicked the Capture button to move 5 into the MBR. Thirdly, I set the operation switches to 00 for ADD and clicked the Action button (Button 4) to add the MBR to the top value of the stack. The result, 9, ended up in the MBR and on the display.

3 - 1 = 2

subtraction.mp4

2 * 3 = 6

multiplication.mp4

8 / 4 = 2

division.mp4

### 3. Extensions

#### 1) Put in checks so your stack will never go over or under value.

In my calculator, I checked the lower bound when state is "000" and the upper bound when state is "001".

Since the stacker pointer is incremented or decremented by 1 at a time, I simply checked the condition "is not."  #### 2) Expand the number of arithmetic operations.

I added 4 more operations mod, rem, shift left, and shift right in my moreOpCalc by expanding op to 3 bits.

op codeoperation
000

001subtract
010multiply
011divide
100mod
101rem
110shift left by 1
111shift right by 1

Testing using the board:

8 mod 3 = 2

mod.mp4

4 rem 3 = 1

rem.mp4

2 sll 1 = 4 sll 1 = 8 sll 1 = 0x10 (dec 16) sll 1 = 0x20 (dec 32)

Shifting left by 1 is equivalent to multiplying by 2.

sll.mp4

0x10 (dec 16) srl 1 = 8 srl 1 = 4 srl 1 = 2 srl 1 = 1

Shifting right by 1 is equivalent to dividing by 2.

srl.mp4

#### 3) Write a test bench program in VHDL that you can use to test your circuit with GHDL.

I wrote a test file calcbench.vhd.

In order for it to work properly, I slightly modified my calculator to get rid of temp and limit multiplication to the low four bits of the two operands.

I tested the following operations:

2 + 3 = 5

10 - 3 = 7

2 * 4 = 8

7 / 2 = 3 #### 4) Expand the size of the data to 12-bit registers.

In my largerCalc, I modified data to be 6-bit so that it could be reused to represent the low 6 bits and high 6 bits of a 12-bit operand.

I also added a new state "011" to my state machine.

At state "000," if button 2 is hit, then data will be stored into the low 6 bits of mbr and state will go to 011.

At state "011," if button 3 is hit, then the new data will be stored into the high 6 bits of mbr and state will go to 111. Below is an example:

binary:  000001000001 + 000010000010 = 000011000011

decimal:  65 + 130 = 195

hex:  41 + 82 = C3

Testing on the board:

largerCalc.mp4

I first used button 2 to capture the low 6 bits of the first operand--000001, then I used button 3 to capture the high 6 bits of the first operand–000001. Next, I pushed the first operand onto the stack by pressing button 3.

Then I entered the second operand in the same fashion.

The result of addition--C3--was shown after I pressed button 4.

Bruce

Di Luo

Labels