/ src / dir.s
dir.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  ; Show disk directory
  7  ;
  8  
  9  ;----------------------------------------------------------------------
 10  dir_echo_to_modem:
 11  	.byte 0		; 0: f6, 1: c=f6
 12  dirfn:
 13  	.byte '$'
 14  
 15  ;----------------------------------------------------------------------
 16  ; Show the disk directory, optionally send it to the modem
 17  ; (the filename has to be set already)
 18  dir:
 19  	jsr disablexfer
 20  	lda #LFN_DIR
 21  	ldx device_disk
 22  	ldy #0
 23  	jsr setlfs
 24  	jsr is_drive_present
 25  	jmi dir_exit	; no
 26  	jsr clrchn
 27  	jsr text_color_save
 28  	lda #CR
 29  	jsr chrout
 30  	jsr open
 31  	lda #0
 32  	sta dir_echo_to_modem
 33  	lda SHFLAG
 34  	cmp #SHFLAG_CBM	; c= f6
 35  	bne :+
 36  	lda #1
 37  	sta dir_echo_to_modem
 38  :	ldx #LFN_DIR
 39  	jsr chkin
 40  	ldy #3		; skip 4 bytes (load address and link pointer)
 41  @loop1:	jsr getch
 42  	dey
 43  	bpl @loop1
 44  	jsr getch	; blocks/line number (lo)
 45  	sta tmp0b
 46  	jsr getch	; blocks/line number (lo)
 47  	ldx tmp0b
 48  	jsr outnum	; print blocks
 49  	lda #' '
 50  	jsr chrout
 51  @loop2:	jsr getch
 52  	ldx dir_echo_to_modem
 53  	beq @1
 54  	cmp #0
 55  	beq @2
 56  	cmp #$20	; suppress special chars if we need to read back
 57  	bcc @loop2	; the screen later [XXX not enough, also $80-$9F]
 58  @1:	jsr chrout
 59  	bne @loop2
 60  @2:	jsr dir_once_per_line
 61  	ldy #1		; skip 2 bytes next time (link pointer)
 62  	bne @loop1
 63  
 64  ;----------------------------------------------------------------------
 65  getch:
 66  	jsr getin
 67  	ldx status
 68  	bne dir_cancel
 69  	cmp #0
 70  	rts
 71  dir_cancel:
 72  	pla
 73  	pla
 74  dir_exit:
 75  	jsr clrchn
 76  	jsr text_color_restore
 77  	lda #LFN_DIR
 78  	jsr chrout	; [XXX ugly: LFN matches code for CR]
 79  	jsr close
 80  	jmp enablexfer
 81  
 82  ;----------------------------------------------------------------------
 83  dir_once_per_line:
 84  	lda #CR
 85  	jsr chrout
 86  	jsr clrchn
 87  	jsr getin	; keyboard
 88  	beq @nokey
 89  
 90  ; keypress
 91  	cmp #3		; STOP
 92  	beq dir_cancel
 93  ; pause output until another keypress
 94  	lda #0
 95  	sta NDX		; clear keyboard queue
 96  :	jsr getin	; wait for key
 97  	beq :-
 98  @nokey:
 99  
100  	ldx dir_echo_to_modem
101  	beq @skip
102  
103  ; send line to modem by reading back the printed line from the screen
104  	lda #CSR_UP
105  	jsr chrout	; position cursor over last printed line
106  	lda #3
107  	sta DFLTN	; input from screen
108  	ldy #0
109  @loop:
110  ; eat all bytes from the modem
111  	lda #5
112  	sta timeout
113  	jsr modget_timeout
114  	bcs :+		; nothing
115  	jmp @loop	; [XXX bcc @loop would work]
116  :
117  
118  	jsr disablexfer
119  	jsr getin	; input from screen
120  	jsr enablexfer
121  	jsr modput
122  	tya
123  	pha
124  	lda #21
125  	sta timeout
126  	jsr modget_timeout; eat echo
127  	pla
128  	tay
129  	iny
130  	cpy #27		; max with (will send extra spaces at the end)
131  	bcc @loop
132  	lda #CR
133  	jsr modput
134  	jsr clrchn
135  	lda #CR
136  	jsr chrout	; screen
137  
138  ; eat all bytes in the RS232 buffer
139  :	jsr modget
140  	lda RIDBE
141  	cmp RIDBS	; [XXX isn't this what clear232 does?]
142  	bne :-
143  
144  @skip:
145  	jsr clrchn
146  	ldx #LFN_DIR
147  	jmp chkin
148  
149  ;----------------------------------------------------------------------
150  is_drive_present:
151  	lda #0
152  	sta status
153  	lda device_disk
154  	jsr $ed0c	; LISTEN
155  	lda #$f0
156  	jsr $edbb	; SECND
157  	ldx status
158  	bmi :+
159  	jsr $f654	; UNLISTEN
160  	lda #0
161  :	rts
162  
163  ;----------------------------------------------------------------------
164  ; get character with timeout
165  ;
166  ; (this timeout failsafe makes sure the byte is received back from modem
167  ;  before accessing disk for another byte otherwise we can have
168  ;  all sorts of nmi related issues.... this solves everything.
169  ;  uses the 'fake' rtc / jiffy counter function / same as xmmget...)
170  modget_timeout:
171  timeout=*+1
172  	lda #10		; timeout failsafe
173  	sta xmodel
174  	lda #0
175  	sta rtca1
176  	sta rtca2
177  	sta rtca0
178  @1:	jsr modget
179  	bcs :+		; [XXX bcc @rts]
180  	jmp @rts
181  :	jsr xmmrtc
182  	lda rtca1
183  	cmp xmodel
184  	bcc @1
185  @rts:	rts