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