-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen-code.c
435 lines (346 loc) · 11 KB
/
gen-code.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
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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <png.h>
typedef struct {
int rows;
int cols;
int map[200][320];
} Image;
Image* readPNGImage(char* filename) {
Image *image = malloc(sizeof(Image));
png_byte color_type;
png_bytep * row_pointers;
png_structp png_ptr;
png_infop info_ptr;
int i, j;
// 8 is the maximum size that can be checked
char header[8];
/* open file and test for it being a png */
FILE *fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "[read_png_file] File %s could not be opened for reading", filename);
return NULL;
}
if (fread(header, 1, 8, fp) != 8){
fprintf(stderr, "[read_png_file] File %s is not is too short", filename);
return NULL;
}
if (png_sig_cmp(header, 0, 8)){
fprintf(stderr, "[read_png_file] File %s is not recognized as a PNG file", filename);
return NULL;
}
/* initialize stuff */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr){
fprintf(stderr, "[read_png_file] png_create_read_struct failed");
return NULL;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr){
fprintf(stderr, "[read_png_file] png_create_info_struct failed");
return NULL;
}
if (setjmp(png_jmpbuf(png_ptr))){
fprintf(stderr, "[read_png_file] Error during init_io");
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
image->cols = png_get_image_width(png_ptr, info_ptr);
image->rows = png_get_image_height(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
if(color_type != PNG_COLOR_TYPE_RGB){
fprintf(stderr, "[read_png_file] Only RGB PNGs are supported");
return NULL;
}
png_read_update_info(png_ptr, info_ptr);
/* read file */
if (setjmp(png_jmpbuf(png_ptr))){
fprintf(stderr, "[read_png_file] Error during read_image");
return NULL;
}
/* memory allocation */
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * image->rows);
for (i = 0; i < image->rows; i += 1){
row_pointers[i] = (png_byte*) malloc(png_get_rowbytes(png_ptr, info_ptr));
}
png_read_image(png_ptr, row_pointers);
fclose(fp);
for (i = 0; i < image->rows; i += 1) {
png_byte* row = row_pointers[i];
for (j = 0; j < image->cols; j += 1) {
png_byte* ptr = &(row[j * 3]);
image->map[i][j] = ptr[0];
}
}
/* clean up */
for (i = 0; i < image->rows; i += 1){
free(row_pointers[i]);
}
free(row_pointers);
return image;
}
unsigned char code[2048];
int offset;
void assert_bytes(FILE *fp, int bytes) {
int left = (sizeof(code) - 1) - (offset + 3);
if (left >= bytes) {
return;
}
while (left >= 2) {
code[offset++] = 0x80; // BRA rel
code[offset++] = 0x00;
left -= 2;
}
while (left >= 1) {
code[offset++] = 0xEA; // NOP
left--;
}
code[offset++] = 0x4C; // JMP abs
code[offset++] = 0x00;
code[offset++] = 0xC8;
fwrite(code, sizeof(code), 1, fp);
memset(code, 0, sizeof(code));
offset = 0;
}
void set_addr(FILE *fp, int addr) {
assert_bytes(fp, 2);
code[offset++] = 0xA9; // LDA imm
code[offset++] = addr;
assert_bytes(fp, 3);
code[offset++] = 0x8D; // STA abs
code[offset++] = 0x3E;
code[offset++] = 0xC0;
assert_bytes(fp, 2);
code[offset++] = 0xA9; // LDA imm
code[offset++] = addr>>8;
assert_bytes(fp, 3);
code[offset++] = 0x8D; // STA abs
code[offset++] = 0x3F;
code[offset++] = 0xC0;
}
long put_raw(FILE *fp, FILE *raw)
{
static long size = 0;
static int addr[2] = {0x0000, 0x8000};
unsigned char buffer[877][2];
if (fread(buffer, sizeof(buffer), 1, raw) != 1) {
return size;
}
for (int c = 0; c < 2; c++) {
set_addr(fp, addr[c]);
for (int i = 0; i < 877; i++) {
int val = buffer[i][c];
if (val == 0x00) {
val = 0x01;
}
assert_bytes(fp, 2);
code[offset++] = 0xA9; // LDA imm
code[offset++] = val;
assert_bytes(fp, 3);
code[offset++] = 0x8D; // STA abs
code[offset++] = 0x3D;
code[offset++] = 0xC0;
size++;
addr[c]++;
if (addr[c] % 0x8000 == 0x0000) {
addr[c] -= 0x8000;
if (i < 876) {
set_addr(fp, addr[c]);
}
}
}
}
return size;
}
int main(int argc, char **argv) {
char name[100];
Image *image[2];
int img = 1;
static int mod_stat[370];
long total_mod = 0;
int max_mod = 0;
int max_mod_img = 0;
FILE *fp = fopen("Bad-Apple-IIgs.code", "wb");
FILE *raw = fopen("data/bad-apple.raw", "rb");
while (put_raw(fp, raw) < 60000) {
}
////////////////////////////////////////////// -> DOC
assert_bytes(fp, 2);
code[offset++] = 0xA9; // LDA imm
code[offset++] = 0b00100000; // DOC regs, auto-inc
assert_bytes(fp, 3);
code[offset++] = 0x8D; // STA abs
code[offset++] = 0x3C;
code[offset++] = 0xC0;
////////////////////////////////////////////// OSC 1 -> Sync
assert_bytes(fp, 2);
code[offset++] = 0xA9; // LDA imm
code[offset++] = 0xA1; // Ctrl, OSC 1
assert_bytes(fp, 3);
code[offset++] = 0x8D; // STA abs
code[offset++] = 0x3E;
code[offset++] = 0xC0;
assert_bytes(fp, 2);
code[offset++] = 0xA9; // LDA imm
code[offset++] = 0b00000100; // Right, no IRQ, sync, go
assert_bytes(fp, 3);
code[offset++] = 0x8D; // STA abs
code[offset++] = 0x3D;
code[offset++] = 0xC0;
////////////////////////////////////////////// OSC 0 -> Free Run
assert_bytes(fp, 2);
code[offset++] = 0xA9; // LDA imm
code[offset++] = 0xA0; // Ctrl, OSC 0
assert_bytes(fp, 3);
code[offset++] = 0x8D; // STA abs
code[offset++] = 0x3E;
code[offset++] = 0xC0;
assert_bytes(fp, 2);
code[offset++] = 0xA9; // LDA imm
code[offset++] = 0b00010000; // Left, no IRQ, free run, go
assert_bytes(fp, 3);
code[offset++] = 0x8D; // STA abs
code[offset++] = 0x3D;
code[offset++] = 0xC0;
////////////////////////////////////////////// OSC 1 -> Free Run
assert_bytes(fp, 2);
code[offset++] = 0xA9; // LDA imm
code[offset++] = 0b00000000; // Right, no IRQ, free run, go
assert_bytes(fp, 3);
code[offset++] = 0x8D; // STA abs
code[offset++] = 0x3D;
code[offset++] = 0xC0;
////////////////////////////////////////////// -> RAM
assert_bytes(fp, 4);
code[offset++] = 0xAF; // LDA long
code[offset++] = 0xCA;
code[offset++] = 0x00;
code[offset++] = 0xE1;
assert_bytes(fp, 2);
code[offset++] = 0x29; // AND imm
code[offset++] = 0b00001111; // System volume
assert_bytes(fp, 2);
code[offset++] = 0x09; // ORA imm
code[offset++] = 0b01100000; // DOC RAM, auto-inc
assert_bytes(fp, 3);
code[offset++] = 0x8D; // STA abs
code[offset++] = 0x3C;
code[offset++] = 0xC0;
sprintf(name, "data/bad-apple-%d.png", img);
image[0] = readPNGImage(name);
if (!image[0] || image[0]->cols != 320 || image[0]->rows != 200) {
return 1;
}
img++;
while (img < 6556) {
put_raw(fp, raw);
sprintf(name, "data/bad-apple-%d.png", img);
image[1] = readPNGImage(name);
if (!image[1] || image[1]->cols != 320 || image[1]->rows != 200) {
return 1;
}
int mod = 0;
for (int y = 0; y < 200; y++) {
int edge[2][320];
for (int i = 0; i < 2; i++) {
edge[i][0] = image[i]->map[y][0] ? 1 : -1;
for (int x = 1; x < 320; x++) {
if (image[i]->map[y][x] == image[i]->map[y][x-1]) {
edge[i][x] = 0;
}
if (image[i]->map[y][x] > image[i]->map[y][x-1]) {
edge[i][x] = 1;
}
if (image[i]->map[y][x] < image[i]->map[y][x-1]) {
edge[i][x] = -1;
}
}
}
for (int x = 0; x < 320; x += 2) {
if (edge[1][x] != edge[0][x] ||
edge[1][x+1] != edge[0][x+1]) {
int val = 0;
switch (edge[1][x]) {
case 0:
val |= 0x00;
break;
case -1:
val |= 0x10;
break;
case 1:
val |= 0x20;
break;
}
switch (edge[1][x+1]) {
case 0:
val |= 0x00;
break;
case -1:
val |= 0x01;
break;
case 1:
val |= 0x02;
break;
}
assert_bytes(fp, 2);
code[offset++] = 0xA9; // LDA imm
code[offset++] = val;
assert_bytes(fp, 3);
int addr = 0x2000 + y * (320/2) + x/2;
code[offset++] = 0x8D; // STA abs
code[offset++] = addr;
code[offset++] = addr>>8;
mod++;
}
}
}
if (mod < 1200) {
assert_bytes(fp, 2);
code[offset++] = 0xA9; // LDA imm
code[offset++] = 0x30;
assert_bytes(fp, 3);
code[offset++] = 0x20; // JSR
code[offset++] = 0xA8;
code[offset++] = 0xFC;
}
assert_bytes(fp, 5);
code[offset++] = 0x2C; // BIT abs
code[offset++] = 0x19;
code[offset++] = 0xC0;
code[offset++] = 0x10; // BPL
code[offset++] = 0xFB;
assert_bytes(fp, 5);
code[offset++] = 0x2C; // BIT abs
code[offset++] = 0x19;
code[offset++] = 0xC0;
code[offset++] = 0x30; // BMI
code[offset++] = 0xFB;
if (mod > 2370) {
printf("mod %d: %d\n", img, mod);
}
mod_stat[mod / 10]++;
total_mod += mod;
if (mod > max_mod) {
max_mod = mod;
max_mod_img = img;
}
free(image[0]);
image[0] = image[1];
img++;
}
assert_bytes(fp, 2);
code[offset++] = 0x80; // BRA
code[offset++] = 0xFE;
fwrite(code, sizeof(code), 1, fp);
fclose(fp);
for (int i = 0; i < 370; i++) {
printf("%04d: %d\n", (i + 1) * 10, mod_stat[i]);
}
printf("total mod %ld\n", total_mod);
printf("max mod %d: %d\n", max_mod_img, max_mod);
return 0;
}