FPGA學習筆記24--用函數實現簡單的處理器

module mpc(instr,out); 

input[17:0] instr;                         //instr爲輸入的指令 


    output[8:0] out;                          //輸出結果 

    reg[8:0] out; 

    reg func; 

    reg[7:0] op1,op2;                         //從指令中提取的兩個操作數 

    function[16:0] code_add;                  //函數的定義 

    input[17:0] instr; 

    reg add_func; 

    reg[7:0] code,opr1,opr2; 

        begin 

        code=instr[17:16];                    //輸入指令 instr的高2位是操作碼 

        opr1=instr[7:0];                      //輸入指令 instr的低 8位是操作數 opr1 

        case(code) 

         2'b00: 

            begin 

             add_func=1; 

             opr2=instr[15:8];                //從 instr中取第二個操作數 

             end 

         2'b01: 

            begin 

              add_func=0; 

              opr2=instr[15:8];               //從 instr中取第二個操作數 

            end 

         2'b10: 

            begin 

              add_func=1; 

              opr2=8'd1;                      //第二個操作數取爲 1,實現+1操作 

             end 

         default: 

            begin 

              add_func=0; 

              opr2=8'd1;                      //實現-1操作 

             end 

         endcase 

        code_add={add_func,opr2,opr1}; 

      end 

     endfunction 

    always @(instr) 

        begin 


      {func,op2,op1}=code_add(instr);          //調用函數

      if(func==1)  out=op1+op2;                //實現兩數相加、操作數 1加 1操作 

     else          out=op1-op2;                //實現兩數相減、操作數 1減 1操作 

     end 

endmodule 


`timescale 10ns/1ns 

module mpc_tp(); 

reg[17:0] instr; 

wire[8:0] out; 

parameter DELY=10; 

mpc m1(instr,out);                             //調用待測試模塊 

initial begin 

       instr=18'd0; 

#DELY instr=18'b00_01001101_00101111; 

#DELY instr=18'b00_11001101_11101111; 

#DELY instr=18'b01_01001101_11101111; 

#DELY instr=18'b01_01001101_00101111; 

#DELY instr=18'b10_01001101_00101111; 

#DELY instr=18'b11_01001101_00101111; 

#DELY instr=18'b00_01001101_00101111; 

#DELY $finish; 

end 

initial $monitor($time,,,"instr=%b out=%b",instr,out); 

endmodule 

在這裏插入圖片描述在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章