home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / enterprs / c128 / util / sfxsrc.lzh / START.A < prev    next >
Encoding:
Text File  |  1990-02-12  |  7.4 KB  |  288 lines

  1. ; start.a
  2. ;──────────────────────────────────────────────────────────────────────────────
  3. ; LHARChive self extraction module for C64/C128 - Copyright 1990 - Chris Smeets
  4. ;──────────────────────────────────────────────────────────────────────────────
  5. ; Startup code
  6.  
  7.             INSTXT "sfx.i"
  8.  
  9.             PUBLIC _Dend_           ; Start of data segment
  10.             PUBLIC _Dorg_           ; End of data segment
  11.             PUBLIC _Corg_           ; Start of code segment
  12.             PUBLIC _Cend_           ; End of code segment
  13.  
  14.             EXT Decode
  15.             EXT GenTab
  16.             EXT GenCRC
  17.             EXT Portable
  18.             EXT Romin
  19.             EXT Romout
  20.             EXT Prt
  21.             EXT Message
  22.             EXT Basic
  23.             EXT clrchn
  24.             EXT chrin
  25.  
  26. Save_SP     EQU $03d6               ; Not used in 64, INSTR work area in 128
  27.  
  28. RelCount    EQU $00bb               ; Number of relocation items
  29. Offset      EQU $00fa               ; Save relocation offset here
  30. CP          EQU $00fb               ; Pointer into CSEG
  31. RP          EQU $00fd               ; Pointer into relocation info
  32.  
  33.             PUBLIC OutDrive
  34.             PUBLIC OutUnit
  35.             PUBLIC P_flag
  36.             PUBLIC FirstTime
  37.  
  38.             BSS OutDrive,1
  39.             BSS OutUnit,1
  40.             BSS P_flag,1
  41.             BSS FirstTime,1
  42.  
  43.             BSS Save_ZP,Z_high-Z_low
  44.  
  45. ;───────────────────────────────────────────
  46. ; BASIC will sys(.begin) so this can't move
  47. ;───────────────────────────────────────────
  48. ; Start by relocating the program.
  49.  
  50.             PUBLIC .begin
  51.             ENTRY  .begin
  52.  
  53. .begin      lda #%00001110
  54.             sta $ff00
  55. Relocate    lda #$60                ; Store an RTS
  56.             sta $fc
  57.             jsr $fc                 ; push actual value of #>Rel0-1
  58. Rel0        tsx
  59.             stx Save_SP
  60.             dex
  61.             txs
  62.             pla
  63.             sec
  64.             sbc #>Rel0-1            ; Less what the linker thinks it is
  65.             sta Offset              ; Will be zero if run twice
  66.  
  67.             lda #<_Corg_            ; Compute true _Corg_
  68.             sta CP
  69.             lda #>_Corg_
  70.             clc
  71.             adc Offset
  72.             sta CP+1
  73.             lda #<_Dend_+2          ; and True _Dend_
  74.             sta RP
  75.             lda #>_Dend_+2
  76.             clc
  77.             adc Offset
  78.             sta RP+1
  79.  
  80.             ldx #0
  81.             ldy #1
  82.             lda (RP,x)
  83.             sta RelCount
  84.             lda (RP),y
  85.             sta RelCount+1
  86.             lda #2
  87.             fcb $2c
  88. Rel1        lda #1
  89.             clc
  90.             adc RP
  91.             sta RP
  92.             bcc Rel2
  93.             inc RP+1
  94. Rel2        lda RelCount
  95.             ora RelCount+1
  96.             beq Done
  97. Rel3        lda (RP,x)
  98.             pha
  99.             clc
  100.             adc CP
  101.             sta CP
  102.             bcc Rel4
  103.             inc CP+1
  104. Rel4        pla
  105.             cmp #$ff
  106.             beq Rel1
  107.             clc
  108.             lda (CP,x)
  109.             adc Offset
  110.             sta (CP,x)
  111.             lda RelCount
  112.             bne Rel5
  113.             dec RelCount+1
  114. Rel5        dec RelCount
  115.             clc
  116.             bcc Rel1
  117.  
  118. ;─────────────────────────────────────────────
  119. ; Relocation Complete. Do some initialization
  120. ;─────────────────────────────────────────────
  121.  
  122. Done        jsr Portable            ; Setup C64/C128 jumptable
  123.             jsr Romout              ; Run in all RAM configuration
  124.             ldx RP                  ; Data starts where relocation info stops
  125.             ldy RP+1
  126.             stx IBUF
  127.             sty IBUF+1
  128.             ldx #Z_low              ; Copy the zero page we need to BSS
  129.             ldy #0
  130.             sty FirstTime           ; Flag 1st time for title display
  131.             sty OutUnit             ; Flag that we need to ask for output dev
  132. .SaveZ      lda 0,x
  133.             sta Save_ZP,y
  134.             iny
  135.             inx
  136.             cpx #Z_high
  137.             bne .SaveZ
  138.  
  139.             jsr GenTab              ; Generate d_code and d_len tables
  140.             jsr GenCRC              ; Generate CRC calculation tables
  141.  
  142. Again       jsr Decode              ; Decode one file
  143.             cmp #NullFile           ; Empty file, just try again
  144.             beq Again
  145.             cmp #DiskError          ; Disk error? If so, abort
  146.             bne E1
  147.             ldx #<DiskMssg
  148.             ldy #>DiskMssg
  149.             jmp Abort
  150.  
  151. E1          cmp #BadHeader          ; Bad header, its the end of the SFX
  152.             bne E2
  153.             ldx #<DoneMssg
  154.             ldy #>DoneMssg
  155.             jmp Abort
  156.  
  157. E2          cmp #CRCError
  158.             bne E3
  159.             ldx #<CRCMssg
  160.             ldy #>CRCMssg
  161. GoAgain     bit P_flag
  162.             bmi NoMessage
  163.             jsr Message
  164. NoMessage   jmp Again
  165.  
  166. E3          cmp #DecodeOK
  167.             bne E4
  168.             ldx #<OKMssg
  169.             ldy #>OKMssg
  170.             bne GoAgain
  171.  
  172. E4          ldx #<ErrMssg           ; Unknown return code? Assume its an error
  173.             ldy #>ErrMssg
  174.  
  175.             PUBLIC Abort
  176.  
  177. Abort       jsr Message             ; Print error message
  178.  
  179. AllDone     ldy #0                  ; Restore zero page
  180.             ldx #Z_low
  181. .RestZ      lda Save_ZP,y
  182.             sta 0,x
  183.             iny
  184.             inx
  185.             cpx #Z_high
  186.             bne .RestZ
  187.             jsr Romin
  188.             ldx Save_ZP             ; Restore startup SP
  189.             txs
  190.             jmp Basic               ; And back to BASIC ROM
  191.  
  192.             DSEG
  193.  
  194. DiskMssg    fcb 13
  195.             fcc "DISK ERROR"
  196.             fcb 13,0
  197. DoneMssg    fcb 13
  198.             fcc "ALL DONE"
  199.             fcb 13,0
  200. CRCMssg     fcc "CRC ERROR!"
  201.             fcb 13,0
  202. OKMssg      fcc "OK"
  203.             fcb 13,0
  204. ErrMssg     fcb 13
  205.             fcc "DECODE ERROR"
  206.             fcb 13,0
  207.  
  208.             PUBLIC CopyRight
  209.  
  210. CopyRight   fcb 13
  211.             fcc "C64/C128 SELF EXTRACTING LHARCHIVE"
  212.             fcb 13
  213.             fcc "COPYRIGHT 1990 - C.SMEETS"
  214.             fcb 13
  215.             fcc "TORONTO,CANADA"
  216.             fcb 13,13,0
  217. AskDrive    fcc "DRIVE (0 OR 1) ? 0"
  218.             fcb 157,0
  219. AskUnit     fcc "UNIT  (8,9...) ? 8"
  220.             fcb 157,0
  221.  
  222.             CSEG
  223.  
  224.             PUBLIC CRLF
  225.  
  226. GetNum      jsr Message
  227.             jsr clrchn
  228.             lda #0
  229.             sta T0
  230.             sta T1
  231.             jsr Digit
  232.             bcs CRLF
  233.             jsr Digit
  234.             lda T0
  235. Tens        dec T1
  236.             bmi GotTens
  237.             clc
  238.             adc #10
  239.             bcc Tens
  240. GotTens     clc
  241. CRLF        php
  242.             pha
  243.             lda #13
  244.             jsr Prt
  245.             pla
  246.             plp
  247.             rts
  248.  
  249. Digit       jsr chrin
  250.             cmp #'0'
  251.             bcc NoNum
  252.             cmp #'9'+1
  253.             bcs NoNum
  254.             and #$0f
  255.             ldx T0
  256.             sta T0
  257.             stx T1
  258.             clc
  259.             rts
  260.  
  261. NoNum       sec
  262.             rts
  263.  
  264.             PUBLIC AskDrives
  265.  
  266. AskDrives   lda OutUnit
  267.             bne GotDrives
  268.             ldx #<CopyRight
  269.             ldy #>CopyRight
  270.             jsr Message
  271.             ldx #<AskDrive
  272.             ldy #>AskDrive
  273.             jsr GetNum
  274.             bcc SaveDrive
  275. BadInfo     jmp AllDone
  276.  
  277. SaveDrive   sta OutDrive
  278.             ldx #<AskUnit
  279.             ldy #>AskUnit
  280.             jsr GetNum
  281.             bcs BadInfo
  282.             sta OutUnit
  283.             jsr CRLF
  284. GotDrives   rts
  285.  
  286.             END
  287.  
  288.