1
0
mirror of https://github.com/pevans/erc-c.git synced 2025-04-11 02:37:32 +00:00

Add initial struct and drive code

This commit is contained in:
Peter Evans 2017-12-13 20:30:32 -06:00
parent 577e92de9e
commit 0162544fa6
2 changed files with 142 additions and 0 deletions

View File

@ -0,0 +1,69 @@
#ifndef _APPLE2_DISK_DRIVE_H
#define _APPLE2_DISK_DRIVE_H
/*
* These are the possible modes a drive can be in.
*/
enum apple2_disk_drive_mode {
DD_READ,
DD_WRITE,
};
#define MAX_DRIVE_STEPS 70
typedef struct {
/*
* Disk II drives allow the stepper to move in half-tracks, so we
* track (pun intended) the position of the head in those
* half-tracks rather than in full tracks.
*
* For example, if track_pos is 4, then the effective track is 2. If
* the track_pos is 9, then the effective track is 4, except that
* the head is on the half-track position.
*
* There are, at most, 35 tracks in a conventional disk, so there
* would be at most 70 track positions that we can iterate to.
*/
int track_pos;
/*
* The data field is where the actual byte data for the image is
* kept.
*/
vm_segment *data;
/*
* A disk drive may be "off" or "on", regardless of whether it's
* been selected by the peripheral interface.
*/
bool online;
/*
* This is one of DD_READ or DD_WRITE (defined in the enum above).
* The drive can only read or write at once, and the mode of
* operation must be made explicit through this mechanism.
*/
int mode;
/*
* Write protection is an attribute of the disk. Back in the day, a
* disk would have a small segment cut out of the disk on the side;
* this would make it writeable. A disk without that would be
* write-protected. You could take a writeable disk and make it
* write-protected simply by putting some solid-colored tape over
* the cut-out.
*
* For our purposes, write protection is a simply boolean attribute
* that you can enable or disable on the drive.
*/
bool write_protect;
} apple2_disk_drive;
extern apple2_disk_drive *apple2_disk_drive_create();
extern void apple2_disk_drive_free(apple2_disk_drive *);
extern void apple2_disk_drive_step(apple2_disk_drive *, int);
extern void apple2_disk_drive_set_mode(apple2_disk_drive *, int);
extern void apple2_disk_drive_turn_on(apple2_disk_drive *, bool);
extern void apple2_disk_drive_write_protect(apple2_disk_drive *, bool);
#endif

73
src/apple2.disk_drive.c Normal file
View File

@ -0,0 +1,73 @@
/*
* apple2.disk_drive.c
*/
#include "apple2.disk_drive.h"
apple2_disk_drive *
apple2_disk_drive_create()
{
apple2_disk_drive *drive;
drive = malloc(sizeof(apple2_disk_drive));
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;
drive->online = false;
drive->write_protect = true;
drive->mode = DD_READ;
return drive;
}
void
apple2_disk_drive_free(apple2_disk_drive *drive)
{
if (drive->data) {
vm_segment_free(drive->data);
}
free(drive);
}
void
apple2_disk_drive_step(apple2_disk_drive *drive, int steps)
{
drive->track_pos += steps;
if (drive->track_pos > MAX_DRIVE_STEPS) {
drive->track_pos = MAX_DRIVE_STEPS;
} else if (drive->track_pos < -MAX_DRIVE_STEPS) {
drive->track_pos = -MAX_DRIVE_STEPS;
}
}
void
apple2_disk_drive_set_mode(apple2_disk_drive *drive, int mode)
{
if (mode != DD_READ && mode != DD_WRITE) {
return;
}
drive->mode = mode;
}
void
apple2_disk_drive_turn_on(apple2_disk_drive *drive, bool online)
{
drive->online = online;
}
void
apple2_disk_drive_write_protect(apple2_disk_drive *drive, bool protect)
{
drive->write_protect = protect;
}