1 |
2 |
alfik |
#include <cstdio>
|
2 |
|
|
#include <cstdlib>
|
3 |
|
|
|
4 |
|
|
#include <dlfcn.h>
|
5 |
|
|
|
6 |
|
|
#include <sys/mman.h>
|
7 |
|
|
#include <sys/types.h>
|
8 |
|
|
#include <sys/stat.h>
|
9 |
|
|
#include <fcntl.h>
|
10 |
|
|
#include <unistd.h>
|
11 |
|
|
|
12 |
|
|
#include "Vfloppy.h"
|
13 |
|
|
#include "verilated.h"
|
14 |
|
|
#include "verilated_vcd_c.h"
|
15 |
|
|
|
16 |
|
|
#include "shared_mem.h"
|
17 |
|
|
|
18 |
|
|
//------------------------------------------------------------------------------
|
19 |
|
|
//------------------------------------------------------------------------------
|
20 |
|
|
//------------------------------------------------------------------------------
|
21 |
|
|
|
22 |
|
|
volatile shared_mem_t *shared_ptr = NULL;
|
23 |
|
|
|
24 |
|
|
//------------------------------------------------------------------------------
|
25 |
|
|
//------------------------------------------------------------------------------
|
26 |
|
|
//------------------------------------------------------------------------------
|
27 |
|
|
|
28 |
|
|
//------------------------------------------------------------------------------
|
29 |
|
|
|
30 |
|
|
int main(int argc, char **argv) {
|
31 |
|
|
//map shared memory
|
32 |
|
|
int fd = open("./../../../sim_pc/shared_mem.dat", O_RDWR, S_IRUSR | S_IWUSR);
|
33 |
|
|
|
34 |
|
|
if(fd == -1) {
|
35 |
|
|
perror("open() failed for shared_mem.dat");
|
36 |
|
|
return -1;
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
shared_ptr = (shared_mem_t *)mmap(NULL, sizeof(shared_mem_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
40 |
|
|
|
41 |
|
|
if(shared_ptr == MAP_FAILED) {
|
42 |
|
|
perror("mmap() failed");
|
43 |
|
|
close(fd);
|
44 |
|
|
return -2;
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
Verilated::commandArgs(argc, argv);
|
48 |
|
|
|
49 |
|
|
Verilated::traceEverOn(true);
|
50 |
|
|
VerilatedVcdC* tracer = new VerilatedVcdC;
|
51 |
|
|
|
52 |
|
|
Vfloppy *top = new Vfloppy();
|
53 |
|
|
top->trace (tracer, 99);
|
54 |
|
|
//tracer->rolloverMB(1000000);
|
55 |
|
|
tracer->open("floppy.vcd");
|
56 |
|
|
|
57 |
|
|
bool dump = false;
|
58 |
|
|
|
59 |
|
|
//reset
|
60 |
|
|
top->clk = 0; top->rst_n = 1; top->eval();
|
61 |
|
|
top->clk = 1; top->rst_n = 1; top->eval();
|
62 |
|
|
top->clk = 1; top->rst_n = 0; top->eval();
|
63 |
|
|
top->clk = 0; top->rst_n = 0; top->eval();
|
64 |
|
|
top->clk = 0; top->rst_n = 1; top->eval();
|
65 |
|
|
|
66 |
|
|
uint64 cycle = 0;
|
67 |
|
|
bool read_cycle = false;
|
68 |
|
|
|
69 |
|
|
/*
|
70 |
|
|
0x00.[0]: media present
|
71 |
|
|
0x01.[0]: media writeprotect
|
72 |
|
|
0x02.[7:0]: media cylinders
|
73 |
|
|
0x03.[7:0]: media sectors per track
|
74 |
|
|
0x04.[31:0]: media total sector count
|
75 |
|
|
0x05.[1:0]: media heads
|
76 |
|
|
0x06.[31:0]: media sd base
|
77 |
|
|
0x07.[15:0]: media wait cycles: 200000 us / spt
|
78 |
|
|
0x08.[15:0]: media wait rate 0: 1000 us
|
79 |
|
|
0x09.[15:0]: media wait rate 1: 1666 us
|
80 |
|
|
0x0A.[15:0]: media wait rate 2: 2000 us
|
81 |
|
|
0x0B.[15:0]: media wait rate 3: 500 us
|
82 |
|
|
0x0C.[7:0]: media type: 8'h20 none; 8'h00 old; 8'hC0 720k; 8'h80 1_44M; 8'h40 2_88M
|
83 |
|
|
*/
|
84 |
|
|
|
85 |
|
|
int floppy_index = -1;
|
86 |
|
|
int floppy_sd_base = 0;
|
87 |
|
|
|
88 |
|
|
bool floppy_is_160k = false;
|
89 |
|
|
bool floppy_is_180k = false;
|
90 |
|
|
bool floppy_is_320k = false;
|
91 |
|
|
bool floppy_is_360k = false;
|
92 |
|
|
bool floppy_is_720k = false;
|
93 |
|
|
bool floppy_is_1_2m = false;
|
94 |
|
|
bool floppy_is_1_44m= true;
|
95 |
|
|
bool floppy_is_2_88m= false;
|
96 |
|
|
|
97 |
|
|
bool floppy_writeprotect = true;
|
98 |
|
|
|
99 |
|
|
int floppy_cylinders = (floppy_is_2_88m || floppy_is_1_44m || floppy_is_1_2m || floppy_is_720k)? 80 : 40;
|
100 |
|
|
int floppy_spt =
|
101 |
|
|
(floppy_is_160k)? 8 :
|
102 |
|
|
(floppy_is_180k)? 9 :
|
103 |
|
|
(floppy_is_320k)? 8 :
|
104 |
|
|
(floppy_is_360k)? 9 :
|
105 |
|
|
(floppy_is_720k)? 9 :
|
106 |
|
|
(floppy_is_1_2m)? 15 :
|
107 |
|
|
(floppy_is_1_44m)? 18 :
|
108 |
|
|
(floppy_is_2_88m)? 36 :
|
109 |
|
|
0;
|
110 |
|
|
int floppy_total_sectors =
|
111 |
|
|
(floppy_is_160k)? 320 :
|
112 |
|
|
(floppy_is_180k)? 360 :
|
113 |
|
|
(floppy_is_320k)? 640 :
|
114 |
|
|
(floppy_is_360k)? 720 :
|
115 |
|
|
(floppy_is_720k)? 1440 :
|
116 |
|
|
(floppy_is_1_2m)? 2400 :
|
117 |
|
|
(floppy_is_1_44m)? 2880 :
|
118 |
|
|
(floppy_is_2_88m)? 5760 :
|
119 |
|
|
0;
|
120 |
|
|
int floppy_heads = (floppy_is_160k || floppy_is_180k)? 1 : 2;
|
121 |
|
|
|
122 |
|
|
int floppy_wait_cycles = 200000000 / floppy_spt;
|
123 |
|
|
|
124 |
|
|
int floppy_media =
|
125 |
|
|
(floppy_index < 0)? 0x20 :
|
126 |
|
|
(floppy_is_160k)? 0x00 :
|
127 |
|
|
(floppy_is_180k)? 0x00 :
|
128 |
|
|
(floppy_is_320k)? 0x00 :
|
129 |
|
|
(floppy_is_360k)? 0x00 :
|
130 |
|
|
(floppy_is_720k)? 0xC0 :
|
131 |
|
|
(floppy_is_1_2m)? 0x00 :
|
132 |
|
|
(floppy_is_1_44m)? 0x80 :
|
133 |
|
|
(floppy_is_2_88m)? 0x40 :
|
134 |
|
|
0x20;
|
135 |
|
|
|
136 |
|
|
for(uint32 i=0; i<13; i++) {
|
137 |
|
|
|
138 |
|
|
top->mgmt_address = 0;
|
139 |
|
|
top->mgmt_write = 1;
|
140 |
|
|
top->mgmt_writedata =
|
141 |
|
|
(i==0)? (floppy_index >= 0? 1 : 0) :
|
142 |
|
|
(i==1)? (floppy_writeprotect? 1 : 0) :
|
143 |
|
|
(i==2)? floppy_cylinders :
|
144 |
|
|
(i==3)? floppy_spt :
|
145 |
|
|
(i==4)? floppy_total_sectors :
|
146 |
|
|
(i==5)? floppy_heads :
|
147 |
|
|
(i==6)? floppy_sd_base :
|
148 |
|
|
(i==7)? 10000 : //floppy_wait_cycles
|
149 |
|
|
(i==8)? 1000 : //wait rate 0: 1000us
|
150 |
|
|
(i==9)? 1666 : //wait rate 1: 1666us
|
151 |
|
|
(i==10)? 2000 : //wait rate 2: 2000us
|
152 |
|
|
(i==11)? 500 : //wait rate 3 : 500us
|
153 |
|
|
floppy_media;
|
154 |
|
|
top->clk = 0;
|
155 |
|
|
top->eval();
|
156 |
|
|
if(dump) tracer->dump(cycle++);
|
157 |
|
|
|
158 |
|
|
top->clk = 1;
|
159 |
|
|
top->eval();
|
160 |
|
|
if(dump) tracer->dump(cycle++);
|
161 |
|
|
|
162 |
|
|
tracer->flush();
|
163 |
|
|
}
|
164 |
|
|
top->mgmt_write = 0;
|
165 |
|
|
|
166 |
|
|
printf("floppy main_plugin.cpp\n");
|
167 |
|
|
while(!Verilated::gotFinish()) {
|
168 |
|
|
|
169 |
|
|
//----------------------------------------------------------------------
|
170 |
|
|
|
171 |
|
|
/*
|
172 |
|
|
uint32 combined.io_address;
|
173 |
|
|
uint32 combined.io_data;
|
174 |
|
|
uint32 combined.io_byteenable;
|
175 |
|
|
uint32 combined.io_is_write;
|
176 |
|
|
step_t combined.io_step;
|
177 |
|
|
|
178 |
|
|
//avalon slave 3f0-3f8
|
179 |
|
|
input [2:0] io_address,
|
180 |
|
|
input io_read,
|
181 |
|
|
output reg [7:0] io_readdata,
|
182 |
|
|
input io_write,
|
183 |
|
|
input [7:0] io_writedata,
|
184 |
|
|
*/
|
185 |
|
|
|
186 |
|
|
top->io_read = 0;
|
187 |
|
|
top->io_write = 0;
|
188 |
|
|
|
189 |
|
|
if(shared_ptr->combined.io_step == STEP_REQ && shared_ptr->combined.io_is_write && (shared_ptr->combined.io_address == 0x03F0 || shared_ptr->combined.io_address == 0x03F4) &&
|
190 |
|
|
(shared_ptr->combined.io_address == 0x03F0 || ((shared_ptr->combined.io_byteenable >> 2) & 1) == 0))
|
191 |
|
|
{
|
192 |
|
|
if(shared_ptr->combined.io_byteenable != 1 && shared_ptr->combined.io_byteenable != 2 && shared_ptr->combined.io_byteenable != 4 && shared_ptr->combined.io_byteenable != 8) {
|
193 |
|
|
printf("Vfloppy: combined.io_byteenable invalid: %x\n", shared_ptr->combined.io_byteenable);
|
194 |
|
|
exit(-1);
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
top->io_address = (shared_ptr->combined.io_address == 0x03F0 && shared_ptr->combined.io_byteenable == 1)? 0 :
|
198 |
|
|
(shared_ptr->combined.io_address == 0x03F0 && shared_ptr->combined.io_byteenable == 2)? 1 :
|
199 |
|
|
(shared_ptr->combined.io_address == 0x03F0 && shared_ptr->combined.io_byteenable == 4)? 2 :
|
200 |
|
|
(shared_ptr->combined.io_address == 0x03F0 && shared_ptr->combined.io_byteenable == 8)? 3 :
|
201 |
|
|
(shared_ptr->combined.io_address == 0x03F4 && shared_ptr->combined.io_byteenable == 1)? 4 :
|
202 |
|
|
(shared_ptr->combined.io_address == 0x03F4 && shared_ptr->combined.io_byteenable == 2)? 5 :
|
203 |
|
|
(shared_ptr->combined.io_address == 0x03F4 && shared_ptr->combined.io_byteenable == 4)? 6 :
|
204 |
|
|
7;
|
205 |
|
|
top->io_writedata = (shared_ptr->combined.io_byteenable == 1)? shared_ptr->combined.io_data & 0xFF :
|
206 |
|
|
(shared_ptr->combined.io_byteenable == 2)? (shared_ptr->combined.io_data >> 8) & 0xFF :
|
207 |
|
|
(shared_ptr->combined.io_byteenable == 4)? (shared_ptr->combined.io_data >> 16) & 0xFF :
|
208 |
|
|
(shared_ptr->combined.io_data >> 24) & 0xFF;
|
209 |
|
|
top->io_read = 0;
|
210 |
|
|
top->io_write = 1;
|
211 |
|
|
|
212 |
|
|
shared_ptr->combined.io_step = STEP_ACK;
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
if(read_cycle == false) {
|
216 |
|
|
if(shared_ptr->combined.io_step == STEP_REQ && shared_ptr->combined.io_is_write == 0 && (shared_ptr->combined.io_address == 0x03F0 || shared_ptr->combined.io_address == 0x03F4) &&
|
217 |
|
|
(shared_ptr->combined.io_address == 0x03F0 || ((shared_ptr->combined.io_byteenable >> 2) & 1) == 0))
|
218 |
|
|
{
|
219 |
|
|
if(shared_ptr->combined.io_byteenable != 1 && shared_ptr->combined.io_byteenable != 2 && shared_ptr->combined.io_byteenable != 4 && shared_ptr->combined.io_byteenable != 8) {
|
220 |
|
|
printf("Vfloppy: combined.io_byteenable invalid: %x\n", shared_ptr->combined.io_byteenable);
|
221 |
|
|
exit(-1);
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
top->io_address= (shared_ptr->combined.io_address == 0x03F0 && shared_ptr->combined.io_byteenable == 1)? 0 :
|
225 |
|
|
(shared_ptr->combined.io_address == 0x03F0 && shared_ptr->combined.io_byteenable == 2)? 1 :
|
226 |
|
|
(shared_ptr->combined.io_address == 0x03F0 && shared_ptr->combined.io_byteenable == 4)? 2 :
|
227 |
|
|
(shared_ptr->combined.io_address == 0x03F0 && shared_ptr->combined.io_byteenable == 8)? 3 :
|
228 |
|
|
(shared_ptr->combined.io_address == 0x03F4 && shared_ptr->combined.io_byteenable == 1)? 4 :
|
229 |
|
|
(shared_ptr->combined.io_address == 0x03F4 && shared_ptr->combined.io_byteenable == 2)? 5 :
|
230 |
|
|
(shared_ptr->combined.io_address == 0x03F4 && shared_ptr->combined.io_byteenable == 4)? 6 :
|
231 |
|
|
7;
|
232 |
|
|
top->io_writedata = 0;
|
233 |
|
|
|
234 |
|
|
top->io_read = 1;
|
235 |
|
|
top->io_write = 0;
|
236 |
|
|
|
237 |
|
|
read_cycle = true;
|
238 |
|
|
}
|
239 |
|
|
}
|
240 |
|
|
else {
|
241 |
|
|
if(shared_ptr->combined.io_step == STEP_REQ && shared_ptr->combined.io_is_write == 0 && (shared_ptr->combined.io_address == 0x03F0 || shared_ptr->combined.io_address == 0x03F4) &&
|
242 |
|
|
(shared_ptr->combined.io_address == 0x03F0 || ((shared_ptr->combined.io_byteenable >> 2) & 1) == 0))
|
243 |
|
|
{
|
244 |
|
|
uint32 val = top->io_readdata;
|
245 |
|
|
|
246 |
|
|
if(shared_ptr->combined.io_byteenable & 1) val <<= 0;
|
247 |
|
|
if(shared_ptr->combined.io_byteenable & 2) val <<= 8;
|
248 |
|
|
if(shared_ptr->combined.io_byteenable & 4) val <<= 16;
|
249 |
|
|
if(shared_ptr->combined.io_byteenable & 8) val <<= 24;
|
250 |
|
|
|
251 |
|
|
shared_ptr->combined.io_data = val;
|
252 |
|
|
|
253 |
|
|
read_cycle = false;
|
254 |
|
|
shared_ptr->combined.io_step = STEP_ACK;
|
255 |
|
|
}
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
//----------------------------------------------------------------------
|
259 |
|
|
|
260 |
|
|
if(top->irq) {
|
261 |
|
|
if(shared_ptr->floppy_irq_step == STEP_IDLE) printf("floppy IRQ raise\n");
|
262 |
|
|
shared_ptr->floppy_irq_step = STEP_REQ;
|
263 |
|
|
}
|
264 |
|
|
else {
|
265 |
|
|
if(shared_ptr->floppy_irq_step == STEP_REQ) printf("floppy IRQ lower\n");
|
266 |
|
|
shared_ptr->floppy_irq_step = STEP_IDLE;
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
//----------------------------------------------------------------------
|
270 |
|
|
|
271 |
|
|
top->dma_floppy_ack = 0;
|
272 |
|
|
|
273 |
|
|
if(top->dma_floppy_req != 0) {
|
274 |
|
|
printf("Error: dma_floppy_req : %d\n", top->dma_floppy_req);
|
275 |
|
|
exit(-1);
|
276 |
|
|
}
|
277 |
|
|
|
278 |
|
|
if(top->ide_3f6_read != 0) {
|
279 |
|
|
printf("Error: ide_3f6_read : %d\n", top->ide_3f6_read);
|
280 |
|
|
exit(-1);
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
if(top->ide_3f6_write != 0) {
|
284 |
|
|
printf("Error: ide_3f6_write : %d\n", top->ide_3f6_write);
|
285 |
|
|
exit(-1);
|
286 |
|
|
}
|
287 |
|
|
|
288 |
|
|
if(top->sd_master_read != 0) {
|
289 |
|
|
printf("Error: sd_master_read : %d\n", top->sd_master_read);
|
290 |
|
|
exit(-1);
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
if(top->sd_master_write != 0) {
|
294 |
|
|
printf("Error: sd_master_write : %d\n", top->sd_master_write);
|
295 |
|
|
exit(-1);
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
//----------------------------------------------------------------------
|
299 |
|
|
|
300 |
|
|
top->clk = 0;
|
301 |
|
|
top->eval();
|
302 |
|
|
if(dump) tracer->dump(cycle++);
|
303 |
|
|
|
304 |
|
|
top->clk = 1;
|
305 |
|
|
top->eval();
|
306 |
|
|
if(dump) tracer->dump(cycle++);
|
307 |
|
|
|
308 |
|
|
tracer->flush();
|
309 |
|
|
|
310 |
|
|
usleep(1);
|
311 |
|
|
}
|
312 |
|
|
tracer->close();
|
313 |
|
|
delete tracer;
|
314 |
|
|
delete top;
|
315 |
|
|
|
316 |
|
|
return 0;
|
317 |
|
|
}
|
318 |
|
|
|
319 |
|
|
//------------------------------------------------------------------------------
|
320 |
|
|
|
321 |
|
|
/*
|
322 |
|
|
module floppy(
|
323 |
|
|
input clk,
|
324 |
|
|
input rst_n,
|
325 |
|
|
|
326 |
|
|
//dma
|
327 |
|
|
output dma_floppy_req,
|
328 |
|
|
input dma_floppy_ack,
|
329 |
|
|
input dma_floppy_terminal,
|
330 |
|
|
input [7:0] dma_floppy_readdata,
|
331 |
|
|
output [7:0] dma_floppy_writedata,
|
332 |
|
|
|
333 |
|
|
//irq
|
334 |
|
|
output reg irq,
|
335 |
|
|
|
336 |
|
|
//avalon slave
|
337 |
|
|
input [2:0] io_address,
|
338 |
|
|
input io_read,
|
339 |
|
|
output reg [7:0] io_readdata,
|
340 |
|
|
input io_write,
|
341 |
|
|
input [7:0] io_writedata,
|
342 |
|
|
|
343 |
|
|
//ide shared port 0x3F6
|
344 |
|
|
output ide_3f6_read,
|
345 |
|
|
input [7:0] ide_3f6_readdata,
|
346 |
|
|
output ide_3f6_write,
|
347 |
|
|
output [7:0] ide_3f6_writedata,
|
348 |
|
|
|
349 |
|
|
//master to control sd
|
350 |
|
|
output [31:0] sd_master_address,
|
351 |
|
|
input sd_master_waitrequest,
|
352 |
|
|
output sd_master_read,
|
353 |
|
|
input sd_master_readdatavalid,
|
354 |
|
|
input [31:0] sd_master_readdata,
|
355 |
|
|
output sd_master_write,
|
356 |
|
|
output [31:0] sd_master_writedata,
|
357 |
|
|
|
358 |
|
|
//slave for sd
|
359 |
|
|
input [8:0] sd_slave_address,
|
360 |
|
|
input sd_slave_read,
|
361 |
|
|
output reg [7:0] sd_slave_readdata,
|
362 |
|
|
input sd_slave_write,
|
363 |
|
|
input [7:0] sd_slave_writedata,
|
364 |
|
|
|
365 |
|
|
//slave for management
|
366 |
|
|
|
367 |
|
|
0x00.[0]: media present
|
368 |
|
|
0x01.[0]: media writeprotect
|
369 |
|
|
0x02.[7:0]: media cylinders
|
370 |
|
|
0x03.[7:0]: media sectors per track
|
371 |
|
|
0x04.[31:0]: media total sector count
|
372 |
|
|
0x05.[1:0]: media heads
|
373 |
|
|
0x06.[31:0]: media sd base
|
374 |
|
|
0x07.[15:0]: media wait cycles: 200000 us / spt
|
375 |
|
|
0x08.[15:0]: media wait rate 0: 1000 us
|
376 |
|
|
0x09.[15:0]: media wait rate 1: 1666 us
|
377 |
|
|
0x0A.[15:0]: media wait rate 2: 2000 us
|
378 |
|
|
0x0B.[15:0]: media wait rate 3: 500 us
|
379 |
|
|
0x0C.[7:0]: media type: 8'h20 none; 8'h00 old; 8'hC0 720k; 8'h80 1_44M; 8'h40 2_88M
|
380 |
|
|
|
381 |
|
|
input [3:0] mgmt_address,
|
382 |
|
|
input mgmt_write,
|
383 |
|
|
input [31:0] mgmt_writedata
|
384 |
|
|
);
|
385 |
|
|
*/
|