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

  1. /* ExcTranf.c : exchange, block transfer and search block
  2.  *             instructions emulation.
  3.  *
  4.  * Copyright 1996 Rui Fernando Ferreira Ribeiro.
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /*
  22.  * History:
  23.  *    5th April 96:
  24.  *           . Modified IX/IY logic
  25.  */
  26.  
  27. #include "env.h"
  28.  
  29. /*=========================================================================*
  30.  *                            ex_de_hl                                     *
  31.  *=========================================================================*/
  32. void ex_de_hl()
  33. {
  34.    LOCAL USHORT tmp;
  35.  
  36.    T(4);
  37.    tmp = HL;
  38.    HL  = DE;
  39.    DE  = tmp;
  40. }
  41.  
  42. /*=========================================================================*
  43.  *                            ex_de_ix                                     *
  44.  *=========================================================================*/
  45. void ex_de_ix()
  46. {
  47.    LOCAL USHORT tmp;
  48.  
  49.    T(8);
  50.    /*tmp = IX; */ /* since in comp.sys.sinclair they disagree... */
  51.    tmp = HL;
  52.    /*IX  = DE; */
  53.    HL  = DE;
  54.    DE  = tmp;
  55. }
  56.  
  57. /*=========================================================================*
  58.  *                            ex_de_iy                                     *
  59.  *=========================================================================*/
  60. void ex_de_iy()
  61. {
  62.    LOCAL USHORT tmp;
  63.  
  64.    T(8);
  65.    /* tmp = IY; */
  66.    tmp = HL;
  67.    /* IY  = DE; */
  68.    HL  = DE;
  69.    DE  = tmp;
  70. }
  71.  
  72. /*=========================================================================*
  73.  *                            ex_af_af2                                    *
  74.  *=========================================================================*/
  75. void ex_af_af2()
  76. {
  77.    LOCAL USHORT tmp;
  78.  
  79.    T(4);
  80.    build_F();
  81.    tmp = AF;
  82.    AF  = AF2;
  83.    AF2 = tmp;
  84.    read_F();
  85. }
  86.  
  87. /*=========================================================================*
  88.  *                            exx                                          *
  89.  *=========================================================================*/
  90. void exx()
  91. {
  92.    LOCAL USHORT tmp;
  93.  
  94.    T(4);
  95.    tmp = BC;
  96.    BC  = BC2;
  97.    BC2 = tmp;
  98.    tmp = DE;
  99.    DE  = DE2;
  100.    DE2 = tmp;
  101.    tmp = HL;
  102.    HL  = HL2;
  103.    HL2 = tmp;
  104. }
  105.  
  106. /*=========================================================================*
  107.  *                            ex_psp_hl                                    *
  108.  *=========================================================================*/
  109. void ex_psp_hl()
  110. {
  111.    LOCAL USHORT tmp;
  112.  
  113.    T(19);
  114.    tmp = HL;
  115.    HL  = pop();
  116.    push(tmp);
  117. }
  118.  
  119. /*=========================================================================*
  120.  *                            ex_psp_ix                                    *
  121.  *=========================================================================*/
  122. void ex_psp_ix()
  123. {
  124.    LOCAL USHORT tmp;
  125.  
  126.    T(23);
  127.    tmp = IX;
  128.    IX  = pop();
  129.    push(tmp);
  130. }
  131.  
  132. /*=========================================================================*
  133.  *                            ex_psp_iy                                    *
  134.  *=========================================================================*/
  135. void ex_psp_iy()
  136. {
  137.    LOCAL USHORT tmp;
  138.  
  139.    T(23);
  140.    tmp = IY;
  141.    IY  = pop();
  142.    push(tmp);
  143. }
  144.  
  145.  
  146. /*=========================================================================*
  147.  *                            ldi                                          *
  148.  *=========================================================================*/
  149. void ldi()
  150. {
  151. #define ldi \
  152.    T(16); \
  153.    writebyte(DE++, readbyte(HL++) ); \
  154.    flags._P = --BC; \
  155.    flags._H = flags._N = 0;
  156.  
  157.    ldi
  158. }
  159.  
  160. /*=========================================================================*
  161.  *                            ldir                                         *
  162.  *=========================================================================*/
  163. void ldir()
  164. {
  165.    /* This block is not really needed ---
  166.    just here to speed up emulation
  167.     */
  168.    if(BC > 3)
  169.    {
  170.       LOCAL USHORT tmp;
  171.  
  172.       /* calculate time for next interrupt
  173.        */
  174.       if((tmp=clock_ticks/16)>BC)
  175.      tmp = BC;
  176.  
  177.       while(tmp > 1)
  178.       {
  179.      /* If LDIR would auto-modify its on opcode, will have to leave
  180.       */
  181.      if((HL - PC) < 2)
  182.         break;
  183.      writebyte(DE++, readbyte(HL++));
  184.      BC--;
  185.      tmp--;
  186.       }
  187.    }
  188.    /* The instruction would work only with the following code
  189.     */
  190.    ldi
  191.    if(BC)
  192.    {
  193.       T(5);
  194.       PC -= 2;
  195.    }
  196. }
  197.  
  198. #undef ldi
  199.  
  200. /*=========================================================================*
  201.  *                            ldd                                          *
  202.  *=========================================================================*/
  203. void ldd()
  204. {
  205. #define ldd \
  206.    T(16); \
  207.    writebyte(DE--, readbyte(HL--) ); \
  208.    flags._P = --BC; \
  209.    flags._H = flags._N = 0;
  210.  
  211.    ldd
  212. }
  213.  
  214. /*=========================================================================*
  215.  *                            lddr                                         *
  216.  *=========================================================================*/
  217. void lddr()
  218. {
  219.    /* This block is not really needed ---
  220.    just here to speed up emulation
  221.     */
  222.    if(BC > 3)
  223.    {
  224.       LOCAL USHORT tmp;
  225.  
  226.       /* calculate time for next interrupt
  227.        */
  228.       if((tmp=clock_ticks/16)>BC)
  229.      tmp = BC;
  230.  
  231.       while(tmp > 1)
  232.       {
  233.      /* If LDDR would auto-modify its on opcode, will have to leave
  234.       */
  235.      if((PC - HL) < 2)
  236.         break;
  237.      writebyte(DE--, readbyte(HL--));
  238.      BC--;
  239.      tmp--;
  240.       }
  241.    }
  242.    /* The instruction would work only with the following code
  243.     */
  244.    ldd
  245.    if(BC)
  246.    {
  247.       T(5);
  248.       PC -= 2;
  249.    }
  250. }
  251.  
  252. #undef ldd
  253.  
  254. /*=========================================================================*
  255.  *                            cpi                                          *
  256.  *=========================================================================*/
  257. void cpi()
  258. {
  259.  
  260. #define cpi \
  261.    LOCAL UCHAR tmp, n; \
  262.    \
  263.    T(16); \
  264.    flags._N = 1; \
  265.    \
  266.    /* cp_phl ∩inline∩ */ \
  267.    flags._H = (((n = readbyte(HL)) & 0xF) > (A & 0xF)); \
  268.    flags._Z = !(tmp = (UCHAR)((UCHAR)A - (UCHAR)n)); \
  269.    flags._C = (n > A); \
  270.    flags._S = (tmp & BIT_7); \
  271.    /* -------------------------- */ \
  272.    flags._P = --BC; \
  273.    HL++; \
  274.    flags._X = tmp & (UCHAR)BIT_5; \
  275.    flags._Y = tmp & (UCHAR)BIT_3;
  276.  
  277.    cpi
  278. }
  279.  
  280. /*=========================================================================*
  281.  *                            cpir                                         *
  282.  *=========================================================================*/
  283. void cpir()
  284. {
  285.    cpi
  286.    if(BC && !flags._Z)
  287.    {
  288.       T(5);
  289.       PC -= 2;
  290.    }
  291. }
  292.  
  293. #undef cpi
  294.  
  295. /*=========================================================================*
  296.  *                            cpd                                          *
  297.  *=========================================================================*/
  298. void cpd()
  299. {
  300. #define cpd \
  301.    LOCAL UCHAR tmp, n; \
  302.    \
  303.    T(16); \
  304.    flags._N = 1; \
  305.    flags._H = (((n  = readbyte(HL)) & 0xF) > (A & 0xF)); \
  306.    flags._Z = !(tmp = (UCHAR)((UCHAR)A - (UCHAR)n)); \
  307.    flags._C = (n > A); \
  308.    flags._S = (tmp & BIT_7); \
  309.    flags._P = --BC; \
  310.    HL--; \
  311.    flags._X = tmp & (UCHAR)BIT_5; \
  312.    flags._Y = tmp & (UCHAR)BIT_3;
  313.  
  314.    cpd
  315. }
  316.  
  317. /*=========================================================================*
  318.  *                            cpdr                                         *
  319.  *=========================================================================*/
  320. void cpdr()
  321. {
  322.    cpd
  323.    if(BC && !flags._Z)
  324.    {
  325.       T(5);
  326.       PC -= 2;
  327.    }
  328. }
  329.  
  330. #undef cpd
  331.  
  332. /* EOF: ExcTranf.c */
  333.