6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Wed May 08, 2024 8:09 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Jun 17, 2022 11:01 pm 
Offline

Joined: Sun Oct 03, 2021 2:17 am
Posts: 114
I notice that a lot of the CPU-derailing crashes and freezes I write in 65816 assembly have to do with the m and x flags. Sometimes the assembler thinks these are set differently from how they actually turn out to be at runtime, so it generates the wrong size instructions and the CPU gets de-synched from the code, loading operand bytes and trying to interpret them as opcode/admode bytes. This usually causes showstopping problems, not little annoying bugs. If you suspect a crash/freeze might be because of getting these mx flags and register sizes out of sync, these macros will make ca65 print out what it thinks the register sizes currently are in a given program location. Then you can compare them to what the surrounding assembly source code wants the register sizes to be.

Code:
.macro p_isize
.if .isize = 8
    .out ".isize 8"
.else
    .out ".isize 16"
.endif
.endmacro

.macro p_asize
.if .asize = 8
    .out ".asize 8"
.else
    .out ".asize 16"
.endif
.endmacro

In general it is not possible for an assembler to figure out what the state of the m and x flags will be at runtime because of Rice's theorem - that is the limitation of the .SMART directive. If there are other reasons you dislike the .SMART directive, I'm curious to know why. Also, if anyone has macros like these for other assemblers, feel free to post them here.


Top
 Profile  
Reply with quote  
PostPosted: Fri Jun 17, 2022 11:57 pm 
Offline
User avatar

Joined: Fri Aug 03, 2018 8:52 am
Posts: 746
Location: Germany
jeffythedragonslayer wrote:
Sometimes the assembler thinks these are set differently from how they actually turn out to be at runtime, so it generates the wrong size instructions and the CPU gets de-synched from the code, loading operand bytes and trying to interpret them as opcode/admode bytes.

with ca65 you can just directly tell the assembler what width the registers have right now using the .A8 .A16 .I8 .I16 directives.
include those inside your register width switching macros and you shouldn't have this issue anymore. much better than relying on the .SMART directive.

Code:
.macro accu8
   SEP #%00100000
   .A8
.endmacro

.macro accu16
   REP #%00100000
   .A16
.endmacro

.macro index8
   SEP #%00010000
   .I8
.endmacro

.macro index16
   REP #%00010000
   .I16
.endmacro

.macro all8
   SEP #%00110000
   .A8
   .I8
.endmacro

.macro all16
   REP #%00110000
   .A16
   .I16
.endmacro

also remember to include the .A8 .A16 .I8 .I16 directives at the start of each subroutine, just to be safe.


Top
 Profile  
Reply with quote  
PostPosted: Sat Jun 18, 2022 12:51 am 
Offline

Joined: Sun Oct 03, 2021 2:17 am
Posts: 114
Hi Proxy, I find that taking up another two lines at the beginning of every routine is a lot of linenoise so I don't always do it. Perhaps some souped-up .proc-like macros that fold those .Ann/.Inn directives into the procedure declaration would ease that. BigEd pointed out the 65816 only has 5 modes after all when it comes to these register sizes (and emulation/native):

viewtopic.php?f=2&t=7139&p=93299&hilit=has+5+modes#p92986


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: