Posted By
Harry Potter on 2022-06-28 10:10:35
| Bug in Plus4Cart03/cc65
Hi! I already have two Plus4 cartridge configurations for the Plus4: one for a 16k cartridge and one for a 32k cartridge to treat the upper 16k as hidden data via a port of my Hidden64 library. Now, I am working on a 32k cartridge where the whole 32k is accessible and kernal access is delegated. It has a bug: the first time it is run, it works. The demo is doing everything it's supposed to do. However, when I exit the program, the first piece of text is displayed, but the first key press doesn't register. Following is the crt0.s code:
-------------------------- startup: ;Thi sei ldx #$ff txs lda $fb pha ;ldx #$02 ; cartrige 1 lo, kernal sta $FDD2 jsr $FF84 ; Initialize I/O devices jsr $FF87 ; RAM Test pla sta $FB jsr $FF8A ; Restore vectors to initial values jsr $FF81 ; Initialize screen editor cli
; Switch to second charset
lda #14 jsr $FFD2
; Clear the BSS data
jsr zerobss
;lda #<(__RAM_START__ + __RAM_SIZE__) lda #$00 sta sp ;lda #>(__RAM_START__ + __RAM_SIZE__) lda #$80 sta sp+1 ; Set argument stack ptr
lda #$03 sta $FF0C lda #$FF sta $FF0D
sta $FDD2 ; Enable the ROM ; If we have IRQ functions, chain our stub into the IRQ vector
; lda #<__INTERRUPTOR_COUNT__ ; beq NoIRQ1 ; lda IRQVec ; ldx IRQVec+1 ; sta IRQInd+1 ; stx IRQInd+2 ; lda #; ldx #>IRQStub ; sei ; sta IRQVec ; stx IRQVec+1 ; cli
; Call module constructors
NoIRQ1: lda #<__DATA_RUN__ ldx #>__DATA_RUN__ jsr pushax lda #<__DATA_LOAD__ ldx #>__DATA_LOAD__ jsr pushax lda #<__DATA_SIZE__ ldx #>__DATA_SIZE__ jsr _memcpy
jsr initlib
; Push arguments and call main ;ra: ; inc $D021 ; jmp ra
jsr _main
; Back from main (This is also the _exit entry). Run module destructors
_exit: jsr donelib
; Reset the IRQ vector if we chained it.
; ;pha ; Save the return code on stack ; lda #<__INTERRUPTOR_COUNT__ ; beq NoIRQ2 ; lda IRQInd+1 ; ldx IRQInd+2 ; sei ; sta IRQVec ; stx IRQVec+1 ; cli
NoIRQ2: jmp startup ------------------------------
The write.s code:
----------------------------- ;-------------------------------------------------------------------------- ; initstdout: Open the stdout and stderr file descriptors for the screen.
.segment "ONCE"
.proc initstdout
lda #STDOUT_FILENO + LFN_OFFS jsr @L1 lda #STDERR_FILENO + LFN_OFFS @L1: ldx #CBMDEV_SCREEN ldy #$FF sta $FDD2
jsr SETLFS jsr OPEN ; Will always succeed sta $FDDA rts
.endproc
;-------------------------------------------------------------------------- ; _write
;.code .segment "LOWCODE"
.proc _write
jsr rwcommon ; Pop params, check handle bcs invalidfd ; Invalid handle
; Check if the LFN is valid and the file is open for writing
adc #LFN_OFFS ; Carry is already clear tax lda fdtab-LFN_OFFS,x; Get flags for this handle and #LFN_WRITE ; File open for writing? beq invalidfd
; Valid lfn. Make it the output file
sta $FDD2 jsr CKOUT bcc @L2 @error: jmp __mappederrno ; Store into __oserror, map to errno, return -1
; Output the next character from the buffer
@L0: ldy #0 sta $FDDA lda (ptr1),y inc ptr1 bne @L1 inc ptr1+1 ; A = *buf++;
@L1: sta $FDD2 jsr BSOUT
; Check the status
pha jsr READST lsr a ; Bit zero is write timeout bne devnotpresent2 pla bcs @L3
; Count characters written
inc ptr3 bne @L2 inc ptr3+1
; Decrement count
@L2: dec ptr2 bne @L0 dec ptr2+1 bne @L0
; Wrote all chars or disk full. Close the output channel
@L3: jsr CLRCH
; Clear _oserror and return the number of chars written
lda #0 sta __oserror lda ptr3 ldx ptr3+1 sta $FDDA rts
; Error entry: Device not present
devnotpresent2: pla devnotpresent: lda #ENODEV .byte $2C ; Skip next opcode via BIT
; Error entry: The given file descriptor is not valid or not open
invalidfd: lda #EBADF jmp __directerrno ; Sets _errno, clears _oserror, returns -1
.endproc ---------------------- and the key press code: ---------------------- .proc _getkey sta $FDD2 @lp: jsr $ffe4 ;Call the getin kernal routine to ;get the key-press. cmp #0 ;Continue until key pressed. beq @lp sta $FDDA ldx #0 ;Clear MSB because I keep seeing ;this done in cc65's source code. rts ;Return. .endproc -------------------------
|