module M65C02_IntHndlr #(
    parameter pIRQ_Vector = 16'hFFFE,
    parameter pBRK_Vector = 16'hFFFE,
    parameter pRST_Vector = 16'hFFFC,
    parameter pNMI_Vector = 16'hFFFA,
    parameter pBRK        = 3'b010
)(
    input   Rst,
    input   Clk,
    
    input   nNMI,
    input   nIRQ,
    input   [2:0] Mode,
    
    input   IRQ_Msk,
    input   IntSvc,
    
    output  reg Int,
    output  reg [15:0] Vector,
    
    output  reg NMI,
    output  reg IRQ,
    output  reg Brk
);

////////////////////////////////////////////////////////////////////////////////
//
//  Local Declarations
//

wire    RE_NMI;
wire    CE_NMI;
reg     nIRQ_IFD;
     

//  Perform falling edge detection on the external non-maskable interrupt input

fedet   FE3 (
            .rst(Rst), 
            .clk(Clk), 
            .din(nNMI), 
            .pls(RE_NMI)
        );

//  Capture and hold the rising edge pulse for NMI in NMI FF until serviced by
//      the processor.

assign CE_NMI = (Rst | IntSvc | RE_NMI);
always @(posedge Clk) NMI <= #1 ((CE_NMI) ? RE_NMI : 0);

//  Synchronize external IRQ input to Clk

always @(posedge Clk or posedge Rst)
begin
    if(Rst) begin
        nIRQ_IFD <= #1 1;
        IRQ      <= #1 0;
    end else begin
        nIRQ_IFD <= #1 nIRQ;
        IRQ      <= #1 ~nIRQ_IFD;
    end
end

//assign Brk    = (Mode == pBRK);
//assign Int    = (NMI | (~IRQ_Msk & IRQ));
//
//always @(*) Vector = ((Int) ? ((NMI) ? pNMI_Vector
//                                     : pIRQ_Vector)
//                            : ((Brk) ? pBRK_Vector
//                                     : pRST_Vector));

always @(posedge Clk or posedge Rst)
begin
    if(Rst) begin
        Brk    <= #1 0;
        Int    <= #1 0;
        Vector <= #1 pRST_Vector;
    end else begin
        Brk    <= #1 (Mode == pBRK);
        Int    <= #1 (NMI | (~IRQ_Msk & IRQ));
        Vector <= #1 ((Int) ? ((NMI) ? pNMI_Vector
                                     : pIRQ_Vector)
                            : ((Brk) ? pBRK_Vector
                                     : pRST_Vector));
    end
end
                       
endmodule