-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathO2_rom_to_bitmap.cpp
More file actions
117 lines (95 loc) · 3.06 KB
/
Copy pathO2_rom_to_bitmap.cpp
File metadata and controls
117 lines (95 loc) · 3.06 KB
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
/*
* O2_rom_to_bitmap
*
* Converts .bin ROM Odyssey2 file to visual bits drawings.
*
* Gregoire TELLIER (gregoire.tellier@proton.me), may 2024
*
* Feel free to copy, change, criticize, etc ;-)
*
*
* Program uses one or two arguments: rom_file and output_file (optionaly).
*
* ROM can be either 2, 4 or 8 kB
*
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
int main(int argc, char **argv)
{
struct stat cartridge_stat;
uint8_t *cartridge;
uint8_t cartridge_size = 0;
FILE *cartridge_file = NULL;
FILE *output_file = NULL;
bool output_file_flag = false;
if (argc < 2)
{
fprintf(stderr, "%s: error = missing argument\n", argv[0]);
fprintf(stderr, "Syntax: %s <cartridgefile> [resulting_file]\n", argv[0]);
exit(-1);
}
if (stat(argv[1], &cartridge_stat) == -1)
{
fprintf(stderr, "%s: error while accessing file %\n", argv[0], argv[1]);
exit(EXIT_FAILURE);
}
cartridge_size = cartridge_stat.st_size / 1024;
printf("Cartridge ROM is %d kB\n", cartridge_size);
cartridge_file = fopen(argv[1], "r");
if (cartridge_file == NULL)
{
fprintf(stderr, "%s: error while opening file %\ns",
argv[0], argv[1]);
exit(EXIT_FAILURE);
}
if (argc > 2)
{
output_file_flag = true;
output_file = fopen(argv[2], "w");
if (output_file == NULL)
{
fprintf(stderr, "%s: error while creating file %\n", argv[0], argv[2]);
exit(EXIT_FAILURE);
}
}
cartridge = (uint8_t *)malloc(cartridge_size * 1024);
if (cartridge == NULL)
{
fprintf(stderr, "%s: memory allocation error\n", argv[0]);
exit(EXIT_FAILURE);
}
memset(cartridge, 0, cartridge_size * 1024);
fread(cartridge, 1024, cartridge_size, cartridge_file);
fclose(cartridge_file);
fprintf(stderr, "%s: processing file %s (cartridge ROM)\n", argv[0], argv[1]);
if (!output_file_flag)
output_file = stdout;
fprintf(output_file, "//\n");
fprintf(output_file, "// %s: processing file %s (cartridge ROM), size %d kB\n", argv[0], argv[1], cartridge_size);
fprintf(output_file, "//\n");
for (int i = 0; i < cartridge_size * 1024; i++)
{
uint8_t mask = 1;
if (i % 8 == 0)
fprintf(output_file, "\n");
fprintf(output_file, "Page %d\t0x%02x (0x%04X)\t0x%02X\t", ((i / 256) % 8) + 1, i % 256, i, cartridge[i]);
for (uint8_t j = 0; j < 8; j++)
{
if (cartridge[i] & mask)
printf("OO");
else
printf(" ");
mask <<= 1;
}
printf("\n");
}
printf("//\n");
printf("// %s: end of file %s\n", argv[0], argv[1]);
printf("//\n");
exit(0);
}