gdevherc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* Copyright (C) 1990, 1991, 1993 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gdevherc.c,v 1.2 2000/09/19 19:00:13 lpd Exp $*/
  16. /* IBM PC-compatible Hercules Graphics display driver */
  17. /* using direct access to frame buffer */
  18. #define FB_RASTER 90
  19. #define SCREEN_HEIGHT 350
  20. #define SCREEN_ASPECT_RATIO (54.0/35.0)
  21. #define VIDEO_MODE 0x07
  22. #define regen 0xb0000000L
  23. #define interrupt /* patch ANSI incompatibility */
  24. #include "dos_.h"
  25. typedef union REGS registers;
  26. #include "gx.h"
  27. #include "gsmatrix.h" /* for gxdevice.h */
  28. #include "gxbitmap.h"
  29. #include "gxdevice.h"
  30. /* outportb is defined in dos_.h */
  31. #define outport2(port, index, data)\
  32. (outportb(port, index), outportb((port)+1, data))
  33. /* Define the nominal page height in inches. */
  34. #ifdef A4
  35. # define PAGE_HEIGHT_INCHES 11.69
  36. #else
  37. # define PAGE_HEIGHT_INCHES 11.0
  38. #endif
  39. /* Dimensions of screen */
  40. #define screen_size_x (FB_RASTER * 8)
  41. #define screen_size_y SCREEN_HEIGHT
  42. /* Other display parameters */
  43. #define raster_x FB_RASTER
  44. #define aspect_ratio SCREEN_ASPECT_RATIO
  45. #define graphics_video_mode VIDEO_MODE
  46. /* Procedures */
  47. /* See gxdevice.h for the definitions of the procedures. */
  48. dev_proc_open_device(herc_open);
  49. dev_proc_close_device(herc_close);
  50. dev_proc_fill_rectangle(herc_fill_rectangle);
  51. dev_proc_copy_mono(herc_copy_mono);
  52. dev_proc_copy_color(herc_copy_color);
  53. /* The device descriptor */
  54. private gx_device_procs herc_procs = {
  55. herc_open,
  56. gx_default_get_initial_matrix,
  57. gx_default_sync_output,
  58. gx_default_output_page,
  59. herc_close,
  60. gx_default_map_rgb_color,
  61. gx_default_map_color_rgb,
  62. herc_fill_rectangle,
  63. gx_default_tile_rectangle,
  64. herc_copy_mono,
  65. herc_copy_color
  66. };
  67. gx_device far_data gs_herc_device = {
  68. std_device_std_body(gx_device, &herc_procs, "herc",
  69. screen_size_x, screen_size_y,
  70. /* The following parameters map an appropriate fraction of */
  71. /* the screen to a full-page coordinate space. */
  72. /* This may or may not be what is desired! */
  73. (screen_size_y * aspect_ratio) / PAGE_HEIGHT_INCHES, /* x dpi */
  74. screen_size_y / PAGE_HEIGHT_INCHES /* y dpi */
  75. )
  76. };
  77. /* Forward declarations */
  78. private int herc_get_mode(P0());
  79. private void herc_set_mode(P1(int));
  80. /* Save the HERC mode */
  81. private int herc_save_mode = -1;
  82. /* Reinitialize the herc for text mode */
  83. int
  84. herc_close(gx_device *dev)
  85. { if ( herc_save_mode >= 0 ) herc_set_mode(herc_save_mode);
  86. return 0;
  87. }
  88. /* ------ Internal routines ------ */
  89. /* Read the device mode */
  90. private int
  91. herc_get_mode(void)
  92. { registers regs;
  93. regs.h.ah = 0xf;
  94. int86(0x10, &regs, &regs);
  95. return regs.h.al;
  96. }
  97. /* Set the device mode */
  98. private void
  99. herc_set_mode(int mode)
  100. { registers regs;
  101. regs.h.ah = 0;
  102. regs.h.al = mode;
  103. int86(0x10, &regs, &regs);
  104. }
  105. /****************************************************************/
  106. /* Hercules graphics card functions */
  107. /* */
  108. /* -- Taken from Jan/Feb 1988 issue of Micro Cornucopia #39 */
  109. /* */
  110. /* --rewritten for MSC 5.1 on 02/18/91 by Phillip Conrad */
  111. /****************************************************************/
  112. static const char paramg[12] = {0x35, 0x2d, 0x2e, 0x07, 0x5b, 0x02,
  113. 0x57, 0x57, 0x02, 0x03, 0x00, 0x00};
  114. /* (Never used)
  115. static const char paramt[12] = {0x61, 0x50, 0x52, 0x0f, 0x19, 0x06,
  116. 0x19, 0x19, 0x02, 0x0d, 0x0b, 0x0c};
  117. */
  118. /* Type and macro for frame buffer pointers. */
  119. /*** Intimately tied to the 80x86 (x<2) addressing architecture. ***/
  120. typedef byte far *fb_ptr;
  121. # define mk_fb_ptr(x, y)\
  122. (fb_ptr)((regen) + ((0x2000 * ((y) % 4) + (90 * ((y) >> 2))) + ((int)(x) >> 3)))
  123. /* Structure for operation parameters. */
  124. /* Note that this structure is known to assembly code. */
  125. /* Not all parameters are used for every operation. */
  126. typedef struct rop_params_s {
  127. fb_ptr dest; /* pointer to frame buffer */
  128. int draster; /* raster of frame buffer */
  129. const byte far *src; /* pointer to source data */
  130. int sraster; /* source raster */
  131. int width; /* width in bytes */
  132. int height; /* height in scan lines */
  133. int shift; /* amount to right shift source */
  134. int invert; /* 0 or -1 to invert source */
  135. int data; /* data for fill */
  136. int x_pos; /*>>added--2/24/91 */
  137. int y_pos;
  138. } rop_params;
  139. /* Define the device port and register numbers, and the regen map base */
  140. #define seq_addr 0x3b4 /* changed for HERC card (6845 ports)*/
  141. #define graph_mode 0x3b8
  142. #define graph_stat 0x3ba
  143. #define graph_config 0x3bf
  144. #ifndef regen
  145. #define regen 0xa0000000L
  146. #endif
  147. /* Initialize the display for Hercules graphics mode */
  148. int
  149. herc_open(gx_device *dev)
  150. { int i;
  151. if ( herc_save_mode < 0 ) herc_save_mode = herc_get_mode();
  152. /* herc_set_mode(graphics_video_mode); */
  153. outportb(graph_config,3);
  154. for(i=0;i<sizeof(paramg);i++)
  155. {
  156. outport2(seq_addr,i,paramg[i]);
  157. }
  158. outportb(graph_mode,0x0a); /* set page 0 */
  159. for(i=0;i<0x3FFFL;i++) /* clear the screen */
  160. {
  161. int far *loc = (int far *)( regen +(2L*i));
  162. *loc = 0;
  163. }
  164. return 0;
  165. }
  166. /* Macro for testing bit-inclusion */
  167. #define bit_included_in(x,y) !((x)&~(y))
  168. /* Copy a monochrome bitmap. The colors are given explicitly. */
  169. /* Color = gx_no_color_index means transparent (no effect on the image). */
  170. int
  171. herc_copy_mono(gx_device *dev,
  172. const byte *base, int sourcex, int raster, gx_bitmap_id id,
  173. int x, int y, int w, int h, gx_color_index izero, gx_color_index ione)
  174. { rop_params params;
  175. #define czero (int)izero
  176. #define cone (int)ione
  177. int dleft, sleft, count;
  178. int invert, zmask, omask;
  179. byte mask, rmask;
  180. if ( cone == czero ) /* vacuous case */
  181. return herc_fill_rectangle(dev, x, y, w, h, izero);
  182. /* clip */
  183. fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  184. params.dest = mk_fb_ptr(x, y);
  185. params.draster = raster_x;
  186. params.src = base + (sourcex >> 3);
  187. params.sraster = raster;
  188. params.height = h;
  189. params.shift = (x - sourcex) & 7;
  190. params.y_pos = y;
  191. params.x_pos = x;
  192. params.width = w;
  193. if(czero > cone) params.invert = -1;
  194. /* Macros for writing partial bytes. */
  195. /* bits has already been inverted by xor'ing with invert. */
  196. #define write_byte_masked(ptr, bits, mask)\
  197. *ptr = ((bits | ~mask | zmask) & (*ptr | (bits & mask & omask)))
  198. #define write_byte(ptr, bits)\
  199. *ptr = ((bits | zmask) & (*ptr | (bits & omask)))
  200. invert = (czero == 1 || cone == 0 ? -1 : 0);
  201. /* invert = (czero == 1 || cone == 1 ? -1 : 0); */
  202. zmask = (czero == 0 || cone == 0 ? 0 : -1);
  203. omask = (czero == 1 || cone == 1 ? -1 : 0);
  204. #undef czero
  205. #undef cone
  206. /* Actually copy the bits. */
  207. sleft = 8 - (sourcex & 7);
  208. dleft = 8 - (x & 7);
  209. mask = 0xff >> (8 - dleft);
  210. count = w;
  211. if ( w < dleft )
  212. mask -= mask >> w,
  213. rmask = 0;
  214. else
  215. rmask = 0xff00 >> ((w - dleft) & 7);
  216. if (sleft == dleft) /* optimize the aligned case */
  217. {
  218. w -= dleft;
  219. while ( --h >= 0 )
  220. {
  221. register const byte *bptr = params.src;
  222. register byte *optr = mk_fb_ptr(params.x_pos,params.y_pos);
  223. register int bits = *bptr ^ invert; /* first partial byte */
  224. count = w;
  225. write_byte_masked(optr, bits, mask);
  226. /* Do full bytes. */
  227. while ((count -= 8) >= 0)
  228. {
  229. bits = *++bptr ^ invert;
  230. params.x_pos += 8;
  231. optr = mk_fb_ptr(params.x_pos,params.y_pos);
  232. write_byte(optr, bits);
  233. }
  234. /* Do last byte */
  235. if (count > -8)
  236. {
  237. bits = *++bptr ^ invert;
  238. params.x_pos += 8;
  239. optr = mk_fb_ptr(params.x_pos,params.y_pos);
  240. write_byte_masked(optr, bits, rmask);
  241. }
  242. /* dest += BPL; */
  243. params.y_pos++;
  244. params.x_pos = x;
  245. params.src += raster;
  246. }
  247. }
  248. else
  249. {
  250. int skew = (sleft - dleft) & 7;
  251. int cskew = 8 - skew;
  252. while (--h >= 0)
  253. {
  254. const byte *bptr = params.src;
  255. byte *optr = mk_fb_ptr(params.x_pos,params.y_pos);
  256. register int bits;
  257. count = w;
  258. /* Do the first partial byte */
  259. if (sleft >= dleft)
  260. {
  261. bits = *bptr >> skew;
  262. }
  263. else /* ( sleft < dleft ) */
  264. {
  265. bits = *bptr++ << cskew;
  266. if (count > sleft)
  267. bits += *bptr >> skew;
  268. }
  269. bits ^= invert;
  270. write_byte_masked(optr, bits, mask);
  271. count -= dleft;
  272. params.x_pos += 8;
  273. optr = mk_fb_ptr(params.x_pos,params.y_pos);
  274. /* Do full bytes. */
  275. while ( count >= 8 )
  276. {
  277. bits = *bptr++ << cskew;
  278. bits += *bptr >> skew;
  279. bits ^= invert;
  280. write_byte(optr, bits);
  281. count -= 8;
  282. params.x_pos += 8;
  283. optr = mk_fb_ptr(params.x_pos,params.y_pos);
  284. }
  285. /* Do last byte */
  286. if (count > 0)
  287. {
  288. bits = *bptr++ << cskew;
  289. if (count > skew)
  290. bits += *bptr >> skew;
  291. bits ^= invert;
  292. write_byte_masked(optr, bits, rmask);
  293. }
  294. /* dest += BPL;
  295. line += raster;
  296. */
  297. params.y_pos++;
  298. params.x_pos = x;
  299. params.src += raster;
  300. }
  301. }
  302. return 0;
  303. }
  304. /* Copy a color pixelmap. This is just like a bitmap, */
  305. int
  306. herc_copy_color(gx_device *dev,
  307. const byte *base, int sourcex, int raster, gx_bitmap_id id,
  308. int x, int y, int w, int h)
  309. { return herc_copy_mono(dev, base, sourcex, raster, id,
  310. x, y, w, h,(gx_color_index)0, (gx_color_index)1);
  311. }
  312. # define mk_fb_yptr(x, y)\
  313. (fb_ptr)((regen) + ((0x2000 * ((y) % 4) + (90 * ((y) >> 2))) + x))
  314. /* Fill a rectangle. */
  315. int
  316. herc_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  317. gx_color_index color)
  318. { rop_params params;
  319. int x2, y2, xlen;
  320. byte led, red, d;
  321. byte far *ptr;
  322. int xloc;
  323. fit_fill(dev, x, y, w, h);
  324. params.dest = mk_fb_ptr(x, y);
  325. params.y_pos = y;
  326. params.x_pos = x;
  327. x2 = x + w - 1;
  328. y2 = y + h - 1;
  329. xlen = (x2 >> 3) - (x >> 3) - 1;
  330. led = 0xff >> (x & 7);
  331. red = 0xff << (7 - (x2 & 7));
  332. ptr = mk_fb_ptr(x,y);
  333. if (color)
  334. {
  335. /* here to set pixels */
  336. if (xlen == -1)
  337. {
  338. /* special for rectangles that fit in a byte */
  339. d = led & red;
  340. for(; h >= 0; h--, ptr = mk_fb_ptr(x,params.y_pos))
  341. {
  342. *ptr |= d;
  343. params.y_pos++;
  344. }
  345. return 0;
  346. }
  347. /* normal fill */
  348. xloc = params.x_pos >> 3;
  349. for(; h >= 0; h--, ptr = mk_fb_ptr(x,params.y_pos))
  350. { register int x_count = xlen;
  351. register byte far *p = ptr;
  352. *p |= led;
  353. /* params.x_pos += 8; */
  354. xloc++;
  355. p = mk_fb_yptr(xloc,params.y_pos);
  356. while ( x_count-- ) {
  357. *p = 0xff;
  358. /* params.x_pos += 8; */
  359. xloc++;
  360. p = mk_fb_yptr(xloc,params.y_pos);
  361. }
  362. *p |= red;
  363. /* params.x_pos = x; */
  364. xloc = params.x_pos >> 3;
  365. params.y_pos++;
  366. }
  367. }
  368. /* here to clear pixels */
  369. led = ~led;
  370. red = ~red;
  371. if (xlen == -1)
  372. {
  373. /* special for rectangles that fit in a byte */
  374. d = led | red;
  375. for(; h >= 0; h--, ptr = mk_fb_ptr(x,params.y_pos))
  376. {
  377. *ptr &= d;
  378. params.y_pos++;
  379. }
  380. return 0;
  381. }
  382. /* normal fill */
  383. xloc = x >> 3;
  384. for(; h >= 0; h--, ptr = mk_fb_ptr(x,params.y_pos))
  385. { register int x_count = xlen;
  386. register byte far *p = ptr;
  387. *p &= led;
  388. /* params.x_pos += 8; */
  389. xloc++;
  390. p = mk_fb_yptr(xloc,params.y_pos);
  391. while ( x_count-- ) {
  392. *p = 0x00;
  393. /* params.x_pos += 8; */
  394. xloc++;
  395. p = mk_fb_yptr(xloc,params.y_pos);
  396. }
  397. *p &= red;
  398. /* params.x_pos = x; */
  399. xloc = params.x_pos >> 3;
  400. params.y_pos++;
  401. }
  402. return 0;
  403. }