www.pudn.com > Verilog_SOM.rar > som.v, change:2006-09-01,size:44696b


///////////////////////////////////////////////////////////////////////////////////// 
//$DATE 2006/08/28 
//$ATTHOR 
////////////////////////////////////////////////////////////////////////////////////// 
 
`include "define.v" 
`include "220model.v" 
`include "get_max6x1.v" 
///////////////////////////////////////////////////////////////////////////////////// 
module     som(  //input 
                   clk, 
                   init_wts, 
                   start_train, 
                   load_x, 
                   X1, 
                   X2, 
                   X3, 
                 //outputs 
                   single_data_finish, 
                   frame_finish, 
                   pos_x, 
                   pos_y, 
                   test 
               ); 
input                                clk, 
                                     init_wts, 
                                     start_train, 
                                     load_x; 
input      [`BIT_WIDTH_DATA-1:0]     X1, 
                                     X2, 
                                     X3; 
output                               single_data_finish; 
output                               frame_finish; 
output     [`BIT_WIDTH_DATA-1:0]     pos_x, 
                                     pos_y; 
reg                                  single_data_finish, 
                                     frame_finish; 
wire       [2:0]                     winner_x, 
                                     winner_y; 
reg        [3:0]                     NC;                      //SOM算法中的邻域函数 
reg        [10:0]                    lammda;                  //SOM算法学习因子 
reg        [3:0]                     Nc; 
reg        [10:0]                    lam; 
//reg        [2:0]                     p_x,p_y; 
output     [`BIT_WIDTH_DISTANCE-1:0] test;                                                                       
reg        [`BIT_WIDTH_DISTANCE-1:0]  distance[`AREA_NUM-1:0]; 
reg        [`BIT_WIDTH_DATA-1:0]      weights1[`AREA_NUM-1:0]; 
reg        [`BIT_WIDTH_DATA-1:0]      weights2[`AREA_NUM-1:0]; 
reg        [`BIT_WIDTH_DATA-1:0]      weights3[`AREA_NUM-1:0]; 
reg        [5:0]                  state1,state2,state3; 
reg        [2:0]                  state; 
reg        [9:0]                  count_num; 
 
reg        [`BIT_WIDTH_DATA:0]        data_temp_b1,data_temp_b2,data_temp_b3;  
 
wire       [`BIT_WIDTH_DISTANCE-1:0]  distance_temp1,distance_temp2,distance_temp3; 
reg        [`BIT_WIDTH_DATA:0]        wts_temp1,wts_temp2,wts_temp3; 
wire       [`BIT_WIDTH_DATA-1:0]        deta1,deta2,deta3;   
integer file_p;  
integer i,j;                               
///////////////////////////////// 
initial  
  begin 
    file_p=$fopen("wts.dat"); 
    #6000000 $fclose(file_p); 
  end 
assign test=state;//distance[35];//{winner_x,winner_y};//{winner_x,winner_y};//distance[1];   
//////////////////////////////////////////// 
lpm_mult mul_distance1( 
                .dataa(data_temp_b1), 
                .datab(data_temp_b1), 
                .result(distance_temp1) 
                            ); 
defparam mul_distance1.lpm_widtha=`BIT_WIDTH_DATA+1; 
defparam mul_distance1.lpm_widthb=`BIT_WIDTH_DATA+1; 
defparam mul_distance1.lpm_widthp=`BIT_WIDTH_DISTANCE; 
defparam mul_distance1.lpm_widths=2*(`BIT_WIDTH_DATA+1); 
defparam mul_distance1.lpm_representation="SIGNED"; 
 
lpm_mult mul_distance2( 
                .dataa(data_temp_b2), 
                .datab(data_temp_b2), 
                .result(distance_temp2) 
                            ); 
defparam mul_distance2.lpm_widtha=`BIT_WIDTH_DATA+1; 
defparam mul_distance2.lpm_widthb=`BIT_WIDTH_DATA+1; 
defparam mul_distance2.lpm_widthp=`BIT_WIDTH_DISTANCE; 
defparam mul_distance2.lpm_widths=2*(`BIT_WIDTH_DATA+1); 
defparam mul_distance2.lpm_representation="SIGNED"; 
 
lpm_mult mul_distance3( 
                .dataa(data_temp_b3), 
                .datab(data_temp_b3), 
                .result(distance_temp3) 
                            ); 
defparam mul_distance3.lpm_widtha=`BIT_WIDTH_DATA+1; 
defparam mul_distance3.lpm_widthb=`BIT_WIDTH_DATA+1; 
defparam mul_distance3.lpm_widthp=`BIT_WIDTH_DISTANCE; 
defparam mul_distance3.lpm_widths=2*(`BIT_WIDTH_DATA+1); 
defparam mul_distance3.lpm_representation="SIGNED"; 
 
//////////////////////////////// 
wire         [2:0]    index1,index2,index3,index4,index5,index6; 
wire         [`BIT_WIDTH_DATA-1:0]    D1,D2,D3,D4,D5,D6; 
min6x1 A1(                                                             //6X6中取最小值 
               //output 
               .index(index1), 
               .D(D1), 
               //inputs 
               .D1(distance[0]), 
               .D2(distance[1]), 
               .D3(distance[2]), 
               .D4(distance[3]), 
               .D5(distance[4]), 
               .D6(distance[5]) 
              ); 
min6x1 A2( 
               //output 
               .index(index2), 
               .D(D2), 
               //inputs 
               .D1(distance[6]), 
               .D2(distance[7]), 
               .D3(distance[8]), 
               .D4(distance[9]), 
               .D5(distance[10]), 
               .D6(distance[11]) 
              ); 
min6x1 A3( 
               //output 
               .index(index3), 
               .D(D3), 
               //inputs 
               .D1(distance[12]), 
               .D2(distance[13]), 
               .D3(distance[14]), 
               .D4(distance[15]), 
               .D5(distance[16]), 
               .D6(distance[17]) 
              ); 
min6x1 A4( 
               //output 
               .index(index4), 
               .D(D4), 
               //inputs 
               .D1(distance[18]), 
               .D2(distance[19]), 
               .D3(distance[20]), 
               .D4(distance[21]), 
               .D5(distance[22]), 
               .D6(distance[23]) 
              ); 
min6x1 A5( 
               //output 
               .index(index5), 
               .D(D5), 
               //inputs 
               .D1(distance[24]), 
               .D2(distance[25]), 
               .D3(distance[26]), 
               .D4(distance[27]), 
               .D5(distance[28]), 
               .D6(distance[29]) 
              ); 
min6x1 A6( 
               //output 
               .index(index6), 
               .D(D6), 
               //inputs 
               .D1(distance[30]), 
               .D2(distance[31]), 
               .D3(distance[32]), 
               .D4(distance[33]), 
               .D5(distance[34]), 
               .D6(distance[35]) 
              ); 
min6x1_with_index A7(  //outputs 
                           .winner_x(winner_x), 
                           .winner_y(winner_y), 
                           //inputs 
                           .D1(D1), 
                           .D2(D2), 
                           .D3(D3), 
                           .D4(D4), 
                           .D5(D5), 
                           .D6(D6), 
                           .index1(index1), 
                           .index2(index2), 
                           .index3(index3), 
                           .index4(index4), 
                           .index5(index5), 
                           .index6(index6) 
                         );   
/////////////////////////////////////////////////////// 
wire         [`BIT_WIDTH_DATA-1:0]    D1_x,D2_x,D3_x,D4_x,D5_x,D6_x; 
wire         [`BIT_WIDTH_DATA-1:0]    D1_y,D2_y,D3_y,D4_y,D5_y,D6_y; 
wire         [`BIT_WIDTH_DATA-1:0]    D1_z,D2_z,D3_z,D4_z,D5_z,D6_z; 
max6x1 B1( 
           //output 
          .D(D1_z), 
          .D_x(D1_x), 
          .D_y(D1_y), 
          //inputs 
           .D1(weights1[0]), 
           .D1_x(weights2[0]), 
           .D1_y(weights3[0]), 
           .D2(weights1[1]), 
           .D2_x(weights2[1]), 
           .D2_y(weights3[1]), 
           .D3(weights1[2]), 
           .D3_x(weights2[2]), 
           .D3_y(weights3[2]), 
           .D4(weights1[3]), 
           .D4_x(weights2[3]), 
           .D4_y(weights3[3]), 
           .D5(weights1[4]), 
           .D5_x(weights2[4]), 
           .D5_y(weights3[4]), 
           .D6(weights1[5]), 
           .D6_x(weights2[5]), 
           .D6_y(weights3[5]) 
           ); 
max6x1 B2( 
           //output 
          .D(D2_z), 
          .D_x(D2_x), 
          .D_y(D2_y), 
          //inputs 
           .D1(weights1[6]), 
           .D1_x(weights2[6]), 
           .D1_y(weights3[6]), 
           .D2(weights1[7]), 
           .D2_x(weights2[7]), 
           .D2_y(weights3[7]), 
           .D3(weights1[8]), 
           .D3_x(weights2[8]), 
           .D3_y(weights3[8]), 
           .D4(weights1[9]), 
           .D4_x(weights2[9]), 
           .D4_y(weights3[9]), 
           .D5(weights1[10]), 
           .D5_x(weights2[10]), 
           .D5_y(weights3[10]), 
           .D6(weights1[11]), 
           .D6_x(weights2[11]), 
           .D6_y(weights3[11]) 
           ); 
max6x1 B3( 
           //output 
          .D(D3_z), 
          .D_x(D3_x), 
          .D_y(D3_y), 
          //inputs 
           .D1(weights1[12]), 
           .D1_x(weights2[12]), 
           .D1_y(weights3[12]), 
           .D2(weights1[13]), 
           .D2_x(weights2[13]), 
           .D2_y(weights3[13]), 
           .D3(weights1[14]), 
           .D3_x(weights2[14]), 
           .D3_y(weights3[14]), 
           .D4(weights1[15]), 
           .D4_x(weights2[15]), 
           .D4_y(weights3[15]), 
           .D5(weights1[16]), 
           .D5_x(weights2[16]), 
           .D5_y(weights3[16]), 
           .D6(weights1[17]), 
           .D6_x(weights2[17]), 
           .D6_y(weights3[17]) 
           ); 
max6x1 B4( 
           //output 
          .D(D4_z), 
          .D_x(D4_x), 
          .D_y(D4_y), 
          //inputs 
           .D1(weights1[18]), 
           .D1_x(weights2[18]), 
           .D1_y(weights3[18]), 
           .D2(weights1[19]), 
           .D2_x(weights2[19]), 
           .D2_y(weights3[19]), 
           .D3(weights1[20]), 
           .D3_x(weights2[20]), 
           .D3_y(weights3[20]), 
           .D4(weights1[21]), 
           .D4_x(weights2[21]), 
           .D4_y(weights3[21]), 
           .D5(weights1[22]), 
           .D5_x(weights2[22]), 
           .D5_y(weights3[22]), 
           .D6(weights1[23]), 
           .D6_x(weights2[23]), 
           .D6_y(weights3[23]) 
           ); 
max6x1 B5( 
           //output 
          .D(D5_z), 
          .D_x(D5_x), 
          .D_y(D5_y), 
          //inputs 
           .D1(weights1[24]), 
           .D1_x(weights2[24]), 
           .D1_y(weights3[24]), 
           .D2(weights1[25]), 
           .D2_x(weights2[25]), 
           .D2_y(weights3[25]), 
           .D3(weights1[26]), 
           .D3_x(weights2[26]), 
           .D3_y(weights3[26]), 
           .D4(weights1[27]), 
           .D4_x(weights2[27]), 
           .D4_y(weights3[27]), 
           .D5(weights1[28]), 
           .D5_x(weights2[28]), 
           .D5_y(weights3[28]), 
           .D6(weights1[29]), 
           .D6_x(weights2[29]), 
           .D6_y(weights3[29]) 
           ); 
max6x1 B6( 
           //output 
          .D(D6_z), 
          .D_x(D6_x), 
          .D_y(D6_y), 
          //inputs 
           .D1(weights1[30]), 
           .D1_x(weights2[30]), 
           .D1_y(weights3[30]), 
           .D2(weights1[31]), 
           .D2_x(weights2[31]), 
           .D2_y(weights3[31]), 
           .D3(weights1[32]), 
           .D3_x(weights2[32]), 
           .D3_y(weights3[32]), 
           .D4(weights1[33]), 
           .D4_x(weights2[33]), 
           .D4_y(weights3[33]), 
           .D5(weights1[34]), 
           .D5_x(weights2[34]), 
           .D5_y(weights3[34]), 
           .D6(weights1[35]), 
           .D6_x(weights2[35]), 
           .D6_y(weights3[35]) 
           ); 
max6x1_without_index B7( 
               //output 
               .D_x(pos_x), 
               .D_y(pos_y), 
               //inputs 
               .D1(D1_z), 
               .D1_x(D1_x), 
               .D1_y(D1_y), 
               .D2(D2_z), 
               .D2_x(D2_x), 
               .D2_y(D2_y), 
               .D3(D3_z), 
               .D3_x(D3_x), 
               .D3_y(D3_y), 
               .D4(D4_z), 
               .D4_x(D4_x), 
               .D4_y(D4_y), 
               .D5(D5_z), 
               .D5_x(D5_x), 
               .D5_y(D5_y), 
               .D6(D6_z), 
               .D6_x(D6_x), 
               .D6_y(D6_y) 
              ); 
/////////////////////////////////////////////////////// 
wire          check; 
reg    [2:0]  ii,jj; 
always 
@(posedge clk or posedge load_x)                                     //初始化权值 
  begin 
    if(init_wts) 
    begin 
      NC<=12; 
      lammda<=11'b01111111111; 
      frame_finish<=0; 
      count_num<=0; 
      state<=3'b001; 
      state1<=0; 
    end 
    else if(load_x&&state==3'b001) 
     begin 
      case(state1)  
         0:begin  
           state1<=1; 
           single_data_finish<=0; 
           weights1[0]<=X1;weights2[0]<=X2;weights3[0]<=X3; 
           end 
         1:begin  
           state1<=2; 
           weights1[1]<=X1;weights2[1]<=X2;weights3[1]<=X3; 
           end 
         2:begin  
           state1<=3; 
           //single_data_finish<=1; 
           weights1[2]<=X1;weights2[2]<=X2;weights3[2]<=X3; 
           end 
         3:begin  
           state1<=4; 
           weights1[3]<=X1;weights2[3]<=X2;weights3[3]<=X3; 
           end 
         4:begin  
           state1<=5; 
           weights1[4]<=X1;weights2[4]<=X2;weights3[4]<=X3; 
           end 
         5:begin  
           state1<=6; 
           weights1[5]<=X1;weights2[5]<=X2;weights3[5]<=X3; 
           end 
         6:begin  
           state1<=7; 
           weights1[6]<=X1;weights2[6]<=X2;weights3[6]<=X3; 
           end 
         7:begin  
           state1<=8; 
           weights1[7]<=X1;weights2[7]<=X2;weights3[7]<=X3; 
           end 
         8:begin  
           state1<=9; 
           weights1[8]<=X1;weights2[8]<=X2;weights3[8]<=X3; 
           end 
         9:begin  
           state1<=10; 
           weights1[9]<=X1;weights2[9]<=X2;weights3[9]<=X3; 
           end 
         10:begin  
           state1<=11; 
           weights1[10]<=X1;weights2[10]<=X2;weights3[10]<=X3; 
           end 
         11:begin  
           state1<=12; 
           weights1[11]<=X1;weights2[11]<=X2;weights3[11]<=X3; 
           end 
         12:begin  
           state1<=13; 
           weights1[12]<=X1;weights2[12]<=X2;weights3[12]<=X3; 
           end 
         13:begin  
           state1<=14; 
           weights1[13]<=X1;weights2[13]<=X2;weights3[13]<=X3; 
           end 
         14:begin  
           state1<=15; 
           weights1[14]<=X1;weights2[14]<=X2;weights3[14]<=X3; 
           end 
         15:begin  
           state1<=16; 
           weights1[15]<=X1;weights2[15]<=X2;weights3[15]<=X3; 
           end 
         16:begin  
           state1<=17; 
           weights1[16]<=X1;weights2[16]<=X2;weights3[16]<=X3; 
           end 
         17:begin  
           state1<=18; 
           weights1[17]<=X1;weights2[17]<=X2;weights3[17]<=X3; 
           end 
         18:begin  
           state1<=19; 
           weights1[18]<=X1;weights2[18]<=X2;weights3[18]<=X3; 
           end 
         19:begin  
           state1<=20; 
           weights1[19]<=X1;weights2[19]<=X2;weights3[19]<=X3; 
           end 
         20:begin  
           state1<=21; 
           weights1[20]<=X1;weights2[20]<=X2;weights3[20]<=X3; 
           end 
         21:begin  
           state1<=22; 
           weights1[21]<=X1;weights2[21]<=X2;weights3[21]<=X3; 
           end 
         22:begin  
           state1<=23; 
           weights1[22]<=X1;weights2[22]<=X2;weights3[22]<=X3; 
           end 
         23:begin  
           state1<=24; 
           weights1[23]<=X1;weights2[23]<=X2;weights3[23]<=X3; 
           end 
         24:begin  
           state1<=25; 
           weights1[24]<=X1;weights2[24]<=X2;weights3[24]<=X3; 
           end 
         25:begin  
           state1<=26; 
           weights1[25]<=X1;weights2[25]<=X2;weights3[25]<=X3; 
           end 
         26:begin  
           state1<=27; 
           weights1[26]<=X1;weights2[26]<=X2;weights3[26]<=X3; 
           end 
         27:begin  
           state1<=28; 
           weights1[27]<=X1;weights2[27]<=X2;weights3[27]<=X3; 
           end 
         28:begin  
           state1<=29; 
           weights1[28]<=X1;weights2[28]<=X2;weights3[28]<=X3; 
           end 
         29:begin  
           state1<=30; 
           weights1[29]<=X1;weights2[29]<=X2;weights3[29]<=X3; 
           end 
         30:begin  
           state1<=31; 
            
           weights1[30]<=X1;weights2[30]<=X2;weights3[30]<=X3; 
           end 
         31:begin  
           state1<=32; 
           weights1[31]<=X1;weights2[31]<=X2;weights3[31]<=X3; 
           end 
         32:begin  
           state1<=33; 
           weights1[32]<=X1;weights2[32]<=X2;weights3[32]<=X3; 
           end 
         33:begin  
           state1<=34; 
           weights1[33]<=X1;weights2[33]<=X2;weights3[33]<=X3; 
           end 
         34:begin  
           state1<=35; 
           weights1[34]<=X1;weights2[34]<=X2;weights3[34]<=X3; 
           end 
         35:begin  
           state1<=36; 
           state<=3'b010; 
           single_data_finish<=1; 
           weights1[35]<=X1;weights2[35]<=X2;weights3[35]<=X3; 
           end 
 
       default: 
           state1<=0; 
      endcase 
    end 
  else if(start_train&&load_x&&state==3'b010) 
    begin 
        
       single_data_finish<=0; 
       if(count_num==1000) 
        begin 
        frame_finish<=1; 
        state<=3'b000; 
        NC<=12; 
        lammda<=1023; 
        count_num<=0; 
        end 
       else 
        begin 
          state<=3'b011; 
          lammda<=lammda-1; 
          NC<=(NC>1)? NC-1:1; 
      
          count_num<=count_num+1; 
      end 
      //state2<=0; 
    end 
  else if(state==3'b011)           //计算距离 
    begin   
      case(state2) 
        0:begin 
             state2<=1; 
             data_temp_b1<=X1-weights1[0];data_temp_b2<=X2-weights2[0];data_temp_b3<=X3-weights3[0]; 
          end 
        1:begin 
             state2<=2; 
             data_temp_b1<=X1-weights1[1];data_temp_b2<=X2-weights2[1];data_temp_b3<=X3-weights3[1]; 
             distance[0]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        2:begin 
             state2<=3; 
             data_temp_b1<=X1-weights1[2];data_temp_b2<=X2-weights2[2];data_temp_b3<=X3-weights3[2]; 
             distance[1]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        3:begin 
             state2<=4; 
             data_temp_b1<=X1-weights1[3];data_temp_b2<=X2-weights2[3];data_temp_b3<=X3-weights3[3]; 
             distance[2]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        4:begin 
             state2<=5; 
             data_temp_b1<=X1-weights1[4];data_temp_b2<=X2-weights2[4];data_temp_b3<=X3-weights3[4]; 
             distance[3]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        5:begin 
             state2<=6; 
             data_temp_b1<=X1-weights1[5];data_temp_b2<=X2-weights2[5];data_temp_b3<=X3-weights3[5]; 
             distance[4]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        6:begin 
             state2<=7; 
             data_temp_b1<=X1-weights1[6];data_temp_b2<=X2-weights2[6];data_temp_b3<=X3-weights3[6]; 
             distance[5]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        7:begin 
             state2<=8; 
             data_temp_b1<=X1-weights1[7];data_temp_b2<=X2-weights2[7];data_temp_b3<=X3-weights3[7]; 
             distance[6]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        8:begin 
             state2<=9; 
             data_temp_b1<=X1-weights1[8];data_temp_b2<=X2-weights2[8];data_temp_b3<=X3-weights3[8]; 
             distance[7]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        9:begin 
             state2<=10; 
             data_temp_b1<=X1-weights1[9];data_temp_b2<=X2-weights2[9];data_temp_b3<=X3-weights3[9]; 
             distance[8]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        10:begin 
             state2<=11; 
             data_temp_b1<=X1-weights1[10];data_temp_b2<=X2-weights2[10];data_temp_b3<=X3-weights3[10]; 
             distance[9]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        11:begin 
             state2<=12; 
             data_temp_b1<=X1-weights1[11];data_temp_b2<=X2-weights2[11];data_temp_b3<=X3-weights3[11]; 
             distance[10]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        12:begin 
             state2<=13; 
             data_temp_b1<=X1-weights1[12];data_temp_b2<=X2-weights2[12];data_temp_b3<=X3-weights3[12]; 
             distance[11]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        13:begin 
             state2<=14; 
             data_temp_b1<=X1-weights1[13];data_temp_b2<=X2-weights2[13];data_temp_b3<=X3-weights3[13]; 
             distance[12]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        14:begin 
             state2<=15; 
             data_temp_b1<=X1-weights1[14];data_temp_b2<=X2-weights2[14];data_temp_b3<=X3-weights3[14]; 
             distance[13]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        15:begin 
             state2<=16; 
             data_temp_b1<=X1-weights1[15];data_temp_b2<=X2-weights2[15];data_temp_b3<=X3-weights3[15]; 
             distance[14]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        16:begin 
             state2<=17; 
             data_temp_b1<=X1-weights1[16];data_temp_b2<=X2-weights2[16];data_temp_b3<=X3-weights3[16]; 
             distance[15]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        17:begin 
             state2<=18; 
             data_temp_b1<=X1-weights1[17];data_temp_b2<=X2-weights2[17];data_temp_b3<=X3-weights3[17]; 
             distance[16]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        18:begin 
             state2<=19; 
             data_temp_b1<=X1-weights1[18];data_temp_b2<=X2-weights2[18];data_temp_b3<=X3-weights3[18]; 
             distance[17]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        19:begin 
             state2<=20; 
             data_temp_b1<=X1-weights1[19];data_temp_b2<=X2-weights2[19];data_temp_b3<=X3-weights3[19]; 
             distance[18]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        20:begin 
             state2<=21; 
             data_temp_b1<=X1-weights1[20];data_temp_b2<=X2-weights2[20];data_temp_b3<=X3-weights3[20]; 
             distance[19]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        21:begin 
             state2<=22; 
             data_temp_b1<=X1-weights1[21];data_temp_b2<=X2-weights2[21];data_temp_b3<=X3-weights3[21]; 
             distance[20]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        22:begin 
             state2<=23; 
             data_temp_b1<=X1-weights1[22];data_temp_b2<=X2-weights2[22];data_temp_b3<=X3-weights3[22]; 
             distance[21]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        23:begin 
             state2<=24; 
             data_temp_b1<=X1-weights1[23];data_temp_b2<=X2-weights2[23];data_temp_b3<=X3-weights3[23]; 
             distance[22]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        24:begin 
             state2<=25; 
             data_temp_b1<=X1-weights1[24];data_temp_b2<=X2-weights2[24];data_temp_b3<=X3-weights3[24]; 
             distance[23]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        25:begin 
             state2<=26; 
             data_temp_b1<=X1-weights1[25];data_temp_b2<=X2-weights2[25];data_temp_b3<=X3-weights3[25]; 
             distance[24]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        26:begin 
             state2<=27; 
             data_temp_b1<=X1-weights1[26];data_temp_b2<=X2-weights2[26];data_temp_b3<=X3-weights3[26]; 
             distance[25]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        27:begin 
             state2<=28; 
             data_temp_b1<=X1-weights1[27];data_temp_b2<=X2-weights2[27];data_temp_b3<=X3-weights3[27]; 
             distance[26]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        28:begin 
             state2<=29; 
             data_temp_b1<=X1-weights1[28];data_temp_b2<=X2-weights2[28];data_temp_b3<=X3-weights3[28]; 
             distance[27]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        29:begin 
             state2<=30; 
             data_temp_b1<=X1-weights1[29];data_temp_b2<=X2-weights2[29];data_temp_b3<=X3-weights3[29]; 
             distance[28]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        30:begin 
             state2<=31; 
             data_temp_b1<=X1-weights1[30];data_temp_b2<=X2-weights2[30];data_temp_b3<=X3-weights3[30]; 
             distance[29]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        31:begin 
             state2<=32; 
             data_temp_b1<=X1-weights1[31];data_temp_b2<=X2-weights2[31];data_temp_b3<=X3-weights3[31]; 
             distance[30]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        32:begin 
             state2<=33; 
             data_temp_b1<=X1-weights1[32];data_temp_b2<=X2-weights2[32];data_temp_b3<=X3-weights3[32]; 
             distance[31]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        33:begin 
             state2<=34; 
             data_temp_b1<=X1-weights1[33];data_temp_b2<=X2-weights2[33];data_temp_b3<=X3-weights3[33]; 
             distance[32]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        34:begin 
             state2<=35; 
             data_temp_b1<=X1-weights1[34];data_temp_b2<=X2-weights2[34];data_temp_b3<=X3-weights3[34]; 
             distance[33]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        35:begin 
             state2<=36; 
             data_temp_b1<=X1-weights1[35];data_temp_b2<=X2-weights2[35];data_temp_b3<=X3-weights3[35]; 
             distance[34]<=distance_temp1+distance_temp2+distance_temp3; 
          end 
        36:begin 
             state2<=37; 
             state<=3'b100; 
             Nc<=NC; 
             lam<=lammda; 
             distance[35]<=distance_temp1+distance_temp2+distance_temp3; 
           end 
        default: 
           state2<=0; 
      endcase 
    end 
   else if(state==3'b100) 
     begin   
      for(i=0;i<6;i=i+1) 
       begin 
         for(j=0;j<6;j=j+1) 
          begin 
            $fwrite(file_p,"%3d  ",weights1[6*i+j]); 
          end 
        $fwrite(file_p,"\n "); 
       end 
       $fwrite(file_p,"\n "); 
     for(i=0;i<6;i=i+1) 
       begin 
         for(j=0;j<6;j=j+1) 
          begin 
            $fwrite(file_p,"%3d  ",weights2[6*i+j]); 
          end 
        $fwrite(file_p,"\n "); 
       end 
       $fwrite(file_p,"\n "); 
    for(i=0;i<6;i=i+1) 
       begin 
         for(j=0;j<6;j=j+1) 
          begin 
            $fwrite(file_p,"%3d  ",weights3[6*i+j]); 
          end 
        $fwrite(file_p,"\n "); 
       end 
       $fwrite(file_p,"\n "); 
        for(i=0;i<6;i=i+1) 
       begin 
         for(j=0;j<6;j=j+1) 
          begin 
            $fwrite(file_p,"%3d  ",distance[6*i+j]); 
          end 
        $fwrite(file_p,"\n "); 
       end 
       $fwrite(file_p,"\n "); 
     $fwrite(file_p,"lam=%3d ,Nc=%3d,winner_x=%d,winner_y=%d,X1=%d,X2=%d,X3=%d ,deta1=%d\n ",lam,Nc,winner_x,winner_y,X1,X2,X3,deta1); 
       if(Nc==0)                           //开始训练 
         begin 
           single_data_finish<=1; 
           state<=3'b010; 
         end 
       else 
         begin 
           state<=3'b101; 
           lam<=(lam>1)? lam>>1:1; 
        end 
      end 
   else if(state==3'b101) 
     begin 
      
         case(state3) 
           0:begin 
               state3<=1; 
               ii<=1;jj<=1; 
               wts_temp1<=X1-weights1[0];wts_temp2<=X2-weights2[0];wts_temp3<=X3-weights3[0]; 
             end 
           1:begin 
               state3<=2; 
               ii<=1;jj<=2; 
               wts_temp1<=X1-weights1[1];wts_temp2<=X2-weights2[1];wts_temp3<=X3-weights3[1]; 
               if(check) 
                 begin 
                   weights1[0]<=weights1[0]+deta1;weights2[0]<=weights2[0]+deta2;weights3[0]<=weights3[0]+deta3; 
                 end 
               end 
           2:begin 
               state3<=3; 
               ii<=1;jj<=3; 
               wts_temp1<=X1-weights1[2];wts_temp2<=X2-weights2[2];wts_temp3<=X3-weights3[2]; 
               if(check) 
                 begin 
                   weights1[1]<=weights1[1]+deta1;weights2[1]<=weights2[1]+deta2;weights3[1]<=weights3[1]+deta3; 
                 end 
               end 
           3:begin 
               state3<=4; 
               ii<=1;jj<=4; 
               wts_temp1<=X1-weights1[3];wts_temp2<=X2-weights2[3];wts_temp3<=X3-weights3[3]; 
               if(check) 
                 begin 
                   weights1[2]<=weights1[2]+deta1;weights2[2]<=weights2[2]+deta2;weights3[2]<=weights3[2]+deta3; 
                 end 
               end 
           4:begin 
               state3<=5; 
               ii<=1;jj<=5; 
               wts_temp1<=X1-weights1[4];wts_temp2<=X2-weights2[4];wts_temp3<=X3-weights3[4]; 
               if(check) 
                 begin 
                   weights1[3]<=weights1[3]+deta1;weights2[3]<=weights2[3]+deta2;weights3[3]<=weights3[3]+deta3; 
                 end 
               end 
           5:begin 
               state3<=6; 
               ii<=1;jj<=6; 
               wts_temp1<=X1-weights1[5];wts_temp2<=X2-weights2[5];wts_temp3<=X3-weights3[5]; 
               if(check) 
                 begin 
                   weights1[4]<=weights1[4]+deta1;weights2[4]<=weights2[4]+deta2;weights3[4]<=weights3[4]+deta3; 
                 end 
               end 
           6:begin 
               state3<=7; 
               ii<=2;jj<=1; 
               wts_temp1<=X1-weights1[6];wts_temp2<=X2-weights2[6];wts_temp3<=X3-weights3[6]; 
               if(check) 
                 begin 
                   weights1[5]<=weights1[5]+deta1;weights2[5]<=weights2[5]+deta2;weights3[5]<=weights3[5]+deta3; 
                 end 
               end 
           7:begin 
               state3<=8; 
               ii<=2;jj<=2; 
               wts_temp1<=X1-weights1[7];wts_temp2<=X2-weights2[7];wts_temp3<=X3-weights3[7]; 
               if(check) 
                 begin 
                   weights1[6]<=weights1[6]+deta1;weights2[6]<=weights2[6]+deta2;weights3[6]<=weights3[6]+deta3; 
                 end 
               end 
           8:begin 
               state3<=9; 
               ii<=2;jj<=3; 
               wts_temp1<=X1-weights1[8];wts_temp2<=X2-weights2[8];wts_temp3<=X3-weights3[8]; 
               if(check) 
                 begin 
                   weights1[7]<=weights1[7]+deta1;weights2[7]<=weights2[7]+deta2;weights3[7]<=weights3[7]+deta3; 
                 end 
               end 
           9:begin 
               state3<=10; 
               ii<=2;jj<=4; 
               wts_temp1<=X1-weights1[9];wts_temp2<=X2-weights2[9];wts_temp3<=X3-weights3[9]; 
               if(check) 
                 begin 
                   weights1[8]<=weights1[8]+deta1;weights2[8]<=weights2[8]+deta2;weights3[8]<=weights3[8]+deta3; 
                 end 
               end 
           10:begin 
               state3<=11; 
               ii<=2;jj<=5; 
               wts_temp1<=X1-weights1[10];wts_temp2<=X2-weights2[10];wts_temp3<=X3-weights3[10]; 
               if(check) 
                 begin 
                   weights1[9]<=weights1[9]+deta1;weights2[9]<=weights2[9]+deta2;weights3[9]<=weights3[9]+deta3; 
                 end 
               end 
           11:begin 
               state3<=12; 
               ii<=2;jj<=6; 
               wts_temp1<=X1-weights1[11];wts_temp2<=X2-weights2[11];wts_temp3<=X3-weights3[11]; 
               if(check) 
                 begin 
                   weights1[10]<=weights1[10]+deta1;weights2[10]<=weights2[10]+deta2;weights3[10]<=weights3[10]+deta3; 
                 end 
               end 
           12:begin 
               state3<=13; 
               ii<=3;jj<=1; 
               wts_temp1<=X1-weights1[12];wts_temp2<=X2-weights2[12];wts_temp3<=X3-weights3[12]; 
               if(check) 
                 begin 
                   weights1[11]<=weights1[11]+deta1;weights2[11]<=weights2[11]+deta2;weights3[11]<=weights3[11]+deta3; 
                 end 
               end 
           13:begin 
               state3<=14; 
               ii<=3;jj<=2; 
               wts_temp1<=X1-weights1[13];wts_temp2<=X2-weights2[13];wts_temp3<=X3-weights3[13]; 
               if(check) 
                 begin 
                   weights1[12]<=weights1[12]+deta1;weights2[12]<=weights2[12]+deta2;weights3[12]<=weights3[12]+deta3; 
                 end 
               end 
           14:begin 
               state3<=15; 
               ii<=3;jj<=3; 
               wts_temp1<=X1-weights1[14];wts_temp2<=X2-weights2[14];wts_temp3<=X3-weights3[14]; 
               if(check) 
                 begin 
                   weights1[13]<=weights1[13]+deta1;weights2[13]<=weights2[13]+deta2;weights3[13]<=weights3[13]+deta3; 
                 end 
               end 
           15:begin 
               state3<=16; 
               ii<=3;jj<=4; 
               wts_temp1<=X1-weights1[15];wts_temp2<=X2-weights2[15];wts_temp3<=X3-weights3[15]; 
               if(check) 
                 begin 
                   weights1[14]<=weights1[14]+deta1;weights2[14]<=weights2[14]+deta2;weights3[14]<=weights3[14]+deta3; 
                 end 
               end 
           16:begin 
               state3<=17; 
               ii<=3;jj<=5; 
               wts_temp1<=X1-weights1[16];wts_temp2<=X2-weights2[16];wts_temp3<=X3-weights3[16]; 
               if(check) 
                 begin 
                   weights1[15]<=weights1[15]+deta1;weights2[15]<=weights2[15]+deta2;weights3[15]<=weights3[15]+deta3; 
                 end 
               end 
           17:begin 
               state3<=18; 
               ii<=3;jj<=6; 
               wts_temp1<=X1-weights1[17];wts_temp2<=X2-weights2[17];wts_temp3<=X3-weights3[17]; 
               if(check) 
                 begin 
                   weights1[16]<=weights1[16]+deta1;weights2[16]<=weights2[16]+deta2;weights3[16]<=weights3[16]+deta3; 
                 end 
               end 
           18:begin 
               state3<=19; 
               ii<=4;jj<=1; 
               wts_temp1<=X1-weights1[18];wts_temp2<=X2-weights2[18];wts_temp3<=X3-weights3[18]; 
               if(check) 
                 begin 
                   weights1[17]<=weights1[17]+deta1;weights2[17]<=weights2[17]+deta2;weights3[17]<=weights3[17]+deta3; 
                 end 
               end 
           19:begin 
               state3<=20; 
               ii<=4;jj<=2; 
               wts_temp1<=X1-weights1[19];wts_temp2<=X2-weights2[19];wts_temp3<=X3-weights3[19]; 
               if(check) 
                 begin 
                   weights1[18]<=weights1[18]+deta1;weights2[18]<=weights2[18]+deta2;weights3[18]<=weights3[18]+deta3; 
                 end 
               end 
           20:begin 
               state3<=21; 
               ii<=4;jj<=3; 
               wts_temp1<=X1-weights1[20];wts_temp2<=X2-weights2[20];wts_temp3<=X3-weights3[20]; 
               if(check) 
                 begin 
                   weights1[19]<=weights1[19]+deta1;weights2[19]<=weights2[19]+deta2;weights3[19]<=weights3[19]+deta3; 
                 end 
               end 
           21:begin 
               state3<=22; 
               ii<=4;jj<=4; 
               wts_temp1<=X1-weights1[21];wts_temp2<=X2-weights2[21];wts_temp3<=X3-weights3[21]; 
               if(check) 
                 begin 
                   weights1[20]<=weights1[20]+deta1;weights2[20]<=weights2[20]+deta2;weights3[20]<=weights3[20]+deta3; 
                 end 
               end 
           22:begin 
               state3<=23; 
               ii<=4;jj<=5; 
               wts_temp1<=X1-weights1[22];wts_temp2<=X2-weights2[22];wts_temp3<=X3-weights3[22]; 
               if(check) 
                 begin 
                   weights1[21]<=weights1[21]+deta1;weights2[21]<=weights2[21]+deta2;weights3[21]<=weights3[21]+deta3; 
                 end 
               end 
           23:begin 
               state3<=24; 
               ii<=4;jj<=6; 
               wts_temp1<=X1-weights1[23];wts_temp2<=X2-weights2[23];wts_temp3<=X3-weights3[23]; 
               if(check) 
                 begin 
                   weights1[22]<=weights1[22]+deta1;weights2[22]<=weights2[22]+deta2;weights3[22]<=weights3[22]+deta3; 
                 end 
               end 
           24:begin 
               state3<=25; 
               ii<=5;jj<=1; 
               wts_temp1<=X1-weights1[24];wts_temp2<=X2-weights2[24];wts_temp3<=X3-weights3[24]; 
               if(check) 
                 begin 
                   weights1[23]<=weights1[23]+deta1;weights2[23]<=weights2[23]+deta2;weights3[23]<=weights3[23]+deta3; 
                 end 
               end 
           25:begin 
               state3<=26; 
               ii<=5;jj<=2; 
               wts_temp1<=X1-weights1[25];wts_temp2<=X2-weights2[25];wts_temp3<=X3-weights3[25]; 
               if(check) 
                 begin 
                   weights1[24]<=weights1[24]+deta1;weights2[24]<=weights2[24]+deta2;weights3[24]<=weights3[24]+deta3; 
                 end 
               end 
           26:begin 
               state3<=27; 
               ii<=5;jj<=3; 
               wts_temp1<=X1-weights1[26];wts_temp2<=X2-weights2[26];wts_temp3<=X3-weights3[26]; 
               if(check) 
                 begin 
                   weights1[25]<=weights1[25]+deta1;weights2[25]<=weights2[25]+deta2;weights3[25]<=weights3[25]+deta3; 
                 end 
               end 
           27:begin 
               state3<=28; 
               ii<=5;jj<=4; 
               wts_temp1<=X1-weights1[27];wts_temp2<=X2-weights2[27];wts_temp3<=X3-weights3[27]; 
               if(check) 
                 begin 
                   weights1[26]<=weights1[26]+deta1;weights2[26]<=weights2[26]+deta2;weights3[26]<=weights3[26]+deta3; 
                 end 
               end 
           28:begin 
               state3<=29; 
               ii<=5;jj<=5; 
               wts_temp1<=X1-weights1[28];wts_temp2<=X2-weights2[28];wts_temp3<=X3-weights3[28]; 
               if(check) 
                 begin 
                   weights1[27]<=weights1[27]+deta1;weights2[27]<=weights2[27]+deta2;weights3[27]<=weights3[27]+deta3; 
                 end 
               end 
           29:begin 
               state3<=30; 
               ii<=5;jj<=6; 
               wts_temp1<=X1-weights1[29];wts_temp2<=X2-weights2[29];wts_temp3<=X3-weights3[29]; 
               if(check) 
                 begin 
                   weights1[28]<=weights1[28]+deta1;weights2[28]<=weights2[28]+deta2;weights3[28]<=weights3[28]+deta3; 
                 end 
               end 
           30:begin 
               state3<=31; 
               ii<=6;jj<=1; 
               wts_temp1<=X1-weights1[30];wts_temp2<=X2-weights2[30];wts_temp3<=X3-weights3[30]; 
               if(check) 
                 begin 
                   weights1[29]<=weights1[29]+deta1;weights2[29]<=weights2[29]+deta2;weights3[29]<=weights3[29]+deta3; 
                 end 
               end 
           31:begin 
               state3<=32; 
               ii<=6;jj<=2; 
               wts_temp1<=X1-weights1[31];wts_temp2<=X2-weights2[31];wts_temp3<=X3-weights3[31]; 
               if(check) 
                 begin 
                   weights1[30]<=weights1[30]+deta1;weights2[30]<=weights2[30]+deta2;weights3[30]<=weights3[30]+deta3; 
                 end 
               end 
           32:begin 
               state3<=33; 
               ii<=6;jj<=3; 
               wts_temp1<=X1-weights1[32];wts_temp2<=X2-weights2[32];wts_temp3<=X3-weights3[32]; 
               if(check) 
                 begin 
                   weights1[31]<=weights1[31]+deta1;weights2[31]<=weights2[31]+deta2;weights3[31]<=weights3[31]+deta3; 
                 end 
               end 
           33:begin 
               state3<=34; 
               ii<=6;jj<=4; 
               wts_temp1<=X1-weights1[33];wts_temp2<=X2-weights2[33];wts_temp3<=X3-weights3[33]; 
               if(check) 
                 begin 
                   weights1[32]<=weights1[32]+deta1;weights2[32]<=weights2[32]+deta2;weights3[32]<=weights3[32]+deta3; 
                 end 
               end 
           34:begin 
               state3<=35; 
               ii<=6;jj<=5; 
               wts_temp1<=X1-weights1[34];wts_temp2<=X2-weights2[34];wts_temp3<=X3-weights3[34]; 
               if(check) 
                 begin 
                   weights1[33]<=weights1[33]+deta1;weights2[33]<=weights2[33]+deta2;weights3[33]<=weights3[33]+deta3; 
                end 
               end 
           35:begin 
               state3<=36; 
               ii<=6;jj<=6; 
               wts_temp1<=X1-weights1[35];wts_temp2<=X2-weights2[35];wts_temp3<=X3-weights3[35]; 
               if(check) 
                 begin 
                   weights1[34]<=weights1[34]+deta1;weights2[34]<=weights2[34]+deta2;weights3[34]<=weights3[34]+deta3; 
                 end 
               end 
           36:begin 
               state3<=37; 
               Nc<=Nc-1; 
               state<=3'b100; 
                
               if(check) 
                 begin 
                   weights1[35]<=weights1[35]+deta1;weights2[35]<=weights2[35]+deta2;weights3[35]<=weights3[35]+deta3; 
                 end 
               end 
           default: 
               state3<=0; 
         endcase     
      end  
end 
/////////////////////////////////////////////////////////////   
lpm_mult mul_update_wts1( 
                .dataa(lam), 
                .datab(wts_temp1), 
                .result(deta1) 
                            ); 
defparam mul_update_wts1.lpm_widtha=11; 
defparam mul_update_wts1.lpm_widthb=`BIT_WIDTH_DATA+1; 
defparam mul_update_wts1.lpm_widthp=`BIT_WIDTH_DATA; 
defparam mul_update_wts1.lpm_widths=20; 
defparam mul_update_wts1.lpm_representation="SIGNED";   
  
lpm_mult mul_update_wts2( 
                .dataa(lam), 
                .datab(wts_temp2), 
                .result(deta2) 
                            ); 
defparam mul_update_wts2.lpm_widtha=11; 
defparam mul_update_wts2.lpm_widthb=`BIT_WIDTH_DATA+1; 
defparam mul_update_wts2.lpm_widthp=`BIT_WIDTH_DATA; 
defparam mul_update_wts2.lpm_widths=20; 
defparam mul_update_wts2.lpm_representation="SIGNED"; 
 
lpm_mult mul_update_wts3( 
                .dataa(lam), 
                .datab(wts_temp3), 
                .result(deta3) 
                            ); 
defparam mul_update_wts3.lpm_widtha=11; 
defparam mul_update_wts3.lpm_widthb=`BIT_WIDTH_DATA+1; 
defparam mul_update_wts3.lpm_widthp=`BIT_WIDTH_DATA; 
defparam mul_update_wts3.lpm_widths=20; 
defparam mul_update_wts3.lpm_representation="SIGNED"; 
 
check_nearby B( 
                       //output  
                        .check(check), 
                       //inputs 
                        .ii(ii), 
                        .jj(jj), 
                        .p_x(winner_x), 
                        .p_y(winner_y), 
                        .Nc(Nc) 
                ); 
endmodule 
////////////////////////////////////////////////////// 
//check if max(abs(ii-p_x),abs(jj-p_y))<Nc 
module  check_nearby( 
                       //output  
                        check, 
                       //inputs 
                        ii, 
                        jj, 
                        p_x, 
                        p_y, 
                        Nc 
                     ); 
output                 check; 
input     [2:0]        ii,jj; 
input     [2:0]        p_x,p_y; 
input     [3:0]        Nc; 
 
wire      [3:0]       a,b,c,d,nc; 
assign    nc=(Nc>1)? Nc>>1:1; 
assign    a=nc+p_x; 
assign    b=nc+ii; 
assign    c=nc+p_y; 
assign    d=nc+jj; 
assign    check=(a>ii&&b>p_x&&c>jj&&d>p_y)? 1:0; 
endmodule