Advertisement
Sidsh

Untitled

Feb 9th, 2022
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Dijkstra(input clk_50;)
  2. reg[11:0] dist [0:36];
  3. reg visited [0:36];
  4. reg [6:0] length = 0;
  5. reg [6:0] sp [36:0];
  6. reg [1:0] p_dc [0:36];
  7. reg [6:0] parent [0:36];
  8. reg [5:0] V = 37;   //parameter V =37
  9.  
  10. reg [7:0] i,j,k;
  11. reg [11:0] cost [0:36][0:36];
  12.  
  13. //shortest distance function
  14.  
  15. function [6:0]minNode;
  16.  
  17. input [6:0] a;
  18. reg [11:0] minValue = 999; //   reg [6:0]minValue = 999;
  19. reg [11:0] minNode1 = 0
  20. begin
  21.   for (k = 0; k <V ; k = k+1)
  22.   begin
  23.    if (dist[k] < minValue && visited[k] == 0)
  24.    begin
  25.         minValue = dist[k];
  26.         minNode1 = k;
  27.         visited[k] = 1;    //To stop back tracing
  28.    end
  29.   end  
  30.   minNode = minNode1;
  31. end
  32. endfunction
  33.  
  34. //add a cost matrix
  35.  
  36.  
  37. reg [6:0] start1 = 33;
  38. reg [6:0] end1 = 3;
  39. reg [6:0] s6 = 22;
  40. reg [6:0] dzn1 =7;
  41.  
  42. function dijkstra;
  43. input [6:0] start;
  44. input [6:0] enda;
  45. begin
  46.     $display("enter dijkstra");
  47.     for ( i = 0; i < V ; i = i + 1)
  48.     begin
  49.         dist[i] = 999;
  50.         parent[i] = i;
  51.         visited[i] = 0;
  52.         sp[i] = 0;
  53.         p_dc[i] = -1;
  54.     end
  55.  
  56.     dist[start] = 0
  57.     for ( i = 0; i < V; i = i + 1)
  58.     begin
  59.         near = minNode(1);   /////
  60.         visited[near] = 1;   /////
  61.  
  62.         for (adj = 0; adj < V ; adj = adj + 1)
  63.         begin
  64.             if (cost[near][adj] != 0 && dist [adj] > dist [near] + cost [near][adj])
  65.             begin
  66.                 dist[adj] = dist[near] + cost[near][adj];
  67.                 parent[adj] = near;
  68.             end
  69.         end
  70.     end
  71.  
  72.     length = length + 1 ;
  73.     sp[length] = start;
  74.     i = 0;   /////
  75.  
  76.     for ( j = 35; j > 0; j = j - 1)
  77.     begin
  78.         if (j<length && j>0)
  79.         begin
  80.              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)))
  81.                     begin $display("Straight");  
  82.                         p_dc[i]=0;
  83.                        
  84.                         $display("%6d",p_dc[i]);
  85.                         i=i+1;
  86.                     end
  87.              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)))
  88.                     begin $display("Left");
  89.                         p_dc[i]=1;
  90.                        
  91.                         $display("%6d",p_dc[i]);
  92.                         i=i+1;
  93.                     end
  94.                 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)))
  95.                     begin $display("Right");
  96.                         p_dc[i]=2;
  97.                         $display("%6d",p_dc[i]);
  98.                         i=i+1;
  99.                     end    
  100.         end
  101.     end
  102.  
  103.     dijkstra = 1
  104.  
  105.  
  106. end
  107. endfunction
  108. reg flag_dijkstra = 0;
  109. reg signed [6:0] count_dijkstra = -1;
  110. if (flag_dijkstra == 0;)
  111. begin
  112.     flag_dijkstra = dijkstra(start1,end1)
  113. end
  114.  
  115. if (flag_dijkstra == 1)
  116. begin
  117.      count_dijkstra = count_dijkstra+1;    
  118.     $display("%6d, %6d, %6d",p_dc[0], count_dijkstra, p_dc[count_dijkstra]);
  119.     if(count_dijkstra==(length-1))
  120.         begin
  121.             start1 = end1;
  122.             if(sp[0] == end1) begin
  123.                 end1=s6;
  124.             end
  125.             if(sp[0] == s6) begin
  126.                 end1=dzn1;
  127.             end
  128.             flag_dijkstra = 0;
  129.         end
  130.  
  131. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement