Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `define NO_OP 2'b00
  2. `define PUSH1 2'b01
  3. `define POP   2'b10
  4.  
  5. `define PUSH2 3'b000
  6. `define ADD   3'b001
  7. `define SUB   3'b010
  8. `define MUL   3'b011
  9. `define COM   3'b100
  10. `define PUSHANS 3'b101
  11. `define READCOM 3'b110
  12. `define INIT  3'b111
  13.  
  14.  
  15.  
  16. module DFF (clk, next_out, out);
  17.   parameter n = 1;
  18.   input clk;
  19.   input [n-1:0] next_out;
  20.   output reg [n-1:0] out;
  21.   always@(posedge clk) begin
  22.       out <= next_out;
  23.   end
  24. endmodule
  25.  
  26.  
  27. module SM(clk, rst_n, instr, pc, d_valid, out_data, err_code, fin);
  28.     input clk;
  29.     input rst_n;
  30.     input [12:0] instr;
  31.     output [9:0] pc;
  32.     output d_valid;
  33.     output signed [19:0] out_data;
  34.     output reg [2:0] err_code;
  35.     output fin;
  36.     wire [9:0]  next_pc;
  37.     wire [2:0]  com,com_next;
  38.     wire signed [19:0] w_data, r_data;
  39.     wire [1:0]  cntrl;
  40.     wire full,empty;
  41.     reg  [9:0]  next_pc1;
  42.     reg  [2:0]  com_next1,last;
  43.     reg  [12:0] num;
  44.     reg  [1:0]  cntrl_next;
  45.     reg  signed [19:0] temp1,temp2,out_next,w_data1;
  46.     reg  fin1,d_next1;
  47.     wire [4:0] true_num,num_next;
  48.     reg  [4:0] stack_num;
  49.     DFF #(10) DFF0(clk, next_pc, pc);
  50.     DFF #(3) DFF1(clk, com_next, com);
  51.     DFF #(5) DFF2(clk,num_next,true_num);
  52.     SM_Mem SM0(clk, rst_n, cntrl_next, w_data1, r_data, full, empty);
  53.    
  54.     always@(*) begin
  55.         next_pc1 = pc;
  56.         com_next1 = com;
  57.         d_next1 = 10'd0;
  58.         stack_num = true_num;
  59.         //out_next = out_data;
  60.         fin1 = fin;
  61.        
  62.         case(com)
  63.             `INIT: begin
  64.                 cntrl_next = `NO_OP;
  65.                 next_pc1 = next_pc1 + 10'd1;
  66.                 d_next1 = 10'd0;
  67.                 w_data1 = 0;
  68.                 err_code = 3'b000;
  69.                 out_next = 0;
  70.                 stack_num = 0;
  71.                 com_next1 = `READCOM;   //it's a big fault
  72.             end
  73.             `READCOM: begin
  74.                 cntrl_next = `NO_OP;
  75.                 w_data1 = 0;
  76.                 if(instr[12:10] == 3'b100) begin
  77.                     d_next1 = 1;
  78.                     out_next = 0;
  79.                     err_code = 3'b010;
  80.                     next_pc1 = next_pc1 + 10'd1;
  81.                     com_next1 = `READCOM;
  82.                 end
  83.                 else if(instr[12:10] == 3'b101) begin
  84.                     d_next1 = 1;
  85.                     out_next = 0;
  86.                     err_code = 3'b010;
  87.                     next_pc1 = next_pc1 + 10'd1;
  88.                     com_next1 = `READCOM;
  89.                 end
  90.                 else if(instr[12:10] == 3'b110) begin
  91.                     d_next1 = 1;
  92.                     out_next = 0;
  93.                     err_code = 3'b010;
  94.                     next_pc1 = next_pc1 + 10'd1;
  95.                     com_next1 = `READCOM;      
  96.                 end
  97.                 else if(instr[12:10] == 3'b111) begin
  98.                     d_next1 = 1;
  99.                     out_next = 0;
  100.                     err_code = 3'b010;
  101.                     next_pc1 = next_pc1 + 10'd1;
  102.                     com_next1 = `READCOM;
  103.                 end
  104.                 else begin//*/
  105.                     d_next1 = 10'd0;
  106.                     err_code = 3'b000;
  107.                     out_next = 0;
  108.                     com_next1 = instr[12:10];
  109.                end
  110.                 w_data1 = 10'd0;
  111.                 //next_pc1 = next_pc1 + 10'd1;
  112.             end
  113.             `PUSH2: begin
  114.                  out_next = 0;
  115.                 if(true_num < 8) begin
  116.                     cntrl_next = `PUSH1;
  117.                     err_code = 3'b000;
  118.                     w_data1 = instr[9:0];
  119.                     next_pc1 = next_pc1 + 10'd1;
  120.                     d_next1 = 10'd0;
  121.                     stack_num = stack_num + 1;
  122.                     com_next1 = `READCOM;
  123.                 end
  124.                 else begin
  125.                     d_next1 = 1;
  126.                     cntrl_next = `NO_OP;
  127.                     err_code = 3'b001;
  128.                     next_pc1 = next_pc1 + 10'd1;
  129.                     com_next1 = `READCOM;
  130.                 end
  131.             end
  132.             `ADD: begin//1
  133.                 out_next = 0;
  134.                 w_data1 = 0;
  135.                 if(stack_num >= 2) begin
  136.                     cntrl_next = `POP;
  137.                     err_code = 3'b000;
  138.                     temp1 = r_data;
  139.                     last = `ADD;
  140.                     com_next1 = `COM;
  141.                     d_next1 = 10'd0;
  142.                 end
  143.                 else begin
  144.                     d_next1 = 1;
  145.                     cntrl_next = `NO_OP;
  146.                     err_code = 3'b100;
  147.                     next_pc1 = next_pc1 + 10'd1;
  148.                     com_next1 = `READCOM;
  149.                 end
  150.                 //next_pc1 = next_pc1 + 10'd1;
  151.             end
  152.             `SUB: begin//2
  153.                  out_next = 0;
  154.                  w_data1 = 0;
  155.                 if(stack_num >= 2) begin
  156.                     cntrl_next = `POP;
  157.                     err_code = 3'b000;
  158.                     temp1 = r_data;
  159.                     last = `SUB;
  160.                     com_next1 = `COM;
  161.                     d_next1 = 10'd0;
  162.                 end
  163.                 else begin
  164.                     d_next1 = 1;
  165.                     //out_next = 0;
  166.                     cntrl_next = `NO_OP;
  167.                     err_code = 3'b100;
  168.                     next_pc1 = next_pc1 + 10'd1;
  169.                     com_next1 = `READCOM;
  170.                 end
  171.                 //next_pc1 = next_pc1 + 10'd1;
  172.             end
  173.             `MUL: begin//3
  174.                  out_next = 0;
  175.                  w_data1 = 0;
  176.                 if(stack_num >= 2) begin
  177.                     cntrl_next = `POP;
  178.                     err_code = 3'b000;
  179.                     temp1 = r_data;
  180.                     last = `MUL;
  181.                     com_next1 = `COM;
  182.                     d_next1 = 10'd0;
  183.                 end
  184.                 else begin
  185.                     d_next1 = 1;
  186.                     //out_next = 0;
  187.                     cntrl_next = `NO_OP;
  188.                     err_code = 3'b100;
  189.                     next_pc1 = next_pc1 + 10'd1;
  190.                     com_next1 = `READCOM;
  191.                 end
  192.                 //next_pc1 = next_pc1 + 10'd1;
  193.             end
  194.             `COM: begin
  195.                 cntrl_next = `POP;
  196.                 err_code = 3'b000;
  197.                 out_next = 0;
  198.                 w_data1 = 0;
  199.                 d_next1 = 10'd0;
  200.                 temp2 = r_data;
  201.                 com_next1 = `PUSHANS;
  202.                 stack_num = stack_num - 1;
  203.                
  204.             end
  205.             `PUSHANS: begin
  206.                 cntrl_next = `PUSH1;
  207.                 err_code = 3'b000;
  208.                 d_next1 = 10'd1;
  209.                 next_pc1 = next_pc1 + 10'd1;
  210.                 com_next1 = `READCOM;
  211.                
  212.                 if(instr[12:10] == `ADD) begin
  213.                     out_next = temp1 + temp2;
  214.                     w_data1 = temp1 +temp2;
  215.                 end
  216.                 else if(instr[12:10] == `SUB) begin
  217.                     out_next = temp1 - temp2;
  218.                     w_data1 = temp1 - temp2;
  219.                 end
  220.                 else if(instr[12:10] == `MUL)  begin
  221.                     out_next = temp1 * temp2;
  222.                    w_data1 = temp1 * temp2;
  223.                 end
  224.                 else begin end
  225.             end
  226.             default: begin
  227.                 out_next = 0;
  228.             end
  229.         endcase
  230.         if(pc == 10'd1023) begin
  231.             num = instr[9:0];
  232.         end
  233.         else begin end
  234.         fin1 = (pc == num)? 10'd1: 10'd0;
  235.         //d_next1 = (fin1 == 10'd1)? 10'd1: d_next1;
  236.     end
  237.    // assign w_data = (rst_n == 10'd0)? 10'd0: w_data1;
  238.     //assign cntrl = (rst_n == 10'd0)? `NO_OP: cntrl_next;
  239.     assign next_pc = (rst_n == 10'd0)? 10'd1023: next_pc1;
  240.     assign fin = (rst_n == 10'd0)? 10'd0: fin1;
  241.     assign com_next = (rst_n == 10'd0)? `INIT: com_next1;
  242.     assign d_valid = (rst_n == 10'd0)? 10'd0: d_next1;
  243.     assign out_data = (rst_n == 10'd0)? 10'd0: out_next;
  244.     assign num_next = (rst_n == 0)? 0: stack_num;
  245.     // your code here, remember to instantiate your SM_Mem
  246. endmodule
  247. module SM_Mem(clk, rst_n, cntrl, w_data, r_data, full, empty);
  248.     input clk;
  249.     input rst_n;
  250.     input [1:0] cntrl;
  251.     input signed[19:0] w_data;
  252.     output signed [19:0] r_data;
  253.     output reg full;
  254.     output reg empty;
  255.     reg signed [19:0] r_data1;
  256.     wire [3:0] trueptr,next_ptr;
  257.     reg [2:0] ptr;
  258.     reg signed [19:0] stack [7:0];
  259.     reg signed [19:0] temp;
  260.     DFF #(4) DFF0(clk, next_ptr, trueptr);
  261.     always @(*) begin
  262.        ptr = trueptr;
  263.        temp = trueptr - 1;
  264.         if (rst_n == 0) begin
  265.             ptr = 0;
  266.             r_data1 = 0;
  267.         end
  268.         else begin end
  269.         if (cntrl == `PUSH1) begin
  270.             if(trueptr < 8) begin
  271.                  stack[trueptr] = w_data;
  272.                  ptr = trueptr + 1;
  273.                  //r_data1 = 0;
  274.             end
  275.             else begin
  276.                  //r_data1 = 0;
  277.             end
  278.         end
  279.         else if (cntrl == `POP) begin
  280.             if(trueptr > 0) begin
  281.                  ptr = trueptr - 1;
  282.                  r_data1 = stack[temp];
  283.             end
  284.             else begin
  285.                  //r_data1 = 0;
  286.             end
  287.         end
  288.         else begin
  289.           //r_data1 = 0;
  290.         end
  291.         full = (trueptr < 8)? 0: 1;
  292.         empty = (trueptr > 0)? 1: 0;
  293.     end
  294.     assign next_ptr = (rst_n == 10'd0)? 10'd0: ptr;
  295.     assign r_data = (rst_n == 0)? 0: r_data1;
  296.     // your code
  297. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement