home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tcom96 / samples / down.seq < prev    next >
Encoding:
Text File  |  1991-03-19  |  10.7 KB  |  353 lines

  1. \ DOWN.SEQ      Boot downloader, located at 200h
  2.  
  3. long_branch
  4. long_library
  5.  
  6. registers       \ by default this starts at $24
  7.  
  8. 2variable       DPOINT          \ pointers and counters
  9. variable        DCOUNT          \ for display block move
  10. variable        ROW_COUNTER
  11.  
  12. variable        SP_FLAGS        \ serial port status image
  13.  
  14. $200 cseg down-cseg
  15. down-cseg
  16.  
  17. : MAIN
  18.         BEGIN
  19.                 SP_POLL
  20.                 AGAIN
  21.         ;
  22.  
  23.  
  24. \ Serial port support
  25.  
  26. $0D CONSTANT HI_THERE   \ enquiry from PC
  27. '!' constant IM_HERE    \ response to PC
  28. $0AF constant LISTEN_UP \ wake-up request from PC
  29. $0FA constant GO_AHEAD  \ response to PC's LISTEN-UP
  30.  
  31. 9600 constant BAUD                                      \ actual baud rate
  32. 7500 constant BAUD_VAL                                  \ 1/(BAUD/100)
  33. BAUD_VAL 1- 255 AND             constant BAUD_L         \ baud value to set
  34. BAUD_VAL 1- 256 / $80 OR        constant BAUD_H
  35.  
  36. $49     constant SP_SETUP        \ enable transmitter, set mode=1
  37. 3       constant TXE_BIT         \ transmit empty bit
  38. 8       constant TXE_MASK
  39. TXE_MASK not constant NOT.TXE_MASK
  40. 6       constant RI_BIT          \ receive interrupt bit
  41. $40     constant RI_MASK
  42. RI_MASK not constant NOT.RI_MASK
  43. $21     constant IOC1_SETUP      \ to enable serial port and PWM
  44.  
  45. order
  46. LABEL SP_SEND?L \ ready to send?
  47.                 \ usage    1 $: CALL    SP_SEND?L
  48.                 \               JNC     1 $
  49. order           \               CALL    SP_SENDL
  50.                 \
  51.                 \ returns with W1 = SP_FLAGS
  52.                 \
  53.         LDB     W1  SP_STAT             \ get the status port
  54.         ORB     W1  SP_FLAGS            \ and the remembered flags
  55.         STB     W1  SP_FLAGS            \ remember them again
  56.         BITSET  W1 TXE_BIT IF  SETC  THEN
  57.         RET
  58.         end-code                \ return with C set if ready
  59.  
  60. LABEL SP_SENDL  \ send the byte in W0
  61.         LD      W2  0
  62. 1 $:
  63.         DEC     W2
  64.         JE      2 $             \ timeout in case transmitter broken
  65.         CALL    SP_SEND?L
  66.         JNC     1 $             \ wait for transmitter ready
  67.         LDB     SBUF W0         \ send the byte
  68.  
  69.         ANDB    W1  # NOT.TXE_MASK
  70.         STB     W1  SP_FLAGS            \ update the remembered flags
  71.         RET
  72. 2 $:
  73.         LDB     SBUF  W0        \ if timed out send the byte, anyway
  74.         RET
  75.         end-code
  76.  
  77. CODE SP_SEND ( c -- ) \ send one byte for hi-level
  78.         LD      W0 TTOS
  79.         CALL    SP_SENDL
  80.         LOAD_TTOS
  81.         RET
  82.         end-code
  83.  
  84. LABEL SP_RX?L   \ something received?
  85.                 \ usage    1 $: CALL    SP_RX?L
  86.                 \               JNC     1 $
  87.                 \               CALL    SP_RXL
  88.                 \
  89.                 \ returns with W1 = SP_FLAGS
  90.                 \
  91.                 \ changes W1 and SP_FLAGS
  92.                 \
  93.         LDB     W1  SP_STAT             \ get the status port
  94.         ORB     W1  SP_FLAGS            \ and the remembered flags
  95.         STB     W1  SP_FLAGS            \ remember them again
  96.         BITSET  W1 RI_BIT IF  SETC  THEN
  97.         RET             \ return with C set if something received
  98.         END-CODE
  99.  
  100. CODE SP_RX? ( -- f ) \ see if something received for hi-level
  101.         SAVE_TTOS
  102.         CLR TTOS
  103.         CALL    SP_RX?L
  104.         C0<> IF   DEC TTOS   THEN     \ FFFF on stack if something to pick up
  105.         RET
  106.         end-code
  107.  
  108. LABEL SP_RXL ( -- C ) \ get received byte, in W0, for code definitions
  109.         BEGIN   CALL SP_RX?L
  110.                 C0<> UNTIL              \ wait for something to get
  111.         LDB     W0 SBUF                 \ get the byte
  112.         ANDB    W1  # NOT.RI_MASK
  113.         STB     W1  SP_FLAGS            \ update the remembered flags
  114.         RET
  115.         end-code
  116.  
  117. CODE SP_RX ( -- c ) \ get recieved byte for high-level
  118.         SAVE_TTOS
  119.         CALL    SP_RXL
  120.         LDB     TTOS W0
  121.         CLRB    TTOSH
  122.         RET
  123.         end-code
  124.  
  125.  
  126. LABEL SP_RX_TL ( -- )  \ get received byte, or timeout after 10ms
  127.                 \ on exit, W0 = received byte
  128.                 \          if timed out, W2 = 0 and Z flag is true
  129.                 \          if not timed out W2 <> 0 and Z flag is false
  130.         LD      W2  # 900
  131. 3 $:                   \ ( approx. 58 states in loop, = 10 usec. )
  132.         DEC     W2              \ decrement timeout counter
  133.         JE      4 $             \ go exit if timed out
  134.         CALL    SP_RX?L
  135.         JNC     3 $             \ wait for item in receiver
  136.         CALL    SP_RXL          \ get item from receiver
  137. 4 $:    RET     end-code
  138.  
  139.  
  140. CODE SP_RX_T ( -- C )  \ get received byte, or timeout after 10ms
  141.                 \          if timed out TTOS word = -1
  142.                 \          if not timed out TTOSH = 0 and TTOS has the byte
  143.         SAVE_TTOS
  144.         LD      W2  # 900
  145. 3 $:                   \ ( approx. 58 states in loop, = 10 usec. )
  146.         DEC     W2              \ decrement timeout counter
  147.         JE      4 $             \ go exit if timed out
  148.         CALL    SP_RX?L
  149.         JNC     3 $             \ wait for item in receiver
  150.         CALL    SP_RXL          \ get item from receiver
  151.         LDB     TTOS W0
  152.         CLRB    TTOSH           \ return the received byte on the stack
  153.         RET
  154. 4 $:    LD      TTOS # -1       \ otherwise put -1 on the stack
  155.         RET
  156.         end-code
  157.  
  158. CODE SP_POLLC   \ serial port poll
  159.         DI
  160.         CALL    SP_RX?L                 \ see if there's something received
  161.         JNC     2 $                     \ if not, return to background
  162.         CALL    SP_RXL                  \ get received byte
  163.         CMPB    W0  # HI_THERE          \ see if it is a "hi-there"
  164.         JNE     1 $
  165.  
  166.         LD      W0  # IM_HERE            \ if so  respond with a "i'm here"
  167.         CALL    SP_SENDL
  168.         SJMP    2 $
  169. 1 $:
  170.         CMPB    W0  # LISTEN_UP         \ see if it is a request
  171.         JNE     2 $                     \ if not, return to background
  172.         LD      W0  # GO_AHEAD
  173.         CALL    SP_SENDL                \ send "I'm here" byte
  174.         RET                     \ and let high-level do the rest
  175.  
  176. 2 $:|   POP     W0      \ exit from the word that called SP_POLLC
  177.                         \  if there's no command to do
  178.         RET
  179.         end-code
  180.  
  181. LABEL SP_POLL_L  END-CODE       \ entry to SP_POLL from a code word
  182. : SP_POLL
  183.         SP_POLLC        \ It may exit SP_POLL from within SP_POLLC
  184.         SP_RX_T
  185.         DUP 0< IF  DROP EXIT  THEN   \ exit if it timed out
  186. \        DUP 30 >= IF  DROP EXIT  THEN   \ exit if it is too big
  187.         exec:
  188. \ table of function subroutines
  189. \ 0
  190.         JUMP_TO
  191.         NULL_FUNCT
  192.         NULL_FUNCT      \        MEASURE_ALL
  193.         WRITE_BYTES
  194.         READ_BYTES
  195. \ 5
  196.         NULL_FUNCT      \        MEASURE_AD
  197.         NULL_FUNCT
  198.         NULL_FUNCT
  199.         NULL_FUNCT
  200.         NULL_FUNCT
  201. \ 10
  202.         NULL_FUNCT
  203.         NULL_FUNCT
  204.         NULL_FUNCT      \        WRITE_BYTESW
  205.         NULL_FUNCT      \        READ_BYTESW     \ 0Dh -- subverted by the "Hi-there"
  206.         NULL_FUNCT      \        WRITE_WORDSW
  207. \ 15
  208.         NULL_FUNCT      \        READ_WORDSW
  209.         NULL_FUNCT
  210.         NULL_FUNCT
  211.         NULL_FUNCT
  212.         NULL_FUNCT
  213. \ 20
  214.         NULL_FUNCT
  215.         NULL_FUNCT
  216.         NULL_FUNCT
  217.         NULL_FUNCT
  218.         FILL_MEM
  219. \ 25
  220.         NULL_FUNCT
  221.         NULL_FUNCT
  222.         NULL_FUNCT
  223.         NULL_FUNCT
  224.         NULL_FUNCT
  225.         NULL_FUNCT
  226.         ;
  227.  
  228. : NULL_FUNCT  ;
  229.  
  230. \ SPCMDS.SEQ    serial port commands.
  231.  
  232. LABEL GET_WORDL         \ Get a word value into W0 from the serial port
  233.         PUSH    TTOS
  234.         CALL    SP_RX_TL
  235.         JE      1 $             \ low byte
  236.         LDB     TTOS W0
  237.         CALL    SP_RX_TL        \ high byte
  238.         LDB     TTOSH W0
  239.         LD      W0 TTOS         \ now W0 is the value
  240. 1 $:    POP     TTOS
  241.         RET
  242.         end-code
  243.  
  244. \ *************************************************************************
  245. \
  246. \       Serial port function to go call a subroutine
  247. \
  248. \ *************************************************************************
  249.  
  250. CODE JUMP_TO
  251.         CALL    GET_WORDL        \ get an address
  252.         0<> IF  BR[] W0  THEN    \ jump to it
  253.                         \ subsequent RET will be to idle loop
  254.         RET
  255.         end-code
  256.  
  257.  
  258. \ *************************************************************************
  259. \
  260. \       Write and Read bytes in
  261. \       regular memory, or the normal register set
  262. \
  263. \ *************************************************************************
  264.  
  265.  
  266. CODE FILL_MEM           \ fill memory with a byte value
  267.         PUSH    TTOS
  268.         CALL    GET_WORDL       \ get address
  269.         JE      2 $
  270.         LD      TTOS W0
  271.         CALL    GET_WORDL       \ third and fourth are byte count
  272.         JE      2 $
  273.         PUSH    W0
  274.         CALL    SP_RX_TL         \ get the byte to write
  275.         POP     W2
  276.         JE      2 $
  277. 1 $:
  278.         STB     W0  []+ TTOS    \ write the byte
  279.         DEC     W2
  280.         JNE     1 $
  281. 2 $:
  282.         CALL    SP_SENDL        \ send a byte to say it is done
  283.         POP     TTOS
  284.         RET
  285.         end-code
  286.  
  287. CODE WRITE_BYTES        \ write memory byte contents
  288.         PUSH    TTOS
  289.         PUSH    TTOS2
  290.         CALL    GET_WORDL       \ get address
  291.         JE      2 $
  292.         LD      TTOS W0
  293.         CALL    SP_RX_TL
  294.         JE      2 $
  295.         LDB     TTOS2 W0        \ third byte is byte count, put it in TTOS2
  296. 1 $:
  297.         CALL    SP_RX_TL        \ get a byte of data to write
  298.         JE      2 $
  299.         STB     W0  []+ TTOS    \ write the byte
  300.         DJNZ    TTOS2  1 $
  301. 2 $:    POP     TTOS2
  302.         POP     TTOS
  303.         RET
  304.         end-code
  305.  
  306. CODE READ_BYTES         \ return memory byte contents
  307.         PUSH    TTOS
  308.         PUSH    TTOS2
  309.         CALL    GET_WORDL       \ get address into W0
  310.         JE      2 $
  311.         PUSH    W0
  312.         CALL    SP_RX_TL        \ third byte is byte count
  313.         LDB     TTOS2 W0        \ put it in TTOS2
  314.         POP     TTOS            \ address in TTOS
  315.         JE      2 $
  316. 1 $:
  317.         LDB     W0  []+ TTOS    \ read a byte
  318.         CALL    SP_SENDL        \ send the byte
  319.         DJNZ    TTOS2 1 $
  320. 2 $:    POP     TTOS2
  321.         POP     TTOS
  322.         RET
  323.         end-code
  324.  
  325.  
  326. end-cseg
  327.  
  328. \s
  329. comment:
  330. code set-hso ( n -- ) \ set bits n in the HSO
  331.         LDB     W0  IOS0
  332.         LDB     WSR  # 15
  333.         ORB     W0  # TTOS
  334.         LDB     IOS0  W0
  335.         LDB     WSR  0
  336.         LOAD_TTOS
  337.         ret
  338.         end-code
  339.  
  340. code clear-hso ( n -- ) \ reset bits n in the HSO
  341.         NOTB    TTOS
  342.         LDB     W0  IOS0
  343.         LDB     WSR  # 15
  344.         ANDB    W0  # TTOS
  345.         LDB     IOS0  W0
  346.         LDB     WSR  0
  347.         LOAD_TTOS
  348.         ret
  349.         end-code
  350. comment;
  351.  
  352.  
  353.