|##########|
|#MAGIC   #|CLABLILH
|#PROJECT #|""
|#PATHS   #|"StdProject"
|#FLAGS   #|xx-x-x--x---x-x-----------------
|#USERSW  #|--------------------------------
|#USERMASK#|--------------------------------
|#SWITCHES#|xx---xxxxx------
|##########|
DEFINITION MODULE Hardware;
(* $A- *)
FROM System IMPORT BITSET,PROC,SHORTSET;

TYPE
  AdkFlags      = (use0v1,use1v2,use2v3,use3vn,use0p1,use1p2,use2p3,use3pn,
                   fast,msbSync,wordSync,uartBrk,mfmPrec,preComp0,
                   preComp1,adkSet);
  AdkFlagSet    = SET OF AdkFlags;

CONST
  pre000ns      = AdkFlagSet:{};
  pre140ns      = AdkFlagSet:{preComp0};
  pre280ns      = AdkFlagSet:{preComp1};
  pre560ns      = AdkFlagSet:{preComp0,preComp1};

TYPE
  DmaFlags      = (aud0,aud1,aud2,aud3,disk,sprite,blitter,copper,
                   raster,master,blithog,df11,df12,bltnzero,bltdone,dmaSet);
  DmaFlagSet    = SET OF DmaFlags;

CONST
  DmaAll        = DmaFlagSet:{aud0..raster};

TYPE
  IntFlags      = (tbe,      dskblk,   softint,  ports,    coper,    vertb,
                   blit,     aud0i,    aud1i,    aud2i,    aud3i,    rbf,
                   disksync, exter,    inten,    intSet );
  IntFlagSet    = SET OF IntFlags;

CONST
  hSizeBits     = 6;
  vSizeBits     = 16-hSizeBits;
  hSizeMask     = %0000000000111111;
  vSizeMask     = %0000001111111111;
  maxBytesPerRow= 128;

TYPE
  BC0Flags      = (nanbnc,nanbc,nabnc,nabc,anbnc,anbc,abnc,abc,
                   dest,srcC,srcB,srcA,ash1,ash2,ash4,ash8);
  BC0FlagSet    = SET OF BC0Flags;
  BC0FlagSetLow = SET OF [nanbnc..abc];

CONST
  aORb          = BC0FlagSet:{nabnc,nabc,anbnc,anbc,abnc,abc};
  aORc          = BC0FlagSet:{nanbc,nabc,anbnc,anbc,abnc,abc};
  aXORc         = BC0FlagSet:{nabc,abnc,nanbc,anbnc};
  aTOd          = BC0FlagSet:{abc,anbc,abnc,anbnc};

TYPE
  BC1Flags      = (lineMode,desc,fillCarryIn,fillOr,fillXor,ovFlag,signFlag,
                   bf7,bf8,bf9,bf10,bf11,bsh1,bsh2,bsh4,bsh8);
  BC1FlagSet    = SET OF BC1Flags;

CONST
  OneDot        = desc;
  blitReverse   = desc;
  aul           = BC1FlagSet:{fillCarryIn};
  sul           = BC1FlagSet:{fillOr};
  sud           = BC1FlagSet:{fillXor};
  octant1       = sud;
  octant2       = BC1FlagSet:{};
  octant3       = sul;
  octant4       = aul+sud;
  octant5       = aul+sul+sud;
  octant6       = aul+sul;
  octant7       = aul;
  octant8       = sul+sud;

TYPE
  BltNodePtr    = POINTER TO BltNode;
  BltNode       = RECORD
                    next      : BltNodePtr;
                    function  : PROC;
                    stat      : SHORTINT;
                    blitSize  : INTEGER;
                    beamSync  : INTEGER;
                    cleanUp   : PROC
                  END;

CONST
  cleanup       = $40;

TYPE
  BP0Flags          = (useBP3,ersy,bplace,lpen,bp04,bp05,bp06,bp07,gaud,
                       color,dbplf,homod,bpu0,bpu1,bpu2,bphires);
  BP0FlagSet        = SET OF BP0Flags;
  BP1Flags          = (p1h0,p1h1,p1h2,p1h3,p2h0,p2h1,p2h2,p2h3,bp18);
  BP1FlagSet        = SET OF BP1Flags;
  BP2Flags          = (pf1p0,pf1p1,pf1p2,pf2p0,pf2p1,pf2p2,pf2pri,bp27,
                       bp28,bp29,zdCten,zdBPen,zdBPSel0,zdBPSel1,zdBPSel2);
  BP2FlagSet        = SET OF BP2Flags;
  BP3Flags          = (extBlnkEn,extBlkZD,zdClkEn,bp33,brdNTran,brdNBlnk);
  BP3FlagSet        = SET OF BP3Flags;

  Beam0Flags        = (hSyncTrue,vSyncTrue,CSyncTrue,csBlank,
                       varCSync,displayPal,displayDual,varBeam,
                       varHSync,varVSync,cscBlankEn,loLDis,
                       varVBlank);
  Beam0FlagSet      = SET OF Beam0Flags;

  CXDFlags          = (p1p2,p1s01,p1s23,p1s45,p1s67,p2s01,p2s23,p2s45,p2s67,
                       s01s23,s01s45,s01s67,s23s45,s23s67,s45s67);
  CXDFlagSet        = SET OF CXDFlags;
  CXCFlags          = (mvbp1,mvbp2,mvbp3,mvbp4,mvbp5,mvbp6,enbp1,enbp2,
                       enbp3,enbp4,enbp5,enbp6,ensp1,ensp3,ensp5,ensp7);
  CXCFlagSet        = SET OF CXCFlags;

  PotFlags          = (start,pf1,pf2,pf3,pf4,pf5,pf6,pf7,datalx,outlx,
                       dataly,outly,datarx,outrx,datary,outry);
  PotFlagSet        = SET OF PotFlags;

  SpriteControlFlags = (sho,ev8,sv8,sct3,sct4,sct5,sct6,att);
  SpriteControlFlagSet = SET OF SpriteControlFlags;
  SpriteControlInfo = RECORD
                        ev     : SHORTCARD;
                        flags  : SpriteControlFlagSet;
                      END;
  SpriteInfo        = RECORD
                        pos   : CARDINAL;
                        ctl   : SpriteControlInfo;
                        data  : LONGCARD;
                      END;
  Sprites           = ARRAY [0..7] OF SpriteInfo;

  SerialFlags       = (d8,stop,sf2,rxd,tsre,tbes,rbfs,ovrun);
  SerialFlagSet     = SET OF SerialFlags;
  SerialInfo        = RECORD
                        flags : SerialFlagSet;
                        data  : CHAR
                      END;

  DiskFlags         = (df0,df1,df2,df3,wordEqual,diskWrite,dmaOn,dskByte);
  DiskFlagSet       = SET OF DiskFlags;
  DiskInfo          = RECORD
                        flags : DiskFlagSet;
                        data  : SHORTCARD
                      END;
  Coord             = RECORD
                        v,h : SHORTINT
                      END;

  CustomPtr         = POINTER TO
                        RECORD
                          bltddat    : BITSET;
                          dmaconr    : DmaFlagSet;
                          vposr      : LONGCARD;
                          dskdatr    : CARDINAL;
                          joy0dat,
                          joy1dat    : Coord;
                          clxdat     : CXDFlagSet;
                          adkconr    : AdkFlagSet;
                          pot0dat,
                          pot1dat    : Coord;
                          potgor     : PotFlagSet;
                          serdatr    : SerialInfo;
                          dskbytr    : DiskInfo;
                          intenar,
                          intreqr    : IntFlagSet;
                          dskpt      : ANYPTR;
                          dsklen     : CARDINAL;
                          dskdat     : CARDINAL;
                          refptr     : CARDINAL;
                          vpos       : LONGCARD;
                          copcon     : BOOLEAN;
                          serdat     : SerialInfo;
                          serper     : CARDINAL;
                          potgo      : PotFlagSet;
                          joytest    : Coord;
                          strequ     : CARDINAL;
                          strvbl     : CARDINAL;
                          strhor     : CARDINAL;
                          strlong    : CARDINAL;
                          bltcon0    : BC0FlagSet;
                          bltcon1    : BC1FlagSet;
                          bltafwm,
                          bltalwm    : BITSET;
                          bltcpt,
                          bltbpt,
                          bltapt,
                          bltdpt     : ANYPTR;
                          bltsize    : CARDINAL;
                          pad2d      : SHORTCARD;
                          bltcon0l   : BC0FlagSetLow;
                          bltsizv,
                          bltsizh    : CARDINAL;
                          bltcmod,
                          bltbmod,
                          bltamod,
                          bltdmod    : CARDINAL;
                          unused2    : ARRAY [0..3] OF CARDINAL;
                          bltcdat,
                          bltbdat,
                          bltadat    : BITSET;
                          unused3    : ARRAY [0..2] OF CARDINAL;
                          deniseid   : CARDINAL;
                          dsksync    : BITSET;
                          cop1lc,
                          cop2lc     : ANYPTR;
                          copjmp1,
                          copjmp2    : INTEGER;
                          copins     : CARDINAL;
                          diwstrt,
                          diwstop,
                          ddfstrt,
                          ddfstop    : Coord;
                          dmacon     : DmaFlagSet;
                          clxcon     : CXCFlagSet;
                          intena,
                          intreq     : IntFlagSet;
                          adkcon     : AdkFlagSet;
                          aud        : ARRAY [0..3] OF
                                         RECORD
                                            audlc  : ANYPTR;
                                            audlen : CARDINAL;
                                            audper : CARDINAL;
                                            audvol : CARDINAL;
                                            auddat : INTEGER;
                                            unused : LONGINT
                                          END;
                          bplpt      : ARRAY [0..7] OF ANYPTR;
                          bplcon0    : BP0FlagSet;
                          bplcon1    : BP1FlagSet;
                          bplcon2    : BP2FlagSet;
                          bplcon3    : BP3FlagSet;
                          bpl1mod,
                          bpl2mod    : CARDINAL;
                          unused5    : LONGINT;
                          bpldat     : ARRAY [0..7] OF CARDINAL;
                          sprpt      : ARRAY [0..7] OF ANYPTR;
                          spr        : Sprites;
                          colors     : ARRAY [0..31] OF CARDINAL;
                          htotal     : CARDINAL;
                          hsstop     : CARDINAL;
                          hbstrt     : CARDINAL;
                          hbstop     : CARDINAL;
                          vtotal     : CARDINAL;
                          vsstop     : CARDINAL;
                          vbstrt     : CARDINAL;
                          vbstop     : CARDINAL;
                          sprhstrt   : CARDINAL;
                          sprhstop   : CARDINAL;
                          bplhstrt   : CARDINAL;
                          bplhstop   : CARDINAL;
                          hhposw     : CARDINAL;
                          hhposr     : CARDINAL;
                          beamcon0   : Beam0FlagSet;
                          hsstrt     : CARDINAL;
                          vsstrt     : CARDINAL;
                          hcenter    : CARDINAL;
                          diwhigh    : CARDINAL;
                        END;

CONST
  Custom        = CustomPtr($DFF000);

TYPE
  CiaIcrFlags   = (ta,tb,alrm,so,flg,if5,if6,setClr);
  CiaIcrFlagSet = SET OF CiaIcrFlags;

CONST
  ir            = setClr;

TYPE
  CiaCraFlags   = (craStart,craPbon,craOutmode,craRunmode,craLoad,
                   craInmode,craSpmode,craTodin);
  CiaCraFlagSet = SET OF CiaCraFlags;
  CiaCrbFlags   = (crbStart,crbPbon,crbOutmode,crbRunmode,crbLoad,
                   crbInmode0,crbInmode1,crbAlarm);
  CiaCrbFlagSet = SET OF CiaCrbFlags;

  CiaaPraFlags  = (overlay,led,dskChange,dskPrt,dskTrack0,dskRdy,
                   gamePort0,gamePort1);
  CiaaPraFlagSet= SET OF CiaaPraFlags;
  CiaaPrbFlags  = [0..7];
  CiaaPrbFlagSet= SET OF CiaaPrbFlags;

  CiabPraFlags  = (prtrBusy,prtrPOut,prtrSel,comDSR,comCTS,comCD,comRTS,
                   comDTR);
  CiabPraFlagSet= SET OF CiabPraFlags;
  CiabPrbFlags  = (dskStep,dskDirec,dskSide,dskSel0,dskSel1,dskSel2,
                   dskSel3,dskMotor);
  CiabPrbFlagSet= SET OF CiabPrbFlags;

  Pad           = ARRAY [2..255] OF SHORTINT;

  CIAAPtr       = POINTER TO
                    RECORD
                      pra    : CiaaPraFlagSet; pad0  : Pad;
                      prb    : CiaaPrbFlagSet; pad1  : Pad;
                      ddra   : CiaaPraFlagSet; pad2  : Pad;
                      ddrb   : CiaaPrbFlagSet; pad3  : Pad;
                      talo   : SHORTCARD;      pad4  : Pad;
                      tahi   : SHORTCARD;      pad5  : Pad;
                      tblo   : SHORTCARD;      pad6  : Pad;
                      tbhi   : SHORTCARD;      pad7  : Pad;
                      todlo  : SHORTCARD;      pad8  : Pad;
                      todmid : SHORTCARD;      pad9  : Pad;
                      todhi  : SHORTCARD;      pad10 : Pad;
                      unused : SHORTCARD;      pad11 : Pad;
                      sdr    : SHORTSET;       pad12 : Pad;
                      icr    : CiaIcrFlagSet;  pad13 : Pad;
                      cra    : CiaCraFlagSet;  pad14 : Pad;
                      crb    : CiaCrbFlagSet;  pad15 : Pad;
                    END;
  CIABPtr       = POINTER TO
                    RECORD
                      pra    : CiabPraFlagSet; pad0  : Pad;
                      prb    : CiabPrbFlagSet; pad1  : Pad;
                      ddra   : CiabPraFlagSet; pad2  : Pad;
                      ddrb   : CiabPrbFlagSet; pad3  : Pad;
                      talo   : SHORTCARD;      pad4  : Pad;
                      tahi   : SHORTCARD;      pad5  : Pad;
                      tblo   : SHORTCARD;      pad6  : Pad;
                      tbhi   : SHORTCARD;      pad7  : Pad;
                      todlo  : SHORTCARD;      pad8  : Pad;
                      todmid : SHORTCARD;      pad9  : Pad;
                      todhi  : SHORTCARD;      pad10 : Pad;
                      unused : SHORTCARD;      pad11 : Pad;
                      sdr    : SHORTSET;       pad12 : Pad;
                      icr    : CiaIcrFlagSet;  pad13 : Pad;
                      cra    : CiaCraFlagSet;  pad14 : Pad;
                      crb    : CiaCrbFlagSet;  pad15 : Pad;
                    END;

CONST
  CIAA          = CIAAPtr($BFE001);
  CIAB          = CIABPtr($BFD000);

END Hardware.

