Centralized debug code, fixed stack print and FATAL_ERROR code in mem mgr.

This commit is contained in:
Martin Haye 2014-01-02 15:27:31 -08:00
parent 48777e7af8
commit 1315c387c4
4 changed files with 221 additions and 131 deletions

View File

@ -17,6 +17,7 @@
<target name="clean">
<delete failonerror="false" dir="${build.dir}"/>
<ant dir="${src.dir}/raycast" target="clean" useNativeBasedir="true" inheritAll="false"/>
<ant dir="${src.dir}/core" target="clean" useNativeBasedir="true" inheritAll="false"/>
</target>
<target name="build">
@ -24,6 +25,10 @@
<!-- Create build directory -->
<mkdir dir="${build.dir}"/>
<!-- Build sub-projects -->
<echo>Building core.</echo>
<ant dir="${src.dir}/core" target="build" useNativeBasedir="true" inheritAll="false"/>
<!-- Build sub-projects -->
<echo>Building raycast.</echo>
<ant dir="${src.dir}/raycast" target="build" useNativeBasedir="true" inheritAll="false"/>
@ -32,6 +37,7 @@
<delete failonerror="false" dir="${build.dir}/root"/>
<mkdir dir="${build.dir}/root"/>
<copy todir="${build.dir}/root">
<fileset dir="${src.dir}/core/build" includes="*.bin*"/>
<fileset dir="${src.dir}/raycast/build" includes="*.bin*"/>
</copy>
<mkdir dir="${build.dir}/root/"/>

View File

@ -216,10 +216,15 @@ FATAL_ERROR = $18
jmp main_dispatch
jmp aux_dispatch
DEBUG = 1
.include "../include/debug.i"
;------------------------------------------------------------------------------
; Variables
isAuxCommand:
.byte 0
scanStart:
.byte 0
targetPage:
.byte 0
nextLoaderVec:
@ -247,6 +252,9 @@ shared_dispatch:
bne :+
stx targetPage
rts
: cmp #FREE_MEMORY
bne :+
jmp main_free
: cmp #FATAL_ERROR
bne :+
jmp fatalError
@ -259,8 +267,12 @@ aux_dispatch:
bne :+
jmp aux_request
: cmp #QUEUE_LOAD
bne shared_dispatch
bne :+
jmp aux_queueLoad
: cmp #FREE_MEMORY
bne :+
jmp aux_free
: jmp shared_dispatch
;------------------------------------------------------------------------------
; Print fatal error message (custom or predefined) and print the
@ -272,46 +284,65 @@ fatalError:
jsr textinit
jsr setvid
jsr setkbd
jsr crout ; a couple newlines
jsr crout
ldy #0 ; start at first byte of message
ldy #0
: lda fatalMsg,y ; print out prefix message
beq :+
ora #$80
jsr cout
iny
bne :-
: ldy #0 ; start at first byte of message
: lda (pTmp),y
beq :+
ora #$80 ; ensure hi-bit ASCII for cout
jsr cout
iny
bne :-
.if DEBUG
; Print call stack
: jsr crout
tsx ; start at current stack pointer
: ldy #0
: lda stackMsg,y
beq :+
ora #$80
jsr cout
iny
bne :-
: tsx ; start at current stack pointer
@stackLoop:
lda 101,x ; JSR increments PC twice before pushing it
lda $101,x ; JSR increments PC twice before pushing it
sec
sbc #2
tay
lda 102,x
lda $102,x
sbc #0
sta @load+2
and #$C0 ; avoid accidentally grabbing data from the IO area
cmp #$C0
beq :-
beq @next
@load:
lda $1000,y ; is there a JSR there?
cmp #$20
bne :-
bne @next ; no, it's probably not an actual call
lda @load+2
jsr prbyte
tya
jsr prbyte
lda #$A0
jsr cout
@next:
inx ; work up to...
cpx #$FF ; ...top of stack
bcc @stackLoop
.endif
jsr crout
; Beep, and loop forever
jsr bell
loopForever:
jmp loopForever
fatalMsg:
.byte $8D,"FATAL ERROR: ", 0
stackMsg:
.byte $8D," Call stack: ", 0
;------------------------------------------------------------------------------
init:
@ -343,8 +374,18 @@ init:
; Lock pages 0 and 1 in aux mem
sta aux_pageTbl1
sta aux_pageTbl1+1
.if DEBUG
jmp test
.endif
rts
;------------------------------------------------------------------------------
.if DEBUG
test:
DEBUG_STR "Testing memory manager."
jmp reservedErr
.endif
;------------------------------------------------------------------------------
main_setup:
lda #<main_pageTbl1
@ -406,15 +447,20 @@ shared_request:
bne @gotPage ; if SET_MEM_TARGET was called, don't scan
; need to scan for a block that has enough pages
stx tmp ; save number of pages
ldy #1 ; begin scan at page 2 (1+1)
ldy scanStart ; start scanning at end of last block
dey
@blockLoop:
iny ; try next page
sty tmp+1 ; remember starting page of area
ldx #0 ; initialize count of free pages found
@pageLoop:
cpy #$C0 ; stop at end of mem
bcs outOfMemErr
lda (pPageTbl1),y ; is page active?
cpy scanStart ; are we back where we started?
beq outOfMemErr ; if so, we have failed.
cpy #$C0 ; reached end of mem?
bne :+ ; no, proceed
ldy #$FF ; yes, start over at beginning of memory
bne @blockLoop ; always taken
: lda (pPageTbl1),y ; is page active?
bmi @blockLoop ; yes active, skip it
iny
inx ; got one more inactive page
@ -435,6 +481,7 @@ shared_request:
iny
dex
bne :-
sty scanStart ; start next scan at page after the last one allocated
lda tmp+1 ; return starting page
rts
@ -457,6 +504,36 @@ aux_request:
jsr aux_setup
jmp shared_request
;------------------------------------------------------------------------------
main_free:
jsr main_setup
shared_free:
txa ; move page num
tay ; from X to Y
lda (pPageTbl1),y ; fetch flags of first page
and #$C0 ; active and primary?
bpl @err ; no, that's an error
lda (pPageTbl1),y ; clear the active flag
and #$7F
sta (pPageTbl1),y
iny
@loop:
lda (pPageTbl1),y
bpl @done
and #$7F
sta (pPageTbl1),y
iny
cpy #$C0
bne @loop
@done:
rts
@err:
jmp reservedErr
aux_free:
jsr aux_setup
jmp shared_free
;------------------------------------------------------------------------------
main_queueLoad:
jsr main_setup

View File

@ -0,0 +1,117 @@
; Debug macros
.macro DEBUG_STR str
.if DEBUG
php
pha
jsr _writeStr
.byte str,0
pla
plp
.endif
.endmacro
.macro DEBUG_BYTE byte
.if DEBUG
php
pha
lda byte
jsr prbyte
lda #$A0
jsr cout
pla
plp
.endif
.endmacro
.macro DEBUG_WORD word
.if DEBUG
php
pha
lda word+1
jsr prbyte
lda word
jsr prbyte
lda #$A0
jsr cout
pla
plp
.endif
.endmacro
.macro DEBUG_LN
.if DEBUG
php
pha
jsr crout
pla
plp
.endif
.endmacro
.macro DEBUG_RDKEY
.if DEBUG
php
pha
tya
pha
txa
pha
jsr rdkey
pla
tay
pla
tax
pla
plp
.endif
.endmacro
; Non-debug function to print a string. Does not preserve registers.
.macro WRITE_STR str
jsr _writeStr
.byte str,0
.endmacro
; Support to print a string following the JSR, in high or low bit ASCII,
; terminated by zero. If the string has a period "." it will be followed
; automatically by the next address and a CR.
_writeStr:
pla
clc
adc #1
sta @ld+1
pla
adc #0
sta @ld+2
@ld:
lda $2000
beq @done
ora #$80
jsr cout
cmp #$AE
bne :+
lda #$DB ; [
jsr cout
lda @ld+1
clc
adc #4
pha
lda @ld+2
adc #0
jsr prbyte
pla
jsr prbyte
lda #$DD ; ]
jsr cout
jsr crout
: inc @ld+1
bne @ld
inc @ld+2
bne @ld ; always taken
@done:
lda @ld+2
pha
lda @ld+1
pha
rts

View File

@ -12,135 +12,21 @@ DEBUG = 0 ; turn on verbose logging
; Shared constants, zero page, buffer locations, etc.
.include "render.i"
; Debug macros and support functions
.include "../include/debug.i"
; Variables
backBuf: .byte 0 ; (value 0 or 1)
frontBuf: .byte 0 ; (value 0 or 1)
mapBase: .word 0
nTextures: .byte 0
mapRayOrigin: .word 0
; texture addresses
MAX_TEXTURES = 20
texAddrLo: .res MAX_TEXTURES
texAddrHi: .res MAX_TEXTURES
; Debug macros
.macro DEBUG_STR str
.if DEBUG
php
pha
jsr _writeStr
.byte str,0
pla
plp
.endif
.endmacro
.macro DEBUG_BYTE byte
.if DEBUG
php
pha
lda byte
jsr prbyte
lda #$A0
jsr cout
pla
plp
.endif
.endmacro
.macro DEBUG_WORD word
.if DEBUG
php
pha
lda word+1
jsr prbyte
lda word
jsr prbyte
lda #$A0
jsr cout
pla
plp
.endif
.endmacro
.macro DEBUG_LN
.if DEBUG
php
pha
jsr crout
pla
plp
.endif
.endmacro
.macro DEBUG_RDKEY
.if DEBUG
php
pha
tya
pha
txa
pha
jsr rdkey
pla
tay
pla
tax
pla
plp
.endif
.endmacro
; Non-debug function to print a string. Does not preserve registers.
.macro WRITE_STR str
jsr _writeStr
.byte str,0
.endmacro
; Support to print a string following the JSR, in high or low bit ASCII,
; terminated by zero. If the string has a period "." it will be followed
; automatically by the next address and a CR.
_writeStr:
pla
clc
adc #1
sta @ld+1
pla
adc #0
sta @ld+2
@ld:
lda $2000
beq @done
ora #$80
jsr cout
cmp #$AE
bne :+
lda #$DB ; [
jsr cout
lda @ld+1
clc
adc #4
pha
lda @ld+2
adc #0
jsr prbyte
pla
jsr prbyte
lda #$DD ; ]
jsr cout
jsr crout
: inc @ld+1
bne @ld
inc @ld+2
bne @ld ; always taken
@done:
lda @ld+2
pha
lda @ld+1
pha
rts
;-------------------------------------------------------------------------------
; Multiply two bytes, quickly but somewhat inaccurately, using logarithms.
; Utilizes tbl_log2_b_b and tbl_pow2_b_b, which translate to and from 3+5 bit
@ -615,6 +501,10 @@ castRay:
jmp rdkey
.endif
castAllRays:
DEBUG_STR "castAllRays not implemented yet."
brk
; Advance pLine to the next line on the hi-res screen
nextLine:
lda pLine+1 ; Hi byte of line