base.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import struct
  2. def align4(s):
  3. return s + chr(0) * (-len(s) & 3)
  4. def f16(v):
  5. return int(round(65536 * v))
  6. class GD2:
  7. def c4(self, i):
  8. """Send a 32-bit value to the GD2."""
  9. self.c(struct.pack("I", i))
  10. def ac(self, s):
  11. self.c(align4(s))
  12. # The basic graphics instructions
  13. def AlphaFunc(self, func,ref):
  14. self.c4((9 << 24) | ((func & 7) << 8) | ((ref & 255) << 0))
  15. def Begin(self, prim):
  16. self.c4((31 << 24) | ((prim & 15) << 0))
  17. def BitmapHandle(self, handle):
  18. self.c4((5 << 24) | ((handle & 31) << 0))
  19. def BitmapLayout(self, format,linestride,height):
  20. self.c4((7 << 24) | ((format & 31) << 19) | ((linestride & 1023) << 9) | ((height & 511) << 0))
  21. def BitmapSize(self, filter,wrapx,wrapy,width,height):
  22. self.c4((8 << 24) | ((filter & 1) << 20) | ((wrapx & 1) << 19) | ((wrapy & 1) << 18) | ((width & 511) << 9) | ((height & 511) << 0))
  23. def BitmapSource(self, addr):
  24. self.c4((1 << 24) | ((addr & 8388607) << 0))
  25. def BitmapTransformA(self, a):
  26. self.c4((21 << 24) | ((a & 131071) << 0))
  27. def BitmapTransformB(self, b):
  28. self.c4((22 << 24) | ((b & 131071) << 0))
  29. def BitmapTransformC(self, c):
  30. self.c4((23 << 24) | ((c & 16777215) << 0))
  31. def BitmapTransformD(self, d):
  32. self.c4((24 << 24) | ((d & 131071) << 0))
  33. def BitmapTransformE(self, e):
  34. self.c4((25 << 24) | ((e & 131071) << 0))
  35. def BitmapTransformF(self, f):
  36. self.c4((26 << 24) | ((f & 16777215) << 0))
  37. def BlendFunc(self, src,dst):
  38. self.c4((11 << 24) | ((src & 7) << 3) | ((dst & 7) << 0))
  39. def Call(self, dest):
  40. self.c4((29 << 24) | ((dest & 65535) << 0))
  41. def Cell(self, cell):
  42. self.c4((6 << 24) | ((cell & 127) << 0))
  43. def ClearColorA(self, alpha):
  44. self.c4((15 << 24) | ((alpha & 255) << 0))
  45. def ClearColorRGB(self, red,green,blue):
  46. self.c4((2 << 24) | ((red & 255) << 16) | ((green & 255) << 8) | ((blue & 255) << 0))
  47. def Clear(self, c = 1,s = 1,t = 1):
  48. self.c4((38 << 24) | ((c & 1) << 2) | ((s & 1) << 1) | ((t & 1) << 0))
  49. def ClearStencil(self, s):
  50. self.c4((17 << 24) | ((s & 255) << 0))
  51. def ClearTag(self, s):
  52. self.c4((18 << 24) | ((s & 255) << 0))
  53. def ColorA(self, alpha):
  54. self.c4((16 << 24) | ((alpha & 255) << 0))
  55. def ColorMask(self, r,g,b,a):
  56. self.c4((32 << 24) | ((r & 1) << 3) | ((g & 1) << 2) | ((b & 1) << 1) | ((a & 1) << 0))
  57. def ColorRGB(self, red,green,blue):
  58. self.c4((4 << 24) | ((red & 255) << 16) | ((green & 255) << 8) | ((blue & 255) << 0))
  59. def Display(self):
  60. self.c4((0 << 24))
  61. def End(self):
  62. self.c4((33 << 24))
  63. def Jump(self, dest):
  64. self.c4((30 << 24) | ((dest & 65535) << 0))
  65. def LineWidth(self, width):
  66. self.c4((14 << 24) | ((width & 4095) << 0))
  67. def Macro(self, m):
  68. self.c4((37 << 24) | ((m & 1) << 0))
  69. def PointSize(self, size):
  70. self.c4((13 << 24) | ((size & 8191) << 0))
  71. def RestoreContext(self):
  72. self.c4((35 << 24))
  73. def Return(self):
  74. self.c4((36 << 24))
  75. def SaveContext(self):
  76. self.c4((34 << 24))
  77. def ScissorSize(self, width,height):
  78. self.c4((28 << 24) | ((width & 1023) << 10) | ((height & 1023) << 0))
  79. def ScissorXY(self, x,y):
  80. self.c4((27 << 24) | ((x & 511) << 9) | ((y & 511) << 0))
  81. def StencilFunc(self, func,ref,mask):
  82. self.c4((10 << 24) | ((func & 7) << 16) | ((ref & 255) << 8) | ((mask & 255) << 0))
  83. def StencilMask(self, mask):
  84. self.c4((19 << 24) | ((mask & 255) << 0))
  85. def StencilOp(self, sfail,spass):
  86. self.c4((12 << 24) | ((sfail & 7) << 3) | ((spass & 7) << 0))
  87. def TagMask(self, mask):
  88. self.c4((20 << 24) | ((mask & 1) << 0))
  89. def Tag(self, s):
  90. self.c4((3 << 24) | ((s & 255) << 0))
  91. def Vertex2f(self, x, y):
  92. x = int(16 * x)
  93. y = int(16 * y)
  94. self.c4((1 << 30) | ((x & 32767) << 15) | ((y & 32767) << 0))
  95. def Vertex2ii(self, x,y,handle,cell):
  96. self.c4((2 << 30) | ((x & 511) << 21) | ((y & 511) << 12) | ((handle & 31) << 7) | ((cell & 127) << 0))
  97. # Higher-level graphics commands
  98. def cmd_append(self, ptr, num):
  99. self.c(struct.pack("III", 0xffffff1e, ptr, num))
  100. def cmd_bgcolor(self, c):
  101. self.c(struct.pack("II", 0xffffff09, c))
  102. def cmd_bitmap_transform(self, x0, y0, x1, y1, x2, y2, tx0, ty0, tx1, ty1, tx2, ty2, result):
  103. self.c(struct.pack("IiiiiiiiiiiiiH", 0xffffff21, x0, y0, x1, y1, x2, y2, tx0, ty0, tx1, ty1, tx2, ty2, result))
  104. def cmd_button(self, x, y, w, h, font, options, s):
  105. self.c(struct.pack("IhhhhhH", 0xffffff0d, x, y, w, h, font, options) + s + chr(0))
  106. def cmd_calibrate(self, result):
  107. self.c(struct.pack("II", 0xffffff15, result))
  108. def cmd_clock(self, x, y, r, options, h, m, s, ms):
  109. self.c(struct.pack("IhhhHHHHH", 0xffffff14, x, y, r, options, h, m, s, ms))
  110. def cmd_coldstart(self):
  111. self.c(struct.pack("I", 0xffffff32))
  112. def cmd_dial(self, x, y, r, options, val):
  113. self.c(struct.pack("IhhhHH", 0xffffff2d, x, y, r, options, val))
  114. def cmd_dlstart(self):
  115. self.c(struct.pack("I", 0xffffff00))
  116. def cmd_fgcolor(self, c):
  117. self.c(struct.pack("II", 0xffffff0a, c))
  118. def cmd_gauge(self, x, y, r, options, major, minor, val, range):
  119. self.c(struct.pack("IhhhHHHHH", 0xffffff13, x, y, r, options, major, minor, val, range))
  120. def cmd_getmatrix(self, a, b, c, d, e, f):
  121. self.c(struct.pack("Iiiiiii", 0xffffff33, a, b, c, d, e, f))
  122. def cmd_getprops(self, ptr, w, h):
  123. self.c(struct.pack("IIII", 0xffffff25, ptr, w, h))
  124. def cmd_getptr(self, result):
  125. self.c(struct.pack("II", 0xffffff23, result))
  126. def cmd_gradcolor(self, c):
  127. self.c(struct.pack("II", 0xffffff34, c))
  128. def cmd_gradient(self, x0, y0, rgb0, x1, y1, rgb1):
  129. self.c(struct.pack("IhhIhhI", 0xffffff0b, x0, y0, rgb0, x1, y1, rgb1))
  130. def cmd_inflate(self, ptr):
  131. self.c(struct.pack("II", 0xffffff22, ptr))
  132. def cmd_interrupt(self, ms):
  133. self.c(struct.pack("II", 0xffffff02, ms))
  134. def cmd_keys(self, x, y, w, h, font, options, s):
  135. self.c(struct.pack("IhhhhhH", 0xffffff0e, x, y, w, h, font, options) + s + chr(0))
  136. def cmd_loadidentity(self):
  137. self.c(struct.pack("I", 0xffffff26))
  138. def cmd_loadimage(self, ptr, options):
  139. self.c(struct.pack("III", 0xffffff24, ptr, options))
  140. def cmd_logo(self):
  141. self.c(struct.pack("I", 0xffffff31))
  142. def cmd_memcpy(self, dest, src, num):
  143. self.c(struct.pack("IIII", 0xffffff1d, dest, src, num))
  144. def cmd_memcrc(self, ptr, num, result):
  145. self.c(struct.pack("IIII", 0xffffff18, ptr, num, result))
  146. def cmd_memset(self, ptr, value, num):
  147. self.c(struct.pack("IIII", 0xffffff1b, ptr, value, num))
  148. def cmd_memwrite(self, ptr, num):
  149. self.c(struct.pack("III", 0xffffff1a, ptr, num))
  150. def cmd_regwrite(self, ptr, val):
  151. self.memwrite(ptr, 4)
  152. self.c4(val)
  153. def cmd_regwrite(self, ptr, val):
  154. self.c(struct.pack("IIII", 0xffffff1a, ptr, 4, val))
  155. def cmd_memzero(self, ptr, num):
  156. self.c(struct.pack("III", 0xffffff1c, ptr, num))
  157. def cmd_number(self, x, y, font, options, n):
  158. self.c(struct.pack("IhhhHi", 0xffffff2e, x, y, font, options, n))
  159. def cmd_progress(self, x, y, w, h, options, val, range):
  160. self.c(struct.pack("IhhhhHHH", 0xffffff0f, x, y, w, h, options, val, range))
  161. def cmd_regread(self, ptr, result):
  162. self.c(struct.pack("III", 0xffffff19, ptr, result))
  163. def cmd_rotate(self, a):
  164. self.c(struct.pack("Ii", 0xffffff29, a))
  165. def cmd_scale(self, sx, sy):
  166. self.c(struct.pack("Iii", 0xffffff28, f16(sx), f16(sy)))
  167. def cmd_screensaver(self):
  168. self.c(struct.pack("I", 0xffffff2f))
  169. def cmd_scrollbar(self, x, y, w, h, options, val, size, range):
  170. self.c(struct.pack("IhhhhHHHH", 0xffffff11, x, y, w, h, options, val, size, range))
  171. def cmd_setfont(self, font, ptr):
  172. self.c(struct.pack("III", 0xffffff2b, font, ptr))
  173. def cmd_setmatrix(self):
  174. self.c(struct.pack("I", 0xffffff2a))
  175. def cmd_sketch(self, x, y, w, h, ptr, format):
  176. self.c(struct.pack("IhhHHIH", 0xffffff30, x, y, w, h, ptr, format))
  177. def cmd_slider(self, x, y, w, h, options, val, range):
  178. self.c(struct.pack("IhhhhHHH", 0xffffff10, x, y, w, h, options, val, range))
  179. def cmd_snapshot(self, ptr):
  180. self.c(struct.pack("II", 0xffffff1f, ptr))
  181. def cmd_spinner(self, x, y, style, scale):
  182. self.c(struct.pack("IhhHH", 0xffffff16, x, y, style, scale))
  183. def cmd_stop(self):
  184. self.c(struct.pack("I", 0xffffff17))
  185. def cmd_swap(self):
  186. self.c(struct.pack("I", 0xffffff01))
  187. def cmd_text(self, x, y, font, options, s):
  188. self.c(align4(struct.pack("IhhhH", 0xffffff0c, x, y, font, options) + s + chr(0)))
  189. def cmd_toggle(self, x, y, w, font, options, state, s):
  190. self.c(struct.pack("IhhhhHH", 0xffffff12, x, y, w, font, options, state) + s + chr(0))
  191. def cmd_touch_transform(self, x0, y0, x1, y1, x2, y2, tx0, ty0, tx1, ty1, tx2, ty2, result):
  192. self.c(struct.pack("IiiiiiiiiiiiiH", 0xffffff20, x0, y0, x1, y1, x2, y2, tx0, ty0, tx1, ty1, tx2, ty2, result))
  193. def cmd_track(self, x, y, w, h, tag):
  194. self.c(struct.pack("Ihhhhh", 0xffffff2c, x, y, w, h, tag))
  195. def cmd_translate(self, tx, ty):
  196. self.c(struct.pack("Iii", 0xffffff27, f16(tx), f16(ty)))
  197. # Converting versions, *f
  198. def cmd_translatef(self, tx, ty):
  199. self.cmd_translate(int(65536 * tx), int(65536 * ty))
  200. #
  201. # The new 810 opcodes
  202. #
  203. def VertexFormat(self, frac):
  204. self.c4((39 << 24) | (((frac) & 7) << 0))
  205. def BitmapLayoutH(self, linestride,height):
  206. self.c4((40 << 24) | (((linestride) & 3) << 2) | (((height) & 3) << 0))
  207. def BitmapSizeH(self, width,height):
  208. self.c4((41 << 24) | (((width) & 3) << 2) | (((height) & 3) << 0))
  209. def PaletteSource(self, addr):
  210. self.c4((42 << 24) | (((addr) & 4194303) << 0))
  211. def VertexTranslateX(self, x):
  212. self.c4((43 << 24) | (((x) & 131071) << 0))
  213. def VertexTranslateY(self, y):
  214. self.c4((44 << 24) | (((y) & 131071) << 0))
  215. def Nop(self):
  216. self.c4((45 << 24))
  217. #
  218. # The new 810 commands
  219. #
  220. def cmd_romfont(self, dst, src):
  221. self.c(struct.pack("III", 0xffffff3f, dst, src))
  222. def cmd_mediafifo(self, ptr, size):
  223. self.c(struct.pack("III", 0xffffff39, ptr, size))
  224. def cmd_sync(self):
  225. self.c(struct.pack("I", 0xffffff42))
  226. def cmd_setrotate(self, o):
  227. self.c(struct.pack("II", 0xffffff36, o))
  228. def cmd_setbitmap(self, source, fmt, w, h):
  229. self.c(struct.pack("IIhhhh", 0xffffff43, source, fmt, w, h, 0))
  230. # def cmd_snapshot2(self,
  231. # def cmd_setbase(self,
  232. # def cmd_playvideo(self,
  233. # def cmd_setfont2(self,
  234. # def cmd_setscratch(self,
  235. # def cmd_videostart(self,
  236. # def cmd_videoframe(self,
  237. # def cmd_sync(self,
  238. # def cmd_setbitmap(self,
  239. def swap(self):
  240. self.Display()
  241. self.cmd_swap()
  242. self.cmd_dlstart()