source: thomson/code/C/HxCHost/pff/diskio.c@ 7cabd49

main
Last change on this file since 7cabd49 was 7cabd49, checked in by Adrien Destugues <pulkomandy@…>, 12 years ago

HxC host software : work at Forever XIII party

  • Switch to lwtools gcc toolchain
  • Fix some bugs.
  • Editing the HxC settings works
  • Selecting a floppy image works (sometimes)

git-svn-id: svn://localhost/thomson@24 85ae3b6b-dc8f-4344-a89d-598714f2e4e5

  • Property mode set to 100644
File size: 3.5 KB
Line 
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/*******/
12extern unsigned char* secbuf;
13extern unsigned char mark[];
14
15static DWORD prevsec = -1;
16/*******/
17
18/*-----------------------------------------------------------------------*/
19/* Initialize Disk Drive */
20/*-----------------------------------------------------------------------*/
21
22DSTATUS 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
39void 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
58
59/*-----------------------------------------------------------------------*/
60/* Read Partial Sector */
61/*-----------------------------------------------------------------------*/
62
63DRESULT 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 {
75 map_sector(sector, 0);
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
97DRESULT disk_writep (
98 const BYTE* buff,
99 /* Pointer to the data to be written,
100 * NULL:Initiate/Finalize write operation */
101 DWORD sc /* Sector number (LBA) or Number of bytes to send */
102)
103{
104 static WORD ptr;
105 static char wrbuf[256];
106 // Separate buffer because we need to use map_sector, and it kills secbuf
107
108 if (!buff) {
109 if (sc) {
110 // Initiate write process
111 prevsec = sc;
112 ptr = 0;
113 } else {
114 // Called with both param = 0 - flush buffer to disk
115 // First make sure it's zero-filled
116 for(;ptr < 512; ++ptr)
117 {
118 wrbuf[ptr] = 0;
119 }
120
121 // map in the sector (no need to read from SD)
122 map_sector(prevsec, 0xA5);
123
124 write(255,1,wrbuf);
125 }
126 } else {
127 // Here SC is a bytecount. copy that much bytes to the buffer
128 for(int j = 0; j <sc;++j)
129 {
130 wrbuf[ptr] = *(char*)(j+buff);
131 ++ptr;
132 }
133 mon_putc(' ');
134 }
135
136 return RES_OK;
137}
138
Note: See TracBrowser for help on using the repository browser.