mirror of
https://github.com/cc65/cc65.git
synced 2024-12-22 12:30:41 +00:00
Use structs
git-svn-id: svn://svn.cc65.org/cc65/trunk@2707 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
parent
92a001d3af
commit
c15fd58d3b
@ -7,10 +7,11 @@
|
||||
; Assembler include file that makes the constants and structures in _file.h
|
||||
; available for asm code.
|
||||
|
||||
; Struct _FILE offsets and size
|
||||
_FILE_f_fd = $00
|
||||
_FILE_f_flags = $01
|
||||
_FILE_size = $02
|
||||
; Struct _FILE
|
||||
.struct _FILE
|
||||
f_fd .byte
|
||||
f_flags .byte
|
||||
.endstruct
|
||||
|
||||
; Flags field
|
||||
_FCLOSED = $00
|
||||
|
@ -7,15 +7,18 @@
|
||||
; Assembler include file that makes the constants and structures in _heap.h
|
||||
; available for asm code.
|
||||
|
||||
HEAP_ADMIN_SPACE = 2
|
||||
HEAP_MIN_BLOCKSIZE = 6 ; Minimum size of an allocated block
|
||||
; Struct freeblock
|
||||
; NOTE: For performance reasons, the asm code often uses increment/decrement
|
||||
; operators to access other offsets, so just changing offsets here will
|
||||
; probably not work.
|
||||
.struct freeblock
|
||||
size .word
|
||||
next .word
|
||||
prev .word
|
||||
.endstruct
|
||||
|
||||
; Struct freeblock offsets and size. NOTE: For performance reasons, the asm
|
||||
; code often uses increment/decrement operators to access other offsets, so
|
||||
; just changing offsets here will probably not work.
|
||||
freeblock_size = 0
|
||||
freeblock_next = 2
|
||||
freeblock_prev = 4
|
||||
HEAP_ADMIN_SPACE = 2
|
||||
HEAP_MIN_BLOCKSIZE = .sizeof (freeblock) ; Minimum size of an allocated block
|
||||
|
||||
; Variables
|
||||
.global __heaporg
|
||||
|
@ -96,13 +96,13 @@ L1: lda sp,x
|
||||
|
||||
lda #0
|
||||
jsr getfd
|
||||
sta __filetab + (0 * _FILE_size) ; setup stdin
|
||||
sta __filetab + (0 * .sizeof(_FILE)); setup stdin
|
||||
lda #0
|
||||
jsr getfd
|
||||
sta __filetab + (1 * _FILE_size) ; setup stdout
|
||||
sta __filetab + (1 * .sizeof(_FILE)); setup stdout
|
||||
lda #0
|
||||
jsr getfd
|
||||
sta __filetab + (2 * _FILE_size) ; setup stderr
|
||||
sta __filetab + (2 * .sizeof(_FILE)); setup stderr
|
||||
|
||||
; Push arguments and call main
|
||||
|
||||
|
@ -15,12 +15,12 @@
|
||||
|
||||
ldy #0
|
||||
lda #_FOPEN
|
||||
Loop: and __filetab + _FILE_f_flags,y ; load flags
|
||||
Loop: and __filetab + _FILE::f_flags,y ; load flags
|
||||
beq Found ; jump if closed
|
||||
.repeat ::_FILE_size
|
||||
.repeat .sizeof(_FILE)
|
||||
iny
|
||||
.endrepeat
|
||||
cpy #(FOPEN_MAX * _FILE_size) ; Done?
|
||||
cpy #(FOPEN_MAX * .sizeof(_FILE)) ; Done?
|
||||
bne Loop
|
||||
|
||||
; File table is full
|
||||
|
@ -27,12 +27,12 @@ __filetab:
|
||||
; Standard file descriptors
|
||||
|
||||
_stdin:
|
||||
.word __filetab + (STDIN_FILENO * _FILE_size)
|
||||
.word __filetab + (STDIN_FILENO * .sizeof(_FILE))
|
||||
|
||||
_stdout:
|
||||
.word __filetab + (STDOUT_FILENO * _FILE_size)
|
||||
.word __filetab + (STDOUT_FILENO * .sizeof(_FILE))
|
||||
|
||||
_stderr:
|
||||
.word __filetab + (STDERR_FILENO * _FILE_size)
|
||||
.word __filetab + (STDERR_FILENO * .sizeof(_FILE))
|
||||
|
||||
|
||||
|
@ -101,9 +101,9 @@ openok: ldy file
|
||||
sty ptr1
|
||||
ldy file+1
|
||||
sty ptr1+1
|
||||
ldy #_FILE_f_fd
|
||||
ldy #_FILE::f_fd
|
||||
sta (ptr1),y ; file->f_fd = fd;
|
||||
ldy #_FILE_f_flags
|
||||
ldy #_FILE::f_flags
|
||||
lda #_FOPEN
|
||||
sta (ptr1),y ; file->f_flags = _FOPEN;
|
||||
|
||||
|
@ -40,7 +40,7 @@ __heapadd:
|
||||
|
||||
; The block is large enough. Set the size field in the block.
|
||||
|
||||
@L1: ldy #freeblock_size
|
||||
@L1: ldy #freeblock::size
|
||||
sta (ptr2),y
|
||||
iny
|
||||
txa
|
||||
|
@ -42,7 +42,7 @@ __heapmaxavail:
|
||||
|
||||
; if (Size < F->size) {
|
||||
|
||||
ldy #freeblock_size
|
||||
ldy #freeblock::size
|
||||
lda ptr2
|
||||
sub (ptr1),y
|
||||
iny
|
||||
@ -52,7 +52,7 @@ __heapmaxavail:
|
||||
|
||||
; Size = F->size;
|
||||
|
||||
ldy #freeblock_size
|
||||
ldy #freeblock::size
|
||||
lda (ptr1),y
|
||||
sta ptr2
|
||||
iny
|
||||
|
@ -39,7 +39,7 @@ __heapmemavail:
|
||||
|
||||
; Size += F->size;
|
||||
|
||||
ldy #freeblock_size
|
||||
ldy #freeblock::size
|
||||
lda (ptr1),y
|
||||
add ptr2
|
||||
sta ptr2
|
||||
|
@ -23,7 +23,7 @@
|
||||
|
||||
; Check if the file is really open
|
||||
|
||||
ldy #_FILE_f_flags
|
||||
ldy #_FILE::f_flags
|
||||
lda (ptr1),y
|
||||
and #_FOPEN
|
||||
bne @L1
|
||||
@ -43,7 +43,7 @@
|
||||
@L1: lda #_FCLOSED
|
||||
sta (ptr1),y
|
||||
|
||||
ldy #_FILE_f_fd
|
||||
ldy #_FILE::f_fd
|
||||
lda (ptr1),y
|
||||
ldx #0
|
||||
jmp _close ; Will set errno and return an error flag
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
getf: sta ptr1
|
||||
stx ptr1+1
|
||||
ldy #_FILE_f_flags
|
||||
ldy #_FILE::f_flags
|
||||
lda (ptr1),y ; get f->f_flags
|
||||
and #_FOPEN ; file open?
|
||||
beq @L1 ; jump if no
|
||||
|
@ -31,7 +31,7 @@
|
||||
|
||||
; Check if the file is open
|
||||
|
||||
ldy #_FILE_f_flags
|
||||
ldy #_FILE::f_flags
|
||||
lda (ptr1),y
|
||||
and #_FOPEN ; Is the file open?
|
||||
bne @L2 ; Branch if yes
|
||||
@ -53,7 +53,7 @@
|
||||
|
||||
; Build the stackframe for read()
|
||||
|
||||
ldy #_FILE_f_fd
|
||||
ldy #_FILE::f_fd
|
||||
lda (ptr1),y
|
||||
ldx #$00
|
||||
jsr pushax ; file->f_fd
|
||||
@ -104,7 +104,7 @@
|
||||
sta ptr1
|
||||
lda file+1
|
||||
sta ptr1+1
|
||||
ldy #_FILE_f_flags
|
||||
ldy #_FILE::f_flags
|
||||
lda (ptr1),y
|
||||
ora tmp1
|
||||
sta (ptr1),y
|
||||
|
@ -86,7 +86,7 @@ _free: sta ptr2
|
||||
sta ptr2
|
||||
bcs @L1
|
||||
dec ptr2+1
|
||||
@L1: ldy #freeblock_size+1
|
||||
@L1: ldy #freeblock::size+1
|
||||
lda (ptr2),y ; High byte of size
|
||||
sta ptr1+1 ; Save it
|
||||
dey
|
||||
@ -122,7 +122,7 @@ _free: sta ptr2
|
||||
lda __heaplast+1
|
||||
sta ptr1+1 ; Pointer to last block now in ptr1
|
||||
|
||||
ldy #freeblock_size
|
||||
ldy #freeblock::size
|
||||
lda (ptr1),y ; Low byte of block size
|
||||
add ptr1
|
||||
tax
|
||||
@ -144,7 +144,7 @@ _free: sta ptr2
|
||||
|
||||
; Correct the next pointer of the now last block
|
||||
|
||||
ldy #freeblock_prev+1 ; Offset of ->prev field
|
||||
ldy #freeblock::prev+1 ; Offset of ->prev field
|
||||
lda (ptr1),y
|
||||
sta ptr2+1 ; Remember f->prev in ptr2
|
||||
sta __heaplast+1
|
||||
@ -282,10 +282,10 @@ heapadd:
|
||||
; The free list is empty, so this is the first and only block. A contains
|
||||
; zero if we come here.
|
||||
|
||||
ldy #freeblock_next-1
|
||||
ldy #freeblock::next-1
|
||||
@L2: iny ; f->next = f->prev = 0;
|
||||
sta (ptr2),y
|
||||
cpy #freeblock_prev+1 ; Done?
|
||||
cpy #freeblock::prev+1 ; Done?
|
||||
bne @L2
|
||||
|
||||
lda ptr2
|
||||
@ -308,7 +308,7 @@ SearchFreeList:
|
||||
lda #0
|
||||
sta ptr4
|
||||
sta ptr4+1 ; left = 0;
|
||||
ldy #freeblock_next+1
|
||||
ldy #freeblock::next+1
|
||||
ldx ptr3
|
||||
|
||||
@Loop: lda ptr3+1 ; High byte of right
|
||||
@ -367,7 +367,7 @@ CheckRightMerge:
|
||||
|
||||
; Merge with the right block. Do f->size += right->size;
|
||||
|
||||
ldy #freeblock_size
|
||||
ldy #freeblock::size
|
||||
lda ptr1
|
||||
add (ptr3),y
|
||||
sta (ptr2),y
|
||||
@ -411,7 +411,7 @@ CheckRightMerge:
|
||||
; No right merge, just set the link.
|
||||
|
||||
NoRightMerge:
|
||||
ldy #freeblock_next ; f->next = right;
|
||||
ldy #freeblock::next ; f->next = right;
|
||||
lda ptr3
|
||||
sta (ptr2),y
|
||||
iny ; Points to next+1
|
||||
@ -434,7 +434,7 @@ CheckLeftMerge:
|
||||
|
||||
; We don't have a left block, so f is actually the new freelist start
|
||||
|
||||
ldy #freeblock_prev
|
||||
ldy #freeblock::prev
|
||||
sta (ptr2),y ; f->prev = 0;
|
||||
iny
|
||||
sta (ptr2),y
|
||||
@ -449,7 +449,7 @@ CheckLeftMerge:
|
||||
; Check if the left block is adjacent to the following one
|
||||
|
||||
CheckLeftMerge2:
|
||||
ldy #freeblock_size ; Calculate left + left->size
|
||||
ldy #freeblock::size ; Calculate left + left->size
|
||||
lda (ptr4),y ; Low byte of left->size
|
||||
add ptr4
|
||||
tax
|
||||
|
@ -31,7 +31,7 @@
|
||||
|
||||
; Check if the file is open
|
||||
|
||||
ldy #_FILE_f_flags
|
||||
ldy #_FILE::f_flags
|
||||
lda (ptr1),y
|
||||
and #_FOPEN ; Is the file open?
|
||||
bne @L2 ; Branch if yes
|
||||
@ -53,7 +53,7 @@
|
||||
|
||||
; Build the stackframe for write()
|
||||
|
||||
ldy #_FILE_f_fd
|
||||
ldy #_FILE::f_fd
|
||||
lda (ptr1),y
|
||||
ldx #$00
|
||||
jsr pushax ; file->f_fd
|
||||
@ -102,7 +102,7 @@
|
||||
sta ptr1
|
||||
lda file+1
|
||||
sta ptr1+1
|
||||
ldy #_FILE_f_flags
|
||||
ldy #_FILE::f_flags
|
||||
lda (ptr1),y
|
||||
ora #_FERROR
|
||||
sta (ptr1),y
|
||||
|
@ -150,21 +150,21 @@ _malloc:
|
||||
|
||||
jmp @L4
|
||||
|
||||
@L3: ldy #freeblock_size
|
||||
@L3: ldy #freeblock::size
|
||||
lda (ptr2),y
|
||||
sub ptr1
|
||||
tax ; Remember low byte for later
|
||||
iny ; Y points to freeblock_size+1
|
||||
iny ; Y points to freeblock::size+1
|
||||
lda (ptr2),y
|
||||
sbc ptr1+1
|
||||
bcs BlockFound ; Beware: Contents of a/x/y are known!
|
||||
|
||||
; Next block in list
|
||||
|
||||
iny ; Points to freeblock_next
|
||||
iny ; Points to freeblock::next
|
||||
lda (ptr2),y
|
||||
tax
|
||||
iny ; Points to freeblock_next+1
|
||||
iny ; Points to freeblock::next+1
|
||||
lda (ptr2),y
|
||||
stx ptr2
|
||||
sta ptr2+1
|
||||
@ -221,13 +221,13 @@ BlockFound:
|
||||
; does already contain the correct size word, all we have to do is to
|
||||
; remove it from the free list.
|
||||
|
||||
ldy #freeblock_prev+1 ; Load f->prev
|
||||
ldy #freeblock::prev+1 ; Load f->prev
|
||||
lda (ptr2),y
|
||||
sta ptr3+1
|
||||
dey
|
||||
lda (ptr2),y
|
||||
sta ptr3
|
||||
dey ; Points to freeblock_next+1
|
||||
dey ; Points to freeblock::next+1
|
||||
ora ptr3+1
|
||||
beq @L1 ; Jump if f->prev zero
|
||||
|
||||
@ -312,10 +312,10 @@ SliceBlock:
|
||||
; Fill the size into the admin space of the block and return the user pointer
|
||||
|
||||
FillSizeAndRet:
|
||||
ldy #freeblock_size ; *p = size;
|
||||
ldy #freeblock::size ; *p = size;
|
||||
lda ptr1 ; Low byte of block size
|
||||
sta (ptr2),y
|
||||
iny ; Points to freeblock_size+1
|
||||
iny ; Points to freeblock::size+1
|
||||
lda ptr1+1
|
||||
sta (ptr2),y
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user