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