[pypy-svn] r52983 - in pypy/branch/gameboy-emulator/pypy/lang/gameboy: . test

cami at codespeak.net cami at codespeak.net
Thu Mar 27 09:47:52 CET 2008


Author: cami
Date: Thu Mar 27 09:47:50 2008
New Revision: 52983

Modified:
   pypy/branch/gameboy-emulator/pypy/lang/gameboy/constants.py
   pypy/branch/gameboy-emulator/pypy/lang/gameboy/joypad.py
   pypy/branch/gameboy-emulator/pypy/lang/gameboy/test/test_joypad.py
Log:
implemented joypad tests
added joypaddriver class
made joypad more readable by introducing separate vars for joyp and buttons


Modified: pypy/branch/gameboy-emulator/pypy/lang/gameboy/constants.py
==============================================================================
--- pypy/branch/gameboy-emulator/pypy/lang/gameboy/constants.py	(original)
+++ pypy/branch/gameboy-emulator/pypy/lang/gameboy/constants.py	Thu Mar 27 09:47:50 2008
@@ -157,6 +157,17 @@
 JOYPAD_CLOCK = GAMEBOY_CLOCK >> 6
 
 
+BUTTON_DOWN = 0x08
+BUTTON_UP = 0x04
+BUTTON_LEFT = 0x02
+BUTTON_RIGHT = 0x01
+ 
+BUTTON_START = 0x08
+BUTTON_SELECT = 0x04
+BUTTON_B = 0x02
+BUTTON_A = 0x01
+
+
 
 # ___________________________________________________________________________
 # SERIAL

Modified: pypy/branch/gameboy-emulator/pypy/lang/gameboy/joypad.py
==============================================================================
--- pypy/branch/gameboy-emulator/pypy/lang/gameboy/joypad.py	(original)
+++ pypy/branch/gameboy-emulator/pypy/lang/gameboy/joypad.py	Thu Mar 27 09:47:50 2008
@@ -14,7 +14,8 @@
         self.reset()
 
     def reset(self):
-        self.joyp = 0xFF
+        self.joyp = 0xF
+        self.buttons = 0xF
         self.cycles = constants.JOYPAD_CLOCK
 
     def cycles(self):
@@ -29,36 +30,42 @@
 
     def write(self, address, data):
         if (address == constants.JOYP):
-            self.joyp = (self.joyp & 0xCF) + (data & 0x30)
+            self.joyp = (self.joyp & 0xC) + (data & 0x3)
             self.update()
 
     def read(self, address):
         if (address == constants.JOYP):
-            return self.joyp
+            return (self.joyp << 4) + self.buttons
         return 0xFF
 
     def update(self):
-        data = self.joyp & 0xF0
+        oldButtons = self.buttons
+        if self.joyp == 0x1:
+            self.buttons = self.driver.getButtons()
+        elif self.joyp == 0x2:
+            self.buttons = self.driver.getDirections()
+        else:
+            self.buttons  = 0xF
 
-        switch = (data & 0x30)
-        if switch==0x10:
-            data |= self.driver.getButtons()
-        elif switch==0x20:
-            data |= self.driver.getDirections()
-        elif switch==0x30:
-            data |= 0x0F
-
-        if ((self.joyp & ~data & 0x0F) != 0):
+        if oldButtons != self.buttons:
             self.interrupt.raiseInterrupt(constants.JOYPAD)
 
-        self.joyp = data
-
 
 
-class Driver(object):
-    
+class JoypadDriver(object):
+    """
+    Maps the Input to the Button and Direction Codes
+    """
+    def __init__(self):
+        self.raised = False
+        self.buttons = 0xF
+        self.directions = 0xF
+        
     def getButtons(self):
-        pass
+        return self.buttons
     
     def getDirections(self):
-        pass
\ No newline at end of file
+        return self.directions
+    
+    def isRaised(self):
+        return self.raised
\ No newline at end of file

Modified: pypy/branch/gameboy-emulator/pypy/lang/gameboy/test/test_joypad.py
==============================================================================
--- pypy/branch/gameboy-emulator/pypy/lang/gameboy/test/test_joypad.py	(original)
+++ pypy/branch/gameboy-emulator/pypy/lang/gameboy/test/test_joypad.py	Thu Mar 27 09:47:50 2008
@@ -0,0 +1,81 @@
+from pypy.lang.gameboy.joypad import *
+from pypy.lang.gameboy.interrupt import *
+from pypy.lang.gameboy import constants
+
+
+def get_joypad():
+    return Joypad(get_driver(), Interrupt())
+
+def get_driver():
+    return JoypadDriver()
+
+
+# ------------------------------------------------------------------------------
+
+def test_reset(joypad=None):
+    if joypad == None:
+        joypad = get_joypad()
+    assert joypad.joyp == 0xF
+    assert joypad.cycles == constants.JOYPAD_CLOCK
+        
+def test_emulate():
+    joypad = get_joypad()
+    ticks = 2
+    cycles = joypad.cycles
+    joypad.emulate(ticks)
+    assert cycles - joypad.cycles == ticks
+
+def test_emulate_zero_ticks():
+    joypad = get_joypad()
+    joypad.cycles = 2
+    ticks = 2
+    joypad.emulate(ticks)
+    assert joypad.cycles == constants.JOYPAD_CLOCK
+    
+def test_emulate_zero_ticks_update():   
+    joypad = get_joypad() 
+    value = 0x1
+    valueButtons = 0x4
+    joypad.joyp = value
+    joypad.driver.buttons = valueButtons
+    joypad.driver.raised = True
+    joypad.cycles = 2
+    ticks = 2
+    joypad.emulate(ticks)
+    assert joypad.cycles == constants.JOYPAD_CLOCK
+    assert joypad.joyp == value
+    assert joypad.buttons == valueButtons
+    
+def test_read_write():
+    joypad = get_joypad()
+    value = 0x2
+    joypad.write(constants.JOYP, value)
+    joyp = 0xC + (value & 0x3)
+    assert joypad.joyp == joyp
+    joyp = (joyp << 4) + 0xF
+    assert joypad.read(constants.JOYP) == joyp
+    assert joypad.read(constants.JOYP+1) == 0xFF
+    # no change on writing the wrong address
+    joypad.write(constants.JOYP+1, value+1)
+    assert joypad.read(constants.JOYP) == joyp
+    
+    
+def test_update():
+    joypad = get_joypad()
+    joypad.driver.buttons = 0x1
+    assert joypad.driver.getButtons() == 0x1
+    joypad.driver.directions = 0x2
+    assert joypad.driver.getDirections() == 0x2
+    assert joypad.buttons == 0xF
+    joypad.joyp = 0x1
+    joypad.update()
+    assert joypad.buttons == joypad.driver.buttons
+    
+    joypad.joyp = 0x2
+    joypad.update()
+    assert joypad.buttons == joypad.driver.directions
+    
+    joypad.joyp = 0x3
+    joypad.update()
+    assert joypad.buttons == 0xF
+    
\ No newline at end of file



More information about the Pypy-commit mailing list