-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.c
149 lines (122 loc) · 3.15 KB
/
data.c
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
#include <stdio.h>
#include <stdlib.h>
#include "../foenixLibrary/fatfs/ff.h"
#include "data.h"
#include "header.h"
#define SWAP16(x) (x>>8) | (x<<8)
unsigned short data_highmemAddress;
BYTE *zData;
//FILE *zFile;
FATFS FatFs; /* Work area (filesystem object) for logical drive */
FIL zFile; /* File object */
void data_initialise(unsigned short highmemAddress, char *filename)
{
FRESULT fr;
UINT bytesRead, bw; /* File read/write count */
data_highmemAddress = highmemAddress;
// mount SD-card
f_mount(&FatFs, "1:", 0);
//load dynamic data to memory
//zFile = fopen(filename, "rb");
fr = f_open(&zFile, filename, FA_READ);
if (fr)
{
printf("Error loading data, %d", (int)fr);
}
/*
char cwd[2048];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working dir: %s\n", cwd);
}
struct stat fileStat;
if (fstat(fileno(fp),&fileStat) < 0)
return 1;
printf("File Size: \t\t%d bytes\n",fileStat.st_size);
/* size array for data archive */
// allocate memory to contain the whole file:
//zorkData = (u_int8_t*) malloc (sizeof(u_int8_t) * fileStat.st_size);
zData = (BYTE *)malloc(highmemAddress);
if (zData == NULL) {fputs ("Memory error\n",stderr); exit (2);}
/* make sure we are at the start of the file */
f_lseek(&zFile, 0);
/* read data archive into array */
f_read(&zFile, zData, highmemAddress, &bytesRead);
if (bytesRead == highmemAddress)
printf("File read succesfully\n");
else
{
printf("I could only read %d of %d bytes.\n", bytesRead, sizeof(struct header));
exit(1);
}
}
void data_close()
{
f_close(&zFile);
f_mount(0, "1:", 0);
}
short data_loadWord(ushort address)
{
short value = 0;
UINT bytesRead = 0;
if (address > data_highmemAddress)
{
//load from disk
f_lseek(&zFile, address);
f_read(&zFile, &value, 2, &bytesRead);
//value = ntohs(value);
asm { lda %%value; xba; sta %%value; }
return value;
}
else
{
//load from memory
return (zData[address] << 8) + zData[address + 1];
}
}
void data_saveWord(ushort address, short value)
{
if (address > data_highmemAddress)
{
//save to disk
printf("should not happen, on disk part is read only!");
exit(1);
}
else
{
//save to from memory
zData[address] = value >> 8;
zData[address + 1] = (value & 0xFF);
}
}
byte data_loadByte(ushort address)
{
byte value = 0;
UINT bytesRead = 0;
if (address > data_highmemAddress)
{
//load from disk
f_lseek(&zFile, address);
f_read(&zFile, &value, 1, &bytesRead);
return value;
}
else
{
//printf("\nmem address: %d",address);
//load from memory
return zData[address];
}
}
void data_saveByte(ushort address, byte value)
{
if (address > data_highmemAddress)
{
//save to disk
printf("should not happen, on disk part is read only!");
exit(1);
}
else
{
//save to memory
zData[address] = value;
}
}