-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.h
100 lines (88 loc) · 2.28 KB
/
chip8.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h> //for random seed
#include "raylib.h"
#define screenW 64
#define screenH 32
#define MEM_SIZE 4096
#define MEM_PROGRAM_START 0x200
#define FONTS_ADDR 0x50
#define FONTS_SIZE 5*16
#define OPCODESPERFRAME 10
#define PC chip8->pc
#define SP chip8->sp
#define CURRENTOPCODE (chip8->mem[PC] << 8 | chip8->mem[PC+1])
#define VX chip8->v[opcode->x]
#define VY chip8->v[opcode->y]
#define VF chip8->v[15]
int fontSize = 12;
// buffer pintado
unsigned char buffer[64][32];
const unsigned int keys[]=
{
KEY_X,
KEY_ONE, //1
KEY_TWO, //2
KEY_THREE, //3
KEY_Q,
KEY_W,
KEY_E,
KEY_A,
KEY_S,
KEY_D,
KEY_Z,
KEY_C,
KEY_FOUR, //...
KEY_R,
KEY_F,
KEY_V
};
// *************************************************
// Internal Font data 8x5 pixeles
// *************************************************
const unsigned char fonts [16][5] =
{
{0xF0, 0x90, 0x90, 0x90, 0xF0},// 0
{0x20, 0x60, 0x20, 0x20, 0x70},// 1
{0xF0, 0x10, 0xF0, 0x80, 0xF0},// 2
{0xF0, 0x10, 0xF0, 0x10, 0xF0},// 3
{0x90, 0x90, 0xF0, 0x10, 0x10},// 4
{0xF0, 0x80, 0xF0, 0x10, 0xF0},// 5
{0xF0, 0x80, 0xF0, 0x90, 0xF0},// 6
{0xF0, 0x10, 0x20, 0x40, 0x40},// 7
{0xF0, 0x90, 0xF0, 0x90, 0xF0},// 8
{0xF0, 0x90, 0xF0, 0x10, 0xF0},// 9
{0xF0, 0x90, 0xF0, 0x90, 0x90},// A
{0xE0, 0x90, 0xE0, 0x90, 0xE0},// B
{0xF0, 0x80, 0x80, 0x80, 0xF0},// C
{0xE0, 0x90, 0x90, 0x90, 0xE0},// D
{0xF0, 0x80, 0xF0, 0x80, 0xF0},// E
{0xF0, 0x80, 0xF0, 0x80, 0x80} // F
};
// Maquina a 500-600 Hz. Hay que limitar la velocidad a unos 10 opcodes por frame
struct chip8_t
{
unsigned char mem[MEM_SIZE]; //memoria
unsigned short pc; //program counter
unsigned char v[16]; //registros generales
unsigned short I; //registro I
unsigned char delayTimer; //temporizador
unsigned char soundTimer; //temporizador audio
unsigned short stack[16]; //stack
unsigned short sp; //stack pointer
};
struct opcode_t
{
unsigned short instruction;
unsigned char x;
unsigned char y;
unsigned char n;
unsigned char nn;
unsigned short nnn;
};
void initChip8(struct chip8_t *chip8);
void loadRom(struct chip8_t *chip8, const char* filename);
void getOpcode(struct chip8_t *chip8, struct opcode_t *opcode);
void decodeOpcode(struct chip8_t* chip8, struct opcode_t *opcode);
void getInput();