Dr Jefyll wrote:
Hmmm. Earlier you said the RTI instruction jumps to the wrong place. Just for fun, what happens if you use RTI to jump somewhere specific -- $1234, for example -- by doing this (below)? Maybe the success or failure of this will reveal a clue, or raise a new question.
Code:
lda #12
pha
lda #34
pha
php
rti
-- Jeff
Good idea! To be fair, I think I sort of mis-spoke earlier. That was before I had really inspected the stack values very much. It does appear that the RTI itself isn't the problem, it's that the stack has the wrong values in it.
I just wrote this real quick:
Code:
.macro rti_jmp addr
lda #>addr
pha
lda #<addr
pha
php
rti
.endmacro
And used that in my main loop, instead of `bra`. It works just fine.
So there's one thing ruled out at least.
EDIT: Do interrupts affect the memory timing? i.e. are the write cycles in an interrupt the same as any other write cycle? I'm just thinking, it really seems like the wrong values are being written to RAM by the CPU somehow. Not that I think that the CPU itself is getting the wrong answer, but maybe there's some weird timing/electrical issue that only occurs when the CPU is writing to the stack during an interrupt. *shrugs*
EDIT 2: I just wrote this as a test:
Code:
ldx #0
: txa
sta $0100,x
lda $0100,x
jsr acia_put_hex_byte
lda #10
jsr acia_putc
inx
bne :-
However, surprisingly, this did not work; I had to use the Y register rather than the index to get it to behave as expected:
Code:
ldy #0
: tya
sta $0100,y
lda $0100,y
jsr acia_put_hex_byte
lda #10
jsr acia_putc
iny
bne :-
Am I just being dumb or is this some kind of bug? I will note that the `acia_put_hex_byte` procedure uses the X register to implement a lookup table for converting hex digits to ASCII characters, but it pushes and pops it to and from the stack. (Also, `acia_putc` uses X for a short delay loop.) The fact that this loop fails when using X only when X is pushed and pops tells me that the problem is that any(?) data written to the stack gets corrupted occasionally. Although, it's strange that this issue would _always_ happen on interrupts...
EDIT 3: Everything I said in EDIT 2 was fake news; I thought I was pushing and popping X but I actually wasn't! Oh well. Fixed that anyway. And, what I was testing for gave the correct results: writing and reading back every stack location works fine.