]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/hal/h8300/h8300h/v2_0/src/h8300h_stub.c
Initial revision
[karo-tx-redboot.git] / packages / hal / h8300 / h8300h / v2_0 / src / h8300h_stub.c
1 //========================================================================
2 //
3 //      h8300h_stub.c
4 //
5 //      Helper functions for H8/300H stub
6 //
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.
12 //
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.
16 //
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
20 // for more details.
21 //
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.
25 //
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.
32 //
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.
35 //
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####
42 //
43 // Author(s):     Yoshinori Sato
44 // Contributors:  Yoshinori Sato
45 // Date:          2002-05-03
46 // Purpose:       
47 // Description:   Helper functions for H8/300H stub
48 // Usage:         
49 //
50 //####DESCRIPTIONEND####
51 //
52 //========================================================================
53
54 #include <stddef.h>
55
56 #include <pkgconf/hal.h>
57
58 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
59
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>
64
65 #ifdef CYGDBG_HAL_DEBUG_GDB_THREAD_SUPPORT
66 #include <cyg/hal/dbg-threads-api.h>    // dbg_currthread_id
67 #endif
68
69 /*--------------------------------------------------------------------*/
70 /* Given a trap value TRAP, return the corresponding signal. */
71
72 int __computeSignal (unsigned int trap_number)
73 {
74     switch (trap_number) {
75     case CYGNUM_HAL_VECTOR_TRAP3:
76         return SIGTRAP;
77     default:
78         return SIGINT;
79     }
80 }
81
82 /*--------------------------------------------------------------------*/
83 /* Return the trap number corresponding to the last-taken trap. */
84
85 int __get_trap_number (void)
86 {
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);
91 }
92
93 /*--------------------------------------------------------------------*/
94 /* Set the currently-saved pc register value to PC. This also updates NPC
95    as needed. */
96
97 void set_pc (target_register_t pc)
98 {
99     put_register (PC, pc);
100 }
101
102
103 /*----------------------------------------------------------------------
104  * Single-step support. 
105  */
106
107 typedef struct {
108     unsigned short *addr;
109     unsigned short inst;
110 } breakinfo;
111
112 static breakinfo InstBuffer = {(unsigned short *)-1,0};
113
114 /* Clear any single-step breakpoint(s) that may have been set.  */
115
116 void __clear_single_step (void)
117 {
118     if ((long)InstBuffer.addr != -1L) {
119         *InstBuffer.addr = InstBuffer.inst;
120         InstBuffer.addr = (unsigned short *)-1;
121     }
122 }
123
124 /* calculate next pc */
125
126 enum jump_type{none,aabs,aind,ret,reg,relb,relw};
127
128 /* opcode decode table define
129    ptn: opcode pattern
130    msk: opcode bitmask
131    len: instruction length (<0 next table index)
132    jmp: jump operation mode */
133
134 struct optable {
135     unsigned char pattern;
136     unsigned char mask;
137     signed   char length;
138     char          type;
139 } __attribute__((aligned(1),packed));
140
141 #define OPTABLE(ptn,msk,len,jmp) {ptn,msk,len,jmp}
142
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 */
174 };
175
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 */
181 };
182
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? */
186 };
187
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 */
193 };
194
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? */
198 };
199
200 const static struct {
201     const struct optable *op;
202     int length;
203 } optables[] = {
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)},
209 };
210
211 const static unsigned char condmask[] = {
212     0x00,0x40,0x01,0x04,0x02,0x08,0x10,0x20
213 };
214
215 static int isbranch(int reson)
216 {
217     unsigned char cond = get_register(CCR);
218     /* encode complex conditions */
219     /* B4: N^V
220        B5: Z|(N^V)
221        B6: C|Z */
222     __asm__("bld #3,%w0\n\t"
223             "bxor #1,%w0\n\t"
224             "bst #4,%w0\n\t"
225             "bor #2,%w0\n\t"
226             "bst #5,%w0\n\t"
227             "bld #2,%w0\n\t"
228             "bor #0,%w0\n\t"
229             "bst #6,%w0\n\t"
230             :"=&r"(cond):"g"(cond):"cc");
231     cond &= condmask[reson >> 1];
232     if (!(reson & 1))
233         return cond == 0;
234     else
235         return cond != 0;
236 }
237
238 static unsigned short *getnextpc(unsigned short *pc)
239 {
240     const struct optable *op;
241     unsigned char *fetch_p;
242     unsigned char inst;
243     unsigned long addr;
244     unsigned long *sp;
245     int op_len;
246     op = optables[0].op;
247     op_len = optables[0].length;
248     fetch_p = (unsigned char *)pc;
249     inst = *fetch_p++;
250     do {
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;
255                 inst = *fetch_p++;
256             } else {
257                 switch (op->type) {
258                 case none:
259                     return pc + op->length;
260                 case aabs:
261                     addr = *(unsigned long *)pc;
262                     return (unsigned short *)(addr & 0x00ffffff);
263                 case aind:
264                     addr = *pc & 0xff;
265                     return (unsigned short *)(*(unsigned long *)addr);
266                 case ret:
267                     sp = (unsigned long *)get_register(SP);
268                     return (unsigned short *)(*(sp+3) & 0x00ffffff);
269                 case reg:
270                     addr = get_register((*pc >> 4) & 0x07);
271                     return (unsigned short *)addr;
272                 case relb:
273                     if ((inst = 0x55) || isbranch(inst & 0x0f))
274                         (unsigned char *)pc += (signed char)(*fetch_p);
275                     return pc+1; /* skip myself */
276                 case relw:
277                     if ((inst = 0x5c) || isbranch((*fetch_p & 0xf0) >> 4))
278                         (unsigned char *)pc += (signed short)(*(pc+1));
279                     return pc+2; /* skip myself */
280                 }
281             }
282         } else
283             op++;
284     } while(--op_len > 0);
285     return NULL;
286 }
287
288 /* Set breakpoint(s) to simulate a single step from the current PC.  */
289
290 void __single_step (void)
291 {
292     unsigned short *nextpc;
293     nextpc = getnextpc((unsigned short *)get_register(PC));
294     InstBuffer.addr = nextpc;
295     InstBuffer.inst = *nextpc;
296     *nextpc = HAL_BREAKINST;
297 }
298
299 void __install_breakpoints (void)
300 {
301     /* NOP since single-step HW exceptions are used instead of
302        breakpoints. */
303 }
304
305 void __clear_breakpoints (void)
306 {
307
308 }
309
310
311 /* If the breakpoint we hit is in the breakpoint() instruction, return a
312    non-zero value. */
313
314 externC void CYG_LABEL_NAME(breakinst)(void);
315 int
316 __is_breakpoint_function ()
317 {
318     return get_register (PC) == (target_register_t)&CYG_LABEL_NAME(breakinst);
319 }
320
321
322 /* Skip the current instruction. */
323 /* only TRAPA instruction */
324
325 void __skipinst (void)
326 {
327     put_register (PC, get_register(PC) + 2);
328 }
329
330 #endif // CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS