home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Emulation / AmiTRS80 / SRC / AmiTRS80v0.9f.c next >
C/C++ Source or Header  |  1999-01-03  |  9KB  |  472 lines

  1. #define INTUI_V36_NAMES_ONLY
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <exec/types.h>
  6. #include <exec/tasks.h>
  7. #include <intuition/intuition.h>
  8. #include <intuition/screens.h>
  9. #include <graphics/displayinfo.h>
  10. #include <graphics/text.h>
  11. #include <dos/dos.h>
  12.  
  13. #include <clib/exec_protos.h>
  14. #include <clib/alib_protos.h>
  15. #include <clib/dos_protos.h>
  16. #include <clib/asl_protos.h>
  17. #include <clib/intuition_protos.h>
  18.  
  19.  
  20. int CXBRK(void)    { return(0); }  /* Disable CTRL/C handling */
  21. int chkabort(void) { return(0); }  /* really */
  22.  
  23. #include "screenchar.h"    /* trs80 font definition*/
  24. #include "gadgets.h"    /* gadget definitions    */
  25. #include "keyboard.h"    /* trs80 keyboard map    */
  26.  
  27.  
  28. extern UBYTE far rom;    /* trs80 rom file    */
  29. extern far UBYTE *JUMPTBL; /* trs80 jumptbl ptr */
  30. struct Task *trstask=NULL;
  31. char *trstaskname = "Trashtask";
  32.  
  33.  
  34. struct Library *IntuitionBase;
  35. struct Library *AslBase;
  36.  
  37. UBYTE *scrn[1024];    /* array of ptrs to actual scrn locs */
  38. ULONG SIGMASK=0;
  39. UWORD PROGCOUNT=0;
  40. UWORD CASSPTR=65535;
  41. UWORD CHL;
  42. UWORD CBC;
  43. UWORD CDE;
  44. UWORD TRACE=0;    /*0=NO TRACING 1=SINGLESTEP*/
  45.  
  46. VOID initscrnarray(struct Screen *scree); /* initialize the screen ptr array*/
  47. void START00(void);
  48. void RESET00(void);
  49. void PAUSE00(void);
  50. int loadcmd(void);
  51. int savecass(void);
  52. int loadcass(void);
  53. int skip(BPTR filep, int n);
  54. int getlowhi(BPTR filep, int *val);
  55. int gethilow(BPTR filep, int *val);
  56. BPTR fp;
  57. char fname[256];
  58. struct FileRequester *fr;
  59. struct Window *win;
  60.  
  61.  
  62. VOID main()
  63. {
  64.  
  65. UWORD pens[] = { ~0 };
  66. struct ColorSpec cols[]={0,0,0,0,    /* colour 0*/
  67.              1,0,205,0,    /* colour 1*/
  68.              -1,0,0,0};    /* end of array */
  69.  
  70.  
  71. struct TextAttr myta = {"topaz.font",8,NULL,NULL,};
  72.  
  73. UBYTE *romptr;
  74. UBYTE done=FALSE;
  75.  
  76. struct Screen *my_screen;
  77. struct Gadget *lesgad;
  78. ULONG class;
  79. UWORD code;
  80. struct IntuiMessage *message;
  81. BOOL shiftkey = FALSE;
  82. UBYTE *shiftloc;
  83. UBYTE *keyloc;
  84. UBYTE *keydown;
  85. int c;
  86.  
  87.  
  88.  
  89. IntuitionBase = OpenLibrary("intuition.library",0);
  90. if (NULL != IntuitionBase)
  91.     {
  92.     if (IntuitionBase->lib_Version >= 37)
  93.         {
  94.          if (NULL != (my_screen = OpenScreenTags(NULL,
  95.                      SA_DisplayID, HIRES_KEY,
  96.                      SA_Width, 512,
  97.                      SA_Height, 171,
  98.                      SA_Left, 32,
  99.                      SA_Pens, (ULONG)pens,
  100.                      SA_Colors, cols,
  101.                      SA_Title,"AmiTRS-80 Model 1 Emulator V0.9f 1999 RedSkull^DC",
  102.                                      SA_Depth, 1,
  103.                      SA_Font, &myta,
  104.                      TAG_DONE)))
  105.             {
  106.             /* screen successfully opened */
  107.  
  108.          if (NULL != (win = OpenWindowTags(NULL,
  109.                     WA_CustomScreen, my_screen,
  110.                     WA_Width, 512,
  111.                     WA_Height, 171,
  112.                     WA_Gadgets,&buttonGad,
  113.                     WA_Backdrop, TRUE,
  114.                     WA_IDCMP, IDCMP_GADGETUP | IDCMP_RAWKEY,
  115.                     WA_Borderless, TRUE,
  116.                     WA_Activate, TRUE,
  117.                     TAG_DONE)))
  118.         {
  119.         /* Window now opened */
  120.  
  121.         romptr=&rom; /* start of trs80 mem map */
  122.         initscrnarray(my_screen);
  123.     
  124.             if(JUMPTBL= AllocMem(262144,0))
  125.             {
  126.              shiftloc=&rom+0x3880;
  127.              keyloc=&rom+0x3800;
  128.              keydown=&rom+0x387f;    /* any key down location */
  129.  
  130.              trstask=CreateTask(trstaskname,0,START00,1000L);
  131.              if(!trstask) printf("Couldn't create the TRSTask");
  132.              while (!done)
  133.              {
  134.             Wait(1L<<win->UserPort->mp_SigBit);
  135.             while(message = (struct IntuiMessage *)GetMsg(win->UserPort))
  136.               {
  137.                 class= message->Class;
  138.                 code = message->Code;
  139.             if (class == IDCMP_GADGETUP)
  140.                 lesgad=(struct Gadget *)message->IAddress;
  141.             ReplyMsg((struct Message *)message);
  142.                 if (class==IDCMP_GADGETUP)
  143.                 {
  144.                 switch (lesgad->GadgetID)
  145.  
  146.                     {
  147.                     case 1:
  148.                         Forbid();
  149.                         if(FindTask(trstaskname)) RemTask(trstask);
  150.                         Permit();
  151.                         done=TRUE;
  152.                         break;
  153.                     case 2:
  154.                         if(lesgad->Flags&GFLG_SELECTED)
  155.                         PAUSE00();
  156.                         else
  157.                         Signal(trstask,SIGMASK);
  158.                         break;
  159.                     case 3:
  160.                         RESET00();
  161.                         break;
  162.                     case 4:
  163.                         PAUSE00();
  164.                         loadcmd();
  165.                         Signal(trstask,SIGMASK);
  166.                         break;
  167.  
  168.                         /*SinglStep
  169.                         printf("PC=%04x AF=%04x BC=%04x DE=%04x HL=%04x IX=%04x IY=%04x\n",PROGCOUNT,CASSPTR,CBC,CDE,CHL,CASSPTR,CASSPTR);
  170.                         Signal(trstask,SIGMASK);
  171.                         break;*/
  172.                     case 5:
  173.                         loadcass();
  174.                         break;
  175.                     case 6:
  176.                         savecass();
  177.                         break;
  178.                     }
  179.                 }
  180.             if (class == IDCMP_RAWKEY)
  181.              {
  182.               if (code > 0x7f)
  183.                { 
  184.                 switch(code)
  185.                  {
  186.                   case 0xE0:
  187.                   case 0xE1:
  188.                     shiftkey=FALSE;
  189.                     break;
  190.                   default:
  191.                     c= (int) (0x7f & code);
  192.                     if (shiftkey)
  193.                      {
  194.                      *shiftloc&=~kbdarr[c].shf2;
  195.                      *(keyloc+kbdarr[c].col2)&=~kbdarr[c].bit2;
  196.                      }
  197.                     else
  198.                      {
  199.                      *shiftloc&=~kbdarr[c].shf1;
  200.                      *(keyloc+kbdarr[c].col1)&=~kbdarr[c].bit1;
  201.                      }
  202.  
  203.                  }
  204.                 *keydown=0;    /* no key is now pressed */
  205.                }
  206.               else
  207.                { 
  208.                 switch(code)
  209.                  {
  210.                   case 0x60:
  211.                   case 0x61:
  212.                     shiftkey=TRUE;
  213.                     break;
  214.                   default:
  215.                     c= (int) (0x7f & code);
  216.                     if (shiftkey)
  217.                      {
  218.                      *shiftloc|=kbdarr[c].shf2;
  219.                      *(keyloc+kbdarr[c].col2)|=kbdarr[c].bit2;
  220.                      Delay(1L);
  221.                      }
  222.                     else
  223.                      {
  224.                      *shiftloc|=kbdarr[c].shf1;
  225.                      *(keyloc+kbdarr[c].col1)|=kbdarr[c].bit1;
  226.                      Delay(1L);
  227.                      }
  228.                  }
  229.                 *keydown=255;    /* set "any key down" */
  230.                }
  231.                }
  232.  
  233.                }
  234.              }
  235.              Delay(50L);
  236.              FreeMem(JUMPTBL,262144);
  237.             }
  238.  
  239.         
  240.  
  241.         CloseWindow(win);
  242.         }
  243.             CloseScreen(my_screen);
  244.             }
  245.         }
  246.     CloseLibrary(IntuitionBase);
  247.     }
  248. }
  249.  
  250. VOID initscrnarray(struct Screen *scree)
  251. {
  252. UBYTE *testloc;
  253. USHORT cnt;
  254.  
  255.         testloc=scree->BitMap.Planes[0];
  256.         testloc+=1728; /* start at line 4 */
  257.         
  258.         for (cnt=0;cnt<1024;cnt+=1)
  259.         {
  260.  
  261.         if(cnt==64)testloc+=512;
  262.         if(cnt==128)testloc+=512;
  263.         if(cnt==192)testloc+=512;
  264.         if(cnt==256)testloc+=512;
  265.         if(cnt==320)testloc+=512;
  266.         if(cnt==384)testloc+=512;
  267.         if(cnt==448)testloc+=512;
  268.         if(cnt==512)testloc+=512;
  269.         if(cnt==576)testloc+=512;
  270.         if(cnt==640)testloc+=512;
  271.         if(cnt==704)testloc+=512;
  272.         if(cnt==768)testloc+=512;
  273.         if(cnt==832)testloc+=512;
  274.         if(cnt==896)testloc+=512;
  275.         if(cnt==960)testloc+=512;
  276.  
  277.         scrn[cnt]=testloc;
  278.         testloc+=1;
  279.         }
  280. }
  281.  
  282.  
  283.  
  284.  
  285. int loadcmd(void)
  286. {
  287.     int    n;
  288.     int i;
  289.     unsigned int addr;
  290.     UBYTE *dum;
  291.     int ch;
  292.  
  293.  if (AslBase = OpenLibrary("asl.library", 37L))
  294.    {
  295.     if (fr = (struct FileRequester *)
  296.        AllocAslRequestTags(ASL_FileRequest,
  297.         ASL_Hail,       (ULONG)"Disk Image file requester",
  298.         ASL_Height,     150,
  299.         ASL_Width,      220,
  300.         ASL_LeftEdge,   20,
  301.         ASL_TopEdge,    20,
  302.         ASL_OKText,     (ULONG)"OKAY",
  303.         ASL_CancelText, (ULONG)"not OK",
  304.         ASL_File,       (ULONG)"newdos80",
  305.         ASL_Window,     win,
  306.         TAG_DONE))
  307.       {
  308.        if (AslRequest(fr, NULL))
  309.          {
  310.           strcpy(fname,fr->rf_Dir);
  311.           AddPart(fname,fr->rf_File,255);
  312.          }
  313.        FreeAslRequest(fr);
  314.       }
  315.     else printf("User Cancelled\n");
  316.     if(AslBase)CloseLibrary(AslBase);
  317.    }
  318.     if (!(fp = Open(fname,MODE_OLDFILE)))
  319.         return 0;
  320.  
  321.     ch = FGetC(fp);
  322.  
  323.     if (ch == 1)
  324.         UnGetC(fp,ch);
  325.     else if (ch != 5) {
  326.         Close(fp);
  327.         printf("No magic byte of 5, not a .cmd file.");
  328.         return 0;
  329.     }
  330.     else if (!skip(fp, FGetC(fp))) {
  331.         Close(fp);
  332.         return 0;
  333.     }
  334.  
  335.     for (;;) {
  336.         if (!gethilow(fp, &n)) { Close(fp); return 0; }
  337.         if (n == 514) break;
  338.  
  339.         if (n > 256)n -= 256;
  340.  
  341.         n -= 2;
  342.  
  343.         if (n == 0) n = 256;
  344.         else if (n < 0)n += 256;
  345.  
  346.         if (!getlowhi(fp, &addr)) { Close(fp); return 0; }
  347.         dum=&rom+addr;
  348.         for (i = 0; i < n; i++)
  349.             {
  350.             *dum++=FGetC(fp);
  351.             }
  352.     }
  353.  
  354.     if (!getlowhi(fp, &addr)) { Close(fp); return 0; }
  355.     Close(fp);
  356.     PROGCOUNT=(UWORD)addr;
  357.     return 1;
  358. }
  359.  
  360. int skip(BPTR filep, int n)
  361. {
  362.     while (n-- > 0)
  363.         if (FGetC(filep) == EOF) {
  364.             return 0;
  365.         }
  366.  
  367.     return 1;
  368. }
  369.  
  370. int gethilow(BPTR filep, int *val)
  371. {
  372.     int hi;
  373.     int low;
  374.  
  375.     hi = FGetC(filep);
  376.     low = FGetC(filep);
  377.  
  378.     if (hi == EOF || low == EOF) {
  379.         return 0;
  380.     }
  381.  
  382.     *val =  hi * 256 + low;
  383.  
  384.     return 1;
  385. }
  386.  
  387. int getlowhi(BPTR filep, int *val)
  388. {
  389.     int hi;
  390.     int low;
  391.  
  392.     low = FGetC(filep);
  393.     hi = FGetC(filep);
  394.  
  395.     if (low == EOF || hi == EOF) {
  396.         return 0;
  397.     }
  398.  
  399.     *val = hi * 256 + low;
  400.  
  401.     return 1;
  402. }
  403.     
  404. int savecass(void)
  405. {
  406.  if (AslBase = OpenLibrary("asl.library", 37L))
  407.    {
  408.     if (fr = (struct FileRequester *)
  409.        AllocAslRequestTags(ASL_FileRequest,
  410.         ASL_Hail,       (ULONG)"Cassfile requester",
  411.         ASL_Height,     150,
  412.         ASL_Width,      220,
  413.         ASL_LeftEdge,   20,
  414.         ASL_TopEdge,    20,
  415.         ASL_OKText,     (ULONG)"OKAY",
  416.         ASL_CancelText, (ULONG)"not OK",
  417.         ASL_File,       (ULONG)"Cassfile",
  418.         ASL_Window,     win,
  419.         TAG_DONE))
  420.       {
  421.        if (AslRequest(fr, NULL))
  422.          {
  423.           strcpy(fname,fr->rf_Dir);
  424.           AddPart(fname,fr->rf_File,255);
  425.          }
  426.        FreeAslRequest(fr);
  427.       }
  428.     if(AslBase)CloseLibrary(AslBase);
  429.    }
  430.     if (!(fp = Open(fname,MODE_NEWFILE)))
  431.         return 0;
  432.  
  433.     Write(fp,&rom+0x10000,(LONG)CASSPTR+1);
  434.     Close(fp);
  435.     return 1;
  436.  
  437. }
  438. int loadcass(void)
  439. {
  440.  if (AslBase = OpenLibrary("asl.library", 37L))
  441.    {
  442.     if (fr = (struct FileRequester *)
  443.        AllocAslRequestTags(ASL_FileRequest,
  444.         ASL_Hail,       (ULONG)"Cassfile requester",
  445.         ASL_Height,     150,
  446.         ASL_Width,      220,
  447.         ASL_LeftEdge,   20,
  448.         ASL_TopEdge,    20,
  449.         ASL_OKText,     (ULONG)"OKAY",
  450.         ASL_CancelText, (ULONG)"not OK",
  451.         ASL_File,       (ULONG)"Cassfile",
  452.         ASL_Window,     win,
  453.         TAG_DONE))
  454.       {
  455.        if (AslRequest(fr, NULL))
  456.          {
  457.           strcpy(fname,fr->rf_Dir);
  458.           AddPart(fname,fr->rf_File,255);
  459.          }
  460.        FreeAslRequest(fr);
  461.       }
  462.     if(AslBase)CloseLibrary(AslBase);
  463.    }
  464.     if (!(fp = Open(fname,MODE_OLDFILE)))
  465.         return 0;
  466.  
  467.     CASSPTR=(UWORD)Read(fp,&rom+0x10000,65536L)-1;
  468.     Close(fp);
  469.     return 1;
  470.  
  471. }
  472.