mod_ctrl struct cleanup

git-svn-id: svn://svn.cc65.org/cc65/trunk@1260 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2002-04-24 18:47:11 +00:00
parent 5c4205b284
commit 231fed08f2

View file

@ -68,14 +68,12 @@ Header: .res O65_HDR_SIZE ; The o65 header
; Input ; Input
InputByte = Header ; Byte read from input InputByte = Header ; Byte read from input
; Stuff needed for relocation. Since the ld65 linker uses a relocation base ; Segment addresses. Since the ld65 linker uses a relocation base address
; address of zero for all segments, the relocation values needed are actually ; of zero for all segments, the addresses of the segments are also the
; the start addresses of the segments. Among other things this means that the ; relocation values.
; relocation value for the text segment is the same as the start address as CodeAddr = Module ; Address of code seg
; the whole module block. DataAddr = Header + 1 ; Address of data seg
TextReloc = Module ; Relocation value for code seg BssAddr = Header + 3 ; Address of bss seg
DataReloc = Header + 1 ; Relocation value for data seg
BssReloc = Header + 3 ; Relocation value for bss seg
.data .data
Read: jmp $FFFF ; Jump to read routine Read: jmp $FFFF ; Jump to read routine
@ -97,18 +95,6 @@ PushCtrl:
ldx Ctrl+1 ldx Ctrl+1
jmp pushax jmp pushax
;------------------------------------------------------------------------------
; LoadCtrl: Load a word from the control structure into a/x. The offset of the
; high byte is passed in Y.
.code
LoadCtrl:
lda (Ctrl),y
tax
dey
lda (Ctrl),y
rts
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; RestoreRegBank: Restore the register bank contents from the save area. Will ; RestoreRegBank: Restore the register bank contents from the save area. Will
; destroy A and X (the latter will be zero on return). ; destroy A and X (the latter will be zero on return).
@ -129,20 +115,20 @@ RestoreRegBank:
GetReloc: GetReloc:
cmp #O65_SEGID_TEXT cmp #O65_SEGID_TEXT
bne @L1 bne @L1
lda TextReloc lda CodeAddr
ldx TextReloc+1 ldx CodeAddr+1
rts rts
@L1: cmp #O65_SEGID_DATA @L1: cmp #O65_SEGID_DATA
bne @L2 bne @L2
lda DataReloc lda DataAddr
ldx DataReloc+1 ldx DataAddr+1
rts rts
@L2: cmp #O65_SEGID_BSS @L2: cmp #O65_SEGID_BSS
bne @L3 bne @L3
lda BssReloc lda BssAddr
ldx BssReloc+1 ldx BssAddr+1
rts rts
@L3: cmp #O65_SEGID_ZP @L3: cmp #O65_SEGID_ZP
@ -344,10 +330,12 @@ _mod_load:
; Get the read function pointer from the control structure and place it into ; Get the read function pointer from the control structure and place it into
; our call vector ; our call vector
ldy #MODCTRL_READ+1 ldy #MODCTRL_READ
jsr LoadCtrl lda (Ctrl),y
sta Read+1 sta Read+1
stx Read+2 iny
lda (Ctrl),y
sta Read+2
; Read the o65 header: C->read (C, &H, sizeof (H)) ; Read the o65 header: C->read (C, &H, sizeof (H))
@ -418,58 +406,33 @@ OptDone:
lda #MLOAD_ERR_MEM lda #MLOAD_ERR_MEM
jmp CleanupAndExit jmp CleanupAndExit
; We got the memory block. Setup the pointers and sizes in the control ; We got the memory block. Place a pointer to the memory block also in the
; structure. We will use internal knowlege about the layout of the structure ; module control structure.
; here to save some code.
GotMem: lda Module ; Ctrl->module = Module; GotMem: lda Module ; Ctrl->module = Module;
ldy #MODCTRL_MODULE ldy #MODCTRL_MODULE
sta (Ctrl),y sta (Ctrl),y
ldy #MODCTRL_CODE ; Ctrl->code = Module;
sta (Ctrl),y
txa txa
iny iny
sta (Ctrl),y ; MODCTRL_CODE+1
ldy #MODCTRL_MODULE+1
sta (Ctrl),y sta (Ctrl),y
; The following loop will also copy some information that is not needed just ; Calculate the start addresses of the segments. Since the linker uses a
; to save some code. ; base address of zero for all segments, the load addresses are also the
; relocation values for the segments.
ldx #O65_HDR_TLEN
ldy #MODCTRL_CODE_SIZE
CLoop: lda Header,x
sta (Ctrl),y
inx
iny
cpy #MODCTRL_SIZE
bne CLoop
; Missing in the control structure now: start of the data and bss segments.
; Since the linker relocates all segments to zero, these addresses are also
; the relocation values for the segments.
ldy #MODCTRL_DATA
lda Module lda Module
add Header + O65_HDR_TLEN add Header + O65_HDR_TLEN
sta (Ctrl),y sta DataAddr
sta DataReloc
iny
lda Module + 1 lda Module + 1
adc Header + O65_HDR_TLEN + 1 adc Header + O65_HDR_TLEN + 1
sta (Ctrl),y sta DataAddr + 1
sta DataReloc + 1
ldy #MODCTRL_BSS
lda Module lda Module
add TPtr add TPtr
sta (Ctrl),y sta BssAddr
sta BssReloc
iny
lda Module+1 lda Module+1
add TPtr+1 add TPtr+1
sta (Ctrl),y sta BssAddr + 1
sta BssReloc + 1
; Control structure is complete now. Load code and data segment into memory. ; Control structure is complete now. Load code and data segment into memory.
; The sum of the sizes of code+data segment is still in TPtr. ; The sum of the sizes of code+data segment is still in TPtr.
@ -494,8 +457,8 @@ CLoop: lda Header,x
beq Reloc beq Reloc
Undef: jmp FormatError Undef: jmp FormatError
; Number of undefined references was zero. Next sections are the relocation ; Number of undefined references was zero. Next come the relocation tables
; tables for code and data segment. Relocate the code segment ; for code and data segment. Relocate the code segment
Reloc: lda Module Reloc: lda Module
ldx Module + 1 ; Code segment address ldx Module + 1 ; Code segment address
@ -503,14 +466,19 @@ Reloc: lda Module
; Relocate the data segment ; Relocate the data segment
ldy #MODCTRL_DATA + 1 lda Module
jsr LoadCtrl ; Get data segment address add Header + O65_HDR_TLEN
pha
lda Module + 1
adc Header + O65_HDR_TLEN + 1
tax
pla ; Date segment address in a/x
jsr RelocSeg jsr RelocSeg
; Clear the bss segment ; Clear the bss segment: memset (bss_addr, 0, bss_size)
ldy #MODCTRL_BSS + 1 lda BssAddr
jsr LoadCtrl ; Load bss segment address ldx BssAddr + 1
jsr pushax jsr pushax
jsr push0 jsr push0
lda Header + O65_HDR_BLEN lda Header + O65_HDR_BLEN