[pypy-svn] r58592 - in pypy/dist/pypy/lang/gameboy: . test
cami at codespeak.net
cami at codespeak.net
Sat Oct 4 13:34:13 CEST 2008
Author: cami
Date: Sat Oct 4 13:34:12 2008
New Revision: 58592
Added:
pypy/dist/pypy/lang/gameboy/cpu_register.py
pypy/dist/pypy/lang/gameboy/test/test_cpu_register.py (props changed)
- copied unchanged from r58454, pypy/dist/pypy/lang/gameboy/test/test_register.py
Removed:
pypy/dist/pypy/lang/gameboy/test/test_register.py
Modified:
pypy/dist/pypy/lang/gameboy/cpu.py
pypy/dist/pypy/lang/gameboy/test/test_cpu_2.py
pypy/dist/pypy/lang/gameboy/video.py
pypy/dist/pypy/lang/gameboy/video_mode.py
Log:
added separate cpu_register module
renamed test_register to test_cpu_register
adapted video, test_video to the introduced separation
Modified: pypy/dist/pypy/lang/gameboy/cpu.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/cpu.py (original)
+++ pypy/dist/pypy/lang/gameboy/cpu.py Sat Oct 4 13:34:12 2008
@@ -2,6 +2,8 @@
from pypy.lang.gameboy import constants
from pypy.lang.gameboy.ram import *
from pypy.lang.gameboy.interrupt import *
+from pypy.lang.gameboy.cpu_register import Register, DoubleRegister, \
+ FlagRegister, ImmediatePseudoRegister
import pdb
# ---------------------------------------------------------------------------
@@ -13,217 +15,7 @@
#else :
# return value
return (value ^ 0x80) - 128
-# ---------------------------------------------------------------------------
-
-class AbstractRegister(object):
- def get(self, use_cycles=True):
- return 0xFF
-
-class Register(AbstractRegister):
-
- def __init__(self, cpu, value=0):
- assert isinstance(cpu, CPU)
- self.reset_value = self.value = value
- self.cpu = cpu
- if value != 0:
- self.set(value)
-
- def reset(self):
- self.value = self.reset_value
-
- def set(self, value, use_cycles=True):
- self.value = value & 0xFF
- if use_cycles:
- self.cpu.cycles -= 1
-
- def get(self, use_cycles=True):
- return self.value
-
- def add(self, value, use_cycles=True):
- self.set(self.get(use_cycles)+value, use_cycles)
-
- def sub(self, value, use_cycles=True):
- self.set(self.get(use_cycles)-value, use_cycles)
-
-#------------------------------------------------------------------------------
-
-class DoubleRegister(AbstractRegister):
-
- def __init__(self, cpu, hi, lo, reset_value=0):
- assert isinstance(cpu, CPU)
- assert isinstance(lo, Register)
- assert isinstance(hi, Register)
- self.cpu = cpu
- self.hi = hi
- self.lo = lo
- self.reset_value = reset_value
-
- def set(self, value, use_cycles=True):
- value = value & 0xFFFF
- self.set_hi(value >> 8, use_cycles)
- self.set_lo(value & 0xFF, use_cycles)
- if use_cycles:
- self.cpu.cycles += 1
-
- def set_hi_lo(self, hi, lo, use_cycles=True):
- self.set_hi(hi, use_cycles)
- self.set_lo(lo, use_cycles)
-
- def reset(self):
- self.set(self.reset_value, use_cycles=False)
-
- def set_hi(self, hi=0, use_cycles=True):
- self.hi.set(hi, use_cycles)
-
- def set_lo(self, lo=0, use_cycles=True):
- self.lo.set(lo, use_cycles)
-
- def get(self, use_cycles=True):
- return (self.hi.get(use_cycles)<<8) + self.lo.get(use_cycles)
-
- def get_hi(self, use_cycles=True):
- return self.hi.get(use_cycles)
-
- def get_lo(self, use_cycles=True):
- return self.lo.get(use_cycles)
-
- def inc(self, use_cycles=True):
- self.set(self.get(use_cycles) +1, use_cycles=use_cycles)
- if use_cycles:
- self.cpu.cycles -= 1
-
- def dec(self, use_cycles=True):
- self.set(self.get(use_cycles) - 1, use_cycles=use_cycles)
- if use_cycles:
- self.cpu.cycles -= 1
-
- def add(self, value, use_cycles=True):
- self.set(self.get(use_cycles) + value, use_cycles=use_cycles)
- if use_cycles:
- self.cpu.cycles -= 2
-
-
-# ------------------------------------------------------------------------------
-
-class ImmediatePseudoRegister(Register):
-
- def __init__(self, cpu, hl):
- assert isinstance(cpu, CPU)
- self.cpu = cpu
- self.hl = hl
-
- def set(self, value, use_cycles=True):
- self.cpu.write(self.hl.get(use_cycles=use_cycles), value) # 2 + 0
- if not use_cycles:
- self.cpu.cycles += 2
-
- def get(self, use_cycles=True):
- if not use_cycles:
- self.cpu.cycles += 1
- return self.cpu.read(self.hl.get(use_cycles=use_cycles)) # 1
-
-# ------------------------------------------------------------------------------
-
-class FlagRegister(Register):
- """
- The Flag Register (lower 8bit of AF register)
- Bit Name Set Clr Expl.
- 7 zf Z NZ Zero Flag
- 6 n - - Add/Sub-Flag (BCD)
- 5 h - - Half Carry Flag (BCD)
- 4 cy C NC Carry Flag
- 3-0 - - - Not used (always zero)
- Conatins the result from the recent instruction which has affected flags.
-
- The Zero Flag (Z)
- This bit becomes set (1) if the result of an operation has been zero (0).
- Used for conditional jumps.
-
- The Carry Flag (C, or Cy)
- Becomes set when the result of an addition became bigger than FFh (8bit) or
- FFFFh (16bit). Or when the result of a subtraction or comparision became
- less than zero (much as for Z80 and 80x86 CPUs, but unlike as for 65XX and
- ARM CPUs). Also the flag becomes set when a rotate/shift operation has
- shifted-out a "1"-bit.
- Used for conditional jumps, and for instructions such like ADC, SBC, RL,
- RLA, etc.
-
- The BCD Flags (N, H)
- These flags are (rarely) used for the DAA instruction only, N Indicates
- whether the previous instruction has been an addition or subtraction, and H
- indicates carry for lower 4bits of the result, also for DAA, the C flag must
- indicate carry for upper 8bits.
- After adding/subtracting two BCD numbers, DAA is intended to convert the
- result into BCD format; BCD numbers are ranged from 00h to 99h rather than
- 00h to FFh.
- Because C and H flags must contain carry-outs for each digit, DAA cannot be
- used for 16bit operations (which have 4 digits), or for INC/DEC operations
- (which do not affect C-flag).
- """
- def __init__(self, cpu, reset_value):
- assert isinstance(cpu, CPU)
- self.cpu = cpu
- self.reset_value = reset_value
- self.reset()
-
- def reset(self):
- self.partial_reset()
-
- def partial_reset(self, keep_is_zero=False, keep_is_subtraction=False,
- keep_is_half_carry=False, keep_is_carry=False,\
- keep_p=False, keep_s=False):
- if not keep_is_zero:
- self.is_zero = False
- if not keep_is_subtraction:
- self.is_subtraction = False
- if not keep_is_half_carry:
- self.is_half_carry = False
- if not keep_is_carry:
- self.is_carry = False
- if not keep_p:
- self.p_flag = False
- if not keep_s:
- self.s_flag = False
- self.lower = 0x00
-
- def get(self, use_cycles=True):
- value = 0
- value += (int(self.is_carry) << 4)
- value += (int(self.is_half_carry) << 5)
- value += (int(self.is_subtraction) << 6)
- value += (int(self.is_zero) << 7)
- return value + self.lower
-
- def set(self, value, use_cycles=True):
- self.is_carry = bool(value & (1 << 4))
- self.is_half_carry = bool(value & (1 << 5))
- self.is_subtraction = bool(value & (1 << 6))
- self.is_zero = bool(value & (1 << 7))
- self.lower = value & 0x0F
- if use_cycles:
- self.cpu.cycles -= 1
-
- def zero_check(self, a, reset=False):
- if reset:
- self.reset()
- if isinstance(a, (Register)):
- a = a.get()
- self.is_zero = ((a & 0xFF) == 0)
-
- def is_carry_compare(self, value, compare_and=0x01, reset=False):
- if reset:
- self.reset()
- self.is_carry = ((value & compare_and) != 0)
-
- def is_half_carry_compare(self, value, a, inverted=False):
- if inverted:
- self.is_half_carry = ((value & 0x0F) < (a & 0x0F))
- else:
- self.is_half_carry = ((value & 0x0F) > (a & 0x0F))
-
- #def is_carry_compare(self, a, b):
- # self.is_carry = (a < b)
-
+
# # ------------------------------------------------------------------------------
Added: pypy/dist/pypy/lang/gameboy/cpu_register.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lang/gameboy/cpu_register.py Sat Oct 4 13:34:12 2008
@@ -0,0 +1,212 @@
+
+# ---------------------------------------------------------------------------
+
+class AbstractRegister(object):
+ def get(self, use_cycles=True):
+ return 0xFF
+
+class Register(AbstractRegister):
+
+ def __init__(self, cpu, value=0):
+ # assert isinstance(cpu, CPU)
+ self.reset_value = self.value = value
+ self.cpu = cpu
+ if value != 0:
+ self.set(value)
+
+ def reset(self):
+ self.value = self.reset_value
+
+ def set(self, value, use_cycles=True):
+ self.value = value & 0xFF
+ if use_cycles:
+ self.cpu.cycles -= 1
+
+ def get(self, use_cycles=True):
+ return self.value
+
+ def add(self, value, use_cycles=True):
+ self.set(self.get(use_cycles)+value, use_cycles)
+
+ def sub(self, value, use_cycles=True):
+ self.set(self.get(use_cycles)-value, use_cycles)
+
+#------------------------------------------------------------------------------
+
+class DoubleRegister(AbstractRegister):
+
+ def __init__(self, cpu, hi, lo, reset_value=0):
+ #assert isinstance(cpu, CPU)
+ #assert isinstance(lo, Register)
+ #assert isinstance(hi, Register)
+ self.cpu = cpu
+ self.hi = hi
+ self.lo = lo
+ self.reset_value = reset_value
+
+ def set(self, value, use_cycles=True):
+ value = value & 0xFFFF
+ self.set_hi(value >> 8, use_cycles)
+ self.set_lo(value & 0xFF, use_cycles)
+ if use_cycles:
+ self.cpu.cycles += 1
+
+ def set_hi_lo(self, hi, lo, use_cycles=True):
+ self.set_hi(hi, use_cycles)
+ self.set_lo(lo, use_cycles)
+
+ def reset(self):
+ self.set(self.reset_value, use_cycles=False)
+
+ def set_hi(self, hi=0, use_cycles=True):
+ self.hi.set(hi, use_cycles)
+
+ def set_lo(self, lo=0, use_cycles=True):
+ self.lo.set(lo, use_cycles)
+
+ def get(self, use_cycles=True):
+ return (self.hi.get(use_cycles)<<8) + self.lo.get(use_cycles)
+
+ def get_hi(self, use_cycles=True):
+ return self.hi.get(use_cycles)
+
+ def get_lo(self, use_cycles=True):
+ return self.lo.get(use_cycles)
+
+ def inc(self, use_cycles=True):
+ self.set(self.get(use_cycles) +1, use_cycles=use_cycles)
+ if use_cycles:
+ self.cpu.cycles -= 1
+
+ def dec(self, use_cycles=True):
+ self.set(self.get(use_cycles) - 1, use_cycles=use_cycles)
+ if use_cycles:
+ self.cpu.cycles -= 1
+
+ def add(self, value, use_cycles=True):
+ self.set(self.get(use_cycles) + value, use_cycles=use_cycles)
+ if use_cycles:
+ self.cpu.cycles -= 2
+
+
+# ------------------------------------------------------------------------------
+
+class ImmediatePseudoRegister(Register):
+
+ def __init__(self, cpu, hl):
+ #assert isinstance(cpu, CPU)
+ self.cpu = cpu
+ self.hl = hl
+
+ def set(self, value, use_cycles=True):
+ self.cpu.write(self.hl.get(use_cycles=use_cycles), value) # 2 + 0
+ if not use_cycles:
+ self.cpu.cycles += 2
+
+ def get(self, use_cycles=True):
+ if not use_cycles:
+ self.cpu.cycles += 1
+ return self.cpu.read(self.hl.get(use_cycles=use_cycles)) # 1
+
+# ------------------------------------------------------------------------------
+
+class FlagRegister(Register):
+ """
+ The Flag Register (lower 8bit of AF register)
+ Bit Name Set Clr Expl.
+ 7 zf Z NZ Zero Flag
+ 6 n - - Add/Sub-Flag (BCD)
+ 5 h - - Half Carry Flag (BCD)
+ 4 cy C NC Carry Flag
+ 3-0 - - - Not used (always zero)
+ Contains the result from the recent instruction which has affected flags.
+
+ The Zero Flag (Z)
+ This bit becomes set (1) if the result of an operation has been zero (0).
+ Used for conditional jumps.
+
+ The Carry Flag (C, or Cy)
+ Becomes set when the result of an addition became bigger than FFh (8bit) or
+ FFFFh (16bit). Or when the result of a subtraction or comparision became
+ less than zero (much as for Z80 and 80x86 CPUs, but unlike as for 65XX and
+ ARM CPUs). Also the flag becomes set when a rotate/shift operation has
+ shifted-out a "1"-bit.
+ Used for conditional jumps, and for instructions such like ADC, SBC, RL,
+ RLA, etc.
+
+ The BCD Flags (N, H)
+ These flags are (rarely) used for the DAA instruction only, N Indicates
+ whether the previous instruction has been an addition or subtraction, and H
+ indicates carry for lower 4bits of the result, also for DAA, the C flag must
+ indicate carry for upper 8bits.
+ After adding/subtracting two BCD numbers, DAA is intended to convert the
+ result into BCD format; BCD numbers are ranged from 00h to 99h rather than
+ 00h to FFh.
+ Because C and H flags must contain carry-outs for each digit, DAA cannot be
+ used for 16bit operations (which have 4 digits), or for INC/DEC operations
+ (which do not affect C-flag).
+ """
+ def __init__(self, cpu, reset_value):
+ #assert isinstance(cpu, CPU)
+ self.cpu = cpu
+ self.reset_value = reset_value
+ self.reset()
+
+ def reset(self):
+ self.partial_reset()
+
+ def partial_reset(self, keep_is_zero=False, keep_is_subtraction=False,
+ keep_is_half_carry=False, keep_is_carry=False,\
+ keep_p=False, keep_s=False):
+ if not keep_is_zero:
+ self.is_zero = False
+ if not keep_is_subtraction:
+ self.is_subtraction = False
+ if not keep_is_half_carry:
+ self.is_half_carry = False
+ if not keep_is_carry:
+ self.is_carry = False
+ if not keep_p:
+ self.p_flag = False
+ if not keep_s:
+ self.s_flag = False
+ self.lower = 0x00
+
+ def get(self, use_cycles=True):
+ value = 0
+ value += (int(self.is_carry) << 4)
+ value += (int(self.is_half_carry) << 5)
+ value += (int(self.is_subtraction) << 6)
+ value += (int(self.is_zero) << 7)
+ return value + self.lower
+
+ def set(self, value, use_cycles=True):
+ self.is_carry = bool(value & (1 << 4))
+ self.is_half_carry = bool(value & (1 << 5))
+ self.is_subtraction = bool(value & (1 << 6))
+ self.is_zero = bool(value & (1 << 7))
+ self.lower = value & 0x0F
+ if use_cycles:
+ self.cpu.cycles -= 1
+
+ def zero_check(self, a, reset=False):
+ if reset:
+ self.reset()
+ if isinstance(a, (Register)):
+ a = a.get()
+ self.is_zero = ((a & 0xFF) == 0)
+
+ def is_carry_compare(self, value, compare_and=0x01, reset=False):
+ if reset:
+ self.reset()
+ self.is_carry = ((value & compare_and) != 0)
+
+ def is_half_carry_compare(self, value, a, inverted=False):
+ if inverted:
+ self.is_half_carry = ((value & 0x0F) < (a & 0x0F))
+ else:
+ self.is_half_carry = ((value & 0x0F) > (a & 0x0F))
+
+ #def is_carry_compare(self, a, b):
+ # self.is_carry = (a < b)
+
\ No newline at end of file
Modified: pypy/dist/pypy/lang/gameboy/test/test_cpu_2.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/test/test_cpu_2.py (original)
+++ pypy/dist/pypy/lang/gameboy/test/test_cpu_2.py Sat Oct 4 13:34:12 2008
@@ -30,6 +30,8 @@
TEST_CPU.reset()
return TEST_CPU
+# -----------------------------------------------------------------------------
+
def assert_default_registers(cpu, a=constants.RESET_A, bc=constants.RESET_BC,\
de=constants.RESET_DE, f=constants.RESET_F,\
hl=constants.RESET_HL, sp=constants.RESET_SP,\
@@ -38,19 +40,26 @@
def assert_registers(cpu, a=None, bc=None, de=None, f=None, hl=None, sp=None, pc=None):
if a is not None:
- assert cpu.a.get() == a, "Register a is %s but should be %s" % (hex(cpu.a.get()), hex(a))
+ assert cpu.a.get() == a, \
+ "Register a is %s but should be %s" % (hex(cpu.a.get()), hex(a))
if bc is not None:
- assert cpu.bc.get() == bc, "Register bc is %s but should be %s" % (hex(cpu.bc.get()), hex(bc))
+ assert cpu.bc.get() == bc, \
+ "Register bc is %s but should be %s" % (hex(cpu.bc.get()), hex(bc))
if de is not None:
- assert cpu.de.get() == de, "Register de is %s but should be %s" % (hex(cpu.de.get()),hex(de))
+ assert cpu.de.get() == de, \
+ "Register de is %s but should be %s" % (hex(cpu.de.get()),hex(de))
if f is not None:
- assert cpu.flag.get() == f, "Register f is %s but should be %s" % (hex(cpu.flag.get()),hex(f))
+ assert cpu.flag.get() == f, \
+ "Register f is %s but should be %s" % (hex(cpu.flag.get()),hex(f))
if hl is not None:
- assert cpu.hl.get() == hl, "Register hl is %s but should be %s" % (hex(cpu.hl.get()), hex(hl))
+ assert cpu.hl.get() == hl, \
+ "Register hl is %s but should be %s" % (hex(cpu.hl.get()), hex(hl))
if sp is not None:
- assert cpu.sp.get() == sp, "Register sp is %s but should be %s" % (hex(cpu.sp.get()), hex(sp))
+ assert cpu.sp.get() == sp, \
+ "Register sp is %s but should be %s" % (hex(cpu.sp.get()), hex(sp))
if pc is not None:
- assert cpu.pc.get() == pc, "Register pc is %s but should be %s" % (hex(cpu.pc.get()), hex(pc))
+ assert cpu.pc.get() == pc, \
+ "Register pc is %s but should be %s" % (hex(cpu.pc.get()), hex(pc))
def assert_defaults(cpu, z=True, n=False, h=False, c=False, p=False, s=False):
@@ -58,17 +67,23 @@
def assert_flags(cpu, z=None, n=None, h=None, c=None, p=None, s=None):
if z is not None:
- assert cpu.flag.is_zero == z, "Z-Flag is %s but should be %s" % (cpu.flag.is_zero, z)
+ assert cpu.flag.is_zero == z, \
+ "Z-Flag is %s but should be %s" % (cpu.flag.is_zero, z)
if n is not None:
- assert cpu.flag.is_subtraction == n, "N-Flag is %s but should be %s" % (cpu.flag.is_subtraction, n)
+ assert cpu.flag.is_subtraction == n, \
+ "N-Flag is %s but should be %s" % (cpu.flag.is_subtraction, n)
if h is not None:
- assert cpu.flag.is_half_carry == h, "H-Flag is %s but should be %s" % (cpu.flag.is_half_carry, h)
+ assert cpu.flag.is_half_carry == h, \
+ "H-Flag is %s but should be %s" % (cpu.flag.is_half_carry, h)
if c is not None:
- assert cpu.flag.is_carry == c, "C-Flag is %s but should be %s" % (cpu.flag.is_carry, c)
+ assert cpu.flag.is_carry == c, \
+ "C-Flag is %s but should be %s" % (cpu.flag.is_carry, c)
if p is not None:
- assert cpu.flag.p_flag == p, "P-Flag is %s but should be %s" % (cpu.flag.p_flag, p)
+ assert cpu.flag.p_flag == p, \
+ "P-Flag is %s but should be %s" % (cpu.flag.p_flag, p)
if s is not None:
- assert cpu.flag.s_flag == s, "S-Flag is %s but should be %s" % (cpu.flag.s_flag, s)
+ assert cpu.flag.s_flag == s, \
+ "S-Flag is %s but should be %s" % (cpu.flag.s_flag, s)
def prepare_for_double_fetch(cpu, value):
prepare_for_fetch(cpu, (value & 0xFF00) >> 8, value & 0x00FF)
Modified: pypy/dist/pypy/lang/gameboy/video.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/video.py (original)
+++ pypy/dist/pypy/lang/gameboy/video.py Sat Oct 4 13:34:12 2008
@@ -461,6 +461,7 @@
if self.window.enabled:
self.window.draw_line(self.line_y)
if self.control.sprites_enabled:
+ #self.draw_sprites_line_new()
self.draw_sprites_line()
self.draw_pixels_line()
@@ -499,6 +500,9 @@
"""
return sprites.sort(key=operator.itemgetter("x"))
+
+ # -----------------------------------------------
+
def draw_sprites_line(self):
count = self.scan_sprites()
lastx = 176
Modified: pypy/dist/pypy/lang/gameboy/video_mode.py
==============================================================================
--- pypy/dist/pypy/lang/gameboy/video_mode.py (original)
+++ pypy/dist/pypy/lang/gameboy/video_mode.py Sat Oct 4 13:34:12 2008
@@ -30,7 +30,7 @@
def id(self):
raise Exception("unimplemented method")
- def activate(self, previous_mode):
+ def activate(self):
raise Exception("unimplemented method")
def emulate(self):
More information about the Pypy-commit
mailing list