prjbrook

forth85_33.asm S' for strings OK thus far

Sep 13th, 2014
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 94.58 KB | None | 0 0
  1. ;this is forth85_33 second go from _32 Tidies up forth85_33 first ver. Down to last 10ish words.
  2. ;Got if..else..endif going. Lots of byte calcs
  3.  
  4. ;.equ testing = 1 ;Very handy. Used a lot in AVR Studio4; makes io verbose. comment out later
  5. ;.equ livetesting = 1 ;Very handy when live; comment out to take out the little dumps and diagnostics.
  6.  
  7. .NOLIST
  8. .include "tn85def.inc"
  9. .LIST
  10. .include "macros.asm"
  11. .include "blockdefs.asm"
  12. ;---------------------------------------------------
  13. .def mylatest =r2 ;r2,r3 is mylatest
  14. .def myhere =r4 ;r4,r5 is myhere. The pointer to flash copy in buf2.
  15. .def SOFPG=r6 ;start of flash page
  16. ;r6,r7 byte adr of flash page (11c0)
  17. ;r8,r9 (0012) offset when flash comes into buf2. r8 +E0 = myhere
  18. .def SECONDLETTER =r10 ;helpful for debugging
  19. .def FOUNDCOUNTER = r11 ;dealWithWord clicks this if found =1. Counts successful finds in dictionary.
  20. .def STATE = r12
  21. .def STOP = r13 ;stop interpreting line of words
  22. .def BOTTOM = r14 ;have hit the bottom of the dict and not found a match
  23. .def FOUND = r15 ;if found=1 we have a match of Ram word on dictionary
  24. .def spmcsr_val=r18
  25. .def buf_ctr =r19 ;for flash section
  26. ;r20 is length of word in WORD
  27. ;r21 is the flash length of word with immediate bit 8, if any, still there
  28.  
  29. .def vl = r22
  30. .def vh = r23 ; u,v,w,x,y,z are all pointers
  31. .def wl = r24 ;w=r24,25
  32. .def wh = r25
  33.  
  34. .equ TX_PIN = 0
  35. .equ RX_PIN = 2 ; Tx,Rx pins are PB0 and PB2 resp
  36.  
  37. .def serialByteReg = r16
  38. .def rxByte = r18
  39. .def counterReg = r17
  40. ;---------------------------------------------------------------
  41. .eseg
  42. .org $10
  43. .dw HERE, LATEST , $0160 ;these should be burned into tn85 with code
  44. ;--------------------------------------------------------------------
  45. .DSEG
  46. .ORG 0x60
  47.  
  48. .equ BUF1LENGTH = 128
  49. .equ eHERE = $0010 ;eeprom adr of system varial eHere
  50. .equ eLATEST = $0012
  51. .equ eVar = $0014 ;holds next ram adr for next var declaration
  52.  
  53. buf1: .byte BUF1LENGTH ;input buffer. Lines max about 125
  54. buf2: .byte BUF1LENGTH ;this fits two flash buffers
  55. buf3: .byte 64 ;new for 5.8.14 Allows 3rd flash page. And 128 byte input buffer,buf1
  56. ;So buf1=060..0df,buf2=0e0..15f,buf3= 160..19f
  57. ;varspace=1a0..1df,mystack=1e0..ret stack space that ends at 25f (128 bytes for both stacks)
  58. varSpace: .byte 64 ;might need more than 32 variables
  59. myStackStart: .byte 64 ;currently at $1E0.Meets return stack.
  60. ;---------------------------------------------------------------------------------
  61. ;---------------------------------------------------------------------------------
  62. .CSEG
  63. .ORG 0x800 ;dictionary starts at 4K (2K words) mark
  64. ;----------------------------------------------------
  65. one_1:
  66. .db 0,0,3, "one" ;code for one
  67. one:
  68. ; rcall stackme
  69. rcall stackme_2
  70. .db 01, 00
  71. ret
  72. ;----------------------------------------------
  73. two_1:
  74. header one_1, 3, "two"
  75. two:
  76. rcall stackme_2
  77. .db 02,00
  78. ret
  79. ;------------------------------------------
  80. dup_1:
  81. header two_1,3,"dup"
  82. dup:
  83. mypop r17
  84. mypop r16
  85. mypush r16
  86. mypush r17
  87. mypush r16
  88. mypush r17
  89.  
  90. ret
  91. ;-------------------------------------------
  92. drop_1:
  93. header dup_1,4,"drop"
  94. drop:
  95. mypop r17
  96. mypop r16 ;TODO what if stack pointer goes thru floor?
  97. ret
  98. ;----------------------------------
  99. swapp_1: ;twp p's becasue assembler recognizes avr opcode swap
  100. header drop_1,4, "swap" ;rcall swapp but otherwise it's "swap"
  101. swapp:
  102. mypop2 r17,r16
  103. mypop2 r19,r18
  104. mypush2 r16,r17
  105. mypush2 r18,r19
  106. ret
  107.  
  108.  
  109. ;-------------------------------------------------
  110. ;shift this later
  111.  
  112. S_1:
  113. ;the EOL token that gets put into end of buf1 to stop parsing
  114. header swapp_1,$81,"S" ;NB always immediate
  115. S: ldi r16,02
  116. mov BOTTOM,r16 ;r14 =2 means a nice stop. EOL without errors
  117. clr STOP
  118. inc STOP ;set time-to-quit flag
  119. takemeout 's'
  120. ret
  121. ;------------------------------------------
  122.  
  123. fetch_1: ;doesn't like label = @-1
  124. ;classic fetch. (adr -- num). Only in RAM
  125. header S_1,1,"@"
  126. fetch:
  127. pushx ;going to use x to point so better save
  128. mypop xh
  129. mypop xl
  130. ld r16,x+
  131. ld r17,x
  132. mypush r16
  133. mypush r17 ; and put them on my stack
  134. popx ;return with x intact and RAM val on my stack
  135. ret
  136. ;dddddddddddddddddddddddddddddddddddddddddddddddd
  137.  
  138. cfetch_1: ;doesn't like label = c@-1
  139. ;classic fetch. (adr -- num). Only in RAM. Do I want y to advance just one byte on mystack
  140. header fetch_1,2,"c@"
  141. cfetch:
  142. pushx ;going to use x to point so better save
  143. mypop xh
  144. mypop xl
  145. ld r16,x+
  146. mypush r16
  147. clr r16
  148. mypush r16 ;so we get a 16 bit val on stack
  149. popx ;return with x intact and RAM val on my stack
  150. ret
  151. ;dddddddddddddddddddddddddddddddddddddddddddddddd
  152.  
  153. store_1: ;classic != "store"(num adr --) . Num is now at cell adr.
  154. header cfetch_1,1,"!"
  155. store:
  156.  
  157. pushx
  158. mypop2 xh,xl ;there goes the address
  159. mypop2 r17,r16 ;there goes the num
  160. st x+,r16
  161. st x,r17 ;num goes to cell with location=adr
  162. popx
  163. ret
  164. ;ddddddddddddddddddddddddddddddddddddddddddddddddddd
  165.  
  166. cstore_1: ;classic c!= "store"(adr 16bit --) . Lower 8 bits Num is now at cell adr.
  167. header store_1,2,"c!"
  168. cstore:
  169. mypop r17 ;there's the high byte. Thrown away
  170. mypop r16 ;there goes the num. Just 8 bits at this stage.
  171. pushx
  172. mypop2 xh,xl ;there goes the address
  173. st x+,r16
  174. ; st x,r17 ;num goes to cell with location=adr
  175. popx
  176. ret
  177. ;------------------------------------
  178.  
  179. star_1: ;classic 16*16 mulitply (n n -- n*n)
  180. header cstore_1,1,"*"
  181. star:
  182. mypop2 r17,r16
  183. mypop2 r19,r18 ;now have both numbers in r16..r19
  184. rcall mpy16s ; multiply them. Result in r18..r21. Overflow in r20,21
  185. mypush2 r18,r19
  186. ret
  187. ;-----------------------------------------
  188.  
  189. slashMod_1: ;classic /MOD (n m -- n/m rem)
  190. header star_1,4,"/mod"
  191. slashMod:
  192. push r13
  193. push r14 ;this is STOP but is used by div16s, so better save it
  194. mypop2 r19,r18 ; that's m
  195. mypop2 r17,r16 ;that's n
  196. rcall div16s ;the the 16 by 16 bit divsion
  197. mypush2 r16,r17 ;answer ie n/m
  198. mypush2 r14,r15 ;remainder
  199. pop r14
  200. pop r13
  201. ret
  202. ;dddddddddddddddddddddddddddddddddddddddddddd
  203.  
  204. plus_1: ;classic + ( n n -- n+n)
  205. header slashMod_1,1,"+"
  206. plus:
  207. mypop2 r17,r16
  208. mypop2 r19,r18
  209. clc
  210. add r16,r18
  211. adc r17,r19
  212. mypush2 r16,r17
  213. ret
  214. ;--
  215.  
  216. minus_1: ;classic - ( n m -- n-m)
  217. header plus_1,1,"-"
  218. minus:
  219. mypop2 r19,r18
  220. mypop2 r17,r16
  221. clc
  222. sub r16,r18
  223. sbc r17,r19
  224. mypush2 r16,r17
  225. ret
  226. ;dddddddddddddddddddddddddddddddddddddddddd
  227.  
  228. pstore_1: ;expects eg. 0003 PORTB P! etc, "output 3 via PORTB"
  229. header minus_1,2, "p!"
  230. pstore:
  231. mypopb ;get rid of PORTB number, not used for tiny85, just one port
  232. mypopa ; this is used. it's eg the 003 = R16 above
  233. out PORTB,r16
  234. ret
  235. ;ddddddddddddddddddddddddd
  236.  
  237. portblabel_1:
  238. header pstore_1,5,"PORTB" ; note caps just a filler that point 0b in stack for dropping
  239. portblabel:
  240. ; Extend later on to include perhaps other ports
  241. ; one:
  242. ; rcall stackme
  243.  
  244. rcall stackme_2
  245. .db $0b, 00
  246. ret
  247. ;---------------------
  248.  
  249. datadirstore_1: ;set ddrb. invioked like this 000f PORTB dd! to make pb0..pb3 all outputs
  250. header portblabel_1, 3, "dd!"
  251. datadirstore:
  252. mypopb ; there goes useless 0b = PORTB
  253. mypopa ; 000f now in r17:16
  254. out DDRB,r16
  255. ret
  256. ;dddddddddddddddddddddddddddddddddddd
  257. ;sbilabel_1 ;set bit in PORTB. Just a kludge at this stage
  258. ;header datadirstore_1,3,"sbi" TODO sort out sbi and delay later. Now get on with compiler.
  259. ;first need store system vars in the eeprom. Arbitrarily 0010 is HERE and 0012 (in eeprom) is LATEST
  260. ;----------------------------------------
  261.  
  262. percentcstore_1: ;(n16 adr16 --) %c! stores stack val LSbyte to eeprom adr
  263. ; eg 10 00 1234 stores 34 to 0010 <--eeprom adr
  264. header datadirstore_1,3,"%c!"
  265. percentcstore:
  266. mypopb ;adr in r18,19
  267. mypopa ;data. Lower byte into r16
  268.  
  269. rcall eewritebyte ;burn it into eeprom
  270. ret
  271. ;----------------------------------
  272.  
  273. percentstore_1: ; (n16 adr16 --) b16 stored at eeprom adr adr16 and adr16+1
  274. header percentcstore_1,2, "e!" ;changed %! to e! PB!!
  275. percentstore:
  276. mypopb ;adr in b=r18,19
  277. mypopa ;n16 into r16,17 as data
  278.  
  279. rcall eewritebyte ;burn low data byte
  280. clc
  281. inc r18
  282. brne outpcs
  283. inc r17 ;sets up adr+1 for next byte
  284. outpcs:
  285. mov r16,r17 ;r16 now conatins hi byte
  286. rcall eewritebyte
  287. ret
  288. ;-------------------------------
  289.  
  290. percentcfetch_1: ;(eepromadr16--char). Fetch eeprom byte at adr on stack
  291. header percentstore_1,3,"%c@"
  292. percentcfetch:
  293. mypopb ;adr now in r18,19
  294. rcall eereadbyte
  295. mypush r16 ; there's the char going on stack. Should be n16? Not n8?
  296. ret
  297. ;-------------------
  298.  
  299. percentfetch_1: ;(adr16--n16) get 16bits at adr and adr+1
  300. header percentcfetch_1,2,"e@" ;PB!! changed from %@
  301. percentfetch:
  302. rcall percentcfetch ;low byte now on stack
  303. inc r18
  304. brcc downpf
  305. inc r19
  306. downpf:
  307. rcall eereadbyte ;there's the high byte hitting the mystack
  308. mypush r16
  309. ret
  310. ;-------------------------------
  311. gethere_1: ; leaves current value of eHERE on stack
  312. header percentfetch_1,7,"gethere"
  313. gethere:
  314. rcall stackme_2
  315. .dw eHere
  316. rcall percentfetch
  317. ret
  318. ;--------------------
  319. getlatest_1: ;leaves current value of latest on stack
  320. header gethere_1,9, "getlatest"
  321. getlatest:
  322. rcall stackme_2
  323. .dw eLATEST ;the address of the latest link lives in eeprom at address 0012
  324. rcall percentfetch ;get the val out of eeprom
  325. ret
  326. ;------------------
  327.  
  328. colon_1: ;classic ":"compiling new word marker
  329. header getlatest_1,1,":"
  330. rcall coloncode
  331. ret
  332. ;----------------------------------------
  333.  
  334. comma_1: ;classic comma. ;Put adr on stack into dictionary at myhere and bump myhere
  335. header colon_1,1,","
  336. comma:
  337. mypopa ;adr now in r16,17
  338. pushz ;save z
  339. movw zl,myhere ;z now pnts to next avail space in dic
  340. st z+,r16
  341. st z+,r17
  342. movw myhere,zl ;so that myhere is updated as ptr
  343. popz ;bring z back
  344. ret
  345. ;------------------------------------
  346.  
  347. tic_1: ;clasic tic('). Put cfa of next word on stack
  348. header comma_1,1, "'"
  349. tic:
  350. rcall word ;point to next word in input
  351. rcall findword ;leaving cfa in z
  352. mypush2 zl,zh
  353. rcall two ;but it's in bytes. Need words so / by 2
  354. rcall slashmod
  355. rcall drop ;that's the remainder dropped
  356. ;now have cfa of word after ' on stack in word-units.
  357. ret
  358. ;-----------------------
  359.  
  360. dummy_1: ;handy debugging place to put a break point
  361. header tic_1,$85,"dummy" ;first immediate word
  362. dummy:
  363. nop
  364. ret
  365. ;--------------------------------
  366.  
  367. compstackme_2_1: ;needed infront of every number compiled
  368. header dummy_1, $0d,"compstackme_2"
  369. compstackme_2:
  370. ldi r16,low(stackme_2)
  371. ldi r17,high(stackme_2)
  372. mypush2 r16,r17 ;in words need to *2 to convert to bytes
  373. rcall two
  374. rcall star
  375. rcall compileme
  376. ret
  377. ;-----------------------------------------
  378.  
  379. double_1: ;whatever's on stack gets doubled. Usful words-->bytes. (n...2*n)
  380. header compstackme_2_1, 6, "double"
  381. double:
  382. mypopa ;stk to r16,17
  383. clc ;going to do shifts
  384. rol r16
  385. rol r17 ;r16,17 now doubled
  386. mypush2 r16,r17
  387. ret ;with 2*n on my stk
  388. ;--------------------------------------
  389.  
  390. semi_1: ;classic ";". Immediate TODO compile a final ret
  391. header double_1,$81,";"
  392. semi:
  393. nop
  394. rcall insertret ;compile ret
  395.  
  396. ;rcall oneBitTime
  397. rcall delay100ms ;trying some waits to give spm time
  398. rcall burnbuf2and3
  399. rcall delay100ms ;want plenty of burn time before doing eeprom work
  400. ;rcall oneBitTime ;ditto
  401. ;rcall oneBitTime ;ditto. Seems to be working. eeprom writes wreck spm's.
  402. rcall rbrac ;after semi w'got back to executing
  403. ; rcall updatevars ;update HERE and LATEST in eeprom
  404. rcall updatevars2 ;Better version. update HERE and LATEST in eeprom
  405.  
  406. ret
  407. ;---------------------------------------
  408.  
  409. rbrac_1: ;classic "]" ,immediate
  410. header semi_1,$81,"]"
  411. rbrac:
  412. clr STATE ;go to executing
  413. ret
  414. ;------------------------------------------------
  415.  
  416. immediate_1: ;classic IMMEDIATE. Redo len byte so MSbit =1
  417. header rbrac_1,$89,"immediate"
  418. immediate:
  419. mypush2 r2,r3 ;this is mylatest. pnts to link of new word
  420. rcall two
  421. rcall plus ;jmp over link to pnt to len byte
  422. pushx ;better save x
  423. mypop2 xh,xl ;x now pnts to len byte
  424. ld r16,x ; and put it into r6
  425. ldi r18,$80 ;mask
  426. or r16,r18 ;eg 03 --> 83 in hex
  427. st x,r16 ;put len byte back
  428. popx ;back where it was
  429. ret ;done now newly created word is immediate
  430. ;-------------------------------------------------
  431.  
  432. emit_1: ;(n8 --) classic emit
  433.  
  434. header immediate_1, 4, "emit"
  435. emit:
  436. rcall emitcode
  437. ret
  438. ;--------------------------------------
  439.  
  440. getline_1: ;rx a line of chars from serialin. eol = $0d
  441. ;this is the line that gets interpreted
  442. header emit_1,7, "getline"
  443. getline:
  444. rcall rxStrEndCR ;write 64 TODO 128? bytes into buf1 from serial io
  445. .ifdef testing
  446. rcall dumpbuf1
  447. .endif
  448. ret ;with buf1 ready to interpret
  449. ;-------------------------------------------------
  450.  
  451. zero_1: ;stack a zero
  452. header getline_1,4,"zero"
  453. zero:
  454. rcall stackme_2
  455. .db 0,0
  456. ret
  457. ;----------------------------------------
  458.  
  459. equal_1: ;(n1 n2 -- flag)
  460. header zero_1,1,"="
  461. equal:
  462. rcall equalcode
  463. ret
  464. ;----------------------------------------
  465.  
  466. zeroequal_1: ;(n -- flag)
  467. header equal_1,2,"0="
  468. zeroequal:
  469. rcall zero
  470. rcall equal
  471. ret
  472. ;-------------------------
  473.  
  474. over_1: ;(n1 n2 --n1 n2 n1)
  475. header zero_1,4,"over"
  476. over:
  477. mypopa
  478. mypopb
  479. mypush2 r18,r19 ;n1
  480. mypush2 r16,r17 ;n2
  481. mypush2 r18,r19 ;n1. so end up with (n1,n2,n1
  482. ret
  483. ;-----------------------------------
  484.  
  485. rot_1: ;classic (n1 n2 n3 -- n2 n3 n1)
  486. header over_1,3,"rot"
  487. rot:
  488. mypopa
  489. push r17
  490. push r16 ;save n3
  491. rcall swapp ; n2 n1
  492. pop r16
  493. pop r17
  494. mypush2 r16,r17 ;n2 n1 n3
  495. rcall swapp ;n2 n3 n1
  496. ret
  497. ;------------------------------------
  498.  
  499. reverse3_1: ;PB (n1 n2 n3 -- n3 n2 n1). Reverses top 3 order
  500. header rot_1,8,"reverse3"
  501. reverse3:
  502. rcall swapp ; n1 n3 n2
  503. rcall rot ; n3 n2 n1
  504. ret ;so (n1 n2 n3 -- n3 n2 n1)
  505. ;--------------------------------------------
  506.  
  507. unrot_1: ;PB (n1 n2 n3 -- n3 n1 n2) Buries topitem two down
  508. header reverse3_1,5,"unrot"
  509. unrot:
  510. rcall reverse3 ; (n1 n2 n3 -- n3 n2 n1)
  511. rcall swapp ; n3 n1 n2
  512. ret
  513. ;--------------------------------
  514.  
  515. andd_1: ;classic AND
  516. header unrot_1,4,"andd" ; two d's otherwise asm problems
  517. andd:
  518. mypopa
  519. mypopb
  520. and r16,r18
  521. and r17,r19
  522. mypush2 r16,r17
  523. ret
  524. ;----------------------------------------
  525.  
  526.  
  527. orr_1: ;classic OR
  528. header andd_1,3,"orr" ; two r's otherwise asm problems
  529. orr:
  530. mypopa
  531. mypopb
  532. or r16,r18
  533. or r17,r19
  534. mypush2 r16,r17
  535. ret
  536. ;------------------------
  537.  
  538. calcjump_1: ;(to from -- opcode)
  539. header orr_1,$88, "calcjump"
  540. calcjump:
  541. rcall calcjumpcode
  542. ret ;with opcode on stack
  543. ;-----------------------
  544.  
  545. begin_1: ;( -- adr) classic BEGIN. Used in most loops
  546. header calcjump_1,$85,"begin"
  547. begin:
  548. rcall stackmyhere ;put next adr on stack. For AGAIN etc
  549. ret ;with adr on stack
  550. ;---------------------------
  551. again_1: ; (to_adr -- ) classic AGAIN cts loop back to BEGIN
  552. header begin_1, $85,"again"
  553. again:
  554. rcall stackmyhere ; to_adr fr_adr
  555. rcall minus ;rel_adr_distance eg $ffdd
  556. rcall stackme_2
  557. .dw $0002
  558. rcall div ;now adr difference in words. Works better.
  559. rcall stackme_2
  560. .dw $0fff ;$ffdd $0fff
  561. rcall andd ;$0fdd eg.
  562. rcall stackme_2
  563. .dw $c000 ;$0fdd $c000
  564. rcall orr ;$cffdd = rjmp back_to_again
  565. rcall one
  566. rcall minus ;t0-fr-1 = the jump part of rjmp
  567. rcall comma ;insert into dic
  568. ret ;with rjmp opcode in next pos in dic
  569. ;------------------------------
  570.  
  571. div_1: ; (n1 n2 -- n1/n2) classic / Could make 2 / faster with >, one right shift
  572. header again_1,1,"/"
  573. div:
  574. rcall slashMod
  575. rcall drop
  576. ret
  577. ;---------------------------------
  578.  
  579. halve_1: ; (n -- n/2) use shifts to halve num on stack. Handy
  580. header div_1,5,"halve"
  581. halve:
  582. mypopa
  583. clc
  584. ror r17
  585. ror r16
  586. mypush2 r16,r17 ;now num on stack has been halved
  587. ret ;with n/2 on stk
  588. ;--------------------
  589.  
  590. dumpb1_1: ;dumpbuf1 to serial
  591. header halve_1,6,"dumpb1"
  592. dumpb1:
  593. rcall dumpbuf1
  594. ret
  595. ;---------------------
  596.  
  597. OK_1: ;classic "ok"
  598. header dumpb1_1,2,"OK"
  599. OK:
  600. ldi r16,'K'
  601. ldi r17,'O'
  602. clr r18
  603. mypush2 r16,r18 ;16bits K
  604. mypush2 r17,r18 ;'O'
  605.  
  606. rcall emitcode
  607. rcall emitcode
  608. ldi r16,'}' ;try this for a cursor prompt
  609. clr r18
  610. mypush2 r16,r18
  611. rcall emitcode
  612.  
  613. ret ;after having emitted "OK" to terminal
  614. ;-------------------------------
  615.  
  616. CR_1: ;output a carriage return. Need $0d too?
  617. header OK_1,2, "CR"
  618. CR:
  619. ldi r16,$0d
  620. mypush r16
  621. clr r16
  622. mypush r16 ;all stack items are 16bits
  623. rcall emitcode
  624. ret ;after sending CR to terminal
  625. ;--------------------------
  626.  
  627. test1_1: ;just need some dic word to try with new serialFill
  628. header CR_1,5,"test1"
  629. test1:
  630. ldi serialByteReg, '*'
  631. rcall sendSerialByte
  632. ldi serialByteReg, 'T'
  633. rcall sendSerialByte
  634. ldi serialByteReg, 'T'
  635. rcall sendSerialByte
  636. ldi serialByteReg, '*'
  637. rcall sendSerialByte
  638. inc r1
  639. inc r1 ;TESTING take out later TODO
  640. ret
  641. ;-------------------------------------------------------
  642. dotS_1: ;classic .S Prints stack items nondestructively
  643. header test1_1,2,".S"
  644. dotS:
  645. rcall dotScode ;TODO check there is *something* on the stack first
  646. ret
  647. ;----------------------------------------------------------
  648.  
  649. dot_1: ;( n16 -- ) classic "." that prints the num on the TOS
  650. header dotS_1,1,"."
  651. dot:
  652. push r16
  653. push r17
  654. mypopa ;TO_stk --> r16r17
  655. rcall d1617 ;print it
  656. pop r17
  657. pop r16
  658. ret
  659. ;-----------------------------
  660.  
  661. Sdot_1: ;( adr16 len16 --) classic S" Prints string from flash
  662. header dot_1,2,"S."
  663. Sdot:
  664. push r16
  665. push r17
  666. push r18
  667. ; pushz
  668. mypopb ;r18 = len
  669. mypop2 zh,zl ;x gets the adr in flash of the string
  670. upsd:
  671. lpm r16,z+ ;get byte from flash
  672. rcall sendserialbyte
  673. ;rcall d16
  674. dec r18
  675. brne upsd ;do this for len times
  676. ; popz
  677. pop r18
  678. pop r17
  679. pop r16
  680. ret
  681. ;----------------------------------------
  682.  
  683. words_1: ;classic words. All words get printed out tot the terminal.
  684. header Sdot_1,5,"words"
  685. words:
  686. rcall wordscode
  687. ret
  688. ;---------------------------------------
  689.  
  690. getvarptr_1: ;leaves current value of varptr,currently at 0012,on stack
  691. header words_1,9, "getvarptr"
  692. getvarptr:
  693. rcall stackme_2
  694. .dw eVar ;the address of the latest link lives in eeprom at address 0012
  695. rcall percentfetch ;get the val out of eeprom
  696. ret ;with next avaialble adr for variable on stack. Lives in buf just below mystack
  697. ;-----------------------------------------------
  698. hereadr_1: ;classic here. Puts adr of eHere on stack. Currently 010 in eeprom
  699. header getvarptr_1,7,"hereadr"
  700. hereadr:
  701. rcall stackme_2
  702. .dw eHere
  703. ret ;with eg 010 on stack, the eeprom adr of eHere
  704. ;-----------------------------------------------------
  705. latestadr_1: ;classic latest. Puts adr of eLatest on stack. Currently 012 in eeprom
  706. header hereadr_1,9,"latestadr"
  707. latestadr:
  708. rcall stackme_2
  709. .dw eLatest
  710. ret ;with eg 012 on stack, the current eeprom adr of elatest
  711. ;----------------------------------
  712.  
  713. varptradr_1: ; Puts adr of eVar on stack. Currently 014 in eeprom
  714. header latestadr_1,9,"varptradr"
  715. varptradr:
  716. rcall stackme_2
  717. .dw eVar
  718. ret ;with eg 014 on stack, the eeprom adr of eVar
  719. ;----------------------------------
  720.  
  721. tx16_1: ;need easier word than "sendserialbyte"
  722. header varptradr_1,4,"tx16"
  723. tx16:
  724. rcall sendserialbyte
  725. ret
  726. ;--------------------------------------------
  727. space_1: ;send a space
  728. header tx16_1,5,"space"
  729. space:
  730. rcall stackme_2
  731. .dw $0020
  732. rcall emitcode
  733. ret ;with space sent
  734. ;------------------------------------------
  735.  
  736. report_1: ;send a report at the start of the prog. Esp for system vars debugging
  737. header space_1,6,"report"
  738. report:
  739. ;.ifdef livetesting
  740. rcall gethere
  741. rcall dot
  742. rcall space
  743. rcall getlatest
  744. rcall dot
  745. rcall space
  746. rcall getvarptr
  747. rcall dot
  748. rcall space
  749. ;.endif
  750. ret
  751. ;----------------------------------------------------
  752.  
  753. variable_1: ;classic variable
  754. header report_1,8,"variable"
  755. variable:
  756. rcall variablecode
  757. takemeout '~'
  758. rcall dumpbuf1
  759. rcall report
  760. takemeout '!'
  761. ret ;with variable's name and ram adr in word in flash dictionary
  762. ;---------------------------
  763.  
  764. depth_1: ;classic size of stack
  765. header variable_1,5,"depth"
  766. depth:
  767. rcall depthcode
  768. ret ;with depth num on stack
  769. ;--------------------------------------
  770.  
  771. rx18_1: ;wait for serial byte from terminal and put it into r18
  772. header depth_1,4,"rx18"
  773. rx18:
  774. rcall getserialbyte ;too long a name, hence this one
  775. ret ;with key typed in r18
  776. ;-------------------------------------
  777. ;LATEST:
  778. getkey_1: ;wait for key to be pressed and put ascii-16 on stack
  779. header rx18_1,6,"getkey"
  780. getkey:
  781. ldi r18,'-'
  782. clr r19
  783. mypush2 r18,r19
  784. rcall emitcode
  785. rcall rx18
  786. clr r19
  787. mypush2 r18,r19
  788. ret ;with key value on stack
  789. ;---------insert AVR Studio stuff here-----------------------------
  790.  
  791.  
  792. ;-------hhhhhhhhhhhhhhhhhhere -------------------------
  793.  
  794. zerobranch_1: ;classic obranch code
  795. header getkey_1,7,"0BRANCH"
  796. zerobranch:
  797. ;( flag --) if flag is 0, do nothing,so as to go onto
  798. ; next instruction which will be a jump. If flag is 1 step over rjmp.
  799. mypopa
  800. or r16,r17 ;any 1's?
  801. breq out0b ;a 0 means get out
  802. ;if here the flag was 1 so we have to skip over next instruction
  803. pop r17
  804. pop r16
  805. clc
  806. inc r16
  807. ; inc r16 ;add one to ret adr. It's in WORDS
  808. brcc down0b
  809. inc r17
  810. down0b:
  811. push r16
  812. push r17
  813. out0b:
  814. ret
  815.  
  816. ;--------------------------------------
  817.  
  818. comp0branch_1: ;needed during IF compling
  819. header zerobranch_1,$0b,"comp0branch"
  820. comp0branch:
  821. ldi r16,low(zerobranch)
  822. ldi r17,high(zerobranch)
  823. mypush2 r16,r17 ;in words need to *2 to convert to bytes
  824. rcall two
  825. rcall star
  826. rcall compileme
  827. ret ;with "rcall 0branch"in next
  828. ;--------------------------
  829.  
  830. if_1: ;classic if
  831. header comp0branch_1,$82,"if"
  832. if:
  833. rcall comp0branch
  834. rcall stkmyhere
  835. rcall stackme_2
  836. .dw 00000
  837. rcall comma
  838. ret ; with (rcall 0branch,0000) in dic and adr of the 0000 in myhere on stack
  839. ;-------------------------
  840.  
  841. endif_1: ;classic "then" used in IF .. THEN, but endif better.
  842. header if_1,$85,"endif"
  843. endif: ;(there_adr -- rjmp code )
  844. rcall dup ;need there_adr twice,calc+ store
  845. rcall stkmyhere ;so we can use calc rjmp --> there - here -1
  846. rcall swapp ;because the jump is put into "there"
  847. rcall calcjumpcode ;(jmpcode now on stack)
  848. rcall swapp ;wrong way round for store !
  849. rcall store ;put jmpcode where there are just 0 placeholders near if
  850. ret ;with all the If..End if statement's codes in right place
  851. ;---------------------------------
  852. delay100ms_1: ;handy; delay for about 0.1 sec = 100 ms
  853. header endif_1,10,"delay100ms"
  854. delay100ms:
  855. .ifdef testing
  856. ldi r16,1
  857. .else
  858. ldi r16,60
  859. .endif
  860. upd100:
  861. rcall oneBitTime
  862. dec r16
  863. brne upd100
  864. ret ;after about a tenth of a second
  865. ;----------------------------------------------
  866.  
  867. greq_1: ;(n m -- flag) flag =1 if n>=m, otherwise 0. Signed
  868. header delay100ms_1,2,">="
  869. greq:
  870. mypop2 r19,r18 ;that's m
  871. mypop2 r17,r16 ;that's n
  872. cp r16,r18
  873. cpc r17,r19 ;got this from the net
  874. brge downlo
  875. rcall zero ;if n<m
  876. rjmp outgr
  877. downlo:
  878. rcall one
  879. outgr:
  880. ret ;with flag on stack
  881. ;--------------------------------------
  882.  
  883. lt_1: ;(n m -- flag) flag =1 if n<m, otherwise 0. Signed
  884. header greq_1,1,"<"
  885. lt:
  886. mypop2 r19,r18 ;that's m
  887. mypop2 r17,r16 ;that's n
  888. cp r16,r18
  889. cpc r17,r19 ;got this from the net
  890. brlt downlt
  891. rcall zero ;if n>=m
  892. rjmp outlt
  893. downlt:
  894. rcall one
  895. outlt:
  896. ret ;with flag on stack
  897. ;-------------------------------
  898.  
  899. stkmyhere_1: ;( -- n16) useful
  900. header lt_1,9,"stkmyhere"
  901. stkmyhere1: ;Note spelling. put myhere on the stack, handy
  902. mypush2 myhere,r5
  903. ret
  904. ;------------------------------------------
  905. FBFlag_1: ;first variable. If 0 take input from serial, if 1 take it from BLOCK
  906. header stkmyhere_1,$46,"fbflag" ;NB first varaiable. Look at bit 6 of len
  907. FBFlag:
  908. rcall stackme_2
  909. .dw $01a0
  910. ret ;with first var adr 1a0 on stack
  911. ;-----------------------------------------
  912.  
  913. FBPtr_1: ;second variable. points to current address in BLOCK. Starts at $1c0
  914. header FBFlag_1,$45,"fbptr" ;NB first varaiable. Look at bit 6 of len
  915. FBPtr:
  916. rcall stackme_2
  917. .dw $01a2
  918. ret ;with second var adr 1a2 on stack
  919.  
  920. ;-------------------new---------
  921.  
  922. readblock_1: ;set flag in ram $1a0,1 to 0001. Reads from BLOCK not serialfill
  923. header FBPtr_1,9,"readblock"
  924. readblock:
  925. pushx ;macro, save xl, xh
  926. ldi xl,$a0
  927. ldi xh,$01 ;point to ram VARS, esp. FBFlag
  928. ldi r16,$01
  929. st x+,r16
  930. clr r16
  931. st x+,r16 ;that's FBFlag made 1.(ie use block fill not serialfill)
  932. popx ;restore x
  933. ret
  934. ;---------------------------------------------
  935.  
  936. blockfinish_1: ;put at end of block to make next inputs via serialfill
  937. header readblock_1,11,"finishblock"
  938. blockfinish:
  939. ldi xl,$a0
  940. ldi xh,$01 ;point to ram VARS, esp. FBFlag
  941. clr r16
  942. st x+,r16
  943. st x+,r16 ;that's FBFlag made 0.(ie use serialfill not blockfill)
  944. ; rjmp cold ;reset everythig
  945. rjmp cold ;better? cold or quit?
  946. ;note, no ret as cold sorts out stacks for nice restart.
  947. ; TODO indicate when start is cold eg cold}} or cokd}} etc
  948. ;--------------------------------------------------
  949. ;major word. Assumes there's some colon defs in last 1k. ie at byte adr $1c00, $0e00 word adr.
  950. ;these defs end with the un-colon word "blockfinish". Each def ends in CR = $0d.
  951. ;Normally input comes into buf1 via serialfill. If flag in ram adr $01a0 is 0001 then we use blockfill
  952. ; but if the flag is 0000, default, we use serial fill. The adjacent am adr $01a2 is the pointer into
  953. ; the BLOCK. Initially at $1c00 but will change as the defs are brought in one by one. All come in
  954. ; one block and are compiled just like serial input (v, quickly typed) of lots of defs.
  955.  
  956. blockfill_1: ;assumed called in quit when FGBFlag ($01a0) = 0001 and FBPtr ($01a2) = $1c00.
  957. header blockfinish_1,9,"blockfill"
  958. blockfill:
  959. rcall blockfillcode
  960. ret
  961. ;-------------------------------------------
  962.  
  963. testingstopper_1: ;need a way of crashing to halt after BLOCK work when testing
  964. header blockfill_1,14,"testingstopper"
  965. testingstopper:
  966. rjmp testingstopper
  967. ;--------------------------------
  968.  
  969. else_1: ;classic ELSE. Won't compile nicely thru block as keeps going immediate
  970. header testingstopper_1,$84,"else"
  971. else: ;(n16 --) expects if's adr on stack
  972. ;try this order above and below here
  973. ; rcall endif ;see endif
  974.  
  975. rcall dup ;need there_adr twice,calc+ store
  976. rcall stkmyhere ;so we can use calc rjmp --> there - here -1
  977. rcall two
  978. rcall plus ;because we have to jump over the 0000 adr to be filled in later
  979. rcall swapp ;because the jump is put into "there"
  980. rcall calcjumpcode ;(jmpcode now on stack)
  981. rcall swapp ;wrong way round for store !
  982. rcall store ;put jmpcode where there are just 0 placeholders near if
  983. ;ret ;with all the If..End if statement's codes in right place
  984.  
  985.  
  986.  
  987. rcall stkmyhere ;for endif at the end of def using else
  988. rcall zero ;filled in by endif
  989. rcall comma
  990.  
  991. ret
  992. ;--------------------------------------------------------------
  993.  
  994. rs_1: ;( adr16 len16 -- ) ram string-print (assembler doesn't like rs._1 etc)
  995. header else_1,3,"rs."
  996. rs:
  997. pushx
  998. mypopb ;the len's now in r18,19
  999. mypop2 xh,xl ;str adr in x
  1000. uprs:
  1001. ld r16,x+ ;get char from string
  1002. rcall tx16 ; and print it to term
  1003. dec r18 ;len--, finished?
  1004. brne uprs
  1005. popx ;recover x for other work
  1006. ret ;with ram string printed to term
  1007. ;-------------------------------------------
  1008.  
  1009. qmark_1: ;prints ?
  1010. header rs_1,5,"qmark"
  1011. qmark:
  1012. ldi r16,'?'
  1013. rcall tx16
  1014. ret ;with ? printed to terminal
  1015. ;-----------------------------------------------
  1016. ;LATEST:
  1017. findfirstvar_1: ;(--adr16) search dic for topmost var. Return with its RAM adr.
  1018. header qmark_1,12,"findfirstvar"
  1019. findfirstvar:
  1020. rcall findfirstvarcode
  1021. ret ; with RAM adr of first var on stack. Useful after forget.
  1022. ;)))))))))))))))))))))))))))))
  1023. ;LATEST:
  1024. compstrout_1: ;needed infront of every number compiled
  1025. header findfirstvar_1,10,"compstrout"
  1026. compstrout:
  1027. ldi r16,low(strout)
  1028. ldi r17,high(strout)
  1029. mypush2 r16,r17 ;in words need to *2 to convert to bytes
  1030. rcall two
  1031. rcall star
  1032. rcall compileme
  1033. ret
  1034. ;000000000000000000000000
  1035. LATEST:
  1036. squote_1: ; classic S" . Used to output strings in compiled words.
  1037. header compstrout_1,$82,"S'" ;compiler doesn't like S" in quotes
  1038. squote:
  1039. rcall compstrout
  1040. rcall stkmyhere ;stack adr of 00 that length is going into
  1041. rcall zero
  1042.  
  1043. rcall comma
  1044. pushz ;going to use z to point to RAM dic
  1045. ;inc xl
  1046. ;brcc downsq ;step over space after
  1047. movw zl,r4 ;z <-- myhere
  1048. clr r18 ;counter
  1049. movtxt:
  1050. ld r16,x+ ;first char to move is space after S'
  1051. cpi r16,$27 ;got to end of string? ;$27 = '
  1052. breq outsq ;keep filling in chars in dic til hit a '
  1053. st z+,r16 ;fill up ram dic with string after S'
  1054. inc r18 ;this is for len. later on
  1055. rjmp movtxt
  1056. nop
  1057. ; may have an odd num of chars. If so add padding byte.
  1058. outsq:
  1059. sbrs r18,0 ;is r18 an odd num eg. len = 5
  1060. rjmp downsq
  1061. ;if here lsb = 1 in len so we're on a padding byte and have to add 1 to get to a 2 byte boundary
  1062. clr r16
  1063. st z+,r16 ;insert padding byte
  1064. downsq:
  1065. clr r19 ;topup
  1066. mypush2 r18,r19 ;now len is on the mystack (so have ( adrOf00 len--)
  1067. rcall swapp
  1068. rcall store ; mystk empty and len word in right place just before the str.
  1069. movw myhere, zl ;advance myhere so that next word compiles straight after
  1070. popz
  1071. ret
  1072. ;0000000000000000000000000000000000000000000
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084. ;-----------------------------------------------
  1085. HERE:
  1086. .db "444444444444444444444444444444"
  1087. ;---------------stackme_2 used to live here---------------------------------
  1088.  
  1089. ;====================================================================================================
  1090.  
  1091. .ORG 0
  1092. rjmp cold
  1093. typein: .db "readblock",$0d
  1094. ;.db ": myworxxd 0001 if 0005 dup endif ; ", $0d
  1095. ;-----------------------------------------------------
  1096. cold: ;come here on reset or for big cleanup
  1097. ldi r16, low(RAMEND)
  1098. out SPL, r16
  1099. ldi r16,high(RAMEND)
  1100. out SPH, r16
  1101.  
  1102. ldi YL,low(myStackStart)
  1103. ldi YH,high(myStackStart)
  1104. rcall housekeeping
  1105. ;rcall test_buf2ToFlashBuffer
  1106. ;rjmp cold
  1107. ;rcall blockfillcode
  1108. ;rcall interpretLine
  1109. ;rcall blockfillcode
  1110. ;rcall blockfillcode
  1111. ;rcall blockfinish
  1112. ;rcall test_rs
  1113. ;here3: rjmp here3
  1114.  
  1115. quit:
  1116. ldi r16, low(RAMEND)
  1117. out SPL, r16
  1118. ldi r16,high(RAMEND)
  1119. out SPH, r16
  1120.  
  1121. ; ldi YL,low(myStackStart)
  1122. ; ldi YH,high(myStackStart)
  1123.  
  1124.  
  1125. ;---------new------------
  1126. rcall FBFlag ;put $1a0 (blockfill flag) on stack
  1127. rcall fetch ;either 0000 (do serialfill) or 0001 (blockfill)
  1128. mypopa ;r16,17 get flag
  1129. tst r16 ;is flag (lower byte anyway) a zero?
  1130. .ifndef testing
  1131. breq doSF ;flag = 0 do (normal) serialfill
  1132. .else
  1133. breq getli
  1134. .endif
  1135. rcall blockfillcode ;because (if here) flag is a 1 = do
  1136.  
  1137. ;rjmp interp ;interpret the block fill def
  1138. rcall interpretLine ;but only if filling from BLOCK
  1139. rjmp quit ;quit
  1140.  
  1141. .ifdef testing
  1142. getli:
  1143. rcall getline0
  1144. rcall interpretLine
  1145. quithere:
  1146. rjmp quit;here
  1147. .endif
  1148.  
  1149.  
  1150. .ifndef testing
  1151. doSF:
  1152. rcall serialFill
  1153. interp: ;have buf1 filled with words, def etc now find them on dic etc.
  1154. clr STOP
  1155. clr r1
  1156. clr SECONDLETTER
  1157. clr BOTTOM
  1158.  
  1159. rcall interpretLine
  1160. rjmp quit
  1161. .endif
  1162.  
  1163. ;-------------------------------------------------------------
  1164. ;mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
  1165. ;--------------------------------------------------------------
  1166. getline0: ;force a line into buf1 via flash string. Simulates GETLINE
  1167. ldi zl, low(typein<<1)
  1168. ldi zh, high(typein<<1)
  1169. ldi xl, low(buf1)
  1170. ldi xh, high(buf1)
  1171. type0:
  1172. lpm r16,Z+
  1173. st x+,r16
  1174. cpi r16,0x0d ;have we got to the end of the line?
  1175. brne type0
  1176. ret
  1177. ;--------------------------------------------
  1178. serialFill: ;main input routine from terminal. Output OK} then
  1179. ; wait until buf1 has string of words ( <64 chars?) ending in $0d
  1180. rcall clrbuf1
  1181. rcall CR
  1182. ;rcall report
  1183. rcall OK
  1184. rcall rxStrEndCR
  1185. ret ; buf1 now filled with words from terminal
  1186. ;--------------------------------------------------------------
  1187. interpretLine: ;given line of words in buf one, search for words one by one. Don't do code
  1188. ; or compile at this stage, just find and report that and go into next one.
  1189. rcall pasteEOL
  1190. ldi xl, low(buf1)
  1191. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  1192. clr FOUNDCOUNTER ;counts finds in line parsing.
  1193. nextWord:
  1194. tst STOP
  1195. brne stopLine
  1196. nop
  1197. rcall word
  1198. rcall findWord
  1199. rcall dealWithWord ;go and run code STATE=0, or compile (STATE =1).{ c0de, comp1le}
  1200. rjmp nextWord
  1201. stopLine:
  1202. ret
  1203. ;----------------------------------------------------------
  1204. ;WORD gets x to point to start of word (copy in w=r24,25) with the length in len = r20
  1205. ;assume word points to somewhere in buf1. Should advance thru spaces=0x20 to first real char
  1206. word: ;maybe give it a header later
  1207. ld SECONDLETTER, x ;for debugging. TODO. Should be firstletter?
  1208. ld r16,x+ ;get char
  1209. cpi r16,0x20 ;is it a space?
  1210. breq word ;if so get next char
  1211. ;if here we're point to word start. so save this adr in w
  1212. mov r24,xl
  1213. mov r25,xh ;wordstart now saved in w
  1214. clr r20 ;length initially 0
  1215. nextchar:
  1216. inc r20 ;r20 = word length
  1217. ld r16,x+ ;get next char
  1218. cpi r16,0x20
  1219. brne nextchar
  1220. dec r24 ;adjust start of word
  1221. ;if here we've found a word.Starting at w length in r20.x points to space just past word
  1222. ret
  1223. ;----------------------------------------
  1224. compare: ;given a word in buf1 and a word in the dic are they the same? The word in the dic is pointed to by Z.
  1225. ; and the word in buf1 is pointed to by w=r24,25. len = r20. Z on entry points to the link. Needs +2 to
  1226. lpm r23,z+
  1227. lpm r22,z+ ;store next link in v=r22,23. z now points to len byte
  1228.  
  1229. startc:
  1230. ;TODO save copy of flash word in r21 and also do masking of immediates
  1231. push r20 ;save length
  1232. lpm r16,Z+ ;length of dictionary word, first entry now in r16
  1233. mov r21,r16 ;copy length-in-flash to r21. May have immediate bit (bit 7)
  1234. andi r16,$0f ;mask off top nibble before comparing
  1235. cp r16,r20 ;same lengths?
  1236. brne outcom ;not = so bail out
  1237. ;if here the words are the same length, what about the rest of the chars.First get x to point to word.
  1238. mov xl,r24
  1239. mov xh,r25 ;x now point to start of buf1 word
  1240. upcom:
  1241. lpm r16,z+
  1242. ld r17,x+ ;get one corresponding char from each word
  1243. cp r16,r17 ;same word?
  1244. brne outcom ;bail out if chars are different
  1245. dec r20 ;count chars
  1246. brne upcom ;still matching and not finished so keep going
  1247. ;if here r20 is 0 so match must have been perfect so FOUND = 1
  1248. clr FOUND
  1249. inc FOUND
  1250. outcom:
  1251. pop r20 ;get old lngth of buf1 word back
  1252. ret
  1253. ;-------------------------------------------
  1254. jmpNextWord: ;go to next word in the dictionary. Assume v=r22,23 contains next link word(not byte)
  1255. ; and w = r24,25 contains RAM word start with len in r20
  1256. ;exit with z pointing to next word ready for next COMPARE.
  1257. clc
  1258. rol r22
  1259. rol r23 ;above 3 instructions change word address into byte address by doubling
  1260. movw r30,r22 ;z now points to next word
  1261. ret
  1262. ;-----------------------------------------
  1263.  
  1264. doLatest: ;set up so first jump in dictionary is to top=LATEST and other flags set up.
  1265. ; ldi vl, low(LATEST)
  1266. ; ldi vh, high(LATEST)
  1267. nop
  1268. rcall getlatest ;from eeprom. Now on stack
  1269. mypop2 vh,vl ;
  1270. ; rcall halve
  1271. clr FOUND
  1272. clr BOTTOM ;not yet found the match, not yet at the bottom. Either will stop search.
  1273. clr STOP ;keep parsing words til this goes to a 1
  1274. ret
  1275. ;---------------------------------------------
  1276. ;-----------------------------------------------------------------
  1277. findWord:
  1278. rcall doLatest
  1279. nop
  1280. ;rcall dumpbuf1
  1281. ;FIND reg values here.
  1282. rcall considercode
  1283. upjmpf:
  1284. rcall jmpNextWord
  1285. takemeout 'f'
  1286.  
  1287. rcall compare
  1288. tst FOUND
  1289. brne stopsearchf ;if last compare got a match (FOUND=1) then stop searching
  1290. tst vl
  1291. brne upjmpf ;if v=0000 then we've hit the bottom of the dictionary
  1292. tst vh
  1293. brne upjmpf ;not found and not at bottom so keep going
  1294. ;if here FOUND =0, ie no match yet and we've hit the bottom of the dictionary
  1295. clr BOTTOM
  1296. inc BOTTOM ;exit with FOUND=0 and BOTTOM =1
  1297. stopsearchf:
  1298. nop
  1299. ret
  1300. ;----------------------------
  1301. test_interpretLine:
  1302. rcall interpretLine
  1303. til: rjmp til ;** with r24 pointing to 'S' and FOUND = r15 =1
  1304. ;------------------------------
  1305. dealWithWord: ;come here when it's time to compile or run code
  1306. ;Good debugging spot. Enter here with Z pointing to CFA of word found. Y points to myStack. X points to just
  1307. ; past the word we are seeking (w-s). r10 is 2nd letter of w-s. w = start adr of w-s. v is a link
  1308. ; to the next word in dic. Either just below the found word or 0000 if we get to the bottome with no match
  1309. ;
  1310. nop
  1311. tst FOUND
  1312. breq notfound
  1313. inc FOUNDCOUNTER
  1314.  
  1315. ;want to hop over filler bytes,0's added to keep codes on even byte boundaries
  1316. ; so if r30 is odd at this stage inc it. odd is lsbit = 1.
  1317. sbrs r30,0 ;skip next instruction if final bit lsb = 1
  1318. rjmp downd
  1319. ;if here lsb = 1 so we're on a padding byte and have to add 1 to get to a 2 byte boundary
  1320. inc r30
  1321. brcc downd
  1322. inc r31 ;add one to z before converting to bytes
  1323. ;have to ask at this point, is the word immediate? If so, bit 7 of r21 will be set.
  1324. downd:
  1325. sbrs r21,7
  1326. rjmp downdw ;not immediate so just go on with STATE test
  1327. rjmp executeme ;yes, immediate so execute every time.
  1328.  
  1329.  
  1330. downdw: tst STATE
  1331. breq executeme
  1332. rcall compilecode
  1333. rjmp outdww
  1334. executeme:
  1335. clc
  1336. ror zh
  1337. ror zl ;put z back into word values
  1338.  
  1339.  
  1340. rcall executeCode
  1341.  
  1342.  
  1343.  
  1344. .MESSAGE "Word found"
  1345. rjmp outdww
  1346. notfound:
  1347. nop
  1348. ; .MESSAGE "Word not found"
  1349. ; clr STOP
  1350. ; inc STOP ;stop parsing line
  1351. takemeout 'n'
  1352. rcall numberh ; word not in dict so must be a number? Form = HHHH
  1353. ;now have to add 3 to x so it points past this word ready not next one
  1354. clc
  1355. inc r26
  1356. inc r26
  1357. inc r26
  1358. brcc outdww
  1359. inc r27 ;but only if overflow
  1360. nop
  1361. outdww:
  1362. ret ;with STOP =1 in not a number
  1363. ;------------------------------------------------------------------------
  1364. pasteEOL: ;when a line of text is TYPEd into buf1 it should end with CR=$0d. This gets replaced with ]}, a
  1365. ; special end of line word. When the word is invoked it casues a QUIT back to the waiting for input stage.
  1366. ; Start at buf1 start and inspect each char for a $0D. When found replace with a "$20 S $20 "
  1367.  
  1368. ldi xl, low(buf1)
  1369. ldi xh, high(buf1) ;pnt to start of buffer
  1370. clr r17
  1371. nxtChar:
  1372. inc r17 ;r17 is counter. Bail out when r17 > BUF1LENGTH
  1373. cpi r17, BUF1LENGTH -3
  1374. breq outProb
  1375. ld r16, x+
  1376. cpi r16, $0d
  1377. brne nxtChar
  1378. ;if here we've found a $0d in buf1 before the end, so replace with an EOL token. x points to just after it.
  1379. ldi r16,$20
  1380. st -x, r16 ;back up. Then go forward.
  1381. TAKEMEOUT 'p'
  1382. ; ldi r16, ']'
  1383. ldi r16,$20 ;This took about 4 day's work to insert this line. Why is it needed?
  1384. st x+, r16
  1385. ldi r16,'S'
  1386. st x+, r16
  1387. ; ldi r16, '}'
  1388. ; st x+, r16
  1389. ldi r16, $20
  1390. st x, r16
  1391. rjmp outpel
  1392.  
  1393.  
  1394. outProb:
  1395. takemeout 'O'
  1396. nop
  1397. .MESSAGE "Couldn't find $0d"
  1398. outpel:
  1399. ret
  1400.  
  1401. ;-------------------------------------
  1402. executeCode: ;with Z pointing to cfa. Not sure whether to jmp or call
  1403.  
  1404. ijmp
  1405. ret
  1406. ;---------------------------------------
  1407. test_fetch: ;do run thru of @
  1408. rcall getline0 ;change later to real getline via terminal
  1409. rcall pasteEOL
  1410. ldi xl, low(buf1)
  1411. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  1412.  
  1413. ldi r16,$62
  1414. mypush r16
  1415. ldi r16,$0
  1416. mypush r16 ;should now have adr $0062 on mystack
  1417. rcall fetch
  1418. tf1:
  1419. rjmp tf1
  1420. ;---------------------------------
  1421. test_cfetch: ;do run thru of @
  1422. rcall getline0 ;change later to real getline via terminal
  1423. rcall pasteEOL
  1424. ldi xl, low(buf1)
  1425. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  1426.  
  1427. ldi r16,$62
  1428. mypush r16
  1429. ldi r16,$0
  1430. mypush r16 ;should now have adr $62 on mystack
  1431. rcall cfetch
  1432. tcf1:
  1433. rjmp tcf1
  1434. ;----------------------------
  1435. test_store:
  1436. rcall getline0 ;change later to real getline via terminal
  1437. rcall pasteEOL
  1438. ldi xl, low(buf1)
  1439. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  1440. ldi r16,$62
  1441. ldi r17,$0
  1442. mypush2 r16,r17 ;should now have adr $62 on mystack
  1443. ldi r16, $AB
  1444. ldi r17, $CD
  1445. mypush2 r16,r17 ;now have $ABCD on mystack
  1446. rcall store
  1447. ts1:
  1448. rjmp ts1
  1449. ;------------------------
  1450. test_cstore:
  1451. rcall getline0 ;change later to real getline via terminal
  1452. rcall pasteEOL
  1453. ldi xl, low(buf1)
  1454. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  1455. ldi r16,$62
  1456. ldi r17,$0
  1457. mypush2 r16,r17 ;should now have adr $62 on mystack
  1458. ldi r16, $AB
  1459. ; ldi r17, $CD
  1460. mypush r16 ;now have $ABCD on mystack
  1461. rcall cstore
  1462.  
  1463. ts11:
  1464. rjmp ts11
  1465. ;Now put arith routines here. Are from AVR200. Just using 16*16 for * but get 32bit result.
  1466.  
  1467.  
  1468. ;***************************************************************************
  1469. ;*
  1470. ;* "mpy16s" - 16x16 Bit Signed Multiplication
  1471. ;*
  1472. ;* This subroutine multiplies signed the two 16-bit register variables
  1473. ;* mp16sH:mp16sL and mc16sH:mc16sL.
  1474. ;* The result is placed in m16s3:m16s2:m16s1:m16s0.
  1475. ;* The routine is an implementation of Booth's algorithm. If all 32 bits
  1476. ;* in the result are needed, avoid calling the routine with
  1477. ;* -32768 ($8000) as multiplicand
  1478. ;*
  1479. ;* Number of words :16 + return
  1480. ;* Number of cycles :210/226 (Min/Max) + return
  1481. ;* Low registers used :None
  1482. ;* High registers used :7 (mp16sL,mp16sH,mc16sL/m16s0,mc16sH/m16s1,
  1483. ;* m16s2,m16s3,mcnt16s)
  1484. ;*
  1485. ;***************************************************************************
  1486.  
  1487. ;***** Subroutine Register Variables
  1488.  
  1489. .def mc16sL =r16 ;multiplicand low byte
  1490. .def mc16sH =r17 ;multiplicand high byte
  1491. .def mp16sL =r18 ;multiplier low byte
  1492. .def mp16sH =r19 ;multiplier high byte
  1493. .def m16s0 =r18 ;result byte 0 (LSB)
  1494. .def m16s1 =r19 ;result byte 1
  1495. .def m16s2 =r20 ;result byte 2
  1496. .def m16s3 =r21 ;result byte 3 (MSB)
  1497. .def mcnt16s =r22 ;loop counter
  1498.  
  1499. ;***** Code
  1500. mpy16s: clr m16s3 ;clear result byte 3
  1501. sub m16s2,m16s2 ;clear result byte 2 and carry
  1502. ldi mcnt16s,16 ;init loop counter
  1503. m16s_1: brcc m16s_2 ;if carry (previous bit) set
  1504. add m16s2,mc16sL ; add multiplicand Low to result byte 2
  1505. adc m16s3,mc16sH ; add multiplicand High to result byte 3
  1506. m16s_2: sbrc mp16sL,0 ;if current bit set
  1507. sub m16s2,mc16sL ; sub multiplicand Low from result byte 2
  1508. sbrc mp16sL,0 ;if current bit set
  1509. sbc m16s3,mc16sH ; sub multiplicand High from result byte 3
  1510. asr m16s3 ;shift right result and multiplier
  1511. ror m16s2
  1512. ror m16s1
  1513. ror m16s0
  1514. dec mcnt16s ;decrement counter
  1515. brne m16s_1 ;if not done, loop more
  1516. ret
  1517. ;----------------------------------------------------------
  1518. ;***** Multiply Two Signed 16-Bit Numbers (-12345*(-4321))
  1519. test_mpy16s:
  1520. ldi mc16sL,low(-12345)
  1521. ldi mc16sH,high(-12345)
  1522. ldi mp16sL,low(-4321)
  1523. ldi mp16sH,high(-4321)
  1524. rcall mpy16s ;result: m16s3:m16s2:m16s1:m16s0
  1525. ;=$032df219 (53,342,745)
  1526. tmpy: rjmp tmpy
  1527.  
  1528. test_mpy16s0:
  1529. ldi mc16sL,low(123)
  1530. ldi mc16sH,high(123)
  1531. ldi mp16sL,low(147)
  1532. ldi mp16sH,high(147)
  1533. rcall mpy16s ;result: m16s3:m16s2:m16s1:m16s0
  1534. tmpy0: rjmp tmpy0
  1535. ;-----------------------
  1536. test_star:
  1537. ldi r16,-$7b
  1538. mypush r16
  1539. ldi r16,$00
  1540. mypush r16 ;that's decimal 123 on stack
  1541. ldi r16,$93
  1542. mypush r16
  1543. ldi r16,$00
  1544. mypush r16 ; and thats dec'147
  1545. rcall star
  1546. tsr: rjmp tsr
  1547.  
  1548. ;--------------------------
  1549. ;***************************************************************************
  1550. ;*
  1551. ;* "div16s" - 16/16 Bit Signed Division
  1552. ;*
  1553. ;* This subroutine divides signed the two 16 bit numbers
  1554. ;* "dd16sH:dd16sL" (dividend) and "dv16sH:dv16sL" (divisor).
  1555. ;* The result is placed in "dres16sH:dres16sL" and the remainder in
  1556. ;* "drem16sH:drem16sL".
  1557. ;*
  1558. ;* Number of words :39
  1559. ;* Number of cycles :247/263 (Min/Max)
  1560. ;* Low registers used :3 (d16s,drem16sL,drem16sH)
  1561. ;* High registers used :7 (dres16sL/dd16sL,dres16sH/dd16sH,dv16sL,dv16sH,
  1562. ;* dcnt16sH)
  1563. ;*
  1564. ;***************************************************************************
  1565.  
  1566. ;***** Subroutine Register Variables
  1567.  
  1568. .def d16s =r13 ;sign register
  1569. .def drem16sL=r14 ;remainder low byte
  1570. .def drem16sH=r15 ;remainder high byte
  1571. .def dres16sL=r16 ;result low byte
  1572. .def dres16sH=r17 ;result high byte
  1573. .def dd16sL =r16 ;dividend low byte
  1574. .def dd16sH =r17 ;dividend high byte
  1575. .def dv16sL =r18 ;divisor low byte
  1576. .def dv16sH =r19 ;divisor high byte
  1577. .def dcnt16s =r20 ;loop counter
  1578.  
  1579. ;***** Code
  1580.  
  1581. div16s: ;push r13 ;PB !!
  1582. ;push r14 ;PB !!
  1583. mov d16s,dd16sH ;move dividend High to sign register
  1584. eor d16s,dv16sH ;xor divisor High with sign register
  1585. sbrs dd16sH,7 ;if MSB in dividend set
  1586. rjmp d16s_1
  1587. com dd16sH ; change sign of dividend
  1588. com dd16sL
  1589. subi dd16sL,low(-1)
  1590. sbci dd16sL,high(-1)
  1591. d16s_1: sbrs dv16sH,7 ;if MSB in divisor set
  1592. rjmp d16s_2
  1593. com dv16sH ; change sign of divisor
  1594. com dv16sL
  1595. subi dv16sL,low(-1)
  1596. sbci dv16sL,high(-1)
  1597. d16s_2: clr drem16sL ;clear remainder Low byte
  1598. sub drem16sH,drem16sH;clear remainder High byte and carry
  1599. ldi dcnt16s,17 ;init loop counter
  1600.  
  1601. d16s_3: rol dd16sL ;shift left dividend
  1602. rol dd16sH
  1603. dec dcnt16s ;decrement counter
  1604. brne d16s_5 ;if done
  1605. sbrs d16s,7 ; if MSB in sign register set
  1606. rjmp d16s_4
  1607. com dres16sH ; change sign of result
  1608. com dres16sL
  1609. subi dres16sL,low(-1)
  1610. sbci dres16sH,high(-1)
  1611. d16s_4: ;pop r14 ;PB!!
  1612. ;pop r13 ;PB!!
  1613. ret ; return
  1614. d16s_5: rol drem16sL ;shift dividend into remainder
  1615. rol drem16sH
  1616. sub drem16sL,dv16sL ;remainder = remainder - divisor
  1617. sbc drem16sH,dv16sH ;
  1618. brcc d16s_6 ;if result negative
  1619. add drem16sL,dv16sL ; restore remainder
  1620. adc drem16sH,dv16sH
  1621. clc ; clear carry to be shifted into result
  1622. rjmp d16s_3 ;else
  1623. d16s_6: sec ; set carry to be shifted into result
  1624. rjmp d16s_3
  1625.  
  1626. ;-----------------------------------------------
  1627.  
  1628. test_div16s:
  1629. ;***** Divide Two Signed 16-Bit Numbers (-22,222/10)
  1630. ldi dd16sL,low(-22222)
  1631. ldi dd16sH,high(-22222)
  1632. ldi dv16sL,low(10)
  1633. ldi dv16sH,high(10)
  1634. rcall div16s ;result: $f752 (-2222)
  1635. ;remainder: $0002 (2)
  1636.  
  1637. forever:rjmp forever
  1638. ;----------------------------------
  1639. test_slashMod:
  1640. ldi r16,$12
  1641. mypush r16
  1642. ldi r16,$34
  1643. mypush r16
  1644. ldi r16,$56 ;NB this is $3412 not $1234
  1645. mypush r16
  1646. ldi r16,$00
  1647. mypush r16
  1648. rcall slashMod ;$3412 / $56 = $9b rem 0 works
  1649. tslm: rjmp tslm
  1650.  
  1651. ;---------------------------------------
  1652. ;From http://www.avr-asm-tutorial.net/avr_en/calc/CONVERT.html#hex2bin
  1653. ; Hex4ToBin2
  1654. ; converts a 4-digit-hex-ascii to a 16-bit-binary
  1655. ; In: Z points to first digit of a Hex-ASCII-coded number
  1656. ; Out: T-flag has general result:
  1657. ; T=0: rBin1H:L has the 16-bit-binary result, Z points
  1658. ; to the first digit of the Hex-ASCII number
  1659. ; T=1: illegal character encountered, Z points to the
  1660. ; first non-hex-ASCII character
  1661. ; Used registers: rBin1H:L (result), R0 (restored after
  1662. ; use), rmp
  1663. ; Called subroutines: Hex2ToBin1, Hex1ToBin1
  1664.  
  1665. .def rBin1H =r17
  1666. .def rBin1L = r16
  1667. .def rmp = r18
  1668. ;
  1669. Hex4ToBin2:
  1670. clt ; Clear error flag
  1671. rcall Hex2ToBin1 ; convert two digits hex to Byte
  1672. brts Hex4ToBin2a ; Error, go back
  1673. mov rBin1H,rmp ; Byte to result MSB
  1674. rcall Hex2ToBin1 ; next two chars
  1675. brts Hex4ToBin2a ; Error, go back
  1676. mov rBin1L,rmp ; Byte to result LSB
  1677. sbiw ZL,4 ; result ok, go back to start
  1678. Hex4ToBin2a:
  1679. ret
  1680. ;
  1681. ; Hex2ToBin1 converts 2-digit-hex-ASCII to 8-bit-binary
  1682. ; Called By: Hex4ToBin2
  1683. ;
  1684. Hex2ToBin1:
  1685. push R0 ; Save register
  1686. rcall Hex1ToBin1 ; Read next char
  1687. brts Hex2ToBin1a ; Error
  1688. swap rmp; To upper nibble
  1689. mov R0,rmp ; interim storage
  1690. rcall Hex1ToBin1 ; Read another char
  1691. brts Hex2ToBin1a ; Error
  1692. or rmp,R0 ; pack the two nibbles together
  1693. Hex2ToBin1a:
  1694. pop R0 ; Restore R0
  1695. ret ; and return
  1696. ;
  1697. ; Hex1ToBin1 reads one char and converts to binary
  1698. ;
  1699. Hex1ToBin1:
  1700. ld rmp,z+ ; read the char
  1701. subi rmp,'0' ; ASCII to binary
  1702. brcs Hex1ToBin1b ; Error in char
  1703. cpi rmp,10 ; A..F
  1704. brcs Hex1ToBin1c ; not A..F
  1705. cpi rmp,$30 ; small letters?
  1706. brcs Hex1ToBin1a ; No
  1707. subi rmp,$20 ; small to capital letters
  1708. Hex1ToBin1a:
  1709. subi rmp,7 ; A..F
  1710. cpi rmp,10 ; A..F?
  1711. brcs Hex1ToBin1b ; Error, is smaller than A
  1712. cpi rmp,16 ; bigger than F?
  1713. brcs Hex1ToBin1c ; No, digit ok
  1714. Hex1ToBin1b: ; Error
  1715. sbiw ZL,1 ; one back
  1716. set ; Set flag
  1717. Hex1ToBin1c:
  1718. ret ; Return
  1719. ;--------------------------------------
  1720. test_Hex4ToBin2:
  1721. pushz
  1722. ldi zl,$60
  1723. clr zh ;z now points to start of buf1
  1724. ldi r16,'0'
  1725. st z+,r16
  1726. ldi r16,'f'
  1727. st z+,r16
  1728. ldi r16,'2'
  1729. st z+,r16
  1730. ldi r16,'3'
  1731. st z+,r16
  1732. ldi zl,$60
  1733. clr zh ;z now points back to start of buf1
  1734. rcall Hex4ToBin2
  1735. popz
  1736. th4: rjmp th4
  1737. ;-------------------------------------
  1738. numberh: ;word not in dictionary. Try to convert it to hex.
  1739. pushz ;algorithm uses z, pity
  1740. movw zl,r24 ;r4,25 = w holds start of current word
  1741. ;z now points eg to '12ab'start. If t=0 then it coverts to real hex
  1742. rcall hex4ToBin2 ;try to convert
  1743. ;above call needs 4 hex digits to emerge with t=0 and binary in r16,17
  1744. ;want this. If t=0 stack r16,17 and carry on interpreting, else emerge with
  1745. ; t=1 and zpointing to first problem char
  1746. brtc gotHex
  1747. ; if here there's a problem that z is pointing to. Bail out of interpret line
  1748. clr STOP
  1749. inc STOP
  1750. ;TODO put routine here that notes the word can't be excuted and it's
  1751. ; not a number. So output ramstring starting at adr = r24,25 and len in r20
  1752. rcall whatq
  1753. rjmp cold ; quit ;outnh **check
  1754.  
  1755. gotHex: ;sucess.Real hex in r16,17
  1756. mypush2 r16,r17 ; so push num onto mystack
  1757. ;maybe we're compiling. If so, push num into dic preceded by a call to stackme_2
  1758. tst STATE
  1759. breq outnh ;STATE =0 means executing
  1760. ; rcall tic
  1761. ; .db "stackme_2" ;has to be in dic before a number. cfa of stackme_2 on stack
  1762. rcall compstackme_2
  1763. ; rcall compileme ;insert "rcall stackme_2"opcode into dic
  1764. rcall comma ;there's the number going in
  1765.  
  1766. outnh:
  1767. popz ; but will it be pointing to "right"place in buf1? Yes now OK
  1768.  
  1769. ret
  1770. ; numberh not working fully, ie doesn't point to right place after action.
  1771. ; also no action if not a number? DONE better save this first.
  1772. ;---------------------------------
  1773. ;eeroutines
  1774. eewritebyte: ;write what's in r16 to eeprom adr in r18,19
  1775. sbic EECR,EEPE
  1776. rjmp eewritebyte ;keep looping til ready to write
  1777. ;if here the previous write is all done and we can write the next byte to eeprom
  1778. out EEARH,r19
  1779. out EEARL,r18 ;adr done
  1780. out EEDR,r16 ;byte in right place now
  1781. sbi EECR,EEMPE
  1782. sbi EECR,EEPE ;last 2 instruc write eprom. Takes 3.4 ms
  1783. ret
  1784. ;test with %!
  1785. ;---------------------------------
  1786. eereadbyte: ; read eeprom byte at adr in r18,19 into r16
  1787. ; Wait for completion of previous write
  1788. sbic EECR,EEPE
  1789. rjmp eereadbyte
  1790. ; Set up address (r18:r17) in address register
  1791. out EEARH, r19
  1792. out EEARL, r18
  1793. ; Start eeprom read by writing EERE
  1794. sbi EECR,EERE
  1795. ; Read data from data register
  1796. in r16,EEDR
  1797. ret
  1798. ;------------------------------
  1799. setupforflashin: ;using here etc get appropriate page, offset,myhere values.
  1800. ; ldi r16,low(HERE)
  1801. ; ldi r17,high(HERE) ;get here, but from eeprom better?
  1802. ; mypush2 r16,r17
  1803.  
  1804. ;above was a problem replace with one line below
  1805. rcall gethere ;HERE = eg 0a12.Now on stk.Comes from eepprom each time
  1806.  
  1807. rcall stackme_2
  1808. .dw 0002
  1809. rcall star ;now have current HERE in bytes in flash. But what is myhere?
  1810. rcall stackme_2
  1811. .db $0040 ;64 bytes per page
  1812. rcall slashMod
  1813. ;offset on top pagenum under. eg pg 0047, offset 0012
  1814. mypop2 r9,r8 ;store offset (in bytes)
  1815. rcall stackme_2
  1816. .db $0040
  1817. rcall star ;pgnum*64 = byte adr of start of flash page
  1818. mypop2 r7,r6
  1819. mypush2 r8,r9 ;push back offset
  1820. rcall stackme_2
  1821. .dw buf2
  1822. nop
  1823. ;at this stage we have offset in r8,r9 (0012). Also byte adr of flash page
  1824. ; start in r6,r7.(11c0) Stk is (offset buf2Start --) (0012 00E0 --). Need to
  1825. ; add these two together to get myhere, the pointer to RAM here position.
  1826. rcall plus ;add offset to buf2 start to get myhere (00f2)
  1827. ; put my here in r4,r5 for time being.
  1828. mypop2 r5,r4 ;contains eg 00f2 <--myhere
  1829. pushz ;going to use z so save it
  1830. movw zl,r6 ;r6,7 have byte adr of flsh pg strt
  1831. pushx ;save x
  1832. ldi xl,low(buf2)
  1833. ldi xh,high(buf2) ;point x to start of buf2
  1834. ldi r18,128 ;r18=ctr. Two flash pages = 128 bytes
  1835. upflash:
  1836. lpm r16,z+ ;get byte from flash page
  1837. st x+, r16 ; and put into buf2
  1838. dec r18
  1839. brne upflash
  1840. ;done. Now have two flash pages in ram in buf2. Myhere points to where next
  1841. ; entry will go. Where's page num?
  1842. popx
  1843. popz ;as if nothing happened
  1844.  
  1845.  
  1846. ret
  1847.  
  1848.  
  1849.  
  1850. ;outsufi: rjmp outsufi
  1851. ;-----------------------------------
  1852. burneepromvars: ;send latest versions of eHERE and eLATEST to eeprom
  1853. ldi r16,low(HERE)
  1854. ldi r17,high(HERE)
  1855. mypush2 r16,r17
  1856. ;up top we have .equ eHERE = $0010
  1857. ldi r16,low(eHERE)
  1858. ldi r17,high(eHERE)
  1859. mypush2 r16,r17
  1860. ;now have n16 eadr on stack ready for e!
  1861. rcall percentstore
  1862.  
  1863. ;send latest versions of eLATEST to eeprom
  1864. ldi r16,low(LATEST)
  1865. ldi r17,high(LATEST)
  1866. mypush2 r16,r17
  1867. ;up top we have .equ eLATEST = $0010
  1868. ldi r16,low(eLATEST)
  1869. ldi r17,high(eLATEST)
  1870. mypush2 r16,r17
  1871. ;now have n16 eadr on stack ready for e!
  1872. rcall percentstore
  1873. ret
  1874. ;-------------------------------------------
  1875. coloncode: ;this is the classic colon defining word.
  1876. rcall setupforflashin ;get all the relevant vars and bring in flash to buf2
  1877. ;rcall dxyz
  1878. rcall relinkcode ; insert link into first cell
  1879. rcall create ;compile word preceeded by length
  1880. rcall leftbrac ;set state to 1, we're compiling
  1881. ;takemeout 'c'
  1882. ;rcall report
  1883. ;takemeout 'c'
  1884. ret ;now every word gets compiled until we hit ";"
  1885. ;-------------------------
  1886. relinkcode: ;put LATEST into where myhere is pointing and update ptr = myhere
  1887. ;also create mylatest
  1888. rcall getlatest ;now on stack
  1889. mypopa ;latest in r16,17
  1890. pushz ;better save z
  1891. movw mylatest,myhere ;mylatest <-- myhere
  1892. movw zl,myhere ;z now points to next available spot in buf2
  1893. st z+,r17 ;problem. Don't work unless highbye first in mem.Why?
  1894. st z+,r16 ;now have new link in start of dic word
  1895. movw myhere,zl ;update myhere to point to length byte. (Not yet there.)
  1896. popz ;restore z
  1897. ret
  1898. ;-------------------------------------------------
  1899. create: ;put word after ":" into dictionary, aftyer link, preceeded by len
  1900. rcall word ;start with x pnting just after ":".End with len in r20, x pointing to
  1901. ; space just after word and start of word in w=r24,25
  1902. pushz ;save z. It's going to be used on ram dictionary
  1903. movw zl,myhere ;z now pnts to next spot in ram dic
  1904. st z+,r20 ; put len byte into ram dic
  1905. mov r18,r20 ;use r18 as ctr, don't wreck r20
  1906. pushx ;save x. It's going to be word ptr in buf1
  1907. movw xl,wl ;x now points to start of word. Going to be sent to buf2
  1908. sendbytes:
  1909. ld r16,x+ ;tx byte from buf1 to
  1910. st z+,r16 ; buf2
  1911. dec r18 ;repeat r20=r18=len times
  1912. brne sendbytes
  1913.  
  1914. sbrs r30,0 ;skip next instruction if final bit lsb = 1
  1915. rjmp downcr
  1916. ;if here lsb = 1 so we're on a padding byte and have to add 1 to get to a 2 byte boundary
  1917. clr r16
  1918. st z+,r16 ;insert padding byte
  1919. ;inc r30
  1920. ;brcc downcr
  1921. ;inc r31 ;add one to z before converting to bytes
  1922.  
  1923. downcr:
  1924. movw myhere,zl ;myhere now points to beyond word in dic
  1925. popx
  1926. popz
  1927. ret ;with word in dic
  1928. ;----------------------------------------------
  1929. leftbrac: ;classic turn on compiling
  1930. clr STATE
  1931. inc STATE ;state =1 ==> now compiling
  1932. ret
  1933. ;------------------------
  1934. compilecode: ;come here with STATE =1 ie compile, not execute. Want to put
  1935. ; eg rcall dup in code in dictionary but not to execute dup. If here
  1936. ; z points to byte address of word
  1937. mypush2 zl,zh
  1938. compileme:
  1939. mypush2 myhere,r5 ;push ptr to RAM dic
  1940. ;next is entry point for eg ' stackme2 already on stack and have to compile
  1941.  
  1942. ldi r16,low(buf2)
  1943. ldi r17,high(buf2) ;start of buf that conatins flash pg in RAM
  1944. mypush2 r16,r17
  1945. rcall minus ; myhere - buf2-start = offset in page
  1946. mypush2 SOFPG,r7 ;push start of flash page address
  1947. rcall plus ;SOFPG + offset = adr of next rcall in dic
  1948. ;if here we have two flash addresses on the stack. TOS = here. Next is there.
  1949. ;want to insert code for "rcall there w"hen I'm at here. eg current debugging indicates
  1950. ; here = $11EB and there is $1012 (cfa of "two"). First compute
  1951. ; relative branch "there - here -2". Then fiddle this val into the rcall opcode
  1952. rcall minus ;that;s there - here. Usu negative.
  1953. ;I got fffffffff..ffe27 for above vals. First mask off all those f's
  1954. rcall two ;stack a 2
  1955. rcall minus ;now have there-here -2 = fe24. When there,here in bytes.
  1956. mypopa ;bring fe26 into r16,17
  1957. clc
  1958. ror r17
  1959. ror r16 ;now a:= a/2
  1960. ldi r18,$ff
  1961. ldi r19,$0f ;mask
  1962. and r16,r18
  1963. and r17,r19
  1964. ; mypush2 r16,r17 ;now fe26 --> 0e26
  1965. ;the rcall opcode is Dxxx where xxx is the branch
  1966. ; mypopa ;bring fe26 into r16,17
  1967. ldi r19, $d0 ;mask
  1968. or r17,r19
  1969. mypush2 r16,r17 ;now have $de26 on stack which is (?) rcall two
  1970. rcall comma ;store this opcode into dic. myhere is ptr
  1971. ret
  1972. ;---------------------------
  1973. stackme_2: ;stacks on my stack next 16bit num. Address of 16bit number is on SP-stack
  1974. ; Used like this stackme_2 0034. Puts 0034 on myStack and increments past number on return stack.
  1975. pop r17
  1976. pop r16 ; they now contain eg 0x0804 which contain the 16bit num
  1977. movw zl,r16 ;z now points to cell that cobtains the number
  1978. clc
  1979. rol zl
  1980. rol zh ;double word address for z. lpm coming up
  1981.  
  1982.  
  1983.  
  1984. lpm r16,z+
  1985. lpm r17,z+ ;now have 16bit number in r16,17
  1986.  
  1987. st y+,r16
  1988. st y+, r17 ;mystack now contains the number
  1989.  
  1990. clc
  1991. ror zh
  1992. ror zl ;halve the z pointer to step past the number to return at the right place
  1993.  
  1994. push zl
  1995. push zh
  1996.  
  1997. ret
  1998. ;------------------------------flash write section--------------------
  1999.  
  2000. do_spm:
  2001. ;lds r16,SPMCSR
  2002. in r16,SPMCSR
  2003. andi r16,1
  2004. cpi r16,1
  2005. breq do_spm
  2006. mov r16,spmcsr_val
  2007. out SPMCSR,r16
  2008. spm
  2009. ret
  2010. ;-------------------------------------------------------------------
  2011. buf2ToFlashBuffer: ;send the 64 bytes, 32 words to flash page <-- Z pnts there.
  2012. push r30 ;save for later spm work.
  2013. push r19
  2014. push xl
  2015. push xh ;used as buf_ctr but may interfere with other uses
  2016. ldi XL,low(buf2) ;X pnts to buf1 that contains the 64 bytes.
  2017. ldi XH, high(buf2)
  2018. ;assume Z is already pointing to correct flash start of page.
  2019. flashbuf:
  2020. ldi buf_ctr,32 ;send 32 words
  2021. sendr0r1:
  2022. ld r16, x+ ;get first byte
  2023. mov r0,r16 ; into r0
  2024. ld r16, x+ ; and get the second of the pair into
  2025. mov r1,r16 ; into r1
  2026. ldi spmcsr_val,01 ;set up for write into spare buffer flash page
  2027. rcall do_spm ;that's r0,r1 gone in.
  2028. inc r30
  2029. inc r30
  2030. dec buf_ctr ;done 32 times?
  2031. brne sendr0r1
  2032. pop xh
  2033. pop xl
  2034. pop r19 ;dont need buf_ctr any more.
  2035. pop r30 ;for next spm job
  2036.  
  2037. ret
  2038. ;--------------------------------------------------------------------------
  2039. ;TODO just have 1 burn routine with buf different
  2040. buf3ToFlashBuffer: ;send the 64 bytes, 32 words to flash page <-- Z pnts there.
  2041. push r30 ;save for later spm work.
  2042. push r19 ;used as buf_ctr but may interfere with other uses
  2043. push xl
  2044. push xh
  2045. ldi XL,low(buf2+64) ;X pnts to buf1 that contains the 64 bytes.
  2046. ldi XH, high(buf2+64)
  2047. ;assume Z is already pointing to correct flash start of page.
  2048. rjmp flashbuf
  2049. ldi buf_ctr,32 ;send 32 words
  2050. sendr0r3:
  2051. ld r16, x+ ;get first byte
  2052. mov r0,r16 ; into r0
  2053. ld r16, x+ ; and get the second of the pair into
  2054. mov r1,r16 ; into r1
  2055. ldi spmcsr_val,01 ;set up for write into spare buffer flash page
  2056. rcall do_spm ;that's r0,r1 gone in.
  2057. inc r30
  2058. inc r30
  2059. dec buf_ctr ;done 32 times?
  2060. brne sendr0r3
  2061. pop r19 ;dont need buf_ctr any more.
  2062. pop r30 ;for next spm job
  2063. ret
  2064.  
  2065. erasePage: ; assume Z points to start of a flash page. Erase it.
  2066. ldi spmcsr_val,0x03 ;this is the page erase command
  2067. rcall do_spm
  2068. ret
  2069. ;------------------------------------------------------------------
  2070. writePage:
  2071. ldi spmcsr_val, 0x05 ;command that writes temp buffer to flash. 64 bytes
  2072. rcall do_spm
  2073. nop ; page now written. z still points to start of this page
  2074. ret
  2075. ;---------------------------------------------------------------
  2076. test_buf2ToFlashBuffer: ;(adr_flashbufstartinBytes -- )
  2077. ; rcall fillBuf
  2078. ; ldi ZH, $10
  2079. ; ldi ZL,$c0 ;z=$01c0. Start of page 67.
  2080. rcall gethere
  2081. rcall double ;want bytes not words for flash adr
  2082. mypopa ;flashPgStart byte adr now in r16,17
  2083.  
  2084.  
  2085. movw zl,r16 ;z <--start of flash buffer
  2086. rcall erasePage
  2087. rcall buf2ToFlashBuffer
  2088. rcall writePage
  2089. herettt:
  2090. rjmp herettt
  2091. ;----------------------
  2092. ; y2. Come here from ";". The pair r6,r7 point to start of flash pg (bytes)
  2093. burnbuf2and3:
  2094. ;takemeout 'U'
  2095. ;ldi r16, 'U'
  2096. ;clr r17
  2097. ;mypush2 r16,r17
  2098. ;rcall emitcode
  2099. ;rcall dlowR
  2100. movw zl,r6 ;z now pnts to start of flash buf
  2101. ;rcall dxyz ;having !!! PROBS take out later
  2102. rcall erasePage
  2103. rcall buf2ToFlashBuffer
  2104. rcall writePage
  2105. ;now going to burn next ram buffer to next flash page. Bump Z by 64 bytes.
  2106. adiw zh:zl,63 ;z now points to start of next flash buffer
  2107. lpm r16,z+ ;advance z pointer by one.adiw only lets max of 63 to be added.
  2108. ;now z points to start of next 64 byte buffer. Time to put buf3 into it.
  2109. rcall erasePage
  2110. rcall buf3ToFlashBuffer
  2111. rcall writePage
  2112. ret
  2113. heret:
  2114. rjmp heret
  2115. ;-------------------------------------------------------------
  2116. updatevars: ;after doing a colon def we have to update sys vars
  2117. ;TODO new version of LATEST is just old version of HERE.
  2118. ;TODO rplace all this code with updatevars2
  2119. ; just shif HERE into LATEST in eeprom to update. Gen. tidy required.
  2120. mypush2 r4,r5 ;put myhere on stack (E8)
  2121. ldi r16,low(buf2)
  2122. ldi r17,high(buf2)
  2123. mypush2 r16,r17 ;start of buf2 on stack (E0)
  2124. rcall minus ;myhere-buf2 = offset. (e8-e0 = 08)
  2125. mypush2 SOFPG,r7 ; push onto stk start adr of flash page
  2126. rcall plus ;SOFG + offset = new HERE
  2127. ;now put also on stack new version of LATEST
  2128. mypush2 r2,r3 ;that's mylatest on stack
  2129. ldi r16,low(buf2)
  2130. ldi r17,high(buf2)
  2131. mypush2 r16,r17 ;start of buf2 on stack (E0)
  2132. rcall minus ;myhere-buf2 = offset. (e8-e0 = 08)
  2133. mypush2 SOFPG,r7 ; push onto stk start adr of flash page
  2134. rcall plus ;SOFG + offset = new LATEST
  2135. ; now have both LATEST (tos) and HERE on stack. Burn these into eeprom
  2136. ;up top we have .equ eLATEST = $0010
  2137. ;But it's too big. In bytes and causing probs. Solution=covert to words
  2138. rcall halve
  2139. ldi r16,low(eLATEST)
  2140. ldi r17,high(eLATEST)
  2141. mypush2 r16,r17
  2142. ;now have n16 eadr on stack ready for e!
  2143. rcall percentstore
  2144. ; TODO the value for HERE is prob in bytes too. Convert to words.
  2145. ;up top we have .equ eLATEST = $0010
  2146. ldi r16,low(eHERE)
  2147. ldi r17,high(eHERE)
  2148. mypush2 r16,r17
  2149. ;now have n16 eadr on stack ready for e!
  2150. rcall halve ;TODO check this
  2151. rcall percentstore
  2152. ret ;with stack clear and new vals for HERE and LATEST in eeprom
  2153. ;----------
  2154. ;;;;;;;;;;;;;;;;;;;;;;;;;;;Now serial stuff starts;;;;;;;;;;;;;;;;;;;;;;;;;
  2155. halfBitTime: ;better name for this delay. Half of 1/600
  2156. ;myDelay1200:
  2157. ;ldi r21,13 ; 13 works for m328 at 16Mhz
  2158. push r20
  2159. push r21
  2160. ldi r21,7 ;try 7 for tiny85 at 8Hmz
  2161. ldi r20,130 ;r20,21 at 130,7 give 833uS. Good for 600baud at 8Mhz
  2162. starthbt:
  2163. inc r20
  2164. nop
  2165. brne starthbt
  2166. dec r21
  2167. brne starthbt
  2168. pop r21
  2169. pop r20
  2170. ret
  2171. ;--------------------------------------------------
  2172. oneBitTime:
  2173. rcall halfBitTime
  2174. rcall halfBitTime
  2175. ret
  2176. ;-------------------------------------------------
  2177. sendAZero:
  2178. ;output 0 on Tx pin
  2179. cbi PORTB,TX_PIN ; send a zero out PB0
  2180. ret
  2181. ;-----------------------------------------------------
  2182.  
  2183. sendAOne:
  2184. ;output 1 on Tx pin
  2185. sbi PORTB,TX_PIN ; send a zero out PB0
  2186. ret
  2187. ;-----------------------------------------------------
  2188. sendStartBit:
  2189. ; send a 0 for one bit time
  2190. rcall sendAZero
  2191. rcall oneBitTime
  2192. ret
  2193. ;-------------------------------------------------------
  2194. sendNextDataBit: ;main output routine for serial tx
  2195. lsr serialByteReg ;push high bit into carry flag then inspect it
  2196. ;originally did lsl but found lsb first.
  2197. brcc gotzero ;if it's a 0 do nothing
  2198. rcall sendAOne ;must have been a 1 in carry
  2199. rjmp down
  2200. gotzero:
  2201. rcall sendAZero ;if here carry was a zero
  2202. down:
  2203. rcall oneBitTime ;so that 1 or 0 lasts 1/600 sec
  2204. ret
  2205. ;-------------------------------------------------------------
  2206. send8DataBits: ; send all bits in serialByteReg
  2207. ldi counterReg,8 ;8 data bits
  2208. sendBit:
  2209. rcall sendNextDataBit
  2210. dec counterReg
  2211. brne sendBit
  2212. ret
  2213. ;--------------------------------------------------------
  2214. sendStopBit:
  2215. ; send a 1 for one bit time
  2216. rcall sendAOne
  2217. rcall oneBitTime
  2218. ret
  2219. ;--------------------------------------------------------
  2220. sendSerialByte: ;main routine. Byte in serialByteReg = r16
  2221. .ifdef testing
  2222. mov r0, r16
  2223. .else
  2224. push counterReg
  2225. rcall sendStartBit
  2226. rcall send8DataBits
  2227. rcall sendStopBit
  2228. rcall sendStopBit ;two stops
  2229. pop counterReg
  2230. .endif
  2231. ret
  2232. ;**************************************************************
  2233. serialTest0: ;output series of 'AAAA..'s
  2234. ldi serialByteReg, 0x43 ;0x41
  2235. rcall sendSerialByte
  2236. rcall oneBitTime ; take a rest
  2237. ldi r16,$44
  2238. mypush r16
  2239. rcall emitcode
  2240.  
  2241. rjmp serialTest0 ;continue forever
  2242. ;---------------------------------------------------------
  2243. ;---------Now do SerialRx routines-------------------
  2244. waitForHigh: ;loop til RX is high
  2245. sbis PINB,RX_PIN ;test that pin for set (PB2)
  2246. rjmp waitForHigh ; loop if rx pin is low
  2247. ret
  2248. ;-----------------------------------------------
  2249. waitForLow: ;PRONBLEMs loop til RX is low. FIXED.
  2250. sbic PINB,2 ;test that pin for set (PB2)
  2251. rjmp waitForLow ; loop if rx pin is high
  2252. ret
  2253. ;---------------------------------------------------
  2254. waitForStartBit: ;loop til get a real start bit
  2255. rcall waitForHigh ;should be marking at start
  2256. rcall waitForLow ;gone low. might be noise
  2257. rcall halfBitTime ;is it still low in middle of bit time
  2258. sbic PINB,RX_PIN ;..well, is it?
  2259. rjmp waitForStartBit ;loop if level gone back high. Not a start bit.
  2260. ret ;we've got our start bit
  2261. ;----------------------------------------------------
  2262. checkForStopBit: ;at end, get carry flag to reflect level. Prob if c=0
  2263. rcall oneBitTime ; go into stop bit frame, halfway
  2264. sec ;should stay a 1 in C if stop bit OK
  2265. sbis PINB,RX_PIN ;don't clc if bit is high
  2266. clc ;but only if we have a weird low stop bit
  2267. ret ;with carry flag = stop bit. Should be a 1
  2268. ;-------------------------------------------------------------
  2269. get8Bits: ;get the 8 data bits. No frame stuff
  2270. clr rxbyte ;this will fill up with bits read from RX_PIN
  2271. push counterReg ;going to use this so save contents for later
  2272. ldi counterReg,8 ;because we're expecting 8 databits
  2273. nextBit:
  2274. rcall oneBitTime ;first enter here when mid-startbit
  2275. rcall rxABit ;get one bit
  2276. dec counterReg ;done?
  2277. brne nextBit ;no, round again
  2278. pop counterReg ;yes, finished, restor counter and get out
  2279. ret
  2280. ;---------------------------------------------------------------
  2281. rxABit: ;big serial input routine for one bit
  2282. clc ;assume a 0
  2283. sbic PINB,RX_PIN ; skip nxt if pin low
  2284. sec ;rx pin was high
  2285. ror rxbyte ;carry flag rolls into msb first
  2286. ret
  2287. ;********************************
  2288. getSerialByte: ;big routine. Serial ends up in rxByte
  2289. push counterReg
  2290. rcall waitForStartBit ;**change
  2291. rcall get8Bits
  2292. rcall checkForStopBit
  2293. pop counterReg
  2294. ret ;with rxByte containing serial bye
  2295. ;----------------------------------------------------
  2296. serialTest1: ;output A then reflect input. Worked OK
  2297. ldi serialByteReg, 0x36 ;0x41
  2298. rcall sendSerialByte
  2299. rcall oneBitTime ; take a rest
  2300. rcall getSerialByte
  2301. mov serialByteReg,rxByte ;output what's been read
  2302. rcall sendSerialByte
  2303. rjmp serialTest1
  2304. ;--------------------------------------------------------
  2305. ;----------Now doing buffer work. Want to and from 64 bytes----------
  2306. fillBuf:
  2307. ldi ZL,low(buf1) ;buf1 is my buffer
  2308. ldi ZH, high(buf1) ;Z now points to buf1
  2309. ldi counterReg,64 ;64 bytes in buffer
  2310. ldi r16,$30
  2311. storeB0:
  2312. st z+,r16
  2313. inc r16
  2314. dec counterReg
  2315. brne storeB0
  2316. herefb:
  2317. ; rjmp herefb
  2318. ret
  2319. ;----------------------------------------------------------
  2320. serialStrOut: ;X points to start of string,r17 has length
  2321. ld serialByteReg, x+
  2322.  
  2323. rcall sendSerialByte
  2324. dec r17 ;got to end of string?
  2325. brne serialStrOut
  2326. ret
  2327. ;----------------------------------
  2328. test_serialStrOut:
  2329. rcall fillBuf
  2330. ldi XL,low(buf1) ;buf1 start of str
  2331. ldi XH, high(buf1)
  2332. ldi r17,64 ;going to send len=r17 bytes
  2333. rcall serialStrOut
  2334. here2:
  2335. rjmp here2
  2336. ;--------------------------------------
  2337. waitForCharD: ;wait til eg a 'D' is pressed then do something.
  2338. ldi serialByteReg, '>' ;0x41
  2339. rcall sendSerialByte
  2340. rcall oneBitTime ; take a rest
  2341. rcall getSerialByte
  2342. mov serialByteReg,rxByte ;output what's been read
  2343. cpi rxByte, 'D'
  2344. brne waitForCharD
  2345. ldi serialByteReg, '*'
  2346. rcall sendSerialByte
  2347. rjmp waitForCharD
  2348. ;-----------------------------------------------------------
  2349. dumpbuf1:
  2350. .ifdef livetesting
  2351. ldi XL,low(buf1) ;buf1 start of str
  2352. ldi XH, high(buf1)
  2353. ldi r17,64 ;going to send len=r17 bytes
  2354. rcall serialStrOut
  2355. .endif
  2356. ret
  2357. ;-------------------------------------------------------------
  2358. test_dumpbuf1:
  2359. rcall fillBuf
  2360. rcall getSerialByte ;any one will do.
  2361. rcall dumpbuf1
  2362. rjmp test_dumpbuf1
  2363. ;----------------------------------------------------------
  2364. waitForDDump: ;wait til eg a 'D' is pressed then dump buf1
  2365. ldi serialByteReg, '>' ;0x41
  2366. rcall sendSerialByte
  2367. rcall oneBitTime ; take a rest
  2368. rcall getSerialByte
  2369. mov serialByteReg,rxByte ;output what's been read
  2370. cpi rxByte, 'D'
  2371. brne waitForDDump
  2372. rcall dumpbuf1
  2373. rjmp waitForCharD
  2374. ;---------------------------------------------------------------
  2375. rxStrEndCR: ;get a serial string that ends with CR
  2376. clr counterReg
  2377. ldi XL,low(buf1) ;buf1 is where str will go
  2378. ldi XH, high(buf1)
  2379. takemeout 'A'
  2380. upsec:
  2381. rcall getSerialByte
  2382.  
  2383. st x+, rxByte ;char goes into buffer="buf1"
  2384.  
  2385. cpi rxByte,$0d ;is it CR = end of string?
  2386. breq fin
  2387. inc counterReg ;don't go over 64 bytes
  2388. cpi counterReg,64
  2389. brne upsec ;not too long and not CR so keep going
  2390. fin:
  2391. ret
  2392. ;---------------------------------------------
  2393. test_rxStrEndCR: ;just a test of above
  2394. rcall OK
  2395. rcall CR
  2396. rcall rxStrEndCR
  2397. rcall dumpbuf1
  2398. rcall CR
  2399. ; rcall waitForDDump
  2400. rjmp test_rxStrEndCR
  2401. ;------------------------------------------------------
  2402. test2_rxStrEndCR: ;want a diagnostic dump if testing. Works with .IFDEF
  2403. rcall rxStrEndCR
  2404. .IFDEF testing
  2405. rcall dumpbuf1
  2406. .ENDIF
  2407. rjmp test2_rxStrEndCR
  2408. ;------------------------------------------------------------
  2409. rxStrWithLen: ;expect len char char char.. for len chars
  2410. push counterReg
  2411. ldi XL,low(buf1) ;buf1 is where str will go
  2412. ldi XH, high(buf1)
  2413. rcall getSerialByte ; get length bye Must be less than 65
  2414. mov counterReg, rxByte ;save len in counter
  2415. cpi counterReg,65 ;
  2416. brlo allOK ;less than 65 so carry on. Branch if Lower
  2417. ldi counterReg,64 ; if len>64 then len=64. Buffer = buf1 only 64 bytes
  2418. allOK:
  2419. tst counterReg ;zero yet?
  2420. breq finrs
  2421. rcall getSerialByte ;next serial input byte
  2422. st x+, rxByte ;put into buffer
  2423. dec counterReg ;have we done len=counterReg bytes?
  2424. rjmp allOK
  2425. finrs:
  2426. pop counterReg
  2427. ret
  2428. ;---------------------------------------------------------------
  2429. test_rsStrWithLen: ;works ok with macro $05GHIJKLM. Sends GHIJK
  2430. ldi r16, '#'
  2431. rcall sendSerialByte
  2432. rcall rxStrWithLen
  2433. rcall dumpbuf1
  2434. rjmp test_rsStrWithLen
  2435. ;-----------------------------now start forth i/o words like emit------------------
  2436. emitcode: ; (n8 --)classic emit
  2437. mypop r16
  2438. mypop r16 ;want lower byte eg in 0041 want just the 41
  2439. rcall sendserialbyte
  2440. ret
  2441. ;------------------------------------------------
  2442. insertret: ;semi has to end new word with ret = $9508 opcode
  2443. pushx ;both xl,xh saved for later
  2444. movw xl,myhere ;myhere points to next available spot in ram dic
  2445. ldi r16,$08
  2446. st x+,r16 ;$08 part goes first
  2447. ldi r16,$95
  2448. st x+,r16 ;ret now in ram. Just tidy pointers
  2449. movw myhere,xl
  2450. popx ;so x back where it was and ret inserted.
  2451. ret
  2452. ;--------------------------------
  2453. equalcode: ;(n1 n2 -- flag) if n1 = n2 flag = 0001 else 0000
  2454. mypopa
  2455. mypopb ; now have TOS in r16,17, underneath that in r18,19
  2456. cp r16,r18 ;low bytes =?
  2457. brne zout ;not equal so go out
  2458. cp r17,r19 ;hi bytes =?
  2459. brne zout ;no, so out
  2460. ;if here both n16's are equal so push a 0001
  2461. rcall one
  2462. rjmp aout ;done
  2463. zout:
  2464. rcall zero ;not = so push a zero
  2465. aout:
  2466. ret ;with a flag on stack replacing to n16's
  2467. ;------------------------------
  2468. ;TODO eliminate below and replace with simpler RAM jmp code.
  2469. calcjumpcode: ;(to from -- opcode_for_rjmp to at from)
  2470. ;used when compiling. What is the rjmp opcode if
  2471. ; we know the from and to adr on stack. ( to fr --)
  2472. ldi r16, low(buf2)
  2473. ldi r17, high(buf2)
  2474. mypush2 r16,r17 ; (to fr $e0 --)
  2475. rcall dup ;t f $e0 $eo
  2476. rcall unrot ;t $e0 fr $e0
  2477. rcall minus ;t $e0 frOffset
  2478. rcall unrot ;frOffset t $e0
  2479. rcall minus ;frOffset toOffset
  2480. ;now apply these offsets in flash buffer. Add them to start of flash buffer adr
  2481. mypush2 SOFPG,r7 ; frOffset toOffset SOFPG
  2482. rcall dup ;frOffset toOffset SOFPG SOFPG
  2483. rcall unrot ;frOffset SOFPG toOffset SOFPG
  2484. rcall plus ;frOffset SOFPG toFlashAdr
  2485. rcall unrot ;toFlashAdr frOffset SOFPG
  2486. rcall plus ;toFlashAdr frFlashAdr
  2487. rcall minus ;to -from give last 3 nibbles in rjmp opcode +1
  2488. ; rcall one
  2489. rcall two ;to - from - 2 when working with bytes
  2490. rcall minus ; now have to - from -2
  2491. rcall halve ;now have jmp length in words. Required for opcode.
  2492. rcall stackme_2
  2493. .dw $0fff
  2494. rcall andd ; now have eg. 0f20. Want Cf20
  2495. rcall stackme_2
  2496. .dw $c000 ;should now have right opcode eg cf20
  2497. rcall orr ;don't forget this !!!!!
  2498. ret ;with correct rjmp kkk on stack. Ready to insert into RAM dic.
  2499. ;-------------------
  2500. stackmyhere: ;( --- adr) put RAM ptr myhere on stack
  2501. mypush2 myhere, r5
  2502. ret
  2503. ;---------------------------
  2504. begincode: ;when using BEGIN just stack current address.No dic entry
  2505. rcall stackmyhere ;put next adr on stack
  2506. ret
  2507. ;----------------------------
  2508. stkmyhere: ;put myhere on the stack, handy
  2509. mypush2 myhere,r5
  2510. ret
  2511. ;-----------------------------------
  2512. stkSOBuf2: ;stack start of buf2. Handy.
  2513. ldi r16,low(buf2)
  2514. ldi r17,high(buf2)
  2515. mypush2 r16,r17
  2516. ret ;with adr of buf2 on stk
  2517. ;--------------------------
  2518. stkSOFPG: ;put start of flash page on stack, In bytes.
  2519. mypush2 SOFPG,r7
  2520. ret ;with start of current flash page's adr on stack.
  2521. ;-------------------------------
  2522. stklatestadr: ;put e-adr of eLatest. Currently 012 in eeprom
  2523. ldi r16,low(eLATEST)
  2524. ldi r17,high(eLATEST)
  2525. mypush2 r16,r17
  2526. ret ;with 012 or adr of eLatest on stk
  2527. ;-------------------------------------
  2528. stkhereadr: ;same as above but for HERE
  2529. ldi r16,low(eHERE)
  2530. ldi r17,high(eHERE)
  2531. mypush2 r16,r17
  2532. ret ;with adr of ehere,current eeprom adr = $010
  2533. ;-------------------------------------------
  2534. updatevars2: ;better version of update vars. Come here after ";"
  2535. ;TODO check this version.DONE and eliminate other one.
  2536. rcall gethere ;the HERE val now on stack. It's a pointer to flash.
  2537. rcall stklatestadr ;usually 012
  2538. rcall percentstore
  2539. ;now with LATEST now containing old HERE. Next fix HERE
  2540. rcall stkmyhere ;current ptr to RAM dic's next free byte
  2541. rcall stkSOBuf2 ;start of buf2 adr
  2542. rcall minus ;gives distance into the buffer
  2543. rcall stkSOFPG ;will add distance to start of flashbuf
  2544. rcall plus ;got flash adr, but in bytes
  2545. rcall halve ;now adr in words
  2546. rcall stkhereadr ;usually %010 in eeprom
  2547. rcall percentstore ;eHERE now updated
  2548. ret ;with vals for HERE and LATEST in eeprom updated after ";"
  2549. ;--------------------
  2550. testOKCR:
  2551. rcall OK
  2552. rcall OK
  2553. rcall CR
  2554. rjmp testOKCR
  2555. ;--------------------
  2556.  
  2557. ;------------------------dump routines _______________
  2558. outnib: ;given $23 in r16, output the 3 as '3' = $33
  2559. push r18 ;going to use this
  2560. andi r16,$0f ; $3a --> $0a
  2561. cpi r16,$0a ;more than 10?
  2562. brge gothexo ;Nibble >= 10 jump down to gothex
  2563. ldi r18,$30 ; add $30 to 0..9
  2564. rjmp doneon
  2565. gothexo:
  2566. ldi r18,$37
  2567. doneon:
  2568. add r16,r18 ;now r16 nibble $03 is a '3'
  2569. rcall sendserialbyte ;print it
  2570. pop r18 ;used this as counter
  2571. ret ;note, it wrecks r16
  2572. ;--------------------------------------------
  2573. d16: ;dump contents of r16. Good for debugging.
  2574. push r16 ;keep contents for later
  2575. push r16 ;need this one after swap
  2576. swap r16 ;$34 wants 3 to come out first
  2577. rcall outnib ;print ascii eg '3'in above if r16 = $34
  2578. pop r16 ;get nice version back eg $34
  2579. rcall outnib ;print the '4'
  2580. pop r16 ;so r16 not wrecked.
  2581. ret ;with r16 printed in ascii
  2582. ;-----------------------------------
  2583. test_d16: ldi r16,$a5
  2584. rcall d16
  2585. ldi r16,$b6
  2586. rcall d16
  2587. rjmp test_d16
  2588. ;--------------------------------
  2589. d1617: ;dump r16 and r17 for debugging purposes
  2590. push r16
  2591. push r17 ;
  2592. push r16 ;just one min
  2593. mov r16, r17
  2594. rcall d16 ;that's r17 gone
  2595. pop r16
  2596. rcall d16 ;and then r16
  2597. pop r17
  2598. pop r16
  2599. ret ;with r17:r16 output in ascii
  2600. ;----------------------------------------
  2601. test_d1617:
  2602. ldi r16,$34
  2603. ldi r17,$1F
  2604. rcall d1617
  2605. rjmp test_d1617
  2606. ;-----------------------------------
  2607. dlowR: ;dump low registers. r0..r15 for debugging
  2608. ;.ifdef livetesting
  2609. push r16
  2610. push r18
  2611. pushx ;macro
  2612. clr xl
  2613. clr xh
  2614. ldi r18,16 ;r18 is a counter
  2615. prlow:
  2616. ld r16,x+ ;assume is x is 0 we'll get r0
  2617. rcall d16
  2618. rcall spacecode
  2619. dec r18
  2620. cpi r18,$07
  2621. breq doeseq7
  2622. tst r18
  2623. brne prlow
  2624. rjmp outprl
  2625. doeseq7:
  2626. ldi r16,'L'
  2627. rcall sendserialbyte
  2628. rcall spacecode
  2629. rjmp prlow
  2630.  
  2631. outprl:
  2632. popx ;macro
  2633. pop r18
  2634. pop r16
  2635. ;B.endif
  2636. ret ;with all the registers r0 ..r15 output in ascii to terminal screen
  2637. ;----------------------------------
  2638. test_dlowR:
  2639. rcall CR
  2640. ldi r16,$02
  2641. mov r0,r16
  2642. ldi r16,$52
  2643. mov r5,r16
  2644. ldi r16,$f2
  2645. mov r15,r16
  2646. rcall dlowR
  2647. rcall CR
  2648. rjmp test_dlowR
  2649. ;-----------------------------
  2650. spacecode: ;output a space
  2651. push r16
  2652. ldi r16,$20
  2653. rcall sendserialbyte
  2654. pop r16
  2655. ret
  2656. ;-------------------------------
  2657. dhighR: ;dump high registers. r18..r25 for debugging
  2658. push r16
  2659. push r17
  2660. pushx ;macro
  2661. ldi xl,18
  2662. ; clr xl
  2663. clr xh
  2664. ldi r17,8 ;r18 is a counter
  2665. prhi:
  2666. ld r16,x+ ;assume is x is 18 we'll get r18
  2667. rcall d16
  2668. rcall spacecode
  2669. dec r17
  2670. cpi r17,5
  2671. breq doeseq21
  2672. tst r17
  2673. brne prhi
  2674. rjmp outprh
  2675. doeseq21:
  2676. ldi r16,'H'
  2677. rcall sendserialbyte
  2678. rcall spacecode
  2679. rjmp prhi
  2680.  
  2681. outprh:
  2682. popx ;macro
  2683. pop r17
  2684. pop r16
  2685. ret ;with all the registers r0 ..r15 output in ascii to terminal screen
  2686. ;----------------------------------
  2687. test_dhighR:
  2688. rcall CR
  2689. ldi r18,$88
  2690. ldi r19,$19
  2691. ldi r20,$88 ;
  2692. ldi r21,$88
  2693. ldi r22,$22
  2694. ldi r23,$23
  2695. ldi r24,$24
  2696. ldi r25,$25
  2697. rcall dhighR
  2698. rcall CR
  2699. rjmp test_dhighR
  2700. ;------------------------------------
  2701. dxyz: ;dump the three pointer regs x,y,z
  2702.  
  2703. push r16
  2704. push r17
  2705. movw r16,xl ;r17:16 gets xh:xl
  2706. rcall d1617
  2707. rcall spacecode
  2708. movw r16,yl
  2709. rcall d1617
  2710. rcall spacecode
  2711. movw r16,zl
  2712. rcall d1617
  2713. rcall spacecode
  2714. pop r17
  2715. pop r16
  2716. ret ;with x,y,z output in ascii as a tripple
  2717. ;--------------------------------------
  2718. test_dxyz:
  2719. rcall CR
  2720. ldi xl,$12
  2721. ldi xh,$34
  2722. ldi yl,$56
  2723. ldi yh,$78
  2724. ldi zl,$9A
  2725. ldi zh,$bc
  2726. rcall CR
  2727. rcall dxyz
  2728. rcall CR
  2729. rjmp test_dxyz
  2730. ;--------------------------------
  2731. ;mystack needs a DEPTH word.
  2732. depthcode: ; (--n16)
  2733. ;leave on mystack the number of items on the stack by bytes.
  2734. movw r16,yl ;now r16,17 has y pointer
  2735. ldi r18, low(myStackStart) ;
  2736. ldi r19, high(myStackStart) ;r18,19 probably contain $1A0, the start of mystack
  2737. mypush2 r16,r17
  2738. mypush2 r18,r19 ;setup for eg $1a6 - $1a0
  2739. rcall minus ;difference=depth = eg 0006 as above.
  2740. ret ; with depth on stack
  2741. ;-----------------------------------------
  2742. test_depthcode:
  2743. ldi r16,$01
  2744. ldi r17,$23
  2745. mypush2 r16,r17
  2746. mypush2 r16,r17
  2747. mypush2 r16,r17
  2748. rcall depthcode
  2749. uptd: mypopa ;depth now in r16,17
  2750. up2: rcall d1617
  2751. rjmp up2
  2752. ;------------------------------------
  2753. dotScode: ;classic .S, print stack non-destructively
  2754. push r16
  2755. push r18
  2756. pushx ;macro
  2757. rcall depthcode ;now depth = len of stk on the mystack top
  2758. ; rcall drop ;stk =eg 0006 . want just len = 06
  2759. mypop2 r17,r18 ;so r18 now has length in bytes we're printing
  2760. ldi xl, low(myStackStart)
  2761. ldi xh, high(myStackStart)
  2762.  
  2763. ; movw xl,yl ;use x as temp ptr. Keep y pointing to mystack top
  2764. upds:
  2765. ld r16,x+ ;get tos, Pre-decrement.
  2766. rcall d16 ;print it
  2767. rcall spacecode ;
  2768. dec r18
  2769. brne upds
  2770. ldi r16, ']'
  2771. rcall sendserialbyte
  2772. rcall spacecode
  2773. popx ;macro
  2774. pop r18
  2775. pop r16
  2776. ret ;with the stack items printed to term screen + ]
  2777. ;-----------------------------
  2778. test_dotScode:
  2779. ldi r16,$A1
  2780. ldi r17,$B2
  2781. mypush2 r16,r17
  2782. mypush2 r16,r17
  2783. mypush2 r16,r17
  2784. rcall dotScode
  2785. rcall drop
  2786. rcall drop
  2787. rcall drop
  2788. uptds:
  2789. rjmp uptds
  2790. ;---------------------------------
  2791. wordscode: ;classic words. List all the words in the dic
  2792. push r16
  2793. push r17
  2794. push r22
  2795. push r23
  2796. push r24
  2797. pushz
  2798. rcall doLatest ;get first link into v
  2799. upwo:
  2800. rcall jmpNextWord ;pnt to link part of next word
  2801. lpm r23,z+
  2802. lpm r22,z+ ;store link into v=r23,24
  2803. lpm r16,z+ ;get len
  2804. andi r16,$0f ;don't want eg $85 to be len when it means immediate len 5.
  2805. clr r17 ;need eg 0006 on stk not 06 later
  2806. mypush2 r16,r17 ;len byte now on mystk
  2807. ;at this stage z points to the start of word name
  2808. mypush2 zl,zh ;flash start adr of string now on mystack
  2809. rcall swapp ; but wrong way round. Want len = TOS
  2810. rcall Sdot ;print the string on the term
  2811. rcall spacecode ;but add space after each word
  2812. tst vl
  2813. brne upwo ;if vl:vh = r23,24 = 0000 finish
  2814. tst vh
  2815. brne upwo
  2816. popz ;
  2817. pop r24
  2818. pop r23
  2819. pop r22
  2820. pop r17 ;TODO macro with multiple pops & pushes
  2821. pop r16
  2822. ret ;with all the words in dic printed
  2823. ;-----------------------------------------------
  2824. clrbuf1:
  2825. ldi ZL,low(buf1) ;buf1 is my buffer
  2826. ldi ZH, high(buf1) ;Z now points to buf1
  2827. ldi counterReg,64 ;64 bytes in buffer
  2828. ldi r16,$30
  2829. storecl:
  2830. st z+,r16
  2831. inc r16
  2832. dec counterReg
  2833. brne storecl
  2834.  
  2835. ret
  2836. ;-----------------------
  2837. updatevarptrcode: ;update varptr currently at eeprom's 0016. Add 2 to its contents.
  2838. rcall getvarptr ;eg 0160 in ram
  2839. rcall two
  2840. rcall plus ;now is eg 0162
  2841. rcall varptradr ;usually 0016 in eeprom
  2842. rcall percentstore ;should be called estore ie e!
  2843. ret ;with ptr val = old ptrval + 2
  2844. ;-------------------------
  2845. variablecode: ;big word called each time variable is declared
  2846. rcall coloncode ;does all the create work in buf
  2847.  
  2848. rcall getvarptr ;put eg 0162 on stack. Address of next RAM var place.
  2849. rcall compstackme_2 ;put stackme_2 as first code when called
  2850.  
  2851. rcall comma
  2852. rcall updatevarptrcode ;add 2 to varptr
  2853. rcall semi ;finish off and burn to flash
  2854.  
  2855. ret ;with variable created.
  2856. ;----------------------------------
  2857. considercode: ;having probs with findword going awol. Need another debug routine.
  2858. .ifdef livetesting
  2859. rcall CR
  2860. takemeout '[' ;just little mark for Id
  2861. rcall dhighR ;
  2862. ;Used when we've found a word.Starting at w(r24,25) length in r20. x points to space just past word.
  2863. ; u = r22,23
  2864. takemeout ']' ;just little mark for Id
  2865. .endif
  2866. ret
  2867. ;-------------------------
  2868. ifcode: ;classic IF
  2869. rcall tic
  2870. rcall zerobranch
  2871. rcall comma
  2872. rcall stackmyhere
  2873. rcall zero
  2874. rcall comma
  2875. ret ;with (rcall zerobranch, 0000) in dictionary in RAM
  2876. ;-------------------new parts to below----
  2877. housekeeping: ;cold start routines
  2878. ldi r16, 0xf9 ;PORTB setup
  2879. out DDRB,r16 ;
  2880. nop
  2881. ldi r16, $ff
  2882. out PORTB,r16
  2883. .IFDEF testing ;testing = simulating on avrstudio4
  2884. .ifndef firsttime ;just want to burn vars on first cold start
  2885. nop
  2886. rcall burneepromvars ;maybe a simple flag is better ?
  2887. .equ firsttime = 1
  2888. .endif
  2889.  
  2890. .ENDIF
  2891. clr STATE
  2892. rcall OK ;two OK}s mean cold start.
  2893. ldi xl,$a0
  2894. ldi xh,$01 ;point to ram VARS
  2895.  
  2896. clr r16
  2897. st x+,r16
  2898. st x+,r16 ;that's FBFlag mae 0.(ie use serialfill, not block fill)
  2899. st x+,r16 ;lower byte of FBPointer ie the 00 of $1c00.
  2900. ldi r16,$1c
  2901. st x+,r16 ;so now have $1c00 in FBPntr. Pnts to start of BLOCK.
  2902.  
  2903. ret ;with the housekeeping done
  2904. ;-------------------------------
  2905. blockfillcode: ; pull in one def from BLOCK at $1c00 (bytes)
  2906. rcall FBPtr ;now have $01a2, holds ptr to last 1K of flash, on stk
  2907. rcall fetch ;get ptr on stack. Start at $1c00 (bytes) in flash
  2908. mypop2 zh,zl ;point to first (or next) def with z
  2909. ldi xl,low(buf1)
  2910. ldi xh,high(buf1) ;x points to buffer, just like serial fill
  2911. upbfc:
  2912. lpm r16,z+ ;get char in BLOCK def
  2913. tst r16 ;it might be a zero, pad bytes have been added sometimes
  2914. brne downbfc ;get out if not a zero
  2915. gota0:
  2916. ldi r16,$20 ;if it's a zero, change it to a space
  2917. downbfc: ;TODO should really count chars and stop at,say,120
  2918. st x+,r16 ;flash byte now in AM buf1
  2919. cpi r16,$0d ;all defs end in CR. Got to end yet?
  2920. brne upbfc ;keep going if it's just a char != $0d.
  2921. mypush2 zl,zh ;finished so save pointer for next def
  2922. rcall FBPtr ;put $01a2 on stack, adr of ptr to last k defs
  2923. rcall store ;z-->FBPtr
  2924. clr STOP ;stop flag still going from last def or word
  2925. ret ;with one more def placed into buf from block. This gets interpreted in normal way.
  2926. ;--------------------------------------------
  2927. test_rs: ;test the rs. word that prints ram strings
  2928. rcall fillbuf
  2929. ldi r16,$60
  2930. clr r17
  2931. mypush2 r16,r17 ;pnt to buf1
  2932. ldi r16,10 ;len
  2933. mypush2 r16,r17
  2934. rcall rs
  2935. rcall qmark ;test qmark too
  2936. trs: rjmp trs
  2937. ;---------------------------
  2938. whatq: ;outputs word? when word not in dic and not a number
  2939. mypush2 r24,r25 ;adr of strange word during numberh
  2940. mypush r20 ;the len
  2941. clr r16
  2942. mypush r16 ;topup. Now have req (adr len --) on stack. To to call rs.
  2943. rcall rs
  2944. rcall qmark
  2945. ret
  2946. ;---------------------------------------
  2947. findfirstvarcode: ;( -- adr16) ;go down the dictionary finding first var,(bit6 of len set)
  2948. pushz
  2949. rcall dolatest
  2950. upffv:
  2951. rcall jmpNextWord
  2952. lpm r23,z+
  2953. lpm r22,z+ ;link for next word goes into r22,23 = v
  2954. ;lpm r16,z+
  2955. ;lpm r16,z+
  2956. lpm r16,z+ ;now point to len. Len in r16
  2957. sbrs r16,6
  2958. rjmp upffv ;if bit 6 is clear (not a var) go to up
  2959. andi r16, $0f ;mask off top nib to give real len
  2960. clc ;going to add
  2961. add zl,r16 ;step over name of var
  2962. inc zl
  2963. inc zl
  2964. brcc downffv ;maybe zl has over flowed
  2965. inc zh ;only if overflow
  2966. downffv:
  2967. lpm r16,z+ ;z points to ram adr after stackme2
  2968. lpm r17,z ;now have RAM adr of var eg $01a4
  2969. mypush2 r16,r17
  2970. popz
  2971. ret ;with ram adr of top var on mystack
  2972.  
  2973. ;------------------------------------------
  2974. strout: ; comes in dic like stackme-2 with structure assumptions. Should be followed by
  2975. ; len then a string of len chars. like this /strout/len/c c c c / other rcalls. Strout puts adr of
  2976. ; str on mstack and len then calls S. to print the string . It also makes reurn adr pnt to other.
  2977. pop zh ;hope we don't have to save z
  2978. pop zl ;check on order. Z now pnts to len
  2979. clc ;need to double z to get byte adr
  2980. rol zl
  2981. rol zh
  2982. lpm r16,z+
  2983. lpm r17,z+ ;r16,17 now have len. z points to str
  2984. mypush2 r16,r17 ;len on mystack
  2985. rcall dup ; ( l l --)
  2986. mypush2 zl,zh ; ( l l adr --) adr is of str /c c c ../ above
  2987. rcall dup ; ( l l adr adr --)
  2988. rcall rot ; ( l adr adr l --)
  2989. rcall plus ; ( l adr (adr+l) --) adr + l = adr of "other rcalls" above
  2990. rcall halve ;adr going onto ret stk needs to be word, not byte adr
  2991. brcc downstro ; clear carry means halve exact, not 00 padding bytes
  2992. rcall one
  2993. rcall plus ;add 1 to skip over padding byte of carry set by halve
  2994. downstro:
  2995. mypopa ; adr of other in r16,17. stk = ( l adr --)
  2996. push r16 ;check order
  2997. push r17 ; return adr now points to "other"
  2998. rcall swapp ; now ( adr l--) ready for next line
  2999. rcall Sdot ; print the string
  3000. ret ; after string print to other, just past the string
Advertisement
Add Comment
Please, Sign In to add comment