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 / bdmcpu16.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-27  |  11.1 KB  |  475 lines

  1. /* bdmcpu16.c - routines to talk to CPU16 target system
  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.  */
  6.  
  7. #include    <stdio.h>
  8. #include    <stdarg.h>
  9. #include    "bdmcalls.h"
  10. #include    "bdm.h"
  11. #include    "trgtstat.h"
  12. #include    "regs-16.h"
  13. #include    "bdmerror.h"
  14.  
  15. /* BDM commands for CPU16 */
  16.  
  17. #define         BDM_RREGM       0x1780    /* Read multiple CPU registers */
  18. #define         BDM_WREGM       0x1781    /* Write multiple CPU registers */
  19. #define        BDM_RDMAC    0x178A    /* Read MAC registers */
  20. #define        BDM_WRMAC    0x178B    /* Write MAC registers */
  21. #define         BDM_RPCSP       0x1782    /* Read PC and SP registers */
  22. #define         BDM_WPCSP       0x1783    /* Write PC and SP registers */
  23. #define         BDM_READ        0x1784    /* Read Data Memory (Byte/Word/Long) */
  24. #define         BDM_WRITE       0x1785    /* Write Data Memory (Byte/Word/Long) */
  25. #define        BDM_RPMEM    0x1786    /* Read Program Memory (Word only) */
  26. #define        BDM_WPMEM    0x1787    /* Write Program Memory (Word only) */
  27. #define         BDM_GO          0x1788    /* Execute starting at address in PC */
  28. #define         BDM_NOP         0x1789    /* No Operation */
  29. #define        BDM_RESYNC         0x0000    /* Used when we lose bit sync with target */
  30. #define        BDM_REGMASK    0x7f
  31.  
  32. /* these codes are added to BDM_READ/BDM_WRITE to specify size */
  33.  
  34. #define         BDM_BYTESIZE    0x0000
  35. #define         BDM_WORDSIZE    0x4000
  36. #define         BDM_LONGSIZE    0x8000
  37.  
  38. /* define status codes returned from CPU16 */
  39.  
  40. #define         BDM_NOTREADY    0x10000
  41. #define         BDM_BERR        0x10001
  42. #define         BDM_ILLEGAL     0x1FFFF
  43. #define         BDM_CMDCMPLTE   0x0FFFF
  44.  
  45. static void bdm_clrerror (void);    /* routine to re-sync with target */
  46. static BYTE fc = SupervisorData;    /* function code used for mem accesses */
  47. static char last_rw;            /* last mem access was read (1) or write (0) */
  48. char RegsValid = 0;            /* record which RAM images of target registers are OK */
  49. static LONG last_addr;            /* address of last mem access */
  50. WORD go_cmd = BDM_GO,            /* value of 'GO' command (for Restart ()/Go () ) */
  51.     CommandBitCount = 17;        /* number of bits in a BDM command */
  52. static WORD RegValues [REG_MAX];    /* our RAM image of target registers */
  53. static Initted = 0;            /* 1 = Init () was previously called */
  54.  
  55.         /* masks for RegsValid */
  56.  
  57. #define    PCSPRegsValid    1
  58. #define    DSPRegsValid    2
  59. #define    CPURegsValid    4
  60.  
  61. /* set_fc is an internal function which saves current function code
  62.  * original function code is restored by restore_fc () below
  63.  */
  64.  
  65. int set_fc (void)
  66. {
  67.     return 0;
  68. }
  69.  
  70. int restore_fc (void)
  71. {
  72.     return 0;
  73. }
  74.  
  75. /* ValidPorts returns unsigned integer where each bit position
  76.  * indicates an installed LPT port (Bit 0 = LPT1, bit 1 = LPT2, etc)
  77.  * This will have to go and mess around in /proc/ioports to find out...
  78.  * For now we just assume a single one, /dev/lp1
  79.  */
  80.  
  81. unsigned ValidPorts (void)
  82. {
  83.     return 1;
  84. }
  85.  
  86. /* SetFC is called by user program to select desired memory space for
  87.  * subsequent accesses to target memory
  88.  * use the function code as defined by the CPU16/CPU32/68k processors
  89.  * eg. 1 = user data, 2 = user code, 5 = supervisor data, etc
  90.  * on CPU16 only 5 (supervisor data) and 6 (supervisor code) are valid
  91.  */
  92.  
  93. void SetFC (unsigned NewFC)
  94. {
  95.     if (NewFC == SupervisorData || NewFC == SupervisorCode)
  96.         fc = NewFC;
  97. }
  98.  
  99. /* DeInit must be called before program quits or shells to DOS
  100.  * un-do any bad things which have been done to the PC's LPT hardware
  101.  * in order to talk to target
  102.  */
  103.  
  104. void DeInit (void)
  105. {
  106.     if (Initted) bdm_deinit ();
  107.     Initted = 0;
  108. }
  109.  
  110. /* Init initializes parallel port to talk to target */
  111.  
  112. int Init (unsigned port, unsigned baud)
  113. {
  114.     DeInit ();
  115.     if (!(ValidPorts () & (1 << (port - 1)))) return -1;
  116.  
  117.     bdm_init (0x378, baud);
  118.     GetStatus ();
  119.     Initted = 1;
  120.     return 0;
  121. }
  122.  
  123. /* bdm_error handles call to DriverError if error detected by bdm routines */
  124.  
  125. void bdm_error (int type)
  126. {
  127.     if ((type > BDM_FAULT_BERR) || (type < 0)) type = 0;
  128.     DriverError (type, last_rw, last_addr);
  129. }
  130.  
  131. static void bdm_clrerror (void)
  132. {
  133.     int count;
  134.     long response;
  135.  
  136.     bdm_clk (BDM_RESYNC, CommandBitCount);
  137.     bdm_clk (BDM_RESYNC, CommandBitCount);
  138.     bdm_clk (BDM_RESYNC, CommandBitCount);
  139.     bdm_clk (BDM_RESYNC, CommandBitCount);
  140.     bdm_clk (BDM_RESYNC, CommandBitCount);
  141.     while (!bdm_clk (0, 1)) ;
  142.     while (bdm_clk (0, 1)) ;
  143.     while (!bdm_clk (0, 1)) ;
  144.     while (bdm_clk (0, 1)) ;
  145.     bdm_clk (BDM_NOP, CommandBitCount - 2);
  146. }
  147.  
  148. static LONG bdm_read (LONG addr, int pcount, unsigned ResponseSize,
  149.     WORD *Where, WORD cmd, ...)
  150. {
  151.     va_list arg;
  152.     WORD FirstArg;
  153.     int err,count;
  154.     LONG response,result;
  155.  
  156.     last_addr = addr;
  157.     last_rw = 1;
  158.     if (!Initted) bdm_error (BDM_FAULT_PORT);
  159.     for (err = 3; err; err--)
  160.     {
  161.         result = 0;
  162.         count = pcount;
  163.         va_start (arg,cmd);
  164.         response = bdm_clk (cmd, CommandBitCount);
  165.         while (count--)
  166.             if ((response = bdm_clk (va_arg(arg,WORD), CommandBitCount)) > BDM_NOTREADY)
  167.             {
  168.                 if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  169.                 continue;
  170.             }
  171.         for (count = ResponseSize; count; count--)
  172.         {
  173.             while ((response = bdm_clk (BDM_NOP, CommandBitCount)) == BDM_NOTREADY) ;
  174.             if (response < BDM_NOTREADY)
  175.             {
  176.                 result <<= 16;
  177.                 result |= response;
  178.                 if (Where) *Where++ = response;
  179.             }
  180.             else
  181.             {
  182.                 if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  183.                 break;
  184.             }
  185.         }
  186.         if (response > BDM_NOTREADY)
  187.         {
  188.             if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  189.             bdm_clrerror ();
  190.             continue;
  191.         }
  192.         va_end (arg);
  193.         break;
  194.     }
  195.     if (!err) bdm_error (BDM_FAULT_UNKNOWN);
  196.     response = bdm_clk (BDM_NOP, CommandBitCount);
  197.     if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  198.     return result;
  199. }
  200.  
  201. static void bdm_write (LONG addr, int pcount, WORD cmd, ...)
  202. {
  203.     va_list arg;
  204.     int err,count;
  205.     LONG response,result;
  206.  
  207.     last_addr = addr;
  208.     last_rw = 0;
  209.     if (!Initted) bdm_error (BDM_FAULT_PORT);
  210.     for (err = 3; err; err--)
  211.     {
  212.         count = pcount;
  213.         va_start (arg,cmd);
  214.         response = bdm_clk (cmd, CommandBitCount);
  215.         while (count--)
  216.             if ((response = bdm_clk (va_arg(arg,WORD), CommandBitCount)) > BDM_NOTREADY)
  217.             {
  218.                 if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  219.                 bdm_clrerror ();
  220.                 continue;
  221.             }
  222.         while ((response = bdm_clk (BDM_NOP, CommandBitCount)) == BDM_NOTREADY) ;
  223.         if (response == BDM_CMDCMPLTE) break;
  224.         else if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  225.  
  226.     }
  227.     va_end (arg);
  228.     if (!err) bdm_error (BDM_FAULT_UNKNOWN);
  229.     response = bdm_clk (BDM_NOP, CommandBitCount);
  230.     if (response == BDM_BERR) bdm_error (BDM_FAULT_BERR);
  231. }
  232.  
  233. void RunChip (LONG where)
  234. {
  235.     StopChip ();
  236.     if (where) PutReg (REG_PC, where);
  237.     bdm_clk (BDM_GO, CommandBitCount);
  238.     RegsValid = 0;
  239. }
  240.  
  241. LONG GetByte (LONG x)
  242. {
  243.     int frozen,c;
  244.     LONG result;
  245.  
  246.     frozen = StopChip ();
  247.     if (fc == SupervisorData)
  248.         result = bdm_read (x, 2, 1, 0,
  249.             BDM_READ, (WORD) (x >> 16), (WORD) x);
  250.     else
  251.     {
  252.         result = bdm_read (x, 2, 1, 0,
  253.             BDM_RPMEM, (WORD) (x >> 16), (WORD) x & 0xfffe);
  254.         if (x & 1) result &= 0xff;
  255.         else result >>= 8;
  256.     }
  257.     if (frozen) RunChip (0L);
  258.     return result;
  259. }
  260.  
  261. void PutByte (LONG x, BYTE y)
  262. {
  263.     int frozen;
  264.     unsigned stuff;
  265.  
  266.     frozen = StopChip ();
  267.     if (fc == SupervisorData)
  268.         bdm_write (x, 3, BDM_WRITE,
  269.             (WORD) (x >> 16), (WORD) x, (WORD) y);
  270.     else
  271.     {
  272.         if (x & 1) stuff = y;
  273.         else
  274.         {
  275.             stuff = bdm_read (x, 2, 1, 0, BDM_RPMEM,
  276.                 (WORD) (x >> 16), (WORD) (x & 0xfffe));
  277.             stuff &= (x & 1) ? 0xff00 : 0xff;
  278.             stuff |= (x & 1) ? y : (WORD) y << 8;
  279.             bdm_write (x, 3, BDM_WPMEM,
  280.                 (WORD) (x >> 16), (WORD) (x & 0xfffe), (WORD) stuff);
  281.         }
  282.     }
  283.     if (frozen) RunChip (0L);
  284. }
  285.  
  286. void FillByte (LONG x, BYTE y)
  287. {
  288.     PutByte (x, y);
  289. }
  290.  
  291. LONG DumpByte (LONG x)
  292. {
  293.     return GetByte (x);
  294. }
  295.  
  296. LONG GetWord (LONG x)
  297. {
  298.     int frozen,c,c1;
  299.     LONG result;
  300.  
  301.     frozen = StopChip ();
  302.     if (fc == SupervisorData)
  303.         result = bdm_read (x, 2, 1, 0,
  304.         BDM_READ, BDM_WORDSIZE | (WORD) (x >> 16), (WORD) x);
  305.     else if (x & 1)
  306.         result = ((WORD) GetByte (x) << 8) | GetByte (x + 1);
  307.         else result = bdm_read (x, 2, 1, 0, BDM_RPMEM,
  308.             (WORD) (x >> 16), (WORD) x);
  309.     if (frozen) RunChip (0L);
  310.     return result;
  311. }
  312.  
  313. void PutWord (LONG x, WORD y)
  314. {
  315.     int frozen;
  316.  
  317.     frozen = StopChip ();
  318.     if (fc == SupervisorData)
  319.         bdm_write (x, 3, BDM_WRITE, BDM_WORDSIZE | (WORD) (x >> 16),
  320.             (WORD) x, (WORD) y);
  321.     else if (x & 1)
  322.     {
  323.         PutByte (x, y >> 8);
  324.         PutByte (x + 1, y);
  325.     }
  326.     else bdm_write (x, 3, BDM_WPMEM, (WORD) (x >> 16), (WORD) x, (WORD) y);
  327.     if (frozen) RunChip (0L);
  328. }
  329.  
  330. void FillWord (LONG x, WORD y)
  331. {
  332.     PutWord (x, y);
  333. }
  334.  
  335. LONG DumpWord (LONG x)
  336. {
  337.     return GetWord (x);
  338. }
  339.  
  340. LONG GetLong (LONG x)
  341. {
  342.     int frozen;
  343.     LONG result;
  344.  
  345.     frozen = StopChip ();
  346.     if (fc == SupervisorData)
  347.         result = bdm_read (x, 2, 2, 0, BDM_READ,
  348.             BDM_LONGSIZE | (WORD) (x >> 16), (WORD) x);
  349.     else if (x & 1)
  350.         result = (LONG) GetByte (x) << 24
  351.             | (LONG) GetWord (x + 1) << 8
  352.             | GetByte (x + 3);
  353.     else result = (LONG) GetWord (x) << 16 | GetWord (x + 2);
  354.     if (frozen) RunChip (0L);
  355.     return result;
  356. }
  357.  
  358. void PutLong (LONG x, LONG y)
  359. {
  360.     int frozen;
  361.  
  362.     frozen = StopChip ();
  363.     if (fc == SupervisorData)
  364.         bdm_write (x, 3, BDM_WRITE, BDM_LONGSIZE | (WORD) (x >> 16),
  365.             (WORD) x, (WORD) y);
  366.     else if (x & 1)
  367.     {
  368.         PutByte (x, y >> 24);
  369.         PutWord (x + 1, y >> 8);
  370.         PutByte (x + 3, y);
  371.     }
  372.     else
  373.     {
  374.         PutWord (x, y >> 16);
  375.         PutWord (x + 2, y);
  376.     }
  377.     if (frozen) RunChip (0L);
  378. }
  379.  
  380. void FillLong (LONG x, LONG y)
  381. {
  382.     PutLong (x, y);
  383. }
  384.  
  385. LONG DumpLong (LONG x)
  386. {
  387.     return GetLong (x);
  388. }
  389.  
  390. static void ReadCPURegisters (void)
  391. {
  392.     bdm_read (REG_MINCPU, 1, REG_MAXCPU - REG_MINCPU + 1,
  393.         &RegValues [REG_MINCPU], BDM_RREGM, (WORD) BDM_REGMASK);
  394.     RegsValid |= CPURegsValid;
  395. }
  396.  
  397. static void ReadDSPRegisters (void)
  398. {
  399.     bdm_read (REG_MINDSP, 0, REG_MAXDSP - REG_MINDSP + 1,
  400.         &RegValues [REG_MINDSP], BDM_RDMAC);
  401.     RegsValid |= DSPRegsValid;
  402. }
  403.  
  404. static void ReadPCSPRegisters (void)
  405. {
  406.     bdm_read (REG_MINPCSP, 0, REG_MAXPCSP - REG_MINPCSP + 1,
  407.         &RegValues [REG_MINPCSP], BDM_RPCSP);
  408.     RegsValid |= PCSPRegsValid;
  409. }
  410.  
  411. static void WriteDSPRegisters (void)
  412. {
  413.     bdm_write (0, REG_MAXDSP - REG_MINDSP + 1, BDM_WRMAC,
  414.         RegValues [REG_H], RegValues [REG_I], RegValues [REG_AM0],
  415.         RegValues [REG_AM1],RegValues [REG_AM2],RegValues [REG_XMYM]);
  416. }
  417.  
  418. static void WritePCSPRegisters (void)
  419. {
  420.     bdm_write (0, REG_MAXPCSP - REG_MINPCSP + 1, BDM_WPCSP,
  421.         RegValues [REG_PK],RegValues [REG_PC],
  422.         RegValues [REG_SK],RegValues [REG_SP]);
  423. }
  424.  
  425. LONG GetReg (unsigned which)
  426. {
  427.     int frozen;
  428.     LONG result;
  429.  
  430.     frozen = StopChip ();
  431.  
  432.     if (which <= REG_MAXCPU)
  433.     {
  434.         if (!(RegsValid & CPURegsValid)) ReadCPURegisters ();
  435.     }
  436.     else if (which >= REG_MINDSP && which <= REG_MAXDSP)
  437.     {
  438.         if (!(RegsValid & DSPRegsValid)) ReadDSPRegisters ();
  439.     }
  440.     else if (which >= REG_MINPCSP && which <= REG_MAXPCSP)
  441.     {
  442.         if (!(RegsValid & PCSPRegsValid)) ReadPCSPRegisters ();
  443.     }
  444.     result = RegValues [which];
  445.     if (frozen) RunChip (0L);
  446.     return result;
  447. }
  448.  
  449. void PutReg (unsigned which, LONG data)
  450. {
  451.     int frozen = StopChip ();
  452.  
  453.     if (which <= REG_MAXCPU)
  454.     {
  455.         RegValues [which] = data;
  456.         bdm_write (0, 2, BDM_WREGM, (WORD) 1 << which, (WORD) data);
  457.     }
  458.     else if (which >= REG_MINDSP && which <= REG_MAXDSP)
  459.     {
  460.         if (!(RegsValid & DSPRegsValid)) ReadDSPRegisters ();
  461.         RegValues [which] = data;
  462.         WriteDSPRegisters ();
  463.     }
  464.     else if (which >= REG_MINPCSP && which <= REG_MAXPCSP)
  465.     {
  466.         if (!(RegsValid & PCSPRegsValid)) ReadPCSPRegisters ();
  467.         RegValues [which] = data;
  468.         WritePCSPRegisters ();
  469.     }
  470.     if (frozen) RunChip (0L);
  471. }
  472.  
  473.  
  474. /* end of bdmcpu16.c */
  475.