#*************************************************************************
# Copyright (c) 2002 The University of Chicago, as Operator of Argonne
#     National Laboratory.
# Copyright (c) 2002 The Regents of the University of California, as
#     Operator of Los Alamos National Laboratory.
# EPICS BASE is distributed subject to a Software License Agreement found
# in file LICENSE that is included with this distribution.
#*************************************************************************

=title Multi-Bit Binary Output Direct Record (mbboDirect)

The mbboDirect record performs roughly the opposite function to that of the
L<mbbiDirect record|mbbiDirectRecord>.

It can accept boolean values in its 32 bit fields (B0-B9, BA-BF, B10-B19 and
B1A-B1F), and converts them to a 32-bit signed integer in VAL which is provided
to the device support. A zero value in a bit field becomes a zero bit in VAL, a
non-zero value in a bit field becomes a one bit in VAL, with B0 being the least
signficant bit and B1F the MSB/sign bit.

=recordtype mbboDirect

=cut

recordtype(mbboDirect) {

=head2 Parameter Fields

The record-specific fields are described below, grouped by functionality.

=head3 Scan Parameters

The mbboDirect record has the standard fields for specifying under what
circumstances it will be processed.
These fields are listed in L<Scan Fields|dbCommonRecord/Scan Fields>.

=fields SCAN, PHAS, EVNT, PRIO, PINI

=head3 Desired Output Parameters

Like all output records, the mbboDirect record must specify where its output
should originate when it gets processed. The Output Mode SeLect field (OMSL)
determines whether the output value should be read from another record or not.
When set to C<<< closed_loop >>>, a 32-bit integer value (the "desired output")
will be read from a link specified in the Desired Output Link (DOL) field and
placed into the VAL field.

When OMSL is set to C<<< supervisory >>>, the DOL field is ignored during
processing and the contents of VAL are used. A value to be output may thus be
written direcly into the VAL field from elsewhere as long as the record is in
C<<< supervisory >>> mode.

=fields OMSL, DOL, VAL

=head4 Bit Fields

The fields B0 through BF and B10 through B1F provide an alternative way to set
the individual bits of the VAL field when the record is in C<<< supervisory >>>
mode. Writing to one of these fields will then modify the corresponding bit in
VAL, and writing to VAL will update these bit fields from that value.

The VAL field is signed so it can be accessed through Channel Access as an
integer; if it were made unsigned (a C<DBF_ULONG>) its representation through
Channel Access would become a C<double>, which could cause problems with some
client programs.

Prior to the EPICS 7.0.6.1 release the individual bit fields were not updated
while the record was in C<<< closed_loop >>> mode with VAL being set from the
DOL link, and writing to the bit fields in that mode could cause the record to
process but the actual field values would not affect VAL at all. Changing the
OMSL field from C<<< closed_loop >>> to C<<< supervisory >>> would set the bit
fields from VAL at that time and trigger a monitor event for the bits that
changed at that time. At record initialization if VAL is defined and the OMSL
field is C<<< supervisory >>> the bit fields would be set from VAL.

From EPICS 7.0.6.1 the bit fields get updated from VAL during record processing
and monitors are triggered on them in either mode. Attempts to write to the bit
fields while in C<<< closed_loop >>> mode will be rejected by the C<special()>
routine which may trigger an error from the client that wrote to them. During
initialization if the record is still undefined (UDF) after DOL has been read
and the device support initialized but at least one of the B0-B1F fields is
non-zero, the VAL field will be set from those fields and UDF will be cleared.

=fields B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, BA, BB, BC, BD, BE, BF, B10, B11, B12, B13, B14, B15, B16, B17, B18, B19, B1A, B1B, B1C, B1D, B1E, B1F

=head3 Convert and Write Parameters

For records that are to write values to hardware devices, the OUT output link
must contain the address of the I/O card, and the DTYP field must specify
the proper device support module. Be aware that the address format differs
according to the I/O bus used. See L<Address
Specification|https://docs.epics-controls.org/en/latest/guides/EPICS_Process_Database_Concepts.html#address-specification>
for information on the format of hardware addresses.

During record processing VAL is converted into RVAL, which is the actual 32-bit
word to be sent out. RVAL is set to VAL shifted left by the number of bits
specified in the SHFT field (SHFT is normally set by device support). RVAL is
then sent out to the location specified in the OUT field.

The fields NOBT and MASK can be used by device support to force some of the
output bits written by that support to be zero. By default all 32 bits can be
sent, but the NOBT field can be set to specify a smaller number of contiguous
bits, or MASK can specify a non-contiguous set of bits. When setting MASK it is
often necessary to set NOBT to a non-zero value as well, although in this case
the actual value of NOBT may be ignored by the device support. If a device
support sets the SHFT field it will also left-shift the value of MASK at the
same time.

For mbboDirect records writing to a link instead of to hardware, the DTYP field
must select one of the soft device support routines C<<< Soft Channel >>> or
C<<< Raw Soft Channel >>>. The C<<< Soft Channel >>> support writes the contents
of the VAL field to the output link. The C<<< Raw Soft Channel >>> support
allows SHFT to be set in the DB file, and sends the result of ANDing the shifted
MASK with the RVAL field's value.

=fields OUT, RVAL, SHFT, MASK, NOBT

=head3 Operator Display Parameters

See L<Fields Common to All Record Types|dbCommonRecord/Operator Display
Parameters> for more on the record name (NAME) and description (DESC) fields.


=fields NAME, DESC

=cut

    include "dbCommon.dbd"
    %/* Declare Device Support Entry Table */
    %struct mbboDirectRecord;
    %typedef struct mbbodirectdset {
    %    dset common; /*init_record returns: (0, 2)=>(success, success no convert)*/
    %    long (*write_mbbo)(struct mbboDirectRecord *prec); /*returns: (0, 2)=>(success, success no convert)*/
    %} mbbodirectdset;
    %#define HAS_mbbodirectdset
    %
    field(VAL,DBF_LONG) {
        prompt("Word")
        promptgroup("50 - Output")
        asl(ASL0)
        pp(TRUE)
    }
    field(OMSL,DBF_MENU) {
        prompt("Output Mode Select")
        promptgroup("50 - Output")
        pp(TRUE)
        interest(1)
        menu(menuOmsl)
    }
    field(NOBT,DBF_SHORT) {
        prompt("Number of Bits")
        promptgroup("50 - Output")
        special(SPC_NOMOD)
        interest(1)
    }
    field(DOL,DBF_INLINK) {
        prompt("Desired Output Link")
        promptgroup("40 - Input")
        interest(1)
    }
    field(OUT,DBF_OUTLINK) {
        prompt("Output Specification")
        promptgroup("50 - Output")
        interest(1)
    }
    field(RVAL,DBF_ULONG) {
        prompt("Raw Value")
        special(SPC_NOMOD)
        pp(TRUE)
    }
    field(ORAW,DBF_ULONG) {
        prompt("Prev Raw Value")
        special(SPC_NOMOD)
        interest(3)
    }
    field(RBV,DBF_ULONG) {
        prompt("Readback Value")
        special(SPC_NOMOD)
    }
    field(ORBV,DBF_ULONG) {
        prompt("Prev Readback Value")
        special(SPC_NOMOD)
        interest(3)
    }
    field(MASK,DBF_ULONG) {
        prompt("Hardware Mask")
        special(SPC_NOMOD)
        interest(1)
    }
    field(MLST,DBF_LONG) {
        prompt("Last Value Monitored")
        special(SPC_NOMOD)
        interest(3)
    }
    field(OBIT,DBF_LONG) {
        prompt("Last Bit mask Monitored")
        special(SPC_NOMOD)
        interest(3)
    }
    field(SHFT,DBF_USHORT) {
        prompt("Shift")
        promptgroup("50 - Output")
        interest(1)
    }

=head3 Run-time Parameters

These parameters are used by the run-time code for processing the mbbo Direct
record.

MASK is used by device support routine to read the hardware register. Record
support sets the low order NOBT bits of MASK at initialization, and device
support is allowed to shift this value.

MLST holds the value when the last monitor for value change was triggered.
OBIT has a similar role for bits held in the B0-B1F fields.

=fields NOBT, ORAW, MASK, MLST, OBIT

=head3 Simulation Mode Parameters

The following fields are used to operate the record in simulation mode.

If SIMM (fetched through SIML) is YES, the record is put in SIMS
severity and the value is written through SIOL, without conversion.
If SIMM is RAW, the value is converted and RVAL is written.
SSCN sets a different SCAN mechanism to use in simulation mode.
SDLY sets a delay (in sec) that is used for asynchronous simulation
processing.

See L<Output Simulation Fields|dbCommonOutput/Output Simulation Fields>
for more information on simulation mode and its fields.

=fields SIML, SIMM, SIOL, SIMS, SDLY, SSCN

=cut

    field(SIOL,DBF_OUTLINK) {
        prompt("Simulation Output Link")
        promptgroup("90 - Simulate")
        interest(1)
    }
    field(SIML,DBF_INLINK) {
        prompt("Simulation Mode Link")
        promptgroup("90 - Simulate")
        interest(1)
    }
    field(SIMM,DBF_MENU) {
        prompt("Simulation Mode")
        special(SPC_MOD)
        interest(1)
        menu(menuSimm)
    }
    field(SIMS,DBF_MENU) {
        prompt("Simulation Mode Severity")
        promptgroup("90 - Simulate")
        interest(2)
        menu(menuAlarmSevr)
    }
    field(OLDSIMM,DBF_MENU) {
        prompt("Prev. Simulation Mode")
        special(SPC_NOMOD)
        interest(4)
        menu(menuSimm)
    }
    field(SSCN,DBF_MENU) {
        prompt("Sim. Mode Scan")
        promptgroup("90 - Simulate")
        interest(1)
        menu(menuScan)
        initial("65535")
    }
    field(SDLY,DBF_DOUBLE) {
        prompt("Sim. Mode Async Delay")
        promptgroup("90 - Simulate")
        interest(2)
        initial("-1.0")
    }
    %#include "callback.h"
    field(SIMPVT,DBF_NOACCESS) {
        prompt("Sim. Mode Private")
        special(SPC_NOMOD)
        interest(4)
        extra("epicsCallback            *simpvt")
    }

=head3 Alarm Parameters

The possible alarm conditions for mbboDirect records are the SCAN, READ, and
INVALID alarms.

The IVOA field specifies an action to take when an INVALID alarm is triggered.
There are three possible actions: C<<< Continue normally >>>, C<<< Don't drive
outputs >>>, or C<<< Set output to IVOV >>>. When C<<< Set output to IVOV >>> is
specified and a INVALID alarm is triggered, the record will write the value in
the IVOV field to the output.

See L<Invalid Output Action Fields|dbCommonOutput/Invalid Output Action Fields>
for more information about IVOA and IVOV.

See L<Alarm Specification|https://docs.epics-controls.org/en/latest/guides/EPICS_Process_Database_Concepts.html#alarm-specification>
for a complete explanation of record alarms and of the standard fields.
L<Alarm Fields|dbCommonRecord/Alarm Fields> lists other fields related
to alarms that are common to all record types.

=fields IVOA, IVOV

=cut

    field(IVOA,DBF_MENU) {
        prompt("INVALID outpt action")
        promptgroup("50 - Output")
        interest(2)
        menu(menuIvoa)
    }
    field(IVOV,DBF_LONG) {
        prompt("INVALID output value")
        promptgroup("50 - Output")
        interest(2)
    }

    field(B0,DBF_UCHAR) {
        prompt("Bit 0")
        promptgroup("51 - Output 0-7")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B1,DBF_UCHAR) {
        prompt("Bit 1")
        promptgroup("51 - Output 0-7")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B2,DBF_UCHAR) {
        prompt("Bit 2")
        promptgroup("51 - Output 0-7")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B3,DBF_UCHAR) {
        prompt("Bit 3")
        promptgroup("51 - Output 0-7")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B4,DBF_UCHAR) {
        prompt("Bit 4")
        promptgroup("51 - Output 0-7")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B5,DBF_UCHAR) {
        prompt("Bit 5")
        promptgroup("51 - Output 0-7")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B6,DBF_UCHAR) {
        prompt("Bit 6")
        promptgroup("51 - Output 0-7")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B7,DBF_UCHAR) {
        prompt("Bit 7")
        promptgroup("51 - Output 0-7")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B8,DBF_UCHAR) {
        prompt("Bit 8")
        promptgroup("52 - Output 8-15")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B9,DBF_UCHAR) {
        prompt("Bit 9")
        promptgroup("52 - Output 8-15")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(BA,DBF_UCHAR) {
        prompt("Bit 10")
        promptgroup("52 - Output 8-15")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(BB,DBF_UCHAR) {
        prompt("Bit 11")
        promptgroup("52 - Output 8-15")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(BC,DBF_UCHAR) {
        prompt("Bit 12")
        promptgroup("52 - Output 8-15")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(BD,DBF_UCHAR) {
        prompt("Bit 13")
        promptgroup("52 - Output 8-15")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(BE,DBF_UCHAR) {
        prompt("Bit 14")
        promptgroup("52 - Output 8-15")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(BF,DBF_UCHAR) {
        prompt("Bit 15")
        promptgroup("52 - Output 8-15")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B10,DBF_UCHAR) {
        prompt("Bit 16")
        promptgroup("53 - Output 16-23")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B11,DBF_UCHAR) {
        prompt("Bit 17")
        promptgroup("53 - Output 16-23")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B12,DBF_UCHAR) {
        prompt("Bit 18")
        promptgroup("53 - Output 16-23")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B13,DBF_UCHAR) {
        prompt("Bit 19")
        promptgroup("53 - Output 16-23")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B14,DBF_UCHAR) {
        prompt("Bit 20")
        promptgroup("53 - Output 16-23")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B15,DBF_UCHAR) {
        prompt("Bit 21")
        promptgroup("53 - Output 16-23")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B16,DBF_UCHAR) {
        prompt("Bit 22")
        promptgroup("53 - Output 16-23")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B17,DBF_UCHAR) {
        prompt("Bit 23")
        promptgroup("53 - Output 16-23")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B18,DBF_UCHAR) {
        prompt("Bit 24")
        promptgroup("54 - Output 24-31")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B19,DBF_UCHAR) {
        prompt("Bit 25")
        promptgroup("54 - Output 24-31")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B1A,DBF_UCHAR) {
        prompt("Bit 26")
        promptgroup("54 - Output 24-31")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B1B,DBF_UCHAR) {
        prompt("Bit 27")
        promptgroup("54 - Output 24-31")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B1C,DBF_UCHAR) {
        prompt("Bit 28")
        promptgroup("54 - Output 24-31")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B1D,DBF_UCHAR) {
        prompt("Bit 29")
        promptgroup("54 - Output 24-31")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B1E,DBF_UCHAR) {
        prompt("Bit 30")
        promptgroup("54 - Output 24-31")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }
    field(B1F,DBF_UCHAR) {
        prompt("Bit 31")
        promptgroup("54 - Output 24-31")
        asl(ASL0)
        special(SPC_MOD)
        pp(TRUE)
        interest(1)
    }

=head2 Record Support

=head3 Record Support Routines

=head4 init_record

This routine initializes SIMM if SIML is a constant or creates a channel access
link if SIML is PV_LINK. If SIOL is PV_LINK a channel access link is created.

This routine next checks to see that device support is available.The routine
next checks to see if the device support write routine is defined. If either
device support or the device support write routine does not exist, an error
message is issued and processing is terminated.

If DOL is a constant, then VAL is initialized to its value and UDF is set to
FALSE.

MASK is cleared and then the NOBT low order bits are set.

If device support includes C<init_record()>, it is called.

If device support returns success, VAL is then set from RVAL and UDF is set to
FALSE.

=head4 Process

See next section.

=head3 Record Processing

Routine process implements the following algorithm:

=over

=item 1.

Check to see that the appropriate device support module exists. If it doesn't,
an error message is issued and processing is terminated with the PACT field
still set to TRUE. This ensures that processes will no longer be called for this
record. Thus error storms will not occur.

=item 2.

If PACT is FALSE

=over

=item * If DOL is DB_LINK and OMSL is CLOSED_LOOP

=over

=item * Get value from DOL

=item * Set PACT to FALSE

=back

=back

=item 3.

Convert

=over

=item * If PACT is FALSE, compute RVAL

=over

=item * Set RVAL = VAL

=item * Shift RVAL left SHFT bits

=back

=item * Status=write_mbboDirect

=back

=item 4.

If PACT has been changed to TRUE, the device support write output routine has
started but has not completed writing the new value. In this case, the
processing routine merely returns, leaving PACT TRUE.

=item 5.

Check to see if monitors should be invoked.

=over

=item * Alarm monitors are invoked if the alarm status or severity has changed.

=item * Archive and value change monitors are invoked if MLST is not equal to VAL.

=item * Monitors for RVAL and RBV are checked whenever other monitors are invoked.

=item * NSEV and NSTA are reset to 0.

=back

=item 6.

Scan forward link if necessary, set PACT FALSE, and return.

=back

=begin html

<br><hr><br>

=end html

=head2 Device Support

=head3 Fields Of Interest To Device Support

Each mbboDirect record must have an associated set of device support routines.
The primary responsibility of the device support routines is to obtain a new raw
mbbo value whenever write_mbboDirect is called. The device support routines are
primarily interested in the following fields:

=fields PACT, DPVT, UDF, NSEV, NSTA, NOBT, OUT, RVAL, RBV, MASK, SHFT

=head3 Device Support Routines

Device support consists of the following routines:

=head4 long report(int level)

This optional routine is called by the IOC command C<dbior> and is passed the
report level that was requested by the user.
It should print a report on the state of the device support to stdout.
The C<level> parameter may be used to output increasingly more detailed
information at higher levels, or to select different types of information with
different levels.
Level zero should print no more than a small summary.

=head4 long init(int after)

This optional routine is called twice at IOC initialization time.
The first call happens before any of the C<init_record()> calls are made, with
the integer parameter C<after> set to 0.
The second call happens after all of the C<init_record()> calls have been made,
with C<after> set to 1.

=head4 init_record

  init_record(precord)

This routine is optional. If provided, it is called by the record support
C<init_record()> routine. If MASK is used, it should be shifted if necessary and
SHFT given a value.

=head4 get_ioint_info

  get_ioint_info(int cmd,struct dbCommon *precord,IOSCANPVT *ppvt)

This routine is called by the ioEventScan system each time the record is added
or deleted from an I/O event scan list. C<cmd> has the value (0,1) if the
record is being (added to, deleted from) an I/O event list. It must be
provided for any device type that can use the ioEvent scanner.

=head4 write_mbboDirect

  write_mbboDirect(precord)

This routine must output a new value. It returns the following values:

=over

=item * 0: Success.

=item * Other: Error.

=back

=head3 Device Support For Soft Records

This C<<< SOft Channel >>> module writes the current value of VAL.

If the OUT link type is PV_LINK, then dbCaAddInlink is called by
C<init_record()>.

write_mbboDirect calls recGblPutLinkValue to write the current value of VAL.

See L<Soft Output|Soft_Output>.

=cut

}
