Mirror of CollapseOS
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

360 satır
6.5KB

  1. ( Z80 assembler )
  2. ( Splits word into msb/lsb, lsb being on TOS )
  3. : SPLITB
  4. 256 /MOD SWAP
  5. ;
  6. ( H@ offset at which we consider our PC 0. Used to compute
  7. PC. To have a proper PC, call "H@ ORG !" at the beginning
  8. of your assembly process. )
  9. (sysv) ORG
  10. : PC H@ ORG @ - ;
  11. ( A, spits an assembled byte, A,, spits an assembled word
  12. Both increase PC. To debug, change C, to .X )
  13. : A, C, ;
  14. : A,, SPLITB A, A, ;
  15. ( Labels are a convenient way of managing relative jump
  16. calculations. Backward labels are easy. It is only a matter
  17. or recording "HERE" and do subtractions. Forward labels
  18. record the place where we should write the offset, and then
  19. when we get to that point later on, the label records the
  20. offset there.
  21. To avoid using dict memory in compilation targets, we
  22. pre-declare label variables here, which means we have a
  23. limited number of it. For now, 6 ought to be enough. )
  24. (sysv) L1
  25. (sysv) L2
  26. (sysv) L3
  27. (sysv) L4
  28. (sysv) L5
  29. (sysv) L6
  30. ( There are 2 label types: backward and forward. For each
  31. type, there are two actions: set and write. Setting a label
  32. is declaring where it is. It has to be performed at the
  33. label's destination. Writing a label is writing its offset
  34. difference to the binary result. It has to be done right
  35. after a relative jump operation. Yes, labels are only for
  36. relative jumps.
  37. For backward labels, set happens before write. For forward
  38. labels, write happen before set. The write operation writes
  39. a dummy placeholder, and then the set operation writes the
  40. offset at that placeholder's address.
  41. Variable actions are expected to be called with labels in
  42. front of them. Example, "L2 FSET"
  43. About that "1 -": z80 relative jumps record "e-2", that is,
  44. the offset that *counts the 2 bytes of the jump itself*.
  45. Because we set the label *after* the jump OP1 itself, that's
  46. 1 byte that is taken care of. We still need to adjust by
  47. another byte before writing the offset.
  48. )
  49. : BSET PC SWAP ! ;
  50. : BWR @ PC - 1 - A, ;
  51. ( same as BSET, but we need to write a placeholder )
  52. : FWR BSET 0 A, ;
  53. : FSET
  54. @ DUP PC ( l l pc )
  55. -^ 1 - ( l off )
  56. ( warning: l is a PC offset, not a mem addr! )
  57. SWAP ORG @ + ( off addr )
  58. C!
  59. ;
  60. ( "r" register constants )
  61. 7 CONSTANT A
  62. 0 CONSTANT B
  63. 1 CONSTANT C
  64. 2 CONSTANT D
  65. 3 CONSTANT E
  66. 4 CONSTANT H
  67. 5 CONSTANT L
  68. 6 CONSTANT (HL)
  69. ( "ss" register constants )
  70. 0 CONSTANT BC
  71. 1 CONSTANT DE
  72. 2 CONSTANT HL
  73. 3 CONSTANT AF
  74. 3 CONSTANT SP
  75. ( "cc" condition constants )
  76. 0 CONSTANT CNZ
  77. 1 CONSTANT CZ
  78. 2 CONSTANT CNC
  79. 3 CONSTANT CC
  80. 4 CONSTANT CPO
  81. 5 CONSTANT CPE
  82. 6 CONSTANT CP
  83. 7 CONSTANT CM
  84. ( As a general rule, IX and IY are equivalent to spitting an
  85. extra 0xdd / 0xfd and then spit the equivalent of HL )
  86. : IX 0xdd A, HL ;
  87. : IY 0xfd A, HL ;
  88. : _ix+- 0xff AND 0xdd A, (HL) ;
  89. : _iy+- 0xff AND 0xfd A, (HL) ;
  90. : IX+ _ix+- ;
  91. : IX- 0 -^ _ix+- ;
  92. : IY+ _iy+- ;
  93. : IY- 0 -^ _iy+- ;
  94. : <<3 8 * ;
  95. : <<4 16 * ;
  96. ( -- )
  97. : OP1 CREATE C, DOES> C@ A, ;
  98. 0xeb OP1 EXDEHL,
  99. 0xd9 OP1 EXX,
  100. 0x76 OP1 HALT,
  101. 0xe9 OP1 JP(HL),
  102. 0x12 OP1 LD(DE)A,
  103. 0x1a OP1 LDA(DE),
  104. 0x00 OP1 NOP,
  105. 0xc9 OP1 RET,
  106. 0x17 OP1 RLA,
  107. 0x07 OP1 RLCA,
  108. 0x1f OP1 RRA,
  109. 0x0f OP1 RRCA,
  110. 0x37 OP1 SCF,
  111. ( Relative jumps are a bit special. They're supposed to take
  112. an argument, but they don't take it so they can work with
  113. the label system. Therefore, relative jumps are an OP1 but
  114. when you use them, you're expected to write the offset
  115. afterwards yourself. )
  116. 0x18 OP1 JR,
  117. 0x38 OP1 JRC,
  118. 0x30 OP1 JRNC,
  119. 0x28 OP1 JRZ,
  120. 0x20 OP1 JRNZ,
  121. 0x10 OP1 DJNZ,
  122. ( r -- )
  123. : OP1r
  124. CREATE C,
  125. DOES>
  126. C@ ( r op )
  127. SWAP ( op r )
  128. <<3 ( op r<<3 )
  129. OR A,
  130. ;
  131. 0x04 OP1r INCr,
  132. 0x05 OP1r DECr,
  133. ( also works for cc )
  134. 0xc0 OP1r RETcc,
  135. ( r -- )
  136. : OP1r0
  137. CREATE C,
  138. DOES>
  139. C@ ( r op )
  140. OR A,
  141. ;
  142. 0x80 OP1r0 ADDr,
  143. 0x88 OP1r0 ADCr,
  144. 0xa0 OP1r0 ANDr,
  145. 0xb8 OP1r0 CPr,
  146. 0xb0 OP1r0 ORr,
  147. 0x90 OP1r0 SUBr,
  148. 0x98 OP1r0 SBCr,
  149. 0xa8 OP1r0 XORr,
  150. ( qq -- also works for ss )
  151. : OP1qq
  152. CREATE C,
  153. DOES>
  154. C@ ( qq op )
  155. SWAP ( op qq )
  156. <<4 ( op qq<<4 )
  157. OR A,
  158. ;
  159. 0xc5 OP1qq PUSHqq,
  160. 0xc1 OP1qq POPqq,
  161. 0x03 OP1qq INCss,
  162. 0x0b OP1qq DECss,
  163. 0x09 OP1qq ADDHLss,
  164. : _1rr
  165. C@ ( rd rr op )
  166. ROT ( rr op rd )
  167. <<3 ( rr op rd<<3 )
  168. OR OR A,
  169. ;
  170. ( rd rr )
  171. : OP1rr
  172. CREATE C,
  173. DOES>
  174. _1rr
  175. ;
  176. 0x40 OP1rr LDrr,
  177. ( ixy+- HL rd )
  178. : LDIXYr,
  179. ( dd/fd has already been spit )
  180. LDrr, ( ixy+- )
  181. A,
  182. ;
  183. ( rd ixy+- HL )
  184. : LDrIXY,
  185. ROT ( ixy+- HL rd )
  186. SWAP ( ixy+- rd HL )
  187. LDIXYr,
  188. ;
  189. : OP2 CREATE , DOES> @ 256 /MOD A, A, ;
  190. 0xedb1 OP2 CPIR,
  191. 0xed44 OP2 NEG,
  192. ( n -- )
  193. : OP2n
  194. CREATE C,
  195. DOES>
  196. C@ A, A,
  197. ;
  198. 0xd3 OP2n OUTnA,
  199. 0xdb OP2n INAn,
  200. 0xc6 OP2n ADDn,
  201. 0xe6 OP2n ANDn,
  202. 0xf6 OP2n Orn,
  203. 0xd6 OP2n SUBn,
  204. ( r n -- )
  205. : OP2rn
  206. CREATE C,
  207. DOES>
  208. C@ ( r n op )
  209. ROT ( n op r )
  210. <<3 ( n op r<<3 )
  211. OR A, A,
  212. ;
  213. 0x06 OP2rn LDrn,
  214. ( b r -- )
  215. : OP2br
  216. CREATE C,
  217. DOES>
  218. 0xcb A,
  219. C@ ( b r op )
  220. ROT ( r op b )
  221. <<3 ( r op b<<3 )
  222. OR OR A,
  223. ;
  224. 0xc0 OP2br SETbr,
  225. 0x80 OP2br RESbr,
  226. 0x40 OP2br BITbr,
  227. ( bitwise rotation ops have a similar sig )
  228. ( r -- )
  229. : OProt
  230. CREATE C,
  231. DOES>
  232. 0xcb A,
  233. C@ ( r op )
  234. OR A,
  235. ;
  236. 0x10 OProt RLr,
  237. 0x00 OProt RLCr,
  238. 0x18 OProt RRr,
  239. 0x08 OProt RRCr,
  240. 0x20 OProt SLAr,
  241. 0x38 OProt SRLr,
  242. ( cell contains both bytes. MSB is spit as-is, LSB is ORed with r )
  243. ( r -- )
  244. : OP2r
  245. CREATE ,
  246. DOES>
  247. @ SPLITB SWAP ( r lsb msb )
  248. A, ( r lsb )
  249. SWAP <<3 ( lsb r<<3 )
  250. OR A,
  251. ;
  252. 0xed41 OP2r OUT(C)r,
  253. 0xed40 OP2r INr(C),
  254. ( ss -- )
  255. : OP2ss
  256. CREATE C,
  257. DOES>
  258. 0xed A,
  259. C@ SWAP ( op ss )
  260. <<4 ( op ss<< 4 )
  261. OR A,
  262. ;
  263. 0x4a OP2ss ADCHLss,
  264. 0x42 OP2ss SBCHLss,
  265. ( dd nn -- )
  266. : OP3ddnn
  267. CREATE C,
  268. DOES>
  269. C@ ( dd nn op )
  270. ROT ( nn op dd )
  271. <<4 ( nn op dd<<4 )
  272. OR A,
  273. A,,
  274. ;
  275. 0x01 OP3ddnn LDddnn,
  276. ( nn -- )
  277. : OP3nn
  278. CREATE C,
  279. DOES>
  280. C@ A,
  281. A,,
  282. ;
  283. 0xcd OP3nn CALLnn,
  284. 0xc3 OP3nn JPnn,
  285. 0x22 OP3nn LD(nn)HL,
  286. 0x2a OP3nn LDHL(nn),
  287. ( Specials )
  288. ( dd nn -- )
  289. : LDdd(nn),
  290. 0xed A,
  291. SWAP <<4 0x4b OR A,
  292. A,,
  293. ;
  294. ( nn dd -- )
  295. : LD(nn)dd,
  296. 0xed A,
  297. <<4 0x43 OR A,
  298. A,,
  299. ;
  300. : JP(IX), IX DROP JP(HL), ;
  301. : JP(IY), IY DROP JP(HL), ;
  302. ( 26 == next )
  303. : JPNEXT, 26 JPnn, ;
  304. : CODE
  305. ( same as CREATE, but with native word )
  306. (entry)
  307. ( 23 == nativeWord )
  308. 23 ,
  309. ;
  310. : ;CODE JPNEXT, ;
  311. ( Routines )
  312. ( 29 == chkPS )
  313. : chkPS, 29 CALLnn, ;