Ủng hộ tôi
Ngăn xếp (Stack) là một thành phần phần cứng quan trọng được sử dụng để lưu trữ dữ liệu tạm thời. Ngăn xếp hoạt động tuân theo nguyên tắc mục dữ liệu cuối cùng được nhập (được ghi) vào Ngăn xếp là mục dữ liệu đầu tiên được xóa (đọc) khỏi nó, hay còn gọi là LIFO (Last-In-First-Out).
Dưới đây là ví dụ về Ngăn xếp LIFO đồng bộ đơn giản được mô tẳ bằng Verilog HDL:
module StackLIFO (
input wire clk, // Clock signal
input wire reset, // Synchronus reset signal
input wire push_en, // Push enable signal
input wire pop_en, // Pop enable signal
input wire [31 : 0] data_in, // Input data
output reg [31 : 0] data_out, // Output data
output reg full, // Full flag
output reg empty // Empty flag
);
parameter DEPTH = 8; // Depth of the LIFO (can be modified as needed)
reg [31 : 0] memory [0 : DEPTH - 1]; // Memory array to store data
reg ptr = 0; // Pointer to top of the stack
// Full and empty flag logic
always @(*) begin
full = (ptr == DEPTH - 1);
empty = (ptr == 0);
end
// Push operation
always @(posedge clk) begin
if (reset) begin
ptr <= 0;
end else if (push_en && !full) begin
memory[ptr] <= data_in;
ptr <= ptr + 1;
end
end
// Pop operation
always @(posedge clk) begin
if (reset) begin
ptr <= 0;
end else if (pop_en && !empty) begin
ptr <= ptr - 1;
data_out <= memory[ptr];
end
end
endmodule