home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / CROSSASM / 68ASMSIM.ZIP / simsrc / code5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-09  |  3.3 KB  |  186 lines

  1.  
  2. /***************************** 68000 SIMULATOR ****************************
  3.  
  4. File Name: CODE5.C
  5. Version: 1.0
  6.  
  7. The instructions implemented in this file are the binary coded decimal
  8.    operations:
  9.  
  10.         ABCD, SBCD, NBCD
  11.  
  12.  
  13. ***************************************************************************/
  14.  
  15.  
  16. #include <stdio.h>
  17. #include "extern.h"         /* contains global "extern" declarations */
  18.  
  19.  
  20.  
  21.  
  22. int    ABCD()
  23. {
  24. int    Rx, Ry, carry, temp_result;
  25.  
  26. Rx = (inst >> 9) & 0x0007;
  27. Ry = inst & 0x0007;
  28.  
  29. /* perform the ABCD operation */
  30. if (inst & 0x0008)  /* Rx & Ry are address registers used in predecrement */
  31.     {                     /* mode */
  32.     Rx = a_reg(Rx);
  33.     Ry = a_reg(Ry);
  34.     A[Rx]--;
  35.     A[Ry]--;
  36.     source = memory[A[Ry]];
  37.     dest = memory[A[Rx]];
  38.     }
  39. else        /* Rx & Ry are data registers */
  40.     {
  41.     source = D[Ry] & BYTE;
  42.     dest = D[Rx] & BYTE;
  43.     }
  44.  
  45. /* perform the ABCD operation */
  46. result = ((SR & xbit) >> 4) + (source & 0xf) + (dest & 0xf);
  47. if (result > 9)
  48.     {
  49.     result = result - 10;
  50.     carry = 1;
  51.     }
  52. else
  53.     carry = 0;
  54. temp_result = ((source >> 4) & 0xf) + ((dest >> 4) & 0xf) + carry;
  55. if (temp_result > 9)
  56.     {
  57.     temp_result = temp_result - 10;
  58.     carry = 1;
  59.     }
  60. else
  61.     carry = 0;
  62. result = result + (temp_result << 4);
  63.  
  64. if (inst & 0x0008)
  65.     put (&memory[A[Rx]], result, (long) BYTE);
  66. else
  67.     put (&D[Rx], result, (long) BYTE);
  68. if (carry)
  69.     SR = SR | cbit;
  70. else
  71.     SR = SR & ~cbit;
  72.  
  73. cc_update (GEN, UND, CASE_1, UND, N_A, source, dest, result, (long) BYTE, 0);
  74.  
  75. inc_cyc ( (inst & 0x0008) ? 18 : 6);
  76.  
  77. return SUCCESS;
  78.  
  79. }
  80.  
  81.  
  82.  
  83. int    SBCD()
  84. {
  85. int    Rx, Ry, borrow, temp_result;
  86.  
  87. Rx = (inst >> 9) & 0x0007;
  88. Ry = inst & 0x0007;
  89.  
  90. /* perform the SUB operation */
  91. if (inst & 0x0008)     /* Rx & Ry are address registers used in */
  92.     {           /* predecrement mode */
  93.     Rx = a_reg(Rx);
  94.     Ry = a_reg(Ry);
  95.     A[Rx]--;
  96.     A[Ry]--;
  97.     source = memory[A[Ry]];
  98.     dest = memory[A[Rx]];
  99.     }
  100. else
  101.     {        /* Rx & Ry are data registers */
  102.     source = D[Ry];
  103.     dest = D[Rx];
  104.     }
  105.  
  106. /* perform the SBCD operation */
  107. result = (dest & 0xf) - (source & 0xf) - ((SR & xbit) >> 4);
  108. if (result < 0)
  109.     {
  110.     result = result + 10;
  111.     borrow = 1;
  112.     }
  113. else
  114.     borrow = 0;
  115. temp_result = ((dest >> 4) & 0xf) - ((source >> 4) & 0xf) - borrow;
  116. if (temp_result < 0)
  117.     {
  118.     temp_result = temp_result + 10;
  119.     borrow = 1;
  120.     }
  121. else
  122.     borrow = 0;
  123. result = result + (temp_result << 4);
  124.  
  125. if (inst & 0x0008)
  126.     put (&memory[A[Rx]], result, (long) BYTE);
  127. else
  128.     put (&D[Rx], result, (long) BYTE);
  129.  
  130. if (borrow)
  131.     SR = SR | cbit;
  132. else
  133.     SR = SR & ~cbit;
  134.  
  135. cc_update (GEN, UND, CASE_1, UND, N_A, source, dest, result, (long) BYTE, 0);
  136.  
  137. inc_cyc ( (inst & 0x0008) ? 18 : 6);
  138.  
  139. return SUCCESS;
  140.  
  141. }
  142.  
  143.  
  144.  
  145. int    NBCD()
  146. {
  147. int    borrow, temp_result;
  148.  
  149. if (eff_addr ((long) BYTE, DATA_ALT_ADDR, TRUE))
  150.     return (BAD_INST);            /* illegal effective address */
  151.  
  152. dest = EV1 & BYTE;
  153.  
  154. /* perform the NBCD operation */
  155. result = 10 - (dest & 0xf) - ((SR & xbit) >> 4);
  156.  
  157. if (result < 10)
  158.     borrow = 1;
  159. else
  160.     borrow = 0;
  161.  
  162. temp_result = 10 - ((dest >> 4) & 0xf) - borrow;
  163.  
  164. if (temp_result < 10)
  165.     borrow = 1;
  166. else
  167.     borrow = 0;
  168.  
  169. result = result + (temp_result << 4);
  170.  
  171. if (borrow)
  172.     SR = SR | cbit;
  173. else
  174.     SR = SR & ~cbit;
  175.  
  176. put (EA1, result, (long) BYTE);
  177.  
  178. cc_update (GEN, UND, CASE_1, UND, N_A, source, dest, result, (long) BYTE, 0);
  179.  
  180. inc_cyc ( (inst & 0x0030) ? 8 : 6);
  181.  
  182. return SUCCESS;
  183.  
  184. }
  185.  
  186.