/ src / buffer.s
buffer.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  ; Buffer manipulation functions
  7  ;
  8  
  9  ;----------------------------------------------------------------------
 10  SET_PETSCII
 11  txt_buffer:
 12  	.byte WHITE,"Buffer ",0
 13  
 14  txt_bufcmds:
 15  	.byte " bytes free.  "
 16  	.byte CR,HILITE
 17  	.byte "open  "
 18  	.byte HILITE
 19  	.byte "close  "
 20  	.byte HILITE
 21  	.byte "erase  "
 22  	.byte HILITE
 23  	.byte "transfer"
 24  	.byte CR,HILITE
 25  	.byte "load  "
 26  	.byte HILITE
 27  	.byte "save   "
 28  	.byte HILITE
 29  	.byte "print  "
 30  	.byte HILITE
 31  	.byte "view: "
 32  	.byte 0
 33  
 34  txt_open:
 35  	.byte "Open",0
 36  
 37  txt_closed:
 38  	.byte "Closed",0
 39  
 40  txt_erase_buffer:
 41  	.byte  "Erase Buffer! - "
 42  	.byte HILITE
 43  	.byte "yes or "
 44  	.byte HILITE
 45  	.byte "no?       "
 46  	.byte CSR_LEFT,CSR_LEFT,CSR_LEFT,15
 47  	.byte CSR_LEFT,CSR_LEFT,CSR_LEFT,0
 48  
 49  txt_sending_buffer:
 50  	.byte CR,CR
 51  	.byte "Sending buffer..."
 52  	.byte CR,CR,00
 53  
 54  txt_done:
 55  	.byte CR,CR,WHITE
 56  	.byte "Done."
 57  	.byte CR,0
 58  
 59  txt_reu:
 60  	.byte WHITE,"REU ",0
 61  SET_ASCII
 62  
 63  ;----------------------------------------------------------------------
 64  print_buffer_info:
 65  	lda reu_enabled
 66  	beq :+
 67  	lda #<txt_reu
 68  	ldy #>txt_reu
 69  	jsr outstr
 70  :	lda #<txt_buffer
 71  	ldy #>txt_buffer
 72  	jsr outstr
 73  	lda buffer_open
 74  	beq @1
 75  	lda #<txt_open
 76  	ldy #>txt_open
 77  	clc
 78  	bcc @2
 79  @1:	lda #<txt_closed
 80  	ldy #>txt_closed
 81  @2:	jmp outstr
 82  
 83  ;----------------------------------------------------------------------
 84  print_buffer_menu:
 85  	lda #CR
 86  	jsr chrout
 87  	jsr print_buffer_info
 88  	lda #' '
 89  	jsr chrout
 90  	lda #'-'
 91  	jsr chrout
 92  	lda #' '
 93  	jsr chrout
 94  	lda bufend
 95  	sec
 96  	sbc buffer_ptr
 97  	tax
 98  	lda bufend+1
 99  	sbc buffer_ptr+1
100  	jsr outnum	; # of bytes free
101  	lda #<txt_bufcmds
102  	ldy #>txt_bufcmds
103  	jmp outstr
104  
105  ;----------------------------------------------------------------------
106  handle_f4_buffer:
107  	ldx SHFLAG
108  	cpx #SHFLAG_CBM
109  	jeq cf3_multi_receive
110  
111  	jsr text_color_save
112  bufask
113  	lda #CR
114  	jsr chrout
115  	jsr print_buffer_menu
116  bufwat
117  	jsr invert_csr_char
118  :	jsr getin
119  	beq :-
120  	and #$7f
121  	pha
122  	jsr restore_csr_char
123  	pla
124  	cmp #CR
125  	bne bufcmd
126  
127  return_to_term:
128  	lda #' '
129  	jsr chrout
130  	lda #CR
131  	jsr chrout
132  	jsr chrout
133  	jsr text_color_restore
134  	jsr enablexfer
135  	jmp term_mainloop
136  
137  bufcmd:
138  ; O: open
139  	cmp #'O'
140  	bne @no1
141  	ldx #1
142  	stx buffer_open
143  	bne bufex1
144  @no1:
145  
146  ; C: close
147  	cmp #'C'
148  	bne no2
149  	ldx #0
150  	stx buffer_open
151  bufex1
152  	ora #$80
153  bufexa
154  	jsr outcap
155  	lda #CR
156  	jsr chrout
157  	lda #CSR_UP
158  	ldx #4
159  :	jsr chrout
160  	dex
161  	bpl :-
162  	jmp bufask
163  no2
164  
165  ; E: erase
166  	cmp #'E'
167  	bne @no3
168  	ora #$80
169  	jsr outcap
170  	lda #CR
171  	jsr chrout
172  	lda #CSR_UP
173  	ldx #2
174  :	jsr chrout
175  	dex
176  	bpl :-
177  	lda #<txt_erase_buffer
178  	ldy #>txt_erase_buffer
179  	jsr outstr
180  	jsr invert_csr_char
181  :	jsr getin
182  	beq :-
183  	and #$7f
184  	cmp #'N'
185  	beq :+
186  	cmp #'Y'
187  	bne :-
188  	jsr bufclr
189  :	jsr restore_csr_char
190  	lda #CSR_UP
191  	jsr chrout
192  	jsr chrout
193  	jmp bufask
194  @no3
195  
196  ; P: print
197  	cmp #'P'
198  	bne @no4
199  	ora #$80
200  	jsr outcap
201  	jmp print_buffer
202  @no4
203  
204  ; V: view
205  	cmp #'V'
206  	bne no5
207  	lda #CLR
208  	jsr chrout
209  	lda #$80
210  	sta bufflg
211  	and #0		; [XXX lda #0]
212  	sta buffl2
213  	jsr prtbuf
214  	jmp term_mainloop
215  
216  ;----------------------------------------------------------------------
217  ; buf to screen
218  prtbuf:
219  	lda buffst
220  	pha
221  	lda buffst+1
222  	pha
223  	lda #$2f
224  	sta $00
225  	lda #$36	; disable BASIC ROM
226  	sta $01
227  	jsr dskout
228  	lda #$37	; enable BASIC ROM
229  	sta $01
230  	pla
231  	sta buffst+1
232  	pla
233  	sta buffst
234  	rts
235  
236  ;----------------------------------------------------------------------
237  buffer_get_byte:
238  	ldx buffst
239  	cpx buffer_ptr
240  	bcc memok
241  	ldx buffst+1
242  	cpx buffer_ptr+1
243  	bcc memok
244  memgab	ldx #$40	; EOI
245  	stx status
246  	rts
247  
248  memok	ldy reu_enabled
249  	beq memok2
250  	jsr reuread
251  	jmp memok3
252  memok2
253  	ldy #0
254  	lda (buffst),y
255  memok3
256  	inc buffst
257  	bne memext
258  	inc buffst+1
259  memext
260  	ldx #0
261  	stx status
262  	rts
263  
264  ;----------------------------------------------------------------------
265  buffer_skip_256:
266  	lda buffst+1
267  	cmp buffer_ptr+1
268  	bcs memgab	; EOI
269  	inc buffst+1
270  skpbf2
271  	lda buffst+1
272  	cmp buffer_ptr+1
273  	bcc memext	; OK
274  	lda buffst
275  	cmp buffer_ptr
276  	bcs memgab	; EOI
277  	bcc memext	; OK
278  no5
279  
280  ; S: save
281  	cmp #'S'
282  	jne no6
283  	jsr solfil
284  	jmp savbuf
285  solfil
286  	ora #$80
287  	jsr outcap
288  	lda #CR
289  	jsr chrout
290  	jsr chrout
291  	jsr ui_get_filename
292  	bne solfok
293  	jmp ui_abort
294  solfok	rts
295  savbuf
296  	jsr disablexfer;to be save 5-13 fix?? worked without it, but this should be here
297  	lda #0
298  	sta mulcnt
299  	lda #2
300  	sta filetype
301  	jsr dowsfn
302  	lda #$36
303  	sta $01
304  	lda buffst;start of buffer
305  	sta $c1;I/O Start Address ($c1 $c2)
306  	lda buffst+1
307  	sta $c2
308  	lda buffer_ptr;end of buffer
309  	clc
310  	adc #1
311  	sta $ae;Tape End Addresses/End of Program ($ae / $af)
312  	lda buffer_ptr+1
313  	adc #0
314  	sta $af
315  	lda #$61
316  	sta $b9
317  	jsr $f3d5;open file on serial bus
318  	jsr $f68f;print saving and filename
319  	lda $ba
320  	jsr $ed0c;send listen to serial bus
321  	lda $b9
322  	jsr $edb9;LSTNSA. Send LISTEN secondary address to serial bus
323  	ldy #0
324  	jsr $fb8e;Move the Tape SAVE/LOAD Address into the Pointer at 172 ($ac)
325  	lda reu_enabled
326  	beq afuckit
327  	jsr af624
328  	lda #0
329  	sta buffst
330  	sta buffst+1
331  	jmp :+
332  afuckit	jsr $f624
333  :	php
334  	lda #$37
335  	sta $01
336  	plp
337  	bcc bsaved
338  	lda #CR
339  	jsr chrout
340  	jsr bell
341  	lda #0
342  	sta buffst
343  	sta buffst+1
344  	jmp ui_abort
345  
346  ;----------------------------------------------------------------------
347  ;reu needs a special save routine cause craig decided to be all fancy with this one :)
348  af624	jsr $fcd1	; check the tape read/write pointer
349  	bcs @end
350  ;	lda ($ac),y
351  	jsr reuread
352  	jsr $eddd	; send a byte to an i/o device over the serial bus
353  	jsr $ffe1	; stop. query stop key indicator, at memory address $0091; if pressed, call clrchn and clear keyboard buffer.
354  	bne :+
355  	jsr $f642
356  	lda #0
357  	sec
358  	rts
359  :	inc buffst
360  	lda buffst
361  	beq :+
362  	jsr $fcdb
363  	bne af624
364  :	inc buffst+1
365  	jsr $fcdb;advance tape pointer
366  	bne af624
367  @end:	jsr $edfe;UNLSTN.
368  	jmp $f642
369  ;----------------------------------------------------------------------
370  
371  bsaved
372  	jsr enablexfer
373  	jmp return_to_term
374  no6
375  
376  ; L: load
377  	cmp #'L'
378  	bne bufcm7
379  	jsr solfil
380  lodbuf
381  	jsr disablexfer;5-13 put in, didnt seem to need it, need to test with it. might crash with it cause the program does that sometimes....
382  	lda #2
383  	ldx device_disk
384  	tay
385  	jsr setlfs
386  	lda max
387  	ldx #<inpbuf
388  	ldy #>inpbuf
389  	jsr setnam
390  	jsr open
391  	ldx #LFN_FILE
392  	jsr chkin
393  lodbfl
394  	jsr getin
395  	ldx status
396  	bne lodbex
397  	ldx buffer_ptr
398  	cpx bufend
399  	bne lodbok
400  	ldx buffer_ptr+1
401  	cpx bufend+1
402  	beq lodbex
403  lodbok
404  	ldy reu_enabled
405  	beq lodbokram
406  	jsr reuwrite
407  	jmp lodbokreu
408  lodbokram
409  	ldy #0
410  	sta (buffer_ptr),y
411  lodbokreu
412  	inc buffer_ptr
413  	bne lodbfl
414  	inc buffer_ptr+1
415  	bne lodbfl
416  lodbex
417  	jsr clrchn
418  	lda #2
419  	jsr close
420  	jsr enablexfer
421  	jmp return_to_term
422  bufcm7
423  
424  ; T: transfer
425  	cmp #'T'
426  	beq send_buffer
427  
428  ; </>
429  	cmp #'<'
430  	beq :+
431  	cmp #'>'
432  	bne bufbak
433  :	jsr switch_buffer
434  	jmp bufexa
435  
436  bufbak
437  	jmp bufwat
438  
439  
440  send_buffer:
441  	ora #$80
442  	jsr outcap
443  	lda #<txt_sending_buffer
444  	ldy #>txt_sending_buffer
445  	jsr outstr
446  	lda #$ff
447  	sta bufflg
448  	sta buffl2
449  	jsr prtbuf
450  	jsr text_color_save
451  	jsr clear232
452  	lda #<txt_done
453  	ldy #>txt_done
454  	jsr outstr
455  	jsr text_color_restore
456  	jsr enablexfer
457  	jmp term_mainloop
458  
459  ;----------------------------------------------------------------------
460  switch_buffer:
461  	pha
462  	cmp #'>'
463  	beq @3
464  	lda buffer_ptr+1
465  	cmp #>endprg
466  	bne @1
467  	lda buffer_ptr
468  	cmp #<endprg
469  	beq @end
470  @1:	lda buffer_ptr
471  	bne @2
472  	dec buffer_ptr+1
473  @2:	dec buffer_ptr
474  	jmp @end
475  
476  @3:	lda buffer_ptr+1
477  	cmp bufend
478  	bne @4
479  	lda buffer_ptr
480  	cmp bufend+1
481  	beq @end
482  @4:	inc buffer_ptr
483  	bne @end
484  	inc buffer_ptr+1
485  
486  @end:	ldx #1
487  	stx KOUNT
488  	pla
489  	rts
490  
491  ;----------------------------------------------------------------------
492  SET_PETSCII
493  txt_device:
494  	.byte CR,CR,"Device",0
495  txt_sec_addr:
496  	.byte CR,"SEC.A.: ",0
497  txt_printing:
498  	.byte CLR,CR,"PRINTING...",CR,0
499  SET_ASCII
500  
501  ;----------------------------------------------------------------------
502  print_buffer:
503  	lda #<txt_device
504  	ldy #>txt_device
505  	ldx #1
506  	jsr inpset
507  	lda #'4'
508  	jsr chrout
509  	jsr inputl
510  	bne @2
511  @1:	lda #CR
512  	jsr chrout
513  	jmp ui_abort
514  
515  @2:	lda inpbuf
516  	cmp #'3'
517  	bcc @1
518  	cmp #'6'
519  	bcs @1
520  	and #$0f
521  	pha
522  	lda #<txt_sec_addr
523  	ldy #>txt_sec_addr
524  	ldx #1
525  	jsr inpset
526  	lda #'7'
527  	jsr chrout
528  	jsr inputl
529  	beq @1
530  	lda inpbuf
531  	cmp #'0'
532  	bcc @1
533  	cmp #'9'+1
534  	bcs @1
535  	and #$0f
536  	tay
537  	pla
538  	tax
539  	lda #LFN_PRINTER
540  	jsr setlfs
541  	lda #0
542  	jsr setnam
543  	lda #<txt_printing
544  	ldy #>txt_printing
545  	jsr outstr
546  	jsr open
547  	ldx status
548  	bne @3
549  	lda buffst
550  	pha
551  	lda buffst+1
552  	pha
553  	lda #$2f
554  	sta $00
555  	lda #$36	; disable BASIC ROM
556  	sta $01
557  	jsr print_buffer_bytes
558  	lda #$37	; enable BASIC ROM
559  	sta $01
560  	pla
561  	sta buffst+1
562  	pla
563  	sta buffst
564  @3:	lda #LFN_PRINTER
565  	jsr close
566  	lda #<txt_done
567  	ldy #>txt_done
568  	jsr outstr
569  	jsr text_color_restore
570  	jsr enablexfer
571  	jmp term_mainloop
572  
573  ;----------------------------------------------------------------------
574  print_buffer_bytes:
575  	jsr buffer_get_byte
576  	bne @3
577  	pha
578  	and #$7f
579  	cmp #CR
580  	beq @1
581  	cmp #' '
582  	bcc @2
583  @1:	ldx #LFN_PRINTER
584  	jsr chkout
585  	pla
586  	jsr chrout
587  	ldx status
588  	bne @3
589  	jmp print_buffer_bytes
590  @2:	pla
591  	jmp print_buffer_bytes
592  @3:	jmp clrchn