Got basics of new fixup mechanism working.

This commit is contained in:
Martin Haye 2016-07-03 07:33:12 -07:00
parent bb0a9339fe
commit ea5800155f
6 changed files with 72 additions and 16 deletions

View File

@ -1044,7 +1044,8 @@ class PackPartitions
def newFixup = []
dp = 0
while (fixup[sp] != 0) {
assert (fixup[sp++] & 0xFF) == 0x81 // We can only handle WORD sized INTERN fixups
def fixupType = fixup[sp++] & 0xFF
assert fixupType == 0x81 || fixupType == 0x91 // We can only handle WORD sized INTERN or EXTERN fixups
def addr = fixup[sp++] & 0xFF
addr |= (fixup[sp++] & 0xFF) << 8
@ -1063,24 +1064,31 @@ class PackPartitions
def target = (codeBuf[addr] & 0xFF) | ((codeBuf[addr+1] & 0xFF) << 8)
//println String.format("...target=0x%04x", target)
if (invDefs.containsKey(target)) {
if (fixupType == 0x91) { // external fixup
// don't modify target addr
}
else if (invDefs.containsKey(target)) {
target = invDefs[target]
//println String.format("...translated to def offset 0x%04x", target)
assert target >= 5 && target < newAsmCode.length
}
else {
target -= 0x1000
target -= asmCodeStart
target += stubsSize // account for the stubs we prepended to the asm code
//println String.format("...adjusted to target offset 0x%04x", target)
assert target >= 5 && target < newAsmCode.length
}
assert target >= 5 && target < newAsmCode.length
// Put the adjusted target back in the code
codeBuf[addr] = (byte)(target & 0xFF)
codeBuf[addr+1] = (byte)((target >> 8) & 0xFF)
// And record the fixup
newFixup.add((byte)((addr>>8) & 0xFF) | (inByteCode ? 0x80 : 0))
assert addr >= 0 && addr <= 0x3FFF : "code module too big"
newFixup.add((byte)((addr>>8) & 0x3F) |
(inByteCode ? 0x40 : 0) |
((fixupType == 0x91) ? 0x80 : 0))
newFixup.add((byte)(addr & 0xFF))
assert fixup[sp++] == 0 // not sure what the zero byte is
}

View File

@ -384,12 +384,36 @@ init: !zone
lda #QUEUE_LOAD
jsr main_dispatch
stx .gomod+1
txa
pha ; save addr for scanning later
sty .gomod+2
tya
pha
lda #LOCK_MEMORY ; lock it in forever
jsr main_dispatch
ldx #1 ; keep open for efficiency's sake
lda #FINISH_LOAD
jsr main_dispatch
; find the end of the stubs in the first module
pla ; hi byte
sta pTmp+1
pla ; lo byte
sta pTmp
ldy #0
- lda (pTmp),y
cmp #$20 ; look for first non-JSR
bne +
lda pTmp
clc
adc #5 ; not found, advance by 5 bytes (size of one stub)
sta pTmp
bcc -
inc pTmp+1
bne -
+ lda pTmp ; store the result
sta glibBase
lda pTmp+1
sta glibBase+1
ldx #$10 ; initial eval stack index
.gomod: jmp $1111 ; jump to module for further bootstrapping
@ -2769,49 +2793,58 @@ doAllFixups: !zone
; Process the fixups
.proc jsr .fetchFixup ; get key byte
tax ; save it aside, and also check the hi bit
bmi .fxAux ; yes, it's aux mem fixup
.fxMain jsr .fetchFixup ; get the lo byte of the offset
pha ; save it aside
ldx #0 ; normal mode
asl ; get hi bit
bcc + ; 0=normal, 1=glib
ldx #4 ; glib mode - use glib base addr instead of main addr
+ asl ; check second-to-hi bit, which indicates main=0 or aux=1
bcs .fxAux ; yes, it's aux mem fixup
.fxMain jsr .fetchFixup ; get the lo byte of the offset (and set y to 0)
clc
adc .mainBase
sta pDst
txa
pla
and #$3F ; mask off the flags
adc .mainBase+1
sta pDst+1
!if DEBUG >= 2 { jsr .debug2 }
clc
jsr .adMain ; recalc and store lo byte
iny
inx
jsr .adMain ; recalc and store hi byte
bne .proc ; always taken
.adMain lda (pDst),y ; get num to add to offset
adc .mainBase,y ; add the offset
adc .mainBase,x ; add the offset
sta (pDst),y ; *STORE* back the result
rts
.fxAux cmp #$FF ; end of fixups?
.fxAux cmp #$FC ; end of fixups? ($FF shifted up two bits)
beq .stubs ; if so, go do the stubs
jsr .fetchFixup ; get lo byte of offset
jsr .fetchFixup ; get lo byte of offset (and set y to 0)
clc
adc .auxBase
sta pDst
txa
and #$7F ; mask off the hi bit flag
pla
and #$3F ; mask off the flags
adc .auxBase+1
sta pDst+1
!if DEBUG >= 2 { jsr .debug3 }
sta setAuxWr
jsr .adAux ; recalc and store lo byte
iny
inx
jsr .adAux ; recalc and store hi byte
sta clrAuxWr
bne .proc ; always taken
.adAux sta setAuxRd
lda (pDst),y ; get num to add to offset
sta clrAuxRd
adc .mainBase,y ; add the offset
adc .mainBase,x ; add the offset
sta (pDst),y ; *STORE* back the result
rts
.stubs ; fix up the stubs
pla ; discard saved value
lda .mainBase
sta pDst
lda .mainBase+1
@ -2885,6 +2918,7 @@ doAllFixups: !zone
}
.mainBase !word 0
.auxBase !word 0
glibBase !word $1111
;------------------------------------------------------------------------------
; Segment tables

View File

@ -513,6 +513,7 @@ def _combat_zoneEncounter(s_encZone)
answer = startCombat(s_encZone)
if answer == 99
global=>p_enemyGroups = NULL // results in no living enemies, or "win"
nEnemiesFighting = 0
elsif !answer
isFleeing = TRUE
fin

View File

@ -8,6 +8,14 @@
// governing permissions and limitations under the License.
///////////////////////////////////////////////////////////////////////////////////////////////////
import gamelib
word glib
struc t_gamelib
word testFunc1
word testFunc2
end
end
///////////////////////////////////////////////////////////////////////////////////////////////////
// Handy constants.
const FALSE = 0
@ -68,6 +76,7 @@ const HEAP_SIZE = $800
///////////////////////////////////////////////////////////////////////////////////////////////////
// Shared library routines
const gameLibVecs = $1F00
const setScriptInfo = gameLibVecs + 3*0

View File

@ -38,6 +38,8 @@ include "combat.plh"
include "party.plh"
include "diskops.plh"
// NOTE: GLIB goes here!
///////////////////////////////////////////////////////////////////////////////////////////////////
// Data structures
include "playtype.pla"
@ -1474,6 +1476,7 @@ def setMap(is3D, num, x, y, dir)
needRender = TRUE
else
flipToPage1()
mmgr(FINISH_LOAD, WITH_CLOSE)
showMapName("Traveling...")
setMapWindow(); clearWindow()
setWindow2(); clearWindow()
@ -1876,6 +1879,7 @@ def loadEngine(moduleNum)
saveMapPos()
flipToPage1()
diskActivity($FF)
mmgr(FINISH_LOAD, WITH_CLOSE)
mmgr(RESET_MEMORY, 0)
renderLoaded = FALSE
mapIs3D = FALSE

View File

@ -311,7 +311,7 @@ LOAD_SCRIPTS_NO_CALC:
RTS
.got CMP SCRIPTS_ID
BNE .diff
+finishLoad 0 ; all done
+finishLoad 1 ; all done
RTS
.diff STA SCRIPTS_ID
PHA
@ -330,7 +330,7 @@ LOAD_SCRIPTS_NO_CALC:
}
STX SCRIPTS_LOC
STY SCRIPTS_LOC+1
+finishLoad 0 ; all done
+finishLoad 1 ; all done
!if DEBUG { +prStr : !text "Calling init script.",0 }
LDX PLASMA_X
JSR .callit ; perform script init