1
0
mirror of https://github.com/simh/simh.git synced 2026-01-11 23:52:58 +00:00
simh.simh/Altair8800/altair8800_dsk.c
Patrick Linstruth aad5351080 Altair8800: New Simulator
This is the initial release of the Altair8800 simulator.

Why another Altair simulator? AltairZ80 has been described as a
“software simulator”, where the intent is to run software designed
specifically for executing under a simulator. Altair8800 is intended
to accurately simulate the Altair hardware and execute software
that will run unchanged on real hardware. Software and disk images
can be moved between the Altair8800 simulator and real Altair and
other S-100 hardware without any changes. The Altair8800 simulator
is a tool that can assist with the restoration of vintage Altair
and other S-100 hardware and software along with the development
of new hardware and software. The accomplish this, the following
are major differences between AltairZ80 and Altair8800.

* The monolithic design where devices access other devices directly
through external variables and functions is no longer supported.
All devices exchange data through a new BUS device. Memory and I/O
address decoding and transfers are now handled by the BUS device.
All interrupt requests are handled by the BUS device.

* System RAM was moved from the CPU device to a new RAM device and
managed by the BUS device.

* Banked RAM was moved from the CPU device to a new BRAM device.

* Banked RAM can only be accessed through the BUS device. Memory in
banks that are not currently selected cannot be accessed. The AZ80
“banked” RAM was removed.

* ROMs were moved from the CPU and DSK devices to the new ROM device.
Mike Douglas’ Altmon Monitor is also available through the ROM
device. The custom AltairZ80 ALTAIRROM, which is not compatible
with original Altair disk images, is also available.

* The custom ALTAIRROM boot loader was replaced with the original
MITS Disk Boot Loader as the default ROM.

* The monolithic Multiple-CPU/RAM/ROM/IO/BankedRAM CPU device has
been replaced with a generic CPU device that provides an abstraction
layer between SIMH and the supported CPU architectures (currently
8080 and Z80). All IO is handled through the BUS device. RAM, Banked
RAM, and ROM are each handled by their own independent devices.

* The AltairZ80 SIO device was replaced with the M2SIO0 and M2SIO1
devices. The M2SIO devices fully support TMXR.

* A new SIO device was added to provide generic, programmable, Serial
IO. TMXR is not supported on this device.

* The Altair 8800 did not have PTR or PTP hardware devices. They have
been removed and replaced with the M2SIO1 device. PTR and PTP devices
are defined by software executing on the simulator.

* Contention between multiple enabled serial devices checking the
single host keyboard for input is now handled by the BUS device.
Port 0xFF sense switches was moved to a new SSW device and IMSAI
programmed output was moved to a new PO device.

* The SIMH pseudo device no longer uses the removed PTR and PTP
devices. The SIMH device has its own IO system. To avoid conflicts
with other devices and remain compatible with the R and W utilities
written for AltairZ80, SIMH “borrows” I/O ports 12H and 13H during
file transfers. Only SIMH commands needed to support R and W file
transfers are supported. All other SIMH commands were removed.

* AltairZ80-specific versions of CP/M are not supported by Altair8800.

* PC queue was removed from CPU device and replaced with CPU HISTORY.

* The Altair8800 simulator only supports 16-bit address and 8-bit
data buses. 8086 and 68K CPU architectures were removed.

* All CPU timing (clockFrequency) and “sleeps” (SIO SLEEP) have been
removed. SIMH THROTTLE is fully supported and is the recommended
way to manage simulator speed and host CPU utilization. Executing
“SET THROTTLE 100K/1”, for example, should provide ample speed
without tasking the host CPU.

* HEXLOAD and HEXSAVE commands were added. The LOAD “-h” option has
been removed. Intel Hex and sRecord (coming soon) formats are
supported.

* The WD179X device was converted to an API.

* A new DSK API was added to provide a consistent way to manage soft
sector raw disk images.

* Support for the proprietary IMD disk image format was removed. Only
RAW disk images are supported.

The following devices are supported by this initial release:

BUS - Altair (S-100) Bus
CPU - Intel 8080 / Zilog Z80
RAM - 64K RAM
ROM - ROMs
BRAM - Banked RAM
DSK - MITS 88-DCDD Floppy Disk Controller
M2SIO0 - MITS 88-2SIO Port 0
M2SIO1 - MITS 88-2SIO Port 1
SSW - Sense Switches
PO - Programmed Output
SIO - Generic Serial I/O
SBC200 - SD Systems SBC-200
TARBELL - Tarbell SD and DD Floppy Disk Controller
VFII - SD Systems VersaFloppy II
SIMH - SIMH Pseudo Device
2025-11-21 17:01:40 -05:00

436 lines
12 KiB
C

/* altair8800_dsk.c: Soft Sector Disk Library
Copyright (c) 2025 Patrick A. Linstruth
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
PETER SCHORN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Patrick Linstruth shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Patrick Linstruth.
History:
13-Nov-2025 Initial version
*/
#include "sim_defs.h"
#include "altair8800_dsk.h"
static void calc_offset(DSK_INFO *d);
/*
* INTERLEAVED disk images are structured as follows:
*
* +------------------+
* | TRACK 0 / HEAD 0 |
* +------------------+
* | TRACK 0 / HEAD 1 |
* +------------------+
* | TRACK 1 / HEAD 0 |
* +------------------+
* | TRACK 1 / HEAD 1 |
* +------------------+
* | TRACK n / HEAD 0 |
* +------------------+
* | TRACK n / HEAD 1 |
* +------------------+
*
* NON-INTERLEAVED disk images are structured as follows:
*
* +------------------+
* | TRACK 0 / HEAD 0 |
* +------------------+
* | TRACK 1 / HEAD 0 |
* +------------------+
* | TRACK n / HEAD 0 |
* +------------------+
* | TRACK 0 / HEAD 1 |
* +------------------+
* | TRACK 1 / HEAD 1 |
* +------------------+
* | TRACK n / HEAD 1 |
* +------------------+
*
*/
t_stat dsk_init(DSK_INFO *d, UNIT *unit, int tracks, int heads, int interleaved)
{
if (d == NULL) {
return SCPE_ARG;
}
if (tracks < 1 || tracks > DSK_MAX_TRACKS) {
return SCPE_ARG;
}
if (heads < 1 || heads > DSK_MAX_HEADS) {
return SCPE_ARG;
}
d->unit = unit;
d->fmt.tracks = tracks;
d->fmt.heads = heads;
d->fmt.interleaved = interleaved;
return SCPE_OK;
}
t_stat dsk_init_format(DSK_INFO *d, int strack, int etrack, int shead, int ehead, int den, int secs, int secsize, int stsec)
{
int tr, hd;
if (d == NULL) {
return SCPE_ARG;
}
if (strack < 0 || strack > d->fmt.tracks - 1) {
return SCPE_ARG;
}
if (shead < 0 || shead > d->fmt.heads - 1) {
return SCPE_ARG;
}
if (strack > etrack || shead > ehead) {
return SCPE_ARG;
}
if (d->fmt.tracks < (etrack - strack + 1)) {
d->fmt.tracks = (etrack - strack + 1);
}
if (d->fmt.heads < (ehead - shead + 1)) {
d->fmt.heads = ehead - shead + 1;
}
if (d->fmt.interleaved && d->fmt.heads > 1) {
for (tr = strack; tr <= etrack; tr++) {
for (hd = shead; hd <= ehead ; hd++) {
d->fmt.track[tr][hd].density = den;
d->fmt.track[tr][hd].sectors = secs;
d->fmt.track[tr][hd].sectorsize = secsize;
d->fmt.track[tr][hd].startsector = stsec;
}
}
}
else {
for (hd = shead; hd <= ehead; hd++) {
for (tr = strack; tr <= etrack; tr++) {
d->fmt.track[tr][hd].density = den;
d->fmt.track[tr][hd].sectors = secs;
d->fmt.track[tr][hd].sectorsize = secsize;
d->fmt.track[tr][hd].startsector = stsec;
}
}
}
calc_offset(d);
return SCPE_OK;
}
static void calc_offset(DSK_INFO *d) {
int t, h, offset = 0;
if (d->fmt.interleaved && d->fmt.heads > 1) {
for (t = 0; t < d->fmt.tracks; t++) {
for (h = 0; h < d->fmt.heads; h++) {
sim_debug(d->dbg_verbose, d->unit->dptr, "T:%02d H:%d O:%d\n", t, h, offset);
d->fmt.track[t][h].offset = offset;
/* Set offset to start of next track */
offset += d->fmt.track[t][h].sectors * d->fmt.track[t][h].sectorsize;
}
}
}
else {
for (h = 0; h < d->fmt.heads; h++) {
for (t = 0; t < d->fmt.tracks; t++) {
sim_debug(d->dbg_verbose, d->unit->dptr, "T:%02d H:%d O:%d\n", t, h, offset);
d->fmt.track[t][h].offset = offset;
/* Set offset to start of next track */
offset += d->fmt.track[t][h].sectors * d->fmt.track[t][h].sectorsize;
}
}
}
}
t_stat dsk_validate(DSK_INFO *d, int track, int head, int sector)
{
if (track < 0 || track > d->fmt.tracks - 1) {
sim_printf("DSK: ** Invalid track number %d\n", track);
return SCPE_IOERR;
}
if (head < 0 || head > (d->fmt.heads - 1)) {
sim_printf("DSK: ** Invalid head number %d\n", head);
return SCPE_IOERR;
}
if (sector < d->fmt.track[track][head].startsector || sector > (d->fmt.track[track][head].sectors - ((d->fmt.track[track][head].startsector) ? 0 : 1))) {
sim_printf("DSK: ** Invalid sector number. track/head %d/%d has %d sectors. %d requested.\n", track, head, d->fmt.track[track][head].sectors, sector);
return SCPE_IOERR;
}
return SCPE_OK;
}
int32 dsk_size(DSK_INFO *d)
{
if (d != NULL && d->unit != NULL && d->unit->fileref != NULL) {
return sim_fsize(d->unit->fileref);
}
return 0;
}
int32 dsk_tracks(DSK_INFO *d)
{
if (d != NULL) {
return d->fmt.tracks;
}
return 0;
}
int32 dsk_track_size(DSK_INFO *d, int32 track, int32 head)
{
if (d != NULL) {
return d->fmt.track[track][head].sectors * d->fmt.track[track][head].sectorsize;
}
return 0;
}
int32 dsk_sectors(DSK_INFO *d, int32 track, int32 head)
{
if (d != NULL) {
return d->fmt.track[track][head].sectors;
}
return 0;
}
int32 dsk_sector_size(DSK_INFO *d, int32 track, int32 head)
{
if (d != NULL) {
return d->fmt.track[track][head].sectorsize;
}
return 0;
}
int32 dsk_start_sector(DSK_INFO *d, int32 track, int32 head)
{
if (d != NULL) {
return d->fmt.track[track][head].startsector;
}
return 0;
}
int32 dsk_sector_offset(DSK_INFO *d, int32 track, int32 head, int32 sector)
{
if (d != NULL) {
return d->fmt.track[track][head].offset + (dsk_sector_size(d, track, head) * (sector - dsk_start_sector(d, track, head)));
}
return 0;
}
t_stat dsk_read_sector(DSK_INFO *d, int32 track, int32 head, int32 sector, uint8 *buf, int32 *bytesread)
{
int32 b, ssize;
t_stat r = SCPE_OK;
if (d == NULL || d->unit == NULL || d->unit->fileref == NULL) {
return SCPE_ARG;
}
if ((r = dsk_validate(d, track, head, sector)) != 0) {
return SCPE_ARG;
}
ssize = dsk_sector_size(d, track, head);
fseek(d->unit->fileref, dsk_sector_offset(d, track, head, sector), SEEK_SET);
if ((b = fread(buf, 1, ssize, d->unit->fileref)) != ssize) {
r = SCPE_IOERR;
}
if (bytesread != NULL) {
*bytesread = b;
sim_debug(d->dbg_verbose, d->unit->dptr, "DSK RD SEC: T:%d H:%d S:%d SS:%d READ:%d\n", track, head, sector, ssize, *bytesread);
}
// dsk_dump_buf(buf, ssize);
return r;
}
t_stat dsk_write_sector(DSK_INFO *d, int32 track, int32 head, int32 sector, const uint8 *buf, int32 *byteswritten)
{
int b, ssize, offset;
int r = SCPE_OK;
if (d == NULL || d->unit == NULL || d->unit->fileref == NULL) {
return SCPE_ARG;
}
if ((r = dsk_validate(d, track, head, sector)) != 0) {
return r;
}
ssize = dsk_sector_size(d, track, head);
offset = dsk_sector_offset(d, track,head, sector);
fseek(d->unit->fileref, offset, SEEK_SET);
b = fwrite(buf, 1, ssize, d->unit->fileref);
if (byteswritten != NULL) {
*byteswritten = b;
sim_debug(d->dbg_verbose, d->unit->dptr, "DSK WR SEC: T:%d H:%d S:%d SS:%d O:%d WRITTEN:%d\n", track, head, sector, ssize, offset, *byteswritten);
}
// dsk_dump_buf(buf, ssize);
return r;
}
t_stat dsk_read_track(DSK_INFO *d, int32 track, int32 head, uint8 *buf)
{
if (d == NULL || d->unit == NULL || d->unit->dptr == NULL) {
return SCPE_ARG;
}
sim_debug(d->dbg_verbose, d->unit->dptr, "DSK RD TRK: T:%d H:%d\n", track, head);
return SCPE_OK;
}
t_stat dsk_write_track(DSK_INFO *d, int32 track, int32 head, uint8 fill)
{
int s, ssize, start;
unsigned char *b;
if (d == NULL) {
return SCPE_ARG;
}
ssize = dsk_sector_size(d, track, head);
start = dsk_start_sector(d, track, head);
if ((b = malloc(ssize)) == NULL) {
return 0;
}
memset(b, fill, ssize);
sim_debug(d->dbg_verbose, d->unit->dptr, "DSK WR TRK: T:%d H:%d SS:%d F:%02X\n", track, head, ssize, fill);
for (s = 0; s < dsk_sectors(d, track, head); s++) {
dsk_write_sector(d, track, head, s + start, b, NULL);
}
free(b);
return 0;
}
t_stat dsk_format(DSK_INFO *d, uint8 fill)
{
int t, h;
if (d == NULL) {
return SCPE_ARG;
}
for (t = 0; t < d->fmt.tracks; t++) {
for (h = 0; h < d->fmt.heads; h++) {
dsk_write_track(d, t, h, fill);
}
}
return SCPE_OK;
}
void dsk_dump_buf(const uint8 *b, int32 size)
{
int i;
if (b == NULL) {
return;
}
for (i = 0; i < size; i++) {
if ((i & 0x0f) == 0x00) {
sim_printf("%04X: ", i);
}
sim_printf("%02X%c", b[i], ((i & 0x0f) == 0x0f) ? '\n' : ' ');
}
}
void dsk_show(DSK_INFO *d)
{
int t, h;
if (d != NULL) {
sim_printf("\n");
sim_printf("fmt.tracks = %d\n", d->fmt.tracks);
sim_printf("fmt.heads = %d\n", d->fmt.heads);
for (t = 0; t < d->fmt.tracks; t++) {
for (h = 0; h < d->fmt.heads; h++) {
sim_printf("T:%02d H:%d D:%s SECS:%02d SECSIZE:%04d OFFSET:%05X\n", t, h,
d->fmt.track[t][h].density == DSK_DENSITY_SD ? "SD" : "DD",
d->fmt.track[t][h].sectors,
d->fmt.track[t][h].sectorsize,
d->fmt.track[t][h].offset);
}
}
}
}
void dsk_set_verbose_flag(DSK_INFO *d, uint32 flag)
{
if (d != NULL) {
d->dbg_verbose = flag;
}
}
t_stat dsk_attach_help(FILE *st, DEVICE *dptr, UNIT *uptr, int32 flag, const char *cptr)
{
uint32 i;
for (i=0; i < dptr->numunits; i++) {
if ((dptr->units[i].flags & UNIT_ATTABLE) &&
!(dptr->units[i].flags & UNIT_DIS)) {
fprintf (st, " sim> ATTACH {switches} %s diskfile\n", sim_uname(&dptr->units[i]));
}
}
fprintf (st, "\n%s attach command switches\n", dptr->name);
fprintf (st, " -E Must Exist (if not specified an attempt to create the indicated\n");
fprintf (st, " disk container will be attempted).\n");
fprintf (st, " -N New file. Existing file is overwritten.\n");
fprintf (st, " -R Attach Read Only.\n");
fprintf (st, "\n\n");
return SCPE_OK;
}