prjbrook

forth85_45. Getting slimmer

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