Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2959 lines
93KB

  1. ( ----- 000 )
  2. MASTER INDEX
  3. 005 Z80 assembler 030 8086 assembler
  4. 050 AVR assembler 70-99 unused
  5. 100 Block editor 120 Visual Editor
  6. 150 Remote Shell
  7. 160 AVR SPI programmer 165 Sega ROM signer
  8. 170-259 unused 260 Cross compilation
  9. 280 Z80 boot code 350 Core words
  10. 400 AT28 EEPROM driver 401 Grid subsystem
  11. 410 PS/2 keyboard subsystem 418 Z80 SPI Relay driver
  12. 420 SD Card subsystem 440 8086 boot code
  13. 470 Z80 TMS9918 driver
  14. 480-519 unused 520 Fonts
  15. ( ----- 005 )
  16. ( Z80 Assembler
  17. 006 Variables & consts
  18. 007 Utils 008 OP1
  19. 010 OP1r 012 OP1d
  20. 013 OP1rr 015 OP2
  21. 016 OP2i 017 OP2ri
  22. 018 OP2br 019 OProt
  23. 020 OP2r 021 OP2d
  24. 022 OP3di 023 OP3i
  25. 024 Specials 025 Flow
  26. 028 Macros )
  27. 1 23 LOADR+
  28. ( ----- 006 )
  29. CREATE ORG 0 ,
  30. CREATE BIN( 0 ,
  31. VARIABLE L1 VARIABLE L2 VARIABLE L3 VARIABLE L4
  32. : A 7 ; : B 0 ; : C 1 ; : D 2 ;
  33. : E 3 ; : H 4 ; : L 5 ; : (HL) 6 ;
  34. : BC 0 ; : DE 1 ; : HL 2 ; : AF 3 ; : SP AF ;
  35. : CNZ 0 ; : CZ 1 ; : CNC 2 ; : CC 3 ;
  36. : CPO 4 ; : CPE 5 ; : CP 6 ; : CM 7 ;
  37. ( ----- 007 )
  38. : PC H@ ORG @ - BIN( @ + ;
  39. : <<3 3 LSHIFT ; : <<4 4 LSHIFT ;
  40. ( As a general rule, IX and IY are equivalent to spitting an
  41. extra 0xdd / 0xfd and then spit the equivalent of HL )
  42. : IX 0xdd C, HL ; : IY 0xfd C, HL ;
  43. : _ix+- 0xff AND 0xdd C, (HL) ;
  44. : _iy+- 0xff AND 0xfd C, (HL) ;
  45. : IX+ _ix+- ; : IX- 0 -^ _ix+- ;
  46. : IY+ _iy+- ; : IY- 0 -^ _iy+- ;
  47. ( ----- 008 )
  48. : OP1 CREATE C, DOES> C@ C, ;
  49. 0xf3 OP1 DI, 0xfb OP1 EI,
  50. 0xeb OP1 EXDEHL, 0xd9 OP1 EXX,
  51. 0x08 OP1 EXAFAF', 0xe3 OP1 EX(SP)HL,
  52. 0x76 OP1 HALT, 0xe9 OP1 JP(HL),
  53. 0x12 OP1 LD(DE)A, 0x1a OP1 LDA(DE),
  54. 0x02 OP1 LD(BC)A, 0x0a OP1 LDA(BC),
  55. 0x00 OP1 NOP, 0xc9 OP1 RET,
  56. 0x17 OP1 RLA, 0x07 OP1 RLCA,
  57. 0x1f OP1 RRA, 0x0f OP1 RRCA,
  58. 0x37 OP1 SCF,
  59. ( ----- 009 )
  60. ( Relative jumps are a bit special. They're supposed to take
  61. an argument, but they don't take it so they can work with
  62. the label system. Therefore, relative jumps are an OP1 but
  63. when you use them, you're expected to write the offset
  64. afterwards yourself. )
  65. 0x18 OP1 JR, 0x10 OP1 DJNZ,
  66. 0x38 OP1 JRC, 0x30 OP1 JRNC,
  67. 0x28 OP1 JRZ, 0x20 OP1 JRNZ,
  68. ( ----- 010 )
  69. ( r -- )
  70. : OP1r
  71. CREATE C,
  72. DOES>
  73. C@ ( r op )
  74. SWAP ( op r )
  75. <<3 ( op r<<3 )
  76. OR C,
  77. ;
  78. 0x04 OP1r INCr, 0x05 OP1r DECr,
  79. : INC(IXY+), INCr, C, ;
  80. : DEC(IXY+), DECr, C, ;
  81. ( also works for c )
  82. 0xc0 OP1r RETc,
  83. ( ----- 011 )
  84. : OP1r0 ( r -- )
  85. CREATE C, DOES>
  86. C@ ( r op ) OR C, ;
  87. 0x80 OP1r0 ADDr, 0x88 OP1r0 ADCr,
  88. 0xa0 OP1r0 ANDr, 0xb8 OP1r0 CPr,
  89. 0xb0 OP1r0 ORr, 0x90 OP1r0 SUBr,
  90. 0x98 OP1r0 SBCr, 0xa8 OP1r0 XORr,
  91. : CP(IXY+), CPr, C, ;
  92. ( ----- 012 )
  93. : OP1d
  94. CREATE C,
  95. DOES>
  96. C@ ( d op )
  97. SWAP ( op d )
  98. <<4 ( op d<<4 )
  99. OR C,
  100. ;
  101. 0xc5 OP1d PUSH, 0xc1 OP1d POP,
  102. 0x03 OP1d INCd, 0x0b OP1d DECd,
  103. 0x09 OP1d ADDHLd,
  104. : ADDIXd, 0xdd C, ADDHLd, ; : ADDIXIX, HL ADDIXd, ;
  105. : ADDIYd, 0xfd C, ADDHLd, ; : ADDIYIY, HL ADDIYd, ;
  106. ( ----- 013 )
  107. : _1rr
  108. C@ ( rd rr op )
  109. ROT ( rr op rd )
  110. <<3 ( rr op rd<<3 )
  111. OR OR C,
  112. ;
  113. ( rd rr )
  114. : OP1rr
  115. CREATE C,
  116. DOES>
  117. _1rr
  118. ;
  119. 0x40 OP1rr LDrr,
  120. ( ----- 014 )
  121. ( ixy+- HL rd )
  122. : LDIXYr,
  123. ( dd/fd has already been spit )
  124. LDrr, ( ixy+- )
  125. C,
  126. ;
  127. ( rd ixy+- HL )
  128. : LDrIXY,
  129. ROT ( ixy+- HL rd )
  130. SWAP ( ixy+- rd HL )
  131. LDIXYr,
  132. ;
  133. ( ----- 015 )
  134. : OP2 CREATE , DOES> @ |M C, C, ;
  135. 0xeda1 OP2 CPI, 0xedb1 OP2 CPIR,
  136. 0xeda9 OP2 CPD, 0xedb9 OP2 CPDR,
  137. 0xed46 OP2 IM0, 0xed56 OP2 IM1,
  138. 0xed5e OP2 IM2,
  139. 0xeda0 OP2 LDI, 0xedb0 OP2 LDIR,
  140. 0xeda8 OP2 LDD, 0xedb8 OP2 LDDR,
  141. 0xed44 OP2 NEG,
  142. 0xed4d OP2 RETI, 0xed45 OP2 RETN,
  143. ( ----- 016 )
  144. : OP2i ( i -- )
  145. CREATE C,
  146. DOES>
  147. C@ C, C,
  148. ;
  149. 0xd3 OP2i OUTiA,
  150. 0xdb OP2i INAi,
  151. 0xc6 OP2i ADDi,
  152. 0xe6 OP2i ANDi,
  153. 0xf6 OP2i ORi,
  154. 0xd6 OP2i SUBi,
  155. 0xee OP2i XORi,
  156. 0xfe OP2i CPi,
  157. ( ----- 017 )
  158. : OP2ri ( r i -- )
  159. CREATE C,
  160. DOES>
  161. C@ ( r i op )
  162. ROT ( i op r )
  163. <<3 ( i op r<<3 )
  164. OR C, C,
  165. ;
  166. 0x06 OP2ri LDri,
  167. ( ----- 018 )
  168. ( b r -- )
  169. : OP2br
  170. CREATE C,
  171. DOES>
  172. 0xcb C,
  173. C@ ( b r op )
  174. ROT ( r op b )
  175. <<3 ( r op b<<3 )
  176. OR OR C,
  177. ;
  178. 0xc0 OP2br SET,
  179. 0x80 OP2br RES,
  180. 0x40 OP2br BIT,
  181. ( ----- 019 )
  182. ( bitwise rotation ops have a similar sig )
  183. : OProt ( r -- )
  184. CREATE C,
  185. DOES>
  186. 0xcb C,
  187. C@ ( r op )
  188. OR C,
  189. ;
  190. 0x10 OProt RL,
  191. 0x00 OProt RLC,
  192. 0x18 OProt RR,
  193. 0x08 OProt RRC,
  194. 0x20 OProt SLA,
  195. 0x38 OProt SRL,
  196. ( ----- 020 )
  197. ( cell contains both bytes. MSB is spit as-is, LSB is ORed
  198. with r )
  199. ( r -- )
  200. : OP2r
  201. CREATE ,
  202. DOES>
  203. @ |M ( r lsb msb )
  204. C, ( r lsb )
  205. SWAP <<3 ( lsb r<<3 )
  206. OR C,
  207. ;
  208. 0xed41 OP2r OUT(C)r,
  209. 0xed40 OP2r INr(C),
  210. ( ----- 021 )
  211. : OP2d ( d -- )
  212. CREATE C,
  213. DOES>
  214. 0xed C,
  215. C@ SWAP ( op d )
  216. <<4 ( op d<< 4 )
  217. OR C,
  218. ;
  219. 0x4a OP2d ADCHLd,
  220. 0x42 OP2d SBCHLd,
  221. ( ----- 022 )
  222. ( d i -- )
  223. : OP3di
  224. CREATE C,
  225. DOES>
  226. C@ ( d n op )
  227. ROT ( n op d )
  228. <<4 ( n op d<<4 )
  229. OR C, ,
  230. ;
  231. 0x01 OP3di LDdi,
  232. ( ----- 023 )
  233. ( i -- )
  234. : OP3i
  235. CREATE C,
  236. DOES>
  237. C@ C, ,
  238. ;
  239. 0xcd OP3i CALL,
  240. 0xc3 OP3i JP,
  241. 0x22 OP3i LD(i)HL, 0x2a OP3i LDHL(i),
  242. 0x32 OP3i LD(i)A, 0x3a OP3i LDA(i),
  243. ( ----- 024 )
  244. : LDd(i), ( d i -- )
  245. 0xed C,
  246. SWAP <<4 0x4b OR C, ,
  247. ;
  248. : LD(i)d, ( i d -- )
  249. 0xed C,
  250. <<4 0x43 OR C, ,
  251. ;
  252. : RST, 0xc7 OR C, ;
  253. : JP(IX), IX DROP JP(HL), ;
  254. : JP(IY), IY DROP JP(HL), ;
  255. ( ----- 025 )
  256. : JPc, SWAP <<3 0xc2 OR C, , ;
  257. : BCALL, BIN( @ + CALL, ;
  258. : BJP, BIN( @ + JP, ;
  259. : BJPc, BIN( @ + JPc, ;
  260. CREATE lblchkPS 0 ,
  261. : chkPS, lblchkPS @ CALL, ; ( chkPS, B305 )
  262. CREATE lblnext 0 , ( stable ABI until set in B300 )
  263. : JPNEXT, lblnext @ ?DUP IF JP, ELSE 0x1a BJP, THEN ;
  264. : CODE ( same as CREATE, but with native word )
  265. (entry) 0 C, ( 0 == native ) ;
  266. : ;CODE JPNEXT, ;
  267. ( ----- 026 )
  268. ( Place BEGIN, where you want to jump back and AGAIN after
  269. a relative jump operator. Just like BSET and BWR. )
  270. : BEGIN,
  271. PC DUP 0x8000 AND IF ABORT" PC must be < 0x8000" THEN ;
  272. : BSET BEGIN, SWAP ! ;
  273. ( same as BSET, but we need to write a placeholder )
  274. : FJR, BEGIN, 0 C, ;
  275. : IFZ, JRNZ, FJR, ;
  276. : IFNZ, JRZ, FJR, ;
  277. : IFC, JRNC, FJR, ;
  278. : IFNC, JRC, FJR, ;
  279. : THEN,
  280. DUP PC ( l l pc ) -^ 1- ( l off )
  281. ( warning: l is a PC offset, not a mem addr! )
  282. SWAP ORG @ + BIN( @ - ( off addr ) C! ;
  283. : ELSE, JR, FJR, SWAP THEN, ;
  284. ( ----- 027 )
  285. : FWR BSET 0 C, ;
  286. : FSET @ THEN, ;
  287. : BREAK, FJR, 0x8000 OR ;
  288. : BREAK?, DUP 0x8000 AND IF
  289. 0x7fff AND 1 ALLOT THEN, -1 ALLOT
  290. THEN ;
  291. : AGAIN, BREAK?, PC - 1- C, ;
  292. : BWR @ AGAIN, ;
  293. ( ----- 028 )
  294. ( Macros )
  295. ( clear carry + SBC )
  296. : SUBHLd, A ORr, SBCHLd, ;
  297. : PUSH0, DE 0 LDdi, DE PUSH, ;
  298. : PUSH1, DE 1 LDdi, DE PUSH, ;
  299. : PUSHZ, DE 0 LDdi, IFZ, DE INCd, THEN, DE PUSH, ;
  300. : PUSHA, D 0 LDri, E A LDrr, DE PUSH, ;
  301. : HLZ, A H LDrr, L ORr, ;
  302. : DEZ, A D LDrr, E ORr, ;
  303. : LDDE(HL), E (HL) LDrr, HL INCd, D (HL) LDrr, ;
  304. : OUTHL, DUP A H LDrr, OUTiA, A L LDrr, OUTiA, ;
  305. : OUTDE, DUP A D LDrr, OUTiA, A E LDrr, OUTiA, ;
  306. ( ----- 030 )
  307. ( 8086 assembler. See doc/asm.txt )
  308. 1 13 LOADR+
  309. ( ----- 031 )
  310. VARIABLE ORG
  311. CREATE BIN( 0 , : BIN(+ BIN( @ + ;
  312. VARIABLE L1 VARIABLE L2 VARIABLE L3 VARIABLE L4
  313. : AL 0 ; : CL 1 ; : DL 2 ; : BL 3 ;
  314. : AH 4 ; : CH 5 ; : DH 6 ; : BH 7 ;
  315. : AX 0 ; : CX 1 ; : DX 2 ; : BX 3 ;
  316. : SP 4 ; : BP 5 ; : SI 6 ; : DI 7 ;
  317. : ES 0 ; : CS 1 ; : SS 2 ; : DS 3 ;
  318. : [BX+SI] 0 ; : [BX+DI] 1 ; : [BP+SI] 2 ; : [BP+DI] 3 ;
  319. : [SI] 4 ; : [DI] 5 ; : [BP] 6 ; : [BX] 7 ;
  320. : <<3 3 LSHIFT ;
  321. ( ----- 032 )
  322. : PC H@ ORG @ - BIN( @ + ;
  323. ( ----- 033 )
  324. : OP1 CREATE C, DOES> C@ C, ;
  325. 0xc3 OP1 RET, 0xfa OP1 CLI, 0xfb OP1 STI,
  326. 0xf4 OP1 HLT, 0xfc OP1 CLD, 0xfd OP1 STD,
  327. 0x90 OP1 NOP, 0x98 OP1 CBW,
  328. 0xf3 OP1 REPZ, 0xf2 OP1 REPNZ, 0xac OP1 LODSB,
  329. 0xad OP1 LODSW, 0xa6 OP1 CMPSB, 0xa7 OP1 CMPSW,
  330. 0xa4 OP1 MOVSB, 0xa5 OP1 MOVSW, 0xae OP1 SCASB,
  331. 0xaf OP1 SCASW, 0xaa OP1 STOSB, 0xab OP1 STOSW,
  332. ( no argument, jumps with relative addrs are special )
  333. 0xeb OP1 JMPs, 0xe9 OP1 JMPn, 0x74 OP1 JZ,
  334. 0x75 OP1 JNZ, 0x72 OP1 JC, 0x73 OP1 JNC,
  335. 0xe8 OP1 CALL,
  336. : OP1r CREATE C, DOES> C@ + C, ;
  337. 0x40 OP1r INCx, 0x48 OP1r DECx,
  338. 0x58 OP1r POPx, 0x50 OP1r PUSHx,
  339. ( ----- 034 )
  340. : OPr0 ( reg op ) CREATE C, C, DOES>
  341. C@+ C, C@ <<3 OR 0xc0 OR C, ;
  342. 0 0xd0 OPr0 ROLr1, 0 0xd1 OPr0 ROLx1, 4 0xf6 OPr0 MULr,
  343. 1 0xd0 OPr0 RORr1, 1 0xd1 OPr0 RORx1, 4 0xf7 OPr0 MULx,
  344. 4 0xd0 OPr0 SHLr1, 4 0xd1 OPr0 SHLx1, 6 0xf6 OPr0 DIVr,
  345. 5 0xd0 OPr0 SHRr1, 5 0xd1 OPr0 SHRx1, 6 0xf7 OPr0 DIVx,
  346. 0 0xd2 OPr0 ROLrCL, 0 0xd3 OPr0 ROLxCL, 1 0xfe OPr0 DECr,
  347. 1 0xd2 OPr0 RORrCL, 1 0xd3 OPr0 RORxCL, 0 0xfe OPr0 INCr,
  348. 4 0xd2 OPr0 SHLrCL, 4 0xd3 OPr0 SHLxCL,
  349. 5 0xd2 OPr0 SHRrCL, 5 0xd3 OPr0 SHRxCL,
  350. ( ----- 035 )
  351. : OPrr CREATE C, DOES> C@ C, <<3 OR 0xc0 OR C, ;
  352. 0x31 OPrr XORxx, 0x30 OPrr XORrr,
  353. 0x88 OPrr MOVrr, 0x89 OPrr MOVxx, 0x28 OPrr SUBrr,
  354. 0x29 OPrr SUBxx, 0x08 OPrr ORrr, 0x09 OPrr ORxx,
  355. 0x38 OPrr CMPrr, 0x39 OPrr CMPxx, 0x00 OPrr ADDrr,
  356. 0x01 OPrr ADDxx, 0x20 OPrr ANDrr, 0x21 OPrr ANDxx,
  357. ( ----- 036 )
  358. : OPm ( modrm op ) CREATE C, C, DOES> C@+ C, C@ OR C, ;
  359. 0 0xff OPm INC[w], 0 0xfe OPm INC[b],
  360. 0x8 0xff OPm DEC[w], 0x8 0xfe OPm DEC[b],
  361. 0x30 0xff OPm PUSH[w], 0 0x8f OPm POP[w],
  362. : OPm+ ( modrm op ) CREATE C, C, DOES>
  363. ( m off ) C@+ C, C@ ROT OR C, C, ;
  364. 0x40 0xff OPm+ INC[w]+, 0x40 0xfe OPm+ INC[b]+,
  365. 0x48 0xff OPm+ DEC[w]+, 0x48 0xfe OPm+ DEC[b]+,
  366. 0x70 0xff OPm+ PUSH[w]+, 0x40 0x8f OPm+ POP[w]+,
  367. ( ----- 037 )
  368. : OPrm CREATE C, DOES> C@ C, SWAP 3 LSHIFT OR C, ;
  369. 0x8a OPrm MOVr[], 0x8b OPrm MOVx[],
  370. 0x3a OPrm CMPr[], 0x3b OPrm CMPx[],
  371. : OPmr CREATE C, DOES> C@ C, 3 LSHIFT OR C, ;
  372. 0x88 OPmr MOV[]r, 0x89 OPmr MOV[]x,
  373. : OPrm+ ( r m off ) CREATE C, DOES>
  374. C@ C, ROT 3 LSHIFT ROT OR 0x40 OR C, C, ;
  375. 0x8a OPrm+ MOVr[]+, 0x8b OPrm+ MOVx[]+,
  376. 0x3a OPrm+ CMPr[]+, 0x3b OPrm+ CMPx[]+,
  377. : OPm+r ( m off r ) CREATE C, DOES>
  378. C@ C, 3 LSHIFT ROT OR 0x40 OR C, C, ;
  379. 0x88 OPm+r MOV[]+r, 0x89 OPm+r MOV[]+x,
  380. ( ----- 038 )
  381. : OPi CREATE C, DOES> C@ C, C, ;
  382. 0x04 OPi ADDALi, 0x24 OPi ANDALi, 0x2c OPi SUBALi,
  383. 0xcd OPi INT,
  384. : OPI CREATE C, DOES> C@ C, , ;
  385. 0x05 OPI ADDAXI, 0x25 OPI ANDAXI, 0x2d OPI SUBAXI,
  386. ( ----- 040 )
  387. : MOVri, SWAP 0xb0 OR C, C, ;
  388. : MOVxI, SWAP 0xb8 OR C, , ;
  389. : MOVsx, 0x8e C, SWAP <<3 OR 0xc0 OR C, ;
  390. : MOVrm, 0x8a C, SWAP <<3 0x6 OR C, , ;
  391. : MOVxm, 0x8b C, SWAP <<3 0x6 OR C, , ;
  392. : MOVmr, 0x88 C, <<3 0x6 OR C, , ;
  393. : MOVmx, 0x89 C, <<3 0x6 OR C, , ;
  394. : PUSHs, <<3 0x06 OR C, ; : POPs, <<3 0x07 OR C, ;
  395. : SUBxi, 0x83 C, SWAP 0xe8 OR C, C, ;
  396. : ADDxi, 0x83 C, SWAP 0xc0 OR C, C, ;
  397. : JMPr, 0xff C, 7 AND 0xe0 OR C, ;
  398. : JMPf, ( seg off ) 0xea C, |L C, C, , ;
  399. ( ----- 041 )
  400. ( Place BEGIN, where you want to jump back and AGAIN after
  401. a relative jump operator. Just like BSET and BWR. )
  402. : BEGIN, PC ;
  403. : BSET PC SWAP ! ;
  404. ( same as BSET, but we need to write a placeholder )
  405. : FJR, PC 0 C, ;
  406. : IFZ, JNZ, FJR, ;
  407. : IFNZ, JZ, FJR, ;
  408. : IFC, JNC, FJR, ;
  409. : IFNC, JC, FJR, ;
  410. : THEN,
  411. DUP PC ( l l pc )
  412. -^ 1- ( l off )
  413. ( warning: l is a PC offset, not a mem addr! )
  414. SWAP ORG @ + BIN( @ - ( off addr )
  415. C! ;
  416. ( ----- 042 )
  417. : FWRs BSET 0 C, ;
  418. : FSET @ THEN, ;
  419. ( TODO: add BREAK, )
  420. : RPCs, PC - 1- DUP 128 + 0xff > IF ABORT" PC ovfl" THEN C, ;
  421. : RPCn, PC - 2- , ;
  422. : AGAIN, ( BREAK?, ) RPCs, ;
  423. ( Use RPCx with appropriate JMP/CALL op. Example:
  424. JMPs, 0x42 RPCs, or CALL, 0x1234 RPCn, )
  425. ( ----- 043 )
  426. : PUSHZ, CX 0 MOVxI, IFZ, CX INCx, THEN, CX PUSHx, ;
  427. : CODE ( same as CREATE, but with native word )
  428. (entry) 0 ( native ) C, ;
  429. : ;CODE JMPn, 0x1a ( next ) RPCn, ;
  430. VARIABLE lblchkPS
  431. : chkPS, ( sz -- )
  432. CX SWAP 2 * MOVxI, CALL, lblchkPS @ RPCn, ;
  433. ( ----- 050 )
  434. 1 12 LOADR+
  435. ( ----- 051 )
  436. VARIABLE ORG
  437. VARIABLE L1 VARIABLE L2 VARIABLE L3 VARIABLE L4
  438. ( We divide by 2 because each PC represents a word. )
  439. : PC H@ ORG @ - 1 RSHIFT ;
  440. ( ----- 052 )
  441. : _oor ." arg out of range: " .X SPC> ." PC: " PC .X NL> ABORT ;
  442. : _r8c DUP 7 > IF _oor THEN ;
  443. : _r32c DUP 31 > IF _oor THEN ;
  444. : _r16+c _r32c DUP 16 < IF _oor THEN ;
  445. : _r64c DUP 63 > IF _oor THEN ;
  446. : _r256c DUP 255 > IF _oor THEN ;
  447. : _Rdp ( op rd -- op', place Rd ) 4 LSHIFT OR ;
  448. ( ----- 053 )
  449. ( 0000 000d dddd 0000 )
  450. : OPRd CREATE , DOES> @ SWAP _r32c _Rdp , ;
  451. 0b1001010000000101 OPRd ASR, 0b1001010000000000 OPRd COM,
  452. 0b1001010000001010 OPRd DEC, 0b1001010000000011 OPRd INC,
  453. 0b1001001000000110 OPRd LAC, 0b1001001000000101 OPRd LAS,
  454. 0b1001001000000111 OPRd LAT,
  455. 0b1001010000000110 OPRd LSR, 0b1001010000000001 OPRd NEG,
  456. 0b1001000000001111 OPRd POP, 0b1001001000001111 OPRd PUSH,
  457. 0b1001010000000111 OPRd ROR, 0b1001010000000010 OPRd SWAP,
  458. 0b1001001000000100 OPRd XCH,
  459. ( ----- 054 )
  460. ( 0000 00rd dddd rrrr )
  461. : OPRdRr CREATE C, DOES> C@ ( rd rr op )
  462. OVER _r32c 0x10 AND 3 RSHIFT OR ( rd rr op' )
  463. 8 LSHIFT OR 0xff0f AND ( rd op' )
  464. SWAP _r32c _Rdp , ;
  465. 0x1c OPRdRr ADC, 0x0c OPRdRr ADD, 0x20 OPRdRr AND,
  466. 0x14 OPRdRr CP, 0x04 OPRdRr CPC, 0x10 OPRdRr CPSE,
  467. 0x24 OPRdRr EOR, 0x2c OPRdRr MOV, 0x9c OPRdRr MUL,
  468. 0x28 OPRdRr OR, 0x08 OPRdRr SBC, 0x18 OPRdRr SUB,
  469. ( 0000 0AAd dddd AAAA )
  470. : OPRdA CREATE C, DOES> C@ ( rd A op )
  471. OVER _r64c 0x30 AND 3 RSHIFT OR ( rd A op' )
  472. 8 LSHIFT OR 0xff0f AND ( rd op' ) SWAP _r32c _Rdp , ;
  473. 0xb0 OPRdA IN, 0xb8 OPRdA _ : OUT, SWAP _ ;
  474. ( ----- 055 )
  475. ( 0000 KKKK dddd KKKK )
  476. : OPRdK CREATE C, DOES> C@ ( rd K op )
  477. OVER _r256c 0xf0 AND 4 RSHIFT OR ( rd K op' )
  478. ROT _r16+c 4 LSHIFT ROT 0x0f AND OR ( op' rdK ) C, C, ;
  479. 0x70 OPRdK ANDI, 0x30 OPRdK CPI, 0xe0 OPRdK LDI,
  480. 0x60 OPRdK ORI, 0x40 OPRdK SBCI, 0x60 OPRdK SBR,
  481. 0x50 OPRdK SUBI,
  482. ( 0000 0000 AAAA Abbb )
  483. : OPAb CREATE C, DOES> C@ ( A b op )
  484. ROT _r32c 3 LSHIFT ROT _r8c OR C, C, ;
  485. 0x98 OPAb CBI, 0x9a OPAb SBI, 0x99 OPAb SBIC,
  486. 0x9b OPAb SBIS,
  487. ( ----- 056 )
  488. : OPNA CREATE , DOES> @ , ;
  489. 0x9598 OPNA BREAK, 0x9488 OPNA CLC, 0x94d8 OPNA CLH,
  490. 0x94f8 OPNA CLI, 0x94a8 OPNA CLN, 0x94c8 OPNA CLS,
  491. 0x94e8 OPNA CLT, 0x94b8 OPNA CLV, 0x9498 OPNA CLZ,
  492. 0x9419 OPNA EIJMP, 0x9509 OPNA ICALL, 0x9519 OPNA EICALL,
  493. 0x9409 OPNA IJMP, 0x0000 OPNA NOP, 0x9508 OPNA RET,
  494. 0x9518 OPNA RETI, 0x9408 OPNA SEC, 0x9458 OPNA SEH,
  495. 0x9478 OPNA SEI, 0x9428 OPNA SEN, 0x9448 OPNA SES,
  496. 0x9468 OPNA SET, 0x9438 OPNA SEV, 0x9418 OPNA SEZ,
  497. 0x9588 OPNA SLEEP, 0x95a8 OPNA WDR,
  498. ( ----- 057 )
  499. ( 0000 0000 0sss 0000 )
  500. : OPb CREATE , DOES> @ ( b op )
  501. SWAP _r8c _Rdp , ;
  502. 0b1001010010001000 OPb BCLR, 0b1001010000001000 OPb BSET,
  503. ( 0000 000d dddd 0bbb )
  504. : OPRdb CREATE , DOES> @ ( rd b op )
  505. ROT _r32c _Rdp SWAP _r8c OR , ;
  506. 0b1111100000000000 OPRdb BLD, 0b1111101000000000 OPRdb BST,
  507. 0b1111110000000000 OPRdb SBRC, 0b1111111000000000 OPRdb SBRS,
  508. ( special cases )
  509. : CLR, DUP EOR, ; : TST, DUP AND, ; : LSL, DUP ADD, ;
  510. ( ----- 058 )
  511. ( a -- k12, absolute addr a, relative to PC in a k12 addr )
  512. : _r7ffc DUP 0x7ff > IF _oor THEN ;
  513. : _raddr12
  514. PC - DUP 0< IF 0x800 + _r7ffc 0x800 OR ELSE _r7ffc THEN ;
  515. : RJMP _raddr12 0xc000 OR ;
  516. : RCALL _raddr12 0xd000 OR ;
  517. : RJMP, RJMP , ; : RCALL, RCALL , ;
  518. ( ----- 059 )
  519. ( a -- k7, absolute addr a, relative to PC in a k7 addr )
  520. : _r3fc DUP 0x3f > IF _oor THEN ;
  521. : _raddr7
  522. PC - DUP 0< IF 0x40 + _r3fc 0x40 OR ELSE _r3fc THEN ;
  523. : _brbx ( a b op -- a ) OR SWAP _raddr7 3 LSHIFT OR ;
  524. : BRBC 0xf400 _brbx ; : BRBS 0xf000 _brbx ; : BRCC 0 BRBC ;
  525. : BRCS 0 BRBS ; : BREQ 1 BRBS ; : BRNE 1 BRBC ; : BRGE 4 BRBC ;
  526. : BRHC 5 BRBC ; : BRHS 5 BRBS ; : BRID 7 BRBC ; : BRIE 7 BRBS ;
  527. : BRLO BRCS ; : BRLT 4 BRBS ; : BRMI 2 BRBS ; : BRPL 2 BRBC ;
  528. : BRSH BRCC ; : BRTC 6 BRBC ; : BRTS 6 BRBS ; : BRVC 3 BRBC ;
  529. : BRVS 3 BRBS ;
  530. ( ----- 060 )
  531. 0b11100 CONSTANT X 0b01000 CONSTANT Y 0b00000 CONSTANT Z
  532. 0b11101 CONSTANT X+ 0b11001 CONSTANT Y+ 0b10001 CONSTANT Z+
  533. 0b11110 CONSTANT -X 0b11010 CONSTANT -Y 0b10010 CONSTANT -Z
  534. : _ldst ( Rd XYZ op ) SWAP DUP 0x10 AND 8 LSHIFT SWAP 0xf AND
  535. OR OR ( Rd op' ) SWAP _Rdp , ;
  536. : LD, 0x8000 _ldst ; : ST, SWAP 0x8200 _ldst ;
  537. ( ----- 061 )
  538. ( L1 LBL! .. L1 ' RJMP LBL, )
  539. : LBL! ( l -- ) PC SWAP ! ;
  540. : LBL, ( l op -- ) SWAP @ 1- SWAP EXECUTE , ;
  541. : SKIP, PC 0 , ;
  542. : TO, ( opw pc ) ( TODO: use !* instead of ! )
  543. ( warning: pc is a PC offset, not a mem addr! )
  544. 2 * ORG @ + PC 1- H@ ( opw addr tgt hbkp )
  545. ROT HERE ! ( opw tgt hbkp ) SWAP ROT EXECUTE H@ ! ( hbkp )
  546. HERE ! ;
  547. ( L1 FLBL, .. L1 ' RJMP FLBL! )
  548. : FLBL, ( l -- ) LBL! 0 , ;
  549. : FLBL! ( l opw -- ) SWAP @ TO, ;
  550. : BEGIN, PC ; : AGAIN?, ( op ) SWAP 1- SWAP EXECUTE , ;
  551. : AGAIN, ['] RJMP AGAIN?, ;
  552. : IF, ['] BREQ SKIP, ; : THEN, TO, ;
  553. ( ----- 062 )
  554. ( Constant common to all AVR models )
  555. : R0 0 ; : R1 1 ; : R2 2 ; : R3 3 ; : R4 4 ; : R5 5 ; : R6 6 ;
  556. : R7 7 ; : R8 8 ; : R9 9 ; : R10 10 ; : R11 11 ; : R12 12 ;
  557. : R13 13 ; : R14 14 ; : R15 15 ; : R16 16 ; : R17 17 ;
  558. : R18 18 ; : R19 19 ; : R20 20 ; : R21 21 ; : R22 22 ;
  559. : R24 24 ; : R25 25 ; : R26 26 ; : R27 27 ; : R28 28 ;
  560. : R29 29 ; : R30 30 ; : R31 31 ; : XL R26 ; : XH R27 ;
  561. : YL R28 ; : YH R29 ; : ZL R30 ; : ZH R31 ;
  562. ( ----- 065 )
  563. ( ATmega328P definitions ) : > CONSTANT ;
  564. 0xc6 > UDR0 0xc4 > UBRR0L 0xc5 > UBRR0H 0xc2 > UCSR0C
  565. 0xc1 > UCSR0B 0xc0 > UCSR0A 0xbd > TWAMR 0xbc > TWCR
  566. 0xbb > TWDR 0xba > TWAR 0xb9 > TWSR 0xb8 > TWBR 0xb6 > ASSR
  567. 0xb4 > OCR2B 0xb3 > OCR2A 0xb2 > TCNT2 0xb1 > TCCR2B
  568. 0xb0 > TCCR2A 0x8a > OCR1BL 0x8b > OCR1BH 0x88 > OCR1AL
  569. 0x89 > OCR1AH 0x86 > ICR1L 0x87 > ICR1H 0x84 > TCNT1L
  570. 0x85 > TCNT1H 0x82 > TCCR1C 0x81 > TCCR1B 0x80 > TCCR1A
  571. 0x7f > DIDR1 0x7e > DIDR0 0x7c > ADMUX 0x7b > ADCSRB
  572. 0x7a > ADCSRA 0x79 > ADCH 0x78 > ADCL 0x70 > TIMSK2
  573. 0x6f > TIMSK1 0x6e > TIMSK0 0x6c > PCMSK1 0x6d > PCMSK2
  574. 0x6b > PCMSK0 0x69 > EICRA 0x68 > PCICR 0x66 > OSCCAL
  575. 0x64 > PRR 0x61 > CLKPR 0x60 > WDTCSR 0x3f > SREG 0x3d > SPL
  576. 0x3e > SPH 0x37 > SPMCSR 0x35 > MCUCR 0x34 > MCUSR 0x33 > SMCR
  577. 0x30 > ACSR 0x2e > SPDR 0x2d > SPSR 0x2c > SPCR 0x2b > GPIOR2
  578. 0x2a > GPIOR1 0x28 > OCR0B 0x27 > OCR0A 0x26 > TCNT0 ( cont. )
  579. ( ----- 066 )
  580. ( cont. ) 0x25 > TCCR0B 0x24 > TCCR0A 0x23 > GTCCR
  581. 0x22 > EEARH 0x21 > EEARL 0x20 > EEDR 0x1f > EECR
  582. 0x1e > GPIOR0 0x1d > EIMSK 0x1c > EIFR 0x1b > PCIFR
  583. 0x17 > TIFR2 0x16 > TIFR1 0x15 > TIFR0 0x0b > PORTD 0x0a > DDRD
  584. 0x09 > PIND 0x08 > PORTC 0x07 > DDRC 0x06 > PINC 0x05 > PORTB
  585. 0x04 > DDRB 0x03 > PINB
  586. ( ----- 100 )
  587. Block editor
  588. This is an application to conveniently browse the contents of
  589. the disk blocks and edit them. You can load it with "105 LOAD".
  590. See doc/ed.txt
  591. ( ----- 105 )
  592. 1 7 LOADR+
  593. ( ----- 106 )
  594. CREATE ACC 0 ,
  595. : _LIST ." Block " DUP . NL> LIST ;
  596. : L BLK> @ _LIST ;
  597. : B BLK> @ 1- BLK@ L ;
  598. : N BLK> @ 1+ BLK@ L ;
  599. ( ----- 107 )
  600. ( Cursor position in buffer. EDPOS/64 is line number )
  601. CREATE EDPOS 0 ,
  602. CREATE IBUF 64 ALLOT0
  603. CREATE FBUF 64 ALLOT0
  604. : _cpos BLK( + ;
  605. : _lpos 64 * _cpos ;
  606. : _pln ( lineno -- )
  607. DUP _lpos DUP 64 + SWAP DO ( lno )
  608. I EDPOS @ _cpos = IF '^' EMIT THEN
  609. I C@ DUP SPC < IF DROP SPC THEN
  610. EMIT
  611. LOOP ( lno ) 1+ . ;
  612. : _zbuf 64 0 FILL ; ( buf -- )
  613. ( ----- 108 )
  614. : _type ( buf -- )
  615. C< DUP CR = IF 2DROP EXIT THEN SWAP DUP _zbuf ( c a )
  616. BEGIN ( c a ) C!+ C< TUCK 0x0d = UNTIL ( c a ) C! ;
  617. ( user-facing lines are 1-based )
  618. : T 1- DUP 64 * EDPOS ! _pln ;
  619. : P IBUF _type IBUF EDPOS @ _cpos 64 MOVE BLK!! ;
  620. : _mvln+ ( ln -- move ln 1 line down )
  621. DUP 14 > IF DROP EXIT THEN
  622. _lpos DUP 64 + 64 MOVE ;
  623. : _mvln- ( ln -- move ln 1 line up )
  624. DUP 14 > IF DROP 15 _lpos _zbuf
  625. ELSE 1+ _lpos DUP 64 - 64 MOVE THEN ;
  626. ( ----- 109 )
  627. : _U ( U without P, used in VE )
  628. 15 EDPOS @ 64 / - ?DUP IF
  629. 0 DO
  630. 14 I - _mvln+
  631. LOOP THEN ;
  632. : U _U P ;
  633. ( ----- 110 )
  634. : _F ( F without _type and _pln. used in VE )
  635. FBUF EDPOS @ _cpos 1+ ( a1 a2 )
  636. BEGIN
  637. C@+ ROT ( a2+1 c2 a1 ) C@+ ROT ( a2+1 a1+1 c1 c2 )
  638. = NOT IF DROP FBUF THEN ( a2 a1 )
  639. TUCK C@ CR = ( a1 a2 f1 )
  640. OVER BLK) = OR ( a1 a2 f1|f2 )
  641. UNTIL ( a1 a2 )
  642. DUP BLK) < IF BLK( - FBUF + -^ EDPOS ! ELSE DROP THEN ;
  643. : F FBUF _type _F EDPOS @ 64 / _pln ;
  644. ( ----- 111 )
  645. : _blen ( buf -- length of str in buf )
  646. DUP BEGIN C@+ SPC < UNTIL -^ 1- ;
  647. : _rbufsz ( size of linebuf to the right of curpos )
  648. EDPOS @ 64 MOD 63 -^ ;
  649. : _lnfix ( --, ensure no ctl chars in line before EDPOS )
  650. EDPOS @ DUP 0xffc0 AND 2DUP = IF 2DROP EXIT THEN DO
  651. I _cpos DUP C@ SPC < IF SPC SWAP C! ELSE DROP THEN LOOP ;
  652. : _i ( i without _pln and _type. used in VE )
  653. _rbufsz IBUF _blen 2DUP > IF
  654. _lnfix TUCK - ( ilen chars-to-move )
  655. SWAP EDPOS @ _cpos 2DUP + ( ctm ilen a a+ilen )
  656. 3 PICK MOVE- ( ctm ilen ) NIP ( ilen )
  657. ELSE DROP 1+ ( ilen becomes rbuffsize+1 ) THEN
  658. DUP IBUF EDPOS @ _cpos ROT MOVE ( ilen ) EDPOS +! BLK!! ;
  659. : i IBUF _type _i EDPOS @ 64 / _pln ;
  660. ( ----- 112 )
  661. : icpy ( n -- copy n chars from cursor to IBUF )
  662. IBUF _zbuf EDPOS @ _cpos IBUF ( n a buf ) ROT MOVE ;
  663. : _X ( n -- )
  664. DUP icpy EDPOS @ _cpos 2DUP + ( n a1 a1+n )
  665. SWAP _rbufsz MOVE ( n )
  666. ( get to next line - n )
  667. DUP EDPOS @ 0xffc0 AND 0x40 + -^ _cpos ( n a )
  668. SWAP 0 FILL BLK!! ;
  669. : X _X EDPOS @ 64 / _pln ;
  670. : _E FBUF _blen _X ;
  671. : E FBUF _blen X ;
  672. : Y FBUF _blen icpy ;
  673. ( ----- 120 )
  674. Visual Editor
  675. This editor, unlike the Block Editor (B100), is grid-based
  676. instead of being command-based. It requires the AT-XY, COLS
  677. and LINES words to be implemented. If you don't have those,
  678. use the Block Editor.
  679. It is loaded with "125 LOAD" and invoked with "VE". Note that
  680. this also fully loads the Block Editor.
  681. This editor uses 19 lines. The top line is the status line and
  682. it's followed by 2 lines showing the contents of IBUF and
  683. FBUF (see B100). There are then 16 contents lines. The contents
  684. shown is that of the currently selected block.
  685. (cont.)
  686. ( ----- 121 )
  687. The status line displays the active block number, then the
  688. "modifier" and then the cursor position. When the block is dir-
  689. ty, an "*" is displayed next. At the right corner, a mode letter
  690. can appear. 'R' for replace, 'I' for insert, 'F' for find.
  691. (cont.)
  692. ( ----- 122 )
  693. All keystrokes are directly interpreted by VE and have the
  694. effect described below.
  695. Pressing a 0-9 digit accumulates that digit into what is named
  696. the "modifier". That modifier affects the behavior of many
  697. keystrokes described below. The modifier starts at zero, but
  698. most commands interpret a zero as a 1 so that they can have an
  699. effect.
  700. 'G' selects the block specified by the modifier as the current
  701. block. Any change made to the previously selected block is
  702. saved beforehand.
  703. '[' and ']' advances the selected block by "modifier". 't' opens
  704. the previously opened block.
  705. (cont.)
  706. ( ----- 123 )
  707. 'h' and 'l' move the cursor by "modifier" characters. 'j' and
  708. 'k', by lines. 'g' moves to "modifier" line.
  709. 'H' goes to the beginning of the line, 'L' to the end.
  710. 'w' moves forward by "modifier" words. 'b' moves backward.
  711. 'W' moves to end-of-word. 'B', backwards.
  712. 'I', 'F', 'Y', 'X' and 'E' invoke the corresponding command
  713. 'o' inserts a blank line after the cursor. 'O', before.
  714. 'D' deletes "modifier" lines at the cursor. The first of those
  715. lines is copied to IBUF.
  716. (cont.)
  717. ( ----- 124 )
  718. 'f' puts the contents of your previous cursor movement into
  719. FBUF. If that movement was a forward movement, it brings the
  720. cursor back where it was. This allows for an efficient combi-
  721. nation of movements and 'E'. For example, if you want to delete
  722. the next word, you type 'w', then 'f', then check your FBUF to
  723. be sure, then press 'E'.
  724. 'R' goes into replace mode at current cursor position.
  725. Following keystrokes replace current character and advance
  726. cursor. Press return to return to normal mode.
  727. '@' re-reads current block even if it's dirty, thus undoing
  728. recent changes.
  729. ( ----- 125 )
  730. -20 LOAD+ ( B105, block editor )
  731. 1 7 LOADR+
  732. ( ----- 126 )
  733. CREATE CMD 2 C, '$' C, 0 C,
  734. CREATE PREVPOS 0 , CREATE PREVBLK 0 , CREATE xoff 0 ,
  735. : MIN ( n n - n ) 2DUP > IF SWAP THEN DROP ;
  736. : MAX ( n n - n ) 2DUP < IF SWAP THEN DROP ;
  737. : large? COLS 67 > ; : col- 67 COLS MIN -^ ;
  738. : width large? IF 64 ELSE COLS THEN ;
  739. : acc@ ACC @ 1 MAX ; : pos@ ( x y -- ) EDPOS @ 64 /MOD ;
  740. : num ACC @ SWAP _pdacc IF ACC ! ELSE DROP THEN ;
  741. : nspcs ( n -- , spit n space ) 0 DO SPC> LOOP ;
  742. : aty 0 SWAP AT-XY ;
  743. : clrscr COLS LINES * 0 DO SPC I CELL! LOOP ;
  744. : gutter ( ln n ) OVER + SWAP DO 67 I AT-XY '|' EMIT LOOP ;
  745. : status 0 aty ." BLK" SPC> BLK> ? SPC> ACC ?
  746. SPC> pos@ . ',' EMIT . xoff @ IF '>' EMIT THEN SPC>
  747. BLKDTY @ IF '*' EMIT THEN 4 nspcs ;
  748. : nums 17 1 DO 2 I + aty I . SPC> SPC> LOOP ;
  749. ( ----- 127 )
  750. : mode! ( c -- ) 4 col- CELL! ;
  751. : @emit C@ SPC MAX 0x7f MIN EMIT ;
  752. : contents
  753. 16 0 DO
  754. large? IF 3 ELSE 0 THEN I 3 + AT-XY
  755. 64 I * BLK( + ( lineaddr ) xoff @ + DUP width + SWAP
  756. DO I @emit LOOP LOOP
  757. large? IF 3 16 gutter THEN ;
  758. : selblk BLK> @ PREVBLK ! BLK@ contents ;
  759. : pos! ( newpos -- ) EDPOS @ PREVPOS !
  760. DUP 0< IF DROP 0 THEN 1023 MIN EDPOS ! ;
  761. : xoff? pos@ DROP ( x )
  762. xoff @ ?DUP IF < IF 0 xoff ! contents THEN ELSE
  763. width >= IF 64 COLS - xoff ! contents THEN THEN ;
  764. : setpos ( -- ) pos@ 3 + ( header ) SWAP ( y x ) xoff @ -
  765. large? IF 3 + ( gutter ) THEN SWAP AT-XY ;
  766. ( ----- 128 )
  767. : cmv ( n -- , char movement ) acc@ * EDPOS @ + pos! ;
  768. : buftype ( buf ln -- )
  769. 3 OVER AT-XY KEY DUP EMIT
  770. DUP SPC < IF 2DROP DROP EXIT THEN
  771. ( buf ln c ) 4 col- nspcs SWAP 4 SWAP AT-XY ( buf c )
  772. SWAP C!+ IN( _zbuf RDLN IN( SWAP 63 MOVE ;
  773. : bufp ( buf -- )
  774. DUP 3 col- + SWAP DO I @emit LOOP ;
  775. : bufs
  776. 1 aty ." I: " IBUF bufp
  777. 2 aty ." F: " FBUF bufp
  778. large? IF 0 3 gutter THEN ;
  779. ( ----- 129 )
  780. : $G ACC @ selblk ;
  781. : $[ BLK> @ acc@ - selblk ;
  782. : $] BLK> @ acc@ + selblk ;
  783. : $t PREVBLK @ selblk ;
  784. : $I 'I' mode! IBUF 1 buftype _i bufs contents SPC mode! ;
  785. : $F 'F' mode! FBUF 2 buftype _F bufs setpos SPC mode! ;
  786. : $Y Y bufs ;
  787. : $E _E bufs contents ;
  788. : $X acc@ _X bufs contents ;
  789. : $h -1 cmv ; : $l 1 cmv ; : $k -64 cmv ; : $j 64 cmv ;
  790. : $H EDPOS @ 0x3c0 AND pos! ;
  791. : $L EDPOS @ 0x3f OR pos! ;
  792. : $g ACC @ 1 MAX 1- 64 * pos! ;
  793. : $@ BLK> @ BLK@* 0 BLKDTY ! contents ;
  794. ( ----- 130 )
  795. : $w EDPOS @ BLK( + acc@ 0 DO
  796. BEGIN C@+ WS? UNTIL BEGIN C@+ WS? NOT UNTIL LOOP
  797. 1- BLK( - pos! ;
  798. : $W EDPOS @ BLK( + acc@ 0 DO
  799. 1+ BEGIN C@+ WS? NOT UNTIL BEGIN C@+ WS? UNTIL LOOP
  800. 2- BLK( - pos! ;
  801. : $b EDPOS @ BLK( + acc@ 0 DO
  802. 1- BEGIN C@- WS? NOT UNTIL BEGIN C@- WS? UNTIL LOOP
  803. 2+ BLK( - pos! ;
  804. : $B EDPOS @ BLK( + acc@ 0 DO
  805. BEGIN C@- WS? UNTIL BEGIN C@- WS? NOT UNTIL LOOP
  806. 1+ BLK( - pos! ;
  807. ( ----- 131 )
  808. : $f EDPOS @ PREVPOS @ 2DUP = IF 2DROP EXIT THEN
  809. 2DUP > IF DUP pos! SWAP THEN
  810. ( p1 p2, p1 < p2 ) OVER - 64 MIN ( pos len ) FBUF _zbuf
  811. SWAP _cpos FBUF ( len src dst ) ROT MOVE bufs ;
  812. : $R ( replace mode )
  813. 'R' mode!
  814. BEGIN setpos KEY DUP BS? IF -1 EDPOS +! DROP 0 THEN
  815. DUP SPC >= IF
  816. DUP EMIT EDPOS @ _cpos C! 1 EDPOS +! BLK!! 0
  817. THEN UNTIL SPC mode! contents ;
  818. : $O _U EDPOS @ 0x3c0 AND DUP pos! _cpos _zbuf BLK!! contents ;
  819. : $o EDPOS @ 0x3c0 < IF EDPOS @ 64 + EDPOS ! $O THEN ;
  820. : $D $H 64 icpy
  821. acc@ 0 DO 16 EDPOS @ 64 / DO I _mvln- LOOP LOOP
  822. BLK!! bufs contents ;
  823. ( ----- 132 )
  824. : UPPER DUP 'a' 'z' =><= IF 32 - THEN ;
  825. : handle ( c -- f )
  826. DUP '0' '9' =><= IF num 0 EXIT THEN
  827. DUP CMD 2+ C! CMD FIND IF EXECUTE ELSE DROP THEN
  828. 0 ACC ! UPPER 'Q' = ;
  829. : VE
  830. 1 XYMODE C! clrscr 0 ACC ! 0 PREVPOS ! nums bufs contents
  831. BEGIN xoff? status setpos KEY handle UNTIL
  832. 0 XYMODE C! 19 aty IN$ ;
  833. ( ----- 150 )
  834. ( Remote Shell )
  835. 0 :* rsh<? 0 :* rsh>
  836. : rsh BEGIN
  837. rsh<? IF
  838. DUP 4 ( EOT ) = IF DROP EXIT THEN EMIT THEN
  839. KEY? IF DUP 0x80 < IF rsh> ELSE DROP EXIT THEN THEN
  840. AGAIN ;
  841. ( ----- 160 )
  842. ( AVR Programmer, load range 160-163. doc/avr.txt )
  843. ( page size in words, 64 is default on atmega328P )
  844. CREATE aspfpgsz 64 ,
  845. VARIABLE aspprevx
  846. : _x ( a -- b ) DUP aspprevx ! (spix) ;
  847. : _xc ( a -- b ) DUP (spix) ( a b )
  848. DUP aspprevx @ = NOT IF ABORT" AVR err" THEN ( a b )
  849. SWAP aspprevx ! ( b ) ;
  850. : _cmd ( b4 b3 b2 b1 -- r4 ) _xc DROP _xc DROP _xc DROP _x ;
  851. : asprdy ( -- ) BEGIN 0 0 0 0xf0 _cmd 1 AND NOT UNTIL ;
  852. : asp$ ( spidevid -- )
  853. ( RESET pulse ) DUP (spie) 0 (spie) (spie)
  854. ( wait >20ms ) 220 TICKS
  855. ( enable prog ) 0xac (spix) DROP
  856. 0x53 _x DROP 0 _xc DROP 0 _x DROP ;
  857. : asperase 0 0 0x80 0xac _cmd asprdy ;
  858. ( ----- 161 )
  859. ( fuse access. read/write one byte at a time )
  860. : aspfl@ ( -- lfuse ) 0 0 0 0x50 _cmd ;
  861. : aspfh@ ( -- hfuse ) 0 0 0x08 0x58 _cmd ;
  862. : aspfe@ ( -- efuse ) 0 0 0x00 0x58 _cmd ;
  863. : aspfl! ( lfuse -- ) 0 0xa0 0xac _cmd ;
  864. : aspfh! ( hfuse -- ) 0 0xa8 0xac _cmd ;
  865. : aspfe! ( efuse -- ) 0 0xa4 0xac _cmd ;
  866. ( ----- 162 )
  867. : aspfb! ( n a --, write word n to flash buffer addr a )
  868. SWAP |L ( a hi lo ) ROT ( hi lo a )
  869. DUP ROT ( hi a a lo ) SWAP ( hi a lo a )
  870. 0 0x40 ( hi a lo a 0 0x40 ) _cmd DROP ( hi a )
  871. 0 0x48 _cmd DROP ;
  872. : aspfp! ( page --, write buffer to page )
  873. 0 SWAP aspfpgsz @ * |M ( 0 lsb msb )
  874. 0x4c _cmd DROP asprdy ;
  875. : aspf@ ( page a -- n, read word from flash )
  876. SWAP aspfpgsz @ * OR ( addr ) |M ( lsb msb )
  877. 2DUP 0 ROT> ( lsb msb 0 lsb msb )
  878. 0x20 _cmd ( lsb msb low )
  879. ROT> 0 ROT> ( low 0 lsb msb ) 0x28 _cmd 8 LSHIFT OR ;
  880. ( ----- 163 )
  881. : aspe@ ( addr -- byte, read from EEPROM )
  882. 0 SWAP |L ( 0 msb lsb )
  883. 0xa0 ( 0 msb lsb 0xa0 ) _cmd ;
  884. : aspe! ( byte addr --, write to EEPROM )
  885. |L ( b msb lsb )
  886. 0xc0 ( b msb lsb 0xc0 ) _cmd DROP asprdy ;
  887. ( ----- 165 )
  888. ( Sega ROM signer. See doc/sega.txt )
  889. : C!+^ ( a c -- a+1 ) OVER C! 1+ ;
  890. : segasig ( addr size -- )
  891. 0x2000 OVER LSHIFT ( a sz bytesz )
  892. ROT TUCK + 0x10 - ( sz a end )
  893. TUCK SWAP 0 ROT> ( sz end sum end a ) DO ( sz end sum )
  894. I C@ + LOOP ( sz end sum ) SWAP ( sz sum end )
  895. 'T' C!+^ 'M' C!+^ 'R' C!+^ SPC C!+^ 'S' C!+^
  896. 'E' C!+^ 'G' C!+^ 'A' C!+^ 0 C!+^ 0 C!+^
  897. ( sum's LSB ) OVER C!+^ ( MSB ) SWAP 8 RSHIFT OVER C! 1+
  898. ( sz end ) 0 C!+^ 0 C!+^ 0 C!+^ SWAP 0x4a + SWAP C! ;
  899. ( ----- 260 )
  900. Cross compilation program
  901. This programs allows cross compilation of boot binary and
  902. core. Run "262 LOAD" right before your cross compilation and
  903. then "270 LOAD" to apply xcomp overrides.
  904. This unit depends on a properly initialized z80a with ORG and
  905. BIN( set. That is how we determine compilation offsets.
  906. This redefines defining words to achieve cross compilation.
  907. The goal is two-fold:
  908. 1. Add an offset to all word references in definitions.
  909. 2. Don't shadow important words we need right now.
  910. (cont.)
  911. ( ----- 261 )
  912. Words overrides like ":", "IMMEDIATE" and "CODE" are not
  913. automatically shadowed to allow the harmless inclusion of
  914. this unit. This shadowing has to take place in your xcomp
  915. configuration.
  916. See /doc/cross.txt for details.
  917. ( ----- 262 )
  918. 1 3 LOADR+
  919. ( ----- 263 )
  920. CREATE XCURRENT 0 ,
  921. : XCON XCURRENT CURRENT* ! ; : XCOFF 0x02 RAM+ CURRENT* ! ;
  922. : (xentry) XCON (entry) XCOFF ; : XCREATE (xentry) 2 C, ;
  923. : X:** (xentry) 5 C, , ;
  924. : XCODE XCON CODE XCOFF ; : XIMM XCON IMMEDIATE XCOFF ;
  925. : _xapply ( a -- a-off )
  926. DUP ORG @ > IF ORG @ - BIN( @ + THEN ;
  927. : XFIND XCURRENT @ SWAP _find DROP _xapply ;
  928. : XLITN LIT" (n)" XFIND , , ;
  929. : X' XCON ' XCOFF ; : X'? XCON '? XCOFF ;
  930. : X['] XCON ' _xapply XLITN XCOFF ;
  931. : XCOMPILE XCON ' _xapply XLITN
  932. LIT" ," FIND DROP _xapply , XCOFF ;
  933. : X[COMPILE] XCON ' _xapply , XCOFF ;
  934. ( ----- 264 )
  935. : XDO LIT" 2>R" XFIND , H@ ;
  936. : XLOOP LIT" (loop)" XFIND , H@ - C, ;
  937. : XIF LIT" (?br)" XFIND , H@ 1 ALLOT ;
  938. : XELSE LIT" (br)" XFIND , 1 ALLOT [COMPILE] THEN H@ 1- ;
  939. : XAGAIN LIT" (br)" XFIND , H@ - C, ;
  940. : XUNTIL LIT" (?br)" XFIND , H@ - C, ;
  941. : XLIT"
  942. LIT" (s)" XFIND , H@ 0 C, ,"
  943. DUP H@ -^ 1- SWAP C!
  944. ;
  945. ( ----- 265 )
  946. : X:
  947. (xentry) 1 ( compiled ) C,
  948. BEGIN
  949. WORD DUP LIT" ;" S= IF
  950. DROP LIT" EXIT" XFIND , EXIT THEN
  951. XCURRENT @ SWAP ( xcur w ) _find ( a f )
  952. IF ( a )
  953. DUP IMMED? IF ABORT THEN
  954. _xapply ,
  955. ELSE ( w )
  956. 0x02 RAM+ @ SWAP ( cur w ) _find ( a f )
  957. IF DUP IMMED? NOT IF ABORT THEN EXECUTE
  958. ELSE (parse) XLITN THEN
  959. THEN
  960. AGAIN ;
  961. ( ----- 270 )
  962. : CODE XCODE ;
  963. : '? X'? ;
  964. : ['] X['] ; IMMEDIATE
  965. : COMPILE XCOMPILE ; IMMEDIATE
  966. : [COMPILE] X[COMPILE] ; IMMEDIATE
  967. : DO XDO ; IMMEDIATE : LOOP XLOOP ; IMMEDIATE
  968. : IF XIF ; IMMEDIATE : ELSE XELSE ; IMMEDIATE
  969. : AGAIN XAGAIN ; IMMEDIATE : UNTIL XUNTIL ; IMMEDIATE
  970. : LIT" XLIT" ; IMMEDIATE : LITN XLITN ;
  971. : IMMEDIATE XIMM ;
  972. : (entry) (xentry) ; : CREATE XCREATE ; : :** X:** ;
  973. : : [ ' X: , ] ;
  974. CURRENT @ XCURRENT !
  975. ( ----- 280 )
  976. Z80 boot code
  977. This assembles the boot binary. It requires the Z80 assembler
  978. (B5) and cross compilation setup (B260). It requires some
  979. constants to be set. See doc/bootstrap.txt for details.
  980. RESERVED REGISTERS: At all times, IX points to RSP TOS and BC
  981. is IP. SP points to PSP TOS, but you can still use the stack
  982. in native code. you just have to make sure you've restored it
  983. before "next".
  984. The boot binary is loaded in 2 parts. The first part, "decla-
  985. rations", are loaded after xcomp, before xcomp overrides, with
  986. "282 LOAD". The rest, after xcomp overrides, with "283 335
  987. LOADR".
  988. ( ----- 282 )
  989. VARIABLE lbluflw VARIABLE lblexec
  990. ( see comment at TICKS' definition )
  991. ( 7.373MHz target: 737t. outer: 37t inner: 16t )
  992. ( tickfactor = (737 - 37) / 16 )
  993. CREATE tickfactor 44 ,
  994. ( Perform a byte write by taking into account the SYSVARS+3e
  995. override. )
  996. : LD(HL)E*, SYSVARS 0x3e + LDA(i), A ORr,
  997. IFZ, (HL) E LDrr, ELSE, SYSVARS 0x3e + CALL, THEN, ;
  998. ( ----- 283 )
  999. H@ ORG ! ( STABLE ABI )
  1000. 0 JP, ( 00, main ) NOP, ( unused ) NOP, NOP, ( 04, BOOT )
  1001. NOP, NOP, ( 06, uflw ) NOP, NOP, ( 08, LATEST )
  1002. NOP, NOP, NOP, NOP, NOP, NOP, ( unused )
  1003. 0 JP, ( RST 10 ) NOP, NOP, ( 13, oflw )
  1004. NOP, NOP, NOP, NOP, NOP, ( unused )
  1005. 0 JP, ( 1a, next ) NOP, NOP, NOP, ( unused )
  1006. 0 JP, ( RST 20 ) 5 ALLOT0
  1007. 0 JP, ( RST 28 ) 5 ALLOT0
  1008. 0 JP, ( RST 30 ) 5 ALLOT0
  1009. 0 JP, ( RST 38 )
  1010. ( ----- 284 )
  1011. PC ORG @ 1 + ! ( main )
  1012. SP PS_ADDR LDdi, IX RS_ADDR LDdi,
  1013. ( LATEST is a label to the latest entry of the dict. It is
  1014. written at offset 0x08 by the process or person building
  1015. Forth. )
  1016. BIN( @ 0x08 + LDHL(i),
  1017. SYSVARS 0x02 ( CURRENT ) + LD(i)HL,
  1018. HERESTART [IF]
  1019. HL HERESTART LDdi,
  1020. [THEN]
  1021. SYSVARS 0x04 + LD(i)HL, ( RAM+04 == HERE )
  1022. A XORr, SYSVARS 0x3e + LD(i)A, ( 3e == ~C! )
  1023. SYSVARS 0x41 + LD(i)A, ( 41 == ~C!ERR )
  1024. DE BIN( @ 0x04 ( BOOT ) + LDd(i),
  1025. JR, L1 FWR ( execute, B287 )
  1026. ( ----- 286 )
  1027. lblnext BSET PC ORG @ 0x1b + ! ( next )
  1028. ( This routine is jumped to at the end of every word. In it,
  1029. we jump to current IP, but we also take care of increasing
  1030. it by 2 before jumping. )
  1031. ( Before we continue: are we overflowing? )
  1032. IX PUSH, EX(SP)HL, ( do EX to count the IX push in SP )
  1033. SP SUBHLd, HL POP,
  1034. IFNC, ( SP <= IX? overflow )
  1035. SP PS_ADDR LDdi, IX RS_ADDR LDdi,
  1036. DE BIN( @ 0x13 ( oflw ) + LDd(i),
  1037. JR, L2 FWR ( execute, B287 )
  1038. THEN,
  1039. LDA(BC), E A LDrr, BC INCd,
  1040. LDA(BC), D A LDrr, BC INCd,
  1041. ( continue to execute )
  1042. ( ----- 287 )
  1043. lblexec BSET L1 FSET ( B284 ) L2 FSET ( B286 )
  1044. ( DE -> wordref )
  1045. LDA(DE), DE INCd, EXDEHL, ( HL points to PFA )
  1046. A ORr, IFZ, JP(HL), THEN,
  1047. A DECr, ( compiled? ) IFNZ, ( no )
  1048. 3 CPi, IFZ, ( alias ) LDDE(HL), JR, lblexec BWR THEN,
  1049. IFNC, ( ialias )
  1050. LDDE(HL), EXDEHL, LDDE(HL), JR, lblexec BWR THEN,
  1051. ( cell or does. push PFA ) HL PUSH,
  1052. A DECr, JRZ, lblnext BWR ( cell )
  1053. HL INCd, HL INCd, LDDE(HL), EXDEHL, ( does )
  1054. THEN, ( continue to compiledWord )
  1055. ( ----- 289 )
  1056. ( compiled word. HL points to its first wordref, which we'll
  1057. execute now.
  1058. 1. Push current IP to RS
  1059. 2. Set new IP to PFA+2
  1060. 3. Execute wordref )
  1061. IX INCd, IX INCd,
  1062. 0 IX+ C LDIXYr,
  1063. 1 IX+ B LDIXYr,
  1064. ( While we inc, dereference into DE for execute call later. )
  1065. LDDE(HL), ( DE is new wordref )
  1066. HL INCd, ( HL is new PFA+2 )
  1067. B H LDrr, C L LDrr, ( --> IP )
  1068. JR, lblexec BWR ( execute-B287 )
  1069. ( ----- 290 )
  1070. lblchkPS BSET ( chkPS )
  1071. ( thread carefully in there: sometimes, we're in the
  1072. middle of a EXX to protect BC. BC must never be touched
  1073. here. )
  1074. EXX,
  1075. ( We have the return address for this very call on the stack
  1076. and protected registers. 2- is to compensate that. )
  1077. HL PS_ADDR 2- LDdi,
  1078. SP SUBHLd,
  1079. EXX,
  1080. CNC RETc, ( PS_ADDR >= SP? good )
  1081. ( continue to uflw )
  1082. lbluflw BSET ( abortUnderflow )
  1083. DE BIN( @ 0x06 ( uflw ) + LDd(i),
  1084. JR, lblexec BWR
  1085. ( ----- 291 )
  1086. ( Native words )
  1087. H@ 5 + XCURRENT ! ( make next CODE have 0 prev field )
  1088. CODE _find ( cur w -- a f )
  1089. HL POP, ( w ) DE POP, ( cur ) chkPS,
  1090. HL PUSH, ( --> lvl 1 )
  1091. ( First, figure out string len )
  1092. A (HL) LDrr, A ORr,
  1093. ( special case. zero len? we never find anything. )
  1094. IFZ, PUSH0, JPNEXT, THEN,
  1095. BC PUSH, ( --> lvl 2, protect )
  1096. ( Let's do something weird: We'll hold HL by the *tail*.
  1097. Because of our dict structure and because we know our
  1098. lengths, it's easier to compare starting from the end. )
  1099. C A LDrr, B 0 LDri, ( C holds our length )
  1100. BC ADDHLd, HL INCd, ( HL points to after-last-char )
  1101. ( cont . )
  1102. ( ----- 292 )
  1103. BEGIN, ( loop )
  1104. ( DE is a wordref, first step, do our len correspond? )
  1105. HL PUSH, ( --> lvl 3 )
  1106. DE PUSH, ( --> lvl 4 )
  1107. DE DECd,
  1108. LDA(DE),
  1109. 0x7f ANDi, ( remove IMMEDIATE flag )
  1110. C CPr, ( cont. )
  1111. ( ----- 293 )
  1112. IFZ,
  1113. ( match, let's compare the string then )
  1114. DE DECd, ( Skip prev field. One less because we )
  1115. DE DECd, ( pre-decrement )
  1116. B C LDrr, ( loop C times )
  1117. BEGIN,
  1118. ( pre-decrement for easier Z matching )
  1119. DE DECd,
  1120. HL DECd,
  1121. LDA(DE),
  1122. (HL) CPr,
  1123. JRNZ, BREAK,
  1124. DJNZ, AGAIN,
  1125. THEN,
  1126. ( cont. )
  1127. ( ----- 294 )
  1128. ( At this point, Z is set if we have a match. In all cases,
  1129. we want to pop HL and DE )
  1130. DE POP, ( <-- lvl 4 )
  1131. IFZ, ( match, we're done! )
  1132. HL POP, BC POP, HL POP, ( <-- lvl 1-3 ) DE PUSH,
  1133. PUSH1, JPNEXT,
  1134. THEN,
  1135. ( no match, go to prev and continue )
  1136. DE DECd, DE DECd, DE DECd, ( prev field )
  1137. DE PUSH, ( --> lvl 4 )
  1138. EXDEHL,
  1139. LDDE(HL),
  1140. ( cont. )
  1141. ( ----- 295 )
  1142. ( DE contains prev offset )
  1143. HL POP, ( <-- lvl 4, prev field )
  1144. DEZ, IFNZ, ( offset not zero )
  1145. ( get absolute addr from offset )
  1146. ( carry cleared from "or e" )
  1147. DE SBCHLd,
  1148. EXDEHL, ( result in DE )
  1149. THEN,
  1150. HL POP, ( <-- lvl 3 )
  1151. JRNZ, AGAIN, ( loop-B292, try to match again )
  1152. BC POP, ( <-- lvl 2 )
  1153. ( Z set? end of dict, not found. "w" already on PSP TOS )
  1154. PUSH0,
  1155. ;CODE
  1156. ( ----- 297 )
  1157. CODE (br)
  1158. L1 BSET ( used in ?br and loop )
  1159. LDA(BC), H 0 LDri, L A LDrr,
  1160. RLA, IFC, H DECr, THEN,
  1161. BC ADDHLd, B H LDrr, C L LDrr,
  1162. ;CODE
  1163. CODE (?br)
  1164. HL POP,
  1165. HLZ,
  1166. JRZ, L1 BWR ( br + 1. False, branch )
  1167. ( True, skip next byte and don't branch )
  1168. BC INCd,
  1169. ;CODE
  1170. ( ----- 298 )
  1171. CODE (loop)
  1172. 0 IX+ INC(IXY+), IFZ, 1 IX+ INC(IXY+), THEN, ( I++ )
  1173. ( Jump if I <> I' )
  1174. A 0 IX+ LDrIXY, 2 IX- CP(IXY+), JRNZ, L1 BWR ( branch )
  1175. A 1 IX+ LDrIXY, 1 IX- CP(IXY+), JRNZ, L1 BWR ( branch )
  1176. ( don't branch )
  1177. IX DECd, IX DECd, IX DECd, IX DECd,
  1178. BC INCd,
  1179. ;CODE
  1180. ( ----- 305 )
  1181. CODE EXECUTE
  1182. DE POP,
  1183. chkPS,
  1184. lblexec @ JP,
  1185. CODE EXIT
  1186. C 0 IX+ LDrIXY,
  1187. B 1 IX+ LDrIXY,
  1188. IX DECd, IX DECd,
  1189. JPNEXT,
  1190. ( ----- 306 )
  1191. CODE (n) ( number literal )
  1192. ( Literal value to push to stack is next to (n) reference
  1193. in the atom list. That is where IP is currently pointing.
  1194. Read, push, then advance IP. )
  1195. LDA(BC), L A LDrr, BC INCd,
  1196. LDA(BC), H A LDrr, BC INCd,
  1197. HL PUSH,
  1198. ;CODE
  1199. ( ----- 307 )
  1200. CODE (s) ( string literal )
  1201. ( Like (n) but instead of being followed by a 2 bytes
  1202. number, it's followed by a string. When called, puts the
  1203. string's address on PS )
  1204. BC PUSH,
  1205. LDA(BC), C ADDr,
  1206. IFC, B INCr, THEN,
  1207. C A LDrr,
  1208. BC INCd,
  1209. ;CODE
  1210. ( ----- 308 )
  1211. CODE ROT ( a b c -- b c a )
  1212. HL POP, ( C ) DE POP, ( B ) IY POP, ( A ) chkPS,
  1213. DE PUSH, ( B ) HL PUSH, ( C ) IY PUSH, ( A )
  1214. ;CODE
  1215. CODE ROT> ( a b c -- c a b )
  1216. HL POP, ( C ) DE POP, ( B ) IY POP, ( A ) chkPS,
  1217. HL PUSH, ( C ) IY PUSH, ( A ) DE PUSH, ( B )
  1218. ;CODE
  1219. CODE DUP ( a -- a a )
  1220. HL POP, chkPS,
  1221. HL PUSH, HL PUSH,
  1222. ;CODE
  1223. CODE ?DUP
  1224. HL POP, chkPS, HL PUSH,
  1225. HLZ, IFNZ, HL PUSH, THEN,
  1226. ;CODE
  1227. ( ----- 309 )
  1228. CODE DROP ( a -- )
  1229. HL POP, chkPS,
  1230. ;CODE
  1231. CODE SWAP ( a b -- b a )
  1232. HL POP, ( B ) DE POP, ( A )
  1233. chkPS,
  1234. HL PUSH, ( B ) DE PUSH, ( A )
  1235. ;CODE
  1236. CODE OVER ( a b -- a b a )
  1237. HL POP, ( B ) DE POP, ( A )
  1238. chkPS,
  1239. DE PUSH, ( A ) HL PUSH, ( B ) DE PUSH, ( A )
  1240. ;CODE
  1241. ( ----- 310 )
  1242. CODE PICK EXX, ( protect BC )
  1243. HL POP,
  1244. ( x2 )
  1245. L SLA, H RL,
  1246. SP ADDHLd,
  1247. C (HL) LDrr,
  1248. HL INCd,
  1249. B (HL) LDrr,
  1250. ( check PS range before returning )
  1251. EXDEHL,
  1252. HL PS_ADDR LDdi,
  1253. DE SUBHLd,
  1254. IFC, EXX, lbluflw @ JP, THEN,
  1255. BC PUSH,
  1256. EXX, ( unprotect BC ) ;CODE
  1257. ( ----- 311 )
  1258. ( Low-level part of ROLL. Example:
  1259. "1 2 3 4 4 (roll)" --> "1 3 4 4". No sanity checks, never
  1260. call with 0. )
  1261. CODE (roll)
  1262. HL POP,
  1263. B H LDrr,
  1264. C L LDrr,
  1265. SP ADDHLd,
  1266. HL INCd,
  1267. D H LDrr,
  1268. E L LDrr,
  1269. HL DECd,
  1270. HL DECd,
  1271. LDDR,
  1272. ;CODE
  1273. ( ----- 312 )
  1274. CODE 2DROP ( a b -- )
  1275. HL POP, HL POP, chkPS,
  1276. ;CODE
  1277. CODE 2DUP ( a b -- a b a b )
  1278. HL POP, ( b ) DE POP, ( a )
  1279. chkPS,
  1280. DE PUSH, HL PUSH,
  1281. DE PUSH, HL PUSH,
  1282. ;CODE
  1283. ( ----- 313 )
  1284. CODE S0
  1285. HL PS_ADDR LDdi,
  1286. HL PUSH,
  1287. ;CODE
  1288. CODE 'S
  1289. HL 0 LDdi,
  1290. SP ADDHLd,
  1291. HL PUSH,
  1292. ;CODE
  1293. ( ----- 314 )
  1294. CODE AND
  1295. HL POP,
  1296. DE POP,
  1297. chkPS,
  1298. A E LDrr,
  1299. L ANDr,
  1300. L A LDrr,
  1301. A D LDrr,
  1302. H ANDr,
  1303. H A LDrr,
  1304. HL PUSH,
  1305. ;CODE
  1306. ( ----- 315 )
  1307. CODE OR
  1308. HL POP,
  1309. DE POP,
  1310. chkPS,
  1311. A E LDrr,
  1312. L ORr,
  1313. L A LDrr,
  1314. A D LDrr,
  1315. H ORr,
  1316. H A LDrr,
  1317. HL PUSH,
  1318. ;CODE
  1319. ( ----- 316 )
  1320. CODE XOR
  1321. HL POP,
  1322. DE POP,
  1323. chkPS,
  1324. A E LDrr,
  1325. L XORr,
  1326. L A LDrr,
  1327. A D LDrr,
  1328. H XORr,
  1329. H A LDrr,
  1330. HL PUSH,
  1331. ;CODE
  1332. ( ----- 317 )
  1333. CODE NOT
  1334. HL POP,
  1335. chkPS,
  1336. HLZ,
  1337. PUSHZ,
  1338. ;CODE
  1339. ( ----- 318 )
  1340. CODE +
  1341. HL POP,
  1342. DE POP,
  1343. chkPS,
  1344. DE ADDHLd,
  1345. HL PUSH,
  1346. ;CODE
  1347. CODE -
  1348. DE POP,
  1349. HL POP,
  1350. chkPS,
  1351. DE SUBHLd,
  1352. HL PUSH,
  1353. ;CODE
  1354. ( ----- 319 )
  1355. CODE * EXX, ( protect BC )
  1356. ( DE * BC -> DE (high) and HL (low) )
  1357. DE POP, BC POP, chkPS,
  1358. HL 0 LDdi,
  1359. A 0x10 LDri,
  1360. BEGIN,
  1361. HL ADDHLd,
  1362. E RL, D RL,
  1363. IFC,
  1364. BC ADDHLd,
  1365. IFC, DE INCd, THEN,
  1366. THEN,
  1367. A DECr,
  1368. JRNZ, AGAIN,
  1369. HL PUSH,
  1370. EXX, ( unprotect BC ) ;CODE
  1371. ( ----- 320 )
  1372. ( Borrowed from http://wikiti.brandonw.net/ )
  1373. ( Divides AC by DE and places the quotient in AC and the
  1374. remainder in HL )
  1375. CODE /MOD EXX, ( protect BC )
  1376. DE POP, BC POP, chkPS,
  1377. A B LDrr, B 16 LDri,
  1378. HL 0 LDdi,
  1379. BEGIN,
  1380. SCF, C RL, RLA,
  1381. HL ADCHLd, DE SBCHLd,
  1382. IFC, DE ADDHLd, C DECr, THEN,
  1383. DJNZ, AGAIN,
  1384. B A LDrr,
  1385. HL PUSH, BC PUSH,
  1386. EXX, ( unprotect BC ) ;CODE
  1387. ( ----- 321 )
  1388. ( The word below is designed to wait the proper 100us per tick
  1389. at 500kHz when tickfactor is 1. If the CPU runs faster,
  1390. tickfactor has to be adjusted accordingly. "t" in comments
  1391. below means "T-cycle", which at 500kHz is worth 2us. )
  1392. CODE TICKS
  1393. HL POP, chkPS,
  1394. ( we pre-dec to compensate for initialization )
  1395. BEGIN,
  1396. HL DECd, ( 6t )
  1397. IFZ, ( 12t ) JPNEXT, THEN,
  1398. A tickfactor @ LDri, ( 7t )
  1399. BEGIN, A DECr, ( 4t ) JRNZ, ( 12t ) AGAIN,
  1400. JR, ( 12t ) AGAIN, ( outer: 37t inner: 16t )
  1401. ( ----- 322 )
  1402. CODE !
  1403. HL POP, DE POP, chkPS,
  1404. LD(HL)E*, HL INCd,
  1405. E D LDrr, LD(HL)E*,
  1406. ;CODE
  1407. CODE @
  1408. HL POP, chkPS,
  1409. E (HL) LDrr,
  1410. HL INCd,
  1411. D (HL) LDrr,
  1412. DE PUSH,
  1413. ;CODE
  1414. ( ----- 323 )
  1415. CODE C!
  1416. HL POP, DE POP, chkPS,
  1417. LD(HL)E*, ;CODE
  1418. CODE C@
  1419. HL POP, chkPS,
  1420. L (HL) LDrr,
  1421. H 0 LDri, HL PUSH, ;CODE
  1422. CODE ~C!
  1423. HL POP, chkPS,
  1424. SYSVARS 0x3f + LD(i)HL,
  1425. HLZ, ( makes A zero if Z is set ) IFNZ,
  1426. A 0xc3 ( JP ) LDri, THEN,
  1427. ( A is either 0 or c3 ) SYSVARS 0x3e + LD(i)A,
  1428. ;CODE
  1429. ( ----- 324 )
  1430. CODE PC! EXX, ( protect BC )
  1431. BC POP, HL POP, chkPS,
  1432. L OUT(C)r,
  1433. EXX, ( unprotect BC ) ;CODE
  1434. CODE PC@ EXX, ( protect BC )
  1435. BC POP, chkPS,
  1436. H 0 LDri,
  1437. L INr(C),
  1438. HL PUSH,
  1439. EXX, ( unprotect BC ) ;CODE
  1440. ( ----- 325 )
  1441. CODE I
  1442. L 0 IX+ LDrIXY, H 1 IX+ LDrIXY,
  1443. HL PUSH,
  1444. ;CODE
  1445. CODE I'
  1446. L 2 IX- LDrIXY, H 1 IX- LDrIXY,
  1447. HL PUSH,
  1448. ;CODE
  1449. CODE J
  1450. L 4 IX- LDrIXY, H 3 IX- LDrIXY,
  1451. HL PUSH,
  1452. ;CODE
  1453. CODE >R
  1454. HL POP, chkPS,
  1455. IX INCd, IX INCd, 0 IX+ L LDIXYr, 1 IX+ H LDIXYr,
  1456. ;CODE
  1457. ( ----- 326 )
  1458. CODE R>
  1459. L 0 IX+ LDrIXY, H 1 IX+ LDrIXY, IX DECd, IX DECd, HL PUSH,
  1460. ;CODE
  1461. CODE 2>R
  1462. DE POP, HL POP, chkPS,
  1463. IX INCd, IX INCd, 0 IX+ L LDIXYr, 1 IX+ H LDIXYr,
  1464. IX INCd, IX INCd, 0 IX+ E LDIXYr, 1 IX+ D LDIXYr,
  1465. ;CODE
  1466. CODE 2R>
  1467. L 0 IX+ LDrIXY, H 1 IX+ LDrIXY, IX DECd, IX DECd,
  1468. E 0 IX+ LDrIXY, D 1 IX+ LDrIXY, IX DECd, IX DECd,
  1469. DE PUSH, HL PUSH,
  1470. ;CODE
  1471. ( ----- 327 )
  1472. CODE BYE
  1473. HALT,
  1474. ;CODE
  1475. CODE (resSP)
  1476. SP PS_ADDR LDdi,
  1477. ;CODE
  1478. CODE (resRS)
  1479. IX RS_ADDR LDdi,
  1480. ;CODE
  1481. ( ----- 328 )
  1482. CODE S= EXX, ( protect BC )
  1483. DE POP, HL POP, chkPS,
  1484. LDA(DE),
  1485. (HL) CPr,
  1486. IFZ, ( same size? )
  1487. B A LDrr, ( loop A times )
  1488. BEGIN,
  1489. HL INCd, DE INCd,
  1490. LDA(DE),
  1491. (HL) CPr,
  1492. JRNZ, BREAK, ( not equal? break early. NZ is set. )
  1493. DJNZ, AGAIN,
  1494. THEN,
  1495. PUSHZ,
  1496. EXX, ( unprotect BC ) ;CODE
  1497. ( ----- 329 )
  1498. CODE CMP
  1499. HL POP,
  1500. DE POP,
  1501. chkPS,
  1502. DE SUBHLd,
  1503. DE 0 LDdi,
  1504. IFNZ, ( < or > )
  1505. DE INCd,
  1506. IFNC, ( < )
  1507. DE DECd,
  1508. DE DECd,
  1509. THEN,
  1510. THEN,
  1511. DE PUSH,
  1512. ;CODE
  1513. ( ----- 331 )
  1514. CODE (im1)
  1515. IM1,
  1516. EI,
  1517. ;CODE
  1518. CODE 0 PUSH0, ;CODE
  1519. CODE 1 PUSH1, ;CODE
  1520. CODE -1
  1521. HL -1 LDdi,
  1522. HL PUSH,
  1523. ;CODE
  1524. ( ----- 332 )
  1525. CODE 1+
  1526. HL POP,
  1527. chkPS,
  1528. HL INCd,
  1529. HL PUSH,
  1530. ;CODE
  1531. CODE 1-
  1532. HL POP,
  1533. chkPS,
  1534. HL DECd,
  1535. HL PUSH,
  1536. ;CODE
  1537. ( ----- 333 )
  1538. CODE 2+
  1539. HL POP,
  1540. chkPS,
  1541. HL INCd,
  1542. HL INCd,
  1543. HL PUSH,
  1544. ;CODE
  1545. CODE 2-
  1546. HL POP,
  1547. chkPS,
  1548. HL DECd,
  1549. HL DECd,
  1550. HL PUSH,
  1551. ;CODE
  1552. ( ----- 334 )
  1553. CODE RSHIFT ( n u -- n )
  1554. DE POP, ( u ) HL POP, ( n ) chkPS,
  1555. A E LDrr,
  1556. A ORr, IFNZ,
  1557. BEGIN, H SRL, L RR, A DECr, JRNZ, AGAIN,
  1558. THEN,
  1559. HL PUSH, ;CODE
  1560. CODE LSHIFT ( n u -- n )
  1561. DE POP, ( u ) HL POP, ( n ) chkPS,
  1562. A E LDrr,
  1563. A ORr, IFNZ,
  1564. BEGIN, L SLA, H RL, A DECr, JRNZ, AGAIN,
  1565. THEN,
  1566. HL PUSH, ;CODE
  1567. ( ----- 335 )
  1568. CODE |L ( n -- msb lsb )
  1569. HL POP, chkPS,
  1570. D 0 LDri, E H LDrr, DE PUSH,
  1571. E L LDrr, DE PUSH, ;CODE
  1572. CODE |M ( n -- lsb msb )
  1573. HL POP, chkPS,
  1574. D 0 LDri, E L LDrr, DE PUSH,
  1575. E H LDrr, DE PUSH, ;CODE
  1576. ( ----- 350 )
  1577. Core words
  1578. This section contains arch-independent core words of Collapse
  1579. OS. Those words are written in a way that make them entirely
  1580. cross-compilable (see B260). When building Collapse OS, these
  1581. words come right after the boot binary (B280).
  1582. Because this unit is designed to be cross-compiled, things are
  1583. a little weird. It is compiling in the context of a full
  1584. Forth interpreter with all bells and whistles (and z80
  1585. assembler), but it has to obey strict rules:
  1586. 1. Although it cannot compile a word that isn't defined yet,
  1587. it can still execute an immediate from the host system.
  1588. (cont.)
  1589. ( ----- 351 )
  1590. 2. Immediate words that have been cross compiled *cannot* be
  1591. used. Only immediates from the host system can be used.
  1592. 3. If an immediate word compiles words, it can only be words
  1593. that are part of the stable ABI.
  1594. All of this is because when cross compiling, all atom ref-
  1595. erences are offsetted to the target system and are thus
  1596. unusable directly. For the same reason, any reference to a word
  1597. in the host system will obviously be wrong in the target
  1598. system. More details in B260.
  1599. (cont.)
  1600. ( ----- 352 )
  1601. This unit is loaded in two "low" and "high" parts. The low part
  1602. is the biggest chunk and has the most definitions. The high
  1603. part is the "sensitive" chunk and contains "LITN", ":" and ";"
  1604. definitions which, once defined, kind of make any more defs
  1605. impossible.
  1606. The gap between these 2 parts is the ideal place to put device
  1607. driver code. Load the low part with "353 LOAD", the high part
  1608. with "390 LOAD"
  1609. ( ----- 353 )
  1610. : RAM+ [ SYSVARS LITN ] + ; : BIN+ [ BIN( @ LITN ] + ;
  1611. : HERE 0x04 RAM+ ; : ~C!ERR 0x41 RAM+ ;
  1612. : CURRENT* 0x51 RAM+ ; : CURRENT CURRENT* @ ;
  1613. : H@ HERE @ ;
  1614. : FIND ( w -- a f ) CURRENT @ SWAP _find ;
  1615. : IN> 0x30 RAM+ ; ( current position in INBUF )
  1616. : IN( 0x60 RAM+ ; ( points to INBUF )
  1617. : IN$ 0 IN( DUP IN> ! ! ; ( flush input buffer )
  1618. : C<* 0x0c RAM+ ;
  1619. : QUIT (resRS) 0 C<* ! IN$ LIT" (main)" FIND DROP EXECUTE ;
  1620. 1 25 LOADR+
  1621. ( ----- 354 )
  1622. : ABORT (resSP) QUIT ;
  1623. : = CMP NOT ; : < CMP -1 = ; : > CMP 1 = ;
  1624. : 0< 32767 > ; : >= < NOT ; : <= > NOT ; : 0>= 0< NOT ;
  1625. : >< ( n l h -- f ) 2 PICK > ( n l f ) ROT> > AND ;
  1626. : =><= 2 PICK >= ( n l f ) ROT> >= AND ;
  1627. : NIP SWAP DROP ; : TUCK SWAP OVER ;
  1628. : -^ SWAP - ;
  1629. : C@+ ( a -- a+1 c ) DUP C@ SWAP 1+ SWAP ;
  1630. : C!+ ( c a -- a+1 ) TUCK C! 1+ ;
  1631. : C@- ( a -- a-1 c ) DUP C@ SWAP 1- SWAP ;
  1632. : C!- ( c a -- a-1 ) TUCK C! 1- ;
  1633. : LEAVE R> R> DROP I 1- >R >R ; : UNLOOP R> 2R> 2DROP >R ;
  1634. ( ----- 355 )
  1635. : +! TUCK @ + SWAP ! ;
  1636. : *! ( addr alias -- ) 1+ ! ;
  1637. : **! ( addr ialias -- ) 1+ @ ! ;
  1638. : / /MOD NIP ;
  1639. : MOD /MOD DROP ;
  1640. : ALLOT HERE +! ;
  1641. : FILL ( a n b -- )
  1642. ROT> OVER ( b a n a ) + SWAP ( b a+n a ) DO ( b )
  1643. DUP I C! LOOP DROP ;
  1644. : ALLOT0 ( n -- ) H@ OVER 0 FILL ALLOT ;
  1645. ( ----- 356 )
  1646. SYSVARS 0x53 + :** EMIT
  1647. : STYPE C@+ ( a len ) 0 DO C@+ EMIT LOOP DROP ;
  1648. : EOT 0x4 ; : BS 0x8 ; : LF 0xa ; : CR 0xd ; : SPC 0x20 ;
  1649. : SPC> SPC EMIT ;
  1650. : NL> 0x50 RAM+ C@ ?DUP IF EMIT ELSE 13 EMIT 10 EMIT THEN ;
  1651. : ERR STYPE ABORT ;
  1652. : (uflw) LIT" stack underflow" ERR ;
  1653. XCURRENT @ _xapply ORG @ 0x06 ( stable ABI uflw ) + !
  1654. : (oflw) LIT" stack overflow" ERR ;
  1655. XCURRENT @ _xapply ORG @ 0x13 ( stable ABI oflw ) + !
  1656. : (wnf) STYPE LIT" word not found" ERR ;
  1657. ( ----- 357 )
  1658. ( r c -- r f )
  1659. ( Parse digit c and accumulate into result r.
  1660. Flag f is true when c was a valid digit )
  1661. : _pdacc
  1662. '0' - DUP 10 < IF ( good, add to running result )
  1663. SWAP 10 * + 1 ( r*10+n f )
  1664. ELSE ( bad ) DROP 0 THEN ;
  1665. ( ----- 358 )
  1666. : _pd ( a -- n f, parse decimal )
  1667. C@+ OVER C@ 0 ( a len firstchar startat )
  1668. ( if we have '-', we only advance. more processing later. )
  1669. SWAP '-' = IF 1+ THEN ( a len startat )
  1670. ( if we can do the whole string, success. if _pdacc returns
  1671. false before, failure. )
  1672. 0 ROT> ( len ) ( startat ) DO ( a r )
  1673. OVER I + C@ ( a r c ) _pdacc ( a r f )
  1674. NOT IF DROP 1- 0 UNLOOP EXIT THEN LOOP ( a r )
  1675. ( if we had '-', we need to invert result. )
  1676. SWAP C@ '-' = IF 0 -^ THEN 1 ( r 1 ) ;
  1677. ( ----- 359 )
  1678. ( strings being sent to parse routines are always null
  1679. terminated )
  1680. : _pc ( a -- n f, parse character )
  1681. ( apostrophe is ASCII 39 )
  1682. DUP 1+ C@ 39 = OVER 3 + C@ 39 = AND ( a f )
  1683. NOT IF 0 EXIT THEN ( a 0 )
  1684. ( surrounded by apos, good, return )
  1685. 2+ C@ 1 ( n 1 )
  1686. ;
  1687. ( ----- 360 )
  1688. ( returns negative value on error )
  1689. : _ ( c -- n )
  1690. DUP '0' '9' =><= IF '0' - EXIT THEN
  1691. DUP 'a' 'f' =><= IF 0x57 ( 'a' - 10 ) - EXIT THEN
  1692. DROP -1 ( bad )
  1693. ;
  1694. : _ph ( a -- n f, parse hex )
  1695. ( '0': ASCII 0x30 'x': 0x78 0x7830 )
  1696. DUP 1+ @ 0x7830 = NOT IF 0 EXIT THEN ( a 0 )
  1697. ( We have "0x" prefix )
  1698. DUP C@ ( a len )
  1699. 0 SWAP 1+ ( len+1 ) 3 DO ( a r )
  1700. OVER I + C@ ( a r c ) _ ( a r n )
  1701. DUP 0< IF 2DROP 0 UNLOOP EXIT THEN
  1702. SWAP 4 LSHIFT + ( a r*16+n ) LOOP
  1703. NIP 1 ;
  1704. ( ----- 361 )
  1705. : _pb ( a -- n f, parse binary )
  1706. ( '0': ASCII 0x30 'b': 0x62 0x6230 )
  1707. DUP 1+ @ 0x6230 = NOT IF 0 EXIT THEN ( a 0 )
  1708. ( We have "0b" prefix )
  1709. DUP C@ ( a len )
  1710. 0 SWAP 1+ ( len+1 ) 3 DO ( a r )
  1711. OVER I + C@ ( a r c )
  1712. DUP '0' '1' =><= NOT IF 2DROP 0 UNLOOP EXIT THEN
  1713. '0' - SWAP 1 LSHIFT + ( a r*2+n ) LOOP
  1714. NIP 1 ;
  1715. : (parse) ( a -- n )
  1716. _pc IF EXIT THEN
  1717. _ph IF EXIT THEN
  1718. _pb IF EXIT THEN
  1719. _pd IF EXIT THEN
  1720. ( nothing works ) (wnf) ;
  1721. ( ----- 362 )
  1722. : EOT? EOT = ;
  1723. SYSVARS 0x55 + :** KEY?
  1724. : KEY BEGIN KEY? UNTIL ;
  1725. ( del is same as backspace )
  1726. : BS? DUP 0x7f = SWAP BS = OR ;
  1727. : RDLN ( Read 1 line in input buff and make IN> point to it )
  1728. IN$ BEGIN
  1729. ( buffer overflow? same as if we typed a newline )
  1730. IN> @ IN( - 0x3e = IF CR ELSE KEY THEN ( c )
  1731. DUP BS? IF
  1732. IN> @ IN( > IF -1 IN> +! BS EMIT THEN SPC> BS EMIT
  1733. ELSE DUP LF = IF DROP CR THEN ( same as CR )
  1734. DUP SPC >= IF DUP EMIT ( echo back ) THEN
  1735. DUP IN> @ ! 1 IN> +! THEN ( c )
  1736. DUP CR = SWAP EOT? OR UNTIL NL> IN( IN> ! ;
  1737. ( ----- 363 )
  1738. : RDLN<
  1739. IN> @ C@ ( c )
  1740. DUP IF ( not EOL? good, inc and return )
  1741. 1 IN> +!
  1742. ELSE ( EOL ? readline. we still return null though )
  1743. RDLN
  1744. THEN ( c )
  1745. ( update C<? flag )
  1746. IN> @ C@ 0 > 0x06 RAM+ ! ( 06 == C<? ) ;
  1747. ( ----- 364 )
  1748. : C<? 0x06 RAM+ @ ;
  1749. : C< C<* @ ?DUP NOT IF RDLN< ELSE EXECUTE THEN ;
  1750. : , H@ ! H@ 2+ HERE ! ;
  1751. : C, H@ C!+ HERE ! ;
  1752. : ,"
  1753. BEGIN
  1754. C< DUP 34 ( ASCII " ) = IF DROP EXIT THEN C,
  1755. AGAIN ;
  1756. : EOT, EOT C, ;
  1757. : WS? SPC <= ;
  1758. : TOWORD ( -- c, c being the first letter of the word )
  1759. 0 ( dummy ) BEGIN
  1760. DROP C< DUP WS? NOT OVER EOT? OR UNTIL ;
  1761. ( ----- 365 )
  1762. ( Read word from C<, copy to WORDBUF, null-terminate, and
  1763. return WORDBUF. )
  1764. : _wb 0x0e RAM+ ;
  1765. : _eot 0x0401 _wb ! _wb ;
  1766. : WORD
  1767. _wb 1+ TOWORD ( a c )
  1768. DUP EOT? IF 2DROP _eot EXIT THEN
  1769. BEGIN
  1770. OVER C! 1+ C< ( a c )
  1771. OVER 0x2e RAM+ = OVER WS? OR
  1772. UNTIL ( a c )
  1773. SWAP _wb - 1- ( ws len ) _wb C!
  1774. EOT? IF _eot ELSE _wb THEN ;
  1775. ( ----- 366 )
  1776. : IMMEDIATE
  1777. CURRENT @ 1-
  1778. DUP C@ 128 OR SWAP C! ;
  1779. : IMMED? 1- C@ 0x80 AND ;
  1780. : '? WORD FIND ;
  1781. : ' '? NOT IF (wnf) THEN ;
  1782. : ROLL
  1783. ?DUP NOT IF EXIT THEN
  1784. 1+ DUP PICK ( n val )
  1785. SWAP 2 * (roll) ( val )
  1786. NIP ;
  1787. ( ----- 367 )
  1788. : MOVE ( a1 a2 u -- )
  1789. ?DUP IF ( u ) 0 DO ( a1 a2 )
  1790. OVER I + C@ ( src dst x )
  1791. OVER I + ( src dst x dst )
  1792. C! ( src dst )
  1793. LOOP THEN 2DROP ;
  1794. : MOVE- ( a1 a2 u -- )
  1795. ?DUP IF ( u ) 0 DO ( a1 a2 )
  1796. OVER I' + I - 1- C@ ( src dst x )
  1797. OVER I' + I - 1- ( src dst x dst )
  1798. C! ( src dst )
  1799. LOOP THEN 2DROP ;
  1800. : MOVE, ( a u -- ) H@ OVER ALLOT SWAP MOVE ;
  1801. ( ----- 368 )
  1802. : [entry] ( w -- )
  1803. C@+ ( w+1 len ) TUCK MOVE, ( len )
  1804. ( write prev value )
  1805. H@ CURRENT @ - ,
  1806. C, ( write size )
  1807. H@ CURRENT ! ;
  1808. : (entry) WORD [entry] ;
  1809. : CREATE (entry) 2 ( cellWord ) C, ;
  1810. : VARIABLE CREATE 2 ALLOT ;
  1811. ( ----- 369 )
  1812. : FORGET
  1813. ' DUP ( w w )
  1814. ( HERE must be at the end of prev's word, that is, at the
  1815. beginning of w. )
  1816. DUP 1- C@ ( name len field )
  1817. 0x7f AND ( remove IMMEDIATE flag )
  1818. 3 + ( fixed header len )
  1819. - HERE ! ( w )
  1820. ( get prev addr ) 3 - DUP @ - CURRENT ! ;
  1821. : EMPTY LIT" _sys" FIND IF DUP HERE ! CURRENT ! THEN ;
  1822. ( ----- 370 )
  1823. : DOES>
  1824. ( Overwrite cellWord in CURRENT )
  1825. 3 ( does ) CURRENT @ C!
  1826. ( When we have a DOES>, we forcefully place HERE to 4
  1827. bytes after CURRENT. This allows a DOES word to use ","
  1828. and "C," without messing everything up. )
  1829. CURRENT @ 3 + HERE !
  1830. ( HERE points to where we should write R> )
  1831. R> ,
  1832. ( We're done. Because we've popped RS, we'll exit parent
  1833. definition ) ;
  1834. : CONSTANT CREATE , DOES> @ ;
  1835. : [IF]
  1836. IF EXIT THEN
  1837. LIT" [THEN]" BEGIN DUP WORD S= UNTIL DROP ;
  1838. : [THEN] ;
  1839. ( ----- 371 )
  1840. ( n -- Fetches block n and write it to BLK( )
  1841. SYSVARS 0x34 + :** BLK@*
  1842. ( n -- Write back BLK( to storage at block n )
  1843. SYSVARS 0x36 + :** BLK!*
  1844. ( Current blk pointer in ( )
  1845. : BLK> 0x38 RAM+ ;
  1846. ( Whether buffer is dirty )
  1847. : BLKDTY 0x3a RAM+ ;
  1848. : BLK( 0x3c RAM+ @ ;
  1849. : BLK) BLK( 1024 + ;
  1850. : BLK$
  1851. H@ 0x3c ( BLK(* ) RAM+ !
  1852. 1024 ALLOT
  1853. ( LOAD detects end of block with ASCII EOT. This is why
  1854. we write it there. )
  1855. EOT, 0 BLKDTY ! -1 BLK> ! ;
  1856. ( ----- 372 )
  1857. : BLK! ( -- ) BLK> @ BLK!* 0 BLKDTY ! ;
  1858. : FLUSH BLKDTY @ IF BLK! THEN -1 BLK> ! ;
  1859. : BLK@ ( n -- )
  1860. DUP BLK> @ = IF DROP EXIT THEN
  1861. FLUSH DUP BLK> ! BLK@* ;
  1862. : BLK!! 1 BLKDTY ! ;
  1863. : WIPE BLK( 1024 0 FILL BLK!! ;
  1864. : WIPED? ( -- f )
  1865. 1 ( f ) BLK) BLK( DO
  1866. I C@ IF DROP 0 ( f ) LEAVE THEN LOOP ;
  1867. : COPY ( src dst -- )
  1868. FLUSH SWAP BLK@ BLK> ! BLK! ;
  1869. ( ----- 373 )
  1870. : . ( n -- )
  1871. ?DUP NOT IF '0' EMIT EXIT THEN ( 0 is a special case )
  1872. ( handle negative )
  1873. DUP 0< IF '-' EMIT -1 * THEN
  1874. 999 SWAP ( stop indicator )
  1875. BEGIN
  1876. 10 /MOD ( r q )
  1877. SWAP '0' + SWAP ( d q )
  1878. ?DUP NOT UNTIL
  1879. BEGIN EMIT DUP '9' > UNTIL DROP ( drop stop ) ;
  1880. ( ----- 374 )
  1881. : ? @ . ;
  1882. : _
  1883. DUP 9 > IF 10 - 'a' +
  1884. ELSE '0' + THEN ;
  1885. ( For hex display, there are no negatives )
  1886. : .x
  1887. 0xff AND 16 /MOD ( l h )
  1888. _ EMIT _ EMIT ;
  1889. : .X |M .x .x ;
  1890. ( ----- 375 )
  1891. : _ ( a -- a+8 )
  1892. DUP ( a a )
  1893. ':' EMIT DUP .x SPC>
  1894. 4 0 DO DUP @ |L .x .x SPC> 2+ LOOP
  1895. DROP ( a )
  1896. 8 0 DO
  1897. C@+ DUP SPC 0x7e =><= NOT IF DROP '.' THEN EMIT
  1898. LOOP NL> ;
  1899. : DUMP ( n a -- )
  1900. SWAP 8 /MOD SWAP IF 1+ THEN
  1901. 0 DO _ LOOP ;
  1902. ( ----- 376 )
  1903. : LIST
  1904. BLK@
  1905. 16 0 DO
  1906. I 1+ DUP 10 < IF SPC> THEN . SPC>
  1907. 64 I * BLK( + DUP 64 + SWAP DO
  1908. I C@ DUP 0x1f > IF EMIT ELSE DROP LEAVE THEN
  1909. LOOP
  1910. NL>
  1911. LOOP ;
  1912. ( ----- 377 )
  1913. : INTERPRET
  1914. BEGIN
  1915. WORD DUP @ 0x0401 = ( EOT ) IF DROP EXIT THEN
  1916. FIND NOT IF (parse) ELSE EXECUTE THEN
  1917. C<? NOT IF SPC> LIT" ok" STYPE NL> THEN
  1918. AGAIN ;
  1919. ( Read from BOOT C< PTR and inc it. )
  1920. : (boot<)
  1921. 0x2e ( BOOT C< PTR ) RAM+ @ C@+ ( a+1 c )
  1922. SWAP 0x2e RAM+ ! ( c ) ;
  1923. ( ----- 378 )
  1924. : LOAD
  1925. BLK> @ >R ( save restorable variables to RSP )
  1926. C<* @ >R
  1927. 0x06 RAM+ ( C<? ) @ >R 0x2e RAM+ ( boot ptr ) @ >R
  1928. BLK@ BLK( 0x2e RAM+ ! ( Point to beginning of BLK )
  1929. ['] (boot<) 0x0c RAM+ !
  1930. 1 0x06 RAM+ ! ( 06 == C<? )
  1931. INTERPRET
  1932. R> 0x2e RAM+ ! R> 0x06 RAM+ !
  1933. R> C<* ! R> BLK@ ;
  1934. : LOAD+ BLK> @ + LOAD ;
  1935. ( b1 b2 -- )
  1936. : LOADR 1+ SWAP DO I DUP . SPC> LOAD LOOP ;
  1937. : LOADR+ BLK> @ + SWAP BLK> @ + SWAP LOADR ;
  1938. ( ----- 390 )
  1939. ( xcomp core high )
  1940. : (main) INTERPRET BYE ;
  1941. : BOOT
  1942. 0x02 RAM+ CURRENT* !
  1943. CURRENT @ 0x2e RAM+ ! ( 2e == BOOT C< PTR )
  1944. 0 0x50 RAM+ C! ( NL> )
  1945. ['] (emit) ['] EMIT **! ['] (key?) ['] KEY? **!
  1946. ['] (boot<) C<* !
  1947. ( boot< always has a char waiting. 06 == C<?* )
  1948. 1 0x06 RAM+ ! INTERPRET
  1949. 0 C<* ! IN$
  1950. LIT" _sys" [entry]
  1951. LIT" Collapse OS" STYPE NL> (main) ;
  1952. XCURRENT @ _xapply ORG @ 0x04 ( stable ABI BOOT ) + !
  1953. 1 4 LOADR+
  1954. ( ----- 391 )
  1955. ( Now we have "as late as possible" stuff. See bootstrap doc. )
  1956. : :* ( addr -- ) (entry) 4 ( alias ) C, , ;
  1957. : :** ( addr -- ) (entry) 5 ( ialias ) C, , ;
  1958. ( ----- 392 )
  1959. : _bchk DUP 0x7f + 0xff > IF LIT" br ovfl" STYPE ABORT THEN ;
  1960. : DO COMPILE 2>R H@ ; IMMEDIATE
  1961. : LOOP COMPILE (loop) H@ - _bchk C, ; IMMEDIATE
  1962. ( LEAVE is implemented in low xcomp )
  1963. : LITN COMPILE (n) , ;
  1964. ( gets its name at the very end. can't comment afterwards )
  1965. : _ BEGIN LIT" )" WORD S= UNTIL ; IMMEDIATE
  1966. : _ ( : will get its name almost at the very end )
  1967. (entry) 1 ( compiled ) C,
  1968. BEGIN
  1969. WORD DUP LIT" ;" S= IF DROP COMPILE EXIT EXIT THEN
  1970. FIND IF ( is word ) DUP IMMED? IF EXECUTE ELSE , THEN
  1971. ELSE ( maybe number ) (parse) LITN THEN
  1972. AGAIN ;
  1973. ( ----- 393 )
  1974. : IF ( -- a | a: br cell addr )
  1975. COMPILE (?br) H@ 1 ALLOT ( br cell allot )
  1976. ; IMMEDIATE
  1977. : THEN ( a -- | a: br cell addr )
  1978. DUP H@ -^ _bchk SWAP ( a-H a ) C!
  1979. ; IMMEDIATE
  1980. : ELSE ( a1 -- a2 | a1: IF cell a2: ELSE cell )
  1981. COMPILE (br)
  1982. 1 ALLOT
  1983. [COMPILE] THEN
  1984. H@ 1- ( push a. 1- for allot offset )
  1985. ; IMMEDIATE
  1986. : LIT"
  1987. COMPILE (s) H@ 0 C, ,"
  1988. DUP H@ -^ 1- ( a len ) SWAP C!
  1989. ; IMMEDIATE
  1990. ( ----- 394 )
  1991. ( We don't use ." and ABORT in core, they're not xcomp-ed )
  1992. : ." [COMPILE] LIT" COMPILE STYPE ; IMMEDIATE
  1993. : ABORT" [COMPILE] ." COMPILE ABORT ; IMMEDIATE
  1994. : BEGIN H@ ; IMMEDIATE
  1995. : AGAIN COMPILE (br) H@ - _bchk C, ; IMMEDIATE
  1996. : UNTIL COMPILE (?br) H@ - _bchk C, ; IMMEDIATE
  1997. : [ INTERPRET ; IMMEDIATE
  1998. : ] R> DROP ;
  1999. : COMPILE ' LITN ['] , , ; IMMEDIATE
  2000. : [COMPILE] ' , ; IMMEDIATE
  2001. : ['] ' LITN ; IMMEDIATE
  2002. ':' X' _ 4 - C! ( give : its name )
  2003. '(' X' _ 4 - C!
  2004. ( ----- 400 )
  2005. ( Write byte E at addr HL, assumed to be an AT28 EEPROM. After
  2006. that, poll repeatedly that address until writing is complete.
  2007. If last polled value is different than orig, set ~C!ERR )
  2008. (entry) ~AT28 ( warning: don't touch D register )
  2009. (HL) E LDrr, A E LDrr, ( orig ) EXAFAF', ( save )
  2010. E (HL) LDrr, ( poll ) BEGIN,
  2011. A (HL) LDrr, ( poll ) E CPr, ( same as old? )
  2012. E A LDrr, ( save old poll, Z preserved )
  2013. JRNZ, AGAIN,
  2014. EXAFAF', ( orig ) E SUBr, ( equal? )
  2015. IFNZ, SYSVARS 0x41 + ( ~C!ERR ) LD(i)A, THEN,
  2016. RET,
  2017. ( ----- 401 )
  2018. Grid subsystem
  2019. See doc/grid.txt.
  2020. Load range: B402-B403
  2021. ( ----- 402 )
  2022. : XYPOS [ GRID_MEM LITN ] ; : XYMODE [ GRID_MEM LITN ] 2+ ;
  2023. '? CURSOR! NIP NOT [IF] : CURSOR! 2DROP ; [THEN]
  2024. : XYPOS! COLS LINES * MOD DUP XYPOS @ CURSOR! XYPOS ! ;
  2025. : AT-XY ( x y -- ) COLS * + XYPOS! ;
  2026. '? NEWLN NIP NOT [IF]
  2027. : NEWLN ( ln -- ) COLS * DUP COLS + SWAP DO SPC I CELL! LOOP ;
  2028. [THEN]
  2029. : _lf XYMODE C@ IF EXIT THEN
  2030. XYPOS @ COLS / 1+ LINES MOD DUP NEWLN
  2031. COLS * XYPOS! ;
  2032. : _bs SPC XYPOS @ TUCK CELL! ( pos ) 1- XYPOS! ;
  2033. ( ----- 403 )
  2034. : (emit)
  2035. DUP BS? IF DROP _bs EXIT THEN
  2036. DUP CR = IF DROP _lf EXIT THEN
  2037. DUP SPC < IF DROP EXIT THEN
  2038. XYPOS @ CELL!
  2039. XYPOS @ 1+ DUP COLS MOD IF XYPOS! ELSE DROP _lf THEN ;
  2040. : GRID$ 0 XYPOS ! 0 XYMODE C! ;
  2041. ( ----- 410 )
  2042. PS/2 keyboard subsystem
  2043. Provides (key?) from a driver providing the PS/2 protocol. That
  2044. is, for a driver taking care of providing all key codes emanat-
  2045. ing from a PS/2 keyboard, this subsystem takes care of mapping
  2046. those keystrokes to ASCII characters. This code is designed to
  2047. be cross-compiled and loaded with drivers.
  2048. Requires PS2_MEM to be defined.
  2049. Load range: 411-414
  2050. ( ----- 411 )
  2051. : PS2_SHIFT [ PS2_MEM LITN ] ;
  2052. : PS2$ 0 PS2_SHIFT C! ;
  2053. ( A list of the values associated with the 0x80 possible scan
  2054. codes of the set 2 of the PS/2 keyboard specs. 0 means no
  2055. value. That value is a character that can be read in (key?)
  2056. No make code in the PS/2 set 2 reaches 0x80. )
  2057. CREATE PS2_CODES
  2058. ( 00 ) 0 C, 0 C, 0 C, 0 C, 0 C, 0 C, 0 C, 0 C,
  2059. ( 08 ) 0 C, 0 C, 0 C, 0 C, 0 C, 9 C, '`' C, 0 C,
  2060. ( 10 ) 0 C, 0 C, 0 C, 0 C, 0 C, 'q' C, '1' C, 0 C,
  2061. ( I don't know why, but the key 2 is sent as 0x1f by 2 of my
  2062. keyboards. Is it a timing problem on the ATtiny? TODO )
  2063. ( 18 ) 0 C, 0 C, 'z' C, 's' C, 'a' C, 'w' C, '2' C, '2' C,
  2064. ( 20 ) 0 C, 'c' C, 'x' C, 'd' C, 'e' C, '4' C, '3' C, 0 C,
  2065. ( 28 ) 0 C, 32 C, 'v' C, 'f' C, 't' C, 'r' C, '5' C, 0 C,
  2066. ( ----- 412 )
  2067. ( 30 ) 0 C, 'n' C, 'b' C, 'h' C, 'g' C, 'y' C, '6' C, 0 C,
  2068. ( 38 ) 0 C, 0 C, 'm' C, 'j' C, 'u' C, '7' C, '8' C, 0 C,
  2069. ( 40 ) 0 C, ',' C, 'k' C, 'i' C, 'o' C, '0' C, '9' C, 0 C,
  2070. ( 48 ) 0 C, '.' C, '/' C, 'l' C, ';' C, 'p' C, '-' C, 0 C,
  2071. ( 50 ) 0 C, 0 C, ''' C, 0 C, '[' C, '=' C, 0 C, 0 C,
  2072. ( 58 ) 0 C, 0 C, 13 C, ']' C, 0 C, '\' C, 0 C, 0 C,
  2073. ( 60 ) 0 C, 0 C, 0 C, 0 C, 0 C, 0 C, 8 C, 0 C,
  2074. ( 68 ) 0 C, '1' C, 0 C, '4' C, '7' C, 0 C, 0 C, 0 C,
  2075. ( 70 ) '0' C, '.' C, '2' C, '5' C, '6' C, '8' C, 27 C, 0 C,
  2076. ( 78 ) 0 C, 0 C, '3' C, 0 C, 0 C, '9' C, 0 C, 0 C,
  2077. ( Same values, but shifted )
  2078. ( 00 ) 0 C, 0 C, 0 C, 0 C, 0 C, 0 C, 0 C, 0 C,
  2079. ( 08 ) 0 C, 0 C, 0 C, 0 C, 0 C, 9 C, '~' C, 0 C,
  2080. ( 10 ) 0 C, 0 C, 0 C, 0 C, 0 C, 'Q' C, '!' C, 0 C,
  2081. ( 18 ) 0 C, 0 C, 'Z' C, 'S' C, 'A' C, 'W' C, '@' C, '@' C,
  2082. ( 20 ) 0 C, 'C' C, 'X' C, 'D' C, 'E' C, '$' C, '#' C, 0 C,
  2083. ( ----- 413 )
  2084. ( 28 ) 0 C, 32 C, 'V' C, 'F' C, 'T' C, 'R' C, '%' C, 0 C,
  2085. ( 30 ) 0 C, 'N' C, 'B' C, 'H' C, 'G' C, 'Y' C, '^' C, 0 C,
  2086. ( 38 ) 0 C, 0 C, 'M' C, 'J' C, 'U' C, '&' C, '*' C, 0 C,
  2087. ( 40 ) 0 C, '<' C, 'K' C, 'I' C, 'O' C, ')' C, '(' C, 0 C,
  2088. ( 48 ) 0 C, '>' C, '?' C, 'L' C, ':' C, 'P' C, '_' C, 0 C,
  2089. ( 50 ) 0 C, 0 C, '"' C, 0 C, '{' C, '+' C, 0 C, 0 C,
  2090. ( 58 ) 0 C, 0 C, 13 C, '}' C, 0 C, '|' C, 0 C, 0 C,
  2091. ( 60 ) 0 C, 0 C, 0 C, 0 C, 0 C, 0 C, 8 C, 0 C,
  2092. ( 68 ) 0 C, 0 C, 0 C, 0 C, 0 C, 0 C, 0 C, 0 C,
  2093. ( 70 ) 0 C, 0 C, 0 C, 0 C, 0 C, 0 C, 27 C, 0 C,
  2094. ( 78 ) 0 C, 0 C, 0 C, 0 C, 0 C, 0 C, 0 C, 0 C,
  2095. ( ----- 414 )
  2096. : _shift? ( kc -- f ) DUP 0x12 = SWAP 0x59 = OR ;
  2097. : (key?) ( -- c? f )
  2098. (ps2kc) DUP NOT IF EXIT THEN ( kc )
  2099. DUP 0xe0 ( extended ) = IF ( ignore ) DROP 0 EXIT THEN
  2100. DUP 0xf0 ( break ) = IF DROP ( )
  2101. ( get next kc and see if it's a shift )
  2102. BEGIN (ps2kc) ?DUP UNTIL ( kc )
  2103. _shift? IF ( drop shift ) 0 PS2_SHIFT C! THEN
  2104. ( whether we had a shift or not, we return the next )
  2105. 0 EXIT THEN
  2106. DUP 0x7f > IF DROP 0 EXIT THEN
  2107. DUP _shift? IF DROP 1 PS2_SHIFT C! 0 EXIT THEN
  2108. ( ah, finally, we have a gentle run-of-the-mill KC )
  2109. PS2_CODES PS2_SHIFT C@ IF 0x80 + THEN + C@ ( c, maybe 0 )
  2110. ?DUP ( c? f ) ;
  2111. ( ----- 418 )
  2112. SPI relay driver
  2113. This driver is designed for a ad-hoc adapter card that acts as a
  2114. SPI relay between the z80 bus and the SPI device. When writing
  2115. to SPI_CTL, we expect a bitmask of the device to select, with
  2116. 0 meaning that everything is de-selected. Reading SPI_CTL
  2117. returns 0 if the device is ready or 1 if it's still running an
  2118. exchange. Writing to SPI_DATA initiates an exchange.
  2119. Provides the SPI relay protocol. Load driver with "419 LOAD".
  2120. ( ----- 419 )
  2121. CODE (spix) ( n -- n )
  2122. HL POP, chkPS, A L LDrr,
  2123. SPI_DATA OUTiA,
  2124. ( wait until xchg is done )
  2125. BEGIN, SPI_CTL INAi, 1 ANDi, JRNZ, AGAIN,
  2126. SPI_DATA INAi,
  2127. L A LDrr,
  2128. HL PUSH,
  2129. ;CODE
  2130. CODE (spie) ( n -- )
  2131. HL POP, chkPS, A L LDrr,
  2132. SPI_CTL OUTiA,
  2133. ;CODE
  2134. ( ----- 420 )
  2135. SD Card subsystem
  2136. Load range: B423-B436
  2137. This subsystem is designed for a ad-hoc adapter card that acts
  2138. as a SPI relay between the z80 bus and the SD card. It requires
  2139. a driver providing the SPI Relay protocol. You need to define
  2140. SDC_DEVID to specify which ID will be supplied to (spie).
  2141. Through that layer, this driver implements the SDC protocol
  2142. allowing it to provide BLK@ and BLK!.
  2143. ( ----- 423 )
  2144. ( Computes n into crc c with polynomial 0x1021 )
  2145. CODE _crc16 ( c n -- c ) EXX, ( protect BC )
  2146. HL POP, ( n ) DE POP, ( c )
  2147. A L LDrr, D XORr, D A LDrr,
  2148. B 8 LDri,
  2149. BEGIN,
  2150. E SLA, D RL,
  2151. IFC, ( msb is set, apply polynomial )
  2152. A D LDrr, 0x10 XORi, D A LDrr,
  2153. A E LDrr, 0x21 XORi, E A LDrr,
  2154. THEN,
  2155. DJNZ, AGAIN,
  2156. DE PUSH,
  2157. EXX, ( unprotect BC ) ;CODE
  2158. ( ----- 424 )
  2159. ( -- n )
  2160. : _idle 0xff (spix) ;
  2161. ( -- n )
  2162. ( spix 0xff until the response is something else than 0xff
  2163. for a maximum of 20 times. Returns 0xff if no response. )
  2164. : _wait
  2165. 0 ( dummy ) 20 0 DO
  2166. DROP _idle DUP 0xff = NOT IF LEAVE THEN
  2167. LOOP ;
  2168. ( ----- 425 )
  2169. ( -- )
  2170. ( The opposite of sdcWaitResp: we wait until response is 0xff.
  2171. After a successful read or write operation, the card will be
  2172. busy for a while. We need to give it time before interacting
  2173. with it again. Technically, we could continue processing on
  2174. our side while the card it busy, and maybe we will one day,
  2175. but at the moment, I'm having random write errors if I don't
  2176. do this right after a write, so I prefer to stay cautious
  2177. for now. )
  2178. : _ready BEGIN _idle 0xff = UNTIL ;
  2179. ( ----- 426 )
  2180. ( c n -- c )
  2181. ( Computes n into crc c with polynomial 0x09
  2182. Note that the result is "left aligned", that is, that 8th
  2183. bit to the "right" is insignificant (will be stop bit). )
  2184. : _crc7
  2185. XOR ( c )
  2186. 8 0 DO
  2187. 2 * ( <<1 )
  2188. DUP 255 > IF
  2189. ( MSB was set, apply polynomial )
  2190. 0xff AND
  2191. 0x12 XOR ( 0x09 << 1, we apply CRC on high bits )
  2192. THEN
  2193. LOOP
  2194. ;
  2195. ( ----- 427 )
  2196. ( send-and-crc7 )
  2197. ( n c -- c )
  2198. : _s+crc SWAP DUP (spix) DROP _crc7 ;
  2199. ( ----- 428 )
  2200. ( cmd arg1 arg2 -- resp )
  2201. ( Sends a command to the SD card, along with arguments and
  2202. specified CRC fields. (CRC is only needed in initial commands
  2203. though). This does *not* handle CS. You have to
  2204. select/deselect the card outside this routine. )
  2205. : _cmd
  2206. _wait DROP ROT ( a1 a2 cmd )
  2207. 0 _s+crc ( a1 a2 crc )
  2208. ROT |M ROT ( a2 h l crc )
  2209. _s+crc _s+crc ( a2 crc )
  2210. SWAP |M ROT ( h l crc )
  2211. _s+crc _s+crc ( crc )
  2212. 1 OR ( ensure stop bit )
  2213. (spix) DROP ( send CRC )
  2214. _wait ( wait for a valid response... )
  2215. ;
  2216. ( ----- 429 )
  2217. ( cmd arg1 arg2 -- r )
  2218. ( Send a command that expects a R1 response, handling CS. )
  2219. : SDCMDR1 [ SDC_DEVID LITN ] (spie) _cmd 0 (spie) ;
  2220. ( cmd arg1 arg2 -- r arg1 arg2 )
  2221. ( Send a command that expects a R7 response, handling CS. A R7
  2222. is a R1 followed by 4 bytes. arg1 contains bytes 0:1, arg2
  2223. has 2:3 )
  2224. : SDCMDR7
  2225. [ SDC_DEVID LITN ] (spie)
  2226. _cmd ( r )
  2227. _idle 8 LSHIFT _idle + ( r arg1 )
  2228. _idle 8 LSHIFT _idle + ( r arg1 arg2 )
  2229. 0 (spie)
  2230. ;
  2231. ( ----- 430 )
  2232. : _err 0 (spie) LIT" SDerr" ERR ;
  2233. ( Tight definition ahead, pre-comment.
  2234. Initialize a SD card. This should be called at least 1ms
  2235. after the powering up of the card. We begin by waking up the
  2236. SD card. After power up, a SD card has to receive at least
  2237. 74 dummy clocks with CS and DI high. We send 80.
  2238. Then send cmd0 for a maximum of 10 times, success is when
  2239. we get 0x01. Then comes the CMD8. We send it with a 0x01aa
  2240. argument and expect a 0x01aa argument back, along with a
  2241. 0x01 R1 response. After that, we need to repeatedly run
  2242. CMD55+CMD41 (0x40000000) until the card goes out of idle
  2243. mode, that is, when it stops sending us 0x01 response and
  2244. send us 0x00 instead. Any other response means that
  2245. initialization failed. )
  2246. ( ----- 431 )
  2247. : SDC$
  2248. 10 0 DO _idle DROP LOOP
  2249. 0 ( dummy ) 10 0 DO ( r )
  2250. DROP 0x40 0 0 SDCMDR1 ( CMD0 )
  2251. 1 = DUP IF LEAVE THEN
  2252. LOOP NOT IF _err THEN
  2253. 0x48 0 0x1aa ( CMD8 ) SDCMDR7 ( r arg1 arg2 )
  2254. ( expected 1 0 0x1aa )
  2255. 0x1aa = ROT ( arg1 f r ) 1 = AND SWAP ( f&f arg1 )
  2256. NOT ( 0 expected ) AND ( f&f&f ) NOT IF _err THEN
  2257. BEGIN
  2258. 0x77 0 0 SDCMDR1 ( CMD55 )
  2259. 1 = NOT IF _err THEN
  2260. 0x69 0x4000 0 SDCMDR1 ( CMD41 )
  2261. DUP 1 > IF _err THEN
  2262. NOT UNTIL ; ( out of idle mode, success! )
  2263. ( ----- 432 )
  2264. : _ ( dstaddr blkno -- )
  2265. [ SDC_DEVID LITN ] (spie)
  2266. 0x51 ( CMD17 ) 0 ROT ( a cmd 0 blkno ) _cmd
  2267. IF _err THEN
  2268. _wait 0xfe = NOT IF _err THEN
  2269. 0 SWAP ( crc a )
  2270. 512 0 DO ( crc a )
  2271. _idle ( crc a n )
  2272. DUP ROT C!+ ( crc n a+1 )
  2273. ROT> _crc16 ( a+1 crc )
  2274. SWAP ( crc a+1 )
  2275. LOOP
  2276. DROP ( crc1 )
  2277. _idle 8 LSHIFT _idle + ( crc2 )
  2278. _wait DROP 0 (spie)
  2279. = NOT IF _err THEN ;
  2280. ( ----- 433 )
  2281. : SDC@
  2282. 2 * DUP BLK( SWAP ( b a b ) _
  2283. 1+ BLK( 512 + SWAP _
  2284. ;
  2285. ( ----- 434 )
  2286. : _ ( srcaddr blkno -- )
  2287. [ SDC_DEVID LITN ] (spie)
  2288. 0x58 ( CMD24 ) 0 ROT ( a cmd 0 blkno ) _cmd
  2289. IF _err THEN
  2290. _idle DROP 0xfe (spix) DROP 0 SWAP ( crc a )
  2291. 512 0 DO ( crc a )
  2292. C@+ ( crc a+1 n )
  2293. ROT OVER ( a n crc n )
  2294. _crc16 ( a n crc )
  2295. SWAP ( a crc n )
  2296. (spix) DROP ( a crc )
  2297. SWAP ( crc a )
  2298. LOOP
  2299. DROP ( crc ) |M ( lsb msb )
  2300. (spix) DROP (spix) DROP
  2301. _wait DROP 0 (spie) ;
  2302. ( ----- 435 )
  2303. : SDC!
  2304. 2 * DUP BLK( SWAP ( b a b ) _
  2305. 1+ BLK( 512 + SWAP _
  2306. ;
  2307. ( ----- 440 )
  2308. 8086 boot code
  2309. Code in the following blocks assemble into a binary that is
  2310. suitable to plug into Core words (B350) to achieve a fully
  2311. functional Collapse OS. It is structured in a way that is
  2312. very similar to Z80 boot code (B280) and requires the same
  2313. constants to be pre-declared.
  2314. RESERVED REGISTERS: SP is reserved for PSP, BP is for RSP and
  2315. DX is for IP. Whenever you use these registers for another
  2316. purpose, be sure to protect their initial value. Like with
  2317. Z80, you can use SP freely in native code, but you have to make
  2318. sure it goes back to its previous level before next is called.
  2319. (cont.)
  2320. ( ----- 441 )
  2321. PS CHECKS: chkPS, is a bit different than in z80: it is para-
  2322. metrizable. The idea is that we always call chkPS, before pop-
  2323. ping, telling the expected size of stack. This allows for some
  2324. interesting optimization. For example, in SWAP, no need to pop,
  2325. chkPS, then push, we can chkPS and then proceed to optimized
  2326. swapping in PS.
  2327. Load range: B445-B461
  2328. ( ----- 445 )
  2329. VARIABLE lblexec VARIABLE lblnext
  2330. H@ ORG !
  2331. JMPn, 0 , ( 00, main ) 0 C, ( 03, boot driveno )
  2332. 0 , ( 04, BOOT )
  2333. 0 , ( 06, uflw ) 0 , ( 08, LATEST ) 0 , ( unused )
  2334. 0 C, 0 , ( 0b, EXIT )
  2335. 0 , 0 , ( unused ) 0 , ( 13, oflw )
  2336. 0 , 0 , 0 C, ( unused )
  2337. JMPn, 0 , ( 1a, next )
  2338. ( ----- 446 )
  2339. ( TODO: move these words with other native words. )
  2340. H@ 4 + XCURRENT ! ( make next CODE have 0 prev field )
  2341. CODE (br) L1 BSET ( used in ?br )
  2342. DI DX MOVxx, AL [DI] MOVr[], AH AH XORrr, CBW,
  2343. DX AX ADDxx,
  2344. ;CODE
  2345. CODE (?br)
  2346. AX POPx, AX AX ORxx, JZ, L1 @ RPCs, ( False, branch )
  2347. ( True, skip next byte and don't branch )
  2348. DX INCx,
  2349. ;CODE
  2350. ( ----- 447 )
  2351. CODE (loop)
  2352. [BP] 0 INC[w]+, ( I++ )
  2353. ( Jump if I <> I' )
  2354. AX [BP] 0 MOVx[]+, AX [BP] -2 CMPx[]+,
  2355. JNZ, L1 @ RPCs, ( branch )
  2356. ( don't branch )
  2357. BP 4 SUBxi, DX INCx,
  2358. ;CODE
  2359. ( ----- 448 )
  2360. lblnext BSET PC 0x1d - ORG @ 0x1b + ! ( next )
  2361. ( ovfl check )
  2362. BP SP CMPxx,
  2363. IFNC, ( BP >= SP )
  2364. SP PS_ADDR MOVxI, BP RS_ADDR MOVxI,
  2365. DI 0x13 ( oflw ) MOVxm, JMPs, L1 FWRs ( execute )
  2366. THEN,
  2367. DI DX MOVxx, ( <-- IP ) DX INCx, DX INCx,
  2368. DI [DI] MOVx[], ( wordref )
  2369. ( continue to execute ) L1 FSET
  2370. ( ----- 449 )
  2371. lblexec BSET ( DI -> wordref )
  2372. AL [DI] MOVr[], DI INCx, ( PFA )
  2373. AL AL ORrr, IFZ, DI JMPr, THEN, ( native )
  2374. AL DECr, IFNZ, ( not compiled )
  2375. AL DECr, IFZ, ( cell )
  2376. DI PUSHx, JMPs, lblnext @ RPCs, THEN,
  2377. AL DECr, IFZ, ( does )
  2378. DI PUSHx, DI INCx, DI INCx, DI [DI] MOVx[], THEN,
  2379. ( alias or ialias ) DI [DI] MOVx[],
  2380. AL DECr, IFNZ, ( ialias ) DI [DI] MOVx[], THEN,
  2381. JMPs, lblexec @ RPCs,
  2382. THEN, ( continue to compiled )
  2383. BP INCx, BP INCx, [BP] 0 DX MOV[]+x, ( pushRS )
  2384. DX DI MOVxx, DX INCx, DX INCx, ( --> IP )
  2385. DI [DI] MOVx[], JMPs, lblexec @ RPCs,
  2386. ( ----- 450 )
  2387. lblchkPS BSET ( CX -> expected size )
  2388. AX PS_ADDR MOVxI, AX SP SUBxx, 2 SUBAXI, ( CALL adjust )
  2389. AX CX CMPxx,
  2390. IFNC, ( we're good ) RET, THEN,
  2391. ( underflow ) DI 0x06 MOVxm, JMPs, lblexec @ RPCs,
  2392. PC 3 - ORG @ 1+ ! ( main )
  2393. DX POPx, ( boot drive no ) 0x03 DL MOVmr,
  2394. SP PS_ADDR MOVxI, BP RS_ADDR MOVxI,
  2395. DI 0x08 MOVxm, ( LATEST )
  2396. ( HERE begins at CURRENT )
  2397. SYSVARS 0x4 ( HERE ) + DI MOVmx,
  2398. SYSVARS 0x2 ( CURRENT ) + DI MOVmx,
  2399. DI 0x04 ( BOOT ) MOVxm,
  2400. JMPn, lblexec @ RPCn, ( execute )
  2401. ( ----- 451 )
  2402. ( native words )
  2403. CODE EXECUTE 1 chkPS,
  2404. DI POPx, JMPn, lblexec @ RPCn,
  2405. CODE EXIT
  2406. DX [BP] 0 MOVx[]+, BP DECx, BP DECx, ( popRS )
  2407. ;CODE
  2408. ( ----- 452 )
  2409. CODE (n) ( number literal )
  2410. DI DX MOVxx, DI [DI] MOVx[], DI PUSHx,
  2411. DX INCx, DX INCx,
  2412. ;CODE
  2413. CODE (s) ( string literal, see B287 )
  2414. DI DX MOVxx, ( IP )
  2415. AH AH XORrr, AL [DI] MOVr[], ( slen )
  2416. DX PUSHx, DX INCx, DX AX ADDxx,
  2417. ;CODE
  2418. ( ----- 453 )
  2419. CODE >R 1 chkPS,
  2420. BP INCx, BP INCx, [BP] 0 POP[w]+,
  2421. ;CODE NOP, NOP, NOP,
  2422. CODE R>
  2423. [BP] 0 PUSH[w]+, BP DECx, BP DECx,
  2424. ;CODE
  2425. CODE 2>R
  2426. [BP] 4 POP[w]+, [BP] 2 POP[w]+, BP 4 ADDxi,
  2427. ;CODE
  2428. CODE 2R> 2 chkPS,
  2429. [BP] -2 PUSH[w]+, [BP] 0 PUSH[w]+, BP 4 SUBxi,
  2430. ;CODE
  2431. ( ----- 454 )
  2432. CODE ROT ( a b c -- b c a ) 3 chkPS,
  2433. CX POPx, BX POPx, AX POPx,
  2434. BX PUSHx, CX PUSHx, AX PUSHx, ;CODE
  2435. CODE ROT> ( a b c -- c a b ) 3 chkPS,
  2436. CX POPx, BX POPx, AX POPx,
  2437. CX PUSHx, AX PUSHx, BX PUSHx, ;CODE
  2438. CODE DUP 1 chkPS, AX POPx, AX PUSHx, AX PUSHx, ;CODE
  2439. CODE ?DUP 1 chkPS, AX POPx, AX AX ORxx, AX PUSHx,
  2440. IFNZ, AX PUSHx, THEN, ;CODE
  2441. CODE OVER ( a b -- a b a ) 2 chkPS,
  2442. DI SP MOVxx, AX [DI] 2 MOVx[]+, AX PUSHx, ;CODE
  2443. CODE PICK
  2444. DI POPx, DI SHLx1, ( x2 )
  2445. CX DI MOVxx, CX 2 ADDxi, CALL, lblchkPS @ RPCn,
  2446. DI SP ADDxx, DI [DI] MOVx[], DI PUSHx,
  2447. ;CODE
  2448. ( ----- 455 )
  2449. CODE (roll) ( "2 3 4 5 4 --> 2 4 5 5". See B311 )
  2450. CX POPx, CX 2 ADDxi, CALL, lblchkPS @ RPCn, CX 2 SUBxi,
  2451. SI SP MOVxx, SI CX ADDxx,
  2452. DI SI MOVxx, DI 2 ADDxi, STD, REPZ, MOVSB,
  2453. ;CODE
  2454. CODE SWAP AX POPx, BX POPx, AX PUSHx, BX PUSHx, ;CODE
  2455. CODE DROP 1 chkPS, AX POPx, ;CODE
  2456. CODE 2DROP 2 chkPS, SP 4 ADDxi, ;CODE
  2457. CODE 2DUP 2 chkPS,
  2458. AX POPx, BX POPx,
  2459. BX PUSHx, AX PUSHx, BX PUSHx, AX PUSHx,
  2460. ;CODE
  2461. CODE S0 AX PS_ADDR MOVxI, AX PUSHx, ;CODE
  2462. CODE 'S SP PUSHx, ;CODE
  2463. CODE AND 2 chkPS,
  2464. AX POPx, BX POPx, AX BX ANDxx, AX PUSHx, ;CODE
  2465. ( ----- 456 )
  2466. CODE OR 2 chkPS,
  2467. AX POPx, BX POPx, AX BX ORxx, AX PUSHx, ;CODE
  2468. CODE XOR 2 chkPS,
  2469. AX POPx, BX POPx, AX BX XORxx, AX PUSHx, ;CODE
  2470. CODE NOT 1 chkPS,
  2471. AX POPx, AX AX ORxx,
  2472. IFNZ, AX -1 MOVxI, THEN, AX INCx, AX PUSHx, ;CODE
  2473. CODE + 2 chkPS,
  2474. AX POPx, BX POPx, AX BX ADDxx, AX PUSHx, ;CODE
  2475. CODE - 2 chkPS,
  2476. BX POPx, AX POPx, AX BX SUBxx, AX PUSHx, ;CODE
  2477. CODE * 2 chkPS,
  2478. AX POPx, BX POPx,
  2479. DX PUSHx, ( protect from MUL ) BX MULx, DX POPx,
  2480. AX PUSHx, ;CODE
  2481. ( ----- 457 )
  2482. CODE /MOD 2 chkPS,
  2483. BX POPx, AX POPx, DX PUSHx, ( protect )
  2484. DX DX XORxx, BX DIVx,
  2485. BX DX MOVxx, DX POPx, ( unprotect )
  2486. BX PUSHx, ( modulo ) AX PUSHx, ( division )
  2487. ;CODE
  2488. CODE ! 2 chkPS, DI POPx, AX POPx, [DI] AX MOV[]x, ;CODE
  2489. CODE @ 1 chkPS, DI POPx, AX [DI] MOVx[], AX PUSHx, ;CODE
  2490. CODE C! 2 chkPS, DI POPx, AX POPx, [DI] AX MOV[]r, ;CODE
  2491. CODE C@ 1 chkPS,
  2492. DI POPx, AH AH XORrr, AL [DI] MOVr[], AX PUSHx, ;CODE
  2493. CODE I [BP] 0 PUSH[w]+, ;CODE
  2494. CODE I' [BP] -2 PUSH[w]+, ;CODE
  2495. CODE J [BP] -4 PUSH[w]+, ;CODE
  2496. CODE (resSP) SP PS_ADDR MOVxI, ;CODE
  2497. CODE (resRS) BP RS_ADDR MOVxI, ;CODE
  2498. ( ----- 458 )
  2499. CODE BYE HLT, BEGIN, JMPs, AGAIN, ;CODE
  2500. CODE S= 2 chkPS,
  2501. SI POPx, DI POPx, CH CH XORrr, CL [SI] MOVr[],
  2502. CL [DI] CMPr[],
  2503. IFZ, ( same size? )
  2504. SI INCx, DI INCx, CLD, REPZ, CMPSB,
  2505. THEN,
  2506. PUSHZ,
  2507. ;CODE
  2508. CODE CMP 2 chkPS,
  2509. BX POPx, AX POPx, CX CX XORxx, AX BX CMPxx,
  2510. IFNZ, ( < or > )
  2511. CX INCx, IFC, ( < ) CX DECx, CX DECx, THEN,
  2512. THEN,
  2513. CX PUSHx,
  2514. ;CODE
  2515. ( ----- 459 )
  2516. CODE _find ( cur w -- a f ) 2 chkPS,
  2517. SI POPx, ( w ) DI POPx, ( cur )
  2518. CH CH XORrr, CL [SI] MOVr[], ( CX -> strlen )
  2519. SI INCx, ( first char ) AX AX XORxx, ( initial prev )
  2520. BEGIN, ( loop )
  2521. DI AX SUBxx, ( jump to prev wordref )
  2522. AL [DI] -1 MOVr[]+, 0x7f ANDALi, ( strlen )
  2523. CL AL CMPrr, IFZ, ( same len )
  2524. SI PUSHx, DI PUSHx, CX PUSHx, ( --> lvl 3 )
  2525. 3 ADDALi, ( header ) AH AH XORrr, DI AX SUBxx,
  2526. CLD, REPZ, CMPSB,
  2527. CX POPx, DI POPx, SI POPx, ( <-- lvl 3 )
  2528. IFZ, DI PUSHx, AX 1 MOVxI, AX PUSHx,
  2529. JMPn, lblnext @ RPCn, THEN,
  2530. THEN,
  2531. DI 3 SUBxi, AX [DI] MOVx[], ( prev ) AX AX ORxx, ( cont. )
  2532. ( ----- 460 )
  2533. ( cont. find ) JNZ, AGAIN, ( loop )
  2534. SI DECx, SI PUSHx, AX AX XORrr, AX PUSHx,
  2535. ;CODE
  2536. CODE 0 AX AX XORxx, AX PUSHx, ;CODE
  2537. CODE 1 AX 1 MOVxI, AX PUSHx, ;CODE
  2538. CODE -1 AX -1 MOVxI, AX PUSHx, ;CODE
  2539. CODE 1+ 1 chkPS, DI SP MOVxx, [DI] INC[w], ;CODE
  2540. CODE 1- 1 chkPS, DI SP MOVxx, [DI] DEC[w], ;CODE
  2541. CODE 2+ 1 chkPS, DI SP MOVxx, [DI] INC[w], [DI] INC[w], ;CODE
  2542. CODE 2- 1 chkPS, DI SP MOVxx, [DI] DEC[w], [DI] DEC[w], ;CODE
  2543. CODE RSHIFT ( n u -- n ) 2 chkPS,
  2544. CX POPx, AX POPx, AX SHRxCL, AX PUSHx, ;CODE
  2545. CODE LSHIFT ( n u -- n ) 2 chkPS,
  2546. CX POPx, AX POPx, AX SHLxCL, AX PUSHx, ;CODE
  2547. ( ----- 461 )
  2548. ( See comment in B321. TODO: test on real hardware. in qemu,
  2549. the resulting delay is more than 10x too long. )
  2550. CODE TICKS 1 chkPS, ( n=100us )
  2551. SI DX MOVxx, ( protect IP )
  2552. AX POPx, BX 100 MOVxI, BX MULx,
  2553. CX DX MOVxx, ( high ) DX AX MOVxx, ( low )
  2554. AX 0x8600 MOVxI, ( 86h, WAIT ) 0x15 INT,
  2555. DX SI MOVxx, ( restore IP )
  2556. ;CODE
  2557. CODE |M ( n -- lsb msb ) 1 chkPS,
  2558. CX POPx, AH 0 MOVri,
  2559. AL CL MOVrr, AX PUSHx, AL CH MOVrr, AX PUSHx, ;CODE
  2560. CODE |L ( n -- msb lsb ) 1 chkPS,
  2561. CX POPx, AH 0 MOVri,
  2562. AL CH MOVrr, AX PUSHx, AL CL MOVrr, AX PUSHx, ;CODE
  2563. ( ----- 470 )
  2564. ( Z80 driver for TMS9918. Implements grid protocol. Requires
  2565. TMS_CTLPORT, TMS_DATAPORT and ~FNT from the Font compiler at
  2566. B520. Patterns are at addr 0x0000, Names are at 0x3800.
  2567. Load range B470-472 )
  2568. CODE _ctl ( a -- sends LSB then MSB )
  2569. HL POP, chkPS,
  2570. A L LDrr, TMS_CTLPORT OUTiA,
  2571. A H LDrr, TMS_CTLPORT OUTiA,
  2572. ;CODE
  2573. CODE _data
  2574. HL POP, chkPS,
  2575. A L LDrr, TMS_DATAPORT OUTiA,
  2576. ;CODE
  2577. ( ----- 471 )
  2578. : _zero ( x -- send 0 _data x times )
  2579. ( x ) 0 DO 0 _data LOOP ;
  2580. ( Each row in ~FNT is a row of the glyph and there is 7 of
  2581. them. We insert a blank one at the end of those 7. )
  2582. : _sfont ( a -- Send font to TMS )
  2583. 7 0 DO C@+ _data LOOP DROP
  2584. ( blank row ) 0 _data ;
  2585. : _sfont^ ( a -- Send inverted font to TMS )
  2586. 7 0 DO C@+ 0xff XOR _data LOOP DROP
  2587. ( blank row ) 0xff _data ;
  2588. : CELL! ( c pos )
  2589. 0x7800 OR _ctl ( tilenum )
  2590. SPC - ( glyph ) 0x5f MOD _data ;
  2591. ( ----- 472 )
  2592. : CURSOR! ( new old -- )
  2593. DUP 0x3800 OR _ctl [ TMS_DATAPORT LITN ] PC@
  2594. 0x7f AND ( new old glyph ) SWAP 0x7800 OR _ctl _data
  2595. DUP 0x3800 OR _ctl [ TMS_DATAPORT LITN ] PC@
  2596. 0x80 OR ( new glyph ) SWAP 0x7800 OR _ctl _data ;
  2597. : COLS 40 ; : LINES 24 ;
  2598. : TMS$
  2599. 0x8100 _ctl ( blank screen )
  2600. 0x7800 _ctl COLS LINES * _zero
  2601. 0x4000 _ctl 0x5f 0 DO ~FNT I 7 * + _sfont LOOP
  2602. 0x4400 _ctl 0x5f 0 DO ~FNT I 7 * + _sfont^ LOOP
  2603. 0x820e _ctl ( name table 0x3800 )
  2604. 0x8400 _ctl ( pattern table 0x0000 )
  2605. 0x87f0 _ctl ( colors 0 and 1 )
  2606. 0x8000 _ctl 0x81d0 _ctl ( text mode, display on ) ;
  2607. ( ----- 520 )
  2608. Fonts
  2609. Fonts are kept in "source" form in the following blocks and
  2610. then compiled to binary bitmasks by the following code. In
  2611. source form, fonts are a simple sequence of '.' and 'X'. '.'
  2612. means empty, 'X' means filled. Glyphs are entered one after the
  2613. other, starting at 0x21 and ending at 0x7e. To be space
  2614. efficient in blocks, we align glyphs horizontally in the blocks
  2615. to fit as many character as we can. For example, a 5x7 font
  2616. would mean that we would have 12x2 glyphs per block.
  2617. 521 Font compiler 530 3x5 font
  2618. 532 5x7 font 536 7x7 font
  2619. ( ----- 521 )
  2620. ( Converts "dot-X" fonts to binary "glyph rows". One byte for
  2621. each row. In a 5x7 font, each glyph thus use 7 bytes.
  2622. Resulting bytes are aligned to the left of the byte.
  2623. Therefore, for a 5-bit wide char, "X.X.X" translates to
  2624. 0b10101000. Left-aligned bytes are easier to work with when
  2625. compositing glyphs. )
  2626. ( ----- 522 )
  2627. : _g ( given a top-left of dot-X in BLK(, spit 5 bin lines )
  2628. 5 0 DO
  2629. 0 3 0 DO ( a r )
  2630. 1 LSHIFT
  2631. OVER J 64 * I + + C@ 'X' = IF 1+ THEN
  2632. LOOP 5 LSHIFT C, LOOP DROP ;
  2633. : _l ( a u -- a, spit a line of u glyphs )
  2634. ( u ) 0 DO ( a )
  2635. DUP I 3 * + _g
  2636. LOOP ;
  2637. : CPFNT3x5
  2638. 0 , 0 , 0 C, ( space char )
  2639. 530 BLK@ BLK( 21 _l 320 + 21 _l 320 + 21 _l DROP ( 63 )
  2640. 531 BLK@ BLK( 21 _l 320 + 10 _l DROP ( 94! )
  2641. ;
  2642. ( ----- 523 )
  2643. : _g ( given a top-left of dot-X in BLK(, spit 7 bin lines )
  2644. 7 0 DO
  2645. 0 5 0 DO ( a r )
  2646. 1 LSHIFT
  2647. OVER J 64 * I + + C@ 'X' = IF 1+ THEN
  2648. LOOP 3 LSHIFT C, LOOP DROP ;
  2649. : _l ( a u -- a, spit a line of u glyphs )
  2650. ( u ) 0 DO ( a )
  2651. DUP I 5 * + _g
  2652. LOOP ;
  2653. : CPFNT5x7
  2654. 0 , 0 , 0 , 0 C, ( space char )
  2655. 535 532 DO I BLK@ BLK( 12 _l 448 + 12 _l DROP LOOP ( 72 )
  2656. 535 BLK@ BLK( 12 _l 448 + 10 _l DROP ( 94! )
  2657. ;
  2658. ( ----- 524 )
  2659. : _g ( given a top-left of dot-X in BLK(, spit 7 bin lines )
  2660. 7 0 DO
  2661. 0 7 0 DO ( a r )
  2662. 1 LSHIFT
  2663. OVER J 64 * I + + C@ 'X' = IF 1+ THEN
  2664. LOOP 1 LSHIFT C, LOOP DROP ;
  2665. : _l ( a u -- a, spit a line of u glyphs )
  2666. ( u ) 0 DO ( a )
  2667. DUP I 7 * + _g
  2668. LOOP ;
  2669. : CPFNT7x7
  2670. 0 , 0 , 0 , 0 C, ( space char )
  2671. 541 536 DO I BLK@ BLK( 9 _l 448 + 9 _l DROP LOOP ( 90 )
  2672. 542 BLK@ BLK( 4 _l DROP ( 94! )
  2673. ;
  2674. ( ----- 530 )
  2675. .X.X.XX.X.XXX...X..X...XX...X...............X.X..X.XX.XX.X.XXXX
  2676. .X.X.XXXXXX...XX.X.X..X..X.XXX.X............XX.XXX...X..XX.XX..
  2677. .X........XX.X..X.....X..X..X.XXX...XXX....X.X.X.X..X.XX.XXXXX.
  2678. ......XXXXX.X..X.X....X..X.X.X.X..X.......X..X.X.X.X....X..X..X
  2679. .X....X.X.X...X.XX.....XX........X......X.X...X.XXXXXXXX...XXX.
  2680. .XXXXXXXXXXX........X...X..XX..X..X.XX..XXXX.XXXXXX.XXX.XXXXXXX
  2681. X....XX.XX.X.X..X..X.XXX.X...XXXXX.XX.XX..X.XX..X..X..X.X.X...X
  2682. XXX.X.XXXXXX......X.......X.X.XXXXXXXX.X..X.XXX.XX.X.XXXX.X...X
  2683. X.XX..X.X..X.X..X..X.XXX.X....X..X.XX.XX..X.XX..X..X.XX.X.X...X
  2684. XXXX..XXXXX....X....X...X...X..XXX.XXX..XXXX.XXXX...XXX.XXXXXX.
  2685. X.XX..X.XXX.XXXXX.XXXXX..XXXXXX.XX.XX.XX.XX.XXXXXXXX..XXX.X....
  2686. XX.X..XXXX.XX.XX.XX.XX.XX...X.X.XX.XX.XX.XX.X..XX..X....XX.X...
  2687. X..X..XXXX.XX.XXX.X.XXX..X..X.X.XX.XXXX.X..X..X.X...X...X......
  2688. XX.X..X.XX.XX.XX..XXXX.X..X.X.X.XX.XXXXX.X.X.X..X....X..X......
  2689. X.XXXXX.XX.XXXXX...XXX.XXX..X.XXX.X.X.XX.X.X.XXXXXX..XXXX...XXX
  2690. !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
  2691. ( ----- 531 )
  2692. X.....X.......X....XX...X...X...XX..XX.......................X.
  2693. .X.XX.X...XX..X.X.X...X.X........X.X.X.X.XXX..X.XX..XX.XX.XXXXX
  2694. .....XXX.X...XXX.XXX.X.XXX..X...XXX..X.XXXX.XX.XX.XX.XX..XX..X.
  2695. ...XXXX.XX..X.XXX.X...XXX.X.X...XX.X.X.X.XX.XX.XXX..XXX....X.X.
  2696. ...XXXXX..XX.XX.XXX..XX.X.X.X.XX.X.X.XXX.XX.X.X.X....XX..XX..XX
  2697. ...................XX.X.XX.....................................
  2698. X.XX.XX.XX.XX.XXXX.X..X..X..XX
  2699. X.XX.XX.X.X..X..XXX...X...XXX.
  2700. X.XX.XXXX.X..X.XX..X..X..X....
  2701. XXX.X.X.XX.X.X.XXX.XX.X.XX....
  2702. `abcdefghijklmnopqrstuvwxyz{|}~
  2703. ( ----- 532 )
  2704. ..X...X.X........X..............X....X....X.................
  2705. ..X...X.X..X.X..XXXXX...X.XX....X...X......X.X.X.X..X.......
  2706. ..X.......XXXXXX.......X.X..X......X........X.XXX...X.......
  2707. ..X........X.X..XXX...X...XX.......X........XXXXXXXXXXX.....
  2708. ..........XXXXX....X.X....XX.X.....X........X.XXX...X.......
  2709. ..X........X.X.XXXX.X...XX..X.......X......X.X.X.X..X.....X.
  2710. ..X..............X.......XXX.X.......X....X..............X..
  2711. ................XXX...XX..XXX..XXX...XX.XXXXX.XXX.XXXXX.XXX.
  2712. ..............XX...X.X.X.X...XX...X.X.X.X....X........XX...X
  2713. .............X.X..XX...X.....X....XX..X.XXXX.X........XX...X
  2714. XXXXX.......X..X.X.X...X....X...XX.XXXXX....XXXXX....X..XXX.
  2715. ...........X...XX..X...X...X......X...X.....XX...X..X..X...X
  2716. ......XX..X....X...X...X..X...X...X...X.X...XX...X.X...X...X
  2717. ......XX........XXX..XXXXXXXXX.XXX....X..XXX..XXX.X.....XXX.
  2718. !"#$%&'()*+,-./012345678
  2719. ( ----- 533 )
  2720. .XXX...............X.....X.....XXX..XXX..XXX.XXXX..XXX.XXXX.
  2721. X...X..X....X....XX.......XX..X...XX...XX...XX...XX...XX...X
  2722. X...X..X....X...XX..XXXXX..XX.....XX..XXX...XX...XX....X...X
  2723. .XXX...........X.............X...X.X..XXXXXXXXXXX.X....X...X
  2724. ....X..X....X...XX..XXXXX..XX...X..X....X...XX...XX....X...X
  2725. ....X..X...X.....XX.......XX.......X...XX...XX...XX...XX...X
  2726. .XXX...............X.....X......X...XXX.X...XXXXX..XXX.XXXX.
  2727. XXXXXXXXXX.XXX.X...X.XXX....XXX..X.X....X...XX...X.XXX.XXXX.
  2728. X....X....X...XX...X..X......XX.X..X....XX.XXXX..XX...XX...X
  2729. X....X....X....X...X..X......XXX...X....X.X.XXX..XX...XX...X
  2730. XXXX.XXXX.X..XXXXXXX..X......XX....X....X...XX.X.XX...XXXXX.
  2731. X....X....X...XX...X..X......XXX...X....X...XX..XXX...XX....
  2732. X....X....X...XX...X..X..X...XX.X..X....X...XX..XXX...XX....
  2733. XXXXXX.....XXX.X...X.XXX..XXX.X..X.XXXXXX...XX...X.XXX.X....
  2734. 9:;<=>?@ABCDEFGHIJKLMNOP
  2735. ( ----- 534 )
  2736. .XXX.XXXX..XXX.XXXXXX...XX...XX...XX...XX...XXXXXXXXX.......
  2737. X...XX...XX...X..X..X...XX...XX...XX...XX...XX...XX....X....
  2738. X...XX...XX......X..X...XX...XX...X.X.X..X.X....X.X.....X...
  2739. X...XXXXX..XXX...X..X...XX...XX...X..X....X....X..X......X..
  2740. X.X.XX.X......X..X..X...XX...XX.X.X.X.X...X...X...X.......X.
  2741. X..XXX..X.X...X..X..X...X.X.X.X.X.XX...X..X..X...XX........X
  2742. .XXXXX...X.XXX...X...XXX...X...X.X.X...X..X..XXXXXXXX.......
  2743. ..XXX..X.........X..........................................
  2744. ....X.X.X.........X.........................................
  2745. ....XX...X...........XXX.X.....XXX.....X.XXX..XX....XXXX....
  2746. ....X...................XX....X...X....XX...XX..X..X..XX....
  2747. ....X................XXXXXXX..X......XXXXXXXXX......XXXXXX..
  2748. ....X...............X...XX..X.X...X.X..XX....XXX......XX..X.
  2749. ..XXX.....XXXXX......XXXXXXX...XXX...XXX.XXXXX......XX.X..X.
  2750. QRSTUVWXYZ[\]^_`abcdefgh
  2751. ( ----- 535 )
  2752. ............................................................
  2753. ............................................................
  2754. ..X......XX..X..XX...X.X.XXX...XXX.XXX....XXXX.XX..XXX..X...
  2755. ..........X.X....X..X.X.XX..X.X...XX..X..X..XXX...X....XXX..
  2756. ..X......XXX.....X..X...XX...XX...XXXX....XXXX.....XXX..X...
  2757. ..X...X..XX.X....X..X...XX...XX...XX........XX........X.X...
  2758. ..X....XX.X..X...XX.X...XX...X.XXX.X........XX.....XXX...XX.
  2759. ................................XX...X...XX.......
  2760. ...............................X.....X.....X......
  2761. X...XX...XX...XX...XX...XXXXXX.X.....X.....X..X.X.
  2762. X...XX...XX...X.X.X..X.X....X.X......X......XX.X..
  2763. X...XX...XX...X..X....X....X...X.....X.....X......
  2764. X...X.X.X.X.X.X.X.X..X....X....X.....X.....X......
  2765. .XXX...X...X.X.X...XX....XXXXX..XX...X...XX.......
  2766. ijklmnopqrstuvwxyz{|}~
  2767. ( ----- 536 )
  2768. ..XX....XX.XX..XX.XX....XX..XX......XXX......XX.....XX...XX....
  2769. ..XX....XX.XX..XX.XX..XXXXXXXX..XX.XX.XX....XX.....XX.....XX...
  2770. ..XX....XX.XX.XXXXXXXXX.X......XX..XX.XX...XX.....XX.......XX..
  2771. ..XX...........XX.XX..XXXXX...XX....XXX...........XX.......XX..
  2772. ..XX..........XXXXXXX...X.XX.XX....XX.XX.X........XX.......XX..
  2773. ...............XX.XX.XXXXXX.XX..XX.XX..XX..........XX.....XX...
  2774. ..XX...........XX.XX...XX.......XX..XXX.XX..........XX...XX....
  2775. ...........................................XXXX....XX....XXXX..
  2776. ..XX.....XX............................XX.XX..XX..XXX...XX..XX.
  2777. XXXXXX...XX...........................XX..XX.XXX...XX.......XX.
  2778. .XXXX..XXXXXX........XXXXXX..........XX...XXXXXX...XX......XX..
  2779. XXXXXX...XX.........................XX....XXX.XX...XX.....XX...
  2780. ..XX.....XX.....XX............XX...XX.....XX..XX...XX....XX....
  2781. ...............XX.............XX...........XXXX..XXXXXX.XXXXXX.
  2782. !"#$%&'()*+,-./012
  2783. ( ----- 537 )
  2784. .XXXX.....XX..XXXXXX...XXX..XXXXXX..XXXX...XXXX................
  2785. XX..XX...XXX..XX......XX........XX.XX..XX.XX..XX...............
  2786. ....XX..XXXX..XXXXX..XX........XX..XX..XX.XX..XX...XX.....XX...
  2787. ..XXX..XX.XX......XX.XXXXX....XX....XXXX...XXXXX...XX.....XX...
  2788. ....XX.XXXXXX.....XX.XX..XX..XX....XX..XX.....XX...............
  2789. XX..XX....XX..XX..XX.XX..XX..XX....XX..XX....XX....XX.....XX...
  2790. .XXXX.....XX...XXXX...XXXX...XX.....XXXX...XXX.....XX....XX....
  2791. ...XX.........XX......XXXX...XXXX...XXXX..XXXXX...XXXX..XXXX...
  2792. ..XX...........XX....XX..XX.XX..XX.XX..XX.XX..XX.XX..XX.XX.XX..
  2793. .XX....XXXXXX...XX......XX..XX.XXX.XX..XX.XX..XX.XX.....XX..XX.
  2794. XX...............XX....XX...XX.X.X.XXXXXX.XXXXX..XX.....XX..XX.
  2795. .XX....XXXXXX...XX.....XX...XX.XXX.XX..XX.XX..XX.XX.....XX..XX.
  2796. ..XX...........XX...........XX.....XX..XX.XX..XX.XX..XX.XX.XX..
  2797. ...XX.........XX.......XX....XXXX..XX..XX.XXXXX...XXXX..XXXX...
  2798. 3456789:;<=>?@ABCD
  2799. ( ----- 538 )
  2800. XXXXXX.XXXXXX..XXXX..XX..XX.XXXXXX..XXXXX.XX..XX.XX.....XX...XX
  2801. XX.....XX.....XX..XX.XX..XX...XX......XX..XX.XX..XX.....XXX.XXX
  2802. XX.....XX.....XX.....XX..XX...XX......XX..XXXX...XX.....XXXXXXX
  2803. XXXXX..XXXXX..XX.XXX.XXXXXX...XX......XX..XXX....XX.....XX.X.XX
  2804. XX.....XX.....XX..XX.XX..XX...XX......XX..XXXX...XX.....XX.X.XX
  2805. XX.....XX.....XX..XX.XX..XX...XX...XX.XX..XX.XX..XX.....XX...XX
  2806. XXXXXX.XX......XXXX..XX..XX.XXXXXX..XXX...XX..XX.XXXXXX.XX...XX
  2807. XX..XX..XXXX..XXXXX...XXXX..XXXXX...XXXX..XXXXXX.XX..XX.XX..XX.
  2808. XX..XX.XX..XX.XX..XX.XX..XX.XX..XX.XX..XX...XX...XX..XX.XX..XX.
  2809. XXX.XX.XX..XX.XX..XX.XX..XX.XX..XX.XX.......XX...XX..XX.XX..XX.
  2810. XXXXXX.XX..XX.XXXXX..XX..XX.XXXXX...XXXX....XX...XX..XX.XX..XX.
  2811. XX.XXX.XX..XX.XX.....XX.X.X.XX.XX......XX...XX...XX..XX.XX..XX.
  2812. XX..XX.XX..XX.XX.....XX.XX..XX..XX.XX..XX...XX...XX..XX..XXXX..
  2813. XX..XX..XXXX..XX......XX.XX.XX..XX..XXXX....XX....XXXX....XX...
  2814. EFGHIJKLMNOPQRSTUVWXYZ[\]^_
  2815. ( ----- 539 )
  2816. XX...XXXX..XX.XX..XX.XXXXXX.XXXXX.........XXXXX....XX..........
  2817. XX...XXXX..XX.XX..XX.....XX.XX.....XX........XX...XXXX.........
  2818. XX.X.XX.XXXX..XX..XX....XX..XX......XX.......XX..XX..XX........
  2819. XX.X.XX..XX....XXXX....XX...XX.......XX......XX..X....X........
  2820. XXXXXXX.XXXX....XX....XX....XX........XX.....XX................
  2821. XXX.XXXXX..XX...XX...XX.....XX.........XX....XX................
  2822. XX...XXXX..XX...XX...XXXXXX.XXXXX.........XXXXX.........XXXXXXX
  2823. .XX...........XX................XX..........XXX.........XX.....
  2824. ..XX..........XX................XX.........XX.....XXXX..XX.....
  2825. ...XX...XXXX..XXXXX...XXXX...XXXXX..XXXX...XX....XX..XX.XXXXX..
  2826. ...........XX.XX..XX.XX..XX.XX..XX.XX..XX.XXXXX..XX..XX.XX..XX.
  2827. ........XXXXX.XX..XX.XX.....XX..XX.XXXXXX..XX.....XXXXX.XX..XX.
  2828. .......XX..XX.XX..XX.XX..XX.XX..XX.XX......XX........XX.XX..XX.
  2829. ........XXXXX.XXXXX...XXXX...XXXXX..XXXX...XX.....XXX...XX..XX.
  2830. WXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
  2831. ( ----- 540 )
  2832. ..XX.....XX...XX......XXX......................................
  2833. ..............XX.......XX......................................
  2834. .XXX....XXX...XX..XX...XX....XX.XX.XXXXX...XXXX..XXXXX...XXXXX.
  2835. ..XX.....XX...XX.XX....XX...XXXXXXXXX..XX.XX..XX.XX..XX.XX..XX.
  2836. ..XX.....XX...XXXX.....XX...XX.X.XXXX..XX.XX..XX.XX..XX.XX..XX.
  2837. ..XX.....XX...XX.XX....XX...XX.X.XXXX..XX.XX..XX.XXXXX...XXXXX.
  2838. .XXXX..XX.....XX..XX..XXXX..XX...XXXX..XX..XXXX..XX.........XX.
  2839. ...............XX..............................................
  2840. ...............XX..............................................
  2841. XX.XX...XXXXX.XXXXX..XX..XX.XX..XX.XX...XXXX..XX.XX..XX.XXXXXX.
  2842. XXX.XX.XX......XX....XX..XX.XX..XX.XX.X.XX.XXXX..XX..XX....XX..
  2843. XX......XXXX...XX....XX..XX.XX..XX.XX.X.XX..XX...XX..XX...XX...
  2844. XX.........XX..XX....XX..XX..XXXX..XXXXXXX.XXXX...XXXXX..XX....
  2845. XX.....XXXXX....XXX...XXXXX...XX....XX.XX.XX..XX.....XX.XXXXXX.
  2846. ijklmnopqrstuvwxyz{|}~
  2847. ( ----- 541 )
  2848. ...XX....XX...XX......XX...X
  2849. ..XX.....XX....XX....XX.X.XX
  2850. ..XX.....XX....XX....X...XX.
  2851. XXX......XX.....XXX.........
  2852. ..XX.....XX....XX...........
  2853. ..XX.....XX....XX...........
  2854. ...XX....XX...XX............
  2855. {|}~