/ src / disk.s
disk.s
  1  ; CCGMS Terminal
  2  ;
  3  ; Copyright (c) 2016,2020, Craig Smith, alwyz. All rights reserved.
  4  ; This project is licensed under the BSD 3-Clause License.
  5  ;
  6  ; Send file or buffer to screen and/or modem
  7  ;
  8  
  9  ; bufflg
 10  ;   $80: 0: disk, 1: buffer
 11  ;   $40: 0: no delay, 1: delay
 12  ; buffl2:
 13  ;   0:   output to screen
 14  ;   !=0: output to screen and modem
 15  
 16  dskout:
 17  	jsr clrchn
 18  	jsr cursor_show
 19  	lda bufflg
 20  	bpl @dskmo	; -> disk
 21  
 22  ; buffer
 23  	jsr buffer_get_byte
 24  	bit bufflg
 25  	bvs @timdel	; -> buffer & delay
 26  	ldx #$ff
 27  :	dex		; short delay (about 1ms)
 28  	bne :-
 29  	beq @chstat	; always
 30  
 31  ; disk
 32  @dskmo:
 33  	jsr disablexfer
 34  	ldx #LFN_FILE
 35  	jsr chkin
 36  	jsr getin	; get byte from disk
 37  	pha
 38  	pla		; [XXX]
 39  @timdel:
 40  	bit bufflg
 41  	bvc @chstat	; -> no delay
 42  	jsr sleep_50ms
 43  @chstat:
 44  	pha
 45  	lda status
 46  	and #$40
 47  	jne @dskext	; -> EOF
 48  	jsr clrchn
 49  	jsr cursor_off
 50  	pla
 51  	pha
 52  	jsr handle_control_codes
 53  	jsr chrout
 54  	jsr quote_insert_off
 55  	ldx buffl2
 56  	bne @dskmo1	; non zero: also output to modem
 57  	pla
 58  	jmp @chkkey	; skip
 59  
 60  ; output to modem
 61  @dskmo1:
 62  	jsr clear232
 63  	jsr enablexfer
 64  	jsr clear232
 65  	pla
 66  	ldx ascii_mode
 67  	beq :+
 68  	jsr petscii_to_ascii
 69  :	jsr modput
 70  
 71  ; eat echo from modem
 72  ; (this timeout failsafe makes sure the byte is received back from modem
 73  ;  before accessing disk for another byte otherwise we can have
 74  ;  all sorts of nmi related issues.... this solves everything.
 75  ;  uses the 'fake' rtc / jiffy counter function / same as xmmget...)
 76  ; [XXX this is a duplicate of modget_timeout]
 77  	lda #70		; timeout failsafe
 78  	sta xmodel
 79  	lda #0
 80  	sta rtca1
 81  	sta rtca2
 82  	sta rtca0
 83  
 84  :	jsr modget	; get byte (and ignore)
 85  	jcc @chkkey	; done
 86  	jsr xmmrtc	; count up
 87  	lda rtca1
 88  	cmp xmodel
 89  	bcc :-		; retry until time is up
 90  
 91  @chkkey:
 92  	jsr get_key
 93  	jeq dskout	; loop
 94  
 95  ; handle keypress
 96  	cmp #3		; STOP key
 97  	beq @dskex2
 98  
 99  	jsr enablexfer
100  	cmp #'S'
101  	bne @nos
102  
103  ; 'S'
104  	lda bufflg	; only in memory mode
105  	bpl @nos
106  	jsr buffer_skip_256
107  	ldx status	; EOI?
108  	bne @dskex2	; end
109  	jsr enablexfer
110  	jmp dskout	; loop
111  @nos:
112  
113  :	jsr get_key
114  	beq :-
115  
116  	jsr enablexfer
117  	jmp dskout
118  @dskext:
119  	jsr enablexfer
120  	pla
121  @dskex2:
122  	jsr clrchn
123  	jmp cursor_off
124  
125  ;----------------------------------------------------------------------
126  get_key:
127  	jsr clrchn
128  	jsr getin
129  	cmp #0
130  	rts