home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 September / CHIP_CD_1997_09_PL.iso / software / news / wspecem / sources / z80 / math16bi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-10  |  5.4 KB  |  184 lines

  1. /* Ld8Bits.c: Z80 16 bit arithmetic instructions.
  2.  *
  3.  * Copyright 1996 Rui Fernando Ferreira Ribeiro.
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include "env.h"
  21.  
  22. /*=========================================================================*
  23.  *                            add_hl_ss                                    *
  24.  *=========================================================================*/
  25.  
  26. #define add_hl_ss(hl,ss,hh,TS) { \
  27.    LOCAL USHORT tmp; \
  28.    \
  29.    T(TS); \
  30.    flags._H = ((((hl) & (USHORT)0xFFF) + ((ss) & (USHORT)0xFFF)) > (USHORT)0xFFF ); \
  31.    flags._N = 0; \
  32.    flags._C = ( (tmp = (USHORT)(hl + (ss))) < hl ); \
  33.    hl = tmp; \
  34.    flags._X = hh & (UCHAR)BIT_5; \
  35.    flags._Y = hh & (UCHAR)BIT_3; \
  36. }
  37.  
  38.  
  39. void add_hl_bc() add_hl_ss(HL,BC,H,11);
  40. void add_hl_de() add_hl_ss(HL,DE,H,11);
  41.  
  42. void add_hl_hl()
  43. {
  44.    T(11);
  45.    flags._H = ((HL & (USHORT)0xFFF) > (USHORT)0x7FF);
  46.    flags._N = 0;
  47.    flags._C = (HL > (USHORT)0x7FFF);
  48.    HL += HL;
  49.    flags._X = H & (UCHAR)BIT_5;
  50.    flags._Y = H & (UCHAR)BIT_3;
  51. }
  52.  
  53. void add_hl_sp() add_hl_ss(HL,SP,H,11);
  54.  
  55. void add_ix_bc() add_hl_ss(IX,BC,HX,15);
  56. void add_ix_de() add_hl_ss(IX,DE,HX,15);
  57. void add_ix_ix() add_hl_ss(IX,IX,HX,15);
  58. void add_ix_sp() add_hl_ss(IX,SP,HX,15);
  59.  
  60. void add_iy_bc() add_hl_ss(IY,BC,HY,15);
  61. void add_iy_de() add_hl_ss(IY,DE,HY,15);
  62. void add_iy_iy() add_hl_ss(IY,IY,HY,15);
  63. void add_iy_sp() add_hl_ss(IY,SP,HY,15);
  64.  
  65. #undef add_hl_ss
  66.  
  67. /*=========================================================================*
  68.  *                            adc_hl_ss                                    *
  69.  *=========================================================================*/
  70.  
  71. #define adc_hl_ss(ss,r) { \
  72.     LOCAL USHORT tmp; \
  73.     \
  74.     T(15); \
  75.     flags._H = (((HL & (USHORT)0xFFF) + ((ss) & (USHORT)0xFFF)) > (USHORT)0xFFF ); \
  76.     flags._N = 0; \
  77.     flags._C = ( (tmp = (USHORT)(HL + (ss) + (USHORT) \
  78.          (flags._C != 0 ))) < (HL+(flags._C != 0 )) ); \
  79.     flags._P = ( ((H & BIT_7) == ((r) & BIT_7)) && ((HL & (USHORT)0x8000) ^ tmp) ); \
  80.     flags._Z = !(HL = tmp); \
  81.     flags._S = (H & BIT_7); \
  82.     flags._X = H & (UCHAR)BIT_5; \
  83.     flags._Y = H & (UCHAR)BIT_3; \
  84. }
  85.  
  86.  
  87. /**** ADC overflow: -- = + ou ++ = - */
  88.  
  89. void adc_hl_bc() adc_hl_ss(BC, B);
  90. void adc_hl_de() adc_hl_ss(DE, D);
  91. void adc_hl_hl() adc_hl_ss(HL, H);
  92. /*
  93. void adc_hl_hl()
  94. {
  95.     LOCAL USHORT tmp;
  96.  
  97.     T(15);
  98.     flags._H = ((HL & (USHORT)0xFFF) > (USHORT)0x7FF);
  99.     flags._N = 0;
  100.     flags._C = (HL > (USHORT)0x7FFF);
  101.     flags._P = ( (HL & (USHORT)0x8000) ^ tmp);
  102.     flags._Z = !(HL += HL + (flags._C != 0));
  103.     flags._S = (H & BIT_7);
  104.     flags._X = H & (UCHAR)BIT_5;
  105.     flags._Y = H & (UCHAR)BIT_3;
  106. }
  107. */
  108. void adc_hl_sp() adc_hl_ss(SP, SP >> 8);
  109.  
  110. #undef adc_hl_ss
  111.  
  112. /*=========================================================================*
  113.  *                            sbc_hl_ss                                    *
  114.  *=========================================================================*/
  115.  
  116. #define sbc_hl_ss(ss, r) { \
  117.      LOCAL USHORT tmp; \
  118.      \
  119.      T(15); \
  120.      flags._H = (((ss) & (USHORT)0xFFF) > (HL & (USHORT)0xFFF) ); \
  121.      flags._N = 1; \
  122.      flags._Z = !(tmp = (USHORT)(HL - (ss) - \
  123.                     (USHORT)(flags._C != 0)) ); \
  124.      flags._C = ((ss) > HL)||(tmp>HL); \
  125.      flags._P = ( ((H & BIT_7) != ((r) & BIT_7)) && ((HL & (USHORT)0x8000) ^ tmp) ); \
  126.      flags._S = ((HL = tmp) & (USHORT)0x8000); \
  127.      flags._X = H & (UCHAR)BIT_5; \
  128.      flags._Y = H & (UCHAR)BIT_3; \
  129. }
  130.  
  131. /**** SBC overflow: -+ = + ou +- = - */
  132.  
  133. void sbc_hl_bc() sbc_hl_ss(BC, B);
  134. void sbc_hl_de() sbc_hl_ss(DE, D);
  135. void sbc_hl_hl() sbc_hl_ss(HL, H);
  136. /*
  137. void sbc_hl_hl()
  138. {
  139.      LOCAL USHORT tmp;
  140.  
  141.      T(15);
  142.      flags._N = 1;
  143.      flags._Z = !(flags._S = flags._X = flags._Y =
  144.              HL = (flags._C ? (USHORT)0xffff:(USHORT)0));
  145.      flags._P = flags._C = flags._H = 0;
  146.      HL = tmp;
  147. }
  148. */
  149. void sbc_hl_sp() sbc_hl_ss(SP, SP >> 8);
  150.  
  151. #undef sbc_hl_ss
  152.  
  153. /*=========================================================================*
  154.  *                            inc_ss                                       *
  155.  *=========================================================================*/
  156.  
  157. #define inc_ss(r,TS) { T(TS); (r)++; }
  158.  
  159. void inc_bc() inc_ss(BC,6);
  160. void inc_de() inc_ss(DE,6);
  161. void inc_hl() inc_ss(HL,6);
  162. void inc_ix() inc_ss(IX,10);
  163. void inc_iy() inc_ss(IY,10);
  164. void inc_sp() inc_ss(SP,6);
  165.  
  166. #undef inc_ss
  167.  
  168. /*=========================================================================*
  169.  *                            dec_ss                                       *
  170.  *=========================================================================*/
  171.  
  172. #define dec_ss(r,TS) { T(TS); (r)--; }
  173.  
  174. void dec_bc() dec_ss(BC,6);
  175. void dec_de() dec_ss(DE,6);
  176. void dec_hl() dec_ss(HL,6);
  177. void dec_ix() dec_ss(IX,10);
  178. void dec_iy() dec_ss(IY,10);
  179. void dec_sp() dec_ss(SP,6);
  180.  
  181. #undef dec_ss
  182.  
  183. /* EOF: Math16Bi.c */
  184.