data/full_repos/permissive/163670715/ctl.v
None: at end of input
1: b'%Error: data/full_repos/permissive/163670715/ctl.v:27: Cannot find include file: states.i\n`include "states.i" \n         ^~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/163670715,data/full_repos/permissive/163670715/states.i\n             data/full_repos/permissive/163670715,data/full_repos/permissive/163670715/states.i.v\n             data/full_repos/permissive/163670715,data/full_repos/permissive/163670715/states.i.sv\n             states.i\n             states.i.v\n             states.i.sv\n             obj_dir/states.i\n             obj_dir/states.i.v\n             obj_dir/states.i.sv\n%Error: Exiting due to 1 error(s)\n'
module ctl( 
    input clk,
    inout [7:0] DB,
    input RST,
    input IRQ,
    input RDY,
    input NMI,
    output reg WE,
    output reg [2:0] alu_sel,
    output reg [2:0] alu_op,
    output reg [2:0] alu_ld,
    output reg alu_ci,
    input alu_co,
    input alu_n,
    input alu_v,
    input alu_z,
    output tsx,
    output reg [4:0] ab_op );

`include "states.i"
















reg [3:0] state = FETCH;
reg [7:0] IR;
reg Z, N, C, V, I = 1, D;
reg cond_true;
reg rst;
reg irq;

always @(posedge clk)
    if( RDY )
        if( ~IRQ )               irq <= 1;
        else if( state == STK2 ) irq <= 0;

wire take_irq = (irq & ~I);

initial WE = 1;

wire [7:0] P = { N, V, 2'b11, D, I, Z, C };

assign tsx = (state == DECODE && IR == 8'hba);

reg jmp;
reg ind;
reg rmw;
reg load_ir;
reg load_c_alu;
reg load_n_alu;
reg load_z_alu;
reg load_v_alu;

wire indx = (IR[3:0] == 4'b0001);
wire brk = (IR == 8'h00);
wire clv = (IR == 8'hb8);
wire pha = (IR == 8'h48);
wire php = (IR == 8'h08);
wire plp = (IR == 8'h28);
wire rti = (IR == 8'h40);
wire cli = (IR == 8'h58);
wire sei = (IR == 8'h78);
wire clc = (IR == 8'h18);
wire sec = (IR == 8'h38);
wire cld = (IR == 8'hd8);
wire sed = (IR == 8'hf8);

wire cli_sei = cli | sei;
wire clc_sec = clc | sec;
wire cld_sed = cld | sed;

wire load_p = (state == STK0 && plp) || (state == STK0 && rti);
wire write_p = (state == STK0 && php) || (state == STK2 && brk);
wire do_bit = state == DATA && (IR == 8'h24 || IR == 8'h2c);
wire pull = IR[3] ? IR[5]: IR[6];
assign DB = write_p ? P: 8'hzz;


always @(posedge clk)
    if( RDY )
        if( state == DECODE )
            casez( IR )
                8'b0???_?110:           rmw <= 1;
                8'b11??_?110:           rmw <= 1;
                default:                rmw <= 0;
            endcase
        else if( state == DATA )        rmw <= 0;


always @(posedge clk)
    if( RDY )
        if( state == DECODE )
            casez( IR )
                8'b??0?_0000:           ind <= 1;
                8'b011?_??00:           ind <= 1;
                default:                ind <= 0;
            endcase
        else if( state == IND0 )        ind <= 0;


always @* begin
    jmp = 0;
    casez( IR )
        8'b????_0000:                   jmp = 1;
        8'b01??_??00:                   jmp = 1;
    endcase
end


always @* begin
    alu_sel = SEL_0;
    case( state )
        BRA0:                           alu_sel = SEL_MEM;

        IND0:
            casez( IR )
                8'b???1_????:           alu_sel = SEL_CPX;
            endcase

        STK0:
            casez( IR )                 
                8'b????_??0?:           alu_sel = SEL_CMP;
                8'b0???_??1?:           alu_sel = SEL_CPY;
                8'b1???_??1?:           alu_sel = SEL_CPX;
            endcase

        ABS0:        
            casez( IR )
                8'b10?1_??10:           alu_sel = SEL_CPY;
                8'b???1_?0??:           alu_sel = SEL_CPY;
                8'b0111_1100: if( ind ) alu_sel = SEL_CPX;
                              else      alu_sel = SEL_0;
                8'b???1_?1??:           alu_sel = SEL_CPX;
            endcase

        DECODE:         
            casez( IR )
                8'b1?0?_1000:           alu_sel = SEL_INY;
                8'b111?_1000:           alu_sel = SEL_INX;
                8'b00?1_1010:           alu_sel = SEL_0;
                8'b1?0?_1010:           alu_sel = SEL_INX;
                8'b1010_10?0:           alu_sel = SEL_CMP;
                8'b0???_1010:           alu_sel = SEL_CMP;
                8'b10?1_??10:           alu_sel = SEL_CPY;
                8'b???1_?1??:           alu_sel = SEL_CPX;
                8'b???0_?0??:           alu_sel = SEL_CPX;
            endcase

        INDX0:          
            casez( IR )
                8'b???0_????:           alu_sel = SEL_CPX;
            endcase

        DATA, FETCH:                        
            casez( IR )
                8'b11??_??01:           alu_sel = SEL_CMP;
                8'b1?0?_??00:           alu_sel = SEL_CPY;
                8'b111?_??00:           alu_sel = SEL_CPX;
                8'b101?_????:           alu_sel = SEL_MEM;
                8'b100?_??10:           alu_sel = SEL_CPX;
                8'b0010_?100:           alu_sel = SEL_ADD;
                8'b????_??01:           alu_sel = SEL_ADD;
                    default:            alu_sel = SEL_MEM;
            endcase
    endcase
end


always @* 
    casez( IR )
        8'b?1?1_1010:                   alu_op = OP_AI;
        8'b1000_1000:                   alu_op = OP_ADC;
        8'b001?_??00:                   alu_op = OP_AND;
        8'b?11?_??01:  if( D )          alu_op = OP_BCD;
                       else             alu_op = OP_ADC;
        8'b11??_????:                   alu_op = OP_ADC;
        8'b000?_??01:                   alu_op = OP_ORA;
        8'b001?_??01:                   alu_op = OP_AND;
        8'b010?_??01:                   alu_op = OP_EOR;
        8'b00?1_1010:                   alu_op = OP_ADC;
        8'b01??_??10:                   alu_op = OP_ROR;
        8'b00??_??10:                   alu_op = OP_ROL;
             default:                   alu_op = OP_AI;
    endcase


always @* begin
    alu_ld = 0;
    case( state )
        STK0:           
            casez( IR )
                8'b?10?_1???:           alu_ld = LD_M;
            endcase

        DATA:           
            if( !WE )                   alu_ld = LD_M;

        DECODE:         
            casez( IR )
                8'b1000_1010:           alu_ld = LD_A;
                8'b1001_1000:           alu_ld = LD_A;
                8'b0???_1010:           alu_ld = LD_A;
                8'b101?_1010:           alu_ld = LD_X;
                8'b110?_1010:           alu_ld = LD_X;
                8'b1110_1000:           alu_ld = LD_X;
                8'b1??0_1000:           alu_ld = LD_Y;
            endcase

        FETCH:          
            casez( IR )
                8'b0???_??01:           alu_ld = LD_A;
                8'b101?_??01:           alu_ld = LD_A;
                8'b111?_??01:           alu_ld = LD_A;
                8'b0111_1010:           alu_ld = LD_Y;
                8'b1111_1010:           alu_ld = LD_X;
                8'b?11?_10??:           alu_ld = LD_A;
                8'b101?_??10:           alu_ld = LD_X;
                8'b1010_??00:           alu_ld = LD_Y;
                8'b1011_?100:           alu_ld = LD_Y;
            endcase 
    endcase
end


always @* begin
    casez( IR )
        8'b0?1?_?110:                   alu_ci = C;
        8'b110?_?110:                   alu_ci = 0;
        8'b111?_?110:                   alu_ci = 1;
        8'b1100_1010:                   alu_ci = 0;
        8'b1000_1000:                   alu_ci = 0;
        8'b1110_1000:                   alu_ci = 1;
        8'b1100_1000:                   alu_ci = 1;
        8'b0001_1010:                   alu_ci = 1;
        8'b0011_1010:                   alu_ci = 0;
        8'b0?10_1010:                   alu_ci = C;
        8'b?11?_??01:                   alu_ci = C;
        8'b110?_??01:                   alu_ci = 1;
        8'b11??_??00:                   alu_ci = 1;
        default:                        alu_ci = 0;
    endcase
end


always @* begin
    load_z_alu = load_n_alu;
    if( state == FETCH )
        casez( IR )
            8'b0010_?100:               load_z_alu = 1;
        endcase
end


always @* begin
    load_n_alu = 0;
    case( state )
        FETCH:          
            casez( IR )
                8'b0110_1000:           load_n_alu = 1;
                8'b0???_??01:           load_n_alu = 1;
                8'b101?_??01:           load_n_alu = 1;
                8'b11??_??01:           load_n_alu = 1;
                8'b1010_00?0:           load_n_alu = 1;
                8'b101?_?1?0:           load_n_alu = 1;
                8'b11?0_0000:           load_n_alu = 1;
                8'b11??_?100:           load_n_alu = 1;
            endcase
        
        DECODE:         
            casez( IR )
                8'b0???_1010:           load_n_alu = 1;
                8'b1?00_10?0:           load_n_alu = 1;
                8'b1010_10?0:           load_n_alu = 1;
                8'b1001_1000:           load_n_alu = 1; 
                8'b1011_1010:           load_n_alu = 1;
                8'b1110_1000:           load_n_alu = 1;
            endcase

        DATA:        
            casez( IR )
                8'b0???_?110:           load_n_alu = !WE; 
                8'b11??_?110:           load_n_alu = !WE;
            endcase
    endcase
end


always @* begin
    load_v_alu = 0;
    case( state )
        FETCH:          
            casez( IR )
                8'b?11?_??01:           load_v_alu = 1;
            endcase
    endcase
end


always @* begin
    load_c_alu = 0;
    case( state )
        DECODE: 
            casez( IR )
                8'b0??0_1010:           load_c_alu = 1;
            endcase

        DATA:   
            if( !WE ) 
            casez( IR ) 
                8'b0???_?110:           load_c_alu = 1;
            endcase

        FETCH:  
            casez( IR ) 
                8'b011?_??01:           load_c_alu = 1;
                8'b11??_??01:           load_c_alu = 1;
                8'b11?0_??00:           load_c_alu = 1;
            endcase
    endcase
end



always @(posedge clk)
    if( RDY )
        if( load_p )                    Z <= DB[1];
        else if( load_z_alu )           Z <= alu_z;

always @(posedge clk)
    if( RDY )

        if( load_p )                    D <= DB[3];
        else if( cld_sed )              D <= IR[5];

always @(posedge clk)
    if( RDY )
        if( brk && state == STK2 )      I <= 1;
        else if( load_p )               I <= DB[2];
        else if( cli_sei )              I <= IR[5];
        
always @(posedge clk)
    if( RDY )
        if( load_p | do_bit )           N <= DB[7];
        else if( load_n_alu )           N <= alu_n;

always @(posedge clk)
    if( RDY )
        if( load_p | do_bit )           V <= DB[6];
        else if( clv )                  V <= 0;
        else if( load_v_alu )           V <= alu_v;

always @(posedge clk)
    if( RDY )
        if( load_p )                    C <= DB[0]; 
        else if( load_c_alu )           C <= alu_co;
        else if( clc_sec )              C <= IR[5];



always @(posedge clk)
    if( RDY )
        if( !RST )                      rst <= 1;
        else if( state == STK2 )        rst <= 0;



always @* begin
    load_ir = 0; 
    case( state )
        FETCH:                          load_ir = 1;
        DECODE:       
            if( rst )
                load_ir = 1;
            else casez( IR )
                8'b?1?1_1010:           load_ir = 0;
                8'b0??0_1000:           load_ir = 0;
                8'b????_10?0:           load_ir = 1; 
            endcase
    endcase
end


always @(posedge clk)
    if( RDY & load_ir )
        if( rst | take_irq )            IR <= 0;
        else if( load_ir )              IR <= DB;


always @(posedge clk)
    if( RDY ) case( state )
        DATA:                           WE <= !rmw;

        ABS0:              
            casez( IR )
                8'b100?_????:           WE <= 0;
                default:                WE <= 1;
            endcase

        DECODE:
            casez( IR )
                8'b00?0_0000:           WE <= 0;
                8'b0?00_1000:           WE <= 0;
                8'b?101_1010:           WE <= 0;
                8'b100?_01??:           WE <= 0;
                default:                WE <= 1;
            endcase                 

        STK0:                           WE <= !(!IR[6] && !IR[3]);

        STK1:                           WE <= !(!IR[6] && !IR[5]); 
        default:                        WE <= 1;
    endcase



always @* begin
    ab_op = 0;
    case( state )
        DECODE: 
            casez( IR )
                8'b????_01??:           ab_op = AB_ZPXY;
                8'b????_0001:           ab_op = AB_ZPXY;
                8'b0100_0000:           ab_op = AB_PLA;
                8'b0110_0000:           ab_op = AB_PLA;
                8'b0010_1000:           ab_op = AB_PLA;
                8'b0110_1000:           ab_op = AB_PLA;
                8'b?111_1010:           ab_op = AB_PLA;
                8'b0000_0000:           ab_op = take_irq ? AB_PHA : AB_BRK;
                8'b0010_0000:           ab_op = AB_BRK;
                8'b0000_1000:           ab_op = AB_PHA;
                8'b0100_1000:           ab_op = AB_PHA;
                8'b?101_1010:           ab_op = AB_PHA;
                8'b1001_1010:           ab_op = AB_TXS;
                default:                ab_op = take_irq & load_ir ? AB_IRQ0 : AB_FETCH;
            endcase

        STK0:               
            casez( IR )
                8'b?1??_0???:           ab_op = AB_RTS0;
                8'b?0??_0???:           ab_op = AB_JSR0;
                8'b????_1???:           ab_op = AB_DATA;
            endcase
        
        STK1:
            casez( IR )
                8'h00:                  ab_op = AB_BRK1;
                8'h20:                  ab_op = AB_JSR1;
                8'h40:                  ab_op = AB_RTS0;
                8'h60:                  ab_op = AB_RTS1;
            endcase

        STK2:
            if( pull )                  ab_op = AB_JMP0;
            else if( rst )              ab_op = AB_RST;
            else                        ab_op = AB_BRK2;

        FETCH: if( take_irq )           ab_op = AB_IRQ0;
               else                     ab_op = AB_FETCH; 
        INDX0:                          ab_op = AB_INDX0;    

        ABS0:
            if( jmp )                   ab_op = AB_JMP0;
            else if( indx )             ab_op = AB_INDX1;
            else                        ab_op = AB_ABS0;

        DATA:
            if( rmw )                   ab_op = AB_RMW;
            else                        ab_op = AB_DATA;
        BRA0:                           ab_op = AB_BRA0; 
        IND0:                           ab_op = AB_IND0;
    endcase
end



always @(posedge clk)
    if( RDY )
    if( !RST )                          state <= FETCH;
    else case( state )
        DECODE: 
            casez( IR )
                8'b?1?1_1010:           state <= STK0;
                8'b0??0_?000:           state <= STK0;
                8'b1000_0000:           state <= BRA0;
                8'b???1_0000:           state <= cond_true ? BRA0: FETCH;
                8'b????_0001:           state <= INDX0;
                8'b1??0_00?0:           state <= FETCH;
                8'b????_01??:           state <= DATA;
                8'b???0_1001:           state <= FETCH;
                8'b???1_1001:           state <= ABS0;
                8'b????_11??:           state <= ABS0;
            endcase

        STK0: 
            if( ~IR[3] )                state <= STK1;
            else                        state <= FETCH;
    
        STK1: 
            if( ~IR[5] )                state <= STK2;
            else if( pull )             state <= FETCH;
            else                        state <= ABS0;

        STK2: 
            if( pull )                  state <= FETCH;
            else                        state <= IND0;

        DATA: 
            if( !rmw )                  state <= FETCH;
        BRA0:                           state <= FETCH;
        FETCH:                          state <= DECODE;
        INDX0:                          state <= ABS0; 
        IND0:                           state <= ABS0;
        ABS0: 
            if( ind )                   state <= IND0;
            else if( jmp )              state <= FETCH;
            else                        state <= DATA;
    endcase



always @*
    casez( IR[7:4] )
        4'b000?:                        cond_true = ~N;
        4'b001?:                        cond_true = N;
        4'b010?:                        cond_true = ~V;
        4'b011?:                        cond_true = V;
        4'b1000:                        cond_true = 1;
        4'b1001:                        cond_true = ~C;
        4'b101?:                        cond_true = C;
        4'b110?:                        cond_true = ~Z;
        4'b111?:                        cond_true = Z;
    endcase

endmodule
module ctl( 
    input clk,
    inout [7:0] DB,
    input RST,
    input IRQ,
    input RDY,
    input NMI,
    output reg WE,
    output reg [2:0] alu_sel,
    output reg [2:0] alu_op,
    output reg [2:0] alu_ld,
    output reg alu_ci,
    input alu_co,
    input alu_n,
    input alu_v,
    input alu_z,
    output tsx,
    output reg [4:0] ab_op );


`include "states.i"
















reg [3:0] state = FETCH;
reg [7:0] IR;
reg Z, N, C, V, I = 1, D;
reg cond_true;
reg rst;
reg irq;

always @(posedge clk)
    if( RDY )
        if( ~IRQ )               irq <= 1;
        else if( state == STK2 ) irq <= 0;

wire take_irq = (irq & ~I);

initial WE = 1;

wire [7:0] P = { N, V, 2'b11, D, I, Z, C };

assign tsx = (state == DECODE && IR == 8'hba);

reg jmp;
reg ind;
reg rmw;
reg load_ir;
reg load_c_alu;
reg load_n_alu;
reg load_z_alu;
reg load_v_alu;

wire indx = (IR[3:0] == 4'b0001);
wire brk = (IR == 8'h00);
wire clv = (IR == 8'hb8);
wire pha = (IR == 8'h48);
wire php = (IR == 8'h08);
wire plp = (IR == 8'h28);
wire rti = (IR == 8'h40);
wire cli = (IR == 8'h58);
wire sei = (IR == 8'h78);
wire clc = (IR == 8'h18);
wire sec = (IR == 8'h38);
wire cld = (IR == 8'hd8);
wire sed = (IR == 8'hf8);

wire cli_sei = cli | sei;
wire clc_sec = clc | sec;
wire cld_sed = cld | sed;

wire load_p = (state == STK0 && plp) || (state == STK0 && rti);
wire write_p = (state == STK0 && php) || (state == STK2 && brk);
wire do_bit = state == DATA && (IR == 8'h24 || IR == 8'h2c);
wire pull = IR[3] ? IR[5]: IR[6];
assign DB = write_p ? P: 8'hzz;


always @(posedge clk)
    if( RDY )
        if( state == DECODE )
            casez( IR )
                8'b0???_?110:           rmw <= 1;
                8'b11??_?110:           rmw <= 1;
                default:                rmw <= 0;
            endcase
        else if( state == DATA )        rmw <= 0;


always @(posedge clk)
    if( RDY )
        if( state == DECODE )
            casez( IR )
                8'b??0?_0000:           ind <= 1;
                8'b011?_??00:           ind <= 1;
                default:                ind <= 0;
            endcase
        else if( state == IND0 )        ind <= 0;


always @* begin
    jmp = 0;
    casez( IR )
        8'b????_0000:                   jmp = 1;
        8'b01??_??00:                   jmp = 1;
    endcase
end


always @* begin
    alu_sel = SEL_0;
    case( state )
        BRA0:                           alu_sel = SEL_MEM;

        IND0:
            casez( IR )
                8'b???1_????:           alu_sel = SEL_CPX;
            endcase

        STK0:
            casez( IR )                 
                8'b????_??0?:           alu_sel = SEL_CMP;
                8'b0???_??1?:           alu_sel = SEL_CPY;
                8'b1???_??1?:           alu_sel = SEL_CPX;
            endcase

        ABS0:        
            casez( IR )
                8'b10?1_??10:           alu_sel = SEL_CPY;
                8'b???1_?0??:           alu_sel = SEL_CPY;
                8'b0111_1100: if( ind ) alu_sel = SEL_CPX;
                              else      alu_sel = SEL_0;
                8'b???1_?1??:           alu_sel = SEL_CPX;
            endcase

        DECODE:         
            casez( IR )
                8'b1?0?_1000:           alu_sel = SEL_INY;
                8'b111?_1000:           alu_sel = SEL_INX;
                8'b00?1_1010:           alu_sel = SEL_0;
                8'b1?0?_1010:           alu_sel = SEL_INX;
                8'b1010_10?0:           alu_sel = SEL_CMP;
                8'b0???_1010:           alu_sel = SEL_CMP;
                8'b10?1_??10:           alu_sel = SEL_CPY;
                8'b???1_?1??:           alu_sel = SEL_CPX;
                8'b???0_?0??:           alu_sel = SEL_CPX;
            endcase

        INDX0:          
            casez( IR )
                8'b???0_????:           alu_sel = SEL_CPX;
            endcase

        DATA, FETCH:                        
            casez( IR )
                8'b11??_??01:           alu_sel = SEL_CMP;
                8'b1?0?_??00:           alu_sel = SEL_CPY;
                8'b111?_??00:           alu_sel = SEL_CPX;
                8'b101?_????:           alu_sel = SEL_MEM;
                8'b100?_??10:           alu_sel = SEL_CPX;
                8'b0010_?100:           alu_sel = SEL_ADD;
                8'b????_??01:           alu_sel = SEL_ADD;
                    default:            alu_sel = SEL_MEM;
            endcase
    endcase
end


always @* 
    casez( IR )
        8'b?1?1_1010:                   alu_op = OP_AI;
        8'b1000_1000:                   alu_op = OP_ADC;
        8'b001?_??00:                   alu_op = OP_AND;
        8'b?11?_??01:  if( D )          alu_op = OP_BCD;
                       else             alu_op = OP_ADC;
        8'b11??_????:                   alu_op = OP_ADC;
        8'b000?_??01:                   alu_op = OP_ORA;
        8'b001?_??01:                   alu_op = OP_AND;
        8'b010?_??01:                   alu_op = OP_EOR;
        8'b00?1_1010:                   alu_op = OP_ADC;
        8'b01??_??10:                   alu_op = OP_ROR;
        8'b00??_??10:                   alu_op = OP_ROL;
             default:                   alu_op = OP_AI;
    endcase


always @* begin
    alu_ld = 0;
    case( state )
        STK0:           
            casez( IR )
                8'b?10?_1???:           alu_ld = LD_M;
            endcase

        DATA:           
            if( !WE )                   alu_ld = LD_M;

        DECODE:         
            casez( IR )
                8'b1000_1010:           alu_ld = LD_A;
                8'b1001_1000:           alu_ld = LD_A;
                8'b0???_1010:           alu_ld = LD_A;
                8'b101?_1010:           alu_ld = LD_X;
                8'b110?_1010:           alu_ld = LD_X;
                8'b1110_1000:           alu_ld = LD_X;
                8'b1??0_1000:           alu_ld = LD_Y;
            endcase

        FETCH:          
            casez( IR )
                8'b0???_??01:           alu_ld = LD_A;
                8'b101?_??01:           alu_ld = LD_A;
                8'b111?_??01:           alu_ld = LD_A;
                8'b0111_1010:           alu_ld = LD_Y;
                8'b1111_1010:           alu_ld = LD_X;
                8'b?11?_10??:           alu_ld = LD_A;
                8'b101?_??10:           alu_ld = LD_X;
                8'b1010_??00:           alu_ld = LD_Y;
                8'b1011_?100:           alu_ld = LD_Y;
            endcase 
    endcase
end


always @* begin
    casez( IR )
        8'b0?1?_?110:                   alu_ci = C;
        8'b110?_?110:                   alu_ci = 0;
        8'b111?_?110:                   alu_ci = 1;
        8'b1100_1010:                   alu_ci = 0;
        8'b1000_1000:                   alu_ci = 0;
        8'b1110_1000:                   alu_ci = 1;
        8'b1100_1000:                   alu_ci = 1;
        8'b0001_1010:                   alu_ci = 1;
        8'b0011_1010:                   alu_ci = 0;
        8'b0?10_1010:                   alu_ci = C;
        8'b?11?_??01:                   alu_ci = C;
        8'b110?_??01:                   alu_ci = 1;
        8'b11??_??00:                   alu_ci = 1;
        default:                        alu_ci = 0;
    endcase
end


always @* begin
    load_z_alu = load_n_alu;
    if( state == FETCH )
        casez( IR )
            8'b0010_?100:               load_z_alu = 1;
        endcase
end


always @* begin
    load_n_alu = 0;
    case( state )
        FETCH:          
            casez( IR )
                8'b0110_1000:           load_n_alu = 1;
                8'b0???_??01:           load_n_alu = 1;
                8'b101?_??01:           load_n_alu = 1;
                8'b11??_??01:           load_n_alu = 1;
                8'b1010_00?0:           load_n_alu = 1;
                8'b101?_?1?0:           load_n_alu = 1;
                8'b11?0_0000:           load_n_alu = 1;
                8'b11??_?100:           load_n_alu = 1;
            endcase
        
        DECODE:         
            casez( IR )
                8'b0???_1010:           load_n_alu = 1;
                8'b1?00_10?0:           load_n_alu = 1;
                8'b1010_10?0:           load_n_alu = 1;
                8'b1001_1000:           load_n_alu = 1; 
                8'b1011_1010:           load_n_alu = 1;
                8'b1110_1000:           load_n_alu = 1;
            endcase

        DATA:        
            casez( IR )
                8'b0???_?110:           load_n_alu = !WE; 
                8'b11??_?110:           load_n_alu = !WE;
            endcase
    endcase
end


always @* begin
    load_v_alu = 0;
    case( state )
        FETCH:          
            casez( IR )
                8'b?11?_??01:           load_v_alu = 1;
            endcase
    endcase
end


always @* begin
    load_c_alu = 0;
    case( state )
        DECODE: 
            casez( IR )
                8'b0??0_1010:           load_c_alu = 1;
            endcase

        DATA:   
            if( !WE ) 
            casez( IR ) 
                8'b0???_?110:           load_c_alu = 1;
            endcase

        FETCH:  
            casez( IR ) 
                8'b011?_??01:           load_c_alu = 1;
                8'b11??_??01:           load_c_alu = 1;
                8'b11?0_??00:           load_c_alu = 1;
            endcase
    endcase
end



always @(posedge clk)
    if( RDY )
        if( load_p )                    Z <= DB[1];
        else if( load_z_alu )           Z <= alu_z;

always @(posedge clk)
    if( RDY )

        if( load_p )                    D <= DB[3];
        else if( cld_sed )              D <= IR[5];

always @(posedge clk)
    if( RDY )
        if( brk && state == STK2 )      I <= 1;
        else if( load_p )               I <= DB[2];
        else if( cli_sei )              I <= IR[5];
        
always @(posedge clk)
    if( RDY )
        if( load_p | do_bit )           N <= DB[7];
        else if( load_n_alu )           N <= alu_n;

always @(posedge clk)
    if( RDY )
        if( load_p | do_bit )           V <= DB[6];
        else if( clv )                  V <= 0;
        else if( load_v_alu )           V <= alu_v;

always @(posedge clk)
    if( RDY )
        if( load_p )                    C <= DB[0]; 
        else if( load_c_alu )           C <= alu_co;
        else if( clc_sec )              C <= IR[5];



always @(posedge clk)
    if( RDY )
        if( !RST )                      rst <= 1;
        else if( state == STK2 )        rst <= 0;



always @* begin
    load_ir = 0; 
    case( state )
        FETCH:                          load_ir = 1;
        DECODE:       
            if( rst )
                load_ir = 1;
            else casez( IR )
                8'b?1?1_1010:           load_ir = 0;
                8'b0??0_1000:           load_ir = 0;
                8'b????_10?0:           load_ir = 1; 
            endcase
    endcase
end


always @(posedge clk)
    if( RDY & load_ir )
        if( rst | take_irq )            IR <= 0;
        else if( load_ir )              IR <= DB;


always @(posedge clk)
    if( RDY ) case( state )
        DATA:                           WE <= !rmw;

        ABS0:              
            casez( IR )
                8'b100?_????:           WE <= 0;
                default:                WE <= 1;
            endcase

        DECODE:
            casez( IR )
                8'b00?0_0000:           WE <= 0;
                8'b0?00_1000:           WE <= 0;
                8'b?101_1010:           WE <= 0;
                8'b100?_01??:           WE <= 0;
                default:                WE <= 1;
            endcase                 

        STK0:                           WE <= !(!IR[6] && !IR[3]);

        STK1:                           WE <= !(!IR[6] && !IR[5]); 
        default:                        WE <= 1;
    endcase



always @* begin
    ab_op = 0;
    case( state )
        DECODE: 
            casez( IR )
                8'b????_01??:           ab_op = AB_ZPXY;
                8'b????_0001:           ab_op = AB_ZPXY;
                8'b0100_0000:           ab_op = AB_PLA;
                8'b0110_0000:           ab_op = AB_PLA;
                8'b0010_1000:           ab_op = AB_PLA;
                8'b0110_1000:           ab_op = AB_PLA;
                8'b?111_1010:           ab_op = AB_PLA;
                8'b0000_0000:           ab_op = take_irq ? AB_PHA : AB_BRK;
                8'b0010_0000:           ab_op = AB_BRK;
                8'b0000_1000:           ab_op = AB_PHA;
                8'b0100_1000:           ab_op = AB_PHA;
                8'b?101_1010:           ab_op = AB_PHA;
                8'b1001_1010:           ab_op = AB_TXS;
                default:                ab_op = take_irq & load_ir ? AB_IRQ0 : AB_FETCH;
            endcase

        STK0:               
            casez( IR )
                8'b?1??_0???:           ab_op = AB_RTS0;
                8'b?0??_0???:           ab_op = AB_JSR0;
                8'b????_1???:           ab_op = AB_DATA;
            endcase
        
        STK1:
            casez( IR )
                8'h00:                  ab_op = AB_BRK1;
                8'h20:                  ab_op = AB_JSR1;
                8'h40:                  ab_op = AB_RTS0;
                8'h60:                  ab_op = AB_RTS1;
            endcase

        STK2:
            if( pull )                  ab_op = AB_JMP0;
            else if( rst )              ab_op = AB_RST;
            else                        ab_op = AB_BRK2;

        FETCH: if( take_irq )           ab_op = AB_IRQ0;
               else                     ab_op = AB_FETCH; 
        INDX0:                          ab_op = AB_INDX0;    

        ABS0:
            if( jmp )                   ab_op = AB_JMP0;
            else if( indx )             ab_op = AB_INDX1;
            else                        ab_op = AB_ABS0;

        DATA:
            if( rmw )                   ab_op = AB_RMW;
            else                        ab_op = AB_DATA;
        BRA0:                           ab_op = AB_BRA0; 
        IND0:                           ab_op = AB_IND0;
    endcase
end



always @(posedge clk)
    if( RDY )
    if( !RST )                          state <= FETCH;
    else case( state )
        DECODE: 
            casez( IR )
                8'b?1?1_1010:           state <= STK0;
                8'b0??0_?000:           state <= STK0;
                8'b1000_0000:           state <= BRA0;
                8'b???1_0000:           state <= cond_true ? BRA0: FETCH;
                8'b????_0001:           state <= INDX0;
                8'b1??0_00?0:           state <= FETCH;
                8'b????_01??:           state <= DATA;
                8'b???0_1001:           state <= FETCH;
                8'b???1_1001:           state <= ABS0;
                8'b????_11??:           state <= ABS0;
            endcase

        STK0: 
            if( ~IR[3] )                state <= STK1;
            else                        state <= FETCH;
    
        STK1: 
            if( ~IR[5] )                state <= STK2;
            else if( pull )             state <= FETCH;
            else                        state <= ABS0;

        STK2: 
            if( pull )                  state <= FETCH;
            else                        state <= IND0;

        DATA: 
            if( !rmw )                  state <= FETCH;
        BRA0:                           state <= FETCH;
        FETCH:                          state <= DECODE;
        INDX0:                          state <= ABS0; 
        IND0:                           state <= ABS0;
        ABS0: 
            if( ind )                   state <= IND0;
            else if( jmp )              state <= FETCH;
            else                        state <= DATA;
    endcase



always @*
    casez( IR[7:4] )
        4'b000?:                        cond_true = ~N;
        4'b001?:                        cond_true = N;
        4'b010?:                        cond_true = ~V;
        4'b011?:                        cond_true = V;
        4'b1000:                        cond_true = 1;
        4'b1001:                        cond_true = ~C;
        4'b101?:                        cond_true = C;
        4'b110?:                        cond_true = ~Z;
        4'b111?:                        cond_true = Z;
    endcase

endmodule