You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
563 lines
16 KiB
563 lines
16 KiB
#include "hardware.h"
|
|
|
|
int getDriveGeometry( unsigned char DriveID, CHS_DRIVE_GEOMETRY *ptr ){
|
|
if( ptr == NULL ) return -1;
|
|
__dpmi_regs r;
|
|
r.h.ah = DriveID;
|
|
__dpmi_int( 0x13, &r );
|
|
printf( "Drive[0x%02X] Geometry Data\nAX: 0x%04X\nBX: 0x%04X\nCX: 0x%04X\nDX: 0x%04X\n", DriveID, r.x.ax, r.x.bx, r.x.cx, r.x.dx );
|
|
/*
|
|
BL = drive type (AT/PS2 floppies only) (see #00242)
|
|
CH = low eight bits of maximum cylinder number
|
|
CL = maximum sector number (bits 5-0)
|
|
high two bits of maximum cylinder number (bits 7-6)
|
|
DH = maximum head number
|
|
DL = number of drives */
|
|
ptr->drive_type = r.h.bl;
|
|
ptr->max_drives = r.h.dl;
|
|
ptr->c = ( r.h.cl >> 6 ) | r.h.ch;
|
|
ptr->s = r.h.cl & 0x3F;
|
|
ptr->h = r.h.dh;
|
|
printf("Drive [%d] info (C:%d,H:%d,S:%d) mas drives:%d\n",
|
|
ptr->drive_type,
|
|
ptr->c,
|
|
ptr->h,
|
|
ptr->s,
|
|
ptr->max_drives
|
|
);
|
|
return 0;
|
|
};
|
|
/*
|
|
00 dword 32 bit starting sector
|
|
04 word number of sectors to read
|
|
06 word pointer to offset of buffer
|
|
08 word pointer to segment of buffer
|
|
|
|
*/
|
|
|
|
#ifdef __cplusplus
|
|
#pragma pack(1)
|
|
#endif
|
|
|
|
typedef struct S_DOS_IO_CTL_BLOCK{
|
|
uint32_t start_sector;
|
|
uint16_t num_sectors;
|
|
uint32_t buffer_ptr;
|
|
}DOS_IO_CTL_BLOCK;
|
|
|
|
int absread( int diskID, int nsects, long lsect, void *buffer ){
|
|
|
|
/*
|
|
int16_t 25 - Absolute Disk Read
|
|
|
|
AL = logical drive number (0=A:, 1=B:, 2=C:, ...)
|
|
CX = number of sectors to read
|
|
= -1 if DOS 4.x (control block pointer is in DS:BX)
|
|
DX = starting logical sector number (see below for DOS 4.x+)
|
|
DS:BX = pointer to data buffer OR pointer to control block (DOS 4.x+, see below)
|
|
|
|
on return:
|
|
AH = error code if CF set:
|
|
01 bad command
|
|
02 bad address mark
|
|
03 write protect
|
|
04 sector not found
|
|
08 DMA failure
|
|
10 data error (bad CRC)
|
|
20 controller failed
|
|
40 seek failed
|
|
80 attachment failed to respond
|
|
AL = BIOS error code if CF set
|
|
00 write protect error
|
|
01 unknown unit
|
|
02 drive not ready
|
|
03 unknown command
|
|
04 data error (bad CRC)
|
|
05 bad request structure length
|
|
06 seek error
|
|
07 unknown media type
|
|
08 sector not found
|
|
0A write fault
|
|
0B read fault
|
|
0C general failure
|
|
|
|
Control Block Format (DOS 4.x):
|
|
|
|
Offset Size Description
|
|
|
|
00 dword starting sector
|
|
04 word number of sectors to read
|
|
06 dword pointer to buffer
|
|
|
|
|
|
- reads disk sectors into buffer at DS:BX or DS:[BX+6]
|
|
- after calling this interrupt the flags register remains on the
|
|
stack and must be popped manually
|
|
- sectors are logical sectors starting at the beginning of a
|
|
logical disk; each DOS partition on a drive unit is considered
|
|
one logical drive and has it's own logical sector numbers with
|
|
track 0 starting at the first track in the partition
|
|
- this function uses logical drives, and is susceptible to ASSIGN
|
|
- physical sector numbers can be converted to and from DOS sector
|
|
numbers with the following formulas:
|
|
|
|
dos_sector = (sector - 1) + (head * sectors_per_track) +
|
|
(track * sectors_per_track * num_heads)
|
|
|
|
physical_sector = 1 + (dos_sector MOD sectors_per_track)
|
|
physical_head = (dos_sector / sectors_per_track) MOD num_heads
|
|
physical_track = dos_sector / (sectors_per_track * num_heads)
|
|
|
|
*/
|
|
|
|
__dpmi_regs r;
|
|
int tLA = 0;
|
|
int dos_segment = 0;
|
|
int dos_selector = 0;
|
|
DOS_IO_CTL_BLOCK ctlBock;
|
|
r.h.al = diskID & 0xff;
|
|
r.x.cx = 0xFFFF;
|
|
ctlBock.num_sectors = nsects;
|
|
ctlBock.start_sector = lsect;
|
|
if( ( nsects * 512 ) > __tb_size ){
|
|
dos_segment = __dpmi_allocate_dos_memory( ((( nsects * 512 + sizeof(DOS_IO_CTL_BLOCK) )+15)>>4), &dos_selector );
|
|
if( dos_segment == 0 ) return -1; /* undefined error */
|
|
r.x.ds = dos_segment;
|
|
r.x.bx = 0;
|
|
tLA = r.x.ds << 4;
|
|
} else {
|
|
tLA = __tb;
|
|
r.x.ds = tLA >> 4;
|
|
r.x.bx = tLA & 0xf;
|
|
};
|
|
ctlBock.buffer_ptr = ((( tLA + sizeof( DOS_IO_CTL_BLOCK ) ) >> 4 ) << 16 ) | (( tLA + sizeof( DOS_IO_CTL_BLOCK ) ) & 0xf );
|
|
dosmemput( (void *) &ctlBock, sizeof(DOS_IO_CTL_BLOCK), tLA );
|
|
if( __dpmi_int( 0x25, &r ) == 0 && ( r.x.flags & 1 ) == 0 ){
|
|
dosmemget( tLA+sizeof(DOS_IO_CTL_BLOCK), 512*nsects, buffer );
|
|
if( dos_segment ) __dpmi_free_dos_memory( dos_selector );
|
|
} else {
|
|
printf( "Dos Error:");
|
|
switch( r.h.ah ) {
|
|
case 0x01: printf("bad command\n"); break;
|
|
case 0x02: printf("bad address mark\n"); break;
|
|
case 0x03: printf("write protect\n"); break;
|
|
case 0x04: printf("sector not found\n"); break;
|
|
case 0x08: printf("DMA failure\n"); break;
|
|
case 0x10: printf("data error (bad CRC)\n"); break;
|
|
case 0x20: printf("controller failed\n"); break;
|
|
case 0x40: printf("seek failed\n"); break;
|
|
case 0x80: printf("attachment failed to respond\n"); break;
|
|
};
|
|
printf( "BIOS Error:");
|
|
switch( r.h.ah ) {
|
|
case 0x00: printf("write protect error\n"); break;
|
|
case 0x01: printf("unknown unit\n"); break;
|
|
case 0x02: printf("drive not ready\n"); break;
|
|
case 0x03: printf("unknown command\n"); break;
|
|
case 0x04: printf("data error (bad CRC)\n"); break;
|
|
case 0x05: printf("bad request structure length\n"); break;
|
|
case 0x06: printf("seek error\n"); break;
|
|
case 0x07: printf("unknown media type\n"); break;
|
|
case 0x08: printf("sector not found\n"); break;
|
|
case 0x0A: printf("write fault\n"); break;
|
|
case 0x0B: printf("read fault\n"); break;
|
|
case 0x0C: printf("general failure\n"); break;
|
|
};
|
|
if( dos_segment ) __dpmi_free_dos_memory( dos_selector );
|
|
return -1;
|
|
};
|
|
return 0;
|
|
};
|
|
/*
|
|
|
|
int16_t 26 - Absolute Disk Write
|
|
|
|
AL = logical drive number (0=A:, 1=B:, 2=C:, ...)
|
|
CX = number of sectors to write
|
|
= -1 if DOS 4.x (control block pointer is in DS:BX)
|
|
DX = starting logical sector number (see below for DOS 4.x+)
|
|
DS:BX = pointer to data buffer
|
|
= pointer to control block (DOS 4.x+, see below)
|
|
|
|
on return:
|
|
AH = error code if CF set:
|
|
01 bad command
|
|
02 bad address mark
|
|
03 write protect
|
|
04 sector not found
|
|
08 DMA failure
|
|
10 data error (bad CRC)
|
|
20 controller failed
|
|
40 seek failed
|
|
80 attachment failed to respond
|
|
AL = BIOS error code if CF set
|
|
00 write protect error
|
|
01 unknown unit
|
|
02 drive not ready
|
|
03 unknown command
|
|
04 data error (bad CRC)
|
|
05 bad request structure length
|
|
06 seek error
|
|
07 unknown media type
|
|
08 sector not found
|
|
0A write fault
|
|
0B read fault
|
|
0C general failure
|
|
|
|
Control Block Format (DOS 4.x):
|
|
|
|
Offset Size Description
|
|
|
|
00 dword starting sector
|
|
04 word number of sectors to write
|
|
06 dword pointer to buffer
|
|
|
|
|
|
- writes disk sectors from buffer at DS:BX or DS:[BX+6]
|
|
- after calling this interrupt the flags register remains on the
|
|
stack and must be popped manually
|
|
- sectors are logical sectors starting at the beginning of a
|
|
logical disk; each DOS partition on a drive unit is considered
|
|
one logical drive and has it's own logical sector numbers with
|
|
track 0 starting at the first track in the partition
|
|
- this function uses logical drives, and is susceptible to ASSIGN
|
|
- physical sector numbers can be converted to and from DOS sector
|
|
numbers with the following formulas:
|
|
|
|
dos_sector = (sector - 1) + (head * sectors_per_track) +
|
|
(track * sectors_per_track * num_heads)
|
|
|
|
physical_sector = 1 + (dos_sector MOD sectors_per_track)
|
|
physical_head = (dos_sector / sectors_per_track) MOD num_heads
|
|
physical_track = dos_sector / (sectors_per_track * num_heads)
|
|
|
|
|
|
*/
|
|
int abswrite(int diskID, int nsects, long lsect, void *buffer ){
|
|
if( buffer == NULL ) return -1;
|
|
__dpmi_regs r;
|
|
int tLA = 0;
|
|
int dos_segment = 0;
|
|
int dos_selector = 0;
|
|
DOS_IO_CTL_BLOCK ctlBock;
|
|
r.h.al = diskID & 0xff;
|
|
r.x.cx = 0xFFFF;//nsects;
|
|
//r.x.dx = lsect;
|
|
ctlBock.num_sectors = nsects;
|
|
ctlBock.start_sector = lsect;
|
|
if( ( nsects * 512 ) > __tb_size ){
|
|
dos_segment = __dpmi_allocate_dos_memory( ((( nsects * 512 )+15)>>4), &dos_selector );
|
|
if( dos_segment == 0 ) return -1; /* undefined error */
|
|
r.x.ds = dos_segment;
|
|
r.x.bx = 0;
|
|
tLA = r.x.ds << 4;
|
|
} else {
|
|
tLA = __tb;
|
|
r.x.ds = tLA >> 4;
|
|
r.x.bx = tLA & 0xf;
|
|
};
|
|
ctlBock.buffer_ptr = ((( tLA + sizeof( DOS_IO_CTL_BLOCK ) ) >> 4 ) << 16 ) | (( tLA + sizeof( DOS_IO_CTL_BLOCK ) ) & 0xf );
|
|
dosmemput( (void *) &ctlBock, sizeof(DOS_IO_CTL_BLOCK), tLA );
|
|
dosmemput( buffer, 512*nsects, tLA+sizeof(DOS_IO_CTL_BLOCK) );
|
|
if( __dpmi_int( 0x26, &r ) == 0 && ( r.x.flags & 1 ) == 0 ){
|
|
if( dos_segment ) __dpmi_free_dos_memory( dos_selector );
|
|
} else {
|
|
printf( "Dos Error:");
|
|
switch( r.h.ah ) {
|
|
case 0x01: printf("bad command\n"); break;
|
|
case 0x02: printf("bad address mark\n"); break;
|
|
case 0x03: printf("write protect\n"); break;
|
|
case 0x04: printf("sector not found\n"); break;
|
|
case 0x08: printf("DMA failure\n"); break;
|
|
case 0x10: printf("data error (bad CRC)\n"); break;
|
|
case 0x20: printf("controller failed\n"); break;
|
|
case 0x40: printf("seek failed\n"); break;
|
|
case 0x80: printf("attachment failed to respond\n"); break;
|
|
};
|
|
printf( "BIOS Error:");
|
|
switch( r.h.ah ) {
|
|
case 0x00: printf("write protect error\n"); break;
|
|
case 0x01: printf("unknown unit\n"); break;
|
|
case 0x02: printf("drive not ready\n"); break;
|
|
case 0x03: printf("unknown command\n"); break;
|
|
case 0x04: printf("data error (bad CRC)\n"); break;
|
|
case 0x05: printf("bad request structure length\n"); break;
|
|
case 0x06: printf("seek error\n"); break;
|
|
case 0x07: printf("unknown media type\n"); break;
|
|
case 0x08: printf("sector not found\n"); break;
|
|
case 0x0A: printf("write fault\n"); break;
|
|
case 0x0B: printf("read fault\n"); break;
|
|
case 0x0C: printf("general failure\n"); break;
|
|
};
|
|
if( dos_segment ) __dpmi_free_dos_memory( dos_selector );
|
|
return -1;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
|
|
/*!
|
|
\arg \c idx number of 32x port.
|
|
\details Reads North bridge register. The \a idx is port number to read 32 bit value from.
|
|
*/
|
|
unsigned long read_nb(unsigned char idx){
|
|
unsigned long retval;
|
|
#ifdef __DJGPP__
|
|
// https://www.dsl-ltd.co.uk/applications/accessing-north-bridge-south-bridge-registers-vortex86/
|
|
outportl( 0xcf8, ( 0x80000000L | idx ) );
|
|
retval = inportl( 0xcfc );
|
|
#else
|
|
_asm mov dx, 0cf8h
|
|
__emit__(0x66); __emit__(0xb8);
|
|
__emit__(0x00); __emit__(0x00); __emit__(0x00); __emit__(0x80);
|
|
_asm mov al, idx
|
|
__emit__(0x66); _asm out dx, ax
|
|
_asm mov dx, 0cfch
|
|
__emit__(0x66); _asm in ax, dx
|
|
__emit__(0x66); _asm mov uint16_t PTR retval, ax
|
|
#endif
|
|
return retval;
|
|
};
|
|
|
|
// Write north bridge register
|
|
void write_nb(unsigned char idx, unsigned long val){
|
|
#ifdef __DJGPP__
|
|
#else
|
|
_asm mov dx, 0cf8h
|
|
__emit__(0x66); __emit__(0xb8);
|
|
__emit__(0x00); __emit__(0x00); __emit__(0x00); __emit__(0x80);
|
|
_asm mov al, idx
|
|
__emit__(0x66); __emit__(0xef);
|
|
_asm mov dx, 0cfch
|
|
__emit__(0x66); __emit__(0x8b); __emit__(0x46); __emit__(0x08);
|
|
__emit__(0x66); __emit__(0xef);
|
|
#endif
|
|
};
|
|
// Read south bridge register
|
|
unsigned long read_sb(unsigned char idx){
|
|
unsigned long retval;
|
|
#ifdef __DJGPP__
|
|
#else
|
|
_asm mov dx, 0cf8h
|
|
//80009000h
|
|
// mov eax, 80003800h
|
|
__emit__(0x66); __emit__(0xb8);
|
|
//__emit__(0x00); __emit__(0x38); __emit__(0x00); __emit__(0x80);
|
|
__emit__(0x00); __emit__(0x90); __emit__(0x00); __emit__(0x80);
|
|
_asm mov al, idx
|
|
// out edx, eax
|
|
__emit__(0x66); _asm out dx, ax
|
|
|
|
_asm mov dx, 0cfch
|
|
// in eax, edx
|
|
__emit__(0x66); _asm in ax, dx
|
|
// mov retval, eax
|
|
__emit__(0x66); _asm mov uint16_t PTR retval, ax
|
|
#endif
|
|
return retval;
|
|
};
|
|
|
|
// Write south bridge register
|
|
/*void write_sb(unsigned char idx, unsigned long val){
|
|
__asm{
|
|
mov dx, 0cf8h
|
|
mov eax, 80009000h
|
|
mov al, idx
|
|
out dx, eax
|
|
mov dx, 0cfch
|
|
mov eax, DWORD PTR val
|
|
out dx, eax
|
|
}
|
|
};
|
|
*/
|
|
|
|
void make_ws8( void ){
|
|
unsigned long retval;
|
|
#ifdef __DJGPP__
|
|
#else
|
|
_asm mov dx, 0cf8h
|
|
__emit__(0x66); __emit__(0xb8);
|
|
__emit__(0x50); __emit__(0x90); __emit__(0x00); __emit__(0x80);
|
|
__emit__(0x66); _asm out dx, ax
|
|
|
|
_asm mov dx, 0cfch
|
|
__emit__(0x66); _asm in ax, dx
|
|
__emit__(0x66); _asm mov uint16_t PTR retval, ax
|
|
|
|
retval = retval | 0x0000F000UL;
|
|
|
|
_asm mov dx, 0cf8h
|
|
__emit__(0x66); __emit__(0xb8);
|
|
__emit__(0x50); __emit__(0x90); __emit__(0x00); __emit__(0x80);
|
|
__emit__(0x66); _asm out dx, ax
|
|
|
|
_asm mov dx, 0cfch
|
|
__emit__(0x66); _asm mov ax, uint16_t PTR retval
|
|
__emit__(0x66); _asm out dx, ax
|
|
#endif
|
|
};
|
|
// Write south bridge register
|
|
void write_sb( unsigned char idx, unsigned long val ){
|
|
#ifdef __DJGPP__
|
|
#else
|
|
_asm mov dx, 0cf8h
|
|
//80009000h
|
|
// mov eax, 80003800h
|
|
__emit__(0x66); __emit__(0xb8);
|
|
//__emit__(0x00); __emit__(0x38); __emit__(0x00); __emit__(0x80);
|
|
__emit__(0x00); __emit__(0x90); __emit__(0x00); __emit__(0x80);
|
|
_asm mov al, idx
|
|
// out dx, eax
|
|
__emit__(0x66); __emit__(0xef);
|
|
_asm mov dx, 0cfch
|
|
// mov eax, val
|
|
__emit__(0x66); __emit__(0x8b); __emit__(0x46); __emit__(0x08);
|
|
// out dx, eax
|
|
__emit__(0x66); __emit__(0xef);
|
|
#endif
|
|
};
|
|
unsigned char read_sbb( unsigned char idx ){
|
|
unsigned char retval;
|
|
#ifdef __DJGPP__
|
|
#else
|
|
_asm mov dx, 0cf8h
|
|
// mov eax, 80009000h
|
|
__emit__(0x66); __emit__(0xb8);
|
|
__emit__(0x00); __emit__(0x90); __emit__(0x00); __emit__(0x80);
|
|
_asm mov al, idx
|
|
// out edx, eax
|
|
//__emit__(0x66); __emit__(0xef);
|
|
__emit__(0x66); _asm out dx, ax
|
|
/*_asm mov dx, 0cfch
|
|
// in eax, edx
|
|
__emit__(0x66); _asm in al, dx
|
|
// mov retval, eax
|
|
__emit__(0x66); _asm mov uint8_t PTR retval, al
|
|
*/
|
|
retval = inportb( 0x0CFC );
|
|
#endif
|
|
return retval;
|
|
};
|
|
unsigned int read_sbw( unsigned char idx ){
|
|
unsigned int retval;
|
|
#ifdef __DJGPP__
|
|
#else
|
|
_asm mov dx, 0cf8h
|
|
// mov eax, 80009000h
|
|
__emit__(0x66); __emit__(0xb8);
|
|
__emit__(0x00); __emit__(0x90); __emit__(0x00); __emit__(0x80);
|
|
_asm mov al, idx
|
|
// out edx, eax
|
|
//__emit__(0x66); __emit__(0xef);
|
|
__emit__(0x66); _asm out dx, ax
|
|
/*_asm mov dx, 0cfch
|
|
// in eax, edx
|
|
__emit__(0x66); _asm in al, dx
|
|
// mov retval, eax
|
|
__emit__(0x66); _asm mov uint8_t PTR retval, al
|
|
*/
|
|
retval = inport( 0x0CFC );
|
|
#endif
|
|
return retval;
|
|
};
|
|
// Write south bridge register
|
|
void write_sbb( unsigned char idx, unsigned char val ){
|
|
#ifdef __DJGPP__
|
|
#else
|
|
_asm mov dx, 0cf8h
|
|
//80009000h
|
|
// mov eax, 80003800h
|
|
__emit__(0x66); __emit__(0xb8);
|
|
//__emit__(0x00); __emit__(0x38); __emit__(0x00); __emit__(0x80);
|
|
__emit__(0x00); __emit__(0x90); __emit__(0x00); __emit__(0x80);
|
|
_asm mov al, idx
|
|
// out dx, eax
|
|
__emit__(0x66); __emit__(0xef);
|
|
|
|
outportb( 0x0CFC, val );
|
|
/*_asm mov dx, 0cfch
|
|
// mov eax, val
|
|
__emit__(0x66); __emit__(0x8b); __emit__(0x46); __emit__(0x08);
|
|
// out dx, eax
|
|
__emit__(0x66); __emit__(0xef);*/
|
|
#endif
|
|
};
|
|
|
|
#define ZFLINDEX 0x0218
|
|
#define ZFLDATA8 0x0219
|
|
#define ZFLDATA16 0x021A
|
|
|
|
void ZFLWriteb( unsigned char index, unsigned char value ){
|
|
outportb( ZFLINDEX, index );
|
|
outportb( ZFLDATA8, value );
|
|
/*asm
|
|
{
|
|
mov AL,index
|
|
mov DX,ZFLINDEX
|
|
out DX,AL
|
|
mov DX,ZFLDATA8
|
|
mov AL,value
|
|
out DX,AL
|
|
}*/
|
|
};
|
|
|
|
void ZFLWritew( unsigned char index, unsigned int value ){
|
|
outportb( ZFLINDEX, index );
|
|
outportw( ZFLDATA16, value );
|
|
/* asm
|
|
{
|
|
mov AL,index
|
|
mov DX,ZFLINDEX
|
|
out DX,AL
|
|
mov DX,ZFLDATA16
|
|
mov AX,value
|
|
out DX,AX
|
|
} */
|
|
};
|
|
|
|
unsigned char ZFLReadb( unsigned char index ){
|
|
outportb( ZFLINDEX, index );
|
|
return inportb( ZFLDATA8 );
|
|
/* asm
|
|
{
|
|
mov AL,index
|
|
mov DX,ZFLINDEX
|
|
out DX,AL
|
|
mov DX,ZFLDATA8
|
|
in AL,DX
|
|
}
|
|
return _AL;
|
|
*/
|
|
};
|
|
|
|
unsigned int ZFLReadw( unsigned char index ){
|
|
outportb( ZFLINDEX, index );
|
|
return inportw( ZFLDATA16 );
|
|
};
|
|
|
|
void ZFLOrb( unsigned char index, unsigned char mask ){
|
|
/*asm
|
|
{
|
|
mov AL,index
|
|
mov DX,ZFLINDEX
|
|
out DX,AL
|
|
mov DX,ZFLDATA8
|
|
in AL,DX
|
|
or AL,mask
|
|
out DX,AL
|
|
}*/
|
|
outportb( ZFLDATA8, ZFLReadb(index) | mask );
|
|
};
|
|
|
|
|
|
|
|
uint64_t rdtsc( void ){
|
|
uint32_t low, high;
|
|
asm volatile("rdtsc":"=a"(low),"=d"(high));
|
|
return ((uint64_t)high << 32) | low;
|
|
}
|
|
|
|
#ifdef __cplusplus
|
|
#pragma pack()
|
|
#endif
|
|
|