Skip to content

Instantly share code, notes, and snippets.

@raybellis
Created March 28, 2018 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raybellis/76faf71aeac985a358fdf1c1af2898ec to your computer and use it in GitHub Desktop.
Save raybellis/76faf71aeac985a358fdf1c1af2898ec to your computer and use it in GitHub Desktop.
16-bit ROM merger
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stddef.h>
int main(int argc, char *argv[])
{
const size_t bufsize = 4096;
uint8_t buf_a[bufsize];
uint8_t buf_b[bufsize];
uint8_t buf_c[bufsize * 2];
if (argc != 4) {
fprintf(stderr, "usage: merged <msb_file> <lsb_file> <out_file>\n");
return EXIT_FAILURE;
}
int a = open(argv[1], O_RDONLY);
if (a < 0) {
fprintf(stderr, "couldn't open MSB file\n");
return EXIT_FAILURE;
}
int b = open(argv[2], O_RDONLY);
if (b < 0) {
fprintf(stderr, "couldn't open LSB file\n");
return EXIT_FAILURE;
}
int c = open(argv[3], O_RDWR | O_CREAT, 0644);
if (c < 0) {
fprintf(stderr, "couldn't open output file\n");
return EXIT_FAILURE;
}
while (1) {
ssize_t n_a, n_b;
if ((n_a = read(a, &buf_a, bufsize)) <= 0) break;
if ((n_b = read(b, &buf_b, bufsize)) != n_a) break;
uint8_t *p = buf_c;
for (int i = 0; i < n_a; ++i) {
*p++ = buf_a[i];
*p++ = buf_b[i];
}
write(c, &buf_c, bufsize * 2);
}
close(a);
close(b);
close(c);
}
@raybellis
Copy link
Author

This program can re-combine the separate 8-bit ROMs found in some old 16-bit gear into a single file for subsequent analysis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment