RTL Design #20 · 2026-10-13 · Stacks · Medium · about 15 min

A stack that loses count

stack8 is an eight-entry stack, the kind a branch predictor uses to remember return addresses. top shows the newest entry still held and count the number of entries. Each cycle:

  • push alone puts din on top. It's ignored when the stack is full (count = 8).
  • pop alone removes the top entry. It's ignored when the stack is empty.
  • push and pop together replace the top entry with din: the old top goes, din takes its place, and count doesn't change. This works when the stack is full too. If the stack is empty there's nothing to replace, so din is simply pushed.

top may be anything while the stack is empty. The current design gets pushes and pops right one at a time, but not every cycle is that simple. Fix stack.sv.

Write and run your answer in the browser. The hidden tests check it, and it’s free.