Core commit. Compile and run on the OpenPandora
[mupen64plus-pandora.git] / source / mupen64plus-core / src / memory / n64_cic_nus_6105.c
1 /*
2  * Copyright 2011 X-Scale. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without 
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *    1. Redistributions of source code must retain the above copyright notice,
8  *       this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright 
10  *       notice, this list of conditions and the following disclaimer in the 
11  *       documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY X-Scale ``AS IS'' AND ANY EXPRESS OR IMPLIED 
14  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
16  * EVENT SHALL X-Scale OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
19  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
20  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
22  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are 
25  * those of the authors and should not be interpreted as representing official 
26  * policies, either expressed or implied, of X-Scale.
27  *
28  * This software provides an algorithm that emulates the protection scheme of 
29  * N64 PIF/CIC-NUS-6105, by determining the proper response to each challenge. 
30  * It was synthesized after a careful, exhaustive and detailed analysis of the 
31  * challenge/response pairs stored in the 'pif2.dat' file from Project 64. 
32  * These challenge/response pairs were the only resource used during this 
33  * project. There was no kind of physical access to N64 hardware.
34  *
35  * This project would have never been possible without the contribuitions of 
36  * the following individuals and organizations:
37  *
38  * - Oman: For being at the right place at the right time and being brave 
39  *       enough to pay a personal price so we could understand in a much deeper
40  *       way how this magical console really works. We owe you so much.
41  *
42  * - Jovis: For all the positive energy and impressive hacking spirit that you
43  *       shared with the N64 community. You were absolutely instrumental in 
44  *       several key events that shaped the N64 community in the last 14 years.
45  *       Even if you're not physically with us anymore, your heritage, your 
46  *       knowledge and your attitude will never be forgotten. 
47  *
48  *          'The candle that burns twice as bright burns half as long.'
49  *
50  * - LaC: For the endless contributions that you've given to the N64 community 
51  *       since the early days, when N64 was the next big thing. I've always 
52  *       admired the deep knowledge that you've gathered about the most little 
53  *       hardware details. Recently, you challanged us to find a small and 
54  *       concise algorithm that would emulate the behaviour of CIC-NUS-6105
55  *       challenge/response protection scheme and here is the final result. 
56  *       LaC, Oman and Jovis were definitly the dream team of N64 reversing in 
57  *       the late 90's. Without your contributions, we would be much poorer.
58  *
59  * - marshall: For keeping the N64 scene alive during the last decade, when 
60  *       most people lost interest and moved along to different projects. You 
61  *       are the force that has been keeping us all together in the later 
62  *       years. When almost nobody cared about N64 anymore, you were always 
63  *       there, spreading the word, developing in the console, and lately, 
64  *       making impressive advances on the hardware side. I wish the best 
65  *       success to your new 64drive project. 
66  *
67  * - hcs: For your contributions to the better understanding of the inner 
68  *       workings of the Reality Co-Processor (RCP). Your skills have impressed
69  *       me for a long time now. And without your precious help by sharing your
70  *       kownledge, I would have never understood the immense importance of 
71  *       Oman, Jovis and LaC achievements. Thank you !
72  *
73  * - Azimer & Tooie: For sharing with the N64 community your findings about the
74  *       challenge/response pair used in 'Jet Force Gemini' and the 267 
75  *       challenge/response pairs used in 'Banjo Tooie', all stored in the 
76  *       'pif2.dat' file of Project 64. They were instrumental to the final
77  *       success of this endeavour.
78  *
79  * - Silicon Graphics, Inc. (SGI): For creating MIPS R4000, MIPS R4300 and 
80  *       Reality Co-Processor (RCP). You were the ultimate dream creator during
81  *       the late 80's and early 90's. A very special word of gratitude goes to
82  *       the two teams that during those years created RCP and MIPS R4300. They
83  *       were technological breakthroughs back then.
84  * 
85  * On a personal note, I would like to show my deepest gratitude to _Bijou_, 
86  * for being always a source of endless hope and inspiration. 
87  *
88  *   -= X-Scale =- (#n64dev@EFnet)
89  */
90
91 #include "n64_cic_nus_6105.h"
92
93 void n64_cic_nus_6105(char chl[], char rsp[], int len)
94 {
95     static char lut0[0x10] = {
96         0x4, 0x7, 0xA, 0x7, 0xE, 0x5, 0xE, 0x1, 
97         0xC, 0xF, 0x8, 0xF, 0x6, 0x3, 0x6, 0x9
98     };
99     static char lut1[0x10] = {
100         0x4, 0x1, 0xA, 0x7, 0xE, 0x5, 0xE, 0x1, 
101         0xC, 0x9, 0x8, 0x5, 0x6, 0x3, 0xC, 0x9
102     };
103     char key, *lut;
104     int i, sgn, mag, mod;
105         
106     for (key = 0xB, lut = lut0, i = 0; i < len; i++) {
107         rsp[i] = (key + 5 * chl[i]) & 0xF;
108         key = lut[(int) rsp[i]];
109         sgn = (rsp[i] >> 3) & 0x1;
110         mag = ((sgn == 1) ? ~rsp[i] : rsp[i]) & 0x7;
111         mod = (mag % 3 == 1) ? sgn : 1 - sgn;
112         if (lut == lut1 && (rsp[i] == 0x1 || rsp[i] == 0x9))
113             mod = 1;
114         if (lut == lut1 && (rsp[i] == 0xB || rsp[i] == 0xE))
115             mod = 0;
116         lut = (mod == 1) ? lut1 : lut0;
117     }
118 }
119