record(calc, "$(CM,recursive)00:RACKA:PRLSUM") {
  field(DESC, "Rack PRL summary")
  field(SCAN, "1 second")
  field(CALC, "A")
  field(INPA, "$(CM,recursive)00:RACKA:PRL_OFFSET_DIFF.SEVR MS")
  field(INPB, "$(PRLINP=,undefined)")
  field(INPC, "$(CM,recursive)00:RACKA:PRL_OFFSET_STAT.SEVR MS")
  field(INPD, "$(CM,recursive)00:RACKA:PRL_ENABLE_RBV MS")
  field(INPE, "$(CM,recursive)00:PRCA:PRLFLT MS")
}

record(seq, "$(CM,recursive)00:RACKA:CKP_FO") {
  field(DESC, "Set CKP for cavities")
  field(SCAN, "Passive")
  field(SELM, "All")
  field(DOL0, "1")
  field(LNK0, "$(CM,recursive)10:CKP PP")
  field(DOL1, "1")
  field(LNK1, "$(CM,recursive)20:CKP PP")
  field(DOL2, "1")
  field(LNK2, "$(CM,recursive)30:CKP PP")
  field(DOL3, "1")
  field(LNK3, "$(CM,recursive)40:CKP PP")
}


# Set PRL phase alarms based on rack's PRL mode
# PRL_MODE: 0=Fwd/Rev, 1=Fwd, 2=Rev, 3=None

record(calcout, "$(CM,recursive)00:RACKA:PRLFWDALRM_") {
  field(CALC, "((A=0)||(A=1))?1:2")
  field(INPA, "$(CM,recursive)00:RACKA:PRL_MODE CP")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)00:RACKA:PRLFWDALRM1_")
}

# Determine if FWD PRL signal is used
record(seq, "$(CM,recursive)00:RACKA:PRLFWDALRM1_") {
  field(DESC, "Set PRLFWD phase alarms")
  field(SELM, "Specified")
  field(SELL, "$(CM,recursive)00:RACKA:PRLFWDALRM_")
  field(DOL1, "1")
  field(LNK1, "$(CM,recursive)00:RACKA:PRLFWDALRM2_.PROC")
  field(DOL2, "1")
  field(LNK2, "$(CM,recursive)00:RACKA:PRLFWDALRM3_.PROC")
}

# Enable PRLFWD alarms
record(seq, "$(CM,recursive)00:RACKA:PRLFWDALRM2_") {
  field(DESC, "Set PRLFWD phase alarms")
  field(SELM, "All")
  field(DOL1, ".02")
  field(LNK1, "$(CM,recursive)00:RACKA:PRLFWD:PSTD.HIGH")
  field(DOL2, "1")
  field(LNK2, "$(CM,recursive)00:RACKA:PRLFWD:PSTD.HSV")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKA:PRLFWD:PSTD.LOLO")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKA:PRLFWD:PSTD.LLSV")
  field(FLNK, "$(CM,recursive)00:RACKA:PRLREVALRM_")
}

# Disable PRLFWD alarms
record(seq, "$(CM,recursive)00:RACKA:PRLFWDALRM3_") {
  field(DESC, "Set PRLFWD phase alarms")
  field(SELM, "All")
  field(DOL1, "0")
  field(LNK1, "$(CM,recursive)00:RACKA:PRLFWD:PSTD.HIGH")
  field(DOL2, "0")
  field(LNK2, "$(CM,recursive)00:RACKA:PRLFWD:PSTD.HSV")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKA:PRLFWD:PSTD.LOLO")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKA:PRLFWD:PSTD.LLSV")
  field(FLNK, "$(CM,recursive)00:RACKA:PRLREVALRM_")
}

record(calcout, "$(CM,recursive)00:RACKA:PRLREVALRM_") {
  field(CALC, "((A=0)||(A=2))?1:2")
  field(INPA, "$(CM,recursive)00:RACKA:PRL_MODE")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)00:RACKA:PRLREVALRM1_")
}

# Determine if REV PRL signal is used
record(seq, "$(CM,recursive)00:RACKA:PRLREVALRM1_") {
  field(DESC, "Set PRLREV phase alarms")
  field(SELM, "Specified")
  field(SELL, "$(CM,recursive)00:RACKA:PRLREVALRM_")
  field(DOL1, "1")
  field(LNK1, "$(CM,recursive)00:RACKA:PRLREVALRM2_.PROC")
  field(DOL2, "1")
  field(LNK2, "$(CM,recursive)00:RACKA:PRLREVALRM3_.PROC")
}

# Enable PRLREV alarms
record(seq, "$(CM,recursive)00:RACKA:PRLREVALRM2_") {
  field(DESC, "Set PRLREV phase alarms")
  field(SELM, "All")
  field(DOL1, ".02")
  field(LNK1, "$(CM,recursive)00:RACKA:PRLREV:PSTD.HIGH")
  field(DOL2, "1")
  field(LNK2, "$(CM,recursive)00:RACKA:PRLREV:PSTD.HSV")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKA:PRLREV:PSTD.LOLO")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKA:PRLREV:PSTD.LLSV")
}

# Disable PRLREV alarms
record(seq, "$(CM,recursive)00:RACKA:PRLREVALRM3_") {
  field(DESC, "Set PRLREV phase alarms")
  field(SELM, "All")
  field(DOL1, "0")
  field(LNK1, "$(CM,recursive)00:RACKA:PRLREV:PSTD.HIGH")
  field(DOL2, "0")
  field(LNK2, "$(CM,recursive)00:RACKA:PRLREV:PSTD.HSV")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKA:PRLREV:PSTD.LOLO")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKA:PRLREV:PSTD.LLSV")
}

record(calc, "$(CM,recursive)00:RACKA:PRLJITSUM") {
  field(DESC, "Rack PRL phase STD summary")
  field(SCAN, "1 second")
  field(CALC, "MAX(A,B)")
  field(HIGH, "1")
  field(HSV,  "MINOR")
  field(HIHI, "2")
  field(HHSV, "MAJOR")
  field(LOLO, "0")
  field(INPA, "$(CM,recursive)00:RACKA:PRLFWD:PSTD.SEVR CA MSI")
  field(INPB, "$(CM,recursive)00:RACKA:PRLREV:PSTD.SEVR CA MSI")
}
record(calc, "$(CM,recursive)00:RACKB:PRLSUM") {
  field(DESC, "Rack PRL summary")
  field(SCAN, "1 second")
  field(CALC, "A")
  field(INPA, "$(CM,recursive)00:RACKB:PRL_OFFSET_DIFF.SEVR MS")
  field(INPB, "$(PRLINP=,undefined)")
  field(INPC, "$(CM,recursive)00:RACKB:PRL_OFFSET_STAT.SEVR MS")
  field(INPD, "$(CM,recursive)00:RACKB:PRL_ENABLE_RBV MS")
  field(INPE, "$(CM,recursive)00:PRCB:PRLFLT MS")
}

record(seq, "$(CM,recursive)00:RACKB:CKP_FO") {
  field(DESC, "Set CKP for cavities")
  field(SCAN, "Passive")
  field(SELM, "All")
  field(DOL0, "1")
  field(LNK0, "$(CM,recursive)50:CKP PP")
  field(DOL1, "1")
  field(LNK1, "$(CM,recursive)60:CKP PP")
  field(DOL2, "1")
  field(LNK2, "$(CM,recursive)70:CKP PP")
  field(DOL3, "1")
  field(LNK3, "$(CM,recursive)80:CKP PP")
}


# Set PRL phase alarms based on rack's PRL mode
# PRL_MODE: 0=Fwd/Rev, 1=Fwd, 2=Rev, 3=None

record(calcout, "$(CM,recursive)00:RACKB:PRLFWDALRM_") {
  field(CALC, "((A=0)||(A=1))?1:2")
  field(INPA, "$(CM,recursive)00:RACKB:PRL_MODE CP")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)00:RACKB:PRLFWDALRM1_")
}

# Determine if FWD PRL signal is used
record(seq, "$(CM,recursive)00:RACKB:PRLFWDALRM1_") {
  field(DESC, "Set PRLFWD phase alarms")
  field(SELM, "Specified")
  field(SELL, "$(CM,recursive)00:RACKB:PRLFWDALRM_")
  field(DOL1, "1")
  field(LNK1, "$(CM,recursive)00:RACKB:PRLFWDALRM2_.PROC")
  field(DOL2, "1")
  field(LNK2, "$(CM,recursive)00:RACKB:PRLFWDALRM3_.PROC")
}

# Enable PRLFWD alarms
record(seq, "$(CM,recursive)00:RACKB:PRLFWDALRM2_") {
  field(DESC, "Set PRLFWD phase alarms")
  field(SELM, "All")
  field(DOL1, ".02")
  field(LNK1, "$(CM,recursive)00:RACKB:PRLFWD:PSTD.HIGH")
  field(DOL2, "1")
  field(LNK2, "$(CM,recursive)00:RACKB:PRLFWD:PSTD.HSV")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKB:PRLFWD:PSTD.LOLO")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKB:PRLFWD:PSTD.LLSV")
  field(FLNK, "$(CM,recursive)00:RACKB:PRLREVALRM_")
}

# Disable PRLFWD alarms
record(seq, "$(CM,recursive)00:RACKB:PRLFWDALRM3_") {
  field(DESC, "Set PRLFWD phase alarms")
  field(SELM, "All")
  field(DOL1, "0")
  field(LNK1, "$(CM,recursive)00:RACKB:PRLFWD:PSTD.HIGH")
  field(DOL2, "0")
  field(LNK2, "$(CM,recursive)00:RACKB:PRLFWD:PSTD.HSV")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKB:PRLFWD:PSTD.LOLO")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKB:PRLFWD:PSTD.LLSV")
  field(FLNK, "$(CM,recursive)00:RACKB:PRLREVALRM_")
}

record(calcout, "$(CM,recursive)00:RACKB:PRLREVALRM_") {
  field(CALC, "((A=0)||(A=2))?1:2")
  field(INPA, "$(CM,recursive)00:RACKB:PRL_MODE")
  field(PINI, "YES")
  field(FLNK, "$(CM,recursive)00:RACKB:PRLREVALRM1_")
}

# Determine if REV PRL signal is used
record(seq, "$(CM,recursive)00:RACKB:PRLREVALRM1_") {
  field(DESC, "Set PRLREV phase alarms")
  field(SELM, "Specified")
  field(SELL, "$(CM,recursive)00:RACKB:PRLREVALRM_")
  field(DOL1, "1")
  field(LNK1, "$(CM,recursive)00:RACKB:PRLREVALRM2_.PROC")
  field(DOL2, "1")
  field(LNK2, "$(CM,recursive)00:RACKB:PRLREVALRM3_.PROC")
}

# Enable PRLREV alarms
record(seq, "$(CM,recursive)00:RACKB:PRLREVALRM2_") {
  field(DESC, "Set PRLREV phase alarms")
  field(SELM, "All")
  field(DOL1, ".02")
  field(LNK1, "$(CM,recursive)00:RACKB:PRLREV:PSTD.HIGH")
  field(DOL2, "1")
  field(LNK2, "$(CM,recursive)00:RACKB:PRLREV:PSTD.HSV")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKB:PRLREV:PSTD.LOLO")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKB:PRLREV:PSTD.LLSV")
}

# Disable PRLREV alarms
record(seq, "$(CM,recursive)00:RACKB:PRLREVALRM3_") {
  field(DESC, "Set PRLREV phase alarms")
  field(SELM, "All")
  field(DOL1, "0")
  field(LNK1, "$(CM,recursive)00:RACKB:PRLREV:PSTD.HIGH")
  field(DOL2, "0")
  field(LNK2, "$(CM,recursive)00:RACKB:PRLREV:PSTD.HSV")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKB:PRLREV:PSTD.LOLO")
  field(DOL3, "0")
  field(LNK3, "$(CM,recursive)00:RACKB:PRLREV:PSTD.LLSV")
}

record(calc, "$(CM,recursive)00:RACKB:PRLJITSUM") {
  field(DESC, "Rack PRL phase STD summary")
  field(SCAN, "1 second")
  field(CALC, "MAX(A,B)")
  field(HIGH, "1")
  field(HSV,  "MINOR")
  field(HIHI, "2")
  field(HHSV, "MAJOR")
  field(LOLO, "0")
  field(INPA, "$(CM,recursive)00:RACKB:PRLFWD:PSTD.SEVR CA MSI")
  field(INPB, "$(CM,recursive)00:RACKB:PRLREV:PSTD.SEVR CA MSI")
}
