-
Notifications
You must be signed in to change notification settings - Fork 10
/
Eeprom.cpp
135 lines (122 loc) · 3.82 KB
/
Eeprom.cpp
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
/*
* Project 64 - A Nintendo 64 emulator.
*
* (c) Copyright 2001 zilmar ([email protected]) and
* Jabo ([email protected]).
*
* pj64 homepage: www.pj64.net
*
* Permission to use, copy, modify and distribute Project64 in both binary and
* source form, for non-commercial purposes, is hereby granted without fee,
* providing that this license information and copyright notice appear with
* all copies and any derived work.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event shall the authors be held liable for any damages
* arising from the use of this software.
*
* Project64 is freeware for PERSONAL USE only. Commercial users should
* seek permission of the copyright holders first. Commercial use includes
* charging money for Project64 or software derived from Project64.
*
* The copyright holders request that bug fixes and improvements to the code
* should be forwarded to them so if they want them.
*
*/
#include <windows.h>
#include <stdio.h>
#include "main.h"
#include "CPU.h"
static HANDLE hEepromFile = NULL;
BYTE EEPROM[0x800];
void CloseEeprom (void) {
if (hEepromFile) {
CloseHandle(hEepromFile);
hEepromFile = NULL;
}
}
void EepromCommand ( BYTE * Command) {
if (SaveUsing == Auto) { SaveUsing = Eeprom_4K; }
switch (Command[2]) {
case 0: // check
if (SaveUsing != Eeprom_4K && SaveUsing != Eeprom_16K) {
Command[1] |= 0x80;
break;
}
if (Command[1] != 3) {
Command[1] |= 0x40;
if ((Command[1] & 3) > 0) { Command[3] = 0x00; }
if (SaveUsing == Eeprom_4K) {
if ((Command[1] & 3) > 1) { Command[4] = 0x80; }
} else {
if ((Command[1] & 3) > 1) { Command[4] = 0xC0; }
}
if ((Command[1] & 3) > 2) { Command[5] = 0x00; }
} else {
Command[3] = 0x00;
Command[4] = SaveUsing == Eeprom_4K?0x80:0xC0;
Command[5] = 0x00;
}
break;
case 4: // Read from Eeprom
#ifndef EXTERNAL_RELEASE
if (Command[0] != 2) { DisplayError("What am I meant to do with this Eeprom Command"); }
if (Command[1] != 8) { DisplayError("What am I meant to do with this Eeprom Command"); }
#endif
ReadFromEeprom(&Command[4],Command[3]);
break;
case 5:
#ifndef EXTERNAL_RELEASE
if (Command[0] != 10) { DisplayError("What am I meant to do with this Eeprom Command"); }
if (Command[1] != 1) { DisplayError("What am I meant to do with this Eeprom Command"); }
#endif
WriteToEeprom(&Command[4],Command[3]);
break;
default:
if (ShowPifRamErrors) { DisplayError("Unkown EepromCommand %d",Command[2]); }
}
}
void LoadEeprom (void) {
char File[255], Directory[255];
DWORD dwRead;
GetAutoSaveDir(Directory);
sprintf(File,"%s%s.eep",Directory,RomName);
hEepromFile = CreateFile(File,GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ,NULL,OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
if (hEepromFile == INVALID_HANDLE_VALUE) {
switch (GetLastError()) {
case ERROR_PATH_NOT_FOUND:
CreateDirectory(Directory,NULL);
hEepromFile = CreateFile(File,GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ,
NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
if (hEepromFile == INVALID_HANDLE_VALUE) {
DisplayError(GS(MSG_FAIL_OPEN_EEPROM));
}
return;
break;
default:
DisplayError(GS(MSG_FAIL_OPEN_EEPROM));
return;
}
}
memset(EEPROM,0,sizeof(EEPROM));
SetFilePointer(hEepromFile,0,NULL,FILE_BEGIN);
ReadFile(hEepromFile,EEPROM,sizeof(EEPROM),&dwRead,NULL);
}
void ReadFromEeprom(BYTE * Buffer, int line) {
int i;
if (hEepromFile == NULL) {
LoadEeprom();
}
for(i=0;i<8;i++) { Buffer[i]=EEPROM[line*8+i]; }
}
void WriteToEeprom(BYTE * Buffer, int line) {
DWORD dwWritten;
int i;
if (hEepromFile == NULL) {
LoadEeprom();
}
for(i=0;i<8;i++) { EEPROM[line*8+i]=Buffer[i]; }
SetFilePointer(hEepromFile,line*8,NULL,FILE_BEGIN);
WriteFile( hEepromFile,Buffer,8,&dwWritten,NULL );
}