-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmem.inc
2083 lines (1741 loc) · 44.1 KB
/
mem.inc
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
;*******************************************************************************
;Copyright 2022-2024, Stefan Jakobsson
;
;Redistribution and use in source and binary forms, with or without modification,
;are permitted provided that the following conditions are met:
;
;1. Redistributions of source code must retain the above copyright notice, this
; list of conditions and the following disclaimer.
;
;2. Redistributions in binary form must reproduce the above copyright notice,
; this list of conditions and the following disclaimer in the documentation
; and/or other materials provided with the distribution.
;
;THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
;AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
;FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
;SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
;CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
;OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;*******************************************************************************
;**********************************************************
;General information on banked RAM usage and layout
;----------------------------------------------------------
;- The editor's use of banked RAM is controlled by
; the global variables mem_start and mem_top, that
; are set on startup.
;
; mem_start is the first bank number used, and mem_top
; is the last bank number used.
;
; The purpose is to reserve parts of banked RAM for
; other use. Bank 0 is always used by the Kernal.
;
;- The first bank available to the editor (mem_start) is
; used for internal purposes, and not for the text buffer.
;
;- The head of the text buffer is in bank mem_start+1,
; address $a000.
;
;- The text buffer is not organzied as a continuous string,
; as this would cause performance problems when
; editing large text buffers. This would especially be
; an issue if you insert or delete text at the start
; of a large text buffer, as all subsequent content
; would need to be copied/moved forward or backward.
;
; To overcome this, the text buffer is divided into
; memory pages each of 256 bytes. The memory pages
; are doubly linked to each other, holding a
; reference both to the previous and the next
; page.
;
; This makes it possible for the editor to dynamically
; link in new memory pages instead of copying/moving
; the whole buffer.
;
;- There is a memory map (similar to 1541 BAM) to
; keep record of which pages are allocated and which
; are free. The map is primarily used by the alloc
; and free functions.
;
;- The format of each page of the text buffer is as follows:
;
; Offset Description
; $00 Bank where previous page is
; $01 Previous page ($A0-$BF, $00=NULL=>head of buffer)
; $02 Bank where next page is
; $03 Next page ($A0-$BF, $00=NULL=>end of buffer)
; $04 Length of text stored in this page
; $05-$FF Text data
;
;- The described memory strategy will, however, cause
; defragmentation, especially if the user goes back
; and forward a lot inserting and deleting content.
;
; By defragmentation I mean empty memory pages or
; pages with unused space scattered around the buffer.
;
; To overcome this, the editor has a defragmentation
; routine that is called on every interrupt cycle
; where there is no user input to handle.
;
; Not all pages can be defragmented on each run, as
; this would take the same amount of processing time
; as moving/copying one continuous buffer - what
; we are trying to avoid with the paged memory
; strategy.
;
; Therefore, on each invocation of the defragmentation
; routine only one memory page is defragmented. If the
; user is not making any input, 60 pages per second
; are defragmentated. If all pages are allocated for
; the buffer, a maximum of 8,128 pages, one complete
; defragmentation will take a total of 135 seconds.
;
; As the defragmentation is run in the background and
; is paused if the user enters any input during an
; interrupt cycle, it will not affect the end user
; experience in any noticable way.
;
; The routine works by continuously moving text
; upstreams in the buffer. After all pages have been
; looked at, there is unused space only in the last page of
; the buffer.
;**********************************************************
;**********************************************************
;Function name.......: mem_init
;Description.........: Initializes program's memory
; usage, and allocates the first
; page of banked RAM.
;Input...............: Nothing
;Returns.............: Nothing
;Error returns.......: Nothing
.proc mem_init
;Get mem top
bridge_setaddr KERNAL_MEMTOP
sec
bridge_call KERNAL_MEMTOP
dea ;Kernal MEMTOP actually returns memtop+1, so we need to decrease by 1 to make it usable
cmp mem_top ;Use mem top by from Kernal if less than the user request
bcs :+
sta mem_top
;Setup mem map values
: lda mem_start ;Prepare
sta BNK_SEL
lda #<mem_map
sta TMP1_ADR
lda #>mem_map
sta TMP1_ADR+1
ldy #0
lda #255 ;Bank 0 to mem_start: Not part of the text buffer, mark as allocated
ldx mem_start
inx
jsr init_map
lda #%00000001 ;Mark first page in the text buffer as allocated
sta (TMP1_ADR),y
iny
sec ;Bank mem_start+1 to mem_top: Rest of the text buffer, mark as free
lda mem_top
sbc mem_start
tax
lda #0
jsr init_map
sec ;Bank mem_top+1 to 255: Memory above text buffer, mark as allocated
lda #255
sbc mem_top
tax
lda #255
jsr init_map
blocks_free_counter:
;Calculate number of free blocks, (mem_top-mem_start)*256/8 = (mem_top-mem_start)*32
sec
lda mem_top
sbc mem_start
stz mem_blocks_free
sta mem_blocks_free+1 ;By storing mem_top-mem_start in the MSB we are effectively multiplying by 256
ldx #3 ;And here we do a 16 bit division by 8
: lsr mem_blocks_free+1
ror mem_blocks_free
dex
bne :-
;Decrease mem blocks free counter by 1 to account for the head of buffer that was allocated above in this function
sec
lda mem_blocks_free
sbc #1
sta mem_blocks_free
lda mem_blocks_free+1
sbc #0
sta mem_blocks_free+1
;Setup zero page vectors
;Bank
lda mem_start
ina ;The head of buffer is the bank after mem_start (mem_start used for internal data storage)
sta BNK_SEL
sta CRS_BNK ;Cursor
sta LNV_BNK ;Line first visible char
sta SCR_BNK ;Screen first visible char
;Mem page, i.e. most significant byte of address
lda #$a0
sta CRS_ADR+1 ;Cursor
sta LNV_ADR+1 ;Line first visible char
sta SCR_ADR+1 ;Screen first visible char
;Address, least significant byte
stz CRS_ADR ;Cursor
stz LNV_ADR ;Line first visible char
stz SCR_ADR ;Screen first visible char
;Memory offset values
stz CRS_IDX ;Cursor
stz LNV_IDX ;Line first visible char
stz SCR_IDX ;Screen first visible char
;Setup initial memory page metadata (bank mem_start+1, page $a0), bank selected above already
lda #0
ldy #0
: sta (CRS_ADR),y
iny
cpy #5
bne :-
;Set initial memory page for defrag runner
lda mem_start
ina
sta mem_run_defrag_page
lda #$a0
sta mem_run_defrag_page+1
;Set current line=1 and column=1 (each is a 24 bit value)
lda #1
sta mem_cur_line
sta mem_cur_col
stz mem_cur_line+1
stz mem_cur_line+2
stz mem_cur_col+1
stz mem_cur_col+2
;Set status: buffer not modified
stz mem_modified
rts
;Internal subroutine to set mem_map values
;Input: A=value, X=number of banks to write, Y should
init_map:
cpx #0 ;Exit if bank count=0
beq init_map_exit
sta tempvars ;Save input
init_map_loop:
lda tempvars
sta (TMP1_ADR),y
iny
bne :+
inc TMP1_ADR+1
: tya
and #3
bne init_map_loop
dex
bne init_map_loop
init_map_exit:
rts
.endproc
;**********************************************************
;Function name.......: mem_is_head
;Description.........: Is page head of buffer, i.e.
; bank mem_start+1, page $a0
;Input...............: Y Bank
; X Page
;Returns.............: C = 1 Is head
; C = 0 Is not head
;Error returns.......: None
.proc mem_is_head
dey
cpy mem_start
bne :+
cpx #$a0
bne :+
sec
rts
: clc
rts
.endproc
;**********************************************************
;Function name.......: mem_at_bof
;Description.........: Is cursor at beginning of file,
; i.e. first char, not necessarily
; the same as head of buffer
;Input...............: Nothing
;Returns.............: C = 1 Cursor at beginning of file
; C = 0 Not at beginning of file
;Error returns.......: Nothing
.proc mem_at_bof
;Get cursor index
lda CRS_IDX
beq :+
;If cursor index > 0 we are not at bof
clc
rts
;Search for head of buffer, return if we find a char before that
: lda CRS_BNK
sta BNK_SEL
lda CRS_ADR+1
sta TMP1_ADR+1
stz TMP1_ADR
: ldy #1
lda (TMP1_ADR),y ;Prev page
beq at_bof
tax
ldy #0
lda (TMP1_ADR),y ;Prev Bank
sta BNK_SEL
stx TMP1_ADR+1 ;Prev page
ldy #4
lda (TMP1_ADR),y ;Page len
beq :- ;Loop until we find non zero len page
not_bof:
clc
rts
at_bof:
sec
rts
.endproc
;**********************************************************
;Function name.......: mem_at_eof
;Description.........: Returns if cursor is at end of file.
; When inserting text at the end of the
; text buffer, the cursor will be
; one step right of the last char.
; This function returns true both
; when the cursor is at the last
; char and one step right of that.
;Input...............: Nothing
;Returns.............: C = 1 Cursor at end of file
; C = 0 Not at end of file
;Error returns.......: Nothing
.proc mem_at_eof
;Copy cursor pointer to temp pointer
lda CRS_BNK
sta BNK_SEL
lda CRS_ADR+1
sta TMP1_ADR+1
stz TMP1_ADR
;Is cursor index + 1 < page len, we're not at end of mem page, and consequently not at end of file
lda CRS_IDX
ina
ldy #4
cmp (TMP1_ADR),y
bcc not_eof
: ldy #3
lda (TMP1_ADR),y ;Next page
tax
beq at_eof ;Next page=0 => there is no next page, i.e. eof
tax
ldy #2
lda (TMP1_ADR),y ;Next bank
sta BNK_SEL
stx TMP1_ADR+1
ldy #4
lda (TMP1_ADR),y ;Next page len
beq :- ;Loop until we find a non zero len page
not_eof:
clc
rts
at_eof:
sec
rts
.endproc
;**********************************************************
;Funcion name........: mem_alloc
;Description.........: Allocates a page in banked RAM. The
; newly allocated page will be linked
; in after the page pointed to by the
; cursor.
;Input...............: Nothing
;Returns.............: Allocated memory page, Y= bank, and
; X=page (address MSB)
;Error returns.......: X=0 if memory full
.proc mem_alloc
;Select RAM bank mem_start
lda mem_start
sta BNK_SEL
;Prepare searching the memory map
lda #<mem_map
sta TMP1_ADR
lda #>mem_map
sta TMP1_ADR+1
ldy #0
loop:
;Search memory map for free pages: search for entries != $ff
lda (TMP1_ADR),y
cmp #$ff
bne free_mem_found
iny
bne loop
inc TMP1_ADR+1
lda TMP1_ADR+1
cmp #>mem_map+4
bne loop
mem_full:
;If we reach this point there was no free memory, return X=0
ldx #0
rts
free_mem_found:
;Store the value of the byte in mem_map where we found free memory, will be needed later in the function
sta value
;Step 1. Divide the LSB of the number of search loops (= value of .Y) by 4 and we have the first part of the bank
tya
lsr
lsr
sta newbank
;Step 2. Multiply the MSB of the number of search loops (>TMP1_ADR minus >mem_map) by 64, and add that to the bank
lda TMP1_ADR+1
sec
sbc #>mem_map
asl
asl
asl
asl
asl
asl
clc
adc newbank
sta newbank
;Step 3. Multiply the LSB of the number of search loops (= value of .Y) by 8 and we have the first part of the page
tya
asl
asl
asl
;Step 4. And that value by $1f to keep within page boundary (00-1f)
and #$1f
sta newpage
;Step 5. The second part of the page is found in the mem_map value stored earlier. Add the positional value of the first bit not set to the page
ldx #0
: lda value
and bitmask,x
beq mark_page_allocated
inx
bra :-
mark_page_allocated:
lda value
ora bitmask,x
sta (TMP1_ADR),y
;Step 6. Finally add start of banked ram ($a0) to the page. And we're done.
txa
clc
adc newpage
adc #$a0
sta newpage
;Update mem page metadata; we need to change the current page, the new page, and the page following that
lda CRS_BNK
sta BNK_SEL
ldy #2
lda (CRS_ADR),y
sta nextbank
ldy #3
lda (CRS_ADR),y
sta nextpage
lda newpage
sta (CRS_ADR),y
ldy #2
lda newbank
sta (CRS_ADR),y
lda newbank
sta BNK_SEL
lda newpage
sta TMP1_ADR+1
stz TMP1_ADR
ldy #0
lda CRS_BNK
sta (TMP1_ADR),y
ldy #1
lda CRS_ADR+1
sta (TMP1_ADR),y
ldy #2
lda nextbank
sta (TMP1_ADR),y
ldy #3
lda nextpage
sta (TMP1_ADR),y
ldy #4
lda #0
sta (TMP1_ADR),y
lda nextpage
beq exit
sta TMP2_ADR+1
stz TMP2_ADR
lda nextbank
sta BNK_SEL
ldy #0
lda newbank
sta (TMP2_ADR),y
ldy #1
lda newpage
sta (TMP2_ADR),y
exit:
;Return allocated bank and page
ldy newbank
ldx newpage
;Decrease mem blocks free counter
lda mem_blocks_free
bne :+
dec mem_blocks_free+1
: dec mem_blocks_free
rts
newbank = tempvars ;1 byte
newpage = tempvars+1 ;1 byte
value = tempvars+2 ;1 byte
nextbank = tempvars+3 ;1 byte
nextpage = tempvars + 4 ;1 byte
bitmask:
.byt 1, 2, 4, 8, 16, 32, 64, 128
.endproc
;**********************************************************
;Function name.......: mem_free
;Description.........: Frees a page in banked RAM
;Input...............: Y Bank
; X Page
;Returns.............: Nohting
;Error returns.......: C = 1 Could not free page
.proc mem_free
;Save input params
sty bank
stx page
;Check if bank is reserved, exit without deallocating if true
cpy mem_start
beq is_reserved ;Don't touch head of buffer - bank mem_start+1, page $a0
bcc is_reserved
cpy mem_top
beq :+
bcs is_reserved ;Don't touch unpopulated memory
: tya
dea
cmp mem_start
bne :+
lda page
cmp #$a0
bne :+
is_reserved:
sec
rts
;Continue freeing that mem page...
;Find out position in mem_map corresponding to specified bank/page
: lda mem_start ;Select bank mem_start to access mem_map
sta BNK_SEL
lda bank
lsr
lsr
lsr
lsr
lsr
lsr
clc
adc #>mem_map
sta TMP1_ADR+1 ;High byte = bank/64 + mem_mapH
lda bank
asl
asl
adc #<mem_map
sta TMP1_ADR ;Low byte = bank * 4 + mem_mapL
lda #0
adc TMP1_ADR+1
sta TMP1_ADR+1 ;Add possible overflow to high byte
lda page
sec
sbc #$a0
lsr
lsr
lsr
tay ;Offset, Y = page/8
lda page
and #7
tax ;X is now bitmask index corresponding to the page
lda bitmask,x
and (TMP1_ADR),y
beq exit2 ;Page already free, exit
lda bitmask,x
eor #255
and (TMP1_ADR),y
sta (TMP1_ADR),y ;Mark page as free
;Get prev and next page
lda bank
sta BNK_SEL
lda page
sta TMP1_ADR+1
stz TMP1_ADR
ldy #0
lda (TMP1_ADR),y
sta prev
ldy #1
lda (TMP1_ADR),y
sta prev+1
ldy #2
lda (TMP1_ADR),y
sta next
ldy #3
lda (TMP1_ADR),y
sta next+1
;Set prev page pointer
lda prev+1
beq next_page_link ;Prev page null, shouldn't happen as we don't allow freeing the head of the buffer
sta TMP1_ADR+1
lda prev
sta BNK_SEL
ldy #2
lda next
sta (TMP1_ADR),y
ldy #3
lda next+1
sta (TMP1_ADR),y
;Set next page pointer
next_page_link:
lda next+1
beq exit ;Next page null
sta TMP1_ADR+1
lda next
sta BNK_SEL
ldy #0
lda prev
sta (TMP1_ADR),y
ldy #1
lda prev+1
sta (TMP1_ADR),y
exit:
clc
;Increase mem block free counter
inc mem_blocks_free
bne exit2
inc mem_blocks_free+1
exit2:
rts
bank = tempvars ;1 byte
page = tempvars+1 ;1 byte
prev = tempvars+2 ;2 bytes
next = tempvars+4 ;2 bytes
bitmask:
.byt 1,2,4,8,16,32,64,128
.endproc
;**********************************************************
;Function name.......: mem_run_defrag
;Description.........: This function is meant to be invoked
; once every interrupt if there is
; no user input to handle. It will run
; the defrag routine on one mem page
; per invocation. The next candidate
; for defragmentation stored in global
; variable mem_run_defrag_page (16 bits)
;Input...............: Nothing
;Returns.............: Nothing
;Error returns.......: Nothing
.proc mem_run_defrag
;Set pointers to next bank/page to defrag, which is stored in
;mem_run_defrag_page (2 bytes)
lda mem_run_defrag_page
sta BNK_SEL
lda mem_run_defrag_page+1
sta TMP1_ADR+1
stz TMP1_ADR
;First visble char of the screen at page after page to defrag - Pointer to first char could be lost
ldy #2
lda (TMP1_ADR),y
cmp SCR_BNK
bne :+
ldy #3
lda (TMP1_ADR),y
cmp SCR_ADR+1
bne :+
bra exit
;First visible char of line at page after page to defrag - Pointer to first char could be lost
: ldy #2
lda (TMP1_ADR),y
cmp LNV_BNK
bne :+
ldy #3
lda (TMP1_ADR),y
cmp LNV_ADR+1
bne :+
bra exit
;Cursor at page after page to defrag - Pointer to cursor could be lost
: ldy #2
lda (TMP1_ADR),y
cmp CRS_BNK
bne :+
ldy #3
lda (TMP1_ADR),y
cmp CRS_ADR+1
bne :+
bra exit
;Cursor at page to defrag - Not effective, as we're typing here
: lda mem_run_defrag_page
cmp CRS_BNK
bne :+
lda mem_run_defrag_page+1
cmp CRS_ADR+1
bne :+
bra exit
;Cursor at page before page to defrag - Not effective, as we're typing here
: lda CRS_BNK
sta BNK_SEL
ldy #2
lda (CRS_ADR),y
cmp mem_run_defrag_page
bne :+
ldy #3
lda (CRS_ADR),y
cmp mem_run_defrag_page+1
bne :+
bra exit
;All checks passed, go on and run the defrag
: ldy mem_run_defrag_page
ldx mem_run_defrag_page+1
jsr mem_defrag
exit:
;Select bank/page to look at when calling this function next time
lda mem_run_defrag_page
sta BNK_SEL
lda mem_run_defrag_page+1
sta TMP1_ADR+1
stz TMP1_ADR
ldy #3
lda (TMP1_ADR),y
beq eof
sta mem_run_defrag_page+1
ldy #2
lda (TMP1_ADR),y
sta mem_run_defrag_page
rts
eof:
;Start over from head of buffer
lda mem_start
ina
sta mem_run_defrag_page
lda #$a0
sta mem_run_defrag_page+1
rts
.endproc
;**********************************************************
;Function name.......: mem_defrag
;Description.........: Defragments a memory page. This
; function will copy text from the
; page after the page to dedfrag filling
; up the page to defrag. If the len of the
; page after the page to defrag is 0
; before or after this it will be marked
; as free.
;Input...............: Y = bank and X = page of page to defrag
;Returns.............: Nothing
;Error returns.......: Nothing
.proc mem_defrag
;Set pointer to page to defrag
sty BNK_SEL
sty TMP1_BNK
stx TMP1_ADR+1
stz TMP1_ADR
;Get len of page to defrag - exit if full
ldy #4
lda (TMP1_ADR),y
cmp #251
bcc :+
rts
: sta page_len
;Get page after page to defrag - exit if null
ldy #3
lda (TMP1_ADR),y
bne :+
rts
: sta TMP2_ADR+1
stz TMP2_ADR
ldy #2
lda (TMP1_ADR),y
sta TMP2_BNK
sta BNK_SEL
;Get len of page after page to defrag
ldy #4
lda (TMP2_ADR),y
bne :+
bra free ;Next page len = 0, free it
: sta nextpage_len
;Prepare copy to page to defrag
lda #5
sta TMP1_ADR
sta TMP2_ADR
stz nextpage_index
copy:
lda nextpage_index
cmp nextpage_len
bcs copy_done
lda page_len
cmp #251
bcs copy_done
ldy nextpage_index
ldx TMP2_BNK
stx BNK_SEL
lda (TMP2_ADR),y
ldy page_len
ldx TMP1_BNK
stx BNK_SEL
sta (TMP1_ADR),y
inc nextpage_index
inc page_len
bra copy
copy_done:
;Set len of page to defrag after copy done
lda TMP1_BNK
sta BNK_SEL
stz TMP1_ADR
ldy #4
lda page_len
sta (TMP1_ADR),y
;Set len of page after page to defrag after copy done
lda TMP2_BNK
sta BNK_SEL
stz TMP2_ADR
sec
lda nextpage_len
sbc nextpage_index
ldy #4
sta (TMP2_ADR),y
cmp #0
beq free
move_nextpage:
;Move remaining content of page after page to defrag to start of that page
lda TMP2_BNK
sta BNK_SEL
stz nextpage_index2
lda #5
sta TMP2_ADR
: ldy nextpage_index
cpy #251
beq exit
lda (TMP2_ADR),y
ldy nextpage_index2
sta (TMP2_ADR),y
inc nextpage_index
inc nextpage_index2
bra :-
exit:
rts
free:
ldy TMP2_BNK
ldx TMP2_ADR+1
jmp mem_free
page_len = tempvars ;1 byte
nextpage_len = tempvars +1 ;1 byte
nextpage_index = tempvars + 2 ;1 byte
nextpage_index2 = tempvars +3 ;1 byte
.endproc
;**********************************************************
;Function name.......: mem_push
;Description.........: Pushes content of a mem page
; one step forward
;Input...............: Y=mem bank, X=mem page, A=index to start pushing from
;Returns.............: C=1 if overflow; overflow char returned in .A
;Error returns.......: Nothing
.proc mem_push
;Save start index for later use
sta start
;Set pointers to page to push
sty BNK_SEL
stx TMP1_ADR+1
stz TMP1_ADR
stx TMP2_ADR+1
;Get page len
ldy #4
lda (TMP1_ADR),y
sta len
;If len = 0 there's nothing to push. Set len = 1 and exit
cmp #0
bne :+
lda #1
sta (TMP1_ADR),y
clc
rts
;Save last char of page, if there is overflow to return later
: ldy #255
lda (TMP1_ADR),y
sta last_char