Advertisement
Sidsh

Dijkstra Sep

Feb 11th, 2022
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns/1ns
  2.  
  3. module dijkstra(
  4. input clk_50,
  5. input [15:0]s3,
  6. input [15:0]s1,
  7. input [15:0]s2,
  8. output [1:0] pdc
  9. );
  10.  
  11. reg [6:0] i=0, j, k, l, adj, near, parnode;
  12.  
  13. reg [11:0] dist [0:36];
  14. reg [6:0] parent [0:36];
  15. reg visited[0:36];
  16.  
  17. integer length=0;    // length of path
  18. reg [6:0] sp[0:36];     // Store path
  19. reg [1:0] p_dc[0:36];       //Stores The turns
  20.  
  21. reg [11:0] cost[0:37][0:37];
  22. reg [11:0] cost2[0:37][0:37];
  23.  
  24. parameter V=37;
  25.  
  26. function automatic [6:0]minNode;
  27.     input [6:0]a;
  28.     reg [6:0]minValue = 999;
  29.    reg [6:0]minNode1 = 0;
  30.     begin
  31.         for (k = 0; k < V; k=k+1)
  32.         begin
  33.         if (dist[k] < minValue && visited[k] == 0)
  34.         begin
  35.             minValue = dist[k];
  36.             minNode1 = k;
  37.                 //$display("Enter min");
  38.         end
  39.         end
  40.     minNode = minNode1;
  41.      //$display("Function Called %6d", minNode1);
  42.     end
  43. endfunction
  44.  
  45.  
  46. initial begin
  47.     for(i=0;i<V;i=i+1)
  48.     begin
  49.     for(j=0;j<V;j=j+1) begin
  50.        
  51.         if(i==j) begin
  52.                 cost2[i][j] = 0;
  53.                 cost[i][j] = 0;
  54.           end
  55.         else begin
  56.                 cost2[i][j] = 999;
  57.                 cost[i][j] = 999;
  58.           end  
  59.         end
  60.     end
  61. cost2[0][1] = 3;
  62. cost2[0][2] = 2;
  63. cost2[0][8] = 1;
  64.  
  65. cost2[1][0] = 4;
  66.  
  67. cost2[2][0] = 1;
  68. cost2[2][3] = 2;
  69. cost2[2][4] = 3;
  70.  
  71. cost2[3][2] = 1;
  72. cost2[3][6] = 3;
  73. cost2[3][17] = 2;
  74.  
  75. cost2[4][2] = 4;
  76. cost2[4][5] = 2;
  77. cost2[4][11] = 3;
  78.  
  79. cost2[5][4] = 1;
  80.  
  81. cost2[6][3] = 4;
  82. cost2[6][7] = 2;
  83. cost2[6][14] = 3;
  84.  
  85. cost2[7][6] = 1;
  86.  
  87. cost2[8][0] = 4;
  88. cost2[8][10] = 2;
  89. cost2[8][33] = 3;
  90.  
  91. cost2[9][10] = 3;
  92.  
  93. cost2[10][8] = 1;
  94. cost2[10][9] = 4;
  95. cost2[10][11] = 2;
  96.  
  97. cost2[11][4] = 4;
  98. cost2[11][10] = 1;
  99. cost2[11][12] = 2;
  100. cost2[11][18] = 3;
  101.  
  102. cost2[12][11] = 1;        //M
  103. cost2[12][13] = 4;
  104. cost2[12][14] = 2;
  105. cost2[12][23] = 3;
  106.  
  107. cost2[13][12] = 3;
  108.  
  109. cost2[14][6] = 4;        //O
  110. cost2[14][12] = 1;
  111. cost2[14][15] = 2;
  112.  
  113. cost2[15][14] = 1;
  114. cost2[15][16] = 4;
  115. cost2[15][17] = 2;
  116.  
  117. cost2[16][15] = 3;       //Q
  118.  
  119. cost2[17][15] = 1;
  120. cost2[17][3] = 4;
  121. cost2[17][26] = 3;
  122.  
  123. cost2[18][19] = 2;
  124. cost2[18][11] = 4;
  125. cost2[18][21] = 3;       //S
  126.  
  127. cost2[19][18] = 1;
  128.  
  129. cost2[20][21] = 2;       //U
  130.  
  131. cost2[21][20] = 1;
  132. cost2[21][22] = 2;
  133. cost2[21][28] = 3;
  134. cost2[21][18] = 4;
  135.  
  136. cost2[22][21] = 1;       //W
  137.  
  138. cost2[23][24] = 2;
  139. cost2[23][12] = 4;
  140. cost2[23][34] = 3;
  141.  
  142. cost2[24][22] = 1;
  143.  
  144. cost2[25][26] = 2;       //Z
  145.  
  146. cost2[26][25] = 1;
  147. cost2[26][17] = 4;
  148. cost2[26][34] = 3;       //AA
  149.  
  150. cost2[27][28] = 2;
  151.  
  152. cost2[28][27] = 1;
  153. cost2[28][21] = 4;
  154. cost2[28][29] = 2;
  155. cost2[28][31] = 3;
  156.  
  157. cost2[29][28] = 1;       //AD
  158.  
  159. cost2[30][31] = 2;
  160.  
  161. cost2[31][30] = 1;       //AF
  162. cost2[31][28] = 4;
  163. cost2[31][32] = 2;
  164. cost2[31][33] = 3;
  165.  
  166. cost2[32][31] = 1;       //AG
  167.  
  168. cost2[33][31] = 4;
  169. cost2[33][8] = 1;
  170. cost2[33][34] = 2;
  171.  
  172. cost2[34][35] = 2;
  173. cost2[34][23] = 4;
  174. cost2[34][33] = 1;
  175.  
  176. cost2[35][36] = 4;
  177. cost2[35][26] = 2;
  178. cost2[35][34] = 1;
  179.  
  180. cost2[36][35] = 3;
  181. end
  182.  
  183. initial begin
  184. /*for(i=0;i<V;i=i+1)
  185.     begin
  186.     for(j=0;j<V;j=j+1) begin
  187.        
  188.         if(i==j) begin
  189.                 cost[i][j] = 0;
  190.           end
  191.         else begin
  192.                 cost[i][j] = 999;
  193.           end
  194.         end
  195.     end*/
  196. cost[0][1] = 100;               //A
  197. cost[0][2] = 2;
  198. cost[0][8] = 3;
  199.  
  200. cost[1][0] = 100;               //B
  201.  
  202. cost[2][0] = 2;                 //C
  203. cost[2][3] = 3;
  204. cost[2][4] = 2;
  205.  
  206. cost[3][2] = 3;                 //D
  207. cost[3][6] = 2;
  208. cost[3][17] = 4;
  209.  
  210. cost[4][2] = 2;         //E
  211. cost[4][5] = 100;
  212. cost[4][11] = 2;
  213.  
  214. cost[5][4] = 100;       //F
  215.  
  216. cost[6][3] = 2;         //G
  217. cost[6][7] = 100;
  218. cost[6][14] = 3;
  219.  
  220. cost[7][6] = 100;       //H
  221.  
  222. cost[8][0] = 3;         //I
  223. cost[8][10] = 2;
  224. cost[8][33] = 4;
  225.  
  226. cost[9][10] = 100;      //J
  227.  
  228. cost[10][8] = 2;        //K
  229. cost[10][9] = 100;
  230. cost[10][11] = 1;
  231.  
  232. cost[11][4] = 2;        //L
  233. cost[11][10] = 1;
  234. cost[11][12] = 3;
  235. cost[11][18] = 1;
  236.  
  237. cost[12][11] = 3;           //M
  238. cost[12][13] = 100;
  239. cost[12][14] = 1;
  240. cost[12][23] = 2;
  241.  
  242. cost[13][12] = 100;     //N
  243.  
  244. cost[14][6] = 3;            //O
  245. cost[14][12] = 1;
  246. cost[14][15] = 2;
  247.  
  248. cost[15][14] = 2;       //P
  249. cost[15][16] = 100;
  250. cost[15][17] = 1;
  251.  
  252. cost[16][15] = 100;         //Q
  253.  
  254. cost[17][15] = 1;       //R
  255. cost[17][3] = 4;
  256. cost[17][26] = 2;
  257.  
  258. cost[18][19] = 100;     //S
  259. cost[18][11] = 1;
  260. cost[18][21] = 1;      
  261.  
  262. cost[19][18] = 100;     //T
  263.  
  264. cost[20][21] = 100;         //U
  265.  
  266. cost[21][20] = 100;     //V
  267. cost[21][22] = 100;
  268. cost[21][28] = 1;
  269. cost[21][18] = 1;
  270.  
  271. cost[22][21] = 100;             //W
  272.  
  273. cost[23][24] = 100;     //X
  274. cost[23][12] = 2;
  275. cost[23][34] = 3;
  276.  
  277. cost[24][23] = 100;     //Y
  278.  
  279. cost[25][26] = 100;             //Z
  280.  
  281. cost[26][25] = 100;     //AA
  282. cost[26][17] = 2;
  283. cost[26][34] = 4;      
  284.  
  285. cost[27][28] = 100;     //AB
  286.  
  287. cost[28][27] = 100;     //AC
  288. cost[28][21] = 1;
  289. cost[28][29] = 100;
  290. cost[28][31] = 1;
  291.  
  292. cost[29][28] = 100;         //AD
  293.  
  294. cost[30][31] = 100;     //AE
  295.  
  296. cost[31][30] = 100;             //AF
  297. cost[31][28] = 1;
  298. cost[31][32] = 100;
  299. cost[31][33] = 1;
  300.  
  301. cost[32][31] = 100;             //AG
  302.  
  303. cost[33][31] = 1;       //AH
  304. cost[33][8] = 4;
  305. cost[33][34] = 3;
  306.  
  307. cost[34][35] = 2;       //AI
  308. cost[34][23] = 3;
  309. cost[34][33] = 3;
  310.  
  311. cost[35][36] = 100;     //AJ
  312. cost[35][26] = 4;
  313. cost[35][34] = 2;
  314.  
  315. cost[36][35] = 100;     //AK
  316.  
  317. end
  318. function automatic dijkstra;
  319. input [6:0] start;
  320. input [6:0] enda;
  321. begin
  322.      $display("Enter dijkstra");
  323.     for (i = 0; i < V; i=i+1)
  324.     begin
  325.         dist[i] = 999;
  326.         parent[i] = i;
  327.           visited[i] = 0;
  328.           sp[i] = 0;
  329.     end
  330.     dist[start] = 0;
  331.         for (i = 0; i < V; i=i+1)
  332.         begin
  333.         near = minNode(1);
  334.         visited[near] = 1;
  335.  
  336.         for (adj = 0; adj < V; adj=adj+1)
  337.         begin
  338.             if (cost[near][adj] != 0 && dist[adj] > dist[near] + cost[near][adj])
  339.             begin
  340.                 dist[adj] = dist[near] + cost[near][adj];
  341.                 parent[adj] = near;
  342.             end
  343.         end
  344.         end
  345.       parnode = parent[enda];
  346.       sp[length] = enda;
  347.       for(i=0;i<V;i=i+1)
  348.            begin
  349.                 if(parnode != start) begin
  350.                //$display(" <- %6d || %6d",parnode,parent[parnode]);
  351.                 length = length + 1;
  352.                 sp[length] = parnode;
  353.                 parnode = parent[parnode];                   
  354.                     end
  355.             end
  356.         length= length + 1;
  357.         sp[length] = start;
  358.       //$display("%6d", length);
  359.         //direction
  360.         i=0;
  361.         for(j=35;j>0;j=j-1) begin
  362.             //$display("%6d", sp[j]);
  363.            
  364.             if(j<length && j>0) begin
  365.                 $display("%6d", sp[j]);
  366.                 if(((cost2[sp[j]][sp[j+1]]==1) && (cost2[sp[j]][sp[j-1]] == 2)) || ((cost2[sp[j]][sp[j+1]]==2) && (cost2[sp[j]][sp[j-1]] == 1)) || ((cost2[sp[j]][sp[j+1]]==4) && (cost2[sp[j]][sp[j-1]] == 3)) || ((cost2[sp[j]][sp[j+1]]==3) &&(cost2[sp[j]][sp[j-1]] == 4)))
  367.                     begin //$display("Straight");
  368.                         p_dc[i]=0;
  369.                         i=i+1;
  370.                     end
  371.                 if(((cost2[sp[j]][sp[j+1]]==1) && (cost2[sp[j]][sp[j-1]] == 3)) || ((cost2[sp[j]][sp[j+1]]==3) && (cost2[sp[j]][sp[j-1]] == 2)) || ((cost2[sp[j]][sp[j+1]]==2) && (cost2[sp[j]][sp[j-1]] == 4)) || ((cost2[sp[j]][sp[j+1]]==4) && (cost2[sp[j]][sp[j-1]] == 1)))
  372.                     begin //$display("Left");
  373.                         p_dc[i]=1;
  374.                         i=i+1;
  375.                     end
  376.                 if(((cost2[sp[j]][sp[j+1]]==1) && (cost2[sp[j]][sp[j-1]] == 4)) || ((cost2[sp[j]][sp[j+1]]==4) && (cost2[sp[j]][sp[j-1]] == 2)) || ((cost2[sp[j]][sp[j+1]]==2) && (cost2[sp[j]][sp[j-1]] == 3)) || ((cost2[sp[j]][sp[j+1]]==3) && (cost2[sp[j]][sp[j-1]] == 1)))
  377.                     begin //$display("Right");
  378.                         p_dc[i]=2;
  379.                         i=i+1;
  380.                     end
  381.                
  382.             end
  383.            
  384.            
  385.         end
  386.          
  387.     dijkstra = 1;
  388. end
  389. endfunction
  390.  
  391. reg flag=0;
  392. reg [6:0]nodeC=0;
  393.  
  394. reg [6:0] delay=0;
  395.  
  396. reg kl=0;
  397.  
  398. always @(posedge clk_50) begin
  399.     //if(delay == 100) begin
  400.     if(flag == 0) begin
  401.         flag = dijkstra(33, 7);
  402.         i=0;
  403.         end
  404.      /*for (kl=0;kl<36;kl=kl+1) begin
  405.         case(p_dc[kl])
  406.             0: begin
  407.             $display("Straight");
  408.             end
  409.            
  410.             1: begin
  411.             $display("Left");
  412.             end
  413.            
  414.             2: begin
  415.             $display("Right");
  416.             end
  417.         endcase
  418.       end*/
  419.      
  420.       if(kl==0 && s3>2000 && s1>2000 && s2>2000)
  421.         begin
  422.             /*case(p_dc[i])
  423.             0: begin
  424.             $display("Straight");
  425.             end
  426.            
  427.             1: begin
  428.             $display("Left");
  429.             end
  430.            
  431.             2: begin
  432.             $display("Right");
  433.             end
  434.         endcase*/
  435.         if(p_dc[i] == 0) $display("Straight");
  436.        
  437.         if(p_dc[i] == 1) $display("Left");
  438.        
  439.         if(p_dc[i] == 2) $display("Right");
  440.         i=i+1;
  441.         kl=1;
  442.         end
  443.        
  444.      else if(kl==1 && s1<2000 && s2>2000 && s3<2000) begin
  445.         kl=0;
  446.      end
  447.        
  448.  //end
  449.  //else delay = delay + 1;
  450. end
  451.  
  452. assign pdc = p_dc[i];
  453.  
  454. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement