PS2SDK
PS2 Homebrew Libraries
main.c
1 #include <tamtypes.h>
2 #include <stdio.h>
3 #include <loadcore.h>
4 #include <thbase.h>
5 #include <iop_cop0_defs.h>
6 #include <ps2_sbus.h>
7 #include <iopdebug.h>
8 #include <excepman.h>
9 #include "sbus_tty.h"
10 
11 IRX_ID("sbusdebug", 1, 0);
12 
13 volatile int _iop_controlled = 0;
14 volatile int _iop_exception_state = 0;
15 
16 extern void signal_iop_exception(IOP_RegFrame *frame);
17 
18 // this is called by the IOP debug/exception handler
19 void _iop_ex_handler(IOP_RegFrame *frame)
20 {
21  int excode = M_IOP_GET_CAUSE_EXCODE(frame->cause);
22 
23  if(excode == IOP_EXCEPTION_HDB)
24  {
25  _iop_exception_state = 2;
26  }
27  else
28  {
29  _iop_exception_state = 1;
30  }
31 
32  _iop_controlled = 1;
33 
34  // signal to the EE that an IOP exception occured.
35  signal_iop_exception(frame);
36 
37  // wait for EE to send a "IOP control" command and release the IOP.
38  while(_iop_controlled)
39  {
40  // since we're spinning in an interrupt-disabled state, we have to manually
41  // poll for SBUS interrupts from EE, otherwise we'll never get released!
42  SBUS_check_intr();
43  }
44 }
45 
46 int tid;
47 
48 extern void soft_break(void);
49 __asm__
50 (
51  "\t" ".set push" "\n"
52  "\t" ".set noreorder" "\n"
53  "\t" ".set noat" "\n"
54 
55  "\t" ".global soft_break" "\n"
56  "\t" ".ent soft_break" "\n"
57  "\t" "soft_break:" "\n"
58  "\t" "\t" "nop" "\n"
59  "\t" "\t" "break" "\n"
60  "\t" "\t" "nop" "\n"
61  "\t" "\t" "jr $ra" "\n"
62  "\t" "\t" "nop" "\n"
63  "\t" ".end soft_break" "\n"
64 
65  "\t" ".set pop" "\n"
66 );
67 
68 void _controller_thread(void *arg)
69 {
70  (void)arg;
71 
72  while(1)
73  {
74 // printf("IOP: Controller thread going to sleep(%d)...\n", _iop_exception_state);
75  SleepThread();
76 // printf("IOP: Controller thread woke up!\n");
77  soft_break();
78  }
79 }
80 
81 void create_th(void)
82 {
83  iop_thread_t param;
84 
85  param.attr = TH_C;
86  param.thread = _controller_thread;
87  param.priority = 9; // highest possible priority
88  param.stacksize = 512; // tiny stack, not much is needed!
89  param.option = 0;
90 
91  tid = CreateThread(&param);
92  StartThread(tid, 0);
93 }
94 
95 extern void SBUS_dbg_init(void);
96 
97 int _start(int argc, char *argv[])
98 {
99  int i;
100 
101  (void)argc;
102  (void)argv;
103 
104  for(i = 2; i <= 7; i++)
105  {
106  iop_dbg_set_handler(i, (IOP_ExceptionHandler *) _iop_ex_handler);
107  }
108 
109  for(i = 9; i <= 15; i++)
110  {
111  iop_dbg_set_handler(i, (IOP_ExceptionHandler *) _iop_ex_handler);
112  }
113 
114  create_th();
115 
116  if(sbus_tty_init() != 0)
117  {
118  printf("Failed initializing SBUS TTY system!\n");
119  return MODULE_NO_RESIDENT_END;
120  }
121 
122  SBUS_dbg_init();
123 
124  FlushIcache();
125  FlushDcache();
126 
127  printf("IOP SBUS Debug installed!\n");
128 
129  return MODULE_RESIDENT_END;
130 }
thbase.h
excepman.h
st_IOP_RegFrame
Definition: ps2_debug.h:11
_start
int _start(int argc, char *argv[])
Definition: main.c:46
ps2_sbus.h
loadcore.h
tamtypes.h
iop_cop0_defs.h
stdio.h
_iop_thread
Definition: thbase.h:39
iopdebug.h
IOP_EXCEPTION_HDB
#define IOP_EXCEPTION_HDB
Definition: excepman.h:58