home *** CD-ROM | disk | FTP | other *** search
/ Emulator Universe CD / emulatoruniversecd1998.iso / Speccy / Emulators / winemu / SOURCES / Z80 / MATH16BI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-01  |  5.5 KB  |  187 lines

  1. /* Math16Bi.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 ) || ((tmp==hl) && (ss)); \
  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. void add_hl_hl() add_hl_ss(HL,HL,H,11);
  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)) \
  76.                     > (USHORT)0xFFF ); \
  77.     flags._N = 0; \
  78.     flags._C = ( (tmp = (USHORT)(HL + (ss) + (USHORT) \
  79.          (flags._C != 0 ))) < HL ) || ((tmp==HL) && (ss)); \
  80.     flags._P = ( ((H & BIT_7) == ((r) & BIT_7)) && ((HL ^ tmp) \
  81.                 & (USHORT)0x8000) ); \
  82.     flags._Z = !(HL = tmp); \
  83.     flags._S = (H & BIT_7); \
  84.     flags._X = H & (UCHAR)BIT_5; \
  85.     flags._Y = H & (UCHAR)BIT_3; \
  86. }
  87.  
  88.  
  89. /**** ADC overflow: -- = + ou ++ = - */
  90.  
  91. void adc_hl_bc() adc_hl_ss(BC, B);
  92. void adc_hl_de() adc_hl_ss(DE, D);
  93. void adc_hl_hl() adc_hl_ss(HL, H);
  94. /*
  95. void adc_hl_hl()
  96. {
  97.     LOCAL USHORT tmp;
  98.  
  99.     T(15);
  100.     flags._H = ((HL & (USHORT)0xFFF) > (USHORT)0x7FF);
  101.     flags._N = 0;
  102.     flags._C = (HL > (USHORT)0x7FFF);
  103.     flags._P = ( (HL ^ tmp) & (USHORT)0x8000);
  104.     flags._Z = !(HL += HL + (flags._C != 0));
  105.     flags._S = (H & BIT_7);
  106.     flags._X = H & (UCHAR)BIT_5;
  107.     flags._Y = H & (UCHAR)BIT_3;
  108. }
  109. */
  110. void adc_hl_sp() adc_hl_ss(SP, SP >> 8);
  111.  
  112. #undef adc_hl_ss
  113.  
  114. /*=========================================================================*
  115.  *                            sbc_hl_ss                                    *
  116.  *=========================================================================*/
  117.  
  118. #define sbc_hl_ss(ss, r) { \
  119.      LOCAL USHORT tmp; \
  120.      \
  121.      T(15); \
  122.      flags._H = (((ss) & (USHORT)0xFFF) > (HL & (USHORT)0xFFF) ); \
  123.      flags._N = 1; \
  124.      flags._Z = !(tmp = (USHORT)(HL - (ss) - \
  125.             (USHORT)(flags._C != 0)) ); \
  126.      flags._C = ((ss) > HL)||(tmp>HL); \
  127.      flags._P = ( ((H & BIT_7) != ((r) & BIT_7)) && ((HL ^ tmp) \
  128.                 & (USHORT)0x8000) ); \
  129.      flags._S = ((HL = tmp) & (USHORT)0x8000); \
  130.      flags._X = H & (UCHAR)BIT_5; \
  131.      flags._Y = H & (UCHAR)BIT_3; \
  132. }
  133.  
  134. /**** SBC overflow: -+ = + ou +- = - */
  135.  
  136. void sbc_hl_bc() sbc_hl_ss(BC, B);
  137. void sbc_hl_de() sbc_hl_ss(DE, D);
  138. void sbc_hl_hl() sbc_hl_ss(HL, H);
  139. /*
  140. void sbc_hl_hl()
  141. {
  142.      LOCAL USHORT tmp;
  143.  
  144.      T(15);
  145.      flags._N = 1;
  146.      flags._Z = !(flags._S = flags._X = flags._Y =
  147.              HL = (flags._C ? (USHORT)0xffff:(USHORT)0));
  148.      flags._P = flags._C = flags._H = 0;
  149.      HL = tmp;
  150. }
  151. */
  152. void sbc_hl_sp() sbc_hl_ss(SP, SP >> 8);
  153.  
  154. #undef sbc_hl_ss
  155.  
  156. /*=========================================================================*
  157.  *                            inc_ss                                       *
  158.  *=========================================================================*/
  159.  
  160. #define inc_ss(r,TS) { T(TS); (r)++; }
  161.  
  162. void inc_bc() inc_ss(BC,6);
  163. void inc_de() inc_ss(DE,6);
  164. void inc_hl() inc_ss(HL,6);
  165. void inc_ix() inc_ss(IX,10);
  166. void inc_iy() inc_ss(IY,10);
  167. void inc_sp() inc_ss(SP,6);
  168.  
  169. #undef inc_ss
  170.  
  171. /*=========================================================================*
  172.  *                            dec_ss                                       *
  173.  *=========================================================================*/
  174.  
  175. #define dec_ss(r,TS) { T(TS); (r)--; }
  176.  
  177. void dec_bc() dec_ss(BC,6);
  178. void dec_de() dec_ss(DE,6);
  179. void dec_hl() dec_ss(HL,6);
  180. void dec_ix() dec_ss(IX,10);
  181. void dec_iy() dec_ss(IY,10);
  182. void dec_sp() dec_ss(SP,6);
  183.  
  184. #undef dec_ss
  185.  
  186. /* EOF: Math16Bi.c */
  187.