1.8.4. Patching example to emulate a serial port

To emulate a serial port in your source code:

  1. Define a macro that emulates a serial port:

    add unsigned long last_time;       /* create local symbol */
    define int ser_port(offset,base)   /* macro definition */
        int offset;                /* offset of device register */
        unsigned short *base;      /* base of port */
    {
        unsigned short value;
        if (offset == 0)
        {                           /* control register */
            if (last_time && ((@cycle - last_time) < 20))
            {
                error (0, "ser_port: access less than
                    allowed time: %d", @cycle - last_time);
                return (0);
            }
            last_time = @cycle;
            value = base[offset];  /* cache written value */
            base[offset] = 0;      /* reset */
            if (value == 0x20)
            {                      /* want to read */
                ...
            }
            ...
        }
        ...
        $SETREG @PC = #line_num$; /* reset PC to skip the patched lines */
    }
    .
    
  2. Start a debugging session and set a breakpoint on the source code to stop execution immediately before it accesses the serial port, for example at line 20 of module_name.c, and attach your macro to this breakpoint:

    BREAKINSTRUCTION \MODULE_NAME\#20 ;ser_port(0,&ser_port)
    
  3. Continue debugging using the newly-inserted serial port emulation.

As with the previous example, this is only a temporary patch so the source code must be edited and then recompiled. Be careful, however, when using such a patch in optimized code.

Copyright © 2002-2011 ARM. All rights reserved.ARM DUI 0175N
Non-ConfidentialID052111