#!/usr/bin/env ruby require 'yaml' require 'erb' # load template data DATA = YAML.load_file(File.join(__dir__, 'ops.yaml')) switches = Hash.new { |h, k| h[k] = [] } DATA['ops'].each do |set_id, ops| switches[set_id] = ops.select { |op| op['op'] != 'PREFIX' }.map { |op| # op hex string hex = op['hex'].to_s(16).upcase.rjust(2, '0') case op['op'] when 'XX' [ "case 0x%s: /* op: %s, cat: %s */", " cpu_set_state(ctx, old_pc, GB_CPU_STATE_INVALID);", " break;", ].join("\n") % [ hex, op['id'], op['cat'], ] else # pc_expr pc_expr = if op['pc'] 'cpu_ww(ctx, RW_PC, old_pc + %d);' % [op['len']] else '' end # time expr time_expr = case op['time'] when Numeric 'clock = %d;' % [op['time']] when Hash '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", # pc_expr " %s", # time_expr " break;" ].join("\n") % [ hex, op['id'], op['cat'], op['code'] || 'cpu_set_state(ctx, old_pc, GB_CPU_STATE_NOT_IMPLEMENTED);', pc_expr, time_expr, ] end }.join("\n") end t = ERB.new(DATA['templates']['main']) puts t.run(binding)