prjbrook

forth85_36. For..next,forget

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