prjbrook

forth85_12B

Jul 23rd, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.49 KB | None | 0 0
  1. ;this is forth85_12B. Tidies up forth85_12
  2.  
  3.  
  4. .NOLIST
  5. .include "tn85def.inc"
  6. .LIST
  7. .LISTMAC ;sometimes macro code gets in way of clarity in listing
  8. .MACRO header
  9. .db high(@0), low(@0), @1, @2
  10. .ENDMACRO
  11. .MACRO mypop
  12. ld @0,-y
  13. .ENDMACRO
  14. .MACRO mypush
  15. st y+, @0
  16. .ENDMACRO
  17. .MACRO mypop2
  18. mypop @0
  19. mypop @1
  20. .ENDMACRO
  21. .MACRO mypush2
  22. mypush @0
  23. mypush @1
  24. .ENDMACRO
  25. .MACRO pushx
  26. push xl
  27. push xh
  28. .ENDMACRO
  29. .MACRO popx
  30. pop xh
  31. pop xl
  32. .ENDMACRO
  33. .MACRO pushz
  34. push zl
  35. push zh
  36. .ENDMACRO
  37. .MACRO popz
  38. pop zh
  39. pop zl
  40. .ENDMACRO
  41. .MACRO mypopa ;call r16,17 the accumulator a, ditto for r18,r19 for b
  42. mypop r17
  43. mypop r16
  44. .ENDMACRO
  45. .MACRO mypopb
  46. mypop2 r19,r18
  47. .ENDMACRO
  48.  
  49.  
  50.  
  51. .def FOUND = r15 ;if found=1 we have a match of Ram word on dictionary
  52. .def BOTTOM = r14 ;have hit the bottom of the dict and not found a match
  53. .def STOP = r13 ;stop interpreting line of words
  54. .def STATE = r12
  55. .def FOUNDCOUNTER = r11 ;dealWithWord clicks this if found =1. Counts successful finds in dictionary.
  56. .def SECONDLETTER =r10 ;helpful for debugging
  57. .def vl = r22
  58. .def vh = r23 ; u,v,w,x,y,z are all pointers
  59. .DSEG
  60. .ORG 0x60
  61.  
  62. ;consts: .DB "jksdafhsdf",8, 255, 0b01010101, -128, 0xaa
  63. .equ BUF1LENGTH = 128
  64. .equ eHERE = $0010 ;eeprom adr of system varial eHere
  65. .equ eLATEST = $0012
  66.  
  67. ;BUFFer revision!!
  68. buf1: .byte BUF1LENGTH ;input buffer. Lines max about 125
  69. buf2: .byte BUF1LENGTH ;this fits two flash buffers
  70. varSpace: .byte 64 ;might need more than 32 variables
  71. ;.org 0x1E0
  72. myStackStart: .byte 64 ;currently at $1a0.Meets return stack.
  73.  
  74. .cseg
  75. .ORG 0x800 ;dictionary starts at 4K (2K words) mark
  76. ;----------------------------------------------------
  77. one_1:
  78. .db 0,0,3, "one" ;code for one
  79. one:
  80. ; rcall stackme
  81. rcall stackme_2
  82. .db 01, 00
  83. ret
  84. ;----------------------------------------------
  85. two_1:
  86. header one_1, 3, "two"
  87. two:
  88. rcall stackme_2
  89. .db 02,00
  90. ret
  91. ;------------------------------------------
  92. dup_1:
  93. header two_1,3,"dup"
  94. dup:
  95. mypop r17
  96. mypop r16
  97. mypush r16
  98. mypush r17
  99. mypush r16
  100. mypush r17
  101.  
  102. ret
  103. ;-------------------------------------------
  104. drop_1:
  105. header dup_1,4,"drop"
  106. drop:
  107. mypop r17
  108. mypop r16 ;TODO what if stack pointer goes thru floor?
  109. ret
  110. ;----------------------------------
  111. swapp_1: ;twp p's becasue assembler recognizes avr opcode swap
  112. header drop_1,5, "swapp"
  113. swapp:
  114. mypop2 r17,r16
  115. mypop2 r19,r18
  116. mypush2 r16,r17
  117. mypush2 r18,r19
  118. ret
  119.  
  120.  
  121. ;-------------------------------------------------
  122. ;shift this later
  123.  
  124. S_1:
  125. ;the EOL token that gets put into end of buf1 to stop parsing
  126. header swapp_1,1,"S"
  127. S: ldi r16,02
  128. mov BOTTOM,r16 ;r14 =2 means a nice stop. EOL without errors
  129. clr STOP
  130. inc STOP ;set time-to-quit flag
  131. ret
  132. ;------------------------------------------
  133.  
  134. fetch_1: ;doesn't like label = @-1
  135. ;classic fetch. (adr -- num). Only in RAM
  136. header S_1,1,"@"
  137. fetch:
  138. pushx ;going to use x to point so better save
  139. mypop xh
  140. mypop xl
  141. ld r16,x+
  142. ld r17,x
  143. mypush r16
  144. mypush r17 ; and put them on my stack
  145. popx ;return with x intact and RAM val on my stack
  146. ret
  147. ;dddddddddddddddddddddddddddddddddddddddddddddddd
  148.  
  149. cfetch_1: ;doesn't like label = c@-1
  150. ;classic fetch. (adr -- num). Only in RAM. Do I want y to advance just one byte on mystack
  151. header fetch_1,2,"c@"
  152. cfetch:
  153. pushx ;going to use x to point so better save
  154. mypop xh
  155. mypop xl
  156. ld r16,x+
  157. mypush r16
  158. popx ;return with x intact and RAM val on my stack
  159. ret
  160. ;dddddddddddddddddddddddddddddddddddddddddddddddd
  161.  
  162. store_1: ;classic != "store"(adr num --) . Num is now at cell adr.
  163. header cfetch_1,1,"!"
  164. store:
  165. mypop2 r17,r16 ;there goes the num
  166. pushx
  167. mypop2 xh,xl ;there goes the address
  168. st x+,r16
  169. st x,r17 ;num goes to cell with location=adr
  170. popx
  171. ret
  172. ;ddddddddddddddddddddddddddddddddddddddddddddddddddd
  173.  
  174. cstore_1: ;classic c!= "store"(adr 8-bitnum --) . 8 bit Num is now at cell adr.
  175. header store_1,2,"c!"
  176. cstore:
  177. mypop r16 ;there goes the num. Just 8 bits at this stage.
  178. pushx
  179. mypop2 xh,xl ;there goes the address
  180. st x+,r16
  181. ; st x,r17 ;num goes to cell with location=adr
  182. popx
  183. ret
  184. ;------------------------------------
  185.  
  186. star_1: ;classic 16*16 mulitply (n n -- n*n)
  187. header cstore_1,1,"*"
  188. star:
  189. mypop2 r17,r16
  190. mypop2 r19,r18 ;now have both numbers in r16..r19
  191. rcall mpy16s ; multiply them. Result in r18..r21. Overflow in r20,21
  192. mypush2 r18,r19
  193. ret
  194. ;-----------------------------------------
  195.  
  196. slashMod_1: ;classic /MOD (n m -- n/m rem)
  197. header star_1,4,"/mod"
  198. slashMod:
  199. mypop2 r19,r18 ; that's m
  200. mypop2 r17,r16 ;that's n
  201. rcall div16s ;the the 16 by 16 bit divsion
  202. mypush2 r16,r17 ;answer ie n/m
  203. mypush2 r14,r15 ;remainder
  204. ret
  205. ;dddddddddddddddddddddddddddddddddddddddddddd
  206.  
  207. plus_1: ;classic + ( n n -- n+n)
  208. header slashMod_1,1,"+"
  209. plus:
  210. mypop2 r17,r16
  211. mypop2 r19,r18
  212. clc
  213. add r16,r18
  214. adc r17,r19
  215. mypush2 r16,r17
  216. ret
  217. ;--
  218.  
  219. minus_1: ;classic - ( n m -- n-m)
  220. header plus_1,1,"-"
  221. minus:
  222. mypop2 r19,r18
  223. mypop2 r17,r16
  224. clc
  225. sub r16,r18
  226. sbc r17,r19
  227. mypush2 r16,r17
  228. ret
  229. ;dddddddddddddddddddddddddddddddddddddddddd
  230.  
  231. pstore_1: ;expects eg. 0003 PORTB P! etc, "output 3 via PORTB"
  232. header minus_1,2, "p!"
  233. pstore:
  234. mypopb ;get rid of PORTB number, not used for tiny85, just one port
  235. mypopa ; this is used. it's eg the 003 = R16 above
  236. out PORTB,r16
  237. ret
  238. ;ddddddddddddddddddddddddd
  239.  
  240. portblabel_1:
  241. header pstore_1,5,"PORTB" ; note caps just a filler that point 0b in stack for dropping
  242. portblabel:
  243. ; Extend later on to include perhaps other ports
  244. ; one:
  245. ; rcall stackme
  246.  
  247. rcall stackme_2
  248. .db $0b, 00
  249. ret
  250. ;---------------------
  251.  
  252. datadirstore_1: ;set ddrb. invioked like this 000f PORTB dd! to make pb0..pb3 all outputs
  253. header portblabel_1, 3, "dd!"
  254. datadirstore:
  255. mypopb ; there goes useless 0b = PORTB
  256. mypopa ; 000f now in r17:16
  257. out DDRB,r16
  258. ret
  259. ;dddddddddddddddddddddddddddddddddddd
  260. ;sbilabel_1 ;set bit in PORTB. Just a kludge at this stage
  261. ;header datadirstore_1,3,"sbi" TODO sort out sbi and delay later. Now get on with compiler.
  262. ;first need store system vars in the eeprom. Arbitrarily 0010 is HERE and 0012 (in eeprom) is LATEST
  263. ;----------------------------------------
  264.  
  265. percentcstore_1: ;(adr16 n16 --) %c! stores stack val LSbyte to eeprom adr
  266. ; eg 10 00 1234 stores 34 to 0010 <--eeprom adr
  267. header datadirstore_1,3,"%c!"
  268. percentcstore:
  269. mypopa ;data. Lower byte into r16
  270. mypopb ;adr in r18,19
  271. rcall eewritebyte ;burn it into eeprom
  272. ret
  273. ;----------------------------------
  274.  
  275. percentstore_1: ; (adr16 n16 --) b16 stored at eeprom adr adr16 and adr16+1
  276. header percentcstore_1,2, "%!"
  277. percentstore:
  278. mypopa ;n16 into r16,17 as data
  279. mypopb ;adr in b=r18,19
  280. rcall eewritebyte ;burn low data byte
  281. clc
  282. inc r18
  283. brne outpcs
  284. inc r17 ;sets up adr+1 for next byte
  285. outpcs:
  286. mov r16,r17 ;r16 now conatins hi byte
  287. rcall eewritebyte
  288. ret
  289. ;-------------------------------
  290.  
  291. percentcfetch_1: ;(eepromadr16--char). Fetch eeprom byte at adr on stack
  292. header percentstore_1,3,"%c@"
  293. percentcfetch:
  294. mypopb ;adr now in r18,19
  295. rcall eereadbyte
  296. mypush r16 ; there's the char going on stack. Should be n16? Not n8?
  297. ret
  298. ;-------------------
  299.  
  300. percentfetch_1: ;(adr16--n16) get 16bits at adr and adr+1
  301. header percentcfetch_1,2,"%@"
  302. percentfetch:
  303. rcall percentcfetch ;low byte now on stack
  304. inc r18
  305. brcc downpf
  306. inc r19
  307. downpf:
  308. rcall eereadbyte ;there's the high byte hitting the mystack
  309. mypush r16
  310. ret
  311. ;-------------------------------
  312. gethere_1: ; leaves current value of eHERE on stack
  313. header percentfetch_1,7,"gethere"
  314. gethere:
  315. rcall stackme_2
  316. .dw eHere
  317. rcall percentfetch
  318. ret
  319. ;--------------------
  320. LATEST:
  321. getlatest_1: ;leaves current value of latest on stack
  322. header gethere_1,9, "getlatest"
  323. getlatest:
  324. rcall stackme_2
  325. .dw eLATEST ;the address of the latest link lives in eeprom at address 0012
  326. rcall percentfetch ;get the val out of eeprom
  327. ret
  328. ;------------------
  329. HERE:
  330. nop
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345. ;-------------------------------------------------
  346. stackme_2: ;stacks on my stack next 16bit num. Address of 16bit number is on SP-stack
  347. ; Used like this stackme_2 0034. Puts 0034 on myStack and increments past number on return stack.
  348. pop r17
  349. pop r16 ; they now contain eg 0x0804 which contain the 16bit num
  350. movw zl,r16 ;z now points to cell that cobtains the number
  351. clc
  352. rol zl
  353. rol zh ;double word address for z. lpm coming up
  354.  
  355.  
  356.  
  357. lpm r16,z+
  358. lpm r17,z+ ;now have 16bit number in r16,17
  359.  
  360. st y+,r16
  361. st y+, r17 ;mystack now contains the number
  362.  
  363. clc
  364. ror zh
  365. ror zl ;halve the z pointer to step past the number to return at the right place
  366.  
  367. push zl
  368. push zh
  369.  
  370. ret
  371.  
  372.  
  373.  
  374. ;====================================================================================================
  375.  
  376. .ORG 0
  377. rjmp start
  378. typein: .db "0012 abcd %! getlatest",$0d, "0013 %@",0x0d
  379. ;" one 0010 00ab %c! 0012 cdef %! 0013 %c@ 0013 %@ 0987 drop ", 0x0d
  380.  
  381. ;stackme dropx onex stackme swap drop",0x0d
  382. start:
  383. ldi r16, low(RAMEND)
  384. out SPL, r16
  385. ldi r16,high(RAMEND)
  386. out SPH, r16
  387.  
  388. ldi YL,low(myStackStart)
  389. ldi YH,high(myStackStart)
  390.  
  391. ;rjmp test_interpretLine
  392. ;rjmp test_cfetch
  393. ;rjmp test_store
  394. ;rjmp test_cstore
  395. ;rjmp test_mpy16s
  396. ;rjmp test_mpy16s0
  397. ;rjmp test_star
  398. ;rjmp test_div16s
  399. ;rjmp test_slashMod
  400. ;rjmp test_Hex4ToBin2
  401. ;rjmp test_interpretLine
  402. rjmp setupforflashin
  403.  
  404. rjmp start
  405.  
  406. getline0: ;force a line into buf1 via flash string. Simulates GETLINE
  407. ldi zl, low(typein<<1)
  408. ldi zh, high(typein<<1)
  409. ldi xl, low(buf1)
  410. ldi xh, high(buf1)
  411. type0:
  412. lpm r16,Z+
  413. st x+,r16
  414. cpi r16,0x0d ;have we got to the end of the line?
  415. brne type0
  416. ret
  417. ;--------------------------------------------
  418. ;WORD gets x to point to start of word (copy in w=r24,25) with the length in len = r20
  419. ;assume word points to somewhere in buf1. Should advance thru spaces=0x20 to first real char
  420. word: ;maybe give it a header later
  421. ld r16,x+ ;get char
  422. ld SECONDLETTER, x ;for debugging
  423.  
  424. cpi r16,0x20 ;is it a space?
  425. breq word ;if so get next char
  426. ;if here we're point to word start. so save this adr in w
  427. mov r24,xl
  428. mov r25,xh ;wordstart now saved in w
  429.  
  430.  
  431. clr r20 ;length initially 0
  432. nextchar:
  433. inc r20 ;r20 = word length
  434. ld r16,x+ ;get next char
  435. cpi r16,0x20
  436. brne nextchar
  437. dec r24 ;adjust start of word
  438. ;if here we've found a word.Starting at w length in r20.x points to space just past word
  439. ret
  440. ;----------------------------------------
  441.  
  442. 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.
  443. ; and the word in buf1 is pointed to by w=r24,25. len = r20. Z on entry points to the link. Needs +2 to
  444. lpm r23,z+
  445. lpm r22,z+ ;store next link in v=r22,23. z now points to len byte
  446.  
  447. startc:
  448. push r20 ;save length
  449. lpm r16,Z+ ;length of dictionary word, first entry now in r16
  450. cp r16,r20 ;same lengths?
  451. brne outcom ;not = so bail out
  452. ;if here the words are the same length, what about the rest of the chars.First get x to point to word.
  453. mov xl,r24
  454. mov xh,r25 ;x now point to start of buf1 word
  455. upcom:
  456. lpm r16,z+
  457. ld r17,x+ ;get one corresponding char from each word
  458. cp r16,r17 ;same word?
  459. brne outcom ;bail out if chars are different
  460. dec r20 ;count chars
  461. brne upcom ;still matching and not finished so keep going
  462. ;if here r20 is 0 so match must have been perfect so FOUND = 1
  463. clr FOUND
  464. inc FOUND
  465. outcom:
  466. pop r20 ;get old lngth of buf1 word back
  467. ret
  468. ;-------------------------------------------
  469. jmpNextWord: ;go to next word in the dictionary. Assume v=r22,23 contains next link word(not byte)
  470. ; and w = r24,25 contains RAM word start with len in r20
  471. ;exit with z pointing to next word ready for next COMPARE.
  472. clc
  473. rol r22
  474. rol r23 ;above 3 instructions change word address into byte address by doubling
  475. movw r30,r22 ;z now points to next word
  476. ret
  477. ;-----------------------------------------
  478.  
  479. doLatest: ;set up so first jump in dictionary is to top=LATEST and other flags set up.
  480. ldi vl, low(LATEST)
  481. ldi vh, high(LATEST)
  482. clr FOUND
  483. clr BOTTOM ;not yet found the match, not yet at the bottom. Either will stop search.
  484. clr STOP ;keep parsing words til this goes to a 1
  485. ret
  486. ;---------------------------------------------
  487. interpretLine: ;given line of words in buf one, search for words one by one. Don't do code
  488. ; or compile at this stage, just find and report that and go into next one.
  489. rcall getline0 ;change later to real getline via terminal
  490. rcall pasteEOL
  491. ldi xl, low(buf1)
  492. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  493. clr FOUNDCOUNTER ;counts finds in line parsing.
  494.  
  495. nextWord:
  496. tst STOP
  497. brne stopLine
  498. rcall word
  499. rcall findWord ;not done yet
  500. rcall dealWithWord ;go and run code STATE=0, or compile (STATE =1).{ c0de, comp1le}
  501. rjmp nextWord
  502. stopLine:
  503. ret
  504. ;-----------------------------------------------------------------
  505. findWord:
  506. rcall doLatest
  507. upjmpf:
  508. rcall jmpNextWord
  509. rcall compare
  510. tst FOUND
  511. brne stopsearchf ;if last compare got a match (FOUND=1) then stop searching
  512. tst vl
  513. brne upjmpf ;if v=0000 then we've hit the bottom of the dictionary
  514. tst vh
  515. brne upjmpf ;not found and not at bottom so keep going
  516. ;if here FOUND =0, ie no match yet and we've hit the bottom of the dictionary
  517. clr BOTTOM
  518. inc BOTTOM ;exit with FOUND=0 and BOTTOM =1
  519. stopsearchf: nop
  520. ret
  521. ;----------------------------
  522. test_interpretLine:
  523. rcall interpretLine
  524. til: rjmp til ;** with r24 pointing to 'S' and FOUND = r15 =1
  525. ;------------------------------
  526. dealWithWord: ;come here when it's time to compile or run code
  527. ;Good debugging spot. Enter here with Z pointing to CFA of word found. Y points to myStack. X points to just
  528. ; 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
  529. ; to the next word in dic. Either just below the found word or 0000 if we get to the bottome with no match
  530. ;
  531. nop
  532. tst FOUND
  533. breq notfound
  534. inc FOUNDCOUNTER
  535.  
  536. ;want to hop over filler bytes,0's added to keep codes on even byte boundaries
  537. ; so if r30 is odd at this stage inc it. odd is lsbit = 1.
  538. sbrs r30,0 ;skip next instruction if final bit lsb = 1
  539. rjmp downdw
  540. ;if here lsb = 1 so we're on a padding byte and have to add 1 to get to a 2 byte boundary
  541. inc r30
  542. brcc downdw
  543. inc r31 ;add one to z before converting to bytes
  544.  
  545. downdw:
  546. clc
  547. ror zh
  548. ror zl ;put z back into word values
  549.  
  550.  
  551. rcall executeCode
  552.  
  553.  
  554.  
  555. .MESSAGE "Word found"
  556. rjmp outdww
  557. notfound:
  558. nop
  559. ; .MESSAGE "Word not found"
  560. ; clr STOP
  561. ; inc STOP ;stop parsing line
  562. rcall numberh ; word not in dict so must be a number? Form = HHHH
  563. ;now have to add 3 to x so it points past this word ready not next one
  564. clc
  565. inc r26
  566. inc r26
  567. inc r26
  568. brcc outdww
  569. inc r27 ;but only if overflow
  570. nop
  571. outdww:
  572. ret ;with STOP =1 in not a number
  573. ;------------------------------------------------------------------------
  574. pasteEOL: ;when a line of text is TYPEd into buf1 it should end with CR=$0d. This gets replaced with ]}, a
  575. ; special end of line word. When the word is invoked it casues a QUIT back to the waiting for input stage.
  576. ; Start at buf1 start and inspect each char for a $0D. When found replace with a "$20 S $20 "
  577. ldi xl, low(buf1)
  578. ldi xh, high(buf1) ;pnt to start of buffer
  579. clr r17
  580. nxtChar:
  581. inc r17 ;r17 is counter. Bail out when r17 > BUF1LENGTH
  582. cpi r17, BUF1LENGTH -3
  583. breq outProb
  584. ld r16, x+
  585. cpi r16, $0d
  586. brne nxtChar
  587. ;if here we've found a $0d in buf1 before the end, so replace with an EOL token. x points to just after it.
  588. ldi r16,$20
  589. st -x, r16 ;back up. Then go forward.
  590. ; ldi r16, ']'
  591. st x+, r16
  592. ldi r16,'S'
  593. st x+, r16
  594. ; ldi r16, '}'
  595. ; st x+, r16
  596. ldi r16, $20
  597. st x, r16
  598. rjmp outpel
  599.  
  600.  
  601. outProb:
  602. nop
  603. .MESSAGE "Couldn't find $0d"
  604. outpel:
  605. ret
  606.  
  607. ;-------------------------------------
  608. executeCode: ;with Z pointing to cfa. Not sure whether to jmp or call
  609.  
  610. ijmp
  611. ret
  612. ;---------------------------------------
  613. test_fetch: ;do run thru of @
  614. rcall getline0 ;change later to real getline via terminal
  615. rcall pasteEOL
  616. ldi xl, low(buf1)
  617. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  618.  
  619. ldi r16,$62
  620. mypush r16
  621. ldi r16,$0
  622. mypush r16 ;should now have adr $0062 on mystack
  623. rcall fetch
  624. tf1:
  625. rjmp tf1
  626. ;---------------------------------
  627. test_cfetch: ;do run thru of @
  628. rcall getline0 ;change later to real getline via terminal
  629. rcall pasteEOL
  630. ldi xl, low(buf1)
  631. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  632.  
  633. ldi r16,$62
  634. mypush r16
  635. ldi r16,$0
  636. mypush r16 ;should now have adr $62 on mystack
  637. rcall cfetch
  638. tcf1:
  639. rjmp tcf1
  640. ;----------------------------
  641. test_store:
  642. rcall getline0 ;change later to real getline via terminal
  643. rcall pasteEOL
  644. ldi xl, low(buf1)
  645. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  646. ldi r16,$62
  647. ldi r17,$0
  648. mypush2 r16,r17 ;should now have adr $62 on mystack
  649. ldi r16, $AB
  650. ldi r17, $CD
  651. mypush2 r16,r17 ;now have $ABCD on mystack
  652. rcall store
  653. ts1:
  654. rjmp ts1
  655. ;------------------------
  656. test_cstore:
  657. rcall getline0 ;change later to real getline via terminal
  658. rcall pasteEOL
  659. ldi xl, low(buf1)
  660. ldi xh,high(buf1) ;last 3 statemnts are done onece. Now the main loop.
  661. ldi r16,$62
  662. ldi r17,$0
  663. mypush2 r16,r17 ;should now have adr $62 on mystack
  664. ldi r16, $AB
  665. ; ldi r17, $CD
  666. mypush r16 ;now have $ABCD on mystack
  667. rcall cstore
  668.  
  669. ts11:
  670. rjmp ts11
  671. ;Now put arith routines here. Are from AVR200. Just using 16*16 for * but get 32bit result.
  672.  
  673.  
  674. ;***************************************************************************
  675. ;*
  676. ;* "mpy16s" - 16x16 Bit Signed Multiplication
  677. ;*
  678. ;* This subroutine multiplies signed the two 16-bit register variables
  679. ;* mp16sH:mp16sL and mc16sH:mc16sL.
  680. ;* The result is placed in m16s3:m16s2:m16s1:m16s0.
  681. ;* The routine is an implementation of Booth's algorithm. If all 32 bits
  682. ;* in the result are needed, avoid calling the routine with
  683. ;* -32768 ($8000) as multiplicand
  684. ;*
  685. ;* Number of words :16 + return
  686. ;* Number of cycles :210/226 (Min/Max) + return
  687. ;* Low registers used :None
  688. ;* High registers used :7 (mp16sL,mp16sH,mc16sL/m16s0,mc16sH/m16s1,
  689. ;* m16s2,m16s3,mcnt16s)
  690. ;*
  691. ;***************************************************************************
  692.  
  693. ;***** Subroutine Register Variables
  694.  
  695. .def mc16sL =r16 ;multiplicand low byte
  696. .def mc16sH =r17 ;multiplicand high byte
  697. .def mp16sL =r18 ;multiplier low byte
  698. .def mp16sH =r19 ;multiplier high byte
  699. .def m16s0 =r18 ;result byte 0 (LSB)
  700. .def m16s1 =r19 ;result byte 1
  701. .def m16s2 =r20 ;result byte 2
  702. .def m16s3 =r21 ;result byte 3 (MSB)
  703. .def mcnt16s =r22 ;loop counter
  704.  
  705. ;***** Code
  706. mpy16s: clr m16s3 ;clear result byte 3
  707. sub m16s2,m16s2 ;clear result byte 2 and carry
  708. ldi mcnt16s,16 ;init loop counter
  709. m16s_1: brcc m16s_2 ;if carry (previous bit) set
  710. add m16s2,mc16sL ; add multiplicand Low to result byte 2
  711. adc m16s3,mc16sH ; add multiplicand High to result byte 3
  712. m16s_2: sbrc mp16sL,0 ;if current bit set
  713. sub m16s2,mc16sL ; sub multiplicand Low from result byte 2
  714. sbrc mp16sL,0 ;if current bit set
  715. sbc m16s3,mc16sH ; sub multiplicand High from result byte 3
  716. asr m16s3 ;shift right result and multiplier
  717. ror m16s2
  718. ror m16s1
  719. ror m16s0
  720. dec mcnt16s ;decrement counter
  721. brne m16s_1 ;if not done, loop more
  722. ret
  723. ;----------------------------------------------------------
  724. ;***** Multiply Two Signed 16-Bit Numbers (-12345*(-4321))
  725. test_mpy16s:
  726. ldi mc16sL,low(-12345)
  727. ldi mc16sH,high(-12345)
  728. ldi mp16sL,low(-4321)
  729. ldi mp16sH,high(-4321)
  730. rcall mpy16s ;result: m16s3:m16s2:m16s1:m16s0
  731. ;=$032df219 (53,342,745)
  732. tmpy: rjmp tmpy
  733.  
  734. test_mpy16s0:
  735. ldi mc16sL,low(123)
  736. ldi mc16sH,high(123)
  737. ldi mp16sL,low(147)
  738. ldi mp16sH,high(147)
  739. rcall mpy16s ;result: m16s3:m16s2:m16s1:m16s0
  740. tmpy0: rjmp tmpy0
  741. ;-----------------------
  742. test_star:
  743. ldi r16,-$7b
  744. mypush r16
  745. ldi r16,$00
  746. mypush r16 ;that's decimal 123 on stack
  747. ldi r16,$93
  748. mypush r16
  749. ldi r16,$00
  750. mypush r16 ; and thats dec'147
  751. rcall star
  752. tsr: rjmp tsr
  753.  
  754. ;--------------------------
  755. ;***************************************************************************
  756. ;*
  757. ;* "div16s" - 16/16 Bit Signed Division
  758. ;*
  759. ;* This subroutine divides signed the two 16 bit numbers
  760. ;* "dd16sH:dd16sL" (dividend) and "dv16sH:dv16sL" (divisor).
  761. ;* The result is placed in "dres16sH:dres16sL" and the remainder in
  762. ;* "drem16sH:drem16sL".
  763. ;*
  764. ;* Number of words :39
  765. ;* Number of cycles :247/263 (Min/Max)
  766. ;* Low registers used :3 (d16s,drem16sL,drem16sH)
  767. ;* High registers used :7 (dres16sL/dd16sL,dres16sH/dd16sH,dv16sL,dv16sH,
  768. ;* dcnt16sH)
  769. ;*
  770. ;***************************************************************************
  771.  
  772. ;***** Subroutine Register Variables
  773.  
  774. .def d16s =r13 ;sign register
  775. .def drem16sL=r14 ;remainder low byte
  776. .def drem16sH=r15 ;remainder high byte
  777. .def dres16sL=r16 ;result low byte
  778. .def dres16sH=r17 ;result high byte
  779. .def dd16sL =r16 ;dividend low byte
  780. .def dd16sH =r17 ;dividend high byte
  781. .def dv16sL =r18 ;divisor low byte
  782. .def dv16sH =r19 ;divisor high byte
  783. .def dcnt16s =r20 ;loop counter
  784.  
  785. ;***** Code
  786.  
  787. div16s: mov d16s,dd16sH ;move dividend High to sign register
  788. eor d16s,dv16sH ;xor divisor High with sign register
  789. sbrs dd16sH,7 ;if MSB in dividend set
  790. rjmp d16s_1
  791. com dd16sH ; change sign of dividend
  792. com dd16sL
  793. subi dd16sL,low(-1)
  794. sbci dd16sL,high(-1)
  795. d16s_1: sbrs dv16sH,7 ;if MSB in divisor set
  796. rjmp d16s_2
  797. com dv16sH ; change sign of divisor
  798. com dv16sL
  799. subi dv16sL,low(-1)
  800. sbci dv16sL,high(-1)
  801. d16s_2: clr drem16sL ;clear remainder Low byte
  802. sub drem16sH,drem16sH;clear remainder High byte and carry
  803. ldi dcnt16s,17 ;init loop counter
  804.  
  805. d16s_3: rol dd16sL ;shift left dividend
  806. rol dd16sH
  807. dec dcnt16s ;decrement counter
  808. brne d16s_5 ;if done
  809. sbrs d16s,7 ; if MSB in sign register set
  810. rjmp d16s_4
  811. com dres16sH ; change sign of result
  812. com dres16sL
  813. subi dres16sL,low(-1)
  814. sbci dres16sH,high(-1)
  815. d16s_4: ret ; return
  816. d16s_5: rol drem16sL ;shift dividend into remainder
  817. rol drem16sH
  818. sub drem16sL,dv16sL ;remainder = remainder - divisor
  819. sbc drem16sH,dv16sH ;
  820. brcc d16s_6 ;if result negative
  821. add drem16sL,dv16sL ; restore remainder
  822. adc drem16sH,dv16sH
  823. clc ; clear carry to be shifted into result
  824. rjmp d16s_3 ;else
  825. d16s_6: sec ; set carry to be shifted into result
  826. rjmp d16s_3
  827.  
  828. ;-----------------------------------------------
  829.  
  830. test_div16s:
  831. ;***** Divide Two Signed 16-Bit Numbers (-22,222/10)
  832. ldi dd16sL,low(-22222)
  833. ldi dd16sH,high(-22222)
  834. ldi dv16sL,low(10)
  835. ldi dv16sH,high(10)
  836. rcall div16s ;result: $f752 (-2222)
  837. ;remainder: $0002 (2)
  838.  
  839. forever:rjmp forever
  840. ;----------------------------------
  841. test_slashMod:
  842. ldi r16,$12
  843. mypush r16
  844. ldi r16,$34
  845. mypush r16
  846. ldi r16,$56 ;NB this is $3412 not $1234
  847. mypush r16
  848. ldi r16,$00
  849. mypush r16
  850. rcall slashMod ;$3412 / $56 = $9b rem 0 works
  851. tslm: rjmp tslm
  852.  
  853. ;---------------------------------------
  854. ;From http://www.avr-asm-tutorial.net/avr_en/calc/CONVERT.html#hex2bin
  855. ; Hex4ToBin2
  856. ; converts a 4-digit-hex-ascii to a 16-bit-binary
  857. ; In: Z points to first digit of a Hex-ASCII-coded number
  858. ; Out: T-flag has general result:
  859. ; T=0: rBin1H:L has the 16-bit-binary result, Z points
  860. ; to the first digit of the Hex-ASCII number
  861. ; T=1: illegal character encountered, Z points to the
  862. ; first non-hex-ASCII character
  863. ; Used registers: rBin1H:L (result), R0 (restored after
  864. ; use), rmp
  865. ; Called subroutines: Hex2ToBin1, Hex1ToBin1
  866.  
  867. .def rBin1H =r17
  868. .def rBin1L = r16
  869. .def rmp = r18
  870. ;
  871. Hex4ToBin2:
  872. clt ; Clear error flag
  873. rcall Hex2ToBin1 ; convert two digits hex to Byte
  874. brts Hex4ToBin2a ; Error, go back
  875. mov rBin1H,rmp ; Byte to result MSB
  876. rcall Hex2ToBin1 ; next two chars
  877. brts Hex4ToBin2a ; Error, go back
  878. mov rBin1L,rmp ; Byte to result LSB
  879. sbiw ZL,4 ; result ok, go back to start
  880. Hex4ToBin2a:
  881. ret
  882. ;
  883. ; Hex2ToBin1 converts 2-digit-hex-ASCII to 8-bit-binary
  884. ; Called By: Hex4ToBin2
  885. ;
  886. Hex2ToBin1:
  887. push R0 ; Save register
  888. rcall Hex1ToBin1 ; Read next char
  889. brts Hex2ToBin1a ; Error
  890. swap rmp; To upper nibble
  891. mov R0,rmp ; interim storage
  892. rcall Hex1ToBin1 ; Read another char
  893. brts Hex2ToBin1a ; Error
  894. or rmp,R0 ; pack the two nibbles together
  895. Hex2ToBin1a:
  896. pop R0 ; Restore R0
  897. ret ; and return
  898. ;
  899. ; Hex1ToBin1 reads one char and converts to binary
  900. ;
  901. Hex1ToBin1:
  902. ld rmp,z+ ; read the char
  903. subi rmp,'0' ; ASCII to binary
  904. brcs Hex1ToBin1b ; Error in char
  905. cpi rmp,10 ; A..F
  906. brcs Hex1ToBin1c ; not A..F
  907. cpi rmp,$30 ; small letters?
  908. brcs Hex1ToBin1a ; No
  909. subi rmp,$20 ; small to capital letters
  910. Hex1ToBin1a:
  911. subi rmp,7 ; A..F
  912. cpi rmp,10 ; A..F?
  913. brcs Hex1ToBin1b ; Error, is smaller than A
  914. cpi rmp,16 ; bigger than F?
  915. brcs Hex1ToBin1c ; No, digit ok
  916. Hex1ToBin1b: ; Error
  917. sbiw ZL,1 ; one back
  918. set ; Set flag
  919. Hex1ToBin1c:
  920. ret ; Return
  921. ;--------------------------------------
  922. test_Hex4ToBin2:
  923. pushz
  924. ldi zl,$60
  925. clr zh ;z now points to start of buf1
  926. ldi r16,'0'
  927. st z+,r16
  928. ldi r16,'f'
  929. st z+,r16
  930. ldi r16,'2'
  931. st z+,r16
  932. ldi r16,'3'
  933. st z+,r16
  934. ldi zl,$60
  935. clr zh ;z now points back to start of buf1
  936. rcall Hex4ToBin2
  937. popz
  938. th4: rjmp th4
  939. ;-------------------------------------
  940. numberh: ;word not in dictionary. Try to convert it to hex.
  941. pushz ;algorithm uses z, pity
  942. movw zl,r24 ;r4,25 = w holds start of current word
  943. ;z now points eg to '12ab'start. If t=0 then it coverts to real hex
  944. rcall hex4ToBin2 ;try to convert
  945. ;above call needs 4 hex digits to emerge with t=0 and binary in r16,17
  946. ;want this. If t=0 stack r16,17 and carry on interpreting, else emerge with
  947. ; t=1 and zpointing to first problem char
  948. brtc gotHex
  949. ; if here there's a problem that z is pointing to. Bail out of interpret line
  950. clr STOP
  951. inc STOP
  952. rjmp outnh
  953.  
  954. gotHex: ;sucess.Real hex in r16,17
  955. mypush2 r16,r17 ; so push num onto mystack
  956. outnh:
  957. popz ; but will it be pointing to "right"place in buf1? Yes now OK
  958.  
  959. ret
  960. ; numberh not working fully, ie doesn't point to right place after action.
  961. ; also no action if not a number? DONE better save this first.
  962. ;---------------------------------
  963. ;eeroutines
  964. eewritebyte: ;write what's in r16 to eeprom adr in r18,19
  965. sbic EECR,EEPE
  966. rjmp eewritebyte ;keep looping til ready to write
  967. ;if here the previous write is all done and we can write the next byte to eeprom
  968. out EEARH,r19
  969. out EEARL,r18 ;adr done
  970. out EEDR,r16 ;byte in right place now
  971. sbi EECR,EEMPE
  972. sbi EECR,EEPE ;last 2 instruc write eprom. Takes 3.4 ms
  973. ret
  974. ;test with %!
  975. ;---------------------------------
  976. eereadbyte: ; read eeprom byte at adr in r18,19 into r16
  977. ; Wait for completion of previous write
  978. sbic EECR,EEPE
  979. rjmp eereadbyte
  980. ; Set up address (r18:r17) in address register
  981. out EEARH, r19
  982. out EEARL, r18
  983. ; Start eeprom read by writing EERE
  984. sbi EECR,EERE
  985. ; Read data from data register
  986. in r16,EEDR
  987. ret
  988. ;------------------------------
  989. setupforflashin: ;using here etc get appropriate page, offset,myhere values.
  990. ldi r16,low(HERE)
  991. ldi r17,high(HERE)
  992. mypush2 r16,r17
  993. rcall stackme_2
  994. .dw 0002
  995. rcall star ;now have current HERE in bytes in flash. But what is myhere?
  996. rcall stackme_2
  997. .db $0040 ;64 bytes per page
  998. rcall slashMod
  999. ;offset on top pagenum under. eg pg 0047, offset 0012
  1000. mypop2 r9,r8 ;store offset (in bytes)
  1001. rcall stackme_2
  1002. .db $0040
  1003. rcall star ;pgnum*64 = byte adr of start of flash page
  1004. mypop2 r7,r6
  1005. mypush2 r8,r9 ;push back offset
  1006. rcall stackme_2
  1007. .dw buf2
  1008. nop
  1009.  
  1010. outsufi: rjmp outsufi
  1011. ;-----------------------------------
  1012. burneeprmvars: ;send latest versions of eHERE and eLATEST to eeprom
  1013. ldi r16,low(HERE)
  1014. ldi r17,high(HERE)
  1015. mypush2 r16,r17
  1016. ;up top we have .equ eHERE = $0010
  1017. ldi r16,low(eHERE)
  1018. ldi r17,high(eHERE)
  1019. mypush2 r16,r17
  1020. ;now have n16 eadr on stack ready for e!
  1021. rcall percentstore
  1022. ret
Advertisement
Add Comment
Please, Sign In to add comment