Guest User

ThumbDisassembler

a guest
Apr 20th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.60 KB | None | 0 0
  1. //
  2. // instruction format documentation from this source:
  3. // http://bear.ces.cwru.edu/eecs_382/ARM7-TDMI-manual-pt3.pdf
  4. //
  5. #include <stdio.h>
  6.  
  7. // ----------------------------------------------------------------
  8. //
  9. //
  10. // ----------------------------------------------------------------
  11.  
  12. //
  13. #define thuint16 unsigned short int
  14. #define thbyte   unsigned char
  15. #define thbool   bool
  16.  
  17. //
  18. #define FINLINE inline
  19.  
  20. // ----------------------------------------------------------------
  21. // the following section defines the bit masks used in decoding the
  22. // instruction formats. the use of namespaces is purely because I
  23. // like fully qualified enums since it looks more readable.
  24. // ----------------------------------------------------------------
  25.  
  26. //
  27. namespace mask
  28. {
  29.     enum
  30.     {
  31.         //
  32.         insdsc1    = 0xE000,    // 1110000000000000
  33.         insdsc2    = 0x1C00,    // 0001110000000000
  34.         insdsc3    = 0x0200,    // 0000001000000000
  35.         insdsc4    = 0x1000,    // 0001000000000000
  36.         insdsc5    = 0x1000,    // 0001000000000000
  37.         insdsc6    = 0x0400,    // 0000010000000000
  38.         insdsc7    = 0x1000,    // 0001000000000000
  39.         insdsc8    = 0x1000,    // 0001000000000000
  40.  
  41.         //
  42.         ins01opdsc = 0x1800,    // 0001100000000000
  43.         ins02opdsc = 0x0200,    // 0000000100000000
  44.         ins03opdsc = 0x1800,    // 0001100000000000
  45.         ins04opdsc = 0x03C0,    // 0000001111000000
  46.         ins05opdsc = 0x0300,    // 0000001100000000
  47.  
  48.         //
  49.         ins16cond  = 0x0F00,    // 0000111100000000
  50.     };
  51. };
  52.  
  53. //
  54. #define INSDSC1( x ) (x<<13)
  55. namespace insdsc1
  56. {
  57.     enum
  58.     {
  59.         ins00   = INSDSC1( 0x0 ),
  60.         ins03   = INSDSC1( 0x1 ),
  61.         ins04   = INSDSC1( 0x2 ),
  62.         ins09   = INSDSC1( 0x3 ),
  63.         ins10   = INSDSC1( 0x4 ),
  64.         ins12   = INSDSC1( 0x5 ),
  65.         ins15   = INSDSC1( 0x6 ),
  66.         ins18   = INSDSC1( 0x7 )
  67.     };
  68. };
  69.  
  70. //
  71. #define INSDSC2( x ) (x<<10)
  72. namespace insdsc2
  73. {
  74.     enum
  75.     {
  76.         ins4     = INSDSC2( 0x0 ),
  77.         ins5     = INSDSC2( 0x1 ),
  78.         ins6_a   = INSDSC2( 0x2 ),
  79.         ins6_b   = INSDSC2( 0x3 ),
  80.         dsc7_8_a = INSDSC2( 0x4 ),
  81.         dsc7_8_b = INSDSC2( 0x5 ),
  82.         dsc7_8_c = INSDSC2( 0x6 ),
  83.         dsc7_8_d = INSDSC2( 0x7 ),
  84.     };
  85. };
  86.  
  87. //
  88. #define INS01OP( x ) (x<<11)
  89. namespace ins01op
  90. {
  91.     enum
  92.     {
  93.         lsl     = INS01OP( 0x0 ),
  94.         lsr     = INS01OP( 0x1 ),
  95.         asr     = INS01OP( 0x2 ),
  96.         ins02op = INS01OP( 0x3 )
  97.     };
  98. };
  99.  
  100. //
  101. #define INS02OP( x ) (x<<8)
  102. namespace ins02op
  103. {
  104.     enum
  105.     {
  106.         add     = INS02OP( 0x0 ),
  107.         sub     = INS02OP( 0x1 )
  108.     };
  109. };
  110.  
  111. //
  112. #define INS03OP( x ) (x<<11)
  113. namespace ins03op
  114. {
  115.     enum
  116.     {
  117.         mov     = INS03OP( 0x0 ),
  118.         cmp     = INS03OP( 0x1 ),
  119.         add     = INS03OP( 0x2 ),
  120.         sub     = INS03OP( 0x3 )
  121.     };
  122. };
  123.  
  124. //
  125. #define INS04OP( x ) (x<<6)
  126. namespace ins4op
  127. {
  128.     enum
  129.     {
  130.         and     = INS04OP( 0x0 ),
  131.         eor     = INS04OP( 0x1 ),
  132.         lsl     = INS04OP( 0x2 ),
  133.         lsr     = INS04OP( 0x3 ),
  134.         asr     = INS04OP( 0x4 ),
  135.         adc     = INS04OP( 0x5 ),
  136.         sbc     = INS04OP( 0x6 ),
  137.         ror     = INS04OP( 0x7 ),
  138.         tst     = INS04OP( 0x8 ),
  139.         neg     = INS04OP( 0x9 ),
  140.         cmp     = INS04OP( 0xA ),
  141.         cmn     = INS04OP( 0xB ),
  142.         orr     = INS04OP( 0xC ),
  143.         mul     = INS04OP( 0xD ),
  144.         bic     = INS04OP( 0xE ),
  145.         mvn     = INS04OP( 0xF ),
  146.     };
  147. };
  148.  
  149. //
  150. #define INS05OP( x ) (x<<8)
  151. namespace ins5op
  152. {
  153.     enum
  154.     {
  155.         add     = INS05OP( 0 ),
  156.         cmp     = INS05OP( 1 ),
  157.         mov     = INS05OP( 2 ),
  158.         bx      = INS05OP( 3 ),
  159.     };
  160. };
  161.  
  162. //
  163. #define INS16COND( x ) (x<<8)
  164. namespace ins16cond
  165. {
  166.     enum
  167.     {
  168.         beq     = INS16COND( 0x0 ),
  169.         bne     = INS16COND( 0x1 ),
  170.         bcs     = INS16COND( 0x2 ),
  171.         bcc     = INS16COND( 0x3 ),
  172.         bmi     = INS16COND( 0x4 ),
  173.         bpl     = INS16COND( 0x5 ),
  174.         bvs     = INS16COND( 0x6 ),
  175.         bvc     = INS16COND( 0x7 ),
  176.         bhi     = INS16COND( 0x8 ),
  177.         bls     = INS16COND( 0x9 ),
  178.         bge     = INS16COND( 0xA ),
  179.         blt     = INS16COND( 0xB ),
  180.         bgt     = INS16COND( 0xC ),
  181.         ble     = INS16COND( 0xD ),
  182.         ins17op = INS16COND( 0xF ),
  183.     };
  184. };
  185.  
  186. // ----------------------------------------------------------------
  187. // the following code is for the instruction format decoders. they
  188. // are declared in reverse so as to avoid forward declarations.
  189. // these should all be forced inline where possible since they are
  190. // defined as a sepourate function only for clarity.
  191. // ----------------------------------------------------------------
  192.  
  193. // decode format 19 instructions
  194. static FINLINE
  195. void decodeF19( thuint16 *stream )
  196. {
  197.     // decode operand
  198.     thuint16 o11 = (*stream<<1) & 0x0FFE;   // 0000011111111111
  199.     //
  200.     if ( *stream & 0x0800 ) // H set
  201.     {
  202.         printf( "bl1 \t#%d\n", o11 );
  203.     }
  204.     else
  205.     {
  206.         printf( "bl2 \t#%d\n", o11 );
  207.     }
  208. }
  209.  
  210. // decode format 18 instructions
  211. static FINLINE
  212. void decodeF18( thuint16 *stream )
  213. {
  214.     // decode operand
  215.     thuint16 o11 = (*stream<<1) & 0x0FFE;   // 0000011111111111
  216.     //
  217.     printf( "b   \t#%d\n", o11 );
  218. }
  219.  
  220. // decode format 17 instructions
  221. static FINLINE
  222. void decodeF17( thuint16 *stream )
  223. {
  224.     //
  225.     thbyte o8 = *stream & 0x00FF;
  226.     //
  227.     printf( "swi \t#%d\n", o8 );
  228. }
  229.  
  230. // decode format 16 instructions
  231. static FINLINE
  232. void decodeF16( thuint16 *stream )
  233. {
  234.     //
  235.     thbyte o8 = *stream & 0xFF;
  236.     //
  237.     switch ( *stream & mask::ins16cond )
  238.     {
  239.     case ( ins16cond::beq ):
  240.         //
  241.         printf( "beq \t%d\n" );
  242.         break;
  243.     case ( ins16cond::bne ):
  244.         //
  245.         printf( "bne \t%d\n" );
  246.         break;
  247.     case ( ins16cond::bcs ):
  248.         //
  249.         printf( "bcs \t%d\n" );
  250.         break;
  251.     case ( ins16cond::bcc ):
  252.         //
  253.         printf( "bcc \t%d\n" );
  254.         break;
  255.     case ( ins16cond::bmi ):
  256.         //
  257.         printf( "bmi \t%d\n" );
  258.         break;
  259.     case ( ins16cond::bpl ):
  260.         //
  261.         printf( "bpl \t%d\n" );
  262.         break;
  263.     case ( ins16cond::bvs ):
  264.         //
  265.         printf( "bvs \t%d\n" );
  266.         break;
  267.     case ( ins16cond::bvc ):
  268.         //
  269.         printf( "bvc \t%d\n" );
  270.         break;
  271.     case ( ins16cond::bhi ):
  272.         //
  273.         printf( "bhi \t%d\n" );
  274.         break;
  275.     case ( ins16cond::bls ):
  276.         //
  277.         printf( "bls \t%d\n" );
  278.         break;
  279.     case ( ins16cond::bge ):
  280.         //
  281.         printf( "bgt \t%d\n" );
  282.         break;
  283.     case ( ins16cond::blt ):
  284.         //
  285.         printf( "blt \t%d\n" );
  286.         break;
  287.     case ( ins16cond::bgt ):
  288.         //
  289.         printf( "bgt \t%d\n" );
  290.         break;
  291.     case ( ins16cond::ble ):
  292.         //
  293.         printf( "ble \t%d\n" );
  294.         break;
  295.     case ( ins16cond::ins17op ):
  296.         // format 17 decode
  297.         decodeF17( stream );
  298.         break;
  299.     }
  300. }
  301.  
  302. // decode format 15 instructions
  303. static FINLINE
  304. void decodeF15( thuint16 *stream )
  305. {
  306.     thbyte rb = (*stream >> 8) & 0x07;  // 0000011100000000
  307.     //
  308.     if ( *stream & 0x0800 )
  309.     {
  310.         printf( "ldmia\tr%d!, { \n", rb );
  311.         if ( *stream & 0x01 ) { printf( "r0 " ); }
  312.         if ( *stream & 0x02 ) { printf( "r1 " ); }
  313.         if ( *stream & 0x04 ) { printf( "r2 " ); }
  314.         if ( *stream & 0x08 ) { printf( "r3 " ); }
  315.         if ( *stream & 0x10 ) { printf( "r4 " ); }
  316.         if ( *stream & 0x20 ) { printf( "r5 " ); }
  317.         if ( *stream & 0x40 ) { printf( "r6 " ); }
  318.         if ( *stream & 0x80 ) { printf( "r7 " ); }
  319.         printf( "}\n" );
  320.     }
  321.     else
  322.     {
  323.         //
  324.         printf( "stmia\tr%d!, { \n", rb );
  325.         if ( *stream & 0x01 ) { printf( "r0 " ); }
  326.         if ( *stream & 0x02 ) { printf( "r1 " ); }
  327.         if ( *stream & 0x04 ) { printf( "r2 " ); }
  328.         if ( *stream & 0x08 ) { printf( "r3 " ); }
  329.         if ( *stream & 0x10 ) { printf( "r4 " ); }
  330.         if ( *stream & 0x20 ) { printf( "r5 " ); }
  331.         if ( *stream & 0x40 ) { printf( "r6 " ); }
  332.         if ( *stream & 0x80 ) { printf( "r7 " ); }
  333.         printf( "}\n" );
  334.     }
  335. }
  336.  
  337. // decode format 14 instructions
  338. static FINLINE
  339. void decodeF14( thuint16 *stream )
  340. {
  341.     // decode operands
  342.     if (! (*stream & 0x0800) )  // L set
  343.     {
  344.         printf( "push \t{ " );
  345.         if ( *stream & 0x01 ) { printf( "r0 " ); }
  346.         if ( *stream & 0x02 ) { printf( "r1 " ); }
  347.         if ( *stream & 0x04 ) { printf( "r2 " ); }
  348.         if ( *stream & 0x08 ) { printf( "r3 " ); }
  349.         if ( *stream & 0x10 ) { printf( "r4 " ); }
  350.         if ( *stream & 0x20 ) { printf( "r5 " ); }
  351.         if ( *stream & 0x40 ) { printf( "r6 " ); }
  352.         if ( *stream & 0x80 ) { printf( "r7 " ); }
  353.         //
  354.         if ( *stream & 0x0100 ) // R set
  355.         {
  356.             printf( "lr " );
  357.         }
  358.         printf( "}\n" );
  359.     }
  360.     else
  361.     {
  362.         printf( "pop \t{ " );
  363.         if ( *stream & 0x01 ) { printf( "r0 " ); }
  364.         if ( *stream & 0x02 ) { printf( "r1 " ); }
  365.         if ( *stream & 0x04 ) { printf( "r2 " ); }
  366.         if ( *stream & 0x08 ) { printf( "r3 " ); }
  367.         if ( *stream & 0x10 ) { printf( "r4 " ); }
  368.         if ( *stream & 0x20 ) { printf( "r5 " ); }
  369.         if ( *stream & 0x40 ) { printf( "r6 " ); }
  370.         if ( *stream & 0x80 ) { printf( "r7 " ); }
  371.         //
  372.         if ( *stream & 0x0100 ) // R set
  373.         {
  374.             printf( "lr " );
  375.         }
  376.         printf( "}\n" );
  377.     }
  378. }
  379.  
  380. // decode format 13 instructions
  381. static FINLINE
  382. void decodeF13( thuint16 *stream )
  383. {
  384.     // decode register operands
  385.     thuint16 w7 = (*stream << 2 ) & 0x01FC;     // 0000000001111111
  386.     //
  387.     if ( *stream & 0x0080 ) // S set
  388.     {
  389.         printf( "sub \tsp, #%d\n", w7 );
  390.     }
  391.     else
  392.     {
  393.         printf( "add \tsp, #%d\n", w7 );
  394.     }
  395. }
  396.  
  397. // decode format 12 instructions
  398. static FINLINE
  399. void decodeF12( thuint16 *stream )
  400. {
  401.     // decode register operands
  402.     thuint16 w8 = (*stream << 2) & 0x03FC;  // 0000000011111111
  403.     thbyte   rd = (*stream >> 8) & 0x0007;  // 0000011100000000
  404.     //
  405.     if ( *stream & 0x0800 ) // SP set
  406.     {
  407.         printf( "add \tr%d, sp, #%d\n", rd, w8 );
  408.     }
  409.     else
  410.     {
  411.         printf( "add \tr%d, pc, #%d\n", rd, w8 );
  412.     }
  413. }
  414.  
  415. // decode format 11 instructions
  416. static FINLINE
  417. void decodeF11( thuint16 *stream )
  418. {
  419.     // decode register operands
  420.     thuint16 w8 = (*stream << 2) & 0x03FC;  // 0000000011111111
  421.     thbyte   rd = (*stream >> 8) & 0x0007;  // 0000011100000000
  422.     //
  423.     if ( *stream & 0x0800 ) // L set
  424.     {
  425.         printf( "LDR \tr%d, [sp, #%d]\n", rd, w8 );
  426.     }
  427.     else
  428.     {
  429.         printf( "STR \tr%d, [sp, #%d]\n", rd, w8 );
  430.     }
  431. }
  432.  
  433. // decode format 10 instructions
  434. static FINLINE
  435. void decodeF10( thuint16 *stream )
  436. {
  437.     // decode register operands
  438.     thbyte rd = (*stream     ) & 0x07;  // 0000000000000111
  439.     thbyte rb = (*stream >> 3) & 0x07;  // 0000000000111000
  440.     thbyte o5 = (*stream >> 5) & 0x3E;  // 0000011111000000
  441. //         o5 = o5 << 1;
  442.     //
  443.     if ( *stream & 0x0800 ) // L set
  444.     {
  445.         printf( "LDRH\tr%d, [r%d, #%d]\n", rd, rb, o5 );
  446.     }
  447.     else
  448.     {
  449.         printf( "STRH\tr%d, [r%d, #%d]\n", rd, rb, o5 );
  450.     }
  451. }
  452.  
  453. // decode format 9 instructions
  454. static FINLINE
  455. void decodeF09( thuint16 *stream )
  456. {
  457.     // decode register operands
  458.     thbyte   rd = (*stream     ) & 0x07;    // 0000000000000111
  459.     thbyte   rb = (*stream >> 3) & 0x07;    // 0000000000111000
  460.     thuint16 o5 = (*stream >> 6) & 0x1F;    // 0000011111000000
  461.              o5 = o5 << 2;
  462.     //
  463.     switch
  464.     (
  465.         (((*stream & 0x0800)!=0)<<1) +  // L set
  466.          ((*stream & 0x1000)!=0)        // B set
  467.     )
  468.     {
  469.     case ( 0 ):
  470.         printf( "str \tr%d, [r%d, #%d]\n", rd, rb, o5 );
  471.         break;
  472.     case ( 1 ):
  473.         printf( "strb\tr%d, [r%d, #%d]\n", rd, rb, o5 );
  474.         break;
  475.     case ( 2 ):
  476.         printf( "ldr \tr%d, [r%d, #%d]\n", rd, rb, o5 );
  477.         break;
  478.     case ( 3 ):
  479.         printf( "ldrb\tr%d, [r%d, #%d]\n", rd, rb, o5 );
  480.         break;
  481.     }
  482. }
  483.  
  484. // decode format 8 instructions
  485. static FINLINE
  486. void decodeF08( thuint16 *stream )
  487. {
  488.     // decode register operands
  489.     thbyte rd = (*stream     ) & 0x07;  // 0000000000000111
  490.     thbyte rb = (*stream >> 3) & 0x07;  // 0000000000111000
  491.     thbyte ro = (*stream >> 6) & 0x07;  // 0000000111000000
  492.     //
  493.     if ( *stream & 0x0800 ) // H set
  494.     {
  495.  
  496.     }
  497.     if ( *stream & 0x0400 ) // S set
  498.     {
  499.     }
  500.     //
  501.     printf( "[format 8]\n" );
  502. }
  503.  
  504. // decode format 7 instructions
  505. static FINLINE
  506. void decodeF07( thuint16 *stream )
  507. {
  508.     // decode register operands
  509.     thbyte rd = (*stream     ) & 0x07;  // 0000000000000111
  510.     thbyte rb = (*stream >> 3) & 0x07;  // 0000000000111000
  511.     thbyte ro = (*stream >> 6) & 0x07;  // 0000000111000000
  512.     //
  513.     switch
  514.     (
  515.         (((*stream & 0x0800)!=0)<<1) +  // L set
  516.          ((*stream & 0x0400)!=0)        // B set
  517.     )
  518.     {
  519.     case ( 0 ):
  520.         printf( "str \tr%d, [r%d, r%d]\n", rd, rb, ro );
  521.         break;
  522.     case ( 1 ):
  523.         printf( "strb\tr%d, [r%d, r%d]\n", rd, rb, ro );
  524.         break;
  525.     case ( 2 ):
  526.         printf( "ldr \tr%d, [r%d, r%d]\n", rd, rb, ro );
  527.         break;
  528.     case ( 3 ):
  529.         printf( "ldrb\tr%d, [r%d, r%d]\n", rd, rb, ro );
  530.         break;
  531.     }
  532. }
  533.  
  534. // decode format 6 instructions
  535. static FINLINE
  536. void decodeF06( thuint16 *stream )
  537. {
  538.     // decode register operands
  539.     thbyte   rd = (*stream >> 8) & 0x07;    // 0000011100000000
  540.     thuint16 o8 = (*stream     ) & 0xFF;    // 0000000011111111
  541.              o8 = o8 << 2;
  542.     //
  543.     printf( "ldr \tr%d, [pc, #%d]\n", rd, o8 );
  544. }
  545.  
  546. // decode format 5 instructions
  547. static FINLINE
  548. void decodeF05( thuint16 *stream )
  549. {
  550.     // decode register operands
  551.     thbyte rd =  *stream       & 0x07;  // 0000000000000111
  552.     thbyte rs = (*stream >> 3) & 0x07;  // 0000000000111000
  553.     //
  554.     if ( *stream & 0x80 ) // H1 Set
  555.     {
  556.     }
  557.     if ( *stream & 0x40 ) // H2 Set
  558.     {
  559.     }
  560.     //
  561.     switch ( *stream & mask::ins05opdsc )
  562.     {
  563.     case ( ins5op::add ):
  564.         printf( "add \t?r%d, ?r%d\n", rd, rs );
  565.         //
  566.         break;
  567.     case ( ins5op::cmp ):
  568.         printf( "cmp \t?r%d, ?r%d\n", rd, rs );
  569.         //
  570.         break;
  571.     case ( ins5op::mov ):
  572.         printf( "mov \t?r%d, ?r%d\n", rd, rs );
  573.         //
  574.         break;
  575.     case ( ins5op::bx  ):
  576.         printf( "bx  \t?r%d, ?r%d\n", rd, rs );
  577.         //
  578.         break;
  579.     }
  580. }
  581.  
  582. // decode format 4 instructions
  583. static FINLINE
  584. void decodeF04( thuint16 *stream )
  585. {
  586.     // decode register operands
  587.     thbyte rd =  *stream       & 0x07;  // 0000000000000111
  588.     thbyte rs = (*stream >> 3) & 0x07;  // 0000000000111000
  589.     // branch based on opcode
  590.     switch ( *stream & mask::ins04opdsc )
  591.     {
  592.     case ( ins4op::and ):
  593.         // [rd = rd AND rs]
  594.         printf( "and \tr%d, r%d\n", rd, rs );
  595.         break;
  596.     case ( ins4op::eor ):
  597.         // [rd = rd XOR rs]
  598.         printf( "eor \tr%d, r%d\n", rd, rs );
  599.         break;
  600.     case ( ins4op::lsl ):
  601.         // [rd = rd << rs]
  602.         printf( "lsl \tr%d, r%d\n", rd, rs );
  603.         break;
  604.     case ( ins4op::lsr ):
  605.         // [rd = rd >> rs]
  606.         printf( "lsr \tr%d, r%d\n", rd, rs );
  607.         break;
  608.     case ( ins4op::asr ):
  609.         // [rd = rd AND rs]
  610.         printf( "asr \tr%d, r%d\n", rd, rs );
  611.         break;
  612.     case ( ins4op::adc ):
  613.         // [rd = rd + rs + c-bit]
  614.         printf( "adc \tr%d, r%d\n", rd, rs );
  615.         break;
  616.     case ( ins4op::sbc ):
  617.         // [rd = rd - rs - NOT c-bit]
  618.         printf( "sbc \tr%d, r%d\n", rd, rs );
  619.         break;
  620.     case ( ins4op::ror ):
  621.         // [rd = rd ROR rs]
  622.         printf( "ror \tr%d, r%d\n", rd, rs );
  623.         break;
  624.     case ( ins4op::tst ):
  625.         // set condition codes on [rd AND rs]
  626.         printf( "tst \tr%d, r%d\n", rd, rs );
  627.         break;
  628.     case ( ins4op::neg ):
  629.         // [rd = -rs]
  630.         printf( "neg \tr%d, r%d\n", rd, rs );
  631.         break;
  632.     case ( ins4op::cmp ):
  633.         // set condition codes on [rd - rs]
  634.         printf( "cmp \tr%d, r%d\n", rd, rs );
  635.         break;
  636.     case ( ins4op::cmn ):
  637.         // set condition codes on [rd + rs]
  638.         printf( "cmn \tr%d, r%d\n", rd, rs );
  639.         break;
  640.     case ( ins4op::orr ):
  641.         // [rd = rd OR rs]
  642.         printf( "orr \tr%d, r%d\n", rd, rs );
  643.         break;
  644.     case ( ins4op::mul ):
  645.         // [rd = rd * rs]
  646.         printf( "mul \tr%d, r%d\n", rd, rs );
  647.         break;
  648.     case ( ins4op::bic ):
  649.         // [rd = rd AND NOT rs]
  650.         printf( "bic \tr%d, r%d\n", rd, rs );
  651.         break;
  652.     case ( ins4op::mvn ):
  653.         // [rd = NOT rs]
  654.         printf( "mvn \tr%d, r%d\n", rd, rs );
  655.         break;
  656.     }
  657. }
  658.  
  659. // decode format 3 instructions
  660. static FINLINE
  661. void decodeF03( thuint16 *stream )
  662. {
  663.     // decode operands
  664.     thbyte rd = (*stream >> 8 ) & 0x0007;   // 0000011100000000
  665.     thbyte o8 =  *stream        & 0x00FF;   // 0000000011111111
  666.     // decode opcode
  667.     switch ( *stream & mask::ins03opdsc )
  668.     {
  669.     case ( ins03op::mov ):
  670.         // [rd = o8]
  671.         printf( "mov \tr%d, #%d\n", rd, o8 );
  672.         break;
  673.     case ( ins03op::cmp ):
  674.         // compare contentse of rd with o8
  675.         printf( "cmp \tr%d, #%d\n", rd, o8 );
  676.         break;
  677.     case ( ins03op::add ):
  678.         // [rd = rd + o8]
  679.         printf( "add \tr%d, #%d\n", rd, o8 );
  680.         break;
  681.     case ( ins03op::sub ):
  682.         // [rd = rd - o8]
  683.         printf( "sub \tr%d, #%d\n", rd, o8 );
  684.         break;
  685.     }
  686. }
  687.  
  688. // decode format 2 instructions
  689. static FINLINE
  690. void decodeF02( thuint16 *stream )
  691. {
  692.     // decode the operands
  693.     thbyte rd =  *stream        & 0x07; // 0000000000000111
  694.     thbyte rs = (*stream >> 3 ) & 0x07; // 0000000000111000
  695.     thbyte rn = (*stream >> 6 ) & 0x07; // 0000000111000000
  696.     // decode the I flag
  697.     if ( *stream & 0x0200 )             // 0000010000000000
  698.     {
  699.         // index into rs
  700.     }
  701.     // decode the opcode
  702.     switch ( *stream & mask::ins02opdsc )
  703.     {
  704.     case ( ins02op::add ):
  705.         // [rd = rs + rn]
  706.         printf( "add \tr%d, r%d, ?r%d\n", rd, rs, rn );
  707.         break;
  708.     case ( ins02op::sub ):
  709.         // [rd = rs - rn]
  710.         printf( "sub \tr%d, r%d, ?r%d\n", rd, rs, rn );
  711.         break;
  712.     };
  713. }
  714.  
  715. // decode format 1 instructions
  716. static FINLINE
  717. void decodeF01( thuint16 *stream )
  718. {
  719.     // decode register operands
  720.     thbyte rd =  *stream        & 0x0007;   // 0000000000000111
  721.     thbyte rs = (*stream >> 3 ) & 0x0007;   // 0000000000111000
  722.     thbyte o5 = (*stream >> 6 ) & 0x001F;   // 0000011111000000
  723.     // decode the opcode
  724.     switch ( *stream & mask::ins01opdsc )
  725.     {
  726.     case ( ins01op::lsl ):
  727.         // [rd = rs << o5] shift
  728.         printf( "lsl \tr%d, r%d, #%d\n", rd, rs, o5 );
  729.         break;
  730.     case ( ins01op::lsr ):
  731.         // [rd = rs >> o5] logical
  732.         printf( "lsr \tr%d, r%d, #%d\n", rd, rs, o5 );
  733.         break;
  734.     case ( ins01op::asr ):
  735.         // [rd = rs >> o5] arythmetic
  736.         printf( "asr \tr%d, r%d, #%d\n", rd, rs, o5 );
  737.         break;
  738.  
  739.     // this is format 2
  740.     case ( ins01op::ins02op ):
  741.         decodeF02( stream );
  742.     }
  743. }
  744.  
  745. // ----------------------------------------------------------------
  746. //
  747. //
  748. // ----------------------------------------------------------------
  749.  
  750. //
  751. static FINLINE
  752. void decode( thuint16 *stream )
  753. {
  754.     //
  755.     thuint16 ins = (*stream);
  756.     // level 1 descriminator
  757.     switch ( ins & mask::insdsc1 )
  758.     {
  759.     case ( insdsc1::ins00 ):
  760.         // format 1 decode
  761.         decodeF01( stream );
  762.         break;
  763.     case ( insdsc1::ins03 ):
  764.         // format 3 decode
  765.         decodeF03( stream );
  766.         break;
  767.     case ( insdsc1::ins04 ):
  768.         // level 2 descriminator
  769.         switch ( ins & mask::insdsc2 )
  770.         {
  771.         case ( insdsc2::ins4 ):
  772.             // format 4 decode
  773.             decodeF04( stream );
  774.             break;
  775.         case ( insdsc2::ins5 ):
  776.             // format 5 decode
  777.             decodeF05( stream );
  778.             break;
  779.         case ( insdsc2::ins6_a ):
  780.         case ( insdsc2::ins6_b ):
  781.             // format 6 decode
  782.             decodeF06( stream );
  783.             break;
  784.         case ( insdsc2::dsc7_8_a ):
  785.         case ( insdsc2::dsc7_8_b ):
  786.         case ( insdsc2::dsc7_8_c ):
  787.         case ( insdsc2::dsc7_8_d ):
  788.             // stage 3 descriminator
  789.             if (! (ins & mask::insdsc3) )
  790.                 // format 7
  791.                 decodeF07( stream );
  792.             else
  793.                 // format 8
  794.                 decodeF08( stream );
  795.             break;
  796.         }
  797.         break;
  798.     case ( insdsc1::ins09 ):
  799.         // format 9 decode
  800.         decodeF09( stream );
  801.         break;
  802.     case ( insdsc1::ins10 ):
  803.         // stage 4 descriminator
  804.         if (! (ins & mask::insdsc4) )
  805.             // format 10 decode
  806.             decodeF10( stream );
  807.         else
  808.             // format 11 decode
  809.             decodeF11( stream );
  810.         break;
  811.     case ( insdsc1::ins12 ):
  812.         // stage 5 descriminator
  813.         if (! (ins & mask::insdsc5) )
  814.             // format 12 decode
  815.             decodeF12( stream );
  816.         else
  817.         {
  818.             // stage 6 descriminator
  819.             if (! (ins & mask::insdsc6) )
  820.                 // format 13 decode
  821.                 decodeF13( stream );
  822.             else
  823.                 // format 14 decode
  824.                 decodeF14( stream );
  825.         }
  826.         break;
  827.     case ( insdsc1::ins15 ):
  828.         // stage 7 descriminator
  829.         if (! (ins & mask::insdsc7) )
  830.             // format 15 decode
  831.             decodeF15( stream );
  832.         else
  833.             // format 16 decode
  834.             decodeF16( stream );
  835.         break;
  836.     case ( insdsc1::ins18 ):
  837.         // stage 8 descriminator
  838.         if (! (ins & mask::insdsc8) )
  839.             // format 18 decode
  840.             decodeF18( stream );
  841.         else
  842.             // format 19 decode
  843.             decodeF19( stream );
  844.         break;
  845.     }
  846.  
  847. }
  848.  
  849. unsigned char data[] =
  850. {
  851.     0x80,0xB5,0x00,0xAF,0x02,0x23,0x18,0x1C,0xBD,0x46,0x80,0xBC,0x02,0xBC,0x08,0x47,
  852.     0x80,0xB5,0x82,0xB0,0x00,0xAF,0x78,0x60,0x7B,0x68,0x5B,0x00,0x18,0x1C,0xBD,0x46,
  853.     0x02,0xB0,0x80,0xBC,0x02,0xBC,0x08,0x47,0x80,0xB5,0x84,0xB0,0x00,0xAF,0x78,0x60,
  854.     0x3B,0x1C,0x08,0x33,0x01,0x22,0x1A,0x60,0x3B,0x1C,0x08,0x33,0x00,0x22,0x5A,0x60,
  855.     0x3B,0x1C,0x08,0x33,0x7A,0x68,0x92,0x00,0xD3,0x58,0x18,0x1C,0xBD,0x46,0x04,0xB0,
  856.     0x80,0xBC,0x02,0xBC,0x08,0x47,0xC0,0x46,0x80,0xB5,0x82,0xB0,0x00,0xAF,0x00,0x23,
  857.     0x7B,0x60,0x05,0xE0,0xFF,0xF7,0xCC,0xFF,0x03,0x1C,0x7A,0x68,0xD3,0x18,0x7B,0x60,
  858.     0x7B,0x68,0x09,0x2B,0xF6,0xDD,0x7B,0x68,0x18,0x1C,0xBD,0x46,0x02,0xB0,0x80,0xBC,
  859.     0x02,0xBC,0x08,0x47
  860. };
  861.  
  862. //
  863. int main( int argc, char** args )
  864. {
  865.     int i=0;
  866.     thuint16 *ptr = (thuint16*) data;
  867.     while ( ptr < (thuint16*)(data+sizeof(data)) )
  868.     {
  869.         printf( " %4X:  %04X    ", i, *ptr );
  870.         decode( ptr );
  871.         ptr++;
  872.         i += 2;
  873.     }
  874.     getchar( );
  875.     return 0;
  876. }
Advertisement
Add Comment
Please, Sign In to add comment