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

cami at codespeak.net cami at codespeak.net
Thu May 29 16:59:02 CEST 2008


Author: cami
Date: Thu May 29 16:59:00 2008
New Revision: 55398

Modified:
   pypy/dist/pypy/lang/gameboy/cartridge.py
   pypy/dist/pypy/lang/gameboy/gameboy.py
   pypy/dist/pypy/lang/gameboy/gameboyImplementation.py
   pypy/dist/pypy/lang/gameboy/interrupt.py
   pypy/dist/pypy/lang/gameboy/joypad.py
   pypy/dist/pypy/lang/gameboy/ram.py
   pypy/dist/pypy/lang/gameboy/serial.py
   pypy/dist/pypy/lang/gameboy/test/test_ram.py
   pypy/dist/pypy/lang/gameboy/test/test_serial.py
   pypy/dist/pypy/lang/gameboy/video.py
Log:
minor code refactoring, mostly updating comments
adapted tests to the refactoring
activated nintendo startup


Modified: pypy/dist/pypy/lang/gameboy/cartridge.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/cartridge.py	(original)
+++ pypy/dist/pypy/lang/gameboy/cartridge.py	Thu May 29 16:59:00 2008
@@ -69,9 +69,9 @@
     
     def __init__(self, clock):
         assert isinstance(clock, Clock)
-        self.clock = clock
+        self.clock     = clock
         self.cartridge = None
-        self.mbc = None
+        self.mbc       = None
         
     def reset(self):
         if not self.has_battery():
@@ -592,31 +592,32 @@
 
     def update_clock(self):
         now = self.clock.get_time()
-        if (self.clock_control & 0x40) == 0:
-            elapsed = now - self.clock_time
-            elapsed += self.clock_days * 24*60*60
-            elapsed += self.clock_hours * 60*60
-            elapsed += self.clock_minutes * 60
-            elapsed += self.clock_seconds
-            
-            days = int(math.floor(elapsed / (24.0*60*60.0)))
-            self.clock_days += days
-            elapsed -= days * 24*60*60
-
-            hours = int(math.floor(elapsed / (60*60)))
-            self.clock_hours += hours
-            elapsed -= hours * 60*60
-            
-            minutes = int(math.floor(elapsed / 60))
-            self.clock_minutes += minutes
-            elapsed -= minutes * 60
-            
-            self.clock_seconds += elapsed
-            
-            if self.clock_days >= 512:
-                self.clock_days %= 512
-                self.clock_control |= 0x80
+        elapsed = now - self.clock_time
         self.clock_time = now
+        if (self.clock_control & 0x40) != 0:
+            return
+        elapsed += self.clock_days * 24*60*60
+        elapsed += self.clock_hours * 60*60
+        elapsed += self.clock_minutes * 60
+        elapsed += self.clock_seconds
+        
+        days = int(math.floor(elapsed / (24.0*60*60.0)))
+        self.clock_days += days
+        elapsed -= days * 24*60*60
+
+        hours = int(math.floor(elapsed / (60*60)))
+        self.clock_hours += hours
+        elapsed -= hours * 60*60
+        
+        minutes = int(math.floor(elapsed / 60))
+        self.clock_minutes += minutes
+        elapsed -= minutes * 60
+        
+        self.clock_seconds += elapsed
+        
+        if self.clock_days >= 512:
+            self.clock_days %= 512
+            self.clock_control |= 0x80
 
 
 #-------------------------------------------------------------------------------

Modified: pypy/dist/pypy/lang/gameboy/gameboy.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/gameboy.py	(original)
+++ pypy/dist/pypy/lang/gameboy/gameboy.py	Thu May 29 16:59:00 2008
@@ -47,6 +47,7 @@
     def load_cartridge(self, cartridge, verify=True):
         self.cartridge_manager.load(cartridge, verify)
         self.cpu.set_rom(self.cartridge_manager.get_rom())
+        self.memory_bank_controller = self.cartridge_manager.get_memory_bank()
         
     def load_cartridge_file(self, path, verify=True):
         self.load_cartridge(CartridgeFile(path), verify)
@@ -58,7 +59,7 @@
         self.video.set_frame_skip(frameSkip)
 
     def save(self, cartridgeName):
-        self.cartridge.save(cartridgeName)
+        self.cartridge_manager.save(cartridgeName)
 
     def start(self):
         self.sound.start()
@@ -68,7 +69,7 @@
 
     def reset(self):
         self.ram.reset()
-        self.cartridge.reset()
+        self.memory_bank_controller.reset()
         self.interrupt.reset()
         self.cpu.reset()
         self.serial.reset()
@@ -180,23 +181,23 @@
 
     def draw_logo(self):
         for index in range(0, 48):
-            bits = self.cartridge.read(0x0104 + index)
-            pattern0 = ((bits >> 0) & 0x80) + ((bits >> 1) & 0x60)\
-                     + ((bits >> 2) & 0x18) + ((bits >> 3) & 0x06)\
-                     + ((bits >> 4) & 0x01)
-
-            pattern1 = ((bits << 4) & 0x80) + ((bits << 3) & 0x60)\
-                     + ((bits << 2) & 0x18) + ((bits << 1) & 0x06)\
-                     + ((bits << 0) & 0x01)
+            bits = self.memory_bank_controller.read(0x0104 + index)
+            pattern0 = ((bits >> 0) & 0x80) + ((bits >> 1) & 0x60) + \
+                       ((bits >> 2) & 0x18) + ((bits >> 3) & 0x06) + \
+                       ((bits >> 4) & 0x01)
+
+            pattern1 = ((bits << 4) & 0x80) + ((bits << 3) & 0x60) + \
+                       ((bits << 2) & 0x18) + ((bits << 1) & 0x06) + \
+                       ((bits << 0) & 0x01)
 
             self.video.write(0x8010 + (index << 3), pattern0)
             self.video.write(0x8012 + (index << 3), pattern0)
-
             self.video.write(0x8014 + (index << 3), pattern1)
             self.video.write(0x8016 + (index << 3), pattern1)
 
         for index in range(0, 8):
-            self.video.write(0x8190 + (index << 1), constants.REGISTERED_BITMAP[index])
+            self.video.write(0x8190 + (index << 1), \
+                             constants.REGISTERED_BITMAP[index])
 
         for tile in range(0, 12):
             self.video.write(0x9904 + tile, tile + 1)

Modified: pypy/dist/pypy/lang/gameboy/gameboyImplementation.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/gameboyImplementation.py	(original)
+++ pypy/dist/pypy/lang/gameboy/gameboyImplementation.py	Thu May 29 16:59:00 2008
@@ -32,6 +32,7 @@
    
     
     def mainLoop(self):
+        self.reset()
         try:
             while True:
                 if self.poll_event():

Modified: pypy/dist/pypy/lang/gameboy/interrupt.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/interrupt.py	(original)
+++ pypy/dist/pypy/lang/gameboy/interrupt.py	Thu May 29 16:59:00 2008
@@ -5,8 +5,8 @@
 class InterruptFlag(object):
     
     def __init__(self, _reset, mask, call_code):
-        self._reset = _reset
-        self.mask = mask
+        self._reset    = _reset
+        self.mask      = mask
         self.call_code = call_code
         self.reset()
         
@@ -34,18 +34,15 @@
         self.reset()
         
     def create_interrupt_flags(self):
-        self.vblank = InterruptFlag(True, constants.VBLANK, 0x40)
+        self.vblank  = InterruptFlag(True, constants.VBLANK, 0x40)
         self.lcd     = InterruptFlag(False, constants.LCD, 0x48)
         self.timer   = InterruptFlag(False, constants.TIMER, 0x50)
         self.serial  = InterruptFlag(False, constants.SERIAL, 0x58)
         self.joypad  = InterruptFlag(False, constants.JOYPAD, 0x60)
         
     def create_flag_list(self):
-        self.interrupt_flags = [
-            self.vblank, self.lcd, 
-            self.timer, self.serial,
-            self.joypad
-        ]
+        self.interrupt_flags = [ self.vblank, self.lcd, self.timer, self.serial,
+                                 self.joypad]
 
     def create_flag_mask_mapping(self):
         self.mask_mapping = {}

Modified: pypy/dist/pypy/lang/gameboy/joypad.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/joypad.py	(original)
+++ pypy/dist/pypy/lang/gameboy/joypad.py	Thu May 29 16:59:00 2008
@@ -26,7 +26,6 @@
         return self.cycles
 
     def emulate(self, ticks):
-        ticks = int(ticks)
         self.cycles -= ticks
         if self.cycles <= 0:
             if self.driver.is_raised():
@@ -34,13 +33,11 @@
             self.cycles = constants.JOYPAD_CLOCK
 
     def write(self, address, data):
-        address = int(address)
         if address == constants.JOYP:
             self.joyp = (self.joyp & 0xC) + (data & 0x3)
             self.update()
 
     def read(self, address):
-        address = int(address)
         if address == constants.JOYP:
             return (self.joyp << 4) + self.button_code
         return 0xFF

Modified: pypy/dist/pypy/lang/gameboy/ram.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/ram.py	(original)
+++ pypy/dist/pypy/lang/gameboy/ram.py	Thu May 29 16:59:00 2008
@@ -18,35 +18,32 @@
 
     def __init__(self):
         # Work RAM
-        self.w_ram =  [0]*8192
+        self.work_ram =  [0]*8192
         # High RAM
-        self.h_ram =  [0]*128
+        self.hi_ram =  [0]*128
         self.reset()
 
     def reset(self):
         # Work RAM
-        self.w_ram =  [0]*8192
+        self.work_ram =  [0]*8192
         # High RAM
-        self.h_ram =  [0]*128
+        self.hi_ram =  [0]*128
 
     def write(self, address, data):
-        address = int(address)
-        data = int(data)
         # C000-DFFF Work RAM (8KB)
         # E000-FDFF Echo RAM
         if address >= 0xC000 and address <= 0xFDFF:
-            self.w_ram[address & 0x1FFF] = data
+            self.work_ram[address & 0x1FFF] = data
         # FF80-FFFE High RAM
         elif address >= 0xFF80 and address <= 0xFFFE:
-            self.h_ram[address & 0x7F] = data
+            self.hi_ram[address & 0x7F] = data
 
     def read(self, address):
-        address = int(address)
         # C000-DFFF Work RAM
         # E000-FDFF Echo RAM
         if address >= 0xC000 and address <= 0xFDFF:
-            return self.w_ram[address & 0x1FFF] & 0xFF
+            return self.work_ram[address & 0x1FFF] & 0xFF
         # FF80-FFFE High RAM
         elif address >= 0xFF80 and address <= 0xFFFE:
-            return self.h_ram[address & 0x7F] & 0xFF
+            return self.hi_ram[address & 0x7F] & 0xFF
         raise Exception("Invalid Memory access, address out of range")
\ No newline at end of file

Modified: pypy/dist/pypy/lang/gameboy/serial.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/serial.py	(original)
+++ pypy/dist/pypy/lang/gameboy/serial.py	Thu May 29 16:59:00 2008
@@ -16,36 +16,35 @@
 
     def reset(self):
         self.cycles = int(constants.SERIAL_CLOCK)
-        self.sb = 0x00
-        self.sc = 0x00
+        self.serial_data = 0x00
+        self.serial_control = 0x00
 
     def get_cycles(self):
         return self.cycles
 
     def emulate(self, ticks):
-        ticks = int(ticks)
-        if (self.sc & 0x81) != 0x81:
+        if (self.serial_control & 0x81) != 0x81:
             return
         self.cycles -= ticks
         if self.cycles <= 0:
-            self.sb = 0xFF
-            self.sc &= 0x7F
+            self.serial_data = 0xFF
+            self.serial_control &= 0x7F
             self.cycles = constants.SERIAL_IDLE_CLOCK
             self.interrupt.raise_interrupt(constants.SERIAL)
 
     def set_serial_data(self, data):
-        self.sb = data
+        self.serial_data = data
 
     def set_serial_control(self, data):
-        self.sc = data
-        # HACK: delay the serial interrupt (Shin Nihon Pro Wrestling)
+        self.serial_control = data
+        # HACK: delay the serial interrupt
         self.cycles = constants.SERIAL_IDLE_CLOCK + constants.SERIAL_CLOCK
 
     def get_serial_data(self):
-        return self.sb
+        return self.serial_data
 
     def get_serial_control(self):
-        return self.sc
+        return self.serial_control
 
     def write(self, address, data):
         address = int(address)

Modified: pypy/dist/pypy/lang/gameboy/test/test_ram.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/test/test_ram.py	(original)
+++ pypy/dist/pypy/lang/gameboy/test/test_ram.py	Thu May 29 16:59:00 2008
@@ -17,36 +17,36 @@
     except Exception:
         pass
         
-    assert value not in ram.w_ram
-    assert value not in ram.h_ram
+    assert value not in ram.work_ram
+    assert value not in ram.hi_ram
     
     address = 0xC000
     ram.write(address, value)
     assert ram.read(address) == value
-    assert value in  ram.w_ram
-    assert value not in  ram.h_ram
+    assert value in  ram.work_ram
+    assert value not in  ram.hi_ram
     
     address = 0xFDFF
     value += 1
     ram.write(address, value)
     assert ram.read(address) == value
-    assert value in  ram.w_ram
-    assert value not in  ram.h_ram
+    assert value in  ram.work_ram
+    assert value not in  ram.hi_ram
     
     
     address = 0xFF80
     value += 1
     ram.write(address, value)
     assert ram.read(address) == value
-    assert value in  ram.h_ram
-    assert value not in  ram.w_ram
+    assert value in  ram.hi_ram
+    assert value not in  ram.work_ram
     
     address = 0xFFFE
     value += 1
     ram.write(address, value)
     assert ram.read(address) == value
-    assert value in  ram.h_ram
-    assert value not in  ram.w_ram
+    assert value in  ram.hi_ram
+    assert value not in  ram.work_ram
     
     address += 1
     value += 1
@@ -56,5 +56,5 @@
         py.test.fail()
     except Exception:
         pass
-    assert value not in  ram.h_ram
-    assert value not in  ram.w_ram
\ No newline at end of file
+    assert value not in  ram.hi_ram
+    assert value not in  ram.work_ram
\ No newline at end of file

Modified: pypy/dist/pypy/lang/gameboy/test/test_serial.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/test/test_serial.py	(original)
+++ pypy/dist/pypy/lang/gameboy/test/test_serial.py	Thu May 29 16:59:00 2008
@@ -9,13 +9,13 @@
 
 def test_reset():
     serial = get_serial()
-    serial.cycles = 12
-    serial.sb = 12
-    serial.sc = 12
+    serial.cycles         = 12
+    serial.serial_data    = 12
+    serial.serial_control = 12
     serial.reset()
-    assert serial.cycles == constants.SERIAL_CLOCK
-    assert serial.sb == 0
-    assert serial.sc == 0
+    assert serial.cycles         == constants.SERIAL_CLOCK
+    assert serial.serial_data    == 0
+    assert serial.serial_control == 0
     
     
 def test_set_serial_control():
@@ -28,30 +28,30 @@
     
 def test_emulate():
     serial = get_serial()
-    serial.sc = 0x00
+    serial.serial_control = 0x00
     serial.emulate(20)
-    assert serial.cycles == constants.SERIAL_CLOCK
-    assert serial.sb == 0
-    assert serial.sc == 0
+    assert serial.cycles         == constants.SERIAL_CLOCK
+    assert serial.serial_data    == 0
+    assert serial.serial_control == 0
     
     serial.reset()
-    serial.sc = 0x81
+    serial.serial_control = 0x81
     serial.cycles = 10
     cycles = serial.cycles
     serial.emulate(2)
     assert serial.cycles > 0
-    assert cycles-serial.cycles == 2
-    assert serial.sb == 0
-    assert serial.sc == 0x81
+    assert cycles-serial.cycles  == 2
+    assert serial.serial_data    == 0
+    assert serial.serial_control == 0x81
     assert serial.interrupt.serial.is_pending() == False
     
     serial.reset()
-    serial.sc = 0x81
+    serial.serial_control = 0x81
     serial.cycles = 0
     serial.emulate(2)
-    assert serial.sb == 0xFF
-    assert serial.sc == 0x81 & 0x7F
-    assert serial.cycles == constants.SERIAL_IDLE_CLOCK
+    assert serial.serial_data    == 0xFF
+    assert serial.serial_control == 0x81 & 0x7F
+    assert serial.cycles         == constants.SERIAL_IDLE_CLOCK
     assert serial.interrupt.serial.is_pending() == True
     
     
@@ -60,12 +60,12 @@
     value = 0x12
     serial.write(constants.SB, value)
     assert serial.read(constants.SB) == value
-    assert serial.sb == value 
+    assert serial.serial_data        == value 
     
     value += 1
     serial.write(constants.SC, value)
     assert serial.read(constants.SC) == value
-    assert serial.sc == value
+    assert serial.serial_control     == value
     
     assert serial.read(0) == 0xFF
     
\ No newline at end of file

Modified: pypy/dist/pypy/lang/gameboy/video.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/video.py	(original)
+++ pypy/dist/pypy/lang/gameboy/video.py	Thu May 29 16:59:00 2008
@@ -43,7 +43,7 @@
         self.h_blank_interrupt  = False
         self.oam_interrupt      = False
         self.h_blank_interrupt  = False
-        self.vblank_interrupt  = False
+        self.vblank_interrupt   = False
         #Coincidence Flag  (0:LYC<>LY, 1:LYC=LY)
         self.coincidence_flag   = False
         
@@ -68,7 +68,7 @@
     #frames = 0
     #frame_skip = 0
 
-     # Line Buffer, constants.OAM Cache and Color Palette
+     # Line Buffer, OAM Cache and Color Palette
     #line = []#= new int[8 + 160 + 8]
     #objects = []#= new int[OBJECTS_PER_LINE]
     #palette = []#= new int[1024]
@@ -88,7 +88,7 @@
 
     def reset(self):
         self.cycles     = constants.MODE_2_TICKS
-        # used for enabled or disablind window or background
+        # used for enabled or disabled window or background
         # Bit 7 - LCD Display Enable             (0=Off, 1=On)
         # Bit 6 - Window Tile Map Display Select (0=9800-9BFF, 1=9C00-9FFF)
         # Bit 5 - Window Display Enable          (0=Off, 1=On)
@@ -367,7 +367,7 @@
             
     def emulate_hblank_line_y_compare(self):
         if self.line_y == self.line_y_compare:
-            # constants.LYC=LY interrupt
+            # LYC=LY interrupt
             self.stat |= 0x04
             if (self.stat & 0x40) != 0:
                 self.interrupt.raise_interrupt(constants.LCD)
@@ -377,7 +377,7 @@
     def emulate_hblank_part_1(self):
         self.stat = (self.stat & 0xFC) | 0x02
         self.cycles += constants.MODE_2_TICKS
-        # constants.OAM interrupt
+        # OAM interrupt
         if (self.stat & 0x20) != 0 and (self.stat & 0x44) != 0x44:
             self.interrupt.raise_interrupt(constants.LCD)
         
@@ -390,7 +390,6 @@
             self.frames = 0
         else:
             self.display = False
-
         self.stat    = (self.stat & 0xFC) | 0x01
         self.cycles += constants.MODE_1_BEGIN_TICKS
         self.vblank  = True
@@ -416,7 +415,7 @@
     def emulate_vblank_first_y_line(self):
         self.stat = (self.stat & 0xFC) | 0x02
         self.cycles += constants.MODE_2_TICKS
-        # constants.OAM interrupt
+        #OAM interrupt
         if (self.stat & 0x20) != 0 and (self.stat & 0x44) != 0x44:
             self.interrupt.raise_interrupt(constants.LCD)
             
@@ -433,7 +432,7 @@
             self.stat = (self.stat & 0xFC) | 0x01
             self.cycles += constants.MODE_1_TICKS - constants.MODE_1_END_TICKS
         if self.line_y == self.line_y_compare:
-            # constants.LYC=LY interrupt
+            #LYC=LY interrupt
             self.stat |= 0x04
             if (self.stat & 0x40) != 0:
                 self.interrupt.raise_interrupt(constants.LCD)
@@ -642,10 +641,10 @@
             #color
             if (pattern & 0x22) == 0 or ((pattern & 0x08) != 0 and \
                (pattern & 0x11) != 0):
-                # constants.OBJ behind constants.BG color 1-3
+                # OBJ behind BG color 1-3
                 color = (self.background_palette >> ((((pattern >> 3) & 0x02) +\
                         (pattern & 0x01)) << 1)) & 0x03
-             # constants.OBJ above constants.BG
+             # OBJ above BG
             elif ((pattern & 0x04) == 0):
                 color = (self.object_palette_0 >> ((((pattern >> 4) & 0x02) + \
                         ((pattern >> 1) & 0x01)) << 1)) & 0x03



More information about the Pypy-commit mailing list