prjbrook

forth85_27. Variables OK. Sort of ...

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