base.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import struct
  2. ascii_glyphs = [
  3. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x18, 0x00,
  4. 0x6c, 0x6c, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x36, 0x7f, 0x36, 0x7f, 0x36, 0x36, 0x00,
  5. 0x0c, 0x3f, 0x68, 0x3e, 0x0b, 0x7e, 0x18, 0x00, 0x60, 0x66, 0x0c, 0x18, 0x30, 0x66, 0x06, 0x00,
  6. 0x38, 0x6c, 0x6c, 0x38, 0x6d, 0x66, 0x3b, 0x00, 0x0c, 0x18, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
  7. 0x0c, 0x18, 0x30, 0x30, 0x30, 0x18, 0x0c, 0x00, 0x30, 0x18, 0x0c, 0x0c, 0x0c, 0x18, 0x30, 0x00,
  8. 0x00, 0x18, 0x7e, 0x3c, 0x7e, 0x18, 0x00, 0x00, 0x00, 0x18, 0x18, 0x7e, 0x18, 0x18, 0x00, 0x00,
  9. 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00,
  10. 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x06, 0x0c, 0x18, 0x30, 0x60, 0x00, 0x00,
  11. 0x3c, 0x66, 0x6e, 0x7e, 0x76, 0x66, 0x3c, 0x00, 0x18, 0x38, 0x18, 0x18, 0x18, 0x18, 0x7e, 0x00,
  12. 0x3c, 0x66, 0x06, 0x0c, 0x18, 0x30, 0x7e, 0x00, 0x3c, 0x66, 0x06, 0x1c, 0x06, 0x66, 0x3c, 0x00,
  13. 0x0c, 0x1c, 0x3c, 0x6c, 0x7e, 0x0c, 0x0c, 0x00, 0x7e, 0x60, 0x7c, 0x06, 0x06, 0x66, 0x3c, 0x00,
  14. 0x1c, 0x30, 0x60, 0x7c, 0x66, 0x66, 0x3c, 0x00, 0x7e, 0x06, 0x0c, 0x18, 0x30, 0x30, 0x30, 0x00,
  15. 0x3c, 0x66, 0x66, 0x3c, 0x66, 0x66, 0x3c, 0x00, 0x3c, 0x66, 0x66, 0x3e, 0x06, 0x0c, 0x38, 0x00,
  16. 0x00, 0x00, 0x18, 0x18, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x18, 0x18, 0x30,
  17. 0x0c, 0x18, 0x30, 0x60, 0x30, 0x18, 0x0c, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x7e, 0x00, 0x00, 0x00,
  18. 0x30, 0x18, 0x0c, 0x06, 0x0c, 0x18, 0x30, 0x00, 0x3c, 0x66, 0x0c, 0x18, 0x18, 0x00, 0x18, 0x00,
  19. 0x3c, 0x66, 0x6e, 0x6a, 0x6e, 0x60, 0x3c, 0x00, 0x3c, 0x66, 0x66, 0x7e, 0x66, 0x66, 0x66, 0x00,
  20. 0x7c, 0x66, 0x66, 0x7c, 0x66, 0x66, 0x7c, 0x00, 0x3c, 0x66, 0x60, 0x60, 0x60, 0x66, 0x3c, 0x00,
  21. 0x78, 0x6c, 0x66, 0x66, 0x66, 0x6c, 0x78, 0x00, 0x7e, 0x60, 0x60, 0x7c, 0x60, 0x60, 0x7e, 0x00,
  22. 0x7e, 0x60, 0x60, 0x7c, 0x60, 0x60, 0x60, 0x00, 0x3c, 0x66, 0x60, 0x6e, 0x66, 0x66, 0x3c, 0x00,
  23. 0x66, 0x66, 0x66, 0x7e, 0x66, 0x66, 0x66, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7e, 0x00,
  24. 0x3e, 0x0c, 0x0c, 0x0c, 0x0c, 0x6c, 0x38, 0x00, 0x66, 0x6c, 0x78, 0x70, 0x78, 0x6c, 0x66, 0x00,
  25. 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7e, 0x00, 0x63, 0x77, 0x7f, 0x6b, 0x6b, 0x63, 0x63, 0x00,
  26. 0x66, 0x66, 0x76, 0x7e, 0x6e, 0x66, 0x66, 0x00, 0x3c, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3c, 0x00,
  27. 0x7c, 0x66, 0x66, 0x7c, 0x60, 0x60, 0x60, 0x00, 0x3c, 0x66, 0x66, 0x66, 0x6a, 0x6c, 0x36, 0x00,
  28. 0x7c, 0x66, 0x66, 0x7c, 0x6c, 0x66, 0x66, 0x00, 0x3c, 0x66, 0x60, 0x3c, 0x06, 0x66, 0x3c, 0x00,
  29. 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3c, 0x00,
  30. 0x66, 0x66, 0x66, 0x66, 0x66, 0x3c, 0x18, 0x00, 0x63, 0x63, 0x6b, 0x6b, 0x7f, 0x77, 0x63, 0x00,
  31. 0x66, 0x66, 0x3c, 0x18, 0x3c, 0x66, 0x66, 0x00, 0x66, 0x66, 0x66, 0x3c, 0x18, 0x18, 0x18, 0x00,
  32. 0x7e, 0x06, 0x0c, 0x18, 0x30, 0x60, 0x7e, 0x00, 0x7c, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7c, 0x00,
  33. 0x00, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x00, 0x00, 0x3e, 0x06, 0x06, 0x06, 0x06, 0x06, 0x3e, 0x00,
  34. 0x18, 0x3c, 0x66, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  35. 0x1c, 0x36, 0x30, 0x7c, 0x30, 0x30, 0x7e, 0x00, 0x00, 0x00, 0x3c, 0x06, 0x3e, 0x66, 0x3e, 0x00,
  36. 0x60, 0x60, 0x7c, 0x66, 0x66, 0x66, 0x7c, 0x00, 0x00, 0x00, 0x3c, 0x66, 0x60, 0x66, 0x3c, 0x00,
  37. 0x06, 0x06, 0x3e, 0x66, 0x66, 0x66, 0x3e, 0x00, 0x00, 0x00, 0x3c, 0x66, 0x7e, 0x60, 0x3c, 0x00,
  38. 0x1c, 0x30, 0x30, 0x7c, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x3e, 0x66, 0x66, 0x3e, 0x06, 0x3c,
  39. 0x60, 0x60, 0x7c, 0x66, 0x66, 0x66, 0x66, 0x00, 0x18, 0x00, 0x38, 0x18, 0x18, 0x18, 0x3c, 0x00,
  40. 0x18, 0x00, 0x38, 0x18, 0x18, 0x18, 0x18, 0x70, 0x60, 0x60, 0x66, 0x6c, 0x78, 0x6c, 0x66, 0x00,
  41. 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x00, 0x00, 0x00, 0x36, 0x7f, 0x6b, 0x6b, 0x63, 0x00,
  42. 0x00, 0x00, 0x7c, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x3c, 0x66, 0x66, 0x66, 0x3c, 0x00,
  43. 0x00, 0x00, 0x7c, 0x66, 0x66, 0x7c, 0x60, 0x60, 0x00, 0x00, 0x3e, 0x66, 0x66, 0x3e, 0x06, 0x07,
  44. 0x00, 0x00, 0x6c, 0x76, 0x60, 0x60, 0x60, 0x00, 0x00, 0x00, 0x3e, 0x60, 0x3c, 0x06, 0x7c, 0x00,
  45. 0x30, 0x30, 0x7c, 0x30, 0x30, 0x30, 0x1c, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x3e, 0x00,
  46. 0x00, 0x00, 0x66, 0x66, 0x66, 0x3c, 0x18, 0x00, 0x00, 0x00, 0x63, 0x6b, 0x6b, 0x7f, 0x36, 0x00,
  47. 0x00, 0x00, 0x66, 0x3c, 0x18, 0x3c, 0x66, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x3e, 0x06, 0x3c,
  48. 0x00, 0x00, 0x7e, 0x0c, 0x18, 0x30, 0x7e, 0x00, 0x0c, 0x18, 0x18, 0x70, 0x18, 0x18, 0x0c, 0x00,
  49. 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00, 0x30, 0x18, 0x18, 0x0e, 0x18, 0x18, 0x30, 0x00,
  50. 0x31, 0x6b, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51. ]
  52. from gameduino.registers import *
  53. # BaseGameduino is the common base for the Gameduino objects in remote and sim
  54. class BaseGameduino(object):
  55. def coldstart(self):
  56. self.wr(J1_RESET, 1)
  57. self.fill(RAM_PIC, 0, 10 * 1024)
  58. for i in range(512):
  59. self.sprite(i, 400, 400, 0, 0, 0)
  60. self.wr16(SPR_DISABLE, 0)
  61. self.wr16(SPR_PAGE, 0)
  62. self.wr16(JK_MODE, 0)
  63. self.wr16(SCROLL_X, 0)
  64. self.wr16(SCROLL_Y, 0)
  65. self.wr16(BG_COLOR, 0)
  66. self.wr16(SAMPLE_L, 0)
  67. self.wr16(SAMPLE_R, 0)
  68. self.wr16(SCREENSHOT_Y, 0)
  69. def dump(self, a, l):
  70. """ Dump ``l`` bytes memory starting at address ``a`` """
  71. for i in range(0, l, 16):
  72. d16 = self.rdstr(a + i, 16)
  73. print "%04x %s" % (a + i, " ".join(["%02x" % ord(c) for c in d16]))
  74. def wr(self, a, v):
  75. """ Write a single byte ``v`` to address ``a``. """
  76. self.wrstr(a, chr(v))
  77. def fill(self, a, v, c):
  78. """ Fill ``c`` bytes of memory at address ``a`` with value ``v`` """
  79. self.wrstr(a, chr(v) * c)
  80. def putstr(self, x, y, v):
  81. """ Write string ``v`` at screen position (x,y) """
  82. a = y * 64 + x
  83. self.wrstr(a, v)
  84. def rd16(self, a):
  85. return struct.unpack("<H", self.rdstr(a, 2))[0]
  86. def rd32(self, a):
  87. return struct.unpack("<L", self.rdstr(a, 4))[0]
  88. def wr16(self, a, v):
  89. """ Write 16-bit value ``v`` at to address ``a`` """
  90. self.wrstr(a, struct.pack("<H", v))
  91. def wr32(self, a, v):
  92. """ Write 32-bit value ``v`` at to address ``a`` """
  93. self.wrstr(a, struct.pack("<L", v))
  94. def setpal(self, pal, rgb):
  95. self.wr16(RAM_PAL + (pal << 1), rgb);
  96. def ascii(self):
  97. stretch = [
  98. 0x00, 0x03, 0x0c, 0x0f,
  99. 0x30, 0x33, 0x3c, 0x3f,
  100. 0xc0, 0xc3, 0xcc, 0xcf,
  101. 0xf0, 0xf3, 0xfc, 0xff ]
  102. gstr = ""
  103. for i in range(768):
  104. b = ascii_glyphs[i]
  105. h = stretch[b >> 4]
  106. l = stretch[b & 0xf]
  107. gstr += chr(h)
  108. gstr += chr(l)
  109. self.wrstr(0x1000 + (16 * ord(' ')), gstr)
  110. for i in range(0x20, 0x80):
  111. self.setpal(4 * i + 0, TRANSPARENT);
  112. self.setpal(4 * i + 3, RGB(255,255,255));
  113. self.fill(RAM_PIC, ord(' '), 4096);
  114. def voice(self, v, wave, freq, lamp, ramp = None):
  115. """
  116. Set the state of a voice.
  117. :param v: voice number 0-63
  118. :type v: int
  119. :param wave: wave type, 0 for sine 1 for noise
  120. :type wave: int
  121. :param freq: frequency control, in quarter-hertz
  122. :type freq: int
  123. :param lamp: left amplitude 0-255
  124. :type lamp: int
  125. :param ramp: right amplitude 0-255, defaults to same ``lamp``
  126. :type ramp: int
  127. """
  128. if ramp is None:
  129. ramp = lamp
  130. self.wr32(VOICES + (4 * v), freq | (wave << 15) | (lamp << 16) | (ramp << 24))
  131. def silence(self):
  132. """ Switch all voices off """
  133. for i in range(64):
  134. self.voice(i, 0, 4 * 440, 0, 0)
  135. def copy(self, a, v):
  136. self.wrstr(a, v)
  137. def microcode(self, src):
  138. """
  139. Halt coprocessor, load microprogram, restart coprocessor
  140. :param src: the microprogram, as a string, expressed
  141. :type src: string
  142. The string is loaded into the Gameduino's microprogram area,
  143. and can be up to 256 bytes. For example, to load the
  144. :ref:`splitscreen` microprogram::
  145. splitscreen_code = open("splitscreen.binle", "b").read()
  146. gd.microcode(splitscreen_code)
  147. """
  148. self.wr(J1_RESET, 1)
  149. self.copy(J1_CODE, src)
  150. self.wr(J1_RESET, 0)
  151. def sprite(self, spr, x, y, image, palette, rot, jk = 0):
  152. """
  153. Set the state of a hardware sprite
  154. :param spr: sprite number 0-511
  155. :param x: x coordinate
  156. :param y: y coordinate
  157. :param image: sprite source image 0-63
  158. :param palette: sprite palette select, 0-15, see below
  159. :param rot: sprite rotate control 0-7, see :ref:`rotate`
  160. :param jk: collision class control, 0-1
  161. Palette select controls the number of colors used for the sprite, the source palette, and which data bits
  162. to use as source.
  163. """
  164. self.wr32(RAM_SPR + (4 * spr),
  165. (x & 511) | ((rot & 7) << 9) | ((palette & 15) << 12) | ((y & 511) << 16) | ((image & 63) << 25) | ((jk & 1) << 31))
  166. def im(self):
  167. """
  168. Return the current screen as a 400x300 RGB PIL Image::
  169. >>> import gameduino.sim
  170. >>> gd = gameduino.sim.Gameduino()
  171. >>> gd.im().save("screenshot.png")
  172. """
  173. return self._im()