File: shadow_ram_handlers.s

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (1121 lines) | stat: -rw-r--r-- 25,555 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
;
; Atari XL shadow RAM handlers
;
; Christian Groessler, chris@groessler.org, 2013
;

;DEBUG           =       1
CHKBUF          =       1       ; check if bounce buffering is needed (bounce buffering is always done if set to 0)

.ifdef __ATARIXL__

SHRAM_HANDLERS  =       1
        .include        "atari.inc"
        .include        "save_area.inc"
        .include        "zeropage.inc"
        .include        "romswitch.inc"

        .import         __CHARGEN_START__

        .export         sram_init
        .export         KEYBDV_handler
        .export         CIO_handler
        .export         SIO_handler
        .export         SETVBV_handler
        .export         XMOVE_handler

BUFSZ           =       128     ; bounce buffer size
BUFSZ_SIO       =       256

.segment "ONCE"

; Turn off ROMs, install system and interrupt wrappers, set new chargen pointer

sram_init:

; disable all interrupts
        ldx     #0
        stx     NMIEN           ; disable NMI
        sei

; disable ROMs
        disable_rom

; setup interrupt vectors
        lda     #<my_IRQ_han
        sta     $fffe
        lda     #>my_IRQ_han
        sta     $ffff

        lda     #<my_RESET_han
        sta     $fffc
        lda     #>my_RESET_han
        sta     $fffd

        lda     #<my_NMI_han
        sta     $fffa
        lda     #>my_NMI_han
        sta     $fffb

; enable interrupts
        cli
        lda     #$40
        sta     NMIEN

        rts

.segment        "EXTZP" : zeropage

zpptr1: .res    2


.segment "LOWBSS"

; bounce buffers for CIO and SIO calls
bounce_buffer:  .res    BUFSZ_SIO


.segment "LOWCODE"


; Interrupt handlers
; ------------------

; The interrupt handlers don't look at the current state of PORTB and
; unconditionally disable the ROMs on exit.
; Please note that this works, since if the ROMs are enabled we anyway
; aren't being called here because the vectors are pointing to their
; original ROM locations.

.macro  int_wrap orgvec
        .local  ret
        pha
        enable_rom_quick
        lda     #>ret
        pha
        lda     #<ret
        pha
        php
        jmp     (orgvec)
ret:    disable_rom_quick
        pla
        rti
.endmacro

my_IRQ_han:
.ifdef DEBUG
        php
        pha
        tya
        pha
        ldy     #0
        lda     (SAVMSC),y
        clc
        adc     #1
        sta     (SAVMSC),y
        pla
        tay
        pla
        plp
.endif
        int_wrap $FFFE

my_NMI_han:
.ifdef DEBUG
        php
        pha
        tya
        pha
        ldy     #39
        lda     (SAVMSC),y
        clc
        adc     #1
        sta     (SAVMSC),y
        pla
        tay
        pla
        plp
.endif
; set I bit to interrupted value
        pha
        txa
        pha
        tsx
        lda     $103,x
        pha
        plp
        pla
        tax
        pla
        int_wrap $FFFA

my_RESET_han:
        enable_rom
        jmp     ($FFFC)


; System request handlers
; -----------------------


; for filenames we assume they will fit into our bounce buffer

; one filename, terminated by "invalid character", located at ICBAL/ICBAH

CIO_filename:
.if CHKBUF
        jsr     chk_CIO_buf_fn
        bcc     CIO_call_a
.endif
        jsr     setup_zpptr1_y0
        jsr     copy_filename
CIO_fn_cont:
        jsr     bncbuf_to_iocb
        ldy     CIO_y
        jsr     CIO_call_a              ; call CIO (maybe A isn't needed, then we could call CIO_call)
        php
        pha
        jsr     restore_icba            ; restore original ICBAL/ICBAH
        pla
        plp
        rts                             ; back to application


; two filenames, terminated and separated by "invalid character", located at ICBAL/ICBAH

CIO_filename2:
.if CHKBUF
        jsr     chk_CIO_buf_fn2
        bcc     CIO_call_a
.endif
        jsr     setup_zpptr1_y0
        jsr     copy_filename
        iny
        jsr     copy_filename
        jmp     CIO_fn_cont



; CIO handler
; We have buffer pointer and length entries in the IOCB, but their
; usage depends on the function.
; Some functions don't care about any of them (pointer and length),
; and some only use the pointer (like e.g. OPEN), and some use both.
; So we need function specific handlers to correctly deal with
; buffers which are overlapping with the ROM area.
;
; FIXME: Currently only the requests used by the runtime lib are handled.

CIO_handler:

; @@@ TODO: check X for valid IOCB index ((X < $80) and ((X & $F) == 0))

        sta     CIO_a
        sty     CIO_y
        stx     CIO_x

        lda     ICCOM,x                 ; get function
        cmp     #OPEN
        beq     CIO_filename            ; filename as input parameter in buffer, length not used
        cmp     #PUTREC
        bcc     CIO_read                ; input (GETREC or GETCHR)
        cmp     #CLOSE
        bcc     CIO_write_jmp           ; output (PUTREC or PUTCHR)
        beq     CIO_call_a              ; pass through, buffer not used
        cmp     #RENAME                 ; 2 filenames as input parameters in buffer, length not used
        beq     CIO_filename2
        cmp     #GETCWD
        bcc     CIO_filename            ; filename as input parameter in buffer, length not used
        beq     CIO_invalid             ; GETCWD not supported yet
        bcs     CIO_call_a              ; other commands: assume no buffer
; not reached

; enable ROM, call CIO, disable ROM

CIO_call_a:
        lda     CIO_a

CIOV_call:
        pha
        lda     PORTB
        sta     cur_CIOV_PORTB
        enable_rom
        pla
        jsr     CIOV_org
        php
        pha
        disable_rom_val cur_CIOV_PORTB
        pla
        plp
        rts


CIO_write_jmp:
        jmp     CIO_write

CIO_invalid:
        lda     CIO_a
        ldy     #DINVCM
        rts

; READ handler
; ------------

CIO_read:
        lda     ICBLL,x
        ora     ICBLH,x
        beq     CIO_call_a              ; special I/O through A register in case buffer length is 0

.if CHKBUF
        jsr     chk_CIO_buf
        bcc     CIO_call_a
.endif

; If the data length is larger than our bounce buffer, we have to split the request into smaller ones.
; Otherwise we can get away with one call and a copy to the final destination afterwards.

        lda     ICBLH,x                 ; get high byte of length
        bne     big_read                ; not zero -> data too large for our buffers
                                        ; CHANGE HERE TO SUPPORT BOUNCE BUFFERS > 255 BYTES
        lda     #<BUFSZ
        cmp     ICBLL,x
        bcc     big_read

; Data size fits into bounce buffer

        jsr     setup_zpptr1
        jsr     bncbuf_to_iocb
        jsr     CIO_call_a              ; call CIO
        php
        bpl     @no_err
        cpy     #EOFERR
        beq     @no_err
        pha
        jsr     restore_icba
        pla
        plp
        rts                             ; return with error

@no_err:
        sta     CIO_a
        sty     CIO_y

        jsr     copy_to_user            ; copy data into user buffer
        jsr     restore_icba

        lda     CIO_a
        ldy     CIO_y
        plp
        rts                             ; return with success

; Data size does not fit into bounce buffer

big_read:
        lda     #0
        sta     retlen                  ; initialize return length
        sta     retlen+1
        jsr     iocblen_to_orig_len
        jsr     iocbptr_to_orig_ptr
        jsr     setup_zpptr1
        jsr     bncbuf_to_iocb          ; let ICBAL/ICBAH point to bounce buffer

br_loop:
        jsr     cmp_orig_len_bnc_bufsz  ; is transfer length > bounce buffer size?
        bcs     br_last                 ; no, last transfer, use remaining size

        lda     #>BUFSZ
        sta     ICBLH,x                 ; set data length
        lda     #<BUFSZ
        sta     ICBLL,x
        bne     br_cont

br_last:
        lda     orig_len+1
        sta     ICBLH,x                 ; set data length
        lda     orig_len
        sta     ICBLL,x

br_cont:
        sta     req_len                 ; remember length of this request
        lda     ICBLH,x
        sta     req_len+1
        jsr     CIO_call_a              ; do the request
        php
        bpl     br_no_err
        cpy     #EOFERR
        beq     br_no_err

        pha
        jsr     restore_icba
        pla
        plp
        rts                             ; return with error

br_no_err:
        sta     CIO_a
        sty     CIO_y
        pla
        sta     CIO_p
        jsr     copy_to_user

; update retlen
        clc
        lda     retlen
        adc     ICBLL,x
        sta     retlen
        lda     retlen+1
        adc     #0
        sta     retlen+1

; if the request read less bytes than requested, we're done
        lda     ICBLL,x
        cmp     req_len
        bne     br_done
        lda     ICBLH,x
        cmp     req_len+1
        bne     br_done

; update user buffer pointer (zpptr1)
        clc
        lda     zpptr1
        adc     ICBLL,x
        sta     zpptr1
        lda     zpptr1+1
        adc     #0
        sta     zpptr1+1

; update remaining length
        sec
        lda     orig_len
        sbc     ICBLL,x
        sta     orig_len
        lda     orig_len+1
        sbc     #0
        sta     orig_len+1

; still something left to do (remaining length != 0)?
        lda     orig_len
        ora     orig_len+1
        beq     br_done
        jmp     br_loop

; done, write original buffer pointer and total transfer length to IOCB and return to application
br_done:
        lda     retlen
        sta     ICBLL,x
        lda     retlen+1
        sta     ICBLH,x
        jsr     orig_ptr_to_iocbptr
        lda     CIO_p
        pha
        lda     CIO_a
        ldy     CIO_y
        plp
        rts                             ; return with success



CIO_call_a_jmp:
        jmp     CIO_call_a



; WRITE handler
; -------------


CIO_write:
        lda     ICBLL,x
        ora     ICBLH,x
        beq     CIO_call_a_jmp          ; special I/O through A register in case buffer length is 0

.if CHKBUF
        jsr     chk_CIO_buf
        bcc     CIO_call_a_jmp
.endif

; If the data length is larger than our bounce buffer, we have to split the request into smaller ones.
; Otherwise we can get away with a copy to the bounce buffer and the call.

        lda     ICBLH,x                 ; get high byte of length
        bne     big_write               ; not zero -> data too large for our buffers
                                        ; CHANGE HERE TO SUPPORT BOUNCE BUFFERS > 255 BYTES
        lda     #<BUFSZ
        cmp     ICBLL,x
        bcc     big_write


; Data size fits into bounce buffer

        jsr     setup_zpptr1
        jsr     bncbuf_to_iocb
        jsr     copy_from_user
        ldy     CIO_y
        jsr     CIO_call_a
        php
        pha
        jsr     restore_icba
        pla
        plp
        rts                             ; return to application


; Data size does not fit into bounce buffer

big_write:
        lda     #0
        sta     retlen                  ; initialize return length
        sta     retlen+1
        jsr     iocblen_to_orig_len
        jsr     iocbptr_to_orig_ptr
        jsr     setup_zpptr1
        jsr     bncbuf_to_iocb          ; let ICBAL/ICBAH point to bounce buffer

bw_loop:
        jsr     cmp_orig_len_bnc_bufsz  ; is transfer length > bounce buffer size?
        bcs     bw_last                 ; no, last transfer, use remaining size

        lda     #>BUFSZ
        sta     ICBLH,x                 ; set data length
        lda     #<BUFSZ
        sta     ICBLL,x
        bne     bw_cont

bw_last:
        lda     orig_len+1
        sta     ICBLH,x                 ; set data length
        lda     orig_len
        sta     ICBLL,x

bw_cont:
        sta     req_len                 ; remember length of this request
        lda     ICBLH,x
        sta     req_len+1
        jsr     copy_from_user
        jsr     CIO_call_a              ; do the request
        php
        bpl     bw_no_err

        plp
        rts                             ; error return

bw_no_err:
        sta     CIO_a
        sty     CIO_y
        pla
        sta     CIO_p

; update retlen
        clc
        lda     retlen
        adc     ICBLL,x
        sta     retlen
        lda     retlen+1
        adc     #0
        sta     retlen+1

; if the request wrote less bytes than requested, we're done
        lda     ICBLL,x
        cmp     req_len
        bne     bw_done
        lda     ICBLH,x
        cmp     req_len+1
        bne     bw_done

; update user buffer pointer (zpptr1)
        clc
        lda     zpptr1
        adc     ICBLL,x
        sta     zpptr1
        lda     zpptr1+1
        adc     #0
        sta     zpptr1+1

; update remaining length
        sec
        lda     orig_len
        sbc     ICBLL,x
        sta     orig_len
        lda     orig_len+1
        sbc     #0
        sta     orig_len+1

; still something left to do (remaining length != 0)?
        lda     orig_len
        ora     orig_len+1
        beq     bw_done
        jmp     bw_loop

bw_done:
        lda     retlen
        sta     ICBLL,x
        lda     retlen+1
        sta     ICBLH,x
        jsr     orig_ptr_to_iocbptr
        lda     CIO_p
        pha
        lda     CIO_a
        ldy     CIO_y
        plp
        rts                             ; return with success



; check if length is larger than bounce buffer size
; input:   orig_len - length
; output:         A - destroyed
;                CF - 0/1 for larger/not larger
cmp_orig_len_bnc_bufsz:
        sec
        lda     #<BUFSZ
        sbc     orig_len
        lda     #>BUFSZ
        sbc     orig_len+1
        rts


; copy data from bounce buffer into user buffer
; input:   X - IOCB index
;     zpptr1 - pointer to user buffer
; output:  A - destroyed
;          Y - 0
copy_to_user:
        ldy     ICBLL,x                 ; get # of bytes read (CHANGE HERE TO SUPPORT BOUNCE BUFFERS > 255 BYTES)
        beq     @copy_done
@copy:  dey
        lda     bounce_buffer,y
        sta     (zpptr1),y
        cpy     #0
        bne     @copy
@copy_done:
        rts


; copy data from user buffer into bounce buffer
; input:   X - IOCB index
;     zpptr1 - pointer to user buffer
; output:  A - destroyed
;          Y - 0
copy_from_user:
        ldy     ICBLL,x                 ; get # of bytes to write (CHANGE HERE TO SUPPORT BOUNCE BUFFERS > 255 BYTES)
        beq     @copy_done
@copy:  dey
        lda     (zpptr1),y
        sta     bounce_buffer,y
        cpy     #0
        bne     @copy
@copy_done:
        rts


; copy ICBLL/ICBLH to 'orig_len'
; input:   X - IOCB index
; output:  A - destroyed
iocblen_to_orig_len:
        lda     ICBLL,x
        sta     orig_len
        lda     ICBLH,x
        sta     orig_len+1
        rts


; copy ICBAL/ICBAH to 'orig_ptr'
; input:   X - IOCB index
; output:  A - destroyed
iocbptr_to_orig_ptr:
        lda     ICBAL,x
        sta     orig_ptr
        lda     ICBAH,x
        sta     orig_ptr+1
        rts


; copy 'orig_ptr' to ICBAL/ICBAH
; input:   X - IOCB index
; output:  A - destroyed
orig_ptr_to_iocbptr:
        lda     orig_ptr
        sta     ICBAL,x
        lda     orig_ptr+1
        sta     ICBAH,x
        rts


; restore original contents of ICBAL/ICBAH from 'zpptr1'
; input:   X - IOCB index
; output:  A - destroyed
restore_icba:
        lda     zpptr1
        sta     ICBAL,x
        lda     zpptr1+1
        sta     ICBAH,x
        rts


; put bounce buffer address into ICBAL/ICBAH
; input:   X - IOCB index
; output:  A - destroyed
bncbuf_to_iocb:
        lda     #<bounce_buffer
        sta     ICBAL,x
        lda     #>bounce_buffer
        sta     ICBAH,x
        rts


; copy file name pointed to by 'zpptr1' to 'bounce_buffer'
; input:   Y - index into file name buffer and bounce_buffer
; output:  Y - points to first invalid byte after file name
;          A - destroyed
copy_filename:
        lda     (zpptr1),y
        sta     bounce_buffer,y
        beq     copy_fn_done
        iny
        cmp     #ATEOL
        bne     copy_filename
        dey
copy_fn_done:
        rts


; write IOCB buffer address into zpptr1
; input:   X - IOCB index
; output:  Y - 0 (for setup_zpptr1_y0, else unchanged)
;          A - destroyed
setup_zpptr1_y0:
        ldy     #0
setup_zpptr1:
        lda     ICBAL,x                 ; put buffer address into zp pointer
        sta     zpptr1
        lda     ICBAH,x
        sta     zpptr1+1
        rts


.if CHKBUF

; get length of file name pointed to by 'zpptr1'
; input:   Y - index into file name
; output:  Y - length
;          A - destroyed
get_fn_len:
        lda     (zpptr1),y
        beq     @done
        iny
        cmp     #ATEOL
        bne     get_fn_len
        dey
@done:
        rts


chk_CIO_buf_fn2:
        tya
        pha
        lda     ICBLL,x
        pha
        lda     ICBLH,x
        pha
        jsr     setup_zpptr1_y0
        jsr     get_fn_len
        iny                     ; include terminating zero
        bne     fn_cont

chk_CIO_buf_fn:
        tya
        pha
        lda     ICBLL,x
        pha
        lda     ICBLH,x
        pha
        jsr     setup_zpptr1_y0
fn_cont:jsr     get_fn_len
        iny                     ; include terminating zero
        tya
        sta     ICBLL,x
        lda     #0
        sta     ICBLH,x
        jsr     chk_CIO_buf
        pla     
        sta     ICBLH,x
        pla     
        sta     ICBLL,x
        pla
        tay
        rts


; check if a CIO input/output buffer overlaps with ROM area (>= $C000)
; input:                      X - IOCB index
;       ICBAL/ICBAH/ICBLL/ICBLH - buffer address and length
; output:                    CF - 1/0 for overlap/no overlap
;                             A - destroyed

chk_CIO_buf:
        lda     ICBAH,x
        cmp     #$c0
        bcc     @cont
@ret:   
.ifdef DEBUG
        jsr     CIO_buf_noti
.endif
        rts

@cont:  lda     ICBAL,x
        clc
        adc     ICBLL,x
        lda     ICBAH,x
        adc     ICBLH,x
        bcs     @ret            ; ??? wraparound
        cmp     #$c0
.ifdef DEBUG
        jsr     CIO_buf_noti
.endif
        rts

.ifdef DEBUG
; write to screen memory on 2nd line:
; pos 0: # of accesses without buffering
; pos 1: # of accesses with buffering
CIO_buf_noti:
        php
        pha
        tya
        pha
        bcc     @nobuf

        inc     CIObnval_dobuf
        jmp     @cont

@nobuf: inc     CIObnval_nobuf

@cont:  ldy     #40
        lda     CIObnval_nobuf
        sta     (SAVMSC),y
        ldy     #41
        lda     CIObnval_dobuf
        sta     (SAVMSC),y

        pla
        tay
        pla
        plp
        rts

CIObnval_dobuf:
        .byte   0
CIObnval_nobuf:
        .byte   0
.endif

.endif  ; .if CHKBUF

;---------------------------------------------------------

; SIO handler
; We only handle SIO_STAT, SIO_READ, SIO_WRITE, and SIO_WRITEV.
; These are the only functions used by the runtime library currently.
; For other function we return NVALID status code.

SIO_handler:
        lda     DCOMND                  ; get command
        cmp     #SIO_STAT
        beq     SIO_stat
        cmp     #SIO_READ
        beq     SIO_read
        cmp     #SIO_WRITE
        beq     SIO_write
        cmp     #SIO_WRITEV
        beq     SIO_write

        ; unhandled command
        lda     #NVALID
SIO_err:sta     DSTATS
        rts

; SIO_STAT is always called with a low buffer (by the runtime)
SIO_stat:
        ; fall thru

SIO_call:
        lda     PORTB
        sta     cur_SIOV_PORTB
        enable_rom
        jsr     SIOV_org
        php
        pha
        disable_rom_val cur_SIOV_PORTB
        pla
        plp
        rts


; SIO read handler
; ----------------

SIO_read:

.if CHKBUF
        jsr     chk_SIO_buf
        bcc     SIO_call
.endif

; we only support transfers <= bounce buffer size
        jsr     cmp_sio_len_bnc_bufsz
        bcs     sio_read_len_ok

        lda     #DERROR         ; don't know a better status code for this
        bne     SIO_err

sio_read_len_ok:
        lda     DBUFLO
        sta     zpptr1          ; remember destination buffer address
        lda     DBUFHI
        sta     zpptr1+1

        jsr     bncbuf_to_dbuf  ; put bounce buffer address to DBUFLO/DBUFHI

        jsr     SIO_call        ; do the operation
        pha
        lda     DSTATS          ; get status
        bmi     sio_read_ret    ; error

        ; copy data to user buffer
sio_read_ok:
        lda     DBYTHI          ; could be 1 for 256 bytes
        beq     srok1
        ldy     #0
        beq     srok2
srok1:  ldy     DBYTLO
srok2:  dey
sio_read_copy:
        lda     bounce_buffer,y
        sta     (zpptr1),y
        dey
        cpy     #$ff
        bne     sio_read_copy

sio_read_ret:
        jsr     orgbuf_to_dbuf

        pla
        rts                     ; success return


; SIO write handler
; -----------------

SIO_write:

.if CHKBUF
        jsr     chk_SIO_buf
        bcc     SIO_call
.endif

; we only support transfers <= bounce buffer size
        jsr     cmp_sio_len_bnc_bufsz
        bcs     sio_write_len_ok

        lda     #DERROR         ; don't know a better status code for this
        jmp     SIO_err

sio_write_len_ok:
        lda     DBUFLO
        sta     zpptr1          ; get source buffer address
        lda     DBUFHI
        sta     zpptr1+1

        ; copy data from user buffer to bounce buffer
        lda     DBYTHI          ; could be 1 for 256 bytes
        beq     swok1
        ldy     #0
        beq     swok2
swok1:  ldy     DBYTLO
swok2:  dey
sio_write_copy:
        lda     (zpptr1),y
        sta     bounce_buffer,y
        dey
        cpy     #$ff
        bne     sio_write_copy

        jsr     bncbuf_to_dbuf  ; put bounce buffer address to DBUFLO/DBUFHI

        jsr     SIO_call        ; do the operation
        pha
        jsr     orgbuf_to_dbuf
        pla
        rts


; check if SIO length is larger than bounce buffer size
; input:   orig_len - length
; output:         A - destroyed
;                CF - 0/1 for larger/not larger
cmp_sio_len_bnc_bufsz:
        sec
        lda     #<BUFSZ_SIO
        sbc     DBYTLO
        lda     #>BUFSZ_SIO
        sbc     DBYTHI
        rts

; put bounce buffer address into DBUFLO/DBUFHI
; input:   (--)
; output:  A - destroyed
bncbuf_to_dbuf:
        lda     #<bounce_buffer
        sta     DBUFLO
        lda     #>bounce_buffer
        sta     DBUFHI
        rts

; put original buffer address into DBUFLO/DBUFHI
; input:   zpptr1 - original pointer
; output:  A      - destroyed
orgbuf_to_dbuf:
        lda     zpptr1
        sta     DBUFLO
        lda     zpptr1+1
        sta     DBUFHI
        rts


.if CHKBUF

; check if a SIO input/output buffer overlaps with ROM area (>= $C000)
; input: DBUFLO/DBUFHI/DBYTLO/DBYTHI - buffer address and length
; output:                         CF - 1/0 for overlap/no overlap
;                                  A - destroyed

chk_SIO_buf:
        lda     DBUFHI
        cmp     #$c0
        bcc     @cont
@ret:
.ifdef DEBUG
        jsr     SIO_buf_noti
.endif
        rts

@cont:  lda     DBUFLO
        clc
        adc     DBYTLO
        lda     DBUFHI
        adc     DBYTHI
        bcs     @ret            ; ??? wraparound
        cmp     #$c0
.ifdef DEBUG
        jsr     SIO_buf_noti
.endif
        rts

.ifdef DEBUG
; write to screen memory on 2nd line:
; pos 38: # of accesses without buffering
; pos 39: # of accesses with buffering
SIO_buf_noti:
        php
        pha
        tya
        pha
        bcc     @nobuf

        inc     SIObnval_dobuf
        jmp     @cont

@nobuf: inc     SIObnval_nobuf

@cont:  ldy     #78
        lda     SIObnval_nobuf
        sta     (SAVMSC),y
        ldy     #79
        lda     SIObnval_dobuf
        sta     (SAVMSC),y

        pla
        tay
        pla
        plp
        rts

SIObnval_dobuf:
        .byte   0
SIObnval_nobuf:
        .byte   0
.endif

.endif  ; .if CHKBUF

;---------------------------------------------------------

KEYBDV_handler:

        lda     #>(kret-1)
        pha
        lda     #<(kret-1)
        pha
        lda     PORTB
        sta     cur_KEYBDV_PORTB
        enable_rom
        lda     KEYBDV+5
        pha
        lda     KEYBDV+4
        pha
        rts             ; call keyboard handler
kret:   pha
        disable_rom_val cur_KEYBDV_PORTB
        pla
        rts

;---------------------------------------------------------

SETVBV_handler:

        pha
        lda     PORTB
        sta     cur_SETVBV_PORTB
        enable_rom
        pla
        jsr     SETVBV_org
        php
        pha
        disable_rom_val cur_SETVBV_PORTB
        pla
        plp
        rts

;---------------------------------------------------------

XMOVE_handler:

        pha
        lda     PORTB
        sta     cur_XMOVE_PORTB
        enable_rom
        pla
        jsr     XMOVE_org
        php
        pha
        disable_rom_val cur_XMOVE_PORTB
        pla
        plp
        rts


CIO_a:                  .res    1
CIO_x:                  .res    1
CIO_y:                  .res    1
CIO_p:                  .res    1
cur_CIOV_PORTB:         .res    1
cur_SIOV_PORTB:         .res    1
cur_KEYBDV_PORTB:       .res    1
cur_SETVBV_PORTB:       .res    1
cur_XMOVE_PORTB:        .res    1
orig_ptr:               .res    2
orig_len:               .res    2
req_len:                .res    2
retlen:                 .res    2

.endif  ; .ifdef __ATARIXL__