int main(void) {
// ORG   $8000
  case 0x8000:
                                                     mon("L8000    NEG   <$00                    ; 8000: 00 00         \n");
    clockticks = clockticks + 4; 
    val = (UINT8)rd_mem(DP|0x00); 
    res = -val; 
/**/V = val & (UINT8)res; 
/**/C = res & 0x100; 
    val = (UINT8)res; 
    wr_mem(DP|0x00, val); 
/**/Z = val; 
/**/N = val; 
    clockticks = clockticks + 2; 
                   // FCB   $05                     ; 8002:  ' ' 05             Invalid ; 
    // =============== S U B R O U T I N E =======================================
    // Entering trench
    PC = 0x8341; JUMP; // sstep
case 0x8341:
                                                     mon("L8341    LDA   #$00                    ; 8341: 86 00         \n");
    clockticks = clockticks + 2; 
    val = (UINT8)0x00; 
    A = val; 
/**/N = val; 
/**/Z = val; 
/**/V = 0; 
    PC = 0x8343; JUMP; // sstep
case 0x8343:
                                                     mon("         STA   <$44                    ; 8343: 97 44         \n");
    clockticks = clockticks + 4; 
    val = A; 
/**/Z = val; 
/**/N = val; 
/**/V = 0; 
    ea = DP|0x44; 
    wr_mem(ea, val); 
    PC = 0x8345; JUMP; // sstep
case 0x8345:
                                                     mon("         STA   <$45                    ; 8345: 97 45         \n");
    clockticks = clockticks + 4; 
    val = A; 
/**/Z = val; 
/**/N = val; 
/**/V = 0; 
    ea = DP|0x45; 
    wr_mem(ea, val); 
    PC = 0x8347; JUMP; // sstep
case 0x8347:
                                                     mon("         LDX   #$4989                  ; 8347: 8E 49 89      \n");
    clockticks = clockticks + 3; 
    X = 0x4989; 
/**/Z = X; 
/**/N = (X) >> 8; 
/**/V = 0; 
    PC = 0x834A; JUMP; // sstep
case 0x834A:
                                                     mon("         LDA   #$00                    ; 834A: 86 00         \n");
    clockticks = clockticks + 2; 
    val = (UINT8)0x00; 
    A = val; 
/**/N = val; 
/**/Z = val; 
/**/V = 0; 
    PC = 0x834C; JUMP; // sstep
case 0x834C:
                                                     mon("L834C    STA   ,X+                     ; 834C: A7 80         \n");
    clockticks = clockticks + 6; 
    val = A; 
/**/Z = val; 
/**/N = val; 
/**/V = 0; 
    ea = X; 
    wr_mem(ea, val); 
    X = X + 1; 
    PC = 0x834E; JUMP; // sstep
case 0x834E:
                                                     mon("         CMPX  #$49A9                  ; 834E: 8C 49 A9      \n");
    clockticks = clockticks + 4; 
    val = X; 
    arg = 0x49a9; 
    res = val - arg; 
    C = res & 0x10000; 
    res = (UINT16)res; 
    Z = res; 
    N = res >> 8; 
    V = ((val ^ arg) & (val ^ res)) >> 8; 
    PC = 0x8351; JUMP; // sstep
case 0x8351:
                                                     mon("         BCS   L834C                   ; 8351: 25 F9         \n");
    // temp fix to ensure flags synched
    if (C) {
    PC = 0x834c; 
    JUMP;
    }
    clockticks = clockticks + 3; 
    PC = 0x8353; JUMP; // sstep
case 0x8353:
                                                     mon("         LDB   $4B12                   ; 8353: F6 4B 12      \n");
    clockticks = clockticks + 5; 
    val = (UINT8)rd_mem(0x4b12); 
    B = val; 
/**/N = val; 
/**/Z = val; 
/**/V = 0; 
    PC = 0x8356; JUMP; // sstep
case 0x8356:
                                                     mon("         ASLB                          ; 8356: 58            \n");
    val = (UINT8)B; 
    res = val << 1; 
/**/C = res & 0x100; 
    res = (UINT8)res; 
/**/Z = res; 
/**/N = res; 
/**/V = val ^ res; 
    B = res; 
    clockticks = clockticks + 2; 
    PC = 0x8357; JUMP; // sstep
case 0x8357:
                                                     mon("         LDX   #$7CC0                  ; 8357: 8E 7C C0      \n"); // Trench catwalk/turrets data?
    clockticks = clockticks + 3; 
    X = 0x7cc0; 
/**/Z = X; 
/**/N = (X) >> 8; 
/**/V = 0; 
    PC = 0x835A; JUMP; // sstep
case 0x835A:
                                                     mon("         ABX                           ; 835A: 3A            \n");
    X = X + B; 
    clockticks = clockticks + 3; 
    PC = 0x835B; JUMP; // sstep
case 0x835B:
                                                     mon("         CMPX  #$7CD6                  ; 835B: 8C 7C D6      \n"); // Trench catwalk/turrets data?
    clockticks = clockticks + 4; 
    val = X; 
    arg = 0x7cd6; 
    res = val - arg; 
    C = res & 0x10000; 
    res = (UINT16)res; 
    Z = res; 
    N = res >> 8; 
    V = ((val ^ arg) & (val ^ res)) >> 8; 
    PC = 0x835E; JUMP; // sstep
case 0x835E:
                                                     mon("         BCS   L8365                   ; 835E: 25 05         \n");
    // temp fix to ensure flags synched
    if (C) {
    PC = 0x8365; 
    JUMP;
    }
    clockticks = clockticks + 3; 
    PC = 0x8360; JUMP; // sstep
case 0x8360:
                                                     mon("         LDU   #$4B3F                  ; 8360: CE 4B 3F      \n");
    clockticks = clockticks + 3; 
    U = 0x4b3f; 
    Z = U; 
    N = (U) >> 8; 
    V = 0; 
    PC = 0x8363; JUMP; // sstep
case 0x8363:
                                                     mon("         BRA   L8367                   ; 8363: 20 02         \n");
    PC = 0x8367; 
    JUMP;
    clockticks = clockticks + 3; 
    
    // ---------------------------------------------------------------------------
  case 0x8365:
                                                     mon("L8365    LDU   ,X                      ; 8365: EE 84         \n");
    clockticks = clockticks + 4; 
    clockticks = clockticks + 1; 
    ea = rd_mem(X)<<8; 
    ea = ea | rd_mem((UINT16)((X)+1)); 
    U = ea; 
/**/Z = U; 
/**/N = (U) >> 8; 
/**/V = 0; 
    PC = 0x8367; JUMP; // sstep
case 0x8367:
                                                     mon("L8367    STU   $49A9                   ; 8367: FF 49 A9      \n");
    clockticks = clockticks + 5; 
    val = U; 
/**/Z = val; 
/**/N = (UINT8)(val >> 8); 
/**/V = 0; 
    wr_mem(0x49a9, (UINT8)(val >> 8)); // Care needed with I/O space and word fetches
    wr_mem(0x49aa, (UINT8)val); 
    PC = 0x836A; JUMP; // sstep
case 0x836A:
                                                     mon("         STU   $49AB                   ; 836A: FF 49 AB      \n");
    clockticks = clockticks + 5; 
    val = U; 
/**/Z = val; 
/**/N = (UINT8)(val >> 8); 
/**/V = 0; 
    wr_mem(0x49ab, (UINT8)(val >> 8)); // Care needed with I/O space and word fetches
    wr_mem(0x49ac, (UINT8)val); 
    PC = 0x836D; JUMP; // sstep
case 0x836D:
                                                     mon("         LDU   ,U                      ; 836D: EE C4         \n");
    clockticks = clockticks + 4; 
    clockticks = clockticks + 1; 
}
