home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / debugger / bdm-linu.0 / bdm-linu / bdm-linux / test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-27  |  5.2 KB  |  201 lines

  1. /* test.c - test background mode drivers
  2.  * Copyright (C) 1992 by Scott Howard, all rights reserved
  3.  * Permission is hereby granted to freely copy and use this code or derivations thereof
  4.  * as long as no charge is made to anyone for its use
  5.  * this program is a very simple monitor program which will allow the user
  6.  * examine and change memory and registers, start/stop/reset/single step, etc.
  7.  * Functions which are common to both CPU32 and CPU16 targets are in this file
  8.  * test16.c and test32.c contain the code specific to the target
  9.  */
  10.  
  11. #include    <stdio.h>
  12.  
  13. #include    <ctype.h>
  14. #include    <string.h>
  15. #include    <setjmp.h>
  16. #include    "bdmcalls.h"
  17. #include    "trgtstat.h"
  18. #include    "bdmerror.h"
  19. #include    "bdm-util.h"
  20. #include    "textio.h"
  21.  
  22. #define        MaxBuffer    128
  23.  
  24. extern void DumpRegisters (void);
  25. extern char *TargetName;
  26. int GetCommandString (char *where, int count);
  27. char exiting [] = "Exiting \n";
  28. jmp_buf Saviour;
  29.  
  30. /* DriverError is a function called by background mode drivers when error detected
  31.  * this version simply prints an error corresponding to the error code and exits
  32.  */
  33.  
  34. static char *ErrorStrings [] =
  35. {
  36. NEWLINE "Unknown Error Encountered - Check Communications Speed" NEWLINE ,
  37. NEWLINE "Power Failed on Target MCU" NEWLINE,
  38. NEWLINE "Cable Disconnected on Target MCU" NEWLINE,
  39. NEWLINE "No Response from Target MCU" NEWLINE,
  40. NEWLINE "Can't Clock Target MCU while in Reset" NEWLINE ,
  41. NEWLINE "Specified Parallel Port Not Available on this PC" NEWLINE
  42. };
  43.  
  44. int ErrorCode, ErrorRW;
  45. LONG ErrorAddress;
  46.  
  47. void DriverError (int ErrCode, int LastRW, LONG LastAddress)
  48. {
  49.     ErrorCode = ErrCode;
  50.     ErrorRW = LastRW;
  51.     ErrorAddress = LastAddress;
  52.     longjmp (Saviour, 1);
  53. }
  54. void ReportError (void)
  55. {
  56.     if (ErrorCode == BDM_FAULT_BERR)
  57.         printf (NEWLINE "Bus error %s address %lx" NEWLINE ,
  58.             ErrorRW ? "reading" : "writing",
  59.             ErrorAddress);
  60.     else puts (ErrorStrings [ErrorCode]);
  61. }
  62.  
  63. int main ()
  64. {
  65.     unsigned CharCount, Counter, Counter1, SR, GotOne = 0, Ports = ValidPorts ();
  66.     int Stopped, Speed = -1, SelectedPort = 0;
  67.     char *ptr, Buffer [MaxBuffer], QuitFlag = '\0';
  68.     LONG LongTemp, MemDump = 0;
  69.     BYTE ByteTemp;
  70.  
  71. /* clear screen here */
  72.     linux_io_init();
  73.  
  74.     printf ("Test Program for %s Background Mode Drivers" NEWLINE , TargetName);
  75.     printf ("Available LPT Ports: <");
  76.     for (SR = 1, Counter = 1; Counter <= 16; Counter++, SR <<= 1)
  77.     {
  78.         if (SR & Ports)
  79.         {
  80.             if (GotOne) putchar (',');
  81.             GotOne = 1;
  82.             printf ("%d", Counter);
  83.         }
  84.     }
  85.     if (!GotOne) puts ("none");
  86.     puts ( ">" NEWLINE );
  87.     if (!Ports)
  88.     {
  89.         printf ("I need at least one parallel printer port to function!" NEWLINE "%s", exiting);
  90.         exit (1);
  91.     }
  92.     for (;;)
  93.     {
  94.         puts ("Please select Printer Port:");
  95.         CharCount = GetCommandString (Buffer, MaxBuffer);
  96.         if (!CharCount || !sscanf (Buffer, "%d", &SelectedPort)) return 0;
  97.         do
  98.         {
  99.             puts ("Please select Clock Speed (0-100): ");
  100.             CharCount = GetCommandString (Buffer, MaxBuffer);
  101.             if (!CharCount || !sscanf (Buffer, "%d", &Speed)) return 0;
  102.         }
  103.         while (Speed < 0 || Speed > 100);
  104.         if (setjmp (Saviour)) ReportError ();
  105.         else if (Init (SelectedPort, Speed))
  106.             printf ("Can't initialize port %d at speed %d" NEWLINE,
  107.                 SelectedPort, Speed);
  108.         else break;
  109.     }
  110.     printf ("Port %d Initialized at Speed %d" NEWLINE , SelectedPort, Speed);
  111.     puts ("Resetting Target MCU to enable BDM" NEWLINE );
  112.     ResetChip ();
  113.     while (!QuitFlag)
  114.     {
  115.         if (setjmp (Saviour)) ReportError ();
  116.         puts ("Enter Command DHMRSQ('H' for Help):");
  117.         GetCommandString (Buffer, MaxBuffer);
  118.         for (ptr = Buffer ;isspace (*ptr); ptr++) ;
  119.         switch (toupper (*ptr))
  120.         {
  121.         case    'B':
  122.             RestartChip ();
  123.  
  124.         case    'D':
  125.             DumpRegisters ();
  126.             break;
  127.  
  128.         case    'H':
  129.             printf ("Help for TEST (%s Target)" NEWLINE
  130.                 "B: Begin Program Execution from Reset" NEWLINE
  131.                 "D: Dump Target MCU Registers" NEWLINE
  132.                 "H: Print This Help Summary" NEWLINE
  133.                 "L: Load S-Record File into Target" NEWLINE
  134.                 "M: Memory Hex/ASCII Display" NEWLINE
  135.                 "R: Hardware Reset Target MCU" NEWLINE
  136.                 "S: Single Step Target MCU" NEWLINE
  137.                 "Q: Quit" NEWLINE , TargetName);
  138.             break;
  139.  
  140.         case    'L':
  141.             printf ("S-Record File to Load: ");
  142.             if (!GetCommandString (Buffer, MaxBuffer)) break;
  143.             PrintEachRecord = ".";
  144.             do_load (0, Buffer);
  145.             break;
  146.  
  147.         case    'M':
  148.             printf ("Memory Dump start address in hex:");
  149.             GetCommandString (Buffer, MaxBuffer);
  150.             if (!sscanf (Buffer, "%lx", &LongTemp))
  151.                 LongTemp = MemDump;
  152.             Stopped = StopChip ();
  153.             set_fc ();
  154.             for (Counter = 1; Counter; Counter--)
  155.             {
  156.                 printf ("%08lX ", LongTemp);
  157.                 for (Counter1 = 16; Counter1; Counter1--)
  158.                     printf ("%02X ", (BYTE) GetByte (LongTemp++));
  159.                 LongTemp -= 16;
  160.                 putchar (' ');
  161.                 for (Counter1 = 16; Counter1; Counter1--)
  162.                 {
  163.                     ByteTemp = GetByte (LongTemp++);
  164.                     putchar (isprint (ByteTemp) ? ByteTemp : '.');
  165.                 }
  166.                 puts (NEWLINE);
  167.             }
  168.             MemDump = LongTemp;
  169.             restore_fc ();
  170.             if (Stopped) RunChip (0);
  171.             break;
  172.  
  173.         case    'R':
  174.             printf ("Resetting Target MCU" NEWLINE );
  175.             ResetChip ();
  176.             break;
  177.  
  178.         case    'S':
  179.             printf ("Single Step" NEWLINE );
  180.             StepChip ();
  181.             DumpRegisters ();
  182.             break;
  183.  
  184.         case    'Q':
  185.             QuitFlag = 1;
  186.         }
  187.     }
  188.     DeInit ();
  189.     puts (exiting);
  190.     return 0;
  191. }
  192.  
  193. /* GetCommandString returns keyboard entry in buffer, to max length specified */
  194.  
  195. int GetCommandString (char *where, int count)
  196. {
  197.     gets (where);
  198.     puts (NEWLINE);
  199.     return strlen(where);
  200. }
  201.