Altera Code to Demultiplex HC12 Address and Data Busses, and to Select Odd and Even Memory Chips


SUBDESIGN demux_cs
(
    EXP_ENn         :  INPUT;   % Memory expansion enabled if low %

    E               :  INPUT;   % E-Clock %
    R_W             :  INPUT;   % R/W Line %
    RESETn          :  INPUT;   % Reset line %
    LSTRBn          :  INPUT;   % Let's us tell if 8-bit or 16-bit access %

    WEn             :  OUTPUT;  % Write Enable to memory %
    OEn             :  OUTPUT;  % Output Enable to memory %
    CS_MEM_ODDn     :  OUTPUT;  % Chip Select for odd addresses %
    CS_MEM_EVENn    :  OUTPUT;  % Chip Select for even addresses %
    A[15..1]        :  OUTPUT;  % Demultiplexed address bits % 
)


VARIABLE
    demux[15..0]    :  DFF;    % Demuliplexed address internal %
    CS_MEM          :  NODE;   % Tell's when address in range of memory %

BEGIN

% ***********************************************************************%
% Address decoding and demultiplexing                                    %
% Latch address on rising edge of E clock                                %
% ***********************************************************************%

    demux[15..8].d = PA[7..0];
    demux[7..0].d = PB[7..0];
    demux[15..0].clk = E; 
    A[15..1] = demux[15..1].q; % Don't need LSB of address %
    
% Enable writes when E high and R/W low %

    IF (EXP_ENn == GND) & (E == VCC) & (R_W == GND) THEN
        WEn = GND;
    ELSE
        WEn = VCC;
    END IF;

% Enable reads when E high and R/W high %

    IF (EXP_ENn == GND) & (E == VCC) & (R_W == VCC) THEN
        OEn = GND;
    ELSE
        OEn = VCC;
    END IF;

% Access external memory when addresses in the range 0x1000 to 0x7fff %

    IF (EXP_ENn==GND) & (E==VCC) &
        ((demux[15..0].q >= H"0000") & (demux[15..0].q <= H"7FFF")) THEN
            CS_MEM = VCC;
    ELSE
            CS_MEM = GND;
    END IF;

% Access even memory locations when address is even %

    IF (CS_MEM == VCC) & (demux0.q == GND) THEN
            CS_MEM_EVENn = GND;
    ELSE
            CS_MEM_EVENn = VCC;
    END IF;

% Access odd memory locations on 16-bit access to even addresses or %
% eight-bit accesses to odd addresses %

    IF (CS_MEM == VCC) & (LSTRBn == GND) THEN
            CS_MEM_ODDn = GND;
    ELSE
            CS_MEM_ODDn = VCC;
    END IF;

END;