2002-04-21 14:29:30 +00:00
|
|
|
;*****************************************************************************/
|
|
|
|
;* */
|
|
|
|
;* modload.s */
|
|
|
|
;* */
|
|
|
|
;* o65 module loader for the cc65 library */
|
|
|
|
;* */
|
|
|
|
;* */
|
|
|
|
;* */
|
|
|
|
;* (C) 2002 Ullrich von Bassewitz */
|
|
|
|
;* Wacholderweg 14 */
|
|
|
|
;* D-70597 Stuttgart */
|
|
|
|
;* EMail: uz@musoftware.de */
|
|
|
|
;* */
|
|
|
|
;* */
|
|
|
|
;* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
;* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
;* arising from the use of this software. */
|
|
|
|
;* */
|
|
|
|
;* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
;* including commercial applications, and to alter it and redistribute it */
|
|
|
|
;* freely, subject to the following restrictions: */
|
|
|
|
;* */
|
|
|
|
;* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
;* claim that you wrote the original software. If you use this software */
|
|
|
|
;* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
;* appreciated but is not required. */
|
|
|
|
;* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
;* be misrepresented as being the original software. */
|
|
|
|
;* 3. This notice may not be removed or altered from any source */
|
|
|
|
;* distribution. */
|
|
|
|
;* */
|
|
|
|
;*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.include "o65.inc"
|
|
|
|
.include "modload.inc"
|
2012-11-04 15:23:05 +00:00
|
|
|
.include "zeropage.inc"
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
.import pushax, pusha0, push0, push1, decax1
|
2002-05-18 21:38:01 +00:00
|
|
|
.import _malloc, _free, _bzero
|
2002-04-23 19:51:45 +00:00
|
|
|
.import __ZP_START__ ; Linker generated
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
.macpack generic
|
|
|
|
|
|
|
|
;------------------------------------------------------------------------------
|
|
|
|
; Variables stored in the register bank in the zero page. Placing the variables
|
|
|
|
; here will protect them when calling other C functions.
|
|
|
|
|
|
|
|
Module = regbank+0 ; Pointer to module memory
|
|
|
|
Ctrl = regbank+2 ; Pointer to mod_ctrl structure
|
|
|
|
TPtr = regbank+4 ; Pointer to module data for relocation
|
|
|
|
|
|
|
|
;------------------------------------------------------------------------------
|
|
|
|
; Static module data
|
|
|
|
|
|
|
|
.bss
|
|
|
|
|
|
|
|
; Save areas and error recovery data
|
|
|
|
Stack: .byte 0 ; Old stackpointer
|
2012-11-04 15:23:05 +00:00
|
|
|
RegBankSave: .res regbanksize ; Save area for register bank
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-04-23 19:51:45 +00:00
|
|
|
; The header of the o65 file. Since we don't need the first 8 bytes any
|
|
|
|
; longer, once we've checked them, we will overlay them with other data to
|
|
|
|
; save a few bytes.
|
2003-12-03 20:54:43 +00:00
|
|
|
Header: .tag O65_HDR ; The o65 header
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
; Input
|
|
|
|
InputByte = Header ; Byte read from input
|
|
|
|
|
2002-06-23 14:02:09 +00:00
|
|
|
; Relocation
|
|
|
|
RelocVal = Header + 1 ; Relocation value
|
|
|
|
|
2002-11-18 18:54:36 +00:00
|
|
|
.data
|
2002-05-18 21:38:01 +00:00
|
|
|
Read: jmp $FFFF ; Jump to read routine
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
.rodata
|
2002-05-18 21:38:01 +00:00
|
|
|
ExpectedHdr:
|
2003-02-07 11:03:30 +00:00
|
|
|
.byte O65_MARKER_0, O65_MARKER_1 ; non C64 marker
|
|
|
|
.byte O65_MAGIC_0, O65_MAGIC_1, O65_MAGIC_2 ; Magic ("o65")
|
|
|
|
.byte O65_VERSION ; Version
|
|
|
|
.word O65_MODE_CC65 ; Mode word
|
2002-05-18 21:38:01 +00:00
|
|
|
|
2002-04-21 14:29:30 +00:00
|
|
|
ExpectedHdrSize = * - ExpectedHdr
|
|
|
|
|
|
|
|
|
|
|
|
;------------------------------------------------------------------------------
|
2002-11-20 14:21:10 +00:00
|
|
|
; PushCallerData: Push the callerdata member from control structure onto the
|
|
|
|
; C stack.
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
.code
|
2002-11-20 14:21:10 +00:00
|
|
|
PushCallerData:
|
2003-12-03 19:57:27 +00:00
|
|
|
ldy #MOD_CTRL::CALLERDATA+1
|
2002-11-20 14:21:10 +00:00
|
|
|
lda (Ctrl),y
|
|
|
|
tax
|
|
|
|
dey
|
|
|
|
lda (Ctrl),y
|
2002-04-21 14:29:30 +00:00
|
|
|
jmp pushax
|
|
|
|
|
|
|
|
;------------------------------------------------------------------------------
|
|
|
|
; RestoreRegBank: Restore the register bank contents from the save area. Will
|
2002-04-23 19:51:45 +00:00
|
|
|
; destroy A and X (the latter will be zero on return).
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
.code
|
|
|
|
RestoreRegBank:
|
2002-04-23 19:51:45 +00:00
|
|
|
ldx #6
|
|
|
|
@L1: lda RegBankSave-1,x
|
|
|
|
sta regbank-1,x
|
2002-04-21 14:29:30 +00:00
|
|
|
dex
|
2002-04-23 19:51:45 +00:00
|
|
|
bne @L1
|
2002-04-21 14:29:30 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
;------------------------------------------------------------------------------
|
2002-05-18 21:38:01 +00:00
|
|
|
; GetReloc: Return a relocation value based on the segment in A.
|
|
|
|
; The routine uses some knowledge about the values to make the code shorter.
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
.code
|
|
|
|
GetReloc:
|
|
|
|
cmp #O65_SEGID_TEXT
|
2002-05-18 21:38:01 +00:00
|
|
|
bcc FormatError
|
|
|
|
cmp #O65_SEGID_ZP
|
|
|
|
beq @L1
|
|
|
|
bcs FormatError
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-05-18 21:38:01 +00:00
|
|
|
; Text, data and bss segment
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-05-18 21:38:01 +00:00
|
|
|
lda Module
|
|
|
|
ldx Module+1 ; Return start address of buffer
|
2002-04-21 14:29:30 +00:00
|
|
|
rts
|
|
|
|
|
2002-05-18 21:38:01 +00:00
|
|
|
; Zero page relocation
|
|
|
|
|
|
|
|
@L1: lda #<__ZP_START__
|
2002-04-23 19:51:45 +00:00
|
|
|
ldx #>__ZP_START__
|
|
|
|
rts
|
|
|
|
|
2002-11-20 14:21:10 +00:00
|
|
|
;------------------------------------------------------------------------------
|
|
|
|
; ReadByte: Read one byte with error checking into InputByte and A.
|
|
|
|
; ReadAndCheckError: Call read with the current C stack and check for errors.
|
|
|
|
|
|
|
|
.bss
|
|
|
|
ReadSize: .res 2
|
|
|
|
|
|
|
|
.code
|
|
|
|
ReadByte:
|
|
|
|
|
|
|
|
; C->read (C->callerdata, &B, 1)
|
|
|
|
|
|
|
|
jsr PushCallerData
|
|
|
|
lda #<InputByte
|
|
|
|
ldx #>InputByte
|
|
|
|
jsr pushax
|
|
|
|
ldx #0
|
|
|
|
lda #1
|
|
|
|
|
|
|
|
; This is a second entry point used by the other calls to Read
|
|
|
|
|
|
|
|
ReadAndCheckError:
|
|
|
|
sta ReadSize
|
|
|
|
stx ReadSize+1
|
|
|
|
jsr Read
|
|
|
|
|
|
|
|
; Check the return code and bail out in case of problems
|
|
|
|
|
|
|
|
cmp ReadSize
|
|
|
|
bne @L1
|
|
|
|
cpx ReadSize+1
|
|
|
|
beq @L2 ; Jump if ok
|
|
|
|
@L1: lda #MLOAD_ERR_READ
|
|
|
|
bne CleanupAndExit
|
|
|
|
|
|
|
|
; Done
|
|
|
|
|
|
|
|
@L2: lda InputByte ; If called ReadByte, load the byte read
|
|
|
|
Done: rts
|
|
|
|
|
2002-04-23 19:51:45 +00:00
|
|
|
;------------------------------------------------------------------------------
|
|
|
|
; FormatError: Bail out with an o65 format error
|
2002-06-23 14:02:09 +00:00
|
|
|
|
2002-05-18 21:38:01 +00:00
|
|
|
.code
|
2002-04-23 19:51:45 +00:00
|
|
|
FormatError:
|
|
|
|
lda #MLOAD_ERR_FMT
|
2002-04-21 14:29:30 +00:00
|
|
|
; bne CleanupAndExit ; Branch always
|
|
|
|
|
|
|
|
;------------------------------------------------------------------------------
|
|
|
|
; CleanupAndExit: Free any allocated resources, restore the stack and return
|
|
|
|
; to the caller.
|
|
|
|
|
|
|
|
.code
|
|
|
|
CleanupAndExit:
|
|
|
|
|
|
|
|
; Restore the stack so we may return to the caller from here
|
|
|
|
|
|
|
|
ldx Stack
|
|
|
|
txs
|
|
|
|
|
|
|
|
; Save the error return code
|
|
|
|
|
|
|
|
pha
|
|
|
|
|
|
|
|
; Check if we have to free the allocated block
|
|
|
|
|
|
|
|
lda Module
|
|
|
|
ldx Module+1
|
2005-04-01 08:23:56 +00:00
|
|
|
bne @L1
|
|
|
|
tay ; Test high byte
|
|
|
|
beq @L2
|
|
|
|
@L1: jsr _free ; Free the allocated block
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
; Restore the register bank
|
|
|
|
|
2005-04-01 08:23:56 +00:00
|
|
|
@L2: jsr RestoreRegBank
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
; Restore the error code and return to the caller
|
|
|
|
|
|
|
|
ldx #$00 ; Load the high byte
|
|
|
|
pla
|
|
|
|
rts
|
|
|
|
|
|
|
|
;------------------------------------------------------------------------------
|
|
|
|
; RelocSeg: Relocate the segment pointed to by a/x
|
|
|
|
|
|
|
|
.code
|
|
|
|
RelocSeg:
|
|
|
|
jsr decax1 ; Start value is segment-1
|
|
|
|
sta TPtr
|
|
|
|
stx TPtr+1
|
|
|
|
|
|
|
|
Loop: jsr ReadByte ; Read byte from relocation table
|
|
|
|
beq Done ; Bail out if end of table reached
|
|
|
|
|
|
|
|
cmp #255 ; Special offset?
|
|
|
|
bne @L1
|
|
|
|
|
|
|
|
; Increment offset by 254 and continue
|
|
|
|
|
|
|
|
lda TPtr
|
|
|
|
add #254
|
|
|
|
sta TPtr
|
|
|
|
bcc Loop
|
|
|
|
inc TPtr+1
|
|
|
|
jmp Loop
|
|
|
|
|
|
|
|
; Increment offset by A
|
|
|
|
|
|
|
|
@L1: add TPtr
|
|
|
|
sta TPtr
|
|
|
|
bcc @L2
|
|
|
|
inc TPtr+1
|
|
|
|
|
|
|
|
; Read the relocation byte, extract the segment id, fetch the corresponding
|
|
|
|
; relocation value and place it into ptr1
|
|
|
|
|
|
|
|
@L2: jsr ReadByte
|
|
|
|
and #O65_SEGID_MASK
|
|
|
|
jsr GetReloc
|
2002-06-23 14:02:09 +00:00
|
|
|
sta RelocVal
|
|
|
|
stx RelocVal+1
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
; Get the relocation byte again, this time extract the relocation type.
|
|
|
|
|
|
|
|
lda InputByte
|
|
|
|
and #O65_RTYPE_MASK
|
|
|
|
|
|
|
|
; Check for and handle the different relocation types.
|
|
|
|
|
|
|
|
cmp #O65_RTYPE_WORD
|
2002-04-23 19:51:45 +00:00
|
|
|
beq RelocWord
|
|
|
|
cmp #O65_RTYPE_HIGH
|
|
|
|
beq RelocHigh
|
|
|
|
cmp #O65_RTYPE_LOW
|
|
|
|
bne FormatError
|
|
|
|
|
|
|
|
; Relocate the low byte
|
|
|
|
|
|
|
|
RelocLow:
|
2002-04-21 14:29:30 +00:00
|
|
|
ldy #0
|
|
|
|
clc
|
2002-06-23 14:02:09 +00:00
|
|
|
lda RelocVal
|
2002-05-18 21:38:01 +00:00
|
|
|
bcc AddCommon
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-04-23 19:51:45 +00:00
|
|
|
; Relocate a high byte
|
|
|
|
|
|
|
|
RelocHigh:
|
2002-04-21 14:29:30 +00:00
|
|
|
jsr ReadByte ; Read low byte from relocation table
|
|
|
|
ldy #0
|
|
|
|
clc
|
2002-06-23 14:02:09 +00:00
|
|
|
adc RelocVal ; We just need the carry
|
2002-05-18 21:38:01 +00:00
|
|
|
AddHigh:
|
2002-06-23 14:02:09 +00:00
|
|
|
lda RelocVal+1
|
2002-05-18 21:38:01 +00:00
|
|
|
AddCommon:
|
|
|
|
adc (TPtr),y
|
2002-04-21 14:29:30 +00:00
|
|
|
sta (TPtr),y
|
|
|
|
jmp Loop ; Done, next entry
|
|
|
|
|
2002-04-23 19:51:45 +00:00
|
|
|
; Relocate a word
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-04-23 19:51:45 +00:00
|
|
|
RelocWord:
|
|
|
|
ldy #0
|
2002-04-21 14:29:30 +00:00
|
|
|
clc
|
2002-06-23 14:02:09 +00:00
|
|
|
lda RelocVal
|
2002-05-18 21:38:01 +00:00
|
|
|
adc (TPtr),y
|
2002-04-21 14:29:30 +00:00
|
|
|
sta (TPtr),y
|
2002-04-23 19:51:45 +00:00
|
|
|
iny
|
|
|
|
bne AddHigh ; Branch always (add high byte)
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
;------------------------------------------------------------------------------
|
|
|
|
; mod_load: Load and relocate an o65 module
|
|
|
|
|
|
|
|
.code
|
|
|
|
_mod_load:
|
|
|
|
|
|
|
|
; Save the register bank and clear the Module pointer
|
|
|
|
|
|
|
|
pha
|
|
|
|
ldy #6
|
|
|
|
@L1: lda regbank-1,y
|
|
|
|
sta RegBankSave-1,y
|
|
|
|
dey
|
|
|
|
bne @L1
|
|
|
|
sty Module
|
|
|
|
sty Module+1
|
|
|
|
pla
|
|
|
|
|
|
|
|
; Save the passed parameter
|
|
|
|
|
|
|
|
sta Ctrl
|
|
|
|
stx Ctrl+1
|
|
|
|
|
|
|
|
; Save the stack pointer so we can bail out even from subroutines
|
|
|
|
|
|
|
|
tsx
|
|
|
|
stx Stack
|
|
|
|
|
|
|
|
; Get the read function pointer from the control structure and place it into
|
|
|
|
; our call vector
|
|
|
|
|
2003-12-03 19:57:27 +00:00
|
|
|
ldy #MOD_CTRL::READ
|
2002-04-24 18:47:11 +00:00
|
|
|
lda (Ctrl),y
|
2002-04-21 14:29:30 +00:00
|
|
|
sta Read+1
|
2002-04-24 18:47:11 +00:00
|
|
|
iny
|
2002-05-18 21:38:01 +00:00
|
|
|
lda (Ctrl),y
|
2002-04-24 18:47:11 +00:00
|
|
|
sta Read+2
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-11-20 14:21:10 +00:00
|
|
|
; Read the o65 header: C->read (C->callerdata, &H, sizeof (H))
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-11-20 14:21:10 +00:00
|
|
|
jsr PushCallerData
|
2002-04-21 14:29:30 +00:00
|
|
|
lda #<Header
|
|
|
|
ldx #>Header
|
|
|
|
jsr pushax
|
2003-12-03 20:54:43 +00:00
|
|
|
lda #.sizeof(O65_HDR)
|
2002-11-20 14:21:10 +00:00
|
|
|
ldx #0 ; Always less than 256
|
2002-04-23 19:51:45 +00:00
|
|
|
jsr ReadAndCheckError ; Bails out in case of errors
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
; We read the o65 header successfully. Validate it.
|
|
|
|
|
2002-04-23 19:51:45 +00:00
|
|
|
ldy #ExpectedHdrSize-1
|
2002-05-18 21:38:01 +00:00
|
|
|
ValidateHeader:
|
|
|
|
lda Header,y
|
2002-04-21 14:29:30 +00:00
|
|
|
cmp ExpectedHdr,y
|
2002-05-18 21:38:01 +00:00
|
|
|
bne HeaderError
|
|
|
|
dey
|
|
|
|
bpl ValidateHeader
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-05-18 21:38:01 +00:00
|
|
|
; Header is ok as far as we can say now. Read all options, check for the
|
|
|
|
; OS option and ignore all others. The OS option contains a version number
|
|
|
|
; and the module id as additional data.
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-05-18 21:38:01 +00:00
|
|
|
iny ; Y = $00
|
|
|
|
sty TPtr+1 ; Flag for OS option read
|
|
|
|
Opt: jsr ReadByte ; Read the length byte
|
2002-04-21 14:29:30 +00:00
|
|
|
beq OptDone ; Jump if done
|
|
|
|
sta TPtr ; Use TPtr as a counter
|
2002-05-18 21:38:01 +00:00
|
|
|
|
|
|
|
; An option has a length of at least 2 bytes
|
|
|
|
|
|
|
|
cmp #2
|
|
|
|
bcc HeaderError ; Must be 2 bytes total at least
|
|
|
|
|
|
|
|
; Check for the OS option
|
|
|
|
|
|
|
|
dec TPtr
|
|
|
|
jsr ReadByte ; Get the option type
|
|
|
|
cmp #O65_OPT_OS ; OS option?
|
|
|
|
bne SkipOpt ; No: Skip
|
|
|
|
|
|
|
|
lda TPtr ; Get remaining length+1
|
|
|
|
cmp #5 ; CC65 has 6 bytes total
|
|
|
|
bne OSError
|
|
|
|
|
|
|
|
jsr ReadByte ; Get the operating system
|
2005-04-01 08:23:56 +00:00
|
|
|
cmp #O65_OS_CC65
|
2002-05-18 21:38:01 +00:00
|
|
|
bne OSError ; Wrong operating system
|
|
|
|
|
|
|
|
jsr ReadByte ; Get the version number, expect zero
|
|
|
|
bne OSError ; Wrong version
|
|
|
|
|
|
|
|
jsr ReadByte ; Get low byte of id
|
2003-12-03 19:57:27 +00:00
|
|
|
ldy #MOD_CTRL::MODULE_ID
|
2002-05-18 21:38:01 +00:00
|
|
|
sta (Ctrl),y
|
|
|
|
jsr ReadByte
|
2003-12-03 19:57:27 +00:00
|
|
|
ldy #MOD_CTRL::MODULE_ID+1
|
2002-05-18 21:38:01 +00:00
|
|
|
sta (Ctrl),y
|
|
|
|
|
|
|
|
inc TPtr+1 ; Remember that we got the OS
|
|
|
|
|
|
|
|
jmp Opt
|
|
|
|
|
|
|
|
; Skip one option
|
|
|
|
|
|
|
|
SkipOpt:
|
|
|
|
dec TPtr
|
2002-04-21 14:29:30 +00:00
|
|
|
beq Opt ; Next option
|
|
|
|
jsr ReadByte ; Skip one byte
|
2002-05-18 21:38:01 +00:00
|
|
|
jmp SkipOpt
|
|
|
|
|
|
|
|
; Operating system error
|
|
|
|
|
|
|
|
OSError:
|
|
|
|
lda #MLOAD_ERR_OS
|
|
|
|
jmp CleanupAndExit
|
|
|
|
|
|
|
|
; Options done, check that we got the OS option
|
|
|
|
|
2002-04-21 14:29:30 +00:00
|
|
|
OptDone:
|
2002-05-18 21:38:01 +00:00
|
|
|
lda TPtr+1
|
|
|
|
bne CalcSizes
|
|
|
|
|
|
|
|
; Entry point for header errors
|
|
|
|
|
|
|
|
HeaderError:
|
|
|
|
lda #MLOAD_ERR_HDR
|
|
|
|
jmp CleanupAndExit
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-04-23 19:51:45 +00:00
|
|
|
; Skipped all options. Calculate the size of text+data and of text+data+bss
|
|
|
|
; (the latter is the size of the memory block we need). We will store the
|
|
|
|
; total module size also into the control structure for evaluation by the
|
|
|
|
; caller
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-05-18 21:38:01 +00:00
|
|
|
CalcSizes:
|
2003-12-03 20:54:43 +00:00
|
|
|
lda Header + O65_HDR::TLEN
|
|
|
|
add Header + O65_HDR::DLEN
|
2002-04-21 14:29:30 +00:00
|
|
|
sta TPtr
|
2003-12-03 20:54:43 +00:00
|
|
|
lda Header + O65_HDR::TLEN + 1
|
|
|
|
adc Header + O65_HDR::DLEN + 1
|
2002-04-21 14:29:30 +00:00
|
|
|
sta TPtr+1
|
|
|
|
lda TPtr
|
2003-12-03 20:54:43 +00:00
|
|
|
add Header + O65_HDR::BLEN
|
2002-04-23 19:51:45 +00:00
|
|
|
pha ; Save low byte of total size
|
2003-12-03 19:57:27 +00:00
|
|
|
ldy #MOD_CTRL::MODULE_SIZE
|
2002-04-23 19:51:45 +00:00
|
|
|
sta (Ctrl),y
|
2002-04-21 14:29:30 +00:00
|
|
|
lda TPtr+1
|
2003-12-03 20:54:43 +00:00
|
|
|
adc Header + O65_HDR::BLEN + 1
|
2002-04-23 19:51:45 +00:00
|
|
|
iny
|
2002-04-21 14:29:30 +00:00
|
|
|
sta (Ctrl),y
|
|
|
|
tax
|
2002-04-23 19:51:45 +00:00
|
|
|
pla ; Restore low byte of total size
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-05-18 21:38:01 +00:00
|
|
|
; Total memory size is now in a/x. Allocate memory and remember the result,
|
|
|
|
; both, locally and in the control structure so it the caller can access
|
|
|
|
; the memory block. After that, check if we got the requested memory.
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
jsr _malloc
|
|
|
|
sta Module
|
|
|
|
stx Module+1
|
|
|
|
|
2003-12-03 19:57:27 +00:00
|
|
|
ldy #MOD_CTRL::MODULE
|
2002-04-21 14:29:30 +00:00
|
|
|
sta (Ctrl),y
|
|
|
|
txa
|
|
|
|
iny
|
|
|
|
sta (Ctrl),y
|
2002-05-18 21:38:01 +00:00
|
|
|
ora Module
|
|
|
|
bne GotMem
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-05-18 21:38:01 +00:00
|
|
|
; Could not allocate memory
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-05-18 21:38:01 +00:00
|
|
|
lda #MLOAD_ERR_MEM
|
|
|
|
jmp CleanupAndExit
|
|
|
|
|
|
|
|
; Control structure is complete now. Clear the bss segment.
|
|
|
|
; bzero (bss_addr, bss_size)
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-11-18 18:54:36 +00:00
|
|
|
GotMem: lda Module
|
2002-04-21 14:29:30 +00:00
|
|
|
add TPtr
|
2002-05-18 21:38:01 +00:00
|
|
|
pha
|
2002-04-21 14:29:30 +00:00
|
|
|
lda Module+1
|
2002-05-18 21:38:01 +00:00
|
|
|
adc TPtr+1 ; Module + tlen + dlen
|
|
|
|
tax
|
|
|
|
pla
|
|
|
|
jsr pushax
|
2003-12-03 20:54:43 +00:00
|
|
|
lda Header + O65_HDR::BLEN
|
|
|
|
ldx Header + O65_HDR::BLEN+1
|
2002-05-18 21:38:01 +00:00
|
|
|
jsr _bzero ; bzero (bss, bss_size);
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-05-18 21:38:01 +00:00
|
|
|
; Load code and data segment into memory. The sum of the sizes of
|
|
|
|
; code+data segment is still in TPtr.
|
2002-11-20 14:21:10 +00:00
|
|
|
; C->read (C->callerdata, C->module, H.tlen + H.dlen)
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-11-20 14:21:10 +00:00
|
|
|
jsr PushCallerData
|
2002-04-21 14:29:30 +00:00
|
|
|
lda Module
|
|
|
|
ldx Module+1
|
|
|
|
jsr pushax
|
|
|
|
lda TPtr
|
|
|
|
ldx TPtr+1
|
2002-04-23 19:51:45 +00:00
|
|
|
jsr ReadAndCheckError ; Bails out in case of errors
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
; We've got the code and data segments in memory. Next section contains
|
|
|
|
; undefined references which we don't support. So check if the count of
|
|
|
|
; undefined references is actually zero.
|
|
|
|
|
2002-04-23 19:51:45 +00:00
|
|
|
jsr ReadByte
|
2002-04-21 14:29:30 +00:00
|
|
|
bne Undef
|
|
|
|
jsr ReadByte
|
|
|
|
beq Reloc
|
2002-04-23 19:51:45 +00:00
|
|
|
Undef: jmp FormatError
|
2002-04-21 14:29:30 +00:00
|
|
|
|
2002-04-24 18:47:11 +00:00
|
|
|
; Number of undefined references was zero. Next come the relocation tables
|
|
|
|
; for code and data segment. Relocate the code segment
|
2002-04-21 14:29:30 +00:00
|
|
|
|
|
|
|
Reloc: lda Module
|
2002-04-23 19:51:45 +00:00
|
|
|
ldx Module + 1 ; Code segment address
|
2002-04-21 14:29:30 +00:00
|
|
|
jsr RelocSeg
|
|
|
|
|
|
|
|
; Relocate the data segment
|
|
|
|
|
2002-04-24 18:47:11 +00:00
|
|
|
lda Module
|
2003-12-03 20:54:43 +00:00
|
|
|
add Header + O65_HDR::TLEN
|
2002-04-24 18:47:11 +00:00
|
|
|
pha
|
|
|
|
lda Module + 1
|
2003-12-03 20:54:43 +00:00
|
|
|
adc Header + O65_HDR::TLEN + 1
|
2002-04-24 18:47:11 +00:00
|
|
|
tax
|
2002-05-18 21:38:01 +00:00
|
|
|
pla ; Data segment address in a/x
|
2002-04-21 14:29:30 +00:00
|
|
|
jsr RelocSeg
|
|
|
|
|
|
|
|
; We're done. Restore the register bank and return a success code
|
|
|
|
|
2002-04-23 19:51:45 +00:00
|
|
|
jsr RestoreRegBank ; X will be zero on return
|
2002-04-21 14:29:30 +00:00
|
|
|
lda #MLOAD_OK
|
|
|
|
rts
|
|
|
|
|