1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-07-14 05:28:55 +00:00

Change PLP handling

The "smart" PLP handler tries to recover the flags from an earlier
PHP.  The non-smart version just marks all the flags as indeterminate.
This doesn't work well on the 65816 in native mode, because having
the M/X flags in an indeterminate state is rarely what you want.

Code rarely uses PLP to reset the flags to a specific state, preferring
explicit SEP/REP.  The analyzer is more likely to get the correct
answer by simply leaving the flags in their prior state.

A test case has been added to 20052-branches-and-banks, which now has
"smart PLP" disabled.
This commit is contained in:
Andy McFadden 2020-07-20 11:54:00 -07:00
parent cb6ceafd73
commit 288a857e47
10 changed files with 116 additions and 31 deletions

View File

@ -804,12 +804,12 @@ namespace SourceGen {
// it around like one. The E-flag is always carried over from the previous // it around like one. The E-flag is always carried over from the previous
// instruction. // instruction.
int backOffsetLimit = plpOffset - 128; // arbitrary 128-byte reach
if (backOffsetLimit < 0) {
backOffsetLimit = 0;
}
StatusFlags flags = StatusFlags.AllIndeterminate; StatusFlags flags = StatusFlags.AllIndeterminate;
if (mAnalysisParameters.SmartPlpHandling) { if (mAnalysisParameters.SmartPlpHandling) {
int backOffsetLimit = plpOffset - 128; // arbitrary 128-byte reach
if (backOffsetLimit < 0) {
backOffsetLimit = 0;
}
for (int offset = plpOffset - 1; offset >= backOffsetLimit; offset--) { for (int offset = plpOffset - 1; offset >= backOffsetLimit; offset--) {
Anattrib attr = mAnattribs[offset]; Anattrib attr = mAnattribs[offset];
if (!attr.IsInstructionStart || !attr.IsVisited) { if (!attr.IsInstructionStart || !attr.IsVisited) {
@ -824,6 +824,15 @@ namespace SourceGen {
} }
} }
if (flags == StatusFlags.AllIndeterminate &&
(mCpuDef.Type == CpuDef.CpuType.Cpu65816 ||
mCpuDef.Type == CpuDef.CpuType.Cpu65802)) {
// Having indeterminate M/X flags is really bad. If "smart" handling failed or
// is disabled, copy flags from previous instruction.
flags.M = mAnattribs[plpOffset].StatusFlags.M;
flags.X = mAnattribs[plpOffset].StatusFlags.X;
}
// Transfer the 'E' flag. // Transfer the 'E' flag.
flags.E = mAnattribs[plpOffset].StatusFlags.E; flags.E = mAnattribs[plpOffset].StatusFlags.E;
return flags; return flags;

View File

@ -273,7 +273,8 @@ improve automatic operand formatting.</p>
<p>If "smart PLP handling" is checked, the analyzer will try to use <p>If "smart PLP handling" is checked, the analyzer will try to use
the processor status flags from a nearby <code>PHP</code> when a the processor status flags from a nearby <code>PHP</code> when a
<code>PLP</code> is encountered. If not enabled, all flags are set to <code>PLP</code> is encountered. If not enabled, all flags are set to
"indeterminate" following a <code>PLP</code>.</p> "indeterminate" following a <code>PLP</code>, except for the M/X
flags on the 65816, which are left unmodified.</p>
<p>If "smart PLB handling" is checked, the analyzer will watch for <p>If "smart PLB handling" is checked, the analyzer will watch for
code patterns like <code>PLB</code> preceded by <code>PHK</code>, code patterns like <code>PLB</code> preceded by <code>PHK</code>,
and generate appropriate Data Bank Register changes. If not enabled, and generate appropriate Data Bank Register changes. If not enabled,

View File

@ -1,8 +1,8 @@
### 6502bench SourceGen dis65 v1.0 ### ### 6502bench SourceGen dis65 v1.0 ###
{ {
"_ContentVersion":4, "_ContentVersion":4,
"FileDataLength":224, "FileDataLength":234,
"FileDataCrc32":2055368095, "FileDataCrc32":1224113674,
"ProjectProps":{ "ProjectProps":{
"CpuName":"65816", "CpuName":"65816",
"IncludeUndocumentedInstr":false, "IncludeUndocumentedInstr":false,
@ -15,7 +15,7 @@
"MinCharsForString":4, "MinCharsForString":4,
"SeekNearbyTargets":true, "SeekNearbyTargets":true,
"UseRelocData":false, "UseRelocData":false,
"SmartPlpHandling":true, "SmartPlpHandling":false,
"SmartPlbHandling":false}, "SmartPlbHandling":false},
"PlatformSymbolFileIdentifiers":["PROJ:20050-branches-and-banks.sym65"], "PlatformSymbolFileIdentifiers":["PROJ:20050-branches-and-banks.sym65"],

View File

@ -109,14 +109,28 @@ fwdchk nop
L543280 jsr skip+$540000 L543280 jsr skip+$540000
nop nop
phk rep #$30
plb .al
lda $544280 .xl
jsl $544280 php
lda #$0000
sep #$30
.as
.xs
lda #$00
plp
lda #$ea
nop nop
lda $4280 sep #$30
jsr $544280 php
jsr ($544280,x) lda #$00
rep #$30
.al
.xl
lda #$0000
plp
lda #$eaea
rep #$30
nop nop
rtl rtl

View File

@ -103,14 +103,25 @@ fwdchk nop
L543280 jsr skip L543280 jsr skip
nop nop
phk rep #$30
plb mx %00
ldal $544280 php
jsl $544280 lda #$0000
sep #$30
mx %11
lda #$00
plp
lda #$ea
nop nop
lda $4280 sep #$30
jsr $4280 php
jsr ($4280,x) lda #$00
rep #$30
mx %00
lda #$0000
plp
lda #$eaea
rep #$30
nop nop
rtl rtl

View File

@ -7,5 +7,6 @@
!hex 3254af163254af7d3254af163254af7d3254ad1732ad1532ad7e32ad7c32ea20 !hex 3254af163254af7d3254af163254af7d3254ad1732ad1532ad7e32ad7c32ea20
!hex 1632201732201832207d32207e32207f32eaa200fc1932fc7a32206e32207132 !hex 1632201732201832207d32207e32207f32eaa200fc1932fc7a32206e32207132
!hex 206832206b3220743220773280187c19327c7a326c08106c0810dc0810dc0810 !hex 206832206b3220743220773280187c19327c7a326c08106c0810dc0810dc0810
!hex 7d3254eaea60200e20ea4babaf80425422804254eaad8042208042fc8042ea6b !hex 7d3254eaea60200e20eac23008a90000e230a90028a9eaeae23008a900c230a9
!hex 000028a9eaeac230ea6b
} ;!pseudopc } ;!pseudopc

View File

@ -111,14 +111,28 @@ fwdchk: nop
L543280: jsr skip L543280: jsr skip
nop nop
phk rep #$30
plb .a16
lda $544280 .i16
jsl $544280 php
lda #$0000
sep #$30
.a8
.i8
lda #$00
plp
lda #$ea
nop nop
lda $4280 sep #$30
jsr $4280 php
jsr ($4280,x) lda #$00
rep #$30
.a16
.i16
lda #$0000
plp
lda #$eaea
rep #$30
nop nop
rtl rtl

View File

@ -5,7 +5,7 @@ MEMORY {
# MEM001: file=%O, start=$440000, size=28; # MEM001: file=%O, start=$440000, size=28;
# MEM002: file=%O, start=$44ffc0, size=15; # MEM002: file=%O, start=$44ffc0, size=15;
# MEM003: file=%O, start=$2000, size=32; # MEM003: file=%O, start=$2000, size=32;
# MEM004: file=%O, start=$543210, size=138; # MEM004: file=%O, start=$543210, size=148;
} }
SEGMENTS { SEGMENTS {
CODE: load=MAIN, type=rw; CODE: load=MAIN, type=rw;

View File

@ -130,4 +130,39 @@ fwdchk:
nxt54b: nxt54b:
jsr skip ;EDIT: set to "skip" label jsr skip ;EDIT: set to "skip" label
nop
; Test the non-"smart" PLP affects on the M/X flags.
; EDIT: disable "smart PLP" for this project
rep #$30
.a16
.i16
php
lda #$0000
sep #$30
.a8
.i8
lda #$00
plp ;smart->long, unsmart->short
lda #$ea
nop
sep #$30
.a8
.i8
php
lda #$00
rep #$30
.a16
.i16
lda #$0000
plp ;smart->short, unsmart->long
lda #$eaea
rep #$30
.a16
.i16
nop
rtl rtl