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 / bdm-pd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-27  |  4.8 KB  |  171 lines

  1. /* bdm-pd.c - routines to talk to CPU16 or CPU32 target
  2.  * via public-domain interface hardware on IBM PC parallel printer port
  3.  * Copyright (C) 1992 by Scott Howard, all rights reserved
  4.  * Permission is hereby granted to freely copy and use this code
  5.  * or derivations thereof as long as no charge is made to anyone for its use
  6.  * 
  7.  * Adapted for Linux by D.Jeff Dionne, April 25 1995
  8.  */
  9.  
  10. #include    "bdm.h"
  11. #include    "bdmcalls.h"
  12. #include    "bdmerror.h"
  13. #include    "trgtstat.h"
  14.  
  15. #define    bdm_ctl         2               /* offset of control port from base */
  16. #define    step_out        8               /* set low to gate IFETCH onto BKPT */
  17. #define    dsi             4               /* data shift input - PC->MCU       */
  18. #define    rst_out         2               /* set high to force reset on MCU   */
  19. #define    reset           2               /* low when RESET asserted          */
  20. #define    dsclk           1               /* data shift clock/breakpoint pin  */
  21.  
  22. #define    bdm_stat    1        /* offset of status port from base     */
  23. #define    nc              0x80            /* not connected - low when unplugged  */
  24. #define    pwr_dwn         0x40            /* power down - low when Vcc failed    */
  25. #define    dso             0x20            /* data shift output - MCU->PC         */
  26. #define    freeze          8               /* FREEZE asserted when MCU stopped    */
  27.  
  28. #define    nop_cmd         0               /* null (NOP) command                  */
  29. #define    waitcnt         0xffff          /* no of loops to wait for response    */
  30.  
  31. extern void bdm_delay (unsigned);
  32. extern void bdm_error (int);
  33. static unsigned bdm_speed, bdm_port, old_ctl;
  34. extern unsigned go_cmd, CommandBitCount;
  35.  
  36. /* bdm_deinit is called before program quits 
  37.  * un-do any bad things which have been done to talk to target
  38.  */
  39.  
  40. void bdm_deinit (void)
  41. {
  42.     outportb (bdm_port + bdm_ctl, old_ctl);
  43. }
  44.  
  45. /* bdm_init initializes parallel port to talk to target */
  46.  
  47. void bdm_init (int port, int baud)
  48. {
  49.     RegsValid = 0;
  50.     old_ctl = inportb (bdm_port + bdm_ctl);
  51.     outportb (bdm_port+bdm_ctl,step_out);
  52.     bdm_port = port;
  53.     bdm_speed = baud;
  54. }
  55.  
  56. void ResetChip (void)
  57. {
  58.     RegsValid = 0;
  59.     outportb (bdm_port + bdm_ctl, rst_out + step_out);
  60.     bdm_delay (waitcnt);
  61.     outportb (bdm_port + bdm_ctl, step_out);
  62. }
  63.  
  64. void RestartChip (void)
  65. {
  66.     unsigned LoopCount;
  67.  
  68.     RegsValid = 0;
  69.     outportb (bdm_port + bdm_ctl, rst_out  | dsclk);
  70.     bdm_delay (waitcnt);
  71.     outportb (bdm_port + bdm_ctl, dsclk);
  72.     bdm_delay (waitcnt);
  73.     outportb (bdm_port + bdm_ctl, step_out | dsclk);
  74.     for (LoopCount = 0xffff; LoopCount; LoopCount--)
  75.         if (inportb (bdm_port + bdm_stat) & freeze) break;
  76.     if (!LoopCount) bdm_error (BDM_FAULT_RESPONSE);
  77. }
  78.  
  79. int StopChip (void)
  80. {
  81.     unsigned ctr;
  82.     char frozen = 0;
  83.  
  84.     RegsValid = 0;
  85.     if (inportb (bdm_port + bdm_stat) & freeze) return frozen;
  86.     frozen = 1;
  87.     outportb (bdm_port + bdm_ctl, dsclk + step_out);
  88.     for (ctr = waitcnt; ctr; ctr--)
  89.     {
  90.         if (inportb (bdm_port + bdm_stat) & freeze) break;
  91.         bdm_delay (1);
  92.     }
  93.     if (!ctr) bdm_error (BDM_FAULT_RESPONSE);
  94.     return frozen;
  95. }
  96.  
  97. /* bdm_clk sends <value> to MCU for <parameter> bits, returns MCU response */
  98.  
  99. LONG bdm_clk (WORD value, int count)
  100. {
  101.     LONG ShiftRegister = ((LONG) value) << (32 - count);
  102.  
  103.     unsigned char DataOut;
  104.  
  105.     unsigned stat = GetStatus ();
  106.  
  107.     if (stat & TARGETRESET)
  108.         bdm_error (BDM_FAULT_RESET);
  109.     if (stat & TARGETNC)
  110.         bdm_error (BDM_FAULT_CABLE);
  111.     if (stat & TARGETPOWER)
  112.         bdm_error (BDM_FAULT_POWER);
  113.  
  114.     while (count--)
  115.     {
  116.         DataOut = (ShiftRegister & 0x80000000) ? dsi : 0;
  117.         ShiftRegister <<= 1;
  118.         if (!(inportb (bdm_port + bdm_stat) & dso))
  119.             ShiftRegister |= 1;
  120.         outportb (bdm_port + bdm_ctl, DataOut | step_out | dsclk);
  121.         bdm_delay (bdm_speed + 1);
  122.         outportb (bdm_port + bdm_ctl, DataOut | step_out);
  123.         bdm_delay ((bdm_speed >> 1) + 1);
  124.     }
  125.  
  126.     return ShiftRegister;
  127. }
  128.  
  129. /* StepChip sends GO command word, then triggers breakpoint on first fetch        */
  130.  
  131. void StepChip (void)
  132. {
  133. #define    DataOut    (go_cmd & 1 ? dsi : 0)
  134.     unsigned stat = GetStatus ();
  135.  
  136.     RegsValid = 0;
  137.     if (stat & TARGETRESET)
  138.         bdm_error (BDM_FAULT_RESET);
  139.     if (stat & TARGETNC)
  140.         bdm_error (BDM_FAULT_CABLE);
  141.     if (stat & TARGETPOWER)
  142.         bdm_error (BDM_FAULT_POWER);
  143.     bdm_clk (go_cmd >> 1, CommandBitCount - 1);
  144.     outportb (bdm_port + bdm_ctl, dsclk | step_out | DataOut);
  145.     bdm_delay (bdm_speed + 1);
  146.     disable ();
  147.     outportb (bdm_port + bdm_ctl, dsclk | DataOut);
  148.     bdm_delay (1);
  149.     outportb (bdm_port + bdm_ctl, DataOut);
  150.     enable ();
  151. }
  152.  
  153. /* GetStatus returns status of MCU                                              */
  154.  
  155. unsigned GetStatus (void)
  156. {
  157.     BYTE temp = inportb (bdm_port + bdm_stat);
  158.  
  159.     if (!(temp & nc)) return TARGETNC;
  160.     if (!(temp & pwr_dwn)) return TARGETPOWER;
  161.     return     (temp & freeze ? TARGETSTOPPED: 0)
  162.         | (inportb (bdm_port + bdm_ctl) & reset ? TARGETRESET : 0);
  163. }
  164.  
  165. /* GetStatusMask returns mask showing which stat bits are valid */
  166.  
  167. unsigned GetStatusMask (void)
  168. {
  169.     return TARGETRESET | TARGETSTOPPED | TARGETPOWER | TARGETNC;
  170. }
  171.