@@ -101,7 +101,9 @@ public class CentralProcessingUnit extends Thread
101101 // The current operating mode for the CPU
102102 protected int mode ;
103103
104- public static final int DEFAULT_CPU_CYCLE_TIME = 0 ;
104+ public static final int DEFAULT_CPU_CYCLE_TIME = 1 ;
105+
106+ private boolean awaitingKeypress = false ;
105107
106108 CentralProcessingUnit (Memory memory , Keyboard keyboard , Screen screen ) {
107109 this .random = new Random ();
@@ -698,31 +700,29 @@ protected void generateRandomNumber() {
698700 * will be set to 1.
699701 */
700702 protected void drawSprite () {
701- int xRegister = (operand & 0x0F00 ) >> 8 ;
702- int yRegister = (operand & 0x00F0 ) >> 4 ;
703- int xPos = v [xRegister ];
704- int yPos = v [yRegister ];
703+ int x = (operand & 0x0F00 ) >> 8 ;
704+ int y = (operand & 0x00F0 ) >> 4 ;
705705 int numBytes = (operand & 0xF );
706706 v [0xF ] = 0 ;
707707
708708 String drawOperation = "DRAW" ;
709709 if ((mode == MODE_EXTENDED ) && (numBytes == 0 )) {
710710 if (bitplane == 3 ) {
711- drawExtendedSprite (xPos , yPos , 1 );
712- drawExtendedSprite (xPos , yPos , 2 );
711+ drawExtendedSprite (v [ x ], v [ y ] , 1 , index );
712+ drawExtendedSprite (v [ x ], v [ y ] , 2 , index + 32 );
713713 } else {
714- drawExtendedSprite (xPos , yPos , bitplane );
714+ drawExtendedSprite (v [ x ], v [ y ] , bitplane , index );
715715 }
716716 drawOperation = "DRAWEX" ;
717717 } else {
718718 if (bitplane == 3 ) {
719- drawNormalSprite (xPos , yPos , numBytes , 1 );
720- drawNormalSprite (xPos , yPos , numBytes , 2 );
719+ drawNormalSprite (v [ x ], v [ y ] , numBytes , 1 , index );
720+ drawNormalSprite (v [ x ], v [ y ] , numBytes , 2 , index + numBytes );
721721 } else {
722- drawNormalSprite (xPos , yPos , numBytes , bitplane );
722+ drawNormalSprite (v [ x ], v [ y ] , numBytes , bitplane , index );
723723 }
724724 }
725- lastOpDesc = drawOperation + " V" + toHex (xRegister , 1 ) + ", V" + toHex (yRegister , 1 );
725+ lastOpDesc = drawOperation + " V" + toHex (x , 1 ) + ", V" + toHex (y , 1 );
726726 }
727727
728728 /**
@@ -732,26 +732,30 @@ protected void drawSprite() {
732732 * @param xPos the x position to draw the sprite at
733733 * @param yPos the y position to draw the sprite at
734734 * @param bitplane the bitplane to draw to
735+ * @param activeIndex the effective index to use when loading sprite data
735736 */
736- private void drawExtendedSprite (int xPos , int yPos , int bitplane ) {
737+ private void drawExtendedSprite (int xPos , int yPos , int bitplane , int activeIndex ) {
737738 for (int yIndex = 0 ; yIndex < 16 ; yIndex ++) {
738739 for (int xByte = 0 ; xByte < 2 ; xByte ++) {
739- short colorByte = memory .read (index + (yIndex * 2 ) + xByte );
740+ short colorByte = memory .read (activeIndex + (yIndex * 2 ) + xByte );
740741 int yCoord = yPos + yIndex ;
741- yCoord = yCoord % screen .getHeight ();
742-
743- int mask = 0x80 ;
744-
745- for (int xIndex = 0 ; xIndex < 8 ; xIndex ++) {
746- int xCoord = xPos + xIndex + (xByte * 8 );
747- xCoord = xCoord % screen .getWidth ();
748-
749- boolean turnOn = (colorByte & mask ) > 0 ;
750- boolean currentOn = screen .getPixel (xCoord , yCoord , bitplane );
751-
752- v [0xF ] += (turnOn && currentOn ) ? (short ) 1 : (short ) 0 ;
753- screen .drawPixel (xCoord , yCoord , turnOn ^ currentOn , bitplane );
754- mask = mask >> 1 ;
742+ if (yCoord < screen .getHeight ()) {
743+ yCoord = yCoord % screen .getHeight ();
744+ int mask = 0x80 ;
745+
746+ for (int xIndex = 0 ; xIndex < 8 ; xIndex ++) {
747+ int xCoord = xPos + xIndex + (xByte * 8 );
748+ xCoord = xCoord % screen .getWidth ();
749+
750+ boolean turnOn = (colorByte & mask ) > 0 ;
751+ boolean currentOn = screen .getPixel (xCoord , yCoord , bitplane );
752+
753+ v [0xF ] += (turnOn && currentOn ) ? (short ) 1 : (short ) 0 ;
754+ screen .drawPixel (xCoord , yCoord , turnOn ^ currentOn , bitplane );
755+ mask = mask >> 1 ;
756+ }
757+ } else {
758+ v [0xF ] += 1 ;
755759 }
756760 }
757761 }
@@ -764,10 +768,11 @@ private void drawExtendedSprite(int xPos, int yPos, int bitplane) {
764768 * @param yPos the Y position of the sprite
765769 * @param numBytes the number of bytes to draw
766770 * @param bitplane the bitplane to draw to
771+ * @param activeIndex the effective index to use when loading sprite data
767772 */
768- private void drawNormalSprite (int xPos , int yPos , int numBytes , int bitplane ) {
773+ private void drawNormalSprite (int xPos , int yPos , int numBytes , int bitplane , int activeIndex ) {
769774 for (int yIndex = 0 ; yIndex < numBytes ; yIndex ++) {
770- short colorByte = memory .read (index + yIndex );
775+ short colorByte = memory .read (activeIndex + yIndex );
771776 int yCoord = yPos + yIndex ;
772777 yCoord = yCoord % screen .getHeight ();
773778
@@ -909,18 +914,32 @@ protected void moveDelayTimerIntoRegister() {
909914 * into the specified register.
910915 */
911916 protected void waitForKeypress () {
912- int x = (operand & 0x0F00 ) >> 8 ;
917+ awaitingKeypress = true ;
918+ }
919+
920+ /**
921+ * Returns whether the CPU is waiting for a keypress before continuing.
922+ *
923+ * @return false if the CPU is waiting for a keypress, true otherwise
924+ */
925+ protected boolean isAwaitingKeypress () {
926+ return awaitingKeypress ;
927+ }
928+
929+ /**
930+ * Reads a keypress from keyboard, decodes it, and places the value in the
931+ * specified register. If no key is waiting, returns without doing anything.
932+ */
933+ protected void decodeKeypressAndContinue () {
913934 int currentKey = keyboard .getCurrentKey ();
914- while (currentKey == 0 ) {
915- try {
916- Thread .sleep (300 );
917- } catch (InterruptedException e ) {
918- e .printStackTrace ();
919- }
920- currentKey = keyboard .getCurrentKey ();
935+ if (currentKey == -1 ) {
936+ return ;
921937 }
938+
939+ int x = (operand & 0x0F00 ) >> 8 ;
922940 v [x ] = (short ) currentKey ;
923941 lastOpDesc = "KEYD V" + toHex (x , 1 );
942+ awaitingKeypress = false ;
924943 }
925944
926945 /**
@@ -1077,6 +1096,7 @@ public void reset() {
10771096 if (screen != null ) {
10781097 screen .clearScreen (bitplane );
10791098 }
1099+ awaitingKeypress = false ;
10801100 }
10811101
10821102 /**
0 commit comments