Skip to content

Commit

Permalink
added load_memory_tests for CI workflow and independent testing and r…
Browse files Browse the repository at this point in the history
…efactored tests
  • Loading branch information
dronavallipranav committed Aug 18, 2023
1 parent 43be115 commit 24f5a39
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 70 deletions.
9 changes: 6 additions & 3 deletions include/mmu.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
#define EMPTY_NO_IO_SIZE 0x34
#define INTERNAL_HIGH_RAM_SIZE 0x7F

typedef struct MMU {
typedef struct MMU
{
Cartridge *cartridge;
uint8_t *cart_memory;
uint8_t video_ram[VIDEO_RAM_SIZE];
Expand All @@ -62,10 +63,12 @@ typedef struct MMU {

void init_mmu(MMU *mmu);

void load_memory(MMU* mmu, const char* filename);
void load_memory(MMU *mmu, const char *filename);

void load_memory_tests(MMU *mmu);

uint8_t read_byte(MMU *mmu, uint16_t addr);

void write_byte(MMU *mmu, uint16_t addr, uint8_t value);

#endif
#endif
12 changes: 12 additions & 0 deletions src/mmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ void load_memory(MMU *mmu, const char *filename)
fclose(file);
}

void load_memory_tests(MMU *mmu)
{
mmu->cart_memory = (uint8_t *)malloc(CART_SIZE);
if (mmu->cart_memory == NULL)
{
fprintf(stderr, "Failed to allocate memory for ROM\n");
exit(1);
}
memset(mmu->cart_memory, 0, CART_SIZE);
}

uint8_t read_byte(MMU *mmu, uint16_t addr)
{
if (addr >= CART_START && addr <= CART_END)
Expand Down Expand Up @@ -94,6 +105,7 @@ uint8_t read_byte(MMU *mmu, uint16_t addr)

void write_byte(MMU *mmu, uint16_t addr, uint8_t value)
{
printf("reached\n");
if (addr >= CART_START && addr <= CART_END)
{
mmu->cart_memory[addr] = value;
Expand Down
139 changes: 79 additions & 60 deletions tests/test_arithmetic.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
#include "../Instructions/arithmetic.h"
#include "cpu.h"

void test_add8() {
void test_add8()
{

Z80_State cpu;
initCPU(&cpu);
cpu.AF.A = 10;
cpu.BC.B = 5;
add8(&cpu, cpu.BC.B, false);
CU_ASSERT_EQUAL(cpu.AF.A, 15);

}

void test_add16() {
void test_add16()
{
Z80_State cpu;
initCPU(&cpu);
cpu.HL_pair = 0x0F00;
Expand All @@ -25,51 +26,52 @@ void test_add16() {
CU_ASSERT_TRUE(cpu.AF.flags.C == 0);
}

void test_addImm() {

void test_addImm()
{

Z80_State cpu;
initCPU(&cpu);
cpu.AF.A = 10;
uint16_t imm = 15;
addImm(&cpu, imm);
CU_ASSERT_EQUAL(cpu.AF.A, 25);

}

void test_sub8() {
void test_sub8()
{

Z80_State cpu;
initCPU(&cpu);
cpu.AF.A = 10;
cpu.BC.B = 5;
sub8(&cpu, cpu.BC.B, false);
CU_ASSERT_EQUAL(cpu.AF.A, 5);

}

void test_sub16() {

void test_sub16()
{

Z80_State cpu;
initCPU(&cpu);
cpu.AF.A = 30;
cpu.BC_pair = 20;
sub16(&cpu, cpu.BC_pair);
CU_ASSERT_EQUAL(cpu.AF.A, 10);

}

void test_subImm() {

void test_subImm()
{

Z80_State cpu;
initCPU(&cpu);
cpu.AF.A = 20;
uint16_t imm = 15;
subImm(&cpu, imm);
CU_ASSERT_EQUAL(cpu.AF.A, 5);

}

void test_and8() {
void test_and8()
{

Z80_State cpu;
initCPU(&cpu);
Expand All @@ -81,11 +83,11 @@ void test_and8() {
CU_ASSERT_TRUE(cpu.AF.flags.N == 0);
CU_ASSERT_TRUE(cpu.AF.flags.H == 1);
CU_ASSERT_TRUE(cpu.AF.flags.C == 0);

}

void test_or8() {

void test_or8()
{

Z80_State cpu;
initCPU(&cpu);
cpu.AF.A = 0xF0;
Expand All @@ -97,11 +99,11 @@ void test_or8() {
CU_ASSERT_TRUE(cpu.AF.flags.N == 0);
CU_ASSERT_TRUE(cpu.AF.flags.H == 0);
CU_ASSERT_TRUE(cpu.AF.flags.C == 0);

}

void test_xor8() {

void test_xor8()
{

Z80_State cpu;
initCPU(&cpu);
cpu.AF.A = 0xAA;
Expand All @@ -110,108 +112,125 @@ void test_xor8() {
CU_ASSERT_EQUAL(cpu.AF.A, 0x55);
}

void test_cp8() {
void test_cp8()
{

Z80_State cpu;
initCPU(&cpu);
cpu.AF.A = 10;
cpu.BC.B = 5;
cp8(&cpu, cpu.BC.B);
CU_ASSERT_EQUAL(cpu.AF.flags.Z, 0);

}

void test_inc8() {

void test_inc8()
{

Z80_State cpu;
initCPU(&cpu);
cpu.AF.A = 9;
inc8(&cpu, cpu.setA, cpu.AF.A);
CU_ASSERT_EQUAL(cpu.AF.A, 10);

}

void test_dec8() {

void test_dec8()
{

Z80_State cpu;
initCPU(&cpu);
cpu.AF.A = 10;
dec8(&cpu, cpu.setA, cpu.AF.A);
CU_ASSERT_EQUAL(cpu.AF.A, 9);

}

void test_inc16() {
void test_inc16()
{
Z80_State cpu;
initCPU(&cpu);
cpu.HL_pair = 0x0F00;
add16(&cpu, cpu.setHL, cpu.getHL, 1);
CU_ASSERT_EQUAL(cpu.HL_pair, 0x0F01);
}

void test_dec16() {
void test_dec16()
{
Z80_State cpu;
initCPU(&cpu);
cpu.HL_pair = 0x0F00;
add16(&cpu, cpu.setHL, cpu.getHL, -1);
CU_ASSERT_EQUAL(cpu.HL_pair, 0x0EFF);
}

int add_arithmetic_tests(CU_pSuite suite) {
if (NULL == CU_add_test(suite, "Test Add8", test_add8)) {
return -1;
int add_arithmetic_tests(CU_pSuite suite)
{
if (NULL == CU_add_test(suite, "Test Add8", test_add8))
{
return -1;
}

if (NULL == CU_add_test(suite, "Test Add16", test_add16)) {
return -1;
if (NULL == CU_add_test(suite, "Test Add16", test_add16))
{
return -1;
}

if (NULL == CU_add_test(suite, "Test Add16", test_addImm)) {
return -1;
if (NULL == CU_add_test(suite, "Test Add16", test_addImm))
{
return -1;
}

if (NULL == CU_add_test(suite, "Test Add8", test_sub8)) {
return -1;
if (NULL == CU_add_test(suite, "Test Add8", test_sub8))
{
return -1;
}

if (NULL == CU_add_test(suite, "Test Add16", test_sub16)) {
return -1;
if (NULL == CU_add_test(suite, "Test Add16", test_sub16))
{
return -1;
}

if (NULL == CU_add_test(suite, "Test Add16", test_subImm)) {
return -1;
if (NULL == CU_add_test(suite, "Test Add16", test_subImm))
{
return -1;
}

if (NULL == CU_add_test(suite, "Test And8", test_and8)) {
return -1;
if (NULL == CU_add_test(suite, "Test And8", test_and8))
{
return -1;
}

if (NULL == CU_add_test(suite, "Test or8", test_or8)) {
return -1;
if (NULL == CU_add_test(suite, "Test or8", test_or8))
{
return -1;
}

if (NULL == CU_add_test(suite, "Test xor8", test_xor8)) {
return -1;
if (NULL == CU_add_test(suite, "Test xor8", test_xor8))
{
return -1;
}

if (NULL == CU_add_test(suite, "Test cp8", test_cp8)) {
return -1;
if (NULL == CU_add_test(suite, "Test cp8", test_cp8))
{
return -1;
}

if (NULL == CU_add_test(suite, "Test inc8", test_inc8)) {
return -1;
if (NULL == CU_add_test(suite, "Test inc8", test_inc8))
{
return -1;
}

if (NULL == CU_add_test(suite, "Test dec8", test_dec8)) {
return -1;
if (NULL == CU_add_test(suite, "Test dec8", test_dec8))
{
return -1;
}

if (NULL == CU_add_test(suite, "Test inc16", test_inc16)) {
return -1;
if (NULL == CU_add_test(suite, "Test inc16", test_inc16))
{
return -1;
}

if (NULL == CU_add_test(suite, "Test dec16", test_dec16)) {
return -1;
if (NULL == CU_add_test(suite, "Test dec16", test_dec16))
{
return -1;
}
return 0;
}
Loading

0 comments on commit 24f5a39

Please sign in to comment.