Showing posts with label C Language. Show all posts
Showing posts with label C Language. Show all posts

Accessing VDU memory using C Language

C language provide built in function for print information on the screen. Like printf(), puts() etc. But if we want to print information on the screen without using any built in function so we have to use VDU (Visual Display Unit) memory. The microprocessor doesn’t have the ability to produce image or text directly on the screen. It writes the information on the VDU memory. VDU memory present in the display adapter.

VDU memory has a one base address. The bas address of VDU memory changes from different display adapter. E.g. for VGA/EGA/CGA adapter base address is 0XB8000000 (hex), and for MA adapter base address is 0XB0000000 (hex).

For store single character in to VDU memory needs 2 bytes. First byte for the ASCII (American Standard Code for Information Interchange) value and second byte for the color attributes.

In color attributes first 4 bits for the foreground color, 3 bits for the background color and last one for the blinking.

In console application default screen is arranged as 80 characters (columns) by 25 lines (rows). We can also set default screen arrangement using C language for that read this article.

In below example I write one function for print 0 to 9 using VDU memory.
#include<stdio.h>
#include<conio.h>
char far *vdmem = (char far*)0xB8000000;
void printchar(int,int,char,int);
void main()
{
 int i;
 clrscr();
 for(i=48;i<58;i++)
  printchar(0,i-48,i,112);
 getch();
}
void printchar(int r,int c,char ch,int clr)
{
 char far *v;
 v = vdmem + r * 160 + c * 2; //Calculate Address
 *v = ch; //Store Character on VDU Memory
 v++;
 *v = clr;//Set color attribute
}

printchar() function definition we can say that gotoxy(c,r) is equal to vdmem + r * 160 + c * 2, a represent each character need two bytes, so multiples of 2 with c (column) and one row contains 80 character, so multiples of 160 with r (row).

How to change screen display mode using Turbo C

The ROM BIOS (Basic Input Output System) provides device control for the PC's major devices (disk, video, keyboard, serial port, printer), allowing a programmer to communicate with these devices without needing detailed knowledge of their operation. The ROM routines are accessed via the Intel 8088/86 software generated interrupts(Number). 
The ROM BIOS supports many ROM BIOS INTERRUPT(Number) ROUTINES
 ROM BIOS INTERRUPT(Number) ROUTINES
 10      Video routines
 11      Equipment Check
 12      Memory Size Determination
 13      Diskette routines
 14      Communications routines
 15      Cassette
 16      Keyboard routines
 17      Printer
 18      Cassette BASIC
 19      Bootstrap loader
 1A     Time of Day
 we can use this interrupt(Number) routins by setting register value. C language has already beed created, and resides in header file dos.h. it is a union type REGS, which has two parts.
One structure contains the eight bit registers (accessed by .h.), the other structure contains the 16 bit registers (accessed by .x.). To generate the desired interrupt, a special function call has been provided. This function accepts the interrupt number, and pointers for the Input and output register values. The following program demonstrates the use of these concepts to set the display mode to 40x25 color.
#include <dos.h>
union REGS regs;
main()
{
regs.h.ah = 0;
regs.h.al = 1;
int86( 0x10, &regs, &regs );
printf("Fourty by Twenty-Five color mode.");
}
VIDEO ROM BIOS CALLS
The ROM BIOS supports many routines for accessing the video display. The following table illustrates the use of software interrupt 0x10(16). SCREEN DISPLAY MODE FUNCTION CALL (regs.h.ah = 0)
regs.h.al Screen Mode
 0      40.25 BW 
 1      40.25 CO
 2      80.25 BW
 3      80.25 CO
 4      320.200 CO
 5      320.200 BW
 6      640.200 BW
 7      Mono-chrome
 8      160.200 16col PCjr
 9      320.200 16col PCjr
 A     640.200 4col PCjr
 D     320.200 16col EGA
 E      640.200 16col EGA
 F      640.350 mono EGA
 10    640.350 16col EGA