prjbrook

forth85_42. Old routines OK with new serial pins

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