
    0   .ti 81,BNApple //c w/512K Z-RAM buffering program 2.0 8/5/85 dcj
    1  ;SAVE Buf.576K 
    2  ;--------------------------------
    3  ; Dedicated to Allan B. Calhamer.
    4  ;--------------------------------
    5  Printer.ACIA.TxD     .eq $C098 (w)
    6  Printer.ACIA.Status  .eq $C099 (r)
    7  Printer.ACIA.Command .eq $C09A (r/w)
    8  Printer.ACIA.Control .eq $C09B (r/w)
    9  Modem.ACIA.RxD       .eq $C0A8 (r)
   10  Modem.ACIA.Status    .eq $C0A9 (r)
   11  Modem.ACIA.Command   .eq $C0AA (r/w)
   12  Modem.ACIA.Control   .eq $C0AB (r/w)
   13  Z.RAM.Bank.Reg       .eq $C073 (w) same as RamWorks
   14  Keyboard             .eq $C000 (r)
   15  Store80              .eq $C001 (w) on
   16  RAMRd                .eq $C003 (w) aux
   17  RAMWrt               .eq $C005 (w) aux
   18  AltZP                .eq $C009 (w) aux
   19  Vid40                .eq $C00C (w)
   20  SetAltChr            .eq $C00F (w) w/MouseText
   21  Clear.Key.Strobe     .eq $C010 (r)
   22  Text                 .eq $C051 (r)
   23  Page1                .eq $C054 (r) main
   24  Page2                .eq $C055 (r) aux
   25  Hires                .eq $C057 (r) $2000-$3FFF too...
   26  LCRAM2               .eq $C083 (r/w; write doesn't
   27  LCRAM1               .eq $C08B  change write enable)
   28  ;--------------------------------
   29         .op 65C02
   30         .or $2000
   31         .tf Bufit576K
   32  dcj    CLD               rqd (now)
   33         SEI               close this can of worms...
   34         LDA LCRAM2        1x...switches setup
   35         LDA Text
   36         LDA Page1
   37         LDA Hires
   38         STZ Store80
   39         STZ RAMRd
   40         STZ RAMWrt
   41         STZ AltZP
   42         STZ SetAltChr
   43         STZ Vid40
   44         LDA #" "          clear 40 column screen
   45         LDX #0
   46  .1     STA $400,X
   47         STA $500,X
   48         STA $600,X
   49         STA $700,X
   50         INX
   51         BNE .1
   52         LDY #$0F          install Image in aux ZPs/Stacks
   53  .2     STY Z.RAM.Bank.Reg
   54  .3     LDA Image,X
   55         STA $00,X
   56         LDA Image+$100,X
   57         STA $100,X
   58         INX
   59         BNE .3
   60         LDA Msg,Y         put up a message
   61         STA $50C,Y
   62         DEY
   63         BPL .2
   64         LDA #%000.0.10.1.0         bop ACIAs
   65         STA Printer.ACIA.Command
   66    inc  LDA #%000.0.10.1.1         RTS' lo
   67         STA Modem.ACIA.Command
   68         LDA #%0.00.1.1110          9600 baud
   69         STA Printer.ACIA.Control
   70    inc  LDA #%0.00.1.1111          19200 baud!
   71         STA Modem.ACIA.Control
   72         LDA Modem.ACIA.RxD
   73         JMP Scan          go 2 it
   74  Msg    .AS 'A'           as in Apple
   75         .AS -" //c buffer pgm"
   76  Image  .ph $00
   77  ; aux bank specified by Acc, bank adr lo by X-reg,
   78  ;  bank adr hi by Y-reg, and byte passed in S-reg!
   79  Write  STA Z.RAM.Bank.Reg  bank in Z-RAM
   80         STX <.1+1         modify STX operand in "this" bank
   81         STY <.1+2
   82         TSX               get byte to a usable reg!
   83  .1     STX $FFFF         abs adr modified for each write
   84         STZ Z.RAM.Bank.Reg  revert to //c aux bank
   85         JMP W.Ret
   86  ; aux bank specified by Acc, bank adr lo by X-reg,
   87  ;  bank adr hi by Y-reg, and byte returned in Acc.
   88  Read   STA Z.RAM.Bank.Reg  bank in Z-RAM
   89         STX <.1+1         modify LDA operand in "this" bank
   90         STY <.1+2
   91  .1     LDA $FFFF         abs adr modified for each read
   92         STZ Z.RAM.Bank.Reg  revert to //c aux bank
   93         JMP R.Ret
   94  Z.RAM.Banks.Avail   .eq *-3
   95  ; (-3 because JMP R.Ret never executed in Z-RAM)
   96  Z.RAM.used          .eq Z.RAM.Banks.Avail*8
   97  ;--------------------------------
   98  ; buffer starts at first available location in //c aux bank
   99  Receive.Adr.Lo      .da #IIc.Aux.Bank.Avail
  100  Receive.Adr.Hi      .da /IIc.Aux.Bank.Avail
  101  Receive.Bank        .da #$00
  102  Transmit.Adr.Lo     .da #IIc.Aux.Bank.Avail
  103  Transmit.Adr.Hi     .da /IIc.Aux.Bank.Avail
  104  Transmit.Bank       .da #$00
  105  Byte.Counter.Lo     .da #$000000        indicates empty
  106  Byte.Counter.Mid    .da #$000000/256
  107  Byte.Counter.Hi     .da #$000000/65536
  108  RTS.Bit             .da #%000.0.10.0.0  RTS' lo
  109  ;--------------------------------
  110  Scan   LDA Page1         access main text screen
  111         INC $413          show we're alive
  112         DEC $414
  113         LDA Page2         back to aux
  114         LDA Keyboard      scan keyboard
  115         BPL Scan.Modem.Port
  116         CMP #" "          space toggles RTS' (DTR2B) to //e
  117         BNE .2
  118         LDA Modem.ACIA.Command
  119         AND #%000.0.10.0.0
  120         BNE .1            =>It's ok, you can turn it off...
  121         LDA RTS.Bit
  122         BNE Scan.Modem.Port  =>don't do it! (yet)
  123  .1     LDA Modem.ACIA.Command
  124         EOR #%000.0.10.0.0
  125         STA Modem.ACIA.Command
  126         AND #%000.0.10.0.0
  127         STA RTS.Bit
  128  .2     BIT Clear.Key.Strobe
  129  Scan.Modem.Port
  130         LDY Modem.ACIA.Status
  131         TYA
  132         AND #%0000.0111   error bits mask
  133         BEQ .1            =>error-free operation
  134         TAX
  135         LDA Page1         access main text screen
  136         INC $4FF,X        indicate error...
  137         LDA Page2         back to aux
  138  .1     TYA
  139         AND #%0000.1000   receive data reg full mask
  140         BEQ CantRx        =>not full
  141         LDA Byte.Counter.Lo   received a byte,
  142         LDX Byte.Counter.Mid  do we assert RTS' ?
  143         LDY Byte.Counter.Hi
  144         CMP #BufLen-256
  145         BNE .2            =>buffer not @ full-256
  146         CPX /BufLen-256
  147         BNE .2            =>buffer not @ full-256
  148         CPY ^BufLen-256
  149         BNE .2            =>buffer not @ full-256
  150         LDA #%000.0.10.0.0   assert RTS'
  151         TRB Modem.ACIA.Command
  152         LDA Byte.Counter.Lo  reload it
  153  .2     INC               fig next byte count
  154         BNE .3
  155         INX
  156         BNE .3
  157         INY
  158  .3     CMP #BufLen       do we have room for it ?
  159         BNE Room          =>buffer not full
  160         CPX /BufLen
  161         BNE Room          =>buffer not full
  162         CPY ^BufLen
  163         BNE Room          =>buffer not full
  164         LDA Page1         access main text screen
  165         INC $402          indicate full
  166         LDA Page2         back to aux
  167  CantRx BRA Cant.Receive  =>buffer is full!
  168  Room   STA Byte.Counter.Lo
  169         STX Byte.Counter.Mid
  170         STY Byte.Counter.Hi
  171         LDA Page1         access main text screen
  172         INC $400          show we received a byte
  173         LDA Page2         back to aux
  174         LDX Modem.ACIA.RxD
  175         TXS               pass it in S-reg
  176         LDX Receive.Adr.Lo
  177         LDY Receive.Adr.Hi
  178         BIT LCRAM2        normally use LC bank 2
  179         TYA
  180         AND #$F0
  181         CMP /$C000        if adr is in $CXXX range
  182         BNE .1
  183         BIT LCRAM1         use LC bank 1
  184         TYA
  185         ORA /$D000
  186         TAY
  187  .1     LDA Receive.Bank
  188         JMP Write
  189  W.Ret  INC Receive.Adr.Lo  fig next receive adr
  190         BNE Scan.Printer.Port
  191         INC Receive.Adr.Hi
  192         BNE Scan.Printer.Port
  193         LDA Receive.Bank
  194         CMP #1
  195         ADC #1            clear carry if 0, else set it
  196         CMP #$10
  197         BCC .1            =>entering/still in Z-RAM
  198         LDA #$00          wrap to //c bank 0
  199         LDX #IIc.Aux.Bank.Avail
  200         LDY /IIc.Aux.Bank.Avail
  201         BRA .2
  202  .1     LDX #Z.RAM.Banks.Avail
  203         LDY /Z.RAM.Banks.Avail
  204  .2     STA Receive.Bank
  205         STX Receive.Adr.Lo
  206         STY Receive.Adr.Hi
  207  Cant.Receive
  208  Scan.Printer.Port
  209         LDA #%0011.0000   make transmit data reg empty and
  210         AND Printer.ACIA.Status  Data Carrier Detect mask
  211         CMP #%0001.0000   test empty and DCD' lo
  212         BNE Cant.Transmit =>not empty or not ready
  213         LDA Byte.Counter.Lo   printer can take another byte,
  214         ORA Byte.Counter.Mid  do we have one ?
  215         ORA Byte.Counter.Hi
  216         BEQ Cant.Transmit     =>buffer is empty!!!
  217         LDA Byte.Counter.Lo   do we release RTS' ?
  218         LDX Byte.Counter.Mid
  219         LDY Byte.Counter.Hi
  220         CMP #BufLen-2048
  221         BNE .1            =>buffer not @ full-2048
  222         CPX /BufLen-2048
  223         BNE .1            =>buffer not @ full-2048
  224         CPY ^BufLen-2048
  225         BNE .1            =>buffer not @ full-2048
  226         LDA RTS.Bit
  227         TSB Modem.ACIA.Command  release RTS' (maybe)
  228  .1     STA Z.RAM.Bank.Reg+5
  229         LDA Byte.Counter.Lo     fig next byte count
  230         BNE .3
  231         LDA Byte.Counter.Mid
  232         BNE .2
  233         DEC Byte.Counter.Hi
  234  .2     DEC Byte.Counter.Mid
  235  .3     DEC Byte.Counter.Lo
  236         LDA Page1         access main text page
  237         INC $427          show we printed a byte
  238         LDA Page2         back to aux
  239         LDX Transmit.Adr.Lo
  240         LDY Transmit.Adr.Hi
  241         BIT LCRAM2        normally use LC bank 2
  242         TYA
  243         AND #$F0
  244         CMP /$C000        if adr in $CXXX range
  245         BNE .4
  246         BIT LCRAM1         use LC bank 1
  247         TYA
  248         ORA /$D000
  249         TAY
  250  .4     LDA Transmit.Bank
  251         JMP Read
  252  R.Ret  STA Printer.ACIA.TxD
  253         INC Transmit.Adr.Lo  fig next transmit adr
  254         BNE Next
  255         INC Transmit.Adr.Hi
  256         BNE Next
  257         LDA Transmit.Bank
  258         CMP #1            clear carry if 0, else set it
  259         ADC #1
  260         CMP #$10
  261         BCC .1            =>entering/still in Z-RAM
  262         LDA #$00          wrap to //c bank 0
  263         LDX #IIc.Aux.Bank.Avail
  264         LDY /IIc.Aux.Bank.Avail
  265         BRA .2
  266  .1     LDX #Z.RAM.Banks.Avail
  267         LDY /Z.RAM.Banks.Avail
  268  .2     STA Transmit.Bank
  269         STX Transmit.Adr.Lo
  270         STY Transmit.Adr.Hi
  271  Cant.Transmit
  272  Next   JMP Scan
  273  IIc.Aux.Bank.Avail .eq *
  274  BufLen .eq $90000-Z.RAM.Used-IIc.Aux.Bank.Avail
  275   .lif

