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.

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