# These are the records that transform tick counts to time in nanoseconds,
# by using the sampling frequency of the ADCs.

# Giving the frequency in Hz, calculate the time in ns between each sample
record (calc, "BLEN:$(AREA):$(POS):$(INST)_SAMP_TIME") {
  field(DESC, "Time between samples in ns")
  field(INPA, "BLEN:$(AREA):$(POS):$(INST)_AmcClkFreq CP")
  field(CALC, "1000000000/(2 * A)")
}

# Calculate the time correspondent to the number of samples of the pre-edge
# of the weight function
record (calc, "BLEN:$(AREA):$(POS):$(INST)_TIME_PRE_RBV") {
  field(DESC, "Duration of pre-edge in ns")
  field(INPA, "BLEN:$(AREA):$(POS):$(INST)_SAMP_TIME CP")
  field(INPB, "BLEN:$(AREA):$(POS):$(INST)_NumSamp0_RBV")
  field(CALC, "A * B")
}

# Calculate the time correspondent to the number of samples of between edges
# of the weight function. It must be at least the same size of the
# pre-edge time because the time counts from the beginning of the pre-edge.
record (calc, "BLEN:$(AREA):$(POS):$(INST)_TIME_MID_RBV") {
  field(DESC, "Duration between edges in ns")
  field(INPA, "BLEN:$(AREA):$(POS):$(INST)_SAMP_TIME CP")
  field(INPB, "BLEN:$(AREA):$(POS):$(INST)_TrigDelay1_RBV")
  #field(INPC, "BLEN:$(AREA):$(POS):$(INST)_NumSamp0_RBV")
  field(CALC, "B*2 * A")
}

# Calculate the time correspondent to the number of samples of the pos-edge
# of the weight function
record (calc, "BLEN:$(AREA):$(POS):$(INST)_TIME_POS_RBV") {
  field(DESC, "Duration of pos-edge in ns")
  field(INPA, "BLEN:$(AREA):$(POS):$(INST)_SAMP_TIME CP")
  field(INPB, "BLEN:$(AREA):$(POS):$(INST)_NumSamp1_RBV")
  field(CALC, "A * B")
}

# Desired time of the weight function pre-edge
record (ao, "BLEN:$(AREA):$(POS):$(INST)_TIME_PRE") {
  field(DESC, "Duration of pre-edge in ns")
  info(autosaveFields, "VAL")
}

# Calculate the number of samples of the pre-edge of the weight function 
# giving the correspondent time and writes it to the firmware. Only even
# numbers are allowed in the FPGA register (that is why we use ((~0)-1)&
record (calcout, "BLEN:$(AREA):$(POS):$(INST)_NumSamp0_CLC") {
  field(DESC, "Number samples of pre-edge")
  field(INPA, "BLEN:$(AREA):$(POS):$(INST)_TIME_PRE CP")
  field(INPB, "BLEN:$(AREA):$(POS):$(INST)_AmcClkFreq CP")
  field(CALC, "((~0)-1)&FLOOR((2*A*B)/1000000000)")
  field(OUT, "BLEN:$(AREA):$(POS):$(INST)_NumSamp0 PP")
  field(DOPT, "Use CALC")
}

# Desired time of the weight function between edges
record (ao, "BLEN:$(AREA):$(POS):$(INST)_TIME_MID") {
  field(DESC, "Duration of between edge in ns")
  info(autosaveFields, "VAL")
}

# Time between edges is doubled related to the edges. Also, timing zero for
# this time is exactly timing zero for the pre-edge. So, we must not allow
# that the between edges time keeps greater than the pre-edge.
record (calc, "BLEN:$(AREA):$(POS):$(INST)_TIME_MIDC") {
  field(DESC, "Duration of between edge in ns")
  field(INPA, "BLEN:$(AREA):$(POS):$(INST)_TIME_PRE CP")
  field(INPB, "BLEN:$(AREA):$(POS):$(INST)_TIME_MID CP")
  field(CALC, "A<B? B/2 : A/2")
}

# Calculate the number of samples of between edges of the weight function 
# giving the correspondent time and writes it to the firmware. The time
# here is twice the time in pre and post edges. Only even numbers are allowed
# in the FPGA register (that is why we use ((~0)-1)&).
record (calcout, "BLEN:$(AREA):$(POS):$(INST)_TrigDelay1_CLC") {
  field(DESC, "Number samples of between edges")
  field(INPA, "BLEN:$(AREA):$(POS):$(INST)_TIME_MIDC CP")
  field(INPB, "BLEN:$(AREA):$(POS):$(INST)_AmcClkFreq CP")
  field(CALC, "((~0)-1)&FLOOR((2*A*B)/1000000000)")
  field(OUT, "BLEN:$(AREA):$(POS):$(INST)_TrigDelay1 PP")
  field(DOPT, "Use CALC")
}

# Desired time of the weight function pos-edge
record (ao, "BLEN:$(AREA):$(POS):$(INST)_TIME_POS") {
  field(DESC, "Duration of pos-edge in ns")
  info(autosaveFields, "VAL")
}

# Calculate the number of samples of the pos-edge of the weight function 
# giving the correspondent time and writes it to the firmware. Only even
# numbers are allowed in the FPGA register (that is why we use ((~0)-1)&).
record (calcout, "BLEN:$(AREA):$(POS):$(INST)_NumSamp1_CLC") {
  field(DESC, "Number samples of pos-edge")
  field(INPA, "BLEN:$(AREA):$(POS):$(INST)_TIME_POS CP")
  field(INPB, "BLEN:$(AREA):$(POS):$(INST)_AmcClkFreq CP")
  field(CALC, "((~0)-1)&FLOOR((2*A*B)/1000000000)")
  field(OUT, "BLEN:$(AREA):$(POS):$(INST)_NumSamp1 PP")
  field(DOPT, "Use CALC")
}

# Create a waveform with the sampling time. This will be the X axis of the
# waveform.
record (aSub, "BLEN:$(AREA):$(POS):$(INST)_WF_TIME") {
  field(SNAM, "calcTimeArray_facet")
  field(FTA, "LONG")
  field(FTVA, "DOUBLE")
  field(NOA, "2020")
  field(NOVA, "2020")
  field(INPA, "BLEN:$(AREA):$(POS):$(INST)_AmcClkFreq CP")
}
