aboutsummaryrefslogtreecommitdiff
path: root/backends/gp32/debug-gdbstub-usb.cpp
blob: 14ab38d7d6dfb01c78eba0fb5e7d0dfb27d1e1c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001  Ludvig Strigeus
 * Copyright (C) 2001/2004 The ScummVM project
 * Copyright (C) 2002 Ph0x - GP32 Backend
 * Copyright (C) 2003/2004 DJWillis - GP32 Backend
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 *
 * $Header$
 *
 */

/*
 *
 * Basic GP32 USB GDB Debug Stub - Use with Multi-firmware that supports GDB
 * Mithris kindly gave me the basic stub code.
 *
 */
#include <sys/types.h>
#include <gpcomm.h>
#include <gpos_def.h>
#include <gpdef.h>
#include <gpmain.h>
#include <gpos_def.h>
#include <gpstdio.h>
#include <gpgraphic.h>
#include <gpfont.h>
#include <gpstdlib.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

//#define USE_PRINTF // If there is a Printf(const char *pFormat,...) implemented

///////////////////////////////////////////////////////////////
// Externs
extern int __text_start;
extern int __data_start;
extern int __bss_start;
#ifdef USE_PRINTF
extern void Printf(char *pFormat, ...);
#endif
///////////////////////////////////////////////////////////////

extern "C" {
	int OpenUSB();
	void InstallISR();
}

struct g_Namedregisters {
	unsigned int	r0;
	unsigned int	r1;
	unsigned int	r2;
	unsigned int	r3;
	unsigned int	r4;
	unsigned int	r5;
	unsigned int	r6;
	unsigned int	r7;
	unsigned int	r8;
	unsigned int	r9;
	unsigned int	r10;
	unsigned int	r11;
	unsigned int	r12;
	unsigned int	sp;
	unsigned int	lr;
	unsigned int	pc;
	unsigned int	fps;
	unsigned int	fcpsr;
};

///////////////////////////////////////////////////////////////
// Defines


#define _REG_R1		0
#define _REG_R2		2
#define _REG_R3		3
#define _REG_R4		4
#define _REG_R5		5
#define _REG_R6		6
#define _REG_R7		7
#define _REG_R8		8
#define _REG_R9		9
#define _REG_R10	10
#define _REG_R11	11
#define _REG_R12	12
#define _REG_FP		11
#define _REG_IP		12
#define _REG_SL		10
#define _REG_SP		13
#define _REG_LR		14
#define _REG_PC		15

#define PACKET_SIZE 0x100

#define MODE_USER		0
#define MODE_FIQ		1
#define MODE_IRQ		2
#define MODE_SUPERVISOR	3
#define MODE_ABORT		4
#define MODE_UNDEF		5
#define MODE_SYSTEM		6
#define MODE_DUNNO		-1

///////////////////////////////////////////////////////////////
// Global stuff

// Register array.
int g_Registers[50] = {1, 2, 3, 4 ,5, 6, 7, 8, 9, 10, 
					11, 12, 13, 14, 15, 16, 17, 18, 19,
					21, 22, 23, 24, 25, 26, 27, 28, 29,
					31, 32, 33, 34, 35, 36, 37, 38, 39,
					41, 42, 43, 44, 45, 46, 47, 48, 49};

// Register name strings, not used right now.
static char * arm_register_name_strings[] =
{"r0",  "r1",  "r2",  "r3",     /*  0  1  2  3 */
 "r4",  "r5",  "r6",  "r7",     /*  4  5  6  7 */
 "r8",  "r9",  "r10", "r11",    /*  8  9 10 11 */
 "r12", "sp",  "lr",  "pc",     /* 12 13 14 15 */
 "f0",  "f1",  "f2",  "f3",     /* 16 17 18 19 */
 "f4",  "f5",  "f6",  "f7",     /* 20 21 22 23 */
 "fps", "cpsr" };               /* 24 25       */


// Some USB stuff
GPN_DESC		g_CommDesc;
GPN_COMM		g_Comm;
const char 		HexDigits[17] = "0123456789abcdef";
char			g_SendBuffer[256];
char			g_TempBuffer[256];
char			g_ReadBuffer[0x100];
unsigned int	g_CurrentCSPR = 0;
unsigned int	g_SavedStepInstruction = 0;
unsigned int	g_StepAddress = 0;
bool			g_LastWasStep = false;
int				g_iTrap = 0;
bool			g_GDBConnected = false;
///////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////
// Prototypes
void BreakPoint();
int GetException(unsigned int CSPR);
unsigned int *GetNextInstruction(unsigned int *pAddr);
bool CondWillExecute(unsigned int uiCond, unsigned int CSPR);
unsigned int DecodeInstruction(unsigned int uiInstruction, unsigned int PC);

///////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////
// Code Begins here
///////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////
// Open usb comms stuff
int OpenUSB()
{
	int	iResult;
	
	g_CommDesc.port_kind = COMM_USB_D;
	g_CommDesc.tr_mode = 1;
	g_CommDesc.tr_buf_size = PACKET_SIZE;
	g_CommDesc.sz_pkt = PACKET_SIZE;
	g_CommDesc.isr_comm_ram = 0;
  	

	iResult = GpCommCreate(&g_CommDesc, &g_Comm);
	return iResult;
}

///////////////////////////////////////////////////////////////
// No need for explanation
void CloseUSB()
{
	GpCommDelete(&g_CommDesc);
}

///////////////////////////////////////////////////////////////
// No need for explanation
int SendUSB(const void *pData, unsigned int uiSize)
{
	return g_Comm.comm_send((unsigned char *)pData, uiSize);
}


///////////////////////////////////////////////////////////////
// No need for explanation
int RecvUSB(char *pBuffer, unsigned int uiSize)
{
		return g_Comm.comm_recv((unsigned char *)pBuffer, uiSize);
}

///////////////////////////////////////////////////////////////
// Waits for an acknowledge from the server
void WaitACK()
{
	bool bBreak = false;
	int iResult;

	while(!bBreak) {
		iResult = g_Comm.comm_recv((unsigned char *)g_SendBuffer, 0x100);
		if (iResult > 0) {
			bBreak = true;
		}
	}
}
///////////////////////////////////////////////////////////////
// Formats and sends a command to the server, 
// it also calculates checksum
void SendCommand(unsigned char *pCommand)
{
	int		iOffset;
	unsigned int		iCheckSum;

	iOffset = 4;
	g_SendBuffer[iOffset++] = '$';

	iCheckSum = 0;
	while(*pCommand != 0) {
		g_SendBuffer[iOffset++] = *pCommand;
		iCheckSum += *pCommand++;
	}

	g_SendBuffer[iOffset++] = '#';
	iCheckSum = iCheckSum & 0xff;
	g_SendBuffer[iOffset++] = HexDigits[(iCheckSum & 0xf0) >> 4];
	g_SendBuffer[iOffset++] = HexDigits[(iCheckSum & 0x0f)];
	g_SendBuffer[iOffset] = 0;

	(*(int *)&g_SendBuffer[0]) = (iOffset - 4);
	SendUSB(g_SendBuffer, 0x100);
	WaitACK();
}

///////////////////////////////////////////////////////////////
// This function creates and sends a package which tells gdb that
// the last command was unsupported
void UnSupportedCommand()
{
	(*(int *)&g_SendBuffer[0]) = 4;
	g_SendBuffer[4] = '$';
	g_SendBuffer[5] = '#';
	g_SendBuffer[6] = '0';
	g_SendBuffer[7] = '0';
	g_SendBuffer[8] = '0';
	SendUSB(g_SendBuffer, 0x100);

	WaitACK();

}


///////////////////////////////////////////////////////////////
// This function is quite similar to the SendCommand above
// But it allows the user to set the package length.
// If the length of a package exceeds 256bytes including
// the protocol stuff and the package length you can split
// the packages by sending the package size * -1.
// The server will then merge all packages until a package
// with a length >0 is recieved.
void SendCommandSize(unsigned char *pCommand, int iSize)
{
	int		iOffset;
	unsigned int		iCheckSum;

	iOffset = 4;
	g_SendBuffer[iOffset++] = '$';

	iCheckSum = 0;
	while(*pCommand != 0) {
		g_SendBuffer[iOffset++] = *pCommand;
		iCheckSum += *pCommand++;
	}

	g_SendBuffer[iOffset++] = '#';
	iCheckSum = iCheckSum & 0xff;
	g_SendBuffer[iOffset++] = HexDigits[(iCheckSum & 0xf0) >> 4];
	g_SendBuffer[iOffset++] = HexDigits[(iCheckSum & 0x0f)];
	g_SendBuffer[iOffset] = 0;

	(*(int *)&g_SendBuffer[0]) = iSize;
	SendUSB(g_SendBuffer, 0x100);

	WaitACK();
}


///////////////////////////////////////////////////////////////
// Writes a 32bit hexadeciman number in ascii to the string.
void HexToString(char *pString, int iNumber)
{
	pString[0] = HexDigits[(iNumber >> 28) & 0x0f];
	pString[1] = HexDigits[(iNumber >> 24) & 0x0f];
	pString[2] = HexDigits[(iNumber >> 20) & 0x0f];
	pString[3] = HexDigits[(iNumber >> 16) & 0x0f];
	pString[4] = HexDigits[(iNumber >> 12) & 0x0f];
	pString[5] = HexDigits[(iNumber >> 8) & 0x0f];
	pString[6] = HexDigits[(iNumber >> 4) & 0x0f];
	pString[7] = HexDigits[(iNumber) & 0x0f];

}

///////////////////////////////////////////////////////////////
// This does the same, but in a differend endian.
void HexToStringBW(char *pString, int iNumber)
{
	pString[6] = HexDigits[(iNumber >> 28) & 0x0f];
	pString[7] = HexDigits[(iNumber >> 24) & 0x0f];
	pString[4] = HexDigits[(iNumber >> 20) & 0x0f];
	pString[5] = HexDigits[(iNumber >> 16) & 0x0f];
	pString[2] = HexDigits[(iNumber >> 12) & 0x0f];
	pString[3] = HexDigits[(iNumber >> 8) & 0x0f];
	pString[0] = HexDigits[(iNumber >> 4) & 0x0f];
	pString[1] = HexDigits[(iNumber) & 0x0f];
	
	pString[8] = 0;

}


///////////////////////////////////////////////////////////////
// equiv to strcat i imagine
void PrintToString(char *pString, char *pOtherString)
{
	while(*pOtherString != 0) {
		*pString = *pOtherString;
		*pString++;
		*pOtherString++;
	}
}

///////////////////////////////////////////////////////////////
// converts "ff" -> 0xff
unsigned char StringToByte(char *pString)
{
	unsigned char	ucValue = 0;

	for (int i = 0; i < 2; i++) {
		ucValue  = ucValue << 4;
		switch(pString[i]) {
			case '0':
				break;
			case '1':
				ucValue |= (0x01);
				break;
			case '2':
				ucValue |= (0x02);
				break;
			case '3':
				ucValue |= (0x03);
				break;
			case '4':
				ucValue |= (0x04);
				break;
			case '5':
				ucValue |= (0x05);
				break;
			case '6':
				ucValue |= (0x06);
				break;
			case '7':
				ucValue |= (0x07);
				break;
			case '8':
				ucValue |= (0x08);
				break;
			case '9':
				ucValue |= (0x09);
				break;
			case 'a':
				ucValue |= (0x0a);
				break;
			case 'b':
				ucValue |= (0x0b);
				break;
			case 'c':
				ucValue |= (0x0c);
				break;
			case 'd':
				ucValue |= (0x0d);
				break;
			case 'e':
				ucValue |= (0x0e);
				break;
			case 'f':
				ucValue |= (0x0f);
				break;
	
		}
	}

	return ucValue;
}

///////////////////////////////////////////////////////////////
// Sends a package with the program offsets to GDB
// Of some reason all offsets should be NULL.
void SendOffsets()
{
	char    String[255];
	int a = 0;//(int)&__text_start;
	int b = 0;//(int)&__data_start;
	int c = 0;//(int)&__bss_start;
	
	PrintToString(String, "Text=");
	HexToString(&String[5], a);
	PrintToString(&String[5+8], ";Data=");
	HexToString(&String[5+8+6], b);
	PrintToString(&String[5+8+6+8], ";Bss=");
	HexToString(&String[5+8+6+8+5], c);
	String[5+8+6+8+5+8] = 0;

	SendCommand((unsigned char *)String);
}

///////////////////////////////////////////////////////////////
// This function dumps all registers to GDB
// It utilizes the SendCommandSize function to split the package.
void DumpRegisters()
{
	char	Buffer[0x100];
	// Ugly hack so far.

	for (int i = 0; i < 21; i++) {
		//g_Registers[i] = i;
		HexToStringBW(&Buffer[i * 8], g_Registers[i]);
	}
	Buffer[21*8] = 0;
	SendCommandSize((unsigned char *)Buffer, -1 * (21 * 8 + 4));
	for (int i = 0; i < 21; i++) {
		//g_Registers[20 + i] = i;
		HexToStringBW(&Buffer[i * 8], g_Registers[21 + i]);
	}
	Buffer[21*8] = 0;
	
	SendCommandSize((unsigned char *)Buffer, (21 * 8) + 4);
}

///////////////////////////////////////////////////////////////
// This function extracts an address from a string. 
void *GetAddr(char *pBuffer)
{
	int iAddr;
	int i = 0;
	iAddr = 0;
	
	while (pBuffer[i] != ',') {
		iAddr = iAddr << 4;
		switch(pBuffer[i]) {
		case '0':
			//iAddr |= 
			break;
		case '1':
			iAddr |= (0x01);
			break;
		case '2':
			iAddr |= (0x02);
			break;
		case '3':
			iAddr |= (0x03);
			break;
		case '4':
			iAddr |= (0x04);
			break;
		case '5':
			iAddr |= (0x05);
			break;
		case '6':
			iAddr |= (0x06);
			break;
		case '7':
			iAddr |= (0x07);
			break;
		case '8':
			iAddr |= (0x08);
			break;
		case '9':
			iAddr |= (0x09);
			break;
		case 'a':
			iAddr |= (0x0a);
			break;
		case 'b':
			iAddr |= (0x0b);
			break;
		case 'c':
			iAddr |= (0x0c);
			break;
		case 'd':
			iAddr |= (0x0d);
			break;
		case 'e':
			iAddr |= (0x0e);
			break;
		case 'f':
			iAddr |= (0x0f);
			break;

		}
		i++;
	}
	return (void *)iAddr;
}
///////////////////////////////////////////////////////////////
// This function does pretty much the same, but returns an int
// I know, some redundant code.
int	GetBytes(char *pBuffer)
{
	int iBytes = 0;
	int i = 0;


	while ((pBuffer[i] != '#') && (pBuffer[i] != ':')) {
		iBytes = iBytes << 4;
		switch(pBuffer[i]) {
		case '0':
			//iAddr |= 
			break;
		case '1':
			iBytes |= 0x01;
			break;
		case '2':
			iBytes |= 0x02;
			break;
		case '3':
			iBytes |= 0x03;
			break;
		case '4':
			iBytes |= 0x04;
			break;
		case '5':
			iBytes |= 0x05;
			break;
		case '6':
			iBytes |= 0x06;
			break;
		case '7':
			iBytes |= 0x07;
			break;
		case '8':
			iBytes |= 0x08;
			break;
		case '9':
			iBytes |= 0x09;
			break;
		case 'a':
			iBytes |= 0x0a;
			break;
		case 'b':
			iBytes |= 0x0b;
			break;
		case 'c':
			iBytes |= 0x0c;
			break;
		case 'd':
			iBytes |= 0x0d;
			break;
		case 'e':
			iBytes |= 0x0e;
			break;
		case 'f':
			iBytes |= 0x0f;
			break;

		}
		
		i++;
	}
	return iBytes;
}

///////////////////////////////////////////////////////////////
// This function reads memory and sends it back to GDB.
void SendMemory(void *pAddr, int iBytes)
{
	unsigned char	*pData;
	unsigned char 	iData;
	int				iBufferPos = 0;
	int				iBytesToSend;
	char			Byte;
	int				i;

	pData = (unsigned char *)pAddr;


	do {
		if (iBytes > 100) {
			iBytesToSend = 100;
			iBytes -= 100;
		} else {
			iBytesToSend = iBytes;
			iBytes = 0;
		}
		iBufferPos = 0;
		for (i = 0; i < iBytesToSend; i+=1) {
			iData = *pData++;
			g_TempBuffer[iBufferPos++] = HexDigits[(iData & 0xf0) >> 4];
			g_TempBuffer[iBufferPos++] = HexDigits[(iData & 0x0f)];
			
		}
		if (iBytes > 0) {
			// This mean that we have not yet sent our last command.
			g_TempBuffer[iBufferPos] = 0;
			SendCommandSize((unsigned char *)g_TempBuffer, -1 * (i + 1 + 4));
		}
	} while (iBytes > 0 );
	g_TempBuffer[iBufferPos] = 0;
	SendCommand((unsigned char *)g_TempBuffer);
}



///////////////////////////////////////////////////////////////
// Does pretty much what it says.
void WriteMemory(void *pAddr, unsigned int uiBytes, char *pHexString)
{
	unsigned char	*pData = ((unsigned char *)pAddr);
	unsigned int	uiOffset = 0;
	unsigned char	ucByte;

	//Printf("0x%x 0x%x", pAddr, uiBytes);
	for (unsigned int i = 0; i < uiBytes ; i++) {
		ucByte = StringToByte(&pHexString[uiOffset]);
		//Printf("0x%x", ucByte);
		*pData++ = ucByte;
		uiOffset += 2;
	}
	/*
	while(1);
	unsigned int *piData = (unsigned int *)pAddr;
	*piData = 0xe7ffdefe;//0xfedeffe7;
	*/

}


///////////////////////////////////////////////////////////////
// Sends the required information about a trap
// TODO: correct numbers need to be placed there.
void SendBreakPoint()
{
	int iOffset = 0;

	g_TempBuffer[iOffset++] = 'T';
	g_TempBuffer[iOffset++] = '0';
	g_TempBuffer[iOffset++] = '5';
	g_TempBuffer[iOffset++] = '0';
	g_TempBuffer[iOffset++] = 'd';
	g_TempBuffer[iOffset++] = ':';
	HexToStringBW(&g_TempBuffer[iOffset], g_Registers[_REG_SP]);
	iOffset += 8;
	g_TempBuffer[iOffset++] = ';';
	g_TempBuffer[iOffset++] = '0';
	g_TempBuffer[iOffset++] = 'b';
	g_TempBuffer[iOffset++] = ':';
	HexToStringBW(&g_TempBuffer[iOffset], g_Registers[_REG_FP]);
	iOffset += 8;
	g_TempBuffer[iOffset++] = ';';
	g_TempBuffer[iOffset++] = '0';
	g_TempBuffer[iOffset++] = 'f';
	g_TempBuffer[iOffset++] = ':';
	HexToStringBW(&g_TempBuffer[iOffset], g_Registers[_REG_PC]);
	iOffset += 8;
	g_TempBuffer[iOffset++] = ';';
	g_TempBuffer[iOffset] = 0;

	SendCommand((unsigned char *)g_TempBuffer);




}


///////////////////////////////////////////////////////////////
// Finds a character in a string and returns the offset.
int FindChar(char *pBuffer, char sign)
{
	int iRetVal = 0;

	while(*pBuffer != sign) {
		iRetVal++;
		*pBuffer++;
	}
	return iRetVal;
}


// Attibute naked.
///////////////////////////////////////////////////////////////
// This is the ISR(Interrupt Service Routine) which handles
// All traps, it's basically a context switcher.
void ISR() __attribute__ ((naked));
void ISR()
{
	// Lets snatch the registers!
	
	asm volatile(" \n"
		" str		r4, [%0, #0x10] \n"
		" str		r5, [%0, #0x14] \n"
		" str		r6, [%0, #0x18] \n"
		" str		r7, [%0, #0x1C] \n"
		" str		r8, [%0, #0x20] \n"
		" str		r9, [%0, #0x24] \n"
		" str		r10, [%0, #0x28] \n"
		" str		r11, [%0, #0x2C] \n"
		" str		r12, [%0, #0x30] \n"

		" str		r14, [%0, #0x3C] \n"
		
		" @// skip 8 * 12byte(96bit) = 0x60 \n"

		" mov		r4, %0 \n"
		" ldmia	sp!, {r0, r1, r2, r3} \n"
		" str		r0, [r4, #0x00] \n"
		" str		r1, [r4, #0x04] \n"
		" str		r2, [r4, #0x08] \n"
		" str		r3, [r4,#0x0C] \n"

		" mrs		r1, SPSR \n"
		" str		r1, [r4, #0x48] \n"
		" str		r1, [r4,#0xA0] \n"
		" str		r1, [r4,#0xA4] \n"
			
		" mrs		r1, CPSR \n"
		" mov		r2, r1 \n"
		

		" @// Let us set the mode to supervisor so we can get r13 and r14 \n"
		" bic		r1, r1, #0x1f \n"
		" orr		r1, r1, #0x13 \n"
		" msr		CPSR_c, r1 \n"
		" @// Just remember that we're in the supervisor stack aswell now \n"
		" str		r13, [r4,#0x34] \n"
		" str		r14, [r4,#0x38] \n"
		" @// Lets move back to whatever mode we was in. \n"
		" @// Make sure that IRQ's are turned on \n"
		" bic		r2, r2, #0x80 \n"
		" msr		CPSR_fsxc, r2 \n"

		

	
		" \n"
		:
		:  "r" (g_Registers)
		: "%0", "r1", "r2", "r4");

	
	// Get Current CSPR and save LR
	asm volatile(" \n"
		" mrs		r0, CPSR \n"
		" str		r0, [%0] \n"

		" str		r14, [%1, #0x40] \n"
		" str		r0, [%1, #0x44] \n"
		" str		r13, [%1, #0x4C] \n"
		" \n"
		:
		: "r" (&g_CurrentCSPR), "r" (g_Registers)
		: "r0");


	switch(g_CurrentCSPR & 0x1f) {
		case 0x10: // USER
			g_iTrap = 0;
			break;
		case 0x11: // FIQ
			g_iTrap = 1;
			break;
		case 0x12: // IRQ
			g_iTrap = 2;
			break;
		case 0x13: // Supervisor
			g_iTrap = 3;
			break;
		case 0x17: // Abort
			g_iTrap = 4;
			break;
		case 0x1B: // Undefined/Breakpoint
			// We cannot continue like this!
			g_Registers[15] -= 4;
			g_Registers[16] -= 4;
			g_iTrap = 5;
			break;
		case 0x1F: // System
			g_iTrap = 6;
			break;
		default:
			g_iTrap = -1;
	}
	

	#ifdef USE_PRINTF
	Printf("Trap@0x%x:%d", g_Registers[15], g_iTrap);
	#endif
	/*
	switch (g_iTrap) {
		case MODE_USER:
			break;
		case MODE_FIQ:
			break;
		case MODE_IRQ:
			break;
		case MODE_SUPERVISOR:
			break;
		case MODE_ABORT:
			break;
		case MODE_UNDEF:
			break;
		case MODE_SYSTEM:
			break;
		case MODE_DUNNO:
		default:
			while(1);
	}
	*/
	SendBreakPoint();
	BreakPoint();
	
		//Printf("0x%x 0x%x", g_Registers[15], g_Registers[16]);
	// Okay, it's time to continue.
	
	
	switch (g_iTrap) {
		case MODE_USER:
			//Printf("Dunno!!\n");
			break;
		case MODE_FIQ:
			//Printf("Dunno!!\n");
			break;
		case MODE_IRQ:
			//Printf("Dunno!!\n");
			break;
		case MODE_SUPERVISOR:
			//Printf("Dunno!!\n");
			break;
		case MODE_ABORT:
			asm volatile(" \n"
				" mov r10, #0 \n"
				" @Invalidate I&D cache \n"
				" mcr	p15, 0, r10, c7, c7 \n"
				" @ restore the registers \n"
				" ldr	r1,[%0, #0x04] \n"
				" ldr	r2,[%0, #0x08] \n"
				" ldr	r4,[%0, #0x10] \n"
				" ldr	r5,[%0, #0x14] \n"
				" ldr	r6,[%0, #0x18] \n"
				" ldr	r7,[%0, #0x1C] \n"
				" ldr	r8,[%0, #0x20] \n"
				" ldr	r9,[%0, #0x24] \n"
				" ldr	r10,[%0, #0x28] \n"
				" ldr	r11,[%0, #0x2C] \n"
				" ldr	r12,[%0, #0x30] \n"
				" ldr	r14,[%0, #0x40] \n"
				" ldr	r0, [%0, #0x44] \n"
				" msr	CPSR_fsxc, r0 \n"
				" ldr	r0, [%0, #0x48] \n"
				" msr	SPSR_fsxc, r0 \n"

				" ldr	r0,[%0, #0x00] \n"
				" ldr	r3,[%0, #0x0C] \n"
				
				" subs	pc, lr, #0x04 \n"

				" \n"
				:
				:"r" (g_Registers)
				:"r0");
			break;
		case MODE_UNDEF:
			// This will be the breakpoint i'm gonna test with.
			asm volatile(" \n"
				" mov r10, #0 \n"
				" @Invalidate I&D cache \n"
				" mcr	p15, 0, r10, c7, c7 \n"
				" @ restore the registers \n"
				" ldr	r1,[%0, #0x04] \n"
				" ldr	r2,[%0, #0x08] \n"
				" ldr	r4,[%0, #0x10] \n"
				" ldr	r5,[%0, #0x14] \n"
				" ldr	r6,[%0, #0x18] \n"
				" ldr	r7,[%0, #0x1C] \n"
				" ldr	r8,[%0, #0x20] \n"
				" ldr	r9,[%0, #0x24] \n"
				" ldr	r10,[%0, #0x28] \n"
				" ldr	r11,[%0, #0x2C] \n"
				" ldr	r12,[%0, #0x30] \n"
				" ldr	r14,[%0, #0x40] \n"
				" ldr	r0, [%0, #0x44] \n"
				" msr	CPSR_fsxc, r0 \n"
				" ldr	r0, [%0, #0x48] \n"
				" msr	SPSR_fsxc, r0 \n"

				" ldr	r0,[%0, #0x00] \n"
				" ldr	r3,[%0, #0x0C] \n"
				" @ The subbing has already been done! \n"
				" @subs	pc, lr, #0x04 \n"
				" movs	pc, lr \n"
				" \n"
				:
				:"r" (g_Registers)
				:"r0");
			

			break;
		case MODE_SYSTEM:
			//Printf("Dunno!!\n");
			break;
		case MODE_DUNNO:
		default:
			//Printf("Dunno!!\n");
			while(1);
	}


	

}

///////////////////////////////////////////////////////////////
// Returns which exception occured based on CSPR
int GetException(unsigned int CSPR)
{
	switch(CSPR & 0x1f) {
		case 0x10: // USER
			return 0;
			break;
		case 0x11: // FIQ
			return 1;
			break;
		case 0x12: // IRQ
			return 2;
			break;
		case 0x13: // Supervisor
			return 3;
			break;
		case 0x17: // Abort
			return 4;
			break;
		case 0x1B: // Undefined/Breakpoint
			return 5;
			break;
		case 0x1F: // System
			return 6;
			break;
		default:
			return -1;
	}
}


///////////////////////////////////////////////////////////////
// Installs the ISR address into the system RAM
void InstallISR()
{
	int		*pPointer = (int *)(0x0c7ac040);
	int		*pPointer2 = (int *)(0x0c7ac018);

	void (*pISR)();

	pISR = ISR;

	*pPointer = (int)pISR;
	*pPointer2 = (int)pISR;

}

void DEBUG_Print(char *pFormat, ...)
{
	char            Temp[0x100];
	va_list         VaList;
	int				iLength;
	int				iOffset;
	unsigned char	MyChar;

	if (!g_GDBConnected) return;
	
	va_start(VaList , pFormat);
	vsprintf(Temp , pFormat , VaList);
	va_end(VaList);

	iLength = strlen(Temp);
	if (iLength > 100) iLength = 100;

	g_TempBuffer[0] = 'O';
	iOffset = 1;
	for (int i = 0; i < iLength; i++) {
		MyChar = (unsigned char)Temp[i];
		g_TempBuffer[iOffset++] = HexDigits[(MyChar & 0xf0) >> 4];
		g_TempBuffer[iOffset++] = HexDigits[(MyChar & 0x0f)];
	}
	g_TempBuffer[iOffset] = 0;
	SendCommand((unsigned char *)g_TempBuffer);

}


///////////////////////////////////////////////////////////////
// The main thread when the GDB thread has control
void BreakPoint()
{
	unsigned int *pNextInstruction;
	bool	bBreakLoop = false;
	int		iResult;
	int		iMessageLength;
	int		iOffsetAdd;
	int 	iNullVal = 0;
	void	*pAddr;
	int		iOffset;
	int		iBytes;
	

	// Find out if we got here through a STEP command
	if (g_LastWasStep) {
		#ifdef USE_PRINTF
		Printf("I:0x%x 0x%x", *((unsigned int *)(g_Registers[15] + 4)), *((unsigned int *)(g_Registers[15])));
		Printf("S: 0x%x", g_StepAddress);
		#endif
		if ((unsigned int)g_Registers[15] == g_StepAddress) {
			// Yes it was, Lets restore the instruction.
			*((unsigned int *)g_StepAddress) = g_SavedStepInstruction;
			#ifdef USE_PRINTF
			Printf("Restore: 0x%x", g_SavedStepInstruction);
			#endif
		} else {
			while(1);
		}
		g_LastWasStep = false;
	}


	while(!bBreakLoop) {
		iResult = RecvUSB(g_ReadBuffer, 0x100);
		//Printf("%d\n", iResult);
		
		if (iResult > 0) {
			// If we recieve a package we can assume that GDB is connected.. or smth..:D
			g_GDBConnected = true;
			// Well, we have recieved a package, lets print the contents.
			iMessageLength = *(int *)&g_ReadBuffer[0];
			g_ReadBuffer[4 + iMessageLength] = 0;
			//Printf("%s\n %d", &g_ReadBuffer[4], iMessageLength);

			// Let us also send an ACK '+'
			(*(int *)&g_SendBuffer[0]) = 1;
			g_SendBuffer[4] = '+';
			SendUSB((const void *)g_SendBuffer, 0x100);
			WaitACK();
		
			// I can see that i get a bunch of '+' and '-' in the messages.. lets remove them.
			iOffsetAdd = 4;
			while((g_ReadBuffer[iOffsetAdd] == '+') || (g_ReadBuffer[iOffsetAdd] == '-')) iOffsetAdd++;

			// Check whether it's legimit command
			if (g_ReadBuffer[iOffsetAdd] == '$') {
				// Well it is!
				switch(g_ReadBuffer[iOffsetAdd + 1]) {
					case 'H': // Set thread, we're not having any threads.. so.. just return OK
						SendCommand((unsigned char *)"OK");
						break;
					case 'q': // Query, there are some queries, but GDB first asks for Offsets
						switch(g_ReadBuffer[iOffsetAdd + 2]) {
							case 'O': // Offsets
								SendOffsets();
							break;
							case 'C':
								SendCommand((unsigned char *)"QC0000");
								//SendBreakPoint();
								break;

						}
						break;
					case '?':
						// This will have to be modified later to send the correct signal.
						SendBreakPoint();
						break;
					case 'g':
						DumpRegisters();
						break;
					case 'm':
							pAddr = GetAddr(&g_ReadBuffer[iOffsetAdd + 2]);
							iOffset = FindChar(&g_ReadBuffer[iOffsetAdd + 2], ',');
							iBytes = GetBytes(&g_ReadBuffer[iOffsetAdd + 2 + iOffset]);
							SendMemory(pAddr, iBytes);
							break;
					case 'X':	//Write memory binary, which we DON't support.. ofcourse.
						UnSupportedCommand();
						break;
					case 'P':	// Write register
						{
							SendCommand((unsigned char *)"OK");

						}
						break;
					case 'M':	// Write memory not binary
						{
							pAddr = GetAddr(&g_ReadBuffer[iOffsetAdd + 2]);
							iOffset = FindChar(&g_ReadBuffer[iOffsetAdd + 2], ',');
							iBytes = GetBytes(&g_ReadBuffer[iOffsetAdd + 2 + iOffset]);
							iOffset = FindChar(&g_ReadBuffer[iOffsetAdd + 2], ':');
							WriteMemory(pAddr, iBytes, &g_ReadBuffer[iOffsetAdd + 2 + iOffset + 1]);
							SendCommand((unsigned char *)"OK");
	
						}
						break;
					case 'c':	// continue
						{
							return;
							
						}
						break;
					
					case 's': // Stepping.
						{
							// Get the address of the next instruction.
							pNextInstruction = GetNextInstruction((unsigned int *)g_Registers[15]);

							// Read whatsever there.
							g_SavedStepInstruction = *pNextInstruction;
							g_StepAddress = (unsigned int)pNextInstruction;
							g_LastWasStep = true;

							//Printf("Curr: 0x%x", g_Registers[15]);
							#ifdef USE_PRINTF
							Printf("Next: 0x%x->0x%x", g_Registers[15], pNextInstruction);
							#endif
							//Printf("Trap: 0x%x", GetException((unsigned int)g_Registers[40]));
							// Write a breakpoint instruction to the address.
							*pNextInstruction = 0xe7ffdefe;
							return;

						}
						break;
					case 'Z':	// BreakPoint.
						{
							switch(g_ReadBuffer[iOffsetAdd + 2]) {
								case '0':
									// Software breakpoint, i think it's up to me to add, it, lets send OK for now.
									UnSupportedCommand();
									break;
								default:
									// We only support software breakpoints for now, lets return unsupported.
									// Actually we don't even support SW breakpoints now
									UnSupportedCommand();
									break;
							}
						}
						break;
					
					default:
						UnSupportedCommand();
						break;

				}
			}

		}

	}

}

///////////////////////////////////////////////////////////////
// Tries to find the next instruction to be executed after a break
unsigned int *GetNextInstruction(unsigned int *pAddr)
{
	unsigned int	uiInstruction	= *pAddr;
	unsigned int	uiAndVal		= 0;
	unsigned int	iNewPC			= 0;
	int				iNewAddr		= 0;
	int				iRegsbeforePC	= 0;
	unsigned int	uiBaseRegister	= 0;
	unsigned int	uiRegVal		= 0;
	unsigned int	uiData			= 0;
	unsigned int	uiCondMask		= 0;
	int				iPCOffset		= 0;

	unsigned int uiNewPC = DecodeInstruction(uiInstruction, (unsigned int)pAddr);
	return (unsigned int *)uiNewPC;
	
	
	// Set new PC to pAddr + 4, because we really hope that is the case...:D
	iNewPC = (unsigned int)pAddr;
	iNewPC += 4; // Next instruction (atleast in ARM mode, we don't support thumb yet)

	// Now it's a good point to find out if the instruction would be executed anyway.
	uiCondMask = (uiInstruction & 0xf0000000) >> 28;

	if (CondWillExecute(uiCondMask, (unsigned int)g_Registers[18])) {
		//Printf("Condition will execute");
		// Find out if it's a B or BL instruction. (This is the easy one)
		if ((uiInstruction & 0xE000000 ) ==  0xA000000) {
			#ifdef USE_PRINTF
			Printf("0x%x", uiInstruction);
			#endif
			// Okay, it's a branch instruction, lets get the address it's for
			iNewAddr = uiInstruction & 0xffffff;
			// We might need to sign extend this instruction.
			if ((iNewAddr & 0x00800000) != 0) {
				#ifdef USE_PRINTF
				printf("Sign extending");
				#endif
				//iNewAddr *= -1;
				iNewAddr |= 0xff000000;
			}
			#ifdef USE_PRINTF
			Printf("0x%x", iNewAddr);
			#endif
			iNewAddr *= 4; // Instruction size.
			iNewPC = ((int)pAddr + iNewAddr + 8);
		}
	
		// Well, it might be a ldm(ea)?
	
		if ((uiInstruction & 0xE000000) == 0x8000000) {
			#ifdef USE_PRINTF
			Printf("LDM");
			#endif
			// this is a LDM/STM alright.
			if ((uiInstruction & 0x100000) != 0) {
				// This is a LDM instruction
				// Lets see if the PC is ever loaded.
				#ifdef USE_PRINTF
				Printf("includes PC");
				#endif
				if ((uiInstruction & 0x8000) != 0) {
					// Well (damn the PC is loaded)
					for (int i = 0; i < 15; i++) {
						uiAndVal = 1 << i;
						if ((uiInstruction & uiAndVal) != 0) iRegsbeforePC++;
					}
					#ifdef USE_PRINTF
					Printf("%d regs", iRegsbeforePC);
					#endif
					/*
					<mr_spiv> da = fa
					<mr_spiv> ia = fd
					<mr_spiv> db = ea
					<mr_spiv> ib = ed
					*/
					// Lets find out which register is used as base for this operation.
					uiBaseRegister = (uiInstruction & 0xF0000) >> 16;
					uiRegVal = ((unsigned int *)g_Registers)[uiBaseRegister];
					// First, have a look at the U bit.
					if ((uiInstruction & (1 << 23)) != 0) {
						
						// Transfer is made descending
						// Which also means that the PC is closest to the base register i just found out.
						// Lets check the P bit (If i'm supposed to increment before or after.
						
						iPCOffset = iRegsbeforePC * 4;
						if (((uiInstruction) & (1 << 24)) != 0) iPCOffset += 4;
	
					} else {
						// Transfer is done ascending
						// Lets check the P bit (If i'm supposed to decrement before or after.
						if (((uiInstruction) & (1 << 24)) != 0) iPCOffset = -4;
					}
	
	
					iNewPC = *(unsigned int *)((((int)uiRegVal) + iPCOffset) & ~0x03);
				}
			}
		}
	
		// Check if it's a mov pc, Rn
	
	}

	return (unsigned int *)iNewPC;
}

///////////////////////////////////////////////////////////////
// Determines if uiCond will be true with this CSPR
bool CondWillExecute(unsigned int uiCond, unsigned int CSPR)
{

	switch(uiCond) {
		case 0: // EQ
			// This is true if Z is set in CSPR
			if ((CSPR & (1 << 30)) != 0) return true;
			else return false;
			
			break;
		case 1: // NE
			// This should be true if Z is not set.
			if ((CSPR & (1 << 30)) == 0) return true;
			else return false;
			break;
		case 2:	// CS/HS
			//  this one should be true if C is set.
			if ((CSPR & (1 << 29)) != 0) return true;
			else return false;
			break;
		case 3:	// CC/LO
			//  this one should be true if C is clear.
			if ((CSPR & (1 << 29)) == 0) return true;
			else return false;
			break;
		case 4: // MI
			//  this one should be true if N is set
			if ((CSPR & (1 << 31)) != 0) return true;
			else return false;
			break;
		case 5: // PL
			//  this one should be true if N is clear.
			if ((CSPR & (1 << 31)) == 0) return true;
			else return false;
			break;
		case 6: // VS
			//  this one should be true if V is set
			if ((CSPR & (1 << 28)) != 0) return true;
			else return false;
			break;
		case 7:	// VC
			//  this one should be true if V is clear.
			if ((CSPR & (1 << 28)) == 0) return true;
			else return false;
			break;
		case 8: // HI
			// This is true if C and Z is clear
			if (((CSPR & (1 << 30)) == 0) && ((CSPR & (1 << 29)) == 0)) return true;
			else return false;
			break;
		case 9:	// LS
			// C clear OR Z set
			if (((CSPR & (1 << 29)) == 0) || ((CSPR & (1 << 30)) != 0)) return true;
			else return false;
			break;
		case 10: // GE
			// N set AND V set || N clear and V clear
			if ((CSPR & (1 << 31)) == (CSPR & (1 << 28))) return true;
			else return false;
			break;
		case 11: // LT
			// N != V
			if ((CSPR & (1 << 31)) != (CSPR & (1 << 28))) return true;
			else return false;
			break;
		case 12: // GT
			// Z == 0, N == V
			if (((CSPR & (1 << 30)) == 0) && ((CSPR & (1 << 31)) == (CSPR & (1 << 28)))) return true;
			else return false;
			break;
		case 13: // LE
			if (((CSPR & (1 << 30)) == 1) && ((CSPR & (1 << 31)) != (CSPR & (1 << 28)))) return true;
			else return false;
			break;
		case 14: // AL
			return true;
			break;
		default:
			break;
	}
	

}
// I got the idea for this layout from the singlestep.c (found in eCos)
// But i thought the code was a bit tricky to port, and i wanna learn more about this anyway so, i'll just do smth similar to that



typedef struct
{
	unsigned Rm		: 4; // Rm
	unsigned resv2	: 1; // Reserved 2 (0)
	unsigned shift	: 2; // hmm.. dunno actually but probably (LSL, LSR, ASR, ROR )
	unsigned amount	: 5; // Shift amount 0-31
	unsigned Rd		: 4; // Rd
	unsigned Rn		: 4; // Rn
	unsigned s		: 1; //  S-flag
	unsigned opcode	: 4; // Opcode (Mov etc)
	unsigned resv1	: 3; // Reserved 1 (000)
	unsigned cond	: 4; // Condition

} dpisr; // Data Processing Immediate Register Shift
#define DPISR_R1 0
#define DPISR_R2 0
// Example <opcode> Rd, Rm, <shift> amount
typedef struct 
{
	unsigned Rm		: 4; // Rm
	unsigned resv3	: 1; // Reserved 3 (1)
	unsigned shift	: 2; // (LSL, LSR, ASR, ROR )
	unsigned resv2	: 1; // Reserved 2 (0)
	unsigned Rs		: 4; // Rs
	unsigned Rd		: 4; // Rd
	unsigned Rn		: 4; // Rn
	unsigned s		: 1; // S-flag
	unsigned opcode	: 4; // Opcode
	unsigned resv1	: 3; // Reserved 1 (000)
	unsigned cond	: 4; // Condition
} dprrs; // Data Processing Register Register Shift
#define DPRRS_R1	0
#define DPRRS_R2	0
#define DPRRS_R3	1
// Example <opcode> Rd, Rn, Rm <shift> Rs
// This intruction is unpredictable if R15 is one of the used registers anyway.
typedef struct
{
	unsigned immed	: 8; // Immediate value
	unsigned rotate	: 4; // rotate
	unsigned Rd		: 4; // Rd
	unsigned Rn		: 4; // Rn
	unsigned s		: 1; // S-flag
	unsigned opcode	: 4; // Opcode
	unsigned resv1	: 3; // Reserved 1 (001)
	unsigned cond	: 4; // Condition
} dpi; // Data processing immediate
// example add r0, r1, (ror <immed>, <rotate * 2>)
#define DPI_R1	1

typedef struct
{
	unsigned immed	: 12; // Immediate
	unsigned Rd		: 4; // Rd
	unsigned Rn		: 4; // Rn
	unsigned L		: 1; // L-bit (Load/Store)?
	unsigned W		: 1; // W-bit
	unsigned B		: 1; // B-bit
	unsigned U		: 1; // U-bit
	unsigned p		: 1; // P-bit
	unsigned resv1	: 3; // Reserved 1 (010)
	unsigned cond	: 4; // Condition
} lsio; // Load/store immediate offset
// Example ldr Rd, [Rn, #<immed>]
#define LSIO_R1	2


typedef struct
{
	unsigned Rm		: 4; // Rm
	unsigned resv2	: 1; // Reserved 2 (0)
	unsigned shift	: 2; // Shit type (LSL, LSR, ASR, ROR )
	unsigned amount	: 5; // Shift amount (0-31)
	unsigned Rd		: 4; // Rd
	unsigned Rn		: 4; // Rn
	unsigned L		: 1; // L-bit (Load/Store)?
	unsigned W		: 1; // W-bit
	unsigned B		: 1; // B-bit
	unsigned U		: 1; // U-bit
	unsigned p		: 1; // P-bit
	unsigned resv1	: 3; // Reserved 1 (011)
	unsigned cond	: 4; // Condition
} lsro; // Load/Store register offset
// Example ldr Rd, [Rn + Rm lsl 5]
#define LSRO_R1 3
#define LSRO_R2 0


typedef struct
{
	unsigned regs	: 16; // Register mask
	unsigned Rn		: 4; // Rn
	unsigned L		: 1; // L-bit (Load/Store)?
	unsigned W		: 1; // W-bit
	unsigned S		: 1; // B-bit
	unsigned U		: 1; // U-bit
	unsigned p		: 1; // P-bit
	
	unsigned resv1	: 3; // Reserved 1 (100)
	unsigned cond	: 4; // Condition
} lsm; // Load store multiple
// Example: ldm r0, {r1, r2, r3}
#define LSM_R1  4

typedef struct
{
	unsigned offset	: 24; // Branch offset
	unsigned link	: 1; // Link flag
	unsigned resv1	: 3; // Reserved 1 (101)
	unsigned cond	: 4; // Condition
} bl; // Branch with link(optional)
#define BL_R1 5

typedef union {
	dpisr	DPISR;
	dprrs	DPRRS;
	dpi		DPI;
	lsio	LSIO;
	lsro	LSRO;
	lsm		LSM;
	bl		BL;
	unsigned int uiInstruction;
} Instruction;

/*
#define DPISR_R1 0
#define DPISR_R2 0


#define DPRRS_R1	0
#define DPRRS_R2	0
#define DPRRS_R3	1

#define DPI_R1	1

#define LSIO_R1	2

#define LSRO_R1 3
#define LSRO_R2 0

#define LSM_R1  4

#define BL_R1 5
*/

/*
 * Data Processiong Opcode field values
 */
#define OPCODE_MOV  0xD
#define OPCODE_MVN  0xF
#define OPCODE_ADD  0x4
#define OPCODE_ADC  0x5
#define OPCODE_SUB  0x2
#define OPCODE_SBC  0x6
#define OPCODE_RSB  0x3
#define OPCODE_RSC  0x7
#define OPCODE_AND  0x0
#define OPCODE_EOR  0x1
#define OPCODE_ORR  0xC
#define OPCODE_BIC  0xE
#define OPCODE_CMP  0xA
#define OPCODE_CMN  0xB
#define OPCODE_TST  0x8
#define OPCODE_TEQ  0x9

/*
 * Shift field values
 */
#define SHIFT_LSL   0x0
#define SHIFT_LSR   0x1
#define SHIFT_ASR   0x2
#define SHIFT_ROR   0x3
#define SHIFT_RRX   0x3    /* Special case: ROR(0) implies RRX */


unsigned int DecodeDPISR(dpisr Instr, unsigned int PC);
unsigned int DecodeDPRRS(dprrs Instr, unsigned int PC);
unsigned int DecodeDPI(dpi Instr,  unsigned int PC);
unsigned int DecodeLSIO(lsio Instr,  unsigned int PC);
unsigned int DecodeLSRO(lsro Instr,  unsigned int PC);
unsigned int DecodeLSM(lsm Instr,  unsigned int PC);
unsigned int DecodeBL(bl Instr,  unsigned int PC);


///////////////////////////////////////////////////////////////
// 
unsigned int DecodeInstruction(unsigned int uiInstruction, unsigned int PC)
{
	Instruction myInstruction;
	unsigned int uiCondMask;

	uiCondMask = (uiInstruction & 0xf0000000) >> 28;

	// This instruction can do whatever it wants, but if it doesn't execute we don't give a shit.
	if (!CondWillExecute(uiCondMask, (unsigned int)g_Registers[18])) return PC + 4;
	//Printf("CondWillExec");

	myInstruction.uiInstruction = uiInstruction;

	// Start decoding.. phuu

	if ((myInstruction.DPISR.resv1 == DPISR_R1) && (myInstruction.DPISR.resv2 == DPISR_R2)) return DecodeDPISR(myInstruction.DPISR, PC);
	else if ((myInstruction.DPRRS.resv1 == DPRRS_R1) &&
			 (myInstruction.DPRRS.resv2 == DPRRS_R2) &&
			 (myInstruction.DPRRS.resv3 == DPRRS_R3)) return DecodeDPRRS(myInstruction.DPRRS, PC);
	else if ((myInstruction.DPI.resv1 == DPI_R1)) return DecodeDPI(myInstruction.DPI, PC);
	else if ((myInstruction.LSIO.resv1 == LSIO_R1)) return DecodeLSIO(myInstruction.LSIO, PC);
	
	else if ((myInstruction.LSRO.resv1 == LSRO_R1) &&
			 (myInstruction.LSRO.resv2 == LSRO_R2)) return DecodeLSRO(myInstruction.LSRO, PC);
	else if (myInstruction.LSM.resv1 == LSM_R1) return DecodeLSM(myInstruction.LSM, PC);
	else if (myInstruction.BL.resv1 == BL_R1) return  DecodeBL(myInstruction.BL, PC);

	return 0;
}

///////////////////////////////////////////////////////////////
// 
unsigned int LSL(unsigned int uiValue, unsigned int uiSteps)
{
	return uiValue << uiSteps;
}

///////////////////////////////////////////////////////////////
// 
unsigned int LSR(unsigned int uiValue, unsigned int uiSteps)
{
	return uiValue >> uiSteps;
}

///////////////////////////////////////////////////////////////
// 
// This one could be trickier since, i'm nor sure if a signed shift really is a signed shift.
unsigned int ASR(unsigned int uiValue, unsigned int uiSteps)
{
	unsigned int	uiSignMask = 0;

	// Check if it's a negative number
	if (uiValue & 0x80000000) {
		// Yes, damn
		uiSignMask = ((~0) << (32 - uiSteps));
	}

	return ((uiValue >> uiSteps) | uiSignMask);
}

///////////////////////////////////////////////////////////////
// 
unsigned int ROR(unsigned int uiValue, unsigned int uiSteps)
{
	unsigned int uiRetval;

	while(uiSteps-- > 0) {
		if (uiValue & 0x01) {
			uiValue = (uiValue >> 1) | 0x80000000;
		} else {
			uiValue = uiValue >> 1;
		}
	}
	return uiValue;
}


///////////////////////////////////////////////////////////////
// 
unsigned int Shift_Operand(unsigned int Rm, unsigned int amount, unsigned int shift)
{
	unsigned int	uiRegisterValue;
	
	

	
	uiRegisterValue = g_Registers[Rm];
	if (Rm == 0x0f) {
		// Rm is PC, and PC is offseted by 8.
		uiRegisterValue += 8;
	}

	// Determine the shift mode.
	//(LSL, LSR, ASR, ROR )
	switch (shift) {
		case 0: // LSL
			return LSL(uiRegisterValue, amount);
			break;
		case 1: // LSR
			return LSR(uiRegisterValue, amount);
			break;
		case 2: // ASR
			return ASR(uiRegisterValue, amount);
			break;
		case 3: // ROR
			return ROR(uiRegisterValue, amount);
			break;
		
		default:
			break;

	}
	return 0;
}

///////////////////////////////////////////////////////////////
// 
// Example <opcode> Rd, Rm, <shift> amount
unsigned int DecodeDPISR(dpisr Instr, unsigned int uiPC)
{
	unsigned int uiOperand = Shift_Operand(Instr.Rm, Instr.amount, Instr.shift);
	unsigned int uiRnVal = g_Registers[Instr.Rn];


	
	// Only do this i Pc is Rd
	if (Instr.Rd != 0x0f) return uiPC + 4;
	
	// The actual value that PC contains when executing this instruction is the instruction address+8
	if (Instr.Rn == 0x0f) uiRnVal += 8;

	// Check what opcode it is!
	switch (Instr.opcode) {
		case OPCODE_MOV:
			return uiOperand;
		case OPCODE_MVN:
			return ~uiOperand;
		case OPCODE_ADD:
			return uiRnVal + uiOperand; 
		case OPCODE_ADC:
			return uiRnVal + uiOperand + (((g_Registers[18] & (1 << 29))) == 0?0:1);
		case OPCODE_SUB:
			return uiRnVal - uiOperand; 
		case OPCODE_SBC:
			return uiRnVal - uiOperand - (((g_Registers[18] & (1 << 29))) == 0?1:0);
		case OPCODE_RSB:
			return uiOperand - uiRnVal;
		case OPCODE_RSC:
			return uiOperand - uiRnVal - (((g_Registers[18] & (1 << 29))) == 0?1:0);
		case OPCODE_AND:
			return (uiRnVal & uiOperand);
		case OPCODE_EOR:
			return (uiRnVal^uiOperand);
		case OPCODE_ORR:
			return (uiRnVal | uiOperand);
		case OPCODE_BIC:
			return (uiRnVal & ~uiOperand);
		default:
			return 0;
	}
}


///////////////////////////////////////////////////////////////
// 
//dprrs; // Data Processing Register Register Shift
// Example <opcode> Rd, Rn, Rm <shift> Rs
unsigned int DecodeDPRRS(dprrs Instr, unsigned int uiPC)
{
	unsigned int uiRmValue = g_Registers[Instr.Rm];
	unsigned int uiRsValue = g_Registers[Instr.Rs];
	unsigned int uiRnVal = g_Registers[Instr.Rn];
	if ((Instr.Rm = 0x0f)) uiRmValue += 8;


	unsigned int uiOperand = Shift_Operand(uiRmValue, uiRsValue, Instr.shift);

	// Check if destination is PC
	if (Instr.Rd != 0x0f) return uiPC + 4;
	if ((Instr.Rn = 0x0f)) uiRnVal += 8;

	// Check what opcode it is!
	switch (Instr.opcode) {
		case OPCODE_MOV:
			return uiOperand;
		case OPCODE_MVN:
			return ~uiOperand;
		case OPCODE_ADD:
			return uiRnVal + uiOperand; 
		case OPCODE_ADC:
			return uiRnVal + uiOperand + (((g_Registers[18] & (1 << 29))) == 0?0:1);
		case OPCODE_SUB:
			return uiRnVal - uiOperand; 
		case OPCODE_SBC:
			return uiRnVal - uiOperand - (((g_Registers[18] & (1 << 29))) == 0?1:0);
		case OPCODE_RSB:
			return uiOperand - uiRnVal;
		case OPCODE_RSC:
			return uiOperand - uiRnVal - (((g_Registers[18] & (1 << 29))) == 0?1:0);
		case OPCODE_AND:
			return (uiRnVal & uiOperand);
		case OPCODE_EOR:
			return (uiRnVal^uiOperand);
		case OPCODE_ORR:
			return (uiRnVal | uiOperand);
		case OPCODE_BIC:
			return (uiRnVal & ~uiOperand);
		default:
			return 0;
	}

}

///////////////////////////////////////////////////////////////
// 
// dpi; // Data processing immediate
// example add r0, r1, (ror <immed>, <rotate * 2>)
unsigned int DecodeDPI(dpi Instr, unsigned int uiPC)
{
	unsigned int uiOperand = (ROR(Instr.immed, Instr.rotate << 1));
	unsigned int uiRnVal = g_Registers[Instr.Rn];

	// Check if PC is destination
	if (Instr.Rd != 0x0f) return uiPC + 4; // Next instruction
	if ((Instr.Rn = 0x0f)) uiRnVal += 8;

	// Check what opcode it is!
	switch ((Instr.opcode)) {
		case OPCODE_MOV:
			return uiOperand;
		case OPCODE_MVN:
			return ~uiOperand;
		case OPCODE_ADD:
			return uiRnVal + uiOperand; 
		case OPCODE_ADC:
			return uiRnVal + uiOperand + (((g_Registers[18] & (1 << 29))) == 0?0:1);
		case OPCODE_SUB:
			return uiRnVal - uiOperand; 
		case OPCODE_SBC:
			return uiRnVal - uiOperand - (((g_Registers[18] & (1 << 29))) == 0?1:0);
		case OPCODE_RSB:
			return uiOperand - uiRnVal;
		case OPCODE_RSC:
			return uiOperand - uiRnVal - (((g_Registers[18] & (1 << 29))) == 0?1:0);
		case OPCODE_AND:
			return (uiRnVal & uiOperand);
		case OPCODE_EOR:
			return (uiRnVal^uiOperand);
		case OPCODE_ORR:
			return (uiRnVal | uiOperand);
		case OPCODE_BIC:
			return (uiRnVal & ~uiOperand);
		default:
			return 0;
	}

}

///////////////////////////////////////////////////////////////
// 
// lsio; // Load/store immediate offset
// Example ldr Rd, [Rn, #<immed>]
unsigned int DecodeLSIO(lsio Instr, unsigned int uiPC)
{
	unsigned int uiRnValue = g_Registers[Instr.Rn];
	unsigned int uiMemValue;

	// Check if destination is PC
	if (Instr.Rd != 0x0f) return uiPC + 4;
	// Check if it's a LDR instruction
	if (Instr.L != 1) return uiPC + 4;

	if (Instr.Rn == 0x0f) uiRnValue += 8;

	// Check if it's pre-indexed
	if (Instr.p == 1){
	
		if (Instr.U == 1) {
			// Add offset
			uiRnValue += Instr.immed;
		} else {
			// Sub offset
			uiRnValue -= Instr.immed;
		}
	}

	uiMemValue = *(unsigned int *)(uiRnValue);

	return uiMemValue;

}

///////////////////////////////////////////////////////////////
// 
// lsro; // Load/Store register offset
// Example ldr Rd, [Rn + Rm lsl 5]
unsigned int DecodeLSRO(lsro Instr, unsigned int uiPC)
{
	unsigned int uiRnValue = g_Registers[Instr.Rn];
	unsigned int uiRmValue = g_Registers[Instr.Rm];
	unsigned int uiIndex;
	unsigned int uiMemValue;

	if (Instr.Rm == 0x0f) uiRmValue += 8;
	if (Instr.Rn == 0x0f) uiRnValue += 8;

	// Check if destination is PC and that it's LDR instruction
	if ((Instr.Rd != 0x0f) || (Instr.L != 1)) return uiPC + 4;

	uiIndex = Shift_Operand(Instr.Rm, Instr.amount, Instr.shift);

	if (Instr.p == 1){
	
		if (Instr.U == 1) {
			// Add offset
			uiRnValue += uiIndex;
		} else {
			// Sub offset
			uiRnValue -= uiIndex;
		}
	}

	uiMemValue = *(unsigned int *)(uiRnValue);

	return uiMemValue;


}
///////////////////////////////////////////////////////////////
// 
// lsm; // Load store multiple
// Example: ldm r0, {r1, r2, r3}
unsigned int DecodeLSM(lsm Instr, unsigned int uiPC)
{
	unsigned int	uiRnValue = g_Registers[Instr.Rn];
	unsigned int  	uiOffsetToPC = 0;
	unsigned int	uiMemValue;

	// Make sure PC is destination and it's Load instruction
	if (((Instr.regs & (1 << 15)) == 0) || (Instr.L != 1)) return uiPC + 4;


	// Check if U bit it set
	if (Instr.U == 0) {
		// This means that it's ascending
		// Also means that the PC is closest to Rn
		if (Instr.p == 1) {
			// Pre decrement.
			uiOffsetToPC -= 4;
		} else {
			uiOffsetToPC = 0;
		}
	} else {
		// The stack is descending, that means that the PC is as far away as possible.
		// Lets find out how many registers before it.
		for (int i = 0; i < 15; i++) {
			if ((Instr.regs & (1 << i)) != 0) uiOffsetToPC += 4;
		}
	
		// If the P bit is set, it uses pre increment
		if (Instr.p == 1) uiOffsetToPC += 4;
	}

	// read from out calculated address.
	uiMemValue = *(unsigned int *)((uiRnValue + uiOffsetToPC) & ~0x03);

	return uiMemValue;

}

///////////////////////////////////////////////////////////////
// 
// bl; // Branch with link(optional)
unsigned int DecodeBL(bl Instr, unsigned int uiPC)
{
	//Printf("Decode BL");
	unsigned int uiAddress;

	uiAddress = Instr.offset;
	if (uiAddress & 0x00800000) {
		uiAddress |= 0xff000000;
	}

	uiAddress <<= 2;
	uiAddress += 8;

	return uiPC + uiAddress;

}