Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates #2

Merged
merged 7 commits into from
Jan 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ find_package(SDL_image REQUIRED)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-DDEBUG)
endif()

add_executable(${EXEC_NAME}
src/sys/sdl/input.h
src/sys/sdl/serial.c
Expand Down Expand Up @@ -77,6 +81,19 @@ add_executable(${EXEC_NAME}
src/menu/sdl/dialog.h
src/menu/sdl/util.h
src/menu/menu.h

src/debug/debug.c
src/debug/debug.h
src/debug/disasm.c
src/debug/disasm.h
src/debug/event.c
src/debug/event.h
src/debug/record.c
src/debug/record.h
src/debug/watch.c
src/debug/watch.h
src/debug/break.c
src/debug/break.h

src/util/last_rom.h
src/util/card.c
Expand Down
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.3
- Fixed speed_factor not being loaded from config
- Fixed ".." in rom browser being displayed below certain directories

0.2
- Fixed glitches in Alfred's Adventure, Chuck Rock
- Fixed RTC progressing day one hour late
Expand Down
8 changes: 8 additions & 0 deletions src/core/cpu.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#include "timers.h"
#include "sys/sys.h"

#ifdef DEBUG
#include "debug/record.h"
#endif

cpu_t cpu;

Expand Down Expand Up @@ -49,6 +52,11 @@ void cpu_reset() {

u8 cpu_step() {
ints_handle();

#ifdef DEBUG
record_cpu_cycle();
#endif

cpu.op = mem_read_byte(PC++);
return op_exec();
}
Expand Down
Empty file modified src/core/defines.h
100755 → 100644
Empty file.
10 changes: 9 additions & 1 deletion src/core/ints.c
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
#include "ints.h"

#include <assert.h>
#include <stdio.h>

#include "cpu.h"
#include "defines.h"
#include "defines.h"
#include "hw.h"

static inline void exec_int(u8 i) {
hw_step(2);

cpu.irq &= ~(1 << i);
cpu.ime = IME_OFF;

SP -= 2;
mem_write_word(SP, PC);

hw_step(2);

PC = 0x40 + (i<<3);

hw_step(1);
}

void ints_handle() {
Expand Down
32 changes: 30 additions & 2 deletions src/core/joy.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include "defines.h"
#include "joy.h"

#ifdef DEBUG
#include "debug/event.h"
#endif // DEBUG

#define SELECT_DIRECTION_BIT 0x10
#define SELECT_ACTION_BIT 0x20

Expand All @@ -16,6 +20,15 @@ void joy_reset() {
void joy_set_button(u8 button, u8 state) {
u8 old_state = joy.state & button ? JOY_STATE_RELEASED : JOY_STATE_PRESSED;
if(old_state != state) {
#ifdef DEBUG
event_t event;
event.type = EVENT_JOY_INPUT;
event.joy.button = button;
event.joy.state = state;

debug_event(event);
#endif // DEBUG

if(state) {
joy.state |= button;
}
Expand All @@ -33,12 +46,27 @@ void joy_select_col(u8 flag) {
else if((~flag) & SELECT_DIRECTION_BIT) {
joy.col = 0;
}
else {
joy.col = 0xFF;
}
}

u8 joy_read() {
#ifdef DEBUG
if ((joy.col == 0 ? joy.state & 0x0F : joy.state >> 4) != 0xF) {
event_t event;
event.type = EVENT_JOY_NOTICED;
event.joy.state = joy.state;

debug_event(event);
}
#endif // DEBUG

if(joy.col == 0)
return joy.state & 0x0F;
return SELECT_ACTION_BIT | (joy.state & 0x0F);
else if (joy.col == 1)
return SELECT_DIRECTION_BIT | (joy.state >> 4);
else
return joy.state >> 4;
return SELECT_ACTION_BIT | SELECT_DIRECTION_BIT;
}

3 changes: 0 additions & 3 deletions src/core/joy.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
#define JOY_STATE_PRESSED 0x00
#define JOY_STATE_RELEASED 0x01


typedef struct {
u8 state;
u8 col;
} joy_t;


extern joy_t joy;


void joy_reset();

void joy_set_button(u8 button, u8 state);
Expand Down
45 changes: 36 additions & 9 deletions src/core/mem.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include "cpu.h"
#include "mbc.h"

#ifdef DEBUG
#include "debug/watch.h"
#endif // DEBUG

ram_t ram;
card_t card;

Expand Down Expand Up @@ -39,6 +43,20 @@ void mem_reset() {
}

u8 mem_read_byte(u16 adr) {
#ifdef DEBUG
{
static int recurse = 1;

if (recurse) {
recurse = 0;
watch_event_mem_r(adr);
}
else {
recurse = 1;
}
}
#endif // DEBUG

switch(adr >> 12) {
case 0x0: case 0x1: case 0x2: case 0x3:
return card.rombanks[0][adr];
Expand Down Expand Up @@ -98,31 +116,35 @@ u16 mem_read_word(u16 adr) {
}

void mem_write_byte(u16 adr, u8 val) {
#ifdef DEBUG
u8 watch_old_val = mem_read_byte(adr);
#endif // DEBUG

switch(adr >> 12) {
case 0x0: case 0x1: case 0x2: case 0x3:
case 0x4: case 0x5: case 0x6: case 0x7:
mbc_lower_write(adr, val);
return;
break;
case 0x8: case 0x9:
if((lcd.stat & 0x03) != 0x03 || !(lcd.c & 0x80)) {
lcd_vram_write(adr, val);
}
else {
write_locked_mem(adr, val);
}
return;
break;
case 0xA: case 0xB:
mbc_upper_write(adr, val);
return;
break;
case 0xC:
ram.rambanks[0][adr - 0xC000] = (mbc.type == 2) ? (val & 0x0F) : val;
return;
break;
case 0xD:
ram.rambank[adr - 0xD000] = (mbc.type == 2) ? (val & 0x0F) : val;
return;
break;
case 0xE:
mem_write_byte(adr - 0x2000, val);
return;
break;

case 0xF:
if(adr < 0xFE00) {
Expand All @@ -138,17 +160,22 @@ void mem_write_byte(u16 adr, u8 val) {
write_locked_mem(adr, val);
}
else if(adr >= 0xFF00 && adr < 0xFF80) { // IO Registers
return io_write(adr, val);
io_write(adr, val);
}
else if(adr >= 0xFF80 && adr < 0xFFFF) { // HiRAM
ram.hram[adr - 0xFF80] = val;
}
else {
cpu.ie = val & 0x1F;
}
return;
break;
default:
assert(0);
}
assert(0);

#ifdef DEBUG
watch_event_mem_w(adr, watch_old_val, mem_read_byte(adr));
#endif // DEBUG
}

void mem_write_word(u16 adr, u16 val) {
Expand Down
6 changes: 6 additions & 0 deletions src/core/moo.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#include "util/speed.h"
#include "sound.h"

#ifdef DEBUG
#include "debug/debug.h"
#endif

moo_t moo;

Expand Down Expand Up @@ -195,6 +198,9 @@ static void moo_cycle(int num) {
hw_step(1);
}
else {
#ifdef DEBUG
debug_step();
#endif // DEBUG
u8 mcs = cpu_step();
hw_step(mcs);
}
Expand Down
Empty file modified src/core/ops.c
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions src/core/rtc.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ void rtc_advance_seconds(time_t seconds) {
rtc_tick(0);
}
}

63 changes: 63 additions & 0 deletions src/debug/break.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "break.h"

#include <stdio.h>
#include <assert.h>

typedef struct {
breakpoint_t breakpoint;
int enabled;
} breakpoint_handle_t;

#define BREAKPOINT_BUFFER_SIZE 256

static breakpoint_handle_t breakpoints[BREAKPOINT_BUFFER_SIZE];
static int breakpoint_cursor = 0;
static int now = 0;

int break_enable(breakpoint_t breakpoint) {
assert(breakpoint_cursor < BREAKPOINT_BUFFER_SIZE);

breakpoint_handle_t handle;
handle.breakpoint = breakpoint;
handle.enabled = 1;

breakpoints[breakpoint_cursor] = handle;
breakpoints[breakpoint_cursor].enabled = 1;
breakpoint_cursor++;

switch(breakpoints[breakpoint_cursor-1].breakpoint.type) {
case BREAKPOINT_ADDRESS: printf("Breakpoint %i set on address %.4X\n", breakpoint_cursor - 1, breakpoint.address.pc); break;
case BREAKPOINT_EVENT: printf("Breakpoint %i set on event\n", breakpoint_cursor - 1); break;
}

return breakpoint_cursor - 1;
}

void break_disable(int id) {
assert(id >= 0 && id < BREAKPOINT_BUFFER_SIZE);
breakpoints[id].enabled = 0;
}

void break_handle_event(event_t event) {
int b;
for (b = 0; b < breakpoint_cursor; b++) {
if (breakpoints[b].breakpoint.type == BREAKPOINT_EVENT) {
if (event.type == EVENT_JOY_NOTICED) {
now = 1;
}
}
}
}

int break_now() {
int tmp = now;
if (now) {
now = 0;
}
return tmp;
}

void debug_break() { printf("BREAKING\n");
now = 1;
}

35 changes: 35 additions & 0 deletions src/debug/break.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef DEBUG_BREAK_H
#define DEBUG_BREAK_H

#include "core/defines.h"

#include "event.h"

typedef enum {
BREAKPOINT_ADDRESS,
BREAKPOINT_EVENT
} BREAKPOINT_TYPE;

typedef struct {
BREAKPOINT_TYPE type;

union {
// BREAK_ADDRESS
struct {
u16 pc;
} address;

// BREAK_EVENT
struct {
EVENT_TYPE type;
} event;
};
} breakpoint_t;

int break_enable(breakpoint_t breakpoint);
void break_disable(int id);
void break_handle_event(event_t event);
int break_now();
void debug_break();

#endif // DEBUG_BREAK_H
Loading