prjbrook

forth85_37. constant,mask and bits

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