prjbrook

forth85_44 Last bloated one. Usi serial fast.

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