📄 m_hdrv.c
字号:
/*********************************************************************
* FILENAME: $RCSfile: m_hdrv.c,v $
* VERSION : $Revision: 1.2 $
* DATE : $Date: 2000/11/03 19:15:48 $
* Copyright (c) 1997-2000 Texas Instruments Incorporated
*
* Hard Disk Drive Simulation Example:
*____________________________________________________________________
* - Uses ONE output channel.
*
* - This is the module to be run on the TARGET.
* - This program is meant to be used with the RTDX display
* hddprog.exe
********************************************************************/
#include <stdio.h> /* fprintf(), puts() */
#include <stdlib.h> /* abort(), rand() */
#include <time.h> /* time() */
#include <math.h> /* sqrt() */
#include <rtdx.h> /* RTDX */
#include "target.h" /* TARGET_INITIALIZE() */
/* ===================================================================
* Typedefs & Global Data structures
* ================================================================ */
#define Test_Iterations 100
#define Max_Cyl 1000
#define Typical_Seek_Time 100 /* in .1 ms */
#define Max_Settle_Time 10 /* in .1 ms */
#define Max_Seek_Fail 20 /* FAIL rate: 1 in MAX_FAIL */
typedef enum {FALSE, TRUE} BOOL;
int Get_Next_Random_Cyl( const int max_cyl );
int Get_Cyl_Dist( const int cyl1, const int cyl2 );
int Get_Seek_Time( const int cyl_dist );
int Get_Settle_Time( void );
BOOL Get_Seek_Success( void );
/* Declare and initialize an output channel called "ochan" */
RTDX_CreateOutputChannel(ochan);
/* ================================================================ */
void main ( void )
{
int Current_Cyl = 0; /* current cylinder location */
int i; /* looping variable */
struct {
int Cyl; /* Random cylinder to find */
int Cyl_Dist; /* cylinder distance traveled */
int Time_Seek; /* Time to seek cylinder */
int Time_Settle; /* Time to settle on cylinder */
BOOL Seek_Pass; /* Indicates pass/fail of seek */
} chan_data;
/* Target initialization for RTDX */
TARGET_INITIALIZE();
for( i = 0; i < Test_Iterations; i++ ) {
chan_data.Cyl = Get_Next_Random_Cyl(Max_Cyl);
chan_data.Cyl_Dist = Get_Cyl_Dist(Current_Cyl,chan_data.Cyl);
chan_data.Time_Seek = Get_Seek_Time(chan_data.Cyl_Dist);
chan_data.Time_Settle = Get_Settle_Time();
chan_data.Seek_Pass = Get_Seek_Success();
/* Enable the output channel, "ochan" */
RTDX_enableOutput(&ochan);
/* Send the data to the host */
if (!RTDX_write ( &ochan,
&chan_data,
sizeof(chan_data)) ) {
fprintf(stderr,
"\nError: RTDX_write() failed!\n");
abort();
}
/* Wait for data transfer */
while ( RTDX_writing != NULL ) {
#if RTDX_POLLING_IMPLEMENTATION
/* Call Poll to do data transfer */
RTDX_Poll();
#endif
}
Current_Cyl = chan_data.Cyl;
}
/* Disable the output channel, "ochan" */
RTDX_disableOutput(&ochan);
puts("\nProgram Completed!");
}
/* ================================================================ */
int Get_Next_Random_Cyl( const int max_cyl )
{
time_t t;
static BOOL seeded = FALSE;
/* generate seed for random nbr generator */
if ( !seeded ) {
srand( (unsigned) time(&t) );
seeded = TRUE;
}
return ( rand() % max_cyl );/* return cylinder to move to*/
}
/* ================================================================ */
int Get_Cyl_Dist( const int cyl1, const int cyl2 )
{
/* return distance in cylinders */
return( cyl2 - cyl1 );
}
/* ================================================================ */
/*
* Note: Assume Force (acceleration) is constant. a = C;
* Therefore - velocity linear v = Ct;
* Therefore - distance quadratic d = Ct^2;
* Therefore - time to cover distance sqrt t = Csqrt(d);
*/
int Get_Seek_Time( const int cyl_dist )
{
double Force;
float dist;
float seek; /* seek time in ms to return */
Force = Max_Cyl / pow(Typical_Seek_Time,2);
dist = (float)abs(cyl_dist);
/* speed up */
seek = sqrt( (double)dist/Force);
/* slow down */
seek += sqrt( (double)dist/Force);
return (seek);
}
/* ================================================================ */
int Get_Settle_Time( void )
{
/* return settle time in ns */
return ( ( rand() % (Max_Settle_Time + 1) ));
}
/* ================================================================ */
BOOL Get_Seek_Success( void )
{
/* Return if Seek was successful or not */
return((rand() % Max_Seek_Fail == 0 ) ? FALSE : TRUE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -