From b40d6edb627d0e1b69898ca28a6a090b94a8e8a5 Mon Sep 17 00:00:00 2001 From: sacckey Date: Sun, 9 Feb 2025 15:55:45 +0900 Subject: [PATCH 1/4] Add EmulatorHeadless --- lib/rubyboy/emulator_headless.rb | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lib/rubyboy/emulator_headless.rb diff --git a/lib/rubyboy/emulator_headless.rb b/lib/rubyboy/emulator_headless.rb new file mode 100644 index 0000000..910ebd2 --- /dev/null +++ b/lib/rubyboy/emulator_headless.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +require_relative 'apu' +require_relative 'bus' +require_relative 'cpu' +require_relative 'ppu' +require_relative 'rom' +require_relative 'ram' +require_relative 'timer' +require_relative 'joypad' +require_relative 'interrupt' +require_relative 'cartridge/factory' + +module Rubyboy + class EmulatorHeadless + def initialize(rom_path) + rom_data = File.open(rom_path, 'r') { _1.read.bytes } + rom = Rom.new(rom_data) + ram = Ram.new + mbc = Cartridge::Factory.create(rom, ram) + interrupt = Interrupt.new + @ppu = Ppu.new(interrupt) + @timer = Timer.new(interrupt) + joypad = Joypad.new(interrupt) + @apu = Apu.new + bus = Bus.new(@ppu, rom, ram, mbc, @timer, interrupt, joypad, @apu) + @cpu = Cpu.new(bus, interrupt) + end + + def step + loop do + cycles = @cpu.exec + @timer.step(cycles) + @apu.step(cycles) + break if @ppu.step(cycles) + end + end + end +end From 6e35fd68866fe106ab22db8e9546552f19ff84c9 Mon Sep 17 00:00:00 2001 From: sacckey Date: Sun, 9 Feb 2025 15:57:12 +0900 Subject: [PATCH 2/4] Update bench --- exe/rubyboy-bench | 2 +- lib/bench.rb | 24 +++++++++++++----------- lib/stackprof.rb | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 lib/stackprof.rb diff --git a/exe/rubyboy-bench b/exe/rubyboy-bench index 1c907ca..cfdc36b 100755 --- a/exe/rubyboy-bench +++ b/exe/rubyboy-bench @@ -40,4 +40,4 @@ else puts 'YJIT is not available in this environment.' end -Rubyboy::Bench.new.bench(**options) +Rubyboy::Bench.new.run(**options) diff --git a/lib/bench.rb b/lib/bench.rb index 36e4984..1e0ec65 100644 --- a/lib/bench.rb +++ b/lib/bench.rb @@ -1,22 +1,24 @@ # frozen_string_literal: true -require 'stackprof' -require_relative 'rubyboy' +require_relative 'rubyboy/emulator_headless' module Rubyboy class Bench - def stackprof - StackProf.run(mode: :cpu, out: 'stackprof-cpu-myapp.dump', raw: true) do - Rubyboy::Emulator.new('lib/roms/tobu.gb').bench - end - end - - def bench(count: 3, frames: 1500, rom_path: 'lib/roms/tobu.gb') + def run(count: 3, frames: 1500, rom_path: 'lib/roms/tobu.gb') time_sum = 0 + count.times do |i| - time = Rubyboy::Emulator.new(rom_path).bench(frames) - time_sum += time + emulator = Rubyboy::EmulatorHeadless.new(rom_path) + frame_count = 0 + start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) + while frame_count < frames + emulator.step + frame_count += 1 + end + time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) - start_time puts "#{i + 1}: #{time / 1_000_000_000.0} sec" + + time_sum += time end puts "FPS: #{frames * count * 1_000_000_000.0 / time_sum}" diff --git a/lib/stackprof.rb b/lib/stackprof.rb new file mode 100644 index 0000000..c0dbc48 --- /dev/null +++ b/lib/stackprof.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'stackprof' +require_relative 'rubyboy/emulator' + +module Rubyboy + class Stackprof + def run + StackProf.run(mode: :cpu, out: 'stackprof-cpu-myapp.dump', raw: true) do + Rubyboy::Emulator.new('lib/roms/tobu.gb').bench + end + end + end +end From dc65851db7ac78ebc75693acce96fddcc85efcc3 Mon Sep 17 00:00:00 2001 From: sacckey Date: Sun, 9 Feb 2025 16:18:06 +0900 Subject: [PATCH 3/4] Bump ruby.wasm version to 2.7.1 --- Gemfile | 4 ++-- Gemfile.lock | 10 +++++----- docs/worker.js | 2 +- exe/rubyboy-wasm | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index 583a46e..2854700 100644 --- a/Gemfile +++ b/Gemfile @@ -15,6 +15,6 @@ group :development, :test do end group :wasm do - gem 'js', '2.7.0' - gem 'ruby_wasm', '2.7.0' + gem 'js', '2.7.1' + gem 'ruby_wasm', '2.7.1' end diff --git a/Gemfile.lock b/Gemfile.lock index 7b700e9..77618f7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -12,7 +12,7 @@ GEM diff-lcs (1.5.0) ffi (1.16.3) heap-profiler (0.7.0) - js (2.7.0) + js (2.7.1) json (2.6.3) language_server-protocol (3.17.0.3) parallel (1.23.0) @@ -52,8 +52,8 @@ GEM rubocop-ast (1.29.0) parser (>= 3.2.1.0) ruby-progressbar (1.13.0) - ruby_wasm (2.7.0-x86_64-darwin) - ruby_wasm (2.7.0-x86_64-linux) + ruby_wasm (2.7.1-x86_64-darwin) + ruby_wasm (2.7.1-x86_64-linux) stackprof (0.2.25) unicode-display_width (2.5.0) @@ -63,11 +63,11 @@ PLATFORMS DEPENDENCIES heap-profiler (~> 0.7.0) - js (= 2.7.0) + js (= 2.7.1) rake (~> 13.0) rspec (~> 3.12) rubocop (~> 1.57) - ruby_wasm (= 2.7.0) + ruby_wasm (= 2.7.1) rubyboy! stackprof (~> 0.2.25) diff --git a/docs/worker.js b/docs/worker.js index 7e15f48..3c526a7 100644 --- a/docs/worker.js +++ b/docs/worker.js @@ -1,4 +1,4 @@ -import { DefaultRubyVM } from 'https://cdn.jsdelivr.net/npm/@ruby/wasm-wasi@2.7.0/dist/browser/+esm'; +import { DefaultRubyVM } from 'https://cdn.jsdelivr.net/npm/@ruby/wasm-wasi@2.7.1/dist/browser/+esm'; import { File } from 'https://cdn.jsdelivr.net/npm/@bjorn3/browser_wasi_shim@0.3.0/+esm'; const DIRECTION_KEY_MASKS = { diff --git a/exe/rubyboy-wasm b/exe/rubyboy-wasm index ee05fd7..d128598 100755 --- a/exe/rubyboy-wasm +++ b/exe/rubyboy-wasm @@ -3,7 +3,7 @@ case ARGV[0] when 'build' - command = %w[build --ruby-version 3.3 -o ./docs/ruby-js.wasm] + command = %w[build --ruby-version 3.4 -o ./docs/ruby-js.wasm] when 'pack' command = %w[pack ./docs/ruby-js.wasm --dir ./lib::/lib -o ./docs/rubyboy.wasm] else From b01c727f119f72cf3a243e000d044df3d6c813be Mon Sep 17 00:00:00 2001 From: sacckey Date: Sun, 9 Feb 2025 16:25:50 +0900 Subject: [PATCH 4/4] Bump version to 1.5.0 --- CHANGELOG.md | 5 ++++- Gemfile.lock | 2 +- lib/rubyboy/version.rb | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6079d8d..d11e243 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ -## [Unreleased] +## [1.5.0] - 2025-02-09 + +- Add EmulatorHeadless and use it for bench +- Bump ruby.wasm version to 2.7.1 ## [1.4.2] - 2025-01-11 diff --git a/Gemfile.lock b/Gemfile.lock index 77618f7..292be9c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rubyboy (1.4.2) + rubyboy (1.5.0) ffi (~> 1.16, >= 1.16.3) GEM diff --git a/lib/rubyboy/version.rb b/lib/rubyboy/version.rb index 5a28a86..796188f 100644 --- a/lib/rubyboy/version.rb +++ b/lib/rubyboy/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Rubyboy - VERSION = '1.4.2' + VERSION = '1.5.0' end