[pypy-svn] r56868 - in pypy/dist/pypy/lang/gameboy: . debug test

cami at codespeak.net cami at codespeak.net
Wed Jul 30 21:21:11 CEST 2008


Author: cami
Date: Wed Jul 30 21:21:10 2008
New Revision: 56868

Added:
   pypy/dist/pypy/lang/gameboy/debug/
   pypy/dist/pypy/lang/gameboy/debug/gameboy_debug_entry_point.py   (contents, props changed)
Removed:
   pypy/dist/pypy/lang/gameboy/gameboyTest.py
Modified:
   pypy/dist/pypy/lang/gameboy/cpu.py
   pypy/dist/pypy/lang/gameboy/gameboy_implementation.py
   pypy/dist/pypy/lang/gameboy/test/test_cpu.py
Log:
created debug implementation to compare ram and register with the original java version
remove debug statements from the other version (cpu, gameboy_implementation)


Modified: pypy/dist/pypy/lang/gameboy/cpu.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/cpu.py	(original)
+++ pypy/dist/pypy/lang/gameboy/cpu.py	Wed Jul 30 21:21:10 2008
@@ -2,7 +2,10 @@
 from pypy.lang.gameboy import constants
 from pypy.lang.gameboy.ram import *
 from pypy.lang.gameboy.interrupt import *
-#from pypy.lang.gameboy.debug import *
+
+from pypy.rlib.objectmodel import we_are_translated
+if not we_are_translated():
+    from pypy.lang.gameboy.debug import *
 
 # ---------------------------------------------------------------------------
 
@@ -365,7 +368,7 @@
         if self.interrupt.is_pending():
             self.halted = False
             self.cycles -= 4
-        elif (self.cycles > 0):
+        elif self.cycles > 0:
             self.cycles = 0
         
     def lower_pending_interrupt(self):
@@ -377,24 +380,15 @@
                 return
 
     def fetch_execute(self):
-        opCode = self.fetch()
-        #print "    fetch exe:", hex(opCode), "  "
-        #, FETCH_EXECUTE_OP_CODES[opCode].__name__
-        self.last_fetch_execute_op_code = opCode
-        #if DEBUG: log(opCode, is_fetch_execute=True)
-        FETCH_EXECUTE_OP_CODES[opCode](self)
+        op_code = self.fetch()
+        self.last_fetch_execute_op_code = op_code
+        FETCH_EXECUTE_OP_CODES[op_code](self)
         
         
-    def execute(self, opCode):
+    def execute(self, op_code):
         self.instruction_counter += 1
-        #if DEBUG: log(opCode)
-        #print self.instruction_counter, "-"*60
-        #print "exe: ", hex(opCode),  "   "
-        #, OP_CODES[opCode].__name__
-        #print "    pc:", hex(self.pc.get()), "sp:", hex(self.sp.get())
-        #self.print_registers()
-        self.last_op_code = opCode
-        OP_CODES[opCode](self)
+        self.last_op_code = op_code
+        OP_CODES[op_code](self)
         
     def print_registers(self):
         print "    a: "+hex(self.a.get())
@@ -892,7 +886,7 @@
         # RETI 4 cycles
         self.ret() # 4 cycles
         self.enable_interrupts() # 1 cycle + others
-        self.cycles += 1
+        #self.cycles += 1
 
     def restart(self, nn):
         # RST nn 4 cycles
@@ -983,35 +977,35 @@
     def get(self,  use_cycles=True):
         return self.cpu.fetch(use_cycles)
 
-# OPCODE LOOKUP TABLE GENERATION -----------------------------------------------
+# op_code LOOKUP TABLE GENERATION -----------------------------------------------
 
 GROUPED_REGISTERS = [CPU.get_b, CPU.get_c, CPU.get_d,   CPU.get_e,
                      CPU.get_h, CPU.get_l, CPU.get_hli, CPU.get_a]
 
 def create_group_op_codes(table):
-    opCodes =[]
+    op_codes =[]
     for entry in table:
-        opCode   = entry[0]
+        op_code   = entry[0]
         step     = entry[1]
         function = entry[2]
         if len(entry) == 4:
             for registerGetter in GROUPED_REGISTERS:
                 for n in entry[3]:
-                    opCodes.append((opCode, group_lambda(function, registerGetter, n)))
-                    opCode += step
+                    op_codes.append((op_code, group_lambda(function, registerGetter, n)))
+                    op_code += step
         if len(entry) == 5:
             entryStep = entry[4]
             for registerGetter in GROUPED_REGISTERS:
-                stepOpCode = opCode
+                stepop_code = op_code
                 for n in entry[3]:
-                    opCodes.append((stepOpCode, group_lambda(function, registerGetter, n)))
-                    stepOpCode += entryStep
-                opCode+=step
+                    op_codes.append((stepop_code, group_lambda(function, registerGetter, n)))
+                    stepop_code += entryStep
+                op_code+=step
         else:
             for registerGetter in GROUPED_REGISTERS:
-                opCodes.append((opCode,group_lambda(function, registerGetter)))
-                opCode += step
-    return opCodes
+                op_codes.append((op_code,group_lambda(function, registerGetter)))
+                op_code += step
+    return op_codes
 
 def group_lambda(function, register_getter, value=None):
     if value is None:
@@ -1022,29 +1016,29 @@
                                RegisterCallWrapper(register_getter(s)), value)
     
 def create_load_group_op_codes():
-    opCodes = []
-    opCode  = 0x40
+    op_codes = []
+    op_code  = 0x40
     for storeRegister in GROUPED_REGISTERS:
         for loadRegister in GROUPED_REGISTERS:
             if loadRegister != CPU.get_hli or storeRegister != CPU.get_hli:
-                opCodes.append((opCode, load_group_lambda(storeRegister, loadRegister)))
-            opCode += 1
-    return opCodes
+                op_codes.append((op_code, load_group_lambda(storeRegister, loadRegister)))
+            op_code += 1
+    return op_codes
             
 def load_group_lambda(store_register, load_register):
         return lambda s: CPU.ld(s, RegisterCallWrapper(load_register(s)),
                                    RegisterCallWrapper(store_register(s)))
     
 def create_register_op_codes(table):
-    opCodes = []
+    op_codes = []
     for entry in table:
-        opCode   = entry[0]
+        op_code   = entry[0]
         step     = entry[1]
         function = entry[2]
         for registerOrGetter in entry[3]:
-            opCodes.append((opCode, register_lambda(function, registerOrGetter)))
-            opCode += step
-    return opCodes
+            op_codes.append((op_code, register_lambda(function, registerOrGetter)))
+            op_code += step
+    return op_codes
 
 def register_lambda(function, registerOrGetter):
     if callable(registerOrGetter):
@@ -1066,7 +1060,7 @@
             result[pos] = entry[-1]
     return result
 
-# OPCODE TABLES ---------------------------------------------------------------
+# op_code TABLES ---------------------------------------------------------------
 # Table with one to one mapping of simple OP Codes                
 FIRST_ORDER_OP_CODES = [
     (0x00, CPU.nop),
@@ -1147,7 +1141,7 @@
 REGISTER_SET_B    = [CPU.get_bc,   CPU.get_de, CPU.get_hl,   CPU.get_af]
 FLAG_REGISTER_SET = [CPU.is_not_z, CPU.is_z,   CPU.is_not_c, CPU.is_c]
 
-# Table for Register OP Codes: (startAddress, delta, method, regsiters)
+# Table for Register OP Codes: (startAddress, delta, method, registers)
 REGISTER_OP_CODES = [ 
     (0x01, 0x10, CPU.fetch_double_register,     REGISTER_SET_A),
     (0x09, 0x10, CPU.add_hl,                    REGISTER_SET_A),
@@ -1160,7 +1154,7 @@
     (0xC1, 0x10, CPU.pop_double_register,       REGISTER_SET_B),
     (0xC5, 0x10, CPU.push_double_register,      REGISTER_SET_B)
 ]
-# Table for Second Order OPCodes: (startAddress, delta, method, [args])
+# Table for Second Order op_codes: (startAddress, delta, method, [args])
 SECOND_ORDER_REGISTER_GROUP_OP_CODES = [
     (0x00, 0x01, CPU.rotate_left_circular),    
     (0x08, 0x01, CPU.rotate_right_circular),    
@@ -1175,7 +1169,7 @@
     (0x80, 0x01, CPU.reset_bit, range(0, 8), 0x08)         
 ]
 
-# RAW OPCODE TABLE INITIALIZATION ----------------------------------------------
+# RAW op_code TABLE INITIALIZATION ----------------------------------------------
 
 FIRST_ORDER_OP_CODES  += create_register_op_codes(REGISTER_OP_CODES)
 FIRST_ORDER_OP_CODES  += create_group_op_codes(REGISTER_GROUP_OP_CODES)

Added: pypy/dist/pypy/lang/gameboy/debug/gameboy_debug_entry_point.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lang/gameboy/debug/gameboy_debug_entry_point.py	Wed Jul 30 21:21:10 2008
@@ -0,0 +1,28 @@
+from pypy.lang.gameboy.debug.gameboy_debug_implementation import *
+import py
+import sys
+
+from AppKit import NSApplication
+NSApplication.sharedApplication()
+
+ROM_PATH = str(py.magic.autopath().dirpath().dirpath())+"/rom"
+
+filename = ""
+if len(sys.argv) > 1:
+    print sys.argv
+    filename = sys.argv[1]
+else:
+    pos = str(9)
+    filename = ROM_PATH+"/rom"+pos+"/rom"+pos+".gb"
+print "loading rom: ", str(filename)
+
+gameBoy = GameBoyDebugImplementation()
+
+try:
+    gameBoy.load_cartridge_file(str(filename))
+except:
+    gameBoy.load_cartridge_file(str(filename), verify=False)
+    print "Cartridge is Corrupted!"
+    
+gameBoy.mainLoop()
+

Modified: pypy/dist/pypy/lang/gameboy/gameboy_implementation.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/gameboy_implementation.py	(original)
+++ pypy/dist/pypy/lang/gameboy/gameboy_implementation.py	Wed Jul 30 21:21:10 2008
@@ -6,12 +6,10 @@
 from pypy.lang.gameboy.sound import SoundDriver
 from pypy.lang.gameboy.timer import Clock
 from pypy.lang.gameboy import constants
-#from pypy.lang.gameboy import debug
 
 from pypy.rlib.rsdl import RSDL, RSDL_helper
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.rlib.objectmodel import specialize
-import py
 
 # GAMEBOY ----------------------------------------------------------------------
 
@@ -41,9 +39,12 @@
         finally:
             lltype.free(self.event, flavor='raw')
             RSDL.Quit()
-            #debug.print_results()
+            self.handle_execution_error()
         return 0
     
+    def handle_execution_error(self):
+        pass
+    
     def handle_events(self):
         isRunning = True
         while self.poll_event():
@@ -93,7 +94,6 @@
                 #if y%2 == 0 or True:
                 #    px = self.get_pixel_color(x, y)
                 #    str += ["#", "%", "+", " ", " "][px]
-                RSDL_helper.set_pixel(self.screen, x, y, self.pixel_map(x, y))
                 pass
         #print str;
              

Modified: pypy/dist/pypy/lang/gameboy/test/test_cpu.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/test/test_cpu.py	(original)
+++ pypy/dist/pypy/lang/gameboy/test/test_cpu.py	Wed Jul 30 21:21:10 2008
@@ -356,32 +356,37 @@
     assert_default_registers(cpu, pc=pc+1)
     
 # jr_nn
-def test_0x18():
+def test_0x18_relative_jump():
     cpu   = get_cpu();
-    pc    = cpu.pc.get()
-    value = 0x12
-    cpu.rom[constants.RESET_PC] = value
-    assert_default_registers(cpu)
-    cycle_test(cpu, 0x18, 3)
-    assert_default_registers(cpu, pc=pc+value+1)
+    for jump in range(-128,128):
+        cpu.pc.set(0x1234)
+        prepare_for_fetch(cpu, jump & 0xFF)
+        cycle_test(cpu, 0x18, 3)
+        # relative offset + one fetch
+        assert cpu.pc.get() == 0x1234 + jump + 1
+        assert_default_registers(cpu, f=cpu.f.get(), pc=0x1234+jump+1)
     
 # jr_NZ_nn see test_jr_cc_nn
-def test_0x20_0x28_0x30():
+def test_0x20_to_0x38_relative_conditional_jump():
     cpu    = get_cpu()
     flags  = [~constants.Z_FLAG, constants.Z_FLAG, ~constants.C_FLAG, constants.C_FLAG]
     opCode = 0x20
     value  = 0x12
     for i in range(0, 4):
-        prepare_for_fetch(cpu, value)
-        pc = cpu.pc.get()
-        cpu.f.set(flags[i])
-        cycle_test(cpu, opCode, 3)
-        assert cpu.pc.get() == pc+value+1
+        for jump in range(-128,128):
+            cpu.pc.set(0x1234)
+            prepare_for_fetch(cpu, jump & 0xFF)
+            cpu.f.set(flags[i])
+            cycle_test(cpu, opCode, 3)
+            # relative offset + one fetch
+            assert cpu.pc.get() == 0x1234 + jump + 1
+            assert_default_registers(cpu, f=cpu.f.get(), pc=0x1234+jump+1)
         
         pc = cpu.pc.get()
         cpu.f.set(~flags[i])
         cycle_test(cpu, opCode, 2)
         assert cpu.pc.get() == pc+1
+        assert_default_registers(cpu, f=cpu.f.get(), pc=pc+1)
         value  += 3
         opCode += 0x08
         
@@ -443,7 +448,7 @@
     assert cpu.read(cpu.de.get()) == cpu.a.get()
 
 # load_a_DEi
-def test_0x1A():
+def test_0x1A_store_memory_at_de_in_a():
     cpu     = get_cpu()
     value   = 0x12
     address = 0xC020
@@ -1056,7 +1061,7 @@
     assert_default_registers(cpu, a=value, pc=pc+2)
 
 # ld_mem_A
-def test_0xEA():
+def test_0xEA_store_a_at_fetched_address():
     cpu    = get_cpu()
     valueA = 0x56
     prepare_for_fetch(cpu, 0x12, 0x34)
@@ -1110,7 +1115,8 @@
     prepare_for_pop(cpu, value >> 8, value & 0xFF)
     prepare_for_fetch(cpu, 0x00)
     pc    = cpu.pc.get()
-    cycle_test(cpu, 0xD9, 4+2) 
+    cycle_test(cpu, 0xD9, 4+1+1) 
+    # pc: popped value + 
     assert_default_registers(cpu, pc=value+1, sp=2)
     
 def test_handle_interrupt():
@@ -1194,7 +1200,7 @@
         opCode += 0x08
 
 # ldh_Ci_A
-def test_0xE2_write_a_at_expaded_c_address():
+def test_0xE2_write_a_at_expanded_c_address():
     cpu    = get_cpu()
     value  = 0x12
     valueA = value+1
@@ -1214,14 +1220,6 @@
     cycle_test(cpu, 0xF2, 2)
     assert_default_registers(cpu, a=valueA, bc=valueC)
 
-# jp_nnnn
-def test_0xC3():
-    cpu = get_cpu()
-    prepare_for_fetch(cpu, 0x12, 0x34)
-    cycle_test(cpu, 0xC3, 4)
-    assert_default_registers(cpu, pc=0x1234)
-
-
 # di
 def test_0xF3_disable_interrupt():
     cpu = get_cpu()
@@ -1269,6 +1267,15 @@
     assert cpu.interrupt.serial.is_pending() == False
     assert cpu.interrupt.is_pending()        == False
 
+# jp_nnnn
+
+# JUMP AND CALL TESTING ========================================================
+def test_0xC3_jump():
+    cpu = get_cpu()
+    prepare_for_fetch(cpu, 0x12, 0x34)
+    cycle_test(cpu, 0xC3, 4)
+    assert_default_registers(cpu, pc=0x1234)
+
 def conditional_call_test(cpu, opCode, flagSetter):
     # set the condition to false and dont call
     flagSetter(cpu, False)
@@ -1323,30 +1330,35 @@
 def set_flag_0xDC(cpu, value):
     cpu.f.c_flag = value
 
+# call_nnnn
+def test_0xCD_call():
+    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)
+
 # push_BC to push_AF
-def test_0xC5_to_0xF5_push():
+def test_0xC5_to_0xF5_push_double_register():
     cpu        = get_cpu()
     registers  = [cpu.bc, cpu.de, cpu.hl, cpu.af]
     opCode     = 0xC5
     value      = 0x1234
     for register in registers:
+        sp = cpu.sp.get()
         register.set(value)
         cycle_test(cpu, opCode, 4)
         assert cpu.memory.read(cpu.sp.get()+1) == value >> 8
-        assert cpu.memory.read(cpu.sp.get()) == value & 0xFF
+        assert cpu.memory.read(cpu.sp.get())   == value & 0xFF
+        assert cpu.sp.get() == sp - 2
+        assert cpu.pop() == value & 0xFF
+        assert cpu.pop() == value >> 8
+        assert cpu.sp.get() == sp 
         opCode += 0x10
         value  += 0x0101
-            
-
-# call_nnnn
-def test_0xCD_call():
-    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()



More information about the Pypy-commit mailing list