← All exercises

Mux 4 with Fallback

medium

Route one of four inputs by select value, falling back to a safe default and raising a fault flag when the select is out of range.

What it teaches

Defensive routing — real selector values come from HMIs and networks and can be garbage. In range, behave like Mux 4; out of range, route the configured fallback value and raise a fault flag instead of acting on a stale or random source. The validate-then-act guard that wraps every external command in production code.

Inputs and outputs

TagTypeDefaultDescription
In_SelectDINT0Selector index
In_Source0REAL5First input value
In_Source1REAL10Second input value
In_Source2REAL20Third input value
In_Source3REAL30Fourth input value
Cfg_FallbackREAL-99Safe default value routed when selector is out of range
Out_SelectedREALRouted value
Sts_FaultBOOLOut of range fault flag

Required behavior

  • Out_Selected equals In_Source0 when In_Select is 0.
  • Out_Selected equals In_Source1 when In_Select is 1.
  • Out_Selected equals In_Source2 when In_Select is 2.
  • Out_Selected equals In_Source3 when In_Select is 3.
  • Out_Selected equals Cfg_Fallback when In_Select is less than 0 or greater than 3.
  • Sts_Fault is 1 when In_Select is less than 0 or greater than 3.
  • Sts_Fault is 0 when In_Select is between 0 and 3 inclusive.