event_macros.asm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. ;\1 = event index
  2. ;\2 = return result in carry instead of zero flag
  3. CheckEvent: MACRO
  4. event_byte = ((\1) / 8)
  5. ld a, [wEventFlags + event_byte]
  6. IF _NARG > 1
  7. IF ((\1) % 8) == 7
  8. add a
  9. ELSE
  10. REPT ((\1) % 8) + 1
  11. rrca
  12. ENDR
  13. ENDC
  14. ELSE
  15. bit (\1) % 8, a
  16. ENDC
  17. ENDM
  18. ;\1 = event index
  19. CheckEventReuseA: MACRO
  20. IF event_byte != ((\1) / 8)
  21. event_byte = ((\1) / 8)
  22. ld a, [wEventFlags + event_byte]
  23. ENDC
  24. bit (\1) % 8, a
  25. ENDM
  26. ;\1 = event index
  27. ;\2 = event index of the last event used before the branch
  28. CheckEventAfterBranchReuseA: MACRO
  29. event_byte = ((\2) / 8)
  30. IF event_byte != ((\1) / 8)
  31. event_byte = ((\1) / 8)
  32. ld a, [wEventFlags + event_byte]
  33. ENDC
  34. bit (\1) % 8, a
  35. ENDM
  36. ;\1 = reg
  37. ;\2 = event index
  38. ;\3 = event index this event is relative to (optional, this is needed when there is a fixed flag address)
  39. EventFlagBit: MACRO
  40. IF _NARG > 2
  41. ld \1, ((\3) % 8) + ((\2) - (\3))
  42. ELSE
  43. ld \1, (\2) % 8
  44. ENDC
  45. ENDM
  46. ;\1 = reg
  47. ;\2 = event index
  48. EventFlagAddress: MACRO
  49. event_byte = ((\2) / 8)
  50. ld \1, wEventFlags + event_byte
  51. ENDM
  52. ;\1 = event index
  53. CheckEventHL: MACRO
  54. event_byte = ((\1) / 8)
  55. ld hl, wEventFlags + event_byte
  56. bit (\1) % 8, [hl]
  57. ENDM
  58. ;\1 = event index
  59. CheckEventReuseHL: MACRO
  60. IF event_byte != ((\1) / 8)
  61. event_byte = ((\1) / 8)
  62. ld hl, wEventFlags + event_byte
  63. ENDC
  64. bit (\1) % 8, [hl]
  65. ENDM
  66. ; dangerous, only use when HL is guaranteed to be the desired value
  67. ;\1 = event index
  68. CheckEventForceReuseHL: MACRO
  69. event_byte = ((\1) / 8)
  70. bit (\1) % 8, [hl]
  71. ENDM
  72. ;\1 = event index
  73. ;\2 = event index of the last event used before the branch
  74. CheckEventAfterBranchReuseHL: MACRO
  75. event_byte = ((\2) / 8)
  76. IF event_byte != ((\1) / 8)
  77. event_byte = ((\1) / 8)
  78. ld hl, wEventFlags + event_byte
  79. ENDC
  80. bit (\1) % 8, [hl]
  81. ENDM
  82. ;\1 = event index
  83. CheckAndSetEvent: MACRO
  84. event_byte = ((\1) / 8)
  85. ld hl, wEventFlags + event_byte
  86. bit (\1) % 8, [hl]
  87. set (\1) % 8, [hl]
  88. ENDM
  89. ;\1 = event index
  90. CheckAndResetEvent: MACRO
  91. event_byte = ((\1) / 8)
  92. ld hl, wEventFlags + event_byte
  93. bit (\1) % 8, [hl]
  94. res (\1) % 8, [hl]
  95. ENDM
  96. ;\1 = event index
  97. CheckAndSetEventA: MACRO
  98. ld a, [wEventFlags + ((\1) / 8)]
  99. bit (\1) % 8, a
  100. set (\1) % 8, a
  101. ld [wEventFlags + ((\1) / 8)], a
  102. ENDM
  103. ;\1 = event index
  104. CheckAndResetEventA: MACRO
  105. ld a, [wEventFlags + ((\1) / 8)]
  106. bit (\1) % 8, a
  107. res (\1) % 8, a
  108. ld [wEventFlags + ((\1) / 8)], a
  109. ENDM
  110. ;\1 = event index
  111. SetEvent: MACRO
  112. event_byte = ((\1) / 8)
  113. ld hl, wEventFlags + event_byte
  114. set (\1) % 8, [hl]
  115. ENDM
  116. ;\1 = event index
  117. SetEventReuseHL: MACRO
  118. IF event_byte != ((\1) / 8)
  119. event_byte = ((\1) / 8)
  120. ld hl, wEventFlags + event_byte
  121. ENDC
  122. set (\1) % 8, [hl]
  123. ENDM
  124. ;\1 = event index
  125. ;\2 = event index of the last event used before the branch
  126. SetEventAfterBranchReuseHL: MACRO
  127. event_byte = ((\2) / 8)
  128. IF event_byte != ((\1) / 8)
  129. event_byte = ((\1) / 8)
  130. ld hl, wEventFlags + event_byte
  131. ENDC
  132. set (\1) % 8, [hl]
  133. ENDM
  134. ; dangerous, only use when HL is guaranteed to be the desired value
  135. ;\1 = event index
  136. SetEventForceReuseHL: MACRO
  137. event_byte = ((\1) / 8)
  138. set (\1) % 8, [hl]
  139. ENDM
  140. ;\1 = event index
  141. ;\2 = event index
  142. ;\3, \4, ... = additional (optional) event indices
  143. SetEvents: MACRO
  144. SetEvent \1
  145. rept (_NARG + -1)
  146. SetEventReuseHL \2
  147. shift
  148. endr
  149. ENDM
  150. ;\1 = event index
  151. ResetEvent: MACRO
  152. event_byte = ((\1) / 8)
  153. ld hl, wEventFlags + event_byte
  154. res (\1) % 8, [hl]
  155. ENDM
  156. ;\1 = event index
  157. ResetEventReuseHL: MACRO
  158. IF event_byte != ((\1) / 8)
  159. event_byte = ((\1) / 8)
  160. ld hl, wEventFlags + event_byte
  161. ENDC
  162. res (\1) % 8, [hl]
  163. ENDM
  164. ;\1 = event index
  165. ;\2 = event index of the last event used before the branch
  166. ResetEventAfterBranchReuseHL: MACRO
  167. event_byte = ((\2) / 8)
  168. IF event_byte != ((\1) / 8)
  169. event_byte = ((\1) / 8)
  170. ld hl, wEventFlags + event_byte
  171. ENDC
  172. res (\1) % 8, [hl]
  173. ENDM
  174. ; dangerous, only use when HL is guaranteed to be the desired value
  175. ;\1 = event index
  176. ResetEventForceReuseHL: MACRO
  177. event_byte = ((\1) / 8)
  178. res (\1) % 8, [hl]
  179. ENDM
  180. ;\1 = event index
  181. ;\2 = event index
  182. ;\3 = event index (optional)
  183. ResetEvents: MACRO
  184. ResetEvent \1
  185. rept (_NARG + -1)
  186. ResetEventReuseHL \2
  187. shift
  188. endr
  189. ENDM
  190. ;\1 = event index
  191. ;\2 = number of bytes away from the base address (optional, for matching the ROM)
  192. dbEventFlagBit: MACRO
  193. IF _NARG > 1
  194. db ((\1) % 8) + ((\2) * 8)
  195. ELSE
  196. db ((\1) % 8)
  197. ENDC
  198. ENDM
  199. ;\1 = event index
  200. ;\2 = number of bytes away from the base address (optional, for matching the ROM)
  201. dwEventFlagAddress: MACRO
  202. IF _NARG > 1
  203. dw wEventFlags + ((\1) / 8) - (\2)
  204. ELSE
  205. dw wEventFlags + ((\1) / 8)
  206. ENDC
  207. ENDM
  208. ;\1 = start
  209. ;\2 = end
  210. SetEventRange: MACRO
  211. event_start_byte = ((\1) / 8)
  212. event_end_byte = ((\2) / 8)
  213. IF event_end_byte < event_start_byte
  214. FAIL "Incorrect argument order in SetEventRange."
  215. ENDC
  216. IF event_start_byte == event_end_byte
  217. ld a, [wEventFlags + event_start_byte]
  218. or (1 << (((\2) % 8) + 1)) - (1 << ((\1) % 8))
  219. ld [wEventFlags + event_start_byte], a
  220. ELSE
  221. event_fill_start = event_start_byte + 1
  222. event_fill_count = event_end_byte - event_start_byte - 1
  223. IF ((\1) % 8) == 0
  224. event_fill_start = event_fill_start + -1
  225. event_fill_count = event_fill_count + 1
  226. ELSE
  227. ld a, [wEventFlags + event_start_byte]
  228. or $ff - ((1 << ((\1) % 8)) - 1)
  229. ld [wEventFlags + event_start_byte], a
  230. ENDC
  231. IF ((\2) % 8) == 7
  232. event_fill_count = event_fill_count + 1
  233. ENDC
  234. IF event_fill_count == 1
  235. ld hl, wEventFlags + event_fill_start
  236. ld [hl], $ff
  237. ENDC
  238. IF event_fill_count > 1
  239. ld a, $ff
  240. ld hl, wEventFlags + event_fill_start
  241. REPT event_fill_count + -1
  242. ld [hli], a
  243. ENDR
  244. ld [hl], a
  245. ENDC
  246. IF ((\2) % 8) == 0
  247. ld hl, wEventFlags + event_end_byte
  248. set 0, [hl]
  249. ELSE
  250. IF ((\2) % 8) != 7
  251. ld a, [wEventFlags + event_end_byte]
  252. or (1 << (((\2) % 8) + 1)) - 1
  253. ld [wEventFlags + event_end_byte], a
  254. ENDC
  255. ENDC
  256. ENDC
  257. ENDM
  258. ;\1 = start
  259. ;\2 = end
  260. ;\3 = assume a is 0 if present
  261. ResetEventRange: MACRO
  262. event_start_byte = ((\1) / 8)
  263. event_end_byte = ((\2) / 8)
  264. IF event_end_byte < event_start_byte
  265. FAIL "Incorrect argument order in ResetEventRange."
  266. ENDC
  267. IF event_start_byte == event_end_byte
  268. ld a, [wEventFlags + event_start_byte]
  269. and ~((1 << (((\2) % 8) + 1)) - (1 << ((\1) % 8))) & $ff
  270. ld [wEventFlags + event_start_byte], a
  271. ELSE
  272. event_fill_start = event_start_byte + 1
  273. event_fill_count = event_end_byte - event_start_byte - 1
  274. IF ((\1) % 8) == 0
  275. event_fill_start = event_fill_start + -1
  276. event_fill_count = event_fill_count + 1
  277. ELSE
  278. ld a, [wEventFlags + event_start_byte]
  279. and ~($ff - ((1 << ((\1) % 8)) - 1)) & $ff
  280. ld [wEventFlags + event_start_byte], a
  281. ENDC
  282. IF ((\2) % 8) == 7
  283. event_fill_count = event_fill_count + 1
  284. ENDC
  285. IF event_fill_count == 1
  286. ld hl, wEventFlags + event_fill_start
  287. ld [hl], 0
  288. ENDC
  289. IF event_fill_count > 1
  290. ld hl, wEventFlags + event_fill_start
  291. ; force xor a if we just to wrote to it above
  292. IF (_NARG < 3) || (((\1) % 8) != 0)
  293. xor a
  294. ENDC
  295. REPT event_fill_count + -1
  296. ld [hli], a
  297. ENDR
  298. ld [hl], a
  299. ENDC
  300. IF ((\2) % 8) == 0
  301. ld hl, wEventFlags + event_end_byte
  302. res 0, [hl]
  303. ELSE
  304. IF ((\2) % 8) != 7
  305. ld a, [wEventFlags + event_end_byte]
  306. and ~((1 << (((\2) % 8) + 1)) - 1) & $ff
  307. ld [wEventFlags + event_end_byte], a
  308. ENDC
  309. ENDC
  310. ENDC
  311. ENDM
  312. ; returns whether both events are set in Z flag
  313. ; This is counter-intuitive because the other event checks set the Z flag when
  314. ; the event is not set, but this sets the Z flag when the event is set.
  315. ;\1 = event index 1
  316. ;\2 = event index 2
  317. ;\3 = try to reuse a (optional)
  318. CheckBothEventsSet: MACRO
  319. IF ((\1) / 8) == ((\2) / 8)
  320. IF (_NARG < 3) || (((\1) / 8) != event_byte)
  321. event_byte = ((\1) / 8)
  322. ld a, [wEventFlags + ((\1) / 8)]
  323. ENDC
  324. and (1 << ((\1) % 8)) | (1 << ((\2) % 8))
  325. cp (1 << ((\1) % 8)) | (1 << ((\2) % 8))
  326. ELSE
  327. ; This case doesn't happen in the original ROM.
  328. IF ((\1) % 8) == ((\2) % 8)
  329. push hl
  330. ld a, [wEventFlags + ((\1) / 8)]
  331. ld hl, wEventFlags + ((\2) / 8)
  332. and [hl]
  333. cpl
  334. bit ((\1) % 8), a
  335. pop hl
  336. ELSE
  337. push bc
  338. ld a, [wEventFlags + ((\1) / 8)]
  339. and (1 << ((\1) % 8))
  340. ld b, a
  341. ld a, [wEventFlags + ((\2) / 8)]
  342. and (1 << ((\2) % 8))
  343. or b
  344. cp (1 << ((\1) % 8)) | (1 << ((\2) % 8))
  345. pop bc
  346. ENDC
  347. ENDC
  348. ENDM
  349. ; returns the complement of whether either event is set in Z flag
  350. ;\1 = event index 1
  351. ;\2 = event index 2
  352. CheckEitherEventSet: MACRO
  353. IF ((\1) / 8) == ((\2) / 8)
  354. ld a, [wEventFlags + ((\1) / 8)]
  355. and (1 << ((\1) % 8)) | (1 << ((\2) % 8))
  356. ELSE
  357. ; This case doesn't happen in the original ROM.
  358. IF ((\1) % 8) == ((\2) % 8)
  359. push hl
  360. ld a, [wEventFlags + ((\1) / 8)]
  361. ld hl, wEventFlags + ((\2) / 8)
  362. or [hl]
  363. bit ((\1) % 8), a
  364. pop hl
  365. ELSE
  366. push bc
  367. ld a, [wEventFlags + ((\1) / 8)]
  368. and (1 << ((\1) % 8))
  369. ld b, a
  370. ld a, [wEventFlags + ((\2) / 8)]
  371. and (1 << ((\2) % 8))
  372. or b
  373. pop bc
  374. ENDC
  375. ENDC
  376. ENDM
  377. ; for handling fixed event bits when events are inserted/removed
  378. ;\1 = event index
  379. ;\2 = fixed flag bit
  380. AdjustEventBit: MACRO
  381. IF ((\1) % 8) != (\2)
  382. add ((\1) % 8) - (\2)
  383. ENDC
  384. ENDM