Skip to content

Commit

Permalink
Added extra memory regions and default value of memory
Browse files Browse the repository at this point in the history
  • Loading branch information
subratkatiyar committed May 1, 2024
1 parent 8b6e976 commit a15ad51
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 84 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(SOURCES
src/main.c
src/memory.c
src/cpu.c
src/INS_load.c
# Add other source files here as needed
)

Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ Trying to emulate gameboy for understanding how a CPU works in general
| Ex | | | | | | | | | | | | | | | | |
| Fx | | | | | | | | | | | | | | | | |

# Memory Regions Observartions.
0x0000 - 0x3FFF = Fixed from cartridge </br>
0x4000 - 0x7FFF = Switchable from cartridge </br>
0x8000 - 0x9FFF = VIDEO RAM ?? </br>
0xA000 - 0xBFFF = 0xFF </br>
0xC000 - 0xCFFF = Random </br>
0xD000 - 0xDFFF = Random </br>
0xE000 - 0xFDFF = Echo Ram 0xC000 to 0xDDFF </br>
0xFE00 - 0xFE9F = Random </br>
0xFEA0 - 0xFEFF = 0x00 </br>

0xFF00 - 0xFF2F = Constant. </br>
0xFF30 - 0xFF3F = Random. </br>
0xFF40 - 0xFF7F = Constant. </br>

0xFF80 - 0xFFFE = Random. </br>
0xFFFF = 0x00 </br>

## Important Links
- OPCodes: https://gbdev.io/gb-opcodes/optables/
10 changes: 10 additions & 0 deletions include/INS_load.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "memory.h"

void execute_load_1(GameboyMemory *memory, GameboyRegisters *registers);
void execute_load_2(GameboyMemory *memory, GameboyRegisters *registers);
void execute_load_3(GameboyMemory *memory, GameboyRegisters *registers);
void execute_load_4(GameboyMemory *memory, GameboyRegisters *registers);
void execute_load_5(GameboyMemory *memory, GameboyRegisters *registers);
void execute_load_6(GameboyMemory *memory, GameboyRegisters *registers);
void execute_load_7(GameboyMemory *memory, GameboyRegisters *registers);
void execute_load_8(GameboyMemory *memory, GameboyRegisters *registers);
4 changes: 4 additions & 0 deletions include/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#define VRAM_SIZE 0x2000 // Video RAM size (8KB)
#define ERAM_SIZE 0x2000 // External RAM size (8KB)
#define WRAM_SIZE 0x2000 // Working RAM size (8KB)
#define ECHO_RAM 0x1E00 // Echo Ram [New]
#define OAM_SIZE 0xA0 // Object Attribute Memory size (160 bytes)
#define NOT_USABLE 0x60 // Not Usable [New]
#define IO_SIZE 0x80 // I/O Registers size (128 bytes)
#define HRAM_SIZE 0x7F // High RAM size (127 bytes)

Expand All @@ -20,7 +22,9 @@ typedef struct {
uint8_t vram[VRAM_SIZE]; // Video RAM
uint8_t eram[ERAM_SIZE]; // External RAM (cartridge RAM)
uint8_t wram[WRAM_SIZE]; // Working RAM
uint8_t echoram[ECHO_RAM]; // Echo Ram [New]
uint8_t oam[OAM_SIZE]; // Object Attribute Memory
uint8_t notuse[NOT_USABLE]; // Not Usable [New]
uint8_t io[IO_SIZE]; // I/O Registers
uint8_t hram[HRAM_SIZE]; // High RAM
uint8_t ie; // Interrupt Enable Register
Expand Down
69 changes: 69 additions & 0 deletions src/INS_load.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <stdio.h>
#include "INS_load.h"

void execute_load_1(GameboyMemory *memory, GameboyRegisters *registers){
uint16_t valueLoc = registers->pc + 0x01;
uint16_t value = read_memory_16_le(memory, valueLoc);
printf("LOAD HL 0x%04X\n", value);
registers->hl = value;
registers->pc += 0x03;
}

void execute_load_2(GameboyMemory *memory, GameboyRegisters *registers){
uint16_t valueLoc = registers->pc + 0x01;
uint16_t value = read_memory(memory, valueLoc);
printf("LOAD C 0x%02X\n", value);
registers->c = value;
registers->pc += 0x02;
}

void execute_load_3(GameboyMemory *memory, GameboyRegisters *registers){
uint16_t valueLoc = registers->pc + 0x01;
uint16_t value = read_memory(memory, valueLoc);
printf("LOAD B 0x%02X\n", value);
registers->b = value;
registers->pc += 0x02;
}

void execute_load_4(GameboyMemory *memory, GameboyRegisters *registers){
uint8_t value = registers->a;
printf("LOAD *HL A\n");
write_memory(memory, registers->hl, value);
registers->hl -= 0x01;
registers->pc += 0x01;
}

void execute_load_5(GameboyMemory *memory, GameboyRegisters *registers){
printf("LOAD L C\n");
registers->l = registers->c;
registers->pc += 0x01;
}

void execute_load_6(GameboyMemory *memory, GameboyRegisters *registers){
uint16_t valueLoc = registers->pc + 0x01;
uint8_t value = read_memory(memory, valueLoc);
printf("LOAD A 0x%02X\n", value);
registers->a = value;
registers->pc += 0x02;
}

void execute_load_7(GameboyMemory *memory, GameboyRegisters *registers){
uint16_t valueLoc = registers->pc + 0x01;
uint8_t value = read_memory(memory, valueLoc);
printf("LOAD FF00+ 0x%02X, A\n", value);

uint16_t address = 0xFF00 + value;
write_memory(memory, address, registers->a);

registers->pc += 0x02;
}

void execute_load_8(GameboyMemory *memory, GameboyRegisters *registers){
uint16_t valueLoc = registers->pc + 0x01;
uint8_t value = read_memory(memory, valueLoc);
uint16_t address = 0xFF00 + value;
value = read_memory(memory, address);
printf("LOAD A, FF00+ 0x%02X, A\n", value);
registers->a = value;
registers->pc += 0x02;
}
70 changes: 19 additions & 51 deletions src/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,57 +71,7 @@ void execute_xor(GameboyRegisters *registers){
registers->pc += 0x01;
}

void execute_load_1(GameboyMemory *memory, GameboyRegisters *registers){
uint16_t valueLoc = registers->pc + 0x01;
uint16_t value = read_memory_16_le(memory, valueLoc);
printf("LOAD HL 0x%04X\n", value);
registers->hl = value;
registers->pc += 0x03;
}

void execute_load_2(GameboyMemory *memory, GameboyRegisters *registers){
uint16_t valueLoc = registers->pc + 0x01;
uint16_t value = read_memory(memory, valueLoc);
printf("LOAD C 0x%02X\n", value);
registers->c = value;
registers->pc += 0x02;
}

void execute_load_3(GameboyMemory *memory, GameboyRegisters *registers){
uint16_t valueLoc = registers->pc + 0x01;
uint16_t value = read_memory(memory, valueLoc);
printf("LOAD B 0x%02X\n", value);
registers->b = value;
registers->pc += 0x02;
}

void execute_load_4(GameboyMemory *memory, GameboyRegisters *registers){
uint8_t value = registers->a;
printf("LOAD *HL A\n");
write_memory(memory, registers->hl, value);
registers->hl -= 0x01;
registers->pc += 0x01;
}

void execute_load_5(GameboyMemory *memory, GameboyRegisters *registers){
printf("LOAD L C\n");
registers->l = registers->c;
registers->pc += 0x01;
}

void execute_load_6(GameboyMemory *memory, GameboyRegisters *registers){
uint16_t valueLoc = registers->pc + 0x01;
uint8_t value = read_memory(memory, valueLoc);
printf("LOAD A 0x%02X\n", value);
registers->a = value;
registers->pc += 0x02;
}

void execute_load_7(GameboyMemory *memory, GameboyRegisters *registers){
printf("LOAD L C\n");
registers->l = registers->c;
registers->pc += 0x01;
}

void execute_decrease_1(GameboyMemory *memory, GameboyRegisters *registers){
printf("DEC B\n");
Expand Down Expand Up @@ -198,6 +148,19 @@ void execute_disable_interrupts(GameboyRegisters *registers){
registers->pc += 0x01;
}

void execute_compare_1(GameboyMemory *memory, GameboyRegisters *registers){
// registers->pc += 0x01;
// int8_t n = (int8_t)read_memory(memory, registers->pc);
// registers->pc += 0x01;

// printf("CP A 0x%02X\n", n);
// if(registers->a == n)
// registers->f = 1;
// else
// registers->z = 0;

}

void execute_instruction(GameboyMemory *memory, GameboyRegisters *registers, uint8_t opcode){
switch(opcode){
case 0x00:
Expand Down Expand Up @@ -239,7 +202,12 @@ void execute_instruction(GameboyMemory *memory, GameboyRegisters *registers, uin
case 0xE0:
execute_load_7(memory, registers);
break;

case 0xF0:
execute_load_8(memory, registers);
break;
case 0xFE:
execute_compare_1(memory, registers);
break;
// case 0x69:
// execute_load_5(memory, registers);
// break;
Expand Down
Loading

0 comments on commit a15ad51

Please sign in to comment.