VLSI 程式設計

設計一個堆疊結構,包含:push, pop, peep等功能

 

*
*    CLK     : 表示clock
*    OPCODE  : 表示要做何種處理
*               0: reset
*               1: isFull
*               2: isEmpty
*               3: push
*               4: pop
*               5: peep
*    INPUT   : 8 bits data input
*    OUTPUT  : 8 bits data output
*               OPCODE = 1,2,3  ==>  OUTPUT = 0b00000000
*               OPCODE = 4,5    ==>  OUTPUT = 0b00000000 ~ 0b11111111
*    STATUS  : 2 bits
*             0: action success
*             1: isFull or isEmpty
*             2: overflow 
*             3: underflow
*     
*/
module stack(CLK, OPCODE, INPUT, OUTPUT, STATUS);
    parameter SIZE = 4;              //stack size default=4
    input  CLK, OPCODE, INPUT;
    output OUTPUT, STATUS;

    reg [7:0] STACK_ITEM [SIZE:0];     //定義堆疊資料的儲存空間
    integer TOP;                       //定義指向堆疊頂端的位置
    wire [7:0] INPUT;                  //定義輸入資料
    wire [3:0] OPCODE;                 //定義控制指令
    reg [7:0] OUTPUT;                  //定義出出資料
    reg [2:0] STATUS;                  //定義狀態輸出
    reg i;                             //計數用

    always @ (posedge CLK)
    begin
        case (OPCODE)
            3'd0:                      //清除Stack中的所有資料,做出始化動作   
                TOP = -1;
            3'd1:                      //判斷Stack是否已經滿了    
                begin
                    OUTPUT = 8'd0;     //將OUTPUT資料設為0
                    if(TOP >= SIZE-1)  //如果TOP的值大於等於SIZE-1表示Stack已經滿了或overflow   
                        STATUS = 2'd1;      
                    else               //否則Stack還沒滿
                        STATUS = 2'd0;
                end
            3'd2:                      //判斷Stack是否為空
                begin
                    OUTPUT = 8'd0;     //將OUTPUT資料設為0
                    if(TOP <= 0)       //如果TOP的值小於等於0表示Stack已經滿了或underflow
                        STATUS = 2'd1;
                    else               //否則Stack中有資料
                        STATUS = 2'd0;
                end
            3'd3:                      //push data into Stack               
                begin
                    OUTPUT = 8'd0;
                    TOP = TOP + 1;                   
                    if(TOP == SIZE)    //如果TOP==SIZE表示已經滿了
                        begin
                            STATUS = 2'd2;
                            TOP = TOP - 1;
                        end
                    else
                        begin
                            STATUS = 2'd0;
                            for(i=0; i<=7; i=i+1)
                                STACK_ITEM[TOP][i] <= INPUT[i];
                        end
                end
            3'd4:
                if(TOP == -1)          //如果TOP==-1表示Stack已經空了
                    STATUS = 2'd3;
                else
                    begin
                        for(i=0; i<7; i=i+1)
                            OUTPUT[i] <= STACK_ITEM[TOP][i];
                        TOP = TOP - 1;
                    end
            3'd5:
                if(TOP == -1)          //如果TOP==-1表示Stack已經空了
                    STATUS = 2'd3;
                else                   
                    for(i=0; i<7; i=i+1)
                        OUTPUT[i] <= STACK_ITEM[TOP][i];                                                  
        endcase
    end

endmodule

 

 

創作者介紹
創作者 CJY0503 的頭像
CJY0503

程式設計者天堂

CJY0503 發表在 痞客邦 留言(0) 人氣()