module top
    (
        CLOCK_R,// around 10 MHz
        CLOCK_X, // 1..200 MHz
        SCL,     // I2C Clock
        SDA,     // I2C Data
        LED,     // status leds, active high
        OSC     // Measuring pod
    );


input           CLOCK_R;
input           CLOCK_X;
input           SCL;
inout           SDA;
output [5:0]    LED;
output [3:0]    OSC;

parameter LED_START = 0;
parameter LED_UNDERFLOW = 1;
parameter LED_READY_MEASURE = 2;
parameter LED_READY_MULTIPLY = 3; // EPM240 will not have this
parameter LED_READY_DIVIDE = 4;   // EPM240 will not have this
parameter LED_BLINK = 5;         // EPM240 will might have this

// Delete this for EPM240 if it can not fit.
// reg [22:0] led_counter;
// assign LED[LED_BLINK] = led_counter[22];
// always @(negedge CLOCK_R) 
// begin
//     led_counter <= led_counter+1;
// end
assign LED[LED_BLINK] = CLOCK_X; // to help set the schmitt trigger

reg [2:0] scale_clk;

always @(posedge CLOCK_R) 
begin
    scale_clk <= scale_clk + 1;
end
assign OSC[0] = CLOCK_R; // measuring clock
assign OSC[3:1] = scale_clk; // measuring clock
  
wire ready_measure; 
assign LED[LED_READY_MEASURE] = ready_measure;

wire [31:0] cnt_r;
wire [31:0] cnt_x;

wire underflow;
assign LED[LED_UNDERFLOW] = underflow;

wire [7:0] i2c_data_o;
wire start;
assign start = i2c_data_o[7];

assign LED[LED_START] = start;

counter u1(
    .start_i (start),
    .clk_r_i (CLOCK_R),
    .clk_x_i (CLOCK_X),
    .ready_o (ready_measure),
    .underflow_o (underflow), 
    .cnt_r_o (cnt_r), 
    .cnt_x_o (cnt_x)
);

wire [7:0] i2c_data_i;

i2c u2 (
    .sda_io (SDA),
    .scl_i (SCL),
    .data_i (i2c_data_i), 
    .data_o (i2c_data_o)
);

// Delete this for EPM240 if it can not fit.
// multiplier, divider

wire [7:0] result[8:0];

assign result[0] = cnt_x[7:0];
assign result[1] = cnt_x[15:8];
assign result[2] = cnt_x[23:16];
assign result[3] = cnt_x[31:24];
assign result[4] = cnt_r[7:0];
assign result[5] = cnt_r[15:8];
assign result[6] = cnt_r[23:16];
assign result[7] = cnt_r[31:24];

assign result[8] = { 6'b0,  underflow, ready_measure }; 

assign i2c_data_i = i2c_data_o[6] ? result[8] : result[i2c_data_o[2:0]];

//assign LED[LED_READY_MULTIPLY]  = ^cnt_r;
//assign LED[LED_READY_DIVIDE]  = ^cnt_x;

endmodule
