The setup decodes RAM from $0000-bfff, an IO page at $c000-c0ff and ROM from $c100-ffff. I use Michael's clock-qualified read and write along with the /io flag to access ROM and RAM as shown in the diagram. The VIA is selected with the same /io flag and has registers mapped to A0-3 (so appear 16x in the $c1xx page). I have the 65c02 r/w connected directly to VIA's corresponding pin - afaik it handles the clock-qualified access?
I use the second half of the '139 like Michael shows so that A4-5 (not 6-7) generate four /iox signals. I use /io1 for lcd, /io2 for kbd shift register and /io3 for sd shift register (not wired yet). I also have a speaker set up on a portb pin which I can drive with a timer. /io0 is not connected so when I r/w to $c000-c00f I can manipulate VIA registers with no device active. For example with the LCD I set up portb control pins and porta data pins without enabling the device, and then BIT $c01x to enable the LCD while presenting the correct data and control values.
This all seems to work fine for several thousand instructions including initializing the LCD, clearing the screen and writing some text, but then at one point it returns from a JSR (which is called multiple times earlier) and unstacks $f11d after originally storing $f01d, so returns exactly a page beyond where intended. I have a simple monitor trace (below) where I can't see any other access to those stack locations. I've inspected memory at several intervening points and it seems like the stored value $f0 is correct until some time just before it reads the wrong value. My best guess is some kind of timing issue but can't understand why it happens in this repeatable way after so much other code runs correctly, nor any theory for how it gets that particular value wrong.
I've run the same binary image in a simulator without seeing the problem, so it doesn't appear to be a software issue. I've run various memory tests for data bus, addr bus including extensive use of the stack and recursive JSR and that all seems to be fine as well (code here viewtopic.php?f=4&t=144&p=109620#p109620 ).
Please lmk if you have any ideas on what else I could try.
Code: Select all
=== trace begin ===
* f207 r 60 RTS
f208 r a5
01fd r f1 17 f0
f017 r f1
* f018 r 20 08
01ff r f0 *******************
01ff W f0 *******************
01fe W 1a *******************
f01a r f2 JSR $f208
* f208 r a5 03 LDA $0003
0003 r 00
...
* f20a r 10 fc BPL $f208
f20c r 29
* f208 r a5 03 LDA $0003
0003 r 00
* f20a r 10
f20a r 10
01fd W f2 <========== KB IRQ
01fc W 0a
01fb W 23
fffe r 11 f2
* f211 r 48 PHA
f212 r ad
01fa W 00
* f212 r ad 03 c0 LDA $c003
c003 r ff
* f215 r 48 PHA
f216 r 9c
01f9 W ff
* f216 r 9c 03 c0 STZ $c003
c003 W 00
* f219 r ad 21 c0 LDA $c021
c021 r 35
* f21c r 85 04 STA $0004
0004 W 35
* f21e r 09 80 ORA #$80
* f220 r 85 03 STA $0003
0003 W b5
* f222 r 68 PLA
f223 r 8d
01f8 r fc ff
* f223 r 8d 03 c0 STA $c003
c003 W ff
* f226 r 68 PLA
f227 r 40
01f9 r ff 00
* f227 r 40 RTI
f228 r a9
01fa r 00 23 0a f2
* f20a r 10 fc BPL $f208
f20c r 29
* f208 r a5 03 LDA $0003
0003 r b5
* f20a r 10 fc BPL $f208
* f20c r 29 7f AND #$7f
* f20e r 64 03 STZ $0003
0003 W 00
* f210 r 60 RTS
f211 r 48
01fd r f2 1a f0
f01a r f2
* f01b r 20 70 <= start of JSR, should return to $f01e
01ff r f0 *******************
01ff W f0 ******************* <= write f0 TOS
01fe W 1d ******************* <= write 1d = %0001_1001 NOS
f01d r f1 JSR $f170
* f170 r 38 SEC
f171 r e9
* f171 r e9 20
* f173 r 85 00 STA $0000
0000 W 15
* f175 r a0 c0 LDY #$c0
* f177 r 18 CLC
f178 r da
* f178 r da PHX
f179 r a2
01fd W ff
* f179 r a2 00 LDX #$00
* f17b r 20 c7
01fc r 0a
01fc W f1
01fb W 7d
f17d r f1 JSR $f1c7
* f1c7 r 9c 03 c0 STZ $c003
c003 W 00
* f1ca r a9 05 LDA #$05
* f1cc r 8d 00 c0 STA $c000
c000 W 05
* f1cf r ad 11 c0 LDA $c011
c011 r 2b
* f1d2 r 29 03 AND #$03
* f1d4 r 49 03 EOR #$03
* f1d6 r d0 f7 BNE $f1cf
* f1d8 r a9 ff LDA #$ff
* f1da r 8d 03 c0 STA $c003
c003 W ff
* f1dd r 60 RTS
f1de r 9c
01fa r 00 7d f1
f17d r f1
* f17e r a9 02 LDA #$02
* f180 r 8d 00 c0 STA $c000
c000 W 02
* f183 r b5 00 LDA $0000,X
f184 r 00
0000 r 15
* f185 r 8d 01 c0 STA $c001
c001 W 15
* f188 r 2c 11 c0 BIT $c011
c011 r 15
* f18b r 90 04 BCC $f191
f18d r 18
* f191 r fa PLX
f192 r 20
01fc r f1 ff
* f192 r 20 c7
01fd r ff
01fd W f1
01fc W 94
f194 r f1 JSR $f1c7
* f1c7 r 9c 03 c0 STZ $c003
c003 W 00
* f1ca r a9 05 LDA #$05
* f1cc r 8d 00 c0 STA $c000
c000 W 05
* f1cf r ad 11 c0 LDA $c011
c011 r a3
* f1d2 r 29 03 AND #$03
* f1d4 r 49 03 EOR #$03
* f1d6 r d0 f7 BNE $f1cf
* f1d8 r a9 ff LDA #$ff
* f1da r 8d 03 c0 STA $c003
c003 W ff
* f1dd r 60 RTS
f1de r 9c
01fb r 7d 94 f1
f194 r f1
* f195 r a9 03 LDA #$03
* f197 r 8d 00 c0 STA $c000
c000 W 03
* f19a r 8c 01 c0 STY $c001
c001 W c0
* f19d r 2c 11 c0 BIT $c011
c011 r c0
* f1a0 r 60 RTS
f1a1 r a0
01fd r f1 1d f1 ******************* TOS is now 1d f1 instead of 1d f0
f11d r f1
* f11e r a0 84 LDY #$84 <= returns to $f11e instead of $f01e
* f120 r 20 92
01ff r f1 *******************
01ff W f1 *******************
01fe W 22
f122 r f1 JSR $f192
* f192 r 20 c7
01fd r f1
01fd W f1
01fc W 94
f194 r f1 JSR $f1c7
* f1c7 r 9c 03 c0 STZ $c003
c003 W 00
* f1ca r a9 05 LDA #$05
* f1cc r 8d 00 c0 STA $c000
c000 W 05
* f1cf r ad 11
=== trace end ===