Project 6: Stack-based Calculator
Due Monday 13 April 2026
Purpose: The purpose of this project is to make use of RAM to implement a stack-based four-function calculator. The concepts of stacks and the use of memory will lead into the next project where you design a complete CPU.
Overview
A stack-based calculator 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.
If you imagine a calculator and its display, we're going to use a register [MBR] to represent the display, which is also the currently active number. We'll also use the MBR as the top of the stack for the purpose of operations.
To control our calculator, we're going to use three buttons to execute actions, two switches to select the mathematical operation (+, -, *, /), and eight switches to specify values. Button 0 will be the Capture button and will write the current value from the switches to the MBR. Button 1 will be the Enter button and will push the MBR onto the stack. Button 2 will be the Action button and 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.
As an example, consider that we want to add 4 plus 5. We would do this in the following steps.
- Set the value switches to the binary value for 4.
- Click the Capture button to move 4 into the MBR. A 4 should now show up on the 7-segment display.
- Click the Enter button to push 4 onto the stack.
- Set the value switches to the binary value for 5.
- Click the Capture button to move 5 into the MBR. A 5 should now show up on the 7-segment display.
- Set the operation switches to 00 for ADD.
- Click the Action button to add the MBR to the top value of the stack. The result, 9, should end up in the MBR and on the display.
The value of the MBR will always be echoed in the two seven segment displays.
Tasks
- Setup
Make a new folder for Project 6 (You can copy your lab stacker.vhd into this folder as part of your submission). If you are going to use a 7-segment display to show the contents of memory, then copy over your VHDL 7-segment display driver in to the new folder. I'll refer to the project and top-level circuit as calculator.
- Create a 16 x 8 bit RAM
Use the Tools:MegaWizard Plug-in Manager to create a RAM that has 16 words of 8 bits each (call the file memram.vhd). This memory is larger than the memory from lab - we have 4 address bits and 8 data bits.
- Define the calculator entity
Create a top-level VHDL file with an entity that has the same name as the project (e.g. calculator). You will need the following input and output signals for the entity's port statement.
clock: in std_logic clock signal b0: in std_logic button 0, Capture input b1: in std_logic button 1, Enter b2: in std_logic button 2, Action op: in std_logic_vector(1 downto 0) Action switches (2) data: in std_logic_vector(7 downto 0) Input data switches (8) digit0: out std_logic_vector(6 downto 0) Output values for 7-segment display digit1: out std_logic_vector(6 downto 0) Output values for 7-segment display - Construct the RAM component statement
Create a component statement for the RAM in the architecture header. Be sure to include the VHDL memram file created by the Wizard in your project.
- Define the internal signals
Create the internal signals you will need to build the state machine and access the RAM. You will need signals for the RAM input, RAM write enable, RAM output, stack pointer, memory buffer register, and state.
- Design and implement the calculator state machine
Plan out a state machine that will implement the calculator. You can use the lab circuit as a model, being sure to include a reset state (follow the lab model of having the user press b1 and b2 together to enter the reset condition). Include your state machine diagram as part of your report.
Note: multiplication requires a destination that has twice as many bits as the operands. When executing multiplication you can either assign the result to a 16-bit temporary register and then truncate it to 8-bits or you can limit multiplication to the low four bits of the two operands.
Implement your state machine.
- Port map the RAM and 7-segment display circuits
Port map the RAM and the seven-segment displays. The seven-segment circuits should take in the low 4 and high 4 bits of the MBR, and their outputs should connect to the digit0 and digit1 output signals. Connect the stack pointer to the stackview output signal of your entity.
If you built your 7-segment display as a BDF diagram, use the "create/update" option and create the VHDL file for your 7-segement display circuit, which you can then port map into your main circuit.
- Demonstrate your circuit works for all 4 operations (on either the board or with a Python movie)
- If you demonstrate on the board, assign pins and test your circuit on the board. Take some videos of it in action for all of the different operations.
- If you demonstrate with simulation, write a VHDL testbench, run the simulation, then include an image of your waeform in your report, along with an explanation of how you can tell it is correct..
Regardless of which demonstration you choose to include, also include a description of your testing strategy in your report . This means you should include both the mathematical expression you are evaluating (e.g. 2+(6-(4-0)) written in infix notation or 2 (6 (4 0 -) -) + written in postfix notation) AND the set of operations to complete the calculations, e.g.
-- operations: -- mbr = 2 -- push MBR -- mbr = 6 -- push MBR -- mbr = 4 -- push MBR -- mbr = 0 -- pop 4, and MBR = 4-MBR = 4-0 = 4 -- pop 6, and MBR = 6-MBR = 6-4 = 2 -- pop 2, and MBR = 2+MBR = 2+2 = 4
Extensions
- Expand the number of arithmetic operations.
- Put in checks so your stack will never go over or under value.
- Try fixed-point math.
- Expand the size of the data to 12-bit registers. If you go to 16 bits, it will require more care when entering data.
Report
Create a Word/Pages/etc document for your report.
- Demonstrate that you got your Lab 6 stacker working by including the GHDL output in your report and its VHDL program in your submission folder.
- Provide a description of your top-level design for Project 6 calculator. This should include your state machine diagram and a brief description of the state machine (aka how does your calculator work).
- Describe the testing for all 4 operations. The description should include the mathematical expression you are evaluating AND the set of operations to complete the calculations.
- Include a video of your testing described above.
- Indicate all your extensions. For each extension, include a description of the extension, how you achieve it, and testing results(movies or gtkwave) demonstrating it works.
- Include an Acknowledgements section in which you list any resources you consulted. These could be on-line (movies or web pages) or people (peers, TA's, professors). This is your opportunity to celebrate the community you were able to plug into.
Please note that all these report requirements are reflected on your project 6 feedback spreadsheet. Remember to include all required elements in your report and grant access to "Anyone with the link" if you provide video links.
Handin
Put your vhd and write-up files in a folder called Project6_<username>, zip it up (so its name will be Project6_<username>), and upload the zip file to the Google classroom assignment for this project.
Spring 2026