prjbrook

forth85_35. Begin..until seems OK

Sep 14th, 2014
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 96.13 KB | None | 0 0
  1. ;this is forth85_35 Tidies up forth85_34.
  2. ;begin..while..repeat. Seems to work OK.
  3. ;Now going to do begin..until
  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 zeroequal_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.  
  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. while_1: ; (--adr16) classic in begin..while..repeat.
  1075. header squote_1,$85,"while"
  1076. while:
  1077. rcall comp0branch ;if true skip over next jump
  1078. rcall stkmyhere ;not pos of 00 for leter fill in by repeat
  1079. ;get order above and below this right
  1080. rcall zero ;temp filler for branch if false
  1081. rcall comma ;compile this 00. repeat will fill it in later
  1082. ret ;with adr of unfilled branch on my stack and 0branch compiled.
  1083. ;--------------------------------------------
  1084.  
  1085. repeat_1: ;( adrb adrw --) classic. adrb,adrw are stacked by begin,while resp. Myheres
  1086. header while_1,$86,"repeat"
  1087. repeat:
  1088. ; rcall endif ; this will fill in the 00 done by while with jmp to past repeat
  1089. ;got this from else_
  1090.  
  1091. rcall dup ;need there_adr twice,calc+ store
  1092. rcall stkmyhere ;so we can use calc rjmp --> there - here -1
  1093. rcall two
  1094. rcall plus ;because we have to jump over the rjmp to begin we create below
  1095. rcall swapp ;because the jump is put into "there"
  1096. rcall calcjumpcode ;(jmpcode now on stack)
  1097. rcall swapp ;wrong way round for store !
  1098. rcall store ;put jmpcode where there are just 0 placeholders near if
  1099.  
  1100.  
  1101. rcall again ; this will give a rjmp, uncondit back to begin.
  1102. ret ;with all the begin..while..repeat all filled in.
  1103. ;-----------------------------------
  1104. LATEST:
  1105. until_1: ;( adr16 --) enter with adr of begin on stack. Loop back to there if true.
  1106. header repeat_1,$85,"until"
  1107. until:
  1108. rcall comp0branch
  1109. rcall again ;again code gets us back to start, after begin
  1110. ret ;with two jmps (obranch and again jump ) all in right places
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123. ;-----------------------------------------------
  1124. HERE:
  1125. .db "444444444444444444444444444444"
  1126. ;---------------stackme_2 used to live here---------------------------------
  1127.  
  1128. ;====================================================================================================
  1129.  
  1130. .ORG 0
  1131. rjmp cold
  1132. typein: .db "readblock ",$0d
  1133. ;.db " : beee begin 0002 while 0003 repeat 0004 ; ",$0d
  1134. ;.db ": myworxxd 0001 if 0005 dup endif ; ", $0d
  1135. ;-----------------------------------------------------
  1136. cold: ;come here on reset or for big cleanup
  1137. ldi r16, low(RAMEND)
  1138. out SPL, r16
  1139. ldi r16,high(RAMEND)
  1140. out SPH, r16
  1141.  
  1142. ldi YL,low(myStackStart)
  1143. ldi YH,high(myStackStart)
  1144. rcall housekeeping
  1145. ;rcall test_buf2ToFlashBuffer
  1146. ;rjmp cold
  1147. ;rcall blockfillcode
  1148. ;rcall interpretLine
  1149. ;rcall blockfillcode
  1150. ;rcall blockfillcode
  1151. ;rcall blockfinish
  1152. ;rcall test_rs
  1153. ;here3: rjmp here3
  1154.  
  1155. quit:
  1156. ldi r16, low(RAMEND)
  1157. out SPL, r16
  1158. ldi r16,high(RAMEND)
  1159. out SPH, r16
  1160.  
  1161. ; ldi YL,low(myStackStart)
  1162. ; ldi YH,high(myStackStart)
  1163.  
  1164.  
  1165. ;---------new------------
  1166. rcall FBFlag ;put $1a0 (blockfill flag) on stack
  1167. rcall fetch ;either 0000 (do serialfill) or 0001 (blockfill)
  1168. mypopa ;r16,17 get flag
  1169. tst r16 ;is flag (lower byte anyway) a zero?
  1170. .ifndef testing
  1171. breq doSF ;flag = 0 do (normal) serialfill
  1172. .else
  1173. breq getli
  1174. .endif
  1175. rcall blockfillcode ;because (if here) flag is a 1 = do
  1176.  
  1177. ;rjmp interp ;interpret the block fill def
  1178. rcall interpretLine ;but only if filling from BLOCK
  1179. rjmp quit ;quit
  1180.  
  1181. .ifdef testing
  1182. getli:
  1183. rcall getline0
  1184. rcall interpretLine
  1185. quithere:
  1186. rjmp quit;here
  1187. .endif
  1188.  
  1189.  
  1190. .ifndef testing
  1191. doSF:
  1192. rcall serialFill
  1193. interp: ;have buf1 filled with words, def etc now find them on dic etc.
  1194. clr STOP
  1195. clr r1
  1196. clr SECONDLETTER
  1197. clr BOTTOM
  1198.  
  1199. rcall interpretLine
  1200. rjmp quit
  1201. .endif
  1202.  
  1203. ;-------------------------------------------------------------
  1204. ;mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
  1205. ;--------------------------------------------------------------
  1206. getline0: ;force a line into buf1 via flash string. Simulates GETLINE
  1207. ldi zl, low(typein<<1)
  1208. ldi zh, high(typein<<1)
  1209. ldi xl, low(buf1)
  1210. ldi xh, high(buf1)
  1211. type0:
  1212. lpm r16,Z+
  1213. st x+,r16
  1214. cpi r16,0x0d ;have we got to the end of the line?
  1215. brne type0
  1216. ret
  1217. ;--------------------------------------------
  1218. serialFill: ;main input routine from terminal. Output OK} then
  1219. ; wait until buf1 has string of words ( <64 chars?) ending in $0d
  1220. rcall clrbuf1
  1221. rcall CR
  1222. ;rcall report
  1223. rcall OK
  1224. rcall rxStrEndCR
  1225. ret ; buf1 now filled with words from terminal
  1226. ;--------------------------------------------------------------
  1227. interpretLine: ;given line of words in buf one, search for words one by one. Don't do code
  1228. ; or compile at this stage, just find and report that and go into next one.
  1229. rcall pasteEOL
  1230. ldi xl, low(buf1)
  1231. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  1232. clr FOUNDCOUNTER ;counts finds in line parsing.
  1233. nextWord:
  1234. tst STOP
  1235. brne stopLine
  1236. nop
  1237. rcall word
  1238. rcall findWord
  1239. rcall dealWithWord ;go and run code STATE=0, or compile (STATE =1).{ c0de, comp1le}
  1240. rjmp nextWord
  1241. stopLine:
  1242. ret
  1243. ;----------------------------------------------------------
  1244. ;WORD gets x to point to start of word (copy in w=r24,25) with the length in len = r20
  1245. ;assume word points to somewhere in buf1. Should advance thru spaces=0x20 to first real char
  1246. word: ;maybe give it a header later
  1247. ld SECONDLETTER, x ;for debugging. TODO. Should be firstletter?
  1248. ld r16,x+ ;get char
  1249. cpi r16,0x20 ;is it a space?
  1250. breq word ;if so get next char
  1251. ;if here we're point to word start. so save this adr in w
  1252. mov r24,xl
  1253. mov r25,xh ;wordstart now saved in w
  1254. clr r20 ;length initially 0
  1255. nextchar:
  1256. inc r20 ;r20 = word length
  1257. ld r16,x+ ;get next char
  1258. cpi r16,0x20
  1259. brne nextchar
  1260. dec r24 ;adjust start of word
  1261. ;if here we've found a word.Starting at w length in r20.x points to space just past word
  1262. ret
  1263. ;----------------------------------------
  1264. 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.
  1265. ; and the word in buf1 is pointed to by w=r24,25. len = r20. Z on entry points to the link. Needs +2 to
  1266. lpm r23,z+
  1267. lpm r22,z+ ;store next link in v=r22,23. z now points to len byte
  1268.  
  1269. startc:
  1270. ;TODO save copy of flash word in r21 and also do masking of immediates
  1271. push r20 ;save length
  1272. lpm r16,Z+ ;length of dictionary word, first entry now in r16
  1273. mov r21,r16 ;copy length-in-flash to r21. May have immediate bit (bit 7)
  1274. andi r16,$0f ;mask off top nibble before comparing
  1275. cp r16,r20 ;same lengths?
  1276. brne outcom ;not = so bail out
  1277. ;if here the words are the same length, what about the rest of the chars.First get x to point to word.
  1278. mov xl,r24
  1279. mov xh,r25 ;x now point to start of buf1 word
  1280. upcom:
  1281. lpm r16,z+
  1282. ld r17,x+ ;get one corresponding char from each word
  1283. cp r16,r17 ;same word?
  1284. brne outcom ;bail out if chars are different
  1285. dec r20 ;count chars
  1286. brne upcom ;still matching and not finished so keep going
  1287. ;if here r20 is 0 so match must have been perfect so FOUND = 1
  1288. clr FOUND
  1289. inc FOUND
  1290. outcom:
  1291. pop r20 ;get old lngth of buf1 word back
  1292. ret
  1293. ;-------------------------------------------
  1294. jmpNextWord: ;go to next word in the dictionary. Assume v=r22,23 contains next link word(not byte)
  1295. ; and w = r24,25 contains RAM word start with len in r20
  1296. ;exit with z pointing to next word ready for next COMPARE.
  1297. clc
  1298. rol r22
  1299. rol r23 ;above 3 instructions change word address into byte address by doubling
  1300. movw r30,r22 ;z now points to next word
  1301. ret
  1302. ;-----------------------------------------
  1303.  
  1304. doLatest: ;set up so first jump in dictionary is to top=LATEST and other flags set up.
  1305. ; ldi vl, low(LATEST)
  1306. ; ldi vh, high(LATEST)
  1307. nop
  1308. rcall getlatest ;from eeprom. Now on stack
  1309. mypop2 vh,vl ;
  1310. ; rcall halve
  1311. clr FOUND
  1312. clr BOTTOM ;not yet found the match, not yet at the bottom. Either will stop search.
  1313. clr STOP ;keep parsing words til this goes to a 1
  1314. ret
  1315. ;---------------------------------------------
  1316. ;-----------------------------------------------------------------
  1317. findWord:
  1318. rcall doLatest
  1319. nop
  1320. ;rcall dumpbuf1
  1321. ;FIND reg values here.
  1322. rcall considercode
  1323. upjmpf:
  1324. rcall jmpNextWord
  1325. takemeout 'f'
  1326.  
  1327. rcall compare
  1328. tst FOUND
  1329. brne stopsearchf ;if last compare got a match (FOUND=1) then stop searching
  1330. tst vl
  1331. brne upjmpf ;if v=0000 then we've hit the bottom of the dictionary
  1332. tst vh
  1333. brne upjmpf ;not found and not at bottom so keep going
  1334. ;if here FOUND =0, ie no match yet and we've hit the bottom of the dictionary
  1335. clr BOTTOM
  1336. inc BOTTOM ;exit with FOUND=0 and BOTTOM =1
  1337. stopsearchf:
  1338. nop
  1339. ret
  1340. ;----------------------------
  1341. test_interpretLine:
  1342. rcall interpretLine
  1343. til: rjmp til ;** with r24 pointing to 'S' and FOUND = r15 =1
  1344. ;------------------------------
  1345. dealWithWord: ;come here when it's time to compile or run code
  1346. ;Good debugging spot. Enter here with Z pointing to CFA of word found. Y points to myStack. X points to just
  1347. ; 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
  1348. ; to the next word in dic. Either just below the found word or 0000 if we get to the bottome with no match
  1349. ;
  1350. nop
  1351. tst FOUND
  1352. breq notfound
  1353. inc FOUNDCOUNTER
  1354.  
  1355. ;want to hop over filler bytes,0's added to keep codes on even byte boundaries
  1356. ; so if r30 is odd at this stage inc it. odd is lsbit = 1.
  1357. sbrs r30,0 ;skip next instruction if final bit lsb = 1
  1358. rjmp downd
  1359. ;if here lsb = 1 so we're on a padding byte and have to add 1 to get to a 2 byte boundary
  1360. inc r30
  1361. brcc downd
  1362. inc r31 ;add one to z before converting to bytes
  1363. ;have to ask at this point, is the word immediate? If so, bit 7 of r21 will be set.
  1364. downd:
  1365. sbrs r21,7
  1366. rjmp downdw ;not immediate so just go on with STATE test
  1367. rjmp executeme ;yes, immediate so execute every time.
  1368.  
  1369.  
  1370. downdw: tst STATE
  1371. breq executeme
  1372. rcall compilecode
  1373. rjmp outdww
  1374. executeme:
  1375. clc
  1376. ror zh
  1377. ror zl ;put z back into word values
  1378.  
  1379.  
  1380. rcall executeCode
  1381.  
  1382.  
  1383.  
  1384. .MESSAGE "Word found"
  1385. rjmp outdww
  1386. notfound:
  1387. nop
  1388. ; .MESSAGE "Word not found"
  1389. ; clr STOP
  1390. ; inc STOP ;stop parsing line
  1391. takemeout 'n'
  1392. rcall numberh ; word not in dict so must be a number? Form = HHHH
  1393. ;now have to add 3 to x so it points past this word ready not next one
  1394. clc
  1395. inc r26
  1396. inc r26
  1397. inc r26
  1398. brcc outdww
  1399. inc r27 ;but only if overflow
  1400. nop
  1401. outdww:
  1402. ret ;with STOP =1 in not a number
  1403. ;------------------------------------------------------------------------
  1404. pasteEOL: ;when a line of text is TYPEd into buf1 it should end with CR=$0d. This gets replaced with ]}, a
  1405. ; special end of line word. When the word is invoked it casues a QUIT back to the waiting for input stage.
  1406. ; Start at buf1 start and inspect each char for a $0D. When found replace with a "$20 S $20 "
  1407.  
  1408. ldi xl, low(buf1)
  1409. ldi xh, high(buf1) ;pnt to start of buffer
  1410. clr r17
  1411. nxtChar:
  1412. inc r17 ;r17 is counter. Bail out when r17 > BUF1LENGTH
  1413. cpi r17, BUF1LENGTH -3
  1414. breq outProb
  1415. ld r16, x+
  1416. cpi r16, $0d
  1417. brne nxtChar
  1418. ;if here we've found a $0d in buf1 before the end, so replace with an EOL token. x points to just after it.
  1419. ldi r16,$20
  1420. st -x, r16 ;back up. Then go forward.
  1421. TAKEMEOUT 'p'
  1422. ; ldi r16, ']'
  1423. ldi r16,$20 ;This took about 4 day's work to insert this line. Why is it needed?
  1424. st x+, r16
  1425. ldi r16,'S'
  1426. st x+, r16
  1427. ; ldi r16, '}'
  1428. ; st x+, r16
  1429. ldi r16, $20
  1430. st x, r16
  1431. rjmp outpel
  1432.  
  1433.  
  1434. outProb:
  1435. takemeout 'O'
  1436. nop
  1437. .MESSAGE "Couldn't find $0d"
  1438. outpel:
  1439. ret
  1440.  
  1441. ;-------------------------------------
  1442. executeCode: ;with Z pointing to cfa. Not sure whether to jmp or call
  1443.  
  1444. ijmp
  1445. ret
  1446. ;---------------------------------------
  1447. test_fetch: ;do run thru of @
  1448. rcall getline0 ;change later to real getline via terminal
  1449. rcall pasteEOL
  1450. ldi xl, low(buf1)
  1451. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  1452.  
  1453. ldi r16,$62
  1454. mypush r16
  1455. ldi r16,$0
  1456. mypush r16 ;should now have adr $0062 on mystack
  1457. rcall fetch
  1458. tf1:
  1459. rjmp tf1
  1460. ;---------------------------------
  1461. test_cfetch: ;do run thru of @
  1462. rcall getline0 ;change later to real getline via terminal
  1463. rcall pasteEOL
  1464. ldi xl, low(buf1)
  1465. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  1466.  
  1467. ldi r16,$62
  1468. mypush r16
  1469. ldi r16,$0
  1470. mypush r16 ;should now have adr $62 on mystack
  1471. rcall cfetch
  1472. tcf1:
  1473. rjmp tcf1
  1474. ;----------------------------
  1475. test_store:
  1476. rcall getline0 ;change later to real getline via terminal
  1477. rcall pasteEOL
  1478. ldi xl, low(buf1)
  1479. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  1480. ldi r16,$62
  1481. ldi r17,$0
  1482. mypush2 r16,r17 ;should now have adr $62 on mystack
  1483. ldi r16, $AB
  1484. ldi r17, $CD
  1485. mypush2 r16,r17 ;now have $ABCD on mystack
  1486. rcall store
  1487. ts1:
  1488. rjmp ts1
  1489. ;------------------------
  1490. test_cstore:
  1491. rcall getline0 ;change later to real getline via terminal
  1492. rcall pasteEOL
  1493. ldi xl, low(buf1)
  1494. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  1495. ldi r16,$62
  1496. ldi r17,$0
  1497. mypush2 r16,r17 ;should now have adr $62 on mystack
  1498. ldi r16, $AB
  1499. ; ldi r17, $CD
  1500. mypush r16 ;now have $ABCD on mystack
  1501. rcall cstore
  1502.  
  1503. ts11:
  1504. rjmp ts11
  1505. ;Now put arith routines here. Are from AVR200. Just using 16*16 for * but get 32bit result.
  1506.  
  1507.  
  1508. ;***************************************************************************
  1509. ;*
  1510. ;* "mpy16s" - 16x16 Bit Signed Multiplication
  1511. ;*
  1512. ;* This subroutine multiplies signed the two 16-bit register variables
  1513. ;* mp16sH:mp16sL and mc16sH:mc16sL.
  1514. ;* The result is placed in m16s3:m16s2:m16s1:m16s0.
  1515. ;* The routine is an implementation of Booth's algorithm. If all 32 bits
  1516. ;* in the result are needed, avoid calling the routine with
  1517. ;* -32768 ($8000) as multiplicand
  1518. ;*
  1519. ;* Number of words :16 + return
  1520. ;* Number of cycles :210/226 (Min/Max) + return
  1521. ;* Low registers used :None
  1522. ;* High registers used :7 (mp16sL,mp16sH,mc16sL/m16s0,mc16sH/m16s1,
  1523. ;* m16s2,m16s3,mcnt16s)
  1524. ;*
  1525. ;***************************************************************************
  1526.  
  1527. ;***** Subroutine Register Variables
  1528.  
  1529. .def mc16sL =r16 ;multiplicand low byte
  1530. .def mc16sH =r17 ;multiplicand high byte
  1531. .def mp16sL =r18 ;multiplier low byte
  1532. .def mp16sH =r19 ;multiplier high byte
  1533. .def m16s0 =r18 ;result byte 0 (LSB)
  1534. .def m16s1 =r19 ;result byte 1
  1535. .def m16s2 =r20 ;result byte 2
  1536. .def m16s3 =r21 ;result byte 3 (MSB)
  1537. .def mcnt16s =r22 ;loop counter
  1538.  
  1539. ;***** Code
  1540. mpy16s: clr m16s3 ;clear result byte 3
  1541. sub m16s2,m16s2 ;clear result byte 2 and carry
  1542. ldi mcnt16s,16 ;init loop counter
  1543. m16s_1: brcc m16s_2 ;if carry (previous bit) set
  1544. add m16s2,mc16sL ; add multiplicand Low to result byte 2
  1545. adc m16s3,mc16sH ; add multiplicand High to result byte 3
  1546. m16s_2: sbrc mp16sL,0 ;if current bit set
  1547. sub m16s2,mc16sL ; sub multiplicand Low from result byte 2
  1548. sbrc mp16sL,0 ;if current bit set
  1549. sbc m16s3,mc16sH ; sub multiplicand High from result byte 3
  1550. asr m16s3 ;shift right result and multiplier
  1551. ror m16s2
  1552. ror m16s1
  1553. ror m16s0
  1554. dec mcnt16s ;decrement counter
  1555. brne m16s_1 ;if not done, loop more
  1556. ret
  1557. ;----------------------------------------------------------
  1558. ;***** Multiply Two Signed 16-Bit Numbers (-12345*(-4321))
  1559. test_mpy16s:
  1560. ldi mc16sL,low(-12345)
  1561. ldi mc16sH,high(-12345)
  1562. ldi mp16sL,low(-4321)
  1563. ldi mp16sH,high(-4321)
  1564. rcall mpy16s ;result: m16s3:m16s2:m16s1:m16s0
  1565. ;=$032df219 (53,342,745)
  1566. tmpy: rjmp tmpy
  1567.  
  1568. test_mpy16s0:
  1569. ldi mc16sL,low(123)
  1570. ldi mc16sH,high(123)
  1571. ldi mp16sL,low(147)
  1572. ldi mp16sH,high(147)
  1573. rcall mpy16s ;result: m16s3:m16s2:m16s1:m16s0
  1574. tmpy0: rjmp tmpy0
  1575. ;-----------------------
  1576. test_star:
  1577. ldi r16,-$7b
  1578. mypush r16
  1579. ldi r16,$00
  1580. mypush r16 ;that's decimal 123 on stack
  1581. ldi r16,$93
  1582. mypush r16
  1583. ldi r16,$00
  1584. mypush r16 ; and thats dec'147
  1585. rcall star
  1586. tsr: rjmp tsr
  1587.  
  1588. ;--------------------------
  1589. ;***************************************************************************
  1590. ;*
  1591. ;* "div16s" - 16/16 Bit Signed Division
  1592. ;*
  1593. ;* This subroutine divides signed the two 16 bit numbers
  1594. ;* "dd16sH:dd16sL" (dividend) and "dv16sH:dv16sL" (divisor).
  1595. ;* The result is placed in "dres16sH:dres16sL" and the remainder in
  1596. ;* "drem16sH:drem16sL".
  1597. ;*
  1598. ;* Number of words :39
  1599. ;* Number of cycles :247/263 (Min/Max)
  1600. ;* Low registers used :3 (d16s,drem16sL,drem16sH)
  1601. ;* High registers used :7 (dres16sL/dd16sL,dres16sH/dd16sH,dv16sL,dv16sH,
  1602. ;* dcnt16sH)
  1603. ;*
  1604. ;***************************************************************************
  1605.  
  1606. ;***** Subroutine Register Variables
  1607.  
  1608. .def d16s =r13 ;sign register
  1609. .def drem16sL=r14 ;remainder low byte
  1610. .def drem16sH=r15 ;remainder high byte
  1611. .def dres16sL=r16 ;result low byte
  1612. .def dres16sH=r17 ;result high byte
  1613. .def dd16sL =r16 ;dividend low byte
  1614. .def dd16sH =r17 ;dividend high byte
  1615. .def dv16sL =r18 ;divisor low byte
  1616. .def dv16sH =r19 ;divisor high byte
  1617. .def dcnt16s =r20 ;loop counter
  1618.  
  1619. ;***** Code
  1620.  
  1621. div16s: ;push r13 ;PB !!
  1622. ;push r14 ;PB !!
  1623. mov d16s,dd16sH ;move dividend High to sign register
  1624. eor d16s,dv16sH ;xor divisor High with sign register
  1625. sbrs dd16sH,7 ;if MSB in dividend set
  1626. rjmp d16s_1
  1627. com dd16sH ; change sign of dividend
  1628. com dd16sL
  1629. subi dd16sL,low(-1)
  1630. sbci dd16sL,high(-1)
  1631. d16s_1: sbrs dv16sH,7 ;if MSB in divisor set
  1632. rjmp d16s_2
  1633. com dv16sH ; change sign of divisor
  1634. com dv16sL
  1635. subi dv16sL,low(-1)
  1636. sbci dv16sL,high(-1)
  1637. d16s_2: clr drem16sL ;clear remainder Low byte
  1638. sub drem16sH,drem16sH;clear remainder High byte and carry
  1639. ldi dcnt16s,17 ;init loop counter
  1640.  
  1641. d16s_3: rol dd16sL ;shift left dividend
  1642. rol dd16sH
  1643. dec dcnt16s ;decrement counter
  1644. brne d16s_5 ;if done
  1645. sbrs d16s,7 ; if MSB in sign register set
  1646. rjmp d16s_4
  1647. com dres16sH ; change sign of result
  1648. com dres16sL
  1649. subi dres16sL,low(-1)
  1650. sbci dres16sH,high(-1)
  1651. d16s_4: ;pop r14 ;PB!!
  1652. ;pop r13 ;PB!!
  1653. ret ; return
  1654. d16s_5: rol drem16sL ;shift dividend into remainder
  1655. rol drem16sH
  1656. sub drem16sL,dv16sL ;remainder = remainder - divisor
  1657. sbc drem16sH,dv16sH ;
  1658. brcc d16s_6 ;if result negative
  1659. add drem16sL,dv16sL ; restore remainder
  1660. adc drem16sH,dv16sH
  1661. clc ; clear carry to be shifted into result
  1662. rjmp d16s_3 ;else
  1663. d16s_6: sec ; set carry to be shifted into result
  1664. rjmp d16s_3
  1665.  
  1666. ;-----------------------------------------------
  1667.  
  1668. test_div16s:
  1669. ;***** Divide Two Signed 16-Bit Numbers (-22,222/10)
  1670. ldi dd16sL,low(-22222)
  1671. ldi dd16sH,high(-22222)
  1672. ldi dv16sL,low(10)
  1673. ldi dv16sH,high(10)
  1674. rcall div16s ;result: $f752 (-2222)
  1675. ;remainder: $0002 (2)
  1676.  
  1677. forever:rjmp forever
  1678. ;----------------------------------
  1679. test_slashMod:
  1680. ldi r16,$12
  1681. mypush r16
  1682. ldi r16,$34
  1683. mypush r16
  1684. ldi r16,$56 ;NB this is $3412 not $1234
  1685. mypush r16
  1686. ldi r16,$00
  1687. mypush r16
  1688. rcall slashMod ;$3412 / $56 = $9b rem 0 works
  1689. tslm: rjmp tslm
  1690.  
  1691. ;---------------------------------------
  1692. ;From http://www.avr-asm-tutorial.net/avr_en/calc/CONVERT.html#hex2bin
  1693. ; Hex4ToBin2
  1694. ; converts a 4-digit-hex-ascii to a 16-bit-binary
  1695. ; In: Z points to first digit of a Hex-ASCII-coded number
  1696. ; Out: T-flag has general result:
  1697. ; T=0: rBin1H:L has the 16-bit-binary result, Z points
  1698. ; to the first digit of the Hex-ASCII number
  1699. ; T=1: illegal character encountered, Z points to the
  1700. ; first non-hex-ASCII character
  1701. ; Used registers: rBin1H:L (result), R0 (restored after
  1702. ; use), rmp
  1703. ; Called subroutines: Hex2ToBin1, Hex1ToBin1
  1704.  
  1705. .def rBin1H =r17
  1706. .def rBin1L = r16
  1707. .def rmp = r18
  1708. ;
  1709. Hex4ToBin2:
  1710. clt ; Clear error flag
  1711. rcall Hex2ToBin1 ; convert two digits hex to Byte
  1712. brts Hex4ToBin2a ; Error, go back
  1713. mov rBin1H,rmp ; Byte to result MSB
  1714. rcall Hex2ToBin1 ; next two chars
  1715. brts Hex4ToBin2a ; Error, go back
  1716. mov rBin1L,rmp ; Byte to result LSB
  1717. sbiw ZL,4 ; result ok, go back to start
  1718. Hex4ToBin2a:
  1719. ret
  1720. ;
  1721. ; Hex2ToBin1 converts 2-digit-hex-ASCII to 8-bit-binary
  1722. ; Called By: Hex4ToBin2
  1723. ;
  1724. Hex2ToBin1:
  1725. push R0 ; Save register
  1726. rcall Hex1ToBin1 ; Read next char
  1727. brts Hex2ToBin1a ; Error
  1728. swap rmp; To upper nibble
  1729. mov R0,rmp ; interim storage
  1730. rcall Hex1ToBin1 ; Read another char
  1731. brts Hex2ToBin1a ; Error
  1732. or rmp,R0 ; pack the two nibbles together
  1733. Hex2ToBin1a:
  1734. pop R0 ; Restore R0
  1735. ret ; and return
  1736. ;
  1737. ; Hex1ToBin1 reads one char and converts to binary
  1738. ;
  1739. Hex1ToBin1:
  1740. ld rmp,z+ ; read the char
  1741. subi rmp,'0' ; ASCII to binary
  1742. brcs Hex1ToBin1b ; Error in char
  1743. cpi rmp,10 ; A..F
  1744. brcs Hex1ToBin1c ; not A..F
  1745. cpi rmp,$30 ; small letters?
  1746. brcs Hex1ToBin1a ; No
  1747. subi rmp,$20 ; small to capital letters
  1748. Hex1ToBin1a:
  1749. subi rmp,7 ; A..F
  1750. cpi rmp,10 ; A..F?
  1751. brcs Hex1ToBin1b ; Error, is smaller than A
  1752. cpi rmp,16 ; bigger than F?
  1753. brcs Hex1ToBin1c ; No, digit ok
  1754. Hex1ToBin1b: ; Error
  1755. sbiw ZL,1 ; one back
  1756. set ; Set flag
  1757. Hex1ToBin1c:
  1758. ret ; Return
  1759. ;--------------------------------------
  1760. test_Hex4ToBin2:
  1761. pushz
  1762. ldi zl,$60
  1763. clr zh ;z now points to start of buf1
  1764. ldi r16,'0'
  1765. st z+,r16
  1766. ldi r16,'f'
  1767. st z+,r16
  1768. ldi r16,'2'
  1769. st z+,r16
  1770. ldi r16,'3'
  1771. st z+,r16
  1772. ldi zl,$60
  1773. clr zh ;z now points back to start of buf1
  1774. rcall Hex4ToBin2
  1775. popz
  1776. th4: rjmp th4
  1777. ;-------------------------------------
  1778. numberh: ;word not in dictionary. Try to convert it to hex.
  1779. pushz ;algorithm uses z, pity
  1780. movw zl,r24 ;r4,25 = w holds start of current word
  1781. ;z now points eg to '12ab'start. If t=0 then it coverts to real hex
  1782. rcall hex4ToBin2 ;try to convert
  1783. ;above call needs 4 hex digits to emerge with t=0 and binary in r16,17
  1784. ;want this. If t=0 stack r16,17 and carry on interpreting, else emerge with
  1785. ; t=1 and zpointing to first problem char
  1786. brtc gotHex
  1787. ; if here there's a problem that z is pointing to. Bail out of interpret line
  1788. clr STOP
  1789. inc STOP
  1790. ;TODO put routine here that notes the word can't be excuted and it's
  1791. ; not a number. So output ramstring starting at adr = r24,25 and len in r20
  1792. rcall whatq
  1793. rjmp cold ; quit ;outnh **check
  1794.  
  1795. gotHex: ;sucess.Real hex in r16,17
  1796. mypush2 r16,r17 ; so push num onto mystack
  1797. ;maybe we're compiling. If so, push num into dic preceded by a call to stackme_2
  1798. tst STATE
  1799. breq outnh ;STATE =0 means executing
  1800. ; rcall tic
  1801. ; .db "stackme_2" ;has to be in dic before a number. cfa of stackme_2 on stack
  1802. rcall compstackme_2
  1803. ; rcall compileme ;insert "rcall stackme_2"opcode into dic
  1804. rcall comma ;there's the number going in
  1805.  
  1806. outnh:
  1807. popz ; but will it be pointing to "right"place in buf1? Yes now OK
  1808.  
  1809. ret
  1810. ; numberh not working fully, ie doesn't point to right place after action.
  1811. ; also no action if not a number? DONE better save this first.
  1812. ;---------------------------------
  1813. ;eeroutines
  1814. eewritebyte: ;write what's in r16 to eeprom adr in r18,19
  1815. sbic EECR,EEPE
  1816. rjmp eewritebyte ;keep looping til ready to write
  1817. ;if here the previous write is all done and we can write the next byte to eeprom
  1818. out EEARH,r19
  1819. out EEARL,r18 ;adr done
  1820. out EEDR,r16 ;byte in right place now
  1821. sbi EECR,EEMPE
  1822. sbi EECR,EEPE ;last 2 instruc write eprom. Takes 3.4 ms
  1823. ret
  1824. ;test with %!
  1825. ;---------------------------------
  1826. eereadbyte: ; read eeprom byte at adr in r18,19 into r16
  1827. ; Wait for completion of previous write
  1828. sbic EECR,EEPE
  1829. rjmp eereadbyte
  1830. ; Set up address (r18:r17) in address register
  1831. out EEARH, r19
  1832. out EEARL, r18
  1833. ; Start eeprom read by writing EERE
  1834. sbi EECR,EERE
  1835. ; Read data from data register
  1836. in r16,EEDR
  1837. ret
  1838. ;------------------------------
  1839. setupforflashin: ;using here etc get appropriate page, offset,myhere values.
  1840. ; ldi r16,low(HERE)
  1841. ; ldi r17,high(HERE) ;get here, but from eeprom better?
  1842. ; mypush2 r16,r17
  1843.  
  1844. ;above was a problem replace with one line below
  1845. rcall gethere ;HERE = eg 0a12.Now on stk.Comes from eepprom each time
  1846.  
  1847. rcall stackme_2
  1848. .dw 0002
  1849. rcall star ;now have current HERE in bytes in flash. But what is myhere?
  1850. rcall stackme_2
  1851. .db $0040 ;64 bytes per page
  1852. rcall slashMod
  1853. ;offset on top pagenum under. eg pg 0047, offset 0012
  1854. mypop2 r9,r8 ;store offset (in bytes)
  1855. rcall stackme_2
  1856. .db $0040
  1857. rcall star ;pgnum*64 = byte adr of start of flash page
  1858. mypop2 r7,r6
  1859. mypush2 r8,r9 ;push back offset
  1860. rcall stackme_2
  1861. .dw buf2
  1862. nop
  1863. ;at this stage we have offset in r8,r9 (0012). Also byte adr of flash page
  1864. ; start in r6,r7.(11c0) Stk is (offset buf2Start --) (0012 00E0 --). Need to
  1865. ; add these two together to get myhere, the pointer to RAM here position.
  1866. rcall plus ;add offset to buf2 start to get myhere (00f2)
  1867. ; put my here in r4,r5 for time being.
  1868. mypop2 r5,r4 ;contains eg 00f2 <--myhere
  1869. pushz ;going to use z so save it
  1870. movw zl,r6 ;r6,7 have byte adr of flsh pg strt
  1871. pushx ;save x
  1872. ldi xl,low(buf2)
  1873. ldi xh,high(buf2) ;point x to start of buf2
  1874. ldi r18,128 ;r18=ctr. Two flash pages = 128 bytes
  1875. upflash:
  1876. lpm r16,z+ ;get byte from flash page
  1877. st x+, r16 ; and put into buf2
  1878. dec r18
  1879. brne upflash
  1880. ;done. Now have two flash pages in ram in buf2. Myhere points to where next
  1881. ; entry will go. Where's page num?
  1882. popx
  1883. popz ;as if nothing happened
  1884.  
  1885.  
  1886. ret
  1887.  
  1888.  
  1889.  
  1890. ;outsufi: rjmp outsufi
  1891. ;-----------------------------------
  1892. burneepromvars: ;send latest versions of eHERE and eLATEST to eeprom
  1893. ldi r16,low(HERE)
  1894. ldi r17,high(HERE)
  1895. mypush2 r16,r17
  1896. ;up top we have .equ eHERE = $0010
  1897. ldi r16,low(eHERE)
  1898. ldi r17,high(eHERE)
  1899. mypush2 r16,r17
  1900. ;now have n16 eadr on stack ready for e!
  1901. rcall percentstore
  1902.  
  1903. ;send latest versions of eLATEST to eeprom
  1904. ldi r16,low(LATEST)
  1905. ldi r17,high(LATEST)
  1906. mypush2 r16,r17
  1907. ;up top we have .equ eLATEST = $0010
  1908. ldi r16,low(eLATEST)
  1909. ldi r17,high(eLATEST)
  1910. mypush2 r16,r17
  1911. ;now have n16 eadr on stack ready for e!
  1912. rcall percentstore
  1913. ret
  1914. ;-------------------------------------------
  1915. coloncode: ;this is the classic colon defining word.
  1916. rcall setupforflashin ;get all the relevant vars and bring in flash to buf2
  1917. ;rcall dxyz
  1918. rcall relinkcode ; insert link into first cell
  1919. rcall create ;compile word preceeded by length
  1920. rcall leftbrac ;set state to 1, we're compiling
  1921. ;takemeout 'c'
  1922. ;rcall report
  1923. ;takemeout 'c'
  1924. ret ;now every word gets compiled until we hit ";"
  1925. ;-------------------------
  1926. relinkcode: ;put LATEST into where myhere is pointing and update ptr = myhere
  1927. ;also create mylatest
  1928. rcall getlatest ;now on stack
  1929. mypopa ;latest in r16,17
  1930. pushz ;better save z
  1931. movw mylatest,myhere ;mylatest <-- myhere
  1932. movw zl,myhere ;z now points to next available spot in buf2
  1933. st z+,r17 ;problem. Don't work unless highbye first in mem.Why?
  1934. st z+,r16 ;now have new link in start of dic word
  1935. movw myhere,zl ;update myhere to point to length byte. (Not yet there.)
  1936. popz ;restore z
  1937. ret
  1938. ;-------------------------------------------------
  1939. create: ;put word after ":" into dictionary, aftyer link, preceeded by len
  1940. rcall word ;start with x pnting just after ":".End with len in r20, x pointing to
  1941. ; space just after word and start of word in w=r24,25
  1942. pushz ;save z. It's going to be used on ram dictionary
  1943. movw zl,myhere ;z now pnts to next spot in ram dic
  1944. st z+,r20 ; put len byte into ram dic
  1945. mov r18,r20 ;use r18 as ctr, don't wreck r20
  1946. pushx ;save x. It's going to be word ptr in buf1
  1947. movw xl,wl ;x now points to start of word. Going to be sent to buf2
  1948. sendbytes:
  1949. ld r16,x+ ;tx byte from buf1 to
  1950. st z+,r16 ; buf2
  1951. dec r18 ;repeat r20=r18=len times
  1952. brne sendbytes
  1953.  
  1954. sbrs r30,0 ;skip next instruction if final bit lsb = 1
  1955. rjmp downcr
  1956. ;if here lsb = 1 so we're on a padding byte and have to add 1 to get to a 2 byte boundary
  1957. clr r16
  1958. st z+,r16 ;insert padding byte
  1959. ;inc r30
  1960. ;brcc downcr
  1961. ;inc r31 ;add one to z before converting to bytes
  1962.  
  1963. downcr:
  1964. movw myhere,zl ;myhere now points to beyond word in dic
  1965. popx
  1966. popz
  1967. ret ;with word in dic
  1968. ;----------------------------------------------
  1969. leftbrac: ;classic turn on compiling
  1970. clr STATE
  1971. inc STATE ;state =1 ==> now compiling
  1972. ret
  1973. ;------------------------
  1974. compilecode: ;come here with STATE =1 ie compile, not execute. Want to put
  1975. ; eg rcall dup in code in dictionary but not to execute dup. If here
  1976. ; z points to byte address of word
  1977. mypush2 zl,zh
  1978. compileme:
  1979. mypush2 myhere,r5 ;push ptr to RAM dic
  1980. ;next is entry point for eg ' stackme2 already on stack and have to compile
  1981.  
  1982. ldi r16,low(buf2)
  1983. ldi r17,high(buf2) ;start of buf that conatins flash pg in RAM
  1984. mypush2 r16,r17
  1985. rcall minus ; myhere - buf2-start = offset in page
  1986. mypush2 SOFPG,r7 ;push start of flash page address
  1987. rcall plus ;SOFPG + offset = adr of next rcall in dic
  1988. ;if here we have two flash addresses on the stack. TOS = here. Next is there.
  1989. ;want to insert code for "rcall there w"hen I'm at here. eg current debugging indicates
  1990. ; here = $11EB and there is $1012 (cfa of "two"). First compute
  1991. ; relative branch "there - here -2". Then fiddle this val into the rcall opcode
  1992. rcall minus ;that;s there - here. Usu negative.
  1993. ;I got fffffffff..ffe27 for above vals. First mask off all those f's
  1994. rcall two ;stack a 2
  1995. rcall minus ;now have there-here -2 = fe24. When there,here in bytes.
  1996. mypopa ;bring fe26 into r16,17
  1997. clc
  1998. ror r17
  1999. ror r16 ;now a:= a/2
  2000. ldi r18,$ff
  2001. ldi r19,$0f ;mask
  2002. and r16,r18
  2003. and r17,r19
  2004. ; mypush2 r16,r17 ;now fe26 --> 0e26
  2005. ;the rcall opcode is Dxxx where xxx is the branch
  2006. ; mypopa ;bring fe26 into r16,17
  2007. ldi r19, $d0 ;mask
  2008. or r17,r19
  2009. mypush2 r16,r17 ;now have $de26 on stack which is (?) rcall two
  2010. rcall comma ;store this opcode into dic. myhere is ptr
  2011. ret
  2012. ;---------------------------
  2013. stackme_2: ;stacks on my stack next 16bit num. Address of 16bit number is on SP-stack
  2014. ; Used like this stackme_2 0034. Puts 0034 on myStack and increments past number on return stack.
  2015. pop r17
  2016. pop r16 ; they now contain eg 0x0804 which contain the 16bit num
  2017. movw zl,r16 ;z now points to cell that cobtains the number
  2018. clc
  2019. rol zl
  2020. rol zh ;double word address for z. lpm coming up
  2021.  
  2022.  
  2023.  
  2024. lpm r16,z+
  2025. lpm r17,z+ ;now have 16bit number in r16,17
  2026.  
  2027. st y+,r16
  2028. st y+, r17 ;mystack now contains the number
  2029.  
  2030. clc
  2031. ror zh
  2032. ror zl ;halve the z pointer to step past the number to return at the right place
  2033.  
  2034. push zl
  2035. push zh
  2036.  
  2037. ret
  2038. ;------------------------------flash write section--------------------
  2039.  
  2040. do_spm:
  2041. ;lds r16,SPMCSR
  2042. in r16,SPMCSR
  2043. andi r16,1
  2044. cpi r16,1
  2045. breq do_spm
  2046. mov r16,spmcsr_val
  2047. out SPMCSR,r16
  2048. spm
  2049. ret
  2050. ;-------------------------------------------------------------------
  2051. buf2ToFlashBuffer: ;send the 64 bytes, 32 words to flash page <-- Z pnts there.
  2052. push r30 ;save for later spm work.
  2053. push r19
  2054. push xl
  2055. push xh ;used as buf_ctr but may interfere with other uses
  2056. ldi XL,low(buf2) ;X pnts to buf1 that contains the 64 bytes.
  2057. ldi XH, high(buf2)
  2058. ;assume Z is already pointing to correct flash start of page.
  2059. flashbuf:
  2060. ldi buf_ctr,32 ;send 32 words
  2061. sendr0r1:
  2062. ld r16, x+ ;get first byte
  2063. mov r0,r16 ; into r0
  2064. ld r16, x+ ; and get the second of the pair into
  2065. mov r1,r16 ; into r1
  2066. ldi spmcsr_val,01 ;set up for write into spare buffer flash page
  2067. rcall do_spm ;that's r0,r1 gone in.
  2068. inc r30
  2069. inc r30
  2070. dec buf_ctr ;done 32 times?
  2071. brne sendr0r1
  2072. pop xh
  2073. pop xl
  2074. pop r19 ;dont need buf_ctr any more.
  2075. pop r30 ;for next spm job
  2076.  
  2077. ret
  2078. ;--------------------------------------------------------------------------
  2079. ;TODO just have 1 burn routine with buf different
  2080. buf3ToFlashBuffer: ;send the 64 bytes, 32 words to flash page <-- Z pnts there.
  2081. push r30 ;save for later spm work.
  2082. push r19 ;used as buf_ctr but may interfere with other uses
  2083. push xl
  2084. push xh
  2085. ldi XL,low(buf2+64) ;X pnts to buf1 that contains the 64 bytes.
  2086. ldi XH, high(buf2+64)
  2087. ;assume Z is already pointing to correct flash start of page.
  2088. rjmp flashbuf
  2089. ldi buf_ctr,32 ;send 32 words
  2090. sendr0r3:
  2091. ld r16, x+ ;get first byte
  2092. mov r0,r16 ; into r0
  2093. ld r16, x+ ; and get the second of the pair into
  2094. mov r1,r16 ; into r1
  2095. ldi spmcsr_val,01 ;set up for write into spare buffer flash page
  2096. rcall do_spm ;that's r0,r1 gone in.
  2097. inc r30
  2098. inc r30
  2099. dec buf_ctr ;done 32 times?
  2100. brne sendr0r3
  2101. pop r19 ;dont need buf_ctr any more.
  2102. pop r30 ;for next spm job
  2103. ret
  2104.  
  2105. erasePage: ; assume Z points to start of a flash page. Erase it.
  2106. ldi spmcsr_val,0x03 ;this is the page erase command
  2107. rcall do_spm
  2108. ret
  2109. ;------------------------------------------------------------------
  2110. writePage:
  2111. ldi spmcsr_val, 0x05 ;command that writes temp buffer to flash. 64 bytes
  2112. rcall do_spm
  2113. nop ; page now written. z still points to start of this page
  2114. ret
  2115. ;---------------------------------------------------------------
  2116. test_buf2ToFlashBuffer: ;(adr_flashbufstartinBytes -- )
  2117. ; rcall fillBuf
  2118. ; ldi ZH, $10
  2119. ; ldi ZL,$c0 ;z=$01c0. Start of page 67.
  2120. rcall gethere
  2121. rcall double ;want bytes not words for flash adr
  2122. mypopa ;flashPgStart byte adr now in r16,17
  2123.  
  2124.  
  2125. movw zl,r16 ;z <--start of flash buffer
  2126. rcall erasePage
  2127. rcall buf2ToFlashBuffer
  2128. rcall writePage
  2129. herettt:
  2130. rjmp herettt
  2131. ;----------------------
  2132. ; y2. Come here from ";". The pair r6,r7 point to start of flash pg (bytes)
  2133. burnbuf2and3:
  2134. ;takemeout 'U'
  2135. ;ldi r16, 'U'
  2136. ;clr r17
  2137. ;mypush2 r16,r17
  2138. ;rcall emitcode
  2139. ;rcall dlowR
  2140. movw zl,r6 ;z now pnts to start of flash buf
  2141. ;rcall dxyz ;having !!! PROBS take out later
  2142. rcall erasePage
  2143. rcall buf2ToFlashBuffer
  2144. rcall writePage
  2145. ;now going to burn next ram buffer to next flash page. Bump Z by 64 bytes.
  2146. adiw zh:zl,63 ;z now points to start of next flash buffer
  2147. lpm r16,z+ ;advance z pointer by one.adiw only lets max of 63 to be added.
  2148. ;now z points to start of next 64 byte buffer. Time to put buf3 into it.
  2149. rcall erasePage
  2150. rcall buf3ToFlashBuffer
  2151. rcall writePage
  2152. ret
  2153. heret:
  2154. rjmp heret
  2155. ;-------------------------------------------------------------
  2156. updatevars: ;after doing a colon def we have to update sys vars
  2157. ;TODO new version of LATEST is just old version of HERE.
  2158. ;TODO rplace all this code with updatevars2
  2159. ; just shif HERE into LATEST in eeprom to update. Gen. tidy required.
  2160. mypush2 r4,r5 ;put myhere on stack (E8)
  2161. ldi r16,low(buf2)
  2162. ldi r17,high(buf2)
  2163. mypush2 r16,r17 ;start of buf2 on stack (E0)
  2164. rcall minus ;myhere-buf2 = offset. (e8-e0 = 08)
  2165. mypush2 SOFPG,r7 ; push onto stk start adr of flash page
  2166. rcall plus ;SOFG + offset = new HERE
  2167. ;now put also on stack new version of LATEST
  2168. mypush2 r2,r3 ;that's mylatest on stack
  2169. ldi r16,low(buf2)
  2170. ldi r17,high(buf2)
  2171. mypush2 r16,r17 ;start of buf2 on stack (E0)
  2172. rcall minus ;myhere-buf2 = offset. (e8-e0 = 08)
  2173. mypush2 SOFPG,r7 ; push onto stk start adr of flash page
  2174. rcall plus ;SOFG + offset = new LATEST
  2175. ; now have both LATEST (tos) and HERE on stack. Burn these into eeprom
  2176. ;up top we have .equ eLATEST = $0010
  2177. ;But it's too big. In bytes and causing probs. Solution=covert to words
  2178. rcall halve
  2179. ldi r16,low(eLATEST)
  2180. ldi r17,high(eLATEST)
  2181. mypush2 r16,r17
  2182. ;now have n16 eadr on stack ready for e!
  2183. rcall percentstore
  2184. ; TODO the value for HERE is prob in bytes too. Convert to words.
  2185. ;up top we have .equ eLATEST = $0010
  2186. ldi r16,low(eHERE)
  2187. ldi r17,high(eHERE)
  2188. mypush2 r16,r17
  2189. ;now have n16 eadr on stack ready for e!
  2190. rcall halve ;TODO check this
  2191. rcall percentstore
  2192. ret ;with stack clear and new vals for HERE and LATEST in eeprom
  2193. ;----------
  2194. ;;;;;;;;;;;;;;;;;;;;;;;;;;;Now serial stuff starts;;;;;;;;;;;;;;;;;;;;;;;;;
  2195. halfBitTime: ;better name for this delay. Half of 1/600
  2196. ;myDelay1200:
  2197. ;ldi r21,13 ; 13 works for m328 at 16Mhz
  2198. push r20
  2199. push r21
  2200. ldi r21,7 ;try 7 for tiny85 at 8Hmz
  2201. ldi r20,130 ;r20,21 at 130,7 give 833uS. Good for 600baud at 8Mhz
  2202. starthbt:
  2203. inc r20
  2204. nop
  2205. brne starthbt
  2206. dec r21
  2207. brne starthbt
  2208. pop r21
  2209. pop r20
  2210. ret
  2211. ;--------------------------------------------------
  2212. oneBitTime:
  2213. rcall halfBitTime
  2214. rcall halfBitTime
  2215. ret
  2216. ;-------------------------------------------------
  2217. sendAZero:
  2218. ;output 0 on Tx pin
  2219. cbi PORTB,TX_PIN ; send a zero out PB0
  2220. ret
  2221. ;-----------------------------------------------------
  2222.  
  2223. sendAOne:
  2224. ;output 1 on Tx pin
  2225. sbi PORTB,TX_PIN ; send a zero out PB0
  2226. ret
  2227. ;-----------------------------------------------------
  2228. sendStartBit:
  2229. ; send a 0 for one bit time
  2230. rcall sendAZero
  2231. rcall oneBitTime
  2232. ret
  2233. ;-------------------------------------------------------
  2234. sendNextDataBit: ;main output routine for serial tx
  2235. lsr serialByteReg ;push high bit into carry flag then inspect it
  2236. ;originally did lsl but found lsb first.
  2237. brcc gotzero ;if it's a 0 do nothing
  2238. rcall sendAOne ;must have been a 1 in carry
  2239. rjmp down
  2240. gotzero:
  2241. rcall sendAZero ;if here carry was a zero
  2242. down:
  2243. rcall oneBitTime ;so that 1 or 0 lasts 1/600 sec
  2244. ret
  2245. ;-------------------------------------------------------------
  2246. send8DataBits: ; send all bits in serialByteReg
  2247. ldi counterReg,8 ;8 data bits
  2248. sendBit:
  2249. rcall sendNextDataBit
  2250. dec counterReg
  2251. brne sendBit
  2252. ret
  2253. ;--------------------------------------------------------
  2254. sendStopBit:
  2255. ; send a 1 for one bit time
  2256. rcall sendAOne
  2257. rcall oneBitTime
  2258. ret
  2259. ;--------------------------------------------------------
  2260. sendSerialByte: ;main routine. Byte in serialByteReg = r16
  2261. .ifdef testing
  2262. mov r0, r16
  2263. .else
  2264. push counterReg
  2265. rcall sendStartBit
  2266. rcall send8DataBits
  2267. rcall sendStopBit
  2268. rcall sendStopBit ;two stops
  2269. pop counterReg
  2270. .endif
  2271. ret
  2272. ;**************************************************************
  2273. serialTest0: ;output series of 'AAAA..'s
  2274. ldi serialByteReg, 0x43 ;0x41
  2275. rcall sendSerialByte
  2276. rcall oneBitTime ; take a rest
  2277. ldi r16,$44
  2278. mypush r16
  2279. rcall emitcode
  2280.  
  2281. rjmp serialTest0 ;continue forever
  2282. ;---------------------------------------------------------
  2283. ;---------Now do SerialRx routines-------------------
  2284. waitForHigh: ;loop til RX is high
  2285. sbis PINB,RX_PIN ;test that pin for set (PB2)
  2286. rjmp waitForHigh ; loop if rx pin is low
  2287. ret
  2288. ;-----------------------------------------------
  2289. waitForLow: ;PRONBLEMs loop til RX is low. FIXED.
  2290. sbic PINB,2 ;test that pin for set (PB2)
  2291. rjmp waitForLow ; loop if rx pin is high
  2292. ret
  2293. ;---------------------------------------------------
  2294. waitForStartBit: ;loop til get a real start bit
  2295. rcall waitForHigh ;should be marking at start
  2296. rcall waitForLow ;gone low. might be noise
  2297. rcall halfBitTime ;is it still low in middle of bit time
  2298. sbic PINB,RX_PIN ;..well, is it?
  2299. rjmp waitForStartBit ;loop if level gone back high. Not a start bit.
  2300. ret ;we've got our start bit
  2301. ;----------------------------------------------------
  2302. checkForStopBit: ;at end, get carry flag to reflect level. Prob if c=0
  2303. rcall oneBitTime ; go into stop bit frame, halfway
  2304. sec ;should stay a 1 in C if stop bit OK
  2305. sbis PINB,RX_PIN ;don't clc if bit is high
  2306. clc ;but only if we have a weird low stop bit
  2307. ret ;with carry flag = stop bit. Should be a 1
  2308. ;-------------------------------------------------------------
  2309. get8Bits: ;get the 8 data bits. No frame stuff
  2310. clr rxbyte ;this will fill up with bits read from RX_PIN
  2311. push counterReg ;going to use this so save contents for later
  2312. ldi counterReg,8 ;because we're expecting 8 databits
  2313. nextBit:
  2314. rcall oneBitTime ;first enter here when mid-startbit
  2315. rcall rxABit ;get one bit
  2316. dec counterReg ;done?
  2317. brne nextBit ;no, round again
  2318. pop counterReg ;yes, finished, restor counter and get out
  2319. ret
  2320. ;---------------------------------------------------------------
  2321. rxABit: ;big serial input routine for one bit
  2322. clc ;assume a 0
  2323. sbic PINB,RX_PIN ; skip nxt if pin low
  2324. sec ;rx pin was high
  2325. ror rxbyte ;carry flag rolls into msb first
  2326. ret
  2327. ;********************************
  2328. getSerialByte: ;big routine. Serial ends up in rxByte
  2329. push counterReg
  2330. rcall waitForStartBit ;**change
  2331. rcall get8Bits
  2332. rcall checkForStopBit
  2333. pop counterReg
  2334. ret ;with rxByte containing serial bye
  2335. ;----------------------------------------------------
  2336. serialTest1: ;output A then reflect input. Worked OK
  2337. ldi serialByteReg, 0x36 ;0x41
  2338. rcall sendSerialByte
  2339. rcall oneBitTime ; take a rest
  2340. rcall getSerialByte
  2341. mov serialByteReg,rxByte ;output what's been read
  2342. rcall sendSerialByte
  2343. rjmp serialTest1
  2344. ;--------------------------------------------------------
  2345. ;----------Now doing buffer work. Want to and from 64 bytes----------
  2346. fillBuf:
  2347. ldi ZL,low(buf1) ;buf1 is my buffer
  2348. ldi ZH, high(buf1) ;Z now points to buf1
  2349. ldi counterReg,64 ;64 bytes in buffer
  2350. ldi r16,$30
  2351. storeB0:
  2352. st z+,r16
  2353. inc r16
  2354. dec counterReg
  2355. brne storeB0
  2356. herefb:
  2357. ; rjmp herefb
  2358. ret
  2359. ;----------------------------------------------------------
  2360. serialStrOut: ;X points to start of string,r17 has length
  2361. ld serialByteReg, x+
  2362.  
  2363. rcall sendSerialByte
  2364. dec r17 ;got to end of string?
  2365. brne serialStrOut
  2366. ret
  2367. ;----------------------------------
  2368. test_serialStrOut:
  2369. rcall fillBuf
  2370. ldi XL,low(buf1) ;buf1 start of str
  2371. ldi XH, high(buf1)
  2372. ldi r17,64 ;going to send len=r17 bytes
  2373. rcall serialStrOut
  2374. here2:
  2375. rjmp here2
  2376. ;--------------------------------------
  2377. waitForCharD: ;wait til eg a 'D' is pressed then do something.
  2378. ldi serialByteReg, '>' ;0x41
  2379. rcall sendSerialByte
  2380. rcall oneBitTime ; take a rest
  2381. rcall getSerialByte
  2382. mov serialByteReg,rxByte ;output what's been read
  2383. cpi rxByte, 'D'
  2384. brne waitForCharD
  2385. ldi serialByteReg, '*'
  2386. rcall sendSerialByte
  2387. rjmp waitForCharD
  2388. ;-----------------------------------------------------------
  2389. dumpbuf1:
  2390. .ifdef livetesting
  2391. ldi XL,low(buf1) ;buf1 start of str
  2392. ldi XH, high(buf1)
  2393. ldi r17,64 ;going to send len=r17 bytes
  2394. rcall serialStrOut
  2395. .endif
  2396. ret
  2397. ;-------------------------------------------------------------
  2398. test_dumpbuf1:
  2399. rcall fillBuf
  2400. rcall getSerialByte ;any one will do.
  2401. rcall dumpbuf1
  2402. rjmp test_dumpbuf1
  2403. ;----------------------------------------------------------
  2404. waitForDDump: ;wait til eg a 'D' is pressed then dump buf1
  2405. ldi serialByteReg, '>' ;0x41
  2406. rcall sendSerialByte
  2407. rcall oneBitTime ; take a rest
  2408. rcall getSerialByte
  2409. mov serialByteReg,rxByte ;output what's been read
  2410. cpi rxByte, 'D'
  2411. brne waitForDDump
  2412. rcall dumpbuf1
  2413. rjmp waitForCharD
  2414. ;---------------------------------------------------------------
  2415. rxStrEndCR: ;get a serial string that ends with CR
  2416. clr counterReg
  2417. ldi XL,low(buf1) ;buf1 is where str will go
  2418. ldi XH, high(buf1)
  2419. takemeout 'A'
  2420. upsec:
  2421. rcall getSerialByte
  2422.  
  2423. st x+, rxByte ;char goes into buffer="buf1"
  2424.  
  2425. cpi rxByte,$0d ;is it CR = end of string?
  2426. breq fin
  2427. inc counterReg ;don't go over 64 bytes
  2428. cpi counterReg,64
  2429. brne upsec ;not too long and not CR so keep going
  2430. fin:
  2431. ret
  2432. ;---------------------------------------------
  2433. test_rxStrEndCR: ;just a test of above
  2434. rcall OK
  2435. rcall CR
  2436. rcall rxStrEndCR
  2437. rcall dumpbuf1
  2438. rcall CR
  2439. ; rcall waitForDDump
  2440. rjmp test_rxStrEndCR
  2441. ;------------------------------------------------------
  2442. test2_rxStrEndCR: ;want a diagnostic dump if testing. Works with .IFDEF
  2443. rcall rxStrEndCR
  2444. .IFDEF testing
  2445. rcall dumpbuf1
  2446. .ENDIF
  2447. rjmp test2_rxStrEndCR
  2448. ;------------------------------------------------------------
  2449. rxStrWithLen: ;expect len char char char.. for len chars
  2450. push counterReg
  2451. ldi XL,low(buf1) ;buf1 is where str will go
  2452. ldi XH, high(buf1)
  2453. rcall getSerialByte ; get length bye Must be less than 65
  2454. mov counterReg, rxByte ;save len in counter
  2455. cpi counterReg,65 ;
  2456. brlo allOK ;less than 65 so carry on. Branch if Lower
  2457. ldi counterReg,64 ; if len>64 then len=64. Buffer = buf1 only 64 bytes
  2458. allOK:
  2459. tst counterReg ;zero yet?
  2460. breq finrs
  2461. rcall getSerialByte ;next serial input byte
  2462. st x+, rxByte ;put into buffer
  2463. dec counterReg ;have we done len=counterReg bytes?
  2464. rjmp allOK
  2465. finrs:
  2466. pop counterReg
  2467. ret
  2468. ;---------------------------------------------------------------
  2469. test_rsStrWithLen: ;works ok with macro $05GHIJKLM. Sends GHIJK
  2470. ldi r16, '#'
  2471. rcall sendSerialByte
  2472. rcall rxStrWithLen
  2473. rcall dumpbuf1
  2474. rjmp test_rsStrWithLen
  2475. ;-----------------------------now start forth i/o words like emit------------------
  2476. emitcode: ; (n8 --)classic emit
  2477. mypop r16
  2478. mypop r16 ;want lower byte eg in 0041 want just the 41
  2479. rcall sendserialbyte
  2480. ret
  2481. ;------------------------------------------------
  2482. insertret: ;semi has to end new word with ret = $9508 opcode
  2483. pushx ;both xl,xh saved for later
  2484. movw xl,myhere ;myhere points to next available spot in ram dic
  2485. ldi r16,$08
  2486. st x+,r16 ;$08 part goes first
  2487. ldi r16,$95
  2488. st x+,r16 ;ret now in ram. Just tidy pointers
  2489. movw myhere,xl
  2490. popx ;so x back where it was and ret inserted.
  2491. ret
  2492. ;--------------------------------
  2493. equalcode: ;(n1 n2 -- flag) if n1 = n2 flag = 0001 else 0000
  2494. mypopa
  2495. mypopb ; now have TOS in r16,17, underneath that in r18,19
  2496. cp r16,r18 ;low bytes =?
  2497. brne zout ;not equal so go out
  2498. cp r17,r19 ;hi bytes =?
  2499. brne zout ;no, so out
  2500. ;if here both n16's are equal so push a 0001
  2501. rcall one
  2502. rjmp aout ;done
  2503. zout:
  2504. rcall zero ;not = so push a zero
  2505. aout:
  2506. ret ;with a flag on stack replacing to n16's
  2507. ;------------------------------
  2508. ;TODO eliminate below and replace with simpler RAM jmp code.
  2509. calcjumpcode: ;(to from -- opcode_for_rjmp to at from)
  2510. ;used when compiling. What is the rjmp opcode if
  2511. ; we know the from and to adr on stack. ( to fr --)
  2512. ldi r16, low(buf2)
  2513. ldi r17, high(buf2)
  2514. mypush2 r16,r17 ; (to fr $e0 --)
  2515. rcall dup ;t f $e0 $eo
  2516. rcall unrot ;t $e0 fr $e0
  2517. rcall minus ;t $e0 frOffset
  2518. rcall unrot ;frOffset t $e0
  2519. rcall minus ;frOffset toOffset
  2520. ;now apply these offsets in flash buffer. Add them to start of flash buffer adr
  2521. mypush2 SOFPG,r7 ; frOffset toOffset SOFPG
  2522. rcall dup ;frOffset toOffset SOFPG SOFPG
  2523. rcall unrot ;frOffset SOFPG toOffset SOFPG
  2524. rcall plus ;frOffset SOFPG toFlashAdr
  2525. rcall unrot ;toFlashAdr frOffset SOFPG
  2526. rcall plus ;toFlashAdr frFlashAdr
  2527. rcall minus ;to -from give last 3 nibbles in rjmp opcode +1
  2528. ; rcall one
  2529. rcall two ;to - from - 2 when working with bytes
  2530. rcall minus ; now have to - from -2
  2531. rcall halve ;now have jmp length in words. Required for opcode.
  2532. rcall stackme_2
  2533. .dw $0fff
  2534. rcall andd ; now have eg. 0f20. Want Cf20
  2535. rcall stackme_2
  2536. .dw $c000 ;should now have right opcode eg cf20
  2537. rcall orr ;don't forget this !!!!!
  2538. ret ;with correct rjmp kkk on stack. Ready to insert into RAM dic.
  2539. ;-------------------
  2540. stackmyhere: ;( --- adr) put RAM ptr myhere on stack
  2541. mypush2 myhere, r5
  2542. ret
  2543. ;---------------------------
  2544. begincode: ;when using BEGIN just stack current address.No dic entry
  2545. rcall stackmyhere ;put next adr on stack
  2546. ret
  2547. ;----------------------------
  2548. stkmyhere: ;put myhere on the stack, handy
  2549. mypush2 myhere,r5
  2550. ret
  2551. ;-----------------------------------
  2552. stkSOBuf2: ;stack start of buf2. Handy.
  2553. ldi r16,low(buf2)
  2554. ldi r17,high(buf2)
  2555. mypush2 r16,r17
  2556. ret ;with adr of buf2 on stk
  2557. ;--------------------------
  2558. stkSOFPG: ;put start of flash page on stack, In bytes.
  2559. mypush2 SOFPG,r7
  2560. ret ;with start of current flash page's adr on stack.
  2561. ;-------------------------------
  2562. stklatestadr: ;put e-adr of eLatest. Currently 012 in eeprom
  2563. ldi r16,low(eLATEST)
  2564. ldi r17,high(eLATEST)
  2565. mypush2 r16,r17
  2566. ret ;with 012 or adr of eLatest on stk
  2567. ;-------------------------------------
  2568. stkhereadr: ;same as above but for HERE
  2569. ldi r16,low(eHERE)
  2570. ldi r17,high(eHERE)
  2571. mypush2 r16,r17
  2572. ret ;with adr of ehere,current eeprom adr = $010
  2573. ;-------------------------------------------
  2574. updatevars2: ;better version of update vars. Come here after ";"
  2575. ;TODO check this version.DONE and eliminate other one.
  2576. rcall gethere ;the HERE val now on stack. It's a pointer to flash.
  2577. rcall stklatestadr ;usually 012
  2578. rcall percentstore
  2579. ;now with LATEST now containing old HERE. Next fix HERE
  2580. rcall stkmyhere ;current ptr to RAM dic's next free byte
  2581. rcall stkSOBuf2 ;start of buf2 adr
  2582. rcall minus ;gives distance into the buffer
  2583. rcall stkSOFPG ;will add distance to start of flashbuf
  2584. rcall plus ;got flash adr, but in bytes
  2585. rcall halve ;now adr in words
  2586. rcall stkhereadr ;usually %010 in eeprom
  2587. rcall percentstore ;eHERE now updated
  2588. ret ;with vals for HERE and LATEST in eeprom updated after ";"
  2589. ;--------------------
  2590. testOKCR:
  2591. rcall OK
  2592. rcall OK
  2593. rcall CR
  2594. rjmp testOKCR
  2595. ;--------------------
  2596.  
  2597. ;------------------------dump routines _______________
  2598. outnib: ;given $23 in r16, output the 3 as '3' = $33
  2599. push r18 ;going to use this
  2600. andi r16,$0f ; $3a --> $0a
  2601. cpi r16,$0a ;more than 10?
  2602. brge gothexo ;Nibble >= 10 jump down to gothex
  2603. ldi r18,$30 ; add $30 to 0..9
  2604. rjmp doneon
  2605. gothexo:
  2606. ldi r18,$37
  2607. doneon:
  2608. add r16,r18 ;now r16 nibble $03 is a '3'
  2609. rcall sendserialbyte ;print it
  2610. pop r18 ;used this as counter
  2611. ret ;note, it wrecks r16
  2612. ;--------------------------------------------
  2613. d16: ;dump contents of r16. Good for debugging.
  2614. push r16 ;keep contents for later
  2615. push r16 ;need this one after swap
  2616. swap r16 ;$34 wants 3 to come out first
  2617. rcall outnib ;print ascii eg '3'in above if r16 = $34
  2618. pop r16 ;get nice version back eg $34
  2619. rcall outnib ;print the '4'
  2620. pop r16 ;so r16 not wrecked.
  2621. ret ;with r16 printed in ascii
  2622. ;-----------------------------------
  2623. test_d16: ldi r16,$a5
  2624. rcall d16
  2625. ldi r16,$b6
  2626. rcall d16
  2627. rjmp test_d16
  2628. ;--------------------------------
  2629. d1617: ;dump r16 and r17 for debugging purposes
  2630. push r16
  2631. push r17 ;
  2632. push r16 ;just one min
  2633. mov r16, r17
  2634. rcall d16 ;that's r17 gone
  2635. pop r16
  2636. rcall d16 ;and then r16
  2637. pop r17
  2638. pop r16
  2639. ret ;with r17:r16 output in ascii
  2640. ;----------------------------------------
  2641. test_d1617:
  2642. ldi r16,$34
  2643. ldi r17,$1F
  2644. rcall d1617
  2645. rjmp test_d1617
  2646. ;-----------------------------------
  2647. dlowR: ;dump low registers. r0..r15 for debugging
  2648. ;.ifdef livetesting
  2649. push r16
  2650. push r18
  2651. pushx ;macro
  2652. clr xl
  2653. clr xh
  2654. ldi r18,16 ;r18 is a counter
  2655. prlow:
  2656. ld r16,x+ ;assume is x is 0 we'll get r0
  2657. rcall d16
  2658. rcall spacecode
  2659. dec r18
  2660. cpi r18,$07
  2661. breq doeseq7
  2662. tst r18
  2663. brne prlow
  2664. rjmp outprl
  2665. doeseq7:
  2666. ldi r16,'L'
  2667. rcall sendserialbyte
  2668. rcall spacecode
  2669. rjmp prlow
  2670.  
  2671. outprl:
  2672. popx ;macro
  2673. pop r18
  2674. pop r16
  2675. ;B.endif
  2676. ret ;with all the registers r0 ..r15 output in ascii to terminal screen
  2677. ;----------------------------------
  2678. test_dlowR:
  2679. rcall CR
  2680. ldi r16,$02
  2681. mov r0,r16
  2682. ldi r16,$52
  2683. mov r5,r16
  2684. ldi r16,$f2
  2685. mov r15,r16
  2686. rcall dlowR
  2687. rcall CR
  2688. rjmp test_dlowR
  2689. ;-----------------------------
  2690. spacecode: ;output a space
  2691. push r16
  2692. ldi r16,$20
  2693. rcall sendserialbyte
  2694. pop r16
  2695. ret
  2696. ;-------------------------------
  2697. dhighR: ;dump high registers. r18..r25 for debugging
  2698. push r16
  2699. push r17
  2700. pushx ;macro
  2701. ldi xl,18
  2702. ; clr xl
  2703. clr xh
  2704. ldi r17,8 ;r18 is a counter
  2705. prhi:
  2706. ld r16,x+ ;assume is x is 18 we'll get r18
  2707. rcall d16
  2708. rcall spacecode
  2709. dec r17
  2710. cpi r17,5
  2711. breq doeseq21
  2712. tst r17
  2713. brne prhi
  2714. rjmp outprh
  2715. doeseq21:
  2716. ldi r16,'H'
  2717. rcall sendserialbyte
  2718. rcall spacecode
  2719. rjmp prhi
  2720.  
  2721. outprh:
  2722. popx ;macro
  2723. pop r17
  2724. pop r16
  2725. ret ;with all the registers r0 ..r15 output in ascii to terminal screen
  2726. ;----------------------------------
  2727. test_dhighR:
  2728. rcall CR
  2729. ldi r18,$88
  2730. ldi r19,$19
  2731. ldi r20,$88 ;
  2732. ldi r21,$88
  2733. ldi r22,$22
  2734. ldi r23,$23
  2735. ldi r24,$24
  2736. ldi r25,$25
  2737. rcall dhighR
  2738. rcall CR
  2739. rjmp test_dhighR
  2740. ;------------------------------------
  2741. dxyz: ;dump the three pointer regs x,y,z
  2742.  
  2743. push r16
  2744. push r17
  2745. movw r16,xl ;r17:16 gets xh:xl
  2746. rcall d1617
  2747. rcall spacecode
  2748. movw r16,yl
  2749. rcall d1617
  2750. rcall spacecode
  2751. movw r16,zl
  2752. rcall d1617
  2753. rcall spacecode
  2754. pop r17
  2755. pop r16
  2756. ret ;with x,y,z output in ascii as a tripple
  2757. ;--------------------------------------
  2758. test_dxyz:
  2759. rcall CR
  2760. ldi xl,$12
  2761. ldi xh,$34
  2762. ldi yl,$56
  2763. ldi yh,$78
  2764. ldi zl,$9A
  2765. ldi zh,$bc
  2766. rcall CR
  2767. rcall dxyz
  2768. rcall CR
  2769. rjmp test_dxyz
  2770. ;--------------------------------
  2771. ;mystack needs a DEPTH word.
  2772. depthcode: ; (--n16)
  2773. ;leave on mystack the number of items on the stack by bytes.
  2774. movw r16,yl ;now r16,17 has y pointer
  2775. ldi r18, low(myStackStart) ;
  2776. ldi r19, high(myStackStart) ;r18,19 probably contain $1A0, the start of mystack
  2777. mypush2 r16,r17
  2778. mypush2 r18,r19 ;setup for eg $1a6 - $1a0
  2779. rcall minus ;difference=depth = eg 0006 as above.
  2780. ret ; with depth on stack
  2781. ;-----------------------------------------
  2782. test_depthcode:
  2783. ldi r16,$01
  2784. ldi r17,$23
  2785. mypush2 r16,r17
  2786. mypush2 r16,r17
  2787. mypush2 r16,r17
  2788. rcall depthcode
  2789. uptd: mypopa ;depth now in r16,17
  2790. up2: rcall d1617
  2791. rjmp up2
  2792. ;------------------------------------
  2793. dotScode: ;classic .S, print stack non-destructively
  2794. push r16
  2795. push r18
  2796. pushx ;macro
  2797. rcall depthcode ;now depth = len of stk on the mystack top
  2798. ; rcall drop ;stk =eg 0006 . want just len = 06
  2799. mypop2 r17,r18 ;so r18 now has length in bytes we're printing
  2800. ldi xl, low(myStackStart)
  2801. ldi xh, high(myStackStart)
  2802.  
  2803. ; movw xl,yl ;use x as temp ptr. Keep y pointing to mystack top
  2804. upds:
  2805. ld r16,x+ ;get tos, Pre-decrement.
  2806. rcall d16 ;print it
  2807. rcall spacecode ;
  2808. dec r18
  2809. brne upds
  2810. ldi r16, ']'
  2811. rcall sendserialbyte
  2812. rcall spacecode
  2813. popx ;macro
  2814. pop r18
  2815. pop r16
  2816. ret ;with the stack items printed to term screen + ]
  2817. ;-----------------------------
  2818. test_dotScode:
  2819. ldi r16,$A1
  2820. ldi r17,$B2
  2821. mypush2 r16,r17
  2822. mypush2 r16,r17
  2823. mypush2 r16,r17
  2824. rcall dotScode
  2825. rcall drop
  2826. rcall drop
  2827. rcall drop
  2828. uptds:
  2829. rjmp uptds
  2830. ;---------------------------------
  2831. wordscode: ;classic words. List all the words in the dic
  2832. push r16
  2833. push r17
  2834. push r22
  2835. push r23
  2836. push r24
  2837. pushz
  2838. rcall doLatest ;get first link into v
  2839. upwo:
  2840. rcall jmpNextWord ;pnt to link part of next word
  2841. lpm r23,z+
  2842. lpm r22,z+ ;store link into v=r23,24
  2843. lpm r16,z+ ;get len
  2844. andi r16,$0f ;don't want eg $85 to be len when it means immediate len 5.
  2845. clr r17 ;need eg 0006 on stk not 06 later
  2846. mypush2 r16,r17 ;len byte now on mystk
  2847. ;at this stage z points to the start of word name
  2848. mypush2 zl,zh ;flash start adr of string now on mystack
  2849. rcall swapp ; but wrong way round. Want len = TOS
  2850. rcall Sdot ;print the string on the term
  2851. rcall spacecode ;but add space after each word
  2852. tst vl
  2853. brne upwo ;if vl:vh = r23,24 = 0000 finish
  2854. tst vh
  2855. brne upwo
  2856. popz ;
  2857. pop r24
  2858. pop r23
  2859. pop r22
  2860. pop r17 ;TODO macro with multiple pops & pushes
  2861. pop r16
  2862. ret ;with all the words in dic printed
  2863. ;-----------------------------------------------
  2864. clrbuf1:
  2865. ldi ZL,low(buf1) ;buf1 is my buffer
  2866. ldi ZH, high(buf1) ;Z now points to buf1
  2867. ldi counterReg,64 ;64 bytes in buffer
  2868. ldi r16,$30
  2869. storecl:
  2870. st z+,r16
  2871. inc r16
  2872. dec counterReg
  2873. brne storecl
  2874.  
  2875. ret
  2876. ;-----------------------
  2877. updatevarptrcode: ;update varptr currently at eeprom's 0016. Add 2 to its contents.
  2878. rcall getvarptr ;eg 0160 in ram
  2879. rcall two
  2880. rcall plus ;now is eg 0162
  2881. rcall varptradr ;usually 0016 in eeprom
  2882. rcall percentstore ;should be called estore ie e!
  2883. ret ;with ptr val = old ptrval + 2
  2884. ;-------------------------
  2885. variablecode: ;big word called each time variable is declared
  2886. rcall coloncode ;does all the create work in buf
  2887.  
  2888. rcall getvarptr ;put eg 0162 on stack. Address of next RAM var place.
  2889. rcall compstackme_2 ;put stackme_2 as first code when called
  2890.  
  2891. rcall comma
  2892. rcall updatevarptrcode ;add 2 to varptr
  2893. rcall semi ;finish off and burn to flash
  2894.  
  2895. ret ;with variable created.
  2896. ;----------------------------------
  2897. considercode: ;having probs with findword going awol. Need another debug routine.
  2898. .ifdef livetesting
  2899. rcall CR
  2900. takemeout '[' ;just little mark for Id
  2901. rcall dhighR ;
  2902. ;Used when we've found a word.Starting at w(r24,25) length in r20. x points to space just past word.
  2903. ; u = r22,23
  2904. takemeout ']' ;just little mark for Id
  2905. .endif
  2906. ret
  2907. ;-------------------------
  2908. ifcode: ;classic IF
  2909. rcall tic
  2910. rcall zerobranch
  2911. rcall comma
  2912. rcall stackmyhere
  2913. rcall zero
  2914. rcall comma
  2915. ret ;with (rcall zerobranch, 0000) in dictionary in RAM
  2916. ;-------------------new parts to below----
  2917. housekeeping: ;cold start routines
  2918. ldi r16, 0xf9 ;PORTB setup
  2919. out DDRB,r16 ;
  2920. nop
  2921. ldi r16, $ff
  2922. out PORTB,r16
  2923. .IFDEF testing ;testing = simulating on avrstudio4
  2924. .ifndef firsttime ;just want to burn vars on first cold start
  2925. nop
  2926. rcall burneepromvars ;maybe a simple flag is better ?
  2927. .equ firsttime = 1
  2928. .endif
  2929.  
  2930. .ENDIF
  2931. clr STATE
  2932. rcall OK ;two OK}s mean cold start.
  2933. ldi xl,$a0
  2934. ldi xh,$01 ;point to ram VARS
  2935.  
  2936. clr r16
  2937. st x+,r16
  2938. st x+,r16 ;that's FBFlag mae 0.(ie use serialfill, not block fill)
  2939. st x+,r16 ;lower byte of FBPointer ie the 00 of $1c00.
  2940. ldi r16,$1c
  2941. st x+,r16 ;so now have $1c00 in FBPntr. Pnts to start of BLOCK.
  2942.  
  2943. ret ;with the housekeeping done
  2944. ;-------------------------------
  2945. blockfillcode: ; pull in one def from BLOCK at $1c00 (bytes)
  2946. rcall FBPtr ;now have $01a2, holds ptr to last 1K of flash, on stk
  2947. rcall fetch ;get ptr on stack. Start at $1c00 (bytes) in flash
  2948. mypop2 zh,zl ;point to first (or next) def with z
  2949. ldi xl,low(buf1)
  2950. ldi xh,high(buf1) ;x points to buffer, just like serial fill
  2951. upbfc:
  2952. lpm r16,z+ ;get char in BLOCK def
  2953. tst r16 ;it might be a zero, pad bytes have been added sometimes
  2954. brne downbfc ;get out if not a zero
  2955. gota0:
  2956. ldi r16,$20 ;if it's a zero, change it to a space
  2957. downbfc: ;TODO should really count chars and stop at,say,120
  2958. st x+,r16 ;flash byte now in AM buf1
  2959. cpi r16,$0d ;all defs end in CR. Got to end yet?
  2960. brne upbfc ;keep going if it's just a char != $0d.
  2961. mypush2 zl,zh ;finished so save pointer for next def
  2962. rcall FBPtr ;put $01a2 on stack, adr of ptr to last k defs
  2963. rcall store ;z-->FBPtr
  2964. clr STOP ;stop flag still going from last def or word
  2965. ret ;with one more def placed into buf from block. This gets interpreted in normal way.
  2966. ;--------------------------------------------
  2967. test_rs: ;test the rs. word that prints ram strings
  2968. rcall fillbuf
  2969. ldi r16,$60
  2970. clr r17
  2971. mypush2 r16,r17 ;pnt to buf1
  2972. ldi r16,10 ;len
  2973. mypush2 r16,r17
  2974. rcall rs
  2975. rcall qmark ;test qmark too
  2976. trs: rjmp trs
  2977. ;---------------------------
  2978. whatq: ;outputs word? when word not in dic and not a number
  2979. mypush2 r24,r25 ;adr of strange word during numberh
  2980. mypush r20 ;the len
  2981. clr r16
  2982. mypush r16 ;topup. Now have req (adr len --) on stack. To to call rs.
  2983. rcall rs
  2984. rcall qmark
  2985. ret
  2986. ;---------------------------------------
  2987. findfirstvarcode: ;( -- adr16) ;go down the dictionary finding first var,(bit6 of len set)
  2988. pushz
  2989. rcall dolatest
  2990. upffv:
  2991. rcall jmpNextWord
  2992. lpm r23,z+
  2993. lpm r22,z+ ;link for next word goes into r22,23 = v
  2994. ;lpm r16,z+
  2995. ;lpm r16,z+
  2996. lpm r16,z+ ;now point to len. Len in r16
  2997. sbrs r16,6
  2998. rjmp upffv ;if bit 6 is clear (not a var) go to up
  2999. andi r16, $0f ;mask off top nib to give real len
  3000. clc ;going to add
  3001. add zl,r16 ;step over name of var
  3002. inc zl
  3003. inc zl
  3004. brcc downffv ;maybe zl has over flowed
  3005. inc zh ;only if overflow
  3006. downffv:
  3007. lpm r16,z+ ;z points to ram adr after stackme2
  3008. lpm r17,z ;now have RAM adr of var eg $01a4
  3009. mypush2 r16,r17
  3010. popz
  3011. ret ;with ram adr of top var on mystack
  3012.  
  3013. ;------------------------------------------
  3014. strout: ; comes in dic like stackme-2 with structure assumptions. Should be followed by
  3015. ; len then a string of len chars. like this /strout/len/c c c c / other rcalls. Strout puts adr of
  3016. ; str on mstack and len then calls S. to print the string . It also makes reurn adr pnt to other.
  3017. pop zh ;hope we don't have to save z
  3018. pop zl ;check on order. Z now pnts to len
  3019. clc ;need to double z to get byte adr
  3020. rol zl
  3021. rol zh
  3022. lpm r16,z+
  3023. lpm r17,z+ ;r16,17 now have len. z points to str
  3024. mypush2 r16,r17 ;len on mystack
  3025. rcall dup ; ( l l --)
  3026. mypush2 zl,zh ; ( l l adr --) adr is of str /c c c ../ above
  3027. rcall dup ; ( l l adr adr --)
  3028. rcall rot ; ( l adr adr l --)
  3029. rcall plus ; ( l adr (adr+l) --) adr + l = adr of "other rcalls" above
  3030. rcall halve ;adr going onto ret stk needs to be word, not byte adr
  3031. brcc downstro ; clear carry means halve exact, not 00 padding bytes
  3032. rcall one
  3033. rcall plus ;add 1 to skip over padding byte of carry set by halve
  3034. downstro:
  3035. mypopa ; adr of other in r16,17. stk = ( l adr --)
  3036. push r16 ;check order
  3037. push r17 ; return adr now points to "other"
  3038. rcall swapp ; now ( adr l--) ready for next line
  3039. rcall Sdot ; print the string
  3040. ret ; after string print to other, just past the string
Advertisement
Add Comment
Please, Sign In to add comment