Advertisement
Guest User

Untitled

a guest
Aug 27th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VeriLog 15.55 KB | None | 0 0
  1. module ColorDetection (
  2.     input         CLK      ,
  3.     input         ENABLE1  ,
  4.     input         ENABLE2  ,
  5.     input         ENABLE3  ,
  6.     input         ENABLE4  ,
  7.     input         ENABLE5  ,   
  8.     input         ENABLE6  ,
  9.     input         ENABLE7  ,   
  10.     input         ENABLE8  ,   
  11.     input         ENABLE9  ,   
  12.     input         ENABLE10 ,
  13.     input         ENABLEALL,
  14.     input             ENABLE12 ,
  15.     input             ENABLE13 ,
  16.     input             ENABLE14 ,
  17.     input             ENABLE15 ,
  18.     input             ENABLE16 ,
  19.     input             ENABLE17 ,
  20.     input  [ 7:0] R_IN     ,
  21.     input  [ 7:0] G_IN     ,
  22.     input  [ 7:0] B_IN     ,
  23.     input  [12:0] VGA_V_CNT,
  24.     input  [12:0] VGA_H_CNT,
  25.     output [ 7:0] R_OUT    ,
  26.     output [ 7:0] G_OUT    ,
  27.     output [ 7:0] B_OUT    ,
  28.     output [ 5:0] BALL_X   ,
  29.     output [ 5:0] BALL_Y      ,
  30.     output [ 5:0] BALL_X1  ,
  31.     output [ 5:0] BALL_Y1
  32. );
  33.  
  34.     parameter MARGIN      = 1;
  35.     parameter FONT_WIDTH  = 3;
  36.     parameter FONT_HEIGHT = 5;
  37.  
  38.     reg [14:0] font[0:10] = '{
  39.         15'b111_101_101_101_111, // 0
  40.         15'b110_010_010_010_111, // 1
  41.         15'b111_001_111_100_111, // 2
  42.         15'b111_001_111_001_111, // 3
  43.         15'b101_101_111_001_001, // 4
  44.         15'b111_100_111_001_111, // 5
  45.         15'b111_100_111_101_111, // 6
  46.         15'b111_001_001_001_001, // 7
  47.         15'b111_101_111_101_111, // 8
  48.         15'b111_101_111_001_111, // 9
  49.         15'b000_000_111_000_000  // -
  50.     };
  51.  
  52.     function font_pixel;
  53.         input [3:0] digit;
  54.         input [15:0] x_pos;
  55.         input [15:0] y_pos;
  56.         begin
  57.             case (digit)
  58.                 0  : font_pixel = font[0][x_pos + y_pos * FONT_WIDTH];
  59.                 1  : font_pixel = font[1][x_pos + y_pos * FONT_WIDTH];
  60.                 2  : font_pixel = font[2][x_pos + y_pos * FONT_WIDTH];
  61.                 3  : font_pixel = font[3][x_pos + y_pos * FONT_WIDTH];
  62.                 4  : font_pixel = font[4][x_pos + y_pos * FONT_WIDTH];
  63.                 5  : font_pixel = font[5][x_pos + y_pos * FONT_WIDTH];
  64.                 6  : font_pixel = font[6][x_pos + y_pos * FONT_WIDTH];
  65.                 7  : font_pixel = font[7][x_pos + y_pos * FONT_WIDTH];
  66.                 8  : font_pixel = font[8][x_pos + y_pos * FONT_WIDTH];
  67.                 9  : font_pixel = font[9][x_pos + y_pos * FONT_WIDTH];
  68.                 10 : font_pixel = font[10][x_pos + y_pos * FONT_WIDTH];
  69.             endcase
  70.         end
  71.     endfunction
  72.  
  73.     parameter rows       = 30; //default 30
  74.     parameter rowstest   = 10;
  75.     parameter cols       = 40;  //default 40
  76.     parameter block_size = 16; // 640/40=16 480/30=16 so block_size = 16x16 350
  77.     shortint unsigned        modes  = 0;
  78.  
  79.     byte unsigned row              [cols]; // Store the count of green pixels of a single row
  80.     byte unsigned max_count_per_row[rows]; // Store the max green count per row
  81.     byte unsigned max_x_per_row    [rows]; // Store the x of the max green per row
  82.  
  83.     // The max x/y/count so far in for loops for getting the max of the array above
  84.     shortint unsigned max_x, max_y, max_count = 0;
  85.  
  86.     // Current x/y
  87.     shortint unsigned x, y;
  88.  
  89.     // Current x/y on the grid
  90.     shortint unsigned x_grid, y_grid;
  91.     shortint unsigned prev_y_grid = rows; // Previous y on the grid
  92.     // (default = imposible so first clock tick the array is reset)
  93.  
  94.     // HSL values
  95.     shortint unsigned hue, lightness, saturation;
  96.  
  97.     // HSL tmp values
  98.     shortint unsigned max, min, delta, R, G, B;
  99.    
  100.     shortint unsigned y_block_min, y_block_max, x_block_min, x_block_max, side;
  101.    
  102.  
  103.     // Is the current pixel green or not
  104.     reg is_green = 0;
  105.    
  106.     // Is the current pixel blue or not
  107.     reg is_blue = 0;
  108.  
  109.     // Is the current pixel blue or not
  110.     reg is_red = 0;
  111.    
  112.     // Is the current pixel blue or not
  113.     reg is_white = 0;
  114.    
  115.     // Is the current pixel blue or not
  116.     reg is_yellow = 0;
  117.  
  118.     // Is the current pixel blue or not
  119.     reg is_orange = 0;
  120.    
  121.     reg is_greenblock = 0;
  122.     reg is_blueblock = 0;
  123.     shortint unsigned is_blockcolor = 0;  //0 blue, 1 green, 2  rood, 3 wit, 4 geel, 5 oranje
  124.  
  125.  
  126.     always @(posedge CLK) begin
  127. //      if (VGA_H_CNT < X_START + 155 || (VGA_H_CNT - X_START) > VGA_WIDTH - 370 ||
  128. //          VGA_V_CNT < Y_START + 52 || (VGA_V_CNT - Y_START) > VGA_HEIGHT - 320) begin
  129.         if (VGA_H_CNT < X_START || (VGA_H_CNT - X_START) > VGA_WIDTH ||
  130.             VGA_V_CNT < Y_START || (VGA_V_CNT - Y_START) > VGA_HEIGHT) begin
  131.             R_OUT <= R_IN;
  132.             G_OUT <= G_IN;
  133.             B_OUT <= B_IN;
  134.         end else begin
  135.             // Calculate the real x/y values
  136.             x = VGA_H_CNT - X_START;
  137.             y = VGA_V_CNT - Y_START;
  138.  
  139.             // Calculate the x/y on the grid
  140.             x_grid = x / block_size;
  141.             y_grid = y / block_size;
  142.            
  143.             //blokken voor detectie
  144.  
  145.            
  146.             y_block_min = 0;
  147.             y_block_max = 30;
  148.             x_block_min = 0;
  149.             x_block_max = 40;
  150.  
  151.             if (ENABLE10) begin
  152.                 y_block_min = 3;
  153.                 y_block_max = 9;
  154.                 x_block_min = 9;
  155.                 x_block_max = 15;
  156.             end else if (ENABLE12) begin
  157.                 y_block_min = 3;
  158.                 y_block_max = 9;
  159.                 x_block_min = 16;
  160.                 x_block_max = 22;
  161.             end else if (ENABLE13) begin
  162.                 y_block_min = 3;
  163.                 y_block_max = 9;
  164.                 x_block_min = 24;
  165.                 x_block_max = 30;
  166.             end else if (ENABLE14) begin
  167.                 y_block_min = 11;
  168.                 y_block_max = 17;
  169.                 x_block_min = 9;
  170.                 x_block_max = 15;
  171.             end else if (ENABLE15) begin
  172.                 y_block_min = 11;
  173.                 y_block_max = 17;
  174.                 x_block_min = 17;
  175.                 x_block_max = 23;
  176.             end
  177.            
  178.             if (y_grid >= y_block_min && y_grid < y_block_max && x_grid >= x_block_min && x_grid < x_block_max) begin //if (y_grid > 17 && y_grid < 24 && x_grid > 22 && x_grid < 29)
  179.  
  180.                 // Reset the grid to zero's
  181.                 if(y_grid != prev_y_grid) begin
  182.                     prev_y_grid = y_grid;
  183.                     for (int for_x = 0; for_x < cols; for_x++) begin
  184.                         row[for_x] = 0;
  185.                     end
  186.                 end
  187.  
  188.                 // convert 0-255 to 0-100 (normally 0-1 but no floats bruh)
  189.                 R = R_IN * 100 / 255;
  190.                 G = G_IN * 100 / 255;
  191.                 B = B_IN * 100 / 255;
  192.  
  193.                 // Calculate max/min (without helper functions, say whut)
  194.                 max = (R > G) ? (R > B ? R : B) : (G > B ? G : B);
  195.                 min = (R < G) ? (R < B ? R : B) : (G < B ? G : B);
  196.  
  197.                 delta = max - min;
  198.  
  199.                 lightness  = (max + min) / 2;
  200.                 saturation = (lightness > 50) ? (delta * 100) / (200 - max - min) : (delta * 100) / (max + min);
  201.  
  202.                 if (max == R) hue = 60 * ((G - B) / delta % 6);
  203.                 if (max == G) hue = 60 * ((B - R) / delta + 2);
  204.                 if (max == B) hue = 60 * ((R - G) / delta + 4);
  205.  
  206.                 if (max == min) begin // achromatic
  207.                     hue        = 0;
  208.                     saturation = 0;
  209.                 end
  210.  
  211.                 if (hue > 360) hue = hue - 360;
  212.                 if (hue < 0) hue = hue + 360;
  213.  
  214.                 if(ENABLEALL) begin
  215.                     is_blue = hue > 160 && hue < 200 &&  // was 160 200
  216.                         saturation > 20; //default 20
  217.                     // lightness > 10 && lightness < 90;
  218.                    
  219.                     is_green = hue > 70 && hue < 100 &&  // was 50 90
  220.                         saturation > 22; //default 20
  221.                     // lightness > 10 && lightness < 90;
  222.                    
  223.                     is_red = (hue > 320 || hue < 20) && //was 345 20
  224.                         saturation < 70 && lightness < 60; //default 20
  225.                     // lightness > 10 && lightness < 90;
  226.                    
  227.                     is_white = lightness > 95 && saturation < 50;
  228.                    
  229.                     is_yellow = hue > 35 && hue < 70 && // was 25 60
  230.                         saturation > 20; //default 20
  231.                     // lightness > 10 && lightness < 90;
  232.                    
  233.                     is_orange =  hue > 15 && hue < 35 && //was 0 35//////// 10 35 werkt, maar pakt te vaak over rood
  234.                         saturation > 60; //was 50
  235.                         // lightness > 10 && lightness < 90;
  236.                    
  237.                    
  238.                    
  239.                     //hier worden de hoeveelheid groene puntjes berekend
  240.                     // If hue is green
  241.                     // And value is still valid
  242.                     if (is_blue && row[x_grid] < 255) begin
  243.                         // Increase by one
  244.         //              row[x_grid] = 20; //default ++
  245.                         row[x_grid]++;
  246.                     end
  247. //                  if (is_green && row[x_grid] < 255) begin
  248. //                      // Increase by one
  249. //      //              row[x_grid] = 20; //default ++
  250. //                      row[x_grid]++;
  251. //                  end
  252. //                  if (is_red && row[x_grid] < 255) begin
  253. //                      // Increase by one
  254. //      //              row[x_grid] = 20; //default ++
  255. //                      row[x_grid]++;
  256. //                  end
  257.                 end
  258.  
  259.                 R_OUT <= 0;
  260.                 G_OUT <= 0;
  261.                 B_OUT <= 0;
  262.  
  263.                 if(ENABLE1 && y % block_size < FONT_HEIGHT && x_grid > 0 && x_grid < cols - 1) begin
  264.                     shortint unsigned x_tgt, block_x, block_y, dig, x_pos, y_pos;
  265.  
  266.                     // Calculate the start x/y of the block
  267.                     block_x = x_grid * block_size + 1;
  268.                     block_y = y_grid * block_size;
  269.  
  270.                     for (int i = 0; i < 3; i++) begin
  271.                         x_tgt = block_x + i * (FONT_WIDTH + MARGIN); //debug letters
  272.                         if (x >= x_tgt && x < (x_tgt + FONT_WIDTH)) begin
  273.                             byte unsigned x_pos, y_pos;
  274.  
  275.                             x_pos = (x - x_tgt);
  276.                             y_pos = (y - block_y);
  277.  
  278.                             case (i)
  279.                                 0 : dig = row[x_grid] % 1000 / 100;
  280.                                 1 : dig = row[x_grid] % 100 / 10;
  281.                                 2 : dig = row[x_grid] % 10;
  282.                             endcase
  283.  
  284.                             if (font_pixel(dig, FONT_WIDTH - 1 - x_pos, FONT_HEIGHT - 1 - y_pos)) begin
  285.                                 R_OUT <= 255;
  286.                                 G_OUT <= 255;
  287.                                 B_OUT <= 255;
  288.                             end
  289.                         end
  290.                     end
  291.                 end else if(BALL_X == x_grid && BALL_Y == y_grid && 1 == 0) begin
  292.                     // Show the hotbox
  293.                     R_OUT <= 0;
  294.                     G_OUT <= 255;
  295.                     B_OUT <= 0;
  296.                 end else if(BALL_X1 == x_grid && BALL_Y1 == y_grid && 1==0) begin
  297.                     // Show the hotbox
  298.                     R_OUT <= 0;
  299.                     G_OUT <= 0;
  300.                     B_OUT <= 255;
  301.                 end else if(ENABLE4 && is_green) begin  //end else if(ENABLE2 && is_green) begin
  302.                     // Show all the greens with hue 100 > hue < 140
  303.                     R_OUT <= 0;
  304.                     G_OUT <= 255;
  305.                     B_OUT <= 0;
  306.                 end else if(ENABLE5 && is_blue) begin   //end else if(ENABLE5 && is_blue) begin
  307.                     // Show all the greens with hue 100 > hue < 140
  308.                     R_OUT <= 0;
  309.                     G_OUT <= 0;
  310.                     B_OUT <= 255;
  311.                 end else if(ENABLE6 && is_red) begin
  312.                     // Show all the greens with hue 100 > hue < 140
  313.                     R_OUT <= 255;
  314.                     G_OUT <= 0;
  315.                     B_OUT <= 0;
  316.                 end else if(ENABLE7 && is_white) begin
  317.                     // Show all the greens with hue 100 > hue < 140
  318.                     R_OUT <= 0;
  319.                     G_OUT <= 0;
  320.                     B_OUT <= 0;
  321.                 end else if(ENABLE8 && is_yellow) begin
  322.                     // Show all the greens with hue 100 > hue < 140
  323.                     R_OUT <= 255;
  324.                     G_OUT <= 255;
  325.                     B_OUT <= 0;
  326.                 end else if(ENABLE9 && is_orange) begin
  327.                     // Show all the greens with hue 100 > hue < 140
  328.                     R_OUT <= 255;
  329.                     G_OUT <= 125;
  330.                     B_OUT <= 0;
  331.                 end else if(~ENABLE3) begin
  332.                     R_OUT <= R_IN;
  333.                     G_OUT <= G_IN;
  334.                     B_OUT <= B_IN;
  335.                 end else if((is_blue && x == 100 && y == 100) || (is_blue && x == 101 && y == 100) || (is_blue && x == 100 && y == 101) || (is_blue && x == 101 && y == 101)) begin //
  336.                     if(1)   begin //is_green ||
  337.                         R_OUT <= 255;
  338.                         G_OUT <= 0;
  339.                         B_OUT <= 220;
  340.                         is_blueblock = 1;
  341.                         is_blockcolor = 0;//
  342.                     end
  343.                 end else if ((is_green && x == 100 && y == 100) || (is_green && x == 101 && y == 100) || (is_green && x == 100 && y == 101) || (is_green && x == 101 && y == 101)) begin
  344.                     is_greenblock = 1;
  345.                     is_blockcolor = 1;
  346. //              end else if ((~is_green && x == 100 && y == 100) && (~is_green && x == 101 && y == 100) && (~is_green && x == 100 && y == 101) && (~is_green && x == 101 && y == 101) || ENABLE16) begin
  347. //                  //DEZE MOET UIT
  348. //                 
  349. //                  if (1)  begin //~is_green ||
  350. //                      is_greenblock = 0;
  351. //                      is_blueblock = 0;
  352. //                      is_blockcolor = 2;
  353. //                  end
  354.                 end else if ((is_red && x == 100 && y == 100) || (is_red && x == 101 && y == 100) || (is_red && x == 100 && y == 101) || (is_red && x == 101 && y == 101)) begin
  355.                     is_blockcolor = 2;
  356.                 end else if ((is_white && x == 100 && y == 100) || (is_white && x == 101 && y == 100) || (is_white && x == 100 && y == 101) || (is_white && x == 101 && y == 101)) begin
  357.                     is_blockcolor = 3;
  358.                 end else if ((is_yellow && x == 100 && y == 100) || (is_yellow && x == 101 && y == 100) || (is_yellow && x == 100 && y == 101) || (is_yellow && x == 101 && y == 101)) begin
  359.                     is_blockcolor = 4;
  360.                 end else if ((is_orange && x == 100 && y == 100) || (is_orange && x == 101 && y == 100) || (is_orange && x == 100 && y == 101) || (is_orange && x == 101 && y == 101)) begin
  361.                     is_blockcolor = 5;
  362.                 end else if (is_blockcolor == 2 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_greenblock == 0
  363.                 //rood betekend kleur groen niet herkend
  364.                     R_OUT <= 255;
  365.                     G_OUT <= 0;
  366.                     B_OUT <= 0;
  367.                 end else if (is_blockcolor == 0 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_greenblock && ~is_blueblock
  368.                 //blauw
  369.                     R_OUT <= 0;
  370.                     G_OUT <= 0;
  371.                     B_OUT <= 255;
  372.                 end else if (is_blockcolor == 1 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_blueblock && ~is_greenblock
  373.                 //groen
  374.                     R_OUT <= 0;
  375.                     G_OUT <= 255;
  376.                     B_OUT <= 0;
  377.                 end else if (is_blockcolor == 2 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_blueblock && ~is_greenblock
  378.                 //rood
  379.                     R_OUT <= 255;
  380.                     G_OUT <= 0;
  381.                     B_OUT <= 0;
  382.                 end else if (is_blockcolor == 3 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_blueblock && ~is_greenblock
  383.                 //wit
  384.                     R_OUT <= 255;
  385.                     G_OUT <= 255;
  386.                     B_OUT <= 255;
  387.                 end else if (is_blockcolor == 4 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_blueblock && ~is_greenblock
  388.                 //geel
  389.                     R_OUT <= 255;
  390.                     G_OUT <= 255;
  391.                     B_OUT <= 0;
  392.                 end else if (is_blockcolor == 5 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_blueblock && ~is_greenblock
  393.                 //oranje
  394.                     R_OUT <= 255;
  395.                     G_OUT <= 125;
  396.                     B_OUT <= 0;
  397.                 end
  398. //              end else if (ENABLE16) begin
  399. //                  is_greenblock = 0;
  400. //              end
  401.  
  402.  
  403.                 // If this pixel is the last of the row
  404.                 // (x == max of screen and y == on last pixel of row)
  405.                 if(x == VGA_WIDTH - 1 && y % block_size == block_size - 1) begin
  406.                     max_count = 0;
  407.  
  408.                     // For every value in the row
  409.                     // HIER VALLLEN DE ACTIEVE COLUMNS AAN TE PASSEN
  410.                     for (int for_x = 0; for_x < cols; for_x++) begin //                 for (int for_x = 0; for_x < cols; for_x++) begin
  411.                         // If it's bigger than the previous found one
  412.                         if(row[for_x] > max_count &&
  413.                             row[for_x] > (block_size * block_size / 2)) begin
  414.                             // Store it
  415.                             max_count = row[for_x];
  416.                             max_x     = for_x;
  417.                         end
  418.  
  419.                         row[for_x] = 0; // Always reset the cell
  420.                     end
  421.  
  422.                     max_count_per_row[y_grid] = max_count;
  423.                     max_x_per_row[y_grid]     = max_x;
  424.  
  425.                     // On the last row
  426.                     if(y_grid == rows - 1) begin
  427.                         max_count = 0;
  428.  
  429.                         for (int for_y = 0; for_y < rows; for_y++) begin
  430.                             if(max_count_per_row[for_y] > max_count) begin
  431.                                 max_count = max_count_per_row[for_y];
  432.                                 max_y     = for_y;
  433.                                 max_x     = max_x_per_row[for_y];
  434.                             end
  435.                         end
  436.  
  437.                         // Set the new hotbox for the ball
  438.                         BALL_X = max_x;
  439.                         BALL_Y = max_y;
  440.                     end
  441.                 end
  442.                            
  443.     //          // If this pixel is the last of the row
  444.     //          // (x == max of screen and y == on last pixel of row)
  445.     //          if(x == VGA_WIDTH - 1 && y % block_size == block_size - 1) begin
  446.     //              max_count = 0;
  447.     //
  448.     //              // For every value in the row
  449.     //              // HIER VALLLEN DE ACTIEVE COLUMNS AAN TE PASSEN
  450.     //              for (int for_x = 10; for_x < 20; for_x++) begin //              for (int for_x = 0; for_x < cols; for_x++) begin
  451.     //                  // If it's bigger than the previous found one
  452.     //                  if(row[for_x] > max_count &&
  453.     //                      row[for_x] > (block_size * block_size / 2)) begin
  454.     //                      // Store it
  455.     //                      max_count = row[for_x];
  456.     //                      max_x     = for_x;
  457.     //                  end
  458.     //
  459.     //                  row[for_x] = 0; // Always reset the cell
  460.     //              end
  461.     //
  462.     //              max_count_per_row[y_grid] = max_count;
  463.     //              max_x_per_row[y_grid]     = max_x;
  464.     //
  465.     //              // On the last row
  466.     //              if(y_grid == rows - 1) begin
  467.     //                  max_count = 0;
  468.     //
  469.     //                  for (int for_y = 0; for_y < rows; for_y++) begin
  470.     //                      if(max_count_per_row[for_y] > max_count) begin
  471.     //                          max_count = max_count_per_row[for_y];
  472.     //                          max_y     = for_y;
  473.     //                          max_x     = max_x_per_row[for_y];
  474.     //                      end
  475.     //                  end
  476.     //
  477.     //                  // Set the new hotbox for the ball
  478.     //                  BALL_X = max_x;
  479.     //                  BALL_Y = max_y;
  480.     //              end
  481.     //          end
  482.             end else begin
  483.                 R_OUT <= 255;
  484.                 G_OUT <= 255;
  485.                 B_OUT <= 255;
  486.                
  487.             end
  488.         end
  489.     end
  490. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement