I'm surprised no one has responded yet. I can't take time to analyze the function, but I'll make some comments to help efficiency.
bradleystach wrote:
I put together some routines to bit bang SPI for my SBC. These routines are based on
code written by Garth Wilson. I have added support for SPI mode 0-3 and full-duplex transfers. It has been successfully used to talk to a
MAX3100 UART in mode 0, and a DS3234 RTC in mode 1. Mode 2 and 3 are not tested but should work.
The code can be found here
https://github.com/bradleystach/65C02-SPI for anyone who might be interested.
Now keep in mind this was put together by someone who hasn't done much 6502 coding since I was 14 years old back in 1984.
No guarantees on it being the fastest or most compact implementation. If anyone has any suggestions for improvements, I am completely open to them.
Also, if anyone has any questions, feel free to post them here.
Hope they help someone out.
At README.md, you wrote,
Quote:
NOTE: All SPI routines should be called with Interrupts disabled to prevent disruption of data transfer
One beauty of synchronous serial (like SPI) however is that you
can interrupt it, as long as the interrupt does not change the I/O port bits being used for the communication.
In SPI.s65, you have,
Code:
asl
cmp #0
and later I saw
Code:
ldx SPIMODE ; Get the current SPI communication mode
cpx #0 ; Is it mode 0?
Note that the comparison to zero is an automatic, implied part of every load, pull, arithmetic, and logical instruction; so it's not necessary to put it here. It's redundant.
Where you have
Code:
cmp #1 ; Mode 0/1 or mode 2/3
you can do the comparison with one byte less (although it will be the same speed) with DEA if you don't need the original value anymore (as is the case here). DEA (or DEC A) will take the 1 down to 0 and set the Z flag. Every other value will result in Z being cleared. Taking it further, it you want to test for a sequence of numbers, you can DEA, BNE/BEQ, DEA, BNE/BEQ, etc..
You put PHA...PLA as bookends in all the subroutines. Are you sure you need to do that? Subroutines are typically called where, if A matters, it's being used to pass a parameter one or both ways.
I have a lot of programming tips for not-so-experienced 65xx programmers, at
http://wilsonminesco.com/6502primer/PgmTips.html .
In any case, I'm always glad for resources being published to promote the use of SPI and similar with 65xx processors.