module: quantizer_clocked_basic
description: 
timing_sensitivity: posedge clk
parameters:  double in_min, double in_max
             int num_active_bits
inputs:  double in, bool clk
outputs:  bool out[29:0]
classes:  
static_variables:  double scale_factor
init:  
out.set_decimal_value(0);

if (num_active_bits < 2)
  {
   printf("error in 'quantizer_clocked_basic':\n");
   printf("  'num_active_bits' must be greater than one\n");
   printf("  in this case, 'num_active_bits' was set to '%d'\n",
             num_active_bits);
   printf("  -> please change these parameters in your schematic\n");
   exit(1);
  }
if (num_active_bits > 30)
  {
   printf("error in 'quantizer_clocked_basic':\n");
   printf("  'num_active_bits' must be less than 31\n");
   printf("  in this case, 'num_active_bits' was set to '%d'\n",
             num_active_bits);
   printf("  -> please change these parameters in your schematic\n");
   exit(1);
  }
if (in_min >= in_max)
  {
   printf("error in 'quantizer_clocked_basic':\n");
   printf("  'in_min' must be less than 'in_max'\n");
   printf("  in this case, 'in_min' was set to '%5.3g'\n",in_min);
   printf("  and 'in_max' was set to '%5.3g'\n",in_max);
   printf("  -> please change these parameters in your schematic\n");
   exit(1);
  }
scale_factor = in_max - in_min;
if (scale_factor < 1e-30)
   {
   printf("error in 'quantizer_clocked_basic':\n");
   printf("  'in_max' - 'in_min' must > 0.0\n");
   printf("  in this case, 'in_min' was set to '%5.3g'\n",in_min);
   printf("  and 'in_max' was set to '%5.3g'\n",in_max);
   printf("  such that 'in_max' - 'in_min' is '%5.3g'\n",scale_factor);
   printf("  -> please change these parameters in your schematic\n");
   exit(1);
   }

code:
// this code only executes on rising edges of clk due to
// timing_sensitivity: statement above
  
double scaled_input;
int decimal_range, num_bits;

num_bits = (int) floor(num_active_bits + 0.5);
decimal_range = (1<<num_bits) - 1e-30;

scaled_input = ((in - in_min)/scale_factor)*((double) decimal_range); 
out.set_decimal_value((int) floor(scaled_input));
