blob: 6bd5a7ddc40c262767bb67719ac124d086306b14 [file] [log] [blame]
Björn Stenbergd42d78f2002-04-15 08:35:08 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19/* sh-stub.c -- debugging stub for the Hitachi-SH.
20
21 NOTE!! This code has to be compiled with optimization, otherwise the
22 function inlining which generates the exception handlers won't work.
23
24*/
25
26/* This is originally based on an m68k software stub written by Glenn
27 Engel at HP, but has changed quite a bit.
28
29 Modifications for the SH by Ben Lee and Steve Chamberlain
30
31 Even more modifications for GCC 3.0 and The Rockbox by Linus
32 Nielsen Feltzing
33*/
34
35/****************************************************************************
36
37 THIS SOFTWARE IS NOT COPYRIGHTED
38
39 HP offers the following for use in the public domain. HP makes no
40 warranty with regard to the software or it's performance and the
41 user accepts the software "AS IS" with all faults.
42
43 HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
44 TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
45 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46
47****************************************************************************/
48
49
50/* Remote communication protocol.
51
52 A debug packet whose contents are <data>
53 is encapsulated for transmission in the form:
54
55 $ <data> # CSUM1 CSUM2
56
57 <data> must be ASCII alphanumeric and cannot include characters
58 '$' or '#'. If <data> starts with two characters followed by
59 ':', then the existing stubs interpret this as a sequence number.
60
61 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
62 checksum of <data>, the most significant nibble is sent first.
63 the hex digits 0-9,a-f are used.
64
65 Receiver responds with:
66
67 + - if CSUM is correct and ready for next packet
68 - - if CSUM is incorrect
69
70 <data> is as follows:
71 All values are encoded in ascii hex digits.
72
73 Request Packet
74
75 read registers g
76 reply XX....X Each byte of register data
77 is described by two hex digits.
78 Registers are in the internal order
79 for GDB, and the bytes in a register
80 are in the same order the machine uses.
81 or ENN for an error.
82
83 write regs GXX..XX Each byte of register data
84 is described by two hex digits.
85 reply OK for success
86 ENN for an error
87
88 write reg Pn...=r... Write register n... with value r...,
89 which contains two hex digits for each
90 byte in the register (target byte
91 order).
92 reply OK for success
93 ENN for an error
94 (not supported by all stubs).
95
96 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
97 reply XX..XX XX..XX is mem contents
98 Can be fewer bytes than requested
99 if able to read only part of the data.
100 or ENN NN is errno
101
102 write mem MAA..AA,LLLL:XX..XX
103 AA..AA is address,
104 LLLL is number of bytes,
105 XX..XX is data
106 reply OK for success
107 ENN for an error (this includes the case
108 where only part of the data was
109 written).
110
111 cont cAA..AA AA..AA is address to resume
112 If AA..AA is omitted,
113 resume at same address.
114
115 step sAA..AA AA..AA is address to resume
116 If AA..AA is omitted,
117 resume at same address.
118
119 last signal ? Reply the current reason for stopping.
120 This is the same reply as is generated
121 for step or cont : SAA where AA is the
122 signal number.
123
124 There is no immediate reply to step or cont.
125 The reply comes when the machine stops.
126 It is SAA AA is the "signal number"
127
128 or... TAAn...:r...;n:r...;n...:r...;
129 AA = signal number
130 n... = register number
131 r... = register contents
132 or... WAA The process exited, and AA is
133 the exit status. This is only
134 applicable for certains sorts of
135 targets.
136 kill request k
137
138 toggle debug d toggle debug flag (see 386 & 68k stubs)
139 reset r reset -- see sparc stub.
140 reserved <other> On other requests, the stub should
141 ignore the request and send an empty
142 response ($#<checksum>). This way
143 we can extend the protocol and GDB
144 can tell whether the stub it is
145 talking to uses the old or the new.
146 search tAA:PP,MM Search backwards starting at address
147 AA for a match with pattern PP and
148 mask MM. PP and MM are 4 bytes.
149 Not supported by all stubs.
150
151 general query qXXXX Request info about XXXX.
152 general set QXXXX=yyyy Set value of XXXX to yyyy.
153 query sect offs qOffsets Get section offsets. Reply is
154 Text=xxx;Data=yyy;Bss=zzz
155 console output Otext Send text to stdout. Only comes from
156 remote target.
157
158 Responses can be run-length encoded to save space. A '*' means that
159 the next character is an ASCII encoding giving a repeat count which
160 stands for that many repititions of the character preceding the '*'.
161 The encoding is n+29, yielding a printable character where n >=3
162 (which is where rle starts to win). Don't use an n > 126.
163
164 So
165 "0* " means the same as "0000". */
166
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000167#include "sh7034.h"
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000168#include <string.h>
169#include <setjmp.h>
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000170#include <signal.h>
171
172/* We need to undefine this from the sh7034.h file */
173#undef GBR
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000174
175/* Hitachi SH architecture instruction encoding masks */
176
177#define COND_BR_MASK 0xff00
178#define UCOND_DBR_MASK 0xe000
179#define UCOND_RBR_MASK 0xf0df
180#define TRAPA_MASK 0xff00
181
182#define COND_DISP 0x00ff
183#define UCOND_DISP 0x0fff
184#define UCOND_REG 0x0f00
185
186/* Hitachi SH instruction opcodes */
187
188#define BF_INSTR 0x8b00
189#define BT_INSTR 0x8900
190#define BRA_INSTR 0xa000
191#define BSR_INSTR 0xb000
192#define JMP_INSTR 0x402b
193#define JSR_INSTR 0x400b
194#define RTS_INSTR 0x000b
195#define RTE_INSTR 0x002b
196#define TRAPA_INSTR 0xc300
197#define SSTEP_INSTR 0xc37f
198
199/* Hitachi SH processor register masks */
200
201#define T_BIT_MASK 0x0001
202
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000203/*
204 * BUFMAX defines the maximum number of characters in inbound/outbound
205 * buffers. At least NUMREGBYTES*2 are needed for register packets.
206 */
207#define BUFMAX 1024
208
209/*
210 * Number of bytes for registers
211 */
212#define NUMREGBYTES 112 /* 92 */
213
214/*
215 * Forward declarations
216 */
217
218static int hex (char);
219static char *mem2hex (char *mem, char *buf, int count);
220static char *hex2mem (char *buf, char *mem, int count);
221static int hex2int (char **ptr, int *intValue);
222static unsigned char *getpacket (void);
223static void putpacket (register char *buffer);
224static int computeSignal (int exceptionVector);
225void handle_buserror (void);
226void handle_exception (int exceptionVector);
227void init_serial();
228
229void serial_putc (char ch);
230char serial_getc (void);
231
232/* These are in the file but in asm statements so the compiler can't see them */
233void catch_exception_4 (void);
234void catch_exception_5 (void);
235void catch_exception_6 (void);
236void catch_exception_7 (void);
237void catch_exception_8 (void);
238void catch_exception_9 (void);
239void catch_exception_10 (void);
240void catch_exception_11 (void);
241void catch_exception_12 (void);
242void catch_exception_13 (void);
243void catch_exception_14 (void);
244void catch_exception_15 (void);
245void catch_exception_16 (void);
246void catch_exception_17 (void);
247void catch_exception_18 (void);
248void catch_exception_19 (void);
249void catch_exception_20 (void);
250void catch_exception_21 (void);
251void catch_exception_22 (void);
252void catch_exception_23 (void);
253void catch_exception_24 (void);
254void catch_exception_25 (void);
255void catch_exception_26 (void);
256void catch_exception_27 (void);
257void catch_exception_28 (void);
258void catch_exception_29 (void);
259void catch_exception_30 (void);
260void catch_exception_31 (void);
261void catch_exception_32 (void);
262void catch_exception_33 (void);
263void catch_exception_34 (void);
264void catch_exception_35 (void);
265void catch_exception_36 (void);
266void catch_exception_37 (void);
267void catch_exception_38 (void);
268void catch_exception_39 (void);
269void catch_exception_40 (void);
270void catch_exception_41 (void);
271void catch_exception_42 (void);
272void catch_exception_43 (void);
273void catch_exception_44 (void);
274void catch_exception_45 (void);
275void catch_exception_46 (void);
276void catch_exception_47 (void);
277void catch_exception_48 (void);
278void catch_exception_49 (void);
279void catch_exception_50 (void);
280void catch_exception_51 (void);
281void catch_exception_52 (void);
282void catch_exception_53 (void);
283void catch_exception_54 (void);
284void catch_exception_55 (void);
285void catch_exception_56 (void);
286void catch_exception_57 (void);
287void catch_exception_58 (void);
288void catch_exception_59 (void);
289void catch_exception_60 (void);
290void catch_exception_61 (void);
291void catch_exception_62 (void);
292void catch_exception_63 (void);
293void catch_exception_64 (void);
294void catch_exception_65 (void);
295void catch_exception_66 (void);
296void catch_exception_67 (void);
297void catch_exception_68 (void);
298void catch_exception_69 (void);
299void catch_exception_70 (void);
300void catch_exception_71 (void);
301void catch_exception_72 (void);
302void catch_exception_73 (void);
303void catch_exception_74 (void);
304void catch_exception_75 (void);
305void catch_exception_76 (void);
306void catch_exception_77 (void);
307void catch_exception_78 (void);
308void catch_exception_79 (void);
309void catch_exception_80 (void);
310void catch_exception_81 (void);
311void catch_exception_82 (void);
312void catch_exception_83 (void);
313void catch_exception_84 (void);
314void catch_exception_85 (void);
315void catch_exception_86 (void);
316void catch_exception_87 (void);
317void catch_exception_88 (void);
318void catch_exception_89 (void);
319void catch_exception_90 (void);
320void catch_exception_91 (void);
321void catch_exception_92 (void);
322void catch_exception_93 (void);
323void catch_exception_94 (void);
324void catch_exception_95 (void);
325void catch_exception_96 (void);
326void catch_exception_97 (void);
327void catch_exception_98 (void);
328void catch_exception_99 (void);
329void catch_exception_100 (void);
330void catch_exception_101 (void);
331void catch_exception_102 (void);
332void catch_exception_103 (void);
333void catch_exception_104 (void);
334void catch_exception_105 (void);
335void catch_exception_106 (void);
336void catch_exception_107 (void);
337void catch_exception_108 (void);
338void catch_exception_109 (void);
339void catch_exception_110 (void);
340void catch_exception_111 (void);
341void catch_exception_112 (void);
342void catch_exception_113 (void);
343void catch_exception_114 (void);
344void catch_exception_115 (void);
345void catch_exception_116 (void);
346void catch_exception_117 (void);
347void catch_exception_118 (void);
348void catch_exception_119 (void);
349void catch_exception_120 (void);
350void catch_exception_121 (void);
351void catch_exception_122 (void);
352void catch_exception_123 (void);
353void catch_exception_124 (void);
354void catch_exception_125 (void);
355void catch_exception_126 (void);
356void catch_exception_127 (void);
357
358void breakpoint (void);
359
360
361#define init_stack_size 2*1024 /* if you change this you should also modify BINIT */
362#define stub_stack_size 2*1024
363
364int init_stack[init_stack_size] __attribute__ ((section ("stack"))) = {0};
365int stub_stack[stub_stack_size] __attribute__ ((section ("stack"))) = {0};
366
367
368void INIT ();
369void start ();
370
371#define CPU_BUS_ERROR_VEC 9
372#define DMA_BUS_ERROR_VEC 10
373#define NMI_VEC 11
374#define INVALID_INSN_VEC 4
375#define INVALID_SLOT_VEC 6
376#define TRAP_VEC 32
377#define IO_VEC 33
378#define USER_VEC 127
379
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000380char in_nmi; /* Set when handling an NMI, so we don't reenter */
381int dofault; /* Non zero, bus errors will raise exception */
382
383int *stub_sp;
384
385/* debug > 0 prints ill-formed commands in valid packets & checksum errors */
386int remote_debug;
387
388/* jump buffer used for setjmp/longjmp */
389jmp_buf remcomEnv;
390
391enum regnames
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000392{
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000393 R0, R1, R2, R3, R4, R5, R6, R7,
394 R8, R9, R10, R11, R12, R13, R14,
395 R15, PC, PR, GBR, VBR, MACH, MACL, SR,
396 TICKS, STALLS, CYCLES, INSTS, PLR
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000397};
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000398
399typedef struct
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000400{
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000401 short *memAddr;
402 short oldInstr;
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000403}
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000404stepData;
405
406int registers[NUMREGBYTES / 4];
407stepData instrBuffer;
408char stepped;
409static const char hexchars[] = "0123456789abcdef";
410static char remcomInBuffer[BUFMAX];
411static char remcomOutBuffer[BUFMAX];
412
413void blink(void)
414{
415 while(1)
416 {
417 int i;
418 PBDR ^= 0x40; /* toggle PB6 */
419 for(i = 0;i < 500000;i++)
420 {
421 }
422 }
423}
424
425char highhex(int x)
426{
427 return hexchars[(x >> 4) & 0xf];
428}
429
430char lowhex(int x)
431{
432 return hexchars[x & 0xf];
433}
434
435/*
436 * Assembly macros
437 */
438
439#define BREAKPOINT() asm("trapa #0x20"::);
440
441
442/*
443 * Routines to handle hex data
444 */
445
446static int hex (char ch)
447{
448 if ((ch >= 'a') && (ch <= 'f'))
449 return (ch - 'a' + 10);
450 if ((ch >= '0') && (ch <= '9'))
451 return (ch - '0');
452 if ((ch >= 'A') && (ch <= 'F'))
453 return (ch - 'A' + 10);
454 return (-1);
455}
456
457/* convert the memory, pointed to by mem into hex, placing result in buf */
458/* return a pointer to the last char put in buf (null) */
459static char *mem2hex (char *mem, char *buf, int count)
460{
461 int i;
462 int ch;
463 for (i = 0; i < count; i++)
464 {
465 ch = *mem++;
466 *buf++ = highhex (ch);
467 *buf++ = lowhex (ch);
468 }
469 *buf = 0;
470 return (buf);
471}
472
473/* convert the hex array pointed to by buf into binary, to be placed in mem */
474/* return a pointer to the character after the last byte written */
475static char *hex2mem (char *buf, char *mem, int count)
476{
477 int i;
478 unsigned char ch;
479 for (i = 0; i < count; i++)
480 {
481 ch = hex (*buf++) << 4;
482 ch = ch + hex (*buf++);
483 *mem++ = ch;
484 }
485 return (mem);
486}
487
488/**********************************************/
489/* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */
490/* RETURN NUMBER OF CHARS PROCESSED */
491/**********************************************/
492static int hex2int (char **ptr, int *intValue)
493{
494 int numChars = 0;
495 int hexValue;
496
497 *intValue = 0;
498
499 while (**ptr)
500 {
501 hexValue = hex (**ptr);
502 if (hexValue >= 0)
503 {
504 *intValue = (*intValue << 4) | hexValue;
505 numChars++;
506 }
507 else
508 break;
509
510 (*ptr)++;
511 }
512
513 return (numChars);
514}
515
516/*
517 * Routines to get and put packets
518 */
519
520/* scan for the sequence $<data>#<checksum> */
521
522unsigned char *getpacket (void)
523{
524 unsigned char *buffer = &remcomInBuffer[0];
525 unsigned char checksum;
526 unsigned char xmitcsum;
527 int count;
528 char ch;
529
530 while (1)
531 {
532 /* wait around for the start character, ignore all other characters */
533 while ((ch = serial_getc ()) != '$')
534 ;
535
536 retry:
537 checksum = 0;
538 xmitcsum = -1;
539 count = 0;
540
541 /* now, read until a # or end of buffer is found */
542 while (count < BUFMAX)
543 {
544 ch = serial_getc ();
545 if (ch == '$')
546 goto retry;
547 if (ch == '#')
548 break;
549 checksum = checksum + ch;
550 buffer[count] = ch;
551 count = count + 1;
552 }
553 buffer[count] = 0;
554
555 if (ch == '#')
556 {
557 ch = serial_getc ();
558 xmitcsum = hex (ch) << 4;
559 ch = serial_getc ();
560 xmitcsum += hex (ch);
561
562 if (checksum != xmitcsum)
563 {
564 serial_putc ('-'); /* failed checksum */
565 }
566 else
567 {
568 serial_putc ('+'); /* successful transfer */
569
570 /* if a sequence char is present, reply the sequence ID */
571 if (buffer[2] == ':')
572 {
573 serial_putc (buffer[0]);
574 serial_putc (buffer[1]);
575
576 return &buffer[3];
577 }
578
579 return &buffer[0];
580 }
581 }
582 }
583}
584
585
586/* send the packet in buffer. */
587
588static void putpacket (register char *buffer)
589{
590 register int checksum;
591
592 /* $<packet info>#<checksum>. */
593 do
594 {
595 char *src = buffer;
596 serial_putc ('$');
597 checksum = 0;
598
599 while (*src)
600 {
601 int runlen;
602
603 /* Do run length encoding */
604 for (runlen = 0; runlen < 100; runlen ++)
605 {
606 if (src[0] != src[runlen])
607 {
608 if (runlen > 3)
609 {
610 int encode;
611 /* Got a useful amount */
612 serial_putc (*src);
613 checksum += *src;
614 serial_putc ('*');
615 checksum += '*';
616 checksum += (encode = runlen + ' ' - 4);
617 serial_putc (encode);
618 src += runlen;
619 }
620 else
621 {
622 serial_putc (*src);
623 checksum += *src;
624 src++;
625 }
626 break;
627 }
628 }
629 }
630
631
632 serial_putc ('#');
633 serial_putc (highhex(checksum));
634 serial_putc (lowhex(checksum));
635 }
636 while (serial_getc() != '+');
637}
638
639
640/* a bus error has occurred, perform a longjmp
641 to return execution and allow handling of the error */
642
643void handle_buserror (void)
644{
645 longjmp (remcomEnv, 1);
646}
647
648/*
649 * this function takes the SH-1 exception number and attempts to
650 * translate this number into a unix compatible signal value
651 */
652static int computeSignal (int exceptionVector)
653{
654 int sigval;
655 switch (exceptionVector)
656 {
657 case INVALID_INSN_VEC:
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000658 sigval = SIGILL;
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000659 break;
660 case INVALID_SLOT_VEC:
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000661 sigval = SIGILL;
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000662 break;
663 case CPU_BUS_ERROR_VEC:
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000664 sigval = SIGBUS;
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000665 break;
666 case DMA_BUS_ERROR_VEC:
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000667 sigval = SIGBUS;
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000668 break;
669 case NMI_VEC:
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000670 sigval = SIGINT;
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000671 break;
672
673 case TRAP_VEC:
674 case USER_VEC:
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000675 sigval = SIGTRAP;
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000676 break;
677
678 default:
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000679 sigval = SIGEMT; /* "software generated"*/
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000680 break;
681 }
682 return (sigval);
683}
684
685void doSStep (void)
686{
687 short *instrMem;
688 int displacement;
689 int reg;
690 unsigned short opcode;
691
692 instrMem = (short *) registers[PC];
693
694 opcode = *instrMem;
695 stepped = 1;
696
697 if ((opcode & COND_BR_MASK) == BT_INSTR)
698 {
699 if (registers[SR] & T_BIT_MASK)
700 {
701 displacement = (opcode & COND_DISP) << 1;
702 if (displacement & 0x80)
703 displacement |= 0xffffff00;
704 /*
705 * Remember PC points to second instr.
706 * after PC of branch ... so add 4
707 */
708 instrMem = (short *) (registers[PC] + displacement + 4);
709 }
710 else
711 instrMem += 1;
712 }
713 else if ((opcode & COND_BR_MASK) == BF_INSTR)
714 {
715 if (registers[SR] & T_BIT_MASK)
716 instrMem += 1;
717 else
718 {
719 displacement = (opcode & COND_DISP) << 1;
720 if (displacement & 0x80)
721 displacement |= 0xffffff00;
722 /*
723 * Remember PC points to second instr.
724 * after PC of branch ... so add 4
725 */
726 instrMem = (short *) (registers[PC] + displacement + 4);
727 }
728 }
729 else if ((opcode & UCOND_DBR_MASK) == BRA_INSTR)
730 {
731 displacement = (opcode & UCOND_DISP) << 1;
732 if (displacement & 0x0800)
733 displacement |= 0xfffff000;
734
735 /*
736 * Remember PC points to second instr.
737 * after PC of branch ... so add 4
738 */
739 instrMem = (short *) (registers[PC] + displacement + 4);
740 }
741 else if ((opcode & UCOND_RBR_MASK) == JSR_INSTR)
742 {
743 reg = (char) ((opcode & UCOND_REG) >> 8);
744
745 instrMem = (short *) registers[reg];
746 }
747 else if (opcode == RTS_INSTR)
748 instrMem = (short *) registers[PR];
749 else if (opcode == RTE_INSTR)
750 instrMem = (short *) registers[15];
751 else if ((opcode & TRAPA_MASK) == TRAPA_INSTR)
752 instrMem = (short *) ((opcode & ~TRAPA_MASK) << 2);
753 else
754 instrMem += 1;
755
756 instrBuffer.memAddr = instrMem;
757 instrBuffer.oldInstr = *instrMem;
758 *instrMem = SSTEP_INSTR;
759}
760
761
762/* Undo the effect of a previous doSStep. If we single stepped,
763 restore the old instruction. */
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000764void undoSStep (void)
765{
766 if (stepped)
767 {
768 short *instrMem;
769 instrMem = instrBuffer.memAddr;
770 *instrMem = instrBuffer.oldInstr;
771 }
772 stepped = 0;
773}
774
775/*
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000776 * This function does all exception handling. It only does two things -
777 * it figures out why it was called and tells gdb, and then it reacts
778 * to gdb's requests.
779 *
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000780*/
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000781void gdb_handle_exception (int exceptionVector)
782{
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000783 int sigval, stepping;
784 int addr, length;
785 char *ptr;
786
787 /* reply to host that an exception has occurred */
788 sigval = computeSignal (exceptionVector);
789 remcomOutBuffer[0] = 'S';
790 remcomOutBuffer[1] = highhex(sigval);
791 remcomOutBuffer[2] = lowhex (sigval);
792 remcomOutBuffer[3] = 0;
793
794 putpacket (remcomOutBuffer);
795
796 /*
797 * exception 127 indicates a software trap
798 * inserted in place of code ... so back up
799 * PC by one instruction, since this instruction
800 * will later be replaced by its original one!
801 */
802 if (exceptionVector == USER_VEC
803 || exceptionVector == TRAP_VEC)
804 registers[PC] -= 2;
805
806 /*
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000807 * Do the things needed to undo
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000808 * any stepping we may have done!
809 */
810 undoSStep ();
811
812 stepping = 0;
813
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000814 while (1)
815 {
816 remcomOutBuffer[0] = 0;
817 ptr = getpacket ();
818
819 switch (*ptr++)
820 {
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000821 case '?':
822 remcomOutBuffer[0] = 'S';
823 remcomOutBuffer[1] = highhex (sigval);
824 remcomOutBuffer[2] = lowhex (sigval);
825 remcomOutBuffer[3] = 0;
826 break;
827 case 'd':
828 remote_debug = !(remote_debug); /* toggle debug flag */
829 break;
830 case 'g': /* return the value of the CPU registers */
831 mem2hex ((char *) registers, remcomOutBuffer, NUMREGBYTES);
832 break;
833 case 'G': /* set the value of the CPU registers - return OK */
834 hex2mem (ptr, (char *) registers, NUMREGBYTES);
835 strcpy (remcomOutBuffer, "OK");
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000836 break;
837
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000838 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
839 case 'm':
840 if (setjmp (remcomEnv) == 0)
841 {
842 dofault = 0;
843 /* TRY, TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
844 if (hex2int (&ptr, &addr))
845 if (*(ptr++) == ',')
846 if (hex2int (&ptr, &length))
847 {
848 ptr = 0;
849 mem2hex ((char *) addr, remcomOutBuffer, length);
850 }
851 if (ptr)
852 strcpy (remcomOutBuffer, "E01");
853 }
854 else
855 strcpy (remcomOutBuffer, "E03");
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000856
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +0000857 /* restore handler for bus error */
858 dofault = 1;
859 break;
860
861 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
862 case 'M':
863 if (setjmp (remcomEnv) == 0)
864 {
865 dofault = 0;
866
867 /* TRY, TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
868 if (hex2int (&ptr, &addr))
869 if (*(ptr++) == ',')
870 if (hex2int (&ptr, &length))
871 if (*(ptr++) == ':')
872 {
873 hex2mem (ptr, (char *) addr, length);
874 ptr = 0;
875 strcpy (remcomOutBuffer, "OK");
876 }
877 if (ptr)
878 strcpy (remcomOutBuffer, "E02");
879 }
880 else
881 strcpy (remcomOutBuffer, "E03");
882
883 /* restore handler for bus error */
884 dofault = 1;
885 break;
886
887 /* cAA..AA Continue at address AA..AA(optional) */
888 /* sAA..AA Step one instruction from AA..AA(optional) */
889 case 's':
890 stepping = 1;
891 case 'c':
892 {
893 /* tRY, to read optional parameter, pc unchanged if no parm */
894 if (hex2int (&ptr, &addr))
895 registers[PC] = addr;
896
897 if (stepping)
898 doSStep ();
899 }
900
901 return;
902 break;
903
904 /* kill the program */
905 case 'k': /* do nothing */
906 break;
907
908 default:
909 break;
910 } /* switch */
Björn Stenbergd42d78f2002-04-15 08:35:08 +0000911
912 /* reply to the request */
913 putpacket (remcomOutBuffer);
914 }
915}
916
917
918/* We've had an exception - go into the gdb stub */
919void handle_exception(int exceptionVector)
920{
921 gdb_handle_exception (exceptionVector);
922}
923
924/* This function will generate a breakpoint exception. It is used at the
925 beginning of a program to sync up with a debugger and can be used
926 otherwise as a quick means to stop program execution and "break" into
927 the debugger. */
928void breakpoint (void)
929{
930 BREAKPOINT ();
931}
932
933/**** Processor-specific routines start here ****/
934/**** Processor-specific routines start here ****/
935/**** Processor-specific routines start here ****/
936
937/* SH1/SH2 exception vector table format */
938typedef struct
939{
940 void (*func_cold) ();
941 int *stack_cold;
942 void (*func_warm) ();
943 int *stack_warm;
944 void (*(handler[128 - 4])) ();
945} vec_type;
946
947/* vectable is the SH1/SH2 vector table. It must be at address 0
948** or wherever your vbr points.
949** Note that we only define the first 128 vectors, since the Jukebox
950** firmware has its entry point at 0x200
951*/
952
953const vec_type vectable __attribute__ ((section (".vectors"))) =
954{
955 &start, /* 0: Power-on reset PC */
956 init_stack + init_stack_size, /* 1: Power-on reset SP */
957 &start, /* 2: Manual reset PC */
958 init_stack + init_stack_size, /* 3: Manual reset SP */
959 {
960 &catch_exception_4, /* 4: General invalid instruction */
961 &catch_exception_5, /* 5: Reserved for system */
962 &catch_exception_6, /* 6: Invalid slot instruction */
963 &catch_exception_7, /* 7: Reserved for system */
964 &catch_exception_8, /* 8: Reserved for system */
965 &catch_exception_9, /* 9: CPU bus error */
966 &catch_exception_10, /* 10: DMA bus error */
967 &catch_exception_11, /* 11: NMI */
968 &catch_exception_12, /* 12: User break */
969 &catch_exception_13, /* 13: Reserved for system */
970 &catch_exception_14, /* 14: Reserved for system */
971 &catch_exception_15, /* 15: Reserved for system */
972 &catch_exception_16, /* 16: Reserved for system */
973 &catch_exception_17, /* 17: Reserved for system */
974 &catch_exception_18, /* 18: Reserved for system */
975 &catch_exception_19, /* 19: Reserved for system */
976 &catch_exception_20, /* 20: Reserved for system */
977 &catch_exception_21, /* 21: Reserved for system */
978 &catch_exception_22, /* 22: Reserved for system */
979 &catch_exception_23, /* 23: Reserved for system */
980 &catch_exception_24, /* 24: Reserved for system */
981 &catch_exception_25, /* 25: Reserved for system */
982 &catch_exception_26, /* 26: Reserved for system */
983 &catch_exception_27, /* 27: Reserved for system */
984 &catch_exception_28, /* 28: Reserved for system */
985 &catch_exception_29, /* 29: Reserved for system */
986 &catch_exception_30, /* 30: Reserved for system */
987 &catch_exception_31, /* 31: Reserved for system */
988 &catch_exception_32, /* 32: Trap instr (user vectors) */
989 &catch_exception_33, /* 33: Trap instr (user vectors) */
990 &catch_exception_34, /* 34: Trap instr (user vectors) */
991 &catch_exception_35, /* 35: Trap instr (user vectors) */
992 &catch_exception_36, /* 36: Trap instr (user vectors) */
993 &catch_exception_37, /* 37: Trap instr (user vectors) */
994 &catch_exception_38, /* 38: Trap instr (user vectors) */
995 &catch_exception_39, /* 39: Trap instr (user vectors) */
996 &catch_exception_40, /* 40: Trap instr (user vectors) */
997 &catch_exception_41, /* 41: Trap instr (user vectors) */
998 &catch_exception_42, /* 42: Trap instr (user vectors) */
999 &catch_exception_43, /* 43: Trap instr (user vectors) */
1000 &catch_exception_44, /* 44: Trap instr (user vectors) */
1001 &catch_exception_45, /* 45: Trap instr (user vectors) */
1002 &catch_exception_46, /* 46: Trap instr (user vectors) */
1003 &catch_exception_47, /* 47: Trap instr (user vectors) */
1004 &catch_exception_48, /* 48: Trap instr (user vectors) */
1005 &catch_exception_49, /* 49: Trap instr (user vectors) */
1006 &catch_exception_50, /* 50: Trap instr (user vectors) */
1007 &catch_exception_51, /* 51: Trap instr (user vectors) */
1008 &catch_exception_52, /* 52: Trap instr (user vectors) */
1009 &catch_exception_53, /* 53: Trap instr (user vectors) */
1010 &catch_exception_54, /* 54: Trap instr (user vectors) */
1011 &catch_exception_55, /* 55: Trap instr (user vectors) */
1012 &catch_exception_56, /* 56: Trap instr (user vectors) */
1013 &catch_exception_57, /* 57: Trap instr (user vectors) */
1014 &catch_exception_58, /* 58: Trap instr (user vectors) */
1015 &catch_exception_59, /* 59: Trap instr (user vectors) */
1016 &catch_exception_60, /* 60: Trap instr (user vectors) */
1017 &catch_exception_61, /* 61: Trap instr (user vectors) */
1018 &catch_exception_62, /* 62: Trap instr (user vectors) */
1019 &catch_exception_63, /* 63: Trap instr (user vectors) */
1020 &catch_exception_64, /* 64: IRQ0 */
1021 &catch_exception_65, /* 65: IRQ1 */
1022 &catch_exception_66, /* 66: IRQ2 */
1023 &catch_exception_67, /* 67: IRQ3 */
1024 &catch_exception_68, /* 68: IRQ4 */
1025 &catch_exception_69, /* 69: IRQ5 */
1026 &catch_exception_70, /* 70: IRQ6 */
1027 &catch_exception_71, /* 71: IRQ7 */
1028 &catch_exception_72,
1029 &catch_exception_73,
1030 &catch_exception_74,
1031 &catch_exception_75,
1032 &catch_exception_76,
1033 &catch_exception_77,
1034 &catch_exception_78,
1035 &catch_exception_79,
1036 &catch_exception_80,
1037 &catch_exception_81,
1038 &catch_exception_82,
1039 &catch_exception_83,
1040 &catch_exception_84,
1041 &catch_exception_85,
1042 &catch_exception_86,
1043 &catch_exception_87,
1044 &catch_exception_88,
1045 &catch_exception_89,
1046 &catch_exception_90,
1047 &catch_exception_91,
1048 &catch_exception_92,
1049 &catch_exception_93,
1050 &catch_exception_94,
1051 &catch_exception_95,
1052 &catch_exception_96,
1053 &catch_exception_97,
1054 &catch_exception_98,
1055 &catch_exception_99,
1056 &catch_exception_100,
1057 &catch_exception_101,
1058 &catch_exception_102,
1059 &catch_exception_103,
1060 &catch_exception_104,
1061 &catch_exception_105,
1062 &catch_exception_106,
1063 &catch_exception_107,
1064 &catch_exception_108,
1065 &catch_exception_109,
1066 &catch_exception_110,
1067 &catch_exception_111,
1068 &catch_exception_112,
1069 &catch_exception_113,
1070 &catch_exception_114,
1071 &catch_exception_115,
1072 &catch_exception_116,
1073 &catch_exception_117,
1074 &catch_exception_118,
1075 &catch_exception_119,
1076 &catch_exception_120,
1077 &catch_exception_121,
1078 &catch_exception_122,
1079 &catch_exception_123,
1080 &catch_exception_124,
1081 &catch_exception_125,
1082 &catch_exception_126,
1083 &catch_exception_127}};
1084
1085void INIT (void)
1086{
1087 /* Disable all timer interrupts */
1088 TIER0 = 0;
1089 TIER1 = 0;
1090 TIER2 = 0;
1091 TIER3 = 0;
1092 TIER4 = 0;
1093
1094 init_serial();
1095
1096 in_nmi = 0;
1097 dofault = 1;
1098 stepped = 0;
1099
1100 stub_sp = stub_stack + stub_stack_size;
1101 breakpoint ();
1102
1103 /* We should never come here */
1104 blink();
1105}
1106
Björn Stenbergd42d78f2002-04-15 08:35:08 +00001107void sr()
1108{
1109 /* Calling Reset does the same as pressing the button */
1110 asm (".global _Reset\n"
1111 " .global _WarmReset\n"
1112 "_Reset:\n"
1113 "_WarmReset:\n"
1114 " mov.l L_sp,r15\n"
1115 " bra _INIT\n"
1116 " nop\n"
1117 " .align 2\n"
1118 "L_sp: .long _init_stack + 8000");
1119
1120 asm("saveRegisters:\n");
1121 asm(" mov.l @(L_reg, pc), r0\n"
1122 " mov.l @r15+, r1 ! pop R0\n"
1123 " mov.l r2, @(0x08, r0) ! save R2\n"
1124 " mov.l r1, @r0 ! save R0\n"
1125 " mov.l @r15+, r1 ! pop R1\n"
1126 " mov.l r3, @(0x0c, r0) ! save R3\n"
1127 " mov.l r1, @(0x04, r0) ! save R1\n"
1128 " mov.l r4, @(0x10, r0) ! save R4\n"
1129 " mov.l r5, @(0x14, r0) ! save R5\n"
1130 " mov.l r6, @(0x18, r0) ! save R6\n"
1131 " mov.l r7, @(0x1c, r0) ! save R7\n"
1132 " mov.l r8, @(0x20, r0) ! save R8\n"
1133 " mov.l r9, @(0x24, r0) ! save R9\n"
1134 " mov.l r10, @(0x28, r0) ! save R10\n"
1135 " mov.l r11, @(0x2c, r0) ! save R11\n"
1136 " mov.l r12, @(0x30, r0) ! save R12\n"
1137 " mov.l r13, @(0x34, r0) ! save R13\n"
1138 " mov.l r14, @(0x38, r0) ! save R14\n"
1139 " mov.l @r15+, r4 ! save arg to handleException\n"
1140 " add #8, r15 ! hide PC/SR values on stack\n"
1141 " mov.l r15, @(0x3c, r0) ! save R15\n"
1142 " add #-8, r15 ! save still needs old SP value\n"
1143 " add #92, r0 ! readjust register pointer\n"
1144 " mov r15, r2\n"
1145 " add #4, r2\n"
1146 " mov.l @r2, r2 ! R2 has SR\n"
1147 " mov.l @r15, r1 ! R1 has PC\n"
1148 " mov.l r2, @-r0 ! save SR\n"
1149 " sts.l macl, @-r0 ! save MACL\n"
1150 " sts.l mach, @-r0 ! save MACH\n"
1151 " stc.l vbr, @-r0 ! save VBR\n"
1152 " stc.l gbr, @-r0 ! save GBR\n"
1153 " sts.l pr, @-r0 ! save PR\n"
1154 " mov.l @(L_stubstack, pc), r2\n"
1155 " mov.l @(L_hdl_except, pc), r3\n"
1156 " mov.l @r2, r15\n"
1157 " jsr @r3\n"
1158 " mov.l r1, @-r0 ! save PC\n"
1159 " mov.l @(L_stubstack, pc), r0\n"
1160 " mov.l @(L_reg, pc), r1\n"
1161 " bra restoreRegisters\n"
1162 " mov.l r15, @r0 ! save __stub_stack\n"
1163
1164 " .align 2\n"
1165 "L_reg:\n"
1166 " .long _registers\n"
1167 "L_stubstack:\n"
1168 " .long _stub_sp\n"
1169 "L_hdl_except:\n"
1170 " .long _handle_exception");
1171}
1172
1173void rr()
1174{
1175 asm(" .align 2 \n"
1176 " .global _resume\n"
1177 "_resume:\n"
1178 " mov r4,r1\n"
1179 "restoreRegisters:\n"
1180 " add #8, r1 ! skip to R2\n"
1181 " mov.l @r1+, r2 ! restore R2\n"
1182 " mov.l @r1+, r3 ! restore R3\n"
1183 " mov.l @r1+, r4 ! restore R4\n"
1184 " mov.l @r1+, r5 ! restore R5\n"
1185 " mov.l @r1+, r6 ! restore R6\n"
1186 " mov.l @r1+, r7 ! restore R7\n"
1187 " mov.l @r1+, r8 ! restore R8\n"
1188 " mov.l @r1+, r9 ! restore R9\n"
1189 " mov.l @r1+, r10 ! restore R10\n"
1190 " mov.l @r1+, r11 ! restore R11\n"
1191 " mov.l @r1+, r12 ! restore R12\n"
1192 " mov.l @r1+, r13 ! restore R13\n"
1193 " mov.l @r1+, r14 ! restore R14\n"
1194 " mov.l @r1+, r15 ! restore programs stack\n"
1195 " mov.l @r1+, r0\n"
1196 " add #-8, r15 ! uncover PC/SR on stack \n"
1197 " mov.l r0, @r15 ! restore PC onto stack\n"
1198 " lds.l @r1+, pr ! restore PR\n"
1199 " ldc.l @r1+, gbr ! restore GBR\n"
1200 " ldc.l @r1+, vbr ! restore VBR\n"
1201 " lds.l @r1+, mach ! restore MACH\n"
1202 " lds.l @r1+, macl ! restore MACL\n"
1203 " mov.l @r1, r0 \n"
1204 " add #-88, r1 ! readjust reg pointer to R1\n"
1205 " mov.l r0, @(4, r15) ! restore SR onto stack+4\n"
1206 " mov.l r2, @-r15\n"
1207 " mov.l L_in_nmi, r0\n"
1208 " mov #0, r2\n"
1209 " mov.b r2, @r0\n"
1210 " mov.l @r15+, r2\n"
1211 " mov.l @r1+, r0 ! restore R0\n"
1212 " rte\n"
1213 " mov.l @r1, r1 ! restore R1");
1214}
1215
Linus Nielsen Feltzingf9389fe2002-04-23 20:21:31 +00001216static inline void code_for_catch_exception(unsigned int n)
Björn Stenbergd42d78f2002-04-15 08:35:08 +00001217{
1218 asm(" .globl _catch_exception_%O0" : : "X" (n) );
1219 asm(" _catch_exception_%O0:" :: "X" (n) );
1220
1221 asm(" add #-4, r15 ! reserve spot on stack ");
1222 asm(" mov.l r1, @-r15 ! push R1 ");
1223
1224 if (n == NMI_VEC)
1225 {
1226 /* Special case for NMI - make sure that they don't nest */
1227 asm(" mov.l r0, @-r15 ! push R0");
1228 asm(" mov.l L_in_nmi, r0");
1229 asm(" tas.b @r0 ! Fend off against addtnl NMIs");
1230 asm(" bt noNMI");
1231 asm(" mov.l @r15+, r0");
1232 asm(" mov.l @r15+, r1");
1233 asm(" add #4, r15");
1234 asm(" rte");
1235 asm(" nop");
1236 asm(".align 2");
1237 asm("L_in_nmi: .long _in_nmi");
1238 asm("noNMI:");
1239 }
1240 else
1241 {
1242
1243 if (n == CPU_BUS_ERROR_VEC)
1244 {
1245 /* Exception 9 (bus errors) are disasbleable - so that you
1246 can probe memory and get zero instead of a fault.
1247 Because the vector table may be in ROM we don't revector
1248 the interrupt like all the other stubs, we check in here
1249 */
1250 asm("mov.l L_dofault,r1");
1251 asm("mov.l @r1,r1");
1252 asm("tst r1,r1");
1253 asm("bf faultaway");
1254 asm("bsr _handle_buserror");
1255 asm(".align 2");
1256 asm("L_dofault: .long _dofault");
1257 asm("faultaway:");
1258 }
1259 asm(" mov #15<<4, r1 ");
1260 asm(" ldc r1, sr ! disable interrupts ");
1261 asm(" mov.l r0, @-r15 ! push R0 ");
1262 }
1263
1264 /* Prepare for saving context, we've already pushed r0 and r1, stick
1265 exception number into the frame */
1266 asm(" mov r15, r0 ");
1267 asm(" add #8, r0 ");
1268 asm(" mov %0,r1" :: "X" (n));
1269 asm(" extu.b r1,r1 ");
1270 asm(" bra saveRegisters ! save register values ");
1271 asm(" mov.l r1, @r0 ! save exception # ");
1272}
1273
1274/* Here we call all defined exceptions, so the inline assembler gets
1275 generated */
1276void exceptions (void)
1277{
1278 code_for_catch_exception (4);
1279 code_for_catch_exception (5);
1280 code_for_catch_exception (6);
1281 code_for_catch_exception (7);
1282 code_for_catch_exception (8);
1283 code_for_catch_exception (9);
1284 code_for_catch_exception (10);
1285 code_for_catch_exception (11);
1286 code_for_catch_exception (12);
1287 code_for_catch_exception (13);
1288 code_for_catch_exception (14);
1289 code_for_catch_exception (15);
1290 code_for_catch_exception (16);
1291 code_for_catch_exception (17);
1292 code_for_catch_exception (18);
1293 code_for_catch_exception (19);
1294 code_for_catch_exception (20);
1295 code_for_catch_exception (21);
1296 code_for_catch_exception (22);
1297 code_for_catch_exception (23);
1298 code_for_catch_exception (24);
1299 code_for_catch_exception (25);
1300 code_for_catch_exception (26);
1301 code_for_catch_exception (27);
1302 code_for_catch_exception (28);
1303 code_for_catch_exception (29);
1304 code_for_catch_exception (30);
1305 code_for_catch_exception (31);
1306 code_for_catch_exception (32);
1307 code_for_catch_exception (33);
1308 code_for_catch_exception (34);
1309 code_for_catch_exception (35);
1310 code_for_catch_exception (36);
1311 code_for_catch_exception (37);
1312 code_for_catch_exception (38);
1313 code_for_catch_exception (39);
1314 code_for_catch_exception (40);
1315 code_for_catch_exception (41);
1316 code_for_catch_exception (42);
1317 code_for_catch_exception (43);
1318 code_for_catch_exception (44);
1319 code_for_catch_exception (45);
1320 code_for_catch_exception (46);
1321 code_for_catch_exception (47);
1322 code_for_catch_exception (48);
1323 code_for_catch_exception (49);
1324 code_for_catch_exception (50);
1325 code_for_catch_exception (51);
1326 code_for_catch_exception (52);
1327 code_for_catch_exception (53);
1328 code_for_catch_exception (54);
1329 code_for_catch_exception (55);
1330 code_for_catch_exception (56);
1331 code_for_catch_exception (57);
1332 code_for_catch_exception (58);
1333 code_for_catch_exception (59);
1334 code_for_catch_exception (60);
1335 code_for_catch_exception (61);
1336 code_for_catch_exception (62);
1337 code_for_catch_exception (63);
1338 code_for_catch_exception (64);
1339 code_for_catch_exception (65);
1340 code_for_catch_exception (66);
1341 code_for_catch_exception (67);
1342 code_for_catch_exception (68);
1343 code_for_catch_exception (69);
1344 code_for_catch_exception (70);
1345 code_for_catch_exception (71);
1346 code_for_catch_exception (72);
1347 code_for_catch_exception (73);
1348 code_for_catch_exception (74);
1349 code_for_catch_exception (75);
1350 code_for_catch_exception (76);
1351 code_for_catch_exception (77);
1352 code_for_catch_exception (78);
1353 code_for_catch_exception (79);
1354 code_for_catch_exception (80);
1355 code_for_catch_exception (81);
1356 code_for_catch_exception (82);
1357 code_for_catch_exception (83);
1358 code_for_catch_exception (84);
1359 code_for_catch_exception (85);
1360 code_for_catch_exception (86);
1361 code_for_catch_exception (87);
1362 code_for_catch_exception (88);
1363 code_for_catch_exception (89);
1364 code_for_catch_exception (90);
1365 code_for_catch_exception (91);
1366 code_for_catch_exception (92);
1367 code_for_catch_exception (93);
1368 code_for_catch_exception (94);
1369 code_for_catch_exception (95);
1370 code_for_catch_exception (96);
1371 code_for_catch_exception (97);
1372 code_for_catch_exception (98);
1373 code_for_catch_exception (99);
1374 code_for_catch_exception (100);
1375 code_for_catch_exception (101);
1376 code_for_catch_exception (102);
1377 code_for_catch_exception (103);
1378 code_for_catch_exception (104);
1379 code_for_catch_exception (105);
1380 code_for_catch_exception (106);
1381 code_for_catch_exception (107);
1382 code_for_catch_exception (108);
1383 code_for_catch_exception (109);
1384 code_for_catch_exception (110);
1385 code_for_catch_exception (111);
1386 code_for_catch_exception (112);
1387 code_for_catch_exception (113);
1388 code_for_catch_exception (114);
1389 code_for_catch_exception (115);
1390 code_for_catch_exception (116);
1391 code_for_catch_exception (117);
1392 code_for_catch_exception (118);
1393 code_for_catch_exception (119);
1394 code_for_catch_exception (120);
1395 code_for_catch_exception (121);
1396 code_for_catch_exception (122);
1397 code_for_catch_exception (123);
1398 code_for_catch_exception (124);
1399 code_for_catch_exception (125);
1400 code_for_catch_exception (126);
1401 code_for_catch_exception (127);
1402}
1403
1404/*
1405 * Port B Control Register (PBCR1)
1406 */
1407#define PB15MD1 0x8000
1408#define PB15MD0 0x4000
1409#define PB14MD1 0x2000
1410#define PB14MD0 0x1000
1411#define PB13MD1 0x0800
1412#define PB13MD0 0x0400
1413#define PB12MD1 0x0200
1414#define PB12MD0 0x0100
1415#define PB11MD1 0x0080
1416#define PB11MD0 0x0040
1417#define PB10MD1 0x0020
1418#define PB10MD0 0x0010
1419#define PB9MD1 0x0008
1420#define PB9MD0 0x0004
1421#define PB8MD1 0x0002
1422#define PB8MD0 0x0001
1423
1424#define PB15MD PB15MD1|PB14MD0
1425#define PB14MD PB14MD1|PB14MD0
1426#define PB13MD PB13MD1|PB13MD0
1427#define PB12MD PB12MD1|PB12MD0
1428#define PB11MD PB11MD1|PB11MD0
1429#define PB10MD PB10MD1|PB10MD0
1430#define PB9MD PB9MD1|PB9MD0
1431#define PB8MD PB8MD1|PB8MD0
1432
1433#define PB_TXD1 PB11MD1
1434#define PB_RXD1 PB10MD1
1435#define PB_TXD0 PB9MD1
1436#define PB_RXD0 PB8MD1
1437
1438#define PB7MD PB7MD1|PB7MD0
1439#define PB6MD PB6MD1|PB6MD0
1440#define PB5MD PB5MD1|PB5MD0
1441#define PB4MD PB4MD1|PB4MD0
1442#define PB3MD PB3MD1|PB3MD0
1443#define PB2MD PB2MD1|PB2MD0
1444#define PB1MD PB1MD1|PB1MD0
1445#define PB0MD PB0MD1|PB0MD0
1446
1447
1448void handleError (char theSSR);
1449
1450void nop (void)
1451{
1452}
1453
1454void init_serial (void)
1455{
1456 int i;
1457
1458 /* Clear Channel 1's SCR */
1459 SCR1 = 0;
1460
1461 /* Set communication to be async, 8-bit data,
1462 no parity, 1 stop bit and use internal clock */
1463 SMR1 = 0;
1464/* BRR1 = SYSCLOCK / (9600 * 32) - 1;*/
1465 BRR1 = 9; /* 38400 */
1466
1467 SCR1 &= ~(SCI_CKE1 | SCI_CKE0);
1468
1469 /* let the hardware settle */
1470 for (i = 0; i < 1000; i++)
1471 nop ();
1472
1473 /* Turn on in and out */
1474 SCR1 |= SCI_RE | SCI_TE;
1475
1476 /* Set the PFC to make RXD1 (pin PB8) an input pin
1477 and TXD1 (pin PB9) an output pin */
1478 PBCR1 &= ~(PB_TXD1 | PB_RXD1);
1479 PBCR1 |= PB_TXD1 | PB_RXD1;
1480}
1481
1482
1483int serial_waitc(void)
1484{
1485 char mySSR;
1486 mySSR = SSR1 & ( SCI_PER | SCI_FER | SCI_ORER );
1487 if ( mySSR )
1488 handleError ( mySSR );
1489 return SSR1 & SCI_RDRF ;
1490}
1491
1492char serial_getc (void)
1493{
1494 char ch;
1495 char mySSR;
1496
1497 while ( ! serial_waitc())
1498 ;
1499
1500 ch = RDR1;
1501 SSR1 &= ~SCI_RDRF;
1502
1503 mySSR = SSR1 & (SCI_PER | SCI_FER | SCI_ORER);
1504
1505 if (mySSR)
1506 handleError (mySSR);
1507
1508 return ch;
1509}
1510
1511void serial_putc (char ch)
1512{
1513 while (!(SSR1 & SCI_TDRE))
1514 {
1515 ;
1516 }
1517
1518 /*
1519 * Write data into TDR and clear TDRE
1520 */
1521 TDR1 = ch;
1522 SSR1 &= ~SCI_TDRE;
1523}
1524
1525void handleError (char theSSR)
1526{
1527 /* Clear all error bits, otherwise the receiver will stop */
1528 SSR1 &= ~(SCI_ORER | SCI_PER | SCI_FER);
1529}