sel_des.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. { example for :
  2. Selectors and descriptors
  3. DOS memory access
  4. allocate_ldt_descriptors()
  5. free_ldt_descriptors()
  6. get_segment_base_address()
  7. set_segment_base_address()
  8. get_segment_limit()
  9. set_segment_limit()
  10. seg_move()
  11. seg_fillword()
  12. }
  13. { This example demonstrates the usage of descriptors and the effects of
  14. changing its limit and base address.
  15. In more detail, the program fills the region described by an allocated
  16. descriptor in text screen memory with various characters.
  17. Before doing this it saves the entire screen contents to the heap and
  18. restores it afterwards.
  19. Some additional background:
  20. The text screen of a VGA card has it's address space at $B800:0; screen
  21. memory is organized in a linear fashion, e.g. the second line comes
  22. directly after the first, where each cell occupies 2 bytes of memory
  23. (1 byte character data, 1 byte attributes). It is 32 kb in size.
  24. Hence the offset of a single memory cell from its origin is:
  25. Y * columns * 2 + X * 2
  26. where X and Y mark the point and columns is the number of character cells
  27. per line
  28. }
  29. uses crt, { color constants, clreol(), gotoxy(), wherex(), wherey() }
  30. go32;
  31. const maxx = 80; { screen x and y dimensions }
  32. maxy = 25;
  33. bytespercell = 2; { bytes used for every character cell }
  34. screensize = maxx * maxy * bytespercell; { screen size in bytes }
  35. linB8000 = $B800 * 16; { the linear address of $B800:0 }
  36. type string80 = string[80];
  37. var
  38. text_save : array[0..screensize-1] of byte; { holds the old screen contents }
  39. text_oldx, text_oldy : Word; { old cursor x and y coordinates }
  40. text_sel : Word; { selector to the text mode screen }
  41. { prints a status message on the first line of the screen and then waits for
  42. a keypress }
  43. procedure status(s : string80);
  44. begin
  45. gotoxy(1, 1); clreol; write(s); readkey;
  46. end;
  47. { writes some descriptor info on the last 2 lines }
  48. procedure selinfo(sel : Word);
  49. begin
  50. gotoxy(1, 24);
  51. clreol; writeln('Descriptor base address : $', hexstr(get_segment_base_address(sel), 8));
  52. clreol; write('Descriptor limit : ', get_segment_limit(sel));
  53. end;
  54. { returns a 2 byte character cell, which includes character data and its
  55. color attributes }
  56. function makechar(ch : char; color : byte) : Word;
  57. begin
  58. result := byte(ch) or (color shl 8);
  59. end;
  60. begin
  61. { save original screen contents to variable, this time by using seg_move()
  62. and the dosmemselector variable }
  63. seg_move(dosmemselector, linB8000, get_ds, longint(@text_save), screensize);
  64. { additionally we have to save the old screen cursor coordinates }
  65. text_oldx := wherex; text_oldy := wherey;
  66. { clear the whole screen }
  67. seg_fillword(dosmemselector, linB8000, screensize div 2, makechar(' ', Black or (Black shl 4)));
  68. { output message }
  69. status('Creating selector ''text_sel'' to a part of text screen memory');
  70. { allocate descriptor }
  71. text_sel := allocate_ldt_descriptors(1);
  72. { set its base address to the linear address of the text screen + the
  73. byte size of one line (=maxx * bytespercell * 1) }
  74. set_segment_base_address(text_sel, linB8000 + bytespercell * maxx * 1);
  75. { the limit is set to the screensize reduced by one (a must be) and the
  76. number of lines we don't want to have touched (first line + lower 2 lines) }
  77. set_segment_limit(text_sel, screensize - 1 - bytespercell * maxx * 3);
  78. { write descriptor info }
  79. selinfo(text_sel);
  80. status('and clearing entire memory selected by ''text_sel'' descriptor');
  81. { fill the entire selected memory with single characters }
  82. seg_fillword(text_sel, 0, (get_segment_limit(text_sel)+1) div 2, makechar(' ', LightBlue shl 4));
  83. status('Notice that only the memory described by the descriptor changed, nothing else');
  84. status('Now reducing it''s limit and base and setting it''s described memory');
  85. { set the base address of the descriptor (increase it by the byte size of one line) }
  86. set_segment_base_address(text_sel, get_segment_base_address(text_sel) + bytespercell * maxx);
  87. { decrease the limit by byte size of 2 lines (1 line because base address changed,
  88. one line on the lower end) }
  89. set_segment_limit(text_sel, get_segment_limit(text_sel) - bytespercell * maxx * 2);
  90. { write descriptor info }
  91. selinfo(text_sel);
  92. status('Notice that the base addr increased by one line but the limit decreased by 2 lines');
  93. status('This should give you the hint that the limit is relative to the base');
  94. { fill the descriptor area }
  95. seg_fillword(text_sel, 0, (get_segment_limit(text_sel)+1) div 2, makechar(#176, LightMagenta or Brown shl 4));
  96. status('Now let''s get crazy and copy 10 lines of data from the previously saved screen');
  97. { copy memory from the data segment to screen }
  98. seg_move(get_ds, longint(@text_save), text_sel, maxx * bytespercell * 2, maxx * bytespercell * 10);
  99. status('At last freeing the descriptor and restoring the old screen contents..');
  100. status('I hope this little program may give you some hints on working with descriptors');
  101. { free the descriptor so that it can be used for things }
  102. free_ldt_descriptor(text_sel);
  103. { restore old state }
  104. seg_move(get_ds, longint(@text_save), dosmemselector, linB8000, screensize);
  105. gotoxy(text_oldx, text_oldy);
  106. end.