Skip to content

Instantly share code, notes, and snippets.

@carbontwelve
Last active May 10, 2021 21:34
Show Gist options
  • Save carbontwelve/c6a230d4e1c76c50d9fc9706cbf90613 to your computer and use it in GitHub Desktop.
Save carbontwelve/c6a230d4e1c76c50d9fc9706cbf90613 to your computer and use it in GitHub Desktop.
DOS C Programming
#include <conio.h>
/*
This program fills the screen with an extended ASCII character 129 (octal 201). As
you press any key the foreground colour is incremented through all 16 foreground
colours. This was written to test an issue I was having with the Watcom graph.h
library to see if the issue was with my environment (DOS Box) or my program. The
problem was extended ASCII characters always showing as a colour one less than the
background colour.
With thanks to Gered King (http://www.blarg.ca/) for their help in this quest.
The code below was pieced together from snippets of code by the following authors:
Brian Brown:
http://www.cems.uwe.ac.uk/~irjohnso/coursenotes/uqc146/cprogram/advcw2.htm
David Brackeen:
http://www.brackeen.com/vga/
*/
// Pointer to beginning of text mode memory space
char far *scrn = (char far *) 0xb8000000;
// Attr
// Bits 7 Blink Bit
// 654 Background Bits
// 3 Intensity Bit
// 210 Foreground Bits
// 00000000 = Black on Black
// 01001110 = Yellow on Red = 0x4E
void fill_display(char byte, char attr) {
unsigned int loop, scrsize = 80 * 25 * 2;
for( loop = 0; loop < scrsize; loop+= 2) {
// Wait for the horizontal retrace to occur before writing a character
while( (inp(0x3da) & 01) == 0);
while( (inp(0x3da) & 01) != 0);
// Write character plus its attributes
scrn[loop] = byte;
scrn[loop+1] = attr;
}
}
void main() {
register unsigned int i;
for (i=0; i<16; i++){
fill_display('\201', 0x40 + i); // Red bg + i colour
getch();
}
}
#include <conio.h>
#include <graph.h>
/*
This program outputs a red rectangle onto the screen and draws yellow characters
on top. For some reason when cross compiling to 16Bit DOS from Windows 10 the
extended ASCII characters show up as a colour code one less than they have been
set to, in this case that means magenta instead of yellow.
*/
int main() {
_setvideomode(_TEXTC80); // Set to 80x25 text mode
_displaycursor(_GCURSOROFF); // Hide cursor
// Display is 25 rows, 80 columns, 16 colours
_settextwindow(1,1, 25, 80);
// Display rectangle.
_settextwindow(5,8, 20, 74);
_setbkcolor(0x4); // Red
_clearscreen(_GWINDOW);
// I am not sure why I have to do 21 here, but with 20
// it was causing the bottom line to wrap weirdly.
_settextwindow(5,8, 21, 74);
_settextposition(1,1);
_settextcolor(0xE); // Bright Yellow
_outtext("###################################################################");
_settextposition(16,1);
_outtext("###################################################################");
_settextposition(2,1);
_settextcolor(0xE); // Bright Yellow
_outtext("These should all be bright yellow: \262\261\260\176\200");
getch();
_displaycursor(_GCURSORON); // Restore cursor
_setvideomode(_DEFAULTMODE); // Restore default video mode
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment