[645c148] | 1 | /*-----------------------------------------------------------------------*/
|
---|
| 2 | /* Low level disk I/O module skeleton for Petit FatFs (C)ChaN, 2009 */
|
---|
| 3 | /* Driver for MO5/TO8 HxC floppy emulator direct access
|
---|
| 4 | * Copyright 2011, Adrien Destugues <pulkomandy@pulkomandy.ath.cx>
|
---|
| 5 | -------------------------------------------------------------------------*/
|
---|
| 6 |
|
---|
| 7 | #include "diskio.h"
|
---|
| 8 |
|
---|
| 9 | #include "../macros.h"
|
---|
| 10 |
|
---|
| 11 | /*******/
|
---|
| 12 | extern unsigned char* secbuf;
|
---|
| 13 | extern unsigned char mark[];
|
---|
[1b74fa2] | 14 |
|
---|
| 15 | static DWORD prevsec = -1;
|
---|
[645c148] | 16 | /*******/
|
---|
| 17 |
|
---|
| 18 | /*-----------------------------------------------------------------------*/
|
---|
| 19 | /* Initialize Disk Drive */
|
---|
| 20 | /*-----------------------------------------------------------------------*/
|
---|
| 21 |
|
---|
| 22 | DSTATUS disk_initialize (void)
|
---|
| 23 | {
|
---|
| 24 | // Enter host mode
|
---|
| 25 | seek();
|
---|
| 26 |
|
---|
| 27 | // Read header to check everything is ok
|
---|
| 28 | read(255, 0, secbuf);
|
---|
| 29 |
|
---|
| 30 | // Check for HxCFEDA marker
|
---|
| 31 | if (*(long*)(mark) != *(long*)(secbuf))
|
---|
| 32 | return STA_NOINIT;
|
---|
| 33 | if (*(long*)(mark+4) != *(long*)(secbuf+4))
|
---|
| 34 | return STA_NOINIT;
|
---|
| 35 | return 0;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 |
|
---|
[1b74fa2] | 39 | void map_sector(DWORD sector, BYTE write)
|
---|
| 40 | {
|
---|
| 41 | // Set LBA address
|
---|
| 42 | int j = 8;
|
---|
| 43 | mark[j++] = 1;
|
---|
| 44 | mark[j++] = sector;
|
---|
| 45 | mark[j++] = (sector>>8);
|
---|
| 46 | mark[j++] = (sector>>16);
|
---|
| 47 | mark[j++] = (sector>>24);
|
---|
| 48 | mark[j++] = write;
|
---|
| 49 | mark[j++] = 1; // Sector count
|
---|
| 50 | // BEWARE of changing this to something else than 1 !
|
---|
| 51 | // In write burst mode it could erase all the sectors you don't rewrite
|
---|
| 52 |
|
---|
| 53 | // TODO extract a send_hxc_command function from this
|
---|
| 54 | for(; j <512; j++) mark[j] = 0;
|
---|
| 55 | write(255,0,mark);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[645c148] | 58 |
|
---|
| 59 | /*-----------------------------------------------------------------------*/
|
---|
| 60 | /* Read Partial Sector */
|
---|
| 61 | /*-----------------------------------------------------------------------*/
|
---|
| 62 |
|
---|
| 63 | DRESULT disk_readp (
|
---|
| 64 | BYTE* dest, /* Pointer to the destination object */
|
---|
| 65 | DWORD sector, /* Sector number (LBA) */
|
---|
| 66 | WORD sofs, /* Offset in the sector */
|
---|
| 67 | WORD count /* Byte count (bit15:destination) */
|
---|
| 68 | )
|
---|
| 69 | {
|
---|
| 70 | // TODO implement caching system
|
---|
| 71 | // * If sector is reachable with current LBA, don't change it
|
---|
| 72 |
|
---|
| 73 | if (prevsec != sector)
|
---|
| 74 | {
|
---|
[1b74fa2] | 75 | map_sector(sector, 0);
|
---|
[645c148] | 76 | prevsec = sector;
|
---|
| 77 | // Read sector
|
---|
| 78 | read(255,1,secbuf);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | // Copy useful bytes to dest.
|
---|
| 82 | sofs += (int)(secbuf);
|
---|
| 83 | for(int j = 0; j <count;++j)
|
---|
| 84 | {
|
---|
| 85 | *(char*)(dest+j) = *(char*)(j+sofs);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | return RES_OK;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | /*-----------------------------------------------------------------------*/
|
---|
| 94 | /* Write Partial Sector */
|
---|
| 95 | /*-----------------------------------------------------------------------*/
|
---|
| 96 |
|
---|
| 97 | DRESULT disk_writep (
|
---|
| 98 | const BYTE* buff,/* Pointer to the data to be written, NULL:Initiate/Finalize write operation */
|
---|
| 99 | DWORD sc /* Sector number (LBA) or Number of bytes to send */
|
---|
| 100 | )
|
---|
| 101 | {
|
---|
[1b74fa2] | 102 | static WORD ptr;
|
---|
| 103 | static char* wrbuf[256];
|
---|
| 104 | // Separate buffer because we need to use map_sector, and it kills secbuf
|
---|
[645c148] | 105 |
|
---|
| 106 | if (!buff) {
|
---|
| 107 | if (sc) {
|
---|
| 108 | // Initiate write process
|
---|
[1b74fa2] | 109 | prevsec = sc;
|
---|
| 110 | ptr = 0;
|
---|
[645c148] | 111 | } else {
|
---|
[1b74fa2] | 112 | // Called with both param = 0 - flush buffer to disk
|
---|
| 113 | // First make sure it's zero-filled
|
---|
| 114 | for(;ptr < 512; ++ptr)
|
---|
| 115 | {
|
---|
| 116 | *(wrbuf + ptr) = 0;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | // map in the sector (no need to read from SD)
|
---|
| 120 | map_sector(prevsec, 0xA5);
|
---|
| 121 |
|
---|
| 122 | write(255,1,wrbuf);
|
---|
[645c148] | 123 | }
|
---|
| 124 | } else {
|
---|
[1b74fa2] | 125 | // Here SC is a bytecount. copy that much bytes to the buffer
|
---|
| 126 | for(int j = 0; j <sc;++j)
|
---|
| 127 | {
|
---|
| 128 | *(char*)(wrbuf+ptr) = *(char*)(ptr++ + buff);
|
---|
| 129 | }
|
---|
[645c148] | 130 | }
|
---|
| 131 |
|
---|
[1b74fa2] | 132 | return RES_OK;
|
---|
[645c148] | 133 | }
|
---|
| 134 |
|
---|