<commit_before>require 'mimey/cpu/operations'

module Mimey
  # This class represents the Game Boy CPU, a modified Z80.
  class CPU
    # 8 bit registers: A, F, B, C, D, E, H, L
    attr_accessor :a, :f, :b, :c, :d, :e, :h, :l
    # 16 bit PC register (Program counter)
    attr_accessor :pc
    # 16 bit SP register (Stack pointer)
    attr_accessor :sp
    # CPU Clock
    attr_accessor :clock
    # MMU
    attr_accessor :mmu

    # Default register values
    DEFAULTS = {
      a: 0x00,
      f: 0x00,
      b: 0x00,
      c: 0x00,
      d: 0x00,
      e: 0x00,
      h: 0x00,
      l: 0x00,
      pc: 0x0000,
      sp: 0x0000
    }

    # Bitmasks for Z, N, H and C flags from the F register
    Z_FLAG = 0b1000_0000
    N_FLAG = 0b0100_0000
    H_FLAG = 0b0010_0000
    C_FLAG = 0b0001_0000

    # Creates a new CPU and initializes with the provided options
    # if no options where give all the registers will be initialized
    # with deafult values
    def initialize(options = {})
      DEFAULTS.merge(options).each do |k, v|
        instance_variable_set("@#{k}", v)
      end

      @clock = 0
      @mmu = MMU.new
    end

    # Executes next instruction
    def step
      operation = OPERATIONS[next_byte]
      method(operation).call
    end

    # Loads a program
    def load_with(*args)
      @mmu.load_rom(*args)
      self<commit_msg>Added "nop-mode" to CPU<commit_after>require 'mimey/cpu/operations'

module Mimey
  # This class represents the Game Boy CPU, a modified Z80.
  class CPU
    # 8 bit registers: A, F, B, C, D, E, H, L
    attr_accessor :a, :f, :b, :c, :d, :e, :h, :l
    # 16 bit PC register (Program counter)
    attr_accessor :pc
    # 16 bit SP register (Stack pointer)
    attr_accessor :sp
    # CPU Clock
    attr_accessor :clock
    # MMU
    attr_accessor :mmu
    # nop-mode
    attr_accessor :nop_mode

    # Default register values
    DEFAULTS = {
      a: 0x00,
      f: 0x00,
      b: 0x00,
      c: 0x00,
      d: 0x00,
      e: 0x00,
      h: 0x00,
      l: 0x00,
      pc: 0x0000,
      sp: 0x0000
    }

    # Bitmasks for Z, N, H and C flags from the F register
    Z_FLAG = 0b1000_0000
    N_FLAG = 0b0100_0000
    H_FLAG = 0b0010_0000
    C_FLAG = 0b0001_0000

    # Creates a new CPU and initializes with the provided options
    # if no options where give all the registers will be initialized
    # with deafult values
    def initialize(options = {})
      DEFAULTS.merge(options).each do |k, v|
        instance_variable_set("@#{k}", v)
      end

      @clock = 0
      @mmu = MMU.new
    end

    # Executes next instruction
    def step
      operation = OPERATIONS[next_byte]
      if not self.respond_to?(operation) and !!nop_mode
        puts "warning! we don't know how to #{operation}"
        operation = "nop"
      end
      method(operation).call
    end

    # Loads a program
    def load_with(*args)
      @mmu.load_rom(*args)
      self
{'fraction_non_alphanumeric': 0.0905039424065821, 'fraction_numerical': 0.053479602331162154, 'mean_word_length': 2.1717391304347826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '867436', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1026, 'n_words': 335}