summaryrefslogtreecommitdiff
path: root/gen.rb
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2018-05-25 22:20:52 -0400
committerPaul Duncan <pabs@pablotron.org>2018-05-25 22:20:52 -0400
commitcc9b49d62faff2ffb11d6e32c96b53a1d2b221c1 (patch)
tree9a52c86a03fae4dc4690445c7c60c194d9f03bdb /gen.rb
downloadgb-c-cc9b49d62faff2ffb11d6e32c96b53a1d2b221c1.tar.bz2
gb-c-cc9b49d62faff2ffb11d6e32c96b53a1d2b221c1.zip
initial commit
Diffstat (limited to 'gen.rb')
-rw-r--r--gen.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/gen.rb b/gen.rb
new file mode 100644
index 0000000..4b02320
--- /dev/null
+++ b/gen.rb
@@ -0,0 +1,57 @@
+#!/usr/bin/env ruby
+
+require 'yaml'
+require 'erb'
+
+# load template data
+DATA = YAML.load_file(File.join(__dir__, 'ops.yaml'))
+
+switches = DATA['ops'].reduce([]) do |r, set|
+ prefix = (set.first == 'cb') ? 0xCB00 : 0x0000
+
+ set.last.reduce(r) do |r, op|
+ # op hex string
+ hex = (prefix + op['hex']).to_s(16).upcase.rjust(4, '0')
+
+ r << case op['op']
+ when 'XX'
+ [
+ "case 0x%s: /* op: %s, cat: %s */",
+ " invalid(ctx, old_pc);",
+ " break;",
+ ].join("\n") % [
+ hex,
+ op['id'],
+ op['cat'],
+ ]
+ else
+ # time expr
+ time_expr = case op['time']
+ when Numeric
+ 'ctx->cpu.clock += %d;' % [op['time']]
+ when Hash
+ 'ctx->cpu.clock += %d; // t: %d, f: %d' % %w{f t f}.map { |k|
+ op['time'][k]
+ }
+ else
+ raise "unknown op time: #{hex}: #{op['time']}"
+ end
+
+ [
+ "case 0x%s: /* op: \"%s\", cat: \"%s\" */",
+ " %s", # action_expr
+ " %s", # time_expr
+ " break;"
+ ].join("\n") % [
+ hex,
+ op['id'],
+ op['cat'],
+ op['code'] || 'not_implemented(ctx, old_pc, op);',
+ time_expr,
+ ]
+ end
+ end
+end.join("\n")
+
+t = ERB.new(DATA['templates']['main'])
+puts t.run(binding)