summaryrefslogtreecommitdiff
path: root/rbfloppy.c
blob: 3202e2719cfd4f3f655340e1bd671e1b3231182c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*       Rainbow Image Utilities
 *       Copyright © 2008 Jeffrey Armstrong <jeff@rainbow-100.com>
 *       http://jeff.rainbow-100.com/
 *
 *       This program is free software: you can redistribute it and/or modify
 *       it under the terms of the GNU General Public License as published by
 *       the Free Software Foundation, either version 3 of the License, or
 *       (at your option) any later version.
 *       
 *       This program is distributed in the hope that it will be useful
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU General Public License for more details.
 *       
 *       You should have received a copy of the GNU General Public License
 *       along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* rbfloppy.obj - A collection of direct disk access routines
 * by Jeff Armstrong
 */

#include <dos.h>
#include "rbfloppy.h"
#include <stdio.h>

/* Structure that represents the block of data needed to call interrupt
 * 0x65, which allows direct disk access.  The order here is very
 * important.
 */
struct dsk_bios_block {
	unsigned char command;
	unsigned char drive_id;
	unsigned char sector;
	unsigned char phys_unit;
	unsigned int track;
        unsigned int sector_count;
	unsigned int data;
        unsigned int segment;
};

/* The map that is used to skew/deskew sector addresses */
static int skew[11] = {0,1,6,2,7,3,8,4,9,5,10};

/* The current program addressing mode */
static int current_mode = MODE_PHYSICAL;

/* An internal method to unskew a given sector number */
int unskew(int sector);

/* Writes a given sector using the data in buffer */
int write_sector(int unit, int track, int sector, char *buffer)
{
        return access_sector(CMD_WRITE_VERIFY, unit, track, sector, buffer);
}

/* Reads a specified sector from disk into buffer */
int read_sector(int unit, int track, int sector, char *buffer)
{
        return access_sector(CMD_READ, unit, track, sector, buffer);
}

/* Internal routine that performs all actual disk access through calls
 * to interrupt 0x65.  Some command ints are already #defined, but 
 * a few more exist that aren't listed here.
 */
int access_sector(int command, int unit, int track, int sector, char *buffer)
{
struct dsk_bios_block write;
struct SREGS segregs;
union REGS r;

char *pt;

        /* Sectors are numbered 1 to 10 */
        if(sector > 10)
                return ERROR_BAD_SECTOR;

        /* The current data segment is needed for the interrupt call */
        segread(&segregs);

        write.command = (unsigned char)command;
        write.drive_id = (unsigned char)0xFF;
        write.track = (unsigned int)track;

        /* Determine the sector if skewing is necessary */
        if(track > 1 && get_mode() == MODE_PHYSICAL)
                write.sector = (unsigned char)unskew(sector);
        else
                write.sector = (unsigned char)sector;
                
        write.sector_count = (unsigned int)1;
        write.phys_unit = (unsigned char)(0+unit*16);

        write.data = (unsigned int)buffer;
        write.segment = segregs.ds;

        /* Only for debugging */
        pt = &write;

#ifdef DEBUG
        printf("Sz: %d: 0:%d 1:%d 2:%d 3:%d 4:%d 5:%d 6:%d 7:%d of:%d ds%d\n",
               sizeof(write),pt[0],pt[1],pt[2],pt[3],pt[4]+pt[5]*256,
               pt[6]+pt[7]*256,pt[8]+pt[9]*256,pt[10]+pt[11]*256,
               &buffer[0],segregs.ds);
#endif

        r.x.bx = &write;
        
        int86(FLOPPY_INTR,&r,&r);

        if(r.h.al == 0)
                return OK;
        else
                return ERROR_FROM_BIOS;
}

/* Determines the unit number for access_sector() from a given character.
 * Expects a drive letter or a number.  Returns -1 on error.
 */
int drive_id(char id)
{
int drv;

        switch(id) {
                case 'A':
                case 'a':
                case 0:
                case '0':
                        drv = 0;
                        break;
                case 'B':
                case 'b':
                case 1:
                case '1':
                        drv = 1;
                        break;
                case 'C':
                case 'c':
                case 2:
                case '2':
                        drv = 2;
                        break;
                case 'D':
                case 'd':
                case 3:
                case '3':
                        drv = 3;
                        break;
                default:
                        drv = -1;
        }

        return drv;
}

/* Returns an unskewed sector number from a skewed address */
int unskew(int sector) {
        return sector <= 10 && sector > 0 ? skew[sector] : 1;
}

/* Sets the mode of the sector addressing to either physical or
 * logical by assigning a static variable.
 */
void set_mode(int mode)
{
        if(mode == MODE_PHYSICAL || mode == MODE_LOGICAL)
                current_mode = mode;
}

/* Returns the current addressing mode */
int get_mode()
{
        return current_mode;
}