#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

const char	jump_code[8] = { 0x00, 0x08, 0x28, 0x08, 0x00, 0x00, 0x00, 0x00 };

int main(void)
{
	char	buffer[65536];
	size_t	size;
	FILE	*fp;

	memset(buffer, 0, 65536);

	fp = fopen("stage1.bin", "rb");
	
	if (!fp)
	{
		fprintf(stderr, "Couldn't open stage1.bin !\n");
		return -1;
	}

	fseek(fp, 0, SEEK_END);
	size = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	fread(buffer, 1, size, fp);

	fclose(fp);

	fp = fopen("code.lz", "rb");

	if (!fp)
	{
		fprintf(stderr, "Couldn't open code.lz !\n");
		return -1;
	}

	fseek(fp, 0, SEEK_END);
	size = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	fread(&buffer[ 65536 - size ], 1, size, fp);

	fclose(fp);

	fp = fopen("md.SIM", "r+");

	if (!fp)
	{
		fprintf(stderr, "Couldn't open md.sim !\n");
		return -1;
	}

	fseek(fp, 0xAF0, SEEK_SET);
	fwrite(jump_code, 1, 8, fp);

	fseek(fp, 0x2A50, SEEK_SET);
	fwrite(buffer, 1, 65536, fp);

	fclose(fp);
}
