1 //========================================================================
5 // Helper functions for H8/300H stub
7 //========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //========================================================================
41 //#####DESCRIPTIONBEGIN####
43 // Author(s): Yoshinori Sato
44 // Contributors: Yoshinori Sato
47 // Description: Helper functions for H8/300H stub
50 //####DESCRIPTIONEND####
52 //========================================================================
56 #include <pkgconf/hal.h>
58 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
60 #include <cyg/hal/hal_stub.h>
61 #include <cyg/hal/hal_arch.h>
62 #include <cyg/hal/hal_intr.h>
63 #include <cyg/hal/hal_diag.h>
65 #ifdef CYGDBG_HAL_DEBUG_GDB_THREAD_SUPPORT
66 #include <cyg/hal/dbg-threads-api.h> // dbg_currthread_id
69 /*--------------------------------------------------------------------*/
70 /* Given a trap value TRAP, return the corresponding signal. */
72 int __computeSignal (unsigned int trap_number)
74 switch (trap_number) {
75 case CYGNUM_HAL_VECTOR_TRAP3:
82 /*--------------------------------------------------------------------*/
83 /* Return the trap number corresponding to the last-taken trap. */
85 int __get_trap_number (void)
87 extern int CYG_LABEL_NAME(_intvector);
88 // The vector is not not part of the GDB register set so get it
89 // directly from the save context.
90 return CYG_LABEL_NAME(_intvector);
93 /*--------------------------------------------------------------------*/
94 /* Set the currently-saved pc register value to PC. This also updates NPC
97 void set_pc (target_register_t pc)
99 put_register (PC, pc);
103 /*----------------------------------------------------------------------
104 * Single-step support.
108 unsigned short *addr;
112 static breakinfo InstBuffer = {(unsigned short *)-1,0};
114 /* Clear any single-step breakpoint(s) that may have been set. */
116 void __clear_single_step (void)
118 if ((long)InstBuffer.addr != -1L) {
119 *InstBuffer.addr = InstBuffer.inst;
120 InstBuffer.addr = (unsigned short *)-1;
124 /* calculate next pc */
126 enum jump_type{none,aabs,aind,ret,reg,relb,relw};
128 /* opcode decode table define
131 len: instruction length (<0 next table index)
132 jmp: jump operation mode */
135 unsigned char pattern;
139 } __attribute__((aligned(1),packed));
141 #define OPTABLE(ptn,msk,len,jmp) {ptn,msk,len,jmp}
143 const static struct optable optable_0[] = {
144 OPTABLE(0x00,0xff, 1,none), /* 0x00 */
145 OPTABLE(0x01,0xff,-1,none), /* 0x01 */
146 OPTABLE(0x02,0xfe, 1,none), /* 0x02-0x03 */
147 OPTABLE(0x04,0xee, 1,none), /* 0x04-0x05/0x14-0x15 */
148 OPTABLE(0x06,0xfe, 1,none), /* 0x06-0x07 */
149 OPTABLE(0x08,0xea, 1,none), /* 0x08-0x09/0x0c-0x0d/0x18-0x19/0x1c-0x1d */
150 OPTABLE(0x0a,0xee, 1,none), /* 0x0a-0x0b/0x1a-0x1b */
151 OPTABLE(0x0e,0xee, 1,none), /* 0x0e-0x0f/0x1e-0x1f */
152 OPTABLE(0x10,0xfc, 1,none), /* 0x10-0x13 */
153 OPTABLE(0x16,0xfe, 1,none), /* 0x16-0x17 */
154 OPTABLE(0x20,0xe0, 1,none), /* 0x20-0x3f */
155 OPTABLE(0x40,0xf0, 1,relb), /* 0x40-0x4f */
156 OPTABLE(0x50,0xfc, 1,none), /* 0x50-0x53 */
157 OPTABLE(0x54,0xfd, 1,ret ), /* 0x54/0x56 */
158 OPTABLE(0x55,0xff, 1,relb), /* 0x55 */
159 OPTABLE(0x57,0xff, 1,none), /* 0x57 */
160 OPTABLE(0x58,0xfb, 2,relw), /* 0x58/0x5c */
161 OPTABLE(0x59,0xfb, 1,reg ), /* 0x59/0x5b */
162 OPTABLE(0x5a,0xfb, 2,aabs), /* 0x5a/0x5e */
163 OPTABLE(0x5b,0xfb, 2,aind), /* 0x5b/0x5f */
164 OPTABLE(0x60,0xe8, 1,none), /* 0x60-0x67/0x70-0x77 */
165 OPTABLE(0x68,0xfa, 1,none), /* 0x68-0x69/0x6c-0x6d */
166 OPTABLE(0x6a,0xfe,-2,none), /* 0x6a-0x6b */
167 OPTABLE(0x6e,0xfe, 2,none), /* 0x6e-0x6f */
168 OPTABLE(0x78,0xff, 4,none), /* 0x78 */
169 OPTABLE(0x79,0xff, 2,none), /* 0x79 */
170 OPTABLE(0x7a,0xff, 3,none), /* 0x7a */
171 OPTABLE(0x7b,0xff, 2,none), /* 0x7b */
172 OPTABLE(0x7c,0xfc, 2,none), /* 0x7c-0x7f */
173 OPTABLE(0x80,0x80, 1,none), /* 0x80-0xff */
176 const static struct optable optable_1[] = {
177 OPTABLE(0x00,0xff,-3,none), /* 0x0100 */
178 OPTABLE(0x40,0xf0,-3,none), /* 0x0140-0x14f */
179 OPTABLE(0x80,0xf0, 1,none), /* 0x0180-0x018f */
180 OPTABLE(0xc0,0xc0, 2,none), /* 0x01c0-0x01ff */
183 const static struct optable optable_2[] = {
184 OPTABLE(0x00,0x20, 2,none), /* 0x6a0?/0x6a8?/0x6b0?/0x6b8? */
185 OPTABLE(0x20,0x20, 3,none), /* 0x6a2?/0x6aa?/0x6b2?/0x6ba? */
188 const static struct optable optable_3[] = {
189 OPTABLE(0x69,0xfb, 2,none), /* 0x010069/0x01006d/014069/0x01406d */
190 OPTABLE(0x6b,0xff,-4,none), /* 0x01006b/0x01406b */
191 OPTABLE(0x6f,0xff, 3,none), /* 0x01006f/0x01406f */
192 OPTABLE(0x78,0xff, 5,none), /* 0x010078/0x014078 */
195 const static struct optable optable_4[] = {
196 OPTABLE(0x00,0x78, 3,none), /* 0x0100690?/0x01006d0?/0140690/0x01406d0?/0x0100698?/0x01006d8?/0140698?/0x01406d8? */
197 OPTABLE(0x20,0x78, 4,none), /* 0x0100692?/0x01006d2?/0140692/0x01406d2?/0x010069a?/0x01006da?/014069a?/0x01406da? */
200 const static struct {
201 const struct optable *op;
204 {optable_0,sizeof(optable_0)/sizeof(struct optable)},
205 {optable_1,sizeof(optable_1)/sizeof(struct optable)},
206 {optable_2,sizeof(optable_2)/sizeof(struct optable)},
207 {optable_3,sizeof(optable_3)/sizeof(struct optable)},
208 {optable_4,sizeof(optable_4)/sizeof(struct optable)},
211 const static unsigned char condmask[] = {
212 0x00,0x40,0x01,0x04,0x02,0x08,0x10,0x20
215 static int isbranch(int reson)
217 unsigned char cond = get_register(CCR);
218 /* encode complex conditions */
222 __asm__("bld #3,%w0\n\t"
230 :"=&r"(cond):"g"(cond):"cc");
231 cond &= condmask[reson >> 1];
238 static unsigned short *getnextpc(unsigned short *pc)
240 const struct optable *op;
241 unsigned char *fetch_p;
247 op_len = optables[0].length;
248 fetch_p = (unsigned char *)pc;
251 if ((inst & op->mask) == op->pattern) {
252 if (op->length < 0) {
253 op = optables[-op->length].op;
254 op_len = optables[-op->length].length + 1;
259 return pc + op->length;
261 addr = *(unsigned long *)pc;
262 return (unsigned short *)(addr & 0x00ffffff);
265 return (unsigned short *)(*(unsigned long *)addr);
267 sp = (unsigned long *)get_register(SP);
268 return (unsigned short *)(*(sp+3) & 0x00ffffff);
270 addr = get_register((*pc >> 4) & 0x07);
271 return (unsigned short *)addr;
273 if ((inst = 0x55) || isbranch(inst & 0x0f))
274 (unsigned char *)pc += (signed char)(*fetch_p);
275 return pc+1; /* skip myself */
277 if ((inst = 0x5c) || isbranch((*fetch_p & 0xf0) >> 4))
278 (unsigned char *)pc += (signed short)(*(pc+1));
279 return pc+2; /* skip myself */
284 } while(--op_len > 0);
288 /* Set breakpoint(s) to simulate a single step from the current PC. */
290 void __single_step (void)
292 unsigned short *nextpc;
293 nextpc = getnextpc((unsigned short *)get_register(PC));
294 InstBuffer.addr = nextpc;
295 InstBuffer.inst = *nextpc;
296 *nextpc = HAL_BREAKINST;
299 void __install_breakpoints (void)
301 /* NOP since single-step HW exceptions are used instead of
305 void __clear_breakpoints (void)
311 /* If the breakpoint we hit is in the breakpoint() instruction, return a
314 externC void CYG_LABEL_NAME(breakinst)(void);
316 __is_breakpoint_function ()
318 return get_register (PC) == (target_register_t)&CYG_LABEL_NAME(breakinst);
322 /* Skip the current instruction. */
323 /* only TRAPA instruction */
325 void __skipinst (void)
327 put_register (PC, get_register(PC) + 2);
330 #endif // CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS