6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun May 12, 2024 5:49 pm

All times are UTC




Post new topic Reply to topic  [ 26 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Mon Nov 06, 2023 1:49 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10800
Location: England
Potato, potato... BDD doesn't like to call them modes, and recommends we follow, but other opinions are valid too. We all know what we mean and that's the important thing. (BDD might possibly have a point that more careful use of language might help in understanding, but for most of us that ship has sailed, and indeed I find the 816 easier to think about as a five-moded CPU.)

(Perhaps if I'd been more careful, and started with tomato, tomato, I might have communicated more clearly too...)


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 06, 2023 3:22 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3354
Location: Ontario, Canada
Choosing the D bit in the Processor Status Register as an example...

I think we can all agree that manipulating D (the Decimal Mode bit) is the means by which we access certain useful capabilities provided by the processor. Likewise we can agree that D defines the context of the code we write.

Uhhh... which maybe sounds pretentious! :oops: All I mean is, you need to be aware of D as you're writing (or reading) code, because losing track of D's status puts you at risk of losing track of what the code does.

Can we also agree that the same applies to the 816's Emulation bit and to the x and m bits? It can hardly be controversial to observe that losing track of those bits' status puts one at risk of losing track of what the code does! (Thus there's a potential pitfall, even though it's counterbalanced by the considerable benefit of not having to resort to multi-byte opcodes.)

In my view, these bits have a lot in common. But, even so, I won't protest if some folk do refer to them as "mode" bits and other folks don't. For gosh sake -- there's no danger of misunderstanding! :roll:

-- Jeff

Edit: Oops, crossed in the post, Ed, but yes -- "We all know what we mean and that's the important thing."

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 06, 2023 4:13 pm 
Offline
User avatar

Joined: Fri Aug 03, 2018 8:52 am
Posts: 746
Location: Germany
oof, i did not intend to derail the thread that much because of simple naming.

though i do agree that it should be allowed for someone to say stuff like "A is in 8-bit mode" without being corrected due to technicalities. for as long as it's clear what they meant.
obviously someone saying "the CPU is in 16-bit mode" is very ambiguous and needs some clarifying, but if they mention A, X/Y, and a bit width, then it's pretty clear what they mean regardless of if they call it "mode" or "width".


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 06, 2023 5:13 pm 
Offline

Joined: Fri Jul 09, 2021 10:12 pm
Posts: 741
When writing large amounts of 65C186 code, do you guys generally have specific policies on how these flags should be left, as part of calling conventions? Or is it a matter of having any routine that requires them to be set in a particular way ensure that they are set that way on entry?


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 06, 2023 5:33 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10800
Location: England
For myself, with limited experience, I think I expected per-routine conventions but found I was using per-project conventions. It's possible for a routine to change modes and then put things back, if that's advantageous.


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 06, 2023 7:59 pm 
Offline
User avatar

Joined: Wed Feb 14, 2018 2:33 pm
Posts: 1411
Location: Scotland
gfoot wrote:
When writing large amounts of 65C186 code, do you guys generally have specific policies on how these flags should be left, as part of calling conventions? Or is it a matter of having any routine that requires them to be set in a particular way ensure that they are set that way on entry?


The bulk of what I've written for the '816 (a VM for the BCPL compiled code totalling some 16KB at present), generally leaves register and memory in 16-bit mode. ('full native')

The underlying operating system is written in 65C02, so when compiled for the '816, the entry points remember the mode/widths/etc, save the mode/flags/registers as required, run in 8-bit/6502 mode then return to the caller in whatever mode it was in when it got called.

This means that the OS can only be called from the first bank and it can only deal with data in the first bank which for me is exactly what I want as I have no code running in any other bank as it's all claimed by the BCPL VM which makes it look like linear address space to BCPL programs because that what they wanted back in '67 so that's what it is today.

-Gordon

_________________
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 06, 2023 8:14 pm 
Offline
User avatar

Joined: Fri Aug 03, 2018 8:52 am
Posts: 746
Location: Germany
for C stuff Calypsi defaults to 16-bit A, X/Y. though for assembly projects i use 8-bit A and 16-bit X/Y. obviously unless 16-bit math or memory accesses are required.
for calling functions if i need a 16 or 32-bit value (or pointer) i use X and Y. X for the Low Word and Y for the High Word. A is used for single byte functions like printing a character or 8-bit value, or as an extra for other functions.
everything beyond those just sits on the stack before the function call, with local symbols to access them via the stack indexed addressing mode.
because keeping track of raw numbers is a pain, so per function symbols makes that much easier


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 06, 2023 11:01 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8180
Location: Midwestern USA
gfoot wrote:
When writing large amounts of 65C186 code, do you guys generally have specific policies on how these flags should be left, as part of calling conventions?  Or is it a matter of having any routine that requires them to be set in a particular way ensure that they are set that way on entry?

I typically start out my 816 programs with .A, .X and .Y set to 16-bits and DP (direct page pointer) pointing to the system’s direct page.  The style I use in large programs is not unlike what is common in C; the mainline routine calls functions (subroutines) to do most of the grunt work.  Parameters are passed to a function in the 16-bit registers, unless there are more parameters than registers, in which case, the parameters are passed through a stack frame.  The 65C816’s behavior when operating in native mode encourages this sort of programming model.

Functions often change register sizes and since I tend, if possible, to make functions transparent, they will save machine state on the stack.  Any data that needs to be returned to the caller will be returned by rewriting the stack copies of the affected registers before state is restored, or by writing to caller-defined memory locations through pointers passed into the function.  In functions that receive their parameters in a stack frame, the function will clean up the stack before exit, relieving the caller of the need to do so.

Any function that uses direct page for local storage will allocate stack space for the purpose and then point DP to it.  It goes without saying that interrupt handlers have to be careful to save and restore machine state with this sort of code in use, since DP will not be pointing to the system direct page when an interrupt hits during function execution.

Extensive use of macros in the mainline code makes the program easier to read, since the macro name matches the function it calls.  Use of macros also cuts typing tedium, since the assembly language mumbo-jumbo needed to define a stack frame or load registers has already been worked out.  Use of macros to make function calls goes a long way toward avoiding bugs caused by an incorrect stack frame structure.

Attached is an edited version of the “skeleton” I use to write new functions.  Much of the rigmarole needed to describe the stack frames accessed within a function is in this skeleton and more-or-less automatic in behavior.

Attachment:
File comment: Function Skeleton (Kowalski assembler syntax)
funcskel.asm [10.12 KiB]
Downloaded 30 times

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 07, 2023 12:37 am 
Offline

Joined: Sun Jul 11, 2021 9:12 am
Posts: 137
A quick thank you to everyone so far. A great wealth of information here.

Playing around last night, I can see benefits in switching back and forth between register sizes now. Depending on the task, it can be much faster and compact to stay with 8 bit width and alternately the same can be said for 16 bit width.

Sometimes the overhead of switching the flags is enough to warrant staying with a particular width. Depends on how much you want to micro-optimise I guess. To me that's half of the fun right there.


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 07, 2023 1:13 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8180
Location: Midwestern USA
J64C wrote:
Sometimes the overhead of switching the flags is enough to warrant staying with a particular width.  Depends on how much you want to micro-optimise I guess. To me that's half of the fun right there.

As you gain experience writing 816 programs, you’ll become more adept at arranging the program flow to minimize the use of REP and SEP.  As always, there are tradeoffs between code size, speed of execution and degree of complexity.  Sometimes it will ultimately be more efficient to leave the registers set to 16-bits, even though you’re processing 8-bit data.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 07, 2023 1:21 am 
Offline

Joined: Sun Jul 11, 2021 9:12 am
Posts: 137
BigDumbDinosaur wrote:
As you gain experience writing 816 programs, you’ll become more adept at arranging the program flow to minimize the use of REP and SEP.  As always, there are tradeoffs between code size, speed of execution and degree of complexity.  Sometimes it will ultimately be more efficient to leave the registers set to 16-bits, even though you’re processing 8-bit data.


Absolutely! And those are the sorts of mental challenges I enjoy now and then too. The never ending puzzle of "How can I do this better?"


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 26 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 7 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: