RetroBrew Computers Forum
Discussion forum for the RetroBrew Computers community.

Home » RBC Forums » General Discussion » Z80 SPI fast SD (parallel to SPI for SD card design)
Z80 SPI fast SD [message #8496] Sun, 25 April 2021 08:02 Go to next message
lynchaj is currently offline  lynchaj
Messages: 1080
Registered: June 2016
Senior Member
Hi
I've been experimenting with a Z80 SPI interface and I found this interesting Z80 SPI interface article on the web. I attached a PDF of the article which includes the original link and author.

I've implemented the circuit making some adjustments for voltage level translation from 5v to 3.3v although the circuit is a mix of 5v TTL and an 3.3v SD card. The original circuit used 74hcxxx logic which I've converted to all TTL. The 74ls595 part is not available but the 74hct595 is available on eBay.

I am not going to get around to this board for quite a while so I thought I'd put it out here for people to review, discuss, and possibly use for their own designs. The parallel to serial nature of this design has the potential to dramatically speed up SD operations for Z80 compared to bit-banging and also could be the basis for a general purpose SPI board assuming you eliminate the SD card.

/forum/index.php?t=getfile&id=2104&private=0

Hopefully this will be useful to someone. Let me know if you want the KiCAD files. Obviously they are not finished yet but I don't want this to get lost on my hard drive and disappear into nothingness.

Does anyone have the part number for the KiCAD "SD_Card_Receptacle"?
Re: Z80 SPI fast SD [message #8497 is a reply to message #8496] Sun, 25 April 2021 11:06 Go to previous messageGo to next message
monahan is currently offline  monahan
Messages: 25
Registered: November 2015
Junior Member
I looked at this a few times. Has anybody written and tested a software program for a Z80 with SD cards to actully read and write sectors. Different SD cards have a complex protocol interface. I have been using an SPI module in a Cyclone IV FPGA with its own internal Z80. Works fine but like the above proposed simple hardware inherface
Re: Z80 SPI fast SD [message #8498 is a reply to message #8497] Sun, 25 April 2021 11:55 Go to previous messageGo to next message
wsm is currently offline  wsm
Messages: 232
Registered: February 2017
Location: AB, Canada
Senior Member
I've used a CPLD to implement SD SPI at a 25MHz SPI clock rate but haven't tested it at 50MHz. One nice addition is the generation of a WAIT signal which can be triggered when a second I/O operation is initiated but the previous one has not completed. That eliminates the need for software delays and makes it independent of processor clock speed.

Since this was initially for CP/M 2.2 rather than 3, I've also written my own buffering routines using multiple buffers and a simple LRU scheme. I've done a lot of testing using both PIO and DMA on various Z180's with PHI from 6.144 to 36.864MHz and it makes for a very fast CP/M "disk".

The software initialization takes a fair bit of code due to timeout criteria plus MMC, SD, SDHC and SDXC. So far the only subtle issue I've had is that the SandDisk uSD cards I've tested require an extra dummy cycle of clocks during initialization whereas Kingston cards didn't.
Re: Z80 SPI fast SD [message #8501 is a reply to message #8498] Sun, 25 April 2021 14:24 Go to previous messageGo to next message
cluso99 is currently offline  cluso99
Messages: 40
Registered: June 2017
Member
Ouch! I note the PDF uses 5V for the SD card, not the required 3V3. Pleased your schematic shows 3V3.

I have written SD SPI drivers for the Parallax P1 more than 10 years ago, and did the P2 ROM SD boot code. I found the SanDisk cards to be the easiest and most reliable, and not found I needed any more than 96 clocks for initialisation. I've not seen problems with any other legitimate SD cards (ie not the fake ones that misreport larger sizes than in reality), but I do note I have seen lots of articles on various forums where other SD brands have been reported not to work. I did find a card that misimplemented one of the boot responses incorrectly but it was the only brand that did this, but that was many years ago. I've also found when sharing pins, that many cards don't release the DO (data out from the card) correctly and require extra clocks to force the DO drive release but that shouldn't be a proble here. All my P1 designs share the DO pins with others such as the SRAM on my RamBlade designs. On P2, if a Flash chip is included with SD, then all 4 SPI pins are shared, and these extra clocks after a command are required.

As an extra note, a bulk and a bypass cap should be provided right at the card socket pins on the power/GND supply. Older cards were much more demanding.

I now only support SDHC or SDXC cards using sector mode ie addressing is by sector, not the old byte addressing used by all original SD and early SDHC cards. Note also that SDXC cards are usually formatted as exFAT, not FAT32. I think exFAT is still covered by an MS patent and is not backwards compatible with FAT32/FAT16. IIRC FAT32 only covers up to 1TB.

FWIW on P2 ROM boot I implemented an alternative for non-FAT32 formatted cards which can boot from the MBR or VOL sector(s).

My Z80 emulation maps 8x 8MB HDDs onto 8 FAT32 8MB files.

[Updated on: Sun, 25 April 2021 14:27]

Report message to a moderator

Re: Z80 SPI fast SD [message #8502 is a reply to message #8497] Sun, 25 April 2021 14:30 Go to previous messageGo to next message
lynchaj is currently offline  lynchaj
Messages: 1080
Registered: June 2016
Senior Member
monahan wrote on Sun, 25 April 2021 14:06
I looked at this a few times. Has anybody written and tested a software program for a Z80 with SD cards to actully read and write sectors. Different SD cards have a complex protocol interface. I have been using an SPI module in a Cyclone IV FPGA with its own internal Z80. Works fine but like the above proposed simple hardware inherface
Hi John
The article gives this information on how to use the interface which has 4 IO ports. port 0 is for writing, port 1 is for reading, port 2 for lowering CS#, and port 3 for raising CS#. Ports 2 & 3 seem to be used for detecting the presence of an SD card. I think much of the SD code has already been written for PPISD and N8 bit-banging software. I presume it would be a matter of substituting this low level byte transfer code for the low level bit by bit code. I think most of the existing SD code would be unchanged.

Quote:

Assembly Interface
Bytes are transferred through the interface with this simple assembly code:
transfer_byte:
out $00
receive_byte:
out $01
in $00
ret
First the byte to be transmitted is written to port 0, then a write to port 1 causes the
SPI transfer to take place. Finally, a read from port 0 retrieves the received byte.
My notes indicate that no "nop" instructions are required before the "in"
instruction, but I was probably using one of those old 4 MHz Z80 chips at the time.
A 10 MHz chip probably requires two "nop" instructions and a 20 MHz chip
probably requires six, unless you use a faster clock for the SD transfer. The limit on
the SPI clock is probably going to be the counter chip you use in the circuit below.
The SD cards themselves are already designed to transfer data at insane speeds.
and

Quote:

Anyway, this sets up port 0 to read/write the shift registers. A write to port 1 then
starts the SPI transfer. Ports 2 & 3 when written to toggle the /CS on the SD card,
with port 2 lowering /CS and port 3 raising it. Ports 2 & 3 when read from similarly
toggle a pull-up / pull-down resistor attached to the SD card's data output pin,
which can be used to detect if a card is present by changing the polarity and noting
whether the bits read from the interface continue to match the pull-up / pull-down,
so that an empty card slot looks different than an unresposive but present card.
I am still working on the PCB and making minor tweaks but this is just a distraction while I wait for the Z80 serial board PCBs to arrive.

Thanks, Andrew Lynch

PS, BTW I am not making any claims to being original with this, I found the circuit diagram here:

https://www.ecstaticlyrics.com/electronics/SPI/fast_z80_inte rface.html

[Updated on: Sun, 25 April 2021 14:32]

Report message to a moderator

Re: Z80 SPI fast SD [message #8503 is a reply to message #8501] Sun, 25 April 2021 14:36 Go to previous messageGo to next message
lynchaj is currently offline  lynchaj
Messages: 1080
Registered: June 2016
Senior Member
cluso99 wrote on Sun, 25 April 2021 17:24
As an extra note, a bulk and a bypass cap should be provided right at the card socket pins on the power/GND supply. Older cards were much more demanding.
OK, I'll do that. Thanks!

Still a work in progress, not close to done yet. But an interesting design and looking forward to installing it. It's a shame there is no 3D model for the SD Card footprint, at least that I've been able to find. It is the one missing piece of the 3D render


/forum/index.php?t=getfile&id=2112&private=0

  • Attachment: Z80SPI.jpg
    (Size: 163.23KB, Downloaded 2215 times)

[Updated on: Sun, 25 April 2021 15:25]

Report message to a moderator

Re: Z80 SPI fast SD [message #8505 is a reply to message #8503] Sun, 25 April 2021 16:44 Go to previous messageGo to next message
wsm is currently offline  wsm
Messages: 232
Registered: February 2017
Location: AB, Canada
Senior Member
re Bulk cap for SD power: I've only been using uSD cards so I'm not sure about full SD cards. I noted the comments on elm-chan .org (i.e. FatFs) about caps and the various waveforms. Initially I had a similar problem that got worse with larger uSD cards even with a 33uF cap right at the socket. I'd used an MCP1700-330 for the 3.3V regulator and upon further investigation I realized it had very poor transient response. The +3.3V output dipped to less than +2V on a 16GB card insertion while the +5V input showed negligible change. Changing the regulator to an AP2127 with much better transient response made a HUGE difference and the cap can be smaller with a negligible +3.3V dip. I typically use 22uF but have also successfully used 10uF.

re Initialization extra clocks: I also use the SanDisk cards as my reference. The issue was not with the initial 74 clocks but on one of early command responses. Offhand I can't remember which one but it was totally repeatable across several SanDisk cards of different sizes whereas Kingstons worked fine.
Red-faced update: After a quick review of my code, I no longer do this. I'll admit that it might have been during one of the earlier iterations of the CPLD code and SW where I wasn't waiting long enough between bytes or something similar. This kind of timing issue is why I now use the CPLD and WAITs for timing rather than SW and I haven't had any issues since then with dozens of systems and dozens of cards.
Re: Z80 SPI fast SD [message #8508 is a reply to message #8503] Sun, 25 April 2021 17:54 Go to previous messageGo to next message
kman is currently offline  kman
Messages: 45
Registered: February 2019
Location: Germany
Member
Quote:
It's a shame there is no 3D model for the SD Card footprint, at least that I've been able to find. It is the one missing piece of the 3D render
I used this footprint and 3D-shape for a SD-Card holder on my kiCAD Multicomp pcb. You have to change your footprint if you intend to use it. See the zip-file.

cheers
Kurt

[Updated on: Mon, 26 April 2021 05:19]

Report message to a moderator

Re: Z80 SPI fast SD [message #8513 is a reply to message #8508] Mon, 26 April 2021 03:13 Go to previous messageGo to next message
lynchaj is currently offline  lynchaj
Messages: 1080
Registered: June 2016
Senior Member
Thank you, I will look into using that 3D model and updating my footprint. BTW, I was able to find the actual part number for the SD Card. It is part from Amphenol 101-00313-68-02. Major props to Sergey who had this info in his ParPortProp description web page. I am adding this info to the Z80 SPI schematic. Also there seems to be many surplus/similar parts/clones on eBay for reasonable prices.

[Updated on: Mon, 26 April 2021 03:19]

Report message to a moderator

Re: Z80 SPI fast SD [message #8514 is a reply to message #8502] Mon, 26 April 2021 03:17 Go to previous messageGo to next message
lynchaj is currently offline  lynchaj
Messages: 1080
Registered: June 2016
Senior Member
Hi
An update on the Z80 SPI. I contacted the original author of the SPI SD interface for Z80 and had a brief email exchange. Seems like a nice person but no longer active in this particular hobby. However did send me some sample code which I'll have to post later today when I get a chance.

Also, based on some wise counsel I am switching from the 3.3v Zener diode for clamping protection to a all-resistor voltage divider circuit. It certainly is simpler and probably cheaper too. Shouldn't be any concern about rise times with a couple of resistors. So I'll post new info with the adjustments along with the sample code.

BTW, this could easily be an ECB board by just replacing the 2x25 pin header with a DIN 41612 connector. I am using a template for the Z80 MBC but there is nothing special about it.

[Updated on: Mon, 26 April 2021 03:25]

Report message to a moderator

Re: Z80 SPI fast SD [message #8515 is a reply to message #8514] Mon, 26 April 2021 06:59 Go to previous messageGo to next message
plasmo is currently offline  plasmo
Messages: 916
Registered: March 2017
Location: New Mexico, USA
Senior Member
Daryl Rictor has a long thread about SPI on 6502.org with associated documentations and design. It was implemented in a Xilinx CPLD and later on Atmel ATF1504. My little contribution was to convert the WinCUPL design to schematic and ported to Altera EPM7064S CPLD.
http://forum.6502.org/viewtopic.php?f=4&t=1265
http://sbc.rictor.org/65spi2.html

I also breadboard the Altera EPM7065S design and checked it out with a SPI EPROM. It is documented on retrobrewcomputers here:
https://www.retrobrewcomputers.org/doku.php?id=builderpages: plasmo:protorc3:65spi_proto

I like Daryl's documentation and the thought he put into the generalized SPI design, but I think it can be simplified significantly for specific applications and free up the CPLD for other functions.
Bill
Re: Z80 SPI fast SD [message #8517 is a reply to message #8514] Mon, 26 April 2021 07:38 Go to previous messageGo to next message
lynchaj is currently offline  lynchaj
Messages: 1080
Registered: June 2016
Senior Member
Hi
Updated file set with changes and example software routines

[Updated on: Mon, 26 April 2021 07:40]

Report message to a moderator

Re: Z80 SPI fast SD [message #8518 is a reply to message #8517] Mon, 26 April 2021 07:40 Go to previous messageGo to next message
lynchaj is currently offline  lynchaj
Messages: 1080
Registered: June 2016
Senior Member
/forum/index.php?t=getfile&id=2124&private=0
  • Attachment: Z80SPI.jpg
    (Size: 163.12KB, Downloaded 2138 times)
Re: Z80 SPI fast SD [message #8520 is a reply to message #8518] Mon, 26 April 2021 08:14 Go to previous messageGo to next message
cluso99 is currently offline  cluso99
Messages: 40
Registered: June 2017
Member
Andrew, looks like you're using electrolytic for the bulk caps. Tantalum do a way better job here, or else the modern monos. Electros just don't do the job. A 10uF or even 4u7F.

[Updated on: Mon, 26 April 2021 08:16]

Report message to a moderator

Re: Z80 SPI fast SD [message #8523 is a reply to message #8520] Mon, 26 April 2021 08:46 Go to previous messageGo to next message
lynchaj is currently offline  lynchaj
Messages: 1080
Registered: June 2016
Senior Member
OK, I will take a look at substitutes. I like tantalum capacitors but they have a nasty tendency to go "boom" as they age. That's why I wear safety glasses when ever testing old boards or disk drives. Seen plenty of them self-destruct. You raise a good point though, electrolytics are cheap and easy but not the best capacitors.
Re: Z80 SPI fast SD [message #8525 is a reply to message #8517] Mon, 26 April 2021 11:24 Go to previous messageGo to next message
monahan is currently offline  monahan
Messages: 25
Registered: November 2015
Junior Member
Strange assembly language listing.
Anybody got this working with a "regular Zilog" listing?
Re: Z80 SPI fast SD [message #8526 is a reply to message #8501] Mon, 26 April 2021 11:27 Go to previous messageGo to next message
monahan is currently offline  monahan
Messages: 25
Registered: November 2015
Junior Member
I too had problems with multiple SD cards (4) sharing the SPI data line. Cost me a week screwing around! Wish I had known that. The fix was to have each SD card have its own FPGA data pins. Good to know it's not just me!
Re: Z80 SPI fast SD [message #8527 is a reply to message #8526] Mon, 26 April 2021 14:41 Go to previous messageGo to next message
cluso99 is currently offline  cluso99
Messages: 40
Registered: June 2017
Member
monahan wrote on Mon, 26 April 2021 11:27
I too had problems with multiple SD cards (4) sharing the SPI data line. Cost me a week screwing around! Wish I had known that. The fix was to have each SD card have its own FPGA data pins. Good to know it's not just me!
It's definitely not a problem to share all 4 pins. You just need to finish up each command with some extra clocks to force the SD cards to release (tri state) DO. I've been sharing the pins with SRAM on my boards for 10 years without problems.
Re: Z80 SPI fast SD [message #8533 is a reply to message #8496] Mon, 26 April 2021 22:46 Go to previous messageGo to next message
monahan is currently offline  monahan
Messages: 25
Registered: November 2015
Junior Member
Curious about how many clock cycles to be safe.
John
Re: Z80 SPI fast SD [message #8534 is a reply to message #8533] Tue, 27 April 2021 02:15 Go to previous messageGo to next message
cluso99 is currently offline  cluso99
Messages: 40
Registered: June 2017
Member
monahan wrote on Mon, 26 April 2021 22:46
Curious about how many clock cycles to be safe.
John
After the completion of a command transaction I

send an $FF
set CS=1
send an $FF
float CS
float CK
float DI
Re: Z80 SPI fast SD [message #8537 is a reply to message #8534] Tue, 27 April 2021 06:42 Go to previous messageGo to next message
wsm is currently offline  wsm
Messages: 232
Registered: February 2017
Location: AB, Canada
Senior Member
Interesting to see this sequence and I believe it deals with the problem I described above.

Now that the cobwebs have cleared, I remember that I changed my CPLD such that a SPI read returns the last read data but also triggers a new read with MOSI of FFh. Thus it accomplishes the first "send an $FF". On my eZ80 I use it's SPI controller and the same initialization code BUT I also send a FFh after each sequence of initialization reads before setting CS inactive. Neither implementation sends an extra FFh after the CRC on reads using CMD17 (read single block). So far I haven't found a need for the second "send an $FF" after setting CS inactive.

Although I don't use multiple SD sockets, I do have other devices sharing the three common SPI signals in both implementations. That was another issue for the CPLD as the SD operates with CLK at 25 MHz while the RTC is at 5 MHz. The CPLD changes frequency depending upon which CS is active.

I assume it's just semantics but CS, CLK and MOSI shouldn't be floated unless they have weak pullups.
Re: Z80 SPI fast SD [message #8539 is a reply to message #8537] Tue, 27 April 2021 10:58 Go to previous messageGo to next message
monahan is currently offline  monahan
Messages: 25
Registered: November 2015
Junior Member
Thanks cluso99. I did things differently. I just send 8 clocks and stop everything at once.
John

Re: Z80 SPI fast SD [message #8548 is a reply to message #8539] Tue, 27 April 2021 22:46 Go to previous messageGo to next message
cluso99 is currently offline  cluso99
Messages: 40
Registered: June 2017
Member
monahan wrote on Tue, 27 April 2021 10:58
Thanks cluso99. I did things differently. I just send 8 clocks and stop everything at once.
John

Yes, sending $FF and sending 8 clocks are exactly the same thing. For me it was just a call #sendff. But I did do it both before and after dropping CS (ie CS -> 1)
Re: Z80 SPI fast SD [message #8555 is a reply to message #8548] Wed, 28 April 2021 11:23 Go to previous messageGo to next message
monahan is currently offline  monahan
Messages: 25
Registered: November 2015
Junior Member
One final question.
I programming an S100 Board to work with CF and SD cards. See here:-
http://s100computers.com/My%20System%20Pages/FPGA_DC%20Board /FPGA_DC%20Board.htm

I'm finding that sector R/W's to the CF card are about 3 times faster than to the SD card. The latter being done with the FPGA/Z80 as a series of bit flips etc. running at 8MHz. Is that normal.
Re: Z80 SPI fast SD [message #8557 is a reply to message #8555] Wed, 28 April 2021 12:51 Go to previous messageGo to next message
wsm is currently offline  wsm
Messages: 232
Registered: February 2017
Location: AB, Canada
Senior Member
@monahan: Since you're using an FPGA, one way to significantly speed up the SD interface might be to use native SD mode rather than SPI. There's at least one version at opencores.org.

Comparing CF to SD is *VERY* dependent on both the software and the hardware interface, especially with FPGA's or CPLD's. Without allowing for software overhead, maximum device throughput would be:

SD SPI @ 8 MHz = 1 uS/byte
SD SPI @ 25 MHz = 320 nS/byte
SD HS @ 50 MHz = 40 nS/byte
SD UHS-I @ 50 MHz = 20 ns/byte
SD UHS-II = 3.2 ns/byte

CF PIO 4 or DMA 2 = 120 nS/byte or word
CF PIO 5 or DMA 3 = 100 nS /byte or word
CF PIO 6 or DMA 4 = 80 nS/byte or word
CF UDMA 7 = 6 nS/word

SD can be as fast as CF but it all depends on individual SW & HW implementations plus the actual cards. Given the typical homebrew implementation of SPI for SD and 8-bit mode for CF, I would expect CF to be faster. I've found that uSD at 25 MHz on a Z180 via SPI/DMA or 8 MHz on the eZ80 is more than fast enough for all my needs when doing something like PIP *.* to copy a disk. As to SD vs CF ... my preference is for the smaller size and cheaper media with uSD.
Re: Z80 SPI fast SD [message #8560 is a reply to message #8557] Thu, 29 April 2021 08:53 Go to previous messageGo to next message
monahan is currently offline  monahan
Messages: 25
Registered: November 2015
Junior Member
Thanks wsm, missed the opencores post will take a look
John
Re: Z80 SPI fast SD [message #8563 is a reply to message #8560] Thu, 29 April 2021 22:30 Go to previous message
monahan is currently offline  monahan
Messages: 25
Registered: November 2015
Junior Member
A long shot I guess, but has anybody got/seen a Z80 code implementation of an interface with an SD card in native mode.
The closest I have come to is this:-
https://www.fpga4fun.com/SD1.html
but the is little actual info example

Previous Topic: Bus Buffers/Z80 Electrical Characteristic Questions
Next Topic: Programmers section in the Wiki


Current Time: Sat Nov 08 02:59:18 PST 2025

Total time taken to generate the page: 0.02185 seconds