rubyboy 0.3.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rubyboy.rb CHANGED
@@ -1,23 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'benchmark'
4
- require_relative 'rubyboy/version'
3
+ require 'raylib'
5
4
  require_relative 'rubyboy/bus'
6
5
  require_relative 'rubyboy/cpu'
7
6
  require_relative 'rubyboy/ppu'
8
7
  require_relative 'rubyboy/rom'
9
8
  require_relative 'rubyboy/timer'
10
9
  require_relative 'rubyboy/lcd'
10
+ require_relative 'rubyboy/joypad'
11
+ require_relative 'rubyboy/interrupt'
11
12
 
12
13
  module Rubyboy
13
14
  class Console
14
- def initialize(rom_data)
15
+ include Raylib
16
+
17
+ def initialize(rom_path)
18
+ load_raylib
19
+ rom_data = File.open(rom_path, 'r') { _1.read.bytes }
15
20
  rom = Rom.new(rom_data)
16
21
  interrupt = Interrupt.new
17
22
  @ppu = Ppu.new(interrupt)
18
23
  @timer = Timer.new(interrupt)
19
- @bus = Bus.new(@ppu, rom, @timer, interrupt)
20
- @cpu = Cpu.new(@bus)
24
+ @joypad = Joypad.new(interrupt)
25
+ @bus = Bus.new(@ppu, rom, @timer, interrupt, @joypad)
26
+ @cpu = Cpu.new(@bus, interrupt)
21
27
  @lcd = Lcd.new
22
28
  end
23
29
 
@@ -25,7 +31,10 @@ module Rubyboy
25
31
  until @lcd.window_should_close?
26
32
  cycles = @cpu.exec
27
33
  @timer.step(cycles)
28
- draw if @ppu.step(cycles)
34
+ if @ppu.step(cycles)
35
+ draw
36
+ key_input_check
37
+ end
29
38
  end
30
39
  @lcd.close_window
31
40
  rescue StandardError => e
@@ -33,6 +42,8 @@ module Rubyboy
33
42
  raise e
34
43
  end
35
44
 
45
+ private
46
+
36
47
  def draw
37
48
  pixel_data = buffer_to_pixel_data(@ppu.buffer)
38
49
  @lcd.draw(pixel_data)
@@ -43,8 +54,29 @@ module Rubyboy
43
54
  [row, row, row]
44
55
  end.flatten.pack('C*')
45
56
  end
57
+
58
+ def key_input_check
59
+ direction = (IsKeyUp(KEY_D) && 1 || 0) | ((IsKeyUp(KEY_A) && 1 || 0) << 1) | ((IsKeyUp(KEY_W) && 1 || 0) << 2) | ((IsKeyUp(KEY_S) && 1 || 0) << 3)
60
+ action = (IsKeyUp(KEY_K) && 1 || 0) | ((IsKeyUp(KEY_J) && 1 || 0) << 1) | ((IsKeyUp(KEY_U) && 1 || 0) << 2) | ((IsKeyUp(KEY_I) && 1 || 0) << 3)
61
+ @joypad.direction_button(direction)
62
+ @joypad.action_button(action)
63
+ end
64
+
65
+ def load_raylib
66
+ shared_lib_path = "#{Gem::Specification.find_by_name('raylib-bindings').full_gem_path}/lib/"
67
+ case RUBY_PLATFORM
68
+ when /mswin|msys|mingw/ # Windows
69
+ Raylib.load_lib("#{shared_lib_path}libraylib.dll")
70
+ when /darwin/ # macOS
71
+ Raylib.load_lib("#{shared_lib_path}libraylib.dylib")
72
+ when /linux/ # Ubuntu Linux (x86_64 or aarch64)
73
+ arch = RUBY_PLATFORM.split('-')[0]
74
+ Raylib.load_lib(shared_lib_path + "libraylib.#{arch}.so")
75
+ else
76
+ raise "Unknown system: #{RUBY_PLATFORM}"
77
+ end
78
+
79
+ SetTraceLogLevel(LOG_ERROR)
80
+ end
46
81
  end
47
82
  end
48
-
49
- rom_data = File.open(File.expand_path('roms/cpu_instrs/cpu_instrs.gb', __dir__), 'r') { _1.read.bytes }
50
- Rubyboy::Console.new(rom_data).start
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyboy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sacckey
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-19 00:00:00.000000000 Z
11
+ date: 2023-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: raylib-bindings
@@ -26,7 +26,8 @@ dependencies:
26
26
  version: 0.5.7
27
27
  description:
28
28
  email:
29
- executables: []
29
+ executables:
30
+ - rubyboy
30
31
  extensions: []
31
32
  extra_rdoc_files: []
32
33
  files:
@@ -37,6 +38,7 @@ files:
37
38
  - LICENSE.txt
38
39
  - README.md
39
40
  - Rakefile
41
+ - exe/rubyboy
40
42
  - lib/opcodes.json
41
43
  - lib/roms/bgbtest.gb
42
44
  - lib/roms/cpu_instrs/cpu_instrs.gb
@@ -64,10 +66,22 @@ files:
64
66
  - lib/rubyboy/cartridge/nombc.rb
65
67
  - lib/rubyboy/cpu.rb
66
68
  - lib/rubyboy/interrupt.rb
69
+ - lib/rubyboy/joypad.rb
67
70
  - lib/rubyboy/lcd.rb
71
+ - lib/rubyboy/operand.rb
72
+ - lib/rubyboy/operand/direct16.rb
73
+ - lib/rubyboy/operand/direct8.rb
74
+ - lib/rubyboy/operand/hl_dec.rb
75
+ - lib/rubyboy/operand/hl_inc.rb
76
+ - lib/rubyboy/operand/immediate16.rb
77
+ - lib/rubyboy/operand/immediate8.rb
78
+ - lib/rubyboy/operand/indirect.rb
79
+ - lib/rubyboy/operand/register16.rb
80
+ - lib/rubyboy/operand/register8.rb
68
81
  - lib/rubyboy/ppu.rb
69
82
  - lib/rubyboy/ram.rb
70
83
  - lib/rubyboy/register.rb
84
+ - lib/rubyboy/registers.rb
71
85
  - lib/rubyboy/rom.rb
72
86
  - lib/rubyboy/timer.rb
73
87
  - lib/rubyboy/version.rb