home *** CD-ROM | disk | FTP | other *** search
/ Ray Tracing Box / RAY_CD.mdf / raytrace / povp21 / povami / machine / amiga / amiga.c next >
C/C++ Source or Header  |  1993-11-07  |  26KB  |  971 lines

  1. /****************************************************************************
  2. *                   amiga.c
  3. *
  4. *  This module handles all of the Amiga-specific code for the raytracer.
  5. *
  6. *  from Persistence of Vision Raytracer
  7. *  Copyright 1993 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. *  NOTICE: This source code file is provided so that users may experiment
  10. *  with enhancements to POV-Ray and to port the software to platforms other 
  11. *  than those supported by the POV-Ray Team.  There are strict rules under
  12. *  which you are permitted to use this file.  The rules are in the file
  13. *  named POVLEGAL.DOC which should be distributed with this file. If 
  14. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  16. *  Forum.  The latest version of POV-Ray may be found there as well.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. * Modifications by Dave Park.
  23. *
  24. * Note: The AGA HAM8 code does not work.  Consider what is here to be HAM8
  25. * 'hooks'.
  26. *****************************************************************************/
  27.  
  28.  
  29. #include "frame.h"
  30. #include "povproto.h"
  31.  
  32. #include <proto/exec.h>
  33. #include <proto/intuition.h>
  34. #include <proto/graphics.h>
  35. #include <proto/dos.h>
  36. #include <proto/expansion.h>
  37. #include <exec/types.h>
  38. #include <intuition/intuition.h>
  39. #include <graphics/display.h>
  40. #include <graphics/gfxbase.h>
  41. #include <graphics/gfx.h>
  42. #include <libraries/expansionbase.h>
  43.  
  44. void geta4(void);
  45. void Requestor_Handler(void);
  46. void Amiga_open(void);
  47. void Amiga_close(void);
  48. void open_requestor(void);
  49. void close_requestor(void);
  50. void write_byte(int x, int y, unsigned char n);
  51.  
  52. void write_cookie(unsigned char *brand, int line);
  53. void make_hame_palette(struct ViewPort *vp);
  54. void SetRGB8 (short reg, unsigned char rr, unsigned char gg,
  55.               unsigned char bb, short base);
  56.  
  57. int open_ham(int input_width, int inpupt_height);
  58. void  write_ham_pixel(UWORD x, UWORD y, UBYTE r, UBYTE g, UBYTE b);
  59. int open_hame(void);
  60. void  write_hame_pixel(int x, int y, char Red, char Green, char Blue);
  61. int open_firecracker(void);
  62. void  write_firecracker_pixel(UWORD x, UWORD y, UBYTE r, UBYTE g, UBYTE b);
  63.  
  64. extern unsigned int Options;
  65. extern char DisplayFormat;
  66. extern FRAME Frame;
  67.  
  68. #define INT_REV 29L
  69. #define GR_REV 29L
  70.  
  71. struct IntuitionBase *IntuitionBase;
  72. struct GfxBase *GfxBase;
  73. struct Library *ExpansionBase;
  74.  
  75. struct Screen *s = NULL;
  76. volatile struct Window *w;
  77. struct Task *Requestor_Task;
  78.  
  79. volatile int Requestor_Running;
  80. volatile extern int Stop_Flag;
  81.  
  82. int width, height, depth;
  83. int bytesperrow;
  84. int leftedge, rightedge;
  85. int topedge, bottomedge;
  86.  
  87. int IsAGA;
  88.  
  89. struct Rectangle Rect1 =
  90.    {
  91.    0, 0, 0, 0
  92.    };
  93.  
  94. struct TagItem Ham_Screen_Tags[] =
  95.    {
  96.        { SA_DClip, &Rect1 },
  97.        { TAG_END, 0 }
  98.    };
  99.  
  100. struct ExtNewScreen Ham_Screen =
  101.    {
  102.    0, 0,
  103.    0, 0,
  104.    6,
  105.    0, 1,
  106.    HAM,
  107.    SCREENQUIET | NS_EXTENDED,
  108.    NULL,
  109.    (UBYTE *) "POV-Ray",
  110.    NULL,
  111.    NULL,
  112.    Ham_Screen_Tags
  113.    };
  114.  
  115.  
  116. struct NewScreen Ham_E_Screen =
  117.    {
  118.    0, 0,
  119.    0, 0,
  120.    4,
  121.    0, 1,
  122.    INTERLACE | HIRES,
  123.    SCREENQUIET,
  124.    NULL,
  125.    (UBYTE *) "POV-Ray",
  126.    NULL,
  127.    NULL
  128.    };
  129.  
  130. int lacer; /* if non-zero, screen is an interlace screen. Set this... */
  131.            /* ...as soon as you open your HAM-E screen.               */
  132.  
  133. unsigned char *fp0,*fp1,*fp2,*fp3; /* These are pointers which have been... */
  134.                                    /* ...cached from the screens BitMap[]   */
  135.                                    /* ...array. This allows us to get at    */
  136.                                    /* ...them much faster. Set them as soon */
  137.                                    /* ...as you open your HAM-E screen.     */
  138.  
  139. unsigned char bitpat[] =    /* This table is used as a table of masks... */
  140.   {                         /* ...to isolate bits in the HAM-E pixels    */
  141.     128,64,32,16,8,4,2,1,
  142.   };
  143.  
  144. unsigned char ham_cookie[] =  /* ham mode cookie... preceeds any HAM...    */
  145.   {                           /* ...color registers, and triggers hardware */
  146.     0xA2,0xF5,0x84,0xDC,      /* ...into ham mode.                         */
  147.     0x6D,0xB0,0x7F,0x18
  148.   };
  149.  
  150. struct Window *Requestor_Window;
  151. volatile struct MsgPort *Requestor_Port;
  152.  
  153. struct IntuiText chip Body_Text =
  154.    {0, 1, JAM1, 5, 10, NULL, (UBYTE *) "Click to abort the picture", NULL};
  155.  
  156. struct IntuiText chip Abort_Text =
  157.    {0, 1, JAM1, 5, 3, NULL, (UBYTE *) "Abort", NULL};
  158.  
  159. UWORD chip ColorTbl[16] = { 0x000, 0x111, 0x222, 0x333, 0x444, 0x555, 0x666,
  160.                        0x777, 0x888, 0x999, 0xaaa, 0xbbb, 0xccc, 0xddd,
  161.                        0xeee, 0xfff };
  162.  
  163. LONG last_red = 0, last_green = 0, last_blue = 0, last_x = -1, last_y = -1;
  164.  
  165. /* Firecracker routines */
  166. typedef struct BOARD {
  167.     ULONG    orgb;
  168.     ULONG    orgb2;        /* autoincrementing */
  169.     UWORD    pad;
  170.     UBYTE    control0;
  171.     UBYTE    control1;
  172.     UWORD    y, x;
  173. } BOARD;
  174.  
  175. BOARD        *board;
  176.  
  177. void amiga_init_POVRAY(void )
  178.    {
  179.    (void) onbreak(amiga_close_all);
  180.    }
  181.  
  182. int matherr (x)
  183.    struct exception *x;
  184.    {
  185.    fprintf (stderr, "Math error type: %d from function %s values: %g %g\n",
  186.            x->type, x->name, x->arg1, x->arg2);
  187.  
  188.    switch(x->type) 
  189.      {
  190.      case DOMAIN:
  191.      case OVERFLOW:
  192.         x->retval = 1.0e17;
  193.         break;
  194.  
  195.      case SING:
  196.      case UNDERFLOW:
  197.         x->retval = 0.0;
  198.         break;
  199.  
  200.      case TLOSS:
  201.      case PLOSS:
  202.         return (0);
  203.  
  204.      default:
  205.         break;
  206.      }
  207.    return(1);
  208.    }
  209.  
  210. void Requestor_Handler ()
  211.    {
  212.    Requestor_Port = CreatePort ("ray trace port", 0L);
  213.    Requestor_Window = BuildSysRequest
  214.              (NULL, &Body_Text, NULL, &Abort_Text, GADGETUP, 280L, 60L);
  215.    Wait ((1 << Requestor_Port -> mp_SigBit)
  216.           | (1 << Requestor_Window -> UserPort -> mp_SigBit));
  217.  
  218.    Requestor_Running = FALSE;
  219.    Stop_Flag = TRUE;
  220.    }
  221.  
  222. void Amiga_open()
  223.    {
  224.    IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library",INT_REV);
  225.    if (IntuitionBase == NULL)
  226.      exit(FALSE);
  227.  
  228.    GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", GR_REV);
  229.    if (GfxBase == NULL)
  230.      exit(FALSE);
  231.  
  232.    IsAGA = (GfxBase->ChipRevBits0) ? TRUE : FALSE;
  233.  
  234.    Requestor_Running = FALSE;
  235.    }
  236.  
  237. void Amiga_close()
  238.    {
  239.    if (Requestor_Running) {
  240.       Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  241.       Delay (2L);
  242.       }
  243.  
  244.    if (Requestor_Window)
  245.       FreeSysRequest (Requestor_Window);
  246.  
  247.    Requestor_Window = NULL;
  248.  
  249.    CloseLibrary ((struct Library *) GfxBase) ;
  250.    CloseLibrary (IntuitionBase) ;
  251.    }
  252.  
  253. void open_requestor()
  254.    {
  255.    Requestor_Window = NULL;
  256.    Stop_Flag = FALSE;
  257.    Requestor_Running = TRUE;
  258.    Requestor_Task = CreateTask ("Raytrace Requestor", 2L,
  259.                                 (APTR) Requestor_Handler, 20000L);
  260.    }
  261.  
  262. void display_finished ()
  263.    {
  264.    if (Requestor_Running) {
  265.      Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  266.      Delay (2L);
  267.      }
  268.  
  269.    if (Requestor_Window)
  270.       FreeSysRequest (Requestor_Window);
  271.  
  272.    Requestor_Window = NULL;
  273.    if (Options & PROMPTEXIT)
  274.       {
  275.       printf ("Finished.\nPress CR to quit.\n");
  276.       getchar();
  277.       }
  278.    }
  279.  
  280. int open_ham (input_width, input_height)
  281. int input_width;
  282. int input_height;
  283. {
  284.     int screen_width, screen_height;
  285.     int viewmodes;
  286.  
  287.  
  288.     viewmodes = HAM;
  289.  
  290.     if (input_width < 1 || input_height < 1)
  291.     {
  292.         display_close ();
  293.         printf ("? Size must be no smaller than 1 x 1\n");
  294.         return TRUE;
  295.     }
  296.  
  297.     if (input_width > 736 || input_height > 482)
  298.     {
  299.         display_close ();
  300.         printf ("? Size must be no larger than 736 x 482\n");
  301.         return TRUE;
  302.     }
  303.  
  304.     screen_width = 320;
  305.     if (input_width > 368 && input_width <= 736)
  306.     {
  307.         screen_width = 640;
  308.         viewmodes |= HIRES;
  309.     }
  310.     else if (input_width > 736)    /* Add higher resolutions */
  311.     {
  312.         screen_width = 640;
  313.         viewmodes |= HIRES;
  314.     }
  315.  
  316.     width = (input_width>screen_width) ? input_width : screen_width;
  317.  
  318.     leftedge = 0;
  319.     rightedge = width;
  320.     if (input_width < screen_width)
  321.     {
  322.         leftedge  = (screen_width - input_width) / 2;
  323.         rightedge = leftedge + input_width;
  324.     }
  325.  
  326.     screen_height = 200;
  327.     if (input_height > 241 && input_height <= 482)
  328.     {
  329.         screen_height = 400;
  330.         viewmodes |= LACE;
  331.     }
  332.     else if (input_height > 482)    /* Add higher resolutions */
  333.     {
  334.         screen_height = 400;
  335.         viewmodes |= LACE;
  336.     }
  337.  
  338.     height = (input_height>screen_height) ? input_height : screen_height;
  339.  
  340.     topedge = 0;
  341.     bottomedge = height;
  342.     if (input_height < screen_height)
  343.     {
  344.         topedge    = (screen_height - input_height) / 2;
  345.         bottomedge = topedge + input_height;
  346.     }
  347.  
  348.     depth = (IsAGA) ? 8 : 6;
  349.  
  350.     Rect1.MaxX = width-1;
  351.     Rect1.MaxY = height-1;
  352.     Ham_Screen.Width = width;
  353.     Ham_Screen.Height = height;
  354.     Ham_Screen.Depth = depth;
  355.     Ham_Screen.ViewModes |= viewmodes;
  356.  
  357.     if ((s = (struct Screen *) OpenScreen (&Ham_Screen)) == NULL)
  358.     {
  359.        display_close ();
  360.        printf ("? Can't open screen.\n");
  361.        return TRUE;
  362.     }
  363.  
  364.     /* What did we actually get? */
  365.     width = s->Width;
  366.     height = s->Height;
  367.     depth = s->BitMap.Depth;
  368.     bytesperrow = s->BitMap.BytesPerRow;
  369.  
  370.     ShowTitle (s, FALSE);
  371.  
  372.     LoadRGB4 (&(s->ViewPort), ColorTbl, 16L);
  373.     SetAPen (&(s->RastPort), 0L);
  374.     RectFill (&(s -> RastPort), 0L, 0L, width-1, height-1);
  375.  
  376.     return FALSE;
  377. }
  378.  
  379. #define absdif(x,y) ((x > y) ? (x - y) : (y - x))
  380. #define max3(x,y,z) ((x>y)?((x>z)?1:3):((y>z)?2:3))
  381.  
  382. #define HAM6_RED    0x20
  383. #define HAM6_GREEN    0x30
  384. #define HAM6_BLUE    0x10
  385. #define HAM8_MASK    0xFFFC
  386. #define HAM8_RED    0x02
  387. #define HAM8_GREEN    0x03
  388. #define HAM8_BLUE    0x01
  389.  
  390. void write_ham_pixel (x, y, Red, Green, Blue)
  391. UWORD x, y;
  392. UBYTE Red, Green, Blue;
  393. {
  394.    register short colour, index, mask, i, colour_mask;
  395.    register char *addr;
  396.    UBYTE delta_red, delta_green, delta_blue;
  397.  
  398.  
  399.    y += topedge;
  400.    x += leftedge;
  401.  
  402.    if (y < last_y)
  403.       return;
  404.  
  405.    if (last_y != y) {
  406.       last_y = y;
  407.       last_red = last_green = last_blue = 0;
  408.       }
  409.    else
  410.       if (x <= last_x)
  411.          return;
  412.  
  413.    last_x = x;
  414.  
  415.    if (IsAGA) {
  416.       Red   = Red   & HAM8_MASK;
  417.       Green = Green & HAM8_MASK;
  418.       Blue  = Blue  & HAM8_MASK;
  419.    }
  420.    else {
  421.       Red   = (Red   >> 4) & 0x0F;
  422.       Green = (Green >> 4) & 0x0F;
  423.       Blue  = (Blue  >> 4) & 0x0F;
  424.    }
  425.  
  426.    delta_red   = absdif (Red, last_red);
  427.    delta_green = absdif (Green, last_green);
  428.    delta_blue  = absdif (Blue, last_blue);
  429.  
  430.    if (IsAGA) {
  431.       switch (max3(delta_red, delta_green, delta_blue)) {
  432.       case 1:
  433.          last_red = Red;
  434.          colour = Red | HAM8_RED;
  435.          break;
  436.       case 2:
  437.          last_green = Green;
  438.          colour = Green | HAM8_GREEN;
  439.          break;
  440.       case 3:
  441.          last_blue = Blue;
  442.          colour = Blue | HAM8_BLUE;
  443.          break;
  444.       }
  445.    }
  446.    else {
  447.       switch (max3(delta_red, delta_green, delta_blue)) {
  448.       case 1:
  449.          last_red = Red;
  450.          colour = HAM6_RED + Red;
  451.          break;
  452.       case 2:
  453.          last_green = Green;
  454.          colour = HAM6_GREEN + Green;
  455.          break;
  456.       case 3:
  457.          last_blue = Blue;
  458.          colour = HAM6_BLUE + Blue;
  459.          break;
  460.       }
  461.    }
  462.  
  463.    index = (bytesperrow * y) + (x >> 3);
  464.    mask = 0x80 >> (x & 7);
  465.  
  466.    colour_mask = 1;
  467.  
  468.    for (i = 0 ; i < 6 ; i++) {
  469.       addr = &s->BitMap.Planes[i][index];
  470.       *addr &= ~mask;
  471.       *addr |= (colour&colour_mask) ? mask : 0x00;
  472.       colour_mask <<= 1;
  473.       }
  474.    }
  475.  
  476. int open_hame ()
  477. {
  478.     int screen_width, screen_height;
  479.  
  480.  
  481.     screen_width = GfxBase->NormalDisplayColumns>>1;
  482.     Ham_E_Screen.Width = screen_width<<1;
  483.     screen_height = GfxBase->NormalDisplayRows<<1;
  484.     Ham_E_Screen.Height = screen_height+2;
  485.  
  486.     if ((s = (struct Screen *) OpenScreen (&Ham_E_Screen)) == NULL)
  487.     {
  488.         display_close ();
  489.         return (TRUE);
  490.     }
  491.  
  492.     ShowTitle (s, FALSE);
  493.  
  494.     lacer = 1;
  495.  
  496.     fp0 = s->BitMap.Planes[0];
  497.     fp1 = s->BitMap.Planes[1];
  498.     fp2 = s->BitMap.Planes[2];
  499.     fp3 = s->BitMap.Planes[3];
  500.  
  501.     make_hame_palette(&s->ViewPort);
  502.  
  503.     SetAPen (&(s->RastPort), 0L);
  504.     RectFill (&(s -> RastPort), 0L, 0L, Ham_E_Screen.Width-1, 1);
  505.     SetAPen (&(s->RastPort), 1L);
  506.     RectFill (&(s -> RastPort), 0L, 2L, Ham_E_Screen.Width-1,
  507.                                         Ham_E_Screen.Height-1);
  508.     write_cookie(ham_cookie, 0);
  509.     SetRGB8 (0x11, 0x80, 0x80, 0x80, 0);
  510.  
  511.     return FALSE;
  512. }
  513.  
  514. void write_hame_pixel (x, y, Red, Green, Blue)
  515.    int x, y;
  516.    char Red, Green, Blue;
  517.    {
  518.    register unsigned char colour;
  519.    short delta_red, delta_green, delta_blue;
  520.  
  521.    if (y < last_y)
  522.       return;
  523.  
  524.    if ((x >= width) || (y >= height))
  525.       return;
  526.  
  527.    if (last_y != y) {
  528.       last_y = y;
  529.       last_red = last_green = last_blue = 0;
  530.       }
  531.    else
  532.       if (x <= last_x)
  533.          return;
  534.  
  535.    last_x = x;
  536.  
  537.    Red = (Red >> 2) & 0x3F;
  538.    Green = (Green >> 2) & 0x3F;
  539.    Blue = (Blue >> 2) & 0x3F;
  540.  
  541.    delta_red = absdif (Red, last_red);
  542.    delta_green = absdif (Green, last_green);
  543.    delta_blue = absdif (Blue, last_blue);
  544.  
  545.    switch (max3(delta_red, delta_green, delta_blue)) {
  546.       case 1:
  547.          last_red = Red;
  548.          colour = 0x80 + Red;
  549.          break;
  550.       case 2:
  551.          last_green = Green;
  552.          colour = 0xc0 + Green;
  553.          break;
  554.       case 3:
  555.          last_blue = Blue;
  556.          colour = 0x40 + Blue;
  557.          break;
  558.       }
  559.  
  560.    write_byte (x, y+2, colour);
  561.    }
  562.  
  563. int open_firecracker()
  564. {
  565.     struct ConfigDev    *dev;
  566.     ULONG            i;
  567.  
  568.     ExpansionBase = OpenLibrary("expansion.library", 0L);
  569.     if (!ExpansionBase) {
  570.         printf("No expansion library\n");
  571.         return TRUE;
  572.     }
  573.  
  574.     dev = FindConfigDev(NULL, 0x838, 0);
  575.     if (dev) {
  576.         board = (BOARD *)dev->cd_BoardAddr;
  577.     }
  578.     else {
  579.         printf("Can't find FireCracker\n");
  580.         return TRUE;
  581.     }
  582.  
  583.     CloseLibrary((struct Library *) ExpansionBase);
  584.         height = 482;
  585.  
  586.         if (Frame.Screen_Width > 768) {
  587.        board->control0 = 0xf8;
  588.            width = 1024;
  589.            }
  590.         else if (Frame.Screen_Width > 512) {
  591.        board->control0 = 0xb8;           
  592.            width = 768;
  593.            }
  594.         else if (Frame.Screen_Width > 384) {
  595.        board->control0 = 0x78;
  596.            width = 512;
  597.            }
  598.         else {
  599.        board->control0 = 0x38;
  600.            width = 384;
  601.            }
  602.  
  603.     board->control1 = 0x7f;
  604.     board->y = 0; board->x = 0;
  605.     for (i=0; i<width*482; i++) {
  606.            /* I write to the auto-increment and non-autoincrement registers
  607.               to make this work on 68000's as well as 68020's. */
  608.            board->orgb  = 0x40404040;
  609.            board->orgb2 = 0x40404040;
  610.            }
  611.  
  612.     return FALSE;
  613. }
  614.  
  615.  
  616. void write_firecracker_pixel(x,y, r,g,b)
  617. UWORD    x,y;
  618. UBYTE    r,g,b;
  619. {
  620.     ULONG        v1,v2,v3,v;
  621.         int             new_x, new_y;
  622.  
  623.     v1 = r; v2 = g; v3 = b;
  624.     v = ((v1<<16)&0xff0000) + ((v2<<8)&0xff00) + (v3&0xff);
  625.  
  626.     new_y = y+(482-Frame.Screen_Height)/2; new_x = x+(width-Frame.Screen_Width)/2;
  627.         if ((new_x >= 0) && (new_y >= 0)
  628.             && (new_x < width) && (new_y < 482)) {
  629.            board->y = new_y;
  630.            board->x = new_x;
  631.        board->orgb2 = v;
  632.            }
  633. }
  634.  
  635. /*:
  636. .n write_byte()
  637. .k low_level write_byte byte_write write_pixel
  638. .b
  639.  
  640. SYNOPSIS:   void write_byte(x,y,n);
  641.                 short x;
  642.                 int y;
  643.                 unsigned char n;
  644.  
  645. FUNCTION:   This function is similar to the Amiga's WritePixel routine.
  646.             is simply takes the incoming position and value and writes
  647.             them to the HAM-E screen appropriately.
  648.  
  649. INPUTS:     x - horizontal position on screen.
  650.             y - vertical position on screen.
  651.             n - value 0-255 to write in the pixel.
  652.  
  653. RESULTS:    None
  654.  
  655. BUGS:       None Known.
  656.  
  657. LIMITATIONS:Hard coded for screen width of 320 pixels (640 hi-res pixels)
  658.  
  659. SEE ALSO:   Global variables "fp3", "fp2", "fp1", "fp0", "bitpat[]"
  660.  
  661. :*/
  662.  
  663. void write_byte(x,y,n)
  664.   int x;
  665.   int y;
  666.   unsigned char n;
  667.   {
  668.   register int ypos,byte_offset;
  669.   register short bit_offset;
  670.     ypos = y * 80; /* index to correct scan line - note hard coded width! */
  671.     bit_offset = (x << 1) & 7; /* find base bit position */
  672.     byte_offset = (x >> 2);   /* find base byte offset */
  673.     if (n & 128) *(fp3 + ypos + byte_offset) |= bitpat[bit_offset];
  674.             else *(fp3 + ypos + byte_offset) &= ~bitpat[bit_offset];
  675.     if (n &  64) *(fp2 + ypos + byte_offset) |= bitpat[bit_offset];
  676.             else *(fp2 + ypos + byte_offset) &= ~bitpat[bit_offset];
  677.     if (n &  32) *(fp1 + ypos + byte_offset) |= bitpat[bit_offset];
  678.             else *(fp1 + ypos + byte_offset) &= ~bitpat[bit_offset];
  679.     if (n &  16) *(fp0 + ypos + byte_offset) |= bitpat[bit_offset];
  680.             else *(fp0 + ypos + byte_offset) &= ~bitpat[bit_offset];
  681.     bit_offset++; /* to next nybble */
  682.     if (bit_offset == 8) /* carry into next byte? */
  683.       {
  684.         bit_offset=0;
  685.         byte_offset++;
  686.       }
  687.     if (n & 8) *(fp3 + ypos + byte_offset) |= bitpat[bit_offset];
  688.           else *(fp3 + ypos + byte_offset) &= ~bitpat[bit_offset];
  689.     if (n & 4) *(fp2 + ypos + byte_offset) |= bitpat[bit_offset];
  690.           else *(fp2 + ypos + byte_offset) &= ~bitpat[bit_offset];
  691.     if (n & 2) *(fp1 + ypos + byte_offset) |= bitpat[bit_offset];
  692.           else *(fp1 + ypos + byte_offset) &= ~bitpat[bit_offset];
  693.     if (n & 1) *(fp0 + ypos + byte_offset) |= bitpat[bit_offset];
  694.           else *(fp0 + ypos + byte_offset) &= ~bitpat[bit_offset];
  695.   }
  696.  
  697. /*:
  698. .n write_cookie()
  699. .k cookie low_level setup configure
  700. .b
  701.  
  702. SYNOPSIS:   void write_cookie(brand,line);
  703.                 unsigned char *brand;
  704.                 int line;
  705.  
  706. FUNCTION:    This function writes the cookie on a particular line.
  707.              The variable "brand" is a pointer to an arrary of data
  708.              that contains the particular cookie for the mode you want.
  709.              These arrays are the global ones "ham_cookie[]" and
  710.              "reg_cookie[]".
  711.              
  712.              Call as:
  713.              
  714.                   write_cookie(ham_cookie,line);
  715.                               -or-
  716.                   write_cookie(reg_cookie,line);
  717.              
  718.              Note: If you have a four line palette, you need to call
  719.              this function for each successive line the palette exists
  720.              upon, for example:
  721.              
  722.                   write_cookie(reg_cookie,0);
  723.                   write_cookie(reg_cookie,1);
  724.                   write_cookie(reg_cookie,2);
  725.                   write_cookie(reg_cookie,3);
  726.                   
  727.                         -or-
  728.                   
  729.                   for (i=0; i<4; i++)
  730.                     {
  731.                       write_cookie(reg_cookie,i);
  732.                     }
  733.              
  734.              If the global variable "lacer" is set, this function will
  735.              write the cookie data on the appropriate lines in both
  736.              fields... sending four lines of cookie to the function
  737.              with the lines 0,1,2,3 will write cookies on line pairs
  738.              0-1, 2-3, 4-5, and 6-7.
  739.  
  740. INPUTS:     A pointer to the apropriate cookie and the line to put it on.
  741.  
  742. RESULTS:    None
  743.  
  744. BUGS:       None Known.
  745.  
  746. LIMITATIONS:None Known.
  747.  
  748. SEE ALSO:   The global variable "lacer" and the "ham_cookie[]"
  749.             and reg_cookie[] global arrays.
  750.  
  751. :*/
  752. void write_cookie(brand,line)
  753.   unsigned char *brand;
  754.   int line;
  755.   {
  756.   int i;
  757.     if (lacer) /* we need double the cookie data! */
  758.       {
  759.         for (i=0; i<8; i++)
  760.           {
  761.             write_byte(i,line*2,brand[i]);
  762.             write_byte(i,(line*2)+1,brand[i]);
  763.           }
  764.       }
  765.     else
  766.       {
  767.         for (i=0; i<8; i++)
  768.           {
  769.             write_byte(i,line,brand[i]);
  770.           }
  771.       }
  772.   }
  773.  
  774. /*:
  775. .n make_hame_palette()
  776. .k amiga_palette palette_amiga setup configure
  777. .b
  778.  
  779. SYNOPSIS:   void make_hame_palette(vp);
  780.                 struct ViewPort *vp;
  781.  
  782. FUNCTION:   Sets palette for Amiga side of hardware: The palette here is
  783.             designed to achieve two independant goals. First, and most
  784.             importantly, it creates a situation where the IRGB lines at
  785.             the Amiga's data port will exactly mirror the data in the
  786.             bitplanes of the screen as each pixel is emitted. 
  787.             Secondly, this palette makes the images visible, if not
  788.             sensible, on a non HAM-e equipped Amiga... Hopefully this
  789.             distinctive color palette will quickly cue the user that
  790.             they are missing something good. :^)
  791.             
  792.             IRGB to 12 bit correspondence:
  793.             
  794.             bit 8 - b3 of red
  795.             bit 4 - b3 of green
  796.             bit 2 - b3 of blue
  797.             bit 1 - b0 of blue
  798.  
  799. INPUTS:     A pointer to the ViewPort that "belongs" to this screen.
  800.  
  801. RESULTS:    None
  802.  
  803. BUGS:       None Known.
  804.  
  805. LIMITATIONS:None Known.
  806.  
  807. SEE ALSO:   
  808.  
  809. :*/
  810. void make_hame_palette(vp)
  811.   struct ViewPort *vp;
  812.   {
  813.   int rr,gg,bb,i;
  814.   int col;
  815.     col=0;
  816.     for (i=0; i<16; i++)
  817.       {
  818.         rr=0; gg=0; bb=0;
  819.         if (i & 8) rr  = 8; /* this builds the IRGB bit outputs...   */
  820.         if (i & 4) gg  = 8; /* ...these four bits are all that are   */
  821.         if (i & 2) bb  = 8; /* ...required to make the HAM-E run,    */
  822.         if (i & 1) bb |= 1; /* ...they xfer b0->b3 to the IRGB lines */
  823.         if (i != 0)     /* we don't mess with c0 - we leave it black */
  824.           {
  825.             rr += (col & 7); /* build strange colors in cregs 1-15...  */
  826.             col += 2;        /* ...these extra bits sent to the color  */
  827.             gg += (col & 7); /* ...registers make amiga palette very   */
  828.             col += 2;        /* ...interesting to look at if the HAM-E */
  829.             bb += (col & 6); /* ...is NOT attached. Otherwise useless. */
  830.             col += 2;        /* ...The code in this "if" is optional.  */
  831.           }
  832.         SetRGB4(vp,i,rr,gg,bb); /* this actually sets the Amiga color regs */
  833.       }
  834.   }
  835.  
  836. /*:
  837. .n SetRGB8()
  838. .k palette_hame hame_palette setup configure
  839. .b
  840.  
  841. SYNOPSIS:   void SetRGB8(reg,rr,gg,bb,base);
  842.                 int reg,rr,gg,bb,base;
  843.  
  844. FUNCTION:   This routine sets the color registers in the HAM-E hardware.
  845.             It can handle color registers located at any point on screen,
  846.             by setting the "base" variable to the starting scan line where
  847.             the color registers exist. Normally, the cookie and it's
  848.             associated color registers are located beginning at scan line 0.
  849.             If the screen is an interlace screen, this routine will set both
  850.             set of color registers identically; when in interlace, the
  851.             HAM-E maintains separate sets of color registers for the
  852.             odd and even interlace fields. Since this routine sets both
  853.             sets of color registers identically, you don't have to
  854.             worry about dealing with this feature.
  855.             
  856. INPUTS:     reg  - the color register number from 0 to 255 to be set
  857.             rr   - the red value from 0-255
  858.             gg   - the green value from 0-255
  859.             bb   - the blue value from 0-255
  860.             base - the starting scan line of the first coookie position
  861.                    (usually zero)
  862.  
  863. RESULTS:    None
  864.  
  865. BUGS:       None Known.
  866.  
  867. LIMITATIONS:None Known.
  868.  
  869. SEE ALSO:   The global variable "lacer"
  870.  
  871. :*/
  872. void SetRGB8(reg,rr,gg,bb,base)
  873.   short reg;
  874.   unsigned char rr,gg,bb;
  875.   short base;
  876.   {
  877.   short p_row,p_index;
  878.     p_row = (reg >> 6) + base;           /* palette row 0-3.  */
  879.     p_index = ((reg & 0x3f) * 3) + 8;    /* reg 0-63 in p_row */
  880.     if (lacer) /* then we need both fields! */
  881.       {
  882.         /* for the even field: */
  883.         write_byte(p_index,   p_row*2, rr);         /* put RED value     */
  884.         write_byte(p_index+1, p_row*2, gg);         /* put GREEN value   */
  885.         write_byte(p_index+2, p_row*2, bb);         /* put BLUE value    */
  886.         
  887.         /* here is the stuff for the ODD field: */
  888.         write_byte(p_index,   (p_row*2)+1, rr);     /* put RED value     */
  889.         write_byte(p_index+1, (p_row*2)+1, gg);     /* put GREEN value   */
  890.         write_byte(p_index+2, (p_row*2)+1, bb);     /* put BLUE value    */
  891.       }
  892.     else /* just write to one field */
  893.       {
  894.         write_byte(p_index,   p_row, rr);     /* put RED value     */
  895.         write_byte(p_index+1, p_row, gg);     /* put GREEN value   */
  896.         write_byte(p_index+2, p_row, bb);     /* put BLUE value    */
  897.       }
  898.   }
  899.  
  900. void display_init (input_width, input_height)
  901. int input_width, input_height;
  902. {
  903.     Amiga_open();
  904.     open_requestor();
  905.  
  906.     Delay (10);
  907.  
  908.     if (DisplayFormat == 'E') {
  909.         if (open_hame () == TRUE) {
  910.         display_close ();
  911.         exit (FALSE);
  912.         }
  913.     }
  914.     else if (DisplayFormat == 'F') {
  915.         if (open_firecracker () == TRUE) {
  916.         display_close ();
  917.         exit (999);
  918.         }
  919.     }
  920.     else {
  921.         if (DisplayFormat != '2')    /*  If not +d2, then force */
  922.         IsAGA = FALSE;        /*    AGA off              */
  923.  
  924.         IsAGA = FALSE;        /*  For now, AGA is off.   */
  925.  
  926.         if (open_ham (input_width, input_height) == TRUE) {
  927.         display_close ();
  928.         exit (FALSE);
  929.         }
  930.     }
  931. }
  932.  
  933. void display_close ()
  934.    {
  935.    if (Requestor_Running) {
  936.       Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  937.       Delay (2L);
  938.       }
  939.  
  940.    if (Requestor_Window)
  941.       FreeSysRequest (Requestor_Window);
  942.  
  943.    Requestor_Window = NULL;
  944.  
  945.    if (s != NULL) {
  946.       CloseScreen (s);
  947.       s = NULL;
  948.       }
  949.    }
  950.  
  951. void display_plot (x, y, Red, Green, Blue)
  952.    int x, y;
  953.    char Red, Green, Blue;
  954.    {
  955.    if (DisplayFormat == 'E')
  956.     return (write_hame_pixel (x, y, Red, Green, Blue));
  957.    else if (DisplayFormat == 'F')
  958.     return (write_firecracker_pixel((UWORD) x, (UWORD) y,
  959.         (UBYTE) Red, (UBYTE) Green, (UBYTE) Blue));
  960.    else
  961.     return (write_ham_pixel ((UWORD) x, (UWORD) y,
  962.         (UBYTE) Red, (UBYTE) Green, (UBYTE) Blue));
  963.    }
  964.  
  965. int amiga_close_all ()
  966.    {
  967.    close_all();
  968.    return (1);
  969.    }
  970.