A simple function for computing FlexIO Shifter values in State Machine mode

DrItanium

Member
I am using a teensy 4.1 to act as the "Chipset" and memory interface for an Intel i960SB-16 (running at 12MHz). It is combined with an AVR128DB64 for extended strict timing functionality (CCLs are wonderful). Anyway, I have FlexIO1 and FlexIO2 running in state machine mode and I adapted a function from the AVR code to make generating truth tables easier. Here is the code I am using:

C++:
#include <functional>
#include <cstdint>

uint32_t
computeStateMachineBuffer(uint8_t outputs, std::function<uint8_t(bool, bool, bool)> fn) noexcept {
    uint32_t result = static_cast<uint32_t>(outputs) << 24;
    for (int i = 0; i < 8; ++i) {
        result |= static_cast<uint32_t>(fn(
                i & 0b001,
                i & 0b010,
                i & 0b100
            )) << (i * 3);
    }
    return result;
}

Then you can use it in something like this:

C++:
p->SHIFTBUF[_state0] = computeStateMachineBuffer(0xff,
                                                    [this](bool ads, bool, bool) -> uint8_t {
                                                        return ads ? _state0 : _state1;
                                                    }
                                                );
This can be seen in full at: https://github.com/DrItanium/SxChipset_V3/blob/main/projects/Chipset/src/main.cc#L1961

This function really helped me stop worrying about the exact value to write into the shifter and look at it in terms of the expression I wanted to model. Hopefully it will help someone.

NOTE: While it is not impossible to adapt it to operate in a constexpr context (the AVR version does), the use of lambda captures was necessary since I am using the FlexIO_t4 library.
 
Back
Top