Showing posts with label Hardware leavel C Programming. Show all posts
Showing posts with label Hardware leavel C Programming. Show all posts

C Program to write data in DMA Mode

C Program to write data in DMA Mode | Direct Memory Access using C | Writing Data On Hard Disk using DMA mode in C | Working in DMA Mode in C Language.



void main()
{
clrscr();
union REGS regs;
int ans;
char arr[1000];

outp(0x3f2,0x1c); //HDD Motor On
delay(200);
outp(0x3f5,0x0f); //Command Code
delay(200);
outp(0x3f5,0x00); //Command Code
delay(200);
outp(0x3f5,0); //Cylinder no.
delay(200);
outp(0x3f5,0x08); //Sense Interrupt Command
delay(200);

ans=inp(0x3f5); //Reading ST0 in data register
delay(100);
ans=inp(0x3f5); //pcn

cout<<<
delay(200);
outportb(0x12,0); /*initialization of the DMA Mode*/
delay(200);
outportb(0x11,10); /*supplying Mode Byte*/

int ar=FP_OFF(arr);
int ar1=FP_SEG(arr);
outportb(0x10,2); /*supplying channel no. on port 10*/

regs.h.ch = (ar1)&(0x0f00);
regs.x.ax = regs.h.ah+ar;
(regs.h.ch)++;

outportb(0x04,regs.h.al);
delay(200);

outportb(0x04,regs.h.ah);
delay(200);
outportb(0x81,regs.h.ch);
delay(200);
outportb(0x05,1);
// DMA End

//READ command
delay(200);
outportb(0x3f5,46); //Command Code
delay(200);
outportb(0x3f5,0); //Command Code
delay(200);

outportb(0x3f5,0); //Cylinder no.
delay(200);

outportb(0x3f5,0); //Head Addr
delay(200);
outportb(0x3f5,3); //Record
delay(200);
outportb(0x3f5,2); //Sector size
delay(200);
outportb(0x3f5,5); //EOT
delay(200);
outportb(0x3f5,21); //GPL
delay(200);
outportb(0x3f5,10); //DTL

//result
delay(200);


// st0

int st0 = inportb(0x3f5);
printf("
st0 = %x",st0);
if(inportb(0x3f5) & (192)!=0)
{
printf("
st0= %d",st0);
cout<<"
Abnormal termination st0";
delay(200);
outp(0x3f2,0x0c); //Setting Motor off
exit(0);
}

delay(200);

int st1 = inportb(0x3f5);
printf("
st1 = %x",st1);
if((st1=inportb(0x3f5)) !=0)
{
printf("
st1=%d ",st1);
cout<<"
Abnormal termination st1";
delay(200);
outp(0x3f2,0x0c); //Setting Motor off
exit(0);
}

delay(200);

int st2 = inportb(0x3f5);
printf("
st2 = %x",st2);
if((st2=inportb(0x3f5)) != 0)
{
cout<<"st2= "<
cout<<"
Abnormal termination st2";
delay(200);
outp(0x3f2,0x0c); //Setting Motor off
exit(0);
}

cout<<"
Successful Termination";
delay(200);

cout<<"
c= "<
delay(200);

cout<<"
h= "<
delay(200);

cout<<"
r= "<
delay(200);

cout<<"
n= "<
delay(200);

outp(0x3f2,0x0c); //Setting Motor off

getch();

}


Related Links :

C program for cleardevice | cleardevice attached in C

C program for cleardevice | cleardevice attached in C



#include
#include

main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");

outtext("Press any key to clear the screen.");
getch();
cleardevice();
outtext("Press any key to exit...");

getch();
closegraph();
return 0;
}


Related Links :

c program to create dos command DIR, Program to create a DIR Dos Command

c program to create dos command DIR, Program to create a DIR Dos Command


#include “stdio.h”
#include “dos.h”
void main(int count,char *argv[]){
struct find_t q ;
int a;
if(count==1)
argv[1]="*.*";
a = _dos_findfirst(argv[1],1,&q);
if(a==0){
while (!a){
printf(" %s\n", q.name);
a = _dos_findnext(&q);
}
}
else{
printf("File not found");
}
}



Related Links :

c program to create dos command dir



#include
#include
int main(int count,char *argv[]){
struct find_t q ;
int a;
if(count==1)
argv[1]="*.*";
a = _dos_findfirst(argv[1],1,&q);
if(a==0){
while (!a){
printf(" %s\n", q.name);
a = _dos_findnext(&q);
}
}
else{
printf("File not found");
}
return 0;
}

Related Links :

Program to read PC Type from BIOS Data Area and display it.


# include
# include


int main( )
{
clrscr( );

unsigned char far* ptrPcType=(unsigned char far*)0xF000FFFE;

printf("Model Identification Code:\n\n");
printf(" Code (Hex) ::: Meanings\n");
printf(" FC : AT\n");
printf(" FE : XT\n");
printf(" FB : XT\n");
printf(" FF : PC\n");

printf("\n\nYour PC Type Code (Hex): %X",*ptrPcType);

getch( );
return 0;
}


Related Links :

Progranm to timer function CPU calendar Function



#include
#include
#include
#include

int main(void)
{
time_t calendar_start = time(NULL); /* Initial calendar time */
clock_t cpu_start = clock(); /* Initial processor time */
int count = 0; /* Count of number of loops */
const int iterations = 1000000; /* Loop iterations */
char answer = 'y';


printf("Initial clock time = %lu Initial calendar time = %lu\n",
cpu_start, calendar_start);

while(tolower(answer) == 'y')
{
for(int i = 0 ; i {
double x = sqrt(3.14159265);
}
printf("\n%ld square roots completed.", iterations*(++count));
printf("\nDo you want to run some more(y or n)? ");

scanf("\n%c", &answer);
}

clock_t cpu_end = clock(); /* Final cpu time */
time_t calendar_end = time(NULL); /* Final calendar time */

printf("\nFinal clock time = %lu Final calendar time = %lu\n",
cpu_end, calendar_end);
printf("\nCPU time for %ld iterations is %.2lf seconds\n",
count*iterations, ((double)(cpu_end-cpu_start))/CLOCKS_PER_SEC );

printf("\nElapsed calendar time to execute the program is %8.2lf\n",
difftime(calendar_end, calendar_start));
return 0;
}


Related Links :

Program to hide the partation of your disk drive in c.


#include
#include
#include
#include

typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef unsigned long DWORD;

enum BOOL {TRUE=0,FALSE=1};
struct PARTITIONINFO {
BYTE bootid; /* bootable? 0=no, 128=yes */
BYTE beghead; /* beginning head number */
BYTE begsect; /* beginning sector number */
BYTE begcyl; /* 10 bit nmbr, with high 2 bits put in begsect */
BYTE systid; /* Operating System type indicator code */
BYTE endhead; /* ending head number */
BYTE endsect; /* ending sector number */
BYTE endcyl; /* also a 10 bit nmbr, with same high 2 bit trick */
DWORD relsect; /* first sector relative to start of disk */
DWORD numsect; /* number of sectors in partition */
};

struct DISK_ADD_PACKET {
BYTE recordsize;
BYTE reserved;
WORD count;
DWORD transferadd;
DWORD lowbits;
DWORD highbits;
};
struct MBR{
BYTE codes[446];
struct PARTITIONINFO partition[4];
WORD mbrid;
};
struct driveinfo{
DWORD startsect;
BYTE sysid;
};
WORD ExtentionCheck (BYTE drive)
{
union REGS regs;
regs.h.ah = 0x41;
regs.x.bx = 0x55aa;
regs.h.dl = drive;
int86(0x13,®s,®s);
if(regs.x.bx != 0xaa55)
return FALSE;
return TRUE;
}

WORD ReadSect(BYTE disk, int nsects,DWORD lsects,void* data)
{

union REGS iregs,oregs;
struct SREGS sregs;
int count=0;
struct DISK_ADD_PACKET * p;
p = (struct DISK_ADD_PACKET *)malloc(sizeof(struct DISK_ADD_PACKET));
p->recordsize=sizeof(struct DISK_ADD_PACKET);
p->count=nsects;
p->transferadd=(DWORD)data;
p->lowbits=lsects;
p->highbits=0; /* We dont need to access HD > 2TB */
iregs.h.ah = 0x42;
iregs.h.dl = disk;
iregs.x.si = FP_OFF(p);
sregs.ds = FP_SEG(p);
int86x(0x13,&iregs,&oregs,&sregs);
if(oregs.h.ah==0)
{
free(p);
return TRUE;
}

free(p);
return FALSE;

}
WORD WriteSect(BYTE disk, int nsects,DWORD lsects,void* data)
{

union REGS iregs,oregs;
struct SREGS sregs;
int count=0;
struct DISK_ADD_PACKET * p;
p = (struct DISK_ADD_PACKET *)malloc(sizeof(struct DISK_ADD_PACKET));
p->recordsize=sizeof(struct DISK_ADD_PACKET);
p->count=nsects;
p->transferadd=(DWORD)data;
p->lowbits=lsects;
p->highbits=0; /* We dont need to access HD > 2TB */
iregs.x.ax = 0x4302;
iregs.h.dl = disk;
iregs.x.si = FP_OFF(p);
sregs.ds = FP_SEG(p);
int86x(0x13,&iregs,&oregs,&sregs);
if(oregs.h.ah==0)
{
free(p);
return TRUE;
}

free(p);
return FALSE;

}
DWORD lsect=0;
void main()
{
BYTE disk=0x80;
WORD nsect=1,index=0,i=0,hi,choice;
DWORD extsect;
BYTE hideindex[5],id;
struct MBR *mbr;
struct driveinfo dinfo[10];//max 10 partitions
mbr=(struct MBR *)malloc(sizeof(struct MBR));
if(ExtentionCheck(disk)==FALSE)
{printf("extended int 13 is not supported
");
exit(1);
}


if(ReadSect(disk, nsect, lsect,(char *)mbr)==TRUE)
for(i=0;i<4;i++)
if(mbr->partition[i].systid!=0)
{printpart(mbr,i);
dinfo[index].startsect=lsect;
dinfo[index].sysid=mbr->partition[i].systid;
index++;
}
if(mbr->partition[1].systid==0xf)//0xf for extended partition
{lsect=mbr->partition[1].relsect;
extsect=lsect;
link:
if(ReadSect(disk, nsect, lsect,(char *)mbr)==TRUE)
{printpart(mbr,0);
dinfo[index].startsect=lsect;
dinfo[index].sysid=mbr->partition[0].systid;
index++;
if(index>=10){printf("no of partition exceed max limit");exit(1);}
if(mbr->partition[1].systid!=0)
{lsect=extsect+mbr->partition[1].relsect;
goto link;
}
}

}

printf("


Hide partition(1)
");
printf("Revele partition(2)
");
printf("Quit(3)
Enter your choice(1/2/3):");
hideindex[0]=getche();
choice=atoi(hideindex);
switch(choice)
{
case 1:
printf("
Which drive do u want to hide:(0-%d):",index-1);
scanf("%s",hideindex);
hi=atoi(hideindex);
if(hi<=index)
{
id=dinfo[hi].sysid;
if(id==1||id==4||id==7||id==0xb||id==0xc||id==0xe)
{ lsect=dinfo[hi].startsect;
id+=0x10;
if(ReadSect(disk, nsect, lsect,(char *)mbr)==TRUE)
{mbr->partition[0].systid=id;
if(WriteSect(disk,nsect,lsect,(char *)mbr)==TRUE)
printf("
HIDDEN SUCCESSFULLY");
}
}
else
printf("Cant Hide");
}
break;
case 2:
printf("
Which drive do u want to Revele:(0-%d):",index-1);
scanf("%s",hideindex);
hi=atoi(hideindex);
if(hi<=index)
{
id=dinfo[hi].sysid;
if(id==0x11||id==0x14||id==0x17||id==0x1b||id==0x1c||id==0x1e)
{ lsect=dinfo[hi].startsect;
id-=0x10;
if(ReadSect(disk, nsect, lsect,(char *)mbr)==TRUE)
{mbr->partition[0].systid=id;printf("%x",mbr->partition[0].systid);
if(WriteSect(disk,nsect,lsect,(char *)mbr)==TRUE)
printf("
REVELED SUCCESSFULLY");
}
}
else
printf("Cant Revele
");
}

break;
case 3:
exit(1);break;
default:
printf("
invalid choice");exit(1);
break;
};

}
printpart(struct MBR *mbr,WORD i)
{
BYTE bootable[]="YES";
BYTE id[7];
static BYTE c=0,index=0;
if(c==0)//to execute this for once
{
clrscr();
gotoxy(30,1);
printf("Partition Table");
gotoxy(2,2);
printf("INDEX");
gotoxy(8,2);
printf("SystemID");
gotoxy(17,2);
printf("Bootable");
gotoxy(26,2);
printf("StartingLBA");
gotoxy(38,2);
printf("SIZEINSECTORS");
gotoxy(52,2);
printf("SIZEINGB");
c++;
gotoxy(46,20);
printf("Coded by Tapan Kumar Mishra");
gotoxy(55,21);
printf("7th Sem,Electrical Engg.");
gotoxy(55,22);
printf("IGIT Sarang,Orissa");
gotoxy(46,23);
printf("Email id:titu_igit@rediffmail.com");
}
if(mbr->partition[i].bootid!=0x80)
strcpy(bootable,"NO");
gotoxy(2,3+index);
printf("%d",index);
gotoxy(8,3+index);
systemid((BYTE)mbr->partition[i].systid,id);
printf("%s",id);
gotoxy(17,3+index);
printf("%s",bootable);
gotoxy(26,3+index);
printf("%ld",mbr->partition[i].relsect+lsect);
gotoxy(38,3+index);
printf("%ld
",mbr->partition[i].numsect);
gotoxy(52,3+index);
printf("%5.2fGB",(float)mbr->partition[i].numsect/2097152.0);


index++;
return 0;
}
systemid(BYTE systid,BYTE *id)
{
switch(systid)
{
case 00:
strcpy(id,"empty");
break;
case 01:
strcpy(id,"FAT12");
break;
case 04:
strcpy(id,"FAT16");
break;
case 05:
strcpy(id,"EXTNED");
break;
case 0xb:
strcpy(id,"FAT32");
break;
case 0xc:
strcpy(id,"FAT32");
break;
case 0xE:
strcpy(id,"FAT16");
break;
case 0xf:
strcpy(id,"EXNDED");
break;
case 0x82:
strcpy(id,"SWAP");
break;
case 0x83:
strcpy(id,"EXT2fs");
break;
case 0x11:
case 0x14:
case 0x15:
case 0x16:
case 0x17:
case 0x1b:
case 0x1c:
case 0x1e:
case 0x1f:
strcpy(id,"hidden");
break;


}

}

Related Links :

Program to crack the User password of Windows 7, Hacking Windows Vista or 7

Program to crack the User password of Windows 7, Hacking Windows Vista or 7
Use the program at your own risk we are not responsible for any data loss this is just for experimental purpose not for commercial use.



# include
# include
# include
# include
# include
# include
# include

unsigned char huge Data[100001];
unsigned char keystream[1001];
int Rpoint[300];

void main(int argc,char *argv[]){
FILE *fd;
int i,j;
int size;
char ch;
char *name;
int cracked;
int sizemask;
int maxr;
int rsz;
int pos;
int Rall[300]; /* Resourse allocation table */

if(argc<2){ fd="fopen(argv[1]," fd="=" size="0;" name="argv[1];">2)name=argv[2];
printf("Username:%s
",name);

/* Copy encrypted text into keystream */
cracked=size-0x0208;
if(cracked<0)cracked=0;>1000)cracked=1000;
memcpy(keystream,Data+0x208,cracked);

/* Generate 20 bytes of keystream */
for(i=0;i<20;i++){ ch="toupper(name[i]);" ch="=" ch="=" cracked="20;" sizemask="keystream[0]+(keystream[1]<<8);" i="0;i<256;i++){">maxr)maxr=Data[i];
}
}

maxr=(((maxr/16)+1)*16); /* Resourse pointer table size appears to be
divisible by 16 */

/*Search after resources */

Rpoint[0]=0x0208+2*maxr+20+2; /* First resources */
for(i=0;i>8) & 0x00ff;
}
cracked+=maxr*2+2;
printf("%d Bytes of ketstream recoverd
",cracked);

/* Decrypt resources */
for(i=0;icracked)rsz=cracked;
printf("Resource[%d](%d)
",i,rsz);
for(j=0;j


Related Links :

Round-robin CPU scheduling Program Algorithm in C

Definition : The term round-robin describes correspondence to a single address authored or signed by numerous individuals.

Round-robin CPU scheduling Program Algorithm in C



#include
#include
main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,pop=0,stat=0,xx,sq=0;
float awt=0.0,atat=0.0;
clrscr();
printf("Enter number of processes You Have:");
scanf("%d",&n);
printf("Enter burst time for sequences....:");
for(i=0;itq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
xx=st[i];
st[i]=0;
}
sq=sq+xx;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i





Output :


enter the process name : aaa
enter the processing time : 4
enter the process name : bbb
enter the processing time : 3
enter the process name : ccc
enter the processing time : 2
enter the process name : ddd
enter the processing time : 5
enter the process name : eee
enter the processing time : 1

OUTPUT :

p_name p_time w_time
aaa 4 9
bbb 3 3
ccc 2 6
ddd 5 10
eee 1 11

total waiting time : 39
average waiting time : 7.8000

Related Links :

xstrcmp() - compares strings using DOS wildcards ** 'mask' may contain '*' and '?'


int xstrcmp (char *mask, char *s)
{
while (*mask)
{
switch (*mask)
{
case '?':
if (!*s)
return (0);
s++;
mask++;
break;

case '*':
while (*mask == '*')
mask++;
if (!*mask)
return ( 1 );
if (*mask == '?')
break;
while (*s != *mask)
{
if (!*s)
return (0);
s++;
}
s++;
mask++;
break;

default:
if (*s != *mask)
return (0);
s++;
mask++;
}
}

if (!*s && *mask)
return (0);
return ( 1 );
}

#ifdef TEST

#include
void main(int argc, char *argv[])
{
if (3 != argc)
{
puts("Usage: XSTRCMP string_1 string_2");
return;
}
printf("xstrcmp(\"%s\", \"%s\") returned %d\n", argv[1], argv[2],
xstrcmp(argv[1], argv[2]));
}

#endif /* TEST */

Related Links :

Program to Blinking Lights on Keyboard in c


#include
void interrupt mytimer();
void interrupt (*prev)();
int run=0,lt=0,ticks=0;
char far *mode;
int temp;
void main()
{
mode=(char far*)0x417;
prev=getvect(0x8);
setvect(0x8,mytimer);
keep(0,100);
getch();
}
void interrupt mytimer()
{
if(run==0)
{
ticks++;
if(ticks==5)
{
ticks=0;
run=1;
lt++;
if(lt==1)
{
temp=*mode;
temp=temp&0x8f;
temp=temp|0x40;
*mode=temp;
}
else if(lt==2)
{
temp=*mode;
temp=temp&0x8f;
temp=temp|0x20;
*mode=temp;
}
else if(lt==3)
{
temp=*mode;
temp=temp&0x8f;
temp=temp|0x10;
*mode=temp;
lt=0;
}
}
run=0;
}
(*prev)();
}


Related Links :

Program to make initialization of DMA Mode Through C



# include
# include
# include
# include
# include

void main()
{
clrscr();
union REGS regs;
int ans;
char arr[1000];

outp(0x3f2,0x1c); //Motor On
delay(200);
outp(0x3f5,0x0f); //Command Code
delay(200);
outp(0x3f5,0x00); //Command Code
delay(200);
outp(0x3f5,0x0b); //Cylinder no.
delay(200);
outp(0x3f5,0x08); //Sense Interrupt Command
delay(200);

ans=inp(0x3f5); //Reading ST0 in data register
// cout<
delay(100);
ans=inp(0x3f5); //pcn

cout<
outportb(0x12,0); /*initialization of DMA Mode*/
outportb(0x11,10); /*supplying Mode Byte*/
clrscr();
int ar=FP_OFF(arr);
int ar1=FP_SEG(arr);

printf(" %x",ar1);

regs.h.ch = (ar1)&(0x0f00);
printf(" %x",(regs.h.ch));
regs.x.ax = regs.h.ah+ar;
(regs.h.ch)++;
printf(" %x",regs.x.ax);
int z = (regs.x.ax) & (0x0f00);
cout<<""< getch();
outportb(0x04,regs.h.al);
outportb(0x04,regs.h.ah);
outportb(0x81,regs.h.ch);
outportb(0x05,1);
// dma end

outp(0x3f2,0x0c); //Motor off
return;

}

Related Links :

Program To Implement Recalibrate Command in C Starts FDD motor , check its status & turn off Motor



#include
#include
#include

void main()
{
int show;
clrscr();
//Put on the motor
outp(0x3f2,28);

// Check whether the FDC is ready
show=inp(0x3f4); //Read the status of MAIN STATUS REGISTER
show=(show&128);

if(show==128)//Check whether FDC is ready
{

//Input the command parameters
outp(0x3f5,7);//Enter command parameters
delay(200);
outp(0x3f5,0);//Enter Drive No.
delay(300);
}
// Check the status of data register
show=inp(0x3f5);
if(show==0)
printf("Succesfully executed Recalibrate command");
getch();
//Put off the motor
outp(0x3f2,0);

}

Related Links :

Program to read and write absolute disk sectors

(these will work with all versions of DOS 2-7).



#include
#include
#include "snpdskio.h"

int CDECL absdisk(unsigned char function,
unsigned short drive, /* 0 = A:, etc. */
size_t number_of_sectors,
size_t starting_sector,
void * sector_buffer);

int AbsDiskRead(unsigned short drive,
size_t num_of_sectors,
size_t sector,
void *ptr)
{
return absdisk(0x25, drive, num_of_sectors, (unsigned)sector, ptr);
}

int AbsDiskWrite(unsigned short drive,
size_t num_of_sectors,
size_t sector,
void *ptr)
{
return absdisk(0x26, drive, num_of_sectors, (unsigned)sector, ptr);
}




CODE FOR SNPDSKIO.H







#ifndef SNPDKSIO__H
#define SNPDKSIO__H

#include
#include
#include "extkword.h"

/*
** File: ABSDISKC.C and ABSDISK.ASM
*/

int CDECL absdisk(unsigned char function,
unsigned short drive, /* 0 = A:, etc. */
size_t number_of_sectors,
size_t starting_sector,
void * sector_buffer);

int AbsDiskRead(unsigned short drive,
size_t num_of_sectors,
size_t sector,
void *ptr);

int AbsDiskWrite(unsigned short drive,
size_t num_of_sectors,
size_t sector,
void *ptr);

/*
** File: HUGEREAD.C
*/

long hugeread(int fh, unsigned char FAR *buf, long size);
long hugewrite(int fh, unsigned char FAR *buf, long size);
long hugefread(FILE *fp, char FAR *buf, long size);
long hugefwrite(FILE *fp, char FAR *buf, long size);


#endif /* SNPDKSIO__H */



IMP : Save This Second File In your include folder...

Related Links :


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!