OpenCores
URL https://opencores.org/ocsvn/aoocs/aoocs/trunk

Subversion Repositories aoocs

[/] [aoocs/] [trunk/] [rtl/] [ocs_floppy.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
/*
2
 * Copyright 2010, Aleksander Osman, alfik@poczta.fm. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or without modification, are
5
 * permitted provided that the following conditions are met:
6
 *
7
 *  1. Redistributions of source code must retain the above copyright notice, this list of
8
 *     conditions and the following disclaimer.
9
 *
10
 *  2. Redistributions in binary form must reproduce the above copyright notice, this list
11
 *     of conditions and the following disclaimer in the documentation and/or other materials
12
 *     provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
15
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
17
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
 */
24
 
25
/*! \file
26
 * \brief OCS floppy implementation with WISHBONE master and slave interface.
27
 */
28
 
29
/*! \brief \copybrief ocs_floppy.v
30
 
31
List of floppy registers:
32
\verbatim
33
Implemented:
34
     [SERDATR   *018  R   P       Serial port data and status read              read not implemented here]
35
    DSKBYTR     *01A  R   P       Disk data byte and status read                read not implemented here
36
 
37
    DSKPTH    + *020  W   A( E )  Disk pointer (high 3 bits, 5 bits if ECS)
38
    DSKPTL    + *022  W   A       Disk pointer (low 15 bits)
39
    DSKLEN      *024  W   P       Disk length
40
    DSKDAT    & *026  W   P       Disk DMA data write
41
 
42
        [not used 07C]
43
    DSKSYNC     ~07E  W   P       Disk sync pattern register for disk read
44
 
45
Not implemented:
46
    DSKDATR   & *008  ER  P       Disk data early read (dummy address)          not implemented
47
     [JOY0DAT   *00A  R   D       Joystick-mouse 0 data (vert,horiz)            read not implemented here]
48
\endverbatim
49
*/
50
module ocs_floppy(
51
    //% \name Clock and reset
52
    //% @{
53
    input               CLK_I,
54
    input               reset_n,
55
    //% @}
56
 
57
    //% \name On-Screen-Display floppy management interface
58
    //% @{
59
    input               floppy_inserted,
60
    input [31:0]        floppy_sector,
61
    input               floppy_write_enabled,
62
    output reg          floppy_error,
63
    //% @}
64
 
65
    //% \name WISHBONE master
66
    //% @{
67
    output reg          CYC_O,
68
    output reg          STB_O,
69
    output reg          WE_O,
70
    output reg [31:2]   ADR_O,
71
    output reg [3:0]    SEL_O,
72
    output reg [31:0]   master_DAT_O,
73
    input [31:0]        master_DAT_I,
74
    input               ACK_I,
75
    //% @}
76
 
77
    //% \name WISHBONE slave for OCS registers
78
    //% @{
79
    input               CYC_I,
80
    input               STB_I,
81
    input               WE_I,
82
    input [8:2]         ADR_I,
83
    input [3:0]         SEL_I,
84
    input [31:0]        slave_DAT_I,
85
    output reg          ACK_O,
86
    //% @}
87
 
88
    //% \name WISHBONE slave for floppy buffer
89
    //% @{
90
    input               buffer_CYC_I,
91
    input               buffer_STB_I,
92
    input               buffer_WE_I,
93
    input [13:2]        buffer_ADR_I,
94
    input [3:0]         buffer_SEL_I,
95
    input [31:0]        buffer_DAT_I,
96
    output [31:0]       buffer_DAT_O,
97
    output reg          buffer_ACK_O,
98
    //% @}
99
 
100
    //% \name Not aligned register access on a 32-bit WISHBONE bus
101
    //% @{
102
        // DSKBYTR read not implemented here
103
    input               na_dskbytr_read,
104
    output [15:0]       na_dskbytr,
105
    //% @}
106
 
107
    //% \name Internal OCS ports
108
    //% @{
109
    input               line_start,
110
 
111
    input [10:0]        dma_con,
112
    input [14:0]        adk_con,
113
 
114
    output reg          floppy_syn_irq,
115
    output reg          floppy_blk_irq,
116
    //% @}
117
 
118
    //% \name Floppy CIA interface
119
    //% @{
120
    output              fl_rdy_n,
121
    output              fl_tk0_n,
122
    output              fl_wpro_n,
123
    output              fl_chng_n,
124
    output              fl_index_n,
125
 
126
    input               fl_mtr_n,
127
    input [3:0]         fl_sel_n,
128
    input               fl_side_n,
129
    input               fl_dir,
130
    input               fl_step_n,
131
    //% @}
132
 
133
    //% \name Debug signals
134
    //% @{
135
    output [7:0]        debug_floppy,
136
    output [7:0]        debug_track
137
    //% @}
138
);
139
 
140
reg [33:0]  mfm_encoder;
141
reg [31:0]  checksum;
142
reg [3:0]   sector;
143
 
144
reg         last_checksum_bit;
145
reg [31:0]  first_long_word;
146
 
147
wire [63:0] mfm_output = {
148
    ~mfm_encoder[33] & ~mfm_encoder[31], mfm_encoder[31], ~mfm_encoder[31] & ~mfm_encoder[29], mfm_encoder[29],
149
    ~mfm_encoder[29] & ~mfm_encoder[27], mfm_encoder[27], ~mfm_encoder[27] & ~mfm_encoder[25], mfm_encoder[25],
150
    ~mfm_encoder[25] & ~mfm_encoder[23], mfm_encoder[23], ~mfm_encoder[23] & ~mfm_encoder[21], mfm_encoder[21],
151
    ~mfm_encoder[21] & ~mfm_encoder[19], mfm_encoder[19], ~mfm_encoder[19] & ~mfm_encoder[17], mfm_encoder[17],
152
    ~mfm_encoder[17] & ~mfm_encoder[15], mfm_encoder[15], ~mfm_encoder[15] & ~mfm_encoder[13], mfm_encoder[13],
153
    ~mfm_encoder[13] & ~mfm_encoder[11], mfm_encoder[11], ~mfm_encoder[11] & ~mfm_encoder[9],  mfm_encoder[9],
154
    ~mfm_encoder[9]  & ~mfm_encoder[7],  mfm_encoder[7],  ~mfm_encoder[7]  & ~mfm_encoder[5],  mfm_encoder[5],
155
    ~mfm_encoder[5]  & ~mfm_encoder[3],  mfm_encoder[3],  ~mfm_encoder[3]  & ~mfm_encoder[1],  mfm_encoder[1],
156
    ~mfm_encoder[32] & ~mfm_encoder[30], mfm_encoder[30], ~mfm_encoder[30] & ~mfm_encoder[28], mfm_encoder[28],
157
    ~mfm_encoder[28] & ~mfm_encoder[26], mfm_encoder[26], ~mfm_encoder[26] & ~mfm_encoder[24], mfm_encoder[24],
158
    ~mfm_encoder[24] & ~mfm_encoder[22], mfm_encoder[22], ~mfm_encoder[22] & ~mfm_encoder[20], mfm_encoder[20],
159
    ~mfm_encoder[20] & ~mfm_encoder[18], mfm_encoder[18], ~mfm_encoder[18] & ~mfm_encoder[16], mfm_encoder[16],
160
    ~mfm_encoder[16] & ~mfm_encoder[14], mfm_encoder[14], ~mfm_encoder[14] & ~mfm_encoder[12], mfm_encoder[12],
161
    ~mfm_encoder[12] & ~mfm_encoder[10], mfm_encoder[10], ~mfm_encoder[10] & ~mfm_encoder[8],  mfm_encoder[8],
162
    ~mfm_encoder[8]  & ~mfm_encoder[6],  mfm_encoder[6],  ~mfm_encoder[6]  & ~mfm_encoder[4],  mfm_encoder[4],
163
    ~mfm_encoder[4]  & ~mfm_encoder[2],  mfm_encoder[2],  ~mfm_encoder[2]  & ~mfm_encoder[0],  mfm_encoder[0]
164
};
165
 
166
wire [31:0] header =
167
    { 8'hFF, {track, 1'b0} + {7'b0, ~last_fl_side_n}, {4'b0, sector}, 8'd11 - {4'b0, sector} };
168
 
169
wire [31:0] masked_checksum = checksum & 32'h55555555;
170
 
171
reg [47:0] output_shift;
172
reg [15:0] output_first_word;
173
reg [5:0] output_line_cnt;
174
reg [5:0] last_output_line_cnt;
175
reg [4:0] output_shift_cnt;
176
 
177
wire output_shifting = (last_output_line_cnt != 6'd0);
178
wire output_shifted = (last_dsksync_halt == 1'b0 && last_output_line_cnt != 6'd0 && output_line_cnt == 6'd0);
179
wire output_index = (last_buffer_addr == 12'd3124 && buffer_addr == 12'd0);
180
wire write_sector_ready = (buffer_addr == 12'd127);
181
wire [31:0] output_long_word = { output_first_word, output_shift[47:32] };
182
 
183
//*********************************** FLOPPY BUFFER start
184
reg [11:0] buffer_addr;
185
reg [11:0] last_buffer_addr;
186
reg [8:0] buffer_counter;
187
reg last_buffer_ACK_O;
188
 
189
wire buffer_write_cycle =
190
    (buffer_CYC_I == 1'b1 && buffer_STB_I == 1'b1 && buffer_WE_I == 1'b1 && buffer_SEL_I == 4'b1111 && buffer_ACK_O == 1'b0);
191
wire buffer_read_cycle =
192
    (buffer_CYC_I == 1'b1 && buffer_STB_I == 1'b1 && buffer_WE_I == 1'b0 && buffer_SEL_I == 4'b1111 && buffer_ACK_O == 1'b0);
193
 
194
wire buffer_wren =
195
    (enable_write == 1'b1)?                                                         1'b1 :
196
    (buffer_counter >= 9'd1 && buffer_counter <= 9'd14)?                            1'b1 :
197
    (buffer_counter >= 9'd16 && buffer_counter <= 9'd271 && (buffer_ACK_O == 1'b1 || last_buffer_ACK_O == 1'b1))?  1'b1 :
198
    (buffer_counter >= 9'd272 && buffer_counter <= 9'd275)?                         1'b1 :
199
    1'b0;
200
 
201
wire [31:0] buffer_data =
202
    (enable_write == 1'b1)?                                 master_DAT_O :
203
    (buffer_counter == 9'd1)?                               32'hAAAAAAAA :
204
    (buffer_counter == 9'd2)?                               32'h44894489 :
205
    // insert header, insert header checksum
206
    (buffer_counter == 9'd3 || buffer_counter == 9'd13)?    mfm_output[63:32] :
207
    (buffer_counter == 9'd4 || buffer_counter == 9'd14)?    mfm_output[31:0] :
208
    (buffer_counter == 9'd5)?                               { ~mfm_encoder[0], 1'b0, 2'b10, 28'hAAAAAAA } :
209
    (buffer_counter <= 9'd14)?                              32'hAAAAAAAA :
210
    // insert data, count data checksum
211
    (buffer_counter >= 9'd16 && buffer_counter <= 9'd270 && buffer_counter[0] == 1'b0)?
212
                                                            mfm_output[63:32] :
213
    (buffer_counter >= 9'd17 && buffer_counter <= 9'd271 && buffer_counter[0] == 1'b1)?
214
                                                            mfm_output[31:0] :
215
    // fix first and middle bit
216
    (buffer_counter == 9'd272 || buffer_counter == 9'd274)? mfm_output[63:32] :
217
    // write checksum
218
    (buffer_counter == 9'd273 || buffer_counter == 9'd275)? mfm_output[31:0] :
219
                                                            32'd0;
220
wire [31:0] buffer_q;
221
 
222
altsyncram buffer_ram_inst(
223
    .clock0     (CLK_I),
224
    .address_a  (buffer_addr),
225
    .wren_a     (buffer_wren),
226
    .data_a     (buffer_data),
227
    .q_a        (buffer_q),
228
 
229
    .clock1     (CLK_I),
230
    .address_b  (buffer_ADR_I),
231
    .q_b        (buffer_DAT_O)
232
);
233
defparam
234
    buffer_ram_inst.operation_mode  = "BIDIR_DUAL_PORT",
235
    buffer_ram_inst.width_a         = 32,
236
    buffer_ram_inst.widthad_a       = 12,
237
    buffer_ram_inst.width_b         = 32,
238
    buffer_ram_inst.widthad_b       = 12,
239
    buffer_ram_inst.init_file       = "ocs_floppy.mif";
240
/*
241
    buffer_counter
242
    15->16:     load mfm A, load addr A
243
    16->17:     store A, load addr A+128
244
    17->18:     load mfm A+1, store A+128, load addr A+1
245
    18->19:     store A+1, load addr A+129
246
    ......
247
    269->270:   load mfm A+127, store A+254, load addr A+127
248
    270->271:   store A+127, load addr A+255
249
    271->272:   store A+255
250
*/
251
 
252
always @(posedge CLK_I or negedge reset_n) begin
253
    if(reset_n == 1'b0) begin
254
        buffer_addr             <= 12'd0;
255
        last_buffer_addr        <= 12'd0;
256
        buffer_counter          <= 9'd0;
257
        mfm_encoder             <= 34'd0;
258
        checksum                <= 32'd0;
259
        sector                  <= 4'd0;
260
 
261
        last_checksum_bit       <= 1'b0;
262
        first_long_word         <= 32'd0;
263
 
264
        buffer_ACK_O            <= 1'b0;
265
        last_buffer_ACK_O       <= 1'b0;
266
 
267
        output_shift            <= 48'd0;
268
        output_first_word       <= 16'd0;
269
        output_line_cnt         <= 6'd0;
270
        last_output_line_cnt    <= 6'd0;
271
        output_shift_cnt        <= 5'd0;
272
    end
273
    else begin
274
        last_buffer_ACK_O <= buffer_ACK_O;
275
        if(buffer_write_cycle == 1'b1 || buffer_read_cycle == 1'b1) buffer_ACK_O <= 1'b1;
276
        else                                                        buffer_ACK_O <= 1'b0;
277
 
278
        if(buffer_counter >= 9'd3 && buffer_counter <= 9'd12)
279
            checksum <= checksum ^ buffer_data;
280
        else if(buffer_counter == 9'd14)
281
            checksum <= 32'd0;
282
        else if(buffer_counter >= 9'd16 && buffer_counter <= 9'd271 && buffer_wren == 1'b1)
283
            checksum <= checksum ^ buffer_data;
284
        else if(buffer_counter == 9'd275)
285
            checksum <= 32'd0;
286
 
287
        if(buffer_counter == 9'd14)     last_checksum_bit <= buffer_data[0];
288
        if(buffer_counter == 9'd15)     first_long_word <= buffer_DAT_I;
289
 
290
        if(buffer_counter == 9'd275 && sector < 4'd10)  sector <= sector + 4'd1;
291
        else if(buffer_counter == 9'd275)               sector <= 4'd0;
292
 
293
        if(start_read == 1'b1) begin
294
            buffer_counter <= 9'd1;
295
            buffer_addr <= 12'd0;
296
            checksum <= 32'd0;
297
            sector <= 4'd0;
298
        end
299
 
300
        if(buffer_counter >= 9'd1 && buffer_counter <= 9'd14)
301
            buffer_counter <= buffer_counter + 9'd1;
302
        else if(buffer_counter >= 9'd15 && buffer_counter <= 9'd269 && buffer_counter[0] == 1'b1 && buffer_write_cycle == 1'b1)
303
            buffer_counter <= buffer_counter + 9'd1;
304
        else if(buffer_counter >= 9'd16 && buffer_counter <= 9'd270 && buffer_counter[0] == 1'b0)
305
            buffer_counter <= buffer_counter + 9'd1;
306
        else if(buffer_counter >= 9'd271 && buffer_counter <= 9'd274)
307
            buffer_counter <= buffer_counter + 9'd1;
308
        else if(buffer_counter == 9'd275 && sector < 4'd10)
309
            buffer_counter <= 9'd1;
310
        else if(buffer_counter == 9'd275)
311
            buffer_counter <= 9'd0;
312
 
313
        if(buffer_counter >= 9'd1 && buffer_counter <= 9'd13)
314
            buffer_addr <= buffer_addr + 12'd1;
315
        // skip data checksum
316
        else if(buffer_counter == 9'd14)
317
            buffer_addr <= buffer_addr + 12'd3;
318
        else if(buffer_counter >= 9'd17 && buffer_counter <= 9'd269 && buffer_counter[0] == 1'b1 && buffer_write_cycle == 1'b1)
319
            buffer_addr <= buffer_addr - 12'd128 + 12'd1;
320
        else if(buffer_counter >= 9'd16 && buffer_counter <= 9'd270 && buffer_counter[0] == 1'b0)
321
            buffer_addr <= buffer_addr + 12'd128;
322
        else if(buffer_counter == 9'd271)
323
            buffer_addr <= buffer_addr - 12'd256 + 12'd1;
324
        else if(buffer_counter == 9'd272)
325
            buffer_addr <= buffer_addr + 12'd128;
326
        else if(buffer_counter == 9'd273)
327
            buffer_addr <= buffer_addr - 12'd128 - 12'd2;
328
        else if(buffer_counter == 9'd274)
329
            buffer_addr <= buffer_addr + 12'd1;
330
        else if(buffer_counter == 9'd275 && sector < 4'd10)
331
            buffer_addr <= buffer_addr + 12'd1 + 12'd256;
332
        else if(buffer_counter == 9'd275)
333
            buffer_addr <= 12'd0;
334
 
335
        if(buffer_counter == 9'd2)
336
            mfm_encoder <= { 1'b1, header[1], header[31:0] };
337
        // checksum
338
        else if(buffer_counter == 9'd12)
339
            mfm_encoder <= { 1'b0, masked_checksum[1], masked_checksum[31:0] };
340
        else if(buffer_counter == 9'd15 && buffer_write_cycle == 1'b1)
341
            mfm_encoder <= { 1'b1, 1'b1, buffer_DAT_I };
342
        else if(buffer_counter >= 9'd17 && buffer_counter <= 9'd269 && buffer_counter[0] == 1'b1 && buffer_write_cycle == 1'b1)
343
            mfm_encoder <= { mfm_encoder[1], mfm_encoder[0], buffer_DAT_I};
344
        else if(buffer_counter == 9'd271)
345
            mfm_encoder <= { checksum[0]^buffer_data[0], mfm_encoder[1], first_long_word };
346
        else if(buffer_counter == 9'd273)
347
            mfm_encoder <= { last_checksum_bit, masked_checksum[1], masked_checksum[31:0] };
348
 
349
        last_output_line_cnt <= output_line_cnt;
350
        last_buffer_addr <= buffer_addr;
351
 
352
        if(output_line_cnt == 6'd16) output_first_word[15:0] <= output_shift[47:32];
353
 
354
        //if(output_shifted) $display("%08x ", output_long_word);
355
 
356
        // read mfm data
357
        if(buffer_counter == 9'd0 && start_read == 1'b0 && active_write == 1'b0) begin
358
 
359
            if(output_line_cnt != 6'd0 && dsksync_halt == 1'b0) begin
360
                if(output_shift_cnt == 5'd0)    output_shift <= { output_shift[46:32], buffer_q, 1'b0 };
361
                else                            output_shift <= { output_shift[46:0], 1'b0 };
362
 
363
                if(output_shift_cnt == 5'd0) begin
364
                    if(buffer_addr == 12'd3124) buffer_addr <= 12'd0;
365
                    else                        buffer_addr <= buffer_addr + 12'd1;
366
                end
367
                output_shift_cnt <= output_shift_cnt - 5'd1;
368
            end
369
 
370
            if(output_line_cnt != 6'd0 && dsksync_halt == 1'b1) begin
371
                output_line_cnt <= 6'd0;
372
            end
373
            else if(output_line_cnt != 6'd0) begin
374
                output_line_cnt <= output_line_cnt - 6'd1;
375
            end
376
            else if(line_start == 1'b1) begin
377
                output_line_cnt <= 6'd32;
378
            end
379
        end
380
        // write data
381
        else if(buffer_counter == 9'd0 && start_read == 1'b0 && active_write == 1'b1) begin
382
            if(start_write == 1'b1)         buffer_addr <= 12'd0;
383
            else if(enable_write == 1'b1)   buffer_addr <= buffer_addr + 12'd1;
384
        end
385
        else begin
386
            output_shift_cnt <= 5'd0;
387
            output_line_cnt <= 6'd0;
388
        end
389
 
390
    end
391
end
392
 
393
//*********************************** FLOPPY BUFFER end
394
 
395
assign debug_track = { 1'b0, track };
396
 
397
assign debug_floppy = { last_fl_side_n, adk_con[10], dma_active, state > 4'd7, state };
398
 
399
assign fl_tk0_n     = (fl_sel_n == 4'b1110 && track == 7'd0)? 1'b0 : 1'b1;
400
assign fl_wpro_n    = (floppy_inserted == 1'b1 && fl_sel_n == 4'b1110)? floppy_write_enabled : 1'b1;
401
//id bit = 1 for internal drive
402
assign fl_rdy_n     = (floppy_inserted == 1'b1 && fl_sel_n == 4'b1110 && motor_spinup_delay == 15'd32767)? 1'b0 : 1'b1;
403
assign fl_chng_n    = (fl_sel_n == 4'b1110)? reg_fl_chng_n : 1'b1;
404
assign fl_index_n   = (fl_sel_n == 4'b1110)? reg_fl_index_n : 1'b1;
405
 
406
reg [6:0] track;
407
reg last_fl_step_n;
408
reg floppy_pos_changed;
409
reg [3:0] last_fl_sel_n;
410
reg last_fl_side_n;
411
 
412
reg reg_fl_chng_n;
413
reg reg_fl_index_n;
414
reg reg_fl_mtr_n;
415
 
416
reg [14:0] motor_spinup_delay;
417
 
418
reg [31:0] dskptr;
419
reg [15:0] dsklen;
420
reg [15:0] dsksync;
421
reg last_dma_secondary;
422
reg dma_started;
423
 
424
reg [31:0] mfm_decoder;
425
 
426
wire dma_active;
427
assign dma_active =
428
    last_dma_secondary == 1'b1 && dsklen[15] == 1'b1 && dma_con[9] == 1'b1 && dma_con[4] == 1'b1 && dsklen[13:0] != 14'd0;
429
 
430
reg [5:0] byte_dsksync;
431
reg [2:0] byte_counter;
432
 
433
assign na_dskbytr = { byte_counter != 3'd0, dma_active, dsklen[14], byte_dsksync != 6'd0,
434
    (byte_counter == 3'd1)? output_long_word[31:24] :
435
    (byte_counter == 3'd2)? output_long_word[23:16] :
436
    (byte_counter == 3'd3)? output_long_word[15:8] :
437
    output_long_word[7:0]
438
};
439
 
440
reg [3:0] substate;
441
reg [3:0] last_substate;
442
reg [3:0] state;
443
reg [3:0] last_state;
444
parameter [3:0]
445
    S_IDLE              = 4'd0,
446
    S_READ_FROM_SD      = 4'd1,
447
    S_READ_READY_0      = 4'd2,
448
    S_READ_READY_1      = 4'd3,
449
    S_WRITE_CONVERT_0   = 4'd4,
450
    S_WRITE_CONVERT_1   = 4'd5,
451
    S_WRITE_CONVERT_2   = 4'd6,
452
    S_WRITE_TO_SD       = 4'd7;
453
 
454
// byte position:   track*2*11*512  + side*11*512
455
// sector position: track*2*11      + side*11       = track*2*(8+2+1) + side*(8+2+1) = track*(16+4+2) + side*(8+2+1)
456
wire [31:0] sd_track_address;
457
assign sd_track_address =
458
    floppy_sector +
459
    { 21'b0, track, 4'b0 } +
460
    { 23'b0, track, 2'b0 } +
461
    { 24'b0, track, 1'b0 } +
462
    { 28'b0, ~last_fl_side_n, 3'b0 } +
463
    { 30'b0, ~last_fl_side_n, 1'b0 } +
464
    { 31'b0, ~last_fl_side_n };
465
 
466
wire [15:0] mfm_decoder_odd_30_16 = {
467
    master_DAT_I[30], 1'b0, master_DAT_I[28], 1'b0,
468
    master_DAT_I[26], 1'b0, master_DAT_I[24], 1'b0,
469
    master_DAT_I[22], 1'b0, master_DAT_I[20], 1'b0,
470
    master_DAT_I[18], 1'b0, master_DAT_I[16], 1'b0
471
};
472
wire [15:0] mfm_decoder_odd_14_0 = {
473
    master_DAT_I[14], 1'b0, master_DAT_I[12], 1'b0,
474
    master_DAT_I[10], 1'b0, master_DAT_I[8], 1'b0,
475
    master_DAT_I[6], 1'b0, master_DAT_I[4], 1'b0,
476
    master_DAT_I[2], 1'b0, master_DAT_I[0], 1'b0
477
};
478
wire [15:0] mfm_decoder_even_30_16 = {
479
    1'b0, master_DAT_I[30], 1'b0, master_DAT_I[28],
480
    1'b0, master_DAT_I[26], 1'b0, master_DAT_I[24],
481
    1'b0, master_DAT_I[22], 1'b0, master_DAT_I[20],
482
    1'b0, master_DAT_I[18], 1'b0, master_DAT_I[16]
483
};
484
wire [15:0] mfm_decoder_even_14_0 = {
485
    1'b0, master_DAT_I[14], 1'b0, master_DAT_I[12],
486
    1'b0, master_DAT_I[10], 1'b0, master_DAT_I[8],
487
    1'b0, master_DAT_I[6], 1'b0, master_DAT_I[4],
488
    1'b0, master_DAT_I[2], 1'b0, master_DAT_I[0]
489
};
490
wire [31:0] dskptr_sum =
491
    (substate == 4'd0)? dskptr + 32'd0 :
492
    (substate == 4'd1)? dskptr + 32'd2 :
493
    (substate == 4'd2)? dskptr + 32'd512 :
494
    dskptr + 32'd514;
495
 
496
wire start_read     = (last_state == S_IDLE && state == S_READ_FROM_SD);
497
wire start_write    = (last_state == S_WRITE_CONVERT_1 && state == S_WRITE_CONVERT_2);
498
wire active_write   = (state == S_WRITE_CONVERT_2 || state == S_WRITE_TO_SD);
499
wire enable_write   = (last_state == S_WRITE_CONVERT_2 && last_substate == 4'd3 && substate == 4'd0);
500
wire dsksync_halt   = (output_shift[47:32] == dsksync && dma_started == 1'b0 && output_shifting == 1'b1 && dma_active == 1'b1 && dsklen[14] == 1'b0);
501
reg last_dsksync_halt;
502
 
503
always @(posedge CLK_I or negedge reset_n) begin
504
    if(reset_n == 1'b0) begin
505
        floppy_error <= 1'b0;
506
 
507
        CYC_O <= 1'b0;
508
        STB_O <= 1'b0;
509
        WE_O <= 1'b0;
510
        ADR_O <= 30'd0;
511
        SEL_O <= 4'b0000;
512
        master_DAT_O <= 32'd0;
513
        ACK_O <= 1'b0;
514
 
515
        floppy_syn_irq <= 1'b0;
516
        floppy_blk_irq <= 1'b0;
517
 
518
        reg_fl_chng_n <= 1'b1;
519
        reg_fl_index_n <= 1'b1;
520
        reg_fl_mtr_n <= 1'b1;
521
 
522
        motor_spinup_delay <= 15'd0;
523
 
524
        last_fl_step_n <= 1'b1;
525
        last_fl_sel_n <= 4'b1111;
526
        last_fl_side_n <= 1'b1;
527
        track <= 7'd0;
528
        floppy_pos_changed <= 1'b0;
529
 
530
        dskptr <= 32'd0;
531
        dsklen <= 16'd0;
532
        dsksync <= 16'd0;
533
        last_dma_secondary <= 1'b0;
534
 
535
        dma_started <= 1'b0;
536
        last_dsksync_halt <= 1'b0;
537
 
538
        byte_dsksync <= 6'd0;
539
        byte_counter <= 3'd0;
540
 
541
        mfm_decoder <= 32'd0;
542
 
543
        substate <= 4'd0;
544
        last_substate <= 4'd0;
545
        state <= S_IDLE;
546
        last_state <= S_IDLE;
547
    end
548
    else begin
549
        if(floppy_inserted == 1'b1 && fl_sel_n == 4'b1110 && fl_step_n == 1'b0)     reg_fl_chng_n <= 1'b1;
550
        else if(floppy_inserted == 1'b0)                                            reg_fl_chng_n <= 1'b0;
551
 
552
        if(fl_sel_n[3:0] == 4'b1110) begin
553
            last_fl_step_n <= fl_step_n;
554
            last_fl_side_n <= fl_side_n;
555
        end
556
        last_fl_sel_n <= fl_sel_n;
557
        if(fl_sel_n[3:0] == 4'b1110 && last_fl_step_n == 1'b0 && fl_step_n == 1'b1) begin
558
            if(fl_dir == 1'b0 && track < 7'd79)     track <= track + 7'd1;
559
            else if(fl_dir == 1'b1 && track > 7'd0) track <= track - 7'd1;
560
        end
561
 
562
        if(last_fl_sel_n[0] == 1'b1 && fl_sel_n[0] == 1'b0) reg_fl_mtr_n <= fl_mtr_n;
563
 
564
        if(reg_fl_mtr_n == 1'b1)                                        motor_spinup_delay <= 15'd0;
565
        else if(reg_fl_mtr_n == 1'b0 && motor_spinup_delay < 15'd32767) motor_spinup_delay <= motor_spinup_delay + 15'd1;
566
 
567
        if(fl_sel_n[3:0] == 4'b1110 && ((last_fl_step_n == 1'b0 && fl_step_n == 1'b1) || (last_fl_side_n != fl_side_n)))
568
                                                                                        floppy_pos_changed <= 1'b1;
569
        else if(state == S_READ_READY_0)                                                floppy_pos_changed <= 1'b0;
570
 
571
        if(dma_started == 1'b1 && dma_active == 1'b0)   dma_started <= 1'b0;
572
        if(floppy_error == 1'b1)                        floppy_error <= 1'b0;
573
        if(floppy_blk_irq == 1'b1)                      floppy_blk_irq <= 1'b0;
574
        if(floppy_syn_irq == 1'b1)                      floppy_syn_irq <= 1'b0;
575
        if(reg_fl_index_n == 1'b0)                      reg_fl_index_n <= 1'b1;
576
 
577
 
578
        if(floppy_syn_irq == 1'b1)
579
            byte_dsksync <= 6'd1;
580
        else if(byte_dsksync == 6'd61 || floppy_inserted == 1'b0 || reg_fl_mtr_n == 1'b1 || fl_sel_n[0] == 1'b1)
581
            byte_dsksync <= 6'd0;
582
        else if(byte_dsksync != 6'd0)
583
            byte_dsksync <= byte_dsksync + 6'd1;
584
 
585
        if(na_dskbytr_read == 1'b1 && byte_counter > 3'd0 && byte_counter < 3'd4)   byte_counter <= byte_counter + 3'd1;
586
        else if(na_dskbytr_read == 1'b1)                                            byte_counter <= 3'd0;
587
 
588
        last_state <= state;
589
        last_substate <= substate;
590
        last_dsksync_halt <= dsksync_halt;
591
 
592
        if(CYC_I == 1'b1 && STB_I == 1'b1 && ACK_O == 1'b0) ACK_O <= 1'b1;
593
        else ACK_O <= 1'b0;
594
 
595
        if(CYC_I == 1'b1 && STB_I == 1'b1 && WE_I == 1'b1 && ACK_O == 1'b0) begin
596
            if({ ADR_I, 2'b0 } == 9'h020 && SEL_I[0] == 1'b1)   dskptr[7:0]     <= slave_DAT_I[7:0];
597
            if({ ADR_I, 2'b0 } == 9'h020 && SEL_I[1] == 1'b1)   dskptr[15:8]    <= slave_DAT_I[15:8];
598
            if({ ADR_I, 2'b0 } == 9'h020 && SEL_I[2] == 1'b1)   dskptr[23:16]   <= slave_DAT_I[23:16];
599
            if({ ADR_I, 2'b0 } == 9'h020 && SEL_I[3] == 1'b1)   dskptr[31:24]   <= slave_DAT_I[31:24];
600
            if({ ADR_I, 2'b0 } == 9'h024 && SEL_I[0] == 1'b1)   ;
601
            if({ ADR_I, 2'b0 } == 9'h024 && SEL_I[1] == 1'b1)   ;
602
            if({ ADR_I, 2'b0 } == 9'h024 && SEL_I[2] == 1'b1)   dsklen[7:0]      <= slave_DAT_I[23:16];
603
            if({ ADR_I, 2'b0 } == 9'h024 && SEL_I[3] == 1'b1)   dsklen[15:8]     <= slave_DAT_I[31:24];
604
            if({ ADR_I, 2'b0 } == 9'h07C && SEL_I[0] == 1'b1)   dsksync[7:0]     <= slave_DAT_I[7:0];
605
            if({ ADR_I, 2'b0 } == 9'h07C && SEL_I[1] == 1'b1)   dsksync[15:8]    <= slave_DAT_I[15:8];
606
            if({ ADR_I, 2'b0 } == 9'h07C && SEL_I[2] == 1'b1)   ;
607
            if({ ADR_I, 2'b0 } == 9'h07C && SEL_I[3] == 1'b1)   ;
608
 
609
            if({ ADR_I, 2'b0 } == 9'h024 && SEL_I[3] == 1'b1)   last_dma_secondary <= dsklen[15];
610
        end
611
 
612
        if(state == S_IDLE) begin
613
            if(dma_active == 1'b1 || (floppy_inserted == 1'b1 && motor_spinup_delay != 15'd0)) begin
614
                ADR_O <= 30'h4000400; // 0x10001000, sd
615
                substate <= 4'd0;
616
                state <= S_READ_FROM_SD;
617
            end
618
        end
619
        else if(state == S_READ_FROM_SD) begin
620
            if(ACK_I == 1'b1 && CYC_O == 1'b1 && STB_O == 1'b1) begin
621
                CYC_O <= 1'b0;
622
                STB_O <= 1'b0;
623
 
624
                if(substate < 4'd3) begin
625
                    substate <= substate + 4'd1;
626
                    ADR_O <= ADR_O + 30'd1;
627
                end
628
                else if(substate == 4'd3) begin
629
                    substate <= substate + 4'd1;
630
                    ADR_O <= 30'h04000400; // 0x10001000, sd read state
631
                end
632
                else if(substate >= 4'd4 && master_DAT_I == 32'd5) begin
633
                    floppy_error <= 1'b1;
634
                    substate <= 4'd0;
635
                    state <= S_IDLE;
636
                end
637
                else if(substate == 4'd4 && master_DAT_I == 32'd3) begin
638
                    substate <= substate + 4'd1;
639
                end
640
                else if(substate == 4'd5 && master_DAT_I == 32'd2) begin
641
                    substate <= 4'd0;
642
                    state <= S_READ_READY_0;
643
                end
644
            end
645
            else if(ACK_I == 1'b0) begin
646
                CYC_O <= 1'b1;
647
                STB_O <= 1'b1;
648
                WE_O <= (substate <= 4'd3)? 1'b1 : 1'b0;
649
                SEL_O <= 4'b1111;
650
                // ADR_O <= ADR_O
651
                master_DAT_O <=
652
                    (substate == 4'd0)? 32'h10004000 :      // base address
653
                    (substate == 4'd1)? sd_track_address :  // sd sector number
654
                    (substate == 4'd2)? 32'd11 :            // read sector size
655
                    32'd2;                                  // start sd read
656
            end
657
        end
658
 
659
        else if(state == S_READ_READY_0) begin
660
 
661
            if(floppy_pos_changed == 1'b1 || (dma_active == 1'b0 && (floppy_inserted == 1'b0 || motor_spinup_delay == 15'd0))) begin
662
                state <= S_IDLE;
663
            end
664
            else if(dma_active == 1'b1 && dsklen[14] == 1'b1) begin
665
                substate <= 4'd0;
666
                mfm_decoder <= 32'd0;
667
                state <= S_WRITE_CONVERT_0;
668
            end
669
            else begin
670
 
671
 
672
                if(output_index == 1'b1) reg_fl_index_n <= 1'b0;
673
 
674
                if(output_shifted == 1'b1) byte_counter <= 3'd1;
675
 
676
                if(output_shift[47:32] == dsksync && output_shifting == 1'b1 && floppy_syn_irq == 1'b0) floppy_syn_irq <= 1'b1;
677
 
678
                if(output_shifted == 1'b1 && dma_active == 1'b1 && dsklen[14] == 1'b0 && (adk_con[10] == 1'b0 || dma_started == 1'b1)) begin
679
                    master_DAT_O <= (dskptr[1] == 1'b0)? output_long_word : { output_long_word[15:0], output_long_word[31:16] };
680
                    dma_started <= 1'b1;
681
                    substate <= 4'd0;
682
                    state <= S_READ_READY_1;
683
                end
684
                else if(dsksync_halt == 1'b1) begin
685
                    dma_started <= 1'b1;
686
                end
687
 
688
            end
689
 
690
        end
691
 
692
        else if(state == S_READ_READY_1) begin
693
            if(ACK_I == 1'b1 && CYC_O == 1'b1 && STB_O == 1'b1) begin
694
                CYC_O <= 1'b0;
695
                STB_O <= 1'b0;
696
 
697
                if(substate == 4'd0) begin
698
                    dskptr <= dskptr + 32'd2;
699
                    dsklen[13:0] <= dsklen[13:0] - 14'd1;
700
                    if(dsklen[13:0] == 14'd1) begin
701
                        floppy_blk_irq <= 1'b1;
702
                        state <= S_READ_READY_0;
703
                    end
704
                    else substate <= substate + 4'd1;
705
                end
706
                else if(substate == 4'd1) begin
707
                    dskptr <= dskptr + 32'd2;
708
                    dsklen[13:0] <= dsklen[13:0] - 14'd1;
709
                    if(dsklen[13:0] == 14'd1) floppy_blk_irq <= 1'b1;
710
                    state <= S_READ_READY_0;
711
                end
712
            end
713
            else if(ACK_I == 1'b0) begin
714
                CYC_O <= 1'b1;
715
                STB_O <= 1'b1;
716
                WE_O <= 1'b1;
717
                SEL_O <= (dskptr[1] == 1'b0)? 4'b1100 : 4'b0011;
718
                ADR_O <= dskptr[31:2];
719
            end
720
        end
721
 
722
 
723
 
724
 
725
        //  start reading till 0x4489
726
        //  skip 1*2 bytes
727
        else if(state == S_WRITE_CONVERT_0) begin
728
            if(ACK_I == 1'b1 && CYC_O == 1'b1 && STB_O == 1'b1) begin
729
                CYC_O <= 1'b0;
730
                STB_O <= 1'b0;
731
 
732
                if(dsklen[13:0] <= 14'd1) begin
733
                    floppy_blk_irq <= 1'b1;
734
                    dskptr <= dskptr + { 17'd0, dsklen[13:0], 1'b0 };
735
                    dsklen[13:0] <= 14'd0;
736
                    substate <= 4'd0;
737
                    state <= S_IDLE;
738
                end
739
                else if( (dskptr[1] == 1'b0 && master_DAT_I[31:16] == 16'h4489) ||
740
                        (dskptr[1] == 1'b1 && master_DAT_I[15:0] == 16'h4489) )
741
                begin
742
                    dskptr <= dskptr + 32'd4;
743
                    dsklen[13:0] <= dsklen[13:0] - 14'd2;
744
                    substate <= 4'd0;
745
                    state <= S_WRITE_CONVERT_1;
746
                end
747
                else begin
748
                    dskptr <= dskptr + 32'd2;
749
                    dsklen[13:0] <= dsklen[13:0] - 14'd1;
750
                end
751
            end
752
            else if(ACK_I == 1'b0) begin
753
                CYC_O <= 1'b1;
754
                STB_O <= 1'b1;
755
                WE_O <= 1'b0;
756
                SEL_O <= 4'b1111;
757
                ADR_O <= dskptr[31:2];
758
            end
759
        end
760
        //  read next 4 words, decode sector number
761
        //  skip 24*2 bytes
762
        else if(state == S_WRITE_CONVERT_1) begin
763
            if(ACK_I == 1'b1 && CYC_O == 1'b1 && STB_O == 1'b1) begin
764
                CYC_O <= 1'b0;
765
                STB_O <= 1'b0;
766
 
767
                if(dsklen[13:0] <= 14'd24) begin
768
                    floppy_blk_irq <= 1'b1;
769
                    dskptr <= dskptr + { 17'd0, dsklen[13:0], 1'b0 };
770
                    dsklen[13:0] <= 14'd0;
771
                    substate <= 4'd0;
772
                    state <= S_IDLE;
773
                end
774
                else if(substate < 4'd3) begin
775
                    substate <= substate + 4'd1;
776
                    dskptr <= dskptr + 32'd2;
777
                    dsklen[13:0] <= dsklen[13:0] - 14'd1;
778
 
779
                    if(substate == 4'd0)
780
                        mfm_decoder[31:16] <= (dskptr[1] == 1'b0)? mfm_decoder_odd_30_16  : mfm_decoder_odd_14_0;
781
                    else if(substate == 4'd1)
782
                        mfm_decoder[15:0]  <= (dskptr[1] == 1'b0)? mfm_decoder_odd_30_16  : mfm_decoder_odd_14_0;
783
                    else if(substate == 4'd2)
784
                        mfm_decoder[31:16] <= mfm_decoder[31:16] | ((dskptr[1] == 1'b0)? mfm_decoder_even_30_16 : mfm_decoder_even_14_0);
785
                end
786
                else if(substate == 4'd3) begin
787
                    mfm_decoder[15:0]  <= mfm_decoder[15:0]  | ((dskptr[1] == 1'b0)? mfm_decoder_even_30_16 : mfm_decoder_even_14_0);
788
 
789
                    dskptr <= dskptr + 32'd50;
790
                    dsklen[13:0] <= dsklen[13:0] - 14'd25;
791
                    substate <= 4'd0;
792
                    state <= S_WRITE_CONVERT_2;
793
                end
794
            end
795
            else if(ACK_I == 1'b0) begin
796
                CYC_O <= 1'b1;
797
                STB_O <= 1'b1;
798
                WE_O <= 1'b0;
799
                SEL_O <= 4'b1111;
800
                ADR_O <= dskptr[31:2];
801
            end
802
        end
803
        //  read sector, decode to floppy buffer
804
        else if(state == S_WRITE_CONVERT_2) begin
805
            if(ACK_I == 1'b1 && CYC_O == 1'b1 && STB_O == 1'b1) begin
806
                CYC_O <= 1'b0;
807
                STB_O <= 1'b0;
808
 
809
                if(dsklen[13:0] <= 14'd257) begin
810
                    floppy_blk_irq <= 1'b1;
811
                    dskptr <= dskptr + { 17'd0, dsklen[13:0], 1'b0 };
812
                    dsklen[13:0] <= 14'd0;
813
                    substate <= 4'd0;
814
                    state <= S_IDLE;
815
                end
816
                else if(substate < 4'd3) begin
817
                    substate <= substate + 4'd1;
818
 
819
                    if(substate == 4'd0)
820
                        master_DAT_O[31:16] <= (dskptr_sum[1] == 1'b0)? mfm_decoder_odd_30_16  : mfm_decoder_odd_14_0;
821
                    else if(substate == 4'd1)
822
                        master_DAT_O[15:0]  <= (dskptr_sum[1] == 1'b0)? mfm_decoder_odd_30_16  : mfm_decoder_odd_14_0;
823
                    else if(substate == 4'd2)
824
                        master_DAT_O[31:16] <= master_DAT_O[31:16] | ((dskptr_sum[1] == 1'b0)? mfm_decoder_even_30_16 : mfm_decoder_even_14_0);
825
                end
826
                else if(substate == 4'd3) begin
827
                    master_DAT_O[15:0]  <= master_DAT_O[15:0]  | ((dskptr_sum[1] == 1'b0)? mfm_decoder_even_30_16 : mfm_decoder_even_14_0);
828
 
829
                    substate <= 4'd0;
830
                    if(write_sector_ready == 1'b1) begin
831
                                                dskptr <= dskptr + 32'd516;
832
                                                dsklen[13:0] <= dsklen[13:0] - 14'd258;
833
                        ADR_O <= 30'h04000400; // 0x10001000, sd
834
                        state <= S_WRITE_TO_SD;
835
                    end
836
                                        else begin
837
                                                dskptr <= dskptr + 32'd4;
838
                                                dsklen[13:0] <= dsklen[13:0] - 14'd2;
839
                                        end
840
                end
841
            end
842
            else if(ACK_I == 1'b0) begin
843
                CYC_O <= 1'b1;
844
                STB_O <= 1'b1;
845
                WE_O <= 1'b0;
846
                SEL_O <= 4'b1111;
847
                ADR_O <= dskptr_sum[31:2];
848
            end
849
        end
850
        //  sd write
851
        // continue
852
        else if(state == S_WRITE_TO_SD) begin
853
            if(ACK_I == 1'b1 && CYC_O == 1'b1 && STB_O == 1'b1) begin
854
                CYC_O <= 1'b0;
855
                STB_O <= 1'b0;
856
 
857
                if(substate < 4'd3) begin
858
                    substate <= substate + 4'd1;
859
                    ADR_O <= ADR_O + 30'd1;
860
                end
861
                else if(substate == 4'd3) begin
862
                    substate <= substate + 4'd1;
863
                    ADR_O <= 30'h04000400; // 0x10001000, sd read state
864
                end
865
                else if(substate >= 4'd4 && master_DAT_I == 32'd5) begin
866
                    floppy_error <= 1'b1;
867
                    substate <= 4'd0;
868
                    state <= S_IDLE;
869
                end
870
                else if(substate == 4'd4 && master_DAT_I == 32'd4) begin
871
                    substate <= substate + 4'd1;
872
                end
873
                else if(substate == 4'd5 && master_DAT_I == 32'd2) begin
874
                    substate <= 4'd0;
875
                    if(dsklen[13:0] == 14'd0) begin
876
                        floppy_blk_irq <= 1'b1;
877
                        state <= S_IDLE;
878
                    end
879
                    else begin
880
                        state <= S_WRITE_CONVERT_0;
881
                    end
882
                end
883
            end
884
            else if(ACK_I == 1'b0) begin
885
                CYC_O <= 1'b1;
886
                STB_O <= 1'b1;
887
                WE_O <= (substate <= 4'd3)? 1'b1 : 1'b0;
888
                SEL_O <= 4'b1111;
889
                // ADR_O <= ADR_O
890
                master_DAT_O <=
891
                    (substate == 4'd0)? 32'h10004000 :  // base address
892
                    (substate == 4'd1)? sd_track_address + { 28'd0, mfm_decoder[11:8] }: // sd sector number
893
                    (substate == 4'd2)? 32'd1 : // write sector count
894
                    32'd3; // start sd write
895
            end
896
        end
897
    end
898
end
899
 
900
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.