| |||
| Home > Working with the CLI > Source patching with macros > Patching example to emulate a serial port | |||
To emulate a serial port in your source code:
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 */
}
.
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)
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.