bus_iorq_m1.asm 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. ; The entry point is im2value. The routine needs at least 4608 (0x1200) bytes.
  2. ; Well, maybe a bit less depending on where it is located.
  3. ; Base address - should be a multiple of 256.
  4. base equ 6D00h
  5. ; U is the base value for addresses contained in the De Bruijn sequence. The
  6. ; lowest address will be U*257 and the highest address will be (U+15)*257.
  7. ; These correspond to port values of 0 and 254, respectively.
  8. U equ (base>>8)+2 ; from 6F6Fh to 7E7Eh
  9. org base
  10. ; Sorted address to initial value lookup table - 256 entries.
  11. SortedTable: db 000h, 002h, 004h, 006h, 008h, 00Ah, 00Ch, 00Eh
  12. db 010h, 012h, 014h, 016h, 018h, 01Ah, 01Ch, 0FFh
  13. db 001h, 01Fh, 021h, 023h, 025h, 027h, 029h, 02Bh
  14. db 02Dh, 02Fh, 031h, 033h, 035h, 037h, 039h, 01Eh
  15. db 003h, 020h, 03Ch, 03Eh, 040h, 042h, 044h, 046h
  16. db 048h, 04Ah, 04Ch, 04Eh, 050h, 052h, 054h, 03Bh
  17. db 005h, 022h, 03Dh, 057h, 059h, 05Bh, 05Dh, 05Fh
  18. db 061h, 063h, 065h, 067h, 069h, 06Bh, 06Dh, 056h
  19. db 007h, 024h, 03Fh, 058h, 070h, 072h, 074h, 076h
  20. db 078h, 07Ah, 07Ch, 07Eh, 080h, 082h, 084h, 06Fh
  21. db 009h, 026h, 041h, 05Ah, 071h, 087h, 089h, 08Bh
  22. db 08Dh, 08Fh, 091h, 093h, 095h, 097h, 099h, 086h
  23. db 00Bh, 028h, 043h, 05Ch, 073h, 088h, 09Ch, 09Eh
  24. db 0A0h, 0A2h, 0A4h, 0A6h, 0A8h, 0AAh, 0ACh, 09Bh
  25. db 00Dh, 02Ah, 045h, 05Eh, 075h, 08Ah, 09Dh, 0AFh
  26. db 0B1h, 0B3h, 0B5h, 0B7h, 0B9h, 0BBh, 0BDh, 0AEh
  27. db 00Fh, 02Ch, 047h, 060h, 077h, 08Ch, 09Fh, 0B0h
  28. db 0C0h, 0C2h, 0C4h, 0C6h, 0C8h, 0CAh, 0CCh, 0BFh
  29. db 011h, 02Eh, 049h, 062h, 079h, 08Eh, 0A1h, 0B2h
  30. db 0C1h, 0CFh, 0D1h, 0D3h, 0D5h, 0D7h, 0D9h, 0CEh
  31. db 013h, 030h, 04Bh, 064h, 07Bh, 090h, 0A3h, 0B4h
  32. db 0C3h, 0D0h, 0DCh, 0DEh, 0E0h, 0E2h, 0E4h, 0DBh
  33. db 015h, 032h, 04Dh, 066h, 07Dh, 092h, 0A5h, 0B6h
  34. db 0C5h, 0D2h, 0DDh, 0E7h, 0E9h, 0EBh, 0EDh, 0E6h
  35. db 017h, 034h, 04Fh, 068h, 07Fh, 094h, 0A7h, 0B8h
  36. db 0C7h, 0D4h, 0DFh, 0E8h, 0F0h, 0F2h, 0F4h, 0EFh
  37. db 019h, 036h, 051h, 06Ah, 081h, 096h, 0A9h, 0BAh
  38. db 0C9h, 0D6h, 0E1h, 0EAh, 0F1h, 0F7h, 0F9h, 0F6h
  39. db 01Bh, 038h, 053h, 06Ch, 083h, 098h, 0ABh, 0BCh
  40. db 0CBh, 0D8h, 0E3h, 0ECh, 0F3h, 0F8h, 0FCh, 0FBh
  41. db 01Dh, 03Ah, 055h, 06Eh, 085h, 09Ah, 0ADh, 0BEh
  42. db 0CDh, 0DAh, 0E5h, 0EEh, 0F5h, 0FAh, 0FDh, 0FEh
  43. ; Interrupt vectors, organized as a De Bruijn sequence B(16,2)
  44. ; A B(16,2) De Bruijn sequence is a wrapping sequence of 256
  45. ; values, formed by 16 symbols, in which every consecutive
  46. ; pair appears exactly once in the sequence, which is what
  47. ; we need. We use the numbers U+0 through U+15 as symbols.
  48. IntVecs: db U+00h, U+00h, U+01h, U+00h, U+02h, U+00h, U+03h, U+00h
  49. db U+04h, U+00h, U+05h, U+00h, U+06h, U+00h, U+07h, U+00h
  50. db U+08h, U+00h, U+09h, U+00h, U+0Ah, U+00h, U+0Bh, U+00h
  51. db U+0Ch, U+00h, U+0Dh, U+00h, U+0Eh, U+00h, U+0Fh, U+01h
  52. db U+01h, U+02h, U+01h, U+03h, U+01h, U+04h, U+01h, U+05h
  53. db U+01h, U+06h, U+01h, U+07h, U+01h, U+08h, U+01h, U+09h
  54. db U+01h, U+0Ah, U+01h, U+0Bh, U+01h, U+0Ch, U+01h, U+0Dh
  55. db U+01h, U+0Eh, U+01h, U+0Fh, U+02h, U+02h, U+03h, U+02h
  56. db U+04h, U+02h, U+05h, U+02h, U+06h, U+02h, U+07h, U+02h
  57. db U+08h, U+02h, U+09h, U+02h, U+0Ah, U+02h, U+0Bh, U+02h
  58. db U+0Ch, U+02h, U+0Dh, U+02h, U+0Eh, U+02h, U+0Fh, U+03h
  59. db U+03h, U+04h, U+03h, U+05h, U+03h, U+06h, U+03h, U+07h
  60. db U+03h, U+08h, U+03h, U+09h, U+03h, U+0Ah, U+03h, U+0Bh
  61. db U+03h, U+0Ch, U+03h, U+0Dh, U+03h, U+0Eh, U+03h, U+0Fh
  62. db U+04h, U+04h, U+05h, U+04h, U+06h, U+04h, U+07h, U+04h
  63. db U+08h, U+04h, U+09h, U+04h, U+0Ah, U+04h, U+0Bh, U+04h
  64. db U+0Ch, U+04h, U+0Dh, U+04h, U+0Eh, U+04h, U+0Fh, U+05h
  65. db U+05h, U+06h, U+05h, U+07h, U+05h, U+08h, U+05h, U+09h
  66. db U+05h, U+0Ah, U+05h, U+0Bh, U+05h, U+0Ch, U+05h, U+0Dh
  67. db U+05h, U+0Eh, U+05h, U+0Fh, U+06h, U+06h, U+07h, U+06h
  68. db U+08h, U+06h, U+09h, U+06h, U+0Ah, U+06h, U+0Bh, U+06h
  69. db U+0Ch, U+06h, U+0Dh, U+06h, U+0Eh, U+06h, U+0Fh, U+07h
  70. db U+07h, U+08h, U+07h, U+09h, U+07h, U+0Ah, U+07h, U+0Bh
  71. db U+07h, U+0Ch, U+07h, U+0Dh, U+07h, U+0Eh, U+07h, U+0Fh
  72. db U+08h, U+08h, U+09h, U+08h, U+0Ah, U+08h, U+0Bh, U+08h
  73. db U+0Ch, U+08h, U+0Dh, U+08h, U+0Eh, U+08h, U+0Fh, U+09h
  74. db U+09h, U+0Ah, U+09h, U+0Bh, U+09h, U+0Ch, U+09h, U+0Dh
  75. db U+09h, U+0Eh, U+09h, U+0Fh, U+0Ah, U+0Ah, U+0Bh, U+0Ah
  76. db U+0Ch, U+0Ah, U+0Dh, U+0Ah, U+0Eh, U+0Ah, U+0Fh, U+0Bh
  77. db U+0Bh, U+0Ch, U+0Bh, U+0Dh, U+0Bh, U+0Eh, U+0Bh, U+0Fh
  78. db U+0Ch, U+0Ch, U+0Dh, U+0Ch, U+0Eh, U+0Ch, U+0Fh, U+0Dh
  79. db U+0Dh, U+0Eh, U+0Dh, U+0Fh, U+0Eh, U+0Eh, U+0Fh, U+0Fh
  80. db U+00h ; The De Bruijn sequence wraps around
  81. ; but the interrupt vectors don't, so we
  82. ; repeat the first byte here.
  83. ; At start, A = 255.
  84. ; At U*257+0000h, A should decrease by 255 before returning,
  85. ; resulting in 0.
  86. ; At U*257+0001h, A should decrease by 254, resulting in 1.
  87. ; At U*257+0002h, by 253, resulting in 2.
  88. ; ...
  89. ; At U*257+000Fh, by 240, resulting in 15.
  90. ; (Note there's a gap here)
  91. ; At U*257+0100h, by 239, resulting in 16.
  92. ; At U*257+0101h, by 238, resulting in 17.
  93. ; ...
  94. ; At U*257+0F0Eh, by 1, resulting in 254.
  95. ; At U*257+0F0Fh, it should be returned untouched (255).
  96. ;
  97. ; This way, the value of A will be unique for every possible
  98. ; target address, so all that is left is to find out which
  99. ; original bus value led to that value of A. That's what
  100. ; SortedTable is for.
  101. ;
  102. ; The comment at these addresses indicates what value of the
  103. ; bus led to that address.
  104. org U*257+0000h
  105. dec a ; 0
  106. dec a ; 2
  107. dec a ; 4
  108. dec a ; 6
  109. dec a ; 8
  110. dec a ; 10
  111. dec a ; 12
  112. dec a ; 14
  113. dec a ; 16
  114. dec a ; 18
  115. dec a ; 20
  116. dec a ; 22
  117. dec a ; 24
  118. dec a ; 26
  119. dec a ; 28
  120. ;dec a ; 255
  121. ;jp U*257+0100h ; Instead of this, we take a shortcut
  122. sub 240 ; by subtracting as many DEC A's as
  123. ret ; there are remaining, then return.
  124. ; Take advantage of the gap between address groups to insert this routine.
  125. ; The gap is at least 238 bytes long.
  126. ; Bus value finder, using IM2.
  127. ;
  128. ; Returns bus value in A. Clobbers HL.
  129. ; Upon return, interrupts are enabled and IM1 mode is set.
  130. im2value: ld a,HIGH IntVecs
  131. di
  132. ld i,a
  133. ld a,255 ; Prepare initial value
  134. im 2
  135. ei
  136. halt
  137. im 1
  138. ei
  139. ld l,a ; A is sorted index
  140. ld h,HIGH SortedTable
  141. ld a,(hl) ; Grab the value for that index
  142. ret
  143. org U*257+0100h
  144. dec a ; 1
  145. dec a ; 31
  146. dec a ; 33
  147. dec a ; 35
  148. dec a ; 37
  149. dec a ; 39
  150. dec a ; 41
  151. dec a ; 43
  152. dec a ; 45
  153. dec a ; 47
  154. dec a ; 49
  155. dec a ; 51
  156. dec a ; 53
  157. dec a ; 55
  158. dec a ; 57
  159. ;dec a ; 30
  160. ;jp U*257+0200h ; See comment above
  161. sub 224 ; 30
  162. ret
  163. org U*257+0200h
  164. dec a ; 3
  165. dec a ; 32
  166. dec a ; 60
  167. dec a ; 62
  168. dec a ; 64
  169. dec a ; 66
  170. dec a ; 68
  171. dec a ; 70
  172. dec a ; 72
  173. dec a ; 74
  174. dec a ; 76
  175. dec a ; 78
  176. dec a ; 80
  177. dec a ; 82
  178. dec a ; 84
  179. sub 208 ; 59
  180. ret
  181. org U*257+0300h
  182. dec a ; 5
  183. dec a ; 34
  184. dec a ; 61
  185. dec a ; 87
  186. dec a ; 89
  187. dec a ; 91
  188. dec a ; 93
  189. dec a ; 95
  190. dec a ; 97
  191. dec a ; 99
  192. dec a ; 101
  193. dec a ; 103
  194. dec a ; 105
  195. dec a ; 107
  196. dec a ; 109
  197. sub 192 ; 86
  198. ret
  199. org U*257+0400h
  200. dec a ; 7
  201. dec a ; 36
  202. dec a ; 63
  203. dec a ; 88
  204. dec a ; 112
  205. dec a ; 114
  206. dec a ; 116
  207. dec a ; 118
  208. dec a ; 120
  209. dec a ; 122
  210. dec a ; 124
  211. dec a ; 126
  212. dec a ; 128
  213. dec a ; 130
  214. dec a ; 132
  215. sub 176 ; 111
  216. ret
  217. org U*257+0500h
  218. dec a ; 9
  219. dec a ; 38
  220. dec a ; 65
  221. dec a ; 90
  222. dec a ; 113
  223. dec a ; 135
  224. dec a ; 137
  225. dec a ; 139
  226. dec a ; 141
  227. dec a ; 143
  228. dec a ; 145
  229. dec a ; 147
  230. dec a ; 149
  231. dec a ; 151
  232. dec a ; 153
  233. sub 160 ; 134
  234. ret
  235. org U*257+0600h
  236. dec a ; 11
  237. dec a ; 40
  238. dec a ; 67
  239. dec a ; 92
  240. dec a ; 115
  241. dec a ; 136
  242. dec a ; 156
  243. dec a ; 158
  244. dec a ; 160
  245. dec a ; 162
  246. dec a ; 164
  247. dec a ; 166
  248. dec a ; 168
  249. dec a ; 170
  250. dec a ; 172
  251. sub 144 ; 155
  252. ret
  253. org U*257+0700h
  254. dec a ; 13
  255. dec a ; 42
  256. dec a ; 69
  257. dec a ; 94
  258. dec a ; 117
  259. dec a ; 138
  260. dec a ; 157
  261. dec a ; 175
  262. dec a ; 177
  263. dec a ; 179
  264. dec a ; 181
  265. dec a ; 183
  266. dec a ; 185
  267. dec a ; 187
  268. dec a ; 189
  269. sub 128 ; 174
  270. ret
  271. org U*257+0800h
  272. dec a ; 15
  273. dec a ; 44
  274. dec a ; 71
  275. dec a ; 96
  276. dec a ; 119
  277. dec a ; 140
  278. dec a ; 159
  279. dec a ; 176
  280. dec a ; 192
  281. dec a ; 194
  282. dec a ; 196
  283. dec a ; 198
  284. dec a ; 200
  285. dec a ; 202
  286. dec a ; 204
  287. sub 112 ; 191
  288. ret
  289. org U*257+0900h
  290. dec a ; 17
  291. dec a ; 46
  292. dec a ; 73
  293. dec a ; 98
  294. dec a ; 121
  295. dec a ; 142
  296. dec a ; 161
  297. dec a ; 178
  298. dec a ; 193
  299. dec a ; 207
  300. dec a ; 209
  301. dec a ; 211
  302. dec a ; 213
  303. dec a ; 215
  304. dec a ; 217
  305. sub 96 ; 206
  306. ret
  307. org U*257+0A00h
  308. dec a ; 19
  309. dec a ; 48
  310. dec a ; 75
  311. dec a ; 100
  312. dec a ; 123
  313. dec a ; 144
  314. dec a ; 163
  315. dec a ; 180
  316. dec a ; 195
  317. dec a ; 208
  318. dec a ; 220
  319. dec a ; 222
  320. dec a ; 224
  321. dec a ; 226
  322. dec a ; 228
  323. sub 80 ; 219
  324. ret
  325. org U*257+0B00h
  326. dec a ; 21
  327. dec a ; 50
  328. dec a ; 77
  329. dec a ; 102
  330. dec a ; 125
  331. dec a ; 146
  332. dec a ; 165
  333. dec a ; 182
  334. dec a ; 197
  335. dec a ; 210
  336. dec a ; 221
  337. dec a ; 231
  338. dec a ; 233
  339. dec a ; 235
  340. dec a ; 237
  341. sub 64 ; 230
  342. ret
  343. org U*257+0C00h
  344. dec a ; 23
  345. dec a ; 52
  346. dec a ; 79
  347. dec a ; 104
  348. dec a ; 127
  349. dec a ; 148
  350. dec a ; 167
  351. dec a ; 184
  352. dec a ; 199
  353. dec a ; 212
  354. dec a ; 223
  355. dec a ; 232
  356. dec a ; 240
  357. dec a ; 242
  358. dec a ; 244
  359. sub 48 ; 239
  360. ret
  361. org U*257+0D00h
  362. dec a ; 25
  363. dec a ; 54
  364. dec a ; 81
  365. dec a ; 106
  366. dec a ; 129
  367. dec a ; 150
  368. dec a ; 169
  369. dec a ; 186
  370. dec a ; 201
  371. dec a ; 214
  372. dec a ; 225
  373. dec a ; 234
  374. dec a ; 241
  375. dec a ; 247
  376. dec a ; 249
  377. sub 32 ; 246
  378. ret
  379. org U*257+0E00h
  380. dec a ; 27
  381. dec a ; 56
  382. dec a ; 83
  383. dec a ; 108
  384. dec a ; 131
  385. dec a ; 152
  386. dec a ; 171
  387. dec a ; 188
  388. dec a ; 203
  389. dec a ; 216
  390. dec a ; 227
  391. dec a ; 236
  392. dec a ; 243
  393. dec a ; 248
  394. dec a ; 252
  395. sub 16 ; 251
  396. ret
  397. org U*257+0F00h
  398. dec a ; 29
  399. dec a ; 58
  400. dec a ; 85
  401. dec a ; 110
  402. dec a ; 133
  403. dec a ; 154
  404. dec a ; 173
  405. dec a ; 190
  406. dec a ; 205
  407. dec a ; 218
  408. dec a ; 229
  409. dec a ; 238
  410. dec a ; 245
  411. dec a ; 250
  412. dec a ; 253
  413. ret ; 254