--- trunk/src/devices/dev_disk.c 2007/10/08 16:19:23 20 +++ trunk/src/devices/dev_disk.c 2007/10/08 16:22:32 42 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005 Anders Gavare. All rights reserved. + * Copyright (C) 2005-2007 Anders Gavare. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -25,9 +25,11 @@ * SUCH DAMAGE. * * - * $Id: dev_disk.c,v 1.9 2005/11/13 00:14:08 debug Exp $ + * $Id: dev_disk.c,v 1.15 2007/06/15 18:44:19 debug Exp $ * - * Basic "Disk" device. This is a simple test device which can be used to + * COMMENT: A simple disk controller device, for the test machines + * + * Basic "disk" device. This is a simple test device which can be used to * read and write data from disk devices. */ @@ -37,15 +39,14 @@ #include "cpu.h" #include "device.h" -#include "devices.h" #include "diskimage.h" #include "emul.h" #include "machine.h" #include "memory.h" #include "misc.h" +#include "testmachine/dev_disk.h" -#define BUF_SIZE 8192 struct disk_data { int64_t offset; @@ -56,75 +57,71 @@ }; -/* - * dev_disk_buf_access(): - */ -int dev_disk_buf_access(struct cpu *cpu, struct memory *mem, - uint64_t relative_addr, unsigned char *data, size_t len, - int writeflag, void *extra) +DEVICE_ACCESS(disk_buf) { - struct disk_data *d = (struct disk_data *) extra; + struct disk_data *d = extra; if (writeflag == MEM_WRITE) memcpy(d->buf + relative_addr, data, len); else memcpy(data, d->buf + relative_addr, len); + return 1; } -/* - * dev_disk_access(): - */ -int dev_disk_access(struct cpu *cpu, struct memory *mem, - uint64_t relative_addr, unsigned char *data, size_t len, - int writeflag, void *extra) +DEVICE_ACCESS(disk) { - struct disk_data *d = (struct disk_data *) extra; + struct disk_data *d = extra; uint64_t idata = 0, odata = 0; if (writeflag == MEM_WRITE) idata = memory_readmax64(cpu, data, len); switch (relative_addr) { - case 0x00: + + case DEV_DISK_OFFSET: if (writeflag == MEM_READ) { odata = d->offset; } else { d->offset = idata; } break; - case 0x10: + + case DEV_DISK_ID: if (writeflag == MEM_READ) { odata = d->disk_id; } else { d->disk_id = idata; } break; - case 0x20: + + case DEV_DISK_START_OPERATION: if (writeflag == MEM_READ) { odata = d->command; } else { d->command = idata; switch (d->command) { case 0: d->status = diskimage_access(cpu->machine, - d->disk_id, DISKIMAGE_SCSI, 0, + d->disk_id, DISKIMAGE_IDE, 0, d->offset, d->buf, 512); break; case 1: d->status = diskimage_access(cpu->machine, - d->disk_id, DISKIMAGE_SCSI, 1, + d->disk_id, DISKIMAGE_IDE, 1, d->offset, d->buf, 512); break; } } break; - case 0x30: + + case DEV_DISK_STATUS: if (writeflag == MEM_READ) { odata = d->status; } else { d->status = idata; } break; + default:if (writeflag == MEM_WRITE) { fatal("[ disk: unimplemented write to " "offset 0x%x: data=0x%x ]\n", (int) @@ -142,43 +139,34 @@ } -/* - * devinit_disk(): - */ -int devinit_disk(struct devinit *devinit) +DEVINIT(disk) { - struct disk_data *d = malloc(sizeof(struct disk_data)); + struct disk_data *d; size_t nlen; char *n1, *n2; - nlen = strlen(devinit->name) + 30; - n1 = malloc(nlen); - n2 = malloc(nlen); - - if (d == NULL || n1 == NULL || n2 == NULL) { - fprintf(stderr, "out of memory\n"); - exit(1); - } + CHECK_ALLOCATION(d = malloc(sizeof(struct disk_data))); memset(d, 0, sizeof(struct disk_data)); - d->buf = malloc(devinit->machine->arch_pagesize); - if (d->buf == NULL) { - fprintf(stderr, "out of memory\n"); - exit(1); - } + nlen = strlen(devinit->name) + 30; + CHECK_ALLOCATION(n1 = malloc(nlen)); + CHECK_ALLOCATION(n2 = malloc(nlen)); + + CHECK_ALLOCATION(d->buf = malloc(devinit->machine->arch_pagesize)); memset(d->buf, 0, devinit->machine->arch_pagesize); snprintf(n1, nlen, "%s [control]", devinit->name); snprintf(n2, nlen, "%s [data buffer]", devinit->name); memory_device_register(devinit->machine->memory, n1, - devinit->addr, 0x4000, dev_disk_access, (void *)d, + devinit->addr, DEV_DISK_BUFFER, dev_disk_access, (void *)d, DM_DEFAULT, NULL); memory_device_register(devinit->machine->memory, n2, - devinit->addr + 0x4000, devinit->machine->arch_pagesize, - dev_disk_buf_access, (void *)d, DM_DYNTRANS_OK | - DM_DYNTRANS_WRITE_OK | DM_READS_HAVE_NO_SIDE_EFFECTS, d->buf); + devinit->addr + DEV_DISK_BUFFER, + devinit->machine->arch_pagesize, dev_disk_buf_access, + (void *)d, DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK | + DM_READS_HAVE_NO_SIDE_EFFECTS, d->buf); return 1; }