In a current project I am working on, I am stuck with using a family of uC's that do not have built-in IR modulation logic. I know I can use timers & compare registers to do ASK/OOK modulation, albeit the solution is messy to make the carrier & envelope signals work together in the uC & firmware. So I thought of a possible solution that may make the modulation easier using external discreet external logic, but precisely what logic to use in what combination confuses me.
Thus I am asking for help from all you digitally-minded folk out there in EEV-Land!
Assuming that:
(a) There is always present a 36kHz carrier signal on a uC pin;
(b) The envelope signal would have a minimum HIGH or LOW time of >= 1 ms;
(c) If the envelope signal goes high in the middle of a carrier pulse HIGH time, the output is latched low until the next rising edge of the carrier, at which point the output goes high
(d) If the envelope signal goes low in the middle of a carrier pulse HIGH time, the output is latched high until the next falling edge of the carrier, at which point the output goes low
To illustrate this behavior with a diagram:

Area labeled #1: Output waits until the next rising edge of the carrier before it is asserted
Area labeled #2: Output waits until the next falling edge of the carrier before it is de-asserted
Area labeled #3 & #4: Normal gating operation of envelope on carrier
Given that the carrier and envelope already exist on the uC pins, how would the output waveform be constructed using the minimum number of flip-flops & logic gates?