2017-12-14 02:30:32 +00:00
|
|
|
/*
|
|
|
|
* apple2.disk_drive.c
|
|
|
|
*/
|
|
|
|
|
2017-12-15 04:27:45 +00:00
|
|
|
#include "apple2.dd.h"
|
2017-12-14 02:30:32 +00:00
|
|
|
|
2017-12-15 04:14:55 +00:00
|
|
|
/*
|
|
|
|
* This is the length of a typical disk that is formatted in either DOS
|
|
|
|
* 3.3 or ProDOS.
|
|
|
|
*/
|
|
|
|
#define _140K_ 143360
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And this is the length of a disk that has been formatted as a nibble
|
|
|
|
* file (*.NIB). This is not an Apple thing, exactly; it's more of an
|
|
|
|
* emulator thing, that emulators had used to try and get around copy
|
|
|
|
* protection in emulation. It does complicate disk drive operation!
|
|
|
|
*/
|
|
|
|
#define _240K_ 245760
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the last _accessible_ sector position within a track (you can
|
|
|
|
* have 0 - 4095).
|
|
|
|
*/
|
|
|
|
#define MAX_SECTOR_POS 4095
|
|
|
|
|
|
|
|
apple2dd *
|
|
|
|
apple2dd_create()
|
2017-12-14 02:30:32 +00:00
|
|
|
{
|
2017-12-15 04:14:55 +00:00
|
|
|
apple2dd *drive;
|
2017-12-14 02:30:32 +00:00
|
|
|
|
2017-12-15 04:14:55 +00:00
|
|
|
drive = malloc(sizeof(apple2dd));
|
2017-12-14 02:30:32 +00:00
|
|
|
if (drive == NULL) {
|
|
|
|
log_critical("Could not malloc space for apple2 disk drive");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// To begin with, we have no segment for data; that's something that
|
|
|
|
// will depend on the disk you insert. For example, a DOS 3.3 or
|
|
|
|
// ProDOS disk will have 140k, but a NIB file would have more.
|
|
|
|
drive->data = NULL;
|
|
|
|
|
|
|
|
drive->track_pos = 0;
|
2017-12-15 04:14:55 +00:00
|
|
|
drive->sector_pos = 0;
|
2017-12-14 02:30:32 +00:00
|
|
|
drive->online = false;
|
|
|
|
drive->write_protect = true;
|
|
|
|
drive->mode = DD_READ;
|
|
|
|
|
|
|
|
return drive;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-12-15 04:14:55 +00:00
|
|
|
apple2dd_free(apple2dd *drive)
|
2017-12-14 02:30:32 +00:00
|
|
|
{
|
|
|
|
if (drive->data) {
|
|
|
|
vm_segment_free(drive->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(drive);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-12-15 04:14:55 +00:00
|
|
|
apple2dd_step(apple2dd *drive, int steps)
|
2017-12-14 02:30:32 +00:00
|
|
|
{
|
|
|
|
drive->track_pos += steps;
|
|
|
|
|
|
|
|
if (drive->track_pos > MAX_DRIVE_STEPS) {
|
|
|
|
drive->track_pos = MAX_DRIVE_STEPS;
|
2017-12-15 04:46:50 +00:00
|
|
|
} else if (drive->track_pos < 0) {
|
|
|
|
drive->track_pos = 0;
|
2017-12-14 02:30:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-12-15 04:14:55 +00:00
|
|
|
apple2dd_set_mode(apple2dd *drive, int mode)
|
2017-12-14 02:30:32 +00:00
|
|
|
{
|
|
|
|
if (mode != DD_READ && mode != DD_WRITE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
drive->mode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-12-15 04:14:55 +00:00
|
|
|
apple2dd_turn_on(apple2dd *drive, bool online)
|
2017-12-14 02:30:32 +00:00
|
|
|
{
|
|
|
|
drive->online = online;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-12-15 04:14:55 +00:00
|
|
|
apple2dd_write_protect(apple2dd *drive, bool protect)
|
2017-12-14 02:30:32 +00:00
|
|
|
{
|
|
|
|
drive->write_protect = protect;
|
|
|
|
}
|
2017-12-15 04:14:55 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
position(apple2dd *drive)
|
|
|
|
{
|
|
|
|
if (drive->data->size == _140K_) {
|
|
|
|
int track_offset;
|
|
|
|
|
|
|
|
track_offset = (drive->track_pos % 2) * 4096;
|
|
|
|
return track_offset + drive->sector_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
vm_8bit
|
|
|
|
apple2dd_read_byte(apple2dd *drive)
|
|
|
|
{
|
|
|
|
vm_8bit byte = vm_segment_get(drive->data, position(drive));
|
|
|
|
|
|
|
|
// We may have read the very last byte in a sector; if so let's
|
|
|
|
// adjust the track_pos by two half tracks and reset the sector pos.
|
|
|
|
drive->sector_pos++;
|
|
|
|
if (drive->sector_pos > MAX_SECTOR_POS) {
|
|
|
|
drive->track_pos += 2;
|
|
|
|
drive->sector_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
apple2dd_write(apple2dd *drive, vm_8bit byte)
|
|
|
|
{
|
|
|
|
vm_segment_set(drive->data, position(drive), byte);
|
|
|
|
|
|
|
|
drive->sector_pos++;
|
|
|
|
if (drive->sector_pos > MAX_SECTOR_POS) {
|
|
|
|
drive->track_pos += 2;
|
|
|
|
drive->sector_pos = 0;
|
|
|
|
}
|
|
|
|
}
|