-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdisk.sv
660 lines (589 loc) · 15.1 KB
/
disk.sv
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
module disk
(
input clk_sys,
input ce_bus,
input reset,
input reset_full,
input disk_rom,
input bk0010,
output [15:0] ext_mode,
output reg reset_req,
output reg bk0010_stub,
input SPI_SCK,
input SPI_SS2,
input SPI_DI,
input [15:0] bus_din,
input [15:0] bus_addr,
input bus_sync,
input bus_we,
input [1:0] bus_wtbt,
input bus_stb,
output bus_ack,
output dsk_copy,
output dsk_copy_virt,
output [24:0] dsk_copy_addr,
input [15:0] dsk_copy_din,
output [15:0] dsk_copy_dout,
output dsk_copy_we,
output dsk_copy_rd,
output [31:0] sd_lba,
output reg sd_rd,
output reg sd_wr,
input sd_ack,
input sd_ack_conf,
input [8:0] sd_buff_addr,
input [7:0] sd_buff_dout,
output [7:0] sd_buff_din,
input sd_buff_wr,
output sd_conf,
output sd_sdhc,
input sd_mounted
);
assign sd_conf = 1'b0;
assign sd_sdhc = 1'b1;
assign sd_lba = conf ? 0 : lba;
reg [7:0] hdd_hdr[4:0];
wire [31:0] hdd_sig = {hdd_hdr[0],hdd_hdr[1],hdd_hdr[2],hdd_hdr[3]};
wire [7:0] hdd_ver = hdd_hdr[4];
wire [31:0] hdr_out;
reg [6:0] hdr_addr;
sector_b2d sector_hdr
(
.clock(clk_sys),
.data(sd_buff_dout),
.wraddress(sd_buff_addr),
.wren(conf & sd_ack & sd_buff_wr),
.rdaddress(hdr_addr),
.q(hdr_out)
);
wire [15:0] bk_ram_out;
sector_b2w sector_rd
(
.clock(clk_sys),
.data(sd_buff_dout),
.wraddress(sd_buff_addr),
.wren(!conf & sd_ack & sd_buff_wr),
.rdaddress(bk_addr),
.q(bk_ram_out)
);
reg [15:0] bk_data_wr;
reg bk_wr;
wire [7:0] sd_ram_out;
sector_w2b sector_wr
(
.clock(clk_sys),
.data(bk_data_wr),
.wraddress(bk_addr),
.wren(bk_wr),
.rdaddress(sd_buff_addr),
.q(sd_buff_din)
);
always @(posedge clk_sys) begin
reg old_wr;
old_wr <= sd_buff_wr;
if(sd_ack & ~old_wr & sd_buff_wr) begin
if(conf && (sd_buff_addr < 5)) hdd_hdr[sd_buff_addr] <= sd_buff_dout;
end
end
assign dsk_copy = ioctl_download | processing;
assign dsk_copy_we = ioctl_download ? ioctl_we : copy_we;
assign dsk_copy_rd = ioctl_download ? 1'b0 : copy_rd;
assign dsk_copy_addr = ioctl_download ? ioctl_addr : copy_addr;
assign dsk_copy_dout = ioctl_download ? ioctl_dout : copy_dout;
assign dsk_copy_virt = ioctl_download ? 1'b0 : copy_virt;
wire ioctl_download;
wire ioctl_we;
wire [24:0] ioctl_addr;
wire [15:0] ioctl_dout;
wire [7:0] ioctl_index;
reg fdd_ready = 0;
reg [24:0] fdd_size = 0;
reg [15:0] tape_addr;
reg [15:0] tape_len;
always @(posedge clk_sys) begin
reg old_we;
old_we <= ioctl_we;
if(~old_we & ioctl_we) begin
if(ioctl_addr == 25'h100000) tape_addr <= {ioctl_dout[15:1], 1'b0};
if(ioctl_addr == 25'h100002) tape_len <= (ioctl_dout+1'd1) & ~16'd1;
end
end
always @(posedge clk_sys) begin
reg old_download;
reg in_range;
old_download <= ioctl_download;
if(!old_download & ioctl_download & bk0010 & (ioctl_index == 1)) reset_req <=1;
if(old_download & !ioctl_download) begin
reset_req <= 0;
case(ioctl_index)
1: begin
in_range <= 0;
bk0010_stub <= bk0010;
end
'h41: begin
fdd_ready <= 1;
fdd_size <= ioctl_addr - 25'h120000;
end
endcase
end
if(reset_full) fdd_ready <= 0;
if(bus_addr[15:13] == 3'b101) in_range <=1;
if(in_range & (bus_addr[15:13] < 3'b101) & bk0010_stub) bk0010_stub <=0;
end
data_io data_io (.*);
//Allow write for stop/start disk motor and extended memory mode.
wire sel130 = bus_sync && (bus_addr[15:1] == (16'o177130 >> 1)) && bus_wtbt[0];
wire sel130w = sel130 && bus_we;
wire sel130r = sel130 && !bus_we && !(bk0010 && mode130[2]);
assign ext_mode = mode130;
reg [15:0] mode130;
reg mode130_strobe = 1'b0;
always @(posedge clk_sys) begin
reg old_stb;
old_stb <= bus_stb;
if(reset) begin
mode130_strobe <= 1'b0;
mode130 <= bk0010 ? 16'o160 : 16'o140;
end else if(!old_stb & bus_stb & sel130w) begin
mode130[3:2] <= bus_din[3:2];
if(mode130_strobe) begin
mode130[6:4] <= bus_din[6:4];
mode130[11:8] <= {bus_din[0], bus_din[3], bus_din[2], bus_din[10]};
mode130_strobe <= 1'b0;
end else begin
mode130_strobe <= (bus_din == 16'o6);
end
end;
end
//LBA access. Main access for Floppy and HDD read/write.
wire sel132 = bus_we && bus_sync && (bus_addr[15:1] == (16'o177132 >> 1));
//CHS access. Currently not supported and always returns error.
//Paramaters can be recalculated for LBA call, but none of apps
//used CHS access with exception of specific floppy utilities.
wire sel134 = bus_we && bus_sync && (bus_addr[15:1] == (16'o177134 >> 1));
//BIN loader
wire sel670 = bus_we && bus_sync && bk0010_stub && (bus_addr[15:1] == (16'o177670 >> 1));
wire stb132 = bus_stb && sel132;
wire stb134 = bus_stb && sel134;
wire stb670 = bus_stb && sel670;
wire valid = (disk_rom & (sel130w | sel130r | sel132 | sel134)) | sel670;
assign bus_ack = bus_sync & bus_stb & valid;
wire reg_access = (disk_rom & (stb132 | stb134)) | stb670;
reg [24:0] copy_addr;
reg [15:0] copy_dout;
wire [15:0] copy_din = dsk_copy_din;
reg copy_virt;
reg copy_we;
reg copy_rd;
reg [7:0] bk_addr;
reg [31:0] lba;
reg conf = 0;
reg processing = 0;
typedef enum
{
ST_R, ST_R2, ST_R3, ST_R4,
ST_W, ST_W2, ST_W3, ST_W4,
ST_CP_R2V, ST_CP_V2R,
ST_CP, ST_CP2,
ST_PAR, ST_PAR2, ST_PAR3, ST_PAR4, ST_PAR5, ST_PAR6, ST_PAR7, ST_PAR8,
ST_HR, ST_HR2, ST_HR3, ST_HR4, ST_HR5,
ST_HW, ST_HW2, ST_HW3, ST_HW4, ST_HW5, ST_HW6, ST_HW7, ST_HW8, ST_HW9,
ST_FR,
ST_BIN, ST_BIN2, ST_BIN3, ST_BIN4, ST_BIN5,
ST_RES, ST_RES2, ST_RES3,
ST_RES_OK
} io_state_t;
always @(posedge clk_sys) begin
reg old_access, old_mounted, old_reset;
io_state_t io_state, io_cp_ret, io_rw_ret;
reg [5:0] ack;
reg io_busy = 0;
reg mounted = 0;
reg [1:0] cp_virt;
reg [24:0] addr_r, addr_w, cp_len;
reg [15:0] SP, PSW, vaddr, error, total_size, part_size;
reg [31:0] hdd_start, hdd_end;
reg [7:0] disk;
reg write;
if(ce_bus) begin
old_access <= reg_access;
if(!old_access && reg_access) begin
processing <= 1;
io_state <= stb670 ? ST_BIN : ST_PAR;
SP <= bus_din;
copy_rd <= 0;
copy_we <= 0;
bk_wr <= 0;
sd_wr <= 0;
sd_rd <= 0;
end
old_reset <= reset;
if(!old_reset && reset) begin
processing <= 0;
io_busy <= 0;
sd_wr <= 0;
sd_rd <= 0;
copy_rd <= 0;
copy_we <= 0;
end
ack <= {sd_ack, ack[5:1]};
if(ack[0] && !ack[1]) begin
if(conf) begin
mounted <= ((hdd_sig == "BKHD") && (hdd_ver == 1));
conf <= 1'b0;
end
io_busy <= 0;
end
if(!ack[0] && ack[1]) begin
sd_wr <= 0;
sd_rd <= 0;
end
if(processing) begin
case(io_state)
ST_R:
begin
copy_addr <= addr_r;
copy_rd <= 0;
io_state <= io_state.next();
end
ST_R2,
ST_R3:
begin
copy_rd <= 1;
io_state <= io_state.next();
end
ST_R4:
begin
copy_rd <= 0;
addr_r <= addr_r + 2'd2;
io_state <= io_rw_ret;
io_rw_ret <= io_rw_ret.next();
end
ST_W:
begin
copy_addr <= addr_w;
copy_we <= 0;
io_state <= io_state.next();
end
ST_W2,
ST_W3:
begin
copy_we <= 1;
io_state <= io_state.next();
end
ST_W4:
begin
copy_we <= 0;
addr_w <= addr_w + 2'd2;
io_state <= io_rw_ret;
io_rw_ret <= io_rw_ret.next();
end
ST_CP_R2V:
begin
cp_virt <= 2'b01;
io_state <= ST_CP;
end
ST_CP_V2R:
begin
cp_virt <= 2'b10;
io_state <= ST_CP;
end
ST_CP:
begin
if(!cp_len) io_state <= io_cp_ret;
else begin
io_state <= ST_R;
io_rw_ret<= io_state.next();
end
cp_len <= cp_len - 1'd1;
copy_virt <= cp_virt[1];
end
ST_CP2:
begin
copy_virt <= cp_virt[0];
copy_dout <= copy_din;
io_state <= ST_W;
io_rw_ret <= ST_CP;
end
ST_PAR:
begin
copy_virt <= 1;
addr_r <= SP+16'd6;
io_state <= ST_R;
io_rw_ret <= io_state.next();
end
ST_PAR2:
begin
// R3 - address of paramters
addr_r <= copy_din + 16'o34;
io_state <= ST_R;
end
ST_PAR3:
begin
// 34(R3) - Disk number
disk <= copy_din[7:0];
hdr_addr <= 7'd2 + copy_din[6:0];
addr_r <= SP;
io_state <= ST_R;
end
ST_PAR4:
begin
// R0 - start block
lba <= disk ? hdr_out + copy_din : copy_din;
hdd_start <= hdr_out;
io_state <= ST_R;
end
ST_PAR5:
begin
// R1 - length
write <= copy_din[15];
total_size<= copy_din[15] ? (~copy_din[15:0])+1'd1 : copy_din[15:0];
hdr_addr <= hdr_addr + 1'd1;
io_state <= ST_R;
end
ST_PAR6:
begin
// R2 - address of buffer
vaddr <= copy_din;
hdd_end <= hdr_out;
addr_r <= SP+16'd8;
io_state <= ST_R;
end
ST_PAR7:
begin
// PSW to return the status
PSW <= copy_din;
addr_r <= 16'o52;
io_state <= ST_R;
end
ST_PAR8:
begin
error <= copy_din;
addr_w <= vaddr;
addr_r <= vaddr;
if(disk) begin
//VHD access
if((bus_addr != 16'o177132) || !mounted || !hdd_end || !hdd_start || (disk >= 125)) begin
error[7:0] <= 6;
PSW[0] <= 1;
io_state <= ST_RES;
end else if(!total_size || vaddr[0]) begin
error[7:0] <= 10;
PSW[0] <= 1;
io_state <= ST_RES;
end else if((lba+((total_size+255) >> 8)) > hdd_end) begin
error[7:0] <= 5;
PSW[0] <= 1;
io_state <= ST_RES;
end else
if(write) io_state <= ST_HW; // write
else io_state <= ST_HR; // read
end else begin
//DSK access
if((bus_addr != 16'o177132) || write || !fdd_ready || !fdd_size) begin
error[7:0] <= 6;
PSW[0] <= 1;
io_state <= ST_RES;
end else if(!total_size || vaddr[0]) begin
error[7:0] <= 10;
PSW[0] <= 1;
io_state <= ST_RES;
end else if((lba+((total_size+255) >> 8)) > (fdd_size >> 9)) begin
error[7:0] <= 5;
PSW[0] <= 1;
io_state <= ST_RES;
end else begin
lba <= (lba << 9) + 32'h120000;
io_state <= ST_FR; // read
end
end
end
// Floppy read
ST_FR:
begin
addr_r <= lba[24:0];
cp_len <= total_size;
io_state <= ST_CP_R2V;
io_cp_ret <= ST_RES_OK;
end
// HDD read
ST_HR:
begin
if(!io_busy) begin
bk_wr <= 0;
sd_rd <= 1;
io_busy <= 1;
part_size <= (total_size < 16'd256) ? total_size : 16'd256;
io_state <= io_state.next();
end
end
ST_HR2:
begin
if(!io_busy) begin
bk_addr <= 0;
total_size<= total_size - part_size;
io_state <= io_state.next();
end
end
ST_HR3:
begin
copy_virt <= 1;
copy_dout <= bk_ram_out;
io_state <= ST_W;
io_rw_ret <= io_state.next();
end
ST_HR4:
begin
bk_addr <= bk_addr + 2'd1;
part_size <= part_size - 2'd1;
io_state <= io_state.next();
end
ST_HR5:
begin
if(part_size != 0) io_state <= ST_HR3;
else if(total_size != 0) begin
lba <= lba + 1;
io_state <= ST_HR;
end else begin
io_state <= ST_RES_OK;
end
end
// HDD write
ST_HW:
begin
if(!io_busy) begin
bk_wr <= 0;
bk_addr <= 0;
part_size <= (total_size < 16'd256) ? total_size : 16'd256;
io_state <= io_state.next();
end
end
ST_HW2:
begin
total_size <= total_size - part_size;
io_state <= io_state.next();
end
ST_HW3:
begin
copy_virt <= 1;
bk_wr <= 0;
io_state <= ST_R;
io_rw_ret <= io_state.next();
end
ST_HW4:
begin
bk_data_wr <= copy_din;
part_size <= part_size - 2'd1;
io_state <= io_state.next();
end
ST_HW5: begin
bk_wr <= 1;
io_state <= io_state.next();
end
ST_HW6: begin
bk_wr <= 0;
io_state <= io_state.next();
end
ST_HW7: begin
bk_addr <= bk_addr + 2'd1;
io_state <= io_state.next();
if(part_size != 0) io_state <= ST_HW3;
end
ST_HW8:
begin
if(!io_busy) begin
sd_wr <= 1;
io_busy <= 1;
io_state <= io_state.next();
end
end
ST_HW9:
begin
if(!io_busy) begin
if(total_size != 0) begin
lba <= lba + 1;
io_state <= ST_HW;
end else begin
io_state <= ST_RES_OK;
end
end
end
// Successful result.
ST_RES_OK:
begin
error[7:0] <= 0;
PSW[0] <= 0;
io_state <= ST_RES;
end
// Finish. Post the exit code.
ST_RES:
begin
copy_virt <= 1;
copy_dout <= error;
addr_w <= 16'o52;
io_state <= ST_W;
io_rw_ret <= io_state.next();
end
ST_RES2:
begin
copy_dout <= PSW;
addr_w <= SP+16'd8;
io_state <= ST_W;
end
ST_RES3:
begin
processing <= 0;
end
// BIN copy
ST_BIN:
begin
// replace return address in stack
copy_virt <= 1;
copy_dout <= tape_addr;
addr_w <= SP;
io_state <= ST_W;
io_rw_ret <= io_state.next();
end
ST_BIN2:
begin
// start address after EMT 36
copy_dout <= tape_addr;
addr_w <= 16'o264;
io_state <= ST_W;
end
ST_BIN3:
begin
// length after EMT 36
copy_dout <= tape_len;
io_state <= ST_W;
end
ST_BIN4:
begin
addr_r <= 25'h100004;
addr_w <= tape_addr;
cp_len <= tape_len[15:1];
io_state <= ST_CP_R2V;
io_cp_ret <= io_state.next();
end
ST_BIN5:
begin
processing <= 0;
end
endcase
end
end
if(!io_busy) begin
if(!old_mounted && sd_mounted) begin
mounted <= 0;
conf <= 1;
sd_rd <= 1;
io_busy <= 1;
processing <= 0; // can brake on-going IO, but nothing can be done.
sd_wr <= 0;
copy_rd <= 0;
copy_we <= 0;
end
old_mounted <= sd_mounted;
end
end
endmodule