Skip to content

Commit

Permalink
cleanup and switch everything to English
Browse files Browse the repository at this point in the history
  • Loading branch information
dsocolobsky committed Oct 12, 2019
1 parent 8dff35e commit 1f09627
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 65 deletions.
15 changes: 5 additions & 10 deletions chp8.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,14 @@ void chp8_reset(struct emu_t* emu) {
chp8_clear_keypad(emu);
}



void chp8_destroy(struct emu_t* emu) {
free(emu);
}

bool chp8_load_from_file(struct emu_t* emu, const char* filename) {
FILE *rom;
rom = fopen(filename, "r");

FILE *rom = fopen(filename, "r");
if (!rom) {
perror("Error al leer archivo");
perror("Error opening the file");
return false;
}

Expand All @@ -74,18 +70,16 @@ bool chp8_load_from_file(struct emu_t* emu, const char* filename) {
emu->current_rom_size = file_size;
rewind(rom);

printf("El size del fichero es: %ld\n", file_size);
printf("File size is: %ld\n", file_size);

size_t bytes_read = fread(emu->memory + PROGSTART, sizeof(uint8_t), file_size, rom);

if (bytes_read != file_size) {
perror("Error leyendo bytes del archivo");
perror("Error reading from file");
return false;
}


fclose(rom);

return true;
}

Expand All @@ -101,6 +95,7 @@ bool chp8_singlestep(struct emu_t* emu) {
emu->pc += 2;
handle_instruction(emu, instr);

// Update delay timer
if (emu->DT > 0)
emu->DT--;

Expand Down
27 changes: 8 additions & 19 deletions chp8.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,25 @@
#define _CHP8_H
#include <stdbool.h>

/* chp8_init:
* Inicializa las estructuras requeridas para correr el emulador. Éstas
* pueden devolverse con `chp8_destroy(struct emu_t*)`.
*/
/* Initializes the emulator which then can be freed by using chp8_destroy */
struct emu_t* chp8_init();

/* chp8_reset:
* Reinicia el emulador modificando el contador de programa y los registros
* T y VF.
*/
/* Restarts the emulator to the initial state zeroing the required registers */
void chp8_reset(struct emu_t* emu);

/* Restarts the keypad to the initial state (all keys not pressed) */
void chp8_clear_keypad(struct emu_t* emu);

/* chp8_destroy:
* Devuelve los recursos adquiridos por chp8_init().
*/
/* Frees the allocated memory needed by the emulator */
void chp8_destroy(struct emu_t* emu);

/* chp8_load_from_file:
* Carga a la memoria del emulador los contenidos del archivo `filename`.
* Devuelve false en caso de error.
*/
/* Loads the emulator with the ROM specified in the path filename, returns false on error */
bool chp8_load_from_file(struct emu_t* emu, const char* filename);

/* Sets the status of a key in the keypad to 'status' which represents if it's pressed or not */
void chp8_setkey(struct emu_t* emu, uint8_t key, bool status);

/* chp8_singlestep:
* Emula una única instrucción si es posible, devuelve false si la ejecución
* terminó.
*/
/* Emulates a single instruction (if possible) and returns False if the execution ended */
bool chp8_singlestep(struct emu_t* emu);

#endif
34 changes: 14 additions & 20 deletions chp8_gui.h
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
#ifndef _CHP8_GUI_H
#define _CHP8_GUI_H
/* Acción a realizar sobre el emulador:
* - CONTINUE: Pasar el estado a RUNNING
* - STEP: Realizar single-stepping
* - PAUSE: Pasar el estado a PAUSED
* - NONE: No hacer nada */
/** Action to perform in the emulator:
* - CONTINUE: Set state to RUNNING
* - STEP: Single-step
* - PAUSE: Set state to PAUSED
* - NONE: Do nothing
**/
enum action { CONTINUE, STEP, PAUSE, RESET, NONE };

/* Estado del eumlador:
* - RUNNING: Indica que el emulador esta corriendo actualmente y puede ser
* pausado.
* - PAUSED: Indica que el emulador está pausado actualmente y puede
* despausarse o realizar single-stepping
/* State of the emulator:
* - RUNNING: Emulator is currently running and can be paused
* - PAUSED: Emulator is currently running and can be un-paused (or single-stepped)
*/
enum status { RUNNING, PAUSED };

/* chp8_debug_window:
* Muestra la ventana de registros y control de flujo del emulador. Devuelve
* la acción a ejecutar.
*/
/* Shows the register window and flow of the emulator. Returns the action to perform */
enum action chp8_debug_window(struct nk_context *ctx, struct emu_t* emu,
enum status status);

/* chp8_display_window:
* Muestra el display del emulador (texto).
*/
/* Shows the emulator's display (as text) CURRENTLY DISABLED */
void chp8_display_window(struct nk_context *ctx, struct emu_t* emu);

/* chp8_code_window:
* Muestra una versión desensamblada de las próximas instrucciones
*/
/* Show the ROM's disassembly and the current instruction */
void chp8_code_window(struct nk_context* ctx, struct emu_t* emu);

/* Shows the emulator's memory as raw bytes */
void chp8_memory_window(struct nk_context* ctx, struct emu_t* emu);

/* Shows the CHIP8 keypad, which can be interacted with */
void chp8_keypad_window(struct nk_context* ctx, struct emu_t* emu);

#endif
14 changes: 8 additions & 6 deletions defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#include <stdint.h>
#include <stdbool.h>

/* Size de la memoria (en bytes) */
/* Memory size (in bytes) */
#define MEMSIZE 4096
/* Los programas comienzan a partir de los 512B */
/* Programs start on 512B onwards */
#define PROGSTART 512

/* Cols and Rows for the Chip8 display */
#define DISPLAY_COLS 64
#define DISPLAY_ROWS 32

Expand All @@ -30,16 +31,17 @@
#define BIT_SET(X,N) (X & (1<<N))

typedef struct emu_t {
uint8_t *memory;
uint8_t *memory;
uint16_t pc;
uint16_t I;
uint8_t V[16];
uint8_t V[16];
uint16_t DT;
uint16_t ST;
uint8_t SP;
uint8_t SP;
uint16_t stack[48];
uint8_t display[DISPLAY_ROWS][DISPLAY_COLS];
uint8_t display[DISPLAY_ROWS][DISPLAY_COLS];
uint32_t current_rom_size;
bool keypad[16];
} emu_t;

#endif
25 changes: 15 additions & 10 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/* Bad on nuklear sdl_opengl3 example */
/** This file includes code from:
* nuklear/example/canvas.c
* nuklear/demo/sdl_opengl3/main.c
**/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
Expand Down Expand Up @@ -30,10 +34,10 @@

#include "defines.h"

#define WINDOW_WIDTH 1200
#define WINDOW_WIDTH 1200
#define WINDOW_HEIGHT 800

#define MAX_VERTEX_MEMORY 512 * 1024
#define MAX_VERTEX_MEMORY 512 * 1024
#define MAX_ELEMENT_MEMORY 128 * 1024

struct nk_canvas {
Expand Down Expand Up @@ -90,7 +94,6 @@ int main(int argc, char* argv[])
struct nk_canvas canvas;
int ticks = 0;


/* GUI */
struct nk_context *ctx;
struct nk_colorf bg;
Expand Down Expand Up @@ -181,21 +184,23 @@ int main(int argc, char* argv[])
/* Draw */

// Draw Canvas
canvas_begin(ctx, &canvas, NK_WINDOW_NO_INPUT, CANVAS_X, CANVAS_Y, CANVAS_WIDTH, CANVAS_HEIGHT, nk_rgb(32,32,32));
if (ticks == 0) {
for (int row = 0; row < DISPLAY_ROWS; row++) {
canvas_begin(ctx, &canvas, NK_WINDOW_NO_INPUT, CANVAS_X, CANVAS_Y,
CANVAS_WIDTH, CANVAS_HEIGHT, nk_rgb(32,32,32));
{
if (ticks == 0) {
for (int row = 0; row < DISPLAY_ROWS; row++)
for (int col = 0; col < DISPLAY_COLS; col++) {
if (!emu->display[row][col])
continue;

nk_fill_rect(canvas.painter, nk_rect(CANVAS_X+col*CANVAS_PX_SIZE,
CANVAS_Y+row*CANVAS_PX_SIZE,CANVAS_PX_SIZE,CANVAS_PX_SIZE), 0, nk_rgb(255, 0, 0));
CANVAS_Y+row*CANVAS_PX_SIZE,CANVAS_PX_SIZE,CANVAS_PX_SIZE),
0, nk_rgb(255, 0, 0));
}
}
canvas_end(ctx, &canvas);
}

canvas_end(ctx, &canvas);

switch (chp8_debug_window(ctx, emu, status)) {
case PAUSE: status = PAUSED; break;
case STEP: chp8_singlestep(emu); break;
Expand Down

0 comments on commit 1f09627

Please sign in to comment.