-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathsdcard.c
194 lines (175 loc) · 3.96 KB
/
sdcard.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Commander X16 Emulator
// Copyright (c) 2019 Michael Steil
// All rights reserved. License: 2-clause BSD
#include <stdio.h>
#include <stdbool.h>
#include "sdcard.h"
#include "via.h"
// VIA#2
// PB0 SPICLK
// PB1 SS
// PB2-4 unassigned
// PB5 SD write protect
// PB6 SD detect
// PB7 MOSI
// CB1 SPICLK (=PB0)
// CB2 MISO
FILE *sdcard_file = NULL;
static bool initialized;
void
sdcard_init()
{
initialized = false;
}
void
sdcard_step()
{
if (!sdcard_file) {
return;
}
uint8_t port = via2_pb_get_out();
bool clk = port & 1;
bool ss = !((port >> 1) & 1);
bool mosi = port >> 7;
static int cmd_receive_counter;
static uint8_t cmd[6];
static bool last_clk = false;
static bool last_ss;
static int bit_counter = 0;
// only care about rising clock
if (clk == last_clk) {
return;
}
last_clk = clk;
if (clk == 0) {
return;
}
if (ss && !last_ss) {
bit_counter = 0;
cmd_receive_counter = 0;
}
last_ss = ss;
// For initialization, the client has to pull&release CLK 74 times.
// The SD card should be deselected, because it's not actual
// data transmission (we ignore this).
if (!initialized) {
if (clk == 1) {
static int init_counter = 0;
init_counter++;
if (init_counter >= 70) {
cmd_receive_counter = 0;
initialized = true;
}
}
return;
}
// for everything else, the SD card neeeds to be selcted
if (!ss) {
return;
}
// receive byte
static uint8_t inbyte, outbyte;
bool bit = mosi;
inbyte <<= 1;
inbyte |= bit;
// printf("BIT: %d BYTE =$%02x\n", bit, inbyte);
bit_counter++;
if (bit_counter != 8) {
return;
}
bit_counter = 0;
static const uint8_t *response = NULL;
static int response_length = 0;
static int response_counter = 0;
if (initialized) {
if (cmd_receive_counter == 0 && inbyte == 0xff) {
// send response data
if (response) {
outbyte = response[response_counter++];
if (response_counter == response_length) {
response = NULL;
}
} else {
outbyte = 0xff;
}
} else {
cmd[cmd_receive_counter++] = inbyte;
if (cmd_receive_counter == 6) {
cmd_receive_counter = 0;
// printf("*** COMMAND: $40 + %d\n", cmd[0] - 0x40);
switch (cmd[0]) {
case 0x40: {
// CMD0: init SD card to SPI mode
static const uint8_t r[] = { 1 };
response = r;
response_length = sizeof(r);
break;
}
case 0x40 + 8: {
static const uint8_t r[] = { 1, 0x00, 0x00, 0x01, 0xaa };
response = r;
response_length = sizeof(r);
break;
}
case 0x40 + 41: {
static const uint8_t r[] = { 0 };
response = r;
response_length = sizeof(r);
break;
}
case 0x40 + 16: {
// set block size
printf("Set block size = 0x%04x\n", cmd[2] << 8 | cmd[3]);
static const uint8_t r[] = { 1 };
response = r;
response_length = sizeof(r);
break;
}
case 0x40 + 17: {
// read block
uint32_t lba =
cmd[1] << 24 |
cmd[2] << 16 |
cmd[3] << 8 |
cmd[4];
static uint8_t read_block_respose[2 + 512 + 2];
read_block_respose[0] = 0;
read_block_respose[1] = 0xfe;
printf("Reading LBA %d\n", lba);
fseek(sdcard_file, lba * 512, SEEK_SET);
int bytes_read = fread(&read_block_respose[2], 1, 512, sdcard_file);
if (bytes_read != 512) {
printf("Warning: short read!\n");
}
response = read_block_respose;
response_length = 2 + 512 + 2;
break;
}
case 0x40 + 55: {
static const uint8_t r[] = { 1 };
response = r;
response_length = sizeof(r);
break;
}
case 0x40 + 58: {
static const uint8_t r[] = { 0, 0, 0, 0 };
response = r;
response_length = sizeof(r);
break;
}
default: {
static const uint8_t r[] = { 0 };
response = r;
response_length = sizeof(r);
break;
}
}
response_counter = 0;
}
outbyte = 0xff;
}
}
// printf("$%02x ->$%02x\n", inbyte, outbyte);
// send byte
via2_sr_set(outbyte);
}