-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloadsave.asm
More file actions
998 lines (877 loc) · 24.3 KB
/
Copy pathloadsave.asm
File metadata and controls
998 lines (877 loc) · 24.3 KB
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
; ============================================================================
; loadsave.asm — File I/O for PETProject
;
; All disk I/O uses the standard Kernal entry points (LOAD, SETNAM, SETLFS,
; OPEN, CHKIN/CHKOUT, CHRIN/CHROUT, CLOSE, CLRCHN). No fast-loader code or
; resident driver — fastloader cartridges or JiffyDOS installed by the user
; accelerate LOAD transparently.
;
; User file load path: OPEN/CHRIN, NOT Kernal LOAD. Reason: our save path
; (below) writes raw bytes via OPEN/CHROUT with no PRG header. Kernal
; LOAD always consumes the first 2 bytes of a file as a load-address
; header, which would corrupt header-less text files. OPEN/CHRIN reads
; every byte as data. Cost: fastloader cartridges only hook LOAD, so
; user file loads run at standard 1541 speed. Module loads (which DO
; have proper PRG headers) use Kernal LOAD in modules.asm and benefit
; from any installed fastloader.
;
; User file save path: simple synchronous OPEN/CHKOUT/CHROUT with "@0:"
; overwrite prefix. BASIC files are tokenized via MODTOK before
; writing, detokenized via MODDET after, to keep work_buf in plain text
; throughout.
; ============================================================================
PROMPT_NAME_COL = 6
PROMPT_MAX_LEN = 16
PROMPT_BLINK_MASK = $10
IO_LA = 2 ; logical file number we'll use for I/O
; ============================================================================
; do_load_file — F3 handler. Loads a PRG: BASIC PRGs are detokenized, ML PRGs
; are disassembled, other content shown as text (existing behavior).
; ============================================================================
do_load_file:
lda #0
sta LOAD_AS_SOURCE ; F3 = normal PRG load (with type detection)
sta FNAME_LEN
sta PROMPT_IS_SAVE
sta IS_BASIC ; clear until we know otherwise
lda #<prompt_lbl_load
sta LPTR
lda #>prompt_lbl_load
sta LPTR+1
jsr draw_filename_prompt ; C=1 → cancelled
bcc :+
rts ; cancelled — nothing to redraw, just return
:
jmp load_body ; into shared load body
; ============================================================================
; do_load_source_file — F6 handler. Loads a raw SEQ text source file (e.g. an
; externally-created assembly source). Opens with ",S,R" so genuine SEQ files
; open, and treats the content as plain text — no BASIC/ML detection. This is
; the counterpart to F3: F3 for programs, F6 for source.
; ============================================================================
do_load_source_file:
lda #1
sta LOAD_AS_SOURCE ; F6 = force SEQ source load, no detection
lda #0
sta FNAME_LEN
sta PROMPT_IS_SAVE
sta IS_BASIC
lda #<prompt_lbl_load
sta LPTR
lda #>prompt_lbl_load
sta LPTR+1
jsr draw_filename_prompt ; C=1 → cancelled
bcc :+
rts
:
jmp load_body ; into shared body (keeps LOAD_AS_SOURCE=1)
; ============================================================================
; do_load_file_from_fname — load body only, no filename prompt.
;
; Called by run_selected_module when MOD_STATUS=$04 (MODDSK has already
; written the chosen filename into FNAME_BUF/FNAME_LEN).
; On entry: FNAME_BUF and FNAME_LEN must already be set by the caller.
; ============================================================================
do_load_file_from_fname:
lda #0
sta LOAD_AS_SOURCE ; module-driven loads use normal PRG behavior
load_body:
lda #0
sta IS_BASIC ; clear until we know otherwise
sta IS_NEW_FILE ; file came from disk -- not new
; ---- User file load via OPEN/CHRIN ----
; Our save path writes raw bytes via OPEN/CHKOUT/CHROUT with no PRG
; header (the file is a SEQ-style raw byte stream). We must read it
; back symmetrically — Kernal LOAD ($FFD5) consumes the first 2 bytes
; of the file as a "load address" header, which would mangle text
; files where those bytes are real content.
;
; Fastloader cartridges (Action Replay, Final Cartridge III, SS5,
; JiffyDOS) hook LOAD, not OPEN/CHRIN. So we lose fastloader speedup
; on user file loads — but correctness wins over speed, and most user
; source files are small (a few KB) so this is fine in practice.
; Module loads (which DO have proper PRG headers) still use LOAD and
; benefit from fastloader acceleration.
; ---- SETLFS: LA=IO_LA, device=SETTING_DRIVE, SA=2 (open SEQ-style) ----
lda #IO_LA
ldx SETTING_DRIVE
ldy #2
jsr SETLFS
; ---- SETNAM ----
; F6 (LOAD_AS_SOURCE=1): open as SEQ via "<name>,S,R" so genuine SEQ source
; files open. F3 (=0): bare name, which opens PRG files as before.
lda LOAD_AS_SOURCE
bne @name_seq
lda FNAME_LEN
ldx #<FNAME_BUF
ldy #>FNAME_BUF
jsr SETNAM
jmp @name_done
@name_seq:
jsr build_load_name ; IO_NAME_BUF = "<FNAME_BUF>,S,R"
lda IO_NAME_BUF_LEN
ldx #<IO_NAME_BUF
ldy #>IO_NAME_BUF
jsr SETNAM
@name_done:
; ---- OPEN ----
jsr OPEN
bcc :+
jmp @io_err
:
; Check drive status after OPEN
jsr READST
beq :+
jmp @close_err
:
; ---- CHKIN: redirect input from our file ----
ldx #IO_LA
jsr CHKIN
bcc :+
jmp @close_err
:
; ---- Read bytes into work_buf ----
lda #<work_buf
sta BUF_PTR
lda #>work_buf
sta BUF_PTR+1
@read_loop:
jsr READST
bne @read_done ; EOF or error
jsr CHRIN ; A = next byte
; Some drives set EOF on the byte that holds valid data, so check
; status AFTER read and still store the byte if EOF is set.
pha
jsr READST
and #$40 ; bit 6 = EOF
bne @read_eof_with_byte
pla
; Store byte
ldy #0
sta (BUF_PTR),y
; Advance BUF_PTR
inc BUF_PTR
bne :+
inc BUF_PTR+1
:
; Bounds check — don't overflow work_buf
lda BUF_PTR+1
cmp #>work_buf_end
bcc @read_loop
lda BUF_PTR
cmp #<work_buf_end
bcc @read_loop
jmp @read_done ; buffer full — truncate
@read_eof_with_byte:
pla ; restore final byte
ldy #0
sta (BUF_PTR),y
inc BUF_PTR
bne @read_done
inc BUF_PTR+1
@read_done:
jsr CLRCHN
lda #IO_LA
jsr CLOSE
; BUF_PTR now one past the last loaded byte.
; F6 source load: skip all PRG detection — the file is text by request.
lda LOAD_AS_SOURCE
beq @basic_detect
; ---- Set up as plain text (no detokenize, no disassemble) ----
lda #0
sta IS_BASIC
sta IS_NEW_FILE
sta IS_DIRTY
lda BUF_PTR
sta GAP_START
lda BUF_PTR+1
sta GAP_START+1
lda #<work_buf_end
sta GAP_END
lda #>work_buf_end
sta GAP_END+1
jmp @load_viewport
@basic_detect:
; Check first two bytes of work_buf for PRG load address $0801 ($01 $08).
lda work_buf+0
cmp #$01
bne @not_basic
lda work_buf+1
cmp #$08
bne @not_basic
; It's a BASIC PRG. Set flag and run MODDET to detokenize into plain text.
lda #$FF
sta IS_BASIC
lda #0
sta IS_NEW_FILE ; it's an existing file
sta IS_DIRTY ; freshly loaded — not dirty
; Set gap so MODDET knows where tokenized content ends in work_buf.
lda BUF_PTR
sta GAP_START
lda BUF_PTR+1
sta GAP_START+1
lda #<work_buf_end
sta GAP_END
lda #>work_buf_end
sta GAP_END+1
jsr load_run_moddet ; detokenizes work_buf in place; sets GAP from MOD_NEW_END
jmp @load_viewport
@not_basic:
lda #0
sta IS_BASIC
sta IS_NEW_FILE ; it's an existing file
sta IS_DIRTY ; freshly loaded — not dirty
; Set up gap: content = [work_buf..BUF_PTR), gap = [BUF_PTR..work_buf_end)
lda BUF_PTR
sta GAP_START
lda BUF_PTR+1
sta GAP_START+1
lda #<work_buf_end
sta GAP_END
lda #>work_buf_end
sta GAP_END+1
; Detect binary PRG vs text: a text assembly source file always starts
; with a printable character (>= $20). A binary PRG's first byte is the
; lo byte of the load address — common values ($00, $01) are all < $20.
lda work_buf
cmp #$20
bcs @load_viewport ; >= $20 → printable → text file, skip MODDIS
; Binary PRG: run MODDIS to disassemble into assembly source text.
jsr load_run_moddis
@load_viewport:
; Reset viewport to top of file
lda #<work_buf
sta TOP_LINE
lda #>work_buf
sta TOP_LINE+1
lda #0
sta LEFT_COL
sta CURSOR_ROW
sta CURSOR_COL
jmp @done
@close_err:
jsr CLRCHN
lda #IO_LA
jsr CLOSE
; fall through to @io_err
@io_err:
jsr READST
sta IO_STATUS
jsr show_io_error
@done:
jsr render_status
jsr ensure_cursor_visible
jsr render_viewport
jsr draw_cursor
rts
; ============================================================================
; do_save_file — F5 handler
; ============================================================================
do_save_file:
; ---- Compact gap so content is contiguous ----
jsr compact_gap ; WORK_PTR = one-past-end of content
; ---- Ensure content ends with CR ----
lda WORK_PTR
cmp #<work_buf
bne @check_last_cr
lda WORK_PTR+1
cmp #>work_buf
beq @no_append_cr ; empty buffer
@check_last_cr:
; Read byte at WORK_PTR-1 using Y offset trick: set ptr to WORK_PTR-1
; Use TMP as the peek pointer to avoid corrupting WORK_PTR
lda WORK_PTR
sta TMP
lda WORK_PTR+1
sta TMP+1
lda TMP
bne :+
dec TMP+1
: dec TMP
ldy #0
lda (TMP),y ; read last byte — TMP untouched after this
cmp #PET_CR
beq @no_append_cr
lda #PET_CR
sta (WORK_PTR),y ; Y is still 0
inc WORK_PTR
bne @no_append_cr
inc WORK_PTR+1
@no_append_cr:
lda WORK_PTR
sta IO_END_LO
lda WORK_PTR+1
sta IO_END_HI
; Re-open gap at end of compacted content
lda IO_END_LO
sta GAP_START
lda IO_END_HI
sta GAP_START+1
lda #<work_buf_end
sta GAP_END
lda #>work_buf_end
sta GAP_END+1
; ---- If new file, ask BASIC or plain text? ----
lda IS_NEW_FILE
beq @skip_basic_prompt ; $00 = not new, IS_BASIC already set
jsr prompt_basic_or_text ; sets IS_BASIC; C=1 if cancelled
bcc @skip_basic_prompt
jmp @done
@skip_basic_prompt:
; ---- Prompt for filename ----
lda IS_NEW_FILE
beq @have_name
lda #0
sta FNAME_LEN
@have_name:
lda #1
sta PROMPT_IS_SAVE
lda #<prompt_lbl_save
sta LPTR
lda #>prompt_lbl_save
sta LPTR+1
jsr draw_filename_prompt ; C=1 → cancelled
bcc :+
jmp @done
:
; ---- Build "@0:<filename>" in IO_NAME_BUF ----
jsr build_save_name ; A = total length
; ---- If BASIC, tokenize before writing ----
lda IS_BASIC
beq @skip_tokenize
jsr save_run_modtok
lda MOD_STATUS
cmp #$02
bne @tok_err
lda MOD_NEW_END_LO
sta IO_END_LO
lda MOD_NEW_END_HI
sta IO_END_HI
jmp @skip_tokenize
@tok_err:
jsr show_io_error
jmp @restore_gap
@skip_tokenize:
; ---- SETLFS: LA=IO_LA, device=SETTING_DRIVE, SA=1 (write) ----
lda #IO_LA
ldx SETTING_DRIVE
ldy #1
jsr SETLFS
; ---- SETNAM with "@0:<filename>" ----
lda IO_NAME_BUF_LEN
ldx #<IO_NAME_BUF
ldy #>IO_NAME_BUF
jsr SETNAM
; ---- OPEN ----
jsr OPEN
bcc :+
jmp @io_err
:
jsr READST
beq :+
jmp @close_err
:
; ---- CHKOUT: redirect output to our file ----
ldx #IO_LA
jsr CHKOUT
bcc :+
jmp @close_err
:
; ---- Write bytes from work_buf to content end ----
lda #<work_buf
sta BUF_PTR
lda #>work_buf
sta BUF_PTR+1
@write_loop:
lda BUF_PTR
cmp IO_END_LO
bne @write_byte
lda BUF_PTR+1
cmp IO_END_HI
beq @write_done
@write_byte:
ldy #0
lda (BUF_PTR),y
jsr CHROUT
; Check status after write
jsr READST
bne @write_err
inc BUF_PTR
bne @write_loop
inc BUF_PTR+1
jmp @write_loop
@write_err:
sta IO_STATUS
jsr CLRCHN
lda #IO_LA
jsr CLOSE
jsr show_io_error
jmp @done
@write_done:
jsr CLRCHN
lda #IO_LA
jsr CLOSE
lda #0
sta IS_NEW_FILE
sta IS_DIRTY
lda IS_BASIC
beq @done
; Save cursor/viewport state before re-detokenizing
lda TOP_LINE
sta IO_END_LO ; repurpose — I/O is finished at this point
lda TOP_LINE+1
sta IO_END_HI
lda CURSOR_ROW
sta IO_SCRATCH
jsr load_run_moddet
lda IO_END_LO
sta TOP_LINE
lda IO_END_HI
sta TOP_LINE+1
lda IO_SCRATCH
sta CURSOR_ROW
jsr ensure_cursor_visible
jmp @done
@restore_gap:
; Tokenize failed — restore gap pointers to plain-text content end
lda WORK_PTR
sta GAP_START
lda WORK_PTR+1
sta GAP_START+1
lda #<work_buf_end
sta GAP_END
lda #>work_buf_end
sta GAP_END+1
jmp @done
@close_err:
jsr CLRCHN
lda #IO_LA
jsr CLOSE
jmp @show_err
@io_err:
@show_err:
jsr READST
sta IO_STATUS
jsr show_io_error
@done:
jsr render_status
jsr render_viewport
jsr draw_cursor
rts
; ============================================================================
; load_run_moddet — auto-detokenize work_buf after loading a BASIC PRG.
;
; Populates the module parameter block and calls MODDET (module index 0).
; On return: GAP_START set from MOD_NEW_END, GAP_END = work_buf_end.
; Clobbers: A, X, Y, LPTR, FNAME_BUF, FNAME_LEN, MOD_* params.
; ============================================================================
load_run_moddet:
ldx #4 ; module index 4 = MODDET (hidden module)
jsr run_module_by_index
; On return MOD_STATUS should be $02 (buffer replaced).
; If not, something went badly wrong — treat buffer as plain text.
lda MOD_STATUS
cmp #$02
bne @plain ; fall back to treating whole buf as text
; Set gap from detokenized content end
lda MOD_NEW_END_LO
sta GAP_START
lda MOD_NEW_END_HI
sta GAP_START+1
lda #<work_buf_end
sta GAP_END
lda #>work_buf_end
sta GAP_END+1
rts
@plain:
; Fallback: use BUF_PTR as content end (set by read loop)
lda BUF_PTR
sta GAP_START
lda BUF_PTR+1
sta GAP_START+1
lda #<work_buf_end
sta GAP_END
lda #>work_buf_end
sta GAP_END+1
rts
; ============================================================================
; load_run_moddis — disassemble binary content in work_buf to assembly source.
;
; Calls MODDIS (module index 1) which reads the PRG binary starting at
; work_buf, disassembles it, and writes assembly source text back.
; On success: GAP_START set from MOD_NEW_END, GAP_END = work_buf_end.
; On failure: gap left as-is (caller's gap setup stays valid as raw text).
; Clobbers: A, X, Y, LPTR, FNAME_BUF, FNAME_LEN, MOD_* params.
; ============================================================================
load_run_moddis:
ldx #1 ; module index 1 = MODDIS
jsr run_module_by_index
lda MOD_STATUS
cmp #$02
bne @moddis_plain
; Write CONTENT_ROWS sentinel CRs after output end so scrolling past
; the last valid line shows blank rows instead of garbage.
lda MOD_NEW_END_LO
sta BUF_PTR
lda MOD_NEW_END_HI
sta BUF_PTR+1
lda #CONTENT_ROWS
sta TMP
@sentinel:
lda BUF_PTR+1 ; stop if at or past work_buf_end
cmp #>work_buf_end
bcs @sent_done
lda #PET_CR
ldy #0
sta (BUF_PTR),y
inc BUF_PTR
bne :+
inc BUF_PTR+1
: dec TMP
bne @sentinel
@sent_done:
; Zero-size gap at work_buf → cursor at position 0.
; ensure_cursor_visible finds cursor on first comparison: O(1).
lda #<work_buf
sta GAP_START
lda #>work_buf
sta GAP_START+1
lda #<work_buf
sta GAP_END
lda #>work_buf
sta GAP_END+1
rts
@moddis_plain:
lda BUF_PTR
sta GAP_START
lda BUF_PTR+1
sta GAP_START+1
lda #<work_buf_end
sta GAP_END
lda #>work_buf_end
sta GAP_END+1
rts
; ============================================================================
; save_run_modtok — auto-tokenize work_buf before writing a BASIC save.
;
; Populates the module parameter block and calls MODTOK (module index 1).
; On return: MOD_NEW_END holds the end of the tokenized bytes in work_buf.
; MOD_STATUS = $02 on success, $01 on error.
; The gap buffer is NOT updated — caller restores it after the disk write.
; Clobbers: A, X, Y, LPTR, FNAME_BUF, FNAME_LEN, MOD_* params.
; ============================================================================
save_run_modtok:
; The gap must be compacted before this call (do_save_file already did it).
; GAP_START currently = IO_END = content end after compact_gap.
; We need to tell MODTOK where the content ends via MOD_GAP_START.
ldx #5 ; module index 5 = MODTOK (hidden module)
jmp run_module_by_index ; tail call — RTS from there returns to caller
; ============================================================================
; prompt_basic_or_text — show "BASIC (Y/N)?" on status bar for new files.
;
; Sets IS_BASIC ($FF=yes, $00=no) based on keypress.
; On exit: C=0 confirmed, C=1 cancelled (STOP key).
; Clobbers: A, Y.
; ============================================================================
prompt_basic_or_text:
; Print "BASIC (Y/N)?" in screen codes on status row
ldy #0
@lbl:
lda basic_yn_text,y
beq @wait
sta STATUS_ROW,y
lda #DEFAULT_STATUS_COLOR
sta COLOR,y
iny
jmp @lbl
@wait:
jsr GETIN
beq @wait
cmp #PET_STOP
beq @cancel
; 'Y' in PETSCII = $59 (uppercase), also accept $79 (shifted)
cmp #'Y'
beq @yes
cmp #'y'
beq @yes
; 'N' = $4E
cmp #'N'
beq @no
cmp #'n'
beq @no
jmp @wait ; any other key: keep waiting
@yes:
lda #$FF
sta IS_BASIC
lda #0
sta IS_NEW_FILE
clc
rts
@no:
lda #0
sta IS_BASIC
sta IS_NEW_FILE
clc
rts
@cancel:
sec
rts
; "BASIC (Y/N)?" in screen codes
basic_yn_text:
.byte $02,$01,$13,$09,$03,$20,$28,$19,$2F,$0E,$29,$3F, 0
; ============================================================================
; compact_gap — copy [GAP_END..work_buf_end) down to GAP_START.
; Returns new content-end in WORK_PTR.
; Clobbers: WORK_PTR, BUF_PTR, A, Y
; ============================================================================
compact_gap:
lda GAP_END
sta WORK_PTR
lda GAP_END+1
sta WORK_PTR+1
lda GAP_START
sta BUF_PTR
lda GAP_START+1
sta BUF_PTR+1
@loop:
lda WORK_PTR
cmp #<work_buf_end
bne @byte
lda WORK_PTR+1
cmp #>work_buf_end
beq @done
@byte:
ldy #0
lda (WORK_PTR),y
sta (BUF_PTR),y
inc WORK_PTR
bne :+
inc WORK_PTR+1
: inc BUF_PTR
bne @loop
inc BUF_PTR+1
jmp @loop
@done:
lda BUF_PTR
sta WORK_PTR
lda BUF_PTR+1
sta WORK_PTR+1
rts
; ============================================================================
; build_save_name — write "@0:<FNAME_BUF>" into IO_NAME_BUF.
; Stores total length in IO_NAME_BUF_LEN and returns it in A.
; ============================================================================
build_save_name:
lda #'@'
sta IO_NAME_BUF+0
lda #'0'
sta IO_NAME_BUF+1
lda #':'
sta IO_NAME_BUF+2
ldy #0
@cp:
cpy FNAME_LEN
beq @done
lda FNAME_BUF,y
sta IO_NAME_BUF+3,y
iny
jmp @cp
@done:
tya
clc
adc #3
sta IO_NAME_BUF_LEN
rts
; ============================================================================
; build_load_name — write "<FNAME_BUF>,S,R" into IO_NAME_BUF for a SEQ read.
; Used by the F6 "load source" path so genuine SEQ files open (a bare name with
; SA 2 will not match a SEQ file). No "@0:" prefix — that is a save directive.
; Stores total length in IO_NAME_BUF_LEN and returns it in A.
; ============================================================================
build_load_name:
ldy #0
@cp:
cpy FNAME_LEN
beq @suffix
lda FNAME_BUF,y
sta IO_NAME_BUF,y
iny
jmp @cp
@suffix:
tya
tax ; X = name length = write index
ldy #0
@sfx:
lda load_src_suffix,y
sta IO_NAME_BUF,x
inx
iny
cpy #4
bne @sfx
stx IO_NAME_BUF_LEN ; total = namelen + 4
txa
rts
load_src_suffix:
.byte ",S,R" ; SEQ, read
; ============================================================================
; draw_filename_prompt
; On entry: LPTR → 6-byte screen-code label.
; On exit: C=0 confirmed, C=1 cancelled.
; Uses Y as column index throughout — petscii_to_screen only clobbers A.
; ============================================================================
draw_filename_prompt:
ldy #0
@lbl:
lda (LPTR),y
sta STATUS_ROW,y
lda #DEFAULT_STATUS_COLOR
sta COLOR,y
iny
cpy #6
bne @lbl
; Blank cols 6..39
ldy #PROMPT_NAME_COL
@blank:
lda #$20
sta STATUS_ROW,y
lda #DEFAULT_STATUS_COLOR
sta COLOR,y
iny
cpy #COLS
bne @blank
jsr prompt_draw_name
@flush:
jsr GETIN
bne @flush
@inp:
lda JIFFY_LO
and #PROMPT_BLINK_MASK
beq @cur_off
lda #$20 | $80
jmp @cur_draw
@cur_off:
lda #$20
@cur_draw:
ldy FNAME_LEN
sta STATUS_ROW + PROMPT_NAME_COL,y
jsr GETIN
beq @inp
cmp #PET_CR
beq @confirm
cmp #PET_STOP
beq @cancel
cmp #$14 ; INST/DEL
bne @try_char
lda FNAME_LEN
beq @inp
ldy FNAME_LEN
dey
lda #$20
sta STATUS_ROW + PROMPT_NAME_COL,y
sta COLOR + PROMPT_NAME_COL,y
dec FNAME_LEN
jmp @inp
@try_char:
cmp #$20
bcc @inp
ldy FNAME_LEN
cpy #PROMPT_MAX_LEN
beq @inp
sta FNAME_BUF,y
jsr petscii_to_screen
sta STATUS_ROW + PROMPT_NAME_COL,y
lda #DEFAULT_STATUS_COLOR
sta COLOR + PROMPT_NAME_COL,y
inc FNAME_LEN
jmp @inp
@confirm:
ldy FNAME_LEN
lda #$20
sta STATUS_ROW + PROMPT_NAME_COL,y
clc
rts
@cancel:
sec
rts
; ============================================================================
; prompt_draw_name — paint FNAME_BUF onto status row name field.
; Uses Y as column index throughout.
; ============================================================================
prompt_draw_name:
ldy #0
@draw:
cpy FNAME_LEN
beq @pad
lda FNAME_BUF,y
jsr petscii_to_screen
sta STATUS_ROW + PROMPT_NAME_COL,y
lda #DEFAULT_STATUS_COLOR
sta COLOR + PROMPT_NAME_COL,y
iny
jmp @draw
@pad:
cpy #PROMPT_MAX_LEN
beq @done
lda #$20
sta STATUS_ROW + PROMPT_NAME_COL,y
lda #DEFAULT_STATUS_COLOR
sta COLOR + PROMPT_NAME_COL,y
iny
jmp @pad
@done:
rts
; ============================================================================
; show_io_error — "I/O ERROR" on status row ~1.5 s.
; ============================================================================
show_io_error:
ldy #0
@wr:
lda io_error_text,y
beq @wait
sta STATUS_ROW,y
lda #2
sta COLOR,y
iny
jmp @wr
@wait:
lda JIFFY_LO
clc
adc #90
sta IO_SCRATCH
@spin:
lda JIFFY_LO
cmp IO_SCRATCH
bne @spin
rts
io_error_text:
.byte $09,$2F,$0F,$20,$05,$12,$12,$0F,$12,$20, 0
; ============================================================================
; petscii_to_screen — clobbers A only. Y and X preserved.
; ============================================================================
petscii_to_screen:
cmp #$40
bcc @keep
cmp #$60
bcc @sub40
cmp #$80
bcc @sub20
and #$7F
rts
@keep: rts
@sub40: sec
sbc #$40
rts
@sub20: sec
sbc #$20
rts
; ============================================================================
; Prompt labels — 6 bytes each, screen codes
; ============================================================================
prompt_lbl_load:
.byte $0C,$0F,$01,$04,$3A,$20 ; "LOAD: "
prompt_lbl_save:
.byte $13,$01,$16,$05,$3A,$20 ; "SAVE: "
; ============================================================================
; Additional BSS variable — add to .segment "BSS" in editor.asm:
; IO_NAME_BUF_LEN: .res 1
; ============================================================================