Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module question2(clock, resetn, s, done, DA, DB, A, B, P);
  2. input clock, resetn, s;
  3. wire z, b_0;
  4. input [7:0] DA;
  5. input [3:0] DB;
  6. output reg done;
  7. output [7:0] A;
  8. output [7:0] P;
  9. output [3:0] B;
  10.  
  11. reg E, L, EP, sclrP;
  12.  
  13. localparam S1 = 2'd1, S2 = 2'd2, S3 = 2'd3;
  14. reg [1:0] currentState, nextState;
  15.  
  16. moduleA modA(0, L, E, DA, A);
  17. moduleB modB(0, L, E, B, b_0, z);
  18. moduleP modP(EP, sclrP, A, P);
  19.  
  20. initial
  21. begin
  22.     currentState = S1;
  23. end
  24.  
  25. always @ (posedge clock)
  26. begin
  27.     currentState = nextState;
  28. end
  29.  
  30. always @  (resetn, s, z, b_0)
  31. begin
  32.     E = 1'b0;
  33.     L = 1'b0;
  34.     EP = 1'b0;
  35.     sclrP = 1'b0;
  36.  
  37.     if (~resetn)
  38.     begin
  39.         nextState = S1;
  40.     end
  41.  
  42.     if (currentState == S1)
  43.     begin
  44.         sclrP = 1;
  45.         EP = 1;
  46.         if (s)
  47.         begin
  48.             L = 1;
  49.             E = 1;
  50.             nextState = S2;
  51.         end
  52.         else
  53.         begin
  54.             nextState = S1;
  55.         end
  56.     end
  57.  
  58.     if (currentState == S2)
  59.     begin
  60.         E = 1;
  61.         if (z)
  62.         begin
  63.             nextState = S3;
  64.         end
  65.         else
  66.         begin
  67.             if (b_0)
  68.             begin
  69.                 EP = 1;
  70.             end
  71.             nextState = S2;
  72.         end
  73.     end
  74.  
  75.     if (currentState == S3)
  76.     begin
  77.         done = 1;
  78.         if (s)
  79.         begin
  80.             nextState = S3;
  81.         end
  82.         else
  83.         begin
  84.             nextState = S1;
  85.         end
  86.     end
  87. end
  88.  
  89. endmodule
  90.  
  91. module moduleA(din, s_l, E, DA, A_out);
  92.     input din, s_l, E;
  93.     input [7:0] DA;
  94.     output reg [7:0] A_out;
  95.  
  96.     always @ ( * )
  97.     begin
  98.         if (E)
  99.         begin
  100.             if (s_l)
  101.                 // Load data in from file
  102.             begin
  103.             end
  104.             else
  105.             begin
  106.                 A_out = {DA[7:1], 1'b0};// (DA << 1);     // left shift
  107.             end
  108.         end
  109.     end
  110. endmodule
  111.  
  112. module moduleB(din, s_l, E, B, b_0, z);
  113.     input din, s_l, E;
  114.     output reg [3:0] B;
  115.     output reg b_0, z;
  116.  
  117.     always @ ( * )
  118.     begin
  119.         if (E)
  120.         begin
  121.             if (s_l)
  122.             begin
  123.                 // Load data in from file
  124.             end
  125.             else
  126.             begin
  127.                 B = B >> 1;     // right shift
  128.                 z = !(B[3] | B[2] | B[1] | B[0]);
  129.                 b_0 = B[0];
  130.             end
  131.         end
  132.     end
  133. endmodule
  134.  
  135. module moduleP(E, sclr, A, P);
  136.     input E, sclr;
  137.     input [7:0] A;
  138.     output reg [7:0] P;
  139.  
  140.     always @ ( * )
  141.     begin
  142.         if (E)
  143.         begin
  144.             if (sclr)
  145.             begin
  146.                 P <= 8'b0;
  147.             end
  148.             else
  149.                 P <= A + P;
  150.             begin
  151.  
  152.             end
  153.         end
  154.     end
  155. endmodule
  156.  
  157. module question2_tb();
  158.     reg clk, resetn, s;
  159.     reg [7:0] DA;
  160.     reg [3:0] DB;
  161.     reg [3:0] readData[0:63];
  162.     reg [6:0] dataIndex;
  163.  
  164.     initial
  165.     begin
  166.         clk <= 0;
  167.         resetn <= 0;
  168.         s <= 0;
  169.         DA <= 8'b0;
  170.         DB <= 4'b0;
  171.         $readmemb("C:/Users/Administrator/Desktop/Assignment3/Question2/Inputs.txt", readData);
  172.     end
  173.  
  174.     question2 dut(.clock(clk), .resetn(resetn), .s(s), .done(done), .DA(DA), .DB(DB), .A(A), .B(B), .P(P));
  175.  
  176.     always
  177.     begin
  178.         #10
  179.         clk <= ~clk;
  180.     end
  181.    
  182.     always @(posedge clk)
  183.     begin
  184.         resetn <= readData[dataIndex][0];
  185.         dataIndex <= dataIndex + 1;
  186.         DA <= readData[dataIndex];
  187.         dataIndex <= dataIndex + 1;
  188.         DB <= readData[dataIndex];
  189.         dataIndex <= dataIndex + 1;
  190.         s <= readData[dataIndex][0];
  191.         dataIndex <= dataIndex + 1;
  192.     end
  193. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement