Skip to content

Commit c812a1b

Browse files
committed
sd: SdCard renamed to SdCardPeripheral.
This is the type which meets the via6522.ParallelPeripheral interface, but doesn't implement the SD card details.
1 parent b1cce9c commit c812a1b

File tree

4 files changed

+70
-67
lines changed

4 files changed

+70
-67
lines changed

go6502.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func mainReturningStatus() int {
7676
}
7777

7878
if len(options.SdCard) > 0 {
79-
sd, err := sd.NewSdCard(spi.PinMap{
79+
sd, err := sd.NewSdCardPeripheral(spi.PinMap{
8080
Sclk: 0,
8181
Mosi: 6,
8282
Miso: 7,

sd/sd.go

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,3 @@
22
Package SD emulates an SD/MMC card.
33
*/
44
package sd
5-
6-
import (
7-
"io/ioutil"
8-
9-
"github.com/pda/go6502/spi"
10-
)
11-
12-
type SdCard struct {
13-
state *sdState
14-
spi *spi.Slave
15-
}
16-
17-
// SdFromFile creates a new SdCard based on the contents of a file.
18-
func NewSdCard(pm spi.PinMap) (sd *SdCard, err error) {
19-
sd = &SdCard{
20-
state: newSdState(),
21-
spi: spi.NewSlave(pm),
22-
}
23-
24-
// two busy bytes, then ready.
25-
sd.state.queueMisoBytes(0x00, 0x00, 0xFF)
26-
27-
return
28-
}
29-
30-
func (sd *SdCard) PinMask() byte {
31-
return sd.spi.PinMask()
32-
}
33-
34-
// LoadFile is equivalent to inserting an SD card.
35-
func (sd *SdCard) LoadFile(path string) (err error) {
36-
data, err := ioutil.ReadFile(path)
37-
if err != nil {
38-
return
39-
}
40-
sd.state.data = data
41-
return
42-
}
43-
44-
func (sd *SdCard) Shutdown() {
45-
}
46-
47-
func (sd *SdCard) Read() byte {
48-
return sd.spi.Read()
49-
}
50-
51-
// Write takes an updated parallel port state.
52-
func (sd *SdCard) Write(data byte) {
53-
if sd.spi.Write(data) {
54-
if sd.spi.Done {
55-
mosi := sd.spi.Mosi
56-
//fmt.Printf("SD MOSI $%02X %08b <-> $%02X %08b MISO\n",
57-
// mosi, mosi, sd.spi.Miso, sd.spi.Miso)
58-
59-
// consume the byte read, queue miso bytes internally
60-
sd.state.consumeByte(mosi)
61-
// dequeues one miso byte, or a default byte if queue empty.
62-
sd.spi.QueueMisoBits(sd.state.shiftMiso())
63-
}
64-
}
65-
}
66-
67-
func (sd *SdCard) String() string {
68-
return "SD card"
69-
}

sd/sd_card_peripheral.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package sd
2+
3+
import (
4+
"io/ioutil"
5+
6+
"github.com/pda/go6502/spi"
7+
)
8+
9+
type SdCardPeripheral struct {
10+
state *sdState
11+
spi *spi.Slave
12+
}
13+
14+
// SdFromFile creates a new SdCardPeripheral based on the contents of a file.
15+
func NewSdCardPeripheral(pm spi.PinMap) (sd *SdCardPeripheral, err error) {
16+
sd = &SdCardPeripheral{
17+
state: newSdState(),
18+
spi: spi.NewSlave(pm),
19+
}
20+
21+
// two busy bytes, then ready.
22+
sd.state.queueMisoBytes(0x00, 0x00, 0xFF)
23+
24+
return
25+
}
26+
27+
// LoadFile is equivalent to inserting an SD card.
28+
func (sd *SdCardPeripheral) LoadFile(path string) (err error) {
29+
data, err := ioutil.ReadFile(path)
30+
if err != nil {
31+
return
32+
}
33+
sd.state.data = data
34+
return
35+
}
36+
37+
// via6522.ParallelPeripheral interface
38+
39+
func (sd *SdCardPeripheral) PinMask() byte {
40+
return sd.spi.PinMask()
41+
}
42+
43+
func (sd *SdCardPeripheral) Read() byte {
44+
return sd.spi.Read()
45+
}
46+
47+
func (sd *SdCardPeripheral) Shutdown() {
48+
}
49+
50+
// Write takes an updated parallel port state.
51+
func (sd *SdCardPeripheral) Write(data byte) {
52+
if sd.spi.Write(data) {
53+
if sd.spi.Done {
54+
mosi := sd.spi.Mosi
55+
//fmt.Printf("SD MOSI $%02X %08b <-> $%02X %08b MISO\n",
56+
// mosi, mosi, sd.spi.Miso, sd.spi.Miso)
57+
58+
// consume the byte read, queue miso bytes internally
59+
sd.state.consumeByte(mosi)
60+
// dequeues one miso byte, or a default byte if queue empty.
61+
sd.spi.QueueMisoBits(sd.state.shiftMiso())
62+
}
63+
}
64+
}
65+
66+
func (sd *SdCardPeripheral) String() string {
67+
return "SD card"
68+
}

sd/sd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestSdPinMask(t *testing.T) {
11-
sd, _ := NewSdCard(spi.PinMap{Sclk: 4, Mosi: 5, Miso: 6, Ss: 7})
11+
sd, _ := NewSdCardPeripheral(spi.PinMap{Sclk: 4, Mosi: 5, Miso: 6, Ss: 7})
1212
if sd.PinMask() != 0xF0 {
1313
t.Error(fmt.Sprintf("0b%08b != 0b%08b", sd.PinMask(), 0xF0))
1414
}

0 commit comments

Comments
 (0)