home *** CD-ROM | disk | FTP | other *** search
/ PC Shareware 1996 December / PC_Shareware-1996-12.iso / windows / spectrum / sources / z80 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-23  |  6.1 KB  |  212 lines

  1. /* Misc.c: Z80 misc arithmetic & CPU control 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.  *                            daa                                          *
  24.  *=========================================================================*/
  25. void daa()
  26. {
  27.    T(4);
  28.    if(flags._H = (((A & (UCHAR)0xF) > (UCHAR)9) || flags._H ))
  29.       A += (flags._N) ? (UCHAR)(-6) : (UCHAR)6;
  30.    if(flags._C = ((A > (UCHAR)0x9F) || flags._C ))
  31.       A += (flags._N) ? (UCHAR)(-0x60) : (UCHAR)0x60;
  32.    flags._S = ( A & (UCHAR)BIT_7);
  33.    flags._Z = !A;
  34.    flags._P = parity(A);
  35.    flags._X = A & (UCHAR)BIT_5;
  36.    flags._Y = A & (UCHAR)BIT_3;
  37. }
  38.  
  39. /*=========================================================================*
  40.  *                            cpl                                          *
  41.  *=========================================================================*/
  42. void cpl()
  43. {
  44.    T(4);
  45.    A = (UCHAR)~A;
  46.    flags._H = flags._N = 1;
  47.    flags._X = A & (UCHAR)BIT_5;
  48.    flags._Y = A & (UCHAR)BIT_3;
  49. }
  50.  
  51. /*=========================================================================*
  52.  *                            neg                                          *
  53.  *=========================================================================*/
  54. void neg()
  55. {
  56.    T(8);
  57.    flags._H = !(A & (UCHAR)0xF);
  58.    flags._C = !A;
  59.    flags._P = (A == (UCHAR)BIT_7);
  60.    flags._S = ((A =(UCHAR)(~A + 1)) & (UCHAR)BIT_7);
  61.    flags._Z = !A;
  62.    flags._N = 1;
  63.    flags._X = A & (UCHAR)BIT_5;
  64.    flags._Y = A & (UCHAR)BIT_3;
  65. }
  66.  
  67. /*=========================================================================*
  68.  *                            ccf                                          *
  69.  *=========================================================================*/
  70. void ccf()
  71. {
  72.    T(4);
  73.    flags._C = !(flags._H = flags._C);
  74.    flags._N = 0;
  75. }
  76.  
  77. /*=========================================================================*
  78.  *                            scf                                          *
  79.  *=========================================================================*/
  80. void scf()
  81. {
  82.    T(4);
  83.    flags._C = 1;
  84.    flags._H = flags._N = 0;
  85. }
  86.  
  87. /*=========================================================================*
  88.  *                            nop                                          *
  89.  *=========================================================================*/
  90. void nop()
  91. {
  92.    T(4);
  93. }
  94.  
  95. /*=========================================================================*
  96.  *                            halt                                         *
  97.  *=========================================================================*/
  98. void halt()
  99. {
  100.    if(!IFF1)
  101.    {
  102.       Panic("HALT with interrupts disabled!");
  103.    }
  104.  
  105.    /* This loop is cutted because timing software is already
  106.       working
  107.     */
  108.    /*while(!int_time())
  109.    {
  110.       nop();
  111.    } */
  112.    clock_ticks = INT_TIME + (clock_ticks % 4 );
  113. }
  114.  
  115. /*=========================================================================*
  116.  *                            di                                           *
  117.  *=========================================================================*/
  118. void di()
  119. {
  120.    T(4);
  121.    IFF1 = IFF2 = 0;
  122. }
  123.  
  124. /*=========================================================================*
  125.  *                            ei                                           *
  126.  *=========================================================================*/
  127. void ei()
  128. {
  129.    T(4);
  130.    if(int_time())
  131.       clock_ticks = INT_TIME - 1;
  132.  
  133.    IFF1 = IFF2 = 1;
  134. }
  135.  
  136. /*=========================================================================*
  137.  *                            im_0                                         *
  138.  *=========================================================================*/
  139. void im_0()
  140. {
  141.    T(8);
  142.    _IM = 0;
  143. }
  144.  
  145. /*=========================================================================*
  146.  *                            im_1                                         *
  147.  *=========================================================================*/
  148. void im_1()
  149. {
  150.    T(8);
  151.    _IM = 1;
  152. }
  153.  
  154. /*=========================================================================*
  155.  *                            im_2                                         *
  156.  *=========================================================================*/
  157. void im_2()
  158. {
  159.    T(8);
  160.    _IM = 2;
  161. }
  162.  
  163.  
  164. /*** auxiliary routines  ***/
  165.  
  166. /*=========================================================================*
  167.  *                            dummyED                                      *
  168.  *=========================================================================*/
  169. void dummyED()
  170. {
  171.    T(8);
  172.    /* invalid opcode (non existant) */
  173. }
  174.  
  175. void dummyIX()
  176. {
  177.    T(4);
  178.    PC--;
  179. }
  180.  
  181. void dummyIY()
  182. {
  183.    T(4);
  184.    PC--;
  185. }
  186.  
  187. /* No longer used! --- replace by an array
  188.  */
  189.  
  190. /*=========================================================================*
  191.  *                            parity                                       *
  192.  *=========================================================================*/
  193. /* parity -- in Z80 parity is even */
  194. /* USHORT parity(num)
  195. UCHAR num;
  196. {
  197.    LOCAL UCHAR parit;
  198.    LOCAL UCHAR number;
  199.  
  200.    number = num;
  201.    parit = 1;
  202.    while(number)
  203.    {
  204.       if((UCHAR)0x01 & number)
  205.      parit ^= (UCHAR)1;
  206.       number >>= 1;
  207.    }
  208.    return((USHORT)parit);
  209. }
  210. */
  211. /* Misc.c */
  212.