	TITLE "Stack_MEM";

include "stack_mem.inc";
	PARAMETERS (
        WIDTH = 16,
        DEPTH = 8
);
SUBDESIGN Stack_M (
	CLK	: input;
	DI[WIDTH-1..0] : input; 
	DO[WIDTH-1..0] : output;
	CMD[1..0]	: input;
)
VARIABLE
	MEM : stack_mem;
	ADR[8..0]	: DFF;
	ADRi[8..0]	: DFF;
	WEa			: node;
BEGIN
	ADR[].clk = CLK;
	ADRi[].clk = CLK;
	CASE CMD[] IS
		WHEN 0 => ADR[] = ADR[]; WEa = GND; -- nop
		WHEN 1 => ADR[] = ADR[] + 1; WEa = VCC; -- push
		WHEN 2 => ADR[] = ADR[] - 1; WEa = GND; -- pop
		WHEN 3 => ADR[] = ADR[]; WEa = VCC; -- swap
	END CASE;
	ADRi[] = ADR[]+1;
	
	MEM.address_a[8..0] = ADR[];
	MEM.address_b[8..0] = ADRi[];
	MEM.clock = CLK;
	MEM.data_a[15..0] = DI[];
	MEM.data_b[15..0] = DI[];
	MEM.wren_a = WEa;
	MEM.wren_b = GND;

	DO[] = MEM.q_a[];
	
END;