home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / emulate / pcdump.lha / pcdump.c < prev    next >
C/C++ Source or Header  |  1989-08-26  |  8KB  |  211 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <exec/types.h>
  6. #include <janus/janus.h>
  7. #include <proto/exec.h>
  8.  
  9. /********************************************************************/
  10. /*               PC (As in Bridgecard) Memory Dumper                */
  11. /*                       Fully Public Domain                        */
  12. /*                        August 27th, 1989                         */
  13. /*                     (Written in Lattice C)                       */
  14. /*                                                                  */
  15. /* This program is a simple experiment in trying to use the new     */
  16. /* Janus 2.x software to read/write the memory in a PC Bridgecard.  */
  17. /* The Janus kernel has the ability to do so, but such services are */
  18. /* not provided at the CallService() level, so I have to go lower.  */
  19. /* I do not know the level of official support for this method nor  */
  20. /* do I know what collisions I may cause in a different environment.*/
  21. /* It works for me.  I hope to further expand the functionality and */
  22. /* develop routines until I can do all of the following:            */
  23. /*                                                                  */
  24. /*    a) Read/Write PC Memory - This is useful for being able to    */
  25. /*       determine and alter various obscure PCDOS settings.        */
  26. /*                                                                  */
  27. /*    b) Read/Write PC I/O Ports - This could be useful for low-    */
  28. /*       bandwidth I/O, such as the parallel port.                  */
  29. /*                                                                  */
  30. /*    c) Invoke A BIOS Interrupt - I want this in order to use a    */
  31. /*       public-domain serial interface module called FOSSIL.  It   */
  32. /*       provides interrupt-driven serial I/O in a standard fashion.*/
  33. /*       By being able to invoke an interrupt, I can stuff data     */
  34. /*       shared Janus memory and invoke the FOSSIL driver, all in   */
  35. /*       background of PCDOS.  I also want to use it to provide low */
  36. /*       level disk I/O (INT13-style) to additional DOS drives so   */
  37. /*       -someone- can write an Amiga filesystem that runs on 1.44  */
  38. /*       floppies plugged into the PC side of things.  Properly     */
  39. /*       done, the two machines should be able to use each other    */
  40. /*       drives without the user noticing very much.                */
  41. /*                                                                  */
  42. /* All three of these basic services are available in the Janus     */
  43. /* kernel now.  They require no additional code on the PC side.     */
  44. /*                                                                  */
  45. /*                     by Jeff Rush (jrush on BIX)                  */
  46. /*                        Sysop of Rising Star BBS (214) 231-1372   */
  47. /*                                                                  */
  48. /* P.S. I have not attempted to space-reduce this or to error-proof */
  49. /* it.  Its sole purpose in life is as a conceptual demo.           */
  50. /*                                                                  */
  51. /********************************************************************/
  52.  
  53. /************************/
  54. /* Constants and Macros */
  55. /************************/
  56. #define TRUE  1
  57. #define FALSE 0
  58.  
  59. #define BYTE_ALIGNMENT 0
  60. #define WORD_ALIGNMENT 1
  61.  
  62. /***************************/
  63. /* Structures and Typedefs */
  64. /***************************/
  65.  
  66. /**************/
  67. /* Prototypes */
  68. /**************/
  69. int  GetPCdata(int segment, int offset, char *buffer, int buflen, int alignment);
  70. void hexdump(char *buffer, int buflen);
  71.  
  72. /********************/
  73. /* Global Variables */
  74. /********************/
  75. struct JanusBase *JanusBase = NULL;
  76.  
  77. char           buffer[256];
  78. unsigned short wbuffer[32];
  79.  
  80. /********************************************************************/
  81. /*                                                                  */
  82. /********************************************************************/
  83. void
  84. main(int argc, char *argv[])
  85. {
  86.     int i, seg, off;
  87.  
  88.     /********************************/
  89.     /* Perform Basic Initialization */
  90.     /********************************/
  91.     if (argc < 2) {
  92.         printf("Usage: PCDUMP seg:off\n");
  93.         exit(10);
  94.     }
  95.  
  96.     if ((JanusBase = (struct JanusBase *) OpenLibrary(JANUSNAME, 0)) == NULL) {
  97.         printf("Unable to Open Janus.Library\n");
  98.         exit(10);
  99.     }
  100.  
  101.     /*******************/
  102.     /* Parse Arguments */
  103.     /*******************/
  104.     sscanf(argv[1], "%x:%x", &seg, &off);
  105.     printf("Dumping PC Memory %04X:%04X\n", seg, off);
  106.  
  107.     /****************************************/
  108.     /* Obtain Data from PC Side and Dump It */
  109.     /****************************************/
  110.     if (GetPCdata(seg, off, buffer, sizeof(buffer), BYTE_ALIGNMENT))
  111.         hexdump(buffer, sizeof(buffer));
  112.     else
  113.         printf("Unable to Obtain Requested Data.\n");
  114.  
  115.     /************************************/
  116.     /* Obtain Data and Dump It as Words */
  117.     /************************************/
  118.     if (GetPCdata(seg, off, (char *)wbuffer, sizeof(wbuffer), WORD_ALIGNMENT)) {
  119.         for(i=0; i<sizeof(wbuffer); i++)
  120.             printf("  %04X\n", wbuffer[i]);
  121.     } else
  122.         printf("Unable to Obtain Requested Data.\n");
  123.  
  124.     CloseLibrary((struct Library *)JanusBase);
  125. }
  126. /********************************************************************/
  127. /*                                                                  */
  128. /********************************************************************/
  129. int
  130. GetPCdata(int segment, int offset, char *buffer, int buflen, int alignment)
  131. {
  132.     struct SetupSig     *p;
  133.     struct MemReadWrite *q;
  134.     char                *r;
  135.     long                 signal;
  136.     int                  xferamt;
  137.     int                  retcode = FALSE;
  138.  
  139.     xferamt = (buflen > 512) ? 512 : buflen;
  140.  
  141.     if ((signal = AllocSignal(-1)) != -1) { /* Allocate a Signal to Use */
  142.         p = SetupJanusSig(JSERV_READPC, (USHORT)signal,
  143.                 sizeof(struct MemReadWrite), MEMF_PARAMETER | MEM_WORDACCESS);
  144.         if (p != NULL) {
  145.             q    = (struct MemReadWrite *)JanusOffsetToMem(GetParamOffset(JSERV_READPC),
  146.                                                            MEMF_PARAMETER | MEM_WORDACCESS);
  147.  
  148.             r = (char *)AllocJanusMem(xferamt, MEMF_BUFFER |
  149.                     ((alignment == BYTE_ALIGNMENT) ? MEM_BYTEACCESS : MEM_WORDACCESS));
  150.             if (r != NULL) {
  151.  
  152.                 while (buflen) {
  153.                     q->mrw_Command = MRWC_READ;
  154.                     q->mrw_Count   = (buflen < xferamt) ? buflen : xferamt;
  155.                     q->mrw_Address = (long)offset << 16 | (long)segment;
  156.                     q->mrw_Buffer  = JanusMemToOffset((APTR)r);
  157.  
  158.                     SendJanusInt(JSERV_READPC);
  159.                     do {
  160.                         Wait(1L << signal);
  161.                     } while (CheckJanusInt(JSERV_READPC) != 0x00);
  162.  
  163.                     memcpy(buffer, r, xferamt);
  164.  
  165.                     buffer += xferamt;
  166.                     buflen -= xferamt;
  167.                     offset += xferamt;
  168.                 }
  169.                 FreeJanusMem((APTR)r, xferamt);
  170.                 retcode = TRUE;
  171.             }
  172.             CleanupJanusSig(p);
  173.         }
  174.         FreeSignal(signal);
  175.     }
  176.     return retcode;
  177. }
  178. /**********************************************************************/
  179. /*                                                                    */
  180. /**********************************************************************/
  181. void
  182. hexdump(char *buffer, int buflen)
  183. {
  184.     int i, j;
  185.     char text[17];
  186.  
  187.     strcpy(text, "                ");
  188.     for(i=0; i<buflen; i++) {
  189.         if (i % 16 == 0)
  190.             printf("  %04X:", i);
  191.  
  192.         printf(" %02X", buffer[i]);
  193.         text[i % 16] = isprint(buffer[i]) ? buffer[i] : '.';
  194.  
  195.         if (i % 16 == 15) {
  196.             printf(" %s\n", text);
  197.             strcpy(text, "                ");
  198.         }
  199.     }
  200.  
  201.     if (i % 16 != 0) {
  202.         for(j=0; j < (16 - (i % 16)); j++)
  203.             printf("   ");
  204.         printf(" %s\n", text);
  205.     }
  206. }
  207. /********************************************************************/
  208. /*                                                                  */
  209. /********************************************************************/
  210.  
  211.