Advertisement
Sidsh

Dijkstra_only

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