Wednesday, May 18, 2011

IO mapped io

Since any dynamically-loaded driver module is attached to the existing kernel, any error in the driver will crash the entire system.

Resource allocation for a device is one of the main concerns for device-driver developers.

Resources are:
1. I/O memory
2. IRQs
3. Ports

----------------------------------
The most frequent job of any driver is transferring data between the computer and its external environment.
The external environment consists of external devices.

Three techniques are possible for I/O operations:
1. Programmed I/O
2. Interrupt-driver I/O
3. Direct Memory Access(DMA)

The PIO devices pass the data from the system to device or vice versa in two different ways:
1. I/O port
2. I/O memory mapped

PIO requires the CPU to move data to or from the device as each byte

Tuesday, May 3, 2011

#C_CODE: Endian checker

#include <stdio.h>

int isLittleEndian(void)
{
    unsigned int i = 0x01;
    return (*((unsigned char *)&i));
}

int main(void)
{
    printf("Is the machine little endian: %s\n",
    isLittleEndian()?"YES":"NO");
}

Monday, May 2, 2011

Physical & virtual addresses

Physical addresses refer to the hardware addresses of physical memory.
Virtual addresses refer to the virtual store viewed by the process.

Virtual to physical address mapping is done by Memory Management Unit(MMU)

Virtual space is limited by the size of virtual addresses.

Virtual memory space and physical memory space are different.

Paging is the most common method of memory management:
Virtual space of process is divided into fixed-size pages
Virtual address is composed of page number and page offset.
Physical memory is divided into fixed-size frames
Page in virtual space fits into frame in physical memory.

Mapping of VA to PA is done with a page table.
Each process has an associated page table.

Physical address = Frame number * Page size + Page offset
-----------------------------

Modern systems keep more than one program in memory at a time.
All these programs together require more memory than what is visible.
Use a part of disk and make it look like memory - This is called virtual memory.

Friday, April 8, 2011

#C_CODE: Integer to Ascii conversion

#include <stdio.h>
#include <string.h>
void reverse(char *s)
{
 int len = strlen(s) - 1;
 int i;
 for(i=0;i < strlen(s)/2; i++)
 {
  s[i]   ^= s[len];
  s[len] ^= s[i];
  s[i]   ^= s[len--];
 }
}

char *intoa(int num)
{
 static char buff[10];
 char *s = &buff[0];
 int sign;

 if((sign = num) < 0)
 {
  num = -num; //Make num positive  
 }

 do
 {
  *(s++) = (num%10) + '0';
 }while(num /= 10);
 
 if(sign < 0)
  *(s++) = '-';

 *(s) = '\0';
 reverse(&buff[0]);
 return (&buff[0]);
}

int main()
{
 int num,i;
 char *s;

 printf("Enter the num: ");
 scanf("%d",&num);
 s = intoa(num);
 printf("\nThe num %d in ascii is: ",num);
 for(i=0; i < strlen(s); i++)
  printf("\n%c",s[i]);

 fflush(stdin);
 getchar();
}

REF: C Programming Language-K&R

#C_CODE: Reverse String

#include <stdio.h>
#include <string.h>
 
void reverse(char *s)
{
 int len = strlen(s)-1;
 int i;
 for(i=0; i < strlen(s)/2; i++)
 {
  s[i]   ^= s[len];
  s[len] ^= s[i];
  s[i]   ^= s[len--];
 }
}

int main(void)
{
 char buff[35];
 fputs("Input string: ",stdout);
 fgets(buff,sizeof(buff),stdin);
 fflush(stdin);
 *(buff + strlen(buff) -1 ) = '\0';

 reverse(buff);
 printf("\n The reversed string is: %s\n",buff);
 getchar();
}

REF: C Programming Language-K&R

#C_CODE: Binary Search

#include <stdio.h>
 
void InsertionSort(int *ptr,int size)
{
 int i,j,index;
 for(i=1;i {
  index = ptr[i];
  for(j=i; j>0&&ptr[j-1] >index; j--)
   ptr[j] = ptr[j-1];

  ptr[j] = index;
 }
}

int BinarySearch(int *ptr,int key,int size)
{
 int low,mid,high;
 low = 0;
 high = size-1;
 while(low <= high)
 {
  mid = (low + high)/2;
  if(key < ptr[mid])
   high = mid - 1;
  else if(key > ptr[mid])
   low = mid + 1;
  else
   return mid;
 }
 return -1;
}

int main()
{
 int i;
 int a[] = {5,7,0,8,4,9,6,1,3,2};
 int res;
 
 InsertionSort(a,10);
 printf("\nInsertion sort: ");
 for(i=0;i<10;i++)
  printf("%d ",a[i]);
 
 for(i=0; i<10;i++)
 {
  res = BinarySearch(a,i,10);
  if(res != -1)
   printf("\nThe value %d is found in position: %d\n",i,res);
 }

 fflush(stdin);
 getchar();
}

REF: C Programming Language-K&R