OpenCores
URL https://opencores.org/ocsvn/rtf65002/rtf65002/trunk

Subversion Repositories rtf65002

[/] [rtf65002/] [trunk/] [software/] [asm/] [memory.asm] - Blame information for rev 40

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 40 robfinch
 
2
; ============================================================================
3
;        __
4
;   \\__/ o\    (C) 2013, 2014  Robert Finch, Stratford
5
;    \  __ /    All rights reserved.
6
;     \/_//     robfinch@opencores.org
7
;       ||
8
;
9
;
10
; This source file is free software: you can redistribute it and/or modify
11
; it under the terms of the GNU Lesser General Public License as published
12
; by the Free Software Foundation, either version 3 of the License, or
13
; (at your option) any later version.
14
;
15
; This source file is distributed in the hope that it will be useful,
16
; but WITHOUT ANY WARRANTY; without even the implied warranty of
17
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
; GNU General Public License for more details.
19
;
20
; You should have received a copy of the GNU General Public License
21
; along with this program.  If not, see .
22
;
23
; ============================================================================
24
;
25
MAX_VIRTUAL_PAGE        EQU             320
26
MAX_PHYSICAL_PAGE       EQU             2048
27
INV_PAGE        EQU     000             ; page number to use for invalid entries
28
 
29
;------------------------------------------------------------------------------
30
; InitMMU
31
;
32
; Initialize the 64 maps of the MMU.
33
; Initially all the maps are set the same:
34
; Virtual Page  Physical Page
35
; 000-319               000 (invalid page marker)
36
; 320-511               1856-2047
37
; Note that there are only 512 virtual pages per map, and 2048 real
38
; physical pages of memory. This limits maps to 32MB.
39
; This range includes the BIOS assigned stacks for the tasks and tasks
40
; virtual video buffers.
41
; Note that physical pages 0 to 1855 are not mapped, but do exist. They may
42
; be mapped into a task's address space as required.
43
; If changing the maps the last 192 pages (12MB) of the map should always point
44
; to the BIOS area. Don't change map entries 320-511 or the system may
45
; crash. The last 192 pages map the virtual memory to the same physical
46
; addresses so that the physical and virtual address are the same.
47
; If the rts at the end of this routine works, then memory was mapped
48
; successfully.
49
;
50
; System Memory Map (Physical Addresses)
51
; Page
52
; 0000                  BASIC ROM, scratch memory ( 1 page global)
53
; 0001-0063             unassigned (4MB - 63 pages)
54
; 0064-0191             Bitmap video memory (8 MB - 128 pages)
55
; 0192-0336             DOS usage, disk cache etc. (9.4MB - 145 pages)
56
; 0337-1855             Heap space (99MB - 1519 pages)
57
; 1856-1983             Virtual Screen buffers (8MB - 128 pages)
58
; 1984-2047             BIOS/OS area (4MB - 64 pages)
59
;       2032-2047               Stacks area (1MB - 16 pages)
60
; 65535                 BIOS ROM (64kB - 1 Page global)
61
; 261952-262015         I/O area (4MB - 64 pages global)
62
;------------------------------------------------------------------------------
63
 
64
        align   8
65
public InitMMU:
66
        lda             #1
67
        sta             MMU_KVMMU+1
68
        dea
69
        sta             MMU_KVMMU
70
immu1:
71
        sta             MMU_AKEY        ; set access key for map
72
        ldx             #0
73
immu2:
74
        ; set the first 320 pages to invalid page marker
75
        ; set the last 192 pages to physical page 1856-2047
76
        ld              r4,#INV_PAGE
77
        cpx             #320
78
        blo             immu3
79
        ld              r4,r2
80
        add             r4,r4,#1536     ; 1856-320
81
immu3:
82
        st              r4,MMU,x
83
        inx
84
        cpx             #512
85
        bne             immu2
86
        ina
87
        cmp             #64                     ; 64 MMU maps
88
        bne             immu1
89
        stz             MMU_OKEY        ; set operating key to map #0
90
        lda             #2
91
        sta             MMU_FUSE        ; set fuse to 2 clocks before mapping starts
92
        nop
93
        nop
94
 
95
;------------------------------------------------------------------------------
96
; Note that when switching the memory map, the stack address should not change
97
; as the virtual address was mapped to the physical one.
98
;------------------------------------------------------------------------------
99
;
100
        align   8
101
public EnableMMUMapping:
102
        pha
103
        lda             RunningTCB                      ; no need to enable mapping for Monitor/Debugger job
104
        lda             TCB_hJCB,r1
105
        cmp             #2
106
        blo             dmm2
107
        lda             #12                                     ; is there even an MMU present ?
108
        bmt             CONFIGREC
109
        beq             emm1
110
        lda             RunningTCB
111
        lda             TCB_hJCB,r1
112
        sta             MMU_OKEY                        ; select the mmu map for the job
113
        lda             #2
114
        sta             MMU_FUSE                        ; set fuse to 2 clocks before mapping starts
115
        lda             #1
116
        sta             MMU_MAPEN                       ; set MMU_MAPEN = 1
117
emm1:
118
        pla
119
        rts
120
 
121
public DisableMMUMapping:
122
        pha
123
dmm2:
124
        lda             #12                     ; is there even an MMU present ?
125
        bmt             CONFIGREC
126
        beq             dmm1
127
        stz             MMU_MAPEN
128
dmm1:
129
        pla
130
        rts
131
 
132
;------------------------------------------------------------------------------
133
;------------------------------------------------------------------------------
134
;
135
SetAKEYForCurrentJob:
136
        pha
137
        jsr             GetPtrCurrentJCB
138
        lda             JCB_Map,r1
139
        sta             MMU_AKEY
140
        pla
141
        rts
142
 
143
;------------------------------------------------------------------------------
144
;------------------------------------------------------------------------------
145
;
146
        align   8
147
public MemInit:
148
        lda             #1                                      ; initialize memory semaphore
149
        sta             mem_sema
150
        lda             #1519
151
        sta             nPagesFree
152
 
153
        ; Initialize the allocated page map to zero.
154
        lda             #64                             ; 64*32 = 2048 bits
155
        ldx             #0
156
        ldy             #PageMap
157
        stos
158
        ; Mark the last 192 pages as used (by the OS)
159
        ; 6-32 bit words
160
        lda             #-1
161
        sta             PageMap+58
162
        sta             PageMap+59
163
        sta             PageMap+60
164
        sta             PageMap+61
165
        sta             PageMap+62
166
        sta             PageMap+63
167
        ; Mark page #0 used
168
        lda             #1
169
        sta             PageMap
170
        ; Mark 64-336 used (DOS)
171
        lda             #64
172
meminit1:
173
        bms             PageMap
174
        ina
175
        cmp             #336
176
        blo             meminit1
177
        rts
178
 
179
;------------------------------------------------------------------------------
180
; Allocate a memory page from the available memory pool.
181
; Returns a pointer to the page in memory. The address returned is the
182
; virtual memory address.
183
;
184
; Returns:
185
;       r1 = 0 if no more memory is available or max mapped capacity is reached.
186
;       r1 = virtual address of allocated memory page
187
;------------------------------------------------------------------------------
188
;
189
        align   8
190
public AllocMemPage:
191
        phx
192
        phy
193
        ; Search the page bitmap for a free memory page.
194
        lda             #0
195
        ldx             #MAX_PHYSICAL_PAGE
196
        spl             mem_sema + 1
197
amp2:
198
        bmt             PageMap
199
        beq             amp1            ; found a free page ?
200
        ina
201
        dex
202
        bne             amp2
203
        ; Here all memory pages are already in use. No more memmory is available.
204
        stz             mem_sema + 1
205
        ply
206
        plx
207
        lda             #0
208
        rts
209
        ; Here we found an unallocated memory page. Next find a spot in the MMU
210
        ; map to place the page.
211
amp1:
212
        ; Find unallocated map slot in the MMU
213
        jsr             SetAKEYForCurrentJob
214
        ldx             #0
215
amp4:
216
        ldy             MMU,x
217
        cpy             #INV_PAGE
218
        beq             amp3
219
        inx
220
        cpx             #MAX_VIRTUAL_PAGE
221
        bne             amp4
222
        ; Here we searched the entire MMU slots and none were available
223
        stz             mem_sema + 1
224
        ply
225
        plx
226
        lda             #0              ; return NULL pointer
227
        rts
228
        ; Here we have both an available page, and available map slot.
229
amp3:
230
        bms             PageMap         ; mark page as allocated
231
        sta             MMU,x           ; put the page# into the map slot
232
        asl             r1,r2,#14       ; pages are 16kW in size (compute virtual address)
233
        dec             nPagesFree
234
        stz             mem_sema + 1
235
        ply
236
        plx
237
        rts
238
 
239
;------------------------------------------------------------------------------
240
; Parameters:
241
;       r1 = size of allocation in words
242
; Returns:
243
;       r1 = word pointer to memory
244
; No MMU
245
;------------------------------------------------------------------------------
246
;
247
        align   8
248
public AllocMemPages:
249
        php
250
        phx
251
        phy
252
        push    r4
253
        sei
254
amp5:
255
        tay
256
        lsr             r3,r3,#14       ; convert amount to #pages
257
        iny                                     ; round up
258
        cpy             nPagesFree
259
        bhi             amp11
260
        tyx                                     ; x = request size in pages
261
        ; Search for enough free pages to satisfy the request
262
        lda             #0
263
amp7:
264
        bmt             PageMap         ; test for a free page
265
        bne             amp6            ; not a free page
266
        cpx             #1                      ; did we find enough free pages ?
267
        bls             amp8
268
        dex
269
amp6:                                   ; keep checking for next free page
270
        ina
271
        cmp             #1855           ; did we hit end of map ?
272
        bhi             amp11           ; can't allocate enough memory
273
        bra             amp7            ; go back and test for another free page
274
 
275
        ; Insufficient memory, return NULL pointer
276
amp11:
277
        lda             #0
278
        pop             r4
279
        ply
280
        plx
281
        plp
282
        rts
283
 
284
        ; Mark pages as allocated
285
amp8:
286
        tyx             ; x= #pages to allocate
287
        cpx             #1
288
        bne             amp9
289
        txa                                                     ; flag indicates last page
290
        bra             amp10
291
amp9:
292
        lda             #0                                      ; flag indicates middle page
293
amp10:
294
        jsr             AllocMemPage            ; allocate first page
295
        ld              r4,r1                           ; save virtual address of first page allocated
296
        dex
297
        beq             amp14
298
amp13:
299
        cpx             #1
300
        bne             amp15
301
        txa
302
        bra             amp12
303
amp15:
304
        lda             #0
305
amp12:
306
        jsr             AllocMemPage
307
        dex
308
        bne             amp13
309
amp14:
310
        ld              r1,r4                           ; r1 = first virtual address
311
        pop             r4
312
        ply
313
        plx
314
        plp
315
        rts
316
 
317
;------------------------------------------------------------------------------
318
; FreeMemPage:
319
;
320
;       Free a single page of memory. This is an internal function called by
321
; FreeMemPages(). Normally FreeMemPages() will be called to free up the
322
; entire run of pages. This function both unmarks the memory page in the
323
; page bitmap and invalidates the page in the MMU.
324
;
325
; Parameters:
326
;       r1 = virtual memory address
327
;------------------------------------------------------------------------------
328
;
329
        align   8
330
FreeMemPage:
331
        pha
332
        php
333
        phx
334
        sei
335
        ; First mark the page as available in the virtual page map.
336
        pha
337
        lsr             r1,r1,#14
338
        and             #$1ff                   ; 512 virtual pages max
339
        ldx             RunningTCB
340
        ldx             TCB_mmu_map,x   ; x = map #
341
        asl             r2,r2,#4                ; 16 words per map
342
        bmc             VPM_bitmap_b0,x ; clear both bits
343
        bmc             VPM_bitmap_b1,x
344
        pla
345
        ; Mark the page available in the physical page map
346
        pha
347
        jsr             VirtToPhys              ; convert to a physical address
348
        lsr             r1,r1,#14
349
        and             #$7ff                   ; 2048 physical pages max
350
        bmc             PageMap
351
        pla
352
        ; Now mark the MMU slot as empty
353
        lsr             r1,r1,#14               ; / 16kW r1 = page # now
354
        and             #$1ff                   ; 512 pages max
355
        jsr             SetAKEYForCurrentJob
356
        tax
357
        lda             #INV_PAGE
358
        sta             MMU,x
359
        inc             nPagesFree
360
        plx
361
        plp
362
        pla
363
        rts
364
 
365
;------------------------------------------------------------------------------
366
; FreeMemPages:
367
;
368
;       Free up multiple pages of memory. The pages freed are a consecutive
369
; run of pages. A double-bit bitmap is used to identify where the run of
370
; pages ends. Bit code 00 indicates a unallocated page, 01 indicates an
371
; allocated page somewhere in the run, and 11 indicates the end of a run
372
; of allocated pages.
373
;
374
; Parameters:
375
;       r1 = pointer to memory
376
;------------------------------------------------------------------------------
377
;
378
        align   8
379
public FreeMemPages:
380
        cmp             #0x3fff                         ; test for a proper pointer
381
        bls             fmp5
382
        pha
383
        ; Turn the memory pointer into a bit index
384
        lsr             r1,r1,#14                       ; / 16kW acc = virtual page #
385
        cmp             #MAX_VIRTUAL_PAGE       ; make sure index is sensible
386
        bhs             fmp4
387
        phx
388
        spl             mem_sema + 1
389
        ldx             RunningTCB
390
        ldx             TCB_mmu_map,x
391
        asl             r2,r2,#4
392
fmp2:
393
        bmt             VPM_bitmap_b1,x         ; Test to see if end of allocation
394
        bne             fmp3
395
        asl             r1,r1,#14                       ; acc = virtual address
396
        jsr             FreeMemPage                     ;
397
        lsr             r1,r1,#14                       ; acc = virtual page # again
398
        ina
399
        cmp             #MAX_VIRTUAL_PAGE       ; last 192 pages aren't freeable
400
        blo             fmp2
401
fmp3
402
        ; Clear the last bit
403
        asl             r1,r1,#14                       ; acc = virtual address
404
        jsr             FreeMemPage                     ;
405
        lsr             r1,r1,#14                       ; acc = virtual page # again
406
        bmc             VPM_bitmap_b1,x
407
        stz             mem_sema + 1
408
        plx
409
fmp4:
410
        pla
411
fmp5:
412
        rts
413
 
414
;------------------------------------------------------------------------------
415
; Convert a virtual address to a physical address.
416
; Parameters:
417
;       r1 = virtual address to translate
418
; Returns:
419
;       r1 = physical address
420
;------------------------------------------------------------------------------
421
;
422
        align   8
423
public VirtToPhys:
424
        cmp             #$3FFF                          ; page #0 is physical page #0
425
        bls             vtp2
426
        cmp             #$01FFFFFF                      ; outside of managed address bounds (ROM / IO)
427
        bhi             vtp2
428
        phx
429
        ldx             CONFIGREC                       ; check if there is an MMU present
430
        bit             r2,#4096                        ; if not, then virtual and physical addresses
431
        beq             vtp3                            ; will match
432
        phy
433
        tay                                                     ; save original address
434
        and             r3,r3,#$FF803FFF        ; mask off MMU managed address bits
435
        jsr             SetAKEYForCurrentJob
436
        lsr             r2,r1,#14                       ; convert to MMU index
437
        and             r2,r2,#511                      ; 512 mmu pages
438
        lda             MMU,x                           ; a = physical page#
439
        beq             vtp1                            ; zero = invalid address translation
440
        asl             r1,r1,#14                       ; *16kW
441
        or              r1,r1,r3                        ; put back unmanaged address bits
442
vtp1:
443
        ply
444
vtp3:
445
        plx
446
vtp2:
447
        rts
448
 
449
;------------------------------------------------------------------------------
450
; PhysToVirt
451
;
452
; Convert a physical address to a virtual address. A little more complex
453
; than converting virtual to physical addresses as the MMU map table must
454
; be searched for the physical page.
455
;
456
; Parameters:
457
;       r1 = physical address to translate
458
; Returns:
459
;       r1 = virtual address
460
;------------------------------------------------------------------------------
461
;
462
        align   8
463
public PhysToVirt:
464
        cmp             #$3FFF                          ; first check for direct translations
465
        bls             ptv3                            ; outside of the MMU managed range
466
        cmp             #$01FFFFFF
467
        bhi             ptv3
468
        phx
469
        ldx             CONFIGREC                       ; check if there is an MMU present
470
        bit             r2,#4096                        ; if not, then virtual and physical addresses
471
        beq             ptv4                            ; will match
472
        phy
473
        jsr             SetAKEYForCurrentJob
474
        tay
475
        and             r3,r3,#$FF803FFF        ; mask off MMU managed address bits
476
        lsr             r1,r1,#14                       ; /16k to get index
477
        and             r1,r1,#$7ff                     ; 2048 pages max
478
        ldx             #0
479
ptv2:
480
        cmp             MMU,x
481
        beq             ptv1
482
        inx
483
        cpx             #512
484
        bne             ptv2
485
        ; Return NULL pointer if address translation fails
486
        ply
487
        plx
488
        lda             #0
489
        rts
490
ptv1:
491
        asl             r1,r2,#14       ; * 16k
492
        or              r1,r1,r3                        ; put back unmanaged address bits
493
        ply
494
ptv4:
495
        plx
496
ptv3:
497
        rts
498
 
499
; ============================================================================
500
; Heap related functions.
501
;
502
;       The heap is managed as a doublely linked list of memory blocks.
503
; ============================================================================
504
 
505
        align   8
506
public InitHeap:
507
        lda             RunningTCB
508
        ldx             TCB_HeapStart,r1
509
        ldy             TCB_HeapEnd,r1
510
        lda             #$4D454D20
511
        sta             MEM_CHK,x
512
        sta             MEM_FLAG,x
513
        lda             #$6D656D20              ; mark the last block as allocated
514
        sta             MEM_CHK,y
515
        sta             MEM_FLAG,y
516
        lda             #0
517
        sta             MEM_PREV,x              ; prev of first MEMHDR
518
        sty             MEM_NEXT,x
519
        sta             MEM_NEXT,y
520
        stx             MEM_PREV,y
521
        rts
522
 
523
;------------------------------------------------------------------------------
524
; Allocate memory from the heap.
525
; Each task has it's own memory heap.
526
;------------------------------------------------------------------------------
527
        align   8
528
public MemAlloc:
529
        phx
530
        phy
531
        push    r4
532
        ldx             RunningTCB
533
        ldx             TCB_HeapStart,x
534
mema4:
535
        ldy             MEM_FLAG,x              ; Check the flag word to see if this block is available
536
        cpy             #$4D454D20
537
        bne             mema1                   ; block not available, go to next block
538
        ld              r4,MEM_NEXT,x   ; compute the size of this block
539
        sub             r4,r4,r2
540
        sub             r4,r4,#4                ; minus size of block header
541
        cmp             r1,r4                   ; is the block large enough ?
542
        bmi             mema2                   ; if yes, go allocate
543
mema1:
544
        ldx             MEM_NEXT,x              ; go to the next block
545
        beq             mema3                   ; if no more blocks, out of memory error
546
        bra             mema4
547
mema2:
548
        ldy             #$6D656D20
549
        sty             MEM_FLAG,x
550
        sub             r4,r4,r1
551
        cmp             r4,#4                   ; is the block large enough to split
552
        bpl             memaSplit
553
        txa
554
        add             #4                              ; point to payload area
555
        pop             r4
556
        ply
557
        plx
558
        rts
559
mema3:                                          ; insufficient memory
560
        pop             r4
561
        ply
562
        plx
563
        lda             #0
564
        rts
565
memaSplit:
566
        add             r4,r1,r2
567
        add             r4,#4
568
        ldy             #$4D454D20
569
        sty             (r4)
570
        sty             MEM_FLAG,r4
571
        stx             MEM_PREV,r4
572
        ldy             MEM_NEXT,x
573
        sty             MEM_NEXT,r4
574
        st              r4,MEM_PREV,y
575
        ld              r1,r4
576
        add             #4
577
        pop             r4
578
        ply
579
        plx
580
        rts
581
 
582
;------------------------------------------------------------------------------
583
; Free previously allocated memory. Recombine with next and previous blocks
584
; if they are free as well.
585
;------------------------------------------------------------------------------
586
        align   8
587
public MemFree:
588
        cmp             #4                      ; null pointer ?
589
        blo             memf2
590
        phx
591
        phy
592
        sub             #4                      ; backup to header area
593
        ldx             MEM_FLAG,r1
594
        cpx             #$6D656D20      ; is the block allocated ?
595
        bne             memf1
596
        ldx             #$4D454D20
597
        stx             MEM_FLAG,r1     ; mark block as free
598
        ldx             MEM_PREV,r1     ; is the previous block free ?
599
        beq             memf3           ; no previous block
600
        ldy             MEM_FLAG,x
601
        cpy             #$4D454D20
602
        bne             memf3           ; the previous block is not free
603
        ldy             MEM_NEXT,r1
604
        sty             MEM_NEXT,x
605
        beq             memf1           ; no next block
606
        stx             MEM_PREV,y
607
memf3:
608
        ldy             MEM_NEXT,r1
609
        ldx             MEM_FLAG,y
610
        cpx             #$4D454D20
611
        bne             memf1           ; next block not free
612
        ldx             MEM_PREV,r1
613
        stx             MEM_PREV,y
614
        beq             memf1           ; no previous block
615
        sty             MEM_NEXT,x
616
memf1:
617
        ply
618
        plx
619
memf2:
620
        rts
621
 
622
;------------------------------------------------------------------------------
623
; Report the amount of system memory free. Counts up the number of
624
; unallocated pages in the page bitmap.
625
;------------------------------------------------------------------------------
626
;
627
public ReportMemFree:
628
        jsr             CRLF
629
        lda             #' '
630
        jsr             DisplayChar
631
        lda             #0
632
        tay
633
rmf2:
634
        bmt             PageMap
635
        bne             rmf1
636
        iny
637
rmf1:
638
        ina
639
        cmp             #2048
640
        blo             rmf2
641
        tya
642
        asl             r1,r1,#14               ; 16kW per bit
643
        ldx             #5
644
        jsr             PRTNUM
645
        lea             r1,msgMemFree
646
        jsr             DisplayStringB
647
        rts
648
 
649
msgMemFree:
650
        db      " words free",CR,LF,0
651
 
652
;==============================================================================
653
; Memory Management routines follow.
654
;==============================================================================
655
 
656
;------------------------------------------------------------------------------
657
; brk
658
; Establish a new program break
659
;
660
; Parameters:
661
; r1 = new program break address
662
;------------------------------------------------------------------------------
663
;
664
public _brk:
665
        phx
666
        push    r4
667
        push    r5
668
        push    r6
669
        ldx             RunningTCB
670
        ld              r4,TCB_ASID,x
671
        st              r4,MMU_AKEY
672
        ld              r4,TCB_npages,x
673
        lsr             r1,r1,#14
674
        add             r1,r1,#1
675
        cmp             r1,r4
676
        beq             brk6                    ; allocation isn't changing
677
        blo             brk1                    ; reducing allocation
678
 
679
        ; Here we're increasing the amount of memory allocated to the program.
680
        ;
681
        cmp             r1,#320                 ; max 320 RAM pages
682
        bhi             brk2
683
        sub             r1,r1,r4                ; number of new pages
684
        cmp             r1,mem_pages_free       ; are there enough free pages ?
685
        bhi             brk2
686
        ld              r5,mem_pages_free
687
        sub             r5,r5,r1
688
        st              r5,mem_pages_free
689
        ld              r6,r1                   ; r6 = number of pages to allocate
690
        add             r1,r1,r4                ; get back value of address
691
        sta             TCB_npages,x
692
        lda             #0
693
brk5:
694
        bmt             PageMap                 ; test if page is free
695
        bne             brk4                    ; no, go for next page
696
        bms             PageMap                 ; allocate the page
697
        sta             MMU,r4                  ; store the page number in the MMU table
698
        add             r4,#1                   ; move to next MMU entry
699
        sub             r6,#1                   ; decrement count of needed
700
        beq             brk6                    ; we're done if count = 0
701
brk4:
702
        ina
703
        cmp             #2048
704
        blo             brk5
705
 
706
        ; Here there was an OS or hardware error
707
        ; According to mem_pages_free there should have been enough free pages
708
        ; to fulfill the request. Something is corrupt.
709
        ;
710
 
711
        ; Here we are reducing the program break, which means freeing up pages of
712
        ; memory.
713
brk1:
714
        sta             TCB_npages,x
715
        add             r5,r1,#1                ; move to page after last page
716
brk7:
717
        cmp             r5,r4                   ; are we done freeing pages ?
718
        bhi             brk6
719
        lda             MMU,r5                  ; get the page to free
720
        bmc             PageMap                 ; free the page
721
        inc             mem_pages_free
722
        add             r5,#1
723
        bra             brk7
724
 
725
        ; Successful return
726
brk6:
727
        pop             r6
728
        pop             r5
729
        pop             r4
730
        plx
731
        lda             #0
732
        rts
733
 
734
; Return insufficient memory error
735
;
736
brk2:
737
        lda             #E_NoMem
738
        sta             TCB_errno,x
739
        pop             r6
740
        pop             r5
741
        pop             r4
742
        plx
743
        lda             #-1
744
        rts
745
 
746
;------------------------------------------------------------------------------
747
; Parameters:
748
; r1 = change in memory allocation
749
;------------------------------------------------------------------------------
750
public _sbrk:
751
        phx
752
        push    r4
753
        push    r5
754
        ldx             RunningTCB
755
        ld              r4,TCB_npages,x         ; get the current memory allocation
756
        cmp             r1,#0                           ; zero difference = get old brk address
757
        beq             sbrk2
758
        asl             r5,r4,#14                       ; convert to words
759
        add             r1,r1,r5                                ; +/- amount
760
        jsr             _brk
761
        cmp             r1,#-1
762
        bne             sbrk2
763
 
764
; Failure return, return -1
765
;
766
        pop             r5
767
        pop             r4
768
        plx
769
        rts
770
 
771
; Successful return, return the old break address
772
;
773
sbrk2:
774
        ld              r1,r4
775
        asl             r1,r1,#14
776
        pop             r5
777
        pop             r4
778
        plx
779
        rts
780
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.