-
Notifications
You must be signed in to change notification settings - Fork 1
/
block_dev_dpi.cpp
269 lines (218 loc) · 4.32 KB
/
block_dev_dpi.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// block_dev_dpi.cpp --- ---!!!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include "svdpi.h"
#include "Vtest__Dpi.h"
int last_start_bit, last_rd_bit, last_wr_bit;
static int debug;
int fifo_rd;
int fifo_wr;
int fifo_depth;
unsigned short fifo[256 * 128];
static int file_inited;
static int file_fd;
unsigned int lba;
unsigned int rw_status;
static int busy_delay;
static int pending_read;
static int wr_stream;
static int rd_stream;
static int rd_blocks;
static int wr_blocks;
static int bsy_state;
static int rdy_state;
static int err_state;
static int iordy_state;
static int data_out_state;
static void
do_file_setup(void)
{
file_fd = open("disk.img", O_RDWR);
if (debug)
printf("block_dev_dpi: fd %d\n", file_fd);
fifo_depth = 0;
fifo_rd = 0;
fifo_wr = 0;
wr_stream = 0;
rd_stream = 0;
}
static void
do_bd_read(void)
{
int ret;
printf("block_dev_dpi: %d (lba %d) (read)\n",
lba*512, lba);
ret = lseek(file_fd, (off_t)lba*512, SEEK_SET);
ret = read(file_fd, (char *)fifo, 512*2);
if (ret < 0)
perror("read");
if (debug)
printf("block_dev_dpi: buffer %06o %06o %06o %06o\n",
fifo[0], fifo[1], fifo[2], fifo[3]);
fifo_depth = 512;
fifo_rd = 0;
fifo_wr = 0;
}
static void
do_bd_write(void)
{
if (debug)
printf("block_dev_dpi: write prep\n");
fifo_depth = 512;
fifo_rd = 0;
fifo_wr = 0;
}
static void
do_bd_write_done(void)
{
int ret;
printf("block_dev_dpi: %d (lba %d) (write)\n",
lba*512, lba);
ret = lseek(file_fd, (off_t)lba*512, SEEK_SET);
ret = write(file_fd, (char *)fifo, 512*2);
if (ret < 0) {
perror("write");
}
}
void do_busy_delay(void)
{
busy_delay = 20;
}
void block_dev_dpi(int cmd, int start, int* bsy, int* rdy, int* err, int addr,
int data_in, int* data_out, int rd, int wr, int* iordy)
{
int start_start, start_stop, read_start, read_stop, write_start, write_stop;
if (file_inited == 0) {
file_inited = 1;
do_file_setup();
}
start_start = 0;
start_stop = 0;
read_start = 0;
read_stop = 0;
write_start = 0;
write_stop = 0;
if (start != last_start_bit) {
if (start == 1)
start_start = 1;
if (start == 0)
start_stop = 1;
}
last_start_bit = start;
if (rd != last_rd_bit) {
if (rd == 1)
read_start = 1;
if (rd == 0)
read_stop = 1;
}
last_rd_bit = rd;
if (wr != last_wr_bit) {
if (wr == 1)
write_start = 1;
if (wr == 0)
write_stop = 1;
}
last_wr_bit = wr;
iordy_state = 1;
if (start_start) {
rdy_state = 0;
bsy_state = 1;
err_state = 0;
data_out_state = 0;
lba = addr;
switch (cmd) {
case 0:
printf("block_dev_dpi: reset\n");
do_busy_delay();
bsy_state = 1;
break;
case 1:
printf("block_dev_dpi: read\n");
do_bd_read();
do_busy_delay();
bsy_state = 1;
pending_read = 1;
rd_blocks = 2;
break;
case 2:
printf("block_dev_dpi: write\n");
do_bd_write();
do_busy_delay();
bsy_state = 1;
iordy_state = 1;
wr_blocks = 1;
break;
}
}
if (write_start) {
if (debug)
printf("block_dev_dpi: write start\n");
wr_stream = 1;
iordy_state = 1;
}
if (write_stop) {
if (debug)
printf("block_dev_dpi: write stop\n");
wr_stream = 0;
}
if (wr && wr_stream) {
fifo[fifo_wr] = data_in;
if (debug)
printf("block_dev_dpi: write data [%d/%d] %o\n",
fifo_wr, fifo_depth, data_in);
if (fifo_wr < fifo_depth)
fifo_wr++;
if (fifo_wr >= fifo_depth) {
do_bd_write_done();
fifo_wr = 0;
lba++;
wr_blocks--;
if (wr_blocks == 0)
rdy_state = 1;
}
iordy_state = 1;
}
if (read_start) {
if (debug)
printf("block_dev_dpi: read start\n");
rd_stream = 1;
iordy_state = 1;
}
if (read_stop) {
if (debug)
printf("block_dev_dpi: read stop\n");
rd_stream = 0;
iordy_state = 0;
}
if (rd && rd_stream) {
data_out_state = fifo[fifo_rd];
if (debug)
printf("block_dev_dpi: read data [%d/%d] %o\n",
fifo_rd, fifo_depth, data_out_state);
if (fifo_rd < fifo_depth)
fifo_rd++;
if (fifo_rd >= fifo_depth) {
if (debug)
printf("block_dev_dpi: fifo empty\n");
iordy_state = 0;
}
}
if (busy_delay) {
busy_delay--;
if (busy_delay == 0) {
bsy_state = 0;
rdy_state = 1;
if (pending_read) {
pending_read = 0;
iordy_state = 1;
}
}
}
*bsy = bsy_state;
*rdy = rdy_state;
*iordy = iordy_state;
*data_out = data_out_state;
}