forked from nop00/pceas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nes.c
285 lines (228 loc) · 4.48 KB
/
nes.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
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
#include <stdio.h>
#include <string.h>
#include "defs.h"
#include "externs.h"
#include "protos.h"
#include "nes.h"
/* locals */
static int ines_prg; /* number of prg banks */
static int ines_chr; /* number of character banks */
static int ines_mapper[2]; /* rom mapper type */
static struct INES { /* INES rom header */
unsigned char id[4];
unsigned char prg;
unsigned char chr;
unsigned char mapper[2];
unsigned char unused[8];
} header;
/* ----
* write_header()
* ----
* generate and write rom header
*/
void
nes_write_header(FILE *f, int banks)
{
(void)banks;
/* setup INES header */
memset(&header, 0, sizeof(header));
header.id[0] = 'N';
header.id[1] = 'E';
header.id[2] = 'S';
header.id[3] = 26;
header.prg = ines_prg;
header.chr = ines_chr;
header.mapper[0] = ines_mapper[0];
header.mapper[1] = ines_mapper[1];
/* write */
fwrite(&header, sizeof(header), 1, f);
}
/* ----
* pack_8x8_tile()
* ----
* encode a 8x8 tile for the NES
*/
int
nes_pack_8x8_tile(unsigned char *buffer, void *data, int line_offset, int format)
{
int i, j;
int cnt, err;
unsigned int pixel;
unsigned char *ptr;
unsigned int *packed;
/* pack the tile only in the last pass */
if (pass != LAST_PASS)
return (16);
/* clear buffer */
memset(buffer, 0, 16);
/* encode the tile */
switch (format) {
case CHUNKY_TILE:
/* 8-bit chunky format */
cnt = 0;
ptr = data;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
pixel = ptr[j ^ 0x07];
buffer[cnt] |= (pixel & 0x01) ? (1 << j) : 0;
buffer[cnt+8] |= (pixel & 0x02) ? (1 << j) : 0;
}
ptr += line_offset;
cnt += 1;
}
break;
case PACKED_TILE:
/* 4-bit packed format */
cnt = 0;
err = 0;
packed = data;
for (i = 0; i < 8; i++) {
pixel = packed[i];
for (j = 0; j < 8; j++) {
/* check for errors */
if (pixel & 0x0C)
err++;
/* convert the tile */
buffer[cnt] |= (pixel & 0x01) ? (1 << j) : 0;
buffer[cnt+8] |= (pixel & 0x02) ? (1 << j) : 0;
pixel >>= 4;
}
cnt += 1;
}
/* error message */
if (err)
error("Incorrect pixel color index!");
break;
default:
/* other formats not supported */
error("Internal error: unsupported format passed to 'pack_8x8_tile'!");
break;
}
/* ok */
return (16);
}
/* ----
* do_defchr()
* ----
* .defchr pseudo
*/
void
nes_defchr(int *ip)
{
unsigned char buffer[16];
unsigned int data[8];
int size;
int i;
/* define label */
labldef(loccnt, 1);
/* output infos */
data_loccnt = loccnt;
data_size = 3;
data_level = 3;
/* get tile data */
for (i = 0; i < 8; i++) {
/* get value */
if (!evaluate(ip, (i < 7) ? ',' : ';'))
return;
/* store value */
data[i] = value;
}
/* encode tile */
size = nes_pack_8x8_tile(buffer, data, 0, PACKED_TILE);
/* store tile */
putbuffer(buffer, size);
/* output line */
if (pass == LAST_PASS)
println();
}
/* ----
* do_inesprg()
* ----
* .inesprg pseudo
*/
void
nes_inesprg(int *ip)
{
if (!evaluate(ip, ';'))
return;
int svalue = (int)value;
if ((svalue < 0) || (svalue > 64))
{
error("Prg bank value out of range!");
return;
}
ines_prg = value;
if (pass == LAST_PASS)
{
println();
}
}
/* ----
* do_ineschr()
* ----
* .ineschr pseudo
*/
void
nes_ineschr(int *ip)
{
if (!evaluate(ip, ';'))
return;
int svalue = (int)value;
if ((svalue < 0) || (svalue > 64))
{
error("Prg bank value out of range!");
return;
}
ines_chr = value;
if (pass == LAST_PASS)
{
println();
}
}
/* ----
* do_inesmap()
* ----
* .inesmap pseudo
*/
void
nes_inesmap(int *ip)
{
if (!evaluate(ip, ';'))
return;
int svalue = (int)value;
if ((svalue < 0) || (svalue > 255))
{
error("Mapper value out of range!");
return;
}
ines_mapper[0] &= 0x0F;
ines_mapper[0] |= (value & 0x0F) << 4;
ines_mapper[1] = (value & 0xF0);
if (pass == LAST_PASS)
{
println();
}
}
/* ----
* do_inesmir()
* ----
* .ines.mirror pseudo
*/
void
nes_inesmir(int *ip)
{
if (!evaluate(ip, ';'))
return;
int svalue = (int)value;
if ((svalue < 0) || (svalue > 15))
{
error("Mirror value out of range!");
return;
}
ines_mapper[0] &= 0xF0;
ines_mapper[0] |= (value & 0x0F);
if (pass == LAST_PASS)
{
println();
}
}