Skip to content

Commit

Permalink
add the ability to add after instruction hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
infiton committed Jul 13, 2022
1 parent 4e85a43 commit d8f7d7f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
inherit_gem:
rubocop-shopify: rubocop.yml
1 change: 1 addition & 0 deletions ext/ruby6502/fake6502.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
void reset6502();
void step6502();
void exec6502(uint32_t tickcount);
void hookexternal(void *funcptr);

uint16_t getPC();
uint8_t getSP();
Expand Down
35 changes: 35 additions & 0 deletions ext/ruby6502/ruby6502.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "fake6502.h"

static VALUE mRuby6502;
uint8_t has_hooks = 0;

static VALUE program_counter(VALUE self)
{
Expand Down Expand Up @@ -73,6 +74,33 @@ void write6502(uint16_t address, uint8_t value)
rb_ary_store(memArray, address, rbValue);
}

static VALUE set_hooks(VALUE self)
{
has_hooks = 1;
return Qtrue;
}

static VALUE unset_hooks(VALUE self)
{
has_hooks = 0;
return Qtrue;
}

static VALUE get_has_hooks(VALUE self)
{
if (has_hooks) {
return Qtrue;
} else {
return Qfalse;
}
}

void execute_hooks() {
if (has_hooks) {
rb_funcall(mRuby6502, rb_intern("execute_hooks"), 0);
}
}

void Init_ruby6502()
{
mRuby6502 = rb_define_module("Ruby6502");
Expand All @@ -83,7 +111,14 @@ void Init_ruby6502()
rb_define_singleton_method(mRuby6502, "_y_register", y_register, 0);
rb_define_singleton_method(mRuby6502, "_status_flags", status_flags, 0);
rb_define_singleton_method(mRuby6502, "instruction_count", instruction_count, 0);

rb_define_singleton_method(mRuby6502, "set_hooks", set_hooks, 0);
rb_define_singleton_method(mRuby6502, "unset_hooks", unset_hooks, 0);
rb_define_singleton_method(mRuby6502, "hooks?", get_has_hooks, 0);

rb_define_singleton_method(mRuby6502, "reset", reset, 0);
rb_define_singleton_method(mRuby6502, "step", step, 0);
rb_define_singleton_method(mRuby6502, "exec", exec, 1);

hookexternal(execute_hooks);
}
16 changes: 12 additions & 4 deletions lib/ruby6502.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module Ruby6502
MEMORY = [0] * 256 * 256
HOOKS = []

def self.load(bytearray, location: 0)
byte_size = bytearray.size
Expand All @@ -18,11 +19,18 @@ def self.load(bytearray, location: 0)
end

def self.read(location:, bytes:)
if location >= MEMORY.size
raise "#{format("%04x", location)} is outside bounds"
end
raise "#{format("%04x", location)} is outside bounds" if location >= MEMORY.size

MEMORY[location...location + bytes]
end

def self.execute_hooks
HOOKS.each(&:call)
end

return MEMORY[location ... location + bytes]
def self.register_hook(&hook)
set_hooks unless hooks?
HOOKS << hook
end

def self.program_counter
Expand Down
2 changes: 1 addition & 1 deletion ruby6502.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require File.expand_path("../lib/ruby6502/version", __FILE__)
require File.expand_path("lib/ruby6502/version", __dir__)

Ruby6502::GEMSPEC = Gem::Specification.new do |s|
s.name = "ruby6502"
Expand Down

0 comments on commit d8f7d7f

Please sign in to comment.