c8/srcs/memory.c

54 lines
No EOL
1,016 B
C

#include "memory.h"
#include <stdio.h>
#include <string.h>
#include "font.h"
#include "libft.h"
void memory_init(t_memory *mem)
{
ft_memset(mem->memory, 0, MEMORY_SIZE);
}
int memory_load_rom(t_memory *mem, FILE *fp)
{
fseek(fp, 0, SEEK_END);
size_t rom_size = ftell(fp);
rewind(fp);
if (rom_size > (sizeof(mem->memory) - 0x200))
{
return (1);
}
if (fread(&mem->memory[0x200], 1, rom_size, fp) != rom_size)
{
return (1);
}
return (0);
}
void memory_clear_screen(t_memory *mem)
{
ft_memset(mem->screen, 0, SCREEN_WIDTH * SCREEN_HEIGHT);
}
void memory_load_fontset(t_memory *mem)
{
ft_memcpy(&mem->memory[MEMORY_SIZE - FONTSET_SIZE], FONTSET, FONTSET_SIZE);
}
void memory_dump(t_memory *mem, const char *path)
{
FILE *fp = fopen(path, "wb");
if (!fp)
{
printf("Failed to open: %s\n", path);
exit(1);
}
fwrite(mem->memory, 1, MEMORY_SIZE, fp);
fclose(fp);
printf("Memory dumped to: %s\n", path);
}