home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / ucsdappleii / asm.kermit.text < prev    next >
Text File  |  1986-04-07  |  16KB  |  437 lines

  1. ;-----------------------------------------------------------------------
  2. ;-----------------------------------------------------------------------
  3. ;
  4. ;   This procedure is external to the unit kermpack.
  5. ;
  6. ;-----------------------------------------------------------------------
  7. ;-----------------------------------------------------------------------
  8. ;
  9. ;FUNCTION rpack(           n  : INTEGER;
  10. ;                VAR len, num : INTEGER;
  11. ;                VAR    data  : packet_type;
  12. ;                    time_out : INTEGER;
  13. ;                         soh : CHAR        ) : CHAR;
  14. ;------------------------------------------------------------------------
  15. ; This function listens to the serial input port, detects a kermit
  16. ; package, decodes it, returns the data part of the package, the
  17. ; length of the data part and the number of the package. Its function
  18. ; value is the packet-type.
  19. ; n = the number of the last packet send. It is only used to initialize
  20. ; num, otherwise num would be undefined in case of receive failure.
  21. ; The function takes the value '@' in case a transmission error is
  22. ; detected when decoding the packet or when no valid packet has been
  23. ; received during the time_out period.
  24. ; time_out can be specified in seconds : this value will be multiplied
  25. ; within rpack by 8 to approximate real time. Because only the least
  26. ; significant byte of time_out is passed to rpack, the valid range for
  27. ; time_out will be 1..31 seconds.
  28. ; This function will not work without the system.attach and attach.drivers
  29. ; that implement a remin buffer and the remin unitstatus statement.
  30. ;
  31. ;--------------------------------------------------------------------------
  32. ;
  33.            .FUNC RPACK, 6.
  34. ;
  35. BIOSAF     .EQU 0FF5C          ; base of bios jump table. Same in V1.1 & V1.2
  36. BIOSRAM    .EQU 0C083          ; switch for extra bios ram.
  37. INTPRAM    .EQU 0C08B          ; switch back to main ram.
  38. RREAD      .EQU BIOSAF+24.     ; bios remote read routine adress.
  39. RSTAT      .EQU BIOSAF+51.     ; bios remote status routine adress.
  40. DUMMY      .EQU 0FFFF          ; dummy adress : will be filled in at runtime
  41. TEMP1      .EQU 00             ; temp zero page adresses.
  42. TEMP2      .EQU 02
  43. ;
  44. ; get parameters from stack:
  45. ;
  46.            PLA                 ; pop return adress.
  47.            STA RETURN
  48.            PLA
  49.            STA RETURN+1
  50.            ;-------------------
  51.            PLA                 ; remove function bias.
  52.            PLA
  53.            PLA
  54.            PLA
  55.            ;-------------------
  56.            PLA                 ; pop soh ( nearly always ^A )
  57.            STA SOH
  58.            PLA                 ; discard msb.
  59.            ;-------------------
  60.            PLA                 ; pop timeout.
  61.            ASL A               ; timeout = timeout * 8
  62.            ASL A               ; to approximate real time.
  63.            ASL A
  64.            STA TIMEOUT
  65.            PLA                 ; discard msb.
  66.            ;-------------------
  67.            PLA                 ; move adress of recpkt to the the right place.
  68.            STA RPADR+1
  69.            PLA
  70.            STA RPADR+2
  71.            ;-------------------
  72.            PLA                 ; move adress of num .
  73.            STA TEMP1
  74.            STA NUMADR+1
  75.            PLA
  76.            STA TEMP1+1
  77.            STA NUMADR+2
  78.            ;-------------------
  79.            PLA                 ; move adress of len .
  80.            STA TEMP2
  81.            STA LENADR+1
  82.            PLA
  83.            STA TEMP2+1
  84.            STA LENADR+2
  85.            ;-------------------
  86.            PLA                 ; pop n
  87.            AND #3F             ; take mod 64
  88.            LDY #00             ; init num to n in case of receive failure.
  89.            STA @TEMP1,Y
  90.            PLA                 ; discard msb of n.
  91.            TYA
  92.            INY
  93.            STA @TEMP1,Y
  94.            ;-------------------
  95. ;
  96. ; initialization code
  97. ;
  98.            LDA #00             ; init len to zero.
  99.            TAY
  100.            STA @TEMP2,Y
  101.            INY
  102.            STA @TEMP2,Y
  103.            STA RESYNCNT        ; set resynchronization count to 0
  104.            STA C1              ; set all timeout counters to 0
  105.            STA C2
  106.            LDA BIOSRAM         ; switch in bios ram
  107. ;
  108. ; start rpack
  109. ;
  110. WAITSOH    JSR GETCHAR2        ; wait for a soh (^A)
  111.            BNE WAITSOH
  112. RESYN      INC RESYNCNT        ; if more than 256 resync's : give up
  113.            BEQ RECFAIL
  114.            ;-------------------
  115.            JSR GETCHAR1        ; get packet length ( len ).
  116.            BEQ RESYN           ; if it was a soh then resync.
  117.            STA CHKSUM          ; init checksum .
  118.            SEC
  119.            SBC #35.            ; len := len - 32 - 3.
  120.            BMI RECFAIL         ; if len < 0 then something is wrong.
  121.            STA LEN             ; save len temporarily.
  122. LENADR     STA DUMMY           ; save len for pascal.
  123.            ;-------------------
  124.            JSR GETCHAR1        ; get packet number ( num ).
  125.            BEQ RESYN           ; if it was a soh then resync.
  126.            PHA                 ; save num
  127.            CLC
  128.            ADC CHKSUM          ; increase chksum
  129.            STA CHKSUM
  130.            PLA                 ; get original num back.
  131.            SEC
  132.            SBC #32.            ; subtract 32.
  133. NUMADR     STA DUMMY           ; save num for pascal.
  134.            ;-------------------
  135.            JSR GETCHAR1        ; get packet type ( function value of rpack )
  136.            BEQ RESYN
  137.            STA PTYPE
  138.            CLC
  139.            ADC CHKSUM          ; increase checksum
  140.            STA CHKSUM
  141.            ;-------------------
  142.            LDY #00             ; get data char's ( recpkt )
  143. FILLPACK   STY LENCNT          ; save y reg.
  144.            CPY LEN             ; if no (more) data expected : skip this loop.
  145.            BEQ GETCHKSUM
  146.            JSR GETCHAR1        ; get data char.
  147.            BEQ RESYN
  148.            LDY LENCNT          ; restore y reg.
  149. RPADR      STA DUMMY,Y         ; fill in recpkt for pascal
  150.            CLC
  151.            ADC CHKSUM          ; increase checksum
  152.            STA CHKSUM
  153.            INY                 ; increase length counter
  154.            BNE FILLPACK        ; branch always to get next data char.
  155.            ;-------------------
  156. GETCHKSUM  JSR GETCHAR1        ; get packet checksum.
  157.            BEQ RESYN
  158.            SEC
  159.            SBC #32.            ; subtract 32.
  160.            STA PCHKSUM
  161.            ;-------------------
  162.            LDA CHKSUM          ; calculate final checksum.
  163.            ROL A
  164.            ROL A
  165.            ROL A
  166.            AND #03
  167.            CLC
  168.            ADC CHKSUM
  169.            AND #3F
  170.            ; equivalent to s = ( s + ( ( s and 192 ) div 64 ) ) and 63
  171.            CMP PCHKSUM         ; compare to received checksum.
  172.            BEQ EXIT            ; if ok then back to pascal.
  173.            ;-------------------
  174. RECFAIL    LDA #40             ; rpack = '@' if a receive failure was
  175.            STA PTYPE           ; detected.
  176.            ;-------------------
  177. EXIT       LDA #00             ; push msb of function value.
  178.            PHA
  179.            LDA PTYPE           ; push lsb of function value.
  180.            PHA
  181.            ;-------------------
  182.            LDA INTPRAM         ; switch back to main ram.
  183.            ;-------------------
  184.            LDA RETURN+1        ; push return adress
  185.            PHA
  186.            LDA RETURN
  187.            PHA
  188.            ;-------------------
  189.            RTS                 ; back to pascal.
  190. ;---------------------------------------------------------------------
  191. ;
  192. ;  subroutines  GETCHAR1 & GETCHAR2
  193. ;
  194. GETCHAR1   LDA #00             ; zero timeout counters
  195.            STA C1
  196.            STA C2
  197.            ;-------------------
  198. GETCHAR2   JSR RSTATUS         ; entry point without timeout reset.
  199.            LDA BUFLEN          ; something in remin buffer?
  200.            BNE GET             ; then get it.
  201.            INC C1              ; if not then increase timeout counter
  202.            BNE GETCHAR2        ; and keep testing remin buffer.
  203.            INC C2
  204.            LDA C2
  205.            CMP TIMEOUT         ; if timeout period has expired then
  206.            BNE GETCHAR2        ; indicate a receive failure.
  207.            PLA                 ; remove this routine's return adress
  208.            PLA                 ; from stack and go
  209.            JMP RECFAIL         ; back to pascal.
  210.            ;-------------------
  211. GET        LDX #00             ; x = 0 : read request.
  212.            JSR RREAD           ; read remin buffer. Char in accu.
  213.            CMP SOH             ; main rpack will take action if a ^A is
  214.            RTS                 ; detected.
  215. ;---------------------------------------------------------------------
  216. ;
  217. ;  subroutine  RSTATUS
  218. ;
  219. RSTATUS    LDA #00             ; push controlword on stack
  220.            PHA
  221.            LDA #01
  222.            PHA
  223.            ;-------------------
  224.            LDA BUFLENPTR+1     ; push adress of buflen on stack
  225.            PHA
  226.            LDA BUFLENPTR
  227.            PHA
  228.            ;-------------------
  229.            LDX #04             ; x = 4 : status request.
  230.            JSR RSTAT           ; number of char's in reminbuffer
  231.            RTS                 ; can now be found in buflen.
  232. ;---------------------------------------------------------------------
  233. ;
  234. ;  variable space:
  235. ;
  236. RETURN     .WORD 00
  237. SOH        .BYTE 00
  238. TIMEOUT    .BYTE 00
  239. RESYNCNT   .BYTE 00
  240. C1         .BYTE 00
  241. C2         .BYTE 00
  242. LEN        .BYTE 00
  243. LENCNT     .BYTE 00
  244. PTYPE      .BYTE 00
  245. CHKSUM     .BYTE 00
  246. PCHKSUM    .BYTE 00
  247. BUFLEN     .WORD 00
  248. BUFLENPTR  .WORD BUFLEN
  249. ;--------------------------------------------------------------------------
  250. ;--------------------------------------------------------------------------
  251. ;
  252. ;  These procedures are external to unit kermutil.
  253. ;
  254. ;--------------------------------------------------------------------------
  255. ;--------------------------------------------------------------------------
  256. ;
  257. ;  FUNCTION calc_checksum( var packet : packettype; len : integer ) : CHAR;
  258. ;
  259. ;  calculates one character checksum of a packet.
  260. ;
  261. ;  FUNCTION ctl( ch : char ) : CHAR;
  262. ;
  263. ;  transforms a control char to a printable char and vice versa.
  264. ;
  265. ;-----------------------------------------------------------------------
  266. ;
  267.           .FUNC CALCCHECKSUM, 2 ; two parameters
  268. RETURN    .EQU 00
  269. PACKETPTR .EQU 02
  270. CHKSUM    .EQU 04
  271.           ;---------------------
  272.           PLA                   ; pop return address
  273.           STA RETURN
  274.           PLA
  275.           STA RETURN+1
  276.           ;---------------------
  277.           PLA                   ; pop .func bias
  278.           PLA
  279.           PLA
  280.           PLA
  281.           ;---------------------
  282.           PLA                   ; save len in y reg.
  283.           TAY
  284.           DEY                   ; len = len - 1
  285.           PLA                   ; discard msb.
  286.           ;---------------------
  287.           PLA                   ; pop address of var packet
  288.           STA PACKETPTR
  289.           PLA
  290.           STA PACKETPTR+1
  291.           ;---------------------
  292.           LDA #00               ; push msb of function result
  293.           PHA
  294.           ;---------------------
  295. SUM       CLC                   ; sum characters except packet[0]
  296.           ADC @PACKETPTR,Y
  297.           DEY
  298.           BNE SUM
  299.           ;--------------------
  300.           STA CHKSUM            ; save this sum temporarily
  301.           ROL A
  302.           ROL A
  303.           ROL A
  304.           AND #03
  305.           CLC
  306.           ADC CHKSUM
  307.           AND #3F
  308.           ;---------------------
  309.           ; equivalent to  s = ( s + ( ( s and 192 ) div 64 ) ) and 63
  310.           PHA                  ; push lsb of function result
  311.           LDA RETURN+1         ; push return and back to pascal
  312.           PHA
  313.           LDA RETURN
  314.           PHA
  315.           RTS
  316. ;----------------------------------------------------------------------
  317. ;
  318.           .FUNC CTL, 1         ; one parameter
  319.           PLA                  ; save return address in x and y
  320.           TAX
  321.           PLA
  322.           TAY
  323.           ;--------------------
  324.           PLA                  ; pop .func bias
  325.           PLA
  326.           PLA
  327.           PLA
  328.           ;--------------------
  329.           PLA                  ; leave msb function result on stack (=0)
  330.           EOR #40              ; toggle bit 7 of character
  331.           PHA                  ; push lsb funtion result
  332.           ;--------------------
  333.           TYA                  ; push return address
  334.           PHA
  335.           TXA
  336.           PHA
  337.           RTS
  338. ;-------------------------------------------------------------------------
  339. ;-------------------------------------------------------------------------
  340. ;
  341. ;  These procedures are external to the unit kermacia.
  342. ;
  343. ;-------------------------------------------------------------------------
  344. ;-------------------------------------------------------------------------
  345. ;
  346. ;            PROCEDURE   Send_6551_Break ( adr_comm_reg : INTEGER )
  347. ;
  348. ;  This procedure is external to the unit "kermacia" and is specific for a
  349. ;  6551 acia in slot 2.
  350. ;  It sends a "break" signal to the the remote host.
  351. ;  The signal is switched off by pressing any key.
  352. ;  The previous state of the command register is restored.
  353. ;-------------------------------------------------------------------------
  354. ;
  355. ;
  356.           .PROC SEND6551BREAK, 1  ; one parameter : the address of the 6551
  357.                                   ; command register.
  358. COMREG    .EQU 00                 ; zero page pointer.
  359. ;---------------------------------
  360.           PLA                 ; pop return adress.
  361.           STA RETURN
  362.           PLA
  363.           STA RETURN+1
  364.           ;-------------------
  365.           PLA                 ; pop 6511 command reg. address.
  366.           STA COMREG
  367.           PLA
  368.           STA COMREG+1
  369.           ;-------------------
  370.           LDY #00
  371.           LDA @COMREG,Y
  372.           PHA                 ; save content of command register.
  373.           ORA #0C             ; turn on break bits 00001100
  374.           STA @COMREG,Y       ; give break signal.
  375.           ;-------------------
  376. KEYBOARD  LDA 0C000           ; test apple keyboard
  377.           BPL KEYBOARD
  378.           STA 0C010           ; clear keyboard strobe
  379.           ;-------------------
  380.           PLA                 ; retrieve content of command register.
  381.           STA @COMREG,Y       ; and restore old situation
  382.           ;-------------------
  383.           LDA RETURN+1        ; push return adress
  384.           PHA
  385.           LDA RETURN
  386.           PHA
  387.           RTS                 ; and back to pascal.
  388.           ;-------------------
  389. RETURN    .WORD 00
  390. ;----------------------------------------------------------------------
  391. ;
  392. ;            PROCEDURE   Send_6850_Break ( adr_comm_reg : INTEGER )
  393. ;
  394. ;  This procedure is external to the unit "kermacia" and is specific for a
  395. ;  6850 acia in slot 2.
  396. ;  It sends a "break" signal to the the remote host.
  397. ;  The signal is switched off by pressing any key.
  398. ;  The previous state of the command register is restored by the procedure
  399. ;  set_acia_parms in unit kermacia.
  400. ;-------------------------------------------------------------------------
  401. ;
  402. ;
  403.           .PROC SEND6850BREAK, 1  ; one parameter : the address of the 6850
  404.                                   ; command register.
  405. COMREG    .EQU 00                 ; zero page pointer.
  406. ;---------------------------------
  407.           PLA                 ; pop return adress.
  408.           STA RETURN
  409.           PLA
  410.           STA RETURN+1
  411.           ;-------------------
  412.           PLA                 ; pop 6511 command reg. address.
  413.           STA COMREG
  414.           PLA
  415.           STA COMREG+1
  416.           ;-------------------
  417.           LDY #00
  418.           LDA #70             ; set break signal on .
  419.           STA @COMREG,Y
  420.           ;-------------------
  421. KEYBOARD  LDA 0C000           ; test apple keyboard
  422.           BPL KEYBOARD
  423.           STA 0C010           ; clear keyboard strobe
  424.           ;-------------------
  425.           LDA #13             ; give an acia master reset.
  426.           STA @COMREG,Y       ;
  427.           ;-------------------
  428.           LDA RETURN+1        ; push return adress
  429.           PHA
  430.           LDA RETURN
  431.           PHA
  432.           RTS                 ; and back to pascal.
  433.           ;-------------------
  434. RETURN    .WORD 0
  435. ;-----------------------------------------------------------------------
  436.           .END
  437.