Advertisement
Guest User

Untitled

a guest
Aug 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     shortint is_greenblock = 0;
  122.  
  123.  
  124.     always @(posedge CLK) begin
  125. //      if (VGA_H_CNT < X_START + 155 || (VGA_H_CNT - X_START) > VGA_WIDTH - 370 ||
  126. //          VGA_V_CNT < Y_START + 52 || (VGA_V_CNT - Y_START) > VGA_HEIGHT - 320) begin
  127.         if (VGA_H_CNT < X_START || (VGA_H_CNT - X_START) > VGA_WIDTH ||
  128.             VGA_V_CNT < Y_START || (VGA_V_CNT - Y_START) > VGA_HEIGHT) begin
  129.             R_OUT <= R_IN;
  130.             G_OUT <= G_IN;
  131.             B_OUT <= B_IN;
  132.         end else begin
  133.             // Calculate the real x/y values
  134.             x = VGA_H_CNT - X_START;
  135.             y = VGA_V_CNT - Y_START;
  136.  
  137.             // Calculate the x/y on the grid
  138.             x_grid = x / block_size;
  139.             y_grid = y / block_size;
  140.            
  141.             //blokken voor detectie
  142.  
  143.            
  144.             y_block_min = 0;
  145.             y_block_max = 30;
  146.             x_block_min = 0;
  147.             x_block_max = 40;
  148.  
  149.             if (ENABLE10) begin
  150.                 y_block_min = 3;
  151.                 y_block_max = 9;
  152.                 x_block_min = 9;
  153.                 x_block_max = 15;
  154.             end else if (ENABLE12) begin
  155.                 y_block_min = 3;
  156.                 y_block_max = 9;
  157.                 x_block_min = 16;
  158.                 x_block_max = 22;
  159.             end else if (ENABLE13) begin
  160.                 y_block_min = 3;
  161.                 y_block_max = 9;
  162.                 x_block_min = 24;
  163.                 x_block_max = 30;
  164.             end else if (ENABLE14) begin
  165.                 y_block_min = 11;
  166.                 y_block_max = 17;
  167.                 x_block_min = 9;
  168.                 x_block_max = 15;
  169.             end else if (ENABLE15) begin
  170.                 y_block_min = 11;
  171.                 y_block_max = 17;
  172.                 x_block_min = 17;
  173.                 x_block_max = 23;
  174.             end else if (ENABLE16) begin
  175.                 y_block_min = 11;
  176.                 y_block_max = 17;
  177.                 x_block_min = 24;
  178.                 x_block_max = 30;
  179.             end
  180.            
  181.             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)
  182.  
  183.                 // Reset the grid to zero's
  184.                 if(y_grid != prev_y_grid) begin
  185.                     prev_y_grid = y_grid;
  186.                     for (int for_x = 0; for_x < cols; for_x++) begin
  187.                         row[for_x] = 0;
  188.                     end
  189.                 end
  190.  
  191.                 // convert 0-255 to 0-100 (normally 0-1 but no floats bruh)
  192.                 R = R_IN * 100 / 255;
  193.                 G = G_IN * 100 / 255;
  194.                 B = B_IN * 100 / 255;
  195.  
  196.                 // Calculate max/min (without helper functions, say whut)
  197.                 max = (R > G) ? (R > B ? R : B) : (G > B ? G : B);
  198.                 min = (R < G) ? (R < B ? R : B) : (G < B ? G : B);
  199.  
  200.                 delta = max - min;
  201.  
  202.                 lightness  = (max + min) / 2;
  203.                 saturation = (lightness > 50) ? (delta * 100) / (200 - max - min) : (delta * 100) / (max + min);
  204.  
  205.                 if (max == R) hue = 60 * ((G - B) / delta % 6);
  206.                 if (max == G) hue = 60 * ((B - R) / delta + 2);
  207.                 if (max == B) hue = 60 * ((R - G) / delta + 4);
  208.  
  209.                 if (max == min) begin // achromatic
  210.                     hue        = 0;
  211.                     saturation = 0;
  212.                 end
  213.  
  214.                 if (hue > 360) hue = hue - 360;
  215.                 if (hue < 0) hue = hue + 360;
  216.  
  217.                 if(ENABLEALL) begin
  218.                     is_blue = hue > 160 && hue < 200 &&
  219.                         saturation > 20; //default 20
  220.                     // lightness > 10 && lightness < 90;
  221.                    
  222.                     is_green = hue > 45 && hue < 95 &&  // was 50 90
  223.                         saturation > 15; //default 20
  224.                     // lightness > 10 && lightness < 90;
  225.                    
  226.                     is_red = (hue > 345 || hue < 20) &&
  227.                         saturation > 50 && lightness < 50; //default 20
  228.                     // lightness > 10 && lightness < 90;
  229.                    
  230.                     is_white = lightness > 90;
  231.                    
  232.                     is_yellow = hue > 25 && hue < 60 &&
  233.                         saturation > 10; //default 20
  234.                     // lightness > 10 && lightness < 90;
  235.                    
  236.                     is_orange =  hue > 0 && hue < 35 && //hue > 355 ||
  237.                         saturation > 70; //default 20
  238.                     // lightness > 10 && lightness < 90;
  239.                    
  240.                    
  241.                    
  242.                     //hier worden de hoeveelheid groene puntjes berekend
  243.                     // If hue is green
  244.                     // And value is still valid
  245.                     if (is_blue && row[x_grid] < 255) begin
  246.                         // Increase by one
  247.         //              row[x_grid] = 20; //default ++
  248.                         row[x_grid]++;
  249.                     end
  250. //                  if (is_green && row[x_grid] < 255) begin
  251. //                      // Increase by one
  252. //      //              row[x_grid] = 20; //default ++
  253. //                      row[x_grid]++;
  254. //                  end
  255. //                  if (is_red && row[x_grid] < 255) begin
  256. //                      // Increase by one
  257. //      //              row[x_grid] = 20; //default ++
  258. //                      row[x_grid]++;
  259. //                  end
  260.                 end
  261.  
  262.                 R_OUT <= 0;
  263.                 G_OUT <= 0;
  264.                 B_OUT <= 0;
  265.  
  266.                 if(ENABLE1 && y % block_size < FONT_HEIGHT && x_grid > 0 && x_grid < cols - 1) begin
  267.                     shortint unsigned x_tgt, block_x, block_y, dig, x_pos, y_pos;
  268.  
  269.                     // Calculate the start x/y of the block
  270.                     block_x = x_grid * block_size + 1;
  271.                     block_y = y_grid * block_size;
  272.  
  273.                     for (int i = 0; i < 3; i++) begin
  274.                         x_tgt = block_x + i * (FONT_WIDTH + MARGIN); //debug letters
  275.                         if (x >= x_tgt && x < (x_tgt + FONT_WIDTH)) begin
  276.                             byte unsigned x_pos, y_pos;
  277.  
  278.                             x_pos = (x - x_tgt);
  279.                             y_pos = (y - block_y);
  280.  
  281.                             case (i)
  282.                                 0 : dig = row[x_grid] % 1000 / 100;
  283.                                 1 : dig = row[x_grid] % 100 / 10;
  284.                                 2 : dig = row[x_grid] % 10;
  285.                             endcase
  286.  
  287.                             if (font_pixel(dig, FONT_WIDTH - 1 - x_pos, FONT_HEIGHT - 1 - y_pos)) begin
  288.                                 R_OUT <= 255;
  289.                                 G_OUT <= 255;
  290.                                 B_OUT <= 255;
  291.                             end
  292.                         end
  293.                     end
  294.                 end else if(BALL_X == x_grid && BALL_Y == y_grid && 1 == 0) begin
  295.                     // Show the hotbox
  296.                     R_OUT <= 0;
  297.                     G_OUT <= 255;
  298.                     B_OUT <= 0;
  299.                 end else if(BALL_X1 == x_grid && BALL_Y1 == y_grid && 1==0) begin
  300.                     // Show the hotbox
  301.                     R_OUT <= 0;
  302.                     G_OUT <= 0;
  303.                     B_OUT <= 255;
  304.                 end else if(ENABLE4 && is_green) begin  //end else if(ENABLE2 && is_green) begin
  305.                     // Show all the greens with hue 100 > hue < 140
  306.                     R_OUT <= 0;
  307.                     G_OUT <= 255;
  308.                     B_OUT <= 0;
  309.                 end else if(ENABLE5 && is_blue) begin   //end else if(ENABLE5 && is_blue) begin
  310.                     // Show all the greens with hue 100 > hue < 140
  311.                     R_OUT <= 0;
  312.                     G_OUT <= 0;
  313.                     B_OUT <= 255;
  314.                 end else if(ENABLE6 && is_red) begin
  315.                     // Show all the greens with hue 100 > hue < 140
  316.                     R_OUT <= 255;
  317.                     G_OUT <= 0;
  318.                     B_OUT <= 0;
  319.                 end else if(ENABLE7 && is_white) begin
  320.                     // Show all the greens with hue 100 > hue < 140
  321.                     R_OUT <= 0;
  322.                     G_OUT <= 0;
  323.                     B_OUT <= 0;
  324.                 end else if(ENABLE8 && is_yellow) begin
  325.                     // Show all the greens with hue 100 > hue < 140
  326.                     R_OUT <= 255;
  327.                     G_OUT <= 255;
  328.                     B_OUT <= 0;
  329.                 end else if(ENABLE9 && is_orange) begin
  330.                     // Show all the greens with hue 100 > hue < 140
  331.                     R_OUT <= 255;
  332.                     G_OUT <= 150;
  333.                     B_OUT <= 0;
  334.                 end else if(~ENABLE3) begin
  335.                     R_OUT <= R_IN;
  336.                     G_OUT <= G_IN;
  337.                     B_OUT <= B_IN;
  338.                 end else if(x == 100 && y == 100) begin //
  339.                     if(is_green || ~ENABLE17)   begin
  340.                         R_OUT <= 255;
  341.                         G_OUT <= 0;
  342.                         B_OUT <= 220;
  343.                         is_greenblock = 1;
  344.                     end
  345.                 end else if (x == 100 && y == 100) begin
  346.                     if (~is_green || ~ENABLE17) begin
  347.                         is_greenblock = 0;
  348.                     end
  349.                 end else if (is_greenblock == 0 && x < 300 && x > 200 && y < 200 && y > 100) begin
  350.                     R_OUT <= 255;
  351.                     G_OUT <= 0;
  352.                     B_OUT <= 0;
  353.                 end else if (is_greenblock == 1 && x < 300 && x > 200 && y < 200 && y > 100) begin
  354.                     R_OUT <= 0;
  355.                     G_OUT <= 0;
  356.                     B_OUT <= 255;
  357.                 end
  358.  
  359.  
  360.                 // If this pixel is the last of the row
  361.                 // (x == max of screen and y == on last pixel of row)
  362.                 if(x == VGA_WIDTH - 1 && y % block_size == block_size - 1) begin
  363.                     max_count = 0;
  364.  
  365.                     // For every value in the row
  366.                     // HIER VALLLEN DE ACTIEVE COLUMNS AAN TE PASSEN
  367.                     for (int for_x = 0; for_x < cols; for_x++) begin //                 for (int for_x = 0; for_x < cols; for_x++) begin
  368.                         // If it's bigger than the previous found one
  369.                         if(row[for_x] > max_count &&
  370.                             row[for_x] > (block_size * block_size / 2)) begin
  371.                             // Store it
  372.                             max_count = row[for_x];
  373.                             max_x     = for_x;
  374.                         end
  375.  
  376.                         row[for_x] = 0; // Always reset the cell
  377.                     end
  378.  
  379.                     max_count_per_row[y_grid] = max_count;
  380.                     max_x_per_row[y_grid]     = max_x;
  381.  
  382.                     // On the last row
  383.                     if(y_grid == rows - 1) begin
  384.                         max_count = 0;
  385.  
  386.                         for (int for_y = 0; for_y < rows; for_y++) begin
  387.                             if(max_count_per_row[for_y] > max_count) begin
  388.                                 max_count = max_count_per_row[for_y];
  389.                                 max_y     = for_y;
  390.                                 max_x     = max_x_per_row[for_y];
  391.                             end
  392.                         end
  393.  
  394.                         // Set the new hotbox for the ball
  395.                         BALL_X = max_x;
  396.                         BALL_Y = max_y;
  397.                     end
  398.                 end
  399.                            
  400.     //          // If this pixel is the last of the row
  401.     //          // (x == max of screen and y == on last pixel of row)
  402.     //          if(x == VGA_WIDTH - 1 && y % block_size == block_size - 1) begin
  403.     //              max_count = 0;
  404.     //
  405.     //              // For every value in the row
  406.     //              // HIER VALLLEN DE ACTIEVE COLUMNS AAN TE PASSEN
  407.     //              for (int for_x = 10; for_x < 20; for_x++) begin //              for (int for_x = 0; for_x < cols; for_x++) begin
  408.     //                  // If it's bigger than the previous found one
  409.     //                  if(row[for_x] > max_count &&
  410.     //                      row[for_x] > (block_size * block_size / 2)) begin
  411.     //                      // Store it
  412.     //                      max_count = row[for_x];
  413.     //                      max_x     = for_x;
  414.     //                  end
  415.     //
  416.     //                  row[for_x] = 0; // Always reset the cell
  417.     //              end
  418.     //
  419.     //              max_count_per_row[y_grid] = max_count;
  420.     //              max_x_per_row[y_grid]     = max_x;
  421.     //
  422.     //              // On the last row
  423.     //              if(y_grid == rows - 1) begin
  424.     //                  max_count = 0;
  425.     //
  426.     //                  for (int for_y = 0; for_y < rows; for_y++) begin
  427.     //                      if(max_count_per_row[for_y] > max_count) begin
  428.     //                          max_count = max_count_per_row[for_y];
  429.     //                          max_y     = for_y;
  430.     //                          max_x     = max_x_per_row[for_y];
  431.     //                      end
  432.     //                  end
  433.     //
  434.     //                  // Set the new hotbox for the ball
  435.     //                  BALL_X = max_x;
  436.     //                  BALL_Y = max_y;
  437.     //              end
  438.     //          end
  439.             end else begin
  440.                 R_OUT <= 255;
  441.                 G_OUT <= 255;
  442.                 B_OUT <= 255;
  443.                
  444.             end
  445.         end
  446.     end
  447. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement