1
0
mirror of https://github.com/cc65/cc65.git synced 2025-08-08 06:25:17 +00:00

Use structs

git-svn-id: svn://svn.cc65.org/cc65/trunk@2707 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz
2003-12-03 10:15:33 +00:00
parent 92a001d3af
commit c15fd58d3b
15 changed files with 64 additions and 60 deletions

View File

@@ -7,10 +7,11 @@
; Assembler include file that makes the constants and structures in _file.h ; Assembler include file that makes the constants and structures in _file.h
; available for asm code. ; available for asm code.
; Struct _FILE offsets and size ; Struct _FILE
_FILE_f_fd = $00 .struct _FILE
_FILE_f_flags = $01 f_fd .byte
_FILE_size = $02 f_flags .byte
.endstruct
; Flags field ; Flags field
_FCLOSED = $00 _FCLOSED = $00

View File

@@ -7,15 +7,18 @@
; Assembler include file that makes the constants and structures in _heap.h ; Assembler include file that makes the constants and structures in _heap.h
; available for asm code. ; available for asm code.
HEAP_ADMIN_SPACE = 2 ; Struct freeblock
HEAP_MIN_BLOCKSIZE = 6 ; Minimum size of an allocated block ; 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 HEAP_ADMIN_SPACE = 2
; code often uses increment/decrement operators to access other offsets, so HEAP_MIN_BLOCKSIZE = .sizeof (freeblock) ; Minimum size of an allocated block
; just changing offsets here will probably not work.
freeblock_size = 0
freeblock_next = 2
freeblock_prev = 4
; Variables ; Variables
.global __heaporg .global __heaporg

View File

@@ -96,13 +96,13 @@ L1: lda sp,x
lda #0 lda #0
jsr getfd jsr getfd
sta __filetab + (0 * _FILE_size) ; setup stdin sta __filetab + (0 * .sizeof(_FILE)); setup stdin
lda #0 lda #0
jsr getfd jsr getfd
sta __filetab + (1 * _FILE_size) ; setup stdout sta __filetab + (1 * .sizeof(_FILE)); setup stdout
lda #0 lda #0
jsr getfd jsr getfd
sta __filetab + (2 * _FILE_size) ; setup stderr sta __filetab + (2 * .sizeof(_FILE)); setup stderr
; Push arguments and call main ; Push arguments and call main

View File

@@ -15,12 +15,12 @@
ldy #0 ldy #0
lda #_FOPEN 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 beq Found ; jump if closed
.repeat ::_FILE_size .repeat .sizeof(_FILE)
iny iny
.endrepeat .endrepeat
cpy #(FOPEN_MAX * _FILE_size) ; Done? cpy #(FOPEN_MAX * .sizeof(_FILE)) ; Done?
bne Loop bne Loop
; File table is full ; File table is full

View File

@@ -5,7 +5,7 @@
; ;
.export __filetab .export __filetab
.include "stdio.inc" .include "stdio.inc"
.include "fcntl.inc" .include "fcntl.inc"
.include "_file.inc" .include "_file.inc"
@@ -27,12 +27,12 @@ __filetab:
; Standard file descriptors ; Standard file descriptors
_stdin: _stdin:
.word __filetab + (STDIN_FILENO * _FILE_size) .word __filetab + (STDIN_FILENO * .sizeof(_FILE))
_stdout: _stdout:
.word __filetab + (STDOUT_FILENO * _FILE_size) .word __filetab + (STDOUT_FILENO * .sizeof(_FILE))
_stderr: _stderr:
.word __filetab + (STDERR_FILENO * _FILE_size) .word __filetab + (STDERR_FILENO * .sizeof(_FILE))

View File

@@ -1,4 +1,4 @@
; ;
; Ullrich von Bassewitz, 22.11.2002 ; Ullrich von Bassewitz, 22.11.2002
; ;
; FILE* __fastcall__ _fopen (const char* name, const char* mode, FILE* f); ; FILE* __fastcall__ _fopen (const char* name, const char* mode, FILE* f);
@@ -101,9 +101,9 @@ openok: ldy file
sty ptr1 sty ptr1
ldy file+1 ldy file+1
sty ptr1+1 sty ptr1+1
ldy #_FILE_f_fd ldy #_FILE::f_fd
sta (ptr1),y ; file->f_fd = fd; sta (ptr1),y ; file->f_fd = fd;
ldy #_FILE_f_flags ldy #_FILE::f_flags
lda #_FOPEN lda #_FOPEN
sta (ptr1),y ; file->f_flags = _FOPEN; sta (ptr1),y ; file->f_flags = _FOPEN;

View File

@@ -40,7 +40,7 @@ __heapadd:
; The block is large enough. Set the size field in the block. ; The block is large enough. Set the size field in the block.
@L1: ldy #freeblock_size @L1: ldy #freeblock::size
sta (ptr2),y sta (ptr2),y
iny iny
txa txa

View File

@@ -6,7 +6,7 @@
; size_t __fastcall__ _heapmaxavail (void); ; size_t __fastcall__ _heapmaxavail (void);
; ;
; ;
.importzp ptr1, ptr2 .importzp ptr1, ptr2
.export __heapmaxavail .export __heapmaxavail
@@ -42,7 +42,7 @@ __heapmaxavail:
; if (Size < F->size) { ; if (Size < F->size) {
ldy #freeblock_size ldy #freeblock::size
lda ptr2 lda ptr2
sub (ptr1),y sub (ptr1),y
iny iny
@@ -52,7 +52,7 @@ __heapmaxavail:
; Size = F->size; ; Size = F->size;
ldy #freeblock_size ldy #freeblock::size
lda (ptr1),y lda (ptr1),y
sta ptr2 sta ptr2
iny iny

View File

@@ -19,7 +19,7 @@
__heapmemavail: __heapmemavail:
; size_t Size = 0; ; size_t Size = 0;
lda #0 lda #0
sta ptr2 sta ptr2
@@ -39,7 +39,7 @@ __heapmemavail:
; Size += F->size; ; Size += F->size;
ldy #freeblock_size ldy #freeblock::size
lda (ptr1),y lda (ptr1),y
add ptr2 add ptr2
sta ptr2 sta ptr2

View File

@@ -8,7 +8,7 @@
.export _fclose .export _fclose
.import _close .import _close
.importzp ptr1 .importzp ptr1
.include "errno.inc" .include "errno.inc"
.include "_file.inc" .include "_file.inc"
@@ -23,7 +23,7 @@
; Check if the file is really open ; Check if the file is really open
ldy #_FILE_f_flags ldy #_FILE::f_flags
lda (ptr1),y lda (ptr1),y
and #_FOPEN and #_FOPEN
bne @L1 bne @L1
@@ -43,7 +43,7 @@
@L1: lda #_FCLOSED @L1: lda #_FCLOSED
sta (ptr1),y sta (ptr1),y
ldy #_FILE_f_fd ldy #_FILE::f_fd
lda (ptr1),y lda (ptr1),y
ldx #0 ldx #0
jmp _close ; Will set errno and return an error flag jmp _close ; Will set errno and return an error flag

View File

@@ -16,7 +16,7 @@
getf: sta ptr1 getf: sta ptr1
stx ptr1+1 stx ptr1+1
ldy #_FILE_f_flags ldy #_FILE::f_flags
lda (ptr1),y ; get f->f_flags lda (ptr1),y ; get f->f_flags
and #_FOPEN ; file open? and #_FOPEN ; file open?
beq @L1 ; jump if no beq @L1 ; jump if no

View File

@@ -31,7 +31,7 @@
; Check if the file is open ; Check if the file is open
ldy #_FILE_f_flags ldy #_FILE::f_flags
lda (ptr1),y lda (ptr1),y
and #_FOPEN ; Is the file open? and #_FOPEN ; Is the file open?
bne @L2 ; Branch if yes bne @L2 ; Branch if yes
@@ -53,7 +53,7 @@
; Build the stackframe for read() ; Build the stackframe for read()
ldy #_FILE_f_fd ldy #_FILE::f_fd
lda (ptr1),y lda (ptr1),y
ldx #$00 ldx #$00
jsr pushax ; file->f_fd jsr pushax ; file->f_fd
@@ -104,7 +104,7 @@
sta ptr1 sta ptr1
lda file+1 lda file+1
sta ptr1+1 sta ptr1+1
ldy #_FILE_f_flags ldy #_FILE::f_flags
lda (ptr1),y lda (ptr1),y
ora tmp1 ora tmp1
sta (ptr1),y sta (ptr1),y

View File

@@ -86,7 +86,7 @@ _free: sta ptr2
sta ptr2 sta ptr2
bcs @L1 bcs @L1
dec ptr2+1 dec ptr2+1
@L1: ldy #freeblock_size+1 @L1: ldy #freeblock::size+1
lda (ptr2),y ; High byte of size lda (ptr2),y ; High byte of size
sta ptr1+1 ; Save it sta ptr1+1 ; Save it
dey dey
@@ -122,7 +122,7 @@ _free: sta ptr2
lda __heaplast+1 lda __heaplast+1
sta ptr1+1 ; Pointer to last block now in ptr1 sta ptr1+1 ; Pointer to last block now in ptr1
ldy #freeblock_size ldy #freeblock::size
lda (ptr1),y ; Low byte of block size lda (ptr1),y ; Low byte of block size
add ptr1 add ptr1
tax tax
@@ -144,7 +144,7 @@ _free: sta ptr2
; Correct the next pointer of the now last block ; 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 lda (ptr1),y
sta ptr2+1 ; Remember f->prev in ptr2 sta ptr2+1 ; Remember f->prev in ptr2
sta __heaplast+1 sta __heaplast+1
@@ -282,10 +282,10 @@ heapadd:
; The free list is empty, so this is the first and only block. A contains ; The free list is empty, so this is the first and only block. A contains
; zero if we come here. ; zero if we come here.
ldy #freeblock_next-1 ldy #freeblock::next-1
@L2: iny ; f->next = f->prev = 0; @L2: iny ; f->next = f->prev = 0;
sta (ptr2),y sta (ptr2),y
cpy #freeblock_prev+1 ; Done? cpy #freeblock::prev+1 ; Done?
bne @L2 bne @L2
lda ptr2 lda ptr2
@@ -308,7 +308,7 @@ SearchFreeList:
lda #0 lda #0
sta ptr4 sta ptr4
sta ptr4+1 ; left = 0; sta ptr4+1 ; left = 0;
ldy #freeblock_next+1 ldy #freeblock::next+1
ldx ptr3 ldx ptr3
@Loop: lda ptr3+1 ; High byte of right @Loop: lda ptr3+1 ; High byte of right
@@ -367,7 +367,7 @@ CheckRightMerge:
; Merge with the right block. Do f->size += right->size; ; Merge with the right block. Do f->size += right->size;
ldy #freeblock_size ldy #freeblock::size
lda ptr1 lda ptr1
add (ptr3),y add (ptr3),y
sta (ptr2),y sta (ptr2),y
@@ -411,7 +411,7 @@ CheckRightMerge:
; No right merge, just set the link. ; No right merge, just set the link.
NoRightMerge: NoRightMerge:
ldy #freeblock_next ; f->next = right; ldy #freeblock::next ; f->next = right;
lda ptr3 lda ptr3
sta (ptr2),y sta (ptr2),y
iny ; Points to next+1 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 ; 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; sta (ptr2),y ; f->prev = 0;
iny iny
sta (ptr2),y sta (ptr2),y
@@ -449,7 +449,7 @@ CheckLeftMerge:
; Check if the left block is adjacent to the following one ; Check if the left block is adjacent to the following one
CheckLeftMerge2: CheckLeftMerge2:
ldy #freeblock_size ; Calculate left + left->size ldy #freeblock::size ; Calculate left + left->size
lda (ptr4),y ; Low byte of left->size lda (ptr4),y ; Low byte of left->size
add ptr4 add ptr4
tax tax
@@ -529,4 +529,4 @@ NoLeftMerge:

View File

@@ -31,7 +31,7 @@
; Check if the file is open ; Check if the file is open
ldy #_FILE_f_flags ldy #_FILE::f_flags
lda (ptr1),y lda (ptr1),y
and #_FOPEN ; Is the file open? and #_FOPEN ; Is the file open?
bne @L2 ; Branch if yes bne @L2 ; Branch if yes
@@ -53,7 +53,7 @@
; Build the stackframe for write() ; Build the stackframe for write()
ldy #_FILE_f_fd ldy #_FILE::f_fd
lda (ptr1),y lda (ptr1),y
ldx #$00 ldx #$00
jsr pushax ; file->f_fd jsr pushax ; file->f_fd
@@ -102,7 +102,7 @@
sta ptr1 sta ptr1
lda file+1 lda file+1
sta ptr1+1 sta ptr1+1
ldy #_FILE_f_flags ldy #_FILE::f_flags
lda (ptr1),y lda (ptr1),y
ora #_FERROR ora #_FERROR
sta (ptr1),y sta (ptr1),y

View File

@@ -150,21 +150,21 @@ _malloc:
jmp @L4 jmp @L4
@L3: ldy #freeblock_size @L3: ldy #freeblock::size
lda (ptr2),y lda (ptr2),y
sub ptr1 sub ptr1
tax ; Remember low byte for later tax ; Remember low byte for later
iny ; Y points to freeblock_size+1 iny ; Y points to freeblock::size+1
lda (ptr2),y lda (ptr2),y
sbc ptr1+1 sbc ptr1+1
bcs BlockFound ; Beware: Contents of a/x/y are known! bcs BlockFound ; Beware: Contents of a/x/y are known!
; Next block in list ; Next block in list
iny ; Points to freeblock_next iny ; Points to freeblock::next
lda (ptr2),y lda (ptr2),y
tax tax
iny ; Points to freeblock_next+1 iny ; Points to freeblock::next+1
lda (ptr2),y lda (ptr2),y
stx ptr2 stx ptr2
sta ptr2+1 sta ptr2+1
@@ -221,13 +221,13 @@ BlockFound:
; does already contain the correct size word, all we have to do is to ; does already contain the correct size word, all we have to do is to
; remove it from the free list. ; remove it from the free list.
ldy #freeblock_prev+1 ; Load f->prev ldy #freeblock::prev+1 ; Load f->prev
lda (ptr2),y lda (ptr2),y
sta ptr3+1 sta ptr3+1
dey dey
lda (ptr2),y lda (ptr2),y
sta ptr3 sta ptr3
dey ; Points to freeblock_next+1 dey ; Points to freeblock::next+1
ora ptr3+1 ora ptr3+1
beq @L1 ; Jump if f->prev zero 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 ; Fill the size into the admin space of the block and return the user pointer
FillSizeAndRet: FillSizeAndRet:
ldy #freeblock_size ; *p = size; ldy #freeblock::size ; *p = size;
lda ptr1 ; Low byte of block size lda ptr1 ; Low byte of block size
sta (ptr2),y sta (ptr2),y
iny ; Points to freeblock_size+1 iny ; Points to freeblock::size+1
lda ptr1+1 lda ptr1+1
sta (ptr2),y sta (ptr2),y