Codiode

Simple LIFO Stack

MediumVerilog / SystemVerilogBuild

Call stacks and parsers need the last item in to be the first item out. A stack is a memory plus one pointer that tracks the fill level.

The module maintains an internal memory array of depth 2^ADDR_WIDTH. A stack pointer tracks the number of elements currently stored. On a valid push operation, the incoming data is written to the memory at the current pointer location, and the pointer increments. On a valid pop operation, the pointer decrements, and the data at the new pointer location is driven to the output register.

Timing and reset rules:

  • Clock edge: clk is positive edge triggered.
  • Reset type: rst_n is an asynchronous active low reset.
  • Reset behavior: On reset, the internal pointer is cleared to 0, empty is 1, full is 0, and data_out is 0.
  • Priority rules: If both push and pop are asserted on the same clock cycle, the operation is ignored and the state remains unchanged.
  • Boundary rules: A push when the stack is full is ignored. A pop when the stack is empty is ignored.
  • Output behavior: data_out is a registered output. It updates only on a valid pop operation. On all other cycles, including pushes and ignored operations, it holds its previous value.

Cycle by cycle trace:
Cycle 1: rst_n=0 → empty=1, full=0, data_out=0
Cycle 2: rst_n=1, push=1, pop=0, data_in=10 → empty=0, full=0, data_out=0
Cycle 3: push=1, pop=0, data_in=20 → empty=0, full=0, data_out=0
Cycle 4: push=0, pop=1 → empty=0, full=0, data_out=20
Cycle 5: push=0, pop=1 → empty=1, full=0, data_out=10
Cycle 6: push=0, pop=1 → empty=1, full=0, data_out=10 (underflow ignored)

Parameter Default Description
DATA_WIDTH 8 Bit width of the data bus
ADDR_WIDTH 3 Bit width of the memory address; stack depth is 2^ADDR_WIDTH
Signal Direction Width Description
clk input 1 Positive edge triggered clock
rst_n input 1 Asynchronous active low reset
push input 1 Push enable; writes data_in to the stack
pop input 1 Pop enable; reads the top of the stack into data_out
data_in input DATA_WIDTH Data to be pushed onto the stack
data_out output DATA_WIDTH Data popped from the stack
full output 1 Asserted when the stack reaches its maximum capacity
empty output 1 Asserted when the stack contains zero elements

Constraints

  • The pointer must have enough bits to represent the maximum capacity without wrapping to zero.
  • Pushing to a full stack must not overwrite existing data or increment the pointer.
  • Popping from an empty stack must not corrupt the pointer or update data_out.
  • Asserting both push and pop simultaneously results in no operation.

Topics

SequentialMemoryLIFOStack

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of Memory and Storage

Problem 22 of 26 in the Memory and Storage track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.