-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVDriveZ88.asm
5179 lines (3878 loc) · 80.9 KB
/
VDriveZ88.asm
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
module VDriveZ88
include "director.def"
include "dor.def"
include "stdio.def"
include "serintfc.def"
include "syspar.def"
include "fileio.def"
include "error.def"
include "memory.def"
include "integer.def"
include "time.def"
include "fpp.def"
include "vdap.def"
; General constants
defc port_timeout_default = 25 ; 1/4 second
defc port_timeout_long = 50 ; 1/2 second
defc filename_length = 20
defc dir_item_width = 14
defc dir_cols = 6
defc dir_rows = 7
defc dir_left = 0
defc dir_top = 0
defc dir_right = dir_left + dir_item_width * dir_cols
defc dir_bottom = dir_top + dir_rows
; Unsafe workspace variables
defc unsafe_ws = 152
defc safe_ws = 0
defc ram_vars = $1FFE - unsafe_ws
defvars ram_vars {
screen_handle ds.w 1 ; screen handle
port_handle ds.w 1 ; serial port handle
port_timeout ds.w 1 ; serial port timeout
file_handle ds.w 1 ; file handle
filename ds.b 32 ; buffer for filenames
has_disk ds.b 1 ; whether the drive has a disk in it or not
memory_pool ds.w 1 ; memory allocation pool
dir_list ds.w 2 ; pointer to first file in list
dir_count ds.w 1 ; number of files in file list
dir_list_ptr ds.w 2 ; pointer to current item in file list
dir_list_new ds.w 2 ; pointer to new item in the file list
dir_list_old ds.w 2 ; pointer to old item in the file list
cursor_x ds.b 1 ; cursor x
cursor_y ds.b 1 ; cursor y
dir_offset ds.w 1 ; offset into directory listing
dir_working ds.w 1 ; working file index
dir_selected ds.w 1 ; selected file index
path ds.w 2 ; pointer to storage for full file path
file_size ds.b 4 ; size of a file
file_created ds.w 2 ; file creation date+time
file_accessed ds.w 2 ; file accessed date+time
file_modified ds.w 2 ; file modification date+time
oz_date_time ds.b 6 ; OZ-format date+time
local_filename ds.b 32 ; local filename for copy operations
file_buffer ds.w 2 ; allocated buffer to handle file transfers
data_transferred ds.b 4 ; general "data remaining" counter
data_remaining ds.b 4 ; general "data remaining" counter
chunk_size ds.w 2 ; when transferring data to/from the drive
old_pa_txb_size ds.b 1 ; transmit baud rate is 1 or 2 bytes
old_pa_txb ds.w 1 ; original transmit baud rate
old_pa_rxb_size ds.b 1 ; receive baud rate is 1 or 2 bytes
old_pa_rxb ds.w 1 ; original receive baud rate
old_pa_xon ds.b 1 ; original xon/off state
old_pa_par ds.b 1 ; original parity state
ram_vars_end ds.b 1
}
if ram_vars_end - ram_vars > unsafe_ws
error "Too many variables"
endif
; Application bank and origin
defc appl_bank = $3F ; default to top bank loading
org $E000
; Application DOR
.appl_dor
defb 0,0,0 ; links to parent, brother, son
defb 0,0,0
defb 0,0,0
defb $83 ; DOR type - application
defb indorend-indorstart
.indorstart
defb '@' ; key to info section
defb ininfend-ininfstart
.ininfstart
defw 0
defb 'D' ; application key
defb 0 ; no bad app memory
defw 0 ; overhead
defw unsafe_ws ; unsafe workspace
defw safe_ws ; safe workspace
defw appl_start ; entry point
defb 0 ; bank bindings
defb 0
defb 0
defb appl_bank
defb at_popd|at_good ; good popdown
defb 0 ; no caps lock
.ininfend
defb 'H' ; key to help section
defb inhlpend-inhlpstart
.inhlpstart
defw topics_dor
defb appl_bank
defw commands_dor
defb appl_bank
defw help_dor
defb appl_bank
defw tokens_dor
defb appl_bank
.inhlpend
defb 'N' ; key to name section
defb innamend-innamstart
.innamstart
defm "VDrive", 0
.innamend
defb $FF
.indorend
.topics_dor
defb 0 ; start marker
.topic_commands
defb topic_end - topic_commands ; length byte
defm "Commands" ; topic name
defb >(help_commands - help_dor) ; help offset msb
defb <(help_commands - help_dor) ; help offset lsb
defb TPCF_HELP ; topic attributes
defb topic_end - topic_commands ; length byte
.topic_end
defb 0 ; end marker
.commands_dor
defb 0 ; start marker
.cmd_rename
defb cmd_rename_end - cmd_rename ; length byte
defb 'R' ; command code
defm "RE", 0 ; keyboard sequence
defm "Rename" ; command name
defb >(help_rename - help_dor) ; help offset msb
defb <(help_rename - help_dor) ; help offset lsb
defb CMDF_HELP ; command attributes
defb cmd_rename_end - cmd_rename ; length byte
.cmd_rename_end
.cmd_erase
defb cmd_erase_end - cmd_erase ; length byte
defb 'E' ; command code
defm "ER", 0 ; keyboard sequence
defm "Erase" ; command name
defb >(help_erase - help_dor) ; help offset msb
defb <(help_erase - help_dor) ; help offset lsb
defb CMDF_HELP ; command attributes
defb cmd_erase_end - cmd_erase ; length byte
.cmd_erase_end
.cmd_create
defb cmd_create_end - cmd_create ; length byte
defb 'C' ; command code
defm "CD", 0 ; keyboard sequence
defm "Create Directory" ; command name
defb >(help_create - help_dor) ; help offset msb
defb <(help_create - help_dor) ; help offset lsb
defb CMDF_HELP ; command attributes
defb cmd_create_end - cmd_create ; length byte
.cmd_create_end
.cmd_up_dir
defb cmd_up_dir_end - cmd_up_dir ; length byte
defb IN_SUP ; command code
defm IN_SUP, 0 ; keyboard sequence
defm "Up Directory" ; command name
defb >(help_up_dir - help_dor) ; help offset msb
defb <(help_up_dir - help_dor) ; help offset lsb
defb CMDF_COLUMN|CMDF_HELP ; command attributes
defb cmd_up_dir_end - cmd_up_dir ; length byte
.cmd_up_dir_end
.cmd_dwn_dir
defb cmd_dwn_dir_end - cmd_dwn_dir ; length byte
defb IN_SDWN ; command code
defm IN_SDWN, 0 ; keyboard sequence
defm "Down Directory", 0 ; command name
defb >(help_dwn_dir - help_dor) ; help offset msb
defb <(help_dwn_dir - help_dor) ; help offset lsb
defb CMDF_HELP ; command attributes
defb cmd_dwn_dir_end - cmd_dwn_dir ; length byte
.cmd_dwn_dir_end
.cmd_cur_rgt
defb cmd_cur_rgt_end - cmd_cur_rgt ; length byte
defb IN_RGT ; command code
defm IN_RGT, 0 ; keyboard sequence
defm "Cursor Right", 0 ; command name
defb cmd_cur_rgt_end - cmd_cur_rgt ; length byte
.cmd_cur_rgt_end
.cmd_cur_lft
defb cmd_cur_lft_end - cmd_cur_lft ; length byte
defb IN_LFT ; command code
defm IN_LFT, 0 ; keyboard sequence
defm "Cursor Left", 0 ; command name
defb cmd_cur_lft_end - cmd_cur_lft ; length byte
.cmd_cur_lft_end
.cmd_cur_up
defb cmd_cur_up_end - cmd_cur_up ; length byte
defb IN_UP ; command code
defm IN_UP, 0 ; keyboard sequence
defm "Cursor Up", 0 ; command name
defb cmd_cur_up_end - cmd_cur_up ; length byte
.cmd_cur_up_end
.cmd_cur_dwn
defb cmd_cur_dwn_end - cmd_cur_dwn ; length byte
defb IN_DWN ; command code
defm IN_DWN, 0 ; keyboard sequence
defm "Cursor Down", 0 ; command name
defb cmd_cur_dwn_end - cmd_cur_dwn ; length byte
.cmd_cur_dwn_end
.cmd_send
defb cmd_send_end - cmd_send ; length byte
defb 'S' ; command code
defm "ES", 0 ; keyboard sequence
defm "Send to Drive" ; command name
defb >(help_send - help_dor) ; help offset msb
defb <(help_send - help_dor) ; help offset lsb
defb CMDF_COLUMN|CMDF_HELP ; command attributes
defb cmd_send_end - cmd_send ; length byte
.cmd_send_end
.cmd_fetch
defb cmd_fetch_end - cmd_fetch ; length byte
defb 'F' ; command code
defm "EF", 0 ; keyboard sequence
defm "Fetch from Drive" ; command name
defb >(help_fetch - help_dor) ; help offset msb
defb <(help_fetch - help_dor) ; help offset lsb
defb CMDF_HELP ; command attributes
defb cmd_fetch_end - cmd_fetch ; length byte
.cmd_fetch_end
defb 1 ; end of topic
defb 0 ; end of all topics
.help_dor
defm DEL
defm "Version 1.0", DEL
defm DEL
defm "Send files to and fetch files from a Vinculum", DEL
defm "USB host device such as the VDrive or VMusic.", DEL
defm DEL
defm "Ben Ryves 2023 - https://benryves.com/", 0
.help_commands
defm "The current directory listing will be shown with directories", DEL
defm "at the top in small capitals (", SOH, "2+TDIRNAME", SOH, "2-T) and files underneath.", DEL
defm "You can move around the listing using the ", TOK_CURSOR_KEYS, " keys,", DEL
defm "enter directories or fetch files with the ", TOK_ENTER, " key,", DEL
defm "or erase files with the ", TOK_DEL, " key.", DEL
defm "Other operations can be accessed with the ", TOK_MENU, " key.", 0
.help_rename
defm DEL
defm "Rename existing files or directories on the USB drive.", 0
.help_erase
defm DEL
defm "Erase existing files or directories from the USB drive.", DEL
defm DEL
defm "Directories that contain other items cannot be erased,", DEL
defm "so make sure to empty them before erasing them.", DEL
defm DEL
defm "Files or directories can also be erased with the ", TOK_DEL, " key.", 0
.help_create
defm DEL
defm "Create a new directory on the USB drive.", 0
.help_up_dir
defm DEL
defm "Move up to the parent directory of the current directory.", 0
.help_dwn_dir
defm DEL
defm "Move down into the highlighted child directory.", DEL
defm DEL
defm "You can also enter child directories by highlighting them", DEL
defm "and pressing the ", TOK_ENTER, " key.", 0
.help_send
defm DEL
defm "Send a file from the Z88's file system to the", DEL
defm "current directory on the USB drive.", 0
.help_fetch
defm DEL
defm "Fetch the highlighted file from the USB drive and save it", DEL
defm "on the Z88's file system.", DEL
defm DEL
defm "You can also fetch files by highlighting them and pressing", DEL
defm "the ", TOK_ENTER, " key.", 0
.tokens_dor
defb (token_80 - tokens_list) / 2
defb (token_80 - tokens_list) / 2
.tokens_list
defw token_80 - tokens_dor
defw token_81 - tokens_dor
defw token_82 - tokens_dor
defw token_83 - tokens_dor
defw tokens_end - tokens_dor
.token_80
defm SOH, SD_ENT
.token_81
defm SOH, SD_DEL
.token_82
defm SOH, SD_ORGT, SOH, SD_OLFT, SOH, SD_OUP, SOH, SD_ODWN
.token_83
defm SOH, SD_MNU
.tokens_end
defc TOK_ENTER = $80
defc TOK_DEL = $81
defc TOK_CURSOR_KEYS = $82
defc TOK_MENU = $83
; The main entry point
.appl_start
jp appl_main
scf
ret
.appl_main
; clear our RAM variables
ld hl, ram_vars
ld (hl), 0
ld de, ram_vars + 1
ld bc, unsafe_ws - 1
ldir
; back up original panel settings
ld a, 2
ld bc, PA_Txb
ld de, old_pa_txb
oz (OS_Nq)
jr c, bye_error
ld (old_pa_txb_size), a
ld a, 2
ld bc, PA_Rxb
ld de, old_pa_rxb
oz (OS_Nq)
jr c, bye_error
ld (old_pa_rxb_size), a
ld a, 1
ld bc, PA_Xon
ld de, old_pa_xon
oz (OS_Nq)
jr c, bye_error
ld a, 1
ld bc, PA_Par
ld de, old_pa_par
oz (OS_Nq)
jr c, bye_error
; use segment 1 for memory allocations
ld a, MM_S1
ld bc, 0
oz (OS_Mop)
jr nc, opened_pool
; quit with error message
.bye_error
oz (GN_Err)
oz (OS_Bye)
.opened_pool
ld (memory_pool), ix
; allocate memory for the path
xor a
ld bc, 256
oz (OS_Mal)
jp c, appl_exit
ld (path + 0), bc
ld (path + 2), hl
oz (OS_Mpb)
ld (hl), 0
; allocate memory for the file transfer buffer
xor a
ld bc, 256
oz (OS_Mal)
jp c, appl_exit
ld (file_buffer + 0), bc
ld (file_buffer + 2), hl
; open the screen from its name
ld bc, filename_length
ld hl, screen_name
ld de, filename
ld a, OP_UP
oz (GN_Opf)
jp c, appl_exit
; store the port handle
ld (screen_handle), ix
; initialise the full window for output
ld hl, window_full
oz (GN_Sop)
; initialise the drive
call drive_init
ld hl, connecting
oz (GN_Sop)
.connecting_loop
call sync
jr c, connecting_failed
jr z, connecting_succeeded
.connecting_failed
ld bc, 10
oz (OS_Tin)
jr nc, connecting_loop_key
cp RC_TIME
jr z, connecting_loop
jp appl_exit
.connecting_loop_key
cp ESC
jp z, appl_exit
jr connecting_loop
.connecting_succeeded
ld hl, ok
oz (GN_Sop)
oz (Gn_Nln)
call busy_start
; path is already cleared, so just skip to the root directory
ld hl, up_to_root
call change_directory
; clear the screen
ld hl, window_full
oz (GN_Sop)
call busy_end
; skip the next block as we've already reset the path
jr dir_list_start
.dir_list_start_from_current
call busy_start
call dir
call busy_end
jr dir_list_start
.dir_list_start_from_root
; clear the stored path
ld bc, (path + 0)
ld hl, (path + 2)
oz (OS_Mpb)
ld (hl), 0
call busy_start
; initially start from the root directory
ld hl, up_to_root
call change_directory
call busy_end
.dir_list_start
; draw the title bar
ld hl, window_title_begin
oz (GN_Sop)
ld a, (has_disk)
or a
jr nz, dir_list_title_path
ld hl, no_disk
jr dir_list_output_title
.dir_list_title_path
ld hl, path_prefix
oz (GN_Sop)
ld bc, (path + 0)
ld hl, (path + 2)
oz (OS_Mpb)
.dir_list_output_title
oz (GN_Sop)
ld hl, window_title_end
oz (GN_Sop)
; switch to the directory window
ld hl, window_dir
oz (GN_Sop)
; ensure it's ungreyed
call ungrey_window
.dir_list_render
ld hl, (dir_count)
ld a, l
or h
jp z, key_loop
.dir_list_has_files
ld a, dir_left
ld (cursor_x), a
ld a, dir_top
ld (cursor_y), a
.dir_list_selection_on_screen_check
; ensure that the selected index is on-screen
ld hl, (dir_selected)
ld de, (dir_offset)
or a
sbc hl, de
bit 7, h
jr nz, dir_list_selection_off_top
ld de, dir_cols * dir_rows
or a
sbc hl, de
jr c, dir_list_selection_on_screen
.dir_list_selection_off_bottom
; increment the offset
ld hl, (dir_offset)
ld de, +dir_cols
add hl, de
ld (dir_offset), hl
jr dir_list_selection_on_screen_check
.dir_list_selection_off_top
; decrement the offset
ld hl, (dir_offset)
ld de, -dir_cols
add hl, de
ld (dir_offset), hl
jr dir_list_selection_on_screen_check
.dir_list_selection_on_screen
ld de, (dir_offset)
ld (dir_working), de
call dir_set_index
.dir_list_loop
call dir_list_print_file
jr z, dir_list_done
call dir_next
jr nz, dir_list_loop
.dir_list_done
; we've either run out of files/directories to display,
; or we've filled the screen
xor a
ld (cursor_x), a
ld (cursor_y), a
.no_dir_list
; wait for key
.key_loop
call check_events
jr z, no_key_loop_events
; there was an event
jp dir_list_start_from_root
.no_key_loop_events
ld bc, 100
oz (OS_Tin)
jr c, key_error
or a
jr z, extended_key
cp ESC
jp z, appl_exit
cp CR
jp z, dir_enter
cp DEL
jp z, delete_file
; if it's a filename character, can we jump to the next file starting with its initial?
call char_to_upper
call validate_filename_char
jr c, key_loop
; jump to next item starting with the initial just pressed
ld c, a
ld hl, (dir_count)
ld a, h
or l
jr z, key_loop
; start from the current file
push bc
ld de, (dir_selected)
ld (dir_working), de
call dir_set_index
pop bc
.next_lettered_file_loop
; advance to the next file
push bc
ld de, (dir_working)
inc de
push de
call dir_next
pop de
jr nz, next_lettered_file_nowrap
call dir_reset_index
ld de, 0
.next_lettered_file_nowrap
ld (dir_working), de
pop bc
; does the file start with our desired character?
ld de, 4 + 1
add hl, de
ld a, (hl)
cp c
jr z, next_lettered_file_found
; have we looped around?
ld hl, (dir_working)
ld de, (dir_selected)
or a
sbc hl, de
jr nz, next_lettered_file_loop
.next_lettered_file_found
; how far have we moved?
ld hl, (dir_working)
ld de, (dir_selected)
or a
sbc hl, de
jr z, key_loop
ex de, hl
jp dir_move_16
.key_error
; time-outs are OK
cp RC_TIME
jp z, key_loop
cp RC_SUSP
jp z, key_loop
cp RC_DRAW
jp z, dir_list_start
; treat any other errors as a request to quit
jp appl_exit
.extended_key
oz (OS_In)
jr c, key_error
cp 'R'
jp z, rename_file
cp 'E'
jp z, delete_file
cp 'C'
jp z, create_dir
cp IN_SUP
jp z, dir_up_a_level
cp IN_SDWN
jp z, dir_down_a_level
cp IN_RGT
jr z, dir_move_right
cp IN_LFT
jr z, dir_move_left
cp IN_UP
jr z, dir_move_up
cp IN_DWN
jr z, dir_move_down
cp 'S'
jp z, send_file
cp 'F'
jp z, dir_fetch
jp key_loop
.dir_list_print_file
; set cursor position
call goto_cursor
ld a, ' '
oz (OS_Out)
; show file name
ld bc, (dir_list_ptr + 0)
ld hl, (dir_list_ptr + 2)
oz (OS_Mpb)
ld bc, 4
add hl,bc
; first character of name denotes if it's 'f' file or 'd' directory
ld a, (hl)
inc hl
cp 'd'
jr nz, dir_list_is_file
; enable directory style
push hl
ld hl, working_file_is_dir
oz (GN_Sop)
pop hl
.dir_list_is_file
oz (GN_Sop)
; switch back to default file styles
ld hl, working_file_is_file
oz (GN_Sop)
; advance file position, but also check if it was the selected file
ld hl, (dir_selected)
ld de, (dir_working)
or a
sbc hl, de
inc de
ld (dir_working), de
jr nz, dir_list_not_selected
; if the file was selected, apply the reverse video style
call goto_cursor
ld hl, selected_file_reverse_on
oz (GN_Sop)
.dir_list_not_selected
; advance cursor position
ld a, (cursor_x)
add a, dir_item_width
cp dir_right
jr nz, dir_list_cursor_done
ld a, (cursor_y)
inc a
ld (cursor_y), a
cp dir_bottom
ret z
ld a, dir_left
.dir_list_cursor_done
ld (cursor_x), a
cp $FF ; force nz
ret
.dir_move_left
ld a, -1
jr dir_move
.dir_move_right
ld a, +1
jr dir_move
.dir_move_up
ld a, -dir_cols
jr dir_move
.dir_move_down
ld a, +dir_cols
jr dir_move
.dir_move
; sign-extend a -> de
ld e, a
add a, a
sbc a, a
ld d, a
.dir_move_16
; can't move within the directory listing if there aren't any files
ld hl, (dir_count)
ld a, h
or l
jp z, dir_move_skip
; can't move within the directory listing if there's just one file too
dec hl
ld a, h
or l
jp z, dir_move_skip
push de
call goto_selected_file
ld hl, selected_file_reverse_off
oz (GN_Sop)
pop de
ld hl, (dir_selected)
add hl, de
bit 7, h
jr z, dir_move_not_off_start
ld hl, (dir_count)
dec hl
.dir_move_not_off_start
ld (dir_selected), hl
ld de, (dir_count)
or a
sbc hl, de
jr c, dir_move_not_off_end
ld hl, 0
ld (dir_selected), hl
.dir_move_not_off_end
; have we moved off the top or bottom of the screen?
.dir_move_off_edge_loop
ld hl, (dir_selected)
ld de, (dir_offset)
or a
sbc hl, de
bit 7, h
jr nz, dir_moved_off_top
ld de, dir_cols * dir_rows
or a
sbc hl, de
jr c, dir_move_not_off_bottom
; increment the offset
ld hl, (dir_offset)
ld de, +dir_cols
add hl, de
ld (dir_offset), hl
; scroll the screen
ld a, SOH
oz (OS_Out)
ld a, SD_DWN
oz (OS_Out)
; redraw the bottom line of files
ld hl, (dir_offset)
ld de, dir_cols * [ dir_rows - 1 ]
add hl, de
ex de, hl
ld (dir_working), de
call dir_set_index
ld a, dir_left
ld (cursor_x), a
ld a, dir_bottom - 1
ld (cursor_y), a
.dir_move_off_bottom_reprint_loop
call dir_list_print_file
jr z, dir_move_off_edge_loop
call dir_next
jr nz, dir_move_off_bottom_reprint_loop
jr dir_move_off_edge_loop
.dir_moved_off_top
; decrement the offset
ld hl, (dir_offset)
ld de, -dir_cols
add hl, de
ld (dir_offset), hl
; scroll the screen
ld a, SOH
oz (OS_Out)
ld a, SD_UP
oz (OS_Out)
; redraw the top line of files
ld de, (dir_offset)
ld (dir_working), de
call dir_set_index
ld a, dir_left
ld (cursor_x), a
ld a, dir_top
ld (cursor_y), a
ld b, dir_cols
.dir_move_off_top_reprint_loop
push bc
call dir_list_print_file
pop bc
jr z, dir_move_off_edge_loop
push bc
call dir_next
pop bc
jp z, dir_move_off_edge_loop
djnz dir_move_off_top_reprint_loop
jp dir_move_off_edge_loop
.dir_move_not_off_bottom