1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-11-10 03:08:06 +11:00
collapseos/emul/z80/sms_spi.c
Virgil Dupras ce10320030 emul/z80/sms: add SPI exchange through controller port
Theoretically, it works. I can access an emulated SD card on it.

Will it work on real hardware?

I've also made SMS emulation faster. It was unbearably slow for SDC
access.
2020-10-29 19:42:19 -04:00

40 lines
812 B
C

#include "sms_spi.h"
void spi_init(SPI *spi, Tristate *TH, Tristate *TR, EXCH spixfn)
{
spi->TH = TH;
spi->TR = TR;
spi->lastTH = *TH;
spi->bitcnt = 0;
spi->bit = false;
spi->rx = 0;
spi->tx = 0xff;
spi->spixfn = spixfn;
}
byte spi_rd(SPI *spi)
{
return (byte)spi->bit; // return in bit 0
}
void spi_pulse(SPI *spi)
{
if (*spi->TH != spi->lastTH) {
spi->lastTH = *spi->TH;
if (spi->lastTH == TRI_HIGH) {
spi->rx <<= 1;
if (*spi->TR == TRI_HIGH) {
spi->rx++;
}
spi->bit = spi->tx >> 7;
spi->tx <<= 1;
spi->bitcnt++;
if (spi->bitcnt == 8) {
spi->tx = spi->spixfn(spi->rx);
spi->bitcnt = 0;
}
}
}
}