Coverage Report - uk.co.javagear.EmulateLoop
 
Classes in this File Line Coverage Branch Coverage Complexity
EmulateLoop
0% 
0% 
2.444
 
 1  
 /*
 2  
  * EmulateLoop.java
 3  
  *
 4  
  * This file is part of JavaGear.
 5  
  *
 6  
  * JavaGear 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  
  * JavaGear 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 JavaGear; if not, write to the Free Software
 18  
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 19  
  */
 20  
 
 21  
 package uk.co.javagear;
 22  
 
 23  
 /**
 24  
  * Ties JavaGear's components together.
 25  
  * It loops the emulation with the appropriate NTSC/PAL Timing.
 26  
  *
 27  
  * @author Copyright (C) 2002-2003 Chris White
 28  
  * @version 18th January 2003
 29  
  * @see "JavaGear Final Project Report"
 30  
  */
 31  
 public class EmulateLoop implements Runnable {
 32  
     
 33  
     /**
 34  
      * Pointer to VDP.
 35  
      */
 36  
     protected Vdp vdp;
 37  
     
 38  
     /**
 39  
      * Pointer to Z80.
 40  
      */
 41  
     protected Z80 z80;
 42  
     
 43  
     /**
 44  
      * Pointer to SN76496 PSG.
 45  
      */
 46  
     protected SN76496 sn76496;
 47  
     
 48  
     /**
 49  
      * Pointer to Memory.
 50  
      */
 51  
     protected Memory mem;
 52  
     
 53  
     /**
 54  
      * Pointer to Java Screen Panel.
 55  
      */
 56  
     protected Screen screenpanel;
 57  
     
 58  
     /**
 59  
      * Pointer to global system settings.
 60  
      */
 61  
     protected Setup setup;
 62  
     
 63  
     /**
 64  
      * Pointer to this thread.
 65  
      */
 66  
     protected Thread thisThread;
 67  
     
 68  
     /**
 69  
      * Stores whether thread is running.
 70  
      */
 71  
     protected boolean running;
 72  
     
 73  
     /**
 74  
      * Stores Desired FPS.
 75  
      */
 76  
     protected int fps;
 77  
     
 78  
     /**
 79  
      * Number of Scanlines.
 80  
      */
 81  
     protected int numberOfScanlines;
 82  
     
 83  
     /**
 84  
      * CPU Cycles Per Scanline.
 85  
      */
 86  
     protected int cyclesPerLine;
 87  
     
 88  
     /**
 89  
      * EmulateLoop Constructor.
 90  
      *
 91  
      * @param z Z80
 92  
      * @param m Memory
 93  
      * @param v Vdp
 94  
      * @param s SN76496
 95  
      * @param sp Screen
 96  
      * @param setup Setup
 97  
      */
 98  0
     public EmulateLoop(Z80 z, Memory m, Vdp v, SN76496 s, Screen sp, Setup setup) {
 99  0
         this.mem = m;
 100  0
         this.vdp = v;
 101  0
         this.z80 = z;
 102  0
         this.sn76496 = s;
 103  0
         this.screenpanel = sp;
 104  0
         this.setup = setup;
 105  
         
 106  
         // Not Running
 107  0
         running = false;
 108  
         // Default to NTSC Timing
 109  0
         setNTSC();
 110  0
     }
 111  
     
 112  
     /**
 113  
      * Quickly Reset the Z80, Memory, VDP and Sound.
 114  
      */
 115  
     public void reset() {
 116  0
         mem.reset();
 117  0
         z80.reset();
 118  0
         vdp.reset();
 119  0
         sn76496.reset();
 120  0
     }
 121  
     
 122  
     /**
 123  
      * Start Emulation.
 124  
      */
 125  
     public synchronized void startThread() {
 126  0
         if (thisThread == null) {
 127  0
             running = true;
 128  0
             reset();
 129  0
             thisThread = new Thread(this);
 130  0
             thisThread.start();
 131  
         }
 132  0
     }
 133  
     
 134  
     /**
 135  
      * Stop Emulation.
 136  
      */
 137  
     public synchronized void stopThread() {
 138  0
         if (thisThread != null) {
 139  0
             thisThread.interrupt();
 140  0
             running = false;
 141  
         }
 142  
         
 143  0
         while (thisThread != null);
 144  0
     }
 145  
     
 146  
     /**
 147  
      * Suspend Emulation.
 148  
      */
 149  
     public synchronized void suspendThread() {
 150  0
         if (thisThread != null) {
 151  0
             running = false;
 152  
         }
 153  
         
 154  0
         while (thisThread != null);
 155  0
     }
 156  
     
 157  
     /**
 158  
      * Resume suspended Emulation.
 159  
      */
 160  
     public synchronized void resumeThread() {
 161  0
         if (thisThread == null) {
 162  0
             running = true;
 163  0
             thisThread = new Thread(this);
 164  0
             thisThread.start();
 165  
         }
 166  0
     }
 167  
     
 168  
     /**
 169  
      * Set NTSC Timing.
 170  
      */
 171  
     public void setNTSC() {
 172  0
         fps = 60;
 173  0
         numberOfScanlines = 262;
 174  0
         cyclesPerLine = 227;
 175  0
         sn76496.setFPS(fps);
 176  0
         vdp.setNTSC(true);
 177  0
     }
 178  
     
 179  
     
 180  
     /**
 181  
      * Set PAL Timing.
 182  
      */
 183  
     public void setPAL() {
 184  0
         fps = 50;
 185  0
         numberOfScanlines = 312;
 186  0
         cyclesPerLine = 229;
 187  0
         sn76496.setFPS(fps);
 188  0
         vdp.setNTSC(false);
 189  0
     }
 190  
     
 191  
     /**
 192  
      * Run Emulation. This method should not be called directly.
 193  
      * Instead use <code>startThread()</code>.
 194  
      */
 195  
     public void run() {
 196  0
         if (thisThread == null) {
 197  0
             return;
 198  
         }
 199  
         
 200  0
         Throttle.init(fps, thisThread);
 201  
         
 202  0
         while (running) {
 203  0
             boolean drawFrame = !Throttle.skipFrame();
 204  
             
 205  
             // Draw one frame
 206  0
             for (int lineno = 0; lineno < numberOfScanlines; lineno++) {
 207  
                 // Assert Interrupt Line if Necessary
 208  0
                 vdp.interrupts(lineno);
 209  
                 // Draw Next Line
 210  0
                 if (drawFrame) {
 211  0
                     vdp.drawLine(lineno);
 212  
                 }
 213  
                 // Run Z80
 214  0
                 z80.run(cyclesPerLine);
 215  
             }
 216  
             
 217  
             // Refresh Java Screenpanel
 218  0
             if (drawFrame) {
 219  0
                 screenpanel.refresh();
 220  
             }
 221  
             
 222  
             // Output Sound
 223  0
             sn76496.output();
 224  
             
 225  
             // Only Check for Pause Button once per frame to increase emulation speed
 226  0
             if (setup.isPauseButton()) {
 227  0
                 z80.nmi();
 228  0
                 setup.setPauseButton(false);
 229  
             }
 230  
             
 231  0
             Throttle.throttle();
 232  
             
 233  0
         } // End of Running Loop
 234  
         
 235  0
         thisThread = null;
 236  
         
 237  
         // Loop has finished
 238  0
     }
 239  
 }