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

cami at codespeak.net cami at codespeak.net
Mon Apr 28 18:00:29 CEST 2008


Author: cami
Date: Mon Apr 28 18:00:26 2008
New Revision: 54175

Modified:
   pypy/branch/gameboy-emulator/pypy/lang/gameboy/cpu.py
   pypy/branch/gameboy-emulator/pypy/lang/gameboy/test/test_cpu.py
Log:
added some more cpu tests
fixed some minor bugs in cpu



Modified: pypy/branch/gameboy-emulator/pypy/lang/gameboy/cpu.py
==============================================================================
--- pypy/branch/gameboy-emulator/pypy/lang/gameboy/cpu.py	(original)
+++ pypy/branch/gameboy-emulator/pypy/lang/gameboy/cpu.py	Mon Apr 28 18:00:26 2008
@@ -362,13 +362,13 @@
         self.cycles -= 2
 
      # Fetching  1 cycle
-    def fetch(self):
+    def fetch(self, useCycles=True):
         self.cycles += 1
-        if (self.pc.get() <= 0x3FFF):
-            data =  self.rom[self.pc.get()]
+        if self.pc.get(useCycles) <= 0x3FFF:
+            data =  self.rom[self.pc.get(useCycles)]
         else:
-            data = self.memory.read(self.pc.get())
-        self.pc.inc() # 2 cycles
+            data = self.memory.read(self.pc.get(useCycles))
+        self.pc.inc(useCycles) # 2 cycles
         return data
     
     def fetchDoubleAddress(self):
@@ -464,7 +464,7 @@
         
     def carryFlagFinish(self, s, data):
         self.f.reset()
-        # set the hfalg if the 0x10 bit was affected
+        # set the hflag if the 0x10 bit was affected
         if ((s ^ self.a.get() ^ data) & 0x10) != 0:
             self.f.hFlag = True
         if s >= 0x100:
@@ -473,10 +473,14 @@
         self.a.set(s)  # 1 cycle
         
     # 1 cycle
-    def subtract(self, getter, setter=None):
+    def subtractA(self, getter, setter=None):
         self.compareA(getter, setter) # 1 cycle
         self.a.sub(getter(useCycles=False), False)
 
+    def fetchSubtractA(self):
+        data = self.fetch()
+        self.subtractA(lambda useCycles=False: data)
+
     # 1 cycle
     def compareA(self, getter, setter=None):
         s = (self.a.get() - getter()) & 0xFF
@@ -798,8 +802,8 @@
         self.call(self.fetchDoubleAddress())  # 4+2 cycles
 
      # CALL cc,nnnn, 3,6 cycles
-    def conditionalCall(self, getter):
-        if getter():
+    def conditionalCall(self, cc):
+        if cc:
             self.unconditionalCall() # 6 cycles
         else:
             self.pc.add(2) # 3 cycles
@@ -965,7 +969,7 @@
     (0xCD, CPU.unconditionalCall),
     (0xC6, lambda s: CPU.addA(s, s.fetch)),
     (0xCE, lambda s: CPU.addWithCarry(s,  s.fetch)),
-    (0xD6, lambda s: CPU.subtract(s,  s.fetch)),
+    (0xD6, CPU.fetchSubtractA),
     (0xDE, lambda s: CPU.subtractWithCarry(s,  s.fetch)),
     (0xE6, lambda s: CPU.AND(s,  s.fetch)),
     (0xEE, lambda s: CPU.XOR(s,  s.fetch)),
@@ -987,7 +991,7 @@
     (0x06, 0x08, CPU.loadFetchRegister),
     (0x80, 0x01, CPU.addA),    
     (0x88, 0x01, CPU.addWithCarry),    
-    (0x90, 0x01, CPU.subtract),    
+    (0x90, 0x01, CPU.subtractA),    
     (0x98, 0x01, CPU.subtractWithCarry),    
     (0xA0, 0x01, CPU.AND),    
     (0xA8, 0x01, CPU.XOR),    

Modified: pypy/branch/gameboy-emulator/pypy/lang/gameboy/test/test_cpu.py
==============================================================================
--- pypy/branch/gameboy-emulator/pypy/lang/gameboy/test/test_cpu.py	(original)
+++ pypy/branch/gameboy-emulator/pypy/lang/gameboy/test/test_cpu.py	Mon Apr 28 18:00:26 2008
@@ -1377,18 +1377,53 @@
     assert cpu.pc.get() == cpu.interrupt.vBlank.callCode
     assert cpu.ime == False
 
+def conditionalCallTest(cpu, opCode, flagSetter):
+    flagSetter(cpu, False)
+    cpu.pc.set(0)
+    f = cpu.f.get()
+    cycle_test(cpu, 0xC4, 3)
+    assert_default_registers(cpu, pc=2, f=f)
+    
+    cpu.reset()
+    fetchValue = 0x1234
+    flagSetter(cpu, True)
+    cpu.sp.set(fetchValue)
+    prepare_for_fetch(cpu, fetchValue)
+    f = cpu.f.get()
+    cycle_test(cpu, 0xC4, 6)
+    assert_default_registers(cpu, pc=fetchValue, sp=fetchValue-2, f=f)
+    
 # call_NZ_nnnn
 def test_0xC4():
-    pass
+    cpu = get_cpu()
+    conditionalCallTest(cpu, 0xC4, setFlag0xC4)
+    
+def setFlag0xC4(cpu, value):
+    cpu.f.zFlag = not value
+    
 # call_Z_nnnn
 def test_0xCC():
-    pass
+    cpu = get_cpu()
+    conditionalCallTest(cpu, 0xCC, setFlag0xC4)
+
+def setFlag0xCC(cpu, value):
+    cpu.f.cFlag = not value
+    
 # call_NC_nnnn
 def test_0xD4():
-    pass
+    cpu = get_cpu()
+    conditionalCallTest(cpu, 0xD4, setFlag0xC4)
+
+def setFlag0xD4(cpu, value):
+    cpu.f.cFlag = value
+    
 # call_C_nnnn
 def test_0xDC():
-    pass
+    cpu = get_cpu()
+    conditionalCallTest(cpu, 0xDC, setFlag0xC4)
+
+def setFlag0xDC(cpu, value):
+    cpu.f.zFlag = value
 
 # push_BC to push_AF
 def test_0xC5_to_0xF5():
@@ -1407,39 +1442,67 @@
 
 # call_nnnn
 def test_0xCD():
-    pass
+    cpu = get_cpu()
+    fetchValue = 0x1234
+    cpu.sp.set(fetchValue)
+    prepare_for_fetch(cpu, fetchValue)
+    cycle_test(cpu, 0xCD, 6)
+    assert_default_registers(cpu, pc=fetchValue, sp=fetchValue-2)
+
+def a_nn_test(opCode, cycles, opCaller):
+    # flags tested already
+    cpu = get_cpu()
+    value = 0x12
+    valueAdd = 0x12
+    cpu.a.set(value)
+    prepare_for_fetch(cpu, valueAdd,)
+    pc = cpu.pc.get()
+    
+    cycle_test(cpu, opCode, cycles)
+    assert_default_registers(cpu, a=opCaller(value,valueAdd, cpu), pc=pc+1, f=cpu.f.get())
+    return cpu
 
 # add_A_nn
 def test_0xC6():
-    pass
+    a_nn_test(0xC6, 2, lambda a, b, cpu: a+b)
 
 # adc_A_nn
 def test_0xCE():
-    pass
+    a_nn_test(0xCE, 2, lambda a, b, cpu: a+b)
 
 # sub_A_nn
 def test_0xD6():
-    pass
+    a_nn_test(0xD6, 2, lambda a, b, cpu: a-b)
 
 # sbc_A_nn
 def test_0xDE():
-    pass
+    a_nn_test(0xDE, 2, lambda a, b, cpu: a-b)
 
 # and_A_nn
 def test_0xE6():
-    pass
+    a_nn_test(0xE6, 2, lambda a, b, cpu: a&b)
 
 # xor_A_nn
 def test_0xEE():
-    pass
+    a_nn_test(0xEE, 2, lambda a, b, cpu: a^b)
 
 # or_A_nn
 def test_0xF6():
-    pass
+    a_nn_test(0xF6, 2, lambda a, b, cpu: a|b)
 
 # cp_A_nn
 def test_0xFE():
-    pass
+    # flags tested already
+    cpu = get_cpu()
+    value = 0x12
+    valueA = 0x12
+    cpu.a.set(valueA)
+    pc = cpu.pc.get()
+    
+    cycle_test(cpu, 0xFE, 2)
+    
+    assert_default_registers(cpu, a=valueA, pc=pc+1, f=cpu.f.get())
+    assert cpu.f.zFlag == True
 
 # rst(0x00) to rst(0x38)
 def test_0xC7_to_0xFF():



More information about the Pypy-commit mailing list