randoms.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/usr/bin/env python
  2. import sys
  3. import time
  4. import math
  5. import array
  6. import random
  7. import gameduino
  8. import gameduino.remote
  9. import gameduino.sim
  10. from duplicator import Duplicator
  11. random.seed(10)
  12. def read_y(y):
  13. gd.wr16(gameduino.SCREENSHOT_Y, 0x8000 | y)
  14. gd.waitvblank()
  15. gd.waitvblank()
  16. line = "".join([gd.rdstr(gameduino.SCREENSHOT + i, 200) for i in range(0, 800, 200)])
  17. gd.wr16(gameduino.SCREENSHOT_Y, 0)
  18. return array.array('H', line).tolist()
  19. def sling(n):
  20. n1s = random.randrange(n + 1)
  21. return sum([(1 << i) for i in random.sample(range(n), n1s)])
  22. def r(n):
  23. return random.randrange(n)
  24. def randbytes(n):
  25. return array.array('B', [r(256) for i in range(n)])
  26. def matches(r):
  27. return r[0] == r[1]
  28. class TestRegime(object):
  29. def __init__(self, dd):
  30. self.dd = dd
  31. assert matches(dd.rd(gameduino.IDENT))
  32. self.dd.microcode(open("../synth/sketches/j1firmware/thrasher.binle").read())
  33. self.setup()
  34. def scramble(self):
  35. """ Scramble all registers and memories """
  36. for rg in self.reg8s:
  37. self.dd.wr(rg, r(2**8))
  38. for rg in self.reg16s:
  39. self.dd.wr16(rg, r(2**16))
  40. for a,s in self.memories:
  41. self.dd.wrstr(a, randbytes(s))
  42. def setup(self):
  43. self.scramble()
  44. def cycle(self):
  45. for c in xrange(1000000):
  46. print "Cycle", c
  47. for (area,size) in random.sample(self.memories, r(len(self.memories))):
  48. dat = randbytes(1 + sling(6))
  49. if len(dat) == size:
  50. a = area
  51. else:
  52. a = area + random.randrange(0, size - len(dat))
  53. self.dd.wrstr(a, dat)
  54. if r(2) == 0 and self.reg16s:
  55. self.dd.wr16(random.choice(self.reg16s), random.getrandbits(16))
  56. if r(2) == 0 and self.reg8s:
  57. self.dd.wr(random.choice(self.reg8s), random.getrandbits(8))
  58. for (y, (e,a)) in [(y, self.dd.linecrc(y)) for y in self.checklines()]:
  59. if e != a:
  60. print "mismatch at line", y, (e,a)
  61. e = gameduino.sim.screen([y])[y]
  62. a = read_y(y)
  63. print "expected", e
  64. print "actual", a
  65. print set([(ee != aa) for (ee,aa) in zip(e,a)])
  66. print 'y', self.dd.linecrc(y)
  67. sys.exit(1)
  68. a,s = random.choice(self.memories)
  69. if r(5) == 0:
  70. assert matches(self.dd.rd(a + r(s)))
  71. if r(5) == 0:
  72. assert matches(self.dd.memcrc(a, s))
  73. if not matches(self.dd.collcrc()):
  74. def s9(vv):
  75. vv &= 0x1ff
  76. if vv > 400:
  77. vv -= 512;
  78. return vv
  79. if 0:
  80. page = self.dd.spr_page()[0]
  81. for i in range(256):
  82. sprval = gdsim.rd32(page + 4 * i)
  83. sx = s9(sprval)
  84. sy = s9(sprval >> 16)
  85. simg = (sprval >> 25) & 63
  86. spal = (sprval >> 12) & 15
  87. srot = (sprval >> 9) & 7
  88. sjk = (sprval >> 31)
  89. print "%3d: x=%3d y=%3d img=%2d pal=%d rot=%d jk=%d" % (i, sx, sy, simg, spal, srot, sjk)
  90. (e,a) = self.dd.coll()
  91. print 'collcrc', self.dd.collcrc()
  92. import binascii
  93. print 'e crc', 0xffffffff & binascii.crc32(array.array('B', e).tostring())
  94. print 'a crc', 0xffffffff & binascii.crc32(array.array('B', a).tostring())
  95. for i in range(256):
  96. print "%3d: e=%3d a=%3d" % (i, e[i], a[i])
  97. sys.exit(1)
  98. # gdsim.im().save("p%04d.png" % c)
  99. class FullchipRegime(TestRegime):
  100. reg16s = [gameduino.SCROLL_X, gameduino.SCROLL_Y, gameduino.BG_COLOR, gameduino.SAMPLE_L, gameduino.SAMPLE_R]
  101. reg8s = [gameduino.IDENT,
  102. gameduino.REV,
  103. gameduino.SPR_PAGE,
  104. gameduino.JK_MODE,
  105. gameduino.SPR_DISABLE,
  106. gameduino.IOMODE]
  107. memories = [
  108. (gameduino.RAM_PIC, 10 * 1024),
  109. (gameduino.RAM_SPR, 2048),
  110. (gameduino.RAM_SPRPAL, 2048),
  111. (gameduino.RAM_SPRIMG, 16384),
  112. (gameduino.PALETTE16A, 64),
  113. (gameduino.PALETTE4A, 64),
  114. (gameduino.VOICES, 256),
  115. # (gameduino.IDENT, 64), # register file
  116. ]
  117. def checklines(self):
  118. return [0, 299] + random.sample(range(1, 299), 2)
  119. class SpriteRegime(TestRegime):
  120. reg16s = []
  121. reg8s = [gameduino.JK_MODE,
  122. gameduino.SPR_PAGE]
  123. memories = [
  124. (gameduino.RAM_SPR, 2048),
  125. (gameduino.RAM_SPRPAL, 2048),
  126. # (gameduino.RAM_SPRIMG, 16384),
  127. (gameduino.PALETTE16A, 64),
  128. (gameduino.PALETTE4A, 64),
  129. (gameduino.VOICES, 256),
  130. ]
  131. def setup(self):
  132. self.scramble()
  133. patt = (
  134. "0101010101010101"
  135. "1222222222222220"
  136. "0222222222222221"
  137. "1222222222222220"
  138. "0222222222222221"
  139. "1222222222222220"
  140. "0222222222222221"
  141. "1222222222222220"
  142. "0222222222222221"
  143. "1222222222222220"
  144. "0222222222222221"
  145. "1222222222222220"
  146. "0222222222222221"
  147. "1222222222222220"
  148. "0222222222222221"
  149. "1010101010101010" )
  150. def expand(c):
  151. c = int(c)
  152. return c + 4 * c + 16 * c + 64 * c
  153. image = array.array('B', [expand(c) for c in patt])
  154. for i in range(64):
  155. self.dd.wrstr(gameduino.RAM_SPRIMG + 256 * i, image);
  156. self.dd.microcode(open("../synth/sketches/j1firmware/thrasher.binle").read())
  157. def checklines(self):
  158. return []
  159. def main():
  160. gdsim = gameduino.sim.Gameduino()
  161. gd = gameduino.remote.Gameduino(sys.argv[1], 115200)
  162. dd = Duplicator((gdsim, gd))
  163. # rr = SpriteRegime(dd)
  164. rr = FullchipRegime(dd)
  165. rr.cycle()
  166. if __name__ == '__main__':
  167. main()