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.

3070 lines
94KB

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