diff options
author | Manuel Palenzuela <manuelpalenzuelamerino@gmail.com> | 2021-07-19 14:20:20 +0100 |
---|---|---|
committer | Manuel Palenzuela <manuelpalenzuelamerino@gmail.com> | 2021-07-19 14:20:20 +0100 |
commit | 95356a12e955ac8d42e8bb4632075bf13021e201 (patch) | |
tree | c97e38a0edf35a7edafdb3a08114902edb050676 | |
parent | Added Readme (diff) | |
download | CHIP8-Emulator-95356a12e955ac8d42e8bb4632075bf13021e201.tar.gz CHIP8-Emulator-95356a12e955ac8d42e8bb4632075bf13021e201.tar.bz2 CHIP8-Emulator-95356a12e955ac8d42e8bb4632075bf13021e201.zip |
Feature: Added basic rom loading
-rw-r--r-- | config.h | 6 | ||||
-rw-r--r-- | emulator.c | 29 | ||||
-rw-r--r-- | emulator.h | 17 | ||||
-rw-r--r-- | main.c | 37 | ||||
-rw-r--r-- | roms/stars.ch8 | bin | 0 -> 968 bytes |
5 files changed, 86 insertions, 3 deletions
diff --git a/config.h b/config.h new file mode 100644 index 0000000..c70f65b --- /dev/null +++ b/config.h @@ -0,0 +1,6 @@ +#ifndef _CONFIG_H_ +#define _CONFIG_H_ + +#define INSTRUCTIONS_PER_SECOND 700 + +#endif diff --git a/emulator.c b/emulator.c index e69de29..afc78b1 100644 --- a/emulator.c +++ b/emulator.c @@ -0,0 +1,29 @@ +#include <emulator.h> + +int emulator_load_rom(Emulator* emulator, char* rom_name) +{ + printf("load rom!: %s\n", rom_name); + FILE* rom = fopen(rom_name, "r"); + if(rom == NULL) + { + perror("no rom file!\n"); + return 1; + } + + struct stat st; + fstat(fileno(rom), &st); + + int bytes_read = fread(emulator->memory, 1, st.st_size, rom); + if(bytes_read != st.st_size) + { + perror("doesnt cuadrar\n"); + return 2; + } + + return 0; +} + +int emulator_tick(Emulator* emulator) +{ + return 0; +} diff --git a/emulator.h b/emulator.h index e69de29..edffa47 100644 --- a/emulator.h +++ b/emulator.h @@ -0,0 +1,17 @@ +#ifndef _EMULATOR_H_ +#define _EMULATOR_H_ + +#include <stdio.h> +#include <sys/stat.h> + +typedef struct +{ + uint8_t is_on; + uint16_t pc; + uint8_t memory[4096]; +} Emulator; + +int emulator_load_rom(Emulator* emulator, char* rom_name); +int emulator_tick(Emulator* emulator); + +#endif diff --git a/main.c b/main.c index 8f61750..114126f 100644 --- a/main.c +++ b/main.c @@ -1,12 +1,43 @@ #include <stdio.h> -#include "emulator.h" +#include <unistd.h> +#include <config.h> +#include <emulator.h> + +int main(int argc, char** argv); +void show_help(); int main(int argc, char** argv) { - (void) argc; - (void) argv; + if(argc < 2) + { + show_help(); + return 1; + } + + Emulator emulator; + + if(emulator_load_rom(&emulator, argv[1]) != 0) + return 2; printf("Hello brother!\n"); + /*for(int i = 0; i < 4096; ++i) + { + printf("%c ", emulator.memory[i]); + } + + putchar('\n');*/ + + while(emulator.is_on) + { + emulator_tick(&emulator); + usleep(1000000 / INSTRUCTIONS_PER_SECOND); + } + return 0; } + +void show_help() +{ + printf("BAD USAGE! -> ./chip8_emulator [ROM]\n"); +} diff --git a/roms/stars.ch8 b/roms/stars.ch8 new file mode 100644 index 0000000..712e83c --- /dev/null +++ b/roms/stars.ch8 Binary files differ |