8
function FloppyController(cpu, fda_image, fdb_image)
17
this.dma = cpu.devices.dma;
19
this.bytes_expecting = 0;
20
this.receiving_command = new Uint8Array(10);
21
this.receiving_index = 0;
22
this.next_command = null;
24
this.response_data = new Uint8Array(10);
25
this.response_index = 0;
26
this.response_length = 0;
31
this.fda_image = fda_image;
34
this.fdb_image = fdb_image;
42
this.last_cylinder = 0;
52
cpu.devices.rtc.cmos_write(CMOS_FLOPPY_DRIVE_TYPE, 4 << 4);
54
this.sectors_per_track = 0;
55
this.number_of_heads = 0;
56
this.number_of_cylinders = 0;
62
this.floppy_size = fda_image.byteLength;
65
160 : { type: 1, tracks: 40, sectors: 8 , heads: 1 },
66
180 : { type: 1, tracks: 40, sectors: 9 , heads: 1 },
67
200 : { type: 1, tracks: 40, sectors: 10, heads: 1 },
68
320 : { type: 1, tracks: 40, sectors: 8 , heads: 2 },
69
360 : { type: 1, tracks: 40, sectors: 9 , heads: 2 },
70
400 : { type: 1, tracks: 40, sectors: 10, heads: 2 },
71
720 : { type: 3, tracks: 80, sectors: 9 , heads: 2 },
72
1200 : { type: 2, tracks: 80, sectors: 15, heads: 2 },
73
1440 : { type: 4, tracks: 80, sectors: 18, heads: 2 },
74
1722 : { type: 5, tracks: 82, sectors: 21, heads: 2 },
75
2880 : { type: 5, tracks: 80, sectors: 36, heads: 2 },
78
0 : { type: 1, tracks: 1, sectors: 1, heads: 1 },
81
var number_of_cylinders,
84
floppy_type = floppy_types[this.floppy_size >> 10];
86
if(floppy_type && ((this.floppy_size & 0x3FF) === 0 || this.floppy_size === 512))
88
cpu.devices.rtc.cmos_write(CMOS_FLOPPY_DRIVE_TYPE, floppy_type.type << 4);
90
sectors_per_track = floppy_type.sectors;
91
number_of_heads = floppy_type.heads;
92
number_of_cylinders = floppy_type.tracks;
96
throw "Unknown floppy size: " + h(fda_image.byteLength);
99
this.sectors_per_track = sectors_per_track;
100
this.number_of_heads = number_of_heads;
101
this.number_of_cylinders = number_of_cylinders;
104
this.io.register_read(0x3F0, this, this.port3F0_read);
105
this.io.register_read(0x3F2, this, this.port3F2_read);
106
this.io.register_read(0x3F4, this, this.port3F4_read);
107
this.io.register_read(0x3F5, this, this.port3F5_read);
108
this.io.register_read(0x3F7, this, this.port3F7_read);
110
this.io.register_write(0x3F2, this, this.port3F2_write);
111
this.io.register_write(0x3F5, this, this.port3F5_write);
114
FloppyController.prototype.get_state = function()
118
state[0] = this.bytes_expecting;
119
state[1] = this.receiving_command;
120
state[2] = this.receiving_index;
122
state[4] = this.response_data;
123
state[5] = this.response_index;
124
state[6] = this.response_length;
125
state[7] = this.floppy_size;
126
state[8] = this.status_reg0;
127
state[9] = this.status_reg1;
128
state[10] = this.status_reg2;
129
state[11] = this.drive;
130
state[12] = this.last_cylinder;
131
state[13] = this.last_head;
132
state[14] = this.last_sector;
133
state[15] = this.dor;
134
state[16] = this.sectors_per_track;
135
state[17] = this.number_of_heads;
136
state[18] = this.number_of_cylinders;
141
FloppyController.prototype.set_state = function(state)
143
this.bytes_expecting = state[0];
144
this.receiving_command = state[1];
145
this.receiving_index = state[2];
146
this.next_command = state[3];
147
this.response_data = state[4];
148
this.response_index = state[5];
149
this.response_length = state[6];
150
this.floppy_size = state[7];
151
this.status_reg0 = state[8];
152
this.status_reg1 = state[9];
153
this.status_reg2 = state[10];
154
this.drive = state[11];
155
this.last_cylinder = state[12];
156
this.last_head = state[13];
157
this.last_sector = state[14];
158
this.dor = state[15];
159
this.sectors_per_track = state[16];
160
this.number_of_heads = state[17];
161
this.number_of_cylinders = state[18];
164
FloppyController.prototype.port3F0_read = function()
166
dbg_log("3F0 read", LOG_FLOPPY);
172
FloppyController.prototype.port3F4_read = function()
174
dbg_log("3F4 read", LOG_FLOPPY);
176
var return_byte = 0x80;
178
if(this.response_index < this.response_length)
180
return_byte |= 0x40 | 0x10;
183
if((this.dor & 8) === 0)
191
FloppyController.prototype.port3F7_read = function()
193
dbg_log("3F7 read", LOG_FLOPPY);
197
FloppyController.prototype.port3F5_read = function()
199
if(this.response_index < this.response_length)
201
dbg_log("3F5 read: " + this.response_data[this.response_index], LOG_FLOPPY);
202
this.cpu.device_lower_irq(6);
203
return this.response_data[this.response_index++];
207
dbg_log("3F5 read, empty", LOG_FLOPPY);
212
FloppyController.prototype.port3F5_write = function(reg_byte)
214
if(!this.fda_image) return;
216
dbg_log("3F5 write " + h(reg_byte), LOG_FLOPPY);
218
if(this.bytes_expecting > 0)
220
this.receiving_command[this.receiving_index++] = reg_byte;
222
this.bytes_expecting--;
224
if(this.bytes_expecting === 0)
228
var log = "3F5 command received: ";
229
for(var i = 0; i < this.receiving_index; i++)
230
log += h(this.receiving_command[i]) + " ";
231
dbg_log(log, LOG_FLOPPY);
234
this.next_command.call(this, this.receiving_command);
247
this.next_command = this.fix_drive_data;
248
this.bytes_expecting = 2;
251
this.next_command = this.check_drive_status;
252
this.bytes_expecting = 1;
256
this.next_command = function(args) { this.do_sector(true, args); };
257
this.bytes_expecting = 8;
260
this.next_command = function(args) { this.do_sector(false, args); };
261
this.bytes_expecting = 8;
264
this.next_command = this.calibrate;
265
this.bytes_expecting = 1;
268
this.check_interrupt_status();
271
this.next_command = this.read_sector_id;
272
this.bytes_expecting = 1;
275
this.bytes_expecting = 2;
276
this.next_command = this.seek;
280
dbg_log("dump registers", LOG_FLOPPY);
281
this.response_data[0] = 0x80;
282
this.response_index = 0;
283
this.response_length = 1;
285
this.bytes_expecting = 0;
289
dbg_assert(false, "Unimplemented floppy command call " + h(reg_byte));
292
this.receiving_index = 0;
296
FloppyController.prototype.port3F2_read = function()
298
dbg_log("read 3F2: DOR", LOG_FLOPPY);
302
FloppyController.prototype.port3F2_write = function(value)
304
if((value & 4) === 4 && (this.dor & 4) === 0)
307
this.cpu.device_raise_irq(6);
310
dbg_log("start motors: " + h(value >> 4), LOG_FLOPPY);
311
dbg_log("enable dma: " + !!(value & 8), LOG_FLOPPY);
312
dbg_log("reset fdc: " + !!(value & 4), LOG_FLOPPY);
313
dbg_log("drive select: " + (value & 3), LOG_FLOPPY);
314
dbg_log("DOR = " + h(value), LOG_FLOPPY);
319
FloppyController.prototype.check_drive_status = function(args)
321
dbg_log("check drive status", LOG_FLOPPY);
323
this.response_index = 0;
324
this.response_length = 1;
325
this.response_data[0] = 1 << 5;
328
FloppyController.prototype.seek = function(args)
330
dbg_log("seek", LOG_FLOPPY);
331
dbg_assert((args[0] & 3) === 0, "Unhandled seek drive");
333
this.last_cylinder = args[1];
334
this.last_head = args[0] >> 2 & 1;
339
FloppyController.prototype.calibrate = function(args)
341
dbg_log("floppy calibrate", LOG_FLOPPY);
346
FloppyController.prototype.check_interrupt_status = function()
349
dbg_log("floppy check interrupt status", LOG_FLOPPY);
351
this.response_index = 0;
352
this.response_length = 2;
354
this.response_data[0] = 1 << 5;
355
this.response_data[1] = this.last_cylinder;
358
FloppyController.prototype.do_sector = function(is_write, args)
363
sector_size = 128 << args[4],
364
read_count = args[5] - args[3] + 1,
366
read_offset = ((head + this.number_of_heads * cylinder) * this.sectors_per_track + sector - 1) * sector_size;
368
dbg_log("Floppy " + (is_write ? "Write" : "Read"), LOG_FLOPPY);
369
dbg_log("from " + h(read_offset) + " length " + h(read_count * sector_size), LOG_FLOPPY);
370
dbg_log(cylinder + " / " + head + " / " + sector, LOG_FLOPPY);
374
dbg_log("FDC: sector count is zero, use data length instead", LOG_FLOPPY);
384
this.dma.do_write(this.fda_image, read_offset, read_count * sector_size, 2, this.done.bind(this, args, cylinder, head, sector));
388
this.dma.do_read(this.fda_image, read_offset, read_count * sector_size, 2, this.done.bind(this, args, cylinder, head, sector));
392
FloppyController.prototype.done = function(args, cylinder, head, sector, error)
402
if(sector > this.sectors_per_track)
407
if(head >= this.number_of_heads)
414
this.last_cylinder = cylinder;
415
this.last_head = head;
416
this.last_sector = sector;
418
this.response_index = 0;
419
this.response_length = 7;
421
this.response_data[0] = head << 2 | 0x20;
422
this.response_data[1] = 0;
423
this.response_data[2] = 0;
424
this.response_data[3] = cylinder;
425
this.response_data[4] = head;
426
this.response_data[5] = sector;
427
this.response_data[6] = args[4];
432
FloppyController.prototype.fix_drive_data = function(args)
434
dbg_log("floppy fix drive data " + args, LOG_FLOPPY);
437
FloppyController.prototype.read_sector_id = function(args)
439
dbg_log("floppy read sector id " + args, LOG_FLOPPY);
441
this.response_index = 0;
442
this.response_length = 7;
444
this.response_data[0] = 0;
445
this.response_data[1] = 0;
446
this.response_data[2] = 0;
447
this.response_data[3] = 0;
448
this.response_data[4] = 0;
449
this.response_data[5] = 0;
450
this.response_data[6] = 0;
455
FloppyController.prototype.raise_irq = function()
459
this.cpu.device_raise_irq(6);