summaryrefslogtreecommitdiff
path: root/bootblocks/min_buf.c
blob: 797ee7bc9aa5766af63af30395e61b9085134db2 (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

#include "monitor.h"

#ifdef MINI_BUF
#ifndef MAXTRK
#define MAXTRK	21
#endif

int disk_drive = 0;
int disk_spt   = 7;
int disk_heads = 0;
int disk_cyls  = 0;
int bad_track  = -1;

static int  track_no = -1;
static int  buf_len = 0;
static char buffer[MAXTRK*512];

void reset_disk()
{
   disk_spt   = 7;
   disk_heads = 0;
   disk_cyls  = 0;
   bad_track  = -1;
}

char * read_lsector(sectno)
long sectno;
{
   int tries = 5;
   int rv = 0;

   int phy_s = 1;
   int phy_h = 0;
   int phy_c = 0; 
   int ltrack;

   if( sectno == 0 ) reset_disk();
   if( buf_len != disk_spt ) track_no = -1;

   if( disk_spt < 1 || disk_heads < 1 )
      phy_s = sectno;
   else
   {
      phy_s = sectno%disk_spt;
      phy_h = sectno/disk_spt%disk_heads;
      phy_c = sectno/disk_spt/disk_heads;
   }

#ifdef DEBUG
   fprintf(stderr, "read_sector(%ld = %d,%d,%d)\n", sectno, phy_c, phy_h, phy_s);
#endif

   ltrack = phy_c*disk_heads+phy_h;
   if( disk_spt > 1 && disk_spt <= MAXTRK
    && track_no != ltrack && ltrack != bad_track)
   {
     rv = phy_read(disk_drive, phy_c, phy_h, 1, disk_spt, buffer);
     if( rv == 0 )
     {
        track_no = ltrack;
        buf_len  = disk_spt;
     }
     else
        bad_track = ltrack;
   }
   if( track_no == ltrack )
      return buffer + phy_s * 512;

   do
   {
     rv = phy_read(disk_drive, phy_c, phy_h, phy_s+1, 1, buffer);
     tries--;
   }
   while(rv && tries > 0);
   if( rv ) printf("Disk error 0x%02x %d:%d:%d:%d[%2d] -> 0x%04x[]\n",
	            rv, disk_drive, phy_c, phy_h, phy_s+1, 1, buffer);

   if(rv) return 0; else return buffer;
}

#if defined(__MSDOS__) || defined(__STANDALONE__)
phy_read(drive, cyl, head, sect, length, buffer)
{
#asm
  push	bp
  mov	bp,sp

  push	ds
  pop	es

  mov	dl,[bp+2+_phy_read.drive]
  mov	ch,[bp+2+_phy_read.cyl]
  mov	dh,[bp+2+_phy_read.head]
  mov	cl,[bp+2+_phy_read.sect]
  mov	al,[bp+2+_phy_read.length]
  mov	bx,[bp+2+_phy_read.buffer]

  mov	ah,#$02
  int	$13
  jc	read_err
  mov	ax,#0
read_err:

  pop	bp
#endasm
}
#endif

#endif