#include "stdio.h" #include "string.h" #include "hardware.h" #include "menu.h" #include "tos.h" #include "fat.h" #include "fpga.h" #define TOS_BASE_ADDRESS_192k 0xfc0000 #define TOS_BASE_ADDRESS_256k 0xe00000 #define CART_BASE_ADDRESS 0xfa0000 #define VIDEO_BASE_ADDRESS 0x010000 unsigned long tos_system_ctrl = TOS_MEMCONFIG_4M; static unsigned char font[2048]; // buffer for 8x16 atari font // default name of TOS image static char tos_img[12] = "TOS IMG"; static char cart_img[12] = ""; // two floppies static struct { fileTYPE file; unsigned char sides; unsigned char spt; } fdd_image[2]; // one harddisk fileTYPE hdd_image; static unsigned char dma_buffer[512]; static const char *acsi_cmd_name(int cmd) { static const char *cmdname[] = { "Test Drive Ready", "Restore to Zero", "Cmd $2", "Request Sense", "Format Drive", "Read Block limits", "Reassign Blocks", "Cmd $7", "Read Sector", "Cmd $9", "Write Sector", "Seek Block", "Cmd $C", "Cmd $D", "Cmd $E", "Cmd $F", "Cmd $10", "Cmd $11", "Inquiry", "Verify", "Cmd $14", "Mode Select", "Cmd $16", "Cmd $17", "Cmd $18", "Cmd $19", "Mode Sense", "Start/Stop Unit", "Cmd $1C", "Cmd $1D", "Cmd $1E", "Cmd $1F" }; return cmdname[cmd]; } static void mist_memory_set_address(unsigned long a) { a >>= 1; // make word address EnableFpga(); SPI(MIST_SET_ADDRESS); SPI((a >> 24) & 0xff); SPI((a >> 16) & 0xff); SPI((a >> 8) & 0xff); SPI((a >> 0) & 0xff); DisableFpga(); } static void mist_set_control(unsigned long ctrl) { EnableFpga(); SPI(MIST_SET_CONTROL); SPI((ctrl >> 24) & 0xff); SPI((ctrl >> 16) & 0xff); SPI((ctrl >> 8) & 0xff); SPI((ctrl >> 0) & 0xff); DisableFpga(); } static void hexdump(void *data, unsigned long size, unsigned long offset) { int i, b2c; unsigned long n=0; char *ptr = data; if(!size) return; while(size>0) { iprintf("%08x: ", n + offset); b2c = (size>16)?16:size; for(i=0;i words) while(words--) { *data++ = SPI(0); *data++ = SPI(0); } DisableFpga(); } static void mist_memory_write(char *data, unsigned long words) { EnableFpga(); SPI(MIST_WRITE_MEMORY); while(words--) { SPI(*data++); SPI(*data++); } DisableFpga(); } void mist_memory_set(char data, unsigned long words) { EnableFpga(); SPI(MIST_WRITE_MEMORY); while(words--) { SPI(data); SPI(data); } DisableFpga(); } static void handle_acsi(unsigned char *buffer) { unsigned char target = buffer[9] >> 5; unsigned char cmd = buffer[9] & 0x1f; unsigned int dma_address = 256 * 256 * buffer[0] + 256 * buffer[1] + buffer[2]; unsigned char scnt = buffer[3]; unsigned long lba = 256 * 256 * (buffer[10] & 0x1f) + 256 * buffer[11] + buffer[12]; unsigned short length = buffer[13]; if(length == 0) length = 256; iprintf("ACSI: target %d, \"%s\"\n", target, acsi_cmd_name(cmd)); iprintf("ACSI: lba %lu, length %u\n", lba, length); iprintf("DMA: scnt %u, addr %p\n", scnt, dma_address); // only a harddisk on ACSI 0 is supported // ACSI 0 is only supported if a image is loaded if((target == 0) && (hdd_image.size != 0)) { mist_memory_set_address(dma_address); switch(cmd) { case 0x08: // read sector DISKLED_ON; while(length) { FileSeek(&hdd_image, lba++, SEEK_SET); FileRead(&hdd_image, dma_buffer); mist_memory_write(dma_buffer, 256); length--; } DISKLED_OFF; break; case 0x0a: // write sector DISKLED_ON; while(length) { mist_memory_read(dma_buffer, 256); FileSeek(&hdd_image, lba++, SEEK_SET); FileWrite(&hdd_image, dma_buffer); length--; } DISKLED_OFF; break; case 0x12: // inquiry memset(dma_buffer, 0, 512); dma_buffer[2] = 1; // ANSI version dma_buffer[4] = length-8; // len memcpy(dma_buffer+8, "MIST ", 8); // Vendor memcpy(dma_buffer+16, " ", 16); // Clear device entry memcpy(dma_buffer+16, hdd_image.name, 11); // Device mist_memory_write(dma_buffer, length/2); break; case 0x1a: // mode sense { unsigned int blocks = hdd_image.size / 512; iprintf("ACSI: mode sense, blocks = %u\n", blocks); memset(dma_buffer, 0, 512); dma_buffer[3] = 8; // size of extent descriptor list dma_buffer[5] = blocks >> 16; dma_buffer[6] = blocks >> 8; dma_buffer[7] = blocks; dma_buffer[10] = 2; // byte 1 of block size in bytes (512) mist_memory_write(dma_buffer, length/2); } break; default: iprintf("ACSI: Unsupported command\n"); break; } } else iprintf("ACSI: Request for unsupported target\n"); EnableFpga(); SPI(MIST_ACK_DMA); DisableFpga(); } static void handle_fdc(unsigned char *buffer) { // extract contents unsigned int dma_address = 256 * 256 * buffer[0] + 256 * buffer[1] + buffer[2]; unsigned char scnt = buffer[3]; unsigned char fdc_cmd = buffer[4]; unsigned char fdc_track = buffer[5]; unsigned char fdc_sector = buffer[6]; unsigned char fdc_data = buffer[7]; unsigned char drv_sel = 3-((buffer[8]>>2)&3); unsigned char drv_side = 1-((buffer[8]>>1)&1); // check if a matching disk image has been inserted if(drv_sel && fdd_image[drv_sel-1].file.size) { // if the fdc has been asked to write protect the disks, then // write sector commands should never reach the oi controller // read/write sector command if((fdc_cmd & 0xc0) == 0x80) { // convert track/sector/side into disk offset unsigned int offset = drv_side; offset += fdc_track * fdd_image[drv_sel-1].sides; offset *= fdd_image[drv_sel-1].spt; offset += fdc_sector-1; while(scnt) { DISKLED_ON; FileSeek(&fdd_image[drv_sel-1].file, offset, SEEK_SET); mist_memory_set_address(dma_address); if((fdc_cmd & 0xe0) == 0x80) { // read from disk ... FileRead(&fdd_image[drv_sel-1].file, dma_buffer); // ... and copy to ram mist_memory_write(dma_buffer, 256); } else { // read from ram ... mist_memory_read(dma_buffer, 256); // ... and write to disk FileWrite(&(fdd_image[drv_sel-1].file), dma_buffer); } DISKLED_OFF; scnt--; dma_address += 512; offset += 1; } EnableFpga(); SPI(MIST_ACK_DMA); DisableFpga(); } } else { EnableFpga(); SPI(MIST_NAK_DMA); DisableFpga(); } } static void mist_get_dmastate() { static unsigned char buffer[16]; int i; EnableFpga(); SPI(MIST_GET_DMASTATE); for(i=0;i<16;i++) buffer[i] = SPI(0); DisableFpga(); // check if acsi is busy if(buffer[8] & 0x10) handle_acsi(buffer); // check if fdc is busy if(buffer[8] & 0x01) handle_fdc(buffer); } // color test, used to test the shifter without CPU/TOS #define COLORS 20 #define PLANES 4 static void tos_color_test() { unsigned short buffer[COLORS][PLANES]; int y; for(y=0;y<13;y++) { int i, j; for(i=0;i> n; *(d+1) = *d; } } } void tos_load_cartridge(char *name) { fileTYPE file; if(name) strncpy(cart_img, name, 11); // upload cartridge if(cart_img[0] && FileOpen(&file, cart_img)) { int i; char buffer[512]; iprintf("%s:\n size = %d\n", cart_img, file.size); int blocks = file.size / 512; iprintf(" blocks = %d\n", blocks); iprintf("Uploading: ["); mist_memory_set_address(CART_BASE_ADDRESS); DISKLED_ON; for(i=0;i= 256*1024) tos_base = TOS_BASE_ADDRESS_256k; else if(file.size != 192*1024) iprintf("WARNING: Unexpected TOS size!\n"); int blocks = file.size / 512; iprintf(" blocks = %d\n", blocks); iprintf(" address = $%08x\n", tos_base); // extract base address FileRead(&file, buffer); // clear first 16k mist_memory_set_address(0); mist_memory_set(0x00, 8192); #if 0 iprintf("Erasing: "); // clear memory to increase chances of catching write problems mist_memory_set_address(tos_base); mist_memory_set(0x00, file.size/2); iprintf("done\n"); #endif time = GetTimer(0); iprintf("Uploading: ["); for(i=0;i> 20); } else iprintf("Unable to find tos.img\n"); #if 0 { char rx[512], buffer[512]; int i,j; int blocks = file.size / 512; FileSeek(&file, 0, SEEK_SET); mist_memory_set_address(TOS_BASE_ADDRESS); iprintf("Verifying: ["); for(i=0;i 1) { tos_select_hdd_image(file); return; } iprintf("%c: eject\n", i+'A'); // toggle write protect bit to help tos detect a media change int wp_bit = (!i)?TOS_CONTROL_FDC_WR_PROT_A:TOS_CONTROL_FDC_WR_PROT_B; // any disk ejected is "write protected" (as nothing covers the write protect mechanism) mist_set_control(tos_system_ctrl | wp_bit); // first "eject" disk fdd_image[i].file.size = 0; fdd_image[i].sides = 1; fdd_image[i].spt = 0; // no new disk given? if(!file) return; // open floppy fdd_image[i].file = *file; iprintf("%c: insert %.11s\n", i+'A', fdd_image[i].file.name); // check image size and parameters // check if image size suggests it's a two sided disk if(fdd_image[i].file.size > 80*9*512) fdd_image[i].sides = 2; // try common sector/track values int m, s, t; for(m=0;m<=2;m++) // multiplier for hd/ed disks for(s=9;s<=12;s++) for(t=80;t<=85;t++) if(512*(1<